[cfe-dev] Query regarding usage of optimizations on Sanitizers in Clang

Bharadwaj, Ritanya B via cfe-dev cfe-dev at lists.llvm.org
Wed Dec 1 05:03:28 PST 2021


Hi,

I have been using Sanitizers/Archer for testing purposes on C,C++ and Fortran test suites. I notice some change in sanitizer behaviour when I add any kind of optimization to it ( O1, O2, Ofast etc). For example, in the following test case:
#include <alloca.h>
void foo(int index, int len) {
                             volatile char *str = (volatile char*)alloca(len);
                             str[index] = '1';  // Boom!
}
int main(int argc, char **argv) {
                            foo(-1, 10);
}
Compiling it with : clang -g -fsanitize=address Test_case.c

Gives me ASan report :
==80838==ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address 0x7ffdd959ac3f at pc 0x00000050ea21 bp 0x7ffdd959ac10 sp 0x7ffdd959ac08
WRITE of size 1 at 0x7ffdd959ac3f thread T0
...........
Address 0x7ffdd959ac3f is located in stack of thread T0
SUMMARY: AddressSanitizer: dynamic-stack-buffer-overflow (/ptmp1/bhararit/new_petest/petest/cc_address_sanitizer.ar/diffs/a.out+0x50ea20)
             ..........

But when I compile it with optimization: clang -g -fsanitize=address -O1 Test_case.c
I do not see any Sanitizer report.

Is this an expected behaviour? If yes, should we be disabling optimizations before using sanitizers although the clang document states that O2 or higher level of optimizations can be used to enhance the performance. Lastly, what is the default optimization in clang ( is it -O0?)?

It would be very helpful if you could give me more insights on this. Hope to hear from you. Thanks in advance.

Best Regards,
Ritanya.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20211201/144fd5e3/attachment.html>


More information about the cfe-dev mailing list