gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:42 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com