Skip to content

Commit b15adfb

Browse files
committed
Merge branch 'f-FixConfigUB' into development
2 parents f6783d6 + 543dfd7 commit b15adfb

8 files changed

Lines changed: 69 additions & 82 deletions

File tree

include/phasar/Config/Configuration.h

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "boost/program_options.hpp"
2424

2525
#include "llvm/ADT/iterator_range.h"
26-
#include "llvm/Support/ManagedStatic.h"
2726

2827
#include "phasar/Config/Version.h"
2928

@@ -36,57 +35,63 @@ class PhasarConfig {
3635
public:
3736
/// Current Phasar version
3837
// NOLINTNEXTLINE(readability-identifier-naming)
39-
static std::string PhasarVersion() { return XSTR(PHASAR_VERSION); }
38+
[[nodiscard]] static std::string PhasarVersion() {
39+
return XSTR(PHASAR_VERSION);
40+
}
4041

4142
/// Stores the label/ tag with which we annotate the LLVM IR.
4243
// NOLINTNEXTLINE(readability-identifier-naming)
43-
static std::string MetaDataKind() { return "psr.id"; }
44+
[[nodiscard]] static std::string MetaDataKind() { return "psr.id"; }
4445

46+
/// Specifies the directory in which important configuration files are
47+
/// located.
4548
// NOLINTNEXTLINE(readability-identifier-naming)
46-
static std::string ConfigurationDirectory() { return ConfigDir; }
49+
[[nodiscard]] static const std::string &ConfigurationDirectory();
4750

4851
/// Specifies the directory in which Phasar is located.
4952
// NOLINTNEXTLINE(readability-identifier-naming)
50-
static std::string PhasarDirectory() { return PhasarDir; }
53+
[[nodiscard]] static const std::string &PhasarDirectory();
5154

5255
/// Name of the file storing all standard header search paths used for
5356
/// compilation.
5457
// NOLINTNEXTLINE(readability-identifier-naming)
55-
static std::string HeaderSearchPathsFileName() {
58+
[[nodiscard]] static std::string HeaderSearchPathsFileName() {
5659
return "standard_header_paths.conf";
5760
}
5861

5962
/// Name of the compile_commands.json file (in case we wish to rename)
6063
// NOLINTNEXTLINE(readability-identifier-naming)
61-
static std::string CompileCommandsJson() { return "compile_commands.json"; }
64+
[[nodiscard]] static std::string CompileCommandsJson() {
65+
return "compile_commands.json";
66+
}
6267

6368
/// Default Source- and Sink-Functions path
6469
// NOLINTNEXTLINE(readability-identifier-naming)
65-
static std::string DefaultSourceSinkFunctionsPath() {
66-
return std::string(PhasarDirectory() +
67-
"config/phasar-source-sink-function.json");
70+
[[nodiscard]] static std::string DefaultSourceSinkFunctionsPath() {
71+
return PhasarDirectory() + "config/phasar-source-sink-function.json";
6872
}
6973

7074
// Variables to be used in JSON export format
7175
/// Identifier for call graph export
7276
// NOLINTNEXTLINE(readability-identifier-naming)
73-
static std::string JsonCallGraphID() { return "psr.cg"; }
77+
[[nodiscard]] static std::string JsonCallGraphID() { return "psr.cg"; }
7478

7579
/// Identifier for type hierarchy graph export
7680
// NOLINTNEXTLINE(readability-identifier-naming)
77-
static std::string JsonTypeHierarchyID() { return "psr.th"; }
81+
[[nodiscard]] static std::string JsonTypeHierarchyID() { return "psr.th"; }
7882

7983
/// Identifier for points-to graph export
8084
// NOLINTNEXTLINE(readability-identifier-naming)
81-
static std::string JsonPointsToGraphID() { return "psr.pt"; }
85+
[[nodiscard]] static std::string JsonPointsToGraphID() { return "psr.pt"; }
8286

8387
/// Identifier for data-flow results export
8488
// NOLINTNEXTLINE(readability-identifier-naming)
85-
static std::string JsonDataFlowID() { return "psr.df"; }
89+
[[nodiscard]] static std::string JsonDataFlowID() { return "psr.df"; }
8690

87-
static PhasarConfig &getPhasarConfig();
91+
[[nodiscard]] static PhasarConfig &getPhasarConfig();
8892

89-
llvm::iterator_range<std::set<std::string>::iterator> specialFunctionNames() {
93+
[[nodiscard]] llvm::iterator_range<std::set<std::string>::iterator>
94+
specialFunctionNames() const {
9095
return llvm::make_range(SpecialFuncNames.begin(), SpecialFuncNames.end());
9196
}
9297

@@ -102,7 +107,7 @@ class PhasarConfig {
102107

103108
/// Variables map of the parsed command-line parameters
104109
// NOLINTNEXTLINE(readability-identifier-naming)
105-
static boost::program_options::variables_map &VariablesMap() {
110+
[[nodiscard]] static boost::program_options::variables_map &VariablesMap() {
106111
static boost::program_options::variables_map VMap;
107112
return VMap;
108113
}
@@ -116,40 +121,21 @@ class PhasarConfig {
116121
private:
117122
PhasarConfig();
118123

119-
static std::string readConfigFile(const std::string &Path);
120124
void loadGlibcSpecialFunctionNames();
121125
void loadLLVMSpecialFunctionNames();
122126

123127
std::set<std::string> SpecialFuncNames;
124128

125-
/// Specifies the directory in which important configuration files are
126-
/// located.
127-
inline static const std::string ConfigDir = []() {
128-
auto *EnvHome = std::getenv("HOME");
129-
std::string ConfigFolder = "config/";
130-
if (EnvHome) { // Check if HOME was defined in the environment
131-
std::string PhasarConfDir = std::string(EnvHome) + "/.config/phasar/";
132-
if (std::filesystem::exists(PhasarConfDir) &&
133-
std::filesystem::is_directory(PhasarConfDir)) {
134-
ConfigFolder = PhasarConfDir;
135-
}
136-
}
137-
return ConfigFolder;
138-
}();
139-
140-
/// Specifies the directory in which Phasar is located.
141-
static const std::string PhasarDir;
142-
143129
/// Name of the file storing all glibc function names.
144-
static inline const std::string GLIBCFunctionListFileName =
130+
static inline auto GLIBCFunctionListFileName =
145131
"glibc_function_list_v1-04.05.17.conf";
146132

147133
/// Name of the file storing all LLVM intrinsic function names.
148-
static inline const std::string LLVMIntrinsicFunctionListFileName =
134+
static inline auto LLVMIntrinsicFunctionListFileName =
149135
"llvm_intrinsics_function_list_v1-04.05.17.conf";
150136

151137
/// Log file directory
152-
static inline const std::string LogFileDirectory = "log/";
138+
static inline auto LogFileDirectory = "log/";
153139
};
154140

155141
} // namespace psr

include/phasar/Utils/IO.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@
2727

2828
namespace psr {
2929

30-
std::string readTextFile(const std::filesystem::path &Path);
30+
std::string readTextFile(const llvm::Twine &Path);
3131

32-
std::unique_ptr<llvm::MemoryBuffer> readFile(const std::filesystem::path &Path);
3332
std::unique_ptr<llvm::MemoryBuffer> readFile(const llvm::Twine &Path);
3433

3534
nlohmann::json readJsonFile(const llvm::Twine &Path);
36-
nlohmann::json readJsonFile(const std::filesystem::path &Path);
3735

38-
void writeTextFile(const std::filesystem::path &Path, llvm::StringRef Content);
36+
void writeTextFile(const llvm::Twine &Path, llvm::StringRef Content);
3937

4038
} // namespace psr
4139

lib/Config/Configuration.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "phasar/Config/Configuration.h"
2323
#include "phasar/Config/Version.h"
24+
#include "phasar/Utils/IO.h"
2425

2526
using namespace psr;
2627

@@ -34,22 +35,32 @@ PhasarConfig::PhasarConfig() {
3435
SpecialFuncNames.insert({"_Znwm", "_Znam", "_ZdlPv", "_ZdaPv"});
3536
}
3637

37-
std::string PhasarConfig::readConfigFile(const std::string &Path) {
38-
// We use a local file reading function to make phasar_config independent of
39-
// other phasar libraries.
40-
if (std::filesystem::exists(Path) && !std::filesystem::is_directory(Path)) {
41-
std::ifstream Ifs(Path, std::ios::binary);
42-
if (Ifs.is_open()) {
43-
Ifs.seekg(0, std::ifstream::end);
44-
size_t FileSize = Ifs.tellg();
45-
Ifs.seekg(0, std::ifstream::beg);
46-
std::string Content(FileSize + 1, '\0');
47-
std::stringstream SStream;
48-
SStream << Ifs.rdbuf();
49-
return SStream.str();
38+
const std::string &PhasarConfig::ConfigurationDirectory() {
39+
static const std::string ConfigDir = [] {
40+
auto *EnvHome = std::getenv("HOME");
41+
std::string ConfigFolder = "config/";
42+
if (EnvHome) { // Check if HOME was defined in the environment
43+
std::string PhasarConfDir = std::string(EnvHome) + "/.config/phasar/";
44+
if (std::filesystem::exists(PhasarConfDir) &&
45+
std::filesystem::is_directory(PhasarConfDir)) {
46+
ConfigFolder = PhasarConfDir;
47+
}
5048
}
51-
}
52-
throw std::ios_base::failure("could not read file: " + Path);
49+
return ConfigFolder;
50+
}();
51+
52+
return ConfigDir;
53+
}
54+
55+
/// Specifies the directory in which Phasar is located.
56+
const std::string &PhasarConfig::PhasarDirectory() {
57+
static const std::string PhasarDir = [] {
58+
std::string CurrPath = std::filesystem::current_path().string();
59+
size_t I = CurrPath.rfind("build", CurrPath.length());
60+
return CurrPath.substr(0, I);
61+
}();
62+
63+
return PhasarDir;
5364
}
5465

5566
void PhasarConfig::loadGlibcSpecialFunctionNames() {
@@ -59,7 +70,7 @@ void PhasarConfig::loadGlibcSpecialFunctionNames() {
5970
if (std::filesystem::exists(GLIBCFunctionListFilePath)) {
6071
// Load glibc function names specified in the config file
6172
std::vector<std::string> GlibcFunctions;
62-
std::string Glibc = readConfigFile(GLIBCFunctionListFilePath);
73+
std::string Glibc = readTextFile(GLIBCFunctionListFilePath);
6374
// Insert glibc function names
6475
boost::split(GlibcFunctions, Glibc, boost::is_any_of("\n"),
6576
boost::token_compress_on);
@@ -76,7 +87,7 @@ void PhasarConfig::loadLLVMSpecialFunctionNames() {
7687
ConfigurationDirectory() + LLVMIntrinsicFunctionListFileName;
7788
if (std::filesystem::exists(LLVMFunctionListFilePath)) {
7889
// Load LLVM function names specified in the config file
79-
std::string LLVMIntrinsics = readConfigFile(LLVMFunctionListFilePath);
90+
std::string LLVMIntrinsics = readTextFile(LLVMFunctionListFilePath);
8091

8192
std::vector<std::string> LLVMIntrinsicFunctions;
8293
boost::split(LLVMIntrinsicFunctions, LLVMIntrinsics, boost::is_any_of("\n"),
@@ -91,12 +102,6 @@ void PhasarConfig::loadLLVMSpecialFunctionNames() {
91102
}
92103
}
93104

94-
const std::string PhasarConfig::PhasarDir = std::string([]() {
95-
std::string CurrPath = std::filesystem::current_path().string();
96-
size_t I = CurrPath.rfind("build", CurrPath.length());
97-
return CurrPath.substr(0, I);
98-
}());
99-
100105
PhasarConfig &PhasarConfig::getPhasarConfig() {
101106
static PhasarConfig PC;
102107
return PC;

lib/PhasarLLVM/TaintConfig/TaintConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TaintConfig &TC) {
562562

563563
nlohmann::json parseTaintConfig(const std::filesystem::path &Path) {
564564

565-
nlohmann::json TaintConfig = readJsonFile(Path);
565+
nlohmann::json TaintConfig = readJsonFile(Path.string());
566566
nlohmann::json_schema::json_validator Validator;
567567
try {
568568
Validator.set_root_schema(TaintConfigSchema); // insert root-schema

lib/Utils/IO.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222
#include <system_error>
2323

24+
#include "llvm/ADT/SmallString.h"
2425
#include "llvm/Support/MemoryBuffer.h"
2526
#include "llvm/Support/raw_ostream.h"
2627

@@ -31,16 +32,11 @@
3132

3233
namespace psr {
3334

34-
std::string readTextFile(const std::filesystem::path &Path) {
35+
std::string readTextFile(const llvm::Twine &Path) {
3536
auto Buffer = readFile(Path);
3637
return Buffer->getBuffer().str();
3738
}
3839

39-
std::unique_ptr<llvm::MemoryBuffer>
40-
readFile(const std::filesystem::path &Path) {
41-
return readFile(llvm::StringRef(Path.string()));
42-
}
43-
4440
std::unique_ptr<llvm::MemoryBuffer> readFile(const llvm::Twine &Path) {
4541
auto Ret = llvm::MemoryBuffer::getFile(Path);
4642

@@ -57,13 +53,10 @@ nlohmann::json readJsonFile(const llvm::Twine &Path) {
5753
return nlohmann::json::parse(Buf->getBufferStart(), Buf->getBufferEnd());
5854
}
5955

60-
nlohmann::json readJsonFile(const std::filesystem::path &Path) {
61-
return readJsonFile(llvm::StringRef(Path.string()));
62-
}
63-
64-
void writeTextFile(const std::filesystem::path &Path, llvm::StringRef Content) {
56+
void writeTextFile(const llvm::Twine &Path, llvm::StringRef Content) {
6557
std::error_code EC;
66-
llvm::raw_fd_ostream ROS(Path.string(), EC);
58+
llvm::SmallString<256> Buf;
59+
llvm::raw_fd_ostream ROS(Path.toNullTerminatedStringRef(Buf), EC);
6760

6861
if (EC) {
6962
throw std::system_error(EC);

unittests/PhasarLLVM/TypeHierarchy/LLVMTypeHierarchyTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "TestConfig.h"
1414

1515
#include "llvm/Demangle/Demangle.h"
16+
#include "llvm/Support/ManagedStatic.h"
1617

1718
using namespace std;
1819
using namespace psr;

unittests/PhasarLLVM/TypeHierarchy/TypeGraphTest.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
#include "phasar/Config/Configuration.h"
12
#include "phasar/DB/ProjectIRDB.h"
23
#include "phasar/PhasarLLVM/Pointer/TypeGraphs/CachedTypeGraph.h"
34
#include "phasar/PhasarLLVM/Pointer/TypeGraphs/LazyTypeGraph.h"
5+
#include "phasar/Utils/LLVMShorthands.h"
6+
#include "phasar/Utils/Utilities.h"
7+
48
#include "gtest/gtest.h"
59

610
#include "boost/graph/isomorphism.hpp"
7-
#include "phasar/Config/Configuration.h"
8-
#include "phasar/Utils/LLVMShorthands.h"
9-
#include "phasar/Utils/Utilities.h"
1011

1112
#include "TestConfig.h"
1213

14+
#include "llvm/Support/ManagedStatic.h"
15+
1316
using namespace std;
1417
using namespace psr;
1518

unittests/Utils/IOTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
TEST(IO, ReadTextFile) {
1010
std::string File("test.txt");
1111
std::string Path = psr::unittest::PathToTxtTestFiles + File;
12+
1213
std::string Contents = psr::readTextFile(Path);
1314
std::string Expected =
1415
R"(This is a test file that contains very interesting content.

0 commit comments

Comments
 (0)