Coverage Report

Created: 2026-03-13 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/X86/X86DisassemblerDecoder.c
Line
Count
Source
1
/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
2
 *
3
 *                     The LLVM Compiler Infrastructure
4
 *
5
 * This file is distributed under the University of Illinois Open Source
6
 * License. See LICENSE.TXT for details.
7
 *
8
 *===----------------------------------------------------------------------===*
9
 *
10
 * This file is part of the X86 Disassembler.
11
 * It contains the implementation of the instruction decoder.
12
 * Documentation for the disassembler can be found in X86Disassembler.h.
13
 *
14
 *===----------------------------------------------------------------------===*/
15
16
/* Capstone Disassembly Engine */
17
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
18
19
#ifdef CAPSTONE_HAS_X86
20
21
#include <stdarg.h> /* for va_*()       */
22
#if defined(CAPSTONE_HAS_OSXKERNEL)
23
#include <libkern/libkern.h>
24
#else
25
#include <stdlib.h> /* for exit()       */
26
#endif
27
28
#include <string.h>
29
30
#include "../../cs_priv.h"
31
#include "../../utils.h"
32
33
#include "X86DisassemblerDecoder.h"
34
#include "X86Mapping.h"
35
36
/// Specifies whether a ModR/M byte is needed and (if so) which
37
/// instruction each possible value of the ModR/M byte corresponds to.  Once
38
/// this information is known, we have narrowed down to a single instruction.
39
struct ModRMDecision {
40
  uint8_t modrm_type;
41
  uint16_t instructionIDs;
42
};
43
44
/// Specifies which set of ModR/M->instruction tables to look at
45
/// given a particular opcode.
46
struct OpcodeDecision {
47
  struct ModRMDecision modRMDecisions[256];
48
};
49
50
/// Specifies which opcode->instruction tables to look at given
51
/// a particular context (set of attributes).  Since there are many possible
52
/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
53
/// applies given a specific set of attributes.  Hence there are only IC_max
54
/// entries in this table, rather than 2^(ATTR_max).
55
struct ContextDecision {
56
  struct OpcodeDecision opcodeDecisions[IC_max];
57
};
58
59
#ifdef CAPSTONE_X86_REDUCE
60
#include "X86GenDisassemblerTables_reduce.inc"
61
#include "X86GenDisassemblerTables_reduce2.inc"
62
#include "X86Lookup16_reduce.inc"
63
#else
64
#include "X86GenDisassemblerTables.inc"
65
#include "X86GenDisassemblerTables2.inc"
66
#include "X86Lookup16.inc"
67
#endif
68
69
/*
70
 * contextForAttrs - Client for the instruction context table.  Takes a set of
71
 *   attributes and returns the appropriate decode context.
72
 *
73
 * @param attrMask  - Attributes, from the enumeration attributeBits.
74
 * @return          - The InstructionContext to use when looking up an
75
 *                    an instruction with these attributes.
76
 */
77
static InstructionContext contextForAttrs(uint16_t attrMask)
78
1.87M
{
79
1.87M
  return CONTEXTS_SYM[attrMask];
80
1.87M
}
81
82
/*
83
 * modRMRequired - Reads the appropriate instruction table to determine whether
84
 *   the ModR/M byte is required to decode a particular instruction.
85
 *
86
 * @param type        - The opcode type (i.e., how many bytes it has).
87
 * @param insnContext - The context for the instruction, as returned by
88
 *                      contextForAttrs.
89
 * @param opcode      - The last byte of the instruction's opcode, not counting
90
 *                      ModR/M extensions and escapes.
91
 * @return            - true if the ModR/M byte is required, false otherwise.
92
 */
93
static int modRMRequired(OpcodeType type, InstructionContext insnContext,
94
       uint16_t opcode)
95
1.87M
{
96
1.87M
  const struct OpcodeDecision *decision = NULL;
97
1.87M
  const uint8_t *indextable = NULL;
98
1.87M
  unsigned int index;
99
100
1.87M
  switch (type) {
101
0
  default:
102
0
    break;
103
1.59M
  case ONEBYTE:
104
1.59M
    decision = ONEBYTE_SYM;
105
1.59M
    indextable = index_x86DisassemblerOneByteOpcodes;
106
1.59M
    break;
107
136k
  case TWOBYTE:
108
136k
    decision = TWOBYTE_SYM;
109
136k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
136k
    break;
111
46.8k
  case THREEBYTE_38:
112
46.8k
    decision = THREEBYTE38_SYM;
113
46.8k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
46.8k
    break;
115
67.7k
  case THREEBYTE_3A:
116
67.7k
    decision = THREEBYTE3A_SYM;
117
67.7k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
67.7k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
19.2k
  case XOP8_MAP:
121
19.2k
    decision = XOP8_MAP_SYM;
122
19.2k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
19.2k
    break;
124
2.02k
  case XOP9_MAP:
125
2.02k
    decision = XOP9_MAP_SYM;
126
2.02k
    indextable = index_x86DisassemblerXOP9Opcodes;
127
2.02k
    break;
128
1.48k
  case XOPA_MAP:
129
1.48k
    decision = XOPA_MAP_SYM;
130
1.48k
    indextable = index_x86DisassemblerXOPAOpcodes;
131
1.48k
    break;
132
562
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
562
    return true;
135
1.87M
#endif
136
1.87M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.87M
  index = indextable[insnContext];
140
1.87M
  if (index)
141
1.86M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.86M
           MODRM_ONEENTRY;
143
7.57k
  else
144
7.57k
    return false;
145
1.87M
}
146
147
/*
148
 * decode - Reads the appropriate instruction table to obtain the unique ID of
149
 *   an instruction.
150
 *
151
 * @param type        - See modRMRequired().
152
 * @param insnContext - See modRMRequired().
153
 * @param opcode      - See modRMRequired().
154
 * @param modRM       - The ModR/M byte if required, or any value if not.
155
 * @return            - The UID of the instruction, or 0 on failure.
156
 */
157
static InstrUID decode(OpcodeType type, InstructionContext insnContext,
158
           uint8_t opcode, uint8_t modRM)
159
1.87M
{
160
1.87M
  const struct ModRMDecision *dec = NULL;
161
1.87M
  unsigned int index;
162
1.87M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.87M
  switch (type) {
165
0
  default:
166
0
    break; // never reach
167
1.59M
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
1.59M
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
1.59M
    if (index)
171
1.59M
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
264
    else
173
264
      dec = &emptyDecision.modRMDecisions[opcode];
174
1.59M
    break;
175
136k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
136k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
136k
    if (index)
179
134k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
2.12k
    else
181
2.12k
      dec = &emptyDecision.modRMDecisions[opcode];
182
136k
    break;
183
46.7k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
46.7k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
46.7k
    if (index)
187
46.0k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
691
    else
189
691
      dec = &emptyDecision.modRMDecisions[opcode];
190
46.7k
    break;
191
67.7k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
67.7k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
67.7k
    if (index)
195
67.5k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
234
    else
197
234
      dec = &emptyDecision.modRMDecisions[opcode];
198
67.7k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
19.2k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
19.2k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
19.2k
    if (index)
204
15.6k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
3.55k
    else
206
3.55k
      dec = &emptyDecision.modRMDecisions[opcode];
207
19.2k
    break;
208
2.02k
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
2.02k
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
2.02k
    if (index)
212
1.70k
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
319
    else
214
319
      dec = &emptyDecision.modRMDecisions[opcode];
215
2.02k
    break;
216
1.48k
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
1.48k
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
1.48k
    if (index)
220
1.10k
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
379
    else
222
379
      dec = &emptyDecision.modRMDecisions[opcode];
223
1.48k
    break;
224
562
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
562
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
562
    if (index)
228
356
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
356
               .modRMDecisions[opcode];
230
206
    else
231
206
      dec = &emptyDecision.modRMDecisions[opcode];
232
562
    break;
233
1.87M
#endif
234
1.87M
  }
235
236
1.87M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
879k
  case MODRM_ONEENTRY:
241
879k
    return modRMTable[dec->instructionIDs];
242
754k
  case MODRM_SPLITRM:
243
754k
    if (modFromModRM(modRM) == 0x3)
244
152k
      return modRMTable[dec->instructionIDs + 1];
245
601k
    return modRMTable[dec->instructionIDs];
246
198k
  case MODRM_SPLITREG:
247
198k
    if (modFromModRM(modRM) == 0x3)
248
68.8k
      return modRMTable[dec->instructionIDs +
249
68.8k
            ((modRM & 0x38) >> 3) + 8];
250
130k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
37.0k
  case MODRM_SPLITMISC:
252
37.0k
    if (modFromModRM(modRM) == 0x3)
253
12.0k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
12.0k
            8];
255
25.0k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.87M
  }
259
1.87M
}
260
261
/*
262
 * specifierForUID - Given a UID, returns the name and operand specification for
263
 *   that instruction.
264
 *
265
 * @param uid - The unique ID for the instruction.  This should be returned by
266
 *              decode(); specifierForUID will not check bounds.
267
 * @return    - A pointer to the specification for that instruction.
268
 */
269
static const struct InstructionSpecifier *specifierForUID(InstrUID uid)
270
1.56M
{
271
1.56M
  return &INSTRUCTIONS_SYM[uid];
272
1.56M
}
273
274
/*
275
 * consumeByte - Uses the reader function provided by the user to consume one
276
 *   byte from the instruction's memory and advance the cursor.
277
 *
278
 * @param insn  - The instruction with the reader function to use.  The cursor
279
 *                for this instruction is advanced.
280
 * @param byte  - A pointer to a pre-allocated memory buffer to be populated
281
 *                with the data read.
282
 * @return      - 0 if the read was successful; nonzero otherwise.
283
 */
284
static int consumeByte(struct InternalInstruction *insn, uint8_t *byte)
285
4.99M
{
286
4.99M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
4.99M
  if (!ret)
289
4.99M
    ++(insn->readerCursor);
290
291
4.99M
  return ret;
292
4.99M
}
293
294
/*
295
 * lookAtByte - Like consumeByte, but does not advance the cursor.
296
 *
297
 * @param insn  - See consumeByte().
298
 * @param byte  - See consumeByte().
299
 * @return      - See consumeByte().
300
 */
301
static int lookAtByte(struct InternalInstruction *insn, uint8_t *byte)
302
478k
{
303
478k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
478k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
1.75M
{
308
1.75M
  insn->readerCursor--;
309
1.75M
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
275k
  { \
314
275k
    type combined = 0; \
315
275k
    unsigned offset; \
316
910k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
637k
      uint8_t byte; \
318
637k
      int ret = insn->reader(insn->readerArg, &byte, \
319
637k
                 insn->readerCursor + offset); \
320
637k
      if (ret) \
321
637k
        return ret; \
322
637k
      combined = combined | \
323
635k
           ((uint64_t)byte << (offset * 8)); \
324
635k
    } \
325
275k
    *ptr = combined; \
326
273k
    insn->readerCursor += sizeof(type); \
327
273k
    return 0; \
328
275k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
106k
  { \
314
106k
    type combined = 0; \
315
106k
    unsigned offset; \
316
212k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
106k
      uint8_t byte; \
318
106k
      int ret = insn->reader(insn->readerArg, &byte, \
319
106k
                 insn->readerCursor + offset); \
320
106k
      if (ret) \
321
106k
        return ret; \
322
106k
      combined = combined | \
323
105k
           ((uint64_t)byte << (offset * 8)); \
324
105k
    } \
325
106k
    *ptr = combined; \
326
105k
    insn->readerCursor += sizeof(type); \
327
105k
    return 0; \
328
106k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
28.6k
  { \
314
28.6k
    type combined = 0; \
315
28.6k
    unsigned offset; \
316
85.9k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
57.3k
      uint8_t byte; \
318
57.3k
      int ret = insn->reader(insn->readerArg, &byte, \
319
57.3k
                 insn->readerCursor + offset); \
320
57.3k
      if (ret) \
321
57.3k
        return ret; \
322
57.3k
      combined = combined | \
323
57.2k
           ((uint64_t)byte << (offset * 8)); \
324
57.2k
    } \
325
28.6k
    *ptr = combined; \
326
28.5k
    insn->readerCursor += sizeof(type); \
327
28.5k
    return 0; \
328
28.6k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
39.6k
  { \
314
39.6k
    type combined = 0; \
315
39.6k
    unsigned offset; \
316
197k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
157k
      uint8_t byte; \
318
157k
      int ret = insn->reader(insn->readerArg, &byte, \
319
157k
                 insn->readerCursor + offset); \
320
157k
      if (ret) \
321
157k
        return ret; \
322
157k
      combined = combined | \
323
157k
           ((uint64_t)byte << (offset * 8)); \
324
157k
    } \
325
39.6k
    *ptr = combined; \
326
39.2k
    insn->readerCursor += sizeof(type); \
327
39.2k
    return 0; \
328
39.6k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
55.1k
  { \
314
55.1k
    type combined = 0; \
315
55.1k
    unsigned offset; \
316
164k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
110k
      uint8_t byte; \
318
110k
      int ret = insn->reader(insn->readerArg, &byte, \
319
110k
                 insn->readerCursor + offset); \
320
110k
      if (ret) \
321
110k
        return ret; \
322
110k
      combined = combined | \
323
109k
           ((uint64_t)byte << (offset * 8)); \
324
109k
    } \
325
55.1k
    *ptr = combined; \
326
54.8k
    insn->readerCursor += sizeof(type); \
327
54.8k
    return 0; \
328
55.1k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
39.6k
  { \
314
39.6k
    type combined = 0; \
315
39.6k
    unsigned offset; \
316
196k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
157k
      uint8_t byte; \
318
157k
      int ret = insn->reader(insn->readerArg, &byte, \
319
157k
                 insn->readerCursor + offset); \
320
157k
      if (ret) \
321
157k
        return ret; \
322
157k
      combined = combined | \
323
157k
           ((uint64_t)byte << (offset * 8)); \
324
157k
    } \
325
39.6k
    *ptr = combined; \
326
39.1k
    insn->readerCursor += sizeof(type); \
327
39.1k
    return 0; \
328
39.6k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
6.05k
  { \
314
6.05k
    type combined = 0; \
315
6.05k
    unsigned offset; \
316
53.9k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
47.9k
      uint8_t byte; \
318
47.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
47.9k
                 insn->readerCursor + offset); \
320
47.9k
      if (ret) \
321
47.9k
        return ret; \
322
47.9k
      combined = combined | \
323
47.8k
           ((uint64_t)byte << (offset * 8)); \
324
47.8k
    } \
325
6.05k
    *ptr = combined; \
326
5.94k
    insn->readerCursor += sizeof(type); \
327
5.94k
    return 0; \
328
6.05k
  }
329
330
/*
331
 * consume* - Use the reader function provided by the user to consume data
332
 *   values of various sizes from the instruction's memory and advance the
333
 *   cursor appropriately.  These readers perform endian conversion.
334
 *
335
 * @param insn    - See consumeByte().
336
 * @param ptr     - A pointer to a pre-allocated memory of appropriate size to
337
 *                  be populated with the data read.
338
 * @return        - See consumeByte().
339
 */
340
CONSUME_FUNC(consumeInt8, int8_t)
341
CONSUME_FUNC(consumeInt16, int16_t)
342
CONSUME_FUNC(consumeInt32, int32_t)
343
CONSUME_FUNC(consumeUInt16, uint16_t)
344
CONSUME_FUNC(consumeUInt32, uint32_t)
345
CONSUME_FUNC(consumeUInt64, uint64_t)
346
347
static bool isREX(struct InternalInstruction *insn, uint8_t prefix)
348
1.38M
{
349
1.38M
  if (insn->mode == MODE_64BIT)
350
477k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
911k
  return false;
353
1.38M
}
354
355
/*
356
 * setGroup0Prefix - Updates the decoded instruction according to the group 0-prefix.
357
 *
358
 * @param insn      - The instruction to be updated.
359
 * @param prefix    - The group 0 prefix that is present.
360
 */
361
static void setGroup0Prefix(struct InternalInstruction *insn, uint8_t prefix)
362
77.6k
{
363
77.6k
  switch (prefix) {
364
29.6k
  case 0xf0: // LOCK
365
29.6k
    insn->hasLockPrefix = true;
366
29.6k
    insn->repeatPrefix = 0;
367
29.6k
    break;
368
369
25.9k
  case 0xf2: // REPNE/REPNZ
370
47.9k
  case 0xf3: // REP or REPE/REPZ
371
47.9k
    insn->repeatPrefix = prefix;
372
47.9k
    insn->hasLockPrefix = false;
373
47.9k
    break;
374
77.6k
  }
375
77.6k
}
376
377
/*
378
 * setSegmentOverride - Overrides an instruction's prefix1 based on CPU mode.
379
 *
380
 * @param insn      - The instruction to be overridden.
381
 * @param prefix    - The segment override to use.
382
 * @param byte      - The current decoded prefix byte. Must be a segment override.
383
 */
384
static void setSegmentOverride(struct InternalInstruction *insn,
385
             SegmentOverride prefix, uint8_t byte)
386
24.3k
{
387
  // In 32-bit or 16-bit mode all segment override prefixes are used.
388
24.3k
  if (insn->mode != MODE_64BIT) {
389
13.9k
    insn->segmentOverride = prefix;
390
13.9k
    insn->prefix1 = byte;
391
13.9k
    return;
392
13.9k
  }
393
394
  // In 64-bit mode, the ES/CS/SS/DS segment overrides should be ignored.
395
  // In the case there are multiple segment overrides, do not override
396
  // an existing FS or GS segment prefix.
397
10.3k
  switch (insn->prefix1) {
398
1.53k
  case 0x64: // FS
399
2.22k
  case 0x65: // GS
400
2.22k
    return;
401
10.3k
  }
402
403
  // If the proposed override is for FS or GS, mark it overridden.
404
  // All other segment prefixes are ignored.
405
8.14k
  switch (byte) {
406
1.00k
  case 0x64: // FS
407
3.01k
  case 0x65: // GS
408
3.01k
    insn->segmentOverride = prefix;
409
3.01k
    break;
410
8.14k
  }
411
412
  // `prefix1` may later be used to decode the `notrack` prefix.
413
  // The `notrack` prefix reuses the DS segment override, so we
414
  // need to store the prefix even if it is ignored for the segment overrides.
415
8.14k
  insn->prefix1 = byte;
416
8.14k
}
417
418
/*
419
 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
420
 *   instruction as having them.  Also sets the instruction's default operand,
421
 *   address, and other relevant data sizes to report operands correctly.
422
 *
423
 * @param insn  - The instruction whose prefixes are to be read.
424
 * @return      - 0 if the instruction could be read until the end of the prefix
425
 *                bytes, and no prefixes conflicted; nonzero otherwise.
426
 */
427
static int readPrefixes(struct InternalInstruction *insn)
428
1.36M
{
429
1.36M
  bool isPrefix = true;
430
1.36M
  uint8_t byte = 0;
431
1.36M
  uint8_t nextByte;
432
433
2.99M
  while (isPrefix) {
434
1.62M
    if (insn->mode == MODE_64BIT) {
435
      // eliminate consecutive redundant REX bytes in front
436
573k
      if (consumeByte(insn, &byte))
437
160
        return -1;
438
439
573k
      if ((byte & 0xf0) == 0x40) {
440
104k
        while (true) {
441
104k
          if (lookAtByte(
442
104k
                insn,
443
104k
                &byte)) // out of input code
444
132
            return -1;
445
104k
          if ((byte & 0xf0) == 0x40) {
446
            // another REX prefix, but we only remember the last one
447
14.6k
            if (consumeByte(insn, &byte))
448
0
              return -1;
449
14.6k
          } else
450
89.8k
            break;
451
104k
        }
452
453
        // recover the last REX byte if next byte is not a legacy prefix
454
89.8k
        switch (byte) {
455
1.72k
        case 0xf2: /* REPNE/REPNZ */
456
3.04k
        case 0xf3: /* REP or REPE/REPZ */
457
5.09k
        case 0xf0: /* LOCK */
458
5.44k
        case 0x2e: /* CS segment override -OR- Branch not taken */
459
6.14k
        case 0x36: /* SS segment override -OR- Branch taken */
460
6.33k
        case 0x3e: /* DS segment override */
461
6.76k
        case 0x26: /* ES segment override */
462
7.12k
        case 0x64: /* FS segment override */
463
7.56k
        case 0x65: /* GS segment override */
464
8.79k
        case 0x66: /* Operand-size override */
465
9.21k
        case 0x67: /* Address-size override */
466
9.21k
          break;
467
80.6k
        default: /* Not a prefix byte */
468
80.6k
          unconsumeByte(insn);
469
80.6k
          break;
470
89.8k
        }
471
483k
      } else {
472
483k
        unconsumeByte(insn);
473
483k
      }
474
573k
    }
475
476
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
477
1.62M
    if (consumeByte(insn, &byte))
478
181
      return -1;
479
480
1.62M
    if (insn->readerCursor - 1 == insn->startLocation &&
481
1.35M
        (byte == 0xf2 || byte == 0xf3)) {
482
      // prefix requires next byte
483
82.9k
      if (lookAtByte(insn, &nextByte))
484
118
        return -1;
485
486
      /*
487
       * If the byte is 0xf2 or 0xf3, and any of the following conditions are
488
       * met:
489
       * - it is followed by a LOCK (0xf0) prefix
490
       * - it is followed by an xchg instruction
491
       * then it should be disassembled as a xacquire/xrelease not repne/rep.
492
       */
493
82.8k
      if (((nextByte == 0xf0) ||
494
80.3k
           ((nextByte & 0xfe) == 0x86 ||
495
78.6k
            (nextByte & 0xf8) == 0x90))) {
496
5.18k
        insn->xAcquireRelease = byte;
497
5.18k
      }
498
499
      /*
500
       * Also if the byte is 0xf3, and the following condition is met:
501
       * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
502
       *                       "mov mem, imm" (opcode 0xc6/0xc7) instructions.
503
       * then it should be disassembled as an xrelease not rep.
504
       */
505
82.8k
      if (byte == 0xf3 &&
506
39.9k
          (nextByte == 0x88 || nextByte == 0x89 ||
507
39.1k
           nextByte == 0xc6 || nextByte == 0xc7)) {
508
1.27k
        insn->xAcquireRelease = byte;
509
1.27k
      }
510
511
82.8k
      if (isREX(insn, nextByte)) {
512
9.34k
        uint8_t nnextByte;
513
514
        // Go to REX prefix after the current one
515
9.34k
        if (consumeByte(insn, &nnextByte))
516
0
          return -1;
517
518
        // We should be able to read next byte after REX prefix
519
9.34k
        if (lookAtByte(insn, &nnextByte))
520
22
          return -1;
521
522
9.32k
        unconsumeByte(insn);
523
9.32k
      }
524
82.8k
    }
525
526
1.62M
    switch (byte) {
527
66.6k
    case 0xf0: /* LOCK */
528
122k
    case 0xf2: /* REPNE/REPNZ */
529
171k
    case 0xf3: /* REP or REPE/REPZ */
530
      // only accept the last prefix
531
171k
      setGroup0Prefix(insn, byte);
532
171k
      insn->prefix0 = byte;
533
171k
      break;
534
535
9.71k
    case 0x2e: /* CS segment override -OR- Branch not taken */
536
15.4k
    case 0x36: /* SS segment override -OR- Branch taken */
537
22.1k
    case 0x3e: /* DS segment override */
538
28.3k
    case 0x26: /* ES segment override */
539
35.6k
    case 0x64: /* FS segment override */
540
44.7k
    case 0x65: /* GS segment override */
541
44.7k
      switch (byte) {
542
9.71k
      case 0x2e:
543
9.71k
        setSegmentOverride(insn, SEG_OVERRIDE_CS, byte);
544
9.71k
        break;
545
5.77k
      case 0x36:
546
5.77k
        setSegmentOverride(insn, SEG_OVERRIDE_SS, byte);
547
5.77k
        break;
548
6.67k
      case 0x3e:
549
6.67k
        setSegmentOverride(insn, SEG_OVERRIDE_DS, byte);
550
6.67k
        break;
551
6.14k
      case 0x26:
552
6.14k
        setSegmentOverride(insn, SEG_OVERRIDE_ES, byte);
553
6.14k
        break;
554
7.38k
      case 0x64:
555
7.38k
        setSegmentOverride(insn, SEG_OVERRIDE_FS, byte);
556
7.38k
        break;
557
9.03k
      case 0x65:
558
9.03k
        setSegmentOverride(insn, SEG_OVERRIDE_GS, byte);
559
9.03k
        break;
560
0
      default:
561
        // debug("Unhandled override");
562
0
        return -1;
563
44.7k
      }
564
44.7k
      break;
565
566
44.7k
    case 0x66: /* Operand-size override */
567
32.2k
      insn->hasOpSize = true;
568
32.2k
      insn->prefix2 = byte;
569
32.2k
      break;
570
571
13.4k
    case 0x67: /* Address-size override */
572
13.4k
      insn->hasAdSize = true;
573
13.4k
      insn->prefix3 = byte;
574
13.4k
      break;
575
1.36M
    default: /* Not a prefix byte */
576
1.36M
      isPrefix = false;
577
1.36M
      break;
578
1.62M
    }
579
1.62M
  }
580
581
1.36M
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
582
583
1.36M
  if (byte == 0x62) {
584
92.7k
    uint8_t byte1, byte2;
585
586
92.7k
    if (consumeByte(insn, &byte1)) {
587
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
588
61
      return -1;
589
61
    }
590
591
92.6k
    if (lookAtByte(insn, &byte2)) {
592
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
593
89
      unconsumeByte(insn); /* unconsume byte1 */
594
89
      unconsumeByte(insn); /* unconsume byte  */
595
92.5k
    } else {
596
92.5k
      if ((insn->mode == MODE_64BIT ||
597
60.0k
           (byte1 & 0xc0) == 0xc0) &&
598
81.6k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
599
81.2k
        insn->vectorExtensionType = TYPE_EVEX;
600
81.2k
      } else {
601
11.2k
        unconsumeByte(insn); /* unconsume byte1 */
602
11.2k
        unconsumeByte(insn); /* unconsume byte  */
603
11.2k
      }
604
92.5k
    }
605
606
92.6k
    if (insn->vectorExtensionType == TYPE_EVEX) {
607
81.2k
      insn->vectorExtensionPrefix[0] = byte;
608
81.2k
      insn->vectorExtensionPrefix[1] = byte1;
609
81.2k
      if (consumeByte(insn,
610
81.2k
          &insn->vectorExtensionPrefix[2])) {
611
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
612
0
        return -1;
613
0
      }
614
615
81.2k
      if (consumeByte(insn,
616
81.2k
          &insn->vectorExtensionPrefix[3])) {
617
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
618
50
        return -1;
619
50
      }
620
621
      /* We simulate the REX prefix for simplicity's sake */
622
81.2k
      if (insn->mode == MODE_64BIT) {
623
32.2k
        insn->rexPrefix =
624
32.2k
          0x40 |
625
32.2k
          (wFromEVEX3of4(
626
32.2k
             insn->vectorExtensionPrefix[2])
627
32.2k
           << 3) |
628
32.2k
          (rFromEVEX2of4(
629
32.2k
             insn->vectorExtensionPrefix[1])
630
32.2k
           << 2) |
631
32.2k
          (xFromEVEX2of4(
632
32.2k
             insn->vectorExtensionPrefix[1])
633
32.2k
           << 1) |
634
32.2k
          (bFromEVEX2of4(
635
32.2k
             insn->vectorExtensionPrefix[1])
636
32.2k
           << 0);
637
32.2k
      }
638
639
      // dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
640
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
641
      //    insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
642
81.2k
    }
643
1.27M
  } else if (byte == 0xc4) {
644
10.7k
    uint8_t byte1;
645
646
10.7k
    if (lookAtByte(insn, &byte1)) {
647
      // dbgprintf(insn, "Couldn't read second byte of VEX");
648
16
      return -1;
649
16
    }
650
651
10.6k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
652
8.26k
      insn->vectorExtensionType = TYPE_VEX_3B;
653
2.42k
    else
654
2.42k
      unconsumeByte(insn);
655
656
10.6k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
657
8.26k
      insn->vectorExtensionPrefix[0] = byte;
658
8.26k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
659
8.26k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
660
661
      /* We simulate the REX prefix for simplicity's sake */
662
8.26k
      if (insn->mode == MODE_64BIT)
663
3.41k
        insn->rexPrefix =
664
3.41k
          0x40 |
665
3.41k
          (wFromVEX3of3(
666
3.41k
             insn->vectorExtensionPrefix[2])
667
3.41k
           << 3) |
668
3.41k
          (rFromVEX2of3(
669
3.41k
             insn->vectorExtensionPrefix[1])
670
3.41k
           << 2) |
671
3.41k
          (xFromVEX2of3(
672
3.41k
             insn->vectorExtensionPrefix[1])
673
3.41k
           << 1) |
674
3.41k
          (bFromVEX2of3(
675
3.41k
             insn->vectorExtensionPrefix[1])
676
3.41k
           << 0);
677
678
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
679
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
680
      //    insn->vectorExtensionPrefix[2]);
681
8.26k
    }
682
1.26M
  } else if (byte == 0xc5) {
683
14.8k
    uint8_t byte1;
684
685
14.8k
    if (lookAtByte(insn, &byte1)) {
686
      // dbgprintf(insn, "Couldn't read second byte of VEX");
687
24
      return -1;
688
24
    }
689
690
14.8k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
691
12.3k
      insn->vectorExtensionType = TYPE_VEX_2B;
692
2.48k
    else
693
2.48k
      unconsumeByte(insn);
694
695
14.8k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
696
12.3k
      insn->vectorExtensionPrefix[0] = byte;
697
12.3k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
698
699
12.3k
      if (insn->mode == MODE_64BIT)
700
2.64k
        insn->rexPrefix =
701
2.64k
          0x40 |
702
2.64k
          (rFromVEX2of2(
703
2.64k
             insn->vectorExtensionPrefix[1])
704
2.64k
           << 2);
705
706
12.3k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
707
5.34k
      default:
708
5.34k
        break;
709
6.99k
      case VEX_PREFIX_66:
710
6.99k
        insn->hasOpSize = true;
711
6.99k
        break;
712
12.3k
      }
713
714
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
715
      //    insn->vectorExtensionPrefix[0],
716
      //    insn->vectorExtensionPrefix[1]);
717
12.3k
    }
718
1.24M
  } else if (byte == 0x8f) {
719
10.7k
    uint8_t byte1;
720
721
10.7k
    if (lookAtByte(insn, &byte1)) {
722
      // dbgprintf(insn, "Couldn't read second byte of XOP");
723
7
      return -1;
724
7
    }
725
726
10.7k
    if ((byte1 & 0x38) !=
727
10.7k
        0x0) /* 0 in these 3 bits is a POP instruction. */
728
9.84k
      insn->vectorExtensionType = TYPE_XOP;
729
881
    else
730
881
      unconsumeByte(insn);
731
732
10.7k
    if (insn->vectorExtensionType == TYPE_XOP) {
733
9.84k
      insn->vectorExtensionPrefix[0] = byte;
734
9.84k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
735
9.84k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
736
737
      /* We simulate the REX prefix for simplicity's sake */
738
9.84k
      if (insn->mode == MODE_64BIT)
739
1.32k
        insn->rexPrefix =
740
1.32k
          0x40 |
741
1.32k
          (wFromXOP3of3(
742
1.32k
             insn->vectorExtensionPrefix[2])
743
1.32k
           << 3) |
744
1.32k
          (rFromXOP2of3(
745
1.32k
             insn->vectorExtensionPrefix[1])
746
1.32k
           << 2) |
747
1.32k
          (xFromXOP2of3(
748
1.32k
             insn->vectorExtensionPrefix[1])
749
1.32k
           << 1) |
750
1.32k
          (bFromXOP2of3(
751
1.32k
             insn->vectorExtensionPrefix[1])
752
1.32k
           << 0);
753
754
9.84k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
755
9.82k
      default:
756
9.82k
        break;
757
9.82k
      case VEX_PREFIX_66:
758
29
        insn->hasOpSize = true;
759
29
        break;
760
9.84k
      }
761
762
      // dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
763
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
764
      //    insn->vectorExtensionPrefix[2]);
765
9.84k
    }
766
1.23M
  } else if (isREX(insn, byte)) {
767
80.6k
    if (lookAtByte(insn, &nextByte))
768
0
      return -1;
769
770
80.6k
    insn->rexPrefix = byte;
771
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
772
80.6k
  } else
773
1.15M
    unconsumeByte(insn);
774
775
1.36M
  if (insn->mode == MODE_16BIT) {
776
463k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
777
463k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
778
463k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
779
463k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
780
463k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
781
902k
  } else if (insn->mode == MODE_32BIT) {
782
444k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
783
444k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
784
444k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
785
444k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
786
444k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
787
458k
  } else if (insn->mode == MODE_64BIT) {
788
458k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
789
81.2k
      insn->registerSize = 8;
790
81.2k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
791
81.2k
      insn->displacementSize = 4;
792
81.2k
      insn->immediateSize = 4;
793
81.2k
      insn->immSize = 4;
794
377k
    } else {
795
377k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
796
377k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
797
377k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
798
377k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
799
377k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
800
377k
    }
801
458k
  }
802
803
1.36M
  return 0;
804
1.36M
}
805
806
static int readModRM(struct InternalInstruction *insn);
807
808
/*
809
 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
810
 *   extended or escape opcodes).
811
 *
812
 * @param insn  - The instruction whose opcode is to be read.
813
 * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
814
 */
815
static int readOpcode(struct InternalInstruction *insn)
816
682k
{
817
682k
  uint8_t current;
818
819
  // dbgprintf(insn, "readOpcode()");
820
821
682k
  insn->opcodeType = ONEBYTE;
822
823
682k
  if (insn->vectorExtensionType == TYPE_EVEX) {
824
34.4k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
825
7
    default:
826
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
827
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
828
7
      return -1;
829
9.13k
    case VEX_LOB_0F:
830
9.13k
      insn->opcodeType = TWOBYTE;
831
9.13k
      return consumeByte(insn, &insn->opcode);
832
11.9k
    case VEX_LOB_0F38:
833
11.9k
      insn->opcodeType = THREEBYTE_38;
834
11.9k
      return consumeByte(insn, &insn->opcode);
835
13.3k
    case VEX_LOB_0F3A:
836
13.3k
      insn->opcodeType = THREEBYTE_3A;
837
13.3k
      return consumeByte(insn, &insn->opcode);
838
34.4k
    }
839
647k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
840
4.27k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
841
13
    default:
842
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
843
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
844
13
      return -1;
845
220
    case VEX_LOB_0F:
846
      //insn->twoByteEscape = 0x0f;
847
220
      insn->opcodeType = TWOBYTE;
848
220
      return consumeByte(insn, &insn->opcode);
849
1.92k
    case VEX_LOB_0F38:
850
      //insn->twoByteEscape = 0x0f;
851
1.92k
      insn->opcodeType = THREEBYTE_38;
852
1.92k
      return consumeByte(insn, &insn->opcode);
853
2.11k
    case VEX_LOB_0F3A:
854
      //insn->twoByteEscape = 0x0f;
855
2.11k
      insn->opcodeType = THREEBYTE_3A;
856
2.11k
      return consumeByte(insn, &insn->opcode);
857
4.27k
    }
858
643k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
859
    //insn->twoByteEscape = 0x0f;
860
4.97k
    insn->opcodeType = TWOBYTE;
861
4.97k
    return consumeByte(insn, &insn->opcode);
862
638k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
863
3.97k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
864
36
    default:
865
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
866
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
867
36
      return -1;
868
3.25k
    case XOP_MAP_SELECT_8:
869
3.25k
      insn->opcodeType = XOP8_MAP;
870
3.25k
      return consumeByte(insn, &insn->opcode);
871
531
    case XOP_MAP_SELECT_9:
872
531
      insn->opcodeType = XOP9_MAP;
873
531
      return consumeByte(insn, &insn->opcode);
874
147
    case XOP_MAP_SELECT_A:
875
147
      insn->opcodeType = XOPA_MAP;
876
147
      return consumeByte(insn, &insn->opcode);
877
3.97k
    }
878
3.97k
  }
879
880
634k
  if (consumeByte(insn, &current))
881
0
    return -1;
882
883
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
884
634k
  insn->firstByte = current;
885
886
634k
  if (current == 0x0f) {
887
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
888
30.5k
    insn->twoByteEscape = current;
889
890
30.5k
    if (consumeByte(insn, &current))
891
40
      return -1;
892
893
30.4k
    if (current == 0x38) {
894
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
895
667
      if (consumeByte(insn, &current))
896
1
        return -1;
897
898
666
      insn->opcodeType = THREEBYTE_38;
899
29.8k
    } else if (current == 0x3a) {
900
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
901
299
      if (consumeByte(insn, &current))
902
1
        return -1;
903
904
298
      insn->opcodeType = THREEBYTE_3A;
905
29.5k
    } else if (current == 0x0f) {
906
      // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
907
      // Consume operands before the opcode to comply with the 3DNow encoding
908
185
      if (readModRM(insn))
909
1
        return -1;
910
911
184
      if (consumeByte(insn, &current))
912
2
        return -1;
913
914
182
      insn->opcodeType = THREEDNOW_MAP;
915
29.3k
    } else {
916
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
917
29.3k
      insn->opcodeType = TWOBYTE;
918
29.3k
    }
919
30.4k
  }
920
921
  /*
922
   * At this point we have consumed the full opcode.
923
   * Anything we consume from here on must be unconsumed.
924
   */
925
926
634k
  insn->opcode = current;
927
928
634k
  return 0;
929
634k
}
930
931
// Hacky for FEMMS
932
#define GET_INSTRINFO_ENUM
933
#ifndef CAPSTONE_X86_REDUCE
934
#include "X86GenInstrInfo.inc"
935
#else
936
#include "X86GenInstrInfo_reduce.inc"
937
#endif
938
939
/*
940
 * getIDWithAttrMask - Determines the ID of an instruction, consuming
941
 *   the ModR/M byte as appropriate for extended and escape opcodes,
942
 *   and using a supplied attribute mask.
943
 *
944
 * @param instructionID - A pointer whose target is filled in with the ID of the
945
 *                        instruction.
946
 * @param insn          - The instruction whose ID is to be determined.
947
 * @param attrMask      - The attribute mask to search.
948
 * @return              - 0 if the ModR/M could be read when needed or was not
949
 *                        needed; nonzero otherwise.
950
 */
951
static int getIDWithAttrMask(uint16_t *instructionID,
952
           struct InternalInstruction *insn,
953
           uint16_t attrMask)
954
1.87M
{
955
1.87M
  bool hasModRMExtension;
956
957
1.87M
  InstructionContext instructionClass = contextForAttrs(attrMask);
958
959
1.87M
  hasModRMExtension =
960
1.87M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
961
962
1.87M
  if (hasModRMExtension) {
963
993k
    if (readModRM(insn))
964
2.08k
      return -1;
965
966
991k
    *instructionID = decode(insn->opcodeType, instructionClass,
967
991k
          insn->opcode, insn->modRM);
968
991k
  } else {
969
879k
    *instructionID = decode(insn->opcodeType, instructionClass,
970
879k
          insn->opcode, 0);
971
879k
  }
972
973
1.87M
  return 0;
974
1.87M
}
975
976
/*
977
 * is16BitEquivalent - Determines whether two instruction names refer to
978
 * equivalent instructions but one is 16-bit whereas the other is not.
979
 *
980
 * @param orig  - The instruction ID that is not 16-bit
981
 * @param equiv - The instruction ID that is 16-bit
982
 */
983
static bool is16BitEquivalent(unsigned orig, unsigned equiv)
984
433k
{
985
433k
  size_t i;
986
433k
  uint16_t idx;
987
988
433k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
989
213k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
990
213k
          x86_16_bit_eq_tbl[i].first == orig;
991
209k
         i++) {
992
209k
      if (x86_16_bit_eq_tbl[i].second == equiv)
993
205k
        return true;
994
209k
    }
995
209k
  }
996
997
228k
  return false;
998
433k
}
999
1000
/*
1001
 * is64Bit - Determines whether this instruction is a 64-bit instruction.
1002
 *
1003
 * @param name - The instruction that is not 16-bit
1004
 */
1005
static bool is64Bit(uint16_t id)
1006
26.2k
{
1007
26.2k
  unsigned int i = find_insn(id);
1008
26.2k
  if (i != -1) {
1009
26.0k
    return insns[i].is64bit;
1010
26.0k
  }
1011
1012
  // not found??
1013
156
  return false;
1014
26.2k
}
1015
1016
typedef enum {
1017
  DO_NOT_RESOLVE = 0,
1018
  IGNORE_REP = 1,
1019
  IGNORE_DATA_SIZE = 2,
1020
} MandatoryPrefixResolution;
1021
1022
/*
1023
 * shouldResolveMandatoryPrefixConflict - Resolves conflicts between the 
1024
 * data size override prefix and the REP/REPNZ prefixes in the attribute 
1025
 * mask when needed.
1026
 *
1027
 * We need to resolve these conflicts, because the TableGen lookups we 
1028
 * perform distinguish between instructions with and without REP.
1029
 * For example, there may be an entry for SHLD with a DATA16 data size 
1030
 * override prefix, but no entry for REP + DATA16.
1031
 * These entries are split, because in some cases the REP and DATA16
1032
 * prefixes are used as mandatory prefixes.
1033
 * When both are mandatory prefixes, the effect of prefixing both 
1034
 * to an instruction at the same time is not specified by
1035
 * reference manuals.
1036
 *
1037
 * Conflicts are resolved by one of these three resolutions:
1038
 *   - If conflicts should not be resolved, take no action.
1039
 *   - If conflicts should be resolved and the instruction has no 
1040
 *     mandatory prefixes, resolves in favor of data size override.
1041
 *   - If conflicts should be resolved and the instruction has mandatory 
1042
 *     prefixes, resolves in favor of REP/REPNZ.
1043
 * 
1044
 * @param insn - The instruction
1045
 * @param attrMask - The current attribute mask.
1046
 */
1047
static uint16_t resolveMandatoryPrefixConflict(struct InternalInstruction *insn,
1048
                 uint16_t attrMask)
1049
2.45k
{
1050
2.45k
  MandatoryPrefixResolution resolution = DO_NOT_RESOLVE;
1051
1052
  // We inspect the opcode map and opcode to determine how we need to resolve
1053
  // a mandatory prefix conflict.
1054
2.45k
  switch (insn->opcodeType) {
1055
  // No one-byte opcodes have mandatory prefixes.
1056
33
  case ONEBYTE:
1057
33
    resolution = DO_NOT_RESOLVE;
1058
33
    break;
1059
2.31k
  case TWOBYTE:
1060
    // Exceptions for instructions that operate on data size-overridable
1061
    // operands.
1062
2.31k
    if (
1063
      // XADD
1064
2.31k
      (insn->opcode & 0xFE) == 0xC0
1065
1066
      // BSWAP
1067
2.00k
      || (insn->opcode & 0xF8) == 0xC8
1068
1069
      // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX
1070
1.30k
      || (insn->opcode & 0xF8) == 0xB0
1071
1072
      // Group 16, various NOPs
1073
1.25k
      || (insn->opcode & 0xF8) == 0x18
1074
1075
      // UD0
1076
1.32k
      || insn->opcode == 0xFF) {
1077
1.32k
      resolution = IGNORE_REP;
1078
1.32k
      break;
1079
1.32k
    }
1080
1081
    // We inspect the instruction to determine if it operates on xmm
1082
    // registers or general-purpose registers.
1083
    //
1084
    // If it operates on general purpose registers, the data size override
1085
    // prefix is not a mandatory prefix and should not be ignored.
1086
    // In most cases, this also means that the REP prefix is not a mandatory
1087
    // prefix and should be ignored.
1088
    //
1089
    // If the instruction operates on xmm registers, the data size override
1090
    // is used to select the operation type (SS, SD, PS, or PD).
1091
    // In this case, the REP prefixes take priority over the data size [1]
1092
    // override prefixes, and when both are present the data size override
1093
    // prefix should be ignored.
1094
    //
1095
    // The exception is 0xB0, where the REP prefixes are mandatory prefixes
1096
    // but the data size override prefix should still be respected.
1097
    // For this case we return DO_NOT_RESOLVE, which returns attrMask as-is.
1098
    //
1099
    // [1]: https://stackoverflow.com/a/7197365
1100
987
    switch (insn->opcode & 0xf0) {
1101
12
    case 0x10:
1102
83
    case 0x50:
1103
141
    case 0x60:
1104
151
    case 0x70:
1105
183
    case 0xC0:
1106
201
    case 0xD0:
1107
234
    case 0xE0:
1108
296
    case 0xF0:
1109
296
      resolution = IGNORE_DATA_SIZE;
1110
296
      break;
1111
189
    case 0x00:
1112
301
    case 0x20:
1113
545
    case 0x30:
1114
556
    case 0x40:
1115
587
    case 0x80:
1116
642
    case 0x90:
1117
684
    case 0xA0:
1118
684
      resolution = IGNORE_REP;
1119
684
      break;
1120
7
    default: // 0xB0
1121
7
      resolution = DO_NOT_RESOLVE;
1122
7
      break;
1123
987
    }
1124
987
    break;
1125
987
  case THREEBYTE_38:
1126
    // Exception: the ADOX and CRC32 instructions.
1127
    // These ignore the data size override prefix even though they
1128
    // operate on general-purpose registers.
1129
46
    if ((insn->opcode & 0xF0) == 0xF0) {
1130
45
      resolution = IGNORE_DATA_SIZE;
1131
45
      break;
1132
45
    }
1133
1134
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1135
    // or separately specified.
1136
1
    resolution = DO_NOT_RESOLVE;
1137
1
    break;
1138
1
  case THREEBYTE_3A:
1139
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1140
    // or separately specified.
1141
1
    resolution = DO_NOT_RESOLVE;
1142
1
    break;
1143
0
  case XOP8_MAP:
1144
0
  case XOP9_MAP:
1145
0
  case XOPA_MAP:
1146
    // These instructions do not appear to operate on XMM/SSE registers,
1147
    // so the REP prefixes can be safely ignored.
1148
0
    resolution = IGNORE_REP;
1149
0
    break;
1150
59
  case THREEDNOW_MAP:
1151
    // AMD Reference Manual Volume 3, Section 1.2.1, states that all
1152
    // 3DNow! instructions ignore the data size override prefix.
1153
59
    resolution = IGNORE_DATA_SIZE;
1154
59
    break;
1155
2.45k
  }
1156
1157
2.45k
  switch (resolution) {
1158
2.00k
  case IGNORE_REP:
1159
2.00k
    return attrMask & ~(ATTR_XD | ATTR_XS);
1160
400
  case IGNORE_DATA_SIZE:
1161
400
    return attrMask & ~ATTR_OPSIZE;
1162
0
  default:
1163
42
  case DO_NOT_RESOLVE:
1164
42
    return attrMask;
1165
2.45k
  }
1166
2.45k
}
1167
1168
/*
1169
 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
1170
 *   appropriate for extended and escape opcodes.  Determines the attributes and
1171
 *   context for the instruction before doing so.
1172
 *
1173
 * @param insn  - The instruction whose ID is to be determined.
1174
 * @return      - 0 if the ModR/M could be read when needed or was not needed;
1175
 *                nonzero otherwise.
1176
 */
1177
static int getID(struct InternalInstruction *insn)
1178
682k
{
1179
682k
  uint16_t attrMask;
1180
682k
  uint16_t instructionID;
1181
1182
682k
  attrMask = ATTR_NONE;
1183
1184
682k
  if (insn->mode == MODE_64BIT)
1185
239k
    attrMask |= ATTR_64BIT;
1186
1187
682k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1188
47.5k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1189
34.4k
            ATTR_EVEX :
1190
47.5k
            ATTR_VEX;
1191
1192
47.5k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1193
34.4k
      switch (ppFromEVEX3of4(
1194
34.4k
        insn->vectorExtensionPrefix[2])) {
1195
29.5k
      case VEX_PREFIX_66:
1196
29.5k
        attrMask |= ATTR_OPSIZE;
1197
29.5k
        break;
1198
800
      case VEX_PREFIX_F3:
1199
800
        attrMask |= ATTR_XS;
1200
800
        break;
1201
322
      case VEX_PREFIX_F2:
1202
322
        attrMask |= ATTR_XD;
1203
322
        break;
1204
34.4k
      }
1205
1206
34.4k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1207
3.82k
        attrMask |= ATTR_EVEXKZ;
1208
34.4k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1209
11.7k
        attrMask |= ATTR_EVEXB;
1210
34.4k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1211
22.9k
        attrMask |= ATTR_EVEXK;
1212
34.4k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1213
16.7k
        attrMask |= ATTR_EVEXL;
1214
34.4k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1215
14.6k
        attrMask |= ATTR_EVEXL2;
1216
34.4k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1217
4.25k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1218
4.03k
      case VEX_PREFIX_66:
1219
4.03k
        attrMask |= ATTR_OPSIZE;
1220
4.03k
        break;
1221
40
      case VEX_PREFIX_F3:
1222
40
        attrMask |= ATTR_XS;
1223
40
        break;
1224
67
      case VEX_PREFIX_F2:
1225
67
        attrMask |= ATTR_XD;
1226
67
        break;
1227
4.25k
      }
1228
1229
4.25k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1230
2.73k
        attrMask |= ATTR_VEXL;
1231
8.90k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1232
4.97k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1233
2.08k
      case VEX_PREFIX_66:
1234
2.08k
        attrMask |= ATTR_OPSIZE;
1235
2.08k
        break;
1236
195
      case VEX_PREFIX_F3:
1237
195
        attrMask |= ATTR_XS;
1238
195
        break;
1239
1.19k
      case VEX_PREFIX_F2:
1240
1.19k
        attrMask |= ATTR_XD;
1241
1.19k
        break;
1242
4.97k
      }
1243
1244
4.97k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1245
3.93k
        attrMask |= ATTR_VEXL;
1246
4.97k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1247
3.93k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1248
9
      case VEX_PREFIX_66:
1249
9
        attrMask |= ATTR_OPSIZE;
1250
9
        break;
1251
2
      case VEX_PREFIX_F3:
1252
2
        attrMask |= ATTR_XS;
1253
2
        break;
1254
5
      case VEX_PREFIX_F2:
1255
5
        attrMask |= ATTR_XD;
1256
5
        break;
1257
3.93k
      }
1258
1259
3.93k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1260
521
        attrMask |= ATTR_VEXL;
1261
3.93k
    } else {
1262
0
      return -1;
1263
0
    }
1264
634k
  } else {
1265
634k
    if (insn->hasOpSize && insn->mode != MODE_16BIT) {
1266
12.5k
      attrMask |= ATTR_OPSIZE;
1267
12.5k
    }
1268
634k
    if (insn->hasAdSize)
1269
5.27k
      attrMask |= ATTR_ADSIZE;
1270
634k
    if (insn->opcodeType == ONEBYTE) {
1271
604k
      if (insn->repeatPrefix == 0xf3 &&
1272
14.8k
          (insn->opcode == 0x90))
1273
        // Special support for PAUSE
1274
126
        attrMask |= ATTR_XS;
1275
604k
    } else {
1276
30.4k
      if (insn->repeatPrefix == 0xf2)
1277
5.08k
        attrMask |= ATTR_XD;
1278
25.3k
      else if (insn->repeatPrefix == 0xf3)
1279
4.04k
        attrMask |= ATTR_XS;
1280
30.4k
    }
1281
1282
634k
    if ((attrMask & ATTR_OPSIZE) &&
1283
12.5k
        (attrMask & (ATTR_XD | ATTR_XS))) {
1284
2.45k
      attrMask =
1285
2.45k
        resolveMandatoryPrefixConflict(insn, attrMask);
1286
2.45k
    }
1287
634k
  }
1288
1289
682k
  if (insn->rexPrefix & 0x08) {
1290
38.8k
    attrMask |= ATTR_REXW;
1291
38.8k
    attrMask &= ~ATTR_ADSIZE;
1292
38.8k
  }
1293
1294
  /*
1295
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1296
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1297
   */
1298
682k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1299
181k
      insn->opcode == 0xE3)
1300
634
    attrMask ^= ATTR_ADSIZE;
1301
1302
  /*
1303
   * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
1304
   * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
1305
   */
1306
682k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1307
9.48k
    switch (insn->opcode) {
1308
528
    case 0xE8:
1309
642
    case 0xE9:
1310
      // Take care of psubsb and other mmx instructions.
1311
642
      if (insn->opcodeType == ONEBYTE) {
1312
320
        attrMask ^= ATTR_OPSIZE;
1313
320
        insn->immediateSize = 4;
1314
320
        insn->displacementSize = 4;
1315
320
      }
1316
642
      break;
1317
91
    case 0x82:
1318
123
    case 0x83:
1319
197
    case 0x84:
1320
388
    case 0x85:
1321
469
    case 0x86:
1322
1.14k
    case 0x87:
1323
1.95k
    case 0x88:
1324
2.19k
    case 0x89:
1325
2.20k
    case 0x8A:
1326
2.28k
    case 0x8B:
1327
2.35k
    case 0x8C:
1328
2.47k
    case 0x8D:
1329
2.54k
    case 0x8E:
1330
2.61k
    case 0x8F:
1331
      // Take care of lea and three byte ops.
1332
2.61k
      if (insn->opcodeType == TWOBYTE) {
1333
129
        attrMask ^= ATTR_OPSIZE;
1334
129
        insn->immediateSize = 4;
1335
129
        insn->displacementSize = 4;
1336
129
      }
1337
2.61k
      break;
1338
9.48k
    }
1339
9.48k
  }
1340
1341
  /* The following clauses compensate for limitations of the tables. */
1342
682k
  if (insn->mode != MODE_64BIT &&
1343
442k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1344
29.5k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1345
15
      return -1;
1346
15
    }
1347
1348
    /*
1349
     * The tables can't distinguish between cases where the W-bit is used to
1350
     * select register size and cases where it's a required part of the opcode.
1351
     */
1352
29.5k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1353
19.4k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1354
20.2k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1355
2.95k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1356
19.3k
        (insn->vectorExtensionType == TYPE_XOP &&
1357
10.3k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1358
10.3k
      uint16_t instructionIDWithREXW;
1359
1360
10.3k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1361
10.3k
                attrMask | ATTR_REXW)) {
1362
2
        insn->instructionID = instructionID;
1363
2
        insn->spec = specifierForUID(instructionID);
1364
2
        return 0;
1365
2
      }
1366
1367
      // If not a 64-bit instruction. Switch the opcode.
1368
10.3k
      if (!is64Bit(instructionIDWithREXW)) {
1369
9.95k
        insn->instructionID = instructionIDWithREXW;
1370
9.95k
        insn->spec =
1371
9.95k
          specifierForUID(instructionIDWithREXW);
1372
1373
9.95k
        return 0;
1374
9.95k
      }
1375
10.3k
    }
1376
29.5k
  }
1377
1378
  /*
1379
   * Absolute moves, umonitor, and movdir64b need special handling.
1380
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1381
   *  inverted w.r.t.
1382
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1383
   *  any position.
1384
   */
1385
672k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1386
664k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1387
664k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1388
    /* Make sure we observed the prefixes in any position. */
1389
7.87k
    if (insn->hasAdSize)
1390
169
      attrMask |= ATTR_ADSIZE;
1391
1392
7.87k
    if (insn->hasOpSize)
1393
186
      attrMask |= ATTR_OPSIZE;
1394
1395
    /* In 16-bit, invert the attributes. */
1396
7.87k
    if (insn->mode == MODE_16BIT) {
1397
2.35k
      attrMask ^= ATTR_ADSIZE;
1398
1399
      /* The OpSize attribute is only valid with the absolute moves. */
1400
2.35k
      if (insn->opcodeType == ONEBYTE &&
1401
2.00k
          ((insn->opcode & 0xFC) == 0xA0))
1402
2.00k
        attrMask ^= ATTR_OPSIZE;
1403
2.35k
    }
1404
1405
7.87k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1406
1
      return -1;
1407
1
    }
1408
1409
7.87k
    insn->instructionID = instructionID;
1410
7.87k
    insn->spec = specifierForUID(instructionID);
1411
1412
7.87k
    return 0;
1413
7.87k
  }
1414
664k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1415
894
    return -1;
1416
894
  }
1417
1418
663k
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1419
208k
      !(attrMask & ATTR_OPSIZE)) {
1420
    /*
1421
     * The instruction tables make no distinction between instructions that
1422
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1423
     * particular spot (i.e., many MMX operations).  In general we're
1424
     * conservative, but in the specific case where OpSize is present but not
1425
     * in the right place we check if there's a 16-bit operation.
1426
     */
1427
190k
    const struct InstructionSpecifier *spec;
1428
190k
    uint16_t instructionIDWithOpsize;
1429
1430
190k
    spec = specifierForUID(instructionID);
1431
1432
190k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1433
190k
              attrMask | ATTR_OPSIZE)) {
1434
      /*
1435
       * ModRM required with OpSize but not present; give up and return version
1436
       * without OpSize set
1437
       */
1438
1
      insn->instructionID = instructionID;
1439
1
      insn->spec = spec;
1440
1441
1
      return 0;
1442
1
    }
1443
1444
190k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1445
92.0k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1446
91.5k
      insn->instructionID = instructionIDWithOpsize;
1447
91.5k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1448
98.9k
    } else {
1449
98.9k
      insn->instructionID = instructionID;
1450
98.9k
      insn->spec = spec;
1451
98.9k
    }
1452
1453
190k
    return 0;
1454
190k
  }
1455
1456
473k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1457
1.48k
      insn->rexPrefix & 0x01) {
1458
    /*
1459
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1460
     * it should decode as XCHG %r8, %eax.
1461
     */
1462
390
    const struct InstructionSpecifier *spec;
1463
390
    uint16_t instructionIDWithNewOpcode;
1464
390
    const struct InstructionSpecifier *specWithNewOpcode;
1465
1466
390
    spec = specifierForUID(instructionID);
1467
1468
    /* Borrow opcode from one of the other XCHGar opcodes */
1469
390
    insn->opcode = 0x91;
1470
1471
390
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1472
390
              attrMask)) {
1473
0
      insn->opcode = 0x90;
1474
1475
0
      insn->instructionID = instructionID;
1476
0
      insn->spec = spec;
1477
1478
0
      return 0;
1479
0
    }
1480
1481
390
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1482
1483
    /* Change back */
1484
390
    insn->opcode = 0x90;
1485
1486
390
    insn->instructionID = instructionIDWithNewOpcode;
1487
390
    insn->spec = specWithNewOpcode;
1488
1489
390
    return 0;
1490
390
  }
1491
1492
472k
  insn->instructionID = instructionID;
1493
472k
  insn->spec = specifierForUID(insn->instructionID);
1494
1495
472k
  return 0;
1496
473k
}
1497
1498
/*
1499
 * readSIB - Consumes the SIB byte to determine addressing information for an
1500
 *   instruction.
1501
 *
1502
 * @param insn  - The instruction whose SIB byte is to be read.
1503
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1504
 */
1505
static int readSIB(struct InternalInstruction *insn)
1506
34.7k
{
1507
34.7k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1508
34.7k
  uint8_t index, base;
1509
1510
  // dbgprintf(insn, "readSIB()");
1511
1512
34.7k
  if (insn->consumedSIB)
1513
0
    return 0;
1514
1515
34.7k
  insn->consumedSIB = true;
1516
1517
34.7k
  switch (insn->addressSize) {
1518
0
  case 2:
1519
    // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1520
0
    return -1;
1521
15.6k
  case 4:
1522
15.6k
    insn->sibIndexBase = SIB_INDEX_EAX;
1523
15.6k
    sibBaseBase = SIB_BASE_EAX;
1524
15.6k
    break;
1525
19.1k
  case 8:
1526
19.1k
    insn->sibIndexBase = SIB_INDEX_RAX;
1527
19.1k
    sibBaseBase = SIB_BASE_RAX;
1528
19.1k
    break;
1529
34.7k
  }
1530
1531
34.7k
  if (consumeByte(insn, &insn->sib))
1532
46
    return -1;
1533
1534
34.7k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1535
1536
34.7k
  if (index == 0x4) {
1537
5.77k
    insn->sibIndex = SIB_INDEX_NONE;
1538
28.9k
  } else {
1539
28.9k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1540
28.9k
  }
1541
1542
34.7k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1543
1544
34.7k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1545
1546
34.7k
  switch (base) {
1547
3.58k
  case 0x5:
1548
5.13k
  case 0xd:
1549
5.13k
    switch (modFromModRM(insn->modRM)) {
1550
2.86k
    case 0x0:
1551
2.86k
      insn->eaDisplacement = EA_DISP_32;
1552
2.86k
      insn->sibBase = SIB_BASE_NONE;
1553
2.86k
      break;
1554
1.98k
    case 0x1:
1555
1.98k
      insn->eaDisplacement = EA_DISP_8;
1556
1.98k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1557
1.98k
      break;
1558
296
    case 0x2:
1559
296
      insn->eaDisplacement = EA_DISP_32;
1560
296
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1561
296
      break;
1562
0
    case 0x3:
1563
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1564
0
      return -1;
1565
5.13k
    }
1566
5.13k
    break;
1567
29.5k
  default:
1568
29.5k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1569
29.5k
    break;
1570
34.7k
  }
1571
1572
34.7k
  return 0;
1573
34.7k
}
1574
1575
/*
1576
 * readDisplacement - Consumes the displacement of an instruction.
1577
 *
1578
 * @param insn  - The instruction whose displacement is to be read.
1579
 * @return      - 0 if the displacement byte was successfully read; nonzero
1580
 *                otherwise.
1581
 */
1582
static int readDisplacement(struct InternalInstruction *insn)
1583
241k
{
1584
241k
  int8_t d8;
1585
241k
  int16_t d16;
1586
241k
  int32_t d32;
1587
1588
  // dbgprintf(insn, "readDisplacement()");
1589
1590
241k
  if (insn->consumedDisplacement)
1591
0
    return 0;
1592
1593
241k
  insn->consumedDisplacement = true;
1594
241k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1595
1596
241k
  switch (insn->eaDisplacement) {
1597
67.3k
  case EA_DISP_NONE:
1598
67.3k
    insn->consumedDisplacement = false;
1599
67.3k
    break;
1600
106k
  case EA_DISP_8:
1601
106k
    if (consumeInt8(insn, &d8))
1602
198
      return -1;
1603
105k
    insn->displacement = d8;
1604
105k
    break;
1605
28.6k
  case EA_DISP_16:
1606
28.6k
    if (consumeInt16(insn, &d16))
1607
115
      return -1;
1608
28.5k
    insn->displacement = d16;
1609
28.5k
    break;
1610
39.6k
  case EA_DISP_32:
1611
39.6k
    if (consumeInt32(insn, &d32))
1612
339
      return -1;
1613
39.2k
    insn->displacement = d32;
1614
39.2k
    break;
1615
241k
  }
1616
1617
241k
  return 0;
1618
241k
}
1619
1620
/*
1621
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1622
 *   displacement) for an instruction and interprets it.
1623
 *
1624
 * @param insn  - The instruction whose addressing information is to be read.
1625
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1626
 */
1627
static int readModRM(struct InternalInstruction *insn)
1628
2.25M
{
1629
2.25M
  uint8_t mod, rm, reg, evexrm;
1630
1631
  // dbgprintf(insn, "readModRM()");
1632
1633
2.25M
  if (insn->consumedModRM)
1634
1.52M
    return 0;
1635
1636
728k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1637
1638
728k
  if (consumeByte(insn, &insn->modRM))
1639
1.38k
    return -1;
1640
1641
727k
  insn->consumedModRM = true;
1642
1643
  // save original ModRM for later reference
1644
727k
  insn->orgModRM = insn->modRM;
1645
1646
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1647
727k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1648
55.5k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1649
748
    insn->modRM |= 0xC0;
1650
1651
727k
  mod = modFromModRM(insn->modRM);
1652
727k
  rm = rmFromModRM(insn->modRM);
1653
727k
  reg = regFromModRM(insn->modRM);
1654
1655
  /*
1656
   * This goes by insn->registerSize to pick the correct register, which messes
1657
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1658
   * fixupReg().
1659
   */
1660
727k
  switch (insn->registerSize) {
1661
252k
  case 2:
1662
252k
    insn->regBase = MODRM_REG_AX;
1663
252k
    insn->eaRegBase = EA_REG_AX;
1664
252k
    break;
1665
411k
  case 4:
1666
411k
    insn->regBase = MODRM_REG_EAX;
1667
411k
    insn->eaRegBase = EA_REG_EAX;
1668
411k
    break;
1669
62.7k
  case 8:
1670
62.7k
    insn->regBase = MODRM_REG_RAX;
1671
62.7k
    insn->eaRegBase = EA_REG_RAX;
1672
62.7k
    break;
1673
727k
  }
1674
1675
727k
  reg |= rFromREX(insn->rexPrefix) << 3;
1676
727k
  rm |= bFromREX(insn->rexPrefix) << 3;
1677
1678
727k
  evexrm = 0;
1679
727k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1680
80.7k
      insn->mode == MODE_64BIT) {
1681
32.0k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1682
32.0k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1683
32.0k
  }
1684
1685
727k
  insn->reg = (Reg)(insn->regBase + reg);
1686
1687
727k
  switch (insn->addressSize) {
1688
237k
  case 2: {
1689
237k
    EABase eaBaseBase = EA_BASE_BX_SI;
1690
1691
237k
    switch (mod) {
1692
136k
    case 0x0:
1693
136k
      if (rm == 0x6) {
1694
8.19k
        insn->eaBase = EA_BASE_NONE;
1695
8.19k
        insn->eaDisplacement = EA_DISP_16;
1696
8.19k
        if (readDisplacement(insn))
1697
24
          return -1;
1698
128k
      } else {
1699
128k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1700
128k
        insn->eaDisplacement = EA_DISP_NONE;
1701
128k
      }
1702
136k
      break;
1703
136k
    case 0x1:
1704
32.2k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1705
32.2k
      insn->eaDisplacement = EA_DISP_8;
1706
32.2k
      insn->displacementSize = 1;
1707
32.2k
      if (readDisplacement(insn))
1708
65
        return -1;
1709
32.1k
      break;
1710
32.1k
    case 0x2:
1711
20.4k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1712
20.4k
      insn->eaDisplacement = EA_DISP_16;
1713
20.4k
      if (readDisplacement(insn))
1714
91
        return -1;
1715
20.4k
      break;
1716
47.8k
    case 0x3:
1717
47.8k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1718
47.8k
      if (readDisplacement(insn))
1719
0
        return -1;
1720
47.8k
      break;
1721
237k
    }
1722
237k
    break;
1723
237k
  }
1724
1725
237k
  case 4:
1726
489k
  case 8: {
1727
489k
    EABase eaBaseBase =
1728
489k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1729
1730
489k
    switch (mod) {
1731
0
    default:
1732
0
      break;
1733
255k
    case 0x0:
1734
255k
      insn->eaDisplacement =
1735
255k
        EA_DISP_NONE; /* readSIB may override this */
1736
      // In determining whether RIP-relative mode is used (rm=5),
1737
      // or whether a SIB byte is present (rm=4),
1738
      // the extension bits (REX.b and EVEX.x) are ignored.
1739
255k
      switch (rm & 7) {
1740
22.4k
      case 0x4: // SIB byte is present
1741
22.4k
        insn->eaBase = (insn->addressSize == 4 ?
1742
10.0k
              EA_BASE_sib :
1743
22.4k
              EA_BASE_sib64);
1744
22.4k
        if (readSIB(insn) || readDisplacement(insn))
1745
33
          return -1;
1746
22.3k
        break;
1747
22.3k
      case 0x5: // RIP-relative
1748
6.15k
        insn->eaBase = EA_BASE_NONE;
1749
6.15k
        insn->eaDisplacement = EA_DISP_32;
1750
6.15k
        if (readDisplacement(insn))
1751
76
          return -1;
1752
6.08k
        break;
1753
226k
      default:
1754
226k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1755
226k
        break;
1756
255k
      }
1757
255k
      break;
1758
255k
    case 0x1:
1759
73.9k
      insn->displacementSize = 1;
1760
      /* FALLTHROUGH */
1761
104k
    case 0x2:
1762
104k
      insn->eaDisplacement =
1763
104k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1764
104k
      switch (rm & 7) {
1765
12.3k
      case 0x4: // SIB byte is present
1766
12.3k
        insn->eaBase = EA_BASE_sib;
1767
12.3k
        if (readSIB(insn) || readDisplacement(insn))
1768
58
          return -1;
1769
12.2k
        break;
1770
92.2k
      default:
1771
92.2k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1772
92.2k
        if (readDisplacement(insn))
1773
351
          return -1;
1774
91.8k
        break;
1775
104k
      }
1776
104k
      break;
1777
129k
    case 0x3:
1778
129k
      insn->eaDisplacement = EA_DISP_NONE;
1779
129k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1780
129k
      break;
1781
489k
    }
1782
1783
489k
    break;
1784
489k
  }
1785
727k
  } /* switch (insn->addressSize) */
1786
1787
726k
  return 0;
1788
727k
}
1789
1790
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1791
  static uint16_t name(struct InternalInstruction *insn, \
1792
           OperandType type, uint8_t index, uint8_t *valid) \
1793
791k
  { \
1794
791k
    *valid = 1; \
1795
791k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
195k
    case TYPE_Rv: \
1800
195k
      return base + index; \
1801
286k
    case TYPE_R8: \
1802
286k
      index &= mask; \
1803
286k
      if (index > 0xf) \
1804
286k
        *valid = 0; \
1805
286k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
3.99k
        return prefix##_SPL + (index - 4); \
1807
282k
      } else { \
1808
282k
        return prefix##_AL + index; \
1809
282k
      } \
1810
286k
    case TYPE_R16: \
1811
8.76k
      index &= mask; \
1812
8.76k
      if (index > 0xf) \
1813
8.76k
        *valid = 0; \
1814
8.76k
      return prefix##_AX + index; \
1815
286k
    case TYPE_R32: \
1816
2.74k
      index &= mask; \
1817
2.74k
      if (index > 0xf) \
1818
2.74k
        *valid = 0; \
1819
2.74k
      return prefix##_EAX + index; \
1820
286k
    case TYPE_R64: \
1821
26.2k
      index &= mask; \
1822
26.2k
      if (index > 0xf) \
1823
26.2k
        *valid = 0; \
1824
26.2k
      return prefix##_RAX + index; \
1825
286k
    case TYPE_ZMM: \
1826
58.6k
      return prefix##_ZMM0 + index; \
1827
286k
    case TYPE_YMM: \
1828
53.6k
      return prefix##_YMM0 + index; \
1829
286k
    case TYPE_XMM: \
1830
97.3k
      return prefix##_XMM0 + index; \
1831
286k
    case TYPE_VK: \
1832
42.4k
      index &= 0xf; \
1833
42.4k
      if (index > 7) \
1834
42.4k
        *valid = 0; \
1835
42.4k
      return prefix##_K0 + index; \
1836
286k
    case TYPE_MM64: \
1837
6.56k
      return prefix##_MM0 + (index & 0x7); \
1838
286k
    case TYPE_SEGMENTREG: \
1839
2.99k
      if ((index & 7) > 5) \
1840
2.99k
        *valid = 0; \
1841
2.99k
      return prefix##_ES + (index & 7); \
1842
286k
    case TYPE_DEBUGREG: \
1843
351
      return prefix##_DR0 + index; \
1844
286k
    case TYPE_CONTROLREG: \
1845
397
      return prefix##_CR0 + index; \
1846
286k
    case TYPE_BNDR: \
1847
9.70k
      if (index > 3) \
1848
9.70k
        *valid = 0; \
1849
9.70k
      return prefix##_BND0 + index; \
1850
286k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
286k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
286k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
791k
    } \
1857
791k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1793
625k
  { \
1794
625k
    *valid = 1; \
1795
625k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
144k
    case TYPE_Rv: \
1800
144k
      return base + index; \
1801
234k
    case TYPE_R8: \
1802
234k
      index &= mask; \
1803
234k
      if (index > 0xf) \
1804
234k
        *valid = 0; \
1805
234k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
2.81k
        return prefix##_SPL + (index - 4); \
1807
232k
      } else { \
1808
232k
        return prefix##_AL + index; \
1809
232k
      } \
1810
234k
    case TYPE_R16: \
1811
6.38k
      index &= mask; \
1812
6.38k
      if (index > 0xf) \
1813
6.38k
        *valid = 0; \
1814
6.38k
      return prefix##_AX + index; \
1815
234k
    case TYPE_R32: \
1816
1.35k
      index &= mask; \
1817
1.35k
      if (index > 0xf) \
1818
1.35k
        *valid = 0; \
1819
1.35k
      return prefix##_EAX + index; \
1820
234k
    case TYPE_R64: \
1821
13.3k
      index &= mask; \
1822
13.3k
      if (index > 0xf) \
1823
13.3k
        *valid = 0; \
1824
13.3k
      return prefix##_RAX + index; \
1825
234k
    case TYPE_ZMM: \
1826
45.1k
      return prefix##_ZMM0 + index; \
1827
234k
    case TYPE_YMM: \
1828
43.6k
      return prefix##_YMM0 + index; \
1829
234k
    case TYPE_XMM: \
1830
78.6k
      return prefix##_XMM0 + index; \
1831
234k
    case TYPE_VK: \
1832
39.9k
      index &= 0xf; \
1833
39.9k
      if (index > 7) \
1834
39.9k
        *valid = 0; \
1835
39.9k
      return prefix##_K0 + index; \
1836
234k
    case TYPE_MM64: \
1837
4.16k
      return prefix##_MM0 + (index & 0x7); \
1838
234k
    case TYPE_SEGMENTREG: \
1839
2.99k
      if ((index & 7) > 5) \
1840
2.99k
        *valid = 0; \
1841
2.99k
      return prefix##_ES + (index & 7); \
1842
234k
    case TYPE_DEBUGREG: \
1843
351
      return prefix##_DR0 + index; \
1844
234k
    case TYPE_CONTROLREG: \
1845
397
      return prefix##_CR0 + index; \
1846
234k
    case TYPE_BNDR: \
1847
8.92k
      if (index > 3) \
1848
8.92k
        *valid = 0; \
1849
8.92k
      return prefix##_BND0 + index; \
1850
234k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
234k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
234k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
625k
    } \
1857
625k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1793
166k
  { \
1794
166k
    *valid = 1; \
1795
166k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
50.6k
    case TYPE_Rv: \
1800
50.6k
      return base + index; \
1801
51.2k
    case TYPE_R8: \
1802
51.2k
      index &= mask; \
1803
51.2k
      if (index > 0xf) \
1804
51.2k
        *valid = 0; \
1805
51.2k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
1.17k
        return prefix##_SPL + (index - 4); \
1807
50.1k
      } else { \
1808
50.1k
        return prefix##_AL + index; \
1809
50.1k
      } \
1810
51.2k
    case TYPE_R16: \
1811
2.37k
      index &= mask; \
1812
2.37k
      if (index > 0xf) \
1813
2.37k
        *valid = 0; \
1814
2.37k
      return prefix##_AX + index; \
1815
51.2k
    case TYPE_R32: \
1816
1.38k
      index &= mask; \
1817
1.38k
      if (index > 0xf) \
1818
1.38k
        *valid = 0; \
1819
1.38k
      return prefix##_EAX + index; \
1820
51.2k
    case TYPE_R64: \
1821
12.9k
      index &= mask; \
1822
12.9k
      if (index > 0xf) \
1823
12.9k
        *valid = 0; \
1824
12.9k
      return prefix##_RAX + index; \
1825
51.2k
    case TYPE_ZMM: \
1826
13.4k
      return prefix##_ZMM0 + index; \
1827
51.2k
    case TYPE_YMM: \
1828
10.0k
      return prefix##_YMM0 + index; \
1829
51.2k
    case TYPE_XMM: \
1830
18.6k
      return prefix##_XMM0 + index; \
1831
51.2k
    case TYPE_VK: \
1832
2.46k
      index &= 0xf; \
1833
2.46k
      if (index > 7) \
1834
2.46k
        *valid = 0; \
1835
2.46k
      return prefix##_K0 + index; \
1836
51.2k
    case TYPE_MM64: \
1837
2.39k
      return prefix##_MM0 + (index & 0x7); \
1838
51.2k
    case TYPE_SEGMENTREG: \
1839
0
      if ((index & 7) > 5) \
1840
0
        *valid = 0; \
1841
0
      return prefix##_ES + (index & 7); \
1842
51.2k
    case TYPE_DEBUGREG: \
1843
0
      return prefix##_DR0 + index; \
1844
51.2k
    case TYPE_CONTROLREG: \
1845
0
      return prefix##_CR0 + index; \
1846
51.2k
    case TYPE_BNDR: \
1847
777
      if (index > 3) \
1848
777
        *valid = 0; \
1849
777
      return prefix##_BND0 + index; \
1850
51.2k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
51.2k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
51.2k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
166k
    } \
1857
166k
  }
1858
1859
/*
1860
 * fixup*Value - Consults an operand type to determine the meaning of the
1861
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1862
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1863
 *   misinterpret it as.
1864
 *
1865
 * @param insn  - The instruction containing the operand.
1866
 * @param type  - The operand type.
1867
 * @param index - The existing value of the field as reported by readModRM().
1868
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1869
 *                field is valid for the register class; 0 if not.
1870
 * @return      - The proper value.
1871
 */
1872
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1873
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1874
1875
/*
1876
 * fixupReg - Consults an operand specifier to determine which of the
1877
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1878
 *
1879
 * @param insn  - See fixup*Value().
1880
 * @param op    - The operand specifier.
1881
 * @return      - 0 if fixup was successful; -1 if the register returned was
1882
 *                invalid for its class.
1883
 */
1884
static int fixupReg(struct InternalInstruction *insn,
1885
        const struct OperandSpecifier *op)
1886
1.33M
{
1887
1.33M
  uint8_t valid;
1888
1889
1.33M
  switch ((OperandEncoding)op->encoding) {
1890
0
  default:
1891
    // debug("Expected a REG or R/M encoding in fixupReg");
1892
0
    return -1;
1893
80.3k
  case ENCODING_VVVV:
1894
80.3k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1895
80.3k
            insn->vvvv, &valid);
1896
80.3k
    if (!valid)
1897
2
      return -1;
1898
80.3k
    break;
1899
544k
  case ENCODING_REG:
1900
544k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1901
544k
                 insn->reg - insn->regBase,
1902
544k
                 &valid);
1903
544k
    if (!valid)
1904
39
      return -1;
1905
544k
    break;
1906
4.66M
CASE_ENCODING_RM:
1907
4.66M
    if (insn->eaBase >= insn->eaRegBase) {
1908
166k
      insn->eaBase = (EABase)fixupRMValue(
1909
166k
        insn, (OperandType)op->type,
1910
166k
        insn->eaBase - insn->eaRegBase, &valid);
1911
166k
      if (!valid)
1912
4
        return -1;
1913
166k
    }
1914
705k
    break;
1915
1.33M
  }
1916
1917
1.33M
  return 0;
1918
1.33M
}
1919
1920
/*
1921
 * readOpcodeRegister - Reads an operand from the opcode field of an
1922
 *   instruction and interprets it appropriately given the operand width.
1923
 *   Handles AddRegFrm instructions.
1924
 *
1925
 * @param insn  - the instruction whose opcode field is to be read.
1926
 * @param size  - The width (in bytes) of the register being specified.
1927
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1928
 *                RAX.
1929
 * @return      - 0 on success; nonzero otherwise.
1930
 */
1931
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
1932
151k
{
1933
151k
  if (size == 0)
1934
113k
    size = insn->registerSize;
1935
1936
151k
  switch (size) {
1937
17.4k
  case 1:
1938
17.4k
    insn->opcodeRegister =
1939
17.4k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1940
17.4k
                (insn->opcode & 7)));
1941
17.4k
    if (insn->rexPrefix &&
1942
899
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1943
517
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1944
135
      insn->opcodeRegister =
1945
135
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1946
135
                   MODRM_REG_AL - 4));
1947
135
    }
1948
1949
17.4k
    break;
1950
52.5k
  case 2:
1951
52.5k
    insn->opcodeRegister =
1952
52.5k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1953
52.5k
                (insn->opcode & 7)));
1954
52.5k
    break;
1955
60.5k
  case 4:
1956
60.5k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1957
60.5k
               ((bFromREX(insn->rexPrefix) << 3) |
1958
60.5k
                (insn->opcode & 7)));
1959
60.5k
    break;
1960
21.2k
  case 8:
1961
21.2k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1962
21.2k
               ((bFromREX(insn->rexPrefix) << 3) |
1963
21.2k
                (insn->opcode & 7)));
1964
21.2k
    break;
1965
151k
  }
1966
1967
151k
  return 0;
1968
151k
}
1969
1970
/*
1971
 * readImmediate - Consumes an immediate operand from an instruction, given the
1972
 *   desired operand size.
1973
 *
1974
 * @param insn  - The instruction whose operand is to be read.
1975
 * @param size  - The width (in bytes) of the operand.
1976
 * @return      - 0 if the immediate was successfully consumed; nonzero
1977
 *                otherwise.
1978
 */
1979
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
1980
373k
{
1981
373k
  uint8_t imm8;
1982
373k
  uint16_t imm16;
1983
373k
  uint32_t imm32;
1984
373k
  uint64_t imm64;
1985
1986
373k
  if (insn->numImmediatesConsumed == 2) {
1987
    // debug("Already consumed two immediates");
1988
0
    return -1;
1989
0
  }
1990
1991
373k
  if (size == 0)
1992
0
    size = insn->immediateSize;
1993
373k
  else
1994
373k
    insn->immediateSize = size;
1995
1996
373k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1997
1998
373k
  switch (size) {
1999
272k
  case 1:
2000
272k
    if (consumeByte(insn, &imm8))
2001
546
      return -1;
2002
2003
271k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
2004
271k
    break;
2005
55.1k
  case 2:
2006
55.1k
    if (consumeUInt16(insn, &imm16))
2007
224
      return -1;
2008
2009
54.8k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
2010
54.8k
    break;
2011
39.6k
  case 4:
2012
39.6k
    if (consumeUInt32(insn, &imm32))
2013
427
      return -1;
2014
2015
39.1k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
2016
39.1k
    break;
2017
6.05k
  case 8:
2018
6.05k
    if (consumeUInt64(insn, &imm64))
2019
111
      return -1;
2020
5.94k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
2021
5.94k
    break;
2022
373k
  }
2023
2024
371k
  insn->numImmediatesConsumed++;
2025
2026
371k
  return 0;
2027
373k
}
2028
2029
/*
2030
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
2031
 *
2032
 * @param insn  - The instruction whose operand is to be read.
2033
 * @return      - 0 if the vvvv was successfully consumed; nonzero
2034
 *                otherwise.
2035
 */
2036
static int readVVVV(struct InternalInstruction *insn)
2037
1.36M
{
2038
1.36M
  int vvvv;
2039
2040
1.36M
  if (insn->vectorExtensionType == TYPE_EVEX)
2041
80.6k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2042
80.6k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2043
1.28M
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2044
8.14k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2045
1.27M
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2046
12.2k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2047
1.25M
  else if (insn->vectorExtensionType == TYPE_XOP)
2048
9.67k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2049
1.25M
  else
2050
1.25M
    return -1;
2051
2052
110k
  if (insn->mode != MODE_64BIT)
2053
71.4k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2054
2055
110k
  insn->vvvv = (Reg)vvvv;
2056
2057
110k
  return 0;
2058
1.36M
}
2059
2060
/*
2061
 * readMaskRegister - Reads an mask register from the opcode field of an
2062
 *   instruction.
2063
 *
2064
 * @param insn    - The instruction whose opcode field is to be read.
2065
 * @return        - 0 on success; nonzero otherwise.
2066
 */
2067
static int readMaskRegister(struct InternalInstruction *insn)
2068
55.5k
{
2069
55.5k
  if (insn->vectorExtensionType != TYPE_EVEX)
2070
0
    return -1;
2071
2072
55.5k
  insn->writemask =
2073
55.5k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2074
2075
55.5k
  return 0;
2076
55.5k
}
2077
2078
/*
2079
 * readOperands - Consults the specifier for an instruction and consumes all
2080
 *   operands for that instruction, interpreting them as it goes.
2081
 *
2082
 * @param insn  - The instruction whose operands are to be read and interpreted.
2083
 * @return      - 0 if all operands could be read; nonzero otherwise.
2084
 */
2085
static int readOperands(struct InternalInstruction *insn)
2086
1.36M
{
2087
1.36M
  int hasVVVV, needVVVV;
2088
1.36M
  int sawRegImm = 0;
2089
1.36M
  int i;
2090
2091
  /* If non-zero vvvv specified, need to make sure one of the operands
2092
     uses it. */
2093
1.36M
  hasVVVV = !readVVVV(insn);
2094
1.36M
  needVVVV = hasVVVV && (insn->vvvv != 0);
2095
2096
9.51M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2097
8.15M
    const OperandSpecifier *op =
2098
8.15M
      &x86OperandSets[insn->spec->operands][i];
2099
8.15M
    switch (op->encoding) {
2100
5.79M
    case ENCODING_NONE:
2101
5.86M
    case ENCODING_SI:
2102
5.93M
    case ENCODING_DI:
2103
5.93M
      break;
2104
2105
45.8k
CASE_ENCODING_VSIB:
2106
      // VSIB can use the V2 bit so check only the other bits.
2107
45.8k
      if (needVVVV)
2108
5.10k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2109
2110
45.8k
      if (readModRM(insn))
2111
0
        return -1;
2112
2113
      // Reject if SIB wasn't used.
2114
9.11k
      if (insn->eaBase != EA_BASE_sib &&
2115
5.73k
          insn->eaBase != EA_BASE_sib64)
2116
27
        return -1;
2117
2118
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2119
9.08k
      if (insn->sibIndex == SIB_INDEX_NONE)
2120
501
        insn->sibIndex =
2121
501
          (SIBIndex)(insn->sibIndexBase + 4);
2122
2123
      // If EVEX.v2 is set this is one of the 16-31 registers.
2124
9.08k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2125
7.62k
          insn->mode == MODE_64BIT &&
2126
5.42k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2127
3.63k
        insn->sibIndex =
2128
3.63k
          (SIBIndex)(insn->sibIndex + 16);
2129
2130
      // Adjust the index register to the correct size.
2131
9.08k
      switch (op->type) {
2132
0
      default:
2133
        // debug("Unhandled VSIB index type");
2134
0
        return -1;
2135
2.94k
      case TYPE_MVSIBX:
2136
2.94k
        insn->sibIndex =
2137
2.94k
          (SIBIndex)(SIB_INDEX_XMM0 +
2138
2.94k
               (insn->sibIndex -
2139
2.94k
                insn->sibIndexBase));
2140
2.94k
        break;
2141
2.67k
      case TYPE_MVSIBY:
2142
2.67k
        insn->sibIndex =
2143
2.67k
          (SIBIndex)(SIB_INDEX_YMM0 +
2144
2.67k
               (insn->sibIndex -
2145
2.67k
                insn->sibIndexBase));
2146
2.67k
        break;
2147
3.46k
      case TYPE_MVSIBZ:
2148
3.46k
        insn->sibIndex =
2149
3.46k
          (SIBIndex)(SIB_INDEX_ZMM0 +
2150
3.46k
               (insn->sibIndex -
2151
3.46k
                insn->sibIndexBase));
2152
3.46k
        break;
2153
9.08k
      }
2154
2155
      // Apply the AVX512 compressed displacement scaling factor.
2156
9.08k
      if (op->encoding != ENCODING_REG &&
2157
9.08k
          insn->eaDisplacement == EA_DISP_8)
2158
754
        insn->displacement *=
2159
754
          1 << (op->encoding - ENCODING_VSIB);
2160
9.08k
      break;
2161
2162
544k
    case ENCODING_REG:
2163
8.47M
CASE_ENCODING_RM:
2164
8.47M
      if (readModRM(insn))
2165
0
        return -1;
2166
2167
1.25M
      if (fixupReg(insn, op))
2168
43
        return -1;
2169
2170
      // Apply the AVX512 compressed displacement scaling factor.
2171
1.25M
      if (op->encoding != ENCODING_REG &&
2172
705k
          insn->eaDisplacement == EA_DISP_8)
2173
105k
        insn->displacement *=
2174
105k
          1 << (op->encoding - ENCODING_RM);
2175
1.25M
      break;
2176
2177
274k
    case ENCODING_IB:
2178
274k
      if (sawRegImm) {
2179
        /* Saw a register immediate so don't read again and instead split the
2180
             previous immediate.  FIXME: This is a hack. */
2181
2.23k
        insn->immediates[insn->numImmediatesConsumed] =
2182
2.23k
          insn->immediates
2183
2.23k
            [insn->numImmediatesConsumed -
2184
2.23k
             1] &
2185
2.23k
          0xf;
2186
2.23k
        ++insn->numImmediatesConsumed;
2187
2.23k
        break;
2188
2.23k
      }
2189
272k
      if (readImmediate(insn, 1))
2190
546
        return -1;
2191
271k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2192
3.18k
        sawRegImm = 1;
2193
271k
      break;
2194
2195
19.4k
    case ENCODING_IW:
2196
19.4k
      if (readImmediate(insn, 2))
2197
61
        return -1;
2198
19.3k
      break;
2199
2200
19.3k
    case ENCODING_ID:
2201
7.85k
      if (readImmediate(insn, 4))
2202
62
        return -1;
2203
7.78k
      break;
2204
2205
7.78k
    case ENCODING_IO:
2206
958
      if (readImmediate(insn, 8))
2207
14
        return -1;
2208
944
      break;
2209
2210
57.2k
    case ENCODING_Iv:
2211
57.2k
      if (readImmediate(insn, insn->immediateSize))
2212
465
        return -1;
2213
56.8k
      break;
2214
2215
56.8k
    case ENCODING_Ia:
2216
15.2k
      if (readImmediate(insn, insn->addressSize))
2217
160
        return -1;
2218
      /* Direct memory-offset (moffset) immediate will get mapped
2219
           to memory operand later. We want the encoding info to
2220
           reflect that as well. */
2221
15.0k
      insn->displacementOffset = insn->immediateOffset;
2222
15.0k
      insn->consumedDisplacement = true;
2223
15.0k
      insn->displacementSize = insn->immediateSize;
2224
15.0k
      insn->displacement =
2225
15.0k
        insn->immediates[insn->numImmediatesConsumed -
2226
15.0k
             1];
2227
15.0k
      insn->immediateOffset = 0;
2228
15.0k
      insn->immediateSize = 0;
2229
15.0k
      break;
2230
2231
3.90k
    case ENCODING_IRC:
2232
3.90k
      insn->RC =
2233
3.90k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2234
3.90k
         << 1) |
2235
3.90k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2236
3.90k
      break;
2237
2238
17.4k
    case ENCODING_RB:
2239
17.4k
      if (readOpcodeRegister(insn, 1))
2240
0
        return -1;
2241
17.4k
      break;
2242
2243
17.4k
    case ENCODING_RW:
2244
0
      if (readOpcodeRegister(insn, 2))
2245
0
        return -1;
2246
0
      break;
2247
2248
0
    case ENCODING_RD:
2249
0
      if (readOpcodeRegister(insn, 4))
2250
0
        return -1;
2251
0
      break;
2252
2253
20.7k
    case ENCODING_RO:
2254
20.7k
      if (readOpcodeRegister(insn, 8))
2255
0
        return -1;
2256
20.7k
      break;
2257
2258
113k
    case ENCODING_Rv:
2259
113k
      if (readOpcodeRegister(insn, 0))
2260
0
        return -1;
2261
113k
      break;
2262
2263
113k
    case ENCODING_FP:
2264
8.68k
      break;
2265
2266
80.3k
    case ENCODING_VVVV:
2267
80.3k
      if (!hasVVVV)
2268
0
        return -1;
2269
2270
80.3k
      needVVVV =
2271
80.3k
        0; /* Mark that we have found a VVVV operand. */
2272
2273
80.3k
      if (insn->mode != MODE_64BIT)
2274
53.3k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2275
2276
80.3k
      if (fixupReg(insn, op))
2277
2
        return -1;
2278
80.3k
      break;
2279
2280
80.3k
    case ENCODING_WRITEMASK:
2281
55.5k
      if (readMaskRegister(insn))
2282
0
        return -1;
2283
55.5k
      break;
2284
2285
293k
    case ENCODING_DUP:
2286
293k
      break;
2287
2288
0
    default:
2289
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2290
0
      return -1;
2291
8.15M
    }
2292
8.15M
  }
2293
2294
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2295
1.35M
  if (needVVVV)
2296
30
    return -1;
2297
2298
1.35M
  return 0;
2299
1.35M
}
2300
2301
// return True if instruction is illegal to use with prefixes
2302
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2303
static bool checkPrefix(struct InternalInstruction *insn)
2304
1.36M
{
2305
  // LOCK prefix
2306
1.36M
  if (insn->hasLockPrefix) {
2307
55.9k
    switch (insn->instructionID) {
2308
358
    default:
2309
      // invalid LOCK
2310
358
      return true;
2311
2312
    // nop dword [rax]
2313
14
    case X86_NOOPL:
2314
2315
    // DEC
2316
237
    case X86_DEC16m:
2317
414
    case X86_DEC32m:
2318
534
    case X86_DEC64m:
2319
999
    case X86_DEC8m:
2320
2321
    // ADC
2322
1.16k
    case X86_ADC16mi:
2323
1.44k
    case X86_ADC16mi8:
2324
2.23k
    case X86_ADC16mr:
2325
2.36k
    case X86_ADC32mi:
2326
2.55k
    case X86_ADC32mi8:
2327
2.90k
    case X86_ADC32mr:
2328
3.24k
    case X86_ADC64mi32:
2329
3.34k
    case X86_ADC64mi8:
2330
3.44k
    case X86_ADC64mr:
2331
3.81k
    case X86_ADC8mi:
2332
4.18k
    case X86_ADC8mi8:
2333
5.08k
    case X86_ADC8mr:
2334
5.32k
    case X86_ADC8rm:
2335
5.53k
    case X86_ADC16rm:
2336
6.03k
    case X86_ADC32rm:
2337
7.03k
    case X86_ADC64rm:
2338
2339
    // ADD
2340
7.71k
    case X86_ADD16mi:
2341
8.25k
    case X86_ADD16mi8:
2342
8.93k
    case X86_ADD16mr:
2343
9.65k
    case X86_ADD32mi:
2344
9.91k
    case X86_ADD32mi8:
2345
10.5k
    case X86_ADD32mr:
2346
10.6k
    case X86_ADD64mi32:
2347
11.0k
    case X86_ADD64mi8:
2348
11.5k
    case X86_ADD64mr:
2349
11.8k
    case X86_ADD8mi:
2350
12.2k
    case X86_ADD8mi8:
2351
13.0k
    case X86_ADD8mr:
2352
13.3k
    case X86_ADD8rm:
2353
13.8k
    case X86_ADD16rm:
2354
14.1k
    case X86_ADD32rm:
2355
14.7k
    case X86_ADD64rm:
2356
2357
    // AND
2358
14.9k
    case X86_AND16mi:
2359
15.3k
    case X86_AND16mi8:
2360
15.5k
    case X86_AND16mr:
2361
16.0k
    case X86_AND32mi:
2362
16.3k
    case X86_AND32mi8:
2363
17.4k
    case X86_AND32mr:
2364
17.8k
    case X86_AND64mi32:
2365
18.3k
    case X86_AND64mi8:
2366
18.5k
    case X86_AND64mr:
2367
18.6k
    case X86_AND8mi:
2368
19.0k
    case X86_AND8mi8:
2369
19.4k
    case X86_AND8mr:
2370
19.8k
    case X86_AND8rm:
2371
20.0k
    case X86_AND16rm:
2372
20.3k
    case X86_AND32rm:
2373
20.6k
    case X86_AND64rm:
2374
2375
    // BTC
2376
20.9k
    case X86_BTC16mi8:
2377
21.2k
    case X86_BTC16mr:
2378
21.7k
    case X86_BTC32mi8:
2379
22.2k
    case X86_BTC32mr:
2380
22.4k
    case X86_BTC64mi8:
2381
22.6k
    case X86_BTC64mr:
2382
2383
    // BTR
2384
22.8k
    case X86_BTR16mi8:
2385
23.2k
    case X86_BTR16mr:
2386
23.5k
    case X86_BTR32mi8:
2387
23.9k
    case X86_BTR32mr:
2388
24.4k
    case X86_BTR64mi8:
2389
24.6k
    case X86_BTR64mr:
2390
2391
    // BTS
2392
24.8k
    case X86_BTS16mi8:
2393
24.9k
    case X86_BTS16mr:
2394
25.0k
    case X86_BTS32mi8:
2395
25.2k
    case X86_BTS32mr:
2396
25.4k
    case X86_BTS64mi8:
2397
25.7k
    case X86_BTS64mr:
2398
2399
    // CMPXCHG
2400
26.3k
    case X86_CMPXCHG16B:
2401
26.4k
    case X86_CMPXCHG16rm:
2402
26.7k
    case X86_CMPXCHG32rm:
2403
26.9k
    case X86_CMPXCHG64rm:
2404
27.5k
    case X86_CMPXCHG8rm:
2405
27.7k
    case X86_CMPXCHG8B:
2406
2407
    // INC
2408
27.8k
    case X86_INC16m:
2409
28.0k
    case X86_INC32m:
2410
28.2k
    case X86_INC64m:
2411
28.2k
    case X86_INC8m:
2412
2413
    // NEG
2414
28.5k
    case X86_NEG16m:
2415
28.7k
    case X86_NEG32m:
2416
28.9k
    case X86_NEG64m:
2417
29.2k
    case X86_NEG8m:
2418
2419
    // NOT
2420
30.5k
    case X86_NOT16m:
2421
31.4k
    case X86_NOT32m:
2422
31.6k
    case X86_NOT64m:
2423
31.9k
    case X86_NOT8m:
2424
2425
    // OR
2426
32.2k
    case X86_OR16mi:
2427
32.6k
    case X86_OR16mi8:
2428
33.0k
    case X86_OR16mr:
2429
33.3k
    case X86_OR32mi:
2430
34.2k
    case X86_OR32mi8:
2431
34.6k
    case X86_OR32mr:
2432
35.0k
    case X86_OR64mi32:
2433
35.4k
    case X86_OR64mi8:
2434
35.6k
    case X86_OR64mr:
2435
35.9k
    case X86_OR8mi8:
2436
36.3k
    case X86_OR8mi:
2437
36.6k
    case X86_OR8mr:
2438
36.9k
    case X86_OR8rm:
2439
37.2k
    case X86_OR16rm:
2440
37.7k
    case X86_OR32rm:
2441
38.3k
    case X86_OR64rm:
2442
2443
    // SBB
2444
38.7k
    case X86_SBB16mi:
2445
39.0k
    case X86_SBB16mi8:
2446
39.3k
    case X86_SBB16mr:
2447
39.6k
    case X86_SBB32mi:
2448
39.8k
    case X86_SBB32mi8:
2449
40.2k
    case X86_SBB32mr:
2450
40.3k
    case X86_SBB64mi32:
2451
40.9k
    case X86_SBB64mi8:
2452
41.5k
    case X86_SBB64mr:
2453
42.0k
    case X86_SBB8mi:
2454
42.4k
    case X86_SBB8mi8:
2455
42.7k
    case X86_SBB8mr:
2456
2457
    // SUB
2458
43.0k
    case X86_SUB16mi:
2459
43.1k
    case X86_SUB16mi8:
2460
43.4k
    case X86_SUB16mr:
2461
43.6k
    case X86_SUB32mi:
2462
43.8k
    case X86_SUB32mi8:
2463
44.1k
    case X86_SUB32mr:
2464
44.8k
    case X86_SUB64mi32:
2465
45.4k
    case X86_SUB64mi8:
2466
45.6k
    case X86_SUB64mr:
2467
45.6k
    case X86_SUB8mi8:
2468
45.9k
    case X86_SUB8mi:
2469
46.2k
    case X86_SUB8mr:
2470
46.6k
    case X86_SUB8rm:
2471
46.8k
    case X86_SUB16rm:
2472
47.0k
    case X86_SUB32rm:
2473
47.6k
    case X86_SUB64rm:
2474
2475
    // XADD
2476
47.7k
    case X86_XADD16rm:
2477
47.9k
    case X86_XADD32rm:
2478
48.0k
    case X86_XADD64rm:
2479
48.4k
    case X86_XADD8rm:
2480
2481
    // XCHG
2482
48.8k
    case X86_XCHG16rm:
2483
49.3k
    case X86_XCHG32rm:
2484
49.7k
    case X86_XCHG64rm:
2485
50.1k
    case X86_XCHG8rm:
2486
2487
    // XOR
2488
50.9k
    case X86_XOR16mi:
2489
51.2k
    case X86_XOR16mi8:
2490
51.4k
    case X86_XOR16mr:
2491
51.9k
    case X86_XOR32mi:
2492
52.1k
    case X86_XOR32mi8:
2493
52.4k
    case X86_XOR32mr:
2494
52.5k
    case X86_XOR64mi32:
2495
52.8k
    case X86_XOR64mi8:
2496
53.1k
    case X86_XOR64mr:
2497
53.3k
    case X86_XOR8mi8:
2498
53.8k
    case X86_XOR8mi:
2499
54.1k
    case X86_XOR8mr:
2500
54.3k
    case X86_XOR8rm:
2501
54.6k
    case X86_XOR16rm:
2502
55.1k
    case X86_XOR32rm:
2503
55.6k
    case X86_XOR64rm:
2504
2505
      // this instruction can be used with LOCK prefix
2506
55.6k
      return false;
2507
55.9k
    }
2508
55.9k
  }
2509
2510
#if 0
2511
  // REPNE prefix
2512
  if (insn->repeatPrefix) {
2513
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2514
    // In such a case, clear it.
2515
    if (insn->twoByteEscape == 0x0f) {
2516
      insn->prefix0 = 0;
2517
    }
2518
  }
2519
#endif
2520
2521
  // no invalid prefixes
2522
1.30M
  return false;
2523
1.36M
}
2524
2525
/*
2526
 * decodeInstruction - Reads and interprets a full instruction provided by the
2527
 *   user.
2528
 *
2529
 * @param insn      - A pointer to the instruction to be populated.  Must be
2530
 *                    pre-allocated.
2531
 * @param reader    - The function to be used to read the instruction's bytes.
2532
 * @param readerArg - A generic argument to be passed to the reader to store
2533
 *                    any internal state.
2534
 * @param startLoc  - The address (in the reader's address space) of the first
2535
 *                    byte in the instruction.
2536
 * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
2537
 *                    decode the instruction in.
2538
 * @return          - 0 if instruction is valid; nonzero if not.
2539
 */
2540
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader,
2541
          const void *readerArg, uint64_t startLoc,
2542
          DisassemblerMode mode)
2543
1.36M
{
2544
1.36M
  insn->reader = reader;
2545
1.36M
  insn->readerArg = readerArg;
2546
1.36M
  insn->startLocation = startLoc;
2547
1.36M
  insn->readerCursor = startLoc;
2548
1.36M
  insn->mode = mode;
2549
1.36M
  insn->numImmediatesConsumed = 0;
2550
2551
1.36M
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2552
1.36M
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2553
7.51k
    return -1;
2554
2555
1.35M
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2556
2557
  // instruction length must be <= 15 to be valid
2558
1.35M
  if (insn->length > 15)
2559
76
    return -1;
2560
2561
1.35M
  if (insn->operandSize == 0)
2562
1.35M
    insn->operandSize = insn->registerSize;
2563
2564
1.35M
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2565
2566
1.35M
  return 0;
2567
1.35M
}
2568
2569
#endif