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

Swift Taint Tests #592

Open
wants to merge 24 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f41460c
updated codestyle of swift tests
janniclas Feb 22, 2023
3397f94
added swift taint analysis tests
janniclas Feb 23, 2023
e068ca5
added todos in ifds taint analysis to support swift
janniclas Feb 23, 2023
9fefb37
updated taint unit tests for swift
janniclas Feb 23, 2023
ec95a84
updated swift taint tests to correctly generate ir which contains an …
janniclas Feb 24, 2023
298061f
removed old todo
janniclas Feb 24, 2023
9b0c422
using the getElementPtr workaround in taint analysis flow function
janniclas Feb 24, 2023
e0bbc70
summary function for Swift's String.append method
janniclas Mar 22, 2023
cadc004
experimenting with subscript method
janniclas Mar 26, 2023
7df29d0
Merge branch 'f-swift_code_style' into f-taintUpdate
janniclas Mar 26, 2023
f4b8c9a
handling insertvalue instructions in IFDSTaintAnalysis
janniclas Mar 26, 2023
83d763a
Merge branch 'f-taintUpdate' into f-swift_code_style
janniclas Mar 26, 2023
eedd758
merged development
janniclas Mar 26, 2023
cfbfe70
cmake Swift improvement
janniclas Mar 26, 2023
1974ba9
swift cmake improvement
janniclas Mar 29, 2023
494b053
updated swift codegen in cmake file so we do not compile the files wi…
janniclas Mar 29, 2023
41918e6
macos build fixes
janniclas Mar 29, 2023
b5d9bef
extended phasar macos install instructions
janniclas Mar 29, 2023
1e6c03d
Merge branch 'development' into f-swift_code_style
janniclas Mar 30, 2023
c709281
updated LDFLAGS and CPPFLAGS for mac users in cmake
janniclas Apr 13, 2023
510e5f1
Merge branch 'development' into f-swift_code_style
fabianbs96 Jun 17, 2023
beecb60
Merge branch 'development' into f-swift_code_style
fabianbs96 Jul 6, 2023
23cd91f
Merge branch 'development' into f-swift_code_style
fabianbs96 Jul 31, 2023
948173a
Merge branch 'development' into f-swift_code_style
fabianbs96 Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ unittests/PhasarLLVM/ifdside/Problems/IFDSTaintAnalysisTest
unittests/PhasarLLVM/Pointer/LLVMTypeHierarchyTest
unittests/Utils/LLVMShorthandsTest
unittests/Utils/PAMMTest

.DS_Store
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ if (NOT PHASAR_IN_TREE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif()

if (LLVM_ENABLE_LIBCXX)
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(PHASAR_STD_FILESYSTEM stdc++)
set(LLVM_INSTALL_PATH "/opt/homebrew/opt/llvm@14")
set(LDFLAGS "-L${LLVM_INSTALL_PATH}/lib")
set(CPPFLAGS "-I${LLVM_INSTALL_PATH}/include")
set(LLVM_DIR "/opt/homebrew/opt/llvm@14/lib/cmake/llvm")
set(PATH "${LLVM_INSTALL_PATH}/bin:$ENV{PATH}")
elseif (LLVM_ENABLE_LIBCXX)
set(PHASAR_STD_FILESYSTEM c++fs)
else()
set(PHASAR_STD_FILESYSTEM stdc++fs)
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,19 @@ Done!

### Installing PhASAR a MacOS system

Due to unfortunate updates to MacOS and the handling of C++, especially on the newer M1 processors, we can't support native development on Mac.
The easiest solution to develop PhASAR on a Mac right now is to use [dockers development environments](https://docs.docker.com/desktop/dev-environments/). Clone this repository as described in their documentation. Afterwards, you have to login once manually, as a root user by running `docker exec -it -u root <container name> /bin/bash` to complete the rest of the install process as described in this readme (install submodules, run bootstrap.sh, ...).
To install PhASAR on a Mac you need to follow these steps:
1. Install both LLVM 14 and LLVM 17 on your system using e.g. homebrew.
2. Setup LLVM 17 as your compiler to compile PhASAR with using the following environment variables

```
// this must point to LLVM 17 !
CC=/opt/homebrew/opt/llvm/bin/clang
CXX=/opt/homebrew/opt/llvm/bin/clang++
```
3. Check the default settings for `LDFLAGS`, `CPPFLAGS`, and `PATH` in PhASAR's CMakeLists.txt if they point to your LLVM 14 installation. The default install path is `/opt/homebrew/opt/llvm@14`
4. Run `cmake .. -DCMAKE_CXX_COMPILER=$CXX -DPHASAR_BUILD_UNITTESTS=0 -DPHASAR_BUILD_IR=0 -G Ninja` and `ninja` to build PhASAR (currently unit tests break the build, because cmake tries to use LLVM 17 to create the IR. We are working on a solution for this)

The easiest solution to develop PhASAR on a Mac still is to use [dockers development environments](https://docs.docker.com/desktop/dev-environments/). Clone this repository as described in their documentation. Afterwards, you have to login once manually, as a root user by running `docker exec -it -u root <container name> /bin/bash` to complete the rest of the install process as described in this readme (install submodules, run bootstrap.sh, ...).
Now you can just attach your docker container to VS Code or any other IDE, which supports remote development.

### Compiling PhASAR (if not already done using the installation scripts)
Expand Down Expand Up @@ -163,6 +174,7 @@ When using CMake to compile PhASAR the following optional parameters can be used
| **PHASAR_BUILD_UNITTESTS** : BOOL | Build PhASAR unit tests (default is ON) |
| **PHASAR_BUILD_IR** : BOOL | Build PhASAR IR (required for running the unit tests) (default is ON) |
| **PHASAR_BUILD_OPENSSL_TS_UNITTESTS** : BOOL | Build PhASAR unit tests that require OpenSSL (default is OFF) |
| **BUILD_SWIFT_TESTS** : BOOL | Builds the Swift tests (Swift compiler has to be installed manually beforehand!) (default is OFF) |
| **PHASAR_ENABLE_PAMM** : STRING | Enable the performance measurement mechanism ('Off', 'Core' or 'Full', default is Off) |
| **PHASAR_ENABLE_PIC** : BOOL | Build Position-Independed Code (default is ON) |
| **PHASAR_ENABLE_WARNINGS** : BOOL | Enable compiler warnings (default is ON) |
Expand Down
2 changes: 1 addition & 1 deletion include/phasar/Utils/MemoryResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// a workaround here

#ifndef HAS_MEMORY_RESOURCE
#if !defined(__has_include) || __has_include(<memory_resource>)
#if !defined(__has_include) || __has_include(<memory_resource>) && !defined(__APPLE__)
Copy link
Member

Choose a reason for hiding this comment

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

We should probably add a comment to describe why we exclude pmr on apple.

Btw, are the feature-test macros __cpp_lib_memory_resource and __cpp_lib_polymorphic_allocator defined on apple?

#define HAS_MEMORY_RESOURCE 1
#include <memory_resource>
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ auto IFDSTaintAnalysis::getNormalFlowFunction(n_t Curr,
return transferFlow<d_t>(Cast, Cast->getOperand(0));
}

if (const auto *Insert = llvm::dyn_cast<llvm::InsertValueInst>(Curr)) {
return generateFlow(Insert, Insert->getInsertedValueOperand());
Copy link
Member

Choose a reason for hiding this comment

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

We should also generate insert from Insert->getAggregateOperand()

}

// Otherwise we do not care and leave everything as it is
return Identity<d_t>::getInstance();
}
Expand Down Expand Up @@ -261,7 +265,6 @@ auto IFDSTaintAnalysis::getSummaryFlowFunction([[maybe_unused]] n_t CallSite,
// result should be tainted
if (DestFun->getName().equals("$sSS1poiyS2S_SStFZ")) {
const auto *CS = llvm::cast<llvm::CallBase>(CallSite);

return generateFlowIf<d_t>(CallSite, [CS](d_t Source) {
return ((Source == CS->getArgOperand(1)) ||
(Source == CS->getArgOperand(3)));
Expand Down
1 change: 1 addition & 0 deletions test/llvm_swift_test_code/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(linear_constant)
add_subdirectory(taint_analysis)
12 changes: 9 additions & 3 deletions test/llvm_swift_test_code/linear_constant/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
file(GLOB lca_files *.swift)

set(SWIFT_COMPILE_IR_FLAGS -emit-ir -suppress-warnings -g -parse-as-library -Onone -Xfrontend -disable-llvm-optzns -Xfrontend -disable-swift-specific-llvm-optzns)
set(SWIFT_COMPILE_IR_FLAGS -emit-ir -suppress-warnings -g -parse-as-library -Onone)

foreach(TEST_SRC ${lca_files})
get_filename_component(TEST_SRC_FILE ${TEST_SRC} NAME_WE)
add_executable(${TEST_SRC_FILE}.ll ${TEST_SRC})
target_compile_options(${TEST_SRC_FILE}.ll PRIVATE ${SWIFT_COMPILE_IR_FLAGS})
set(TEST_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${TEST_SRC_FILE}.ll")
add_custom_command(
OUTPUT ${TEST_OUTPUT}
COMMAND swiftc ${SWIFT_COMPILE_IR_FLAGS} -o ${TEST_OUTPUT} ${TEST_SRC}
DEPENDS ${TEST_SRC}
)
add_custom_target(${TEST_SRC_FILE} DEPENDS ${TEST_OUTPUT})
add_dependencies(PhasarUnitTests ${TEST_SRC_FILE})
endforeach(TEST_SRC)
2 changes: 1 addition & 1 deletion test/llvm_swift_test_code/linear_constant/call_05.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@main
struct MyMain {
static func main() {
var i = CommandLine.arguments.count
var i = CommandLine.arguments.count
}
}
4 changes: 2 additions & 2 deletions test/llvm_swift_test_code/linear_constant/call_11.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@main
struct MyMain {
static func bar(_ b: Int)-> Int {
static func bar(_ b: Int) -> Int {
return b
}

static func foo(_ a: Int)-> Int {
static func foo(_ a: Int) -> Int {
return bar(a)
}

Expand Down
9 changes: 4 additions & 5 deletions test/llvm_swift_test_code/linear_constant/for_01.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

@main
struct MyMain {
static func main() {
var a = 0
for i in 0...9 {
a += i
}
var a = 0
for i in 0...9 {
a += i
}
}
}
7 changes: 7 additions & 0 deletions test/llvm_swift_test_code/linear_constant/global_00.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var y = 42
@main
struct MyMain {
static func main() {
y += 1
}
}
6 changes: 3 additions & 3 deletions test/llvm_swift_test_code/linear_constant/global_01.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var g3 = 3.1415
@main
struct MyMain {
static func main() {
var i = 666
g1 = 42
g2 = g1
var i = 666
g1 = 42
g2 = g1
}
}
6 changes: 3 additions & 3 deletions test/llvm_swift_test_code/linear_constant/global_02.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var g = 10
@main
struct MyMain {
static func main() {
var i = g
i -= 20
g = i
var i = g
i -= 20
g = i
}
}
12 changes: 6 additions & 6 deletions test/llvm_swift_test_code/linear_constant/global_03.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ var g = 0
@main
struct MyMain {

static func foo() {
g += 1
}
static func foo() {
g += 1
}
static func main() {
var i = 42
g += 1
foo()
var i = 42
g += 1
foo()
}
}
10 changes: 5 additions & 5 deletions test/llvm_swift_test_code/linear_constant/global_04.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ var g = 0
@main
struct MyMain {

static func foo (_ a: Int)-> Int {
return a + 1
}
static func foo(_ a: Int) -> Int {
return a + 1
}

static func main() {
g += 1
var i = foo(g)
g += 1
var i = foo(g)
}
}
10 changes: 5 additions & 5 deletions test/llvm_swift_test_code/linear_constant/global_06.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ var g = 0
@main
struct MyMain {

static func foo ()-> Int {
return g + 1
}
static func foo() -> Int {
return g + 1
}

static func main() {
g += 1
var i = foo()
g += 1
var i = foo()
}
}
26 changes: 13 additions & 13 deletions test/llvm_swift_test_code/linear_constant/global_07.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ var g = 0
@main
struct MyMain {

static func foo (_ a: Int)-> Int {
var x = a
x += g
return x
}
static func foo(_ a: Int) -> Int {
var x = a
x += g
return x
}

static func bar (_ b: Int)-> Int {
g += 1
return b + 1
}
static func bar(_ b: Int) -> Int {
g += 1
return b + 1
}

static func main() {
g += 1
var i = 0
i = foo(10)
i = bar(3)
g += 1
var i = 0
i = foo(10)
i = bar(3)
}
}
24 changes: 12 additions & 12 deletions test/llvm_swift_test_code/linear_constant/global_08.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ var g = 1
@main
struct MyMain {

static func baz (_ c: Int)-> Int {
return g + c
}
static func baz(_ c: Int) -> Int {
return g + c
}

static func bar (_ b: Int)-> Int {
return baz(b + 1)
}
static func bar(_ b: Int) -> Int {
return baz(b + 1)
}

static func foo (_ a: Int)-> Int {
return bar(a + 1)
}
static func foo(_ a: Int) -> Int {
return bar(a + 1)
}

static func main() {
g += 1
var i = 0
i = foo(1)
g += 1
var i = 0
i = foo(1)
}
}
34 changes: 17 additions & 17 deletions test/llvm_swift_test_code/linear_constant/global_09.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ var g1 = 1
@main
struct MyMain {

static func baz (_ c: Int)-> Int {
return c + 3
}
static func baz(_ c: Int) -> Int {
return c + 3
}

static func bar (_ b: Int)-> Int {
g1 += 1
return b + 1
}
static func bar(_ b: Int) -> Int {
g1 += 1
return b + 1
}

static func foo (_ a: Int)-> Int {
var x = a
x+=g1
return x
}
static func foo(_ a: Int) -> Int {
var x = a
x += g1
return x
}

static func main() {
g1 += 1
var i = 0
i = foo(10)
i = bar(3)
i = baz(39)
g1 += 1
var i = 0
i = foo(10)
i = bar(3)
i = baz(39)
}
}
15 changes: 7 additions & 8 deletions test/llvm_swift_test_code/linear_constant/global_11.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ var g2 = 9001
@main
struct MyMain {

static func foo (_ x: Int)-> Int {
var a = x
a = a + 1
return a
}

static func foo(_ x: Int) -> Int {
var a = x
a = a + 1
return a
}

static func main() {
var a = 13
a = foo(a)
var a = 13
a = foo(a)
}
}
14 changes: 7 additions & 7 deletions test/llvm_swift_test_code/linear_constant/global_12.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ var g = 0
@main
struct MyMain {

static func foo (_ x: Int)-> Int {
var a = x
a = a + 1
return a
}
static func foo(_ x: Int) -> Int {
var a = x
a = a + 1
return a
}

static func main() {
var a = g
a = foo(a)
var a = g
a = foo(a)
}
}