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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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 "jit/Disassembler.h"
#include "jit/x86-shared/Encoding-x86-shared.h"
using namespace js;
using namespace js::jit;
using namespace js::jit::X86Encoding;
using namespace js::jit::Disassembler;
MOZ_COLD static bool REX_W(uint8_t rex) { return (rex >> 3) & 0x1; }
MOZ_COLD static bool REX_R(uint8_t rex) { return (rex >> 2) & 0x1; }
MOZ_COLD static bool REX_X(uint8_t rex) { return (rex >> 1) & 0x1; }
MOZ_COLD static bool REX_B(uint8_t rex) { return (rex >> 0) & 0x1; }
MOZ_COLD static uint8_t
MakeREXFlags(bool w, bool r, bool x, bool b)
{
uint8_t rex = (w << 3) | (r << 2) | (x << 1) | (b << 0);
MOZ_RELEASE_ASSERT(REX_W(rex) == w);
MOZ_RELEASE_ASSERT(REX_R(rex) == r);
MOZ_RELEASE_ASSERT(REX_X(rex) == x);
MOZ_RELEASE_ASSERT(REX_B(rex) == b);
return rex;
}
MOZ_COLD static ModRmMode
ModRM_Mode(uint8_t modrm)
{
return ModRmMode((modrm >> 6) & 0x3);
}
MOZ_COLD static uint8_t
ModRM_Reg(uint8_t modrm)
{
return (modrm >> 3) & 0x7;
}
MOZ_COLD static uint8_t
ModRM_RM(uint8_t modrm)
{
return (modrm >> 0) & 0x7;
}
MOZ_COLD static bool
ModRM_hasSIB(uint8_t modrm)
{
return ModRM_Mode(modrm) != ModRmRegister && ModRM_RM(modrm) == hasSib;
}
MOZ_COLD static bool
ModRM_hasDisp8(uint8_t modrm)
{
return ModRM_Mode(modrm) == ModRmMemoryDisp8;
}
MOZ_COLD static bool
ModRM_hasRIP(uint8_t modrm)
{
#ifdef JS_CODEGEN_X64
return ModRM_Mode(modrm) == ModRmMemoryNoDisp && ModRM_RM(modrm) == noBase;
#else
return false;
#endif
}
MOZ_COLD static bool
ModRM_hasDisp32(uint8_t modrm)
{
return ModRM_Mode(modrm) == ModRmMemoryDisp32 ||
ModRM_hasRIP(modrm);
}
MOZ_COLD static uint8_t
SIB_SS(uint8_t sib)
{
return (sib >> 6) & 0x3;
}
MOZ_COLD static uint8_t
SIB_Index(uint8_t sib)
{
return (sib >> 3) & 0x7;
}
MOZ_COLD static uint8_t
SIB_Base(uint8_t sib)
{
return (sib >> 0) & 0x7;
}
MOZ_COLD static bool
SIB_hasRIP(uint8_t sib)
{
return SIB_Base(sib) == noBase && SIB_Index(sib) == noIndex;
}
MOZ_COLD static bool
HasRIP(uint8_t modrm, uint8_t sib, uint8_t rex)
{
return ModRM_hasRIP(modrm) && SIB_hasRIP(sib);
}
MOZ_COLD static bool
HasDisp8(uint8_t modrm)
{
return ModRM_hasDisp8(modrm);
}
MOZ_COLD static bool
HasDisp32(uint8_t modrm, uint8_t sib)
{
return ModRM_hasDisp32(modrm) ||
(SIB_Base(sib) == noBase &&
SIB_Index(sib) == noIndex &&
ModRM_Mode(modrm) == ModRmMemoryNoDisp);
}
MOZ_COLD static uint32_t
Reg(uint8_t modrm, uint8_t sib, uint8_t rex)
{
return ModRM_Reg(modrm) | (REX_R(rex) << 3);
}
MOZ_COLD static bool
HasBase(uint8_t modrm, uint8_t sib)
{
return !ModRM_hasSIB(modrm) ||
SIB_Base(sib) != noBase ||
SIB_Index(sib) != noIndex ||
ModRM_Mode(modrm) != ModRmMemoryNoDisp;
}
MOZ_COLD static RegisterID
DecodeBase(uint8_t modrm, uint8_t sib, uint8_t rex)
{
return HasBase(modrm, sib)
? RegisterID((ModRM_hasSIB(modrm) ? SIB_Base(sib) : ModRM_RM(modrm)) | (REX_B(rex) << 3))
: invalid_reg;
}
MOZ_COLD static RegisterID
DecodeIndex(uint8_t modrm, uint8_t sib, uint8_t rex)
{
RegisterID index = RegisterID(SIB_Index(sib) | (REX_X(rex) << 3));
return ModRM_hasSIB(modrm) && index != noIndex ? index : invalid_reg;
}
MOZ_COLD static uint32_t
DecodeScale(uint8_t modrm, uint8_t sib, uint8_t rex)
{
return ModRM_hasSIB(modrm) ? SIB_SS(sib) : 0;
}
#define PackOpcode(op0, op1, op2) ((op0) | ((op1) << 8) | ((op2) << 16))
#define Pack2ByteOpcode(op1) PackOpcode(OP_2BYTE_ESCAPE, op1, 0)
#define Pack3ByteOpcode(op1, op2) PackOpcode(OP_2BYTE_ESCAPE, op1, op2)
uint8_t*
js::jit::Disassembler::DisassembleHeapAccess(uint8_t* ptr, HeapAccess* access)
{
VexOperandType type = VEX_PS;
uint32_t opcode = OP_HLT;
uint8_t modrm = 0;
uint8_t sib = 0;
uint8_t rex = 0;
int32_t disp = 0;
int32_t imm = 0;
bool haveImm = false;
int opsize = 4;
// Legacy prefixes
switch (*ptr) {
case PRE_LOCK:
case PRE_PREDICT_BRANCH_NOT_TAKEN: // (obsolete), aka %cs
case 0x3E: // aka predict-branch-taken (obsolete)
case 0x36: // %ss
case 0x26: // %es
case 0x64: // %fs
case 0x65: // %gs
case 0x67: // address-size override
MOZ_CRASH("Unable to disassemble instruction");
case PRE_SSE_F2: // aka REPNZ/REPNE
type = VEX_SD;
ptr++;
break;
case PRE_SSE_F3: // aka REP/REPE/REPZ
type = VEX_SS;
ptr++;
break;
case PRE_SSE_66: // aka PRE_OPERAND_SIZE
type = VEX_PD;
opsize = 2;
ptr++;
break;
default:
break;
}
// REX and VEX prefixes
{
int x = 0, b = 0, m = 1, w = 0;
int r, l, p;
switch (*ptr) {
#ifdef JS_CODEGEN_X64
case PRE_REX | 0x0: case PRE_REX | 0x1: case PRE_REX | 0x2: case PRE_REX | 0x3:
case PRE_REX | 0x4: case PRE_REX | 0x5: case PRE_REX | 0x6: case PRE_REX | 0x7:
case PRE_REX | 0x8: case PRE_REX | 0x9: case PRE_REX | 0xa: case PRE_REX | 0xb:
case PRE_REX | 0xc: case PRE_REX | 0xd: case PRE_REX | 0xe: case PRE_REX | 0xf:
rex = *ptr++ & 0xf;
goto rex_done;
#endif
case PRE_VEX_C4: {
if (type != VEX_PS)
MOZ_CRASH("Unable to disassemble instruction");
++ptr;
uint8_t c4a = *ptr++ ^ 0xe0;
uint8_t c4b = *ptr++ ^ 0x78;
r = (c4a >> 7) & 0x1;
x = (c4a >> 6) & 0x1;
b = (c4a >> 5) & 0x1;
m = (c4a >> 0) & 0x1f;
w = (c4b >> 7) & 0x1;
l = (c4b >> 2) & 0x1;
p = (c4b >> 0) & 0x3;
break;
}
case PRE_VEX_C5: {
if (type != VEX_PS)
MOZ_CRASH("Unable to disassemble instruction");
++ptr;
uint8_t c5 = *ptr++ ^ 0xf8;
r = (c5 >> 7) & 0x1;
l = (c5 >> 2) & 0x1;
p = (c5 >> 0) & 0x3;
break;
}
default:
goto rex_done;
}
type = VexOperandType(p);
rex = MakeREXFlags(w, r, x, b);
switch (m) {
case 0x1:
opcode = Pack2ByteOpcode(*ptr++);
goto opcode_done;
case 0x2:
opcode = Pack3ByteOpcode(ESCAPE_38, *ptr++);
goto opcode_done;
case 0x3:
opcode = Pack3ByteOpcode(ESCAPE_3A, *ptr++);
goto opcode_done;
default:
MOZ_CRASH("Unable to disassemble instruction");
}
if (l != 0) // 256-bit SIMD
MOZ_CRASH("Unable to disassemble instruction");
}
rex_done:;
if (REX_W(rex))
opsize = 8;
// Opcode.
opcode = *ptr++;
switch (opcode) {
#ifdef JS_CODEGEN_X64
case OP_PUSH_EAX + 0: case OP_PUSH_EAX + 1: case OP_PUSH_EAX + 2: case OP_PUSH_EAX + 3:
case OP_PUSH_EAX + 4: case OP_PUSH_EAX + 5: case OP_PUSH_EAX + 6: case OP_PUSH_EAX + 7:
case OP_POP_EAX + 0: case OP_POP_EAX + 1: case OP_POP_EAX + 2: case OP_POP_EAX + 3:
case OP_POP_EAX + 4: case OP_POP_EAX + 5: case OP_POP_EAX + 6: case OP_POP_EAX + 7:
case OP_PUSH_Iz:
case OP_PUSH_Ib:
opsize = 8;
break;
#endif
case OP_2BYTE_ESCAPE:
opcode |= *ptr << 8;
switch (*ptr++) {
case ESCAPE_38:
case ESCAPE_3A:
opcode |= *ptr++ << 16;
break;
default:
break;
}
break;
default:
break;
}
opcode_done:;
// ModR/M
modrm = *ptr++;
// SIB
if (ModRM_hasSIB(modrm))
sib = *ptr++;
// Address Displacement
if (HasDisp8(modrm)) {
disp = int8_t(*ptr++);
} else if (HasDisp32(modrm, sib)) {
memcpy(&disp, ptr, sizeof(int32_t));
ptr += sizeof(int32_t);
}
// Immediate operand
switch (opcode) {
case OP_PUSH_Ib:
case OP_IMUL_GvEvIb:
case OP_GROUP1_EbIb:
case OP_GROUP1_EvIb:
case OP_TEST_EAXIb:
case OP_GROUP2_EvIb:
case OP_GROUP11_EvIb:
case OP_GROUP3_EbIb:
case Pack2ByteOpcode(OP2_PSHUFD_VdqWdqIb):
case Pack2ByteOpcode(OP2_PSLLD_UdqIb): // aka OP2_PSRAD_UdqIb, aka OP2_PSRLD_UdqIb
case Pack2ByteOpcode(OP2_PEXTRW_GdUdIb):
case Pack2ByteOpcode(OP2_SHUFPS_VpsWpsIb):
case Pack3ByteOpcode(ESCAPE_3A, OP3_PEXTRD_EdVdqIb):
case Pack3ByteOpcode(ESCAPE_3A, OP3_BLENDPS_VpsWpsIb):
case Pack3ByteOpcode(ESCAPE_3A, OP3_PINSRD_VdqEdIb):
// 8-bit signed immediate
imm = int8_t(*ptr++);
haveImm = true;
break;
case OP_RET_Iz:
// 16-bit unsigned immediate
memcpy(&imm, ptr, sizeof(int16_t));
ptr += sizeof(int16_t);
haveImm = true;
break;
case OP_ADD_EAXIv:
case OP_OR_EAXIv:
case OP_AND_EAXIv:
case OP_SUB_EAXIv:
case OP_XOR_EAXIv:
case OP_CMP_EAXIv:
case OP_PUSH_Iz:
case OP_IMUL_GvEvIz:
case OP_GROUP1_EvIz:
case OP_TEST_EAXIv:
case OP_MOV_EAXIv:
case OP_GROUP3_EvIz:
// 32-bit signed immediate
memcpy(&imm, ptr, sizeof(int32_t));
ptr += sizeof(int32_t);
haveImm = true;
break;
case OP_GROUP11_EvIz:
// opsize-sized signed immediate
memcpy(&imm, ptr, opsize);
imm = (imm << (32 - opsize * 8)) >> (32 - opsize * 8);
ptr += opsize;
haveImm = true;
break;
default:
break;
}
// Interpret the opcode.
if (HasRIP(modrm, sib, rex))
MOZ_CRASH("Unable to disassemble instruction");
size_t memSize = 0;
OtherOperand otherOperand(imm);
HeapAccess::Kind kind = HeapAccess::Unknown;
RegisterID gpr(RegisterID(Reg(modrm, sib, rex)));
XMMRegisterID xmm(XMMRegisterID(Reg(modrm, sib, rex)));
ComplexAddress addr(disp,
DecodeBase(modrm, sib, rex),
DecodeIndex(modrm, sib, rex),
DecodeScale(modrm, sib, rex));
switch (opcode) {
case OP_GROUP11_EvIb:
if (gpr != RegisterID(GROUP11_MOV))
MOZ_CRASH("Unable to disassemble instruction");
MOZ_RELEASE_ASSERT(haveImm);
memSize = 1;
kind = HeapAccess::Store;
break;
case OP_GROUP11_EvIz:
if (gpr != RegisterID(GROUP11_MOV))
MOZ_CRASH("Unable to disassemble instruction");
MOZ_RELEASE_ASSERT(haveImm);
memSize = opsize;
kind = HeapAccess::Store;
break;
case OP_MOV_GvEv:
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = opsize;
kind = HeapAccess::Load;
break;
case OP_MOV_GvEb:
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = 1;
kind = HeapAccess::Load;
break;
case OP_MOV_EvGv:
if (!haveImm)
otherOperand = OtherOperand(gpr);
memSize = opsize;
kind = HeapAccess::Store;
break;
case OP_MOV_EbGv:
if (!haveImm)
otherOperand = OtherOperand(gpr);
memSize = 1;
kind = HeapAccess::Store;
break;
case Pack2ByteOpcode(OP2_MOVZX_GvEb):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = 1;
kind = HeapAccess::Load;
break;
case Pack2ByteOpcode(OP2_MOVZX_GvEw):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = 2;
kind = HeapAccess::Load;
break;
case Pack2ByteOpcode(OP2_MOVSX_GvEb):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = 1;
kind = opsize == 8 ? HeapAccess::LoadSext64 : HeapAccess::LoadSext32;
break;
case Pack2ByteOpcode(OP2_MOVSX_GvEw):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = 2;
kind = opsize == 8 ? HeapAccess::LoadSext64 : HeapAccess::LoadSext32;
break;
#ifdef JS_CODEGEN_X64
case OP_MOVSXD_GvEv:
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(gpr);
memSize = 4;
kind = HeapAccess::LoadSext64;
break;
#endif // JS_CODEGEN_X64
case Pack2ByteOpcode(OP2_MOVDQ_VdqWdq): // aka OP2_MOVDQ_VsdWsd
case Pack2ByteOpcode(OP2_MOVAPS_VsdWsd):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
memSize = 16;
kind = HeapAccess::Load;
break;
case Pack2ByteOpcode(OP2_MOVSD_VsdWsd): // aka OP2_MOVPS_VpsWps
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
switch (type) {
case VEX_SS: memSize = 4; break;
case VEX_SD: memSize = 8; break;
case VEX_PS:
case VEX_PD: memSize = 16; break;
default: MOZ_CRASH("Unexpected VEX type");
}
kind = HeapAccess::Load;
break;
case Pack2ByteOpcode(OP2_MOVDQ_WdqVdq):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
memSize = 16;
kind = HeapAccess::Store;
break;
case Pack2ByteOpcode(OP2_MOVSD_WsdVsd): // aka OP2_MOVPS_WpsVps
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
switch (type) {
case VEX_SS: memSize = 4; break;
case VEX_SD: memSize = 8; break;
case VEX_PS:
case VEX_PD: memSize = 16; break;
default: MOZ_CRASH("Unexpected VEX type");
}
kind = HeapAccess::Store;
break;
case Pack2ByteOpcode(OP2_MOVD_VdEd):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
switch (type) {
case VEX_PD: memSize = 4; break;
default: MOZ_CRASH("Unexpected VEX type");
}
kind = HeapAccess::Load;
break;
case Pack2ByteOpcode(OP2_MOVQ_WdVd):
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
switch (type) {
case VEX_PD: memSize = 8; break;
default: MOZ_CRASH("Unexpected VEX type");
}
kind = HeapAccess::Store;
break;
case Pack2ByteOpcode(OP2_MOVD_EdVd): // aka OP2_MOVQ_VdWd
MOZ_RELEASE_ASSERT(!haveImm);
otherOperand = OtherOperand(xmm);
switch (type) {
case VEX_SS: memSize = 8; kind = HeapAccess::Load; break;
case VEX_PD: memSize = 4; kind = HeapAccess::Store; break;
default: MOZ_CRASH("Unexpected VEX type");
}
break;
default:
MOZ_CRASH("Unable to disassemble instruction");
}
*access = HeapAccess(kind, memSize, addr, otherOperand);
return ptr;
}
#ifdef DEBUG
void
js::jit::Disassembler::DumpHeapAccess(const HeapAccess& access)
{
switch (access.kind()) {
case HeapAccess::Store: fprintf(stderr, "store"); break;
case HeapAccess::Load: fprintf(stderr, "load"); break;
case HeapAccess::LoadSext32: fprintf(stderr, "loadSext32"); break;
case HeapAccess::LoadSext64: fprintf(stderr, "loadSext64"); break;
default: fprintf(stderr, "unknown"); break;
}
fprintf(stderr, "%u ", unsigned(access.size()));
switch (access.otherOperand().kind()) {
case OtherOperand::Imm:
fprintf(stderr, "imm %d", access.otherOperand().imm());
break;
case OtherOperand::GPR:
fprintf(stderr, "gpr %s", X86Encoding::GPRegName(access.otherOperand().gpr()));
break;
case OtherOperand::FPR:
fprintf(stderr, "fpr %s", X86Encoding::XMMRegName(access.otherOperand().fpr()));
break;
default: fprintf(stderr, "unknown");
}
fprintf(stderr, " @ ");
if (access.address().isPCRelative()) {
fprintf(stderr, MEM_o32r " ", ADDR_o32r(access.address().disp()));
} else if (access.address().hasIndex()) {
if (access.address().hasBase()) {
fprintf(stderr, MEM_obs " ",
ADDR_obs(access.address().disp(), access.address().base(),
access.address().index(), access.address().scale()));
} else {
fprintf(stderr, MEM_os " ",
ADDR_os(access.address().disp(),
access.address().index(), access.address().scale()));
}
} else if (access.address().hasBase()) {
fprintf(stderr, MEM_ob " ", ADDR_ob(access.address().disp(), access.address().base()));
} else {
fprintf(stderr, MEM_o " ", ADDR_o(access.address().disp()));
}
fprintf(stderr, "\n");
}
#endif
|