Skip to content

Commit beb3ab4

Browse files
authored
Enable MSAN support again (#46348)
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 cbde379 commit beb3ab4

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)
@@ -30,11 +30,11 @@ endif
3030
# The call to which here is to work around https://cmake.org/Bug/view.php?id=14366
3131
CMAKE_COMMON += -DCMAKE_C_COMPILER="$$(which $(CC_BASE))"
3232
ifneq ($(strip $(CMAKE_CC_ARG)),)
33-
CMAKE_COMMON += -DCMAKE_C_COMPILER_ARG1="$(CMAKE_CC_ARG)"
33+
CMAKE_COMMON += -DCMAKE_C_COMPILER_ARG1="$(CMAKE_CC_ARG) $(SANITIZE_OPTS)"
3434
endif
3535
CMAKE_COMMON += -DCMAKE_CXX_COMPILER="$(CXX_BASE)"
3636
ifneq ($(strip $(CMAKE_CXX_ARG)),)
37-
CMAKE_COMMON += -DCMAKE_CXX_COMPILER_ARG1="$(CMAKE_CXX_ARG)"
37+
CMAKE_COMMON += -DCMAKE_CXX_COMPILER_ARG1="$(CMAKE_CXX_ARG) $(SANITIZE_OPTS)"
3838
endif
3939
CMAKE_COMMON += -DCMAKE_LINKER="$$(which $(LD))" -DCMAKE_AR="$$(which $(AR))" -DCMAKE_RANLIB="$$(which $(RANLIB))"
4040

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());

0 commit comments

Comments
 (0)