Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if building with address sanitizer when building with gcc 4.8?

Tags:

I'm working on a program written in C that I occasionally build with address sanitizer, basically to catch bugs. The program prints a banner in the logs when it starts up with info such as: who built it, the branch it was built on, compiler etc. I was thinking it would be nice to also spell out if the binary was built using address sanitizer. I know there's __has_feature(address_sanitizer), but that only works for clang. I tried the following simple program:

#include <stdio.h>

int main()
{
#if defined(__has_feature)
# if __has_feature(address_sanitizer)
    printf ("We has ASAN!\n");
# else
    printf ("We have has_feature, no ASAN!\n");
# endif
#else
    printf ("We got nothing!\n");
#endif

    return 0;
}

When building with gcc -Wall -g -fsanitize=address -o asan asan.c, this yields:

We got nothing!

With clang -Wall -g -fsanitize=address -o asan asan.c I get:

We has ASAN!

Is there a gcc equivalent to __has_feature?

I know there are ways to check, like the huge VSZ value for programs built with address sanitizer, just wondering if there's a compile-time define or something.

like image 508
fencekicker Avatar asked Jan 15 '16 14:01

fencekicker


People also ask

What is AddressSanitizer GCC?

Address Sanitizer is a tool developed by Google detect memory access error such as use-after-free and memory leaks. It is built into GCC versions >= 4.8 and can be used on both C and C++ codes.

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.

What is AddressSanitizer Xcode?

Addresss Sanitizer is a LLVM-based debug tool to finds memory corruption at run time with less overhead at running time. And it works on OS X, iOS (simulator and device).


1 Answers

From the GCC 4.8.0 manual:

__SANITIZE_ADDRESS__

This macro is defined, with value 1, when -fsanitize=address is in use.

like image 93
cremno Avatar answered Sep 19 '22 10:09

cremno