Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(373)

Side by Side Diff: courgette/rel32_finder_x64.cc

Issue 2008253004: Refactor rel32 searching process for x64 to make it more similar to x86. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve rel32_finder doc Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "courgette/rel32_finder_x64.h"
6
7 namespace courgette {
8
9 Rel32FinderX64::Rel32FinderX64(RVA relocs_start_rva,
10 RVA relocs_end_rva,
11 RVA size_of_image)
12 : Rel32Finder(relocs_start_rva, relocs_end_rva),
13 size_of_image_(size_of_image) {}
14
15 // Scan for opcodes matching the following instructions :
16 // rel32 JMP/CALL
17 // rip mov/lea
huangs 2016/05/30 05:48:34 NIT: Capitalize MOV/LEA for consistency (Jcc is fi
etiennep 2016/05/30 17:07:30 Done.
18 // Jcc (excluding JPO/JPE)
19 // Falsely detected rel32 that collide with known abs32 or that point outside
20 // valid regions are discarded.
21 void Rel32FinderX64::Find(const uint8_t* start_pointer,
22 const uint8_t* end_pointer,
23 RVA start_rva,
24 RVA end_rva,
25 const std::vector<RVA>& abs32_locations) {
26 // Quick way to convert from Pointer to RVA within a single Section is to
27 // subtract 'adjust_pointer_to_rva'.
huangs 2016/05/30 05:48:34 NIT: |adjust_pointer_to_rva|, now that it's a vari
etiennep 2016/05/30 17:07:30 Done.
28 const uint8_t* const adjust_pointer_to_rva = start_pointer - start_rva;
29
30 std::vector<RVA>::const_iterator abs32_pos = abs32_locations.begin();
31
32 // Find the rel32 relocations.
33 const uint8_t* p = start_pointer;
34 while (p < end_pointer) {
35 RVA current_rva = static_cast<RVA>(p - adjust_pointer_to_rva);
36
37 // Skip the base reloation table if we encounter it.
38 // Note: We're not bothering to handle the edge case where a Rel32 pointer
39 // collides with |relocs_start_rva_| by being {1, 2, 3}-bytes before it.
40 if (current_rva >= relocs_start_rva_ && current_rva < relocs_end_rva_) {
41 p += relocs_end_rva_ - current_rva;
42 continue;
43 }
44
45 // Heuristic discovery of rel32 locations in instruction stream: are the
46 // next few bytes the start of an instruction containing a rel32
47 // addressing mode?
48 const uint8_t* rel32 = nullptr;
49 bool is_rip_relative = false;
50
51 if (p + 5 <= end_pointer) {
52 if (p[0] == 0xE8 || p[0] == 0xE9) // jmp rel32 and call rel32
53 rel32 = p + 1;
54 }
55 if (p + 6 <= end_pointer) {
56 if (p[0] == 0x0F && (p[1] & 0xF0) == 0x80) { // Jcc long form
57 if (p[1] != 0x8A && p[1] != 0x8B) // JPE/JPO unlikely
58 rel32 = p + 2;
59 } else if (p[0] == 0xFF && (p[1] == 0x15 || p[1] == 0x25)) {
60 // rip relative call/jmp
61 rel32 = p + 2;
62 is_rip_relative = true;
63 }
64 }
65 // TODO(etiennep): Many rip mov/lea variants are not detected.
huangs 2016/05/30 05:48:34 NIT: ... Experiment and fix.
etiennep 2016/05/30 17:07:30 Done.
66 if (p + 7 <= end_pointer) {
67 if ((p[0] & 0xFB) == 0x48 && // Dst reg : 48/4C [rax-rdi]/[r8-r15]
huangs 2016/05/30 05:48:34 Src or Dst?
etiennep 2016/05/30 17:07:30 Dst
68 p[1] == 0x8D && // lea
69 (p[2] & 0xC7) == 0x05) { // Dst reg : [05-3D]
huangs 2016/05/30 05:48:34 NIT: Dst reg: [05,0D,...,3D] = [r8-r15]
etiennep 2016/05/30 17:07:30 Done.
70 // rip relative lea QWORD
huangs 2016/05/30 05:48:34 QWORD is 64-bits, but we only read 32-bits for thi
etiennep 2016/05/30 17:07:30 Done.
71 rel32 = p + 3;
72 is_rip_relative = true;
73 } else if ((p[0] & 0xFB) == 0x48 && // Dst reg : 48/4C [rax-rdi]/[r8-r15]
huangs 2016/05/30 05:48:34 Note that the two logic can be combined: if ((p[1
etiennep 2016/05/30 17:07:30 Done.
74 p[1] == 0x8B && // mov
75 (p[2] & 0xC7) == 0x05) { // Dst reg : [05-3D]
76 // rip relative mov QWORD
77 rel32 = p + 3;
78 is_rip_relative = true;
79 }
80 }
81
82 if (rel32) {
83 RVA rel32_rva = static_cast<RVA>(rel32 - adjust_pointer_to_rva);
84
85 // Is there an abs32 reloc overlapping the candidate?
86 while (abs32_pos != abs32_locations.end() && *abs32_pos < rel32_rva - 3)
87 ++abs32_pos;
88 // Now: (*abs32_pos > rel32_rva - 4) i.e. the lowest addressed 4-byte
89 // region that could overlap rel32_rva.
90 if (abs32_pos != abs32_locations.end()) {
91 if (*abs32_pos < rel32_rva + 4) {
92 // Beginning of abs32 reloc is before end of rel32 reloc so they
93 // overlap. Skip four bytes past the abs32 reloc.
94 p += (*abs32_pos + 4) - current_rva;
95 continue;
96 }
97 }
98
99 // + 4 since offset is relative to start of next instruction.
100 RVA target_rva = rel32_rva + 4 + Read32LittleEndian(rel32);
101 // To be valid, rel32 target must be within image, and within this
102 // section.
103 if (target_rva < size_of_image_ && // Subsumes rva != kUnassignedRVA.
huangs 2016/05/30 05:48:34 Re. your Patch Set #2 comment on moving |abs32_loc
etiennep 2016/05/30 17:07:30 Will talk about in next cl!
104 (is_rip_relative ||
105 (start_rva <= target_rva && target_rva < end_rva))) {
106 rel32_locations_.push_back(rel32_rva);
107 #if COURGETTE_HISTOGRAM_TARGETS
108 ++rel32_target_rvas_[target_rva];
109 #endif
110 p = rel32 + 4;
111 continue;
112 }
113 }
114 p += 1;
115 }
116 }
117
118 } // namespace courgette
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698