Skip to content

Commit 3a3a6b8

Browse files
pablogsalambv
andauthored
gh-129223: Do not allow the compiler to optimise away symbols for debug sections (#129225)
Signed-off-by: Pablo Galindo <[email protected]> Co-authored-by: Łukasz Langa <[email protected]>
1 parent e635bf2 commit 3a3a6b8

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

Include/internal/pycore_debug_offsets.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,32 @@ extern "C" {
1717

1818
// Macros to burn global values in custom sections so out-of-process
1919
// profilers can locate them easily.
20-
21-
#define GENERATE_DEBUG_SECTION(name, declaration) \
22-
_GENERATE_DEBUG_SECTION_WINDOWS(name) \
23-
_GENERATE_DEBUG_SECTION_APPLE(name) \
24-
declaration \
25-
_GENERATE_DEBUG_SECTION_LINUX(name)
20+
#define GENERATE_DEBUG_SECTION(name, declaration) \
21+
_GENERATE_DEBUG_SECTION_WINDOWS(name) \
22+
_GENERATE_DEBUG_SECTION_APPLE(name) \
23+
declaration \
24+
_GENERATE_DEBUG_SECTION_LINUX(name)
2625

2726
#if defined(MS_WINDOWS)
2827
#define _GENERATE_DEBUG_SECTION_WINDOWS(name) \
29-
_Pragma(Py_STRINGIFY(section(Py_STRINGIFY(name), read, write))) \
30-
__declspec(allocate(Py_STRINGIFY(name)))
28+
_Pragma(Py_STRINGIFY(section(Py_STRINGIFY(name), read, write))) \
29+
__declspec(allocate(Py_STRINGIFY(name)))
3130
#else
3231
#define _GENERATE_DEBUG_SECTION_WINDOWS(name)
3332
#endif
3433

3534
#if defined(__APPLE__)
3635
#define _GENERATE_DEBUG_SECTION_APPLE(name) \
37-
__attribute__((section(SEG_DATA "," Py_STRINGIFY(name))))
36+
__attribute__((section(SEG_DATA "," Py_STRINGIFY(name)))) \
37+
__attribute__((used))
3838
#else
3939
#define _GENERATE_DEBUG_SECTION_APPLE(name)
4040
#endif
4141

4242
#if defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
4343
#define _GENERATE_DEBUG_SECTION_LINUX(name) \
44-
__attribute__((section("." Py_STRINGIFY(name))))
44+
__attribute__((section("." Py_STRINGIFY(name)))) \
45+
__attribute__((used))
4546
#else
4647
#define _GENERATE_DEBUG_SECTION_LINUX(name)
4748
#endif

Modules/_testexternalinspection.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,11 @@ get_py_runtime(pid_t pid)
414414
static uintptr_t
415415
get_async_debug(pid_t pid)
416416
{
417-
return search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
417+
uintptr_t result = search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
418+
if (result == 0 && !PyErr_Occurred()) {
419+
PyErr_SetString(PyExc_RuntimeError, "Cannot find AsyncioDebug section");
420+
}
421+
return result;
418422
}
419423

420424

@@ -560,6 +564,16 @@ read_int(pid_t pid, uintptr_t address, int *result)
560564
return 0;
561565
}
562566

567+
static int
568+
read_unsigned_long(pid_t pid, uintptr_t address, unsigned long *result)
569+
{
570+
int bytes_read = read_memory(pid, address, sizeof(unsigned long), result);
571+
if (bytes_read < 0) {
572+
return -1;
573+
}
574+
return 0;
575+
}
576+
563577
static int
564578
read_pyobj(pid_t pid, uintptr_t address, PyObject *ptr_addr)
565579
{
@@ -627,7 +641,7 @@ read_py_long(pid_t pid, _Py_DebugOffsets* offsets, uintptr_t address)
627641
return 0;
628642
}
629643

630-
char *digits = (char *)PyMem_RawMalloc(size * sizeof(digit));
644+
digit *digits = (digit *)PyMem_RawMalloc(size * sizeof(digit));
631645
if (!digits) {
632646
PyErr_NoMemory();
633647
return -1;
@@ -645,16 +659,13 @@ read_py_long(pid_t pid, _Py_DebugOffsets* offsets, uintptr_t address)
645659

646660
long value = 0;
647661

662+
// In theory this can overflow, but because of llvm/llvm-project#16778
663+
// we can't use __builtin_mul_overflow because it fails to link with
664+
// __muloti4 on aarch64. In practice this is fine because all we're
665+
// testing here are task numbers that would fit in a single byte.
648666
for (ssize_t i = 0; i < size; ++i) {
649-
long long factor;
650-
if (__builtin_mul_overflow(digits[i], (1UL << (ssize_t)(shift * i)),
651-
&factor)
652-
) {
653-
goto error;
654-
}
655-
if (__builtin_add_overflow(value, factor, &value)) {
656-
goto error;
657-
}
667+
long long factor = digits[i] * (1UL << (ssize_t)(shift * i));
668+
value += factor;
658669
}
659670
PyMem_RawFree(digits);
660671
if (negative) {
@@ -693,8 +704,8 @@ parse_task_name(
693704
return NULL;
694705
}
695706

696-
int flags;
697-
err = read_int(
707+
unsigned long flags;
708+
err = read_unsigned_long(
698709
pid,
699710
(uintptr_t)task_name_obj.ob_type + offsets->type_object.tp_flags,
700711
&flags);

0 commit comments

Comments
 (0)