SVF-tools / SVF

Static Value-Flow Analysis Framework for Source Code
http://svf-tools.github.io/SVF/
Other
1.39k stars 434 forks source link

Cannot buildSVFModule after pulling latest commit (from 9658ef4 to the latest) #1250

Closed jjang3 closed 9 months ago

jjang3 commented 9 months ago

Hello, the problem is basically as the title states..

I used the commit 9658ef4 for a while and then decided to upgrade to the latest, but now it seems like this commit doesn't work with my simple code to build SVF module using buildSVFModule (understandably since it's been a while I suppose).

I understand it is probably difficult to provide the exact solution, but what is the good place to start my debugging process to fix this? (or I guess I need to keep using the old commit...)

Here is the code:

StringSet<> SVFAnalysis::analyzeModule(Module &M, ModuleAnalysisManager &MAM) 
{
    SPDLOG_INFO("");
    StringSet<> funResult;

    std::vector<std::string> moduleNameVec;
    auto fileName = M.getSourceFileName();
    auto bitcodeName = M.getModuleIdentifier();
    moduleNameVec.push_back(bitcodeName);

    // cout_dbg is llvm::raw_ostream &cout_dbg = llvm::errs();
    cout_dbg << "File name: " << fileName << "\nBitcode name: " << bitcodeName << "\n";

    // Build Program Assignment Graph (SVFIR)
    PTACallGraph* callgraph = new PTACallGraph();
    // ExtAPI::setExtBcPath(bitcodeName); // I tried using this command after searching through the SVF issues, but didn't fix it
    SVFModule* svfModule = LLVMModuleSet::getLLVMModuleSet()->buildSVFModule(moduleNameVec);
    // SVFIRBuilder builder(svfModule);
    // SVFIR* pag = builder.build();

    /// Create Andersen's pointer analysis
    // Andersen* ander = AndersenWaveDiff::createAndersenWaveDiff(pag);

    return funResult;
}

The output looks like this:

File name: /home/input/epoll.c
Bitcode name: /home/result/epoll/epoll.bc
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/llvm-project-13/llvm-build/bin/opt -load /home/build/lib/libsvfCustom.so -load-pass-plugin /home/build/lib/libsvfCustom.so -passes=svfCustom -S /home/result/epoll/epoll.bc -o /home/result/epoll/epoll_svf.out
 #0 0x0000555557813010 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055555781094e SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ffff7f9e140 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x13140)
 #3 0x00007ffff7ba9ab8 ./string/../sysdeps/x86_64/multiarch/strlen-evex.S:77:0
 #4 0x00007ffff7ea0855 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x134855)
 #5 0x00007ffff747127c getFilePath(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/SVF/svf/lib/Util/ExtAPI.cpp:100:38
 #6 0x00007ffff74714e4 SVF::ExtAPI::getExtBcPath[abi:cxx11]() /home/SVF/svf/lib/Util/ExtAPI.cpp:143:21
 #7 0x00007ffff77f2d3d SVF::LLVMModuleSet::loadExtAPIModules() /home/SVF/svf-llvm/lib/LLVMModule.cpp:534:51
 #8 0x00007ffff77f0834 SVF::LLVMModuleSet::buildSVFModule(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) /home/SVF/svf-llvm/lib/LLVMModule.cpp:106:29
 #9 0x00007ffff79ac992 SVFAnalysis::analyzeModule(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) /home/src/svf-icfg.cpp:25:91

And yes, bitcode file exists:

~ » file /home/result/epoll/epoll.bc                           
 /home/result/epoll/epoll.bc: LLVM IR bitcode

I really appreciate any help you can provide. Kind regards.

yuleisui commented 9 months ago

Not quite sure this is an llvm error. Could you try to build svf using llvm-14?

jjang3 commented 9 months ago

Not quite sure this is an llvm error. Could you try to build svf using llvm-14?

Thank you for the response!

So I tried to build SVF scratch using the LLVM 14, but the issue still remains, as shown below. I don't think I am doing anything too crazy either, as I am simply creating a dynamic library of SVF following the method here: https://github.com/SVF-tools/SVF/pull/1229 and then linking that library with my own LLVM pass in the CMakeFile. I guess I will need to use the old commit for now or figure out an alternative method of using the SVF library unless anyone else has a suggestion I could try.

-- Found Z3: /home/z3-build/lib/libz3.so
-- Z3 include dir: /home/z3-build/include
-- Found LLVM 14.0.0
-- Using LLVMConfig.cmake in: /home/llvm-project-14/llvm-build/lib/cmake/llvm
-- Disable RTTI
-- Disable exceptions
-- Linker detection: GNU ld
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Build spdlog: 1.12.0
-- Build type: Debug
-- Configuring done
-- Generating done
-- Build files have been written to: /home/build
[  1%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/stdout_sinks.cpp.o
[  1%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/spdlog.cpp.o
[  2%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/color_sinks.cpp.o
[  3%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/file_sinks.cpp.o
[  4%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/async.cpp.o
[  4%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/cfg.cpp.o
[  5%] Building CXX object spdlog/CMakeFiles/spdlog.dir/src/bundled_fmtlib_format.cpp.o
[  5%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/BoundedZ3Expr.cpp.o
[  6%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/ConsExeState.cpp.o
[  7%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/ExeState.cpp.o
[  7%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/IntervalExeState.cpp.o
[  8%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/RelExeState.cpp.o
[  9%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/RelationSolver.cpp.o
...
[ 66%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/Util/cJSON.cpp.o
[ 67%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/WPA/Andersen.cpp.o
[ 67%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/WPA/AndersenSCD.cpp.o
[ 69%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/WPA/AndersenStat.cpp.o
[ 68%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/WPA/AndersenSFR.cpp.o
[ 69%] Building CXX object SVF/svf/CMakeFiles/SvfCore.dir/lib/WPA/AndersenWaveDiff.cpp.o
...
[ 77%] Linking CXX shared library ../../lib/libSvfCore.so
[ 78%] Built target SvfCore
...
[ 86%] Linking CXX shared library ../../lib/libSvfLLVM.so
[ 86%] Built target SvfLLVM
[ 86%] Building CXX object SVF/svf-llvm/tools/SABER/CMakeFiles/saber.dir/saber.cpp.o
[ 86%] Building CXX object src/CMakeFiles/arcs.dir/arcs-main.cpp.o
[ 87%] Building CXX object SVF/svf-llvm/tools/WPA/CMakeFiles/wpa.dir/wpa.cpp.o
[ 88%] Building CXX object src/CMakeFiles/arcs.dir/arcs-icfg.cpp.o
[ 88%] Building CXX object SVF/svf-llvm/tools/Example/CMakeFiles/svf-ex.dir/svf-ex.cpp.o
[ 89%] Building CXX object SVF/svf-llvm/tools/DDA/CMakeFiles/dvf.dir/dda.cpp.o
[ 90%] Building CXX object SVF/svf-llvm/tools/MTA/CMakeFiles/mta.dir/mta.cpp.o
[ 90%] Building CXX object SVF/svf-llvm/tools/MTA/CMakeFiles/mta.dir/LockResultValidator.cpp.o
[ 90%] Building CXX object SVF/svf-llvm/tools/LLVM2SVF/CMakeFiles/llvm2svf.dir/llvm2svf.cpp.o
[ 90%] Building CXX object SVF/svf-llvm/tools/CFL/CMakeFiles/cfl.dir/cfl.cpp.o
File name: /home/input/epoll.c
Bitcode name: /home/result/epoll/epoll.bc
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/llvm-project-14/llvm-build/bin/opt -load /home/build/lib/libsvfCustom.so -load-pass-plugin /home/build/lib/libsvfCustom.so -passes=svfCustom -S /home/result/epoll/epoll.bc -o /home/result/epoll/epoll_svf.out
 #0 0x0000555557813010 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055555781094e SignalHandler(int) Signals.cpp:0:0
 #0 0x000055555798e740 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055555798befe SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ffff7f9e140 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x13140)
 #3 0x00007ffff7ba9ab8 ./string/../sysdeps/x86_64/multiarch/strlen-evex.S:77:0
 #4 0x00007ffff7ea0855 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x134855)
 #5 0x00007ffff746f27c getFilePath(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/SVF/svf/lib/Util/ExtAPI.cpp:100:38
 #6 0x00007ffff746f4e4 SVF::ExtAPI::getExtBcPath[abi:cxx11]() /home/SVF/svf/lib/Util/ExtAPI.cpp:143:21
 #7 0x00007ffff77f163d SVF::LLVMModuleSet::loadExtAPIModules() /home/SVF/svf-llvm/lib/LLVMModule.cpp:534:51
 #8 0x00007ffff77ef12e SVF::LLVMModuleSet::buildSVFModule(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) /home/SVF/svf-llvm/lib/LLVMModule.cpp:106:29

Thank you again for your time.

yuleisui commented 9 months ago

Could you follow a step-by-step building of the dynamic lib and link to your sample project using the dynamic lib?

jjang3 commented 9 months ago

Could you follow a step-by-step building of the dynamic lib and link to your sample project using the dynamic lib?

I apologize, but could you tell me what you're asking here? I followed a step-by-step direction on building a dynamic library and then linked it successfully to compile my project pass to ensure everything compiled successfully without errors. This took a bit of process as I could not find explicit direction on building the dynamic library unless I missed it (that's how I found that pull request as I was searching for the solution myself).

If I have to be more specific in terms of what I did:


This is the CMakeLists.txt in the root folder of my project:

cmake_minimum_required(VERSION 3.13.4)
project(simple_proj)

#===============================================================================
# SANITY CHECK
#===============================================================================
set(LT_LLVM_INSTALL_DIR $ENV{LLVM_DIR} CACHE PATH "LLVM installation directory")
...
#===============================================================================
# lOAD LLVM CONFIGURATION
#===============================================================================
list(APPEND CMAKE_PREFIX_PATH "${LT_LLVM_INSTALL_DIR}/lib/cmake/llvm/")

find_package(LLVM 14.0.0 REQUIRED CONFIG)
# Another sanity check
if(NOT "14" VERSION_EQUAL "${LLVM_VERSION_MAJOR}")
  message(FATAL_ERROR "Found LLVM ${LLVM_VERSION_MAJOR}, but need LLVM 14")
endif()

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LT_LLVM_INSTALL_DIR}")
...
# Set the LLVM header and library paths
get_filename_component(PARENT_DIR ../ ABSOLUTE)
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
link_directories(${LLVM_LIBRARY_DIRS})
add_definitions(${LLVM_DEFINITIONS})
...
# Include subdirectories. 
add_subdirectory(src) # my project source files
add_subdirectory(SVF)

and then inside src directory, I edited CMakeLists.txt to make sure I linked SvfLLVM properly

cmake_minimum_required(VERSION 3.13.4)
project(simple_proj)
...
find_package(LLVM REQUIRED CONFIG)
...
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS} 
"${CMAKE_SOURCE_DIR}/SVF/svf/include"
"${CMAKE_SOURCE_DIR}/SVF/svf-llvm/include"
"${CMAKE_SOURCE_DIR}/build/SVF/include"
"${CMAKE_SOURCE_DIR}/spdlog/include")
...
#===============================================================================
# ADD THE TARGET
#===============================================================================
set(PASSES
  src
    )

set(proj_SOURCES
  proj-main.cpp
  proj-icfg.cpp
  )
...
add_library( proj MODULE
  ${proj_HEADERS}
  ${proj_SOURCES}
)
...
target_link_libraries(proj PUBLIC
  # "${CMAKE_SOURCE_DIR}/build/SVF/svf/libSvfCore.a"
  SvfLLVM
)                                                                                                                                                                                                                                                                                                                                               

Lastly, I had to make a minor change in the SVF's CMakeLists.txt as explained here: https://github.com/SVF-tools/SVF/issues/53#issuecomment-433719659 because otherwise, there will be a weird linking error with CommandLine when you try to link your own LLVM pass with libSvfLLVM.so. Therefore, I removed ${llvm_lib} from target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} ${Z3_LIBRARIES} SvfCore), which solved that issue.

Edit: Just to add one last statement, I cmake'd by ensuring cmake -DBUILD_SHARED_LIBS=on ..

If I misunderstood your suggestion, please do let me know. Sincerely,

yuleisui commented 9 months ago

Could you use build.sh and generate the lib by turning this on https://github.com/SVF-tools/SVF/blob/3acbda776297fe4cdada061f13ded580ca061771/build.sh#L228?

provide a sample project (with your cpp file) using the generated svfllvm so file?

jjang3 commented 9 months ago

Ah.... that's what you meant; thank you for your clarification; I think I understood it. Yes, I think your suggestion worked, it seems like using the add_subdirectory to compile SVF was the problem. Just to be explicitly clear;

~/SVF (6c82edfb*) » bash build.sh                                                                                                            
LLVM_DIR=/home/llvm-project-14/llvm-build
Z3_DIR=/home/z3-build
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Z3: /home/z3-build/lib/libz3.so
-- Z3 include dir: /home/z3-build/include
-- Performing Test Terminfo_LINKABLE
-- Performing Test Terminfo_LINKABLE - Success
-- Found Terminfo: /usr/lib/x86_64-linux-gnu/libtinfo.so  
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.10") 
-- Found LLVM 14.0.0
...
-- Build files have been written to: /home/SVF/Release-build
[  0%] Building CXX object svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/BoundedZ3Expr.cpp.o
[  2%] Building CXX object svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/ExeState.cpp.o
[  2%] Building CXX object svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/ConsExeState.cpp.o
[  2%] Built target extapi_ir
[  3%] Building CXX object svf/CMakeFiles/SvfCore.dir/lib/AbstractExecution/IntervalExeState.cpp.o
...
[ 76%] Building CXX object svf/CMakeFiles/SvfCore.dir/lib/WPA/VersionedFlowSensitiveStat.cpp.o
[ 77%] Building CXX object svf/CMakeFiles/SvfCore.dir/lib/WPA/WPAPass.cpp.o
[ 78%] Linking CXX shared library libSvfCore.so
[ 78%] Built target SvfCore
...
[ 86%] Building CXX object svf-llvm/CMakeFiles/SvfLLVM.dir/lib/SymbolTableBuilder.cpp.o
[ 86%] Linking CXX shared library libSvfLLVM.so
[ 86%] Built target SvfLLVM
...
Setting up environment for SVF
SVF_DIR=/home/SVF
LLVM_DIR=/home/llvm-project-14/llvm-build
Z3_DIR=/home/z3-build

and after generating these .so files:

~/SVF (6c82edfb*) » find -iname libSvfLLVM.so                                                                                                     
./Release-build/svf-llvm/libSvfLLVM.so
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~/SVF (6c82edfb*) » find -iname libSvfCore.so                                                                                         
./Release-build/svf/libSvfCore.so

I put them into the src/CMakeLists.txt like this:

target_link_libraries(arcs PUBLIC
  "${CMAKE_SOURCE_DIR}/SVF/Release-build/svf-llvm/libSvfLLVM.so"
  "${CMAKE_SOURCE_DIR}/SVF/Release-build/svf/libSvfCore.so"
  "${src}"
)

Now it works:

File name: /home/input/epoll.c
Bitcode name: /home/result/epoll/epoll.bc

*********CallGraph Stats***************
################ (program : epoll.bc)###############
-------------------------------------------------------
TotalNode           34
TotalEdge           64
TotalCycle          0
MaxNodeInCycle      0
NodeInCycle         0
CalRetPairInCycle   0
#######################################################
...

I appreciate your patience, I hope this issue can help anyone else that may wish to incorporate SVF into their own custom pass (in terms of how to compile dynamically and link it in the CMake).