1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/adjustment_method.h"
17 #include "base/logging.h"
18 #include "base/macros.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/stringprintf.h"
21 #include "courgette/assembly_program.h"
22 #include "courgette/courgette.h"
23 #include "courgette/encoded_program.h"
27 ////////////////////////////////////////////////////////////////////////////////
29 class NullAdjustmentMethod : public AdjustmentMethod {
30 bool Adjust(const AssemblyProgram& model, AssemblyProgram* program) {
35 ////////////////////////////////////////////////////////////////////////////////
37 // The purpose of adjustment is to assign indexes to Labels of a program 'p' to
38 // make the sequence of indexes similar to a 'model' program 'm'. Labels
39 // themselves don't have enough information to do this job, so we work with a
40 // LabelInfo surrogate for each label.
44 Label* label_; // The label that this info a surrogate for.
46 // Information used only in debugging messages.
47 uint32_t is_model_ : 1; // Is the label in the model?
48 uint32_t debug_index_ : 31; // An unique small number for naming the label.
50 uint32_t refs_; // Number of times this Label is referenced.
52 LabelInfo* assignment_; // Label from other program corresponding to this.
54 // LabelInfos are in a doubly linked list ordered by address (label_->rva_) so
55 // we can quickly find Labels adjacent in address order.
56 LabelInfo* next_addr_; // Label(Info) at next highest address.
57 LabelInfo* prev_addr_; // Label(Info) at next lowest address.
59 std::vector<uint32_t> positions_; // Offsets into the trace of references.
61 // Just a no-argument constructor and copy constructor. Actual LabelInfo
62 // objects are allocated in std::pair structs in a std::map.
70 prev_addr_(nullptr) {}
73 void operator=(const LabelInfo*); // Disallow assignment only.
75 // Public compiler generated copy constructor is needed to constuct
76 // std::pair<Label*, LabelInfo> so that fresh LabelInfos can be allocated
80 struct OrderLabelInfoByAddressAscending {
81 bool operator()(const LabelInfo* a, const LabelInfo* b) const {
82 return a->label_->rva_ < b->label_->rva_;
86 static std::string ToString(LabelInfo* info) {
88 base::StringAppendF(&s, "%c%d", "pm"[info->is_model_], info->debug_index_);
89 if (info->label_->index_ != Label::kNoIndex)
90 base::StringAppendF(&s, " (%d)", info->label_->index_);
92 base::StringAppendF(&s, " #%u", info->refs_);
96 // General graph matching is exponential, essentially trying all permutations.
97 // The exponential algorithm can be made faster by avoiding consideration of
98 // impossible or unlikely matches. We can make the matching practical by eager
99 // matching - by looking for likely matches and commiting to them, and using the
100 // committed assignment as the basis for further matching.
102 // The basic eager graph-matching assignment is based on several ideas:
104 // * The strongest match will be for parts of the program that have not
105 // changed. If part of a program has not changed, then the number of
106 // references to a label will be the same, and corresponding pairs of
107 // adjacent labels will have the same RVA difference.
109 // * Some assignments are 'obvious' if you look at the distribution. Example:
110 // if both the program and the model have a label that is referred to much
111 // more often than the next most refered-to label, it is likely the two
112 // labels correspond.
114 // * If a label from the program corresponds to a label in the model, it is
115 // likely that the labels near the corresponding labels also match. A
116 // conservative way of extending the match is to assign only those labels
117 // which have exactly the same address offset and reference count.
119 // * If two labels correspond, then we can try to match up the references
120 // before and after the labels in the reference stream. For this to be
121 // practical, the number of references has to be small, e.g. each label has
122 // exactly one reference.
125 // Note: we also tried a completely different approach: random assignment
126 // followed by simulated annealing. This produced similar results. The results
127 // were not as good for very small differences because the simulated annealing
128 // never quite hit the groove. And simulated annealing was several orders of
132 // TRIE node for suffix strings in the label reference sequence.
134 // We dynamically build a trie for both the program and model, growing the trie
135 // as necessary. The trie node for a (possibly) empty string of label
136 // references contains the distribution of labels following the string. The
137 // roots node (for the empty string) thus contains the simple distribution of
138 // labels within the label reference stream.
141 Node(LabelInfo* in_edge, Node* prev)
142 : in_edge_(in_edge), prev_(prev), count_(0),
144 length_ = 1 + (prev_ ? prev_->length_ : 0);
146 LabelInfo* in_edge_; //
147 Node* prev_; // Node at shorter length.
148 int count_; // Frequency of this path in Trie.
150 typedef std::map<LabelInfo*, Node*> Edges;
152 std::vector<int> places_; // Indexes into sequence of this item.
153 std::list<Node*> edges_in_frequency_order;
156 bool Extended() const { return !edges_.empty(); }
158 uint32_t Weight() const { return edges_in_frequency_order.front()->count_; }
161 static std::string ToString(Node* node) {
162 std::vector<std::string> prefix;
163 for (Node* n = node; n->prev_; n = n->prev_)
164 prefix.push_back(ToString(n->in_edge_));
168 const char* sep = "";
169 while (!prefix.empty()) {
176 s += base::StringPrintf("%u", node->count_);
178 s += base::NumberToString(node->edges_in_frequency_order.size());
183 typedef std::vector<LabelInfo*> Trace;
185 struct OrderNodeByCountDecreasing {
186 bool operator()(Node* a, Node* b) const {
187 if (a->count_ != b->count_)
188 return (a->count_) > (b->count_);
189 return a->places_.at(0) < b->places_.at(0); // Prefer first occuring.
193 struct OrderNodeByWeightDecreasing {
194 bool operator()(Node* a, Node* b) const {
195 // (Maybe tie-break on total count, followed by lowest assigned node indexes
197 uint32_t a_weight = a->Weight();
198 uint32_t b_weight = b->Weight();
199 if (a_weight != b_weight)
200 return a_weight > b_weight;
201 if (a->length_ != b->length_)
202 return a->length_ > b->length_; // Prefer longer.
203 return a->places_.at(0) < b->places_.at(0); // Prefer first occuring.
207 typedef std::set<Node*, OrderNodeByWeightDecreasing> NodeQueue;
209 class AssignmentProblem {
211 AssignmentProblem(const Trace& model, const Trace& problem)
217 ~AssignmentProblem() {
218 for (size_t i = 0; i < all_nodes_.size(); ++i)
219 delete all_nodes_[i];
223 m_root_ = MakeRootNode(m_trace_);
224 p_root_ = MakeRootNode(p_trace_);
227 while (!worklist_.empty()) {
228 Node* node = *worklist_.begin();
229 node->in_queue_ = false;
230 worklist_.erase(node);
234 VLOG(2) << unsolved_.size() << " unsolved items";
239 void AddToQueue(Node* node) {
240 if (node->length_ >= 10) {
241 VLOG(4) << "Length clipped " << ToString(node->prev_);
244 if (node->in_queue_) {
245 LOG(ERROR) << "Double add " << ToString(node);
248 // just to be sure data for prioritizing is available
249 ExtendNode(node, p_trace_);
250 // SkipCommittedLabels(node);
251 if (node->edges_in_frequency_order.empty())
253 node->in_queue_ = true;
254 worklist_.insert(node);
257 void SkipCommittedLabels(Node* node) {
258 ExtendNode(node, p_trace_);
259 uint32_t skipped = 0;
260 while (!node->edges_in_frequency_order.empty() &&
261 node->edges_in_frequency_order.front()->in_edge_->assignment_) {
263 node->edges_in_frequency_order.pop_front();
266 VLOG(4) << "Skipped " << skipped << " at " << ToString(node);
269 void TrySolveNode(Node* p_node) {
270 Node* front = p_node->edges_in_frequency_order.front();
271 if (front->in_edge_->assignment_) {
272 p_node->edges_in_frequency_order.pop_front();
278 // Compare frequencies of unassigned edges, and either make
279 // assignment(s) or move node to unsolved list
281 Node* m_node = FindModelNode(p_node);
283 if (m_node == nullptr) {
284 VLOG(2) << "Can't find model node";
285 unsolved_.insert(p_node);
288 ExtendNode(m_node, m_trace_);
290 // Lets just try greedy
292 SkipCommittedLabels(m_node);
293 if (m_node->edges_in_frequency_order.empty()) {
294 VLOG(4) << "Punting, no elements left in model vs "
295 << p_node->edges_in_frequency_order.size();
296 unsolved_.insert(p_node);
299 Node* m_match = m_node->edges_in_frequency_order.front();
300 Node* p_match = p_node->edges_in_frequency_order.front();
302 if (p_match->count_ > 1.1 * m_match->count_ ||
303 m_match->count_ > 1.1 * p_match->count_) {
304 VLOG(3) << "Tricky distribution "
305 << p_match->count_ << ":" << m_match->count_ << " "
306 << ToString(p_match) << " vs " << ToString(m_match);
310 m_node->edges_in_frequency_order.pop_front();
311 p_node->edges_in_frequency_order.pop_front();
313 LabelInfo* p_label_info = p_match->in_edge_;
314 LabelInfo* m_label_info = m_match->in_edge_;
315 int m_index = p_label_info->label_->index_;
316 if (m_index != Label::kNoIndex) {
317 VLOG(2) << "Cant use unassigned label from model " << m_index;
318 unsolved_.insert(p_node);
322 Assign(p_label_info, m_label_info);
324 AddToQueue(p_match); // find matches within new match
325 AddToQueue(p_node); // and more matches within this node
328 void Assign(LabelInfo* p_info, LabelInfo* m_info) {
329 AssignOne(p_info, m_info);
330 VLOG(4) << "Assign " << ToString(p_info) << " := " << ToString(m_info);
331 // Now consider unassigned adjacent addresses
332 TryExtendAssignment(p_info, m_info);
335 void AssignOne(LabelInfo* p_info, LabelInfo* m_info) {
336 p_info->label_->index_ = m_info->label_->index_;
339 m_info->assignment_ = p_info;
340 p_info->assignment_ = m_info;
343 void TryExtendAssignment(LabelInfo* p_info, LabelInfo* m_info) {
344 RVA m_rva_base = m_info->label_->rva_;
345 RVA p_rva_base = p_info->label_->rva_;
347 LabelInfo* m_info_next = m_info->next_addr_;
348 LabelInfo* p_info_next = p_info->next_addr_;
349 for ( ; m_info_next && p_info_next; ) {
350 if (m_info_next->assignment_)
353 RVA m_rva = m_info_next->label_->rva_;
354 RVA p_rva = p_info_next->label_->rva_;
356 if (m_rva - m_rva_base != p_rva - p_rva_base) {
357 // previous label was pointing to something that is different size
360 LabelInfo* m_info_next_next = m_info_next->next_addr_;
361 LabelInfo* p_info_next_next = p_info_next->next_addr_;
362 if (m_info_next_next && p_info_next_next) {
363 RVA m_rva_next = m_info_next_next->label_->rva_;
364 RVA p_rva_next = p_info_next_next->label_->rva_;
365 if (m_rva_next - m_rva != p_rva_next - p_rva) {
366 // Since following labels are no longer in address lockstep, assume
367 // this address has a difference.
372 // The label has inconsistent numbers of references, it is probably not
374 if (m_info_next->refs_ != p_info_next->refs_) {
378 VLOG(4) << " Extending assignment -> "
379 << ToString(p_info_next) << " := " << ToString(m_info_next);
381 AssignOne(p_info_next, m_info_next);
383 if (p_info_next->refs_ == m_info_next->refs_ &&
384 p_info_next->refs_ == 1) {
385 TryExtendSequence(p_info_next->positions_[0],
386 m_info_next->positions_[0]);
387 TryExtendSequenceBackwards(p_info_next->positions_[0],
388 m_info_next->positions_[0]);
391 p_info_next = p_info_next_next;
392 m_info_next = m_info_next_next;
395 LabelInfo* m_info_prev = m_info->prev_addr_;
396 LabelInfo* p_info_prev = p_info->prev_addr_;
397 for ( ; m_info_prev && p_info_prev; ) {
398 if (m_info_prev->assignment_)
401 RVA m_rva = m_info_prev->label_->rva_;
402 RVA p_rva = p_info_prev->label_->rva_;
404 if (m_rva - m_rva_base != p_rva - p_rva_base) {
405 // previous label was pointing to something that is different size
408 LabelInfo* m_info_prev_prev = m_info_prev->prev_addr_;
409 LabelInfo* p_info_prev_prev = p_info_prev->prev_addr_;
411 // The the label has inconsistent numbers of references, it is
412 // probably not the same thing
413 if (m_info_prev->refs_ != p_info_prev->refs_) {
417 AssignOne(p_info_prev, m_info_prev);
418 VLOG(4) << " Extending assignment <- " << ToString(p_info_prev) << " := "
419 << ToString(m_info_prev);
421 p_info_prev = p_info_prev_prev;
422 m_info_prev = m_info_prev_prev;
426 uint32_t TryExtendSequence(uint32_t p_pos_start, uint32_t m_pos_start) {
427 uint32_t p_pos = p_pos_start + 1;
428 uint32_t m_pos = m_pos_start + 1;
430 while (p_pos < p_trace_.size() && m_pos < m_trace_.size()) {
431 LabelInfo* p_info = p_trace_[p_pos];
432 LabelInfo* m_info = m_trace_[m_pos];
434 // To match, either (1) both are assigned or (2) both are unassigned.
435 if ((p_info->assignment_ == nullptr) != (m_info->assignment_ == nullptr))
438 // If they are assigned, it needs to be consistent (same index).
439 if (p_info->assignment_ && m_info->assignment_) {
440 if (p_info->label_->index_ != m_info->label_->index_)
447 if (p_info->refs_ != m_info->refs_)
450 AssignOne(p_info, m_info);
451 VLOG(4) << " Extending assignment seq[+" << p_pos - p_pos_start
452 << "] -> " << ToString(p_info) << " := " << ToString(m_info);
458 return p_pos - p_pos_start;
461 uint32_t TryExtendSequenceBackwards(uint32_t p_pos_start,
462 uint32_t m_pos_start) {
463 if (p_pos_start == 0 || m_pos_start == 0)
466 uint32_t p_pos = p_pos_start - 1;
467 uint32_t m_pos = m_pos_start - 1;
469 while (p_pos > 0 && m_pos > 0) {
470 LabelInfo* p_info = p_trace_[p_pos];
471 LabelInfo* m_info = m_trace_[m_pos];
473 if ((p_info->assignment_ == nullptr) != (m_info->assignment_ == nullptr))
476 if (p_info->assignment_ && m_info->assignment_) {
477 if (p_info->label_->index_ != m_info->label_->index_)
484 if (p_info->refs_ != m_info->refs_)
487 AssignOne(p_info, m_info);
488 VLOG(4) << " Extending assignment seq[-" << p_pos_start - p_pos
489 << "] <- " << ToString(p_info) << " := " << ToString(m_info);
495 return p_pos - p_pos_start;
498 Node* FindModelNode(Node* node) {
499 if (node->prev_ == nullptr)
502 Node* m_parent = FindModelNode(node->prev_);
503 if (m_parent == nullptr) {
507 ExtendNode(m_parent, m_trace_);
509 LabelInfo* p_label = node->in_edge_;
510 LabelInfo* m_label = p_label->assignment_;
511 if (m_label == nullptr) {
512 VLOG(2) << "Expected assigned prefix";
516 Node::Edges::iterator e = m_parent->edges_.find(m_label);
517 if (e == m_parent->edges_.end()) {
518 VLOG(3) << "Expected defined edge in parent";
525 Node* MakeRootNode(const Trace& trace) {
526 Node* node = new Node(nullptr, nullptr);
527 all_nodes_.push_back(node);
528 for (uint32_t i = 0; i < trace.size(); ++i) {
530 node->places_.push_back(i);
535 void ExtendNode(Node* node, const Trace& trace) {
536 // Make sure trie is filled in at this node.
537 if (node->Extended())
539 for (size_t i = 0; i < node->places_.size(); ++i) {
540 uint32_t index = node->places_.at(i);
541 if (index < trace.size()) {
542 LabelInfo* item = trace.at(index);
543 Node*& slot = node->edges_[item];
544 if (slot == nullptr) {
545 slot = new Node(item, node);
546 all_nodes_.push_back(slot);
547 node->edges_in_frequency_order.push_back(slot);
549 slot->places_.push_back(index + 1);
553 node->edges_in_frequency_order.sort(OrderNodeByCountDecreasing());
556 const Trace& m_trace_;
557 const Trace& p_trace_;
564 std::vector<Node*> all_nodes_;
566 DISALLOW_COPY_AND_ASSIGN(AssignmentProblem);
569 class GraphAdjuster : public AdjustmentMethod {
572 : prog_(nullptr), model_(nullptr), debug_label_index_gen_(0) {}
573 ~GraphAdjuster() = default;
575 bool Adjust(const AssemblyProgram& model, AssemblyProgram* program) {
576 VLOG(1) << "GraphAdjuster::Adjust";
579 debug_label_index_gen_ = 0;
584 prog_->UnassignIndexes();
585 CollectTraces(model_, &model_abs32_, &model_rel32_, true);
586 CollectTraces(prog_, &prog_abs32_, &prog_rel32_, false);
587 Solve(model_abs32_, prog_abs32_);
588 Solve(model_rel32_, prog_rel32_);
589 prog_->AssignRemainingIndexes();
594 void CollectTraces(const AssemblyProgram* program, Trace* abs32, Trace* rel32,
596 for (Label* label : program->abs32_label_annotations())
597 ReferenceLabel(abs32, is_model, label);
598 for (Label* label : program->rel32_label_annotations())
599 ReferenceLabel(rel32, is_model, label);
601 // TODO(sra): we could simply append all the labels in index order to
602 // incorporate some costing for entropy (bigger deltas) that will be
603 // introduced into the label address table by non-monotonic ordering. This
604 // would have some knock-on effects to parts of the algorithm that work on
605 // single-occurrence labels.
608 void Solve(const Trace& model, const Trace& problem) {
609 LinkLabelInfos(model);
610 LinkLabelInfos(problem);
611 AssignmentProblem a(model, problem);
615 void LinkLabelInfos(const Trace& trace) {
616 typedef std::set<LabelInfo*, OrderLabelInfoByAddressAscending> Ordered;
618 for (Trace::const_iterator p = trace.begin(); p != trace.end(); ++p)
620 LabelInfo* prev = nullptr;
621 for (Ordered::iterator p = ordered.begin(); p != ordered.end(); ++p) {
622 LabelInfo* curr = *p;
623 if (prev) prev->next_addr_ = curr;
624 curr->prev_addr_ = prev;
627 if (curr->positions_.size() != curr->refs_)
632 void ReferenceLabel(Trace* trace, bool is_model, Label* label) {
634 MakeLabelInfo(label, is_model, static_cast<uint32_t>(trace->size())));
637 LabelInfo* MakeLabelInfo(Label* label, bool is_model, uint32_t position) {
638 LabelInfo& slot = label_infos_[label];
639 if (slot.label_ == nullptr) {
641 slot.is_model_ = is_model;
642 slot.debug_index_ = ++debug_label_index_gen_;
644 slot.positions_.push_back(position);
649 AssemblyProgram* prog_; // Program to be adjusted, owned by caller.
650 const AssemblyProgram* model_; // Program to be mimicked, owned by caller.
657 int debug_label_index_gen_;
659 // Note LabelInfo is allocated inside map, so the LabelInfo lifetimes are
660 // managed by the map.
661 std::map<Label*, LabelInfo> label_infos_;
664 DISALLOW_COPY_AND_ASSIGN(GraphAdjuster);
668 ////////////////////////////////////////////////////////////////////////////////
670 void AdjustmentMethod::Destroy() { delete this; }
672 AdjustmentMethod* AdjustmentMethod::MakeNullAdjustmentMethod() {
673 return new NullAdjustmentMethod();
676 AdjustmentMethod* AdjustmentMethod::MakeTrieAdjustmentMethod() {
677 return new GraphAdjuster();
680 Status Adjust(const AssemblyProgram& model, AssemblyProgram* program) {
681 AdjustmentMethod* method = AdjustmentMethod::MakeProductionAdjustmentMethod();
682 bool ok = method->Adjust(model, program);
687 return C_ADJUSTMENT_FAILED;
690 } // namespace courgette