From 615f64c32f5a2ab23e0eda762c328cf080b6f2d7 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Wed, 26 Apr 2023 20:46:01 -0400 Subject: [PATCH 1/3] Use NewPM for ASAN/MSAN Co-authored-by: Gabriel Baraldi Co-authored-by: Prem Chintalapudi --- src/aotcompile.cpp | 36 ++++++++++++++++++------------------ src/cgmemmgr.cpp | 4 ++-- src/jitlayers.h | 13 +++++++++++-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp index 391c5d3df46fb..f58746e7097bb 100644 --- a/src/aotcompile.cpp +++ b/src/aotcompile.cpp @@ -1768,15 +1768,15 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level, PM->add(createCFGSimplificationPass(basicSimplifyCFGOptions)); } } -#if defined(_COMPILER_ASAN_ENABLED_) - PM->add(createAddressSanitizerFunctionPass()); -#endif -#if defined(_COMPILER_MSAN_ENABLED_) - PM->add(createMemorySanitizerLegacyPassPass()); -#endif -#if defined(_COMPILER_TSAN_ENABLED_) - PM->add(createThreadSanitizerLegacyPassPass()); -#endif +// #if defined(_COMPILER_ASAN_ENABLED_) +// PM->add(createAddressSanitizerFunctionPass()); +// #endif +// #if defined(_COMPILER_MSAN_ENABLED_) +// PM->add(createMemorySanitizerLegacyPassPass()); +// #endif +// #if defined(_COMPILER_TSAN_ENABLED_) +// PM->add(createThreadSanitizerLegacyPassPass()); +// #endif return; } PM->add(createPropagateJuliaAddrspaces()); @@ -1927,15 +1927,15 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level, } PM->add(createCombineMulAddPass()); PM->add(createDivRemPairsPass()); -#if defined(_COMPILER_ASAN_ENABLED_) - PM->add(createAddressSanitizerFunctionPass()); -#endif -#if defined(_COMPILER_MSAN_ENABLED_) - PM->add(createMemorySanitizerLegacyPassPass()); -#endif -#if defined(_COMPILER_TSAN_ENABLED_) - PM->add(createThreadSanitizerLegacyPassPass()); -#endif +// #if defined(_COMPILER_ASAN_ENABLED_) +// PM->add(createAddressSanitizerFunctionPass()); +// #endif +// #if defined(_COMPILER_MSAN_ENABLED_) +// PM->add(createMemorySanitizerLegacyPassPass()); +// #endif +// #if defined(_COMPILER_TSAN_ENABLED_) +// PM->add(createThreadSanitizerLegacyPassPass()); +// #endif } // An LLVM module pass that just runs all julia passes in order. Useful for diff --git a/src/cgmemmgr.cpp b/src/cgmemmgr.cpp index 9f4d69137c0fd..15d28ff270c55 100644 --- a/src/cgmemmgr.cpp +++ b/src/cgmemmgr.cpp @@ -860,8 +860,8 @@ uint8_t *RTDyldMemoryManagerJL::allocateCodeSection(uintptr_t Size, StringRef SectionName) { // allocating more than one code section can confuse libunwind. -#if !defined(_COMPILER_MSAN_ENABLED_) - // TODO: Figure out why msan needs this. +#if !defined(_COMPILER_MSAN_ENABLED_) && !defined(_COMPILER_ASAN_ENABLED_) + // TODO: Figure out why msan and now asan too need this. assert(!code_allocated); code_allocated = true; #endif diff --git a/src/jitlayers.h b/src/jitlayers.h index 7f07034586c80..a8852d002a064 100644 --- a/src/jitlayers.h +++ b/src/jitlayers.h @@ -42,7 +42,14 @@ // and feature support (e.g. Windows, JITEventListeners for various profilers, // etc.). Thus, we currently only use JITLink where absolutely required, that is, // for Mac/aarch64. -#if defined(_OS_DARWIN_) && defined(_CPU_AARCH64_) || defined(_COMPILER_ASAN_ENABLED_) || defined(JL_FORCE_JITLINK) +// #define JL_FORCE_JITLINK + +#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_) +# define HAS_SANITIZER +#endif +// The sanitizers don't play well with our memory manager + +#if defined(_OS_DARWIN_) && defined(_CPU_AARCH64_) || defined(JL_FORCE_JITLINK) || defined(HAS_SANITIZER) # if JL_LLVM_VERSION < 130000 # pragma message("On aarch64-darwin, LLVM version >= 13 is required for JITLink; fallback suffers from occasional segfaults") # endif @@ -93,7 +100,9 @@ struct OptimizationOptions { // for middle-end IR optimizations. However, we have not qualified the new // pass manager on our optimization pipeline yet, so this remains an optional // define -// #define JL_USE_NEW_PM +#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_TSAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_) +#define JL_USE_NEW_PM +#endif struct NewPM { std::unique_ptr TM; From 329314d4f6083c0719119cf7234a116ae69c165d Mon Sep 17 00:00:00 2001 From: Prem Chintalapudi Date: Thu, 27 Apr 2023 02:00:19 -0400 Subject: [PATCH 2/3] Only use newpm for sanitizers on llvm 15 --- src/aotcompile.cpp | 40 ++++++++++++++++++++++------------------ src/jitlayers.h | 4 ++-- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp index f58746e7097bb..a18d8c8097b7f 100644 --- a/src/aotcompile.cpp +++ b/src/aotcompile.cpp @@ -1768,15 +1768,17 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level, PM->add(createCFGSimplificationPass(basicSimplifyCFGOptions)); } } -// #if defined(_COMPILER_ASAN_ENABLED_) -// PM->add(createAddressSanitizerFunctionPass()); -// #endif -// #if defined(_COMPILER_MSAN_ENABLED_) -// PM->add(createMemorySanitizerLegacyPassPass()); -// #endif -// #if defined(_COMPILER_TSAN_ENABLED_) -// PM->add(createThreadSanitizerLegacyPassPass()); -// #endif +#if JL_LLVM_VERSION < 150000 +#if defined(_COMPILER_ASAN_ENABLED_) + PM->add(createAddressSanitizerFunctionPass()); +#endif +#if defined(_COMPILER_MSAN_ENABLED_) + PM->add(createMemorySanitizerLegacyPassPass()); +#endif +#if defined(_COMPILER_TSAN_ENABLED_) + PM->add(createThreadSanitizerLegacyPassPass()); +#endif +#endif return; } PM->add(createPropagateJuliaAddrspaces()); @@ -1927,15 +1929,17 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level, } PM->add(createCombineMulAddPass()); PM->add(createDivRemPairsPass()); -// #if defined(_COMPILER_ASAN_ENABLED_) -// PM->add(createAddressSanitizerFunctionPass()); -// #endif -// #if defined(_COMPILER_MSAN_ENABLED_) -// PM->add(createMemorySanitizerLegacyPassPass()); -// #endif -// #if defined(_COMPILER_TSAN_ENABLED_) -// PM->add(createThreadSanitizerLegacyPassPass()); -// #endif +#if JL_LLVM_VERSION < 150000 +#if defined(_COMPILER_ASAN_ENABLED_) + PM->add(createAddressSanitizerFunctionPass()); +#endif +#if defined(_COMPILER_MSAN_ENABLED_) + PM->add(createMemorySanitizerLegacyPassPass()); +#endif +#if defined(_COMPILER_TSAN_ENABLED_) + PM->add(createThreadSanitizerLegacyPassPass()); +#endif +#endif } // An LLVM module pass that just runs all julia passes in order. Useful for diff --git a/src/jitlayers.h b/src/jitlayers.h index a8852d002a064..f6e2bfe9fd2af 100644 --- a/src/jitlayers.h +++ b/src/jitlayers.h @@ -44,7 +44,7 @@ // for Mac/aarch64. // #define JL_FORCE_JITLINK -#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_) +#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_) || defined(_COMPILER_TSAN_ENABLED_) # define HAS_SANITIZER #endif // The sanitizers don't play well with our memory manager @@ -100,7 +100,7 @@ struct OptimizationOptions { // for middle-end IR optimizations. However, we have not qualified the new // pass manager on our optimization pipeline yet, so this remains an optional // define -#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_TSAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_) +#if defined(HAS_SANITIZER) && JL_LLVM_VERSION >= 150000 #define JL_USE_NEW_PM #endif From af2d5ac04bc324fb3f232d657378fd45117c3fb0 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 27 Apr 2023 10:43:55 -0400 Subject: [PATCH 3/3] Update src/jitlayers.h --- src/jitlayers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jitlayers.h b/src/jitlayers.h index f6e2bfe9fd2af..f63f3a42842f1 100644 --- a/src/jitlayers.h +++ b/src/jitlayers.h @@ -49,7 +49,7 @@ #endif // The sanitizers don't play well with our memory manager -#if defined(_OS_DARWIN_) && defined(_CPU_AARCH64_) || defined(JL_FORCE_JITLINK) || defined(HAS_SANITIZER) +#if defined(_OS_DARWIN_) && defined(_CPU_AARCH64_) || defined(JL_FORCE_JITLINK) || JL_LLVM_VERSION >= 150000 && defined(HAS_SANITIZER) # if JL_LLVM_VERSION < 130000 # pragma message("On aarch64-darwin, LLVM version >= 13 is required for JITLink; fallback suffers from occasional segfaults") # endif