Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -fno-rtti/fno-exceptions based on LLVM's configuration #1291

Merged
merged 22 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bdbae40
Modified CMakeLists.txt to take whether or not to disable RTTI & exce…
Johanmyst Dec 19, 2023
3959742
Fixed setting of C++ standard and removed requirement for LLVM to be …
Johanmyst Dec 20, 2023
2c574c6
Fix matching parameters and return values for "OVERWRITE" functions a…
shuangxiangkan Dec 18, 2023
c24615f
Remove getPointerElementType()
shuangxiangkan Dec 18, 2023
8f01b0e
SVF code formatter
yuleisui Dec 18, 2023
9ef3df0
Take LLVMContext object from module used to build SVFModule rather th…
Johanmyst Dec 19, 2023
6e83d12
Merge branch 'fix_ctx_src'
Johanmyst Dec 20, 2023
ca5632c
Replaced fatal error for LLVM versions greater than 15 with a warning…
Johanmyst Dec 20, 2023
58a4ffc
Switched adding LLVM definitions, link directories, and include direc…
Johanmyst Dec 20, 2023
af15d88
Switched to CMake-recommended way of linking libraries (through `targ…
Johanmyst Dec 20, 2023
3f51410
Add the tools through `add_llvm_executable`; modify build/setup scrip…
Johanmyst Dec 20, 2023
d0a1b83
Apparently this is not the case for older CMake versions; explicitly …
Johanmyst Dec 20, 2023
1fd01be
Tests require specific output directory for tools
Johanmyst Dec 20, 2023
6cc852b
Merge branch 'fix_rtti_eh_from_llvm'
Johanmyst Dec 20, 2023
6f1f351
Merge branch 'SVF-tools:master' into fix_rtti_eh_from_llvm
Johanmyst Dec 20, 2023
d4e6d6b
Merge branch 'SVF-tools:master' into master
Johanmyst Dec 20, 2023
97d624d
Updated CMake build system to install correctly; moved more LLVM-only…
Johanmyst Dec 22, 2023
d8670ae
Merge branch 'SVF-tools:master' into master
Johanmyst Dec 22, 2023
fabc7df
Merge remote-tracking branch 'origin/master' into fix_rtti_eh_from_ll…
Johanmyst Dec 22, 2023
d85eacb
Removed repetitive setting of C++ standard to top-level CMakeLists.tx…
Johanmyst Dec 28, 2023
1f54a14
FIX: Replaced setting `CXX_STANDARD` (and related) with `CMAKE_CXX_ST…
Johanmyst Dec 28, 2023
1063318
Merge branch 'SVF-tools:master' into fix_rtti_eh_from_llvm
Johanmyst Dec 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 87 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,86 @@ project("SVF")
configure_file(${PROJECT_SOURCE_DIR}/.config.in
${PROJECT_BINARY_DIR}/include/Util/config.h)

# 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can adjust the language standard here, like:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

Those properties will be effective for all new targets by default.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Johanmyst could you add the options here and remove set_property in svf-llvm/CMakLiist.txt and svf/CMakeList.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. 👍

# add -std=gnu++14
set(CMAKE_CXX_EXTENSIONS ON)
# Match configuration used to build LLVM (match C++ standard; match
# runtime typing information (RTTI); match exception handling; etc)
if(COMMAND add_llvm_library)
message(STATUS "Detected in-tree build configuration; skipping LLVM fetching")
set(IN_SOURCE_BUILD 1)
else()
message(STATUS "Detected out-of-tree build configuration; fetching LLVM")

add_compile_options("-fno-rtti")
add_compile_options("-fno-exceptions")
Comment on lines -15 to -16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to enable svfcore as -fno-rtti/fno-exceptions, these two options should also be in the root/CMakeList or svf/CMakeList?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this was why I initially moved the LLVM finding into the top-level CMakeLists.txt since settings these options is based on whether the found LLVM instance supports them. I changed it now so that the CMakeLists.txt in svf-llvm sets a variable in its parent scope, which is then checked in the top-level CMakeLists.txt to conditionally set the flags for all targets.

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}"
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a second look, the current cmakelist in the root folder was not supposed to have anything related to LLVM and those LLVM options should be moved to svf-llvm/CMakelist.txt. I assume this change should be straight-forward?

SVF itself is now designed language-independent, and we may have another module later (e.g., svf-xxx) which may not rely on llvm-based language, so a clean root cmakelist would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright; I moved this check to the top-level CMakeLists.txt because that was where flags like -fno-exceptions or -fno-rtti were added. I've moved this check back to the script in the svf-llvm directory; the flags for -fno-rtti or -fno-exceptions are now only added to SvfLLVM (if users want the main library to be built with it they can just specify it through $CFLAGS or $CXXFLAGS).


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()

if(NOT LLVM_ENABLE_EH)
message(STATUS "Building SVF without exception handling")
add_compile_options("-fno-exceptions")
endif()

if(NOT LLVM_ENABLE_RTTI)
message(STATUS "Building SVF without RTII")
add_compile_options("-fno-rtti")
endif()

if(NOT ${LLVM_LINK_LLVM_DYLIB})
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)
else()
message(STATUS "Linking to LLVM dynamic shared library object")
set(llvm_libs LLVM)
endif()

# Make the "add_llvm_library()" command available
include(AddLLVM)

# Set the default C++ standard used to build SVF; LLVM versions up to version 6 defaulted to C++98,
# and up to (and including) version 15 the default was C++14. From versions 16 and onwards, the
# default C++ standard has been C++17. Build SVF with C++14 unless the LLVM version is below 6.
if (LLVM_VERSION_MAJOR VERSION_LESS 6)
set(CMAKE_CXX_STANDARD 98)
message(STATUS "Got LLVM version ${LLVM_VERSION}; using C++ standard: ${CMAKE_CXX_STANDARD}")
elseif(LLVM_VERSION_MAJOR VERSION_LESS 16)
set(CMAKE_CXX_STANDARD 14)
message(STATUS "Got LLVM version ${LLVM_VERSION}; using C++ standard: ${CMAKE_CXX_STANDARD}")
else()
set(CMAKE_CXX_STANDARD 17)
message(WARNING "Found unsupported LLVM version (got version ${LLVM_VERSION}; currently supported "
"versions are up to 15); using C++ standard: ${CMAKE_CXX_STANDARD}")
endif()

# Always enable extensions and require a compiler that supports at least the set C++ standard
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

endif()

# Treat compiler warnings as errors
add_compile_options("-Werror" "-Wall")
Expand Down Expand Up @@ -80,11 +151,18 @@ endif()
add_subdirectory(svf)
add_subdirectory(svf-llvm)

include(GNUInstallDirs)
install(
TARGETS SvfCore SvfLLVM
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/svf/include/
${PROJECT_SOURCE_DIR}/svf-llvm/include/
COMPONENT devel
DESTINATION include/svf
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/include/svf
FILES_MATCHING
PATTERN "**/*.h")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also move the following to svf-llvm/CMakeList.txt?

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
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also moved the installation commands to the subdirectory so the SvfLLVM target isn't referenced at all in the top-level CMakeLists.txt anymore.

While doing that I also fixed the remaining installation predicates:

  1. <svf_root>/svf/include (i.e. its contents) is installed to <install_prefix>/include/svf/
  2. <svf_root>/svf-llvm/include (i.e. the SVF-LLVM directory therein) is installed to <install_prefix>/include/svf/
  3. <svf_root/[Debug|Release]-Build/include/Util/config.h is installed to <install_prefix>/include/svf/Util/config.h
  4. <svf_root/[Debug|Release]-Build/svf/libSvfCore.[a|so] is installed to <install_prefix>/lib/libSvfCore.[a|so]
  5. <svf_root/[Debug|Release]-Build/svf-llvm/libSvfLLVM.[a|so] is installed to <install_prefix>/lib/libSvfLLVM[a|so]
  6. <svf_root>/[Debug|Release]-Build/svf-llvm/extapi.bc is installed to <install_prefix>/include/svf/SVF-LLVM/extapi.bc (I thought this was the best place as placing it by itself in <install_prefix>/lib/extapi.bc so that it's placed alongside libSVF.[a|so] seemed inconvenient)
  7. Tools from <svf_root>/svf-llvm/tools/... (built to <svf_root>/[Debug|Release]-Build/bin/...) are installed to <install_prefix>/bin/...

This should be unnecessary for the way SVF is currently designed to be used (i.e. through the build.sh and setup.sh scripts), but it allows users who want to install SVF to a specific location to not have to manually link/include the different directories/library files so explicitly.

Expand Down
53 changes: 5 additions & 48 deletions svf-llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
# 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()

if(NOT LLVM_ENABLE_EH)
add_compile_options("-fno-exceptions")
message(STATUS "Disable exceptions")
endif()

list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(AddLLVM)

add_definitions(${LLVM_DEFINITIONS})
# include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})

if(LLVM_LINK_LLVM_DYLIB)
set(llvm_libs LLVM)
else()
llvm_map_components_to_libnames(
llvm_libs
analysis
bitwriter
core
instcombine
instrumentation
ipo
irreader
linker
scalaropts
support
target
transformutils)
endif()
else()
set(IN_SOURCE_BUILD 1)
endif()

# SVF-LLVM contains LLVM Libs
file(GLOB SVFLLVM_SOURCES lib/*.cpp)

add_llvm_library(SvfLLVM ${SVFLLVM_SOURCES})

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(SvfLLVM SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(SvfLLVM PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(SvfLLVM PUBLIC ${LLVM_DEFINITIONS})

target_include_directories(SvfLLVM PUBLIC include)
target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} ${Z3_LIBRARIES} SvfCore)

Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/CFL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ if(DEFINED IN_SOURCE_BUILD)
else()
add_executable(cfl cfl.cpp)

target_link_libraries(cfl SvfLLVM ${llvm_libs})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(cfl PUBLIC include)
target_include_directories(cfl SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(cfl PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(cfl PUBLIC ${LLVM_DEFINITIONS})

target_link_libraries(cfl SvfCore SvfLLVM)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like every tool has these repetitive lines. Could we move them to parent cmakelist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course; I did this because I thought the linking was going wrong but on my system this is difficult to reproduce as my LLVM instance's library directory is also in $LD_LIBRARY_PATH, so I cannot reproduce the issue with finding libLLVM.[so|a]. Especially since the CMake output during the git workflow don't include the build command itself, so I can't see if the issue is due to the rpath not being set right, or if somehow link_directories() is not propagating to targets defined in subdirectories.

Either way, it is not necessary to explicitly link against the LLVM library anyway since the library is defined using add_llvm_library, so I removed this part. Moreover, since SVF aims to support building in- and out-of-tree, the SVF-LLVM tools can/should also be defined using add_llvm_executable or even add_llvm_tool (I didn't do the latter for now as that means they only get built when specific build flags are set for LLVM). This simplifies the linking & the makefiles a bit since there is no longer a need for in-tree detection and what not.

Note that I've also modified build.sh and setup.sh to not only append LLVM's binary directory (for local installations by the build script) to the $PATH variable, but also its library directories to $LD_LIBRARY_PATH so that the linker can reliable find the libraries (idem ditto for Z3). I also did the same for the library files produced by SVF so targets linking against SVF can find them easier. Please let me know if you think this should be done differently!


set_target_properties(cfl PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/DDA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ if(DEFINED IN_SOURCE_BUILD)
else()
add_executable(dvf dda.cpp)

target_link_libraries(dvf SvfLLVM ${llvm_libs})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(dvf PUBLIC include)
target_include_directories(dvf SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(dvf PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(dvf PUBLIC ${LLVM_DEFINITIONS})

target_link_libraries(dvf SvfCore SvfLLVM)

set_target_properties(dvf PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/Example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ if(DEFINED IN_SOURCE_BUILD)
else()
add_executable(svf-ex svf-ex.cpp)

target_link_libraries(svf-ex SvfLLVM ${llvm_libs})
target_link_libraries(svf-ex SvfCore SvfLLVM)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(svf-ex PUBLIC include)
target_include_directories(svf-ex SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(svf-ex PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(svf-ex PUBLIC ${LLVM_DEFINITIONS})

set_target_properties(svf-ex PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/LLVM2SVF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ if(DEFINED IN_SOURCE_BUILD)
else()
add_executable(llvm2svf llvm2svf.cpp)

target_link_libraries(llvm2svf SvfLLVM ${llvm_libs})
target_link_libraries(llvm2svf SvfCore SvfLLVM)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(llvm2svf PUBLIC include)
target_include_directories(llvm2svf SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(llvm2svf PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(llvm2svf PUBLIC ${LLVM_DEFINITIONS})

set_target_properties(llvm2svf PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/MTA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ else()
add_executable(mta mta.cpp LockResultValidator.cpp MTAResultValidator.cpp
MTAAnnotator.cpp)

target_link_libraries(mta SvfLLVM ${llvm_libs})
target_link_libraries(mta SvfCore SvfLLVM)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(mta PUBLIC include)
target_include_directories(mta SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(mta PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(mta PUBLIC ${LLVM_DEFINITIONS})

set_target_properties(mta PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/SABER/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ if(DEFINED IN_SOURCE_BUILD)
else()
add_executable(saber saber.cpp)

target_link_libraries(saber SvfLLVM ${llvm_libs})
target_link_libraries(saber SvfCore SvfLLVM)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(saber PUBLIC include)
target_include_directories(saber SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(saber PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(saber PUBLIC ${LLVM_DEFINITIONS})

set_target_properties(saber PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
8 changes: 7 additions & 1 deletion svf-llvm/tools/WPA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ if(DEFINED IN_SOURCE_BUILD)
else()
add_executable(wpa wpa.cpp)

target_link_libraries(wpa SvfLLVM ${llvm_libs} Threads::Threads)
target_link_libraries(wpa SvfCore SvfLLVM Threads::Threads)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
target_include_directories(wpa PUBLIC include)
target_include_directories(wpa SYSTEM PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_directories(wpa PUBLIC ${LLVM_LIBRARY_DIRS})
target_compile_definitions(wpa PUBLIC ${LLVM_DEFINITIONS})

set_target_properties(wpa PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/bin)
Expand Down
4 changes: 2 additions & 2 deletions svf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Due to a mutual dependencies, all the sub projects of the SVG are merged here
# Otherwise it is impossible to load the dependencies in opt.
# NOTE: if the SVF should be linked into opt, we should probably use the
# Otherwise it is impossible to load the dependencies in opt.
# NOTE: if the SVF should be linked into opt, we should probably use the
# individual sub projects here, rather than the combined project.

file(
Expand Down
Loading