Skip to content

Commit f1514ba

Browse files
authored
Llvm 7 build (#469)
* 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.
1 parent 8739fc6 commit f1514ba

File tree

3 files changed

+133
-29
lines changed

3 files changed

+133
-29
lines changed

include/remill/BC/Compat/CallSite.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2021 Trail of Bits, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include "remill/BC/Version.h"
19+
20+
#include <llvm/IR/Instruction.h>
21+
22+
23+
/* In llvm-11 llvm::CallSite got partially replace by llvm::AbstractCallSite
24+
* for read-only operations and llvm::CallBase was made public (was considered
25+
* implementation before)
26+
* This header tries to provide at least some compatibility for what is
27+
* currently used.
28+
*/
29+
30+
#if LLVM_VERSION_NUMBER < LLVM_VERSION(11, 0)
31+
32+
#include <llvm/IR/CallSite.h>
33+
namespace remill::compat::llvm {
34+
35+
struct CallSite : private ::llvm::CallSite {
36+
using parent = ::llvm::CallSite;
37+
38+
/* List of "allowed" methods (thanks to private inheritance)
39+
* that prevent user from accidentally using functionality that
40+
* would break other llvm version.
41+
* If you want to add method here, make sure other versions have it
42+
* as well.
43+
*/
44+
using parent::parent;
45+
using parent::isInvoke;
46+
using parent::isCall;
47+
using parent::operator bool;
48+
using parent::getCalledValue;
49+
using parent::getCalledFunction;
50+
using parent::setCalledFunction;
51+
};
52+
53+
} // namespace remill::compat::llvm
54+
55+
#else
56+
57+
#include <llvm/IR/AbstractCallSite.h>
58+
namespace remill::compat::llvm {
59+
60+
struct CallSite {
61+
::llvm::CallBase *cb;
62+
63+
CallSite(::llvm::Instruction *inst)
64+
: cb(::llvm::dyn_cast<::llvm::CallBase>(inst))
65+
{}
66+
67+
CallSite(::llvm::User *user)
68+
: CallSite(::llvm::dyn_cast<::llvm::Instruction>(user))
69+
{}
70+
71+
bool isInvoke() const {
72+
return ::llvm::isa<::llvm::InvokeInst>(cb);
73+
}
74+
75+
bool isCall() const {
76+
return ::llvm::isa<::llvm::CallInst>(cb);
77+
}
78+
79+
::llvm::Value *getCalledValue() {
80+
if (!static_cast<bool>(*this)) {
81+
return nullptr;
82+
}
83+
return cb->getCalledOperand();
84+
}
85+
86+
::llvm::Function *getCalledFunction() {
87+
if ( !*this) {
88+
return nullptr;
89+
}
90+
return cb->getCalledFunction();
91+
}
92+
93+
void setCalledFunction(::llvm::Function *fn) {
94+
return cb->setCalledFunction(fn);
95+
}
96+
97+
operator bool() const {
98+
return cb;
99+
}
100+
};
101+
102+
} // namespace remill::compat::llvm
103+
104+
#endif

lib/BC/DeadStoreEliminator.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <vector>
3737

3838
#include "remill/Arch/Arch.h"
39+
#include "remill/BC/Compat/CallSite.h"
3940
#include "remill/BC/Compat/VectorType.h"
4041
#include "remill/BC/ABI.h"
4142
#include "remill/BC/Util.h"
@@ -407,20 +408,20 @@ static void StreamCallOrInvokeToDOT(std::ostream &dot,
407408
dot << "%" << inst.getName().str() << " = ";
408409
}
409410

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

420-
if (called_val->getName().empty()) {
421-
dot << called_val->getValueID();
422-
} else {
423-
dot << called_val->getName().str();
420+
if(!cs.getCalledValue()->getName().empty()) {
421+
dot << cs.getCalledValue()->getName().str();
422+
} else {
423+
dot << cs.getCalledValue()->getValueID();
424+
}
424425
}
425426
}
426427

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

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

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

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

lib/BC/Util.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "remill/BC/ABI.h"
4949
#include "remill/BC/Annotate.h"
5050
#include "remill/BC/Compat/BitcodeReaderWriter.h"
51+
#include "remill/BC/Compat/CallSite.h"
5152
#include "remill/BC/Compat/DebugInfo.h"
5253
#include "remill/BC/Compat/GlobalValue.h"
5354
#include "remill/BC/Compat/IRReader.h"
@@ -694,13 +695,15 @@ void CloneBlockFunctionInto(llvm::Function *func) {
694695

695696
// Returns a list of callers of a specific function.
696697
std::vector<llvm::CallInst *> CallersOf(llvm::Function *func) {
698+
if (!func) {
699+
return {};
700+
}
701+
697702
std::vector<llvm::CallInst *> callers;
698-
if (func) {
699-
for (auto user : func->users()) {
700-
if (auto call_inst = llvm::dyn_cast<llvm::CallInst>(user)) {
701-
if (call_inst->getCalledOperand() == func) {
702-
callers.push_back(call_inst);
703-
}
703+
for (auto user : func->users()) {
704+
if (auto cs = compat::llvm::CallSite(user); cs.isCall()) {
705+
if (cs.getCalledFunction() == func) {
706+
callers.push_back(llvm::cast<llvm::CallInst>(user));
704707
}
705708
}
706709
}
@@ -1380,15 +1383,10 @@ void MoveFunctionIntoModule(llvm::Function *func, llvm::Module *dest_module) {
13801383
}
13811384
}
13821385

1383-
if (auto ci = llvm::dyn_cast<llvm::CallInst>(&inst); ci) {
1384-
if (auto callee = ci->getCalledFunction();
1385-
callee && callee->getParent() != dest_module) {
1386-
ci->setCalledOperand(DeclareFunctionInModule(callee, dest_module));
1387-
}
1388-
} else if (auto ii = llvm::dyn_cast<llvm::InvokeInst>(&inst); ii) {
1389-
if (auto callee = ii->getCalledFunction();
1386+
if (auto cs = compat::llvm::CallSite(&inst)) {
1387+
if (auto callee = cs.getCalledFunction();
13901388
callee && callee->getParent() != dest_module) {
1391-
ii->setCalledOperand(DeclareFunctionInModule(callee, dest_module));
1389+
cs.setCalledFunction(DeclareFunctionInModule(callee, dest_module));
13921390
}
13931391
}
13941392
}

0 commit comments

Comments
 (0)