From 0a0cc1a8eb77c655c68cd7f527c0a2eee745de04 Mon Sep 17 00:00:00 2001 From: Sergey Morozov Date: Mon, 18 Dec 2023 16:45:01 +0300 Subject: [PATCH] [refactor] KBlock made abstract, reduntant VALUE constant from KValueKind enum. --- include/klee/Module/KModule.h | 47 ++++++++++++++++++++++++----------- include/klee/Module/KValue.h | 1 - lib/Expr/SymbolicSource.cpp | 6 +---- lib/Module/KModule.cpp | 39 ++++++++++++++++++----------- 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/include/klee/Module/KModule.h b/include/klee/Module/KModule.h index d46daf029d..5b8871b2e2 100644 --- a/include/klee/Module/KModule.h +++ b/include/klee/Module/KModule.h @@ -67,18 +67,17 @@ struct KBlock : public KValue { return llvm::dyn_cast_or_null(value); } -public: +private: + const KBlockType blockKind; + +protected: KBlock(KFunction *, llvm::BasicBlock *, KModule *, const std::unordered_map &, - 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 { @@ -89,11 +88,19 @@ 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(rhs)); } @@ -101,12 +108,28 @@ struct KBlock : public KValue { typedef std::function KBlockPredicate; +struct KBasicBlock : public KBlock { +public: + KBasicBlock(KFunction *, llvm::BasicBlock *, KModule *, + const std::unordered_map &, + 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(rhs)); + } +}; + struct KCallBlock : KBlock { KInstruction *kcallInstruction; std::set calledFunctions; public: - KCallBlock() = delete; KCallBlock(KFunction *, llvm::BasicBlock *, KModule *, const std::unordered_map &, std::set, KInstruction **, @@ -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; @@ -128,7 +150,6 @@ struct KCallBlock : KBlock { struct KReturnBlock : KBlock { public: - KReturnBlock() = delete; KReturnBlock(KFunction *, llvm::BasicBlock *, KModule *, const std::unordered_map &, KInstruction **, unsigned &globalIndexInc); @@ -136,7 +157,6 @@ struct KReturnBlock : KBlock { 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(rhs)); @@ -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; } diff --git a/include/klee/Module/KValue.h b/include/klee/Module/KValue.h index 857d7651af..278ebed39d 100644 --- a/include/klee/Module/KValue.h +++ b/include/klee/Module/KValue.h @@ -18,7 +18,6 @@ struct KValue { FUNCTION, INLINE_ASM, INSTRUCTION, - VALUE, }; protected: diff --git a/lib/Expr/SymbolicSource.cpp b/lib/Expr/SymbolicSource.cpp index f0321ec55a..2dc798d32d 100644 --- a/lib/Expr/SymbolicSource.cpp +++ b/lib/Expr/SymbolicSource.cpp @@ -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; diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp index 0aac22578e..f7ce474bdb 100644 --- a/lib/Module/KModule.cpp +++ b/lib/Module/KModule.cpp @@ -619,9 +619,9 @@ 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(lit)) { @@ -629,8 +629,8 @@ KFunction::KFunction(llvm::Function *_function, KModule *_km, &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]; @@ -664,8 +664,10 @@ KFunction::~KFunction() { KBlock::KBlock( KFunction *_kfunction, llvm::BasicBlock *block, KModule *km, const std::unordered_map &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) { @@ -687,21 +689,21 @@ KBlock::KBlock( } } -unsigned KBlock::inModuleID() const { - return parent->getGlobalIndex() + static_cast(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(rhs).inModuleID() + ? getGlobalIndex() < cast(rhs).getGlobalIndex() : getKind() < rhs.getKind(); } unsigned KBlock::hash() const { // Use position of a block as a hash - return inModuleID(); + return getGlobalIndex(); } KCallBlock::KCallBlock( @@ -710,7 +712,7 @@ KCallBlock::KCallBlock( std::set _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)) {} @@ -737,12 +739,21 @@ KFunction *KCallBlock::getKFunction() const { : nullptr; } +KBasicBlock::KBasicBlock(KFunction *_kfunction, llvm::BasicBlock *block, + KModule *km, + const std::unordered_map + &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 &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;