Skip to content
Draft
Show file tree
Hide file tree
Changes from 19 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ 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++)
elseif (LLVM_ENABLE_LIBCXX)
set(PHASAR_STD_FILESYSTEM c++fs)
else()
set(PHASAR_STD_FILESYSTEM stdc++fs)
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,23 @@ 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.
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. Update your `LDFLAGS`, `CPPFLAGS`, and `PATH` to point to your LLVM 14 installation
```
export LDFLAGS="-L/opt/homebrew/opt/llvm@14/lib"
export CPPFLAGS="-I/opt/homebrew/opt/llvm@14/include"
export PATH="/opt/homebrew/opt/llvm@14/bin:$PATH"
Copy link
Member

Choose a reason for hiding this comment

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

Do these flags have to be env variables? Or can they also be supplied to cmake directly?

Copy link
Member Author

Choose a reason for hiding this comment

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

What we could do is define it as a default variable in cmake and users could change it with a cmake flag like
-DPathToLLVM=/my/custom/llvm/install/llvm@14
I think this could make sense.

```
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)
### Use Docker for development
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, ...).
Now you can just attach your docker container to VS Code or any other IDE, which supports remote development.

Expand Down Expand Up @@ -152,6 +168,7 @@ When using CMake to compile PhASAR the following optional parameters can be used
| <b>PHASAR_CONFIG_INSTALL_PREFIX</b> : PATH | Path where PhASAR's configuration files will be installed if <br> ninja install” is invoked or the “install” <br> target is built. Expression will be evaluated within CMAKE_INSTALL_PREFIX, so prefer absolute paths (default is $(HOME)/.config/) |
| <b>PHASAR_BUILD_DOC</b> : BOOL | Build PhASAR documentation (default is OFF) |
| <b>PHASAR_BUILD_UNITTESTS</b> : BOOL | Build PhASAR unit tests (default is ON) |
| <b>BUILD_SWIFT_TESTS</b> : BOOL | Builds the Swift tests (Swift compiler has to be installed manually beforehand!) (default is OFF) |
| <b>PHASAR_BUILD_IR</b> : BOOL | Build PhASAR IR (required for running the unit tests) (default is ON) |
| <b>PHASAR_BUILD_OPENSSL_TS_UNITTESTS</b> : BOOL | Build PhASAR unit tests that require OpenSSL (default is OFF) |
| <b>PHASAR_ENABLE_PAMM</b> : STRING | Enable the performance measurement mechanism <br> ('Off', 'Core' or 'Full', default is Off) |
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
24 changes: 5 additions & 19 deletions lib/PhasarLLVM/DataFlow/IfdsIde/Problems/IFDSTaintAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,7 @@ IFDSTaintAnalysis::FlowFunctionPtrType IFDSTaintAnalysis::getNormalFlowFunction(
IFDSTaintAnalysis::n_t Curr, [[maybe_unused]] IFDSTaintAnalysis::n_t Succ) {
// If a tainted value is stored, the store location must be tainted too
if (const auto *Store = llvm::dyn_cast<llvm::StoreInst>(Curr)) {
struct TAFF : FlowFunction<IFDSTaintAnalysis::d_t> {
const llvm::StoreInst *Store;
TAFF(const llvm::StoreInst *S) : Store(S){};
std::set<IFDSTaintAnalysis::d_t>
computeTargets(IFDSTaintAnalysis::d_t Source) override {
if (Store->getValueOperand() == Source) {
return std::set<IFDSTaintAnalysis::d_t>{Store->getPointerOperand(),
Source};
}
if (Store->getValueOperand() != Source &&
Store->getPointerOperand() == Source) {
return {};
}
return {Source};
}
};
return std::make_shared<TAFF>(Store);
return strongUpdateStore(Store);
}
// If a tainted value is loaded, the loaded value is of course tainted
if (const auto *Load = llvm::dyn_cast<llvm::LoadInst>(Curr)) {
Expand All @@ -154,10 +138,13 @@ IFDSTaintAnalysis::FlowFunctionPtrType IFDSTaintAnalysis::getNormalFlowFunction(
// Check if a tainted value is extracted and taint the targets of
// the extract operation accordingly
if (const auto *Extract = llvm::dyn_cast<llvm::ExtractValueInst>(Curr)) {

return generateFlow(Extract, Extract->getAggregateOperand());
}

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<IFDSTaintAnalysis::d_t>::getInstance();
}
Expand Down Expand Up @@ -290,7 +277,6 @@ IFDSTaintAnalysis::getSummaryFlowFunction(
// 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)
}
}
Loading