UBSan And Asan usage with GCC 4.9.2

Ilya picture Ilya · Aug 4, 2015 · Viewed 8.8k times · Source

I have compiled my application with -fsanitize=undefined option. How can I now test my application for undefined behavior?

Also, how do I run an Asan check? I've compiled my program with -fsanitize=address, and it crashes with the following output:

==4563==Sanitizer CHECK failed: ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:85 ((allocated < kCallocPoolSize)) != (0) (0, 0)

I've got GCC 4.9.2 on Ubuntu 15.04.

Answer

Shafik Yaghmour picture Shafik Yaghmour · Aug 9, 2015

Unlike a static analysis check, this check will be done at run-time as explained in the following blog entry: GCC Undefined Behavior Sanitizer – ubsan. It will output a runtime error when it detects undefined behavior:

In order to check your program with ubsan, compile and link the program with -fsanitize=undefined option. Such instrumented binaries have to be executed; if ubsan detects any problem, it outputs a “runtime error:” message, and in most cases continues executing the program. There is a possibility of making these diagnostic messages abort — just use the option -fno-sanitize-recover.

And we can see an example from there:

int main() {
    int i = 23;
    i <<= 32; 
}

when ran using -fsanitize=undefined will output (see it live):

runtime error: shift exponent 32 is too large for 32-bit type 'int'

GCC documents this option in their Options for Debugging Your Program or GCC section and it says:

Enable UndefinedBehaviorSanitizer, a fast undefined behavior detector. Various computations are instrumented to detect undefined behavior at runtime.

As for the asan issue this address-sanitizer document gives you an example and the expected results. Perhaps your case it related to this gcc bug.