forked from OSchip/llvm-project
[ASan] Support for cmake build of ASan unittests in 32-bit LLVM build. Currently, to run ASan unit tests both for 32- and 64 bits one has to maintain two distinct LLVM builds. In a bright future, we'd like to use a single build for this
llvm-svn: 160666
This commit is contained in:
parent
faa9c3b035
commit
96b9393f2d
|
@ -19,6 +19,12 @@ set(ASAN_UNITTEST_COMMON_CFLAGS
|
|||
-Wno-format
|
||||
-fvisibility=hidden
|
||||
)
|
||||
# Support 64-bit and 32-bit builds.
|
||||
if(LLVM_BUILD_32_BITS)
|
||||
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m32)
|
||||
else()
|
||||
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -m64)
|
||||
endif()
|
||||
|
||||
set(ASAN_GTEST_INCLUDE_CFLAGS
|
||||
-I${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include
|
||||
|
@ -45,7 +51,7 @@ add_custom_target(AsanTests)
|
|||
set_target_properties(AsanTests PROPERTIES FOLDER "ASan tests")
|
||||
function(add_asan_test testname)
|
||||
add_unittest(AsanTests ${testname} ${ARGN})
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
if(LLVM_BUILD_32_BITS)
|
||||
target_link_libraries(${testname} clang_rt.asan-i386)
|
||||
else()
|
||||
target_link_libraries(${testname} clang_rt.asan-x86_64)
|
||||
|
@ -53,12 +59,13 @@ function(add_asan_test testname)
|
|||
if (APPLE)
|
||||
# Darwin-specific linker flags.
|
||||
set_property(TARGET ${testname} APPEND PROPERTY
|
||||
LINK_FLAGS "-framework CoreFoundation")
|
||||
LINK_FLAGS "-framework Foundation")
|
||||
elseif (UNIX)
|
||||
# Linux-specific linker flags.
|
||||
set_property(TARGET ${testname} APPEND PROPERTY
|
||||
LINK_FLAGS "-lpthread -ldl -export-dynamic")
|
||||
endif()
|
||||
set(add_compile_flags "")
|
||||
get_property(compile_flags TARGET ${testname} PROPERTY COMPILE_FLAGS)
|
||||
foreach(arg ${ASAN_UNITTEST_COMMON_CFLAGS})
|
||||
set(add_compile_flags "${add_compile_flags} ${arg}")
|
||||
|
@ -67,11 +74,13 @@ function(add_asan_test testname)
|
|||
"${compile_flags} ${add_compile_flags}")
|
||||
endfunction()
|
||||
|
||||
set(ASAN_TEST_FILES
|
||||
set(ASAN_NOINST_TEST_SOURCES
|
||||
asan_noinst_test.cc
|
||||
asan_break_optimization.cc
|
||||
)
|
||||
|
||||
set(ASAN_INST_TEST_OBJECTS)
|
||||
|
||||
# We only support building instrumented tests when we're not cross compiling
|
||||
# and targeting a unix-like system where we can predict viable compilation and
|
||||
# linking strategies.
|
||||
|
@ -80,43 +89,30 @@ if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX)
|
|||
# This function is a custom routine to manage manually compiling source files
|
||||
# for unit tests with the just-built Clang binary, using the ASan
|
||||
# instrumentation, and linking them into a test executable.
|
||||
function(add_asan_compile_cxx_command source)
|
||||
function(add_asan_compile_command source extra_cflags)
|
||||
set(output_obj "${source}.asan.o")
|
||||
add_custom_command(
|
||||
OUTPUT "${source}.asan.o"
|
||||
OUTPUT ${output_obj}
|
||||
COMMAND clang
|
||||
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
|
||||
-c -o "${source}.asan.o"
|
||||
${extra_cflags}
|
||||
-c -o "${output_obj}"
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/${source}
|
||||
MAIN_DEPENDENCY ${source}
|
||||
DEPENDS clang ${ARGN}
|
||||
)
|
||||
endfunction()
|
||||
|
||||
function(add_asan_compile_objc_command source)
|
||||
add_custom_command(
|
||||
OUTPUT "${source}.asan.o"
|
||||
COMMAND clang
|
||||
${ASAN_UNITTEST_INSTRUMENTED_CFLAGS}
|
||||
-ObjC -c -o "${source}.asan.o"
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/${source}
|
||||
MAIN_DEPENDENCY ${source}
|
||||
DEPENDS clang ${ARGN}
|
||||
)
|
||||
endfunction()
|
||||
|
||||
add_asan_compile_cxx_command(asan_globals_test.cc)
|
||||
add_asan_compile_cxx_command(asan_test.cc)
|
||||
list(APPEND ASAN_TEST_FILES
|
||||
asan_globals_test.cc.asan.o
|
||||
asan_test.cc.asan.o
|
||||
)
|
||||
add_asan_compile_command(asan_globals_test.cc "")
|
||||
add_asan_compile_command(asan_test.cc "")
|
||||
list(APPEND ASAN_INST_TEST_OBJECTS asan_globals_test.cc.asan.o
|
||||
asan_test.cc.asan.o)
|
||||
if (APPLE)
|
||||
add_asan_compile_objc_command(asan_mac_test.mm)
|
||||
list(APPEND ASAN_TEST_FILES asan_mac_test.mm.asan.o)
|
||||
add_asan_compile_command(asan_mac_test.mm "-ObjC")
|
||||
list(APPEND ASAN_INST_TEST_OBJECTS asan_mac_test.mm.asan.o)
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
add_asan_test(AsanTest
|
||||
${ASAN_TEST_FILES}
|
||||
)
|
||||
add_asan_test(AsanTest ${ASAN_NOINST_TEST_SOURCES}
|
||||
${ASAN_INST_TEST_OBJECTS})
|
||||
|
|
|
@ -15,4 +15,5 @@
|
|||
// Have this function in a separate file to avoid inlining.
|
||||
// (Yes, we know about cross-file inlining, but let's assume we don't use it).
|
||||
extern "C" void break_optimization(void *x) {
|
||||
(void)x;
|
||||
}
|
||||
|
|
|
@ -221,6 +221,7 @@ void CompressStackTraceTest(size_t n_iter) {
|
|||
std::max((size_t)2, (size_t)my_rand(&seed) % (2 * kNumPcs));
|
||||
size_t n_frames =
|
||||
__asan::AsanStackTrace::CompressStack(&stack0, compressed, compress_size);
|
||||
Ident(n_frames);
|
||||
assert(n_frames <= stack0.size);
|
||||
__asan::AsanStackTrace::UncompressStack(&stack1, compressed, compress_size);
|
||||
assert(stack1.size == n_frames);
|
||||
|
@ -275,6 +276,7 @@ TEST(AddressSanitizer, QuarantineTest) {
|
|||
}
|
||||
|
||||
void *ThreadedQuarantineTestWorker(void *unused) {
|
||||
(void)unused;
|
||||
u32 seed = my_rand(&global_seed);
|
||||
__asan::AsanStackTrace stack;
|
||||
stack.trace[0] = 0x890;
|
||||
|
@ -302,6 +304,7 @@ TEST(AddressSanitizer, ThreadedQuarantineTest) {
|
|||
}
|
||||
|
||||
void *ThreadedOneSizeMallocStress(void *unused) {
|
||||
(void)unused;
|
||||
__asan::AsanStackTrace stack;
|
||||
stack.trace[0] = 0x890;
|
||||
stack.size = 1;
|
||||
|
@ -478,9 +481,10 @@ TEST(AddressSanitizerInterface, GetFreeBytesTest) {
|
|||
|
||||
static const size_t kManyThreadsMallocSizes[] = {5, 1UL<<10, 1UL<<20, 357};
|
||||
static const size_t kManyThreadsIterations = 250;
|
||||
static const size_t kManyThreadsNumThreads = 200;
|
||||
static const size_t kManyThreadsNumThreads = (__WORDSIZE == 32) ? 40 : 200;
|
||||
|
||||
void *ManyThreadsWithStatsWorker(void *arg) {
|
||||
(void)arg;
|
||||
for (size_t iter = 0; iter < kManyThreadsIterations; iter++) {
|
||||
for (size_t size_index = 0; size_index < 4; size_index++) {
|
||||
free(Ident(malloc(kManyThreadsMallocSizes[size_index])));
|
||||
|
|
Loading…
Reference in New Issue