]>
Commit | Line | Data |
---|---|---|
d8178f6c | 1 | /* edtest.c -- Test for libbacktrace storage allocation stress handling |
fbd26352 | 2 | Copyright (C) 2017-2019 Free Software Foundation, Inc. |
d8178f6c | 3 | |
4 | Redistribution and use in source and binary forms, with or without | |
5 | modification, are permitted provided that the following conditions are | |
6 | met: | |
7 | ||
8 | (1) Redistributions of source code must retain the above copyright | |
9 | notice, this list of conditions and the following disclaimer. | |
10 | ||
11 | (2) Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in | |
13 | the documentation and/or other materials provided with the | |
14 | distribution. | |
15 | ||
16 | (3) The name of the author may not be used to | |
17 | endorse or promote products derived from this software without | |
18 | specific prior written permission. | |
19 | ||
20 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
21 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
23 | DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
24 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
28 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
29 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 | POSSIBILITY OF SUCH DAMAGE. */ | |
31 | ||
32 | #include "config.h" | |
33 | ||
34 | #include <assert.h> | |
35 | #include <stdio.h> | |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
38 | #include <sys/types.h> | |
39 | ||
40 | #include "backtrace.h" | |
41 | #include "backtrace-supported.h" | |
42 | #include "internal.h" | |
43 | ||
3be33b09 | 44 | #include "testlib.h" |
d8178f6c | 45 | |
46 | static int test1 (void) __attribute__ ((noinline, unused)); | |
47 | static int test1 (void) __attribute__ ((noinline, unused)); | |
48 | extern int f2 (int); | |
49 | extern int f3 (int, int); | |
50 | ||
51 | static int | |
52 | test1 (void) | |
53 | { | |
54 | /* Returning a value here and elsewhere avoids a tailcall which | |
55 | would mess up the backtrace. */ | |
56 | return f2 (__LINE__) + 1; | |
57 | } | |
58 | ||
d8178f6c | 59 | int |
60 | f3 (int f1line, int f2line) | |
61 | { | |
62 | struct info all[20]; | |
63 | struct bdata data; | |
64 | int f3line; | |
65 | int i; | |
66 | ||
67 | data.all = &all[0]; | |
68 | data.index = 0; | |
69 | data.max = 20; | |
70 | data.failed = 0; | |
71 | ||
72 | f3line = __LINE__ + 1; | |
73 | i = backtrace_full (state, 0, callback_one, error_callback_one, &data); | |
74 | ||
75 | if (i != 0) | |
76 | { | |
77 | fprintf (stderr, "test1: unexpected return value %d\n", i); | |
78 | data.failed = 1; | |
79 | } | |
80 | ||
81 | if (data.index < 3) | |
82 | { | |
83 | fprintf (stderr, | |
84 | "test1: not enough frames; got %zu, expected at least 3\n", | |
85 | data.index); | |
86 | data.failed = 1; | |
87 | } | |
88 | ||
89 | check ("test1", 0, all, f3line, "f3", "edtest.c", &data.failed); | |
90 | check ("test1", 1, all, f2line, "f2", "edtest2_build.c", &data.failed); | |
91 | check ("test1", 2, all, f1line, "test1", "edtest.c", &data.failed); | |
92 | ||
93 | printf ("%s: backtrace_full alloc stress\n", data.failed ? "FAIL" : "PASS"); | |
94 | ||
95 | if (data.failed) | |
96 | ++failures; | |
97 | ||
98 | return failures; | |
99 | } | |
100 | ||
d8178f6c | 101 | int |
102 | main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) | |
103 | { | |
104 | state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS, | |
105 | error_callback_create, NULL); | |
106 | ||
107 | // Grab the storage allocation lock prior to doing anything interesting. | |
108 | // The intent here is to insure that the backtrace_alloc code is forced | |
109 | // to always call mmap() for new memory as opposed to reusing previously | |
110 | // allocated memory from the free list. Doing things this way helps | |
111 | // simulate what you might see in a multithreaded program in which there | |
112 | // are racing calls to the allocator. | |
113 | struct backtrace_state *state_internal = | |
114 | (struct backtrace_state *) state; | |
115 | state_internal->lock_alloc = 1; | |
116 | ||
117 | // Kick off the test | |
118 | test1(); | |
119 | ||
120 | exit (failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS); | |
121 | } |