]>
Commit | Line | Data |
---|---|---|
6d7d248e | 1 | # Torturing Bison. -*- Autotest -*- |
279cabb6 JD |
2 | # Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007 Free Software Foundation, |
3 | # Inc. | |
6d7d248e AD |
4 | |
5 | # This program is free software; you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation; either version 2, or (at your option) | |
8 | # any later version. | |
9 | ||
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | ||
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program; if not, write to the Free Software | |
0fb669f9 PE |
17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | # 02110-1301, USA. | |
6d7d248e AD |
19 | |
20 | AT_BANNER([[Torture Tests.]]) | |
21 | ||
22 | ||
49e794c5 | 23 | # AT_INCREASE_DATA_SIZE(SIZE) |
f79b4f5c | 24 | # --------------------------- |
49e794c5 PE |
25 | # Try to increase the data size to SIZE KiB if possible. |
26 | m4_define([AT_INCREASE_DATA_SIZE], | |
27 | [data_limit=`(ulimit -S -d) 2>/dev/null` | |
28 | case $data_limit in | |
29 | [[0-9]]*) | |
30 | if test "$data_limit" -lt $1; then | |
f79b4f5c | 31 | AT_CHECK([ulimit -S -d $1 || exit 77]) |
49e794c5 PE |
32 | ulimit -S -d $1 |
33 | fi | |
34 | esac]) | |
35 | ||
36 | ||
817e9f41 AD |
37 | ## ------------------------------------- ## |
38 | ## Creating a large artificial grammar. ## | |
39 | ## ------------------------------------- ## | |
40 | ||
41 | # AT_DATA_TRIANGULAR_GRAMMAR(FILE-NAME, SIZE) | |
42 | # ------------------------------------------- | |
43 | # Create FILE-NAME, containing a self checking parser for a huge | |
44 | # triangular grammar. | |
817e9f41 AD |
45 | m4_define([AT_DATA_TRIANGULAR_GRAMMAR], |
46 | [AT_DATA([[gengram.pl]], | |
47 | [[#! /usr/bin/perl -w | |
48 | ||
49 | use strict; | |
50 | my $max = $ARGV[0] || 10; | |
51 | ||
52 | print <<EOF; | |
9501dc6e | 53 | ]AT_DATA_GRAMMAR_PROLOGUE[ |
8f3596a6 AD |
54 | %error-verbose |
55 | %debug | |
817e9f41 AD |
56 | %{ |
57 | #include <stdio.h> | |
58 | #include <stdlib.h> | |
817e9f41 | 59 | |
817e9f41 AD |
60 | static int yylex (void); |
61 | static void yyerror (const char *msg); | |
62 | %} | |
63 | %union | |
64 | { | |
65 | int val; | |
66 | }; | |
67 | ||
68 | %token END "end" | |
69 | %type <val> exp input | |
70 | EOF | |
71 | ||
72 | for my $size (1 .. $max) | |
73 | { | |
e9955c83 | 74 | print "%token t$size $size \"$size\"\n"; |
817e9f41 AD |
75 | }; |
76 | ||
77 | print <<EOF; | |
78 | %% | |
79 | input: | |
66871a81 PE |
80 | exp { if (\@S|@1 != 0) abort (); \$\$ = \@S|@1; } |
81 | | input exp { if (\@S|@2 != \@S|@1 + 1) abort (); \$\$ = \@S|@2; } | |
817e9f41 AD |
82 | ; |
83 | ||
84 | exp: | |
85 | END | |
86 | { \$\$ = 0; } | |
87 | EOF | |
88 | ||
89 | for my $size (1 .. $max) | |
90 | { | |
91 | use Text::Wrap; | |
92 | print wrap ("| ", " ", | |
93 | (map { "\"$_\"" } (1 .. $size)), | |
94 | " END \n"), | |
95 | " { \$\$ = $size; }\n"; | |
96 | }; | |
97 | print ";\n"; | |
98 | ||
99 | print <<EOF; | |
100 | %% | |
101 | static int | |
102 | yylex (void) | |
103 | { | |
104 | static int inner = 1; | |
105 | static int outer = 0; | |
106 | if (outer > $max) | |
107 | return 0; | |
108 | else if (inner > outer) | |
109 | { | |
110 | inner = 1; | |
111 | ++outer; | |
112 | return END; | |
113 | } | |
23c5a174 | 114 | return inner++; |
817e9f41 AD |
115 | } |
116 | ||
117 | static void | |
118 | yyerror (const char *msg) | |
119 | { | |
120 | fprintf (stderr, "%s\\n", msg); | |
121 | } | |
122 | ||
123 | int | |
124 | main (void) | |
125 | { | |
126 | yydebug = !!getenv ("YYDEBUG"); | |
127 | return yyparse (); | |
128 | } | |
129 | EOF | |
130 | ]]) | |
131 | ||
132 | AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout]) | |
133 | mv stdout $1 | |
134 | ]) | |
135 | ||
136 | ||
137 | ## -------------- ## | |
138 | ## Big triangle. ## | |
139 | ## -------------- ## | |
140 | ||
817e9f41 AD |
141 | AT_SETUP([Big triangle]) |
142 | ||
355e7c1c AD |
143 | # I have been able to go up to 2000 on my machine. |
144 | # I tried 3000, a 29Mb grammar file, but then my system killed bison. | |
e9955c83 AD |
145 | # With 500 and the new parser, which consume far too much memory, |
146 | # it gets killed too. Of course the parser is to be cleaned. | |
147 | AT_DATA_TRIANGULAR_GRAMMAR([input.y], [200]) | |
b56471a6 | 148 | AT_CHECK([bison -v -o input.c input.y]) |
1154cced AD |
149 | AT_COMPILE([input]) |
150 | AT_PARSER_CHECK([./input]) | |
817e9f41 AD |
151 | |
152 | AT_CLEANUP | |
153 | ||
154 | ||
155 | ||
62a3e4f0 AD |
156 | # AT_DATA_HORIZONTAL_GRAMMAR(FILE-NAME, SIZE) |
157 | # ------------------------------------------- | |
158 | # Create FILE-NAME, containing a self checking parser for a huge | |
159 | # horizontal grammar. | |
62a3e4f0 AD |
160 | m4_define([AT_DATA_HORIZONTAL_GRAMMAR], |
161 | [AT_DATA([[gengram.pl]], | |
162 | [[#! /usr/bin/perl -w | |
163 | ||
164 | use strict; | |
165 | my $max = $ARGV[0] || 10; | |
166 | ||
167 | print <<EOF; | |
9501dc6e | 168 | ]AT_DATA_GRAMMAR_PROLOGUE[ |
8f3596a6 AD |
169 | %error-verbose |
170 | %debug | |
62a3e4f0 AD |
171 | %{ |
172 | #include <stdio.h> | |
173 | #include <stdlib.h> | |
62a3e4f0 | 174 | |
62a3e4f0 AD |
175 | static int yylex (void); |
176 | static void yyerror (const char *msg); | |
177 | %} | |
62a3e4f0 | 178 | |
8f3596a6 AD |
179 | %token |
180 | EOF | |
62a3e4f0 AD |
181 | for my $size (1 .. $max) |
182 | { | |
8f3596a6 | 183 | print " t$size $size \"$size\"\n"; |
62a3e4f0 AD |
184 | }; |
185 | ||
186 | print <<EOF; | |
8f3596a6 | 187 | |
62a3e4f0 AD |
188 | %% |
189 | EOF | |
190 | ||
191 | use Text::Wrap; | |
192 | ||
193 | wrap ("exp: ", " ", | |
194 | (map { "\"$_\"" } (1 .. $max)), ";"), | |
195 | "\n"; | |
196 | ||
197 | print <<EOF; | |
198 | %% | |
199 | static int | |
200 | yylex (void) | |
201 | { | |
202 | static int counter = 1; | |
cf806753 PE |
203 | if (counter <= $max) |
204 | return counter++; | |
205 | if (counter++ != $max + 1) | |
206 | abort (); | |
207 | return 0; | |
62a3e4f0 AD |
208 | } |
209 | ||
210 | static void | |
211 | yyerror (const char *msg) | |
212 | { | |
213 | fprintf (stderr, "%s\\n", msg); | |
214 | } | |
215 | ||
216 | int | |
217 | main (void) | |
218 | { | |
219 | yydebug = !!getenv ("YYDEBUG"); | |
220 | return yyparse (); | |
221 | } | |
222 | EOF | |
223 | ]]) | |
224 | ||
225 | AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout]) | |
226 | mv stdout $1 | |
227 | ]) | |
228 | ||
229 | ||
230 | ## ---------------- ## | |
231 | ## Big horizontal. ## | |
232 | ## ---------------- ## | |
233 | ||
234 | AT_SETUP([Big horizontal]) | |
235 | ||
236 | # I have been able to go up to 10000 on my machine, but I had to | |
237 | # increase the maximum stack size (* 100). It gave: | |
238 | # | |
239 | # input.y 263k | |
240 | # input.tab.c 1.3M | |
241 | # input 453k | |
242 | # | |
243 | # gengram.pl 10000 0.70s user 0.01s sys 99% cpu 0.711 total | |
244 | # bison input.y 730.56s user 0.53s sys 99% cpu 12:12.34 total | |
245 | # gcc -Wall input.tab.c -o input 5.81s user 0.20s sys 100% cpu 6.01 total | |
246 | # ./input 0.00s user 0.01s sys 108% cpu 0.01 total | |
247 | # | |
248 | AT_DATA_HORIZONTAL_GRAMMAR([input.y], [1000]) | |
49e794c5 PE |
249 | |
250 | # GNU m4 requires about 70 MiB for this test on a 32-bit host. | |
251 | # Ask for 200 MiB, which should be plenty even on a 64-bit host. | |
252 | AT_INCREASE_DATA_SIZE(204000) | |
253 | ||
b56471a6 | 254 | AT_CHECK([bison -v -o input.c input.y]) |
1154cced AD |
255 | AT_COMPILE([input]) |
256 | AT_PARSER_CHECK([./input]) | |
62a3e4f0 AD |
257 | |
258 | AT_CLEANUP | |
259 | ||
260 | ||
261 | ||
742e4900 | 262 | # AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR(FILE-NAME, SIZE) |
8f3596a6 | 263 | # -------------------------------------------------- |
39ceb25b | 264 | # Create FILE-NAME, containing a self checking parser for a grammar |
742e4900 JD |
265 | # requiring SIZE lookahead tokens. |
266 | m4_define([AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR], | |
39ceb25b AD |
267 | [AT_DATA([[gengram.pl]], |
268 | [[#! /usr/bin/perl -w | |
269 | ||
270 | use strict; | |
271 | use Text::Wrap; | |
272 | my $max = $ARGV[0] || 10; | |
273 | ||
274 | print <<EOF; | |
8f3596a6 AD |
275 | %error-verbose |
276 | %debug | |
39ceb25b | 277 | %{ |
8f3596a6 AD |
278 | # include <stdio.h> |
279 | # include <stdlib.h> | |
280 | # include <assert.h> | |
39ceb25b AD |
281 | |
282 | static int yylex (void); | |
283 | static void yyerror (const char *msg); | |
284 | %} | |
285 | %union | |
286 | { | |
287 | int val; | |
288 | }; | |
289 | ||
290 | %type <val> input exp | |
291 | %token token | |
292 | EOF | |
293 | ||
294 | ||
295 | wrap ("%type <val> ", | |
296 | " ", | |
e9955c83 | 297 | map { "n$_" } (1 .. $max)), |
39ceb25b AD |
298 | "\n"; |
299 | ||
8f3596a6 | 300 | print "%token\n"; |
39ceb25b AD |
301 | for my $count (1 .. $max) |
302 | { | |
8f3596a6 | 303 | print " t$count $count \"$count\"\n"; |
39ceb25b AD |
304 | }; |
305 | ||
306 | print <<EOF; | |
307 | %% | |
308 | input: | |
8f3596a6 AD |
309 | exp { assert (\@S|@1 == 1); \$\$ = \@S|@1; } |
310 | | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; } | |
39ceb25b AD |
311 | ; |
312 | ||
313 | exp: | |
8f3596a6 | 314 | n1 "1" { assert (\@S|@1 == 1); \@S|@\@S|@ = \@S|@1; } |
39ceb25b AD |
315 | EOF |
316 | ||
317 | for my $count (2 .. $max) | |
318 | { | |
8f3596a6 | 319 | print "| n$count \"$count\" { assert (\@S|@1 == $count); \@S|@\@S|@ = \@S|@1; }\n"; |
39ceb25b AD |
320 | }; |
321 | print ";\n"; | |
322 | ||
323 | for my $count (1 .. $max) | |
324 | { | |
e9955c83 | 325 | print "n$count: token { \$\$ = $count; };\n"; |
39ceb25b AD |
326 | }; |
327 | ||
328 | print <<EOF; | |
329 | %% | |
330 | static int | |
331 | yylex (void) | |
332 | { | |
333 | static int return_token = 1; | |
334 | static int counter = 1; | |
335 | if (counter > $max) | |
cf806753 PE |
336 | { |
337 | if (counter++ != $max + 1) | |
338 | abort (); | |
339 | return 0; | |
340 | } | |
39ceb25b AD |
341 | if (return_token) |
342 | { | |
343 | return_token = 0; | |
344 | return token; | |
345 | } | |
346 | return_token = 1; | |
347 | return counter++; | |
348 | } | |
349 | ||
350 | static void | |
351 | yyerror (const char *msg) | |
352 | { | |
353 | fprintf (stderr, "%s\\n", msg); | |
354 | } | |
355 | ||
356 | int | |
357 | main (void) | |
358 | { | |
359 | yydebug = !!getenv ("YYDEBUG"); | |
360 | return yyparse (); | |
361 | } | |
362 | EOF | |
363 | ]]) | |
364 | ||
365 | AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout]) | |
366 | mv stdout $1 | |
367 | ]) | |
368 | ||
369 | ||
8dd162d3 | 370 | ## ------------------------ ## |
7d596384 | 371 | ## Many lookahead tokens. ## |
8dd162d3 | 372 | ## ------------------------ ## |
39ceb25b | 373 | |
742e4900 | 374 | AT_SETUP([Many lookahead tokens]) |
39ceb25b | 375 | |
742e4900 | 376 | AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR([input.y], [1000]) |
49e794c5 PE |
377 | |
378 | # GNU m4 requires about 70 MiB for this test on a 32-bit host. | |
379 | # Ask for 200 MiB, which should be plenty even on a 64-bit host. | |
380 | AT_INCREASE_DATA_SIZE(204000) | |
381 | ||
b56471a6 | 382 | AT_CHECK([bison -v -o input.c input.y]) |
1154cced AD |
383 | AT_COMPILE([input]) |
384 | AT_PARSER_CHECK([./input]) | |
39ceb25b AD |
385 | |
386 | AT_CLEANUP | |
387 | ||
388 | ||
389 | ||
7d596384 JD |
390 | # AT_DATA_STACK_TORTURE(C-PROLOGUE, [BISON-DECLS]) |
391 | # ------------------------------------------------ | |
6d7d248e AD |
392 | # A parser specialized in torturing the stack size. |
393 | m4_define([AT_DATA_STACK_TORTURE], | |
394 | [# A grammar of parens growing the stack thanks to right recursion. | |
395 | # exp: | |
396 | AT_DATA([input.y], | |
397 | [[%{ | |
04098407 PE |
398 | #include <errno.h> |
399 | #include <limits.h> | |
6d7d248e AD |
400 | #include <stdio.h> |
401 | #include <stdlib.h> | |
6d7d248e AD |
402 | ]$1[ |
403 | static int yylex (void); | |
404 | static void yyerror (const char *msg); | |
6d7d248e | 405 | %} |
7d596384 | 406 | ]$2[ |
04d843a2 | 407 | %error-verbose |
6d7d248e AD |
408 | %debug |
409 | %token WAIT_FOR_EOF | |
410 | %% | |
411 | exp: WAIT_FOR_EOF exp | ; | |
412 | %% | |
413 | static void | |
414 | yyerror (const char *msg) | |
415 | { | |
416 | fprintf (stderr, "%s\n", msg); | |
6d7d248e AD |
417 | } |
418 | ||
6d7d248e AD |
419 | static int |
420 | yylex (void) | |
421 | { | |
cf806753 PE |
422 | if (yylval < 0) |
423 | abort (); | |
6d7d248e AD |
424 | if (yylval--) |
425 | return WAIT_FOR_EOF; | |
426 | else | |
427 | return EOF; | |
428 | } | |
429 | ||
430 | int | |
431 | main (int argc, const char **argv) | |
432 | { | |
04098407 | 433 | char *endp; |
7d596384 | 434 | YYSTYPE yylval_init; |
66871a81 PE |
435 | if (argc != 2) |
436 | abort (); | |
7d596384 | 437 | yylval_init = strtol (argv[1], &endp, 10); |
04098407 | 438 | if (! (argv[1] != endp |
7d596384 | 439 | && 0 <= yylval_init && yylval_init <= INT_MAX |
04098407 PE |
440 | && errno != ERANGE)) |
441 | abort (); | |
6d7d248e | 442 | yydebug = 1; |
7d596384 JD |
443 | { |
444 | int count; | |
445 | int status; | |
446 | ]m4_bmatch([$2], [%push-], | |
447 | [[ yypstate *yyps = yypstate_new (); | |
448 | ]])[ for (count = 0; count < 2; ++count) | |
449 | { | |
450 | int new_status; | |
451 | yylval = yylval_init; | |
452 | ]m4_bmatch([$2], [%push-], | |
453 | [[ new_status = yypull_parse (yyps); | |
454 | ]], | |
455 | [[ new_status = yyparse (); | |
456 | ]])[ if (count > 0 && new_status != status) | |
457 | abort (); | |
458 | status = new_status; | |
459 | } | |
460 | ]m4_bmatch([$2], [%push-], | |
461 | [[ yypstate_delete (yyps); | |
462 | ]])[ return status; | |
463 | } | |
6d7d248e AD |
464 | } |
465 | ]]) | |
b56471a6 | 466 | AT_CHECK([bison -o input.c input.y]) |
1154cced | 467 | AT_COMPILE([input]) |
6d7d248e AD |
468 | ]) |
469 | ||
470 | ||
471 | ## -------------------------------------- ## | |
472 | ## Exploding the Stack Size with Alloca. ## | |
473 | ## -------------------------------------- ## | |
474 | ||
475 | AT_SETUP([Exploding the Stack Size with Alloca]) | |
476 | ||
7d596384 | 477 | m4_pushdef([AT_USE_ALLOCA], [[ |
0d50976f | 478 | #if (defined __GNUC__ || defined __BUILTIN_VA_ARG_INCR \ |
62c4328e | 479 | || defined _AIX || defined _MSC_VER || defined _ALLOCA_H) |
577d7c33 PE |
480 | # define YYSTACK_USE_ALLOCA 1 |
481 | #endif | |
482 | ]]) | |
6d7d248e | 483 | |
7d596384 JD |
484 | AT_DATA_STACK_TORTURE([AT_USE_ALLOCA]) |
485 | ||
6d7d248e | 486 | # Below the limit of 200. |
e0ac9b4b JD |
487 | AT_PARSER_CHECK([./input 20], 0, [], [ignore], |
488 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
6d7d248e | 489 | # Two enlargements: 2 * 2 * 200. |
e0ac9b4b JD |
490 | AT_PARSER_CHECK([./input 900], 0, [], [ignore], |
491 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
6d7d248e AD |
492 | # Fails: beyond the limit of 10,000 (which we don't reach anyway since we |
493 | # multiply by two starting at 200 => 5120 is the last possible). | |
e0ac9b4b JD |
494 | AT_PARSER_CHECK([./input 10000], 2, [], [ignore], |
495 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
6d7d248e | 496 | |
78143faa JD |
497 | # The push parser can't use alloca since the stacks can't be locals. This test |
498 | # just helps guarantee we don't let the YYSTACK_USE_ALLOCA feature affect | |
499 | # push parsers. | |
7d596384 JD |
500 | AT_DATA_STACK_TORTURE([AT_USE_ALLOCA], |
501 | [[%push-pull-parser | |
502 | ]]) | |
e0ac9b4b JD |
503 | AT_PARSER_CHECK([./input 20], 0, [], [ignore], |
504 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
505 | AT_PARSER_CHECK([./input 900], 0, [], [ignore], | |
506 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
507 | AT_PARSER_CHECK([./input 10000], 2, [], [ignore], | |
508 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
7d596384 JD |
509 | |
510 | m4_popdef([AT_USE_ALLOCA]) | |
511 | ||
6d7d248e AD |
512 | AT_CLEANUP |
513 | ||
514 | ||
515 | ||
516 | ||
517 | ## -------------------------------------- ## | |
518 | ## Exploding the Stack Size with Malloc. ## | |
519 | ## -------------------------------------- ## | |
520 | ||
521 | AT_SETUP([Exploding the Stack Size with Malloc]) | |
522 | ||
7d596384 JD |
523 | m4_pushdef([AT_USE_ALLOCA], [[#define YYSTACK_USE_ALLOCA 0]]) |
524 | ||
525 | AT_DATA_STACK_TORTURE([AT_USE_ALLOCA]) | |
6d7d248e AD |
526 | |
527 | # Below the limit of 200. | |
e0ac9b4b JD |
528 | AT_PARSER_CHECK([./input 20], 0, [], [ignore], |
529 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
6d7d248e | 530 | # Two enlargements: 2 * 2 * 200. |
e0ac9b4b JD |
531 | AT_PARSER_CHECK([./input 900], 0, [], [ignore], |
532 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
6d7d248e AD |
533 | # Fails: beyond the limit of 10,000 (which we don't reach anyway since we |
534 | # multiply by two starting at 200 => 5120 is the possible). | |
e0ac9b4b JD |
535 | AT_PARSER_CHECK([./input 10000], 2, [], [ignore], |
536 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
6d7d248e | 537 | |
7d596384 JD |
538 | AT_DATA_STACK_TORTURE([AT_USE_ALLOCA], |
539 | [[%push-pull-parser | |
540 | ]]) | |
e0ac9b4b JD |
541 | AT_PARSER_CHECK([./input 20], 0, [], [ignore], |
542 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
543 | AT_PARSER_CHECK([./input 900], 0, [], [ignore], | |
544 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
545 | AT_PARSER_CHECK([./input 10000], 2, [], [ignore], | |
546 | [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]]) | |
7d596384 JD |
547 | |
548 | m4_popdef([AT_USE_ALLOCA]) | |
549 | ||
6d7d248e | 550 | AT_CLEANUP |