Skip to content

Commit 5eede8a

Browse files
committed
Enable MSAN support again
Analogous to (and dependent on) #46336, but for msan. The biggest change is a workaround for LLVM's lack for TLS relocation support (I had a fix for that about 7 years ago at https://reviews.llvm.org/D8815, but that was x86_64-linux-gnu only, while this workaround works everywhere - though we should consider resurrecting my patch for performance at some point). The rest is mostly build fixes and plumbing to get the sanitizer flags through to the dependencies.
1 parent 425f6ff commit 5eede8a

20 files changed

+148
-20
lines changed

cli/loader_exe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main(int argc, char * argv[])
3636
{
3737
#endif
3838

39-
#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_TSAN_ENABLED_)
39+
#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_TSAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_)
4040
// ASAN/TSAN do not support RTLD_DEEPBIND
4141
// https://github.com/google/sanitizers/issues/611
4242
putenv("LBT_USE_RTLD_DEEPBIND=0");

deps/blastrampoline.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $(BUILDDIR)/$(BLASTRAMPOLINE_SRC_DIR)/build-configured: $(BUILDDIR)/$(BLASTRAMPO
1111
echo 1 > $@
1212

1313
$(BUILDDIR)/$(BLASTRAMPOLINE_SRC_DIR)/build-compiled: $(BUILDDIR)/$(BLASTRAMPOLINE_SRC_DIR)/build-configured
14-
cd $(dir $@)/src && $(MAKE) $(MAKE_COMMON)
14+
cd $(dir $@)/src && $(MAKE) $(MAKE_COMMON) CC="$(CC) $(SANITIZE_OPTS)"
1515
echo 1 > $@
1616

1717
define BLASTRAMPOLINE_INSTALL

deps/dsfmt.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include $(SRCDIR)/dsfmt.version
33

44
ifneq ($(USE_BINARYBUILDER_DSFMT),1)
55

6-
DSFMT_CFLAGS := $(CFLAGS) -DNDEBUG -DDSFMT_MEXP=19937 $(fPIC) -DDSFMT_DO_NOT_USE_OLD_NAMES -DDSFMT_SHLIB
6+
DSFMT_CFLAGS := $(CFLAGS) -DNDEBUG -DDSFMT_MEXP=19937 $(fPIC) -DDSFMT_DO_NOT_USE_OLD_NAMES -DDSFMT_SHLIB $(SANITIZE_OPTS)
77
DSFMT_CFLAGS += -O3 -finline-functions -fomit-frame-pointer -fno-strict-aliasing \
88
--param max-inline-insns-single=1800 -Wall -std=c99 -shared
99
ifeq ($(ARCH), x86_64)

deps/libsuitesparse.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ SUITESPARSE_LIB := $(LDFLAGS) -L"$(abspath $(BUILDDIR))/SuiteSparse-$(LIBSUITESP
2222
ifeq ($(OS), Darwin)
2323
SUITESPARSE_LIB += $(RPATH_ESCAPED_ORIGIN)
2424
endif
25-
LIBSUITESPARSE_MFLAGS := CC="$(CC)" CXX="$(CXX)" F77="$(FC)" \
25+
LIBSUITESPARSE_MFLAGS := CC="$(CC) $(SANITIZE_OPTS)" CXX="$(CXX) $(SANITIZE_OPTS)" F77="$(FC)" \
2626
AR="$(AR)" RANLIB="$(RANLIB)" \
2727
BLAS="-L$(build_shlibdir) -lblastrampoline" \
2828
LAPACK="-L$(build_shlibdir) -lblastrampoline" \
29-
LDFLAGS="$(SUITESPARSE_LIB)" CFOPENMP="" CUDA=no CUDA_PATH="" \
29+
LDFLAGS="$(SUITESPARSE_LIB) $(SANITIZE_LDFLAGS) -Wl,--warn-unresolved-symbols" CFOPENMP="" CUDA=no CUDA_PATH="" \
3030
UMFPACK_CONFIG="$(UMFPACK_CONFIG)" \
3131
CHOLMOD_CONFIG="$(CHOLMOD_CONFIG)" \
3232
SPQR_CONFIG="$(SPQR_CONFIG)"

deps/llvm.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ endif
147147
ifeq ($(LLVM_SANITIZE),1)
148148
ifeq ($(SANITIZE_MEMORY),1)
149149
LLVM_CFLAGS += -fsanitize=memory -fsanitize-memory-track-origins
150-
LLVM_LDFLAGS += -fsanitize=memory -fsanitize-memory-track-origins
150+
LLVM_LDFLAGS += -fsanitize=memory -fsanitize-memory-track-origins -rpath $(build_shlibdir)
151151
LLVM_CXXFLAGS += -fsanitize=memory -fsanitize-memory-track-origins
152152
LLVM_CMAKE += -DLLVM_USE_SANITIZER="MemoryWithOrigins"
153153
endif

deps/openblas.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ OPENBLAS_GIT_URL := https://github.com/xianyi/OpenBLAS.git
55
OPENBLAS_TAR_URL = https://api.github.com/repos/xianyi/OpenBLAS/tarball/$1
66
$(eval $(call git-external,openblas,OPENBLAS,,,$(BUILDDIR)))
77

8-
OPENBLAS_BUILD_OPTS := CC="$(CC)" FC="$(FC)" LD="$(LD)" RANLIB="$(RANLIB)" TARGET=$(OPENBLAS_TARGET_ARCH) BINARY=$(BINARY)
8+
OPENBLAS_BUILD_OPTS := CC="$(CC) $(SANITIZE_OPTS)" FC="$(FC) $(SANITIZE_OPTS) -L/home/keno/julia-msan/usr/lib" LD="$(LD) $(SANITIZE_LDFLAGS)" RANLIB="$(RANLIB)" BINARY=$(BINARY)
99

1010
# Thread support
1111
ifeq ($(OPENBLAS_USE_THREAD), 1)
@@ -21,9 +21,14 @@ endif
2121
OPENBLAS_BUILD_OPTS += NO_AFFINITY=1
2222

2323
# Build for all architectures - required for distribution
24+
ifeq ($(SANITIZE_MEMORY),1)
25+
OPENBLAS_BUILD_OPTS += TARGET=GENERIC
26+
else
27+
OPENBLAS_BUILD_OPTS += TARGET=$(OPENBLAS_TARGET_ARCH)
2428
ifeq ($(OPENBLAS_DYNAMIC_ARCH), 1)
2529
OPENBLAS_BUILD_OPTS += DYNAMIC_ARCH=1
2630
endif
31+
endif
2732

2833
# 64-bit BLAS interface
2934
ifeq ($(USE_BLAS64), 1)

deps/tools/common.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ endif
1111
ifeq ($(OS),WINNT)
1212
CONFIGURE_COMMON += LDFLAGS="$(LDFLAGS) -Wl,--stack,8388608"
1313
else
14-
CONFIGURE_COMMON += LDFLAGS="$(LDFLAGS) $(RPATH_ESCAPED_ORIGIN)"
14+
CONFIGURE_COMMON += LDFLAGS="$(LDFLAGS) $(RPATH_ESCAPED_ORIGIN) $(SANITIZE_LDFLAGS)"
1515
endif
16-
CONFIGURE_COMMON += F77="$(FC)" CC="$(CC)" CXX="$(CXX)" LD="$(LD)"
16+
CONFIGURE_COMMON += F77="$(FC)" CC="$(CC) $(SANITIZE_OPTS)" CXX="$(CXX) $(SANITIZE_OPTS)" LD="$(LD)"
1717

1818
CMAKE_CC_ARG := $(CC_ARG)
1919
CMAKE_CXX_ARG := $(CXX_ARG)
@@ -31,11 +31,11 @@ endif
3131
# The call to which here is to work around https://cmake.org/Bug/view.php?id=14366
3232
CMAKE_COMMON += -DCMAKE_C_COMPILER="$$(which $(CC_BASE))"
3333
ifneq ($(strip $(CMAKE_CC_ARG)),)
34-
CMAKE_COMMON += -DCMAKE_C_COMPILER_ARG1="$(CMAKE_CC_ARG)"
34+
CMAKE_COMMON += -DCMAKE_C_COMPILER_ARG1="$(CMAKE_CC_ARG) $(SANITIZE_OPTS)"
3535
endif
3636
CMAKE_COMMON += -DCMAKE_CXX_COMPILER="$(CXX_BASE)"
3737
ifneq ($(strip $(CMAKE_CXX_ARG)),)
38-
CMAKE_COMMON += -DCMAKE_CXX_COMPILER_ARG1="$(CMAKE_CXX_ARG)"
38+
CMAKE_COMMON += -DCMAKE_CXX_COMPILER_ARG1="$(CMAKE_CXX_ARG) $(SANITIZE_OPTS)"
3939
endif
4040
CMAKE_COMMON += -DCMAKE_LINKER="$$(which $(LD))" -DCMAKE_AR="$$(which $(AR))" -DCMAKE_RANLIB="$$(which $(RANLIB))"
4141

deps/unwind.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include $(SRCDIR)/unwind.version
33
include $(SRCDIR)/llvmunwind.version
44

55
ifneq ($(USE_BINARYBUILDER_LIBUNWIND),1)
6-
LIBUNWIND_CFLAGS := -U_FORTIFY_SOURCE $(fPIC) -lz
6+
LIBUNWIND_CFLAGS := -U_FORTIFY_SOURCE $(fPIC) -lz $(SANITIZE_OPTS)
77
LIBUNWIND_CPPFLAGS :=
88

99
ifeq ($(USE_SYSTEM_ZLIB),0)

deps/utf8proc.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $(eval $(call git-external,utf8proc,UTF8PROC,,,$(BUILDDIR)))
55

66
UTF8PROC_OBJ_LIB := $(build_libdir)/libutf8proc.a
77
UTF8PROC_OBJ_HEADER := $(build_includedir)/utf8proc.h
8-
UTF8PROC_CFLAGS := -O2
8+
UTF8PROC_CFLAGS := -O2 $(SANITIZE_OPTS)
99
UTF8PROC_MFLAGS := CC="$(CC)" CFLAGS="$(CFLAGS) $(UTF8PROC_CFLAGS)" PICFLAG="$(fPIC)" AR="$(AR)"
1010
UTF8PROC_BUILDDIR := $(BUILDDIR)/$(UTF8PROC_SRC_DIR)
1111

src/aotcompile.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <llvm/Transforms/Scalar.h>
2828
#include <llvm/Transforms/Vectorize.h>
2929
#include <llvm/Transforms/Instrumentation/AddressSanitizer.h>
30+
#include <llvm/Transforms/Instrumentation/MemorySanitizer.h>
3031
#include <llvm/Transforms/Instrumentation/ThreadSanitizer.h>
3132
#include <llvm/Transforms/Scalar/GVN.h>
3233
#include <llvm/Transforms/IPO/AlwaysInliner.h>
@@ -737,7 +738,7 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
737738
PM->add(createAddressSanitizerFunctionPass());
738739
#endif
739740
#if defined(_COMPILER_MSAN_ENABLED_)
740-
PM->add(createMemorySanitizerPass(true));
741+
PM->add(createMemorySanitizerLegacyPassPass());
741742
#endif
742743
#if defined(_COMPILER_TSAN_ENABLED_)
743744
PM->add(createThreadSanitizerLegacyPassPass());
@@ -892,7 +893,7 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
892893
PM->add(createAddressSanitizerFunctionPass());
893894
#endif
894895
#if defined(_COMPILER_MSAN_ENABLED_)
895-
PM->add(createMemorySanitizerPass(true));
896+
PM->add(createMemorySanitizerLegacyPassPass());
896897
#endif
897898
#if defined(_COMPILER_TSAN_ENABLED_)
898899
PM->add(createThreadSanitizerLegacyPassPass());

src/cgmemmgr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,11 @@ uint8_t *RTDyldMemoryManagerJL::allocateCodeSection(uintptr_t Size,
856856
StringRef SectionName)
857857
{
858858
// allocating more than one code section can confuse libunwind.
859+
#if defined(_COMPILER_MSAN_ENABLED_)
860+
// TODO: Figure out why msan needs this.
859861
assert(!code_allocated);
860862
code_allocated = true;
863+
#endif
861864
total_allocated += Size;
862865
if (exe_alloc)
863866
return (uint8_t*)exe_alloc->alloc(Size, Alignment);

src/codegen.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#if defined(_CPU_X86_)
77
#define JL_NEED_FLOATTEMP_VAR 1
88
#endif
9-
#if defined(_OS_WINDOWS_) || defined(_OS_FREEBSD_)
9+
#if defined(_OS_WINDOWS_) || defined(_OS_FREEBSD_) || defined(_COMPILER_MSAN_ENABLED_)
1010
#define JL_DISABLE_FPO
1111
#endif
1212

@@ -2120,6 +2120,9 @@ static void jl_init_function(Function *F)
21202120
#if defined(_COMPILER_ASAN_ENABLED_)
21212121
attr.addAttribute(Attribute::SanitizeAddress);
21222122
#endif
2123+
#if defined(_COMPILER_MSAN_ENABLED_)
2124+
attr.addAttribute(Attribute::SanitizeMemory);
2125+
#endif
21232126
#if JL_LLVM_VERSION >= 140000
21242127
F->addFnAttrs(attr);
21252128
#else

src/debuginfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,15 @@ bool jl_dylib_DI_for_fptr(size_t pointer, object::SectionRef *Section, int64_t *
10671067
#ifdef __GLIBC__
10681068
struct link_map *extra_info;
10691069
dladdr_success = dladdr1((void*)pointer, &dlinfo, (void**)&extra_info, RTLD_DL_LINKMAP) != 0;
1070+
if (dladdr_success) {
1071+
msan_unpoison(&dlinfo, sizeof(dlinfo));
1072+
if (dlinfo.dli_fname)
1073+
msan_unpoison_string(dlinfo.dli_fname);
1074+
if (dlinfo.dli_sname)
1075+
msan_unpoison_string(dlinfo.dli_sname);
1076+
msan_unpoison(&extra_info, sizeof(struct link_map*));
1077+
msan_unpoison(extra_info, sizeof(struct link_map));
1078+
}
10701079
#else
10711080
#ifdef _OS_DARWIN_
10721081
// On macOS 12, dladdr(-1, …) succeeds and returns the main executable image,

src/dlload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static inline uintptr_t RoundUpTo(uintptr_t size, uintptr_t boundary) {
115115
static inline uintptr_t RoundDownTo(uintptr_t x, uintptr_t boundary) {
116116
return x & ~(boundary - 1);
117117
}
118-
void ForEachMappedRegion(struct link_map *map, void (*cb)(const void *, uintptr_t)) {
118+
void ForEachMappedRegion(struct link_map *map, void (*cb)(const volatile void *, uintptr_t)) {
119119
#if !defined(_OS_FREEBSD_)
120120
typedef ElfW(Phdr) Elf_Phdr;
121121
typedef ElfW(Ehdr) Elf_Ehdr;
@@ -207,7 +207,7 @@ JL_DLLEXPORT JL_NO_SANITIZE void *jl_dlopen(const char *filename, unsigned flags
207207
#endif
208208
);
209209
#if defined(_COMPILER_MSAN_ENABLED_) && defined(__GLIBC__)
210-
link_map *map = (link_map*)handle;
210+
struct link_map *map = (struct link_map*)hnd;
211211
if (filename && map)
212212
ForEachMappedRegion(map, __msan_unpoison);
213213
#endif

src/gc-pages.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ void jl_gc_free_page(void *p) JL_NOTSAFEPOINT
316316
#else
317317
madvise(p, decommit_size, MADV_DONTNEED);
318318
#endif
319+
/* TODO: Should we leave this poisoned and rather allow the GC to read poisoned pointers from
320+
* the page when it sweeps pools?
321+
*/
322+
msan_unpoison(p, decommit_size);
319323

320324
no_decommit:
321325
// new pages are now available starting at max of lb and pagetable_i32

src/gc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ static inline jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
12751275
pg->nfree = 0;
12761276
pg->has_young = 1;
12771277
}
1278+
msan_allocated_memory(v, osize);
12781279
return jl_valueof(v);
12791280
}
12801281
// if the freelist is empty we reuse empty but not freed pages
@@ -1299,6 +1300,7 @@ static inline jl_value_t *jl_gc_pool_alloc_inner(jl_ptls_t ptls, int pool_offset
12991300
next = (jl_taggedvalue_t*)((char*)v + osize);
13001301
}
13011302
p->newpages = next;
1303+
msan_allocated_memory(v, osize);
13021304
return jl_valueof(v);
13031305
}
13041306

src/jitlayers.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,58 @@ using namespace llvm;
5252
# include <llvm/ExecutionEngine/SectionMemoryManager.h>
5353
#endif
5454

55+
#ifdef _COMPILER_MSAN_ENABLED_
56+
// TODO: This should not be necessary on ELF x86_64, but LLVM's implementation
57+
// of the TLS relocations is currently broken, so enable this unconditionally.
58+
#define MSAN_EMUTLS_WORKAROUND 1
59+
60+
// See https://github.com/google/sanitizers/wiki/MemorySanitizerJIT
61+
namespace msan_workaround {
62+
63+
extern "C" {
64+
extern __thread unsigned long long __msan_param_tls[];
65+
extern __thread unsigned int __msan_param_origin_tls[];
66+
extern __thread unsigned long long __msan_retval_tls[];
67+
extern __thread unsigned int __msan_retval_origin_tls;
68+
extern __thread unsigned long long __msan_va_arg_tls[];
69+
extern __thread unsigned int __msan_va_arg_origin_tls[];
70+
extern __thread unsigned long long __msan_va_arg_overflow_size_tls;
71+
extern __thread unsigned int __msan_origin_tls;
72+
}
73+
74+
enum class MSanTLS
75+
{
76+
param = 1, // __msan_param_tls
77+
param_origin, //__msan_param_origin_tls
78+
retval, // __msan_retval_tls
79+
retval_origin, //__msan_retval_origin_tls
80+
va_arg, // __msan_va_arg_tls
81+
va_arg_origin, // __msan_va_arg_origin_tls
82+
va_arg_overflow_size, // __msan_va_arg_overflow_size_tls
83+
origin, //__msan_origin_tls
84+
};
85+
86+
static void *getTLSAddress(void *control)
87+
{
88+
auto tlsIndex = static_cast<MSanTLS>(reinterpret_cast<uintptr_t>(control));
89+
switch(tlsIndex)
90+
{
91+
case MSanTLS::param: return reinterpret_cast<void *>(&__msan_param_tls);
92+
case MSanTLS::param_origin: return reinterpret_cast<void *>(&__msan_param_origin_tls);
93+
case MSanTLS::retval: return reinterpret_cast<void *>(&__msan_retval_tls);
94+
case MSanTLS::retval_origin: return reinterpret_cast<void *>(&__msan_retval_origin_tls);
95+
case MSanTLS::va_arg: return reinterpret_cast<void *>(&__msan_va_arg_tls);
96+
case MSanTLS::va_arg_origin: return reinterpret_cast<void *>(&__msan_va_arg_origin_tls);
97+
case MSanTLS::va_arg_overflow_size: return reinterpret_cast<void *>(&__msan_va_arg_overflow_size_tls);
98+
case MSanTLS::origin: return reinterpret_cast<void *>(&__msan_origin_tls);
99+
default:
100+
assert(false && "BAD MSAN TLS INDEX");
101+
return nullptr;
102+
}
103+
}
104+
}
105+
#endif
106+
55107
#define DEBUG_TYPE "jitlayers"
56108

57109
// Snooping on which functions are being compiled, and how long it takes
@@ -823,6 +875,11 @@ namespace {
823875
// LLVM defaults to tls stack guard, which causes issues with Julia's tls implementation
824876
options.StackProtectorGuard = StackProtectorGuards::Global;
825877
#endif
878+
#if defined(MSAN_EMUTLS_WORKAROUND)
879+
options.EmulatedTLS = true;
880+
options.ExplicitEmulatedTLS = true;
881+
#endif
882+
826883
Triple TheTriple(sys::getProcessTriple());
827884
#if defined(FORCE_ELF)
828885
TheTriple.setObjectFormat(Triple::ELF);
@@ -1135,6 +1192,28 @@ JuliaOJIT::JuliaOJIT()
11351192
{ mangle("__truncdfhf2"), { mangle("julia__truncdfhf2"), JITSymbolFlags::Exported } }
11361193
};
11371194
cantFail(GlobalJD.define(orc::symbolAliases(jl_crt)));
1195+
1196+
#ifdef MSAN_EMUTLS_WORKAROUND
1197+
orc::SymbolMap msan_crt;
1198+
msan_crt[mangle("__emutls_get_address")] = JITEvaluatedSymbol::fromPointer(msan_workaround::getTLSAddress, JITSymbolFlags::Exported);
1199+
msan_crt[mangle("__emutls_v.__msan_param_tls")] = JITEvaluatedSymbol::fromPointer(
1200+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::param)), JITSymbolFlags::Exported);
1201+
msan_crt[mangle("__emutls_v.__msan_param_origin_tls")] = JITEvaluatedSymbol::fromPointer(
1202+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::param_origin)), JITSymbolFlags::Exported);
1203+
msan_crt[mangle("__emutls_v.__msan_retval_tls")] = JITEvaluatedSymbol::fromPointer(
1204+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::retval)), JITSymbolFlags::Exported);
1205+
msan_crt[mangle("__emutls_v.__msan_retval_origin_tls")] = JITEvaluatedSymbol::fromPointer(
1206+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::retval_origin)), JITSymbolFlags::Exported);
1207+
msan_crt[mangle("__emutls_v.__msan_va_arg_tls")] = JITEvaluatedSymbol::fromPointer(
1208+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::va_arg)), JITSymbolFlags::Exported);
1209+
msan_crt[mangle("__emutls_v.__msan_va_arg_origin_tls")] = JITEvaluatedSymbol::fromPointer(
1210+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::va_arg_origin)), JITSymbolFlags::Exported);
1211+
msan_crt[mangle("__emutls_v.__msan_va_arg_overflow_size_tls")] = JITEvaluatedSymbol::fromPointer(
1212+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::va_arg_overflow_size)), JITSymbolFlags::Exported);
1213+
msan_crt[mangle("__emutls_v.__msan_origin_tls")] = JITEvaluatedSymbol::fromPointer(
1214+
reinterpret_cast<void *>(static_cast<uintptr_t>(msan_workaround::MSanTLS::origin)), JITSymbolFlags::Exported);
1215+
cantFail(GlobalJD.define(orc::absoluteSymbols(msan_crt)));
1216+
#endif
11381217
}
11391218

11401219
orc::SymbolStringPtr JuliaOJIT::mangle(StringRef Name)

src/julia_internal.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ static inline void asan_unpoison_stack_memory(uintptr_t addr, size_t size) {
6565
static inline void asan_unpoison_task_stack(jl_task_t *ct, jl_jmp_buf *buf) JL_NOTSAFEPOINT {}
6666
static inline void asan_unpoison_stack_memory(uintptr_t addr, size_t size) JL_NOTSAFEPOINT {}
6767
#endif
68+
#ifdef _COMPILER_MSAN_ENABLED_
69+
void __msan_unpoison(const volatile void *a, size_t size);
70+
void __msan_allocated_memory(const volatile void *a, size_t size);
71+
void __msan_unpoison_string(const volatile char *a);
72+
static inline void msan_allocated_memory(const volatile void *a, size_t size) {
73+
__msan_allocated_memory(a, size);
74+
}
75+
static inline void msan_unpoison(const volatile void *a, size_t size) {
76+
__msan_unpoison(a, size);
77+
}
78+
static inline void msan_unpoison_string(const volatile char *a) {
79+
__msan_unpoison_string(a);
80+
}
81+
#else
82+
static inline void msan_unpoison(const volatile void *a, size_t size) {}
83+
static inline void msan_allocated_memory(const volatile void *a, size_t size) {}
84+
static inline void msan_unpoison_string(const volatile char *a) {}
85+
#endif
6886
#ifdef _COMPILER_TSAN_ENABLED_
6987
void *__tsan_create_fiber(unsigned flags);
7088
void *__tsan_get_current_fiber(void);

src/options.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@
166166
// sanitizer defaults ---------------------------------------------------------
167167

168168
// Automatically enable MEMDEBUG and KEEP_BODIES for the sanitizers
169-
#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_)
169+
#if defined(_COMPILER_ASAN_ENABLED_)
170+
// No MEMDEBUG for msan - we just poison allocated memory directly.
170171
#define MEMDEBUG
172+
#endif
173+
174+
#if defined(_COMPILER_ASAN_ENABLED_) || defined(_COMPILER_MSAN_ENABLED_)
171175
#define KEEP_BODIES
172176
#endif
173177

src/support/platform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#if __has_feature(memory_sanitizer)
5757
#define _COMPILER_MSAN_ENABLED_
5858
#undef JL_NO_MSAN
59-
#define JL_NO_MSAN __attribute__((no_sanitize("mempry")))
59+
#define JL_NO_MSAN __attribute__((no_sanitize("memory")))
6060
#endif
6161
#if __has_feature(thread_sanitizer)
6262
#if __clang_major__ < 11

0 commit comments

Comments
 (0)