3

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

1 回答 1

3

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

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>
            )
于 2019-04-01T12:33:06.090 回答