Skip to content

Commit fb0c17e

Browse files
committed
Reset JIT GDB mutex after fork
1 parent 74e65ca commit fb0c17e

5 files changed

Lines changed: 36 additions & 24 deletions

File tree

‎Include/internal/pycore_jit_unwind.h‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
#include <stddef.h>
99
#include <stdint.h>
1010

11-
#if defined(PY_HAVE_PERF_TRAMPOLINE) || (defined(__linux__) && defined(__ELF__))
11+
#if defined(_Py_JIT) && defined(__linux__) && defined(__ELF__)
12+
# define PY_HAVE_JIT_GDB_UNWIND
13+
#endif
14+
15+
#if defined(PY_HAVE_PERF_TRAMPOLINE) || defined(PY_HAVE_JIT_GDB_UNWIND)
16+
17+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
18+
extern PyMutex _Py_jit_debug_mutex;
19+
#endif
1220

1321
/* DWARF exception-handling pointer encodings shared by JIT unwind users. */
1422
enum {
@@ -55,6 +63,6 @@ void *_PyJitUnwind_GdbRegisterCode(const void *code_addr,
5563

5664
void _PyJitUnwind_GdbUnregisterCode(void *handle);
5765

58-
#endif // defined(PY_HAVE_PERF_TRAMPOLINE) || (defined(__linux__) && defined(__ELF__))
66+
#endif // defined(PY_HAVE_PERF_TRAMPOLINE) || defined(PY_HAVE_JIT_GDB_UNWIND)
5967

6068
#endif // Py_INTERNAL_JIT_UNWIND_H

‎Modules/posixmodule.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "pycore_fileutils.h" // _Py_closerange()
2121
#include "pycore_import.h" // _PyImport_AcquireLock()
2222
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
23+
#include "pycore_jit_unwind.h" // _Py_jit_debug_mutex
2324
#include "pycore_long.h" // _PyLong_IsNegative()
2425
#include "pycore_moduleobject.h" // _PyModule_GetState()
2526
#include "pycore_object.h" // _PyObject_LookupSpecial()
@@ -781,6 +782,12 @@ PyOS_AfterFork_Child(void)
781782
}
782783
assert(_PyThreadState_GET() == tstate);
783784

785+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
786+
// The child can inherit this mutex locked if another thread held it at
787+
// fork(), but the child itself cannot be inside gdb_jit_register_code().
788+
_Py_jit_debug_mutex = (PyMutex){0};
789+
#endif
790+
784791
status = _PyPerfTrampoline_AfterFork_Child();
785792
if (_PyStatus_EXCEPTION(status)) {
786793
goto fatal_error;

‎Python/jit.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jit_record_code(const void *code_addr, size_t code_size,
8383
}
8484
#endif
8585

86-
#if defined(__linux__) && defined(__ELF__)
86+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
8787
return _PyJitUnwind_GdbRegisterCode(
8888
code_addr, code_size, entry, filename);
8989
#else
@@ -766,7 +766,7 @@ _PyJIT_Free(_PyExecutorObject *executor)
766766
if (memory) {
767767
executor->jit_code = NULL;
768768
executor->jit_size = 0;
769-
#if defined(__linux__) && defined(__ELF__)
769+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
770770
if (executor->jit_gdb_handle != NULL) {
771771
_PyJitUnwind_GdbUnregisterCode(executor->jit_gdb_handle);
772772
executor->jit_gdb_handle = NULL;

‎Python/jit_unwind.c‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@
99
#include "pycore_jit_unwind.h"
1010
#include "pycore_lock.h"
1111

12-
#if defined(_Py_JIT) && defined(__linux__) && defined(__ELF__)
12+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
1313
# include "jit_unwind_info.h"
1414
# if !JIT_UNWIND_INFO_SUPPORTED
1515
# error "JIT unwind info was not generated for this target"
1616
# endif
17-
# define PY_HAVE_JIT_GDB_UNWIND 1
18-
#else
19-
# define PY_HAVE_JIT_GDB_UNWIND 0
2017
#endif
2118

22-
#if defined(PY_HAVE_PERF_TRAMPOLINE) || (defined(__linux__) && defined(__ELF__))
19+
#if defined(PY_HAVE_PERF_TRAMPOLINE) || defined(PY_HAVE_JIT_GDB_UNWIND)
2320

24-
#if defined(__linux__)
21+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
2522
# include <elf.h>
2623
#endif
2724
#include <stdio.h>
@@ -215,13 +212,13 @@ static void elfctx_append_uleb128(ELFObjectContext* ctx, uint32_t v) {
215212
// =============================================================================
216213

217214
static void elf_init_ehframe_perf(ELFObjectContext* ctx);
218-
#if PY_HAVE_JIT_GDB_UNWIND
215+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
219216
static void elf_init_ehframe_gdb(ELFObjectContext* ctx);
220217
#endif
221218

222219
static inline void elf_init_ehframe(ELFObjectContext* ctx, int absolute_addr) {
223220
if (absolute_addr) {
224-
#if PY_HAVE_JIT_GDB_UNWIND
221+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
225222
elf_init_ehframe_gdb(ctx);
226223
#else
227224
Py_UNREACHABLE();
@@ -638,7 +635,7 @@ static void elf_init_ehframe_perf(ELFObjectContext* ctx) {
638635
* and the FDE body is empty. Tools/jit/_targets.py derives the initial CFI
639636
* rules from the row active at the executor call in the compiled shim object.
640637
*/
641-
#if PY_HAVE_JIT_GDB_UNWIND
638+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
642639
static void elf_init_ehframe_gdb(ELFObjectContext* ctx) {
643640
int fde_ptr_enc = DWRF_EH_PE_absptr;
644641
uint8_t* p = ctx->p;
@@ -677,7 +674,7 @@ static void elf_init_ehframe_gdb(ELFObjectContext* ctx) {
677674
}
678675
#endif
679676

680-
#if defined(__linux__) && defined(__ELF__)
677+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
681678
enum {
682679
JIT_NOACTION = 0,
683680
JIT_REGISTER_FN = 1,
@@ -699,7 +696,7 @@ struct jit_descriptor {
699696
struct jit_code_entry *first_entry;
700697
};
701698

702-
static PyMutex jit_debug_mutex = {0};
699+
PyMutex _Py_jit_debug_mutex = {0};
703700

704701
Py_EXPORTED_SYMBOL volatile struct jit_descriptor __jit_debug_descriptor = {
705702
1, JIT_NOACTION, NULL, NULL
@@ -892,7 +889,7 @@ gdb_jit_register_code(
892889
entry->symfile_size = total_size;
893890
entry->code_addr = code_addr;
894891

895-
PyMutex_Lock(&jit_debug_mutex);
892+
PyMutex_Lock(&_Py_jit_debug_mutex);
896893
entry->prev = NULL;
897894
entry->next = __jit_debug_descriptor.first_entry;
898895
if (entry->next != NULL) {
@@ -904,18 +901,18 @@ gdb_jit_register_code(
904901
__jit_debug_register_code();
905902
__jit_debug_descriptor.action_flag = JIT_NOACTION;
906903
__jit_debug_descriptor.relevant_entry = NULL;
907-
PyMutex_Unlock(&jit_debug_mutex);
904+
PyMutex_Unlock(&_Py_jit_debug_mutex);
908905
return entry;
909906
}
910-
#endif // __linux__ && __ELF__
907+
#endif // defined(PY_HAVE_JIT_GDB_UNWIND)
911908

912909
void *
913910
_PyJitUnwind_GdbRegisterCode(const void *code_addr,
914911
size_t code_size,
915912
const char *entry,
916913
const char *filename)
917914
{
918-
#if defined(__linux__) && defined(__ELF__)
915+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
919916
/* GDB expects a stable symbol name and absolute addresses in .eh_frame. */
920917
if (entry == NULL) {
921918
entry = "";
@@ -954,13 +951,13 @@ _PyJitUnwind_GdbRegisterCode(const void *code_addr,
954951
void
955952
_PyJitUnwind_GdbUnregisterCode(void *handle)
956953
{
957-
#if defined(__linux__) && defined(__ELF__)
954+
#if defined(PY_HAVE_JIT_GDB_UNWIND)
958955
struct jit_code_entry *entry = (struct jit_code_entry *)handle;
959956
if (entry == NULL) {
960957
return;
961958
}
962959

963-
PyMutex_Lock(&jit_debug_mutex);
960+
PyMutex_Lock(&_Py_jit_debug_mutex);
964961
if (entry->prev != NULL) {
965962
entry->prev->next = entry->next;
966963
}
@@ -977,7 +974,7 @@ _PyJitUnwind_GdbUnregisterCode(void *handle)
977974
__jit_debug_descriptor.action_flag = JIT_NOACTION;
978975
__jit_debug_descriptor.relevant_entry = NULL;
979976

980-
PyMutex_Unlock(&jit_debug_mutex);
977+
PyMutex_Unlock(&_Py_jit_debug_mutex);
981978

982979
PyMem_RawFree((void *)entry->symfile_addr);
983980
PyMem_RawFree(entry);
@@ -986,4 +983,4 @@ _PyJitUnwind_GdbUnregisterCode(void *handle)
986983
#endif
987984
}
988985

989-
#endif // defined(PY_HAVE_PERF_TRAMPOLINE) || (defined(__linux__) && defined(__ELF__))
986+
#endif // defined(PY_HAVE_PERF_TRAMPOLINE) || defined(PY_HAVE_JIT_GDB_UNWIND)

‎Tools/c-analyzer/cpython/ignored.tsv‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ Python/intrinsics.c - _PyIntrinsics_BinaryFunctions -
387387
Python/lock.c - TIME_TO_BE_FAIR_NS -
388388
Python/opcode_targets.h - opcode_targets -
389389
Python/jit_unwind.c - __jit_debug_descriptor -
390-
Python/jit_unwind.c - jit_debug_mutex -
390+
Python/jit_unwind.c - _Py_jit_debug_mutex -
391391
Python/perf_trampoline.c - _Py_perfmap_callbacks -
392392
Python/perf_jit_trampoline.c - _Py_perfmap_jit_callbacks -
393393
Python/perf_jit_trampoline.c - perf_jit_map_state -

0 commit comments

Comments
 (0)