Skip to content

Commit ebca2f3

Browse files
S1eGamisonijnik
authored andcommitted
[feat] Adds support for reporting errors in SARIF format:
* Introduces `CodeEvent` as a base unit for storing information about event in code. * `CodeEvent`s are managed with `EventRecorder`, capable of serializing recorded trace. * `termianateStateOnProgramError` receives an `ErrorEvent` object contatining all required information about error. [feat] Enhances `gepExprBases`: * Bases for addresses stored for constant expressions. * Precalculates bases for `llvm::ConstantExpr` (i.e. for `getElementPtr` in arguments of instructions). [perf] Removes checks on `baseInBounds` during memory operations. [fix] Adds hacks for managing objects with neighboring addresses (in some cases `gepExprBases` could assume that the beginning of one object is the end of another). [fix] Fixes ODR violation with `llvm::APFloat::RoundingMode`.
1 parent b399e64 commit ebca2f3

File tree

84 files changed

+3404
-169
lines changed

Some content is hidden

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

84 files changed

+3404
-169
lines changed

include/klee/ADT/ImmutableList.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ template <typename T> class ImmutableList {
9797
node->values.push_back(value);
9898
}
9999

100-
bool empty() { return size() == 0; }
100+
bool empty() const { return size() == 0; }
101101

102-
const T &back() {
102+
const T &back() const {
103103
assert(node && "requiers not empty list");
104104
auto it = iterator(node.get());
105105
it.get = size() - 1;
@@ -109,6 +109,10 @@ template <typename T> class ImmutableList {
109109
ImmutableList() : node(){};
110110
ImmutableList(const ImmutableList<T> &il)
111111
: node(std::make_shared<ImmutableListNode>(il)) {}
112+
ImmutableList &operator=(const ImmutableList<T> &il) {
113+
node = std::make_shared<ImmutableListNode>(il);
114+
return *this;
115+
}
112116
};
113117

114118
} // namespace klee

include/klee/Core/Interpreter.h

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class raw_fd_ostream;
3838
namespace klee {
3939
class ExecutionState;
4040
struct SarifReport;
41+
struct ToolJson;
4142
class Interpreter;
4243
class TreeStreamWriter;
4344

@@ -57,6 +58,8 @@ class InterpreterHandler {
5758

5859
virtual void processTestCase(const ExecutionState &state, const char *message,
5960
const char *suffix, bool isError = false) = 0;
61+
62+
virtual ToolJson info() const = 0;
6063
};
6164

6265
/// [File][Line][Column] -> Opcode
@@ -228,6 +231,10 @@ class Interpreter {
228231

229232
virtual bool getSymbolicSolution(const ExecutionState &state, KTest &res) = 0;
230233

234+
virtual void addSARIFReport(const ExecutionState &state) = 0;
235+
236+
virtual SarifReportJson getSARIFReport() const = 0;
237+
231238
virtual void logState(const ExecutionState &state, int id,
232239
std::unique_ptr<llvm::raw_fd_ostream> &f) = 0;
233240

include/klee/Expr/SourceBuilder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
namespace klee {
77

8-
class KInstruction;
9-
class KGlobalVariable;
8+
struct KInstruction;
9+
struct KGlobalVariable;
1010

1111
template <typename T, typename Eq> class SparseStorage;
1212
template <typename T> class ref;

include/klee/Module/KModule.h

+2
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ class KModule {
381381

382382
KBlock *getKBlock(const llvm::BasicBlock *bb);
383383

384+
bool inMainModule(const llvm::Instruction &i);
385+
384386
bool inMainModule(const llvm::Function &f);
385387

386388
bool inMainModule(const llvm::GlobalVariable &v);

include/klee/Module/LocationInfo.h

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
////===-- LocationInfo.h ----------------------------------*- C++ -*-===//
2-
////
3-
//// The KLEE Symbolic Virtual Machine
4-
////
5-
//// This file is distributed under the University of Illinois Open Source
6-
//// License. See LICENSE.TXT for details.
7-
////
8-
////===----------------------------------------------------------------------===//
1+
////===-- LocationInfo.h ----------------------------------------*- C++ -*-===//
2+
//
3+
// The KLEEF Symbolic Virtual Machine
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
99

1010
#ifndef KLEE_LOCATIONINFO_H
1111
#define KLEE_LOCATIONINFO_H
1212

13+
#include <cstdint>
1314
#include <memory>
15+
#include <optional>
1416
#include <string>
1517

1618
namespace llvm {
@@ -21,11 +23,27 @@ class Module;
2123
} // namespace llvm
2224

2325
namespace klee {
26+
struct PhysicalLocationJson;
27+
}
2428

29+
namespace klee {
30+
31+
/// @brief Immutable struct representing location in source code.
2532
struct LocationInfo {
26-
std::string file;
27-
size_t line;
28-
size_t column;
33+
/// @brief Path to source file for that location.
34+
const std::string file;
35+
36+
/// @brief Code line in source file.
37+
const uint64_t line;
38+
39+
/// @brief Column number in source file.
40+
const std::optional<uint64_t> column;
41+
42+
/// @brief Converts location info to SARIFs representation
43+
/// of location.
44+
/// @param location location info in source code.
45+
/// @return SARIFs representation of location.
46+
PhysicalLocationJson serialize() const;
2947
};
3048

3149
LocationInfo getLocationInfo(const llvm::Function *func);

include/klee/Module/SarifReport.h

+28-10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ std::string getErrorsString(const std::vector<ReachWithError> &errors);
5858

5959
struct FunctionInfo;
6060
struct KBlock;
61+
struct LocationInfo;
62+
63+
struct Message {
64+
std::string text;
65+
};
6166

6267
struct ArtifactLocationJson {
6368
std::optional<std::string> uri;
@@ -76,6 +81,7 @@ struct PhysicalLocationJson {
7681
};
7782

7883
struct LocationJson {
84+
std::optional<Message> message;
7985
std::optional<PhysicalLocationJson> physicalLocation;
8086
};
8187

@@ -92,10 +98,6 @@ struct CodeFlowJson {
9298
std::vector<ThreadFlowJson> threadFlows;
9399
};
94100

95-
struct Message {
96-
std::string text;
97-
};
98-
99101
struct Fingerprints {
100102
std::string cooddy_uid;
101103
};
@@ -110,15 +112,25 @@ static void from_json(const json &j, Fingerprints &p) {
110112

111113
struct ResultJson {
112114
std::optional<std::string> ruleId;
115+
std::optional<std::string> level;
113116
std::optional<Message> message;
114117
std::optional<unsigned> id;
115118
std::optional<Fingerprints> fingerprints;
116119
std::vector<LocationJson> locations;
117120
std::vector<CodeFlowJson> codeFlows;
118121
};
119122

123+
struct RuleJson {
124+
std::string id;
125+
std::optional<std::string> name;
126+
std::optional<Message> shortDescription;
127+
std::optional<std::string> helpUri;
128+
};
129+
120130
struct DriverJson {
121131
std::string name;
132+
std::optional<std::string> informationUri;
133+
std::vector<RuleJson> rules;
122134
};
123135

124136
struct ToolJson {
@@ -131,9 +143,13 @@ struct RunJson {
131143
};
132144

133145
struct SarifReportJson {
146+
std::string version;
134147
std::vector<RunJson> runs;
135148
};
136149

150+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(RuleJson, id, name,
151+
shortDescription, helpUri)
152+
137153
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ArtifactLocationJson, uri)
138154

139155
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(RegionJson, startLine, endLine,
@@ -142,7 +158,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(RegionJson, startLine, endLine,
142158
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(PhysicalLocationJson,
143159
artifactLocation, region)
144160

145-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(LocationJson, physicalLocation)
161+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(LocationJson, message,
162+
physicalLocation)
146163

147164
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ThreadFlowLocationJson,
148165
location, metadata)
@@ -153,17 +170,18 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(CodeFlowJson, threadFlows)
153170

154171
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Message, text)
155172

156-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ResultJson, ruleId, message, id,
157-
fingerprints, codeFlows,
158-
locations)
173+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ResultJson, ruleId, level,
174+
message, id, fingerprints,
175+
codeFlows, locations)
159176

160-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(DriverJson, name)
177+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(DriverJson, name,
178+
informationUri, rules)
161179

162180
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ToolJson, driver)
163181

164182
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(RunJson, results, tool)
165183

166-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SarifReportJson, runs)
184+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(SarifReportJson, version, runs)
167185

168186
struct Location {
169187
struct LocationHash {

lib/Core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ add_library(kleeCore
1010
AddressManager.cpp
1111
AddressSpace.cpp
1212
CallPathManager.cpp
13+
CodeLocation.cpp
1314
Context.cpp
1415
CoreStats.cpp
1516
CXXTypeSystem/CXXTypeManager.cpp
1617
DistanceCalculator.cpp
18+
EventRecorder.cpp
1719
ExecutionState.cpp
1820
Executor.cpp
1921
ExecutorUtil.cpp

0 commit comments

Comments
 (0)