Skip to content

Commit

Permalink
[refactor] KBlock made abstract, reduntant VALUE constant from KValue…
Browse files Browse the repository at this point in the history
…Kind enum.
  • Loading branch information
S1eGa committed Dec 18, 2023
1 parent 7c47870 commit 0a0cc1a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
47 changes: 33 additions & 14 deletions include/klee/Module/KModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,17 @@ struct KBlock : public KValue {
return llvm::dyn_cast_or_null<llvm::BasicBlock>(value);
}

public:
private:
const KBlockType blockKind;

protected:
KBlock(KFunction *, llvm::BasicBlock *, KModule *,
const std::unordered_map<llvm::Instruction *, unsigned> &,
KInstruction **, unsigned &globalIndexInc);
KBlock() = delete;
KInstruction **, unsigned &globalIndexInc, KBlockType blockType);
KBlock(const KBlock &) = delete;
KBlock &operator=(const KBlock &) = delete;
virtual ~KBlock() = default;

virtual KBlockType getKBlockType() const { return KBlockType::Base; }
static bool classof(const KBlock *) { return true; }

public:
unsigned getNumInstructions() const noexcept { return basicBlock()->size(); }
KInstruction *getFirstInstruction() const noexcept { return instructions[0]; }
KInstruction *getLastInstruction() const noexcept {
Expand All @@ -89,24 +88,48 @@ struct KBlock : public KValue {

/// Block number in function
[[nodiscard]] uintptr_t getId() const;
[[nodiscard]] unsigned inModuleID() const;

// Block number in module
[[nodiscard]] unsigned getGlobalIndex() const;

/// Util methods defined for KValue
[[nodiscard]] bool operator<(const KValue &rhs) const override;
[[nodiscard]] unsigned hash() const override;

/// For LLVM RTTI purposes in KBlock inheritance system
[[nodiscard]] KBlockType getKBlockType() const { return blockKind; }
static bool classof(const KBlock *) { return true; }

/// For LLVM RTTI purposes in KValue inheritance system
static bool classof(const KValue *rhs) {
return rhs->getKind() == Kind::BLOCK && classof(cast<KBlock>(rhs));
}
};

typedef std::function<bool(KBlock *)> KBlockPredicate;

struct KBasicBlock : public KBlock {
public:
KBasicBlock(KFunction *, llvm::BasicBlock *, KModule *,
const std::unordered_map<llvm::Instruction *, unsigned> &,
KInstruction **, unsigned &globalIndexInc);

/// For LLVM RTTI purposes in KBlock inheritance system
static bool classof(const KBlock *rhs) {
return rhs->getKBlockType() == KBlockType::Base;
}

/// For LLVM RTTI purposes in KValue inheritance system
static bool classof(const KValue *rhs) {
return rhs->getKind() == Kind::BLOCK && classof(cast<KBasicBlock>(rhs));
}
};

struct KCallBlock : KBlock {
KInstruction *kcallInstruction;
std::set<llvm::Function *> calledFunctions;

public:
KCallBlock() = delete;
KCallBlock(KFunction *, llvm::BasicBlock *, KModule *,
const std::unordered_map<llvm::Instruction *, unsigned> &,
std::set<llvm::Function *>, KInstruction **,
Expand All @@ -115,7 +138,6 @@ struct KCallBlock : KBlock {
static bool classof(const KBlock *E) {
return E->getKBlockType() == KBlockType::Call;
}
KBlockType getKBlockType() const override { return KBlockType::Call; };
bool intrinsic() const;
bool internal() const;
bool kleeHandled() const;
Expand All @@ -128,15 +150,13 @@ struct KCallBlock : KBlock {

struct KReturnBlock : KBlock {
public:
KReturnBlock() = delete;
KReturnBlock(KFunction *, llvm::BasicBlock *, KModule *,
const std::unordered_map<llvm::Instruction *, unsigned> &,
KInstruction **, unsigned &globalIndexInc);
static bool classof(const KReturnBlock *) { return true; }
static bool classof(const KBlock *E) {
return E->getKBlockType() == KBlockType::Return;
}
KBlockType getKBlockType() const override { return KBlockType::Return; };

static bool classof(const KValue *rhs) {
return rhs->getKind() == Kind::BLOCK && classof(cast<KBlock>(rhs));
Expand Down Expand Up @@ -174,11 +194,10 @@ struct KFunction : public KCallable {
bool kleeHandled = false;

explicit KFunction(llvm::Function *, KModule *, unsigned &);
KFunction() = delete;
KFunction(const KFunction &) = delete;
KFunction &operator=(const KFunction &) = delete;

~KFunction();
~KFunction() override;

unsigned getArgRegister(unsigned index) const { return index; }

Expand Down
1 change: 0 additions & 1 deletion include/klee/Module/KValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct KValue {
FUNCTION,
INLINE_ASM,
INSTRUCTION,
VALUE,
};

protected:
Expand Down
6 changes: 1 addition & 5 deletions lib/Expr/SymbolicSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ int SymbolicSizeConstantAddressSource::internalCompare(
return size < ub.size ? -1 : 1;
}
if (allocSite != ub.allocSite) {
if (allocSite->getKind() == ub.allocSite->getKind()) {
return *allocSite < *ub.allocSite ? -1 : 1;
} else {
return allocSite->getKind() < ub.allocSite->getKind() ? -1 : 1;
}
return *allocSite < *ub.allocSite ? -1 : 1;
}

return 0;
Expand Down
39 changes: 25 additions & 14 deletions lib/Module/KModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,18 +619,18 @@ KFunction::KFunction(llvm::Function *_function, KModule *_km,
if (f) {
calledFunctions.insert(f);
}
auto *ckb =
new KCallBlock(this, &*bbit, parent, instructionToRegisterMap,
calledFunctions, &instructions[n], globalIndexInc);
auto *ckb = new KCallBlock(this, &*bbit, parent, instructionToRegisterMap,
std::move(calledFunctions), &instructions[n],
globalIndexInc);
kCallBlocks.push_back(ckb);
kb = ckb;
} else if (SplitReturns && isa<ReturnInst>(lit)) {
kb = new KReturnBlock(this, &*bbit, parent, instructionToRegisterMap,
&instructions[n], globalIndexInc);
returnKBlocks.push_back(kb);
} else {
kb = new KBlock(this, &*bbit, parent, instructionToRegisterMap,
&instructions[n], globalIndexInc);
kb = new KBasicBlock(this, &*bbit, parent, instructionToRegisterMap,
&instructions[n], globalIndexInc);
}
for (unsigned i = 0, ie = kb->getNumInstructions(); i < ie; i++, n++) {
instructionMap[instructions[n]->inst()] = instructions[n];
Expand Down Expand Up @@ -664,8 +664,10 @@ KFunction::~KFunction() {
KBlock::KBlock(
KFunction *_kfunction, llvm::BasicBlock *block, KModule *km,
const std::unordered_map<Instruction *, unsigned> &instructionToRegisterMap,
KInstruction **instructionsKF, unsigned &globalIndexInc)
: KValue(block, KValue::Kind::BLOCK), parent(_kfunction) {
KInstruction **instructionsKF, unsigned &globalIndexInc,
KBlockType blockType)
: KValue(block, KValue::Kind::BLOCK), blockKind(blockType),
parent(_kfunction) {
instructions = instructionsKF;

for (auto &it : *block) {
Expand All @@ -687,21 +689,21 @@ KBlock::KBlock(
}
}

unsigned KBlock::inModuleID() const {
return parent->getGlobalIndex() + static_cast<unsigned>(getId());
unsigned KBlock::getGlobalIndex() const {
return getFirstInstruction()->getGlobalIndex();
}

bool KBlock::operator<(const KValue &rhs) const {
// Additional comparison on block types is redundant,
// as inModuleID defines the position of block.
// as getGlobalIndex defines the position of block.
return getKind() == rhs.getKind()
? inModuleID() < cast<KBlock>(rhs).inModuleID()
? getGlobalIndex() < cast<KBlock>(rhs).getGlobalIndex()
: getKind() < rhs.getKind();
}

unsigned KBlock::hash() const {
// Use position of a block as a hash
return inModuleID();
return getGlobalIndex();
}

KCallBlock::KCallBlock(
Expand All @@ -710,7 +712,7 @@ KCallBlock::KCallBlock(
std::set<llvm::Function *> _calledFunctions, KInstruction **instructionsKF,
unsigned &globalIndexInc)
: KBlock::KBlock(_kfunction, block, km, instructionToRegisterMap,
instructionsKF, globalIndexInc),
instructionsKF, globalIndexInc, KBlockType::Call),
kcallInstruction(this->instructions[0]),
calledFunctions(std::move(_calledFunctions)) {}

Expand All @@ -737,12 +739,21 @@ KFunction *KCallBlock::getKFunction() const {
: nullptr;
}

KBasicBlock::KBasicBlock(KFunction *_kfunction, llvm::BasicBlock *block,
KModule *km,
const std::unordered_map<llvm::Instruction *, unsigned>
&instructionToRegisterMap,
KInstruction **instructionsKF,
unsigned &globalIndexInc)
: KBlock::KBlock(_kfunction, block, km, instructionToRegisterMap,
instructionsKF, globalIndexInc, KBlockType::Base) {}

KReturnBlock::KReturnBlock(
KFunction *_kfunction, llvm::BasicBlock *block, KModule *km,
const std::unordered_map<Instruction *, unsigned> &instructionToRegisterMap,
KInstruction **instructionsKF, unsigned &globalIndexInc)
: KBlock::KBlock(_kfunction, block, km, instructionToRegisterMap,
instructionsKF, globalIndexInc) {}
instructionsKF, globalIndexInc, KBlockType::Return) {}

std::string KBlock::getLabel() const {
std::string _label;
Expand Down

0 comments on commit 0a0cc1a

Please sign in to comment.