1 # Torturing Bison. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007 Free Software Foundation,
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)
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.
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
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 AT_BANNER([[Torture Tests.]])
23 # AT_INCREASE_DATA_SIZE(SIZE)
24 # ---------------------------
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`
30 if test "$data_limit" -lt $1; then
31 AT_CHECK([ulimit -S -d $1 || exit 77])
37 ## ------------------------------------- ##
38 ## Creating a large artificial grammar. ##
39 ## ------------------------------------- ##
41 # AT_DATA_TRIANGULAR_GRAMMAR(FILE-NAME, SIZE)
42 # -------------------------------------------
43 # Create FILE-NAME, containing a self checking parser for a huge
45 m4_define([AT_DATA_TRIANGULAR_GRAMMAR],
46 [AT_DATA([[gengram.pl]],
50 my $max = $ARGV[0] || 10;
53 ]AT_DATA_GRAMMAR_PROLOGUE[
60 static int yylex (void);
61 static void yyerror (const char *msg);
72 for my $size (1 .. $max)
74 print "%token t$size $size \"$size\"\n";
80 exp { if (\@S|@1 != 0) abort (); \$\$ = \@S|@1; }
81 | input exp { if (\@S|@2 != \@S|@1 + 1) abort (); \$\$ = \@S|@2; }
89 for my $size (1 .. $max)
92 print wrap ("| ", " ",
93 (map { "\"$_\"" } (1 .. $size)),
95 " { \$\$ = $size; }\n";
104 static int inner = 1;
105 static int outer = 0;
108 else if (inner > outer)
118 yyerror (const char *msg)
120 fprintf (stderr, "%s\\n", msg);
126 yydebug = !!getenv ("YYDEBUG");
132 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
141 AT_SETUP([Big triangle])
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.
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])
148 AT_CHECK([bison -v -o input.c input.y])
150 AT_PARSER_CHECK([./input])
156 # AT_DATA_HORIZONTAL_GRAMMAR(FILE-NAME, SIZE)
157 # -------------------------------------------
158 # Create FILE-NAME, containing a self checking parser for a huge
159 # horizontal grammar.
160 m4_define([AT_DATA_HORIZONTAL_GRAMMAR],
161 [AT_DATA([[gengram.pl]],
162 [[#! /usr/bin/perl -w
165 my $max = $ARGV[0] || 10;
168 ]AT_DATA_GRAMMAR_PROLOGUE[
175 static int yylex (void);
176 static void yyerror (const char *msg);
181 for my $size (1 .. $max)
183 print " t$size $size \"$size\"\n";
194 (map { "\"$_\"" } (1 .. $max)), ";"),
202 static int counter = 1;
205 if (counter++ != $max + 1)
211 yyerror (const char *msg)
213 fprintf (stderr, "%s\\n", msg);
219 yydebug = !!getenv ("YYDEBUG");
225 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
230 ## ---------------- ##
231 ## Big horizontal. ##
232 ## ---------------- ##
234 AT_SETUP([Big horizontal])
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:
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
248 AT_DATA_HORIZONTAL_GRAMMAR([input.y], [1000])
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)
254 AT_CHECK([bison -v -o input.c input.y])
256 AT_PARSER_CHECK([./input])
262 # AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR(FILE-NAME, SIZE)
263 # --------------------------------------------------
264 # Create FILE-NAME, containing a self checking parser for a grammar
265 # requiring SIZE lookahead tokens.
266 m4_define([AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR],
267 [AT_DATA([[gengram.pl]],
268 [[#! /usr/bin/perl -w
272 my $max = $ARGV[0] || 10;
282 static int yylex (void);
283 static void yyerror (const char *msg);
290 %type <val> input exp
295 wrap ("%type <val> ",
297 map { "n$_" } (1 .. $max)),
301 for my $count (1 .. $max)
303 print " t$count $count \"$count\"\n";
309 exp { assert (\@S|@1 == 1); \$\$ = \@S|@1; }
310 | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
314 n1 "1" { assert (\@S|@1 == 1); \@S|@\@S|@ = \@S|@1; }
317 for my $count (2 .. $max)
319 print "| n$count \"$count\" { assert (\@S|@1 == $count); \@S|@\@S|@ = \@S|@1; }\n";
323 for my $count (1 .. $max)
325 print "n$count: token { \$\$ = $count; };\n";
333 static int return_token = 1;
334 static int counter = 1;
337 if (counter++ != $max + 1)
351 yyerror (const char *msg)
353 fprintf (stderr, "%s\\n", msg);
359 yydebug = !!getenv ("YYDEBUG");
365 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
370 ## ------------------------ ##
371 ## Many lookahead tokens. ##
372 ## ------------------------ ##
374 AT_SETUP([Many lookahead tokens])
376 AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR([input.y], [1000])
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)
382 AT_CHECK([bison -v -o input.c input.y])
384 AT_PARSER_CHECK([./input])
390 # AT_DATA_STACK_TORTURE(C-PROLOGUE, [BISON-DECLS])
391 # ------------------------------------------------
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.
403 static int yylex (void);
404 static void yyerror (const char *msg);
411 exp: WAIT_FOR_EOF exp | ;
414 yyerror (const char *msg)
416 fprintf (stderr, "%s\n", msg);
431 main (int argc, const char **argv)
437 yylval_init = strtol (argv[1], &endp, 10);
438 if (! (argv[1] != endp
439 && 0 <= yylval_init && yylval_init <= INT_MAX
446 ]m4_bmatch([$2], [%push-],
447 [[ yypstate *yyps = yypstate_new ();
448 ]])[ for (count = 0; count < 2; ++count)
451 yylval = yylval_init;
452 ]m4_bmatch([$2], [%push-],
453 [[ new_status = yypull_parse (yyps);
455 [[ new_status = yyparse ();
456 ]])[ if (count > 0 && new_status != status)
460 ]m4_bmatch([$2], [%push-],
461 [[ yypstate_delete (yyps);
466 AT_CHECK([bison -o input.c input.y])
471 ## -------------------------------------- ##
472 ## Exploding the Stack Size with Alloca. ##
473 ## -------------------------------------- ##
475 AT_SETUP([Exploding the Stack Size with Alloca])
477 m4_pushdef([AT_USE_ALLOCA], [[
478 #if (defined __GNUC__ || defined __BUILTIN_VA_ARG_INCR \
479 || defined _AIX || defined _MSC_VER || defined _ALLOCA_H)
480 # define YYSTACK_USE_ALLOCA 1
484 AT_DATA_STACK_TORTURE([AT_USE_ALLOCA])
486 # Below the limit of 200.
487 AT_PARSER_CHECK([./input 20], 0, [], [ignore],
488 [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]])
489 # Two enlargements: 2 * 2 * 200.
490 AT_PARSER_CHECK([./input 900], 0, [], [ignore],
491 [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]])
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).
494 AT_PARSER_CHECK([./input 10000], 2, [], [ignore],
495 [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]])
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
500 AT_DATA_STACK_TORTURE([AT_USE_ALLOCA],
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"]])
510 m4_popdef([AT_USE_ALLOCA])
517 ## -------------------------------------- ##
518 ## Exploding the Stack Size with Malloc. ##
519 ## -------------------------------------- ##
521 AT_SETUP([Exploding the Stack Size with Malloc])
523 m4_pushdef([AT_USE_ALLOCA], [[#define YYSTACK_USE_ALLOCA 0]])
525 AT_DATA_STACK_TORTURE([AT_USE_ALLOCA])
527 # Below the limit of 200.
528 AT_PARSER_CHECK([./input 20], 0, [], [ignore],
529 [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]])
530 # Two enlargements: 2 * 2 * 200.
531 AT_PARSER_CHECK([./input 900], 0, [], [ignore],
532 [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]])
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).
535 AT_PARSER_CHECK([./input 10000], 2, [], [ignore],
536 [[VALGRIND_OPTS="$VALGRIND_OPTS --log-fd=1"]])
538 AT_DATA_STACK_TORTURE([AT_USE_ALLOCA],
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"]])
548 m4_popdef([AT_USE_ALLOCA])