Skip to content

Commit

Permalink
[wip] remove instructionInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ladisgin committed Aug 28, 2023
1 parent 915e90a commit 713f4a7
Show file tree
Hide file tree
Showing 25 changed files with 803 additions and 659 deletions.
21 changes: 14 additions & 7 deletions include/klee/Core/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@

#include "TerminationTypes.h"
#include "klee/Module/Annotation.h"

#include "klee/Module/SarifReport.h"

#include <cstdint>
#include <map>
#include <memory>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <vector>

#include "llvm/ADT/StringRef.h"
#include <nonstd/optional.hpp>

using nonstd::optional;
Expand Down Expand Up @@ -63,6 +64,12 @@ class InterpreterHandler {
const char *suffix, bool isError = false) = 0;
};

// TODO remove
using FInstructions = std::unordered_map<
std::string,
std::unordered_map<
unsigned, std::unordered_map<unsigned, std::unordered_set<unsigned>>>>;

enum class MockStrategy {
None, // No mocks are generated
Naive, // For each function call new symbolic value is generated
Expand Down Expand Up @@ -161,9 +168,9 @@ class Interpreter {
setModule(std::vector<std::unique_ptr<llvm::Module>> &userModules,
std::vector<std::unique_ptr<llvm::Module>> &libsModules,
const ModuleOptions &opts,
const std::unordered_set<std::string> &mainModuleFunctions,
const std::unordered_set<std::string> &mainModuleGlobals,
std::unique_ptr<InstructionInfoTable> origInfos,
std::set<llvm::StringRef> &mainModuleFunctions,
std::set<llvm::StringRef> &mainModuleGlobals,
FInstructions &&origInstructions,
const std::set<std::string> &ignoredExternals,
std::vector<std::pair<std::string, std::string>> redefinitions) = 0;

Expand Down Expand Up @@ -217,7 +224,7 @@ class Interpreter {

virtual void
getCoveredLines(const ExecutionState &state,
std::map<const std::string *, std::set<unsigned>> &res) = 0;
std::map<std::string, std::set<size_t>> &res) = 0;

virtual void getBlockPath(const ExecutionState &state,
std::string &blockPath) = 0;
Expand Down
159 changes: 79 additions & 80 deletions include/klee/Module/InstructionInfoTable.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//===-- InstructionInfoTable.h ----------------------------------*- C++ -*-===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
////===-- InstructionInfoTable.h ----------------------------------*- C++ -*-===//
////
//// The KLEE Symbolic Virtual Machine
////
//// This file is distributed under the University of Illinois Open Source
//// License. See LICENSE.TXT for details.
////
////===----------------------------------------------------------------------===//

#ifndef KLEE_INSTRUCTIONINFOTABLE_H
#define KLEE_INSTRUCTIONINFOTABLE_H
Expand All @@ -31,82 +31,81 @@ class Module;

namespace klee {

/// @brief InstructionInfo stores debug information for a KInstruction.
struct InstructionInfo {
/// @brief The instruction id.
unsigned id;
/// @brief Line number in source file.
unsigned line;
/// @brief Column number in source file.
unsigned column;
/// @brief Line number in generated assembly.ll.
llvm::Optional<uint64_t> assemblyLine;
/// @brief Source file name.
const std::string &file;

public:
InstructionInfo(unsigned id, const std::string &file, unsigned line,
unsigned column, llvm::Optional<uint64_t> assemblyLine)
: id{id}, line{line}, column{column},
assemblyLine{assemblyLine}, file{file} {}
};

/// @brief FunctionInfo stores debug information for a KFunction.
struct FunctionInfo {
/// @brief The function id.
unsigned id;
/// @brief Line number in source file.
unsigned line;
/// @brief Line number in generated assembly.ll.
llvm::Optional<uint64_t> assemblyLine;
/// @brief Source file name.
const std::string &file;

public:
FunctionInfo(unsigned id, const std::string &file, unsigned line,
llvm::Optional<uint64_t> assemblyLine)
: id{id}, line{line}, assemblyLine{assemblyLine}, file{file} {}

FunctionInfo(const FunctionInfo &) = delete;
FunctionInfo &operator=(FunctionInfo const &) = delete;

FunctionInfo(FunctionInfo &&) = default;
//// TODO move to methods of kInstruction
///// @brief InstructionInfo stores debug information for a KInstruction.
//struct InstructionInfo {
// /// @brief The instruction id.
//// unsigned id; // TODO move to kInstruction
// /// @brief Line number in source file.
// // unsigned line;
// /// @brief Column number in source file.
// // unsigned column;
// /// @brief Line number in generated assembly.ll.
//// llvm::Optional<uint64_t> assemblyLine;
// /// @brief Source file name.
//// const std::string &file;
//
//public:
// InstructionInfo(unsigned id) {}
//};
//
///// @brief FunctionInfo stores debug information for a KFunction.
//struct FunctionInfo { // TODO clear this too
// /// @brief The function id.
//// unsigned id; // TODO move to kFunction
// /// @brief Line number in source file.
//// unsigned line;
// /// @brief Line number in generated assembly.ll.
//// llvm::Optional<uint64_t> assemblyLine;
// /// @brief Source file name.
//// const std::string &file;
//
//public:
// FunctionInfo(unsigned id) {}
//
// FunctionInfo(const FunctionInfo &) = delete;
// FunctionInfo &operator=(FunctionInfo const &) = delete;
//
// FunctionInfo(FunctionInfo &&) = default;
//};
//
//class InstructionInfoTable {
//public:
// using LocationToFunctionsMap =
// std::unordered_map<std::string,
// std::unordered_set<const llvm::Function *>>;
//
//private:
// std::unordered_map<const llvm::Instruction *,
// std::unique_ptr<InstructionInfo>> infos;
// std::unordered_map<const llvm::Function *, std::unique_ptr<FunctionInfo>>
// functionInfos;
// LocationToFunctionsMap fileNameToFunctions; // TODO remove
//// Instructions insts; // TODO remove when move prepare target to main
//
//public:
// explicit InstructionInfoTable(
// const llvm::Module &m);
//
//// unsigned getMaxID() const;
// const InstructionInfo &getInfo(const llvm::Instruction &) const;
// const FunctionInfo &getFunctionInfo(const llvm::Function &) const;
// const LocationToFunctionsMap &getFileNameToFunctions() const;
//// Instructions getInstructions();
//};

struct LocationInfo {
std::string file;
size_t line;
size_t column;
};

class InstructionInfoTable {
public:
using Instructions = std::unordered_map<
std::string,
std::unordered_map<
unsigned int,
std::unordered_map<unsigned int, std::unordered_set<unsigned int>>>>;
using LocationToFunctionsMap =
std::unordered_map<std::string,
std::unordered_set<const llvm::Function *>>;

private:
std::unordered_map<const llvm::Instruction *,
std::unique_ptr<InstructionInfo>>
infos;
std::unordered_map<const llvm::Function *, std::unique_ptr<FunctionInfo>>
functionInfos;
LocationToFunctionsMap fileNameToFunctions;
std::vector<std::unique_ptr<std::string>> internedStrings;
std::unordered_set<std::string> filesNames;
Instructions insts;
// TODO need unify with kFunction
LocationInfo getLocationInfo(const llvm::Function *func);

public:
explicit InstructionInfoTable(
const llvm::Module &m, std::unique_ptr<llvm::raw_fd_ostream> assemblyFS,
bool withInstructions = false);

unsigned getMaxID() const;
const InstructionInfo &getInfo(const llvm::Instruction &) const;
const FunctionInfo &getFunctionInfo(const llvm::Function &) const;
const LocationToFunctionsMap &getFileNameToFunctions() const;
const std::unordered_set<std::string> &getFilesNames() const;
Instructions getInstructions();
};
// TODO need unify with kInstruction
LocationInfo getLocationInfo(const llvm::Instruction *inst);

} // namespace klee

Expand Down
54 changes: 41 additions & 13 deletions include/klee/Module/KInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
#define KLEE_KINSTRUCTION_H

#include "klee/Config/Version.h"
#include "klee/Module/InstructionInfoTable.h"

#include "klee/Support/CompilerWarning.h"
DISABLE_WARNING_PUSH
DISABLE_WARNING_DEPRECATED_DECLARATIONS
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/raw_ostream.h"
DISABLE_WARNING_POP

#include <optional>
#include <vector>

namespace llvm {
Expand All @@ -28,34 +27,57 @@ class Instruction;

namespace klee {
class Executor;
struct InstructionInfo;
class KModule;
struct KFunction;
struct KBlock;

/// KInstruction - Intermediate instruction representation used
/// during execution.
struct KInstruction {
llvm::Instruction *inst;
const InstructionInfo *info;
// const InstructionInfo *info; // TODO remove it

/// Value numbers for each operand. -1 is an invalid value,
/// otherwise negative numbers are indices (negated and offset by
/// 2) into the module constant table and positive numbers are
/// register indices.
int *operands;
/// Destination register index.
unsigned dest;
KBlock *parent;

private:
// Instruction index in the basic block
unsigned index;
const unsigned globalIndex;

/// Destination register index.
// unsigned dest;
public:
KInstruction() = default;
explicit KInstruction(const KInstruction &ki);
/// Unique index for KFunction and KInstruction inside KModule
/// from 0 to [KFunction + KInstruction]
[[nodiscard]] unsigned getGlobalIndex() const;
/// Instruction index in the basic block
[[nodiscard]] unsigned getIndex() const;
/// Destination register index.
[[nodiscard]] unsigned getDest() const;

KInstruction(const std::unordered_map<llvm::Instruction *, unsigned>
&_instructionToRegisterMap,
llvm::Instruction *_inst, KModule *_km, KBlock *_kb,
unsigned &_globalIndexInc);

KInstruction() = delete; // TODO remove default constructor
explicit KInstruction(const KInstruction &ki) = delete;
virtual ~KInstruction();
std::string getSourceLocation() const;
std::string toString() const;

[[nodiscard]] size_t getLine() const;
[[nodiscard]] size_t getColumn() const;
[[nodiscard]] std::string getSourceFilepath() const;

[[nodiscard]] std::string getSourceLocationString() const;
[[nodiscard]] std::string toString() const;

[[nodiscard]] KBlock *getKBlock() const;
[[nodiscard]] KFunction *getKFunction() const;
[[nodiscard]] KModule *getKModule() const;
};

struct KGEPInstruction : KInstruction {
Expand All @@ -70,8 +92,14 @@ struct KGEPInstruction : KInstruction {
uint64_t offset;

public:
KGEPInstruction() = default;
explicit KGEPInstruction(const KGEPInstruction &ki);
KGEPInstruction(const std::unordered_map<llvm::Instruction *, unsigned>
&_instructionToRegisterMap,
llvm::Instruction *_inst, KModule *_km, KBlock *_kb,
unsigned &_globalIndexInc)
: KInstruction(_instructionToRegisterMap, _inst, _km, _kb,
_globalIndexInc) {}
KGEPInstruction() = delete;
explicit KGEPInstruction(const KGEPInstruction &ki) = delete;
};
} // namespace klee

Expand Down
Loading

0 comments on commit 713f4a7

Please sign in to comment.