Skip to content

Commit e3908bd

Browse files
authored
Example Programs (#774)
* Update llvm-hello-world + add examples for load-llvm-ir and build-type-hierarchy with phasar * Add examples on how to build a call-graph * Update CG examples to include the reverse-CG + add example for alias info * Add taint analysis example * Add IDE linear constant analysis example * Build and run example programs in CI * Fixing small isuues * Fix examples path in CI * Install phasar to custom location in CI * Set env for install and examples-build in CI * Next attempt to fix CI * Adding simple example on how to write an IFDS analysis * Improve TaintConfigUtilities to provide API that *returns* affected facts, instead of inserting them into an already-existing set * minor * Small simplification * Add example on how to write an IDE linear constant analysis based on the corresponding Wiki entry * Update readmes * Fix broken compile due to merge * minor restructure in README.md * cleanup readme
1 parent f5a9b43 commit e3908bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+6971
-1106
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,16 @@ jobs:
6363
- name: Run Unittests
6464
shell: bash
6565
run: |
66-
cd build
67-
cmake --build . --target check-phasar-unittests
66+
cmake --build ./build --target check-phasar-unittests
67+
68+
- name: Install PhASAR and Build Examples
69+
if: matrix.build == 'DebugLibdeps' # Circumvent conflicting ASAn flags
70+
env:
71+
CXX: ${{ matrix.compiler[0] }}
72+
CC: ${{ matrix.compiler[1] }}
73+
shell: bash
74+
run: |
75+
cmake -DCMAKE_INSTALL_PREFIX=./INSTALL -P ./build/cmake_install.cmake
76+
cd ./examples/how-to
77+
cmake -S . -B build -Dphasar_ROOT=../../INSTALL
78+
cmake --build ./build --target run_sample_programs

BUILD.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Building PhASAR
2+
3+
4+
It is recommended to compile PhASAR yourself in order to get the full C++ experience and to have full control over the build mode.
5+
However, you may also want to try out one of the pre-built versions of PhASAR or the Docker container.
6+
7+
As a shortcut for the very first PhASAR build on your system, you can use our [bootstrap](./bootstrap.sh) script.
8+
Please note that you must have python installed for the script to work properly.
9+
10+
```bash
11+
./bootstrap.sh
12+
```
13+
14+
Note: If you want to do changes within PhASAR, it is recommended to build it in Debug mode:
15+
16+
```bash
17+
./bootstrap.sh -DCMAKE_BUILD_TYPE=Debug
18+
```
19+
20+
The bootstrap script may ask for superuser permissions (to install the dependencies); however it is not recommended to start the whole script with `sudo`.
21+
22+
For subsequent builds, see [Compiling PhASAR](#compiling-phasar-if-not-already-done-using-the-installation-scripts).
23+
24+
### Compiling PhASAR (if not already done using the bootstrap script)
25+
26+
Set the system's variables for the C and C++ compiler to clang:
27+
28+
```bash
29+
export CC=/usr/local/bin/clang
30+
export CXX=/usr/local/bin/clang++
31+
```
32+
33+
You may need to adjust the paths according to your system. When you cloned PhASAR from Github you need to initialize PhASAR's submodules before building it:
34+
35+
```bash
36+
git submodule update --init
37+
```
38+
39+
If you downloaded PhASAR as a compressed release (e.g. .zip or .tar.gz) you can use the `init-submodules-release.sh` script that manually clones the required submodules:
40+
41+
```bash
42+
utils/init-submodules-release.sh
43+
```
44+
45+
Navigate into the PhASAR directory. The following commands will do the job and compile the PhASAR framework:
46+
47+
```bash
48+
mkdir build
49+
cd build/
50+
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release ..
51+
ninja -j $(nproc) # or use a different number of cores to compile it
52+
sudo ninja install # only if you wish to install PhASAR system wide
53+
```
54+
55+
When you have used the `bootstrap.sh` script to install PhASAR, the above steps are already done.
56+
Use them as a reference if you wish to modify PhASAR and recompile it.
57+
58+
After compilation using cmake the following two binaries can be found in the build/tools directory:
59+
60+
+ `phasar-cli` - the PhASAR command-line tool (previously called `phasar-llvm`) that provides access to analyses that are already implemented within PhASAR. Use this if you don't want to build an own tool on top of PhASAR.
61+
+ `myphasartool` - an example tool that shows how tools can be build on top of PhASAR
62+
63+
Please be careful and check if errors occur during the compilation.
64+
65+
When using CMake to compile PhASAR the following optional parameters can be used:
66+
67+
| Parameter : Type| Effect |
68+
|-----------|--------|
69+
| **BUILD_SHARED_LIBS** : BOOL | Build shared libraries -- Not recommended anymore. You may want to use PHASAR_BUILD_DYNLIB instead (default is OFF) |
70+
| **PHASAR_BUILD_DYNLIB** : BOOL | Build one fat shared library (default is OFF) |
71+
| **CMAKE_BUILD_TYPE** : STRING | Build PhASAR in 'Debug', 'RelWithDebInfo' or 'Release' mode (default is 'Debug') |
72+
| **CMAKE_INSTALL_PREFIX** : PATH | Path where PhASAR will be installed if "ninja install” is invoked or the “install” target is built (default is /usr/local/phasar) |
73+
| **PHASAR_CUSTOM_CONFIG_INSTALL_DIR** : PATH | If set, customizes the directory, where configuration files for PhASAR are installed (default is /usr/local/.phasar-config)|
74+
| **PHASAR_ENABLE_DYNAMIC_LOG** : BOOL|Makes it possible to switch the logger on and off at runtime (default is ON)|
75+
| **PHASAR_BUILD_DOC** : BOOL | Build PhASAR documentation (default is OFF) |
76+
| **PHASAR_BUILD_UNITTESTS** : BOOL | Build PhASAR unit tests (default is ON) |
77+
| **PHASAR_BUILD_IR** : BOOL | Build PhASAR IR (required for running the unit tests) (default is ON) |
78+
| **PHASAR_BUILD_OPENSSL_TS_UNITTESTS** : BOOL | Build PhASAR unit tests that require OpenSSL (default is OFF) |
79+
| **PHASAR_ENABLE_PAMM** : STRING | Enable the performance measurement mechanism ('Off', 'Core' or 'Full', default is Off) |
80+
| **PHASAR_ENABLE_PIC** : BOOL | Build Position-Independed Code (default is ON) |
81+
| **PHASAR_ENABLE_WARNINGS** : BOOL | Enable compiler warnings (default is ON) |
82+
| **CMAKE_CXX_STANDARD** : INT|Build phasar in C++17 or C++20 mode (default is 17)|
83+
84+
You can use these parameters either directly or modify the installer-script `bootstrap.sh`
85+
86+
#### A Remark on Compile Time
87+
88+
C++'s long compile times are always a pain. As shown in the above, when using cmake the compilation can easily be run in parallel, resulting in shorter compilation times. Make use of it!
89+
90+
### Running a Test Solver
91+
92+
To test if everything works as expected please run the following command:
93+
94+
`$ phasar-cli -m test/llvm_test_code/basic/module_cpp.ll -D ifds-solvertest`
95+
96+
You can find the `phasar-cli` tool in the build-tree under `tools/phasar-cli`.
97+
98+
If you obtain output other than a segmentation fault or an exception terminating the program abnormally everything works as expected.
99+
100+
### Building PhASAR on a MacOS System
101+
102+
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.
103+
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 build process as described in this readme (install submodules, run bootstrap.sh, ...).
104+
Now you can just attach your docker container to VS Code or any other IDE, which supports remote development.
105+
106+
## Installation
107+
108+
PhASAR can be installed using the installer scripts as explained in the following.
109+
However, you do not need to install PhASAR in order to use it.
110+
111+
### Installing PhASAR on an Ubuntu System
112+
113+
In the following, we would like to give an complete example of how to install
114+
PhASAR using an Ubuntu or Unix-like system.
115+
116+
Therefore, we provide an installation script. To install PhASAR, just navigate to the top-level
117+
directory of PhASAR and use the following command:
118+
119+
```bash
120+
./bootstrap.sh --install
121+
```
122+
123+
The bootstrap script may ask for superuser permissions.
124+
125+
Done!
126+
127+
If You have already built phasar, you can just invoke
128+
```bash
129+
sudo ninja install
130+
```

README.md

Lines changed: 23 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
![PhASAR logo](img/Logo_RGB/Phasar_Logo.png)
22

3-
# PhASAR a LLVM-based Static Analysis Framework
3+
# PhASAR: A LLVM-based Static Analysis Framework
44

55
[![C++ Standard](https://img.shields.io/badge/C++_Standard-C%2B%2B17-blue.svg?style=flat&logo=c%2B%2B)](https://isocpp.org/)
66
[![GitHub license](https://img.shields.io/badge/license-MIT-blueviolet.svg)](https://raw.githubusercontent.com/secure-software-engineering/phasar/master/LICENSE.txt)
77

88
Version 2503
99

10+
## What is PhASAR?
11+
12+
PhASAR is a LLVM-based static analysis framework written in C++. It allows users
13+
to specify arbitrary data-flow problems which are then solved in a
14+
fully-automated manner on the specified LLVM IR target code. Computing points-to
15+
information, call-graph(s), etc. is done by the framework, thus you can focus on
16+
what matters.
17+
18+
You can find available literature on PhASAR [here](https://github.com/secure-software-engineering/phasar/wiki/Useful-Literature#papers-on-phasar).
19+
20+
### How do I get started with PhASAR?
21+
22+
We have some documentation on PhASAR in our [***Wiki***](https://github.com/secure-software-engineering/phasar/wiki). You probably would like to read
23+
this README first.
24+
<!-- and then have a look on the material provided on <https://phasar.org/>
25+
as well. -->
26+
Please also have a look on PhASAR's project directory and notice the project directory [examples](./examples/) as well as the custom tool `tools/example-tool/myphasartool.cpp`.
27+
28+
**NEW:** You can find PhASAR's API reference [here](https://secure-software-engineering.github.io/phasar/).
29+
30+
1031
## Secure Software Engineering Group
1132

1233
PhASAR is primarily developed and maintained by the Secure Software Engineering Group at Heinz Nixdorf Institute (University of Paderborn) and Fraunhofer IEM.
@@ -33,155 +54,13 @@ Although phasar currently does not make use of C++-20 features (except for some
3354

3455
PhASAR is currently set up to support LLVM-15.0.*
3556

36-
## What is PhASAR?
37-
38-
PhASAR is a LLVM-based static analysis framework written in C++. It allows users
39-
to specify arbitrary data-flow problems which are then solved in a
40-
fully-automated manner on the specified LLVM IR target code. Computing points-to
41-
information, call-graph(s), etc. is done by the framework, thus you can focus on
42-
what matters.
43-
4457
## Breaking Changes
4558

4659
To keep PhASAR in a state that it is well suited for state-of-the-art research in static analysis, as well as for productive use, we have to make breaking changes. Please refer to [Breaking Changes](./BreakingChanges.md) for detailed information on what was broken recently and how to migrate.
4760

48-
## How do I get started with PhASAR?
49-
50-
We have some documentation on PhASAR in our [***Wiki***](https://github.com/secure-software-engineering/phasar/wiki). You probably would like to read
51-
this README first.
52-
<!-- and then have a look on the material provided on <https://phasar.org/>
53-
as well. -->
54-
Please also have a look on PhASAR's project directory and notice the project directory `examples/` as well as the custom tool `tools/example-tool/myphasartool.cpp`.
55-
5661
## Building PhASAR
5762

58-
It is recommended to compile PhASAR yourself in order to get the full C++ experience and to have full control over the build mode.
59-
However, you may also want to try out one of the pre-built versions of PhASAR or the Docker container.
60-
61-
As a shortcut for the very first PhASAR build on your system, you can use our [bootstrap](./bootstrap.sh) script.
62-
Please note that you must have python installed for the script to work properly.
63-
64-
```bash
65-
./bootstrap.sh
66-
```
67-
68-
Note: If you want to do changes within PhASAR, it is recommended to build it in Debug mode:
69-
70-
```bash
71-
./bootstrap.sh -DCMAKE_BUILD_TYPE=Debug
72-
```
73-
74-
The bootstrap script may ask for superuser permissions (to install the dependencies); however it is not recommended to start the whole script with `sudo`.
75-
76-
For subsequent builds, see [Compiling PhASAR](#compiling-phasar-if-not-already-done-using-the-installation-scripts).
77-
78-
### Compiling PhASAR (if not already done using the bootstrap script)
79-
80-
Set the system's variables for the C and C++ compiler to clang:
81-
82-
```bash
83-
export CC=/usr/local/bin/clang
84-
export CXX=/usr/local/bin/clang++
85-
```
86-
87-
You may need to adjust the paths according to your system. When you cloned PhASAR from Github you need to initialize PhASAR's submodules before building it:
88-
89-
```bash
90-
git submodule update --init
91-
```
92-
93-
If you downloaded PhASAR as a compressed release (e.g. .zip or .tar.gz) you can use the `init-submodules-release.sh` script that manually clones the required submodules:
94-
95-
```bash
96-
utils/init-submodules-release.sh
97-
```
98-
99-
Navigate into the PhASAR directory. The following commands will do the job and compile the PhASAR framework:
100-
101-
```bash
102-
mkdir build
103-
cd build/
104-
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release ..
105-
ninja -j $(nproc) # or use a different number of cores to compile it
106-
sudo ninja install # only if you wish to install PhASAR system wide
107-
```
108-
109-
When you have used the `bootstrap.sh` script to install PhASAR, the above steps are already done.
110-
Use them as a reference if you wish to modify PhASAR and recompile it.
111-
112-
After compilation using cmake the following two binaries can be found in the build/tools directory:
113-
114-
+ `phasar-cli` - the PhASAR command-line tool (previously called `phasar-llvm`) that provides access to analyses that are already implemented within PhASAR. Use this if you don't want to build an own tool on top of PhASAR.
115-
+ `myphasartool` - an example tool that shows how tools can be build on top of PhASAR
116-
117-
Please be careful and check if errors occur during the compilation.
118-
119-
When using CMake to compile PhASAR the following optional parameters can be used:
120-
121-
| Parameter : Type| Effect |
122-
|-----------|--------|
123-
| **BUILD_SHARED_LIBS** : BOOL | Build shared libraries -- Not recommended anymore. You may want to use PHASAR_BUILD_DYNLIB instead (default is OFF) |
124-
| **PHASAR_BUILD_DYNLIB** : BOOL | Build one fat shared library (default is OFF) |
125-
| **CMAKE_BUILD_TYPE** : STRING | Build PhASAR in 'Debug', 'RelWithDebInfo' or 'Release' mode (default is 'Debug') |
126-
| **CMAKE_INSTALL_PREFIX** : PATH | Path where PhASAR will be installed if "ninja install” is invoked or the “install” target is built (default is /usr/local/phasar) |
127-
| **PHASAR_CUSTOM_CONFIG_INSTALL_DIR** : PATH | If set, customizes the directory, where configuration files for PhASAR are installed (default is /usr/local/.phasar-config)|
128-
| **PHASAR_ENABLE_DYNAMIC_LOG** : BOOL|Makes it possible to switch the logger on and off at runtime (default is ON)|
129-
| **PHASAR_BUILD_DOC** : BOOL | Build PhASAR documentation (default is OFF) |
130-
| **PHASAR_BUILD_UNITTESTS** : BOOL | Build PhASAR unit tests (default is ON) |
131-
| **PHASAR_BUILD_IR** : BOOL | Build PhASAR IR (required for running the unit tests) (default is ON) |
132-
| **PHASAR_BUILD_OPENSSL_TS_UNITTESTS** : BOOL | Build PhASAR unit tests that require OpenSSL (default is OFF) |
133-
| **PHASAR_ENABLE_PAMM** : STRING | Enable the performance measurement mechanism ('Off', 'Core' or 'Full', default is Off) |
134-
| **PHASAR_ENABLE_PIC** : BOOL | Build Position-Independed Code (default is ON) |
135-
| **PHASAR_ENABLE_WARNINGS** : BOOL | Enable compiler warnings (default is ON) |
136-
| **CMAKE_CXX_STANDARD** : INT|Build phasar in C++17 or C++20 mode (default is 17)|
137-
138-
You can use these parameters either directly or modify the installer-script `bootstrap.sh`
139-
140-
#### A Remark on Compile Time
141-
142-
C++'s long compile times are always a pain. As shown in the above, when using cmake the compilation can easily be run in parallel, resulting in shorter compilation times. Make use of it!
143-
144-
### Running a Test Solver
145-
146-
To test if everything works as expected please run the following command:
147-
148-
`$ phasar-cli -m test/llvm_test_code/basic/module_cpp.ll -D ifds-solvertest`
149-
150-
You can find the `phasar-cli` tool in the build-tree under `tools/phasar-cli`.
151-
152-
If you obtain output other than a segmentation fault or an exception terminating the program abnormally everything works as expected.
153-
154-
### Building PhASAR on a MacOS System
155-
156-
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.
157-
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 build process as described in this readme (install submodules, run bootstrap.sh, ...).
158-
Now you can just attach your docker container to VS Code or any other IDE, which supports remote development.
159-
160-
## Installation
161-
162-
PhASAR can be installed using the installer scripts as explained in the following.
163-
However, you do not need to install PhASAR in order to use it.
164-
165-
### Installing PhASAR on an Ubuntu System
166-
167-
In the following, we would like to give an complete example of how to install
168-
PhASAR using an Ubuntu or Unix-like system.
169-
170-
Therefore, we provide an installation script. To install PhASAR, just navigate to the top-level
171-
directory of PhASAR and use the following command:
172-
173-
```bash
174-
./bootstrap.sh --install
175-
```
176-
177-
The bootstrap script may ask for superuser permissions.
178-
179-
Done!
180-
181-
If You have already built phasar, you can just invoke
182-
```bash
183-
sudo ninja install
184-
```
63+
Please refer to [BUILD.md](./BUILD.md) for instructions on how to build PhASAR.
18564

18665
## How to use PhASAR?
18766

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.14...3.28)
2+
3+
project(load-llvm-ir)
4+
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
7+
find_package(phasar REQUIRED CONFIG)
8+
9+
add_executable(load-llvm-ir main.cpp)
10+
target_link_libraries(load-llvm-ir PRIVATE phasar::phasar)
11+
12+
if (TARGET run_sample_programs)
13+
add_custom_target(run_load_llvm_ir
14+
DEPENDS load-llvm-ir
15+
COMMAND $<TARGET_FILE:load-llvm-ir> "${CMAKE_CURRENT_LIST_DIR}/../../llvm-hello-world/target/simple.ll"
16+
)
17+
18+
add_dependencies(run_sample_programs run_load_llvm_ir)
19+
endif()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Load LLVM IR
2+
3+
Shows, how you can use PhASAR to load and manage a LLVM IR module.
4+
5+
## Build
6+
7+
This example program can be built using cmake.
8+
It assumes, that you have installed PhASAR on your system. If you did not install PhASAR to a default location, you can specify `-Dphasar_ROOT=your/path/to/phasar` when invoking `cmake`, replacing "your/path/to/phasar" by the actual path where you have installed PhASAR.
9+
10+
```bash
11+
# Invoked from the 00-load-llvm-ir root folder:
12+
$ mkdir -p build && cd build
13+
$ cmake ..
14+
$ cmake --build .
15+
```
16+
17+
## Test
18+
19+
You can test the example program on the target programs from [llvm-hello-world/target](../../llvm-hello-world/target/).
20+
21+
```bash
22+
# Invoked from the 00-load-llvm-ir/build folder:
23+
./load-llvm-ir ../../../llvm-hello-world/target/simple.ll
24+
```

0 commit comments

Comments
 (0)