Skip to content

Commit

Permalink
ASAN fixes
Browse files Browse the repository at this point in the history
Use same trick as dlopen to support siglongjmp bypass.
  • Loading branch information
vtjnash committed Nov 16, 2023
1 parent 7c207de commit 6c76f31
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2285,15 +2285,9 @@ void (ijl_longjmp)(jmp_buf _Buf, int _Value);
#define jl_setjmp_name "sigsetjmp"
#endif
#define jl_setjmp(a,b) sigsetjmp(a,b)
#if defined(_COMPILER_ASAN_ENABLED_) && __GLIBC__
// Bypass the ASAN longjmp wrapper - we're unpoisoning the stack ourselves.
#if !__GLIBC_PREREQ(2, 34)
JL_DLLIMPORT int __attribute__ ((nothrow)) (__libc_siglongjmp)(jl_jmp_buf buf, int val);
#define jl_longjmp(a,b) __libc_siglongjmp(a,b)
#else
// This broke with glibc 2.34, where the __libc_siglongjmp symbol was removed
#define jl_longjmp(a,b) siglongjmp(a,b)
#endif
#if defined(_COMPILER_ASAN_ENABLED_) && defined(__GLIBC__)
extern void (*real_siglongjmp)(jmp_buf _Buf, int _Value);
#define jl_longjmp(a,b) real_siglongjmp(a,b)
#else
#define jl_longjmp(a,b) siglongjmp(a,b)
#endif
Expand Down
18 changes: 18 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ extern "C" {
#endif

#if defined(_COMPILER_ASAN_ENABLED_)
#if __GLIBC__
#include <dlfcn.h>
// Bypass the ASAN longjmp wrapper - we are unpoisoning the stack ourselves,
// since ASAN normally unpoisons far too much.
// c.f. interceptor in jl_dlopen as well
void (*real_siglongjmp)(jmp_buf _Buf, int _Value) = NULL;
#endif
static inline void sanitizer_start_switch_fiber(jl_ptls_t ptls, jl_task_t *from, jl_task_t *to) {
if (to->copy_stack)
__sanitizer_start_switch_fiber(&from->ctx.asan_fake_stack, (char*)ptls->stackbase-ptls->stacksize, ptls->stacksize);
Expand Down Expand Up @@ -1226,6 +1233,17 @@ void jl_init_tasks(void) JL_GC_DISABLED
exit(1);
}
#endif
#if defined(_COMPILER_ASAN_ENABLED_) && __GLIBC__
void *libc_handle = dlopen("libc.so.6", RTLD_NOW | RTLD_NOLOAD);
if (libc_handle) {
*(void**)&real_siglongjmp = dlsym(libc_handle, "siglongjmp");
dlclose(libc_handle);
}
if (real_siglongjmp == NULL) {
jl_safe_printf("failed to get real siglongjmp\n");
exit(1);
}
#endif
}

#if defined(_COMPILER_ASAN_ENABLED_)
Expand Down

0 comments on commit 6c76f31

Please sign in to comment.