Skip to content

Commit

Permalink
Issue: sdasgup3/validating-binary-decompilation#2: Reaching definitio…
Browse files Browse the repository at this point in the history
…n gives us information about all the definitions reaching a program point. But for data-flow-graph we need which of those reaching defintions are actually used at that program point.
  • Loading branch information
sdasgup3 committed Jul 31, 2019
1 parent aa86423 commit d937ece
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/cfg/cfg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ void Cfg::recompute_reaching_defs_in() {
}
}

// Find the reaching defintions which are actually used at the program points
reaching_and_used_defs_in_.resize(get_code().size() + 1, Dfv_RD(get_code().size(), RegSet::empty()));
for (auto i = ++reachable_begin(), ie = reachable_end(); i != ie; ++i) {
for (size_t j = 0, je = num_instrs(*i); j < je; ++j) {
const auto idx = get_index({*i, j});
auto rd_ins = reaching_defs_in({*i, j});
auto read_set = maybe_read_set(get_code()[idx]);

for (size_t k = 0 ; k < rd_ins.size(); k++) {
if (rd_ins[k] == RegSet::empty()) {
continue;
}
reaching_and_used_defs_in_[idx][k] = rd_ins[k] & read_set;
}
}
}

#ifdef DEBUG_CFG_RD
for (size_t k = 0 ; k < get_code().size(); k++) {
std::cout << get_code()[k] << "\n";
Expand Down
18 changes: 14 additions & 4 deletions src/cfg/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ class Cfg {
Dfv_RD reaching_defs_in(id_type id) const {
assert(is_reachable(id));
return reaching_defs_in_[get_index({id, 0})];
//return reaching_defs_in_kill_[get_index({id, 0})];
}

/** Returns the set of instrucions along with the register
Expand All @@ -403,7 +402,11 @@ class Cfg {
Dfv_RD reaching_defs_in(const loc_type& loc) const {
assert(is_reachable(loc.first));
return reaching_defs_in_[get_index(loc)];
//return reaching_defs_in_kill_[get_index(loc)];
}

Dfv_RD reaching_and_used_defs_in(const loc_type& loc) const {
assert(is_reachable(loc.first));
return reaching_and_used_defs_in_[get_index(loc)];
}

/** Returns the set of registers that are defined on entry to this graph. */
Expand Down Expand Up @@ -537,7 +540,7 @@ class Cfg {
const auto& lbl = instr.get_operand<x64asm::Label>(0);
const auto found = fncs_summary.find(lbl);
if (found != fncs_summary.end()) {
// we do: use it, instead of linux calling convention
//!l!=
return found->second.must_undef_set;
}
}
Expand Down Expand Up @@ -631,10 +634,17 @@ class Cfg {
/** Scratch space for computing reachability. */
std::vector<id_type> work_list_;

/** The set of reaching definitions to every instruction.
/** The set of reaching definitions to the beginning and end of every instruction.
The final element refers to the exit block. */
std::vector<Dfv_RD> reaching_defs_in_;
std::vector<Dfv_RD> reaching_defs_out_;

/** Even though there might be many definitons reaching a particular program
point, but a data flow graph cares only about the ones which are used at that
program point */
std::vector<Dfv_RD> reaching_and_used_defs_in_;


/** The set of registers defined in for every instruction. The final element refers to the exit block. */
std::vector<x64asm::RegSet> def_ins_;
/** The set of registers defined out of every block. */
Expand Down
2 changes: 1 addition & 1 deletion src/cfg/dot_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void DotWriter::plot_dfg(ostream& os, const Cfg& cfg) const {
for (auto i = ++cfg.reachable_begin(), ie = cfg.reachable_end(); i != ie; ++i) {
for (size_t j = 0, je = cfg.num_instrs(*i); j < je; ++j) {
const auto idx = cfg.get_index({*i, j});
auto rd_ins = cfg.reaching_defs_in({*i, j});
auto rd_ins = cfg.reaching_and_used_defs_in({*i, j});

for (size_t k = 0 ; k < rd_ins.size(); k++) {
if (rd_ins[k] == RegSet::empty()) {
Expand Down

0 comments on commit d937ece

Please # to comment.