1if(CLANG_ENABLE_PROTO_FUZZER)
2  set(LLVM_LINK_COMPONENTS
3    Support
4    )
5
6  add_llvm_fuzzer(lldb-expression-fuzzer
7    EXCLUDE_FROM_ALL
8    lldb-expression-fuzzer.cpp
9    )
10
11  if(TARGET lldb-expression-fuzzer)
12    target_include_directories(lldb-expression-fuzzer PRIVATE ..)
13    find_package(Protobuf REQUIRED)
14    add_definitions(-DGOOGLE_PROTOBUF_NO_RTTI)
15    include_directories(${PROTOBUF_INCLUDE_DIRS})
16    include_directories(${CMAKE_CURRENT_BINARY_DIR}/../../../../clang/tools/clang-fuzzer PRIVATE ..)
17    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../clang/tools/clang-fuzzer)
18
19    set(CLANG_CMAKE_MODULE_PATH
20      ${CMAKE_CURRENT_SOURCE_DIR}/../../../../clang/cmake/modules)
21
22    set(CMAKE_MODULE_PATH
23      ${CMAKE_MODULE_PATH}
24      ${CLANG_CMAKE_MODULE_PATH})
25
26
27    set (PBM_PREFIX lldb_protobuf_mutator)
28    include(ProtobufMutator)
29    include_directories(${ProtobufMutator_INCLUDE_DIRS})
30
31    target_link_libraries(lldb-expression-fuzzer
32      PRIVATE
33      ${ProtobufMutator_LIBRARIES}
34      ${LLVM_LIB_FUZZING_ENGINE}
35      clangHandleCXX
36      clangCXXProto
37      clangProtoToCXX
38      liblldb
39      )
40
41    add_custom_command(TARGET lldb-expression-fuzzer PRE_BUILD
42      COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/fuzzer-artifacts/expression-artifacts
43      # Create and compile a simple C program using the command line. This is
44      # needed because LLDB's expression evaluator needs a legitmate target
45      # instead of a dummy target
46      COMMAND echo 'int main (int argc, char** argv) { return 0\; }' | clang -o main.out -xc -
47      )
48
49    # Create a directory for storing the fuzzer's artifacts and run the fuzzer with arguments that will
50    # not attempt to reduce the size of the inputs being generated
51    # Also set the executable that's created above as an environment variable for the
52    # source code to use
53    add_custom_target(fuzz-lldb-expression
54      COMMENT "Running the LLDB expression evaluator fuzzer..."
55      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/fuzzer-artifacts/expression-artifacts
56      COMMAND ${CMAKE_COMMAND} -E env LLDB_FUZZER_TARGET=${CMAKE_CURRENT_BINARY_DIR}/main.out $<TARGET_FILE:lldb-expression-fuzzer> -artifact_prefix=expression- -reduce_inputs=0
57      USES_TERMINAL
58      )
59  endif()
60endif()
61