]> git.ipfire.org Git - thirdparty/glibc.git/blame - debug/tst-backtrace5.c
Fix array overflow in backtrace on PowerPC (bug 25423)
[thirdparty/glibc.git] / debug / tst-backtrace5.c
CommitLineData
31d470ac
JM
1/* Test backtrace and backtrace_symbols for signal frames, where a
2 system call was interrupted by a signal.
688903eb 3 Copyright (C) 2011-2018 Free Software Foundation, Inc.
31d470ac
JM
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <execinfo.h>
21#include <search.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <signal.h>
27#include <unistd.h>
28
8c53a12c
JM
29#include "tst-backtrace.h"
30
d400dcac
AZ
31#ifndef SIGACTION_FLAGS
32# define SIGACTION_FLAGS 0
33#endif
34
31d470ac
JM
35/* The backtrace should include at least handle_signal, a signal
36 trampoline, read, 3 * fn, and do_test. */
37#define NUM_FUNCTIONS 7
38
31d470ac
JM
39void
40handle_signal (int signum)
41{
42 void *addresses[NUM_FUNCTIONS];
43 char **symbols;
44 int n;
45 int i;
46
47 /* Get the backtrace addresses. */
48 n = backtrace (addresses, sizeof (addresses) / sizeof (addresses[0]));
49 printf ("Obtained backtrace with %d functions\n", n);
50 /* Check that there are at least seven functions. */
51 if (n < NUM_FUNCTIONS)
52 {
53 FAIL ();
54 return;
55 }
56 /* Convert them to symbols. */
57 symbols = backtrace_symbols (addresses, n);
58 /* Check that symbols were obtained. */
59 if (symbols == NULL)
60 {
61 FAIL ();
62 return;
63 }
64 for (i = 0; i < n; ++i)
65 printf ("Function %d: %s\n", i, symbols[i]);
66 /* Check that the function names obtained are accurate. */
8c53a12c 67 if (!match (symbols[0], "handle_signal"))
31d470ac
JM
68 {
69 FAIL ();
70 return;
71 }
72 /* Do not check name for signal trampoline. */
73 i = 2;
8c53a12c 74 if (!match (symbols[i++], "read"))
31d470ac
JM
75 {
76 /* Perhaps symbols[2] is __kernel_vsyscall? */
8c53a12c 77 if (!match (symbols[i++], "read"))
31d470ac
JM
78 {
79 FAIL ();
80 return;
81 }
82 }
83 for (; i < n - 1; i++)
8c53a12c 84 if (!match (symbols[i], "fn"))
31d470ac
JM
85 {
86 FAIL ();
87 return;
88 }
89 /* Symbol names are not available for static functions, so we do not
90 check do_test. */
0478174d
AS
91
92 /* Check that backtrace does not return more than what fits in the array
93 (bug 25423). */
94 for (int j = 0; j < NUM_FUNCTIONS; j++)
95 {
96 n = backtrace (addresses, j);
97 if (n > j)
98 {
99 FAIL ();
100 return;
101 }
102 }
31d470ac
JM
103}
104
105NO_INLINE int
d400dcac 106fn (int c, int flags)
31d470ac
JM
107{
108 pid_t parent_pid, child_pid;
109 int pipefd[2];
110 char r[1];
111 struct sigaction act;
112
113 if (c > 0)
114 {
d400dcac 115 fn (c - 1, flags);
31d470ac
JM
116 return x;
117 }
118
119 memset (&act, 0, sizeof (act));
120 act.sa_handler = handle_signal;
d400dcac 121 act.sa_flags = flags;
31d470ac
JM
122 sigemptyset (&act.sa_mask);
123 sigaction (SIGUSR1, &act, NULL);
124 parent_pid = getpid ();
125 if (pipe (pipefd) == -1)
126 abort ();
127
128 child_pid = fork ();
129 if (child_pid == (pid_t) -1)
130 abort ();
131 else if (child_pid == 0)
132 {
133 sleep (1);
134 kill (parent_pid, SIGUSR1);
135 _exit (0);
136 }
137
138 /* In the parent. */
139 read (pipefd[0], r, 1);
140
141 return 0;
142}
143
341657bc 144NO_INLINE int
31d470ac
JM
145do_test (void)
146{
d400dcac 147 fn (2, SIGACTION_FLAGS);
31d470ac
JM
148 return ret;
149}
341657bc
FW
150
151#include <support/test-driver.c>