Skip to content

Commit 7e563b8

Browse files
Kenovtjnash
authored andcommitted
Re-correct TSAN integration with task switching
This reverts commit 15772ba "Update references to tsan state (#42440)", and fixes integration. Looks like #36929 got reverted when trying to simplify the code, but it now isn't legal. Since these must be inlined in the right point in the runtime call/return context, use a macro to ensure that.
1 parent 9ed2413 commit 7e563b8

File tree

7 files changed

+165
-115
lines changed

7 files changed

+165
-115
lines changed

src/gc-stacks.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ void sweep_stack_pools(void)
234234
_jl_free_stack(ptls2, stkbuf, bufsz);
235235
}
236236
#ifdef _COMPILER_TSAN_ENABLED_
237-
if (t->tsan_state) {
238-
__tsan_destroy_fiber(t->tsan_state);
239-
t->tsan_state = NULL;
237+
if (t->ctx.tsan_state) {
238+
__tsan_destroy_fiber(t->ctx.tsan_state);
239+
t->ctx.tsan_state = NULL;
240240
}
241241
#endif
242242
}

src/julia.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,18 +1846,8 @@ typedef struct _jl_task_t {
18461846
jl_excstack_t *excstack;
18471847
// current exception handler
18481848
jl_handler_t *eh;
1849-
1850-
union {
1851-
jl_ucontext_t ctx; // saved thread state
1852-
#ifdef _OS_WINDOWS_
1853-
jl_ucontext_t copy_stack_ctx;
1854-
#else
1855-
struct jl_stack_context_t copy_stack_ctx;
1856-
#endif
1857-
};
1858-
#if defined(_COMPILER_TSAN_ENABLED_)
1859-
void *tsan_state;
1860-
#endif
1849+
// saved thread state
1850+
jl_ucontext_t ctx;
18611851
void *stkbuf; // malloc'd memory (either copybuf or stack)
18621852
size_t bufsz; // actual sizeof stkbuf
18631853
unsigned int copy_stack:31; // sizeof stack for copybuf

src/julia_threads.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ JL_DLLEXPORT void jl_threading_profile(void);
3232

3333
#ifdef _OS_WINDOWS_
3434
#define JL_HAVE_UCONTEXT
35-
typedef win32_ucontext_t jl_ucontext_t;
35+
typedef win32_ucontext_t jl_stack_context_t;
36+
typedef jl_stack_context_t _jl_ucontext_t;
3637
#else
38+
typedef struct {
39+
jl_jmp_buf uc_mcontext;
40+
} jl_stack_context_t;
3741
#if !defined(JL_HAVE_UCONTEXT) && \
3842
!defined(JL_HAVE_ASM) && \
3943
!defined(JL_HAVE_UNW_CONTEXT) && \
@@ -56,13 +60,8 @@ typedef win32_ucontext_t jl_ucontext_t;
5660
#endif
5761
#endif
5862

59-
60-
struct jl_stack_context_t {
61-
jl_jmp_buf uc_mcontext;
62-
};
63-
6463
#if (!defined(JL_HAVE_UNW_CONTEXT) && defined(JL_HAVE_ASM)) || defined(JL_HAVE_SIGALTSTACK)
65-
typedef struct jl_stack_context_t jl_ucontext_t;
64+
typedef jl_stack_context_t _jl_ucontext_t;
6665
#endif
6766
#if defined(JL_HAVE_ASYNCIFY)
6867
#if defined(_COMPILER_TSAN_ENABLED_)
@@ -75,19 +74,30 @@ typedef struct {
7574
// __asyncify_data struct.
7675
void *stackbottom;
7776
void *stacktop;
78-
} jl_ucontext_t;
77+
} _jl_ucontext_t;
7978
#endif
8079
#if defined(JL_HAVE_UNW_CONTEXT)
8180
#define UNW_LOCAL_ONLY
8281
#include <libunwind.h>
83-
typedef unw_context_t jl_ucontext_t;
82+
typedef unw_context_t _jl_ucontext_t;
8483
#endif
8584
#if defined(JL_HAVE_UCONTEXT)
8685
#include <ucontext.h>
87-
typedef ucontext_t jl_ucontext_t;
86+
typedef ucontext_t _jl_ucontext_t;
8887
#endif
8988
#endif
9089

90+
typedef struct {
91+
union {
92+
_jl_ucontext_t ctx;
93+
jl_stack_context_t copy_ctx;
94+
};
95+
#if defined(_COMPILER_TSAN_ENABLED_)
96+
void *tsan_state;
97+
#endif
98+
} jl_ucontext_t;
99+
100+
91101
// handle to reference an OS thread
92102
#ifdef _OS_WINDOWS_
93103
typedef DWORD jl_thread_t;
@@ -225,13 +235,9 @@ typedef struct _jl_tls_states_t {
225235
void *stackbase;
226236
size_t stacksize;
227237
union {
228-
jl_ucontext_t base_ctx; // base context of stack
238+
_jl_ucontext_t base_ctx; // base context of stack
229239
// This hack is needed to support always_copy_stacks:
230-
#ifdef _OS_WINDOWS_
231-
jl_ucontext_t copy_stack_ctx;
232-
#else
233-
struct jl_stack_context_t copy_stack_ctx;
234-
#endif
240+
jl_stack_context_t copy_stack_ctx;
235241
};
236242
// Temp storage for exception thrown in signal handler. Not rooted.
237243
struct _jl_value_t *sig_exception;

src/signals-unix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
#include "julia_assert.h"
4545

4646
// helper function for returning the unw_context_t inside a ucontext_t
47-
static bt_context_t *jl_to_bt_context(void *sigctx)
47+
// (also used by stackwalk.c)
48+
bt_context_t *jl_to_bt_context(void *sigctx)
4849
{
4950
#ifdef __APPLE__
5051
return (bt_context_t*)&((ucontext64_t*)sigctx)->uc_mcontext64->__ss;

src/signals-win.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ static void jl_try_throw_sigint(void)
5959

6060
void __cdecl crt_sig_handler(int sig, int num)
6161
{
62-
jl_task_t *ct = jl_current_task;
6362
CONTEXT Context;
6463
switch (sig) {
6564
case SIGFPE:
@@ -100,8 +99,8 @@ void __cdecl crt_sig_handler(int sig, int num)
10099
// StackOverflowException needs extra stack space to record the backtrace
101100
// so we keep one around, shared by all threads
102101
static jl_mutex_t backtrace_lock;
103-
static jl_ucontext_t collect_backtrace_fiber;
104-
static jl_ucontext_t error_return_fiber;
102+
static win32_ucontext_t collect_backtrace_fiber;
103+
static win32_ucontext_t error_return_fiber;
105104
static PCONTEXT stkerror_ctx;
106105
static jl_ptls_t stkerror_ptls;
107106
static int have_backtrace_fiber;

src/stackwalk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ void jl_rec_backtrace(jl_task_t *t)
717717
#if defined(_OS_WINDOWS_)
718718
bt_context_t c;
719719
memset(&c, 0, sizeof(c));
720-
_JUMP_BUFFER *mctx = (_JUMP_BUFFER*)&t->ctx.uc_mcontext;
720+
_JUMP_BUFFER *mctx = (_JUMP_BUFFER*)&t->ctx.ctx.uc_mcontext;
721721
#if defined(_CPU_X86_64_)
722722
c.Rbx = mctx->Rbx;
723723
c.Rsp = mctx->Rsp;
@@ -737,9 +737,9 @@ void jl_rec_backtrace(jl_task_t *t)
737737
#endif
738738
context = &c;
739739
#elif defined(JL_HAVE_UNW_CONTEXT)
740-
context = &t->ctx;
740+
context = &t->ctx.ctx;
741741
#elif defined(JL_HAVE_UCONTEXT)
742-
context = jl_to_bt_context(&t->ctx);
742+
context = jl_to_bt_context(&t->ctx.ctx);
743743
#else
744744
#endif
745745
if (context)

0 commit comments

Comments
 (0)