Skip to content

Commit 6e0f6f6

Browse files
authored
Move AnalysisController (#724)
* Move Controller to phasar-cli * Simplify AnalysisController * Prevent invalid enum values to be accepted by the CLI parser * Add proper error messages in case of fatal failure * simplify linking * breaking changes * remove controller component from Config.cmake.in
1 parent 0c7b979 commit 6e0f6f6

32 files changed

+211
-251
lines changed

BreakingChanges.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## development HEAD
44

5+
- Removed the phasar-library `phasar_controller`. It is now part of the tool `phasar-cli`.
56
- The API of the `TypeHierarchy` interface (and thus the `LLVMTypeHierarchy` and `DIBasedTypeHierarchy` as well) has changed:
67
- No handling of the super-type relation (only sub-types)
78
- No VTable handling anymore -- has been out-sourced into `LLVMVFTableProvider`

Config.cmake.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ set(PHASAR_COMPONENTS
3737
llvm
3838
llvm_ifdside
3939
analysis_strategy
40-
controller
4140
)
4241

4342
list(REMOVE_DUPLICATES phasar_FIND_COMPONENTS)

include/phasar.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "phasar/AnalysisStrategy.h"
1414
#include "phasar/Config.h"
1515
#include "phasar/ControlFlow.h"
16-
#include "phasar/Controller.h"
1716
#include "phasar/DB.h"
1817
#include "phasar/DataFlow.h"
1918
#include "phasar/Domain.h"

include/phasar/Controller.h

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

include/phasar/Utils/InitPhasar.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/******************************************************************************
2+
* Copyright (c) 2024 Fabian Schiebel.
3+
* All rights reserved. This program and the accompanying materials are made
4+
* available under the terms of LICENSE.txt.
5+
*
6+
* Contributors:
7+
* Fabian Schiebel and others
8+
*****************************************************************************/
9+
10+
#include "llvm/Support/InitLLVM.h"
11+
12+
namespace psr {
13+
class InitPhasar : llvm::InitLLVM {
14+
public:
15+
InitPhasar(int &Argc, const char **&Argv) noexcept;
16+
InitPhasar(int &Argc, char **&Argv) noexcept
17+
: InitPhasar(Argc, (const char **&)Argv) {}
18+
};
19+
} // namespace psr
20+
21+
#define PSR_INITIALIZER(argc, argv) \
22+
const ::psr::InitPhasar PsrInitializerVar((argc), (argv))

lib/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_subdirectory(PhasarPass)
44
add_subdirectory(DB)
55
add_subdirectory(Config)
66
add_subdirectory(Utils)
7-
add_subdirectory(Controller)
87
add_subdirectory(Pointer)
98
add_subdirectory(ControlFlow)
109
if(BUILD_PHASAR_CLANG)
@@ -36,7 +35,6 @@ set(PHASAR_LINK_LIBS
3635
phasar_llvm
3736
phasar_llvm_ifdside
3837
phasar_analysis_strategy
39-
phasar_controller
4038
)
4139
if(SQLite3_FOUND)
4240
list(APPEND PHASAR_LINK_LIBS phasar_db)

lib/Controller/CMakeLists.txt

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

lib/Utils/InitPhasar.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "phasar/Utils/InitPhasar.h"
2+
3+
#include "phasar/Utils/Logger.h"
4+
5+
#include "llvm/Support/ErrorHandling.h"
6+
#include "llvm/Support/InitLLVM.h"
7+
#include "llvm/Support/raw_ostream.h"
8+
9+
using namespace psr;
10+
11+
#define PSR_ISSUE_TRACKER_URL \
12+
"https://github.com/secure-software-engineering/phasar/issues"
13+
14+
InitPhasar::InitPhasar(int &Argc, const char **&Argv) noexcept
15+
: llvm::InitLLVM(Argc, Argv) {
16+
static std::nullptr_t InitGlobals = [] {
17+
// Replace LLVM's bug report URL with ours
18+
llvm::setBugReportMsg("PLEASE create a bug report at " PSR_ISSUE_TRACKER_URL
19+
" and include the crash backtrace.\n ");
20+
21+
// Install custom error handlers, such that fatal errors do not start with
22+
// "LLVM ERROR"
23+
auto FatalErrorHandler = [](void * /*HandlerData*/, const char *Reason,
24+
bool /*GenCrashDiag*/) {
25+
// Prevent recursion in case our error handler itself fails
26+
llvm::remove_fatal_error_handler();
27+
llvm::remove_bad_alloc_error_handler();
28+
29+
// Write the actual error message
30+
Logger::addLinePrefix(llvm::errs(), SeverityLevel::CRITICAL,
31+
std::nullopt);
32+
llvm::errs() << Reason << '\n';
33+
llvm::errs().flush();
34+
};
35+
36+
// NOTE: Install the bad_alloc handler before the fatal_error handler due to
37+
// a bug in LLVM
38+
// https://github.com/llvm/llvm-project/issues/83040
39+
llvm::install_bad_alloc_error_handler(FatalErrorHandler, nullptr);
40+
llvm::install_fatal_error_handler(FatalErrorHandler, nullptr);
41+
// llvm::install_out_of_memory_new_handler() is already done by InitLLVM
42+
return nullptr;
43+
}();
44+
(void)InitGlobals;
45+
}

tools/phasar-cli/CMakeLists.txt

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,14 @@ else()
2020
)
2121
endif()
2222

23-
# Warning! There is a another listing of libraries inside cmake/phasar_macros.cmake.
24-
# If this list is altered the other one should be altered accordingly.
23+
add_subdirectory(Controller)
24+
2525
target_link_libraries(phasar-cli
2626
PRIVATE
27-
phasar_config
28-
phasar_controller
29-
phasar_llvm_controlflow
30-
phasar_llvm_utils
31-
phasar_analysis_strategy
32-
phasar_llvm_ifdside
33-
phasar_utils
34-
phasar_mono
35-
phasar_llvm_db
36-
phasar_passes
37-
phasar_llvm_pointer
38-
phasar_llvm
39-
phasar_llvm_typehierarchy
40-
phasar_taintconfig
41-
27+
phasar
4228
${PHASAR_STD_FILESYSTEM}
4329
)
4430

4531
if (NOT PHASAR_IN_TREE)
46-
if(USE_LLVM_FAT_LIB)
47-
llvm_config(phasar-cli USE_SHARED ${LLVM_LINK_COMPONENTS})
48-
else()
49-
llvm_config(phasar-cli ${LLVM_LINK_COMPONENTS})
50-
endif()
51-
52-
install(TARGETS phasar-cli
53-
RUNTIME DESTINATION bin
54-
LIBRARY DESTINATION lib
55-
ARCHIVE DESTINATION lib
56-
)
32+
install(TARGETS phasar-cli)
5733
endif()

lib/Controller/AnalysisController.cpp renamed to tools/phasar-cli/Controller/AnalysisController.cpp

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Philipp Schubert and others
88
*****************************************************************************/
99

10-
#include "phasar/Controller/AnalysisController.h"
10+
#include "AnalysisController.h"
1111

1212
#include "phasar/PhasarLLVM/Passes/GeneralStatisticsAnalysis.h"
1313
#include "phasar/PhasarLLVM/TypeHierarchy/LLVMTypeHierarchy.h"
@@ -17,9 +17,8 @@
1717

1818
namespace psr {
1919

20-
static void
21-
emitRequestedHelperAnalysisResults(AnalysisController::ControllerData &Data) {
22-
auto WithResultFileOrStdout = [&ResultDirectory = Data.ResultDirectory](
20+
void AnalysisController::emitRequestedHelperAnalysisResults() {
21+
auto WithResultFileOrStdout = [&ResultDirectory = this->ResultDirectory](
2322
const auto &FileName, auto Callback) {
2423
if (!ResultDirectory.empty()) {
2524
if (auto OFS = openFileStream(ResultDirectory.string() + FileName)) {
@@ -30,8 +29,8 @@ emitRequestedHelperAnalysisResults(AnalysisController::ControllerData &Data) {
3029
}
3130
};
3231

33-
auto EmitterOptions = Data.EmitterOptions;
34-
auto &HA = *Data.HA;
32+
auto EmitterOptions = this->EmitterOptions;
33+
auto &HA = *this->HA;
3534

3635
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitIR) {
3736
WithResultFileOrStdout("/psr-preprocess-ir.ll", [&HA](auto &OS) {
@@ -96,24 +95,24 @@ emitRequestedHelperAnalysisResults(AnalysisController::ControllerData &Data) {
9695
}
9796
}
9897

99-
static void executeDemandDriven(AnalysisController::ControllerData & /*Data*/) {
98+
static void executeDemandDriven(AnalysisController & /*Data*/) {
10099
llvm::report_fatal_error(
101100
"AnalysisStrategy 'demand-driven' not supported, yet!");
102101
}
103-
static void executeIncremental(AnalysisController::ControllerData & /*Data*/) {
102+
static void executeIncremental(AnalysisController & /*Data*/) {
104103
llvm::report_fatal_error(
105104
"AnalysisStrategy 'incremental' not supported, yet!");
106105
}
107-
static void executeModuleWise(AnalysisController::ControllerData & /*Data*/) {
106+
static void executeModuleWise(AnalysisController & /*Data*/) {
108107
llvm::report_fatal_error(
109108
"AnalysisStrategy 'module-wise' not supported, yet!");
110109
}
111-
static void executeVariational(AnalysisController::ControllerData & /*Data*/) {
110+
static void executeVariational(AnalysisController & /*Data*/) {
112111
llvm::report_fatal_error(
113112
"AnalysisStrategy 'variational' not supported, yet!");
114113
}
115114

116-
static void executeWholeProgram(AnalysisController::ControllerData &Data) {
115+
static void executeWholeProgram(AnalysisController &Data) {
117116
for (auto DataFlowAnalysis : Data.DataFlowAnalyses) {
118117
using namespace controller;
119118
switch (DataFlowAnalysis) {
@@ -171,60 +170,31 @@ static void executeWholeProgram(AnalysisController::ControllerData &Data) {
171170
}
172171
}
173172

174-
static void executeAs(AnalysisController::ControllerData &Data,
175-
AnalysisStrategy Strategy) {
173+
void AnalysisController::run() {
176174
switch (Strategy) {
177175
case AnalysisStrategy::None:
178176
return;
179177
case AnalysisStrategy::DemandDriven:
180-
executeDemandDriven(Data);
178+
executeDemandDriven(*this);
181179
return;
182180
case AnalysisStrategy::Incremental:
183-
executeIncremental(Data);
181+
executeIncremental(*this);
184182
return;
185183
case AnalysisStrategy::ModuleWise:
186-
executeModuleWise(Data);
184+
executeModuleWise(*this);
187185
return;
188186
case AnalysisStrategy::Variational:
189-
executeVariational(Data);
187+
executeVariational(*this);
190188
return;
191189
case AnalysisStrategy::WholeProgram:
192-
executeWholeProgram(Data);
190+
executeWholeProgram(*this);
193191
return;
194192
}
195193
llvm_unreachable(
196194
"All AnalysisStrategy variants should be handled in the switch above!");
197195
}
198196

199-
AnalysisController::AnalysisController(
200-
HelperAnalyses &HA, std::vector<DataFlowAnalysisType> DataFlowAnalyses,
201-
std::vector<std::string> AnalysisConfigs,
202-
std::vector<std::string> EntryPoints, AnalysisStrategy Strategy,
203-
AnalysisControllerEmitterOptions EmitterOptions,
204-
IFDSIDESolverConfig SolverConfig, std::string ProjectID,
205-
std::string OutDirectory)
206-
: Data{
207-
&HA,
208-
std::move(DataFlowAnalyses),
209-
std::move(AnalysisConfigs),
210-
std::move(EntryPoints),
211-
Strategy,
212-
EmitterOptions,
213-
std::move(ProjectID),
214-
std::move(OutDirectory),
215-
SolverConfig,
216-
} {
217-
if (!Data.ResultDirectory.empty()) {
218-
// create directory for results
219-
Data.ResultDirectory /= Data.ProjectID + "-" + createTimeStamp();
220-
std::filesystem::create_directory(Data.ResultDirectory);
221-
}
222-
emitRequestedHelperAnalysisResults(Data);
223-
executeAs(Data, Strategy);
224-
}
225-
226-
LLVMTaintConfig
227-
controller::makeTaintConfig(AnalysisController::ControllerData &Data) {
197+
LLVMTaintConfig controller::makeTaintConfig(AnalysisController &Data) {
228198
std::string AnalysisConfigPath =
229199
!Data.AnalysisConfigs.empty() ? Data.AnalysisConfigs[0] : "";
230200
return !AnalysisConfigPath.empty()

0 commit comments

Comments
 (0)