From 40a4dd01cf83a2d86b07f7825bbe365b29d61550 Mon Sep 17 00:00:00 2001 From: Aleksandr Misonizhnik Date: Wed, 14 Aug 2024 20:06:56 +0200 Subject: [PATCH] fix: --- build.sh | 2 +- include/klee/Core/MockBuilder.h | 8 ++- lib/Core/CXXTypeSystem/CXXTypeManager.cpp | 13 ++++- lib/Core/Executor.cpp | 12 +--- lib/Core/MockBuilder.cpp | 68 ++++++++++++++++++----- lib/Core/TypeManager.cpp | 10 ++++ lib/Expr/Expr.cpp | 1 + lib/Module/CallSplitter.cpp | 6 ++ lib/Module/FunctionAlias.cpp | 1 + lib/Module/ReturnSplitter.cpp | 6 ++ tools/klee/CMakeLists.txt | 2 +- unittests/Ref/RefTest.cpp | 3 - 12 files changed, 99 insertions(+), 33 deletions(-) diff --git a/build.sh b/build.sh index e716cd7b63..5e03c320e0 100755 --- a/build.sh +++ b/build.sh @@ -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" diff --git a/include/klee/Core/MockBuilder.h b/include/klee/Core/MockBuilder.h index da5d0ea198..34fc81502d 100644 --- a/include/klee/Core/MockBuilder.h +++ b/include/klee/Core/MockBuilder.h @@ -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 #include @@ -58,7 +60,7 @@ class MockBuilder { llvm::Function *func, const std::set &properties); std::map getExternalFunctions(); - std::map getExternalGlobals(); + std::map getExternalGlobals(); std::pair goByOffset(llvm::Value *value, const std::vector &offset); @@ -74,10 +76,10 @@ class MockBuilder { std::set &mainModuleGlobals); std::unique_ptr 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); }; diff --git a/lib/Core/CXXTypeSystem/CXXTypeManager.cpp b/lib/Core/CXXTypeSystem/CXXTypeManager.cpp index af2ad81c4d..b41c89a857 100644 --- a/lib/Core/CXXTypeSystem/CXXTypeManager.cpp +++ b/lib/Core/CXXTypeSystem/CXXTypeManager.cpp @@ -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" @@ -231,9 +232,15 @@ ref cxxtypes::KCXXType::getContentRestrictions(ref object) const { if (type == nullptr) { return nullptr; } +#if LLVM_VERSION_CODE >= LLVM_VERSION(15, 0) + llvm::Type *elementType = type->getNonOpaquePointerElementType(); + return llvm::cast(parent->getWrappedType(elementType)) + ->getPointersRestrictions(object); +#else llvm::Type *elementType = type->getPointerElementType(); return llvm::cast(parent->getWrappedType(elementType)) ->getPointersRestrictions(object); +#endif } ref cxxtypes::KCXXType::getPointersRestrictions(ref) const { @@ -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( + parent->getWrappedType(type->getNonOpaquePointerElementType())); +#else elementType = cast(parent->getWrappedType(type->getPointerElementType())); +#endif } bool cxxtypes::KCXXPointerType::isAccessableFrom( diff --git a/lib/Core/Executor.cpp b/lib/Core/Executor.cpp index 45008ab6e3..8d6d5032e2 100644 --- a/lib/Core/Executor.cpp +++ b/lib/Core/Executor.cpp @@ -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" @@ -3610,16 +3612,6 @@ void Executor::executeInstruction(ExecutionState &state, KInstruction *ki) { result = FPToX87FP80Ext(result); } - if (castToType->isPointerTy()) { - castToType = castToType->getPointerElementType(); - if (ref pointer = cast(makePointer(result))) { - ref base = pointer->getBase(); - if (state.isGEPExpr(base)) { - state.gepExprBases[base] = castToType; - } - } - } - bindLocal(ki, state, result); break; } diff --git a/lib/Core/MockBuilder.cpp b/lib/Core/MockBuilder.cpp index 5c82feee13..eb46968036 100644 --- a/lib/Core/MockBuilder.cpp +++ b/lib/Core/MockBuilder.cpp @@ -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 @@ -69,12 +72,13 @@ MockBuilder::getExternalFunctions() { return externals; } -std::map MockBuilder::getExternalGlobals() { - std::map externals; +std::map +MockBuilder::getExternalGlobals() { + std::map 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); @@ -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( mockModule->getOrInsertGlobal(extName, elementType)); @@ -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; @@ -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 { @@ -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; } @@ -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) { @@ -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(elemType)), - prev); + builder->CreateStore(llvm::ConstantPointerNull::get( + llvm::cast(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(elem) || isa(elem)) { + valueType = llvm::getLoadStoreType(elem); + } else if (auto func = dyn_cast(elem)) { + valueType = func->getFunctionType(); + } +#else + auto valueType = elem->getType()->getPointerElementType(); +#endif auto sizeValue = llvm::ConstantInt::get( ctx, llvm::APInt(64, mockModule->getDataLayout().getTypeStoreSize(valueType), @@ -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, diff --git a/lib/Core/TypeManager.cpp b/lib/Core/TypeManager.cpp index 490ffc3e32..ae7205d9e8 100644 --- a/lib/Core/TypeManager.cpp +++ b/lib/Core/TypeManager.cpp @@ -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()); } @@ -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); } } diff --git a/lib/Expr/Expr.cpp b/lib/Expr/Expr.cpp index e6e4a03ea7..7b0c4d0a4d 100644 --- a/lib/Expr/Expr.cpp +++ b/lib/Expr/Expr.cpp @@ -30,6 +30,7 @@ #include #include +#include #include using namespace klee; diff --git a/lib/Module/CallSplitter.cpp b/lib/Module/CallSplitter.cpp index 0a33411e5d..c1c05ce1fa 100644 --- a/lib/Module/CallSplitter.cpp +++ b/lib/Module/CallSplitter.cpp @@ -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" @@ -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; diff --git a/lib/Module/FunctionAlias.cpp b/lib/Module/FunctionAlias.cpp index f91fcf3dc0..f8fe004de0 100644 --- a/lib/Module/FunctionAlias.cpp +++ b/lib/Module/FunctionAlias.cpp @@ -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" diff --git a/lib/Module/ReturnSplitter.cpp b/lib/Module/ReturnSplitter.cpp index 21b89fd566..92484cf398 100644 --- a/lib/Module/ReturnSplitter.cpp +++ b/lib/Module/ReturnSplitter.cpp @@ -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" @@ -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; diff --git a/tools/klee/CMakeLists.txt b/tools/klee/CMakeLists.txt index 5d63f93901..d54ee27372 100644 --- a/tools/klee/CMakeLists.txt +++ b/tools/klee/CMakeLists.txt @@ -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}) diff --git a/unittests/Ref/RefTest.cpp b/unittests/Ref/RefTest.cpp index 64c3956606..c9ca495f71 100644 --- a/unittests/Ref/RefTest.cpp +++ b/unittests/Ref/RefTest.cpp @@ -94,11 +94,8 @@ TEST(RefTest, SelfMove) { struct Expr *r_e = new Expr(); ref 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);