Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f8295c

Browse files
committed
gh-129223: Do not allow the compiler to optimise away symbols for debug sections
Signed-off-by: Pablo Galindo <[email protected]>
1 parent 5c9a63f commit 0f8295c

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

Include/internal/pycore_debug_offsets.h

Lines changed: 34 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
@@ -327,6 +328,29 @@ typedef struct _Py_DebugOffsets {
327328
}, \
328329
}
329330

331+
// Collection of extern declarations for debug offset symbols in extension
332+
// modules. These declarations are here to ensure the compiler marks them
333+
// as external symbols. Unfortunately there isn't a cross platform way to
334+
// do this elegantly withing the compilation unit itself as different compilers
335+
// complain in different ways.
336+
337+
typedef struct _Py_AsyncioModuleDebugOffsets {
338+
struct _asyncio_task_object {
339+
uint64_t size;
340+
uint64_t task_name;
341+
uint64_t task_awaited_by;
342+
uint64_t task_is_task;
343+
uint64_t task_awaited_by_is_set;
344+
uint64_t task_coro;
345+
} asyncio_task_object;
346+
struct _asyncio_thread_state {
347+
uint64_t size;
348+
uint64_t asyncio_running_loop;
349+
uint64_t asyncio_running_task;
350+
} asyncio_thread_state;
351+
} Py_AsyncioModuleDebugOffsets;
352+
PyAPI_DATA(Py_AsyncioModuleDebugOffsets) AsyncioDebug;
353+
330354

331355
#ifdef __cplusplus
332356
}

Modules/_asynciomodule.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,6 @@ typedef struct {
101101
# define ASYNCIO_STATE_UNLOCK(state) ((void)state)
102102
#endif
103103

104-
typedef struct _Py_AsyncioModuleDebugOffsets {
105-
struct _asyncio_task_object {
106-
uint64_t size;
107-
uint64_t task_name;
108-
uint64_t task_awaited_by;
109-
uint64_t task_is_task;
110-
uint64_t task_awaited_by_is_set;
111-
uint64_t task_coro;
112-
} asyncio_task_object;
113-
struct _asyncio_thread_state {
114-
uint64_t size;
115-
uint64_t asyncio_running_loop;
116-
uint64_t asyncio_running_task;
117-
} asyncio_thread_state;
118-
} Py_AsyncioModuleDebugOffsets;
119-
120104
GENERATE_DEBUG_SECTION(AsyncioDebug, Py_AsyncioModuleDebugOffsets AsyncioDebug)
121105
= {.asyncio_task_object = {
122106
.size = sizeof(TaskObj),

Modules/_testexternalinspection.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@
5959
# define HAVE_PROCESS_VM_READV 0
6060
#endif
6161

62-
struct _Py_AsyncioModuleDebugOffsets {
63-
struct _asyncio_task_object {
64-
uint64_t size;
65-
uint64_t task_name;
66-
uint64_t task_awaited_by;
67-
uint64_t task_is_task;
68-
uint64_t task_awaited_by_is_set;
69-
uint64_t task_coro;
70-
} asyncio_task_object;
71-
struct _asyncio_thread_state {
72-
uint64_t size;
73-
uint64_t asyncio_running_loop;
74-
uint64_t asyncio_running_task;
75-
} asyncio_thread_state;
76-
};
77-
7862
#if defined(__APPLE__) && TARGET_OS_OSX
7963
static uintptr_t
8064
return_section_address(
@@ -414,7 +398,11 @@ get_py_runtime(pid_t pid)
414398
static uintptr_t
415399
get_async_debug(pid_t pid)
416400
{
417-
return search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
401+
uintptr_t result = search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
402+
if (result == 0 && !PyErr_Occurred()) {
403+
PyErr_SetString(PyExc_RuntimeError, "Cannot find AsyncioDebug section");
404+
}
405+
return result;
418406
}
419407

420408

0 commit comments

Comments
 (0)