The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
allocfail.c
Go to the documentation of this file.
1/* allocfail.c -- Test for libbacktrace library
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
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
20THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30POSSIBILITY 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
45extern uint64_t get_nr_allocs (void);
46extern void set_fail_at_alloc (uint64_t);
47extern int at_fail_alloc_p (void);
48
49static int test1 (void) __attribute__ ((noinline, unused));
50static int f2 (int) __attribute__ ((noinline));
51static int f3 (int, int) __attribute__ ((noinline));
52
53static unsigned callback_errors = 0;
54
55static void
56error_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 {
63 return;
64 }
65
66 callback_errors++;
67}
68
69static int
71 const char *filename ATTRIBUTE_UNUSED,
72 int lineno ATTRIBUTE_UNUSED,
73 const char *function ATTRIBUTE_UNUSED)
74{
75
76 return 0;
77}
78
79static int
80test1 (void)
81{
82 return f2 (__LINE__) + 1;
83}
84
85static int
86f2 (int f1line)
87{
88 return f3 (f1line, __LINE__) + 2;
89}
90
91static int
92f3 (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
112int
113main (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
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}
log_entry msg
Definition acutest.h:794
static int f2(int f1line)
Definition allocfail.c:86
int at_fail_alloc_p(void)
int main(int argc, char **argv)
Definition allocfail.c:113
static int test1(void)
Definition allocfail.c:49
static int callback_full(void *vdata ATTRIBUTE_UNUSED, uintptr_t pc ATTRIBUTE_UNUSED, const char *filename ATTRIBUTE_UNUSED, int lineno ATTRIBUTE_UNUSED, const char *function ATTRIBUTE_UNUSED)
Definition allocfail.c:70
void set_fail_at_alloc(uint64_t)
uint64_t get_nr_allocs(void)
static int f3(int f1line ATTRIBUTE_UNUSED, int f2line ATTRIBUTE_UNUSED)
Definition allocfail.c:92
#define BACKTRACE_SUPPORTS_THREADS
int backtrace_full(struct backtrace_state *state, int skip, backtrace_full_callback callback, backtrace_error_callback error_callback, void *data)
struct backtrace_state * backtrace_create_state(const char *filename, int threaded, backtrace_error_callback error_callback, void *data)
Definition state.c:46
#define ATTRIBUTE_UNUSED
Definition filenames.h:42
#define __attribute__(x)
Definition filenames.h:38
void * state
Definition testlib.c:46
int failures
Definition testlib.c:50
Functions to help with cleanup.