Skip to content

Commit

Permalink
cmake: fix searching Clang RT
Browse files Browse the repository at this point in the history
luzer module requires linking with a library
`clang_rt.fuzzer_no_main-x86_64` that is a part of a Clang runtime.
Without linking with it Lua runtime will report an error right on
loading `luzer.so`:

lua5.1: error loading module 'luzer' from file './luzer.so':
        ./luzer.so: undefined symbol: __sanitizer_cov_8bit_counters_init

The patch adds a module that composes a path to Clang runtime libraries
and adds this path to a library search paths. I suppose in some cases
introduced CMake function may fail. One can pass a path to a directory
with Clang RT manually using environment variable CLANG_RT_LIB_DIR.

In a usual case symbol __sanitizer_cov_8bit_counters_init is added by
compiler on instrumentation when compiler option
-fsanitize-coverage=inline-8bit-counters is specified [1].

1. https://clang.llvm.org/docs/SanitizerCoverage.html
  • Loading branch information
ligurio committed Aug 20, 2023
1 parent 40fe059 commit 6810291
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ project(luzer
VERSION "1.0.0"
)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(SetClangRTLibDir)

find_package(Lua 5.1 REQUIRED)
find_package(LLVM REQUIRED CONFIG)
find_library(LIBRT rt)

set(LUA_NAME "lua${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
find_program(LUA_EXECUTABLE "${LUA_NAME}")
Expand Down
25 changes: 25 additions & 0 deletions cmake/SetClangRTLibDir.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# The function sets the given variable in a parent scope to a
# value in CLANG_RT_LIB_DIR environment variable if it
# is set. Otherwise the value with path to a directory with
# Clang RT libraries is composed manually. Function raises
# a fatal message if C compiler is not Clang.

function(SetClangRTLibDir outvar)
set(ClangRTLibDir $ENV{CLANG_RT_LIB_DIR})
if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "C compiler is not a Clang")
endif ()
if (NOT ClangRTLibDir)
string(REPLACE "." ";" VERSION_LIST ${CMAKE_C_COMPILER_VERSION})
list(GET VERSION_LIST 0 CLANG_VERSION_MAJOR)
# Clang >= 15: /usr/lib/llvm-xxx/lib/clang/X/lib/linux/
# Clang < 15: /usr/lib/llvm-xxx/lib/clang/X.Y.Z/lib/linux/
set(CLANG_VERSION ${CMAKE_C_COMPILER_VERSION})
if (CLANG_VERSION_MAJOR GREATER 15)
set(CLANG_VERSION ${CLANG_VERSION_MAJOR})
endif ()
set(ClangRTLibDir "/usr/lib/llvm-${CLANG_VERSION_MAJOR}/lib/clang/${CLANG_VERSION}/lib/linux/")
endif ()
set(${outvar} ${ClangRTLibDir} PARENT_SCOPE)
message(STATUS "[SetClangRTLibDir] ${outvar} is ${ClangRTLibDir}")
endfunction()
20 changes: 4 additions & 16 deletions luzer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
# Locate compiler-rt libraries.
# Location is LLVM_LIBRARY_DIRS/clang/<version>/lib/<OS>/,
# for example LLVM_LIBRARY_DIRS/clang/4.0.0/lib/darwin/.
#
# See https://llvm.org/docs/LibFuzzer.html#using-libfuzzer-as-a-library

set(LLVM_BASE ${LLVM_LIBRARY_DIRS}/clang/${LLVM_PACKAGE_VERSION})
string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} OS_NAME)
set(LIBCLANG_RT ${LLVM_BASE}/lib/${OS_NAME}/libclang_rt.fuzzer_no_main-x86_64.a)
if(EXISTS ${LIBCLANG_RT})
message(STATUS "Found libclang_rt ${LIBCLANG_RT}")
else()
message(FATAL_ERROR "libclang_rt is not found")
endif()
SetClangRTLibDir(ClangRTLibraryDir)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/version.c
Expand All @@ -29,11 +16,12 @@ add_library(${CMAKE_PROJECT_NAME} SHARED ${LUZER_SOURCES})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
${LUA_INCLUDE_DIR}
)
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
${ClangRTLibraryDir})
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
${LUA_LIBRARIES}
${LIBRT}
${LIBCLANG_RT}
-fsanitize=fuzzer-no-link
clang_rt.fuzzer_no_main-x86_64
)
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
-D_FORTIFY_SOURCE=2
Expand Down

0 comments on commit 6810291

Please sign in to comment.