Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
  • Loading branch information
misonijnik committed Aug 15, 2024
1 parent 585efec commit 40a4dd0
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ if [ "$1" = "--debug" ] || [ "$1" = "-g" ]; then
ENABLE_OPTIMIZED=0
ENABLE_DEBUG=1
KLEE_RUNTIME_BUILD="Debug+Asserts"
ENABLE_WARNINGS_AS_ERRORS=0
ENABLE_WARNINGS_AS_ERRORS=1
shift 1
else
KEEP_PARSE="false"
Expand Down
8 changes: 5 additions & 3 deletions include/klee/Core/MockBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
#include "klee/Core/Interpreter.h"
#include "klee/Module/Annotation.h"

#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"

#include <set>
#include <string>
Expand Down Expand Up @@ -58,7 +60,7 @@ class MockBuilder {
llvm::Function *func, const std::set<Statement::Property> &properties);

std::map<std::string, llvm::FunctionType *> getExternalFunctions();
std::map<std::string, llvm::Type *> getExternalGlobals();
std::map<std::string, const llvm::GlobalVariable *> getExternalGlobals();

std::pair<llvm::Value *, llvm::Value *>
goByOffset(llvm::Value *value, const std::vector<std::string> &offset);
Expand All @@ -74,10 +76,10 @@ class MockBuilder {
std::set<std::string> &mainModuleGlobals);

std::unique_ptr<llvm::Module> build();
void buildAllocSource(llvm::Value *prev, llvm::Type *elemType,
void buildAllocSource(llvm::Value *prev, llvm::Value *elem,
const Statement::Alloc *allocSourcePtr);
void buildFree(llvm::Value *elem, const Statement::Free *freePtr);
void processingValue(llvm::Value *prev, llvm::Type *elemType,
void processingValue(llvm::Value *prev, llvm::Value *elem,
const Statement::Alloc *allocSourcePtr,
bool initNullPtr);
};
Expand Down
13 changes: 12 additions & 1 deletion lib/Core/CXXTypeSystem/CXXTypeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "klee/Module/KModule.h"
#include "klee/Module/KType.h"

#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Metadata.h"
Expand Down Expand Up @@ -231,9 +232,15 @@ ref<Expr> cxxtypes::KCXXType::getContentRestrictions(ref<Expr> object) const {
if (type == nullptr) {
return nullptr;
}
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
llvm::Type *elementType = type->getNonOpaquePointerElementType();
return llvm::cast<cxxtypes::KCXXType>(parent->getWrappedType(elementType))
->getPointersRestrictions(object);
#else
llvm::Type *elementType = type->getPointerElementType();
return llvm::cast<cxxtypes::KCXXType>(parent->getWrappedType(elementType))
->getPointersRestrictions(object);
#endif
}

ref<Expr> cxxtypes::KCXXType::getPointersRestrictions(ref<Expr>) const {
Expand Down Expand Up @@ -641,9 +648,13 @@ cxxtypes::KCXXPointerType::KCXXPointerType(llvm::Type *type,
TypeManager *parent)
: KCXXType(type, parent) {
typeKind = CXXTypeKind::POINTER;

#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
elementType = cast<KCXXType>(
parent->getWrappedType(type->getNonOpaquePointerElementType()));
#else
elementType =
cast<KCXXType>(parent->getWrappedType(type->getPointerElementType()));
#endif
}

bool cxxtypes::KCXXPointerType::isAccessableFrom(
Expand Down
12 changes: 2 additions & 10 deletions lib/Core/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "DistanceCalculator.h"
#include "ExecutionState.h"
#include "ExternalDispatcher.h"
#if LLVM_VERSION_CODE <= LLVM_VERSION(14, 0)
#include "GetElementPtrTypeIterator.h"
#endif
#include "ImpliedValue.h"
#include "Memory.h"
#include "MemoryManager.h"
Expand Down Expand Up @@ -3610,16 +3612,6 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) {
result = FPToX87FP80Ext(result);
}

if (castToType->isPointerTy()) {
castToType = castToType->getPointerElementType();
if (ref<PointerExpr> pointer = cast<PointerExpr>(makePointer(result))) {
ref<Expr> base = pointer->getBase();
if (state.isGEPExpr(base)) {
state.gepExprBases[base] = castToType;
}
}
}

bindLocal(ki, state, result);
break;
}
Expand Down
68 changes: 54 additions & 14 deletions lib/Core/MockBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#include "klee/Support/ErrorHandling.h"
#include "klee/Support/ModuleUtil.h"

#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/raw_ostream.h"

#include <memory>
Expand Down Expand Up @@ -69,12 +72,13 @@ MockBuilder::getExternalFunctions() {
return externals;
}

std::map<std::string, llvm::Type *> MockBuilder::getExternalGlobals() {
std::map<std::string, llvm::Type *> externals;
std::map<std::string, const llvm::GlobalVariable *>
MockBuilder::getExternalGlobals() {
std::map<std::string, const llvm::GlobalVariable *> externals;
for (const auto &global : userModule->globals()) {
if (global.isDeclaration() &&
!ignoredExternals.count(global.getName().str())) {
externals.insert(std::make_pair(global.getName(), global.getType()));
externals.insert(std::make_pair(global.getName(), &global));
}
}
removeAliases(userModule, externals);
Expand Down Expand Up @@ -202,7 +206,7 @@ void MockBuilder::buildMockMain() {
void MockBuilder::buildExternalGlobalsDefinitions() {
auto externalGlobals = getExternalGlobals();
for (const auto &[extName, type] : externalGlobals) {
auto elementType = type->getPointerElementType();
auto elementType = type->getValueType();
klee_message("Mocking external variable %s", extName.c_str());
llvm::GlobalVariable *global = dyn_cast_or_null<llvm::GlobalVariable>(
mockModule->getOrInsertGlobal(extName, elementType));
Expand Down Expand Up @@ -304,8 +308,12 @@ MockBuilder::goByOffset(llvm::Value *value,
klee_error("Incorrect annotation offset.");
}
prev = current;
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
current = builder->CreateLoad(current->getType(), current);
#else
current = builder->CreateLoad(current->getType()->getPointerElementType(),
current);
#endif
} else if (inst == "&") {
auto addr = builder->CreateAlloca(current->getType());
prev = current;
Expand All @@ -329,10 +337,17 @@ inline llvm::Type *getTypeByOffset(llvm::Type *value,
llvm::Type *current = value;
for (const auto &inst : offset) {
if (inst == "*") {
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
if (!current->isPointerTy() || current->isOpaquePointerTy()) {
return nullptr;
}
current = current->getNonOpaquePointerElementType();
#else
if (!current->isPointerTy()) {
return nullptr;
}
current = current->getPointerElementType();
#endif
} else if (inst == "&") {
// Not change
} else {
Expand Down Expand Up @@ -460,10 +475,18 @@ void MockBuilder::buildAnnotationForExternalFunctionArgs(
builder->CreateCondBr(brValue, derefBB, contBB);

builder->SetInsertPoint(derefBB);
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
builder->CreateLoad(elem->getType(), elem);
#else
builder->CreateLoad(elem->getType()->getPointerElementType(), elem);
#endif
builder->CreateBr(contBB);

#if LLVM_VERSION_CODE >= LLVM_VERSION(16, 0)
curFunc->insert(curFunc->end(), contBB);
#else
curFunc->getBasicBlockList().push_back(contBB);
#endif
builder->SetInsertPoint(contBB);
break;
}
Expand Down Expand Up @@ -504,12 +527,12 @@ void MockBuilder::buildAnnotationForExternalFunctionArgs(
if (freePtr) {
buildFree(elem, freePtr);
}
processingValue(prev, elem->getType(), allocSourcePtr, initNullPtr);
processingValue(prev, elem, allocSourcePtr, initNullPtr);
}
}
}

void MockBuilder::processingValue(llvm::Value *prev, llvm::Type *elemType,
void MockBuilder::processingValue(llvm::Value *prev, llvm::Value *elem,
const Statement::Alloc *allocSourcePtr,
bool initNullPtr) {
if (initNullPtr) {
Expand All @@ -529,32 +552,49 @@ void MockBuilder::processingValue(llvm::Value *prev, llvm::Type *elemType,
llvm::BasicBlock::Create(ctx, "allocArg", curFunc);
builder->CreateCondBr(brValue, allocBB, initNullBB);
builder->SetInsertPoint(allocBB);
buildAllocSource(prev, elemType, allocSourcePtr);
buildAllocSource(prev, elem, allocSourcePtr);
builder->CreateBr(contBB);
} else {
builder->CreateCondBr(brValue, initNullBB, contBB);
}
#if LLVM_VERSION_CODE >= LLVM_VERSION(16, 0)
curFunc->insert(curFunc->end(), initNullBB);
#else
curFunc->getBasicBlockList().push_back(initNullBB);
#endif
builder->SetInsertPoint(initNullBB);
builder->CreateStore(
llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(elemType)),
prev);
builder->CreateStore(llvm::ConstantPointerNull::get(
llvm::cast<llvm::PointerType>(elem->getType())),
prev);
builder->CreateBr(contBB);

#if LLVM_VERSION_CODE >= LLVM_VERSION(16, 0)
curFunc->insert(curFunc->end(), contBB);
#else
curFunc->getBasicBlockList().push_back(contBB);
#endif
builder->SetInsertPoint(contBB);
} else if (allocSourcePtr) {
buildAllocSource(prev, elemType, allocSourcePtr);
buildAllocSource(prev, elem, allocSourcePtr);
}
}

void MockBuilder::buildAllocSource(llvm::Value *prev, llvm::Type *elemType,
void MockBuilder::buildAllocSource(llvm::Value *prev, llvm::Value *elem,
const Statement::Alloc *allocSourcePtr) {
if (allocSourcePtr->value != Statement::Alloc::ALLOC) {
klee_warning("Annotation: AllocSource \"%d\" not implemented use alloc",
allocSourcePtr->value);
}
auto valueType = elemType->getPointerElementType();
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
auto valueType = elem->getType();
if (isa<llvm::LoadInst>(elem) || isa<llvm::StoreInst>(elem)) {
valueType = llvm::getLoadStoreType(elem);
} else if (auto func = dyn_cast<llvm::Function>(elem)) {
valueType = func->getFunctionType();
}
#else
auto valueType = elem->getType()->getPointerElementType();
#endif
auto sizeValue = llvm::ConstantInt::get(
ctx,
llvm::APInt(64, mockModule->getDataLayout().getTypeStoreSize(valueType),
Expand Down Expand Up @@ -627,7 +667,7 @@ void MockBuilder::buildAnnotationForExternalFunctionReturn(
llvm::Value *retValuePtr = builder->CreateAlloca(returnType, nullptr);

if (returnType->isPointerTy() && (allocSourcePtr || mustInitNull)) {
processingValue(retValuePtr, returnType, allocSourcePtr,
processingValue(retValuePtr, func, allocSourcePtr,
mustInitNull || maybeInitNull);
} else {
buildCallKleeMakeSymbolic("klee_make_mock", retValuePtr, returnType,
Expand Down
10 changes: 10 additions & 0 deletions lib/Core/TypeManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ KType *TypeManager::getWrappedType(llvm::Type *type) {
if (typesMap.count(type) == 0) {
types.emplace_back(new KType(type, this));
typesMap.emplace(type, types.back().get());
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
if (type && type->isPointerTy() && !type->isOpaquePointerTy()) {
getWrappedType(type->getNonOpaquePointerElementType());
}
#else
if (type && type->isPointerTy()) {
getWrappedType(type->getPointerElementType());
}
#endif
if (type && type->isArrayTy()) {
getWrappedType(type->getArrayElementType());
}
Expand Down Expand Up @@ -167,7 +173,11 @@ void TypeManager::initTypeInfo() {
for (auto &type : types) {
llvm::Type *rawType = type->getRawType();
if (rawType && rawType->isSized()) {
#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0)
type->alignment = parent->targetData->getABITypeAlign(rawType).value();
#else
type->alignment = parent->targetData->getABITypeAlignment(rawType);
#endif
type->typeStoreSize = parent->targetData->getTypeStoreSize(rawType);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/Expr/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <cassert>
#include <cfenv>
#include <cmath>
#include <vector>

using namespace klee;
Expand Down
6 changes: 6 additions & 0 deletions lib/Module/CallSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "Passes.h"

#include "klee/Config/Version.h"

#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Expand All @@ -20,7 +22,11 @@ namespace klee {
char CallSplitter::ID = 0;

bool CallSplitter::runOnFunction(Function &F) {
#if LLVM_VERSION_CODE >= LLVM_VERSION(16, 0)
unsigned n = F.size();
#else
unsigned n = F.getBasicBlockList().size();
#endif
BasicBlock **blocks = new BasicBlock *[n];
unsigned i = 0;
for (llvm::Function::iterator bbit = F.begin(), bbie = F.end(); bbit != bbie;
Expand Down
1 change: 1 addition & 0 deletions lib/Module/FunctionAlias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "klee/Support/ErrorHandling.h"
#include "klee/Support/OptionCategories.h"

#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Regex.h"
Expand Down
6 changes: 6 additions & 0 deletions lib/Module/ReturnSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "Passes.h"

#include "klee/Config/Version.h"

#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Expand All @@ -21,7 +23,11 @@ namespace klee {
char ReturnSplitter::ID = 0;

bool ReturnSplitter::runOnFunction(Function &F) {
#if LLVM_VERSION_CODE >= LLVM_VERSION(16, 0)
unsigned n = F.size();
#else
unsigned n = F.getBasicBlockList().size();
#endif
BasicBlock **blocks = new BasicBlock *[n];
unsigned i = 0;
for (llvm::Function::iterator bbit = F.begin(), bbie = F.end(); bbit != bbie;
Expand Down
2 changes: 1 addition & 1 deletion tools/klee/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(KLEE_LIBS
)

target_link_libraries(klee ${KLEE_LIBS})
target_include_directories(klee PRIVATE ${KLEE_INCLUDE_DIRS} ${LLVM_INCLUDE_DIRS})
target_include_directories(klee SYSTEM PRIVATE ${KLEE_INCLUDE_DIRS} ${LLVM_INCLUDE_DIRS})
target_compile_options(klee PRIVATE ${KLEE_COMPONENT_CXX_FLAGS})
target_compile_definitions(klee PRIVATE ${KLEE_COMPONENT_CXX_DEFINES})

Expand Down
3 changes: 0 additions & 3 deletions unittests/Ref/RefTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,8 @@ TEST(RefTest, SelfMove) {
struct Expr *r_e = new Expr();
ref<Expr> r(r_e);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wself-move"
// Check self move
r = std::move(r);
#pragma GCC diagnostic pop
finished = 1;
}
EXPECT_EQ(1, finished_counter);
Expand Down

0 comments on commit 40a4dd0

Please sign in to comment.