The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
edtest.c
Go to the documentation of this file.
1/* edtest.c -- Test for libbacktrace storage allocation stress handling
2 Copyright (C) 2017-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 "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
44#include "testlib.h"
45
46static int test1 (void) __attribute__ ((noinline, noclone, optnone, unused));
47extern int f2 (int);
48extern int f3 (int, int);
49
50static int
51test1 (void)
52{
53 /* Returning a value here and elsewhere avoids a tailcall which
54 would mess up the backtrace. */
55 return f2 (__LINE__) + 1;
56}
57
58int
59f3 (int f1line, int f2line)
60{
61 struct info all[20];
62 struct bdata data;
63 int f3line;
64 int i;
65
66 data.all = &all[0];
67 data.index = 0;
68 data.max = 20;
69 data.failed = 0;
70
71 f3line = __LINE__ + 1;
73
74 if (i != 0)
75 {
76 fprintf (stderr, "test1: unexpected return value %d\n", i);
77 data.failed = 1;
78 }
79
80 if (data.index < 3)
81 {
82 fprintf (stderr,
83 "test1: not enough frames; got %zu, expected at least 3\n",
84 data.index);
85 data.failed = 1;
86 }
87
88 check ("test1", 0, all, f3line, "f3", "edtest.c", &data.failed);
89 check ("test1", 1, all, f2line, "f2", "edtest2_build.c", &data.failed);
90 check ("test1", 2, all, f1line, "test1", "edtest.c", &data.failed);
91
92 printf ("%s: backtrace_full alloc stress\n", data.failed ? "FAIL" : "PASS");
93
94 if (data.failed)
95 ++failures;
96
97 return failures;
98}
99
100int
102{
105
106 // Grab the storage allocation lock prior to doing anything interesting.
107 // The intent here is to insure that the backtrace_alloc code is forced
108 // to always call mmap() for new memory as opposed to reusing previously
109 // allocated memory from the free list. Doing things this way helps
110 // simulate what you might see in a multithreaded program in which there
111 // are racing calls to the allocator.
112 struct backtrace_state *state_internal =
113 (struct backtrace_state *) state;
114 state_internal->lock_alloc = 1;
115
116 // Kick off the test
117 test1();
118
119 exit (failures > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
120}
static int f2(int f1line)
Definition allocfail.c:86
#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
int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
Definition edtest.c:101
int f3(int f1line, int f2line)
Definition edtest.c:59
static int test1(void)
Definition edtest.c:46
#define ATTRIBUTE_UNUSED
Definition filenames.h:42
#define __attribute__(x)
Definition filenames.h:38
void check(const char *name, int index, const struct info *all, int want_lineno, const char *want_function, const char *want_file, int *failed)
Definition testlib.c:72
void * state
Definition testlib.c:46
int failures
Definition testlib.c:50
void error_callback_create(void *data ATTRIBUTE_UNUSED, const char *msg, int errnum)
Definition testlib.c:226
int callback_one(void *vdata, uintptr_t pc ATTRIBUTE_UNUSED, const char *filename, int lineno, const char *function)
Definition testlib.c:107
void error_callback_one(void *vdata, const char *msg, int errnum)
Definition testlib.c:144
struct info * all
Definition testlib.h:64
Definition testlib.h:54
Functions to help with cleanup.
static fr_slen_t data
Definition value.h:1340