Skip to content

Commit

Permalink
Llvm 7 build (#469)
Browse files Browse the repository at this point in the history
* bc: Use llvm::CallSite methods instead of raw llvm instructions.

* bc: Use llvm::CallSite in utils.

* bc: Add compatibility file for CallSite.

* bc: Reflect addition of compat for CallSite.
  • Loading branch information
Aiethel authored Jan 14, 2021
1 parent 8739fc6 commit f1514ba
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 29 deletions.
104 changes: 104 additions & 0 deletions include/remill/BC/Compat/CallSite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2021 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"

#include <llvm/IR/Instruction.h>


/* In llvm-11 llvm::CallSite got partially replace by llvm::AbstractCallSite
* for read-only operations and llvm::CallBase was made public (was considered
* implementation before)
* This header tries to provide at least some compatibility for what is
* currently used.
*/

#if LLVM_VERSION_NUMBER < LLVM_VERSION(11, 0)

#include <llvm/IR/CallSite.h>
namespace remill::compat::llvm {

struct CallSite : private ::llvm::CallSite {
using parent = ::llvm::CallSite;

/* List of "allowed" methods (thanks to private inheritance)
* that prevent user from accidentally using functionality that
* would break other llvm version.
* If you want to add method here, make sure other versions have it
* as well.
*/
using parent::parent;
using parent::isInvoke;
using parent::isCall;
using parent::operator bool;
using parent::getCalledValue;
using parent::getCalledFunction;
using parent::setCalledFunction;
};

} // namespace remill::compat::llvm

#else

#include <llvm/IR/AbstractCallSite.h>
namespace remill::compat::llvm {

struct CallSite {
::llvm::CallBase *cb;

CallSite(::llvm::Instruction *inst)
: cb(::llvm::dyn_cast<::llvm::CallBase>(inst))
{}

CallSite(::llvm::User *user)
: CallSite(::llvm::dyn_cast<::llvm::Instruction>(user))
{}

bool isInvoke() const {
return ::llvm::isa<::llvm::InvokeInst>(cb);
}

bool isCall() const {
return ::llvm::isa<::llvm::CallInst>(cb);
}

::llvm::Value *getCalledValue() {
if (!static_cast<bool>(*this)) {
return nullptr;
}
return cb->getCalledOperand();
}

::llvm::Function *getCalledFunction() {
if ( !*this) {
return nullptr;
}
return cb->getCalledFunction();
}

void setCalledFunction(::llvm::Function *fn) {
return cb->setCalledFunction(fn);
}

operator bool() const {
return cb;
}
};

} // namespace remill::compat::llvm

#endif
32 changes: 17 additions & 15 deletions lib/BC/DeadStoreEliminator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <vector>

#include "remill/Arch/Arch.h"
#include "remill/BC/Compat/CallSite.h"
#include "remill/BC/Compat/VectorType.h"
#include "remill/BC/ABI.h"
#include "remill/BC/Util.h"
Expand Down Expand Up @@ -407,20 +408,20 @@ static void StreamCallOrInvokeToDOT(std::ostream &dot,
dot << "%" << inst.getName().str() << " = ";
}

llvm::Value *called_val = nullptr;
if (auto call_inst = llvm::dyn_cast<llvm::CallInst>(&inst)) {
dot << "call ";
called_val = call_inst->getCalledOperand();
} else {
dot << "invoke ";
auto invoke_inst = llvm::dyn_cast<llvm::InvokeInst>(&inst);
called_val = invoke_inst->getCalledOperand();
}
if (auto cs = compat::llvm::CallSite(&inst)) {
if (cs.isInvoke()) {
dot << "invoke ";
} else if (cs.isCall()) {
dot << "call";
} else {
LOG(ERROR) << "Encountered callsite that is not call nor invoke!";
}

if (called_val->getName().empty()) {
dot << called_val->getValueID();
} else {
dot << called_val->getName().str();
if(!cs.getCalledValue()->getName().empty()) {
dot << cs.getCalledValue()->getName().str();
} else {
dot << cs.getCalledValue()->getValueID();
}
}
}

Expand Down Expand Up @@ -1135,7 +1136,8 @@ VisitResult ForwardAliasVisitor::visitPHINode(llvm::PHINode &inst) {
}

VisitResult ForwardAliasVisitor::visitCallInst(llvm::CallInst &inst) {
const auto val = inst.getCalledOperand()->stripPointerCasts();
//const auto val = inst.getCalledOperand()->stripPointerCasts();
const auto val = compat::llvm::CallSite(&inst).getCalledValue()->stripPointerCasts();
if (auto const_val = llvm::dyn_cast<llvm::Constant>(val); const_val) {

// Don't let this affect anything.
Expand Down Expand Up @@ -1187,7 +1189,7 @@ VisitResult ForwardAliasVisitor::visitCallInst(llvm::CallInst &inst) {
}

VisitResult ForwardAliasVisitor::visitInvokeInst(llvm::InvokeInst &inst) {
auto val = inst.getCalledOperand()->stripPointerCasts();
auto val = compat::llvm::CallSite(&inst).getCalledValue()->stripPointerCasts();
if (llvm::isa<llvm::InlineAsm>(val)) {
live_args[&inst].set(); // Weird to invoke inline assembly.

Expand Down
26 changes: 12 additions & 14 deletions lib/BC/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "remill/BC/ABI.h"
#include "remill/BC/Annotate.h"
#include "remill/BC/Compat/BitcodeReaderWriter.h"
#include "remill/BC/Compat/CallSite.h"
#include "remill/BC/Compat/DebugInfo.h"
#include "remill/BC/Compat/GlobalValue.h"
#include "remill/BC/Compat/IRReader.h"
Expand Down Expand Up @@ -694,13 +695,15 @@ void CloneBlockFunctionInto(llvm::Function *func) {

// Returns a list of callers of a specific function.
std::vector<llvm::CallInst *> CallersOf(llvm::Function *func) {
if (!func) {
return {};
}

std::vector<llvm::CallInst *> callers;
if (func) {
for (auto user : func->users()) {
if (auto call_inst = llvm::dyn_cast<llvm::CallInst>(user)) {
if (call_inst->getCalledOperand() == func) {
callers.push_back(call_inst);
}
for (auto user : func->users()) {
if (auto cs = compat::llvm::CallSite(user); cs.isCall()) {
if (cs.getCalledFunction() == func) {
callers.push_back(llvm::cast<llvm::CallInst>(user));
}
}
}
Expand Down Expand Up @@ -1380,15 +1383,10 @@ void MoveFunctionIntoModule(llvm::Function *func, llvm::Module *dest_module) {
}
}

if (auto ci = llvm::dyn_cast<llvm::CallInst>(&inst); ci) {
if (auto callee = ci->getCalledFunction();
callee && callee->getParent() != dest_module) {
ci->setCalledOperand(DeclareFunctionInModule(callee, dest_module));
}
} else if (auto ii = llvm::dyn_cast<llvm::InvokeInst>(&inst); ii) {
if (auto callee = ii->getCalledFunction();
if (auto cs = compat::llvm::CallSite(&inst)) {
if (auto callee = cs.getCalledFunction();
callee && callee->getParent() != dest_module) {
ii->setCalledOperand(DeclareFunctionInModule(callee, dest_module));
cs.setCalledFunction(DeclareFunctionInModule(callee, dest_module));
}
}
}
Expand Down

0 comments on commit f1514ba

Please sign in to comment.