1 /* allocfail.c -- Test for libbacktrace library
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
8 (1) Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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
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.
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. */
38 #include "filenames.h"
40 #include "backtrace.h"
41 #include "backtrace-supported.h"
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);
49 static int test1 (void) __attribute__ ((noinline
, unused
));
50 static int f2 (int) __attribute__ ((noinline
));
51 static int f3 (int, int) __attribute__ ((noinline
));
53 static unsigned callback_errors
= 0;
56 error_callback_full (void *vdata ATTRIBUTE_UNUSED
,
57 const char *msg ATTRIBUTE_UNUSED
,
58 int errnum ATTRIBUTE_UNUSED
)
60 if (at_fail_alloc_p ())
62 set_fail_at_alloc (0);
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
)
82 return f2 (__LINE__
) + 1;
88 return f3 (f1line
, __LINE__
) + 2;
92 f3 (int f1line ATTRIBUTE_UNUSED
, int f2line ATTRIBUTE_UNUSED
)
96 i
= backtrace_full (state
, 0, callback_full
, error_callback_full
, NULL
);
100 fprintf (stderr
, "test1: unexpected return value %d\n", i
);
110 /* Run all the tests. */
113 main (int argc
, char **argv
)
115 uint64_t fail_at
= 0;
119 fail_at
= atoi (argv
[1]);
120 set_fail_at_alloc (fail_at
);
123 state
= backtrace_create_state (argv
[0], BACKTRACE_SUPPORTS_THREADS
,
124 error_callback_full
, NULL
);
126 exit (failures
? EXIT_FAILURE
: EXIT_SUCCESS
);
128 #if BACKTRACE_SUPPORTED
133 fprintf (stderr
, "%llu\n", (long long unsigned) get_nr_allocs ());
135 exit (failures
? EXIT_FAILURE
: EXIT_SUCCESS
);