From 032a4277d511e850f49cadc60b8bb8510500b352 Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Sat, 25 Jun 2022 17:58:34 +0200 Subject: [PATCH] Enable output of variable names in ASan and MSan error reporting. --- driver/codegenerator.cpp | 8 ++++++-- tests/sanitizers/asan_fiber.d | 5 +++-- tests/sanitizers/asan_fiber_main.d | 5 +++-- tests/sanitizers/asan_stackoverflow.d | 9 ++++----- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index cc98f4957e1..3df77bf79d5 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -17,6 +17,7 @@ #include "dmd/scope.h" #include "driver/cl_options.h" #include "driver/cl_options_instrumentation.h" +#include "driver/cl_options_sanitizers.h" #include "driver/linker.h" #include "driver/toobj.h" #include "gen/dynamiccompile.h" @@ -195,8 +196,11 @@ CodeGenerator::CodeGenerator(llvm::LLVMContext &context, mlirContext_(mlirContext), #endif moduleCount_(0), singleObj_(singleObj), ir_(nullptr) { - // Set the context to discard value names when not generating textual IR. - if (!global.params.output_ll) { + // Set the context to discard value names when not generating textual IR and + // when ASan or MSan are not enabled. + if (!global.params.output_ll && + !opts::isSanitizerEnabled(opts::AddressSanitizer | + opts::MemorySanitizer)) { context_.setDiscardValueNames(true); } } diff --git a/tests/sanitizers/asan_fiber.d b/tests/sanitizers/asan_fiber.d index 80de141a34a..1fc8d538df9 100644 --- a/tests/sanitizers/asan_fiber.d +++ b/tests/sanitizers/asan_fiber.d @@ -27,8 +27,9 @@ void foo(int* ptr) // CHECK-NEXT: #0 {{.*}} in {{.*prefoo.*}} {{.*}}asan_fiber.d:[[@LINE+1]] void prefoo() { - int[10] a; - foo(&a[0]); + // CHECK: 'aiaiaiaiaiaiaiaiaiai'{{.*}} <== {{.*}} overflows this variable + int[10] aiaiaiaiaiaiaiaiaiai; + foo(&aiaiaiaiaiaiaiaiaiai[0]); } void main() diff --git a/tests/sanitizers/asan_fiber_main.d b/tests/sanitizers/asan_fiber_main.d index 6bfc7a9a4f3..e661d32eea2 100644 --- a/tests/sanitizers/asan_fiber_main.d +++ b/tests/sanitizers/asan_fiber_main.d @@ -36,13 +36,14 @@ void foo(int* arr) // FAKESTACK: #0 {{.*}} in {{.*main.*}} {{.*}}asan_fiber_main.d:[[@LINE+1]] void main() { - int[10] a; + // FAKESTACK: 'abcdabcdabcd'{{.*}} <== {{.*}} overflows this variable + int[10] abcdabcdabcd; int b; // Use an extra variable instead of passing `&a[0]` directly to `foo`. // This is to keep `a` on the stack: `ptr` may be heap allocated because // it is used in the lambda (delegate). - int* ptr = &a[0]; + int* ptr = &abcdabcdabcd[0]; auto fib = new Fiber(() => foo(ptr)); fib.call(); version (BAD_AFTER_YIELD) diff --git a/tests/sanitizers/asan_stackoverflow.d b/tests/sanitizers/asan_stackoverflow.d index af32e5fb63f..8551d0bca49 100644 --- a/tests/sanitizers/asan_stackoverflow.d +++ b/tests/sanitizers/asan_stackoverflow.d @@ -17,10 +17,9 @@ void foo(int* arr) // CHECK-NEXT: #0 {{.*}} in {{.*main.*}} {{.*}}asan_stackoverflow.d:[[@LINE+1]] void main() { - // TODO: add test for the name of the variable that is overflown. Right now we get this message: - //[32, 72) '' <== Memory access at offset 72 overflows this variable - // C HECK: 'a'{{.*}} <== {{.*}} overflows this variable - int[10] a; + // Test for the name of the variable that is overflown. + // CHECK: 'aiaiaiaiaiaiai'{{.*}} <== {{.*}} overflows this variable + int[10] aiaiaiaiaiaiai; int b; - foo(&a[0]); + foo(&aiaiaiaiaiaiai[0]); }