From f56c2dd36a47ff3dae26c484c25f821d2d072cd4 Mon Sep 17 00:00:00 2001 From: Henrich Lauko Date: Thu, 5 May 2022 19:33:13 +0200 Subject: [PATCH] Fix LLVM 14 related warnings. (#591) * LLVM 14 compatibility (#589) * LLVM 13/14 compatibility * Fix CUDA support Co-authored-by: Artem Dinaburg * bc:util: Remove unused variable. * arch:x86: Fix bitwise to boolean operations. * arch: Add missing thumb2 serialization. * bc:util: Fix llvm-14 deprecated getElementType from PointerType. * treewide: Fix pointer element naming. * bc:compat: Return nullptr on access of element type of an opaque pointer. Co-authored-by: Eric Kilmer Co-authored-by: Artem Dinaburg --- include/remill/BC/Compat/PointerType.h | 35 ++++++++++++++++++++++++++ lib/Arch/Instruction.cpp | 1 + lib/Arch/SPARC32/Arch.cpp | 1 + lib/Arch/SPARC64/Arch.cpp | 1 + lib/Arch/X86/Semantics/X87.cpp | 4 +-- lib/BC/InstructionLifter.cpp | 1 - lib/BC/Util.cpp | 13 +++++----- 7 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 include/remill/BC/Compat/PointerType.h diff --git a/include/remill/BC/Compat/PointerType.h b/include/remill/BC/Compat/PointerType.h new file mode 100644 index 00000000..aa4ba834 --- /dev/null +++ b/include/remill/BC/Compat/PointerType.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Trail of Bits, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include "remill/BC/Version.h" + +namespace remill { + +inline static llvm::Type *PointerElementType(llvm::PointerType *type) { +#if LLVM_VERSION_NUMBER > LLVM_VERSION(13, 0) + if (type->isOpaque()) + return nullptr; +#endif + +#if LLVM_VERSION_NUMBER < LLVM_VERSION(14, 0) + return type->getElementType(); +#else + return type->getPointerElementType(); +#endif +} + +} // namespace remill diff --git a/lib/Arch/Instruction.cpp b/lib/Arch/Instruction.cpp index 12029fdb..144c6659 100644 --- a/lib/Arch/Instruction.cpp +++ b/lib/Arch/Instruction.cpp @@ -656,6 +656,7 @@ std::string Instruction::Serialize(void) const { case kArchX86: case kArchX86_AVX: case kArchX86_AVX512: ss << "X86"; break; + case kArchThumb2LittleEndian: ss << "Thumb2"; break; case kArchAArch32LittleEndian: ss << "AArch32"; break; case kArchAArch64LittleEndian: ss << "AArch64"; break; case kArchSparc32: ss << "SPARC32"; break; diff --git a/lib/Arch/SPARC32/Arch.cpp b/lib/Arch/SPARC32/Arch.cpp index ae2ebb3d..5176c672 100644 --- a/lib/Arch/SPARC32/Arch.cpp +++ b/lib/Arch/SPARC32/Arch.cpp @@ -21,6 +21,7 @@ #include "Decode.h" #include "remill/Arch/Instruction.h" #include "remill/Arch/Name.h" +#include "remill/BC/Compat/PointerType.h" #include "remill/BC/ABI.h" #include "remill/BC/Util.h" #include "remill/OS/OS.h" diff --git a/lib/Arch/SPARC64/Arch.cpp b/lib/Arch/SPARC64/Arch.cpp index fa2f453c..461445ed 100644 --- a/lib/Arch/SPARC64/Arch.cpp +++ b/lib/Arch/SPARC64/Arch.cpp @@ -22,6 +22,7 @@ #include "remill/Arch/Instruction.h" #include "remill/Arch/Name.h" #include "remill/BC/ABI.h" +#include "remill/BC/Compat/PointerType.h" #include "remill/BC/Util.h" #include "remill/OS/OS.h" diff --git a/lib/Arch/X86/Semantics/X87.cpp b/lib/Arch/X86/Semantics/X87.cpp index 0360b07e..ae611392 100644 --- a/lib/Arch/X86/Semantics/X87.cpp +++ b/lib/Arch/X86/Semantics/X87.cpp @@ -1370,7 +1370,7 @@ DEF_FPU_SEM(DoFYL2X) { auto st1 = Read(X87_ST1); state.sw.ze = IsZero(st0); state.sw.de = IsDenormal(st0) | IsDenormal(st1); - state.sw.ie = (IsSignalingNaN(st0) | IsSignalingNaN(st1)) || + state.sw.ie = (IsSignalingNaN(st0) || IsSignalingNaN(st1)) || (IsNegative(st0) && !IsInfinite(st0) && !state.sw.ze); auto res = FMul(st1, Log2(st0)); state.sw.pe = IsImprecise(res); @@ -1385,7 +1385,7 @@ DEF_FPU_SEM(DoFYL2XP1) { auto st1 = Read(X87_ST1); state.sw.ze = IsZero(st0); state.sw.de = IsDenormal(st0) | IsDenormal(st1); - state.sw.ie = IsSignalingNaN(st0) | IsSignalingNaN(st1); + state.sw.ie = IsSignalingNaN(st0) || IsSignalingNaN(st1); auto res = FMul(st1, Log2(FAdd(st0, Float80(1.0)))); state.sw.pe = IsImprecise(res); Write(X87_ST1, res); diff --git a/lib/BC/InstructionLifter.cpp b/lib/BC/InstructionLifter.cpp index 41f5f3cb..d49c5732 100644 --- a/lib/BC/InstructionLifter.cpp +++ b/lib/BC/InstructionLifter.cpp @@ -575,7 +575,6 @@ llvm::Value *InstructionLifter::LiftRegisterOperand(Instruction &inst, auto val_type = val->getType(); auto val_size = data_layout.getTypeAllocSizeInBits(val_type); auto arg_size = data_layout.getTypeAllocSizeInBits(arg_type); - const auto word_size = impl->arch->address_size; if (val_size < arg_size) { if (arg_type->isIntegerTy()) { diff --git a/lib/BC/Util.cpp b/lib/BC/Util.cpp index 5a16bd43..ed26926d 100644 --- a/lib/BC/Util.cpp +++ b/lib/BC/Util.cpp @@ -53,6 +53,7 @@ #include "remill/BC/Compat/DebugInfo.h" #include "remill/BC/Compat/GlobalValue.h" #include "remill/BC/Compat/IRReader.h" +#include "remill/BC/Compat/PointerType.h" #include "remill/BC/Compat/ToolOutputFile.h" #include "remill/BC/Compat/VectorType.h" #include "remill/BC/Compat/Verifier.h" @@ -944,7 +945,7 @@ RecontextualizeType(llvm::Type *type, llvm::LLVMContext &context, case llvm::Type::PointerTyID: { auto ptr_type = llvm::dyn_cast(type); - auto elem_type = ptr_type->getElementType(); + auto elem_type = PointerElementType(ptr_type); cached = llvm::PointerType::get(RecontextualizeType(elem_type, context, cache), ptr_type->getAddressSpace()); @@ -1476,11 +1477,11 @@ llvm::GlobalVariable *DeclareVarInModule(llvm::GlobalVariable *var, auto &dest_context = dest_module->getContext(); const auto type = ::remill::RecontextualizeType( - var->getType()->getElementType(), dest_context); + PointerElementType(var->getType()), dest_context); auto dest_var = dest_module->getGlobalVariable(var->getName()); if (dest_var) { - CHECK_EQ(type, dest_var->getType()->getElementType()); + CHECK_EQ(type, PointerElementType(dest_var->getType())); moved_var = dest_var; return dest_var; } @@ -1530,7 +1531,7 @@ llvm::GlobalAlias *DeclareAliasInModule(llvm::GlobalAlias *var, } } - const auto elem_type = dest_type->getElementType(); + const auto elem_type = PointerElementType(dest_type); const auto dest_var = llvm::GlobalAlias::create( elem_type, var->getType()->getAddressSpace(), var->getLinkage(), var->getName(), nullptr, dest_module); @@ -2446,8 +2447,8 @@ llvm::Value *BuildPointerToOffset(llvm::IRBuilder<> &ir, llvm::Value *ptr, } } - const auto dest_elem_type = dest_elem_ptr_type->getElementType(); - const auto ptr_elem_type = ptr_type->getElementType(); + const auto dest_elem_type = PointerElementType(dest_elem_ptr_type); + const auto ptr_elem_type = PointerElementType(ptr_type); const auto ptr_elem_size = dl.getTypeAllocSize(ptr_elem_type); const auto base_index = dest_elem_offset / ptr_elem_size;