Skip to content

Commit 2dba9af

Browse files
committed
Compile testlib-only tests once for both fast and check
Testlib-only tests are mitigation-independent: compile into shared OBJECT libraries. Also fix MSVC C4701 warning in cleanup_unused and add unistd.h to protect_fork.
1 parent f3448e6 commit 2dba9af

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

CMakeLists.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,16 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
479479
set(TEST_CLEANUP ${SNMALLOC_CLEANUP})
480480
endif()
481481

482+
# Tests that only include snmalloc_testlib.h (no snmalloc.h or snmalloc_core.h).
483+
# These are mitigation-independent and can be compiled once, then linked
484+
# against both fast and check testlib variants.
485+
set(TESTLIB_ONLY_TESTS
486+
bits first_operation memory_usage multi_atexit multi_threadatexit
487+
redblack statistics teardown
488+
contention external_pointer large_alloc lotsofthreads post_teardown
489+
singlethread startup
490+
)
491+
482492
function(make_tests TAG DEFINES)
483493
foreach(TEST_CATEGORY ${TEST_CATEGORIES})
484494
message(VERBOSE "Adding ${TAG}/${TEST_CATEGORY} tests")
@@ -488,7 +498,32 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
488498
aux_source_directory(${TESTDIR}/${TEST_CATEGORY}/${TEST} SRC)
489499
set(TESTNAME "${TEST_CATEGORY}-${TEST}-${TAG}")
490500

491-
add_executable(${TESTNAME} ${SRC})
501+
# Check if this test is testlib-only (mitigation-independent).
502+
# If so, compile the source once into an object library and reuse
503+
# across flavours.
504+
list(FIND TESTLIB_ONLY_TESTS ${TEST} _testlib_only_idx)
505+
set(OBJLIBNAME "${TEST_CATEGORY}-${TEST}-obj")
506+
if (NOT ${_testlib_only_idx} EQUAL -1 AND NOT TARGET ${OBJLIBNAME})
507+
# First flavour to see this test creates the object library.
508+
add_library(${OBJLIBNAME} OBJECT ${SRC})
509+
target_link_libraries(${OBJLIBNAME} snmalloc)
510+
target_compile_definitions(${OBJLIBNAME} PRIVATE "SNMALLOC_USE_${TEST_CLEANUP}")
511+
add_warning_flags(${OBJLIBNAME})
512+
if(SNMALLOC_SANITIZER)
513+
target_compile_options(${OBJLIBNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer)
514+
if (${SNMALLOC_SANITIZER} MATCHES "thread")
515+
target_compile_definitions(${OBJLIBNAME} PRIVATE SNMALLOC_THREAD_SANITIZER_ENABLED)
516+
endif()
517+
endif()
518+
endif()
519+
520+
if (NOT ${_testlib_only_idx} EQUAL -1)
521+
# Testlib-only: link the shared object library against this
522+
# flavour's testlib.
523+
add_executable(${TESTNAME} $<TARGET_OBJECTS:${OBJLIBNAME}>)
524+
else()
525+
add_executable(${TESTNAME} ${SRC})
526+
endif()
492527

493528
if(SNMALLOC_SANITIZER)
494529
target_compile_options(${TESTNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer)
@@ -551,6 +586,12 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
551586
if (NOT DEFINES STREQUAL " ")
552587
target_compile_definitions(${LIBNAME} PRIVATE ${DEFINES})
553588
endif()
589+
if(SNMALLOC_SANITIZER)
590+
target_compile_options(${LIBNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer)
591+
if (${SNMALLOC_SANITIZER} MATCHES "thread")
592+
target_compile_definitions(${LIBNAME} PRIVATE SNMALLOC_THREAD_SANITIZER_ENABLED)
593+
endif()
594+
endif()
554595
add_warning_flags(${LIBNAME})
555596
endfunction()
556597

src/snmalloc/global/globalalloc.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ namespace snmalloc
1717
// Handling the message queue for each stack is non-atomic.
1818
auto* first = AllocPool<Config_>::extract();
1919
auto* alloc = first;
20-
decltype(alloc) last;
2120

22-
if (alloc != nullptr)
23-
{
24-
while (alloc != nullptr)
25-
{
26-
alloc->flush();
27-
last = alloc;
28-
alloc = AllocPool<Config_>::extract(alloc);
29-
}
21+
if (alloc == nullptr)
22+
return;
3023

31-
AllocPool<Config_>::restore(first, last);
24+
decltype(alloc) last = alloc;
25+
while (alloc != nullptr)
26+
{
27+
alloc->flush();
28+
last = alloc;
29+
alloc = AllocPool<Config_>::extract(alloc);
3230
}
31+
32+
AllocPool<Config_>::restore(first, last);
3333
}
3434

3535
/**

src/test/func/protect_fork/protect_fork.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int main()
1212
# include <snmalloc/ds_core/helpers.h>
1313
# include <snmalloc/ds_aal/prevent_fork.h>
1414
# include <thread>
15+
# include <unistd.h>
1516

1617
void simulate_allocation()
1718
{

0 commit comments

Comments
 (0)