Skip to content

Commit

Permalink
Memory sanitizer support revisited
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Apr 10, 2015
1 parent 3ffbe90 commit 605534f
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 28 deletions.
23 changes: 19 additions & 4 deletions Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,16 @@ include $(JULIAHOME)/Make.user
endif

ifeq ($(SANITIZE),1)
JCXXFLAGS += -fsanitize=address -mllvm -asan-stack=0
JCFLAGS += -fsanitize=address -mllvm -asan-stack=0
LDFLAGS += -fsanitize=address
ifeq ($(SANITIZE_MEMORY),1)
SANITIZE_OPTS = -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer
else
SANITIZE_OPTS = -fsanitize=address -mllvm -asan-stack=0
endif
JCXXFLAGS += $(SANITIZE_OPTS)
JCFLAGS += $(SANITIZE_OPTS)
LDFLAGS += $(SANITIZE_OPTS)
DEPS_CFLAGS += $(SANITIZE_OPTS)
DEPS_CXXFLAGS += $(SANITIZE_OPTS)
endif

TAR=`which gtar 2>/dev/null || which tar 2>/dev/null`
Expand Down Expand Up @@ -668,7 +675,14 @@ else ifneq ($(USEMSVC), 1)
endif

ifeq ($(OS), Linux)
OSLIBS += -ldl -lrt -lpthread -Wl,--export-dynamic -Wl,--version-script=$(JULIAHOME)/src/julia.expmap -Wl,--no-whole-archive $(LIBUNWIND)
OSLIBS += -ldl -lrt -lpthread -Wl,--export-dynamic -Wl,--no-whole-archive $(LIBUNWIND)
ifneq ($(SANITIZE),1)
ifneq ($(SANITIZE_MEMORY),1)
ifneq ($(LLVM_SANITIZE),1)
OSLIBS += -Wl,--version-script=$(JULIAHOME)/src/julia.expmap
endif
endif
endif
JLDFLAGS = -Wl,-Bdynamic
ifeq (-Bsymbolic-functions, $(shell $(LD) --help | grep -o -e "-Bsymbolic-functions"))
JLIBLDFLAGS = -Wl,-Bsymbolic-functions
Expand Down Expand Up @@ -792,6 +806,7 @@ ifeq ($(BUILD_CUSTOM_LIBCXX),1)
LDFLAGS += -L$(build_libdir) -lc++abi
CXXLDFLAGS += -L$(build_libdir) -lc++abi -stdlib=libc++ -lc++
CPPFLAGS += -I$(build_includedir)/c++/v1
CUSTOM_LD_LIBRARY_PATH = LD_LIBRARY_PATH="$(build_libdir)"
ifeq ($(USEICC),1)
CXXFLAGS += -cxxlib-nostd -static-intel
CLDFLAGS += -static-intel
Expand Down
1 change: 1 addition & 0 deletions deps/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/libosxunwind-*
/lighttpd-*
/libcxx-*
/libcxxabi-*
/libffi-*
/libgit*
/llvm-*
Expand Down
52 changes: 38 additions & 14 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include $(JULIAHOME)/Make.inc

## Some shared configuration options ##

CONFIGURE_COMMON = --prefix=$(abspath $(build_prefix)) --build=$(BUILD_MACHINE) --libdir=$(abspath $(build_libdir))
CONFIGURE_COMMON = --prefix=$(abspath $(build_prefix)) --build=$(BUILD_MACHINE) --libdir=$(abspath $(build_libdir)) $(CUSTOM_LD_LIBRARY_PATH)
ifneq ($(XC_HOST),)
CONFIGURE_COMMON += --host=$(XC_HOST)
endif
Expand All @@ -33,19 +33,22 @@ ifneq ($(USEMSVC), 1)
CONFIGURE_COMMON += LDFLAGS=-Wl,--stack,8388608
endif
endif
CONFIGURE_COMMON += F77="$(FC)" CC="$(CC)" CXX="$(CXX)"
CONFIGURE_COMMON += F77="$(FC)" CC="$(CC) $(DEPS_CFLAGS)" CXX="$(CXX) $(DEPS_CXXFLAGS)"

CMAKE_CC_ARG = $(CC_ARG) $(DEPS_CFLAGS)
CMAKE_CXX_ARG = $(CXX_ARG) $(DEPS_CXXFLAGS)

CMAKE_COMMON = -DCMAKE_INSTALL_PREFIX:PATH=$(build_prefix) -DCMAKE_BUILD_TYPE=Release
ifneq ($(VERBOSE), 0)
CMAKE_COMMON += -DCMAKE_VERBOSE_MAKEFILE=ON
endif
CMAKE_COMMON += -DCMAKE_C_COMPILER="$(CC_BASE)"
ifdef CC_ARG
CMAKE_COMMON += -DCMAKE_C_COMPILER_ARG1="$(CC_ARG)"
ifneq ($(strip $(CMAKE_CC_ARG)),)
CMAKE_COMMON += -DCMAKE_C_COMPILER_ARG1="$(CMAKE_CC_ARG)"
endif
CMAKE_COMMON += -DCMAKE_CXX_COMPILER="$(CXX_BASE)"
ifdef CXX_ARG
CMAKE_COMMON += -DCMAKE_CXX_COMPILER_ARG1="$(CXX_ARG)"
ifneq ($(strip $(CMAKE_CXX_ARG)),)
CMAKE_COMMON += -DCMAKE_CXX_COMPILER_ARG1="$(CMAKE_CXX_ARG)"
endif

# If the top-level Makefile is called with environment variables,
Expand Down Expand Up @@ -133,7 +136,7 @@ endif
ifeq ($(USE_SYSTEM_MPFR), 0)
STAGE2_DEPS += mpfr
ifeq ($(USE_SYSTEM_GMP), 0)
MPFR_OPTS = --with-gmp-include=$(abspath $(build_includedir)) --with-gmp-lib=$(abspath $(build_shlibdir))
MPFR_OPTS = --with-gmp-include=$(abspath $(build_includedir)) --with-gmp-lib=$(abspath $(build_shlibdir)) CFLAGS="-O0 -g"
endif
endif
ifeq ($(BUILD_OS),WINNT)
Expand Down Expand Up @@ -232,7 +235,11 @@ LLVM_BUILDTYPE := $(LLVM_BUILDTYPE)+Asserts
endif
LLVM_FLAVOR := $(LLVM_BUILDTYPE)
ifeq ($(LLVM_SANITIZE),1)
LLVM_BUILDTYPE := $(LLVM_BUILDTYPE)+Sanitize
ifeq ($(SANITIZE_MEMORY),1)
LLVM_BUILDTYPE := $(LLVM_BUILDTYPE)+MSAN
else
LLVM_BUILDTYPE := $(LLVM_BUILDTYPE)+ASAN
endif
endif

LLVM_LIB_FILE = libLLVMCodeGen.a
Expand Down Expand Up @@ -331,9 +338,17 @@ endif # ARCH == ppc64


ifeq ($(LLVM_SANITIZE),1)
ifeq ($(SANITIZE_MEMORY),1)
LLVM_CC = CFLAGS="-fsanitize=memory -fsanitize-memory-track-origins"
LLVM_LDFLAGS += -fsanitize=memory -fsanitize-memory-track-origins
LLVM_CXXFLAGS += -fsanitize=memory -fsanitize-memory-track-origins
LLVM_CMAKE += -DLLVM_USE_SANITIZER="MemoryWithOrigins"
else
LLVM_CC = CFLAGS="-fsanitize=address"
LLVM_LDFLAGS += -fsanitize=address
LLVM_CXXFLAGS += -fsanitize=address
LLVM_CMAKE += -DLLVM_USE_SANITIZER="Address"
endif
LLVM_MFLAGS += TOOL_NO_EXPORTS= HAVE_LINK_VERSION_SCRIPT=0
else
LLVM_CC =
Expand All @@ -346,9 +361,9 @@ endif # LLVM_CXXFLAGS
LLVM_MFLAGS += $(LLVM_CC)

ifeq ($(BUILD_CUSTOM_LIBCXX),1)
LLVM_LDFLAGS += -Wl,-R$(build_libdir)
LLVM_LDFLAGS += -Wl,-R$(build_libdir) -lc++ -lc++abi
ifeq ($(USEICC),1)
LLVM_LDFLAGS += -no_cpprt -lc++ -lc++abi
LLVM_LDFLAGS += -no_cpprt
endif # USEICC
endif # BUILD_CUSTOM_LIBCXX

Expand Down Expand Up @@ -425,12 +440,12 @@ libcxx-build:
mkdir -p libcxx-build
libcxx-build/Makefile: llvm-$(LLVM_VER)/projects/libcxx | llvm-$(LLVM_VER)/projects/libcxxabi libcxx-build
cd libcxx-build && \
$(CMAKE) -G "Unix Makefiles" $(CMAKE_COMMON) -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_LIBCXXABI_INCLUDE_PATHS="../llvm-$(LLVM_VER)/projects/libcxxabi/include" ../llvm-$(LLVM_VER)/projects/libcxx -DCMAKE_SHARED_LINKER_FLAGS="-L$(build_libdir) $(LIBCXX_EXTRA_FLAGS)" -DCMAKE_CXX_FLAGS="$(CXXFLAGS)"
$(CMAKE) -G "Unix Makefiles" $(CMAKE_COMMON) $(LLVM_CMAKE) -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_CXX_ABI_INCLUDE_PATHS="../llvm-$(LLVM_VER)/projects/libcxxabi/include" ../llvm-$(LLVM_VER)/projects/libcxx -DCMAKE_SHARED_LINKER_FLAGS="-L$(build_libdir) $(LIBCXX_EXTRA_FLAGS)" -DCMAKE_CXX_FLAGS="$(CXXFLAGS)"
libcxxabi-build:
mkdir -p libcxxabi-build
libcxxabi-build/Makefile: llvm-$(LLVM_VER)/projects/libcxxabi | llvm-$(LLVM_VER)/projects/libcxx libcxxabi-build
cd libcxxabi-build && \
$(CMAKE) -G "Unix Makefiles" $(CMAKE_COMMON) -DLLVM_PATH="../llvm-$(LLVM_VER)" ../llvm-$(LLVM_VER)/projects/libcxxabi -DLIBCXXABI_CXX_ABI_LIBRARIES="$(LIBCXX_EXTRA_FLAGS)" -DCMAKE_CXX_FLAGS="$(CXXFLAGS) -std=c++11"
$(CMAKE) -G "Unix Makefiles" $(CMAKE_COMMON) $(LLVM_CMAKE) -DLLVM_ABI_BREAKING_CHECKS="WITH_ASSERTS" -DLLVM_PATH="../llvm-$(LLVM_VER)" ../llvm-$(LLVM_VER)/projects/libcxxabi -DLIBCXXABI_CXX_ABI_LIBRARIES="$(LIBCXX_EXTRA_FLAGS)" -DCMAKE_CXX_FLAGS="$(CXXFLAGS) -std=c++11"
llvm-$(LLVM_VER)/projects/libcxxabi/lib/libc++abi.so.1.0: llvm-$(LLVM_VER)/projects/libcxxabi | llvm-$(LLVM_VER)/projects/libcxx
libcxxabi-build/lib/libc++abi.so.1.0: | libcxxabi-build/Makefile
cd libcxxabi-build && $(MAKE)
Expand Down Expand Up @@ -1390,7 +1405,7 @@ ifeq (exists, $(shell [ -d $(JULIAHOME)/.git/modules/deps/utf8proc ] && echo exi
$(UTF8PROC_SRC_TARGET): $(JULIAHOME)/.git/modules/deps/utf8proc/HEAD
endif
$(UTF8PROC_SRC_TARGET): utf8proc/Makefile
$(MAKE) -C utf8proc cc="$(CC) -O2 -std=c99 $(fPIC) -DUTF8PROC_EXPORTS" AR="$(AR)" libutf8proc.a
$(MAKE) -C utf8proc cc="$(CC) -O2 -std=c99 $(fPIC) -DUTF8PROC_EXPORTS $(DEPS_CFLAGS)" AR="$(AR)" libutf8proc.a
touch -c $@
utf8proc/checked: $(UTF8PROC_SRC_TARGET)
ifeq ($(OS),$(BUILD_OS))
Expand Down Expand Up @@ -1620,6 +1635,10 @@ install-osxunwind: $(OSXUNWIND_OBJ_TARGET)
GMP_SRC_TARGET = gmp-$(GMP_VER)/.libs/libgmp.$(SHLIB_EXT)
GMP_OBJ_TARGET = $(build_shlibdir)/libgmp.$(SHLIB_EXT)

ifneq ($(SANITIZE),)
GMP_CONFIGURE_OPTS += --disable-assembly
endif

gmp-$(GMP_VER).tar.bz2:
$(JLDOWNLOAD) $@ https://gmplib.org/download/gmp/$@
gmp-$(GMP_VER)/configure: gmp-$(GMP_VER).tar.bz2
Expand All @@ -1631,7 +1650,7 @@ endif
touch -c $@
gmp-$(GMP_VER)/config.status: gmp-$(GMP_VER)/configure
cd gmp-$(GMP_VER) && \
./configure $(CONFIGURE_COMMON) F77= --enable-shared --disable-static
./configure $(CONFIGURE_COMMON) F77= --enable-shared --disable-static $(GMP_CONFIGURE_OPTS)
touch -c $@
$(GMP_SRC_TARGET): gmp-$(GMP_VER)/config.status
$(MAKE) -C gmp-$(GMP_VER) $(LIBTOOL_CCLD)
Expand Down Expand Up @@ -1670,6 +1689,11 @@ ifeq ($(OS),Darwin)
MPFR_CHECK_MFLAGS = LDFLAGS="-Wl,-rpath,'$(build_libdir)'"
endif

ifneq ($(SANITIZE),)
# Force generic C build
MPFR_OPTS += --host=none-unknown-linux
endif

mpfr-$(MPFR_VER).tar.bz2:
$(JLDOWNLOAD) $@ https://www.mpfr.org/mpfr-current/$@
mpfr-$(MPFR_VER)/configure: mpfr-$(MPFR_VER).tar.bz2
Expand Down
38 changes: 31 additions & 7 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "platform.h"
#include "options.h"

#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
Expand Down Expand Up @@ -436,6 +437,8 @@ void jl_dump_objfile(char *fname, int jit_model)
Triple TheTriple = Triple(jl_TargetMachine->getTargetTriple());
#if defined(_OS_WINDOWS_) && defined(USE_MCJIT)
TheTriple.setObjectFormat(Triple::COFF);
#elif defined(_OS_DARWIN_) && defined(FORCE_ELF)
TheTriple.setObjectFormat(Triple::MachO);
#endif
#ifdef LLVM35
std::unique_ptr<TargetMachine>
Expand Down Expand Up @@ -673,8 +676,10 @@ static void jl_setup_module(Module *m, bool add)
llvm::DEBUG_METADATA_VERSION);
#endif
#ifdef LLVM37
if (jl_ExecutionEngine)
if (jl_ExecutionEngine) {
m->setDataLayout(jl_ExecutionEngine->getDataLayout()->getStringRepresentation());
m->setTargetTriple(jl_TargetMachine->getTargetTriple());
}
#elif LLVM36
if (jl_ExecutionEngine)
m->setDataLayout(jl_ExecutionEngine->getDataLayout());
Expand Down Expand Up @@ -731,8 +736,10 @@ extern "C" void jl_generate_fptr(jl_function_t *f)
li->fptr = (jl_fptr_t)jl_ExecutionEngine->getPointerToFunction(llvmf);
#endif
assert(li->fptr != NULL);
#ifndef KEEP_BODIES
if (!imaging_mode)
llvmf->deleteBody();
#endif

if (li->cFunctionList != NULL) {
size_t i;
Expand All @@ -743,9 +750,11 @@ extern "C" void jl_generate_fptr(jl_function_t *f)
#else
(void)jl_ExecutionEngine->getPointerToFunction(list->data[i].f);
#endif
#ifndef KEEP_BODIES
if (!imaging_mode) {
list->data[i].f->deleteBody();
}
#endif
}
}

Expand Down Expand Up @@ -867,6 +876,11 @@ void *jl_function_ptr(jl_function_t *f, jl_value_t *rt, jl_value_t *argt)

extern "C" DLLEXPORT
void *jl_function_ptr_by_llvm_name(char* name) {
#ifdef __has_feature
#if __has_feature(memory_sanitizer)
__msan_unpoison_string(name);
#endif
#endif
return (void*)(intptr_t)jl_ExecutionEngine->FindFunctionNamed(name);
}

Expand Down Expand Up @@ -4973,8 +4987,7 @@ static void init_julia_llvm_env(Module *m)
jlpgcstack_var =
new GlobalVariable(*m, jl_ppvalue_llvmt,
false, GlobalVariable::ExternalLinkage,
NULL, "jl_pgcstack");
add_named_global(jlpgcstack_var, (void*)&jl_pgcstack);
NULL, "jl_pgcstack", NULL);
#endif

global_to_llvm("__stack_chk_guard", (void*)&__stack_chk_guard, m);
Expand Down Expand Up @@ -5398,6 +5411,9 @@ static void init_julia_llvm_env(Module *m)
# if __has_feature(address_sanitizer)
FPM->add(createAddressSanitizerFunctionPass());
# endif
# if __has_feature(memory_sanitizer)
FPM->add(llvm::createMemorySanitizerPass(true));
# endif
#endif
#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 3
#ifndef LLVM37
Expand Down Expand Up @@ -5586,12 +5602,14 @@ extern "C" void jl_init_codegen(void)
.setJITMemoryManager(createJITMemoryManagerWin())
#endif
.setTargetOptions(options)
.setRelocationModel(Reloc::PIC_)
.setCodeModel(CodeModel::Small)
#if defined(USE_MCJIT) && !defined(LLVM36)
.setUseMCJIT(true)
#endif
;
Triple TheTriple(sys::getProcessTriple());
#if defined(_OS_WINDOWS_) && defined(USE_MCJIT)
#if (defined(_OS_WINDOWS_) || defined(FORCE_ELF)) && defined(USE_MCJIT)
TheTriple.setObjectFormat(Triple::ELF);
#endif
jl_TargetMachine = eb->selectTarget(
Expand All @@ -5608,12 +5626,17 @@ extern "C" void jl_init_codegen(void)
jl_TargetMachine->getTargetCPU(),
jl_TargetMachine->getTargetFeatureString(),
jl_TargetMachine->Options,
#ifdef CODEGEN_TLS
Reloc::PIC_,
CodeModel::Small,
#else
Reloc::Default,
CodeModel::JITDefault,
#endif
#ifdef DISABLE_OPT
CodeGenOpt::None // -O3
CodeGenOpt::None
#else
CodeGenOpt::Aggressive
CodeGenOpt::Aggressive // -O3
#endif
);
assert(jl_TargetMachine);
Expand All @@ -5640,11 +5663,12 @@ extern "C" void jl_init_codegen(void)
#ifdef LLVM37
m->setDataLayout(jl_ExecutionEngine->getDataLayout()->getStringRepresentation());
engine_module->setDataLayout(jl_ExecutionEngine->getDataLayout()->getStringRepresentation());
m->setTargetTriple(jl_TargetMachine->getTargetTriple());
engine_module->setTargetTriple(jl_TargetMachine->getTargetTriple());
#elif LLVM36
m->setDataLayout(jl_ExecutionEngine->getDataLayout());
engine_module->setDataLayout(jl_ExecutionEngine->getDataLayout());
#endif

init_julia_llvm_env(m);

RegisterJuliaJITEventListener();
Expand Down
23 changes: 23 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,29 @@ static void jl_find_stack_bottom(void)
size_t stack_size;
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
struct rlimit rl;

// When using memory sanitizer, increase stack size because msan bloats stack usage
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
const rlim_t kStackSize = 32 * 1024 * 1024; // 32MB stack
int result;

result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
#endif
#endif

getrlimit(RLIMIT_STACK, &rl);
stack_size = rl.rlim_cur;
#else
Expand Down
Loading

0 comments on commit 605534f

Please sign in to comment.