Repository navigation
Expand file tree
/
Copy pathgetpcstack.c
More file actions
902 lines (808 loc) · 26.7 KB
/
Copy pathgetpcstack.c
File metadata and controls
902 lines (808 loc) · 26.7 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
* 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>
#include <stdint.h>
/*
* LIBUMEM'S OWN FRAMES ARE NOT RECORDED (T9).
*
* A walk started in getpcstack() first passes through the allocator itself:
* umem_cache_alloc_debug, _umem_cache_alloc, _umem_alloc, the malloc
* interposer, ... How many depends on the path, so no fixed skip count is
* right. Instead every LEADING pc inside libumem's executable segments is
* dropped, so bc_stack[0] is the first frame outside the library.
*
* The ranges are the PF_X PT_LOAD segments of the object containing a given
* address, found once with dl_iterate_phdr(), which does not allocate. They
* are registered from load-time constructors -- never lazily from the walk,
* which runs under cache locks and would then order the loader lock inside
* them. Written once before use; a reader racing a late dlopen sees 0/0 and
* simply skips nothing.
*
* Only a separately loaded object is registered. ponytail: a static link puts
* libumem in the main program's text, where this cannot tell library from
* caller, so nothing is skipped there; a linker-script section range would
* lift that if static audit stacks ever matter.
*/
#if defined(__linux__)
#include <link.h>
#define UMEM_NINTERNAL 4
static struct { uintptr_t lo, hi; } umem_internal[UMEM_NINTERNAL];
static int
umem_internal_cb(struct dl_phdr_info *info, size_t sz, void *arg)
{
uintptr_t want = (uintptr_t)arg;
int i, n = 0, found = 0;
uintptr_t lo[UMEM_NINTERNAL], hi[UMEM_NINTERNAL];
(void) sz;
for (i = 0; i < info->dlpi_phnum && n < UMEM_NINTERNAL; i++) {
const ElfW(Phdr) *ph = &info->dlpi_phdr[i];
if (ph->p_type != PT_LOAD || !(ph->p_flags & PF_X))
continue;
lo[n] = info->dlpi_addr + ph->p_vaddr;
hi[n] = lo[n] + ph->p_memsz;
if (want >= lo[n] && want < hi[n])
found = 1;
n++;
}
if (!found)
return (0);
/* the main program (or a static link): caller code lives here too */
if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0')
return (1);
for (i = 0; i < n; i++) {
int s;
for (s = 0; s < UMEM_NINTERNAL; s++) {
if (umem_internal[s].hi == 0 ||
umem_internal[s].lo == lo[i]) {
umem_internal[s].lo = lo[i];
umem_internal[s].hi = hi[i];
break;
}
}
}
return (1);
}
/* Register the object containing addr as internal. Constructor-time only. */
void
umem_pcstack_internal(const void *addr)
{
(void) dl_iterate_phdr(umem_internal_cb, (void *)(uintptr_t)addr);
}
static int
umem_pc_internal(uintptr_t pc)
{
int s;
for (s = 0; s < UMEM_NINTERNAL && umem_internal[s].hi != 0; s++)
if (pc >= umem_internal[s].lo && pc < umem_internal[s].hi)
return (1);
return (0);
}
static void umem_pcstack_self_init(void) __attribute__((constructor));
static void
umem_pcstack_self_init(void)
{
umem_pcstack_internal((const void *)(uintptr_t)&umem_pc_internal);
}
#else
void
umem_pcstack_internal(const void *addr)
{
(void) addr;
}
static int
umem_pc_internal(uintptr_t pc)
{
(void) pc;
return (0);
}
#endif
/*
* REAL STACK BOUNDS FOR THE FRAME-POINTER WALK (P5.9).
*
* The Linux frame walks below dereference fp[0]/fp[1]. Alignment, a
* monotonically-increasing chain and a 16 MiB span above the starting frame
* were the only limits, and none of them bounds the walk to memory that is
* actually this thread's stack:
*
* - a corrupted saved-fp (UMEM_DEBUG=audit on a program that overflows a
* stack buffer) points anywhere;
* - a caller compiled WITHOUT frame pointers -- the -O2 default -- leaves
* an ordinary data value in %rbp/x29, so the "saved fp" is just whatever
* that function was using the register for.
*
* Either way the allocator READ an attacker-influenced address. Read-only
* (the only writes are pcstack[depth++], bounded by pcstack_limit, into the
* caller's own bufctl), so this is a crash or an info leak, not code
* execution -- but it is still an out-of-bounds read inside malloc.
*
* So ask the system where this thread's stack is and reject every frame
* outside it: the mapping that contains the current stack pointer, read from
* /proc/self/maps (see the re-entrancy note below for why not
* pthread_getattr_np()).
*
* RE-ENTRANCY -- WHY THIS MUST NOT ALLOCATE. getpcstack() runs on the
* allocator's OWN internal paths: vmem_hash_insert() records it for every
* audited segment (vmem.c), the UMEM_AUDIT macro records it per bufctl, and
* both are reached with vmem/cache locks held while the arena state is
* half-updated. So this function and everything it calls MUST be
* allocation-free. The earlier implementation called glibc
* pthread_getattr_np(), which mallocs a buffer for the thread's CPU-affinity
* mask (nptl/pthread_getattr_np.c); under LD_PRELOAD that malloc is libumem's
* own, so it re-entered the allocator from inside vmem_populate() and either
* tripped the VMC_POPULATOR assert or deadlocked on the vmem locks. An
* in_lookup flag only stopped recursion back into THIS function; the
* re-entrant malloc still ran. (Codeberg bug report 2026-11-04.)
*
* So read the bounds without allocating: find the mapping that contains the
* current stack pointer by parsing /proc/self/maps with raw open/read into a
* stack buffer (no stdio, no malloc). This is uniform for the main thread
* and every other thread -- the current SP lands in exactly one mapping -- so
* no pthread descriptor or main-vs-other special case is needed. The answer
* is cached per thread, so the cost is one maps read per thread.
*
* On macOS there is no /proc; the bounds come from libpthread instead (the
* __APPLE__ umem_maps_find_sp() below), which is allocation-free too.
*
* FALLBACK, stated rather than hidden: where no bounds are available (no
* /proc, a parse miss, or a build that is neither Linux nor macOS) the old
* 16 MiB-ceiling heuristic still applies. It is a heuristic: it bounds how
* far the walk can wander UP from a frame that is genuinely on the stack, and
* nothing more.
*/
#if defined(__linux__)
#define UMEM_HAVE_STACK_BOUNDS 1
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h> /* strtoull (allocation-free, used below) */
/*
* Parse /proc/self/maps for the mapping that contains address `sp`, writing
* its [start,end) to *lo/*hi. Returns 1 on a hit, 0 otherwise. Uses only
* open/read/close and a fixed stack buffer -- NO stdio and NO allocation (see
* the re-entrancy note above). The file is read in chunks; a line split
* across two reads is carried over in `line`.
*/
static int
umem_maps_find_sp(uintptr_t sp, uintptr_t *lo, uintptr_t *hi)
{
char buf[4096];
char line[256];
size_t linelen = 0;
int fd, hit = 0;
ssize_t n;
fd = open("/proc/self/maps", O_RDONLY | O_CLOEXEC);
if (fd < 0)
return (0);
while (!hit && (n = read(fd, buf, sizeof (buf))) > 0) {
ssize_t i;
for (i = 0; i < n; i++) {
char c = buf[i];
if (c != '\n') {
if (linelen < sizeof (line) - 1)
line[linelen++] = c;
continue;
}
/* complete line: "<start>-<end> ..." in hex */
line[linelen] = '\0';
linelen = 0;
{
char *p = line, *endp;
uintptr_t start, end;
start = (uintptr_t)strtoull(p, &endp, 16);
if (endp == p || *endp != '-')
continue;
p = endp + 1;
end = (uintptr_t)strtoull(p, &endp, 16);
if (endp == p)
continue;
if (sp >= start && sp < end) {
*lo = start;
*hi = end;
hit = 1;
break;
}
}
}
}
(void) close(fd);
return (hit);
}
#elif defined(__APPLE__)
#define UMEM_HAVE_STACK_BOUNDS 1
#include <pthread.h>
/*
* macOS: libpthread keeps every thread's stack top and size in its pthread_t,
* the main thread's included. For pthread_self() both calls read that
* struct without allocating or taking a lock (libpthread pthread.c); the
* main thread's size may cost one getrlimit(2) the first time. The size is
* not a page multiple on 16 KiB pages, and [top - size, top) is what the
* thread may use, which is all the walk needs.
*/
static int
umem_maps_find_sp(uintptr_t sp, uintptr_t *lo, uintptr_t *hi)
{
uintptr_t top = (uintptr_t)pthread_get_stackaddr_np(pthread_self());
size_t size = pthread_get_stacksize_np(pthread_self());
if (top == 0 || size == 0 || sp < top - size || sp >= top)
return (0);
*lo = top - size;
*hi = top;
return (1);
}
#endif
/*
* Returns 1 and fills [*lop, *hip) with this thread's stack, or 0 if the
* bounds could not be determined. Never allocates (see the re-entrancy note
* above); the cached path is a plain TLS read.
*/
static int
umem_stack_bounds(uintptr_t *lop, uintptr_t *hip)
{
#ifdef UMEM_HAVE_STACK_BOUNDS
static __thread uintptr_t cached_lo
__attribute__((tls_model("initial-exec")));
static __thread uintptr_t cached_hi
__attribute__((tls_model("initial-exec")));
if (cached_hi == 0) {
/*
* The SP of THIS frame is on the stack whose mapping we want.
* The whole lookup is allocation-free, so no in_lookup guard is
* needed any more -- but a missed parse just leaves cached_hi 0
* and we report "unknown" (the heuristic then applies), the same
* as before.
*/
uintptr_t sp = (uintptr_t)__builtin_frame_address(0);
uintptr_t lo = 0, hi = 0;
if (umem_maps_find_sp(sp, &lo, &hi) && hi > lo) {
cached_lo = lo;
cached_hi = hi;
} else {
return (0);
}
}
*lop = cached_lo;
*hip = cached_hi;
return (1);
#else
(void) lop;
(void) hip;
return (0);
#endif
}
/*
* Is the frame at fp safe to dereference? Both fp[0] and fp[1] must lie
* inside the thread stack. With no bounds available, fall back to the
* heuristic ceiling. (Alignment is checked by the caller: it differs per
* architecture.)
*/
static int
umem_frame_readable(uintptr_t fp, int have_bounds, uintptr_t lo, uintptr_t hi,
uintptr_t ceiling)
{
const uintptr_t need = 2 * sizeof (uintptr_t);
if (have_bounds)
return (hi >= need && fp >= lo && fp <= hi - need);
return (fp < ceiling);
}
#if defined(EC_UMEM_DUMMY_PCSTACK) && (defined(__amd64) || defined(__x86_64__) || defined(__i386))
/*
* Linux x86 (32/64-bit) stack unwinding.
*
* The Solaris getpcstack() below relies on stack_getbounds()/thr_stksegment(),
* which do not exist on Linux, so those platforms otherwise fall back to a
* dummy that returns 0 -- leaving UMEM_DEBUG=audit records with empty stacks.
* We instead walk the frame-pointer chain directly. This requires the library
* to be built with -fno-omit-frame-pointer (audit is a debug build anyway).
*
* On x86 the frame layout at [fp] is { saved_rbp, return_addr }, identical in
* shape to the aarch64 path above.
*/
#define UMEM_HAVE_REAL_PCSTACK 1
/*ARGSUSED*/
int
getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_sigthread)
{
uintptr_t *fp = (uintptr_t *)__builtin_frame_address(0);
uintptr_t *nextfp;
uintptr_t fp_ceiling, stk_lo = 0, stk_hi = 0;
int have_bounds;
int depth = 0;
uintptr_t first_internal = 0;
if (check_sigthread)
return (0); /* not safe from a signal handler */
/*
* Real stack bounds where the system will tell us (P5.9); the 16 MiB
* span above the starting frame is only the fallback. See
* umem_stack_bounds() above.
*/
have_bounds = umem_stack_bounds(&stk_lo, &stk_hi);
fp_ceiling = (uintptr_t)fp + (16 * 1024 * 1024);
while (depth < pcstack_limit && fp != NULL) {
/* frame pointers are pointer-aligned */
if ((uintptr_t)fp & (sizeof (uintptr_t) - 1))
break;
/* Never dereference a frame outside this thread's stack */
if (!umem_frame_readable((uintptr_t)fp, have_bounds,
stk_lo, stk_hi, fp_ceiling))
break;
nextfp = (uintptr_t *)(fp[0]); /* saved frame pointer */
/* return address; leading libumem frames are dropped */
if (depth > 0 || !umem_pc_internal(fp[1]))
pcstack[depth++] = fp[1];
else if (first_internal == 0)
first_internal = fp[1];
/* stack grows down: a valid caller frame is at a higher address */
if (nextfp <= fp)
break;
fp = nextfp;
}
/*
* The walk never left libumem (x86_64 without --enable-frame-pointers:
* %rbp holds data after the first frame). Keep that one frame rather
* than an empty stack; findleaks/leaks print why it is only one.
*/
if (depth == 0 && first_internal != 0 && pcstack_limit > 0)
pcstack[depth++] = first_internal;
return (depth);
}
#elif 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;
uintptr_t fp_ceiling, stk_lo = 0, stk_hi = 0;
int have_bounds;
int depth = 0;
uintptr_t first_internal = 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)
);
/*
* Real stack bounds where the system will tell us (P5.9); the 16 MiB
* span above the starting frame is only the fallback. See
* umem_stack_bounds() above.
*/
have_bounds = umem_stack_bounds(&stk_lo, &stk_hi);
fp_ceiling = (uintptr_t)fp + (16 * 1024 * 1024);
/* 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;
}
/* Never dereference a frame outside this thread's stack */
if (!umem_frame_readable((uintptr_t)fp, have_bounds,
stk_lo, stk_hi, fp_ceiling)) {
break;
}
/* nextfp = *fp (previous frame pointer at [fp + 0]) */
nextfp = (uintptr_t *)(*fp);
/* return_address = *(fp + 1) (at [fp + 8]); leading libumem
* frames are dropped */
if (depth == 0 && umem_pc_internal(*(fp + 1))) {
if (first_internal == 0)
first_internal = *(fp + 1);
} else if (depth < pcstack_limit) {
pcstack[depth] = *(fp + 1);
depth++;
}
/* Stop if frame pointer doesn't advance */
if (nextfp <= fp) {
break;
}
fp = nextfp;
}
/*
* The walk never left libumem (x86_64 without --enable-frame-pointers:
* %rbp holds data after the first frame). Keep that one frame rather
* than an empty stack; findleaks/leaks print why it is only one.
*/
if (depth == 0 && first_internal != 0 && pcstack_limit > 0)
pcstack[depth++] = first_internal;
return (depth);
}
#elif defined(__linux__) && defined(__riscv) && (__riscv_xlen == 64)
/*
* RISC-V 64-bit (riscv64) stack unwinding.
*
* The RISC-V ELF psABI frame pointer is s0/x8. With a standard frame
* (requires -fno-omit-frame-pointer; the AAL requests it via
* --enable-frame-pointers, like the x86_64 walk above), s0 points just
* ABOVE the saved register area, so the saved return address is at s0[-1]
* (fp-8) and the previous frame pointer is at s0[-2] (fp-16). This differs
* from x86/aarch64, where the pair sits at fp[0]/fp[1].
*
* The P5.9 stack-bounds guard still applies: a wild or data-holding s0 (a
* caller built without frame pointers) must never fault the allocator.
* umem_frame_readable() bounds [fp-16, fp) against this thread's stack.
*/
#define UMEM_HAVE_REAL_PCSTACK 1
/*ARGSUSED*/
int
getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_sigthread)
{
uintptr_t *fp;
uintptr_t *nextfp;
uintptr_t fp_ceiling, stk_lo = 0, stk_hi = 0;
int have_bounds;
int depth = 0;
uintptr_t first_internal = 0;
if (check_sigthread)
return (0); /* not safe from a signal handler */
/* Current frame pointer (s0/x8). */
__asm__ __volatile__("mv %0, s0" : "=r" (fp));
/*
* Real stack bounds where the system will tell us (P5.9); the 16 MiB
* span above the starting frame is only the fallback.
*/
have_bounds = umem_stack_bounds(&stk_lo, &stk_hi);
fp_ceiling = (uintptr_t)fp + (16 * 1024 * 1024);
while (depth < pcstack_limit && fp != NULL) {
/* frame pointers are pointer-aligned */
if ((uintptr_t)fp & (sizeof (uintptr_t) - 1))
break;
/*
* The RISC-V pair sits BELOW fp (at fp-16/fp-8), so the
* readable range is [fp-16, fp). Reject a frame whose
* saved-register slots fall outside this thread's stack.
*/
if ((uintptr_t)fp < 2 * sizeof (uintptr_t))
break;
if (!umem_frame_readable((uintptr_t)fp - 2 * sizeof (uintptr_t),
have_bounds, stk_lo, stk_hi, fp_ceiling))
break;
nextfp = (uintptr_t *)(fp[-2]); /* saved frame pointer */
/* return address at fp[-1]; leading libumem frames dropped */
if (depth > 0 || !umem_pc_internal(fp[-1]))
pcstack[depth++] = fp[-1];
else if (first_internal == 0)
first_internal = fp[-1];
/* stack grows down: a valid caller frame is higher */
if (nextfp <= fp)
break;
fp = nextfp;
}
/*
* The walk never left libumem (built without frame pointers: s0 holds
* data after the first frame). Keep that one frame rather than an
* empty stack; findleaks/leaks print why it is only one.
*/
if (depth == 0 && first_internal != 0 && pcstack_limit > 0)
pcstack[depth++] = first_internal;
return (depth);
}
#elif defined(__linux__) && defined(__arm__)
/*
* ARM 32-bit (armhf, AAPCS) stack unwinding.
*
* AAPCS leaves the frame-pointer register ABI- and mode-dependent: GCC uses
* r11 for ARM code and r7 for Thumb-2. With -fno-omit-frame-pointer (the
* AAL requests it via --enable-frame-pointers, like the x86_64 walk above)
* GCC emits the APCS-style frame on ARM where the frame-pointer register
* points at a slot holding the saved fp, and the saved return address (lr)
* is in the adjacent higher slot. We read the pair at fp[0]/fp[1] and treat
* fp as pointing at { saved_fp, saved_lr } -- the common AAPCS case.
*
* This is a heuristic shape (ARM has never had one mandated frame layout),
* so the P5.9 stack-bounds guard is load-bearing here: a wild or
* data-holding fp (a caller built without frame pointers, or the "wrong"
* fp register for this TU's mode) must never fault the allocator. Every
* dereference is bounds-checked against this thread's stack and the chain
* must strictly increase; a frame that fails either check simply stops the
* walk instead of reading hostile memory.
*/
#define UMEM_HAVE_REAL_PCSTACK 1
/*ARGSUSED*/
int
getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_sigthread)
{
uintptr_t *fp;
uintptr_t *nextfp;
uintptr_t fp_ceiling, stk_lo = 0, stk_hi = 0;
int have_bounds;
int depth = 0;
uintptr_t first_internal = 0;
if (check_sigthread)
return (0); /* not safe from a signal handler */
/*
* Current frame pointer: r7 in Thumb, r11 in ARM. __thumb__ is
* defined when this TU is compiled as Thumb (the armhf default on
* Debian), so pick the register the compiler actually used.
*/
#if defined(__thumb__)
__asm__ __volatile__("mov %0, r7" : "=r" (fp));
#else
__asm__ __volatile__("mov %0, r11" : "=r" (fp));
#endif
/*
* Real stack bounds where the system will tell us (P5.9); the 16 MiB
* span above the starting frame is only the fallback.
*/
have_bounds = umem_stack_bounds(&stk_lo, &stk_hi);
fp_ceiling = (uintptr_t)fp + (16 * 1024 * 1024);
while (depth < pcstack_limit && fp != NULL) {
/* frame pointers are pointer-aligned */
if ((uintptr_t)fp & (sizeof (uintptr_t) - 1))
break;
/* Never dereference a frame outside this thread's stack */
if (!umem_frame_readable((uintptr_t)fp, have_bounds,
stk_lo, stk_hi, fp_ceiling))
break;
nextfp = (uintptr_t *)(fp[0]); /* saved frame pointer */
/* saved lr at fp[1]; leading libumem frames dropped */
if (depth > 0 || !umem_pc_internal(fp[1]))
pcstack[depth++] = fp[1];
else if (first_internal == 0)
first_internal = fp[1];
/* stack grows down: a valid caller frame is higher */
if (nextfp <= fp)
break;
fp = nextfp;
}
/*
* The walk never left libumem (built without frame pointers: the fp
* register holds data after the first frame). Keep that one frame
* rather than an empty stack; findleaks/leaks print why it is only one.
*/
if (depth == 0 && first_internal != 0 && pcstack_limit > 0)
pcstack[depth++] = first_internal;
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(UMEM_HAVE_REAL_PCSTACK) && \
!(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_BACKTRACE)
/*
* Fallback for arches without a dedicated frame-pointer walk above
* (x86 and aarch64 have their own UMEM_HAVE_REAL_PCSTACK paths).
* Uses backtrace(3) with dlopen-recursion guards.
*/
{
extern int backtrace(void **, int);
/*
* backtrace(3) lazily dlopens libgcc_s on first use; the
* dlopen path calls malloc, which (when libumem interposes)
* re-enters the allocator and back into getpcstack. Guard
* with a TLS re-entry flag, a process-wide "warmed" flag
* (set by umem_stacktrace_init via a non-allocator path),
* and an interposing flag.
*/
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);
/* skip getpcstack and its caller as before, then any further
* leading libumem pcs */
int i = 2;
int out = 0;
while (i < got && umem_pc_internal((uintptr_t)raw[i]))
i++;
for (; i < got && out < pcstack_limit; i++)
pcstack[out++] = (uintptr_t)raw[i];
return (out);
}
#else
(void) pcstack;
(void) pcstack_limit;
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__)) */