• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Build all these tests with -O0, otherwise optimizations may merge some
2# basic blocks and we'll fail to discover the targets.
3# We change the flags for every build type because we might be doing
4# a multi-configuration build (e.g. Xcode) where CMAKE_BUILD_TYPE doesn't
5# mean anything.
6set(variables_to_filter
7  CMAKE_CXX_FLAGS_RELEASE
8  CMAKE_CXX_FLAGS_DEBUG
9  CMAKE_CXX_FLAGS_RELWITHDEBINFO
10  CMAKE_CXX_FLAGS_MINSIZEREL
11  LIBFUZZER_FLAGS_BASE
12  )
13foreach (VARNAME ${variables_to_filter})
14  string(REPLACE " " ";" BUILD_FLAGS_AS_LIST "${${VARNAME}}")
15  set(new_flags "")
16  foreach (flag ${BUILD_FLAGS_AS_LIST})
17    # NOTE: Use of XX here is to avoid a CMake warning due to CMP0054
18    if (NOT ("XX${flag}" MATCHES "XX-O[0123s]"))
19      set(new_flags "${new_flags} ${flag}")
20    else()
21      set(new_flags "${new_flags} -O0")
22    endif()
23  endforeach()
24  set(${VARNAME} "${new_flags}")
25endforeach()
26
27# Enable the coverage instrumentation (it is disabled for the Fuzzer lib).
28set(CMAKE_CXX_FLAGS "${LIBFUZZER_FLAGS_BASE} -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp,trace-div,trace-gep -g")
29
30# add_libfuzzer_test(<name>
31#   SOURCES source0.cpp [source1.cpp ...]
32#   )
33#
34#   Declares a LibFuzzer test executable with target name LLVMFuzzer-<name>.
35#
36#   One or more source files to be compiled into the binary must be declared
37#   after the SOURCES keyword.
38function(add_libfuzzer_test name)
39  set(multi_arg_options "SOURCES")
40  cmake_parse_arguments(
41    "add_libfuzzer_test" "" "" "${multi_arg_options}" ${ARGN})
42  if ("${add_libfuzzer_test_SOURCES}" STREQUAL "")
43    message(FATAL_ERROR "Source files must be specified")
44  endif()
45  add_executable(LLVMFuzzer-${name}
46    ${add_libfuzzer_test_SOURCES}
47    )
48  target_link_libraries(LLVMFuzzer-${name} LLVMFuzzer)
49  # Place binary where llvm-lit expects to find it
50  set_target_properties(LLVMFuzzer-${name}
51    PROPERTIES RUNTIME_OUTPUT_DIRECTORY
52    "${CMAKE_BINARY_DIR}/lib/Fuzzer/test"
53    )
54  set(TestBinaries ${TestBinaries} LLVMFuzzer-${name} PARENT_SCOPE)
55endfunction()
56
57# Variable to keep track of all test targets
58set(TestBinaries)
59
60###############################################################################
61# Basic tests
62###############################################################################
63
64set(Tests
65  AbsNegAndConstantTest
66  AbsNegAndConstant64Test
67  AccumulateAllocationsTest
68  BufferOverflowOnInput
69  CallerCalleeTest
70  CounterTest
71  CustomCrossOverTest
72  CustomMutatorTest
73  DivTest
74  EmptyTest
75  FourIndependentBranchesTest
76  FullCoverageSetTest
77  InitializeTest
78  MemcmpTest
79  LeakTest
80  LeakTimeoutTest
81  LoadTest
82  NullDerefTest
83  NullDerefOnEmptyTest
84  NthRunCrashTest
85  OneHugeAllocTest
86  OutOfMemoryTest
87  OutOfMemorySingleLargeMallocTest
88  RepeatedMemcmp
89  RepeatedBytesTest
90  SimpleCmpTest
91  SimpleDictionaryTest
92  SimpleHashTest
93  SimpleTest
94  SimpleThreadedTest
95  SingleMemcmpTest
96  SingleStrcmpTest
97  SingleStrncmpTest
98  SpamyTest
99  ShrinkControlFlowTest
100  ShrinkValueProfileTest
101  StrcmpTest
102  StrncmpOOBTest
103  StrncmpTest
104  StrstrTest
105  SwapCmpTest
106  SwitchTest
107  Switch2Test
108  ThreadedLeakTest
109  ThreadedTest
110  TimeoutTest
111  TimeoutEmptyTest
112  TraceMallocTest
113  )
114
115if(APPLE)
116  # LeakSanitizer is not supported on OSX right now
117  set(HAS_LSAN 0)
118  message(WARNING "LeakSanitizer is not supported on Apple platforms."
119    " Building and running LibFuzzer LeakSanitizer tests is disabled."
120    )
121else()
122  set(HAS_LSAN 1)
123endif()
124
125foreach(Test ${Tests})
126  add_libfuzzer_test(${Test} SOURCES ${Test}.cpp)
127endforeach()
128
129
130###############################################################################
131# Unit tests
132###############################################################################
133
134add_executable(LLVMFuzzer-Unittest
135  FuzzerUnittest.cpp
136  )
137
138add_executable(LLVMFuzzer-StandaloneInitializeTest
139  InitializeTest.cpp
140  ../standalone/StandaloneFuzzTargetMain.c
141  )
142
143target_link_libraries(LLVMFuzzer-Unittest
144  gtest
145  gtest_main
146  LLVMFuzzerNoMain
147  )
148
149target_include_directories(LLVMFuzzer-Unittest PRIVATE
150  "${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include"
151  )
152
153set(TestBinaries ${TestBinaries} LLVMFuzzer-Unittest)
154set_target_properties(LLVMFuzzer-Unittest
155  PROPERTIES RUNTIME_OUTPUT_DIRECTORY
156  "${CMAKE_CURRENT_BINARY_DIR}"
157)
158
159set(TestBinaries ${TestBinaries} LLVMFuzzer-StandaloneInitializeTest)
160set_target_properties(LLVMFuzzer-StandaloneInitializeTest
161  PROPERTIES RUNTIME_OUTPUT_DIRECTORY
162  "${CMAKE_CURRENT_BINARY_DIR}"
163)
164
165###############################################################################
166# Additional tests
167###############################################################################
168
169include_directories(..)
170
171# add_subdirectory(uninstrumented)
172add_subdirectory(no-coverage)
173add_subdirectory(ubsan)
174
175add_library(LLVMFuzzer-DSO1 SHARED DSO1.cpp)
176add_library(LLVMFuzzer-DSO2 SHARED DSO2.cpp)
177
178add_executable(LLVMFuzzer-DSOTest
179  DSOTestMain.cpp
180  DSOTestExtra.cpp)
181
182target_link_libraries(LLVMFuzzer-DSOTest
183  LLVMFuzzer-DSO1
184  LLVMFuzzer-DSO2
185  LLVMFuzzer
186  )
187
188set_target_properties(LLVMFuzzer-DSOTest PROPERTIES RUNTIME_OUTPUT_DIRECTORY
189  "${CMAKE_BINARY_DIR}/lib/Fuzzer/test")
190set_target_properties(LLVMFuzzer-DSO1 PROPERTIES LIBRARY_OUTPUT_DIRECTORY
191  "${CMAKE_BINARY_DIR}/lib/Fuzzer/lib")
192set_target_properties(LLVMFuzzer-DSO2 PROPERTIES LIBRARY_OUTPUT_DIRECTORY
193  "${CMAKE_BINARY_DIR}/lib/Fuzzer/lib")
194
195set(TestBinaries ${TestBinaries} LLVMFuzzer-DSOTest)
196
197###############################################################################
198# Configure lit to run the tests
199#
200# Note this is done after declaring all tests so we can inform lit if any tests
201# need to be disabled.
202###############################################################################
203
204configure_lit_site_cfg(
205  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
206  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
207  )
208
209configure_lit_site_cfg(
210  ${CMAKE_CURRENT_SOURCE_DIR}/unit/lit.site.cfg.in
211  ${CMAKE_CURRENT_BINARY_DIR}/unit/lit.site.cfg
212  )
213
214add_lit_testsuite(check-fuzzer "Running Fuzzer tests"
215    ${CMAKE_CURRENT_BINARY_DIR}
216    DEPENDS ${TestBinaries} FileCheck not
217    )
218