mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-05 09:31:03 +03:00
Add MSan compiler-rt library to LDC install and add it to linkerline for --fsanitize=memory (#3751)
* Add MSan compiler-rt library to LDC install and add it to linkerline for --fsanitize=memory * MSan: Add support for `-fsanitize-memory-track-origins=`
This commit is contained in:
parent
4e6c9d0ffd
commit
a5025bf83e
6 changed files with 51 additions and 5 deletions
|
@ -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)
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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;
|
||||
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
10
tests/sanitizers/msan_noerror.d
Normal file
10
tests/sanitizers/msan_noerror.d
Normal file
|
@ -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
tests/sanitizers/msan_uninitialized.d
Normal file
22
tests/sanitizers/msan_uninitialized.d
Normal file
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue