Clang 使用模糊器参数显示编译器错误

Clang showing compiler error with fuzzer argument

我正在尝试使用 libFuzzer 库进行试验并浏览玩具示例[1]。

keep-learnings-MacBook-Pro:Ccodeanalysis keep_learning$ cat Fuzzme.cpp 
#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;
}

keep-learnings-MacBook-Pro:Ccodeanalysis keep_learning$ clang++ -fsanitize=address,fuzzer Fuzzme.cpp 
ld: file not found: /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.fuzzer_osx.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)

keep-learnings-MacBook-Pro:Ccodeanalysis keep_learning$ clang++ --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

快速 Google 搜索显示了这个 [2],但除此之外我找不到任何有意义的信息来解决它,因此张贴在这里。有人可以告诉我如何解决这个问题吗?提前致谢。

[1] http://llvm.org/docs/LibFuzzer.html#toy-example
[2] https://bugs.llvm.org/show_bug.cgi?id=39794

正如您所注意到的,Apple 开发人员工具没有附带模糊器运行时。因此,您要么将此问题报告给 Apple 人员,要么自己从源代码(或两者)构建运行时库。

正如 Anton 所说,Apple Developer Tools 不包含模糊器库,让您从源代码编译,或询问 Apple。

原来LLVM also hosts pre-compiled binaries for some releases on their downloads page:

https://releases.llvm.org/download.html.

在该页面上,找到您的 LLVM 版本(例如 "Download LLVM 10.0.0"), and go a bit further until you see Pre-Built Binaries. Don't see binaries for your LLVM version? Pick the nearest lower version. The OP and I both have clang++ 10.0.1, so we'd pick 10.0.0

点击macOSlink下载,弹出终端解压并复制库,就大功告成了。我用一些环境变量(那些路径是杀手!)和一个 cp -n 来保留现有文件。

export CLANG_ROOT=clang+llvm-10.0.0-x86_64-apple-darwin/lib/clang/10.0.0
export XCODE_ROOT=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1

tar xvf clang+llvm-10.0.0-x86_64-apple-darwin.tar.xz  $CLANG_ROOT/include/fuzzer $CLANG_ROOT/lib/darwin

sudo cp -rn $CLANG_ROOT/include/fuzzer $XCODE_ROOT/include
sudo cp -n $CLANG_ROOT/lib/darwin/* $XCODE_ROOT/lib/darwin

我完全按照上面的方法做了,我的代码立即编译并 linked。