When using clang
as a compiler and trying to set CMAKE_C_FLAGS
to include -fsanitize=fuzzer
, generation fails with:
$ cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_C_FLAGS="-fsanitize=fuzzer"
-- The C compiler identification is Clang 14.0.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang - broken
CMake Error at /home/cristi/.local/lib/python3.9/site-packages/cmake/data/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message):
The C compiler
"/usr/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/cristi/sources/sample/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/gmake -f Makefile cmTC_da524/fast && /usr/bin/gmake -f CMakeFiles/cmTC_da524.dir/build.make CMakeFiles/cmTC_da524.dir/build
gmake[1]: Entering directory '/home/cristi/sources/sample/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_da524.dir/testCCompiler.c.o
/usr/bin/clang -fsanitize=fuzzer -MD -MT CMakeFiles/cmTC_da524.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_da524.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_da524.dir/testCCompiler.c.o -c /home/cristi/sources/sample/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_da524
/home/cristi/.local/lib/python3.9/site-packages/cmake/data/bin/cmake -E cmake_link_script CMakeFiles/cmTC_da524.dir/link.txt --verbose=1
/usr/bin/clang -fsanitize=fuzzer CMakeFiles/cmTC_da524.dir/testCCompiler.c.o -o cmTC_da524
/usr/bin/ld: CMakeFiles/cmTC_da524.dir/testCCompiler.c.o: in function `main':
testCCompiler.c:(.text.main[main]+0x0): multiple definition of `main'; /usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.fuzzer-x86_64.a(FuzzerMain.cpp.o):(.text.main+0x0): first defined here
/usr/bin/ld: /usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.fuzzer-x86_64.a(FuzzerMain.cpp.o): in function `main':
(.text.main+0x12): undefined reference to `LLVMFuzzerTestOneInput'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[1]: *** [CMakeFiles/cmTC_da524.dir/build.make:100: cmTC_da524] Error 1
gmake[1]: Leaving directory '/home/cristi/sources/sample/build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:127: cmTC_da524/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
-- Configuring incomplete, errors occurred!
See also "/home/cristi/sources/sample/build/CMakeFiles/CMakeOutput.log".
See also "/home/cristi/sources/sample/build/CMakeFiles/CMakeError.log".
The same thing happens if a CMakePresets.json
file is used. For reference, here is a minimal example:
main.c:
#include <stdint.h>
#include <stdlib.h>
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(sample C)
add_executable(sample main.c)
CMakePresets.json:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 22,
"patch": 0
},
"configurePresets": [
{
"name": "demo",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang",
"CMAKE_C_FLAGS": "-fsanitize=fuzzer"
}
}
],
"buildPresets": [
{
"name": "demo",
"configurePreset": "demo"
}
]
}
This is easy to workaround by setting the flags from the CMakeLists.txt
itself, but I’m just curious if doing things like this is supposed to be working.
I’m using cmake 3.22.0 on Ubuntu 22.04.2 LTS with clang 14.0.0.