Skip to content

Commit c8b06fd

Browse files
Lana243Columpio
authored andcommitted
[squash] of Annotations branch
1 parent 37f0da6 commit c8b06fd

Some content is hidden

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

61 files changed

+2792
-44
lines changed

.gitmodules

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[submodule "json"]
2-
path = json
2+
path = submodules/json
33
url = https://github.com/nlohmann/json.git
44
[submodule "optional"]
5-
path = optional
5+
path = submodules/optional
66
url = https://github.com/martinmoene/optional-lite.git
7+
[submodule "submodules/pugixml"]
8+
path = submodules/pugixml
9+
url = https://github.com/zeux/pugixml.git

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,21 @@ configure_file(${CMAKE_SOURCE_DIR}/include/klee/Config/CompileTimeInfo.h.cmin
667667
################################################################################
668668
include_directories("${CMAKE_BINARY_DIR}/include")
669669
include_directories("${CMAKE_SOURCE_DIR}/include")
670-
include_directories("${CMAKE_SOURCE_DIR}/json/include")
671-
include_directories("${CMAKE_SOURCE_DIR}/optional/include")
670+
include_directories("${CMAKE_SOURCE_DIR}/submodules/json/include")
671+
include_directories("${CMAKE_SOURCE_DIR}/submodules/optional/include")
672+
673+
674+
option(ENABLE_XML_ANNOTATION "Enable XML annotation" ON)
675+
676+
if (ENABLE_XML_ANNOTATION)
677+
message(STATUS "XML annotation is enabled")
678+
file(COPY "${CMAKE_SOURCE_DIR}/submodules/pugiconfig.hpp"
679+
DESTINATION "${CMAKE_SOURCE_DIR}/submodules/pugixml/src")
680+
include_directories("${CMAKE_SOURCE_DIR}/submodules/pugixml/src")
681+
else()
682+
message(STATUS "XML annotation is disabled")
683+
set(ENABLE_XML_ANNOTATION 0)
684+
endif()
672685
# set(KLEE_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/include)
673686

674687
################################################################################

include/klee/Config/config.h.cmin

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,7 @@ macro for that. That would simplify the C++ code. */
122122
/* Install directory for KLEE runtime */
123123
#define KLEE_INSTALL_RUNTIME_DIR "@KLEE_INSTALL_RUNTIME_DIR@"
124124

125+
/* Enable XML annotation parser */
126+
#define ENABLE_XML_ANNOTATION "@ENABLE_XML_ANNOTATION@"
127+
125128
#endif /* KLEE_CONFIG_H */

include/klee/Core/Interpreter.h

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define KLEE_INTERPRETER_H
1111

1212
#include "TerminationTypes.h"
13+
#include "klee/Module/Annotation.h"
1314

1415
#include "klee/Module/SarifReport.h"
1516

@@ -32,6 +33,7 @@ class BasicBlock;
3233
class Function;
3334
class LLVMContext;
3435
class Module;
36+
class Type;
3537
class raw_ostream;
3638
class raw_fd_ostream;
3739
} // namespace llvm
@@ -66,6 +68,13 @@ using FLCtoOpcode = std::unordered_map<
6668
std::unordered_map<
6769
unsigned, std::unordered_map<unsigned, std::unordered_set<unsigned>>>>;
6870

71+
enum class MockStrategy {
72+
None, // No mocks are generated
73+
Naive, // For each function call new symbolic value is generated
74+
Deterministic, // Each function is treated as uninterpreted function in SMT.
75+
// Compatible with Z3 solver only
76+
};
77+
6978
class Interpreter {
7079
public:
7180
enum class GuidanceKind {
@@ -82,21 +91,32 @@ class Interpreter {
8291
std::string LibraryDir;
8392
std::string EntryPoint;
8493
std::string OptSuffix;
94+
std::string MainCurrentName;
95+
std::string MainNameAfterMock;
96+
std::string AnnotationsFile;
8597
bool Optimize;
8698
bool Simplify;
8799
bool CheckDivZero;
88100
bool CheckOvershift;
101+
bool AnnotateOnlyExternal;
89102
bool WithFPRuntime;
90103
bool WithPOSIXRuntime;
91104

92105
ModuleOptions(const std::string &_LibraryDir,
93106
const std::string &_EntryPoint, const std::string &_OptSuffix,
94-
bool _Optimize, bool _Simplify, bool _CheckDivZero,
95-
bool _CheckOvershift, bool _WithFPRuntime,
107+
const std::string &_MainCurrentName,
108+
const std::string &_MainNameAfterMock,
109+
const std::string &_AnnotationsFile, bool _Optimize,
110+
bool _Simplify, bool _CheckDivZero, bool _CheckOvershift,
111+
bool _AnnotateOnlyExternal, bool _WithFPRuntime,
96112
bool _WithPOSIXRuntime)
97113
: LibraryDir(_LibraryDir), EntryPoint(_EntryPoint),
98-
OptSuffix(_OptSuffix), Optimize(_Optimize), Simplify(_Simplify),
99-
CheckDivZero(_CheckDivZero), CheckOvershift(_CheckOvershift),
114+
OptSuffix(_OptSuffix), MainCurrentName(_MainCurrentName),
115+
MainNameAfterMock(_MainNameAfterMock),
116+
AnnotationsFile(_AnnotationsFile), Optimize(_Optimize),
117+
Simplify(_Simplify), CheckDivZero(_CheckDivZero),
118+
CheckOvershift(_CheckOvershift),
119+
AnnotateOnlyExternal(_AnnotateOnlyExternal),
100120
WithFPRuntime(_WithFPRuntime), WithPOSIXRuntime(_WithPOSIXRuntime) {}
101121
};
102122

@@ -115,10 +135,11 @@ class Interpreter {
115135
unsigned MakeConcreteSymbolic;
116136
GuidanceKind Guidance;
117137
nonstd::optional<SarifReport> Paths;
138+
enum MockStrategy MockStrategy;
118139

119140
InterpreterOptions(nonstd::optional<SarifReport> Paths)
120141
: MakeConcreteSymbolic(false), Guidance(GuidanceKind::NoGuidance),
121-
Paths(std::move(Paths)) {}
142+
Paths(std::move(Paths)), MockStrategy(MockStrategy::None) {}
122143
};
123144

124145
protected:
@@ -141,13 +162,13 @@ class Interpreter {
141162
/// module
142163
/// \return The final module after it has been optimized, checks
143164
/// inserted, and modified for interpretation.
144-
virtual llvm::Module *
145-
setModule(std::vector<std::unique_ptr<llvm::Module>> &userModules,
146-
std::vector<std::unique_ptr<llvm::Module>> &libsModules,
147-
const ModuleOptions &opts,
148-
std::set<std::string> &&mainModuleFunctions,
149-
std::set<std::string> &&mainModuleGlobals,
150-
FLCtoOpcode &&origInstructions) = 0;
165+
virtual llvm::Module *setModule(
166+
std::vector<std::unique_ptr<llvm::Module>> &userModules,
167+
std::vector<std::unique_ptr<llvm::Module>> &libsModules,
168+
const ModuleOptions &opts, std::set<std::string> &&mainModuleFunctions,
169+
std::set<std::string> &&mainModuleGlobals, FLCtoOpcode &&origInstructions,
170+
const std::set<std::string> &ignoredExternals,
171+
std::vector<std::pair<std::string, std::string>> redefinitions) = 0;
151172

152173
// supply a tree stream writer which the interpreter will use
153174
// to record the concrete path (as a stream of '0' and '1' bytes).

include/klee/Core/MockBuilder.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#ifndef KLEE_MOCKBUILDER_H
2+
#define KLEE_MOCKBUILDER_H
3+
4+
#include "klee/Core/Interpreter.h"
5+
#include "klee/Module/Annotation.h"
6+
7+
#include "llvm/IR/IRBuilder.h"
8+
#include "llvm/IR/Module.h"
9+
10+
#include <set>
11+
#include <string>
12+
13+
namespace klee {
14+
15+
class MockBuilder {
16+
private:
17+
const llvm::Module *userModule;
18+
std::unique_ptr<llvm::Module> mockModule;
19+
std::unique_ptr<llvm::IRBuilder<>> builder;
20+
21+
const Interpreter::ModuleOptions &opts;
22+
23+
std::set<std::string> ignoredExternals;
24+
std::vector<std::pair<std::string, std::string>> redefinitions;
25+
26+
InterpreterHandler *interpreterHandler;
27+
28+
AnnotationsMap annotations;
29+
30+
void initMockModule();
31+
void buildMockMain();
32+
void buildExternalGlobalsDefinitions();
33+
void buildExternalFunctionsDefinitions();
34+
void
35+
buildCallKleeMakeSymbolic(const std::string &kleeMakeSymbolicFunctionName,
36+
llvm::Value *source, llvm::Type *type,
37+
const std::string &symbolicName);
38+
39+
void buildAnnotationForExternalFunctionArgs(
40+
llvm::Function *func,
41+
const std::vector<std::vector<Statement::Ptr>> &statementsNotAllign);
42+
void buildAnnotationForExternalFunctionReturn(
43+
llvm::Function *func, const std::vector<Statement::Ptr> &statements);
44+
void buildAnnotationForExternalFunctionProperties(
45+
llvm::Function *func, const std::set<Statement::Property> &properties);
46+
47+
std::map<std::string, llvm::FunctionType *> getExternalFunctions();
48+
std::map<std::string, llvm::Type *> getExternalGlobals();
49+
50+
std::pair<llvm::Value *, llvm::Value *>
51+
goByOffset(llvm::Value *value, const std::vector<std::string> &offset);
52+
53+
public:
54+
MockBuilder(const llvm::Module *initModule,
55+
const Interpreter::ModuleOptions &opts,
56+
const std::set<std::string> &ignoredExternals,
57+
std::vector<std::pair<std::string, std::string>> &redefinitions,
58+
InterpreterHandler *interpreterHandler);
59+
60+
std::unique_ptr<llvm::Module> build();
61+
void buildAllocSource(llvm::Value *prev, llvm::Type *elemType,
62+
const Statement::AllocSource *allocSourcePtr);
63+
void buildFree(llvm::Value *elem, const Statement::Free *freePtr);
64+
void processingValue(llvm::Value *prev, llvm::Type *elemType,
65+
const Statement::AllocSource *allocSourcePtr,
66+
const Statement::InitNull *initNullPtr);
67+
};
68+
69+
} // namespace klee
70+
71+
#endif // KLEE_MOCKBUILDER_H

include/klee/Expr/IndependentSet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class IndependentConstraintSet {
109109

110110
InnerSetUnion concretizedSets;
111111

112+
std::set<std::string> uninterpretedFunctions;
113+
112114
ref<const IndependentConstraintSet> addExpr(ref<Expr> e) const;
113115
ref<const IndependentConstraintSet>
114116
updateConcretization(const Assignment &delta,

include/klee/Expr/SourceBuilder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "klee/Expr/SymbolicSource.h"
66
#include "klee/Module/KModule.h"
77

8+
#include "llvm/IR/Function.h"
9+
810
namespace klee {
911

1012
class SourceBuilder {
@@ -28,6 +30,12 @@ class SourceBuilder {
2830
static ref<SymbolicSource> value(const llvm::Value &_allocSite, int _index,
2931
KModule *km);
3032
static ref<SymbolicSource> irreproducible(const std::string &name);
33+
static ref<SymbolicSource> mockNaive(const KModule *kModule,
34+
const llvm::Function &kFunction,
35+
unsigned version);
36+
static ref<SymbolicSource>
37+
mockDeterministic(const KModule *kModule, const llvm::Function &kFunction,
38+
const std::vector<ref<Expr>> &args);
3139
};
3240

3341
}; // namespace klee

include/klee/Expr/SymbolicSource.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ DISABLE_WARNING_POP
1717

1818
namespace klee {
1919

20+
class Expr;
2021
class Array;
2122
class Expr;
2223
class ConstantExpr;
2324
class KModule;
25+
struct KFunction;
2426

2527
class SymbolicSource {
2628
protected:
@@ -41,7 +43,9 @@ class SymbolicSource {
4143
LazyInitializationSize,
4244
Instruction,
4345
Argument,
44-
Irreproducible
46+
Irreproducible,
47+
MockNaive,
48+
MockDeterministic
4549
};
4650

4751
public:
@@ -361,6 +365,58 @@ class IrreproducibleSource : public SymbolicSource {
361365
}
362366
};
363367

368+
class MockSource : public SymbolicSource {
369+
public:
370+
const KModule *km;
371+
const llvm::Function &function;
372+
MockSource(const KModule *_km, const llvm::Function &_function)
373+
: km(_km), function(_function) {}
374+
375+
static bool classof(const SymbolicSource *S) {
376+
return S->getKind() == Kind::MockNaive ||
377+
S->getKind() == Kind::MockDeterministic;
378+
}
379+
};
380+
381+
class MockNaiveSource : public MockSource {
382+
public:
383+
const unsigned version;
384+
385+
MockNaiveSource(const KModule *km, const llvm::Function &function,
386+
unsigned _version)
387+
: MockSource(km, function), version(_version) {}
388+
389+
Kind getKind() const override { return Kind::MockNaive; }
390+
std::string getName() const override { return "mockNaive"; }
391+
392+
static bool classof(const SymbolicSource *S) {
393+
return S->getKind() == Kind::MockNaive;
394+
}
395+
396+
unsigned computeHash() override;
397+
398+
int internalCompare(const SymbolicSource &b) const override;
399+
};
400+
401+
class MockDeterministicSource : public MockSource {
402+
public:
403+
const std::vector<ref<Expr>> args;
404+
405+
MockDeterministicSource(const KModule *_km, const llvm::Function &_function,
406+
const std::vector<ref<Expr>> &_args);
407+
408+
Kind getKind() const override { return Kind::MockDeterministic; }
409+
std::string getName() const override { return "mockDeterministic"; }
410+
411+
static bool classof(const SymbolicSource *S) {
412+
return S->getKind() == Kind::MockDeterministic;
413+
}
414+
415+
unsigned computeHash() override;
416+
417+
int internalCompare(const SymbolicSource &b) const override;
418+
};
419+
364420
} // namespace klee
365421

366422
#endif /* KLEE_SYMBOLICSOURCE_H */

0 commit comments

Comments
 (0)