mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-03 08:30:47 +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'.")
|
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.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.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.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)
|
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);
|
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)) {
|
if (opts::isSanitizerEnabled(opts::MemorySanitizer)) {
|
||||||
args.push_back("-fsanitize=memory");
|
addSanitizerLinkFlags(triple, "msan", "-fsanitize=memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts::isSanitizerEnabled(opts::ThreadSanitizer)) {
|
if (opts::isSanitizerEnabled(opts::ThreadSanitizer)) {
|
||||||
|
|
|
@ -114,6 +114,11 @@ static cl::opt<bool>
|
||||||
disableSLPVectorization("disable-slp-vectorization", cl::ZeroOrMore,
|
disableSLPVectorization("disable-slp-vectorization", cl::ZeroOrMore,
|
||||||
cl::desc("Disable the slp vectorization pass"));
|
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() {
|
unsigned optLevel() {
|
||||||
// Use -O2 as a base for the size-optimization levels.
|
// Use -O2 as a base for the size-optimization levels.
|
||||||
return optimizeLevel >= 0 ? optimizeLevel : 2;
|
return optimizeLevel >= 0 ? optimizeLevel : 2;
|
||||||
|
@ -201,8 +206,14 @@ static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
||||||
|
|
||||||
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
|
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
|
||||||
PassManagerBase &PM) {
|
PassManagerBase &PM) {
|
||||||
#if LDC_LLVM_VER >= 800
|
int trackOrigins = fSanitizeMemoryTrackOrigins;
|
||||||
PM.add(createMemorySanitizerLegacyPassPass());
|
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
|
#else
|
||||||
PM.add(createMemorySanitizerPass());
|
PM.add(createMemorySanitizerPass());
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -11,6 +11,10 @@ sys = platform.system()
|
||||||
if (sys != 'FreeBSD') and (sys != 'Windows') and not (sys == 'Linux' and config.shared_rt_libs_only):
|
if (sys != 'FreeBSD') and (sys != 'Windows') and not (sys == 'Linux' and config.shared_rt_libs_only):
|
||||||
config.available_features.add('TSan')
|
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
|
# Add "Fuzzer" feature, assuming the compiler-rt library is available
|
||||||
config.available_features.add('Fuzzer')
|
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