145
|
1 /* allocfail.c -- Test for libbacktrace library
|
|
2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
|
|
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 <assert.h>
|
|
33 #include <stdio.h>
|
|
34 #include <stdlib.h>
|
|
35 #include <string.h>
|
|
36 #include <unistd.h>
|
|
37
|
|
38 #include "filenames.h"
|
|
39
|
|
40 #include "backtrace.h"
|
|
41 #include "backtrace-supported.h"
|
|
42
|
|
43 #include "testlib.h"
|
|
44
|
|
45 extern uint64_t get_nr_allocs (void);
|
|
46 extern void set_fail_at_alloc (uint64_t);
|
|
47 extern int at_fail_alloc_p (void);
|
|
48
|
|
49 static int test1 (void) __attribute__ ((noinline, unused));
|
|
50 static int f2 (int) __attribute__ ((noinline));
|
|
51 static int f3 (int, int) __attribute__ ((noinline));
|
|
52
|
|
53 static unsigned callback_errors = 0;
|
|
54
|
|
55 static void
|
|
56 error_callback_full (void *vdata ATTRIBUTE_UNUSED,
|
|
57 const char *msg ATTRIBUTE_UNUSED,
|
|
58 int errnum ATTRIBUTE_UNUSED)
|
|
59 {
|
|
60 if (at_fail_alloc_p ())
|
|
61 {
|
|
62 set_fail_at_alloc (0);
|
|
63 return;
|
|
64 }
|
|
65
|
|
66 callback_errors++;
|
|
67 }
|
|
68
|
|
69 static int
|
|
70 callback_full (void *vdata ATTRIBUTE_UNUSED, uintptr_t pc ATTRIBUTE_UNUSED,
|
|
71 const char *filename ATTRIBUTE_UNUSED,
|
|
72 int lineno ATTRIBUTE_UNUSED,
|
|
73 const char *function ATTRIBUTE_UNUSED)
|
|
74 {
|
|
75
|
|
76 return 0;
|
|
77 }
|
|
78
|
|
79 static int
|
|
80 test1 (void)
|
|
81 {
|
|
82 return f2 (__LINE__) + 1;
|
|
83 }
|
|
84
|
|
85 static int
|
|
86 f2 (int f1line)
|
|
87 {
|
|
88 return f3 (f1line, __LINE__) + 2;
|
|
89 }
|
|
90
|
|
91 static int
|
|
92 f3 (int f1line ATTRIBUTE_UNUSED, int f2line ATTRIBUTE_UNUSED)
|
|
93 {
|
|
94 int i;
|
|
95
|
|
96 i = backtrace_full (state, 0, callback_full, error_callback_full, NULL);
|
|
97
|
|
98 if (i != 0)
|
|
99 {
|
|
100 fprintf (stderr, "test1: unexpected return value %d\n", i);
|
|
101 ++failures;
|
|
102 }
|
|
103
|
|
104 if (callback_errors)
|
|
105 ++failures;
|
|
106
|
|
107 return failures;
|
|
108 }
|
|
109
|
|
110 /* Run all the tests. */
|
|
111
|
|
112 int
|
|
113 main (int argc, char **argv)
|
|
114 {
|
|
115 uint64_t fail_at = 0;
|
|
116
|
|
117 if (argc == 2)
|
|
118 {
|
|
119 fail_at = atoi (argv[1]);
|
|
120 set_fail_at_alloc (fail_at);
|
|
121 }
|
|
122
|
|
123 state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
|
|
124 error_callback_full, NULL);
|
|
125 if (state == NULL)
|
|
126 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
127
|
|
128 #if BACKTRACE_SUPPORTED
|
|
129 test1 ();
|
|
130 #endif
|
|
131
|
|
132 if (argc == 1)
|
|
133 fprintf (stderr, "%llu\n", (long long unsigned) get_nr_allocs ());
|
|
134
|
|
135 exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
136 }
|