Skip to content

Commit

Permalink
Fixup [TO SQUASH]
Browse files Browse the repository at this point in the history
  • Loading branch information
ligurio committed Aug 20, 2023
1 parent 8f83548 commit fdb538f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
36 changes: 30 additions & 6 deletions cmake/SetClangRTLibDir.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# The function sets the given variable in a parent scope to a
# value in CLANG_RT_LIB_DIR environment variable if it
# value in FUZZER_NO_MAIN_LIBRARY 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.
# libclang_rt.fuzzer_no_main library is composed manually.
# Function raises a fatal message if C compiler is not Clang.
#
# On Linux installations libFuzzer library is typically located at:
#
# /usr/lib/<llvm-version>/lib/clang/<clang-version>/lib/linux/libclang_rt.fuzzer_no_main-<architecture>.a
#
# Location is LLVM_LIBRARY_DIRS/clang/<version>/lib/<OS>/,
# for example LLVM_LIBRARY_DIRS/clang/4.0.0/lib/darwin/.
#
# 1. https://llvm.org/docs/LibFuzzer.html#using-libfuzzer-as-a-library

function(SetClangRTLibDir outvar)
set(ClangRTLibDir $ENV{CLANG_RT_LIB_DIR})
function(SetFuzzerNoMainLibPath outvar)
if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "C compiler is not a Clang")
endif ()

set(ClangRTLibDir $ENV{FUZZER_NO_MAIN_LIBRARY})

if (NOT ClangRTLibDir)
string(REPLACE "." ";" VERSION_LIST ${CMAKE_C_COMPILER_VERSION})
list(GET VERSION_LIST 0 CLANG_VERSION_MAJOR)
Expand All @@ -18,7 +29,20 @@ function(SetClangRTLibDir outvar)
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/")

if (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(ARCH "i386")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ARCH "x86_64")
endif ()

# find_package(LLVM 17 REQUIRED CONFIG)
# FIXME: Check presence of LLVM.
# set(LLVM_BASE "${LLVM_LIBRARY_DIRS}/clang/")

string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} OS_NAME)

set(ClangRTLibDir "/usr/lib/clang/${CLANG_VERSION}/lib/${OS_NAME}/libclang_rt.fuzzer_no_main-${ARCH}.a")
endif ()
set(${outvar} ${ClangRTLibDir} PARENT_SCOPE)
message(STATUS "[SetClangRTLibDir] ${outvar} is ${ClangRTLibDir}")
Expand Down
6 changes: 2 additions & 4 deletions luzer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SetClangRTLibDir(ClangRTLibraryDir)
SetFuzzerNoMainLibPath(FUZZER_NO_MAIN_LIBRARY)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/version.c
Expand All @@ -16,12 +16,10 @@ 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}
-fsanitize=fuzzer-no-link
clang_rt.fuzzer_no_main-x86_64
${FUZZER_NO_MAIN_LIBRARY}
)
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
-D_FORTIFY_SOURCE=2
Expand Down

0 comments on commit fdb538f

Please sign in to comment.