Skip to content

Commit 87aa290

Browse files
committed
pythongh-149202: Fix frame pointer unwinding on s390x and ARM
-fno-omit-frame-pointer is not enough to make every target walkable by the simple manual frame pointer unwinder. On s390x, GCC and Clang do not emit a usable backchain unless -mbackchain is also enabled. Without it, the unwinder stops at the current C frame and the test reports no Python frames. Once backchains are present, the helper must also stop at the current thread's known C stack bounds; otherwise it can follow the final backchain far enough to dereference an invalid frame and segfault. For Linux s390x backchain frames, the documented z/Architecture stack-frame layout saves r14, the return-address register, at byte offset 112 from the frame pointer, so read the return address from that named slot. Evidence for the s390x offset: Linux's s390 debugging documentation shows the z/Architecture stack-frame layout with the backchain at offset 0 and saved r14 at offset 112: https://www.kernel.org/doc/html/v5.3/s390/debugging390.html#stack-frame-layout This is intentionally not a general s390x unwinder. GNU SFrame's s390x notes call out that the s390x ELF ABI does not generally mandate where RA and FP are saved, if they are saved at all: https://sourceware.org/binutils/docs/sframe-spec.html#s390x On 32-bit ARM, GCC defaults to Thumb mode on common armhf toolchains. The Thumb prologue keeps the saved frame pointer and link register at offsets that depend on the generated frame, which breaks the fp[0]/fp[1] walk used by the helper. Use -marm when it is supported for frame-pointer builds, and teach the helper the GCC ARM-mode saved-fp and saved-lr slots.
1 parent 3efd2f4 commit 87aa290

7 files changed

Lines changed: 246 additions & 24 deletions

File tree

Doc/howto/perf_profiling.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ How to obtain the best results
219219

220220
For best results, keep frame pointers enabled. On supported GCC-compatible
221221
toolchains, CPython builds itself with ``-fno-omit-frame-pointer`` and, when
222-
available, ``-mno-omit-leaf-frame-pointer`` by default. These flags allow
222+
available, ``-mno-omit-leaf-frame-pointer`` by default. On 32-bit ARM,
223+
CPython also adds ``-marm`` when supported. On s390 platforms, CPython also
224+
adds ``-mbackchain`` when supported. These flags allow
223225
profilers to unwind using only the frame pointer and not on DWARF debug
224226
information. This is because as the code that is interposed to allow ``perf``
225227
support is dynamically generated it doesn't have any DWARF debugging information

Doc/using/configure.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,11 @@ also be used to improve performance.
784784

785785
Disable frame pointers, which are enabled by default (see :pep:`831`).
786786

787-
By default, the build appends ``-fno-omit-frame-pointer`` (and
788-
``-mno-omit-leaf-frame-pointer`` when the compiler supports it) to
789-
``BASECFLAGS`` so profilers, debuggers, and system tracing tools
787+
By default, the build appends ``-fno-omit-frame-pointer``,
788+
``-mno-omit-leaf-frame-pointer`` when the compiler supports it,
789+
``-marm`` on 32-bit ARM when supported, and ``-mbackchain`` on s390
790+
platforms when supported, to ``BASECFLAGS`` so
791+
profilers, debuggers, and system tracing tools
790792
(``perf``, ``eBPF``, ``dtrace``, ``gdb``) can walk the C call stack
791793
without DWARF metadata. The flags propagate to third-party C
792794
extensions through :mod:`sysconfig`. On compilers that do not

Doc/whatsnew/3.15.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,8 +2305,9 @@ Build changes
23052305
(:pep:`831`). Pass :option:`--without-frame-pointers` to opt out.
23062306
Authors of C extensions and native libraries built with custom build
23072307
systems should add ``-fno-omit-frame-pointer`` and
2308-
``-mno-omit-leaf-frame-pointer`` to their own ``CFLAGS`` to keep the
2309-
unwind chain intact.
2308+
``-mno-omit-leaf-frame-pointer`` to their own ``CFLAGS``,
2309+
``-marm`` on 32-bit ARM, and ``-mbackchain`` on s390 platforms,
2310+
to keep the unwind chain intact.
23102311
(Contributed by Pablo Galindo Salgado and Savannah Ostrowski in :gh:`149201`.)
23112312

23122313
.. _whatsnew315-windows-tail-calling-interpreter:
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Enable frame pointers by default for GCC-compatible CPython builds, including
2-
``-mno-omit-leaf-frame-pointer`` when the compiler supports it, so profilers
3-
and debuggers can unwind native interpreter frames more reliably. Users can pass
2+
``-mno-omit-leaf-frame-pointer``, ``-marm`` on 32-bit ARM, and ``-mbackchain``
3+
on s390 platforms when the compiler supports them, so profilers and debuggers
4+
can unwind native interpreter frames more reliably. Users can pass
45
``--without-frame-pointers`` to opt out.

Modules/_testinternalcapi.c

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@
5959

6060
static const uintptr_t min_frame_pointer_addr = 0x1000;
6161

62+
#ifdef __s390x__
63+
// Linux s390x stack-frame layout has the backchain at offset 0 and, in the
64+
// z/Architecture column, saved r14 at offset 112. r14 is the return-address
65+
// register, so backchain unwinding reads the return address from this slot.
66+
// See Linux's "Debugging on Linux for s/390 & z/Architecture", Stack Frame
67+
// Layout table:
68+
// https://www.kernel.org/doc/html/v5.3/s390/debugging390.html#stack-frame-layout
69+
# define S390X_FRAME_RETURN_ADDRESS_OFFSET 112
70+
#endif
71+
72+
#if defined(__arm__) && !defined(__thumb__) && !defined(__clang__)
73+
// GCC ARM mode keeps the caller's fp below fp and the saved LR at fp[0].
74+
# define FRAME_POINTER_NEXT_OFFSET (-1)
75+
# define FRAME_POINTER_RETURN_OFFSET 0
76+
#elif defined(__s390x__)
77+
# define FRAME_POINTER_NEXT_OFFSET 0
78+
# define FRAME_POINTER_RETURN_OFFSET \
79+
(S390X_FRAME_RETURN_ADDRESS_OFFSET / (Py_ssize_t)sizeof(uintptr_t))
80+
#else
81+
# define FRAME_POINTER_NEXT_OFFSET 0
82+
# define FRAME_POINTER_RETURN_OFFSET 1
83+
#endif
84+
6285

6386
static PyObject *
6487
_get_current_module(void)
@@ -325,16 +348,97 @@ get_jit_backend(PyObject *self, PyObject *Py_UNUSED(args))
325348
#endif
326349
}
327350

351+
static int
352+
stack_address_is_valid(uintptr_t addr, uintptr_t stack_min, uintptr_t stack_max)
353+
{
354+
if (addr < min_frame_pointer_addr) {
355+
return 0;
356+
}
357+
if (stack_min != 0 && (addr < stack_min || addr >= stack_max)) {
358+
return 0;
359+
}
360+
return 1;
361+
}
362+
363+
static int
364+
frame_pointer_slot_is_valid(uintptr_t *frame_pointer, Py_ssize_t offset,
365+
uintptr_t stack_min, uintptr_t stack_max)
366+
{
367+
uintptr_t fp_addr = (uintptr_t)frame_pointer;
368+
uintptr_t slot_addr;
369+
uintptr_t delta = (uintptr_t)Py_ABS(offset) * sizeof(uintptr_t);
370+
if (offset < 0) {
371+
if (fp_addr < delta) {
372+
return 0;
373+
}
374+
slot_addr = fp_addr - delta;
375+
}
376+
else {
377+
if (fp_addr > UINTPTR_MAX - delta) {
378+
return 0;
379+
}
380+
slot_addr = fp_addr + delta;
381+
}
382+
if (!stack_address_is_valid(slot_addr, stack_min, stack_max)) {
383+
return 0;
384+
}
385+
if (stack_max != 0) {
386+
if (slot_addr > UINTPTR_MAX - sizeof(uintptr_t)) {
387+
return 0;
388+
}
389+
if (slot_addr + sizeof(uintptr_t) > stack_max) {
390+
return 0;
391+
}
392+
}
393+
return 1;
394+
}
395+
396+
static int
397+
next_frame_pointer_is_valid(uintptr_t *frame_pointer, uintptr_t *next_fp,
398+
uintptr_t stack_min, uintptr_t stack_max)
399+
{
400+
uintptr_t fp_addr = (uintptr_t)frame_pointer;
401+
uintptr_t next_addr = (uintptr_t)next_fp;
402+
if (!stack_address_is_valid(next_addr, stack_min, stack_max)) {
403+
return 0;
404+
}
405+
if ((next_addr % sizeof(uintptr_t)) != 0) {
406+
return 0;
407+
}
408+
#if _Py_STACK_GROWS_DOWN
409+
return next_addr > fp_addr;
410+
#else
411+
return next_addr < fp_addr;
412+
#endif
413+
}
414+
328415
static PyObject *
329416
manual_unwind_from_fp(uintptr_t *frame_pointer)
330417
{
331418
Py_ssize_t max_depth = 200;
332-
int stack_grows_down = _Py_STACK_GROWS_DOWN;
419+
uintptr_t stack_min = 0;
420+
uintptr_t stack_max = 0;
421+
422+
#ifdef __s390x__
423+
Py_BUILD_ASSERT(S390X_FRAME_RETURN_ADDRESS_OFFSET % sizeof(uintptr_t) == 0);
424+
#endif
333425

334426
if (frame_pointer == NULL) {
335427
return PyList_New(0);
336428
}
337429

430+
PyThreadState *tstate = _PyThreadState_GET();
431+
if (tstate != NULL) {
432+
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
433+
#if _Py_STACK_GROWS_DOWN
434+
stack_min = tstate_impl->c_stack_hard_limit;
435+
stack_max = tstate_impl->c_stack_top;
436+
#else
437+
stack_min = tstate_impl->c_stack_top;
438+
stack_max = tstate_impl->c_stack_hard_limit;
439+
#endif
440+
}
441+
338442
PyObject *result = PyList_New(0);
339443
if (result == NULL) {
340444
return NULL;
@@ -348,7 +452,21 @@ manual_unwind_from_fp(uintptr_t *frame_pointer)
348452
if ((fp_addr % sizeof(uintptr_t)) != 0) {
349453
break;
350454
}
351-
uintptr_t return_addr = frame_pointer[1];
455+
if (!stack_address_is_valid(fp_addr, stack_min, stack_max)) {
456+
break;
457+
}
458+
if (!frame_pointer_slot_is_valid(frame_pointer,
459+
FRAME_POINTER_NEXT_OFFSET,
460+
stack_min, stack_max)) {
461+
break;
462+
}
463+
if (!frame_pointer_slot_is_valid(frame_pointer,
464+
FRAME_POINTER_RETURN_OFFSET,
465+
stack_min, stack_max)) {
466+
break;
467+
}
468+
uintptr_t *next_fp = (uintptr_t *)frame_pointer[FRAME_POINTER_NEXT_OFFSET];
469+
uintptr_t return_addr = frame_pointer[FRAME_POINTER_RETURN_OFFSET];
352470

353471
PyObject *addr_obj = PyLong_FromUnsignedLongLong(return_addr);
354472
if (addr_obj == NULL) {
@@ -362,22 +480,10 @@ manual_unwind_from_fp(uintptr_t *frame_pointer)
362480
}
363481
Py_DECREF(addr_obj);
364482

365-
uintptr_t *next_fp = (uintptr_t *)frame_pointer[0];
366-
// Stop if the frame pointer is extremely low.
367-
if ((uintptr_t)next_fp < min_frame_pointer_addr) {
483+
if (!next_frame_pointer_is_valid(frame_pointer, next_fp,
484+
stack_min, stack_max)) {
368485
break;
369486
}
370-
uintptr_t next_addr = (uintptr_t)next_fp;
371-
if (stack_grows_down) {
372-
if (next_addr <= fp_addr) {
373-
break;
374-
}
375-
}
376-
else {
377-
if (next_addr >= fp_addr) {
378-
break;
379-
}
380-
}
381487
frame_pointer = next_fp;
382488
}
383489

configure

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,16 @@ AS_VAR_IF([ac_cv_gcc_compat], [yes], [
25482548
AX_CHECK_COMPILE_FLAG([-mno-omit-leaf-frame-pointer], [
25492549
frame_pointer_cflags="$frame_pointer_cflags -mno-omit-leaf-frame-pointer"
25502550
], [], [-Werror])
2551+
AS_CASE([$host_cpu], [arm|armv*], [
2552+
AX_CHECK_COMPILE_FLAG([-marm], [
2553+
frame_pointer_cflags="$frame_pointer_cflags -marm"
2554+
], [], [-Werror])
2555+
])
2556+
AS_CASE([$host_cpu], [s390*], [
2557+
AX_CHECK_COMPILE_FLAG([-mbackchain], [
2558+
frame_pointer_cflags="$frame_pointer_cflags -mbackchain"
2559+
], [], [-Werror])
2560+
])
25512561
], [], [-Werror])
25522562
if test -n "$frame_pointer_cflags" && test "x$with_frame_pointers" != xno; then
25532563
BASECFLAGS="$frame_pointer_cflags $BASECFLAGS"

0 commit comments

Comments
 (0)