Skip to content

Commit 8ece865

Browse files
maleadttkf
andauthored
ASAN improvements (#41675)
* Move sanitizer compiler definitions to platform.h. * Define ASAN defaults in the loader executable. * Default to no RTLD_DEEPBIND for LBT when using ASAN. * Simplify contrib/asan/ Co-authored-by: Takafumi Arakaki <[email protected]>
1 parent 5f4c719 commit 8ece865

File tree

18 files changed

+70
-75
lines changed

18 files changed

+70
-75
lines changed

cli/loader_exe.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ extern "C" {
1313

1414
JULIA_DEFINE_FAST_TLS
1515

16+
#ifdef _COMPILER_ASAN_ENABLED_
17+
JL_DLLEXPORT const char* __asan_default_options()
18+
{
19+
return "allow_user_segv_handler=1:detect_leaks=0";
20+
// FIXME: enable LSAN after fixing leaks & defining __lsan_default_suppressions(),
21+
// or defining __lsan_default_options = exitcode=0 once publicly available
22+
// (here and in flisp/flmain.c)
23+
}
24+
#endif
25+
1626
#ifdef _OS_WINDOWS_
1727
int mainCRTStartup(void)
1828
{
@@ -25,6 +35,12 @@ int main(int argc, char * argv[])
2535
{
2636
#endif
2737

38+
#ifdef _COMPILER_ASAN_ENABLED_
39+
// ASAN does not support RTLD_DEEPBIND
40+
// https://github.com/google/sanitizers/issues/611
41+
putenv("LBT_USE_RTLD_DEEPBIND=0");
42+
#endif
43+
2844
// Convert Windows wchar_t values to UTF8
2945
#ifdef _OS_WINDOWS_
3046
for (int i = 0; i < argc; i++) {

contrib/asan/Make.user.asan

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,3 @@ override JULIA_BUILD_MODE=debug
2121

2222
# make ASAN consume less memory
2323
export ASAN_OPTIONS=detect_leaks=0:fast_unwind_on_malloc=0:allow_user_segv_handler=1:malloc_context_size=2
24-
25-
# tell libblastrampoline to not use RTLD_DEEPBIND
26-
export LBT_USE_RTLD_DEEPBIND=0

contrib/asan/check.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ function main(args = ARGS)::Int
3535
timeout = Threads.Atomic{Bool}(false)
3636
isstarted = false
3737
mktemp() do tmppath, tmpio
38-
cmd = addenv(
39-
`$julia -e $code $tmppath`,
40-
"ASAN_OPTIONS" =>
41-
"detect_leaks=0:fast_unwind_on_malloc=0:allow_user_segv_handler=1:malloc_context_size=2",
42-
"LBT_USE_RTLD_DEEPBIND" => "0",
43-
)
38+
cmd = `$julia -e $code $tmppath`
4439
# Note: Ideally, we set ASAN_SYMBOLIZER_PATH here. But there is no easy
4540
# way to find out the path from just a Julia binary.
4641

src/aotcompile.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -664,13 +664,13 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
664664
PM->add(createLowerSimdLoopPass()); // Annotate loop marked with "loopinfo" as LLVM parallel loop
665665
if (dump_native)
666666
PM->add(createMultiVersioningPass());
667-
#if defined(JL_ASAN_ENABLED)
667+
#if defined(_COMPILER_ASAN_ENABLED_)
668668
PM->add(createAddressSanitizerFunctionPass());
669669
#endif
670-
#if defined(JL_MSAN_ENABLED)
670+
#if defined(_COMPILER_MSAN_ENABLED_)
671671
PM->add(createMemorySanitizerPass(true));
672672
#endif
673-
#if defined(JL_TSAN_ENABLED)
673+
#if defined(_COMPILER_TSAN_ENABLED_)
674674
PM->add(createThreadSanitizerLegacyPassPass());
675675
#endif
676676
return;
@@ -813,13 +813,13 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
813813
}
814814
PM->add(createCombineMulAddPass());
815815
PM->add(createDivRemPairsPass());
816-
#if defined(JL_ASAN_ENABLED)
816+
#if defined(_COMPILER_ASAN_ENABLED_)
817817
PM->add(createAddressSanitizerFunctionPass());
818818
#endif
819-
#if defined(JL_MSAN_ENABLED)
819+
#if defined(_COMPILER_MSAN_ENABLED_)
820820
PM->add(createMemorySanitizerPass(true));
821821
#endif
822-
#if defined(JL_TSAN_ENABLED)
822+
#if defined(_COMPILER_TSAN_ENABLED_)
823823
PM->add(createThreadSanitizerLegacyPassPass());
824824
#endif
825825
}

src/atomics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ enum jl_memory_order {
102102
__atomic_load_n(obj, __ATOMIC_SEQ_CST)
103103
# define jl_atomic_load_acquire(obj) \
104104
__atomic_load_n(obj, __ATOMIC_ACQUIRE)
105-
#ifdef JL_TSAN_ENABLED
105+
#ifdef _COMPILER_TSAN_ENABLED_
106106
// For the sake of tsan, call these loads consume ordering since they will act
107107
// as such on the processors we support while normally, the compiler would
108108
// upgrade this to acquire ordering, which is strong (and slower) than we want.

src/codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ static void jl_init_function(Function *F)
17761776
#ifdef JL_DISABLE_FPO
17771777
F->addFnAttr("frame-pointer", "all");
17781778
#endif
1779-
#if !defined(JL_ASAN_ENABLED) && !defined(_OS_WINDOWS_)
1779+
#if !defined(_COMPILER_ASAN_ENABLED_) && !defined(_OS_WINDOWS_)
17801780
// ASAN won't like us accessing undefined memory causing spurious issues,
17811781
// and Windows has platform-specific handling which causes it to mishandle
17821782
// this annotation. Other platforms should just ignore this if they don't
@@ -6343,7 +6343,7 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
63436343
f->addFnAttr(Attribute::StackProtectStrong);
63446344
#endif
63456345

6346-
#ifdef JL_TSAN_ENABLED
6346+
#ifdef _COMPILER_TSAN_ENABLED_
63476347
// TODO: enable this only when a argument like `-race` is passed to Julia
63486348
// add a macro for no_sanitize_thread
63496349
f->addFnAttr(llvm::Attribute::SanitizeThread);

src/dlload.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ JL_DLLEXPORT void *jl_dlopen(const char *filename, unsigned flags) JL_NOTSAFEPOI
130130
#ifdef RTLD_NOLOAD
131131
| JL_RTLD(flags, NOLOAD)
132132
#endif
133-
#if defined(RTLD_DEEPBIND) && !(defined(JL_ASAN_ENABLED) || defined(JL_TSAN_ENABLED) || defined(JL_MSAN_ENABLED))
133+
#if defined(RTLD_DEEPBIND) && !(defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_TSAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_))
134134
| JL_RTLD(flags, DEEPBIND)
135135
#endif
136136
#ifdef RTLD_FIRST

src/gc-stacks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void sweep_stack_pools(void)
233233
t->stkbuf = NULL;
234234
_jl_free_stack(ptls2, stkbuf, bufsz);
235235
}
236-
#ifdef JL_TSAN_ENABLED
236+
#ifdef _COMPILER_TSAN_ENABLED_
237237
if (t->ctx.tsan_state) {
238238
__tsan_destroy_fiber(t->ctx.tsan_state);
239239
t->ctx.tsan_state = NULL;

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void jl_init_stack_limits(int ismaster, void **stack_lo, void **stack_hi)
115115
static void jl_prep_sanitizers(void)
116116
{
117117
#if !defined(_OS_WINDOWS_)
118-
#if defined(JL_ASAN_ENABLED) || defined(JL_MSAN_ENABLED)
118+
#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_)
119119
struct rlimit rl;
120120

121121
// When using the sanitizers, increase stack size because they bloat

src/jlapi.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,6 @@ JL_DLLEXPORT int jl_set_fenv_rounding(int i)
501501
return fesetround(i);
502502
}
503503

504-
505-
#ifdef JL_ASAN_ENABLED
506-
JL_DLLEXPORT const char* __asan_default_options()
507-
{
508-
return "allow_user_segv_handler=1:detect_leaks=0";
509-
// FIXME: enable LSAN after fixing leaks & defining __lsan_default_suppressions(),
510-
// or defining __lsan_default_options = exitcode=0 once publicly available
511-
// (here and in flisp/flmain.c)
512-
}
513-
#endif
514-
515504
static int exec_program(char *program)
516505
{
517506
JL_TRY {

0 commit comments

Comments
 (0)