-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgetpcstack.c
More file actions
332 lines (292 loc) · 8.32 KB
/
getpcstack.c
File metadata and controls
332 lines (292 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Portions Copyright 2006-2008 Message Systems, Inc.
*/
/* #pragma ident "@(#)getpcstack.c 1.5 05/06/08 SMI" */
#include "config.h"
#include "misc.h"
#if HAVE_UCONTEXT_H
#include <ucontext.h>
#endif
#if HAVE_SYS_FRAME_H
#include <sys/frame.h>
#endif
#if HAVE_SYS_STACK_H
#include <sys/stack.h>
#endif
#include <stdio.h>
#if defined(__aarch64__) || defined(__arm64__)
/*
* ARM64 (aarch64) stack unwinding
*
* Frame pointer (fp/x29) points to {previous_fp, return_address} pair.
* We use direct pointer arithmetic instead of struct frame.
*/
/*ARGSUSED*/
int
getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_sigthread)
{
uintptr_t *fp;
uintptr_t *nextfp;
int depth = 0;
if (check_sigthread) {
/* Skip if in signal handler - not safe */
return (0);
}
/* Get current frame pointer (x29) */
__asm__ __volatile__(
"mov %0, x29"
: "=r" (fp)
);
/* Walk the frame pointer chain */
while (depth < pcstack_limit && fp != NULL) {
/* Validate frame pointer alignment (16-byte on ARM64) */
if ((uintptr_t)fp & 0xf) {
break;
}
/* nextfp = *fp (previous frame pointer at [fp + 0]) */
nextfp = (uintptr_t *)(*fp);
/* return_address = *(fp + 1) (at [fp + 8]) */
if (depth < pcstack_limit) {
pcstack[depth] = *(fp + 1);
depth++;
}
/* Stop if frame pointer doesn't advance */
if (nextfp <= fp) {
break;
}
fp = nextfp;
}
return (depth);
}
#elif defined(__MACH__)
/*
* Darwin doesn't have any exposed frame info, so give it some space.
*/
#define UMEM_FRAMESIZE (2 * sizeof(long long))
#elif (defined(__sparc) || defined(__sparcv9)) && HAVE_SYS_STACK_H
extern void flush_windows(void);
#define UMEM_FRAMESIZE MINFRAME
#elif defined(__i386) || defined(__amd64)
/*
* On x86, MINFRAME is defined to be 0, but we want to be sure we can
* dereference the entire frame structure.
*/
#define UMEM_FRAMESIZE (sizeof (struct frame))
#elif defined(__aarch64__) || defined(__arm64__)
/*
* ARM64 uses direct frame pointer walking, frame size is 16 bytes
* (previous fp + return address, both 64-bit)
*/
#define UMEM_FRAMESIZE (2 * sizeof(void *))
#elif !defined(EC_UMEM_DUMMY_PCSTACK)
#error needs update for new architecture
#endif
#if !(defined(__aarch64__) || defined(__arm64__))
/*
* Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking.
* Returns MIN(current stack depth, pcstack_limit).
*
* Note: ARM64 has its own implementation above.
*/
/*ARGSUSED*/
int
getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
{
#ifdef EC_UMEM_DUMMY_PCSTACK
(void) check_signal;
if (pcstack_limit <= 0)
return (0);
#if defined(HAVE_EXECINFO_H) || defined(__linux__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
{
extern int backtrace(void **, int);
/*
* Re-entry guard. backtrace(3) lazily dlopens libgcc_s
* the first time it is called per process; the dlopen
* path itself calls malloc, which (when libumem is
* intercepting via libumem_malloc.so) re-enters the
* allocator and may call back into getpcstack. We avoid
* the recursion two ways:
*
* 1. A thread-local guard short-circuits any nested
* call to getpcstack while we are inside backtrace().
*
* 2. A process-wide "warmed" flag: the first invocation
* dlopens libgcc_s, which is dangerous when umem is
* the malloc. Until backtrace() has been warmed once
* successfully via a non-allocator code path, we
* return zero rather than risk the dlopen recursion.
* umem_stacktrace_init() warms it explicitly.
*/
static __thread int in_backtrace = 0;
extern int umem_backtrace_warmed;
extern int umem_malloc_is_interposing;
if (in_backtrace)
return (0);
if (!umem_backtrace_warmed)
return (0);
if (umem_malloc_is_interposing)
return (0);
in_backtrace = 1;
void *raw[64];
int cap = pcstack_limit + 2;
if (cap > (int)(sizeof (raw) / sizeof (raw[0])))
cap = (int)(sizeof (raw) / sizeof (raw[0]));
int got = backtrace(raw, cap);
in_backtrace = 0;
if (got <= 2)
return (0);
int skip = 2;
int out = 0;
for (int i = skip; i < got && out < pcstack_limit; i++)
pcstack[out++] = (uintptr_t)raw[i];
return (out);
}
#else
(void) pcstack;
return (0);
#endif
#else
struct frame *fp;
struct frame *nextfp, *minfp;
int depth = 0;
uintptr_t base = 0;
size_t size = 0;
#ifndef UMEM_STANDALONE
int on_altstack = 0;
uintptr_t sigbase = 0;
size_t sigsize = 0;
stack_t st;
if (stack_getbounds(&st) != 0) {
if (thr_stksegment(&st) != 0 ||
(uintptr_t)st.ss_sp < st.ss_size) {
return (0); /* unable to get stack bounds */
}
/*
* thr_stksegment(3C) has a slightly different interface than
* stack_getbounds(3C) -- correct it
*/
st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
st.ss_flags = 0; /* can't be on-stack */
}
on_altstack = (st.ss_flags & SS_ONSTACK);
if (st.ss_size != 0) {
base = (uintptr_t)st.ss_sp;
size = st.ss_size;
} else {
/*
* If size == 0, then ss_sp is the *top* of the stack.
*
* Since we only allow increasing frame pointers, and we
* know our caller set his up correctly, we can treat ss_sp
* as an upper bound safely.
*/
base = 0;
size = (uintptr_t)st.ss_sp;
}
if (check_signal != 0) {
void (*sigfunc)() = NULL;
int sigfuncsize = 0;
extern void thr_sighndlrinfo(void (**)(), int *);
thr_sighndlrinfo(&sigfunc, &sigfuncsize);
sigbase = (uintptr_t)sigfunc;
sigsize = sigfuncsize;
}
#else /* UMEM_STANDALONE */
base = (uintptr_t)umem_min_stack;
size = umem_max_stack - umem_min_stack;
#endif
/*
* shorten size so that fr_savfp and fr_savpc will be within the stack
* bounds.
*/
if (size >= UMEM_FRAMESIZE - 1)
size -= (UMEM_FRAMESIZE - 1);
else
size = 0;
#if defined(__sparc) || defined(__sparcv9)
flush_windows();
#endif
/* LINTED alignment */
fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
minfp = fp;
if (((uintptr_t)fp - base) >= size)
return (0); /* the frame pointer isn't in our stack */
while (depth < pcstack_limit) {
uintptr_t tmp;
/* LINTED alignment */
nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
tmp = (uintptr_t)nextfp;
/*
* Check nextfp for validity. It must be properly aligned,
* increasing compared to the last %fp (or the top of the
* stack we just switched to), and it must be inside
* [base, base + size).
*/
if (tmp != SA(tmp))
break;
else if (nextfp <= minfp || (tmp - base) >= size) {
#ifndef UMEM_STANDALONE
if (tmp == NULL || !on_altstack)
break;
/*
* If we're on an alternate signal stack, try jumping
* to the main thread stack.
*
* If the main thread stack has an unlimited size, we
* punt, since we don't know where the frame pointer's
* been.
*
* (thr_stksegment() returns the *top of stack*
* in ss_sp, not the bottom)
*/
if (thr_stksegment(&st) == 0) {
if (st.ss_size >= (uintptr_t)st.ss_sp ||
st.ss_size < UMEM_FRAMESIZE - 1)
break;
on_altstack = 0;
base = (uintptr_t)st.ss_sp - st.ss_size;
size = st.ss_size - (UMEM_FRAMESIZE - 1);
minfp = (struct frame *)base;
continue; /* try again */
}
#endif
break;
}
#ifndef UMEM_STANDALONE
if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
umem_panic("called from signal handler");
#endif
pcstack[depth++] = fp->fr_savpc;
fp = nextfp;
minfp = fp;
}
return (depth);
#endif
}
#endif /* !(defined(__aarch64__) || defined(__arm64__)) */