Skip to content

Commit 9cc3c28

Browse files
committed
cmake: Add option to measure test coverage
Signed-off-by: Diogo Behrens <[email protected]>
1 parent 665c46b commit 9cc3c28

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ include(GNUInstallDirs)
4343
include(CheckSymbolExists)
4444
include(cmake/export.cmake)
4545

46+
# Coverage
47+
option(DICE_COVERAGE "Enable test coverage" off)
48+
4649
# TSAN on Clang requires the following flag
4750
set(LIBSAN_SHARED)
4851
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
@@ -84,6 +87,7 @@ else()
8487
endif()
8588
option(DICE_TESTS "Enable Dice tests" ${TESTS_DEFAULT})
8689
if(${DICE_TESTS})
90+
include(CTest)
8791
enable_testing()
8892
add_subdirectory(test)
8993
endif()

src/dice/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ add_library(dice SHARED ${SRCS})
8484
target_link_libraries(dice PUBLIC dice.h pthread)
8585
install(TARGETS dice DESTINATION lib)
8686

87+
if(${DICE_COVERAGE})
88+
# do not involve dice-dispatch/dice-box in the coverage metrics
89+
set(TARGETS dice.o dice)
90+
foreach(TARGET ${TARGETS})
91+
target_compile_options(${TARGET} PRIVATE --coverage)
92+
target_link_options(${TARGET} PUBLIC --coverage)
93+
endforeach()
94+
endif()
95+
8796
if(${ENABLE_SANITIZER})
8897
set(TARGETS rbtree_test dice.o dice)
8998
foreach(TARGET ${TARGETS})

src/mod/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ foreach(SRC ${SRCS})
3939
PRIVATE -fsanitize=${DICE_SANITIZER})
4040
endif()
4141
endif()
42+
43+
if(${DICE_COVERAGE})
44+
foreach(ENDING "" ".o" "_testing.o")
45+
target_compile_options(${TARGET}${ENDING} PRIVATE --coverage)
46+
target_link_options(${TARGET}${ENDING} PUBLIC --coverage)
47+
endforeach()
48+
endif()
4249
endforeach()
4350

4451
target_link_libraries(dice-self PRIVATE pthread)

test/pthread_exit_window.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static int exit_called;
1818
// TLS key and once guard
1919
static pthread_key_t key;
2020
static pthread_once_t once;
21+
static void *arg_check; // keep arg in a variable to double check later
2122

2223
// A once-initializer to make Alpine Linux happy. This is called in the second
2324
// thread and in the main thread but the compilation on Alpine doesn't like it.
@@ -36,7 +37,8 @@ run(void *_)
3637
{
3738
(void)_;
3839
pthread_once(&once, tls_init);
39-
if (pthread_setspecific(key, (const void *)malloc(123)) != 0)
40+
arg_check = malloc(123);
41+
if (pthread_setspecific(key, (const void *)arg_check) != 0)
4042
abort();
4143
log_printf("1) tid = %p\n", (void *)pthread_self());
4244
return 0;
@@ -75,6 +77,10 @@ PS_SUBSCRIBE(CAPTURE_BEFORE, EVENT_FREE, {
7577
if (exit_called == 0)
7678
return PS_OK;
7779

80+
struct free_event *ev = EVENT_PAYLOAD(ev);
81+
if (ev->ptr != arg_check)
82+
return PS_OK;
83+
7884
log_printf("4) tid = %p\tself = %p\n", (void *)pthread_self(), md);
7985
log_printf("Retired thread before free. ID should be 2\n");
8086
log_printf("\tuid = %lu\n", self_id(md));

test/traces/trace_lock_unlock.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <trace_checker.h>
66

77
#include <dice/chains/capture.h>
8+
#include <dice/events/memaccess.h>
89
#include <dice/events/pthread.h>
910
#include <dice/events/self.h>
1011
#include <dice/events/thread.h>
@@ -13,8 +14,15 @@
1314

1415
struct expected_event expected[] = {
1516
EXPECTED_EVENT(CAPTURE_EVENT, EVENT_SELF_INIT),
17+
EXPECTED_SUFFIX(CAPTURE_EVENT, EVENT_THREAD_START),
1618
EXPECTED_SUFFIX(CAPTURE_BEFORE, EVENT_PTHREAD_MUTEX_LOCK),
1719
EXPECTED_EVENT(CAPTURE_AFTER, EVENT_PTHREAD_MUTEX_LOCK),
20+
21+
// when checking coverage, these are also triggered
22+
EXPECTED_SOME(CAPTURE_EVENT, EVENT_MA_READ, 0, 1),
23+
EXPECTED_SOME(CAPTURE_EVENT, EVENT_MA_WRITE, 0, 1),
24+
// end
25+
1826
EXPECTED_EVENT(CAPTURE_BEFORE, EVENT_PTHREAD_MUTEX_UNLOCK),
1927
EXPECTED_EVENT(CAPTURE_AFTER, EVENT_PTHREAD_MUTEX_UNLOCK),
2028
EXPECTED_SUFFIX(CAPTURE_EVENT, EVENT_SELF_FINI),

test/traces/trace_mutex_2.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@ struct expected_event expected_1[] = {
8282
EXPECTED_EVENT(CAPTURE_BEFORE, EVENT_THREAD_JOIN),
8383
EXPECTED_EVENT(CAPTURE_AFTER, EVENT_THREAD_JOIN),
8484

85-
#if defined(__linux__)
8685
EXPECTED_EVENT(CAPTURE_EVENT, EVENT_STACKTRACE_EXIT),
87-
EXPECTED_EVENT(CAPTURE_EVENT, EVENT_THREAD_EXIT),
88-
EXPECTED_EVENT(CAPTURE_EVENT, EVENT_SELF_FINI),
89-
EXPECTED_END,
90-
#elif defined(__NetBSD__)
9186
EXPECTED_SUFFIX(CAPTURE_EVENT, EVENT_THREAD_EXIT),
9287
EXPECTED_SUFFIX(CAPTURE_EVENT, EVENT_SELF_FINI),
88+
89+
#if defined(__linux__)
90+
EXPECTED_END,
91+
#elif defined(__NetBSD__)
9392
EXPECTED_ANY_SUFFIX,
9493
#endif
9594
};

0 commit comments

Comments
 (0)