Mercurial > gnulib
annotate tests/test-c-stack.c @ 40057:b06060465f09
maint: Run 'make update-copyright'
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 01 Jan 2019 00:25:11 +0100 |
parents | 10eb9086bea0 |
children |
rev | line source |
---|---|
10213 | 1 /* Test of c-stack module. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2002, 2004, 2006, 2008-2019 Free Software Foundation, Inc. |
10213 | 3 |
4 This program is free software: you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 3 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
10213 | 16 |
17 #include <config.h> | |
18 | |
19 #include "c-stack.h" | |
20 | |
21 #include "exitfail.h" | |
22 #include <stdio.h> | |
23 #if HAVE_SETRLIMIT | |
10346
1d6d83874993
test-c-stack: fix compilation failure on FreeBSD 5.0
Eric Blake <ebb9@byu.net>
parents:
10316
diff
changeset
|
24 /* At least FreeBSD 5.0 needs extra headers before <sys/resource.h> |
1d6d83874993
test-c-stack: fix compilation failure on FreeBSD 5.0
Eric Blake <ebb9@byu.net>
parents:
10316
diff
changeset
|
25 will compile. */ |
1d6d83874993
test-c-stack: fix compilation failure on FreeBSD 5.0
Eric Blake <ebb9@byu.net>
parents:
10316
diff
changeset
|
26 # include <sys/types.h> |
1d6d83874993
test-c-stack: fix compilation failure on FreeBSD 5.0
Eric Blake <ebb9@byu.net>
parents:
10316
diff
changeset
|
27 # include <sys/time.h> |
10213 | 28 # include <sys/resource.h> |
29 #endif | |
30 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
31 #include "macros.h" |
10213 | 32 |
10471
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
33 static volatile int * |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
34 recurse_1 (volatile int n, volatile int *p) |
10213 | 35 { |
10471
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
36 if (n >= 0) |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
37 *recurse_1 (n + 1, p) += n; |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
38 return p; |
10213 | 39 } |
40 | |
10471
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
41 static int |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
42 recurse (volatile int n) |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
43 { |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
44 int sum = 0; |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
45 return *recurse_1 (n, &sum); |
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
46 } |
10213 | 47 |
48 int | |
49 main (int argc, char **argv) | |
50 { | |
51 #if HAVE_SETRLIMIT && defined RLIMIT_STACK | |
10316 | 52 /* Before starting the endless recursion, try to be friendly to the |
53 user's machine. On some Linux 2.2.x systems, there is no stack | |
54 limit for user processes at all. We don't want to kill such | |
55 systems. */ | |
56 struct rlimit rl; | |
57 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */ | |
58 setrlimit (RLIMIT_STACK, &rl); | |
10213 | 59 #endif |
60 | |
14184
3cb76305b3e9
c-stack: assume stack overflow if SA_SIGINFO unsupported
Eric Blake <eblake@redhat.com>
parents:
14079
diff
changeset
|
61 if (c_stack_action (NULL) == 0) |
10275
4c08c1b6678e
c-stack: Expose false positives when not using libsigsegv.
Eric Blake <ebb9@byu.net>
parents:
10213
diff
changeset
|
62 { |
4c08c1b6678e
c-stack: Expose false positives when not using libsigsegv.
Eric Blake <ebb9@byu.net>
parents:
10213
diff
changeset
|
63 if (1 < argc) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10471
diff
changeset
|
64 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10471
diff
changeset
|
65 exit_failure = 77; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10471
diff
changeset
|
66 ++*argv[argc]; /* Intentionally dereference NULL. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10471
diff
changeset
|
67 } |
10471
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
68 return recurse (0); |
10275
4c08c1b6678e
c-stack: Expose false positives when not using libsigsegv.
Eric Blake <ebb9@byu.net>
parents:
10213
diff
changeset
|
69 } |
10471
f318366f3bc7
c-stack: avoid compiler optimizations when provoking overflow
Eric Blake <ebb9@byu.net>
parents:
10346
diff
changeset
|
70 fputs ("skipping test: ", stderr); |
10213 | 71 perror ("c_stack_action"); |
72 return 77; | |
73 } |