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

时间:2019-04-01 09:56:21

标签: c cmake libfuzzer

我有一个使用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

1 个答案:

答案 0 :(得分:0)

我忘记为链接器添加选项。

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>
            )