Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MSan compiler-rt library to LDC install and add it to linkerline for --fsanitize=memory #3751

Merged
merged 2 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ if (LDC_INSTALL_LLVM_RUNTIME_LIBS)
set(LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH "x86_64" CACHE STRING "Non-Mac Posix: architecture used as libname suffix for the compiler-rt source libraries, e.g., 'aarch64'.")

copy_compilerrt_lib("${LDC_INSTALL_LLVM_RUNTIME_LIBS_OS}/libclang_rt.asan-${LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH}.a" "libldc_rt.asan.a" FALSE)
copy_compilerrt_lib("${LDC_INSTALL_LLVM_RUNTIME_LIBS_OS}/libclang_rt.msan-${LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH}.a" "libldc_rt.msan.a" FALSE)
copy_compilerrt_lib("${LDC_INSTALL_LLVM_RUNTIME_LIBS_OS}/libclang_rt.tsan-${LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH}.a" "libldc_rt.tsan.a" FALSE)
copy_compilerrt_lib("${LDC_INSTALL_LLVM_RUNTIME_LIBS_OS}/libclang_rt.builtins-${LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH}.a" "libldc_rt.builtins.a" FALSE)
copy_compilerrt_lib("${LDC_INSTALL_LLVM_RUNTIME_LIBS_OS}/libclang_rt.profile-${LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH}.a" "libldc_rt.profile.a" FALSE)
Expand Down
4 changes: 1 addition & 3 deletions driver/linker-gcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,8 @@ void ArgsBuilder::addSanitizers(const llvm::Triple &triple) {
addFuzzLinkFlags(triple);
}

// TODO: instead of this, we should link with our own sanitizer libraries
// because LDC's LLVM version could be different from the system clang.
if (opts::isSanitizerEnabled(opts::MemorySanitizer)) {
args.push_back("-fsanitize=memory");
addSanitizerLinkFlags(triple, "msan", "-fsanitize=memory");
}

if (opts::isSanitizerEnabled(opts::ThreadSanitizer)) {
Expand Down
15 changes: 13 additions & 2 deletions gen/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ static cl::opt<bool>
disableSLPVectorization("disable-slp-vectorization", cl::ZeroOrMore,
cl::desc("Disable the slp vectorization pass"));

static cl::opt<int> fSanitizeMemoryTrackOrigins(
"fsanitize-memory-track-origins", cl::ZeroOrMore, cl::init(0),
cl::desc(
"Enable origins tracking in MemorySanitizer (0=disabled, default)"));

unsigned optLevel() {
// Use -O2 as a base for the size-optimization levels.
return optimizeLevel >= 0 ? optimizeLevel : 2;
Expand Down Expand Up @@ -201,8 +206,14 @@ static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,

static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
PassManagerBase &PM) {
#if LDC_LLVM_VER >= 800
PM.add(createMemorySanitizerLegacyPassPass());
int trackOrigins = fSanitizeMemoryTrackOrigins;
bool recover = false;
bool kernel = false;
#if LDC_LLVM_VER >= 900
PM.add(createMemorySanitizerLegacyPassPass(
MemorySanitizerOptions{trackOrigins, recover, kernel}));
#elif LDC_LLVM_VER >= 800
PM.add(createMemorySanitizerLegacyPassPass(trackOrigins, recover, kernel));
#else
PM.add(createMemorySanitizerPass());
#endif
Expand Down
4 changes: 4 additions & 0 deletions tests/sanitizers/lit.local.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ sys = platform.system()
if (sys != 'FreeBSD') and (sys != 'Windows') and not (sys == 'Linux' and config.shared_rt_libs_only):
config.available_features.add('TSan')

# MSan is supported on Linux, FreeBSD, and OpenBSD: https://clang.llvm.org/docs/MemorySanitizer.html#supported-platforms
if (sys == 'Linux') or (sys == 'FreeBSD') or (sys == 'OpenBSD'):
config.available_features.add('MSan')

# Add "Fuzzer" feature, assuming the compiler-rt library is available
config.available_features.add('Fuzzer')

Expand Down
10 changes: 10 additions & 0 deletions tests/sanitizers/msan_noerror.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test that a simple progam passes MemorySanitizer without error

// REQUIRES: MSan

// RUN: %ldc -g -fsanitize=memory %s -of=%t%exe
// RUN: %t%exe

void main()
{
}
22 changes: 22 additions & 0 deletions tests/sanitizers/msan_uninitialized.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Test unitialized data access with MemorySanitizer

// REQUIRES: MSan, atleast_llvm800

// RUN: %ldc -g -fsanitize=memory -run %s
// RUN: %ldc -g -fsanitize=memory -fsanitize-memory-track-origins=2 -d-version=BUG %s -of=%t%exe
// RUN: not %t%exe 2>&1 | FileCheck %s

// CHECK: MemorySanitizer: use-of-uninitialized-value

// CHECK: Uninitialized value was created by an allocation of {{.*}} in the stack frame of function '_Dmain'
// CHECK-NEXT: #0 {{.*}} in _Dmain {{.*}}msan_uninitialized.d:[[@LINE+1]]
int main()
{
version (BUG)
int x = void;
else
int x;

int* p = &x;
return *p;
}