Skip to content

Commit 6daea19

Browse files
committed
Fix BasicBlockContext lookups
Fixes the missing literal_struct_2 issue
1 parent d27b985 commit 6daea19

File tree

14 files changed

+60
-24
lines changed

14 files changed

+60
-24
lines changed

include/anvill/ABI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ extern const std::string kAnvillStackZero;
8686
// use this to queue off of then just move it after the split
8787
extern const std::string kStackMetadata;
8888

89-
extern const std::string kBasicBlockMetadata;
89+
extern const std::string kBasicBlockAddrMetadata;
90+
extern const std::string kBasicBlockUidMetadata;
9091

9192

9293
/// Intrinsic that acts like a return instruction but leaves both the basic block and the parent function.

include/anvill/Passes/BasicBlockPass.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace anvill {
1515
class BasicBlockContexts {
1616
public:
1717
virtual std::optional<std::reference_wrapper<const BasicBlockContext>>
18-
GetBasicBlockContextForAddr(uint64_t addr) const = 0;
18+
GetBasicBlockContextForUid(uint64_t uid) const = 0;
1919
virtual const FunctionDecl &GetFunctionAtAddress(uint64_t addr) const = 0;
2020
};
2121

@@ -33,9 +33,9 @@ class BasicBlockPass : public llvm::PassInfoMixin<BasicBlockPass<T>> {
3333
llvm::PreservedAnalyses run(llvm::Function &F,
3434
llvm::FunctionAnalysisManager &AM) {
3535
auto &bb_pass = *static_cast<T *>(this);
36-
auto bbaddr = anvill::GetBasicBlockAddr(&F);
37-
if (bbaddr.has_value()) {
38-
auto maybe_bb_cont = contexts.GetBasicBlockContextForAddr(*bbaddr);
36+
auto bbuid = anvill::GetBasicBlockUid(&F);
37+
if (bbuid.has_value()) {
38+
auto maybe_bb_cont = contexts.GetBasicBlockContextForUid(*bbuid);
3939
if (maybe_bb_cont) {
4040
const BasicBlockContext &bb_cont = *maybe_bb_cont;
4141
auto &parent_func =

include/anvill/Specification.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class SpecBlockContexts : public BasicBlockContexts {
9898
SpecBlockContexts(const Specification &spec);
9999

100100
virtual std::optional<std::reference_wrapper<const BasicBlockContext>>
101-
GetBasicBlockContextForAddr(uint64_t addr) const override;
101+
GetBasicBlockContextForUid(uint64_t uid) const override;
102102

103103
virtual const FunctionDecl &
104104
GetFunctionAtAddress(uint64_t addr) const override;

include/anvill/Utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ llvm::Value *StoreNativeValue(llvm::Value *native_val, const ValueDecl &decl,
135135
llvm::Value *state_ptr, llvm::Value *mem_ptr);
136136

137137
std::optional<uint64_t> GetBasicBlockAddr(llvm::Function *func);
138+
std::optional<uint64_t> GetBasicBlockUid(llvm::Function *func);
138139

139140
llvm::Argument *GetBasicBlockStackPtr(llvm::Function *func);
140141

lib/ABI.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ const std::string kAnvillDataProvenanceFunc(kAnvillNamePrefix +
8282
// `alloca`.
8383
const std::string kAnvillStackZero(kAnvillNamePrefix + "stack_zero");
8484

85-
const std::string kBasicBlockMetadata(kAnvillNamePrefix + "basic_block_md");
85+
const std::string kBasicBlockAddrMetadata(kAnvillNamePrefix + "basic_block_addr_md");
86+
const std::string kBasicBlockUidMetadata(kAnvillNamePrefix + "basic_block_uid_md");
8687

8788
const std::string kStackMetadata(kAnvillNamePrefix + "stack_alloc");
8889

lib/Declarations.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ VariableDecl::DeclareInModule(const std::string &name,
7878

7979
void FunctionDecl::AddBBContexts(
8080
std::unordered_map<uint64_t, SpecBlockContext> &contexts) const {
81-
for (const auto &[addr, _] : this->cfg) {
82-
contexts.insert({addr, this->GetBlockContext(addr)});
81+
for (const auto &[uid, _] : this->cfg) {
82+
contexts.insert({uid, this->GetBlockContext(uid)});
8383
}
8484
}
8585

@@ -475,12 +475,12 @@ void CallableDecl::OverrideFunctionTypeWithABIReturnLayout() {
475475

476476
namespace {
477477
template <class V>
478-
V GetWithDef(uint64_t addr, const std::unordered_map<uint64_t, V> &map, V def) {
479-
if (map.find(addr) == map.end()) {
478+
V GetWithDef(uint64_t uid, const std::unordered_map<uint64_t, V> &map, V def) {
479+
if (map.find(uid) == map.end()) {
480480
return def;
481481
}
482482

483-
return map.find(addr)->second;
483+
return map.find(uid)->second;
484484
}
485485
} // namespace
486486

lib/Lifters/BasicBlockLifter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,12 @@ void BasicBlockLifter::LiftInstructionsIntoLiftedFunction() {
402402
}
403403

404404

405-
llvm::MDNode *BasicBlockLifter::GetBasicBlockAnnotation(uint64_t addr) const {
405+
llvm::MDNode *BasicBlockLifter::GetBasicBlockAddrAnnotation(uint64_t addr) const {
406406
return this->GetAddrAnnotation(addr, this->semantics_module->getContext());
407407
}
408+
llvm::MDNode *BasicBlockLifter::GetBasicBlockUidAnnotation(uint64_t uid) const {
409+
return this->GetUidAnnotation(uid, this->semantics_module->getContext());
410+
}
408411

409412
llvm::Function *BasicBlockLifter::DeclareBasicBlockFunction() {
410413
std::string name_ = "func" + std::to_string(decl.address) + "basic_block" +
@@ -437,8 +440,10 @@ llvm::Function *BasicBlockLifter::DeclareBasicBlockFunction() {
437440

438441
BasicBlockFunction BasicBlockLifter::CreateBasicBlockFunction() {
439442
auto func = bb_func;
440-
func->setMetadata(anvill::kBasicBlockMetadata,
441-
GetBasicBlockAnnotation(this->block_def.addr));
443+
func->setMetadata(anvill::kBasicBlockAddrMetadata,
444+
GetBasicBlockAddrAnnotation(this->block_def.addr));
445+
func->setMetadata(anvill::kBasicBlockUidMetadata,
446+
GetBasicBlockUidAnnotation(this->block_def.uid));
442447

443448
auto &context = this->semantics_module->getContext();
444449
llvm::FunctionType *lifted_func_type =

lib/Lifters/BasicBlockLifter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class BasicBlockLifter : public CodeLifter {
107107
remill::DecodingContext context);
108108

109109

110-
llvm::MDNode *GetBasicBlockAnnotation(uint64_t addr) const;
110+
llvm::MDNode *GetBasicBlockAddrAnnotation(uint64_t addr) const;
111+
llvm::MDNode *GetBasicBlockUidAnnotation(uint64_t uid) const;
111112

112113
public:
113114
BasicBlockLifter(std::unique_ptr<BasicBlockContext> block_context,

lib/Lifters/CodeLifter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ CodeLifter::CodeLifter(const LifterOptions &options,
5757
type_specifier(type_specifier),
5858
address_type(
5959
llvm::Type::getIntNTy(llvm_context, options.arch->address_size)),
60+
uid_type(
61+
llvm::Type::getInt64Ty(llvm_context)),
6062
i8_type(llvm::Type::getInt8Ty(llvm_context)),
6163
i8_zero(llvm::Constant::getNullValue(i8_type)),
6264
i32_type(llvm::Type::getInt32Ty(llvm_context)),
@@ -191,6 +193,14 @@ llvm::MDNode *CodeLifter::GetAddrAnnotation(uint64_t addr,
191193
return llvm::MDNode::get(context, pc_md);
192194
}
193195

196+
llvm::MDNode *CodeLifter::GetUidAnnotation(uint64_t uid,
197+
llvm::LLVMContext &context) const {
198+
auto uid_val = llvm::ConstantInt::get(
199+
remill::RecontextualizeType(uid_type, context), uid);
200+
auto uid_md = llvm::ValueAsMetadata::get(uid_val);
201+
return llvm::MDNode::get(context, uid_md);
202+
}
203+
194204
// Allocate and initialize the state structure.
195205
llvm::Value *
196206
CodeLifter::AllocateAndInitializeStateStructure(llvm::BasicBlock *block,

lib/Lifters/CodeLifter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class CodeLifter {
4545
const TypeProvider &type_provider;
4646
const TypeTranslator &type_specifier;
4747
llvm::IntegerType *const address_type;
48+
llvm::IntegerType *const uid_type;
4849

4950

5051
// Convenient to keep around.
@@ -78,6 +79,8 @@ class CodeLifter {
7879

7980
llvm::MDNode *GetAddrAnnotation(uint64_t addr,
8081
llvm::LLVMContext &context) const;
82+
llvm::MDNode *GetUidAnnotation(uint64_t uid,
83+
llvm::LLVMContext &context) const;
8184

8285
public:
8386
CodeLifter(const LifterOptions &options, llvm::Module *semantics_module,

0 commit comments

Comments
 (0)