Skip to content

Commit

Permalink
Add -fno-rtti/fno-exceptions based on LLVM's configuration (#1291)
Browse files Browse the repository at this point in the history
* Modified CMakeLists.txt to take whether or not to disable RTTI & exception handling from LLVM's configuration, rather than hardcoding

* Fixed setting of C++ standard and removed requirement for LLVM to be built in debug-mode when SVF is being built in debug-mode

* Fix matching parameters and return values for "OVERWRITE" functions and add some new functions sepcfications in extapi.c

* Remove getPointerElementType()

The getPointerElementType will be deprecated in future versions of LLVM. Considering compatibility, avoid using getPointerElementType()->isIntegerTy(8) to determine if the arguments and return values are of the void * type.

* SVF code formatter

* Take LLVMContext object from module used to build SVFModule rather than forcing unique_ptr to avoid erronous garbage collection clearing LLVM internal objects when SVF is used from within an LLVM pass

* Replaced fatal error for LLVM versions greater than 15 with a warning to cater to future LLVM 16 support

* Switched adding LLVM definitions, link directories, and include directories from only to `SvfLLVM` to global additions so that modules defined after `SvfLLVM` or in its subdirectories automatically inherit/can find LLVM as well

* Switched to CMake-recommended way of linking libraries (through `target_link_libraries()`); linked SvfCore and SvfLLVM for tools; LLVM link directory should be added to rpath for tools

* Add the tools through `add_llvm_executable`; modify build/setup scripts to also include the installed LLVM/Z3 instances in $LD_LIBRARY_PATH (same for compiled SVF libraries)

* Apparently this is not the case for older CMake versions; explicitly link LLVM libraries to avoid undefined symbols

* Tests require specific output directory for tools

* Updated CMake build system to install correctly; moved more LLVM-only variables to the CMakeLists.txt script in svf-llm/; modified generated configuration variables in config.h (and updated ExtAPI.h/ExtAPI.cpp accordingly); removed unnecessary exports from build script; added additional configuration information to config.h

* Removed repetitive setting of C++ standard to top-level CMakeLists.txt script; removed target-specific setting of C++ standard

* FIX: Replaced setting `CXX_STANDARD` (and related) with `CMAKE_CXX_STANDARD`

---------

Co-authored-by: shuangxiang kan <[email protected]>
Co-authored-by: GitHub Actions Build <[email protected]>
  • Loading branch information
3 people authored Dec 28, 2023
1 parent dfeb685 commit 52b7034
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 186 deletions.
19 changes: 16 additions & 3 deletions .config.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#ifndef CONFIG_H_IN
#define CONFIG_H_IN

#define PROJECT_PATH "@CMAKE_CURRENT_SOURCE_DIR@"
#define BUILD_TYPE "@CMAKE_BUILD_TYPE@-build"
#define EXTAPI_DIR PROJECT_PATH "/@CMAKE_BUILD_TYPE@-build/svf-llvm"
// Directory structure of SVF build
#define SVF_ROOT "@CMAKE_CURRENT_SOURCE_DIR@"
#define SVF_BUILD_DIR "@CMAKE_CURRENT_BINARY_DIR@"
#define SVF_INSTALL_DIR "@CMAKE_INSTALL_PREFIX@"
#define SVF_BIN_DIR SVF_INSTALL_DIR "/bin"
#define SVF_LIB_DIR SVF_INSTALL_DIR "/lib"
#define SVF_INCLUDE_DIR SVF_INSTALL_DIR "/include"
#define SVF_EXTAPI_DIR SVF_INCLUDE_DIR "/SVF-LLVM"
#define SVF_EXTAPI_BC SVF_EXTAPI_DIR "/extapi.bc"

// Build properties of current SVF build
#define SVF_BUILD_TYPE "@CMAKE_BUILD_TYPE@"
#cmakedefine SVF_ASSERT_MODE
#cmakedefine SVF_COVERAGE
#cmakedefine SVF_SANITIZE "@SVF_SANITIZE@"


#endif
52 changes: 24 additions & 28 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@ cmake_minimum_required(VERSION 3.13.4)

project("SVF")

configure_file(${PROJECT_SOURCE_DIR}/.config.in
${PROJECT_BINARY_DIR}/include/Util/config.h)
message(STATUS "Using CMake with generator: ${CMAKE_GENERATOR}")

# We need to match the build environment for LLVM: In particular, we need C++14
# and the -fno-rtti flag
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# add -std=gnu++14
set(CMAKE_CXX_EXTENSIONS ON)

add_compile_options("-fno-rtti")
add_compile_options("-fno-exceptions")
configure_file(${PROJECT_SOURCE_DIR}/.config.in ${PROJECT_BINARY_DIR}/include/Util/config.h)

# Treat compiler warnings as errors
add_compile_options("-Werror" "-Wall")
add_compile_options("-Werror" "-Wall" "-Wno-deprecated-declarations")

# Build SVF with C++ standard C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Keep assertions enabled if requested
option(SVF_ENABLE_ASSERTIONS "Always enable assertions")
Expand Down Expand Up @@ -80,20 +76,20 @@ endif()
add_subdirectory(svf)
add_subdirectory(svf-llvm)

# Whether RTTI/Exceptions are enabled currently depends on whether the LLVM instance used to build
# SVF had them enabled; since the LLVM instance is found in the "svf-llvm" subdirectory, it sets the
# below variables in its parent directory (i.e. for this CMakeLists.txt) so check them here
if(SVF_NO_RTTI)
add_compile_options("-fno-rtti")
endif()

if(SVF_NO_EH)
add_compile_options("-fno-exceptions")
endif()

# Install generated configuration header (see `configure_file()`) to top-level include dir of SVF
include(GNUInstallDirs)
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/svf/include/
${PROJECT_SOURCE_DIR}/svf-llvm/include/
COMPONENT devel
DESTINATION include/svf
FILES_MATCHING
PATTERN "**/*.h")

# Compile extapi.c to extapi.bc
find_path(LLVM_CLANG_DIR
NAMES clang llvm
HINTS ${LLVM_DIR} ENV LLVM_DIR
PATH_SUFFIXES bin)
add_custom_target(extapi_ir ALL
COMMAND ${LLVM_CLANG_DIR}/clang -w -S -c -Xclang -disable-O0-optnone -fno-discard-value-names -emit-llvm ${PROJECT_SOURCE_DIR}/svf-llvm/lib/extapi.c -o ${PROJECT_BINARY_DIR}/svf-llvm/extapi.bc
DEPENDS ${PROJECT_SOURCE_DIR}/svf-llvm/lib/extapi.c
)
FILES ${PROJECT_BINARY_DIR}/include/Util/config.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/svf/Util
)
5 changes: 4 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ if [[ ! -d "$Z3_DIR" ]]; then
export Z3_DIR="$SVFHOME/$Z3Home"
fi

export PATH=$LLVM_DIR/bin:$PATH
# Add LLVM & Z3 to $PATH and $LD_LIBRARY_PATH (prepend so that selected instances will be used first)
PATH=$LLVM_DIR/bin:$Z3_DIR/bin:$PATH
LD_LIBRARY_PATH=$LLVM_DIR/lib:$Z3_BIN/lib:$LD_LIBRARY_PATH

echo "LLVM_DIR=$LLVM_DIR"
echo "Z3_DIR=$Z3_DIR"

Expand Down
12 changes: 9 additions & 3 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ fi

Build="${PTAOBJTY}-build"

export PATH=$LLVM_DIR/bin:$PATH
PTABIN=$SVF_DIR/$Build/bin
export PATH=$PTABIN:$PATH
# Add LLVM & Z3 to $PATH and $LD_LIBRARY_PATH (prepend so that selected instances will be used first)
export PATH=$LLVM_DIR/bin:$Z3_DIR/bin:$PATH
export LD_LIBRARY_PATH=$LLVM_DIR/lib:$Z3_BIN/lib:$LD_LIBRARY_PATH

# Add compiled SVF binaries dir to $PATH
export PATH=$SVF_DIR/$Build/bin:$PATH

# Add compiled library directories to $LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$SVF_DIR/$Build/svf:$SVF_DIR/$Build/svf-llvm:$LD_LIBRARY_PATH
149 changes: 106 additions & 43 deletions svf-llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,61 +1,124 @@
# To support both in- and out-of-source builds, we check for the presence of the
# add_llvm_loadable_module command. - if this command is not present, we are
# building out-of-source
if(NOT COMMAND add_llvm_library)
find_package(LLVM REQUIRED CONFIG HINTS "${LLVM_DIR}")
set(LLVM_DIR ${LLVM_BINARY_DIR} PARENT_SCOPE)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

if(NOT LLVM_ENABLE_RTTI)
add_compile_options("-fno-rtti")
message(STATUS "Disable RTTI")
endif()
# SVF-LLVM contains LLVM Libs
file(GLOB SVFLLVM_SOURCES lib/*.cpp)

if(NOT LLVM_ENABLE_EH)
add_compile_options("-fno-exceptions")
message(STATUS "Disable exceptions")
endif()
find_package(LLVM REQUIRED CONFIG HINTS ${LLVM_DIR} $ENV{LLVM_DIR})
message(STATUS "LLVM STATUS:
Version ${LLVM_VERSION}
Definitions ${LLVM_DEFINITIONS}
Includes ${LLVM_INCLUDE_DIRS}
Libraries ${LLVM_LIBRARY_DIRS}
Targets ${LLVM_TARGETS_TO_BUILD}
Build type ${LLVM_BUILD_TYPE}
Exceptions ${LLVM_ENABLE_EH}
RTTI ${LLVM_ENABLE_RTTI}
Dynamic lib ${LLVM_LINK_LLVM_DYLIB}"
)

list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(AddLLVM)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT ${LLVM_BUILD_TYPE} STREQUAL "Debug")
message(NOTICE "Building SVF in debug-mode but LLVM was not built in debug-mode; "
"debug information could be incomplete when using SVF from LLVM")
endif()

include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})

add_definitions(${LLVM_DEFINITIONS})
# include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
if(NOT LLVM_ENABLE_EH)
message(STATUS "Building SVF without exception handling")
set(SVF_NO_RTTI ON PARENT_SCOPE) # Set for parent scope because this needs to be set in top-level CMakeLists.txt
endif()

if(LLVM_LINK_LLVM_DYLIB)
set(llvm_libs LLVM)
if(NOT LLVM_ENABLE_RTTI)
message(STATUS "Building SVF without RTTI")
set(SVF_NO_EH ON PARENT_SCOPE) # Set for parent scope because this needs to be set in top-level CMakeLists.txt
endif()

if(LLVM_LINK_LLVM_DYLIB)
message(STATUS "Linking to LLVM dynamic shared library object")
set(llvm_libs LLVM)

# Set which components to include in the dynamic library to include the new SvfLLVM
if (LLVM_DYLIB_COMPONENTS)
message(STATUS "Appending SvfLLVM to LLVM dynamic library components")
list(APPEND LLVM_DYLIB_COMPONENTS SvfLLVM)
else()
llvm_map_components_to_libnames(
llvm_libs
analysis
bitwriter
core
instcombine
instrumentation
ipo
irreader
linker
scalaropts
support
target
transformutils)
message(STATUS "Adding all;SvfLLVM to LLVM dynamic library components (was unset)")
set(LLVM_DYLIB_COMPONENTS all;SvfLLVM)
endif()
else()
set(IN_SOURCE_BUILD 1)
message(STATUS "Linking to separate LLVM static libraries")
llvm_map_components_to_libnames(
llvm_libs
analysis
bitwriter
core
instcombine
instrumentation
ipo
irreader
linker
scalaropts
support
target
transformutils
)
endif()

# SVF-LLVM contains LLVM Libs
file(GLOB SVFLLVM_SOURCES lib/*.cpp)
# Make the "add_llvm_library()" command available and configure LLVM/CMake
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(AddLLVM)

add_llvm_library(SvfLLVM ${SVFLLVM_SOURCES})
target_include_directories(SvfLLVM SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_include_directories(SvfLLVM PUBLIC include)
target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} ${Z3_LIBRARIES} SvfCore)

if(DEFINED IN_SOURCE_BUILD)
# Add intrinsics_gen target if we're building as part of LLVM source build
if(TARGET intrinsics_gen)
add_dependencies(SvfLLVM intrinsics_gen)
endif()

add_subdirectory(tools)

# Compile extapi.c to extapi.bc
set(EXTAPI_TO_BC_FLAGS
"-w"
"-S"
"-c"
"-Xclang"
"-disable-O0-optnone"
"-fno-discard-value-names"
"-emit-llvm"
"${CMAKE_CURRENT_LIST_DIR}/lib/extapi.c"
"-o"
"${CMAKE_BINARY_DIR}/svf-llvm/extapi.bc"
)
find_program(LLVM_CLANG
NAMES clang
HINTS ${LLVM_BINARY_DIR}
PATH_SUFFIXES bin
REQUIRED
)
add_custom_target(extapi_ir ALL
COMMAND ${LLVM_CLANG} ${EXTAPI_TO_BC_FLAGS}
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/lib/extapi.c
)

# Install the headers (as a subdirectory of the main SVF include) and the compiled library binaries
include(GNUInstallDirs)
install(
TARGETS SvfLLVM
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
FILES ${CMAKE_BINARY_DIR}/svf-llvm/extapi.bc
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/svf/SVF-LLVM
)
install(
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/svf
FILES_MATCHING
PATTERN "**/*.h"
)

12 changes: 2 additions & 10 deletions svf-llvm/tools/CFL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
if(DEFINED IN_SOURCE_BUILD)
add_llvm_tool(cfl cfl.cpp)
else()
add_executable(cfl cfl.cpp)

target_link_libraries(cfl SvfLLVM ${llvm_libs})

set_target_properties(cfl PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
endif()
add_llvm_executable(cfl cfl.cpp)
target_link_libraries(cfl PUBLIC ${llvm_libs} SvfLLVM)
31 changes: 15 additions & 16 deletions svf-llvm/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
if(DEFINED IN_SOURCE_BUILD)
set(LLVM_LINK_COMPONENTS
BitWriter
Core
IPO
IrReader
InstCombine
Instrumentation
Target
Linker
Analysis
ScalarOpts
Support
SvfLLVM)
endif()

add_subdirectory(SABER)
add_subdirectory(WPA)
add_subdirectory(Example)
add_subdirectory(DDA)
add_subdirectory(MTA)
add_subdirectory(CFL)
add_subdirectory(LLVM2SVF)

# Build SvfLLVM tools with C++ standard C++17
set_target_properties(
cfl dvf svf-ex llvm2svf mta saber wpa
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

include(GNUInstallDirs)
install(
TARGETS cfl dvf svf-ex llvm2svf mta saber wpa
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
12 changes: 2 additions & 10 deletions svf-llvm/tools/DDA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
if(DEFINED IN_SOURCE_BUILD)
add_llvm_tool(dvf dda.cpp)
else()
add_executable(dvf dda.cpp)

target_link_libraries(dvf SvfLLVM ${llvm_libs})

set_target_properties(dvf PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
endif()
add_llvm_executable(dvf dda.cpp)
target_link_libraries(dvf PUBLIC ${llvm_libs} SvfLLVM)
12 changes: 2 additions & 10 deletions svf-llvm/tools/Example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
if(DEFINED IN_SOURCE_BUILD)
add_llvm_tool(svf-ex svf-ex.cpp)
else()
add_executable(svf-ex svf-ex.cpp)

target_link_libraries(svf-ex SvfLLVM ${llvm_libs})

set_target_properties(svf-ex PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
endif()
add_llvm_executable(svf-ex svf-ex.cpp)
target_link_libraries(svf-ex PUBLIC ${llvm_libs} SvfLLVM)
12 changes: 2 additions & 10 deletions svf-llvm/tools/LLVM2SVF/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
if(DEFINED IN_SOURCE_BUILD)
add_llvm_tool(llvm2svf llvm2svf.cpp)
else()
add_executable(llvm2svf llvm2svf.cpp)

target_link_libraries(llvm2svf SvfLLVM ${llvm_libs})

set_target_properties(llvm2svf PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
endif()
add_llvm_executable(llvm2svf llvm2svf.cpp)
target_link_libraries(llvm2svf PUBLIC ${llvm_libs} SvfLLVM)
14 changes: 2 additions & 12 deletions svf-llvm/tools/MTA/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
if(DEFINED IN_SOURCE_BUILD)
add_llvm_tool(mta mta.cpp LockResultValidator.cpp MTAResultValidator.cpp
MTAAnnotator.cpp)
else()
add_executable(mta mta.cpp LockResultValidator.cpp MTAResultValidator.cpp
MTAAnnotator.cpp)

target_link_libraries(mta SvfLLVM ${llvm_libs})

set_target_properties(mta PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
endif()
add_llvm_executable(mta mta.cpp LockResultValidator.cpp MTAResultValidator.cpp MTAAnnotator.cpp)
target_link_libraries(mta PUBLIC ${llvm_libs} SvfLLVM)
Loading

0 comments on commit 52b7034

Please sign in to comment.