-
Notifications
You must be signed in to change notification settings - Fork 54
/
Assembler.cpp
302 lines (252 loc) · 8.3 KB
/
Assembler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Assembler.h"
#include "ProcessRecordReplay.h"
#include "udis86/types.h"
#include <sys/mman.h>
namespace mozilla {
namespace recordreplay {
Assembler::Assembler()
: mCursor(nullptr), mCursorEnd(nullptr), mCanAllocateStorage(true) {}
Assembler::Assembler(uint8_t* aStorage, size_t aSize)
: mCursor(aStorage),
mCursorEnd(aStorage + aSize),
mCanAllocateStorage(false) {}
Assembler::~Assembler() {
// Patch each jump to the point where the jump's target was copied, if there
// is one.
for (auto pair : mJumps) {
uint8_t* source = pair.first;
uint8_t* target = pair.second;
for (auto copyPair : mCopiedInstructions) {
if (copyPair.first == target) {
PatchJump(source, copyPair.second);
break;
}
}
}
}
void Assembler::NoteOriginalInstruction(uint8_t* aIp) {
mCopiedInstructions.emplaceBack(aIp, Current());
}
void Assembler::Advance(size_t aSize) {
MOZ_RELEASE_ASSERT(aSize <= MaximumAdvance);
mCursor += aSize;
}
static const size_t JumpBytes = 17;
uint8_t* Assembler::Current() {
// Reallocate the buffer if there is not enough space. We need enough for the
// maximum space used by any of the assembling functions, as well as for a
// following jump for fallthrough to the next allocated space.
if (size_t(mCursorEnd - mCursor) <= MaximumAdvance + JumpBytes) {
MOZ_RELEASE_ASSERT(mCanAllocateStorage);
// Allocate some writable, executable memory.
static const size_t BufferSize = PageSize;
uint8_t* buffer = new uint8_t[PageSize];
UnprotectExecutableMemory(buffer, PageSize);
if (mCursor) {
// Patch a jump for fallthrough from the last allocation.
MOZ_RELEASE_ASSERT(size_t(mCursorEnd - mCursor) >= JumpBytes);
PatchJump(mCursor, buffer);
}
mCursor = buffer;
mCursorEnd = &buffer[BufferSize];
}
return mCursor;
}
static void Push16(uint8_t** aIp, uint16_t aValue) {
(*aIp)[0] = 0x66;
(*aIp)[1] = 0x68;
*reinterpret_cast<uint16_t*>(*aIp + 2) = aValue;
(*aIp) += 4;
}
/* static */ void Assembler::PatchJump(uint8_t* aIp, void* aTarget) {
// Push the target literal onto the stack, 2 bytes at a time. This is
// apparently the best way of getting an arbitrary 8 byte literal onto the
// stack, as 4 byte literals we push will be sign extended to 8 bytes.
size_t ntarget = reinterpret_cast<size_t>(aTarget);
Push16(&aIp, ntarget >> 48);
Push16(&aIp, ntarget >> 32);
Push16(&aIp, ntarget >> 16);
Push16(&aIp, ntarget);
*aIp = 0xC3; // ret
}
void Assembler::Jump(void* aTarget) {
PatchJump(Current(), aTarget);
mJumps.emplaceBack(Current(), (uint8_t*)aTarget);
Advance(JumpBytes);
}
static uint8_t OppositeJump(uint8_t aOpcode) {
// Get the opposite single byte jump opcode for a one or two byte conditional
// jump. Opposite opcodes are adjacent, e.g. 0x7C -> jl and 0x7D -> jge.
if (aOpcode >= 0x80 && aOpcode <= 0x8F) {
aOpcode -= 0x10;
} else {
MOZ_RELEASE_ASSERT(aOpcode >= 0x70 && aOpcode <= 0x7F);
}
return (aOpcode & 1) ? aOpcode - 1 : aOpcode + 1;
}
void Assembler::ConditionalJump(uint8_t aCode, void* aTarget) {
uint8_t* ip = Current();
ip[0] = OppositeJump(aCode);
ip[1] = (uint8_t)JumpBytes;
Advance(2);
Jump(aTarget);
}
void Assembler::CopyInstruction(uint8_t* aIp, size_t aSize) {
MOZ_RELEASE_ASSERT(aSize <= MaximumInstructionLength);
memcpy(Current(), aIp, aSize);
Advance(aSize);
}
void Assembler::PushRax() { NewInstruction(0x50); }
void Assembler::PopRax() { NewInstruction(0x58); }
void Assembler::JumpToRax() { NewInstruction(0xFF, 0xE0); }
void Assembler::CallRax() { NewInstruction(0xFF, 0xD0); }
void Assembler::LoadRax(size_t aWidth) {
switch (aWidth) {
case 1:
NewInstruction(0x8A, 0x00);
break;
case 2:
NewInstruction(0x66, 0x8B, 0x00);
break;
case 4:
NewInstruction(0x8B, 0x00);
break;
case 8:
NewInstruction(0x48, 0x8B, 0x00);
break;
default:
MOZ_CRASH();
}
}
void Assembler::CompareRaxWithTopOfStack() {
NewInstruction(0x48, 0x39, 0x04, 0x24);
}
void Assembler::PushRbx() { NewInstruction(0x53); }
void Assembler::PopRbx() { NewInstruction(0x5B); }
void Assembler::StoreRbxToRax(size_t aWidth) {
switch (aWidth) {
case 1:
NewInstruction(0x88, 0x18);
break;
case 2:
NewInstruction(0x66, 0x89, 0x18);
break;
case 4:
NewInstruction(0x89, 0x18);
break;
case 8:
NewInstruction(0x48, 0x89, 0x18);
break;
default:
MOZ_CRASH();
}
}
void Assembler::CompareValueWithRax(uint8_t aValue, size_t aWidth) {
switch (aWidth) {
case 1:
NewInstruction(0x3C, aValue);
break;
case 2:
NewInstruction(0x66, 0x83, 0xF8, aValue);
break;
case 4:
NewInstruction(0x83, 0xF8, aValue);
break;
case 8:
NewInstruction(0x48, 0x83, 0xF8, aValue);
break;
default:
MOZ_CRASH();
}
}
static const size_t MoveImmediateBytes = 10;
/* static */ void Assembler::PatchMoveImmediateToRax(uint8_t* aIp,
void* aValue) {
aIp[0] = 0x40 | (1 << 3);
aIp[1] = 0xB8;
*reinterpret_cast<void**>(aIp + 2) = aValue;
}
void Assembler::MoveImmediateToRax(void* aValue) {
PatchMoveImmediateToRax(Current(), aValue);
Advance(MoveImmediateBytes);
}
void Assembler::MoveRaxToRegister(/*ud_type*/ int aRegister) {
MOZ_RELEASE_ASSERT(aRegister == NormalizeRegister(aRegister));
if (aRegister <= UD_R_RDI) {
NewInstruction(0x48, 0x89, 0xC0 + aRegister - UD_R_RAX);
} else {
NewInstruction(0x49, 0x89, 0xC0 + aRegister - UD_R_R8);
}
}
void Assembler::MoveRegisterToRax(/*ud_type*/ int aRegister) {
MOZ_RELEASE_ASSERT(aRegister == NormalizeRegister(aRegister));
if (aRegister <= UD_R_RDI) {
NewInstruction(0x48, 0x89, 0xC0 + (aRegister - UD_R_RAX) * 8);
} else {
NewInstruction(0x4C, 0x89, 0xC0 + (aRegister - UD_R_R8) * 8);
}
}
void Assembler::ExchangeByteRegisterWithAddressAtRbx(
/*ud_type*/ int aRegister) {
MOZ_RELEASE_ASSERT(aRegister == NormalizeRegister(aRegister));
if (aRegister <= UD_R_RDI) {
NewInstruction(0x86, 0x03 + (aRegister - UD_R_RAX) * 8);
} else {
NewInstruction(0x44, 0x86, 0x03 + (aRegister - UD_R_R8) * 8);
}
}
void Assembler::ExchangeByteRbxWithAddressAtRax() {
NewInstruction(0x86, 0x18);
}
/* static */ /*ud_type*/ int Assembler::NormalizeRegister(
/*ud_type*/ int aRegister) {
if (aRegister >= UD_R_AL && aRegister <= UD_R_R15B) {
return aRegister - UD_R_AL + UD_R_RAX;
}
if (aRegister >= UD_R_AX && aRegister <= UD_R_R15W) {
return aRegister - UD_R_AX + UD_R_RAX;
}
if (aRegister >= UD_R_EAX && aRegister <= UD_R_R15D) {
return aRegister - UD_R_EAX + UD_R_RAX;
}
if (aRegister >= UD_R_RAX && aRegister <= UD_R_R15) {
return aRegister;
}
return UD_NONE;
}
/* static */ bool Assembler::CanPatchShortJump(uint8_t* aIp, void* aTarget) {
return (aIp + 2 - 128 <= aTarget) && (aIp + 2 + 127 >= aTarget);
}
/* static */ void Assembler::PatchShortJump(uint8_t* aIp, void* aTarget) {
MOZ_RELEASE_ASSERT(CanPatchShortJump(aIp, aTarget));
aIp[0] = 0xEB;
aIp[1] = uint8_t(static_cast<uint8_t*>(aTarget) - aIp - 2);
}
/* static */ void Assembler::PatchJumpClobberRax(uint8_t* aIp, void* aTarget) {
PatchMoveImmediateToRax(aIp, aTarget);
aIp[10] = 0x50; // push %rax
aIp[11] = 0xC3; // ret
}
/* static */ void Assembler::PatchClobber(uint8_t* aIp) {
aIp[0] = 0xCC; // int3
}
static uint8_t* PageStart(uint8_t* aPtr) {
static_assert(sizeof(size_t) == sizeof(uintptr_t), "Unsupported Platform");
return reinterpret_cast<uint8_t*>(reinterpret_cast<size_t>(aPtr) &
~(PageSize - 1));
}
void UnprotectExecutableMemory(uint8_t* aAddress, size_t aSize) {
MOZ_ASSERT(aSize);
uint8_t* pageStart = PageStart(aAddress);
uint8_t* pageEnd = PageStart(aAddress + aSize - 1) + PageSize;
int ret = mprotect(pageStart, pageEnd - pageStart,
PROT_READ | PROT_EXEC | PROT_WRITE);
MOZ_RELEASE_ASSERT(ret >= 0);
}
} // namespace recordreplay
} // namespace mozilla