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

标签 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

最佳答案

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

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

关于c - 如何创建最小的 libfuzzer cmake 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55452391/

相关文章:

c - sem_unlink 权限被拒绝

c - 如何将 C 结构转换为 TLV 格式或平面内存缓冲区

cmake - 使用cmake,如何将external_project 语句构建的目标文件链接到另一个库?

cmake ctest 测试后删除文件

c++ - 为什么 ulimit -v 在 clang 的地址 sanitizer 下不起作用?

c - 使用 libfuzzer 在小端环境中模糊大端代码

c - 调用 execv 后出现奇怪的行为

c++ - Qt 和 CMake 因重复符号而失败

c++ - 如何在外部目标文件中调用重命名的符号?

c - 添加节点后返回指向链表开头的指针?