c - How can I know if Leak Sanitizer is enabled at compile time? -
the gcc , clang compilers both have support leaksanitizer helps finding memory leaks in c programs. memory leak unavoidable (because being tested in test suite example).
such memory can annotated using leak sanitizer interface:
#include <sanitizer/lsan_interface.h> void *p = create_new_object(); __lsan_ignore_object(p); this break on compilers not support lsan. in address sanitizer, construct can used detect availablity of asan:
/* __has_feature(address_sanitizer) used later clang, * compatibility other compilers (such gcc , msvc) */ #ifndef __has_feature # define __has_feature(x) 0 #endif #if __has_feature(address_sanitizer) || defined(__sanitize_address__) /* asan-aware code here. */ #endif there no __has_feature(leak_sanitizer) detect existence of lsan in clang , neither __sanitize_leaks__ exist gcc. how can detect asan availability anyway? note lsan can enabled independently of addresssanitizer , threadsanitizer.
as compiler not set preprocessor define 1 have himself.
one compile -fsanitize=leak -dmyleaksan=1 leaksanitizer or without leaksanitizer 1 compile -dmyleaksan=0. if 1 forget define myleaksan compiler halted.
#ifndef myleaksan # error: myleaksan must either 0 or 1 #endif #include <stdio.h> #include <stdlib.h> #if myleaksan # include <sanitizer/lsan_interface.h> #endif int main(void) { void *p = malloc(5); #if myleaksan __lsan_ignore_object(p); #endif }
Comments
Post a Comment