如何创建最小的 libfuzzer cmake 示例?

How to create minimal libfuzzer cmake example?

我有一个 libFuzzer 用法的简单示例。

// Test_fuzzer.cc
#include <stdint.h>
#include <stddef.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  if (size > 0 && data[0] == 'H')
    if (size > 1 && data[1] == 'I')
       if (size > 2 && data[2] == '!')
       __builtin_trap();
  return 0;
}

我可以用 clang 和 运行 编译它。

clang -g -O1 -fsanitize=fuzzer test_fuzzer.cc //OK

现在我想在这个例子中添加cmake。

// CMakeLists.txt file:
cmake_minimum_required (VERSION 3.11)
project (Tutorial)

set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O1 -fsanitize=fuzzer")
add_executable(Tutorial test_fuzzer.cc)
cmake . //OK
make

但是我得到一个错误。如何解决?

/usr/bin/ld: /usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: 
in function `_start': //why gcc, how to force cmake to use clang ????
(.text+0x24): undefined reference to `main'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

生成 VERBOSE=1 个结果

[root@8c80cf55eaa2 test_cmake]# make VERBOSE=1'
[ 50%] Linking CXX executable Tutorial
/usr/bin/cmake -E cmake_link_script CMakeFiles/Tutorial.dir/link.txt --verbose=1
/usr/bin/clang     CMakeFiles/Tutorial.dir/test_fuzzer.cc.o  -o Tutorial 
/usr/bin/ld: /usr/bin/../lib/gcc/x86_64-redhat-linux/8/../../../../lib64/crt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/Tutorial.dir/build.make:84: Tutorial] Error 1
make[2]: Leaving directory '/home/test_cmake'
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/Tutorial.dir/all] Error 2

我忘了给链接器添加选项。

cmake_minimum_required (VERSION 3.11)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang)
project (Tutorial)


add_executable(Tutorial test_fuzzer.cc)
target_compile_options(Tutorial
            PRIVATE $<$<C_COMPILER_ID:Clang>:-g -O1 -fsanitize=fuzzer>
            )

target_link_libraries(Tutorial
            PRIVATE $<$<C_COMPILER_ID:Clang>:-fsanitize=fuzzer>
            )