From c1d737a5adfac404f2a7339be27d236676bb0a3b Mon Sep 17 00:00:00 2001 From: JasonZhongZexin Date: Mon, 4 Jan 2021 03:02:44 +0000 Subject: [PATCH] update template --- .gitignore | 2 ++ CMakeLists.txt | 57 ++++++++++++++++++++++++++++++++++++++ README.md | 43 +++++++++++++++++++++++++++++ build.sh | 6 ++++ env.sh | 21 ++++++++++++++ src/CMakeLists.txt | 10 +++++++ src/svf-ex.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100755 build.sh create mode 100755 env.sh create mode 100644 src/CMakeLists.txt create mode 100644 src/svf-ex.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c7ddedc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +BUILD/ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a7665f8 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,57 @@ +cmake_minimum_required(VERSION 3.4.3) +if (DEFINED LLVM_DIR) + set(ENV{LLVM_DIR} "${LLVM_DIR}") + endif() + if (DEFINED ENV{LLVM_DIR}) + # We need to match the build environment for LLVM: + # In particular, we need C++11 and the -fno-rtti flag + set(CMAKE_CXX_STANDARD 14) + if(CMAKE_BUILD_TYPE MATCHES "Debug") + set(CMAKE_CXX_FLAGS "-fPIC -std=gnu++14 -O0 -fno-rtti -Wno-deprecated") + else() + set(CMAKE_CXX_FLAGS "-fPIC -std=gnu++14 -O3 -fno-rtti -Wno-deprecated") + endif() + set(CMAKE_C_FLAGS "-fPIC") + +find_package(LLVM REQUIRED CONFIG) + + list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") + include(AddLLVM) + + add_definitions(${LLVM_DEFINITIONS}) + include_directories(${LLVM_INCLUDE_DIRS}) + +else() + message(FATAL_ERROR "\ +WARNING: The LLVM_DIR var was not set (required for an out-of-source build)!\n\ +Please set this to environment variable to point to the LLVM build directory\ +(e.g. on linux: export LLVM_DIR=/path/to/llvm/build/dir)") +endif() + +if (EXISTS "${SVF_DIR}") +else() + set(SVF_DIR $ENV{SVF_DIR}) + if(EXISTS "${SVF_DIR}") + else() + message(FATAL_ERROR "\ +WARNING: The SVF_DIR var was not set (required for an out-of-source build)!\n\ +Please set this to environment variable to point to the SVF_DIR directory or set this variable to cmake configuration\n +(e.g. on linux: export SVF_DIR=/path/to/SVF/dir) \n or \n \n(make the project via: cmake -DSVF_DIR=your_path_to_SVF) ") + endif() +endif() + +if(CMAKE_BUILD_TYPE MATCHES "Debug") + MESSAGE (STATUS "building SVF in debug mode") + set(SVF_LIB "${SVF_DIR}/Debug-build/lib/libSvf.a") + set(LLVMCudd "${SVF_DIR}/Debug-build/lib/CUDD/libCudd.a") +else() + MESSAGE (STATUS "building SVF in release mode") + set(SVF_LIB "${SVF_DIR}/Release-build/lib/libSvf.a") + set(LLVMCudd "${SVF_DIR}/Release-build/lib/CUDD/libCudd.a") +endif() +set(SVF_HEADER "${SVF_DIR}/include") +include_directories(${SVF_HEADER}) + + + +add_subdirectory(src) diff --git a/README.md b/README.md index e876cd5..a0a4145 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ # SVF-Teaching Teaching Software Analysis via SVF +# Control Flow Reachability Assignment +In this assignment, you are requested to complete a function which will output the path between the source node and sink node. + +## 1. Setup the assignment environment +This assignment requested the supports of the svf library. To compile the execuateable, you need to setup the svf library on your device. + + + Install svf-lib using npm (tested on both Ubuntu and MacOS) +``` +npm i --silent svf-lib --prefix ${HOME} +``` + + +After the svf-lib has been installed, you can run the **env. sh** script which is in your assignment folder to setup the environment. +``` +source ./env.sh +``` + +While the environment has been setup, you are now able to start your assignment. + +## 2. Coding your solution +For this assignment, you are requested to design a function which will query all reachable paths between the source and sink function. +``` +src() is the source function. +sink() is the sink function. +``` +## 3. Build and submit your solution +Your submission requests 2 components which are your solution codes and the execuateable file of your solution. + +Once you complete the assignment, you need to run the **build. sh** script. This script will build your solution automatically and generate the execuateable file which called **svf-ex**. +``` +./build.sh +``` + +Finally, you are requested to commit and push your code and the execuateable file via the git command. +``` +git add +git commit -m'' +git push +``` +*Note: It is important to submit the execuateable file. Otherwise, your submission will not be marked by the system.* + +After the submission, you work will be automatically mark by the system and you are able to checked your mark at the assignment github repository. \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..538d0a5 --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +mkdir build +cd build +cmake .. +make +cd .. +mv ./build/bin/svf-ex ./ \ No newline at end of file diff --git a/env.sh b/env.sh new file mode 100755 index 0000000..9601f0f --- /dev/null +++ b/env.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +PROJECTHOME=$(pwd) +sysOS=`uname -s` +LLVMHome="llvm-10.0.0.obj" +install_path=`npm root` +export LLVM_DIR=$HOME/$LLVMHome +export PATH=$LLVM_DIR/bin:$PATH +export PATH=$PROJECTHOME/bin:$PATH +echo "export LLVM_DIR=$HOME/$LLVMHome" >> ~/.bashrc +echo "export PATH=$LLVM_DIR/bin:$PROJECTHOME/bin:$PATH" >> ~/.bashrc +if [[ $sysOS == "Darwin" ]] +then + export SVF_DIR=$install_path/SVF/ +elif [[ $sysOS == "Linux" ]] +then + export SVF_DIR=$install_path/SVF/ +fi + +echo "LLVM_DIR="$LLVM_DIR +echo "SVF_DIR="$SVF_DIR \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..c3ae56b --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,10 @@ +llvm_map_components_to_libnames(llvm_libs bitwriter core ipo irreader instcombine instrumentation target linker analysis scalaropts support ) +file (GLOB SOURCES + *.cpp +) +add_executable(svf-ex ${SOURCES}) + +target_link_libraries(svf-ex ${SVF_LIB} ${LLVMCudd} ${llvm_libs}) + +set_target_properties( svf-ex PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) diff --git a/src/svf-ex.cpp b/src/svf-ex.cpp new file mode 100644 index 0000000..48d82c8 --- /dev/null +++ b/src/svf-ex.cpp @@ -0,0 +1,68 @@ +//===- svf-ex.cpp -- A driver example of SVF-------------------------------------// +// +// SVF: Static Value-Flow Analysis +// +// Copyright (C) <2013-> +// + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +//===-----------------------------------------------------------------------===// + +/* + // A driver program of SVF including usages of SVF APIs + // + // Author: Yulei Sui, + */ + +#include "SVF-FE/LLVMUtil.h" +#include "Graphs/SVFG.h" +#include "WPA/Andersen.h" +#include "SABER/LeakChecker.h" +#include "SVF-FE/PAGBuilder.h" + +using namespace SVF; +using namespace llvm; +using namespace std; + +static llvm::cl::opt InputFilename(cl::Positional, + llvm::cl::desc(""), llvm::cl::init("-")); + +static llvm::cl::opt LEAKCHECKER("leak", llvm::cl::init(false), + llvm::cl::desc("Memory Leak Detection")); + +ICFGNode *srcNode = NULL; +ICFGNode *sinkNode = NULL; +PAG *pag; +std::set path_worklist; + +int main(int argc, char ** argv) { + + int arg_num = 0; + char **arg_value = new char*[argc]; + std::vector moduleNameVec; + SVFUtil::processArguments(argc, argv, arg_num, arg_value, moduleNameVec); + cl::ParseCommandLineOptions(arg_num, arg_value, + "Whole Program Points-to Analysis\n"); + + SVFModule* svfModule = LLVMModuleSet::getLLVMModuleSet()->buildSVFModule(moduleNameVec); + + /// Build Program Assignment Graph (PAG) + PAGBuilder builder; + pag = builder.build (svfModule); + pag->dump ("pag"); + + + return 0; +}