Skip to content

Commit b63a2bf

Browse files
committed
Added example code
Basic example showing the main items.
1 parent ad0e72f commit b63a2bf

File tree

8 files changed

+94
-0
lines changed

8 files changed

+94
-0
lines changed

.clang-format

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
BasedOnStyle: LLVM
2+
Language: Cpp
3+
Standard: Cpp11
4+
IndentWidth: 4
5+
ColumnLimit: 100
6+
BinPackParameters: false
7+
AlwaysBreakTemplateDeclarations: true
8+
PenaltyReturnTypeOnItsOwnLine: 10000
9+
BreakConstructorInitializers: AfterColon
10+
ConstructorInitializerAllOnOneLineOrOnePerLine: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*build*/

example/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required( VERSION 3.10 )
2+
project( sanitizer-tests C CXX )
3+
4+
include(../c++-standards.cmake)
5+
include(../code-coverage.cmake)
6+
include(../sanitizers.cmake)
7+
include(../tools.cmake)
8+
9+
# Require C++11
10+
cxx_11()
11+
12+
enable_testing()
13+
14+
add_executable(tsanFail tsan_fail.cpp)
15+
target_code_coverage(tsanFail AUTO ALL)
16+
if(UNIX)
17+
target_link_libraries(tsanFail PUBLIC pthread)
18+
endif()
19+
add_test(tsan tsanFail)
20+
21+
add_executable(lsanFail lsan_fail.c)
22+
target_code_coverage(lsanFail AUTO ALL)
23+
add_test(lsan lsanFail)
24+
25+
if(USE_SANITIZER MATCHES "[Aa]ddress")
26+
add_executable(asanFail asan_fail.cpp)
27+
target_code_coverage(asanFail AUTO ALL)
28+
add_test(asan asanFail)
29+
endif()
30+
31+
add_executable(msanFail msan_fail.cpp)
32+
target_code_coverage(msanFail AUTO ALL)
33+
add_test(msan msanFail)
34+
35+
add_executable(ubsanFail ubsan_fail.cpp)
36+
target_code_coverage(ubsanFail AUTO ALL)
37+
add_test(ubsan ubsanFail)

example/asan_fail.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int main(int argc, char **argv) {
2+
int *array = new int[100];
3+
delete[] array;
4+
return array[argc]; // BOOM
5+
}

example/lsan_fail.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdlib.h>
2+
void *p;
3+
int main() {
4+
p = malloc(7);
5+
p = 0; // The memory is leaked here.
6+
return 0;
7+
}

example/msan_fail.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
int main(int argc, char **argv) {
4+
int *a = new int[10];
5+
a[5] = 0;
6+
if (a[argc])
7+
printf("xx\n");
8+
return 0;
9+
}

example/tsan_fail.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <functional>
2+
#include <map>
3+
#include <stdio.h>
4+
#include <string>
5+
#include <thread>
6+
7+
typedef std::map<std::string, std::string> map_t;
8+
9+
void *threadfunc(void *p) {
10+
map_t &m = *(map_t *)p;
11+
m["foo"] = "bar";
12+
return 0;
13+
}
14+
15+
int main() {
16+
map_t m;
17+
std::thread t(threadfunc, &m);
18+
printf("foo=%s\n", m["foo"].c_str());
19+
t.join();
20+
}

example/ubsan_fail.cpp

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

0 commit comments

Comments
 (0)