This source file includes following definitions.
- Adjust
- prev_addr_
- ToString
- ToString
- p_root_
- Solve
- AddToQueue
- SkipCommittedLabels
- TrySolveNode
- Assign
- AssignOne
- TryExtendAssignment
- TryExtendSequence
- TryExtendSequenceBackwards
- FindModelNode
- MakeRootNode
- ExtendNode
- debug_label_index_gen_
- Adjust
- Finish
- CollectTraces
- Solve
- LinkLabelInfos
- ReferenceLabel
- MakeLabelInfo
- Destroy
- MakeNullAdjustmentMethod
- MakeTrieAdjustmentMethod
- Adjust
#include "courgette/adjustment_method.h"
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "courgette/assembly_program.h"
#include "courgette/courgette.h"
#include "courgette/encoded_program.h"
namespace courgette {
class NullAdjustmentMethod : public AdjustmentMethod {
bool Adjust(const AssemblyProgram& model, AssemblyProgram* program) {
return true;
}
};
class LabelInfo {
public:
Label* label_;
uint32 is_model_ : 1;
uint32 debug_index_ : 31;
uint32 refs_;
LabelInfo* assignment_;
LabelInfo* next_addr_;
LabelInfo* prev_addr_;
std::vector<uint32> positions_;
LabelInfo()
: label_(NULL), is_model_(false), debug_index_(0), refs_(0),
assignment_(NULL),
next_addr_(NULL),
prev_addr_(NULL) {}
private:
void operator=(const LabelInfo*);
};
struct OrderLabelInfoByAddressAscending {
bool operator()(const LabelInfo* a, const LabelInfo* b) const {
return a->label_->rva_ < b->label_->rva_;
}
};
static std::string ToString(LabelInfo* info) {
std::string s;
base::StringAppendF(&s, "%c%d", "pm"[info->is_model_], info->debug_index_);
if (info->label_->index_ != Label::kNoIndex)
base::StringAppendF(&s, " (%d)", info->label_->index_);
base::StringAppendF(&s, " #%u", info->refs_);
return s;
}
struct Node {
Node(LabelInfo* in_edge, Node* prev)
: in_edge_(in_edge), prev_(prev), count_(0),
in_queue_(false) {
length_ = 1 + (prev_ ? prev_->length_ : 0);
}
LabelInfo* in_edge_;
Node* prev_;
int count_;
int length_;
typedef std::map<LabelInfo*, Node*> Edges;
Edges edges_;
std::vector<int> places_;
std::list<Node*> edges_in_frequency_order;
bool in_queue_;
bool Extended() const { return !edges_.empty(); }
uint32 Weight() const {
return edges_in_frequency_order.front()->count_;
}
};
static std::string ToString(Node* node) {
std::vector<std::string> prefix;
for (Node* n = node; n->prev_; n = n->prev_)
prefix.push_back(ToString(n->in_edge_));
std::string s;
s += "{";
const char* sep = "";
while (!prefix.empty()) {
s += sep;
sep = ",";
s += prefix.back();
prefix.pop_back();
}
s += base::StringPrintf("%u", node->count_);
s += " @";
s += base::Uint64ToString(node->edges_in_frequency_order.size());
s += "}";
return s;
}
typedef std::vector<LabelInfo*> Trace;
struct OrderNodeByCountDecreasing {
bool operator()(Node* a, Node* b) const {
if (a->count_ != b->count_)
return (a->count_) > (b->count_);
return a->places_.at(0) < b->places_.at(0);
}
};
struct OrderNodeByWeightDecreasing {
bool operator()(Node* a, Node* b) const {
uint32 a_weight = a->Weight();
uint32 b_weight = b->Weight();
if (a_weight != b_weight)
return a_weight > b_weight;
if (a->length_ != b->length_)
return a->length_ > b->length_;
return a->places_.at(0) < b->places_.at(0);
}
};
typedef std::set<Node*, OrderNodeByWeightDecreasing> NodeQueue;
class AssignmentProblem {
public:
AssignmentProblem(const Trace& model,
const Trace& problem)
: m_trace_(model),
p_trace_(problem),
m_root_(NULL),
p_root_(NULL) {
}
~AssignmentProblem() {
for (size_t i = 0; i < all_nodes_.size(); ++i)
delete all_nodes_[i];
}
bool Solve() {
m_root_ = MakeRootNode(m_trace_);
p_root_ = MakeRootNode(p_trace_);
AddToQueue(p_root_);
while (!worklist_.empty()) {
Node* node = *worklist_.begin();
node->in_queue_ = false;
worklist_.erase(node);
TrySolveNode(node);
}
VLOG(2) << unsolved_.size() << " unsolved items";
return true;
}
private:
void AddToQueue(Node* node) {
if (node->length_ >= 10) {
VLOG(4) << "Length clipped " << ToString(node->prev_);
return;
}
if (node->in_queue_) {
LOG(ERROR) << "Double add " << ToString(node);
return;
}
ExtendNode(node, p_trace_);
if (node->edges_in_frequency_order.empty())
return;
node->in_queue_ = true;
worklist_.insert(node);
}
void SkipCommittedLabels(Node* node) {
ExtendNode(node, p_trace_);
uint32 skipped = 0;
while (!node->edges_in_frequency_order.empty() &&
node->edges_in_frequency_order.front()->in_edge_->assignment_) {
++skipped;
node->edges_in_frequency_order.pop_front();
}
if (skipped > 0)
VLOG(4) << "Skipped " << skipped << " at " << ToString(node);
}
void TrySolveNode(Node* p_node) {
Node* front = p_node->edges_in_frequency_order.front();
if (front->in_edge_->assignment_) {
p_node->edges_in_frequency_order.pop_front();
AddToQueue(front);
AddToQueue(p_node);
return;
}
Node* m_node = FindModelNode(p_node);
if (m_node == NULL) {
VLOG(2) << "Can't find model node";
unsolved_.insert(p_node);
return;
}
ExtendNode(m_node, m_trace_);
SkipCommittedLabels(m_node);
if (m_node->edges_in_frequency_order.empty()) {
VLOG(4) << "Punting, no elements left in model vs "
<< p_node->edges_in_frequency_order.size();
unsolved_.insert(p_node);
return;
}
Node* m_match = m_node->edges_in_frequency_order.front();
Node* p_match = p_node->edges_in_frequency_order.front();
if (p_match->count_ > 1.1 * m_match->count_ ||
m_match->count_ > 1.1 * p_match->count_) {
VLOG(3) << "Tricky distribution "
<< p_match->count_ << ":" << m_match->count_ << " "
<< ToString(p_match) << " vs " << ToString(m_match);
return;
}
m_node->edges_in_frequency_order.pop_front();
p_node->edges_in_frequency_order.pop_front();
LabelInfo* p_label_info = p_match->in_edge_;
LabelInfo* m_label_info = m_match->in_edge_;
int m_index = p_label_info->label_->index_;
if (m_index != Label::kNoIndex) {
VLOG(2) << "Cant use unassigned label from model " << m_index;
unsolved_.insert(p_node);
return;
}
Assign(p_label_info, m_label_info);
AddToQueue(p_match);
AddToQueue(p_node);
}
void Assign(LabelInfo* p_info, LabelInfo* m_info) {
AssignOne(p_info, m_info);
VLOG(4) << "Assign " << ToString(p_info) << " := " << ToString(m_info);
TryExtendAssignment(p_info, m_info);
}
void AssignOne(LabelInfo* p_info, LabelInfo* m_info) {
p_info->label_->index_ = m_info->label_->index_;
m_info->assignment_ = p_info;
p_info->assignment_ = m_info;
}
void TryExtendAssignment(LabelInfo* p_info, LabelInfo* m_info) {
RVA m_rva_base = m_info->label_->rva_;
RVA p_rva_base = p_info->label_->rva_;
LabelInfo* m_info_next = m_info->next_addr_;
LabelInfo* p_info_next = p_info->next_addr_;
for ( ; m_info_next && p_info_next; ) {
if (m_info_next->assignment_)
break;
RVA m_rva = m_info_next->label_->rva_;
RVA p_rva = p_info_next->label_->rva_;
if (m_rva - m_rva_base != p_rva - p_rva_base) {
break;
}
LabelInfo* m_info_next_next = m_info_next->next_addr_;
LabelInfo* p_info_next_next = p_info_next->next_addr_;
if (m_info_next_next && p_info_next_next) {
RVA m_rva_next = m_info_next_next->label_->rva_;
RVA p_rva_next = p_info_next_next->label_->rva_;
if (m_rva_next - m_rva != p_rva_next - p_rva) {
break;
}
}
if (m_info_next->refs_ != p_info_next->refs_) {
break;
}
VLOG(4) << " Extending assignment -> "
<< ToString(p_info_next) << " := " << ToString(m_info_next);
AssignOne(p_info_next, m_info_next);
if (p_info_next->refs_ == m_info_next->refs_ &&
p_info_next->refs_ == 1) {
TryExtendSequence(p_info_next->positions_[0],
m_info_next->positions_[0]);
TryExtendSequenceBackwards(p_info_next->positions_[0],
m_info_next->positions_[0]);
}
p_info_next = p_info_next_next;
m_info_next = m_info_next_next;
}
LabelInfo* m_info_prev = m_info->prev_addr_;
LabelInfo* p_info_prev = p_info->prev_addr_;
for ( ; m_info_prev && p_info_prev; ) {
if (m_info_prev->assignment_)
break;
RVA m_rva = m_info_prev->label_->rva_;
RVA p_rva = p_info_prev->label_->rva_;
if (m_rva - m_rva_base != p_rva - p_rva_base) {
break;
}
LabelInfo* m_info_prev_prev = m_info_prev->prev_addr_;
LabelInfo* p_info_prev_prev = p_info_prev->prev_addr_;
if (m_info_prev->refs_ != p_info_prev->refs_) {
break;
}
AssignOne(p_info_prev, m_info_prev);
VLOG(4) << " Extending assignment <- " << ToString(p_info_prev) << " := "
<< ToString(m_info_prev);
p_info_prev = p_info_prev_prev;
m_info_prev = m_info_prev_prev;
}
}
uint32 TryExtendSequence(uint32 p_pos_start, uint32 m_pos_start) {
uint32 p_pos = p_pos_start + 1;
uint32 m_pos = m_pos_start + 1;
while (p_pos < p_trace_.size() && m_pos < m_trace_.size()) {
LabelInfo* p_info = p_trace_[p_pos];
LabelInfo* m_info = m_trace_[m_pos];
if ((p_info->assignment_ == NULL) != (m_info->assignment_ == NULL))
break;
if (p_info->assignment_ && m_info->assignment_) {
if (p_info->label_->index_ != m_info->label_->index_)
break;
++p_pos;
++m_pos;
continue;
}
if (p_info->refs_ != m_info->refs_)
break;
AssignOne(p_info, m_info);
VLOG(4) << " Extending assignment seq[+" << p_pos - p_pos_start
<< "] -> " << ToString(p_info) << " := " << ToString(m_info);
++p_pos;
++m_pos;
}
return p_pos - p_pos_start;
}
uint32 TryExtendSequenceBackwards(uint32 p_pos_start, uint32 m_pos_start) {
if (p_pos_start == 0 || m_pos_start == 0)
return 0;
uint32 p_pos = p_pos_start - 1;
uint32 m_pos = m_pos_start - 1;
while (p_pos > 0 && m_pos > 0) {
LabelInfo* p_info = p_trace_[p_pos];
LabelInfo* m_info = m_trace_[m_pos];
if ((p_info->assignment_ == NULL) != (m_info->assignment_ == NULL))
break;
if (p_info->assignment_ && m_info->assignment_) {
if (p_info->label_->index_ != m_info->label_->index_)
break;
--p_pos;
--m_pos;
continue;
}
if (p_info->refs_ != m_info->refs_)
break;
AssignOne(p_info, m_info);
VLOG(4) << " Extending assignment seq[-" << p_pos_start - p_pos
<< "] <- " << ToString(p_info) << " := " << ToString(m_info);
--p_pos;
--m_pos;
}
return p_pos - p_pos_start;
}
Node* FindModelNode(Node* node) {
if (node->prev_ == NULL)
return m_root_;
Node* m_parent = FindModelNode(node->prev_);
if (m_parent == NULL) {
return NULL;
}
ExtendNode(m_parent, m_trace_);
LabelInfo* p_label = node->in_edge_;
LabelInfo* m_label = p_label->assignment_;
if (m_label == NULL) {
VLOG(2) << "Expected assigned prefix";
return NULL;
}
Node::Edges::iterator e = m_parent->edges_.find(m_label);
if (e == m_parent->edges_.end()) {
VLOG(3) << "Expected defined edge in parent";
return NULL;
}
return e->second;
}
Node* MakeRootNode(const Trace& trace) {
Node* node = new Node(NULL, NULL);
all_nodes_.push_back(node);
for (uint32 i = 0; i < trace.size(); ++i) {
++node->count_;
node->places_.push_back(i);
}
return node;
}
void ExtendNode(Node* node, const Trace& trace) {
if (node->Extended())
return;
for (size_t i = 0; i < node->places_.size(); ++i) {
uint32 index = node->places_.at(i);
if (index < trace.size()) {
LabelInfo* item = trace.at(index);
Node*& slot = node->edges_[item];
if (slot == NULL) {
slot = new Node(item, node);
all_nodes_.push_back(slot);
node->edges_in_frequency_order.push_back(slot);
}
slot->places_.push_back(index + 1);
++slot->count_;
}
}
node->edges_in_frequency_order.sort(OrderNodeByCountDecreasing());
}
const Trace& m_trace_;
const Trace& p_trace_;
Node* m_root_;
Node* p_root_;
NodeQueue worklist_;
NodeQueue unsolved_;
std::vector<Node*> all_nodes_;
DISALLOW_COPY_AND_ASSIGN(AssignmentProblem);
};
class GraphAdjuster : public AdjustmentMethod {
public:
GraphAdjuster()
: prog_(NULL),
model_(NULL),
debug_label_index_gen_(0) {}
~GraphAdjuster() {}
bool Adjust(const AssemblyProgram& model, AssemblyProgram* program) {
VLOG(1) << "GraphAdjuster::Adjust";
prog_ = program;
model_ = &model;
debug_label_index_gen_ = 0;
return Finish();
}
bool Finish() {
prog_->UnassignIndexes();
CollectTraces(model_, &model_abs32_, &model_rel32_, true);
CollectTraces(prog_, &prog_abs32_, &prog_rel32_, false);
Solve(model_abs32_, prog_abs32_);
Solve(model_rel32_, prog_rel32_);
prog_->AssignRemainingIndexes();
return true;
}
private:
void CollectTraces(const AssemblyProgram* program, Trace* abs32, Trace* rel32,
bool is_model) {
const InstructionVector& instructions = program->instructions();
for (size_t i = 0; i < instructions.size(); ++i) {
Instruction* instruction = instructions[i];
if (Label* label = program->InstructionAbs32Label(instruction))
ReferenceLabel(abs32, label, is_model);
if (Label* label = program->InstructionRel32Label(instruction))
ReferenceLabel(rel32, label, is_model);
}
}
void Solve(const Trace& model, const Trace& problem) {
LinkLabelInfos(model);
LinkLabelInfos(problem);
AssignmentProblem a(model, problem);
a.Solve();
}
void LinkLabelInfos(const Trace& trace) {
typedef std::set<LabelInfo*, OrderLabelInfoByAddressAscending> Ordered;
Ordered ordered;
for (Trace::const_iterator p = trace.begin(); p != trace.end(); ++p)
ordered.insert(*p);
LabelInfo* prev = NULL;
for (Ordered::iterator p = ordered.begin(); p != ordered.end(); ++p) {
LabelInfo* curr = *p;
if (prev) prev->next_addr_ = curr;
curr->prev_addr_ = prev;
prev = curr;
if (curr->positions_.size() != curr->refs_)
NOTREACHED();
}
}
void ReferenceLabel(Trace* trace, Label* label, bool is_model) {
trace->push_back(MakeLabelInfo(label, is_model,
static_cast<uint32>(trace->size())));
}
LabelInfo* MakeLabelInfo(Label* label, bool is_model, uint32 position) {
LabelInfo& slot = label_infos_[label];
if (slot.label_ == NULL) {
slot.label_ = label;
slot.is_model_ = is_model;
slot.debug_index_ = ++debug_label_index_gen_;
}
slot.positions_.push_back(position);
++slot.refs_;
return &slot;
}
AssemblyProgram* prog_;
const AssemblyProgram* model_;
Trace model_abs32_;
Trace model_rel32_;
Trace prog_abs32_;
Trace prog_rel32_;
int debug_label_index_gen_;
std::map<Label*, LabelInfo> label_infos_;
private:
DISALLOW_COPY_AND_ASSIGN(GraphAdjuster);
};
void AdjustmentMethod::Destroy() { delete this; }
AdjustmentMethod* AdjustmentMethod::MakeNullAdjustmentMethod() {
return new NullAdjustmentMethod();
}
AdjustmentMethod* AdjustmentMethod::MakeTrieAdjustmentMethod() {
return new GraphAdjuster();
}
Status Adjust(const AssemblyProgram& model, AssemblyProgram* program) {
AdjustmentMethod* method = AdjustmentMethod::MakeProductionAdjustmentMethod();
bool ok = method->Adjust(model, program);
method->Destroy();
if (ok)
return C_OK;
else
return C_ADJUSTMENT_FAILED;
}
}