Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't clang enable all sanitizers?

Clang has various sanitizers that can be turned on to catch problems at runtime.
However, there are some sanitizers that I can't use together. Why is that?

clang++-3.9 -std=c++1z -g -fsanitize=memory -fsanitize=address -o main main.cpp                                                                                   1 clang: error: invalid argument '-fsanitize=address' not allowed with '-fsanitize=memory' 

It's not a big deal, but when I run my unit tests, it takes longer than it should, because I have create multiple binaries for the same tests, and run each of them separately.

clang++-3.9 -std=c++1z -g -fsanitize=address -o test1 test.cpp clang++-3.9 -std=c++1z -g -fsanitize=memory -fsanitize=undefined  -o test2 test.cpp 
like image 430
Trevor Hickey Avatar asked May 01 '16 21:05

Trevor Hickey


People also ask

What is AddressSanitizer error?

AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. The tool can detect the following types of bugs: Out-of-bounds accesses to heap, stack and globals. Use-after-free.

How does AddressSanitizer work?

AddressSanitizer dedicates one-eighth of the virtual address space to its shadow memory and uses a direct mapping with a scale and offset to translate an applica- tion address to its corresponding shadow address.

How do I turn off AddressSanitizer?

Stack Use After Return (UAR) AddressSanitizer can optionally detect stack use after return problems. This is available by default, or explicitly ( -fsanitize-address-use-after-return=runtime ). To disable this check at runtime, set the environment variable ASAN_OPTIONS=detect_stack_use_after_return=0 .


1 Answers

I think the problem is that Asan and Msan both want to control the heap, and both want to reserve a large amount of memory to use as "shadow memory" which tracks the allocations and usage of the memory your program uses.

They can't both be active because they would be trying to track the memory being used by the other sanitizer (which may not appear to be "safe" according to the rules that the sanitizer checks).

It would also result in crazy memory usage, because both sanitizers would be allocating additional memory to track every byte your program uses.

Maybe in theory they could be re-engineered to share a common framework so they can cooperate and not clash, but there are probably very good practical reasons why that would be difficult, or hurt performance.

like image 183
Jonathan Wakely Avatar answered Oct 05 '22 05:10

Jonathan Wakely