• 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=edge,indirect-calls")
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  AccumulateAllocationsTest
66  BufferOverflowOnInput
67  CallerCalleeTest
68  CounterTest
69  CustomCrossOverTest
70  CustomMutatorTest
71  EmptyTest
72  FourIndependentBranchesTest
73  FullCoverageSetTest
74  InitializeTest
75  MemcmpTest
76  LeakTest
77  LeakTimeoutTest
78  NullDerefTest
79  NullDerefOnEmptyTest
80  NthRunCrashTest
81  OneHugeAllocTest
82  OutOfMemoryTest
83  RepeatedMemcmp
84  SimpleCmpTest
85  SimpleDictionaryTest
86  SimpleFnAdapterTest
87  SimpleHashTest
88  SimpleTest
89  SimpleThreadedTest
90  SpamyTest
91  StrcmpTest
92  StrncmpTest
93  SwitchTest
94  ThreadedTest
95  TimeoutTest
96  )
97
98if(APPLE)
99  # LeakSanitizer is not supported on OSX right now
100  set(HAS_LSAN 0)
101  message(WARNING "LeakSanitizer is not supported on Apple platforms."
102    " Building and running LibFuzzer LeakSanitizer tests is disabled."
103    )
104else()
105  set(HAS_LSAN 1)
106endif()
107
108foreach(Test ${Tests})
109  add_libfuzzer_test(${Test} SOURCES ${Test}.cpp)
110endforeach()
111
112###############################################################################
113# AFL Driver test
114###############################################################################
115
116add_executable(AFLDriverTest
117  AFLDriverTest.cpp ../afl/afl_driver.cpp)
118
119set_target_properties(AFLDriverTest
120    PROPERTIES RUNTIME_OUTPUT_DIRECTORY
121    "${CMAKE_BINARY_DIR}/lib/Fuzzer/test"
122    )
123set(TestBinaries ${TestBinaries} AFLDriverTest)
124
125###############################################################################
126# Unit tests
127###############################################################################
128
129add_executable(LLVMFuzzer-Unittest
130  FuzzerUnittest.cpp
131  FuzzerFnAdapterUnittest.cpp
132  )
133
134target_link_libraries(LLVMFuzzer-Unittest
135  gtest
136  gtest_main
137  LLVMFuzzerNoMain
138  )
139
140target_include_directories(LLVMFuzzer-Unittest PRIVATE
141  "${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include"
142  )
143
144set(TestBinaries ${TestBinaries} LLVMFuzzer-Unittest)
145set_target_properties(LLVMFuzzer-Unittest
146  PROPERTIES RUNTIME_OUTPUT_DIRECTORY
147  "${CMAKE_CURRENT_BINARY_DIR}"
148)
149###############################################################################
150# Additional tests
151###############################################################################
152
153include_directories(..)
154
155if(APPLE)
156  message(WARNING "DataflowSanitizer is not supported on Apple platforms."
157    " Building and running LibFuzzer DataflowSanitizer tests is disabled."
158    )
159  set(HAS_DFSAN 0)
160else()
161  set(HAS_DFSAN 1)
162  add_subdirectory(dfsan)
163endif()
164
165add_subdirectory(uninstrumented)
166add_subdirectory(no-coverage)
167add_subdirectory(ubsan)
168add_subdirectory(trace-bb)
169add_subdirectory(trace-pc)
170
171###############################################################################
172# Configure lit to run the tests
173#
174# Note this is done after declaring all tests so we can inform lit if any tests
175# need to be disabled.
176###############################################################################
177
178configure_lit_site_cfg(
179  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
180  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
181  )
182
183configure_lit_site_cfg(
184  ${CMAKE_CURRENT_SOURCE_DIR}/unit/lit.site.cfg.in
185  ${CMAKE_CURRENT_BINARY_DIR}/unit/lit.site.cfg
186  )
187
188add_lit_testsuite(check-fuzzer "Running Fuzzer tests"
189    ${CMAKE_CURRENT_BINARY_DIR}
190    DEPENDS ${TestBinaries} FileCheck not
191    )
192