Skip to content

Commit 0ea39ba

Browse files
authored
feat: add tests for sanitizers (#320)
* feat: add tests for sanitizers * test: add threadsan to sanitizer tests
1 parent c45e550 commit 0ea39ba

8 files changed

Lines changed: 101 additions & 5 deletions

File tree

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ add_subdirectory(fuzzing)
1111
add_subdirectory(gcc)
1212
add_subdirectory(gcc-arm-none-eabi)
1313
add_subdirectory(mutation)
14+
add_subdirectory(sanitizers)

test/CMakePresets.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"binaryDir": "${sourceDir}/build/${presetName}",
88
"hidden": true,
99
"cacheVariables": {
10-
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
1110
"CMAKE_BUILD_TYPE": "Debug"
1211
}
1312
},
@@ -63,14 +62,19 @@
6362
"targets": ["test-clang-cl"]
6463
},
6564
{
66-
"name": "clang-tidy",
65+
"name": "clang-iwyu",
6766
"configurePreset": "clang",
68-
"targets": ["test-clang-tidy"]
67+
"targets": ["test-clang-iwyu"]
6968
},
7069
{
71-
"name": "clang-iwyu",
70+
"name": "clang-sanitizers",
7271
"configurePreset": "clang",
73-
"targets": ["test-clang-iwyu"]
72+
"targets": ["test-asan", "test-memsan", "test-threadsan", "test-ubsan"]
73+
},
74+
{
75+
"name": "clang-tidy",
76+
"configurePreset": "clang",
77+
"targets": ["test-clang-tidy"]
7478
},
7579
{
7680
"name": "coverage",
@@ -97,6 +101,11 @@
97101
"configurePreset": "gcc",
98102
"targets": ["test-gcc-lld"]
99103
},
104+
{
105+
"name": "gcc-sanitizers",
106+
"configurePreset": "gcc",
107+
"targets": ["test-asan", "test-threadsan", "test-ubsan"]
108+
},
100109
{
101110
"name": "gcc-arm-none-eabi",
102111
"configurePreset": "gcc-arm-none-eabi",

test/sanitizers/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_executable(test-asan EXCLUDE_FROM_ALL asan.cpp)
2+
target_compile_options(test-asan PRIVATE -fsanitize=address -fno-omit-frame-pointer)
3+
target_link_options(test-asan PRIVATE -fsanitize=address)
4+
5+
add_executable(test-threadsan EXCLUDE_FROM_ALL threadsan.cpp)
6+
target_compile_options(test-threadsan PRIVATE -fsanitize=thread -fno-omit-frame-pointer)
7+
target_link_options(test-threadsan PRIVATE -fsanitize=thread)
8+
9+
add_executable(test-ubsan EXCLUDE_FROM_ALL ubsan.cpp)
10+
target_compile_options(test-ubsan PRIVATE -fsanitize=undefined -fno-omit-frame-pointer)
11+
target_link_options(test-ubsan PRIVATE -fsanitize=undefined)
12+
13+
add_executable(test-memsan EXCLUDE_FROM_ALL memsan.cpp)
14+
target_compile_options(test-memsan PRIVATE -fsanitize=memory -fno-omit-frame-pointer)
15+
target_link_options(test-memsan PRIVATE -fsanitize=memory)

test/sanitizers/asan.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <array>
2+
3+
int main()
4+
{
5+
std::array<int, 10> a = { 0 };
6+
return a[10];
7+
}

test/sanitizers/memsan.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int main()
2+
{
3+
int i;
4+
return i;
5+
}

test/sanitizers/threadsan.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <thread>
2+
3+
int main()
4+
{
5+
int i = 0;
6+
7+
std::thread t([&] { i = 10; });
8+
i = 20;
9+
t.join();
10+
11+
return i;
12+
}

test/sanitizers/ubsan.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int main()
2+
{
3+
const char* c = nullptr;
4+
return *c;
5+
}

test/testsuite.bats

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,45 @@ teardown() {
251251
assert_success
252252
assert_output --partial "Server Version:"
253253
}
254+
255+
# bats test_tags=tc:21
256+
@test "sanitizers should detect undefined or suspicious behavior in code compiled with gcc" {
257+
run cmake --preset gcc
258+
run cmake --build --preset gcc-sanitizers
259+
assert_success
260+
261+
run build/gcc/sanitizers/test-asan
262+
assert_failure
263+
assert_output --partial "AddressSanitizer: stack-buffer-overflow"
264+
265+
run build/gcc/sanitizers/test-threadsan
266+
assert_failure
267+
assert_output --partial "ThreadSanitizer: data race"
268+
269+
run build/gcc/sanitizers/test-ubsan
270+
assert_failure
271+
assert_output --partial "runtime error: load of null pointer"
272+
}
273+
274+
# bats test_tags=tc:22
275+
@test "sanitizers should detect undefined or suspicious behavior in code compiled with clang" {
276+
run cmake --preset clang
277+
run cmake --build --preset clang-sanitizers
278+
assert_success
279+
280+
run build/clang/sanitizers/test-asan
281+
assert_failure
282+
assert_output --partial "AddressSanitizer: stack-buffer-overflow"
283+
284+
run build/clang/sanitizers/test-memsan
285+
assert_failure
286+
assert_output --partial "MemorySanitizer: use-of-uninitialized-value"
287+
288+
run build/clang/sanitizers/test-threadsan
289+
assert_failure
290+
assert_output --partial "ThreadSanitizer: data race"
291+
292+
run build/clang/sanitizers/test-ubsan
293+
assert_failure
294+
assert_output --partial "runtime error: load of null pointer"
295+
}

0 commit comments

Comments
 (0)