Skip to content

Commit 374b76e

Browse files
authored
Merge pull request #519 from secure-software-engineering/feature/statisticExport
Feature/statistic export
2 parents c0a2ff2 + 512e9dd commit 374b76e

12 files changed

Lines changed: 73 additions & 35 deletions

File tree

.docker/Dockerfile.devenv

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM ubuntu:22.04
2+
3+
RUN apt -y update && apt -y upgrade && apt install bash -y
4+
RUN DEBIAN_FRONTEND=noninteractive apt -y install wget gpg software-properties-common lsb-release git make python3 python3-pip vim -y
5+
RUN apt -y update && apt -y install cmake clang-14 ninja-build sudo
6+
7+
RUN pip install pre-commit
8+
9+
RUN set -exu; \
10+
useradd -p test -s /bin/bash -m vscode; \
11+
groupadd docker; \
12+
usermod -aG docker vscode; \
13+
usermod -aG sudo vscode;
14+
15+
USER vscode
16+
17+
ENV CC "/usr/bin/clang-14"
18+
ENV CXX "/usr/bin/clang++-14"
19+
ENV CMAKE_GENERATOR "Ninja"

.docker/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dockerfile": "Dockerfile.devenv"
3+
}

Brewfile

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,9 @@ Done!
7373

7474

7575
### Installing PhASAR a MacOS system
76-
Mac OS 10.13.1 or higher only!
77-
To install the framework on a Mac we will rely on Homebrew. (https://brew.sh/)
78-
79-
Please follow the instructions down below.
80-
81-
```
82-
$ brew install boost
83-
$ brew install python3
84-
# Install llvm version 10
85-
$ brew install llvm
86-
# Setting the paths
87-
# Use LLVM's Clang rather than Apple's Clang compiler
88-
$ export CC=/usr/local/opt/llvm/bin/clang
89-
$ export CXX=/usr/local/opt/llvm/bin/clang++
90-
# Set PATH env variable to /usr/local/opt/llvm/bin
91-
# Go to PhASAR directory run the following commands
92-
$ git submodule init
93-
$ git submodule update
94-
$ mkdir build
95-
$ cd build/
96-
$ cmake -DCMAKE_BUILD_TYPE=Release ..
97-
$ make -j $(nproc) # or use a different number of cores to compile it
98-
$ sudo make install # if you wish a system-wise installation
99-
```
100-
76+
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.
77+
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, ...).
78+
Now you can just attach your docker container to VS Code or any other IDE, which supports remote development.
10179

10280
### Compiling PhASAR (if not already done using the installation scripts)
10381
Set the system's variables for the C and C++ compiler to clang:

include/phasar/Controller/AnalysisController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum class AnalysisControllerEmitterOptions : uint32_t {
4949
EmitPTAAsText = (1 << 11),
5050
EmitPTAAsDot = (1 << 12),
5151
EmitPTAAsJson = (1 << 13),
52+
EmitStatisticAsJson = (1 << 14),
5253
};
5354

5455
class AnalysisController {

include/phasar/DB/ProjectIRDB.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/IR/PassManager.h"
2222
#include "llvm/Passes/PassBuilder.h"
2323

24+
#include "nlohmann/json.hpp"
2425
#include "phasar/Utils/EnumFlags.h"
2526

2627
namespace llvm {
@@ -60,6 +61,8 @@ class ProjectIRDB {
6061
std::map<std::string, std::unique_ptr<llvm::Module>> Modules;
6162
// Maps an id to its corresponding instruction
6263
std::map<std::size_t, llvm::Instruction *> IDInstructionMapping;
64+
size_t NumberCallsites;
65+
nlohmann::json StatsJson;
6366

6467
void buildIDModuleMapping(llvm::Module *M);
6568

@@ -171,12 +174,18 @@ class ProjectIRDB {
171174
return IDInstructionMapping.size();
172175
}
173176

177+
[[nodiscard]] inline std::size_t getNumCallsites() const {
178+
return NumberCallsites;
179+
}
180+
174181
[[nodiscard]] std::size_t getNumGlobals() const;
175182

176183
[[nodiscard]] llvm::Instruction *getInstruction(std::size_t Id) const;
177184

178185
[[nodiscard]] static std::size_t getInstructionID(const llvm::Instruction *I);
179186

187+
void printAsJson(llvm::raw_ostream &OS = llvm::outs()) const;
188+
180189
void print() const;
181190

182191
void emitPreprocessedIR(llvm::raw_ostream &OS = llvm::outs(),

include/phasar/PhasarLLVM/Passes/GeneralStatisticsAnalysis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <set>
2121

22+
#include "nlohmann/json.hpp"
2223
#include "llvm/IR/PassManager.h"
2324

2425
namespace llvm {
@@ -115,6 +116,7 @@ class GeneralStatistics {
115116
*/
116117
[[nodiscard]] std::set<const llvm::Instruction *>
117118
getRetResInstructions() const;
119+
[[nodiscard]] nlohmann::json getAsJson() const;
118120
};
119121

120122
/**

lib/Controller/AnalysisController.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ void AnalysisController::emitRequestedHelperAnalysisResults() {
272272
ICF.printAsJson();
273273
}
274274
}
275+
276+
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitStatisticAsJson) {
277+
if (!ResultDirectory.empty()) {
278+
if (auto OFS = openFileStream("/psr-IrStatistic.json")) {
279+
IRDB.printAsJson(*OFS);
280+
}
281+
} else {
282+
IRDB.printAsJson();
283+
}
284+
}
275285
}
276286

277287
std::unique_ptr<llvm::raw_fd_ostream>

lib/DB/ProjectIRDB.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ void ProjectIRDB::preprocessModule(llvm::Module *M) {
126126
MPM.run(*M, MAM);
127127
// retrieve data from the GeneralStatisticsAnalysis registered earlier
128128
auto GSPResult = MAM.getResult<GeneralStatisticsAnalysis>(*M);
129+
StatsJson = GSPResult.getAsJson();
130+
NumberCallsites = GSPResult.getFunctioncalls();
129131
auto Allocas = GSPResult.getAllocaInstructions();
130132
AllocaInstructions.insert(Allocas.begin(), Allocas.end());
131133
auto ATypes = GSPResult.getAllocatedTypes();
@@ -266,6 +268,10 @@ void ProjectIRDB::print() const {
266268
}
267269
}
268270

271+
void ProjectIRDB::printAsJson(llvm::raw_ostream &OS) const {
272+
OS << StatsJson.dump(4) << '\n';
273+
}
274+
269275
void ProjectIRDB::emitPreprocessedIR(llvm::raw_ostream &OS,
270276
bool ShortenIR) const {
271277
for (const auto &[File, Module] : Modules) {

lib/PhasarLLVM/Passes/GeneralStatisticsAnalysis.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,14 @@ GeneralStatistics::getRetResInstructions() const {
216216
return RetResInstructions;
217217
}
218218

219+
nlohmann::json GeneralStatistics::getAsJson() const {
220+
nlohmann::json J;
221+
J["Instructions"] = getInstructions();
222+
J["Functions"] = Functions;
223+
J["Alloca Instructions"] = AllocaInstructions.size();
224+
J["Call Sites"] = CallSites;
225+
J["Global Variables"] = Globals;
226+
return J;
227+
}
228+
219229
} // namespace psr

0 commit comments

Comments
 (0)