Coverage Report

Created: 2026-02-26 07:11

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.08M
{
79
1.08M
  return CONTEXTS_SYM[attrMask];
80
1.08M
}
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.08M
{
96
1.08M
  const struct OpcodeDecision *decision = NULL;
97
1.08M
  const uint8_t *indextable = NULL;
98
1.08M
  unsigned int index;
99
100
1.08M
  switch (type) {
101
0
  default:
102
0
    break;
103
920k
  case ONEBYTE:
104
920k
    decision = ONEBYTE_SYM;
105
920k
    indextable = index_x86DisassemblerOneByteOpcodes;
106
920k
    break;
107
88.3k
  case TWOBYTE:
108
88.3k
    decision = TWOBYTE_SYM;
109
88.3k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
88.3k
    break;
111
25.5k
  case THREEBYTE_38:
112
25.5k
    decision = THREEBYTE38_SYM;
113
25.5k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
25.5k
    break;
115
35.9k
  case THREEBYTE_3A:
116
35.9k
    decision = THREEBYTE3A_SYM;
117
35.9k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
35.9k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
9.78k
  case XOP8_MAP:
121
9.78k
    decision = XOP8_MAP_SYM;
122
9.78k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
9.78k
    break;
124
848
  case XOP9_MAP:
125
848
    decision = XOP9_MAP_SYM;
126
848
    indextable = index_x86DisassemblerXOP9Opcodes;
127
848
    break;
128
1.23k
  case XOPA_MAP:
129
1.23k
    decision = XOPA_MAP_SYM;
130
1.23k
    indextable = index_x86DisassemblerXOPAOpcodes;
131
1.23k
    break;
132
1.32k
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
1.32k
    return true;
135
1.08M
#endif
136
1.08M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.08M
  index = indextable[insnContext];
140
1.08M
  if (index)
141
1.07M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.07M
           MODRM_ONEENTRY;
143
3.60k
  else
144
3.60k
    return false;
145
1.08M
}
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.08M
{
160
1.08M
  const struct ModRMDecision *dec = NULL;
161
1.08M
  unsigned int index;
162
1.08M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.08M
  switch (type) {
165
0
  default:
166
0
    break; // never reach
167
919k
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
919k
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
919k
    if (index)
171
918k
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
270
    else
173
270
      dec = &emptyDecision.modRMDecisions[opcode];
174
919k
    break;
175
88.2k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
88.2k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
88.2k
    if (index)
179
87.2k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
962
    else
181
962
      dec = &emptyDecision.modRMDecisions[opcode];
182
88.2k
    break;
183
25.4k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
25.4k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
25.4k
    if (index)
187
25.0k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
410
    else
189
410
      dec = &emptyDecision.modRMDecisions[opcode];
190
25.4k
    break;
191
35.9k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
35.9k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
35.9k
    if (index)
195
35.7k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
233
    else
197
233
      dec = &emptyDecision.modRMDecisions[opcode];
198
35.9k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
9.78k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
9.78k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
9.78k
    if (index)
204
8.55k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
1.22k
    else
206
1.22k
      dec = &emptyDecision.modRMDecisions[opcode];
207
9.78k
    break;
208
847
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
847
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
847
    if (index)
212
600
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
247
    else
214
247
      dec = &emptyDecision.modRMDecisions[opcode];
215
847
    break;
216
1.23k
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
1.23k
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
1.23k
    if (index)
220
981
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
258
    else
222
258
      dec = &emptyDecision.modRMDecisions[opcode];
223
1.23k
    break;
224
1.32k
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
1.32k
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
1.32k
    if (index)
228
754
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
754
               .modRMDecisions[opcode];
230
570
    else
231
570
      dec = &emptyDecision.modRMDecisions[opcode];
232
1.32k
    break;
233
1.08M
#endif
234
1.08M
  }
235
236
1.08M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
504k
  case MODRM_ONEENTRY:
241
504k
    return modRMTable[dec->instructionIDs];
242
439k
  case MODRM_SPLITRM:
243
439k
    if (modFromModRM(modRM) == 0x3)
244
89.9k
      return modRMTable[dec->instructionIDs + 1];
245
349k
    return modRMTable[dec->instructionIDs];
246
119k
  case MODRM_SPLITREG:
247
119k
    if (modFromModRM(modRM) == 0x3)
248
42.5k
      return modRMTable[dec->instructionIDs +
249
42.5k
            ((modRM & 0x38) >> 3) + 8];
250
77.3k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
17.4k
  case MODRM_SPLITMISC:
252
17.4k
    if (modFromModRM(modRM) == 0x3)
253
3.79k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
3.79k
            8];
255
13.6k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.08M
  }
259
1.08M
}
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
912k
{
271
912k
  return &INSTRUCTIONS_SYM[uid];
272
912k
}
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
2.94M
{
286
2.94M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
2.94M
  if (!ret)
289
2.94M
    ++(insn->readerCursor);
290
291
2.94M
  return ret;
292
2.94M
}
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
236k
{
303
236k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
236k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
1.04M
{
308
1.04M
  insn->readerCursor--;
309
1.04M
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
154k
  { \
314
154k
    type combined = 0; \
315
154k
    unsigned offset; \
316
501k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
347k
      uint8_t byte; \
318
347k
      int ret = insn->reader(insn->readerArg, &byte, \
319
347k
                 insn->readerCursor + offset); \
320
347k
      if (ret) \
321
347k
        return ret; \
322
347k
      combined = combined | \
323
346k
           ((uint64_t)byte << (offset * 8)); \
324
346k
    } \
325
154k
    *ptr = combined; \
326
153k
    insn->readerCursor += sizeof(type); \
327
153k
    return 0; \
328
154k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
63.8k
  { \
314
63.8k
    type combined = 0; \
315
63.8k
    unsigned offset; \
316
127k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
63.8k
      uint8_t byte; \
318
63.8k
      int ret = insn->reader(insn->readerArg, &byte, \
319
63.8k
                 insn->readerCursor + offset); \
320
63.8k
      if (ret) \
321
63.8k
        return ret; \
322
63.8k
      combined = combined | \
323
63.7k
           ((uint64_t)byte << (offset * 8)); \
324
63.7k
    } \
325
63.8k
    *ptr = combined; \
326
63.7k
    insn->readerCursor += sizeof(type); \
327
63.7k
    return 0; \
328
63.8k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
14.3k
  { \
314
14.3k
    type combined = 0; \
315
14.3k
    unsigned offset; \
316
42.8k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
28.5k
      uint8_t byte; \
318
28.5k
      int ret = insn->reader(insn->readerArg, &byte, \
319
28.5k
                 insn->readerCursor + offset); \
320
28.5k
      if (ret) \
321
28.5k
        return ret; \
322
28.5k
      combined = combined | \
323
28.5k
           ((uint64_t)byte << (offset * 8)); \
324
28.5k
    } \
325
14.3k
    *ptr = combined; \
326
14.2k
    insn->readerCursor += sizeof(type); \
327
14.2k
    return 0; \
328
14.3k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
21.3k
  { \
314
21.3k
    type combined = 0; \
315
21.3k
    unsigned offset; \
316
106k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
84.9k
      uint8_t byte; \
318
84.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
84.9k
                 insn->readerCursor + offset); \
320
84.9k
      if (ret) \
321
84.9k
        return ret; \
322
84.9k
      combined = combined | \
323
84.6k
           ((uint64_t)byte << (offset * 8)); \
324
84.6k
    } \
325
21.3k
    *ptr = combined; \
326
21.0k
    insn->readerCursor += sizeof(type); \
327
21.0k
    return 0; \
328
21.3k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
31.0k
  { \
314
31.0k
    type combined = 0; \
315
31.0k
    unsigned offset; \
316
92.7k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
61.9k
      uint8_t byte; \
318
61.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
61.9k
                 insn->readerCursor + offset); \
320
61.9k
      if (ret) \
321
61.9k
        return ret; \
322
61.9k
      combined = combined | \
323
61.7k
           ((uint64_t)byte << (offset * 8)); \
324
61.7k
    } \
325
31.0k
    *ptr = combined; \
326
30.8k
    insn->readerCursor += sizeof(type); \
327
30.8k
    return 0; \
328
31.0k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
20.9k
  { \
314
20.9k
    type combined = 0; \
315
20.9k
    unsigned offset; \
316
103k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
83.1k
      uint8_t byte; \
318
83.1k
      int ret = insn->reader(insn->readerArg, &byte, \
319
83.1k
                 insn->readerCursor + offset); \
320
83.1k
      if (ret) \
321
83.1k
        return ret; \
322
83.1k
      combined = combined | \
323
82.8k
           ((uint64_t)byte << (offset * 8)); \
324
82.8k
    } \
325
20.9k
    *ptr = combined; \
326
20.6k
    insn->readerCursor += sizeof(type); \
327
20.6k
    return 0; \
328
20.9k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
3.16k
  { \
314
3.16k
    type combined = 0; \
315
3.16k
    unsigned offset; \
316
28.0k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
24.9k
      uint8_t byte; \
318
24.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
24.9k
                 insn->readerCursor + offset); \
320
24.9k
      if (ret) \
321
24.9k
        return ret; \
322
24.9k
      combined = combined | \
323
24.9k
           ((uint64_t)byte << (offset * 8)); \
324
24.9k
    } \
325
3.16k
    *ptr = combined; \
326
3.08k
    insn->readerCursor += sizeof(type); \
327
3.08k
    return 0; \
328
3.16k
  }
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
768k
{
349
768k
  if (insn->mode == MODE_64BIT)
350
283k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
485k
  return false;
353
768k
}
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
94.9k
{
363
94.9k
  switch (prefix) {
364
37.4k
  case 0xf0: // LOCK
365
37.4k
    insn->hasLockPrefix = true;
366
37.4k
    insn->repeatPrefix = 0;
367
37.4k
    break;
368
369
31.3k
  case 0xf2: // REPNE/REPNZ
370
57.4k
  case 0xf3: // REP or REPE/REPZ
371
57.4k
    insn->repeatPrefix = prefix;
372
57.4k
    insn->hasLockPrefix = false;
373
57.4k
    break;
374
94.9k
  }
375
94.9k
}
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
31.2k
{
387
  // In 32-bit or 16-bit mode all segment override prefixes are used.
388
31.2k
  if (insn->mode != MODE_64BIT) {
389
15.4k
    insn->segmentOverride = prefix;
390
15.4k
    insn->prefix1 = byte;
391
15.4k
    return;
392
15.4k
  }
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
15.8k
  switch (insn->prefix1) {
398
1.72k
  case 0x64: // FS
399
3.35k
  case 0x65: // GS
400
3.35k
    return;
401
15.8k
  }
402
403
  // If the proposed override is for FS or GS, mark it overridden.
404
  // All other segment prefixes are ignored.
405
12.4k
  switch (byte) {
406
1.30k
  case 0x64: // FS
407
3.41k
  case 0x65: // GS
408
3.41k
    insn->segmentOverride = prefix;
409
3.41k
    break;
410
12.4k
  }
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
12.4k
  insn->prefix1 = byte;
416
12.4k
}
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
799k
{
429
799k
  bool isPrefix = true;
430
799k
  uint8_t byte = 0;
431
799k
  uint8_t nextByte;
432
433
1.75M
  while (isPrefix) {
434
951k
    if (insn->mode == MODE_64BIT) {
435
      // eliminate consecutive redundant REX bytes in front
436
357k
      if (consumeByte(insn, &byte))
437
120
        return -1;
438
439
357k
      if ((byte & 0xf0) == 0x40) {
440
63.0k
        while (true) {
441
63.0k
          if (lookAtByte(
442
63.0k
                insn,
443
63.0k
                &byte)) // out of input code
444
143
            return -1;
445
62.9k
          if ((byte & 0xf0) == 0x40) {
446
            // another REX prefix, but we only remember the last one
447
7.49k
            if (consumeByte(insn, &byte))
448
0
              return -1;
449
7.49k
          } else
450
55.4k
            break;
451
62.9k
        }
452
453
        // recover the last REX byte if next byte is not a legacy prefix
454
55.4k
        switch (byte) {
455
1.25k
        case 0xf2: /* REPNE/REPNZ */
456
2.16k
        case 0xf3: /* REP or REPE/REPZ */
457
4.05k
        case 0xf0: /* LOCK */
458
4.45k
        case 0x2e: /* CS segment override -OR- Branch not taken */
459
5.51k
        case 0x36: /* SS segment override -OR- Branch taken */
460
5.79k
        case 0x3e: /* DS segment override */
461
5.99k
        case 0x26: /* ES segment override */
462
6.36k
        case 0x64: /* FS segment override */
463
6.52k
        case 0x65: /* GS segment override */
464
7.28k
        case 0x66: /* Operand-size override */
465
8.25k
        case 0x67: /* Address-size override */
466
8.25k
          break;
467
47.1k
        default: /* Not a prefix byte */
468
47.1k
          unconsumeByte(insn);
469
47.1k
          break;
470
55.4k
        }
471
301k
      } else {
472
301k
        unconsumeByte(insn);
473
301k
      }
474
357k
    }
475
476
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
477
951k
    if (consumeByte(insn, &byte))
478
99
      return -1;
479
480
951k
    if (insn->readerCursor - 1 == insn->startLocation &&
481
791k
        (byte == 0xf2 || byte == 0xf3)) {
482
      // prefix requires next byte
483
45.1k
      if (lookAtByte(insn, &nextByte))
484
102
        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
45.0k
      if (((nextByte == 0xf0) ||
494
43.3k
           ((nextByte & 0xfe) == 0x86 ||
495
42.2k
            (nextByte & 0xf8) == 0x90))) {
496
4.01k
        insn->xAcquireRelease = byte;
497
4.01k
      }
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
45.0k
      if (byte == 0xf3 &&
506
21.0k
          (nextByte == 0x88 || nextByte == 0x89 ||
507
20.4k
           nextByte == 0xc6 || nextByte == 0xc7)) {
508
690
        insn->xAcquireRelease = byte;
509
690
      }
510
511
45.0k
      if (isREX(insn, nextByte)) {
512
5.33k
        uint8_t nnextByte;
513
514
        // Go to REX prefix after the current one
515
5.33k
        if (consumeByte(insn, &nnextByte))
516
0
          return -1;
517
518
        // We should be able to read next byte after REX prefix
519
5.33k
        if (lookAtByte(insn, &nnextByte))
520
18
          return -1;
521
522
5.31k
        unconsumeByte(insn);
523
5.31k
      }
524
45.0k
    }
525
526
951k
    switch (byte) {
527
37.4k
    case 0xf0: /* LOCK */
528
68.7k
    case 0xf2: /* REPNE/REPNZ */
529
94.9k
    case 0xf3: /* REP or REPE/REPZ */
530
      // only accept the last prefix
531
94.9k
      setGroup0Prefix(insn, byte);
532
94.9k
      insn->prefix0 = byte;
533
94.9k
      break;
534
535
5.83k
    case 0x2e: /* CS segment override -OR- Branch not taken */
536
10.2k
    case 0x36: /* SS segment override -OR- Branch taken */
537
13.6k
    case 0x3e: /* DS segment override */
538
19.5k
    case 0x26: /* ES segment override */
539
24.2k
    case 0x64: /* FS segment override */
540
31.2k
    case 0x65: /* GS segment override */
541
31.2k
      switch (byte) {
542
5.83k
      case 0x2e:
543
5.83k
        setSegmentOverride(insn, SEG_OVERRIDE_CS, byte);
544
5.83k
        break;
545
4.44k
      case 0x36:
546
4.44k
        setSegmentOverride(insn, SEG_OVERRIDE_SS, byte);
547
4.44k
        break;
548
3.39k
      case 0x3e:
549
3.39k
        setSegmentOverride(insn, SEG_OVERRIDE_DS, byte);
550
3.39k
        break;
551
5.90k
      case 0x26:
552
5.90k
        setSegmentOverride(insn, SEG_OVERRIDE_ES, byte);
553
5.90k
        break;
554
4.63k
      case 0x64:
555
4.63k
        setSegmentOverride(insn, SEG_OVERRIDE_FS, byte);
556
4.63k
        break;
557
7.03k
      case 0x65:
558
7.03k
        setSegmentOverride(insn, SEG_OVERRIDE_GS, byte);
559
7.03k
        break;
560
0
      default:
561
        // debug("Unhandled override");
562
0
        return -1;
563
31.2k
      }
564
31.2k
      break;
565
566
31.2k
    case 0x66: /* Operand-size override */
567
16.1k
      insn->hasOpSize = true;
568
16.1k
      insn->prefix2 = byte;
569
16.1k
      break;
570
571
9.83k
    case 0x67: /* Address-size override */
572
9.83k
      insn->hasAdSize = true;
573
9.83k
      insn->prefix3 = byte;
574
9.83k
      break;
575
799k
    default: /* Not a prefix byte */
576
799k
      isPrefix = false;
577
799k
      break;
578
951k
    }
579
951k
  }
580
581
799k
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
582
583
799k
  if (byte == 0x62) {
584
53.2k
    uint8_t byte1, byte2;
585
586
53.2k
    if (consumeByte(insn, &byte1)) {
587
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
588
75
      return -1;
589
75
    }
590
591
53.2k
    if (lookAtByte(insn, &byte2)) {
592
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
593
74
      unconsumeByte(insn); /* unconsume byte1 */
594
74
      unconsumeByte(insn); /* unconsume byte  */
595
53.1k
    } else {
596
53.1k
      if ((insn->mode == MODE_64BIT ||
597
36.7k
           (byte1 & 0xc0) == 0xc0) &&
598
46.7k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
599
46.6k
        insn->vectorExtensionType = TYPE_EVEX;
600
46.6k
      } else {
601
6.51k
        unconsumeByte(insn); /* unconsume byte1 */
602
6.51k
        unconsumeByte(insn); /* unconsume byte  */
603
6.51k
      }
604
53.1k
    }
605
606
53.2k
    if (insn->vectorExtensionType == TYPE_EVEX) {
607
46.6k
      insn->vectorExtensionPrefix[0] = byte;
608
46.6k
      insn->vectorExtensionPrefix[1] = byte1;
609
46.6k
      if (consumeByte(insn,
610
46.6k
          &insn->vectorExtensionPrefix[2])) {
611
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
612
0
        return -1;
613
0
      }
614
615
46.6k
      if (consumeByte(insn,
616
46.6k
          &insn->vectorExtensionPrefix[3])) {
617
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
618
52
        return -1;
619
52
      }
620
621
      /* We simulate the REX prefix for simplicity's sake */
622
46.5k
      if (insn->mode == MODE_64BIT) {
623
16.2k
        insn->rexPrefix =
624
16.2k
          0x40 |
625
16.2k
          (wFromEVEX3of4(
626
16.2k
             insn->vectorExtensionPrefix[2])
627
16.2k
           << 3) |
628
16.2k
          (rFromEVEX2of4(
629
16.2k
             insn->vectorExtensionPrefix[1])
630
16.2k
           << 2) |
631
16.2k
          (xFromEVEX2of4(
632
16.2k
             insn->vectorExtensionPrefix[1])
633
16.2k
           << 1) |
634
16.2k
          (bFromEVEX2of4(
635
16.2k
             insn->vectorExtensionPrefix[1])
636
16.2k
           << 0);
637
16.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
46.5k
    }
643
745k
  } else if (byte == 0xc4) {
644
6.40k
    uint8_t byte1;
645
646
6.40k
    if (lookAtByte(insn, &byte1)) {
647
      // dbgprintf(insn, "Couldn't read second byte of VEX");
648
12
      return -1;
649
12
    }
650
651
6.39k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
652
5.57k
      insn->vectorExtensionType = TYPE_VEX_3B;
653
816
    else
654
816
      unconsumeByte(insn);
655
656
6.39k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
657
5.57k
      insn->vectorExtensionPrefix[0] = byte;
658
5.57k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
659
5.57k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
660
661
      /* We simulate the REX prefix for simplicity's sake */
662
5.57k
      if (insn->mode == MODE_64BIT)
663
2.31k
        insn->rexPrefix =
664
2.31k
          0x40 |
665
2.31k
          (wFromVEX3of3(
666
2.31k
             insn->vectorExtensionPrefix[2])
667
2.31k
           << 3) |
668
2.31k
          (rFromVEX2of3(
669
2.31k
             insn->vectorExtensionPrefix[1])
670
2.31k
           << 2) |
671
2.31k
          (xFromVEX2of3(
672
2.31k
             insn->vectorExtensionPrefix[1])
673
2.31k
           << 1) |
674
2.31k
          (bFromVEX2of3(
675
2.31k
             insn->vectorExtensionPrefix[1])
676
2.31k
           << 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
5.57k
    }
682
739k
  } else if (byte == 0xc5) {
683
9.47k
    uint8_t byte1;
684
685
9.47k
    if (lookAtByte(insn, &byte1)) {
686
      // dbgprintf(insn, "Couldn't read second byte of VEX");
687
9
      return -1;
688
9
    }
689
690
9.46k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
691
8.18k
      insn->vectorExtensionType = TYPE_VEX_2B;
692
1.28k
    else
693
1.28k
      unconsumeByte(insn);
694
695
9.46k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
696
8.18k
      insn->vectorExtensionPrefix[0] = byte;
697
8.18k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
698
699
8.18k
      if (insn->mode == MODE_64BIT)
700
2.49k
        insn->rexPrefix =
701
2.49k
          0x40 |
702
2.49k
          (rFromVEX2of2(
703
2.49k
             insn->vectorExtensionPrefix[1])
704
2.49k
           << 2);
705
706
8.18k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
707
4.46k
      default:
708
4.46k
        break;
709
4.46k
      case VEX_PREFIX_66:
710
3.71k
        insn->hasOpSize = true;
711
3.71k
        break;
712
8.18k
      }
713
714
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
715
      //    insn->vectorExtensionPrefix[0],
716
      //    insn->vectorExtensionPrefix[1]);
717
8.18k
    }
718
729k
  } else if (byte == 0x8f) {
719
6.58k
    uint8_t byte1;
720
721
6.58k
    if (lookAtByte(insn, &byte1)) {
722
      // dbgprintf(insn, "Couldn't read second byte of XOP");
723
14
      return -1;
724
14
    }
725
726
6.56k
    if ((byte1 & 0x38) !=
727
6.56k
        0x0) /* 0 in these 3 bits is a POP instruction. */
728
6.05k
      insn->vectorExtensionType = TYPE_XOP;
729
516
    else
730
516
      unconsumeByte(insn);
731
732
6.56k
    if (insn->vectorExtensionType == TYPE_XOP) {
733
6.05k
      insn->vectorExtensionPrefix[0] = byte;
734
6.05k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
735
6.05k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
736
737
      /* We simulate the REX prefix for simplicity's sake */
738
6.05k
      if (insn->mode == MODE_64BIT)
739
2.08k
        insn->rexPrefix =
740
2.08k
          0x40 |
741
2.08k
          (wFromXOP3of3(
742
2.08k
             insn->vectorExtensionPrefix[2])
743
2.08k
           << 3) |
744
2.08k
          (rFromXOP2of3(
745
2.08k
             insn->vectorExtensionPrefix[1])
746
2.08k
           << 2) |
747
2.08k
          (xFromXOP2of3(
748
2.08k
             insn->vectorExtensionPrefix[1])
749
2.08k
           << 1) |
750
2.08k
          (bFromXOP2of3(
751
2.08k
             insn->vectorExtensionPrefix[1])
752
2.08k
           << 0);
753
754
6.05k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
755
6.03k
      default:
756
6.03k
        break;
757
6.03k
      case VEX_PREFIX_66:
758
16
        insn->hasOpSize = true;
759
16
        break;
760
6.05k
      }
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
6.05k
    }
766
723k
  } else if (isREX(insn, byte)) {
767
47.1k
    if (lookAtByte(insn, &nextByte))
768
0
      return -1;
769
770
47.1k
    insn->rexPrefix = byte;
771
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
772
47.1k
  } else
773
676k
    unconsumeByte(insn);
774
775
798k
  if (insn->mode == MODE_16BIT) {
776
259k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
777
259k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
778
259k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
779
259k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
780
259k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
781
539k
  } else if (insn->mode == MODE_32BIT) {
782
247k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
783
247k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
784
247k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
785
247k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
786
247k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
787
291k
  } else if (insn->mode == MODE_64BIT) {
788
291k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
789
44.6k
      insn->registerSize = 8;
790
44.6k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
791
44.6k
      insn->displacementSize = 4;
792
44.6k
      insn->immediateSize = 4;
793
44.6k
      insn->immSize = 4;
794
246k
    } else {
795
246k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
796
246k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
797
246k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
798
246k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
799
246k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
800
246k
    }
801
291k
  }
802
803
798k
  return 0;
804
799k
}
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
798k
{
817
798k
  uint8_t current;
818
819
  // dbgprintf(insn, "readOpcode()");
820
821
798k
  insn->opcodeType = ONEBYTE;
822
823
798k
  if (insn->vectorExtensionType == TYPE_EVEX) {
824
46.5k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
825
9
    default:
826
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
827
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
828
9
      return -1;
829
16.0k
    case VEX_LOB_0F:
830
16.0k
      insn->opcodeType = TWOBYTE;
831
16.0k
      return consumeByte(insn, &insn->opcode);
832
13.6k
    case VEX_LOB_0F38:
833
13.6k
      insn->opcodeType = THREEBYTE_38;
834
13.6k
      return consumeByte(insn, &insn->opcode);
835
16.9k
    case VEX_LOB_0F3A:
836
16.9k
      insn->opcodeType = THREEBYTE_3A;
837
16.9k
      return consumeByte(insn, &insn->opcode);
838
46.5k
    }
839
752k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
840
5.57k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
841
18
    default:
842
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
843
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
844
18
      return -1;
845
983
    case VEX_LOB_0F:
846
      //insn->twoByteEscape = 0x0f;
847
983
      insn->opcodeType = TWOBYTE;
848
983
      return consumeByte(insn, &insn->opcode);
849
3.03k
    case VEX_LOB_0F38:
850
      //insn->twoByteEscape = 0x0f;
851
3.03k
      insn->opcodeType = THREEBYTE_38;
852
3.03k
      return consumeByte(insn, &insn->opcode);
853
1.54k
    case VEX_LOB_0F3A:
854
      //insn->twoByteEscape = 0x0f;
855
1.54k
      insn->opcodeType = THREEBYTE_3A;
856
1.54k
      return consumeByte(insn, &insn->opcode);
857
5.57k
    }
858
746k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
859
    //insn->twoByteEscape = 0x0f;
860
8.18k
    insn->opcodeType = TWOBYTE;
861
8.18k
    return consumeByte(insn, &insn->opcode);
862
738k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
863
6.05k
    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
5.22k
    case XOP_MAP_SELECT_8:
869
5.22k
      insn->opcodeType = XOP8_MAP;
870
5.22k
      return consumeByte(insn, &insn->opcode);
871
340
    case XOP_MAP_SELECT_9:
872
340
      insn->opcodeType = XOP9_MAP;
873
340
      return consumeByte(insn, &insn->opcode);
874
455
    case XOP_MAP_SELECT_A:
875
455
      insn->opcodeType = XOPA_MAP;
876
455
      return consumeByte(insn, &insn->opcode);
877
6.05k
    }
878
6.05k
  }
879
880
732k
  if (consumeByte(insn, &current))
881
0
    return -1;
882
883
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
884
732k
  insn->firstByte = current;
885
886
732k
  if (current == 0x0f) {
887
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
888
38.9k
    insn->twoByteEscape = current;
889
890
38.9k
    if (consumeByte(insn, &current))
891
77
      return -1;
892
893
38.8k
    if (current == 0x38) {
894
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
895
947
      if (consumeByte(insn, &current))
896
3
        return -1;
897
898
944
      insn->opcodeType = THREEBYTE_38;
899
37.9k
    } else if (current == 0x3a) {
900
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
901
229
      if (consumeByte(insn, &current))
902
1
        return -1;
903
904
228
      insn->opcodeType = THREEBYTE_3A;
905
37.7k
    } 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
690
      if (readModRM(insn))
909
10
        return -1;
910
911
680
      if (consumeByte(insn, &current))
912
3
        return -1;
913
914
677
      insn->opcodeType = THREEDNOW_MAP;
915
37.0k
    } else {
916
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
917
37.0k
      insn->opcodeType = TWOBYTE;
918
37.0k
    }
919
38.8k
  }
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
732k
  insn->opcode = current;
927
928
732k
  return 0;
929
732k
}
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.08M
{
955
1.08M
  bool hasModRMExtension;
956
957
1.08M
  InstructionContext instructionClass = contextForAttrs(attrMask);
958
959
1.08M
  hasModRMExtension =
960
1.08M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
961
962
1.08M
  if (hasModRMExtension) {
963
579k
    if (readModRM(insn))
964
1.55k
      return -1;
965
966
577k
    *instructionID = decode(insn->opcodeType, instructionClass,
967
577k
          insn->opcode, insn->modRM);
968
577k
  } else {
969
504k
    *instructionID = decode(insn->opcodeType, instructionClass,
970
504k
          insn->opcode, 0);
971
504k
  }
972
973
1.08M
  return 0;
974
1.08M
}
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
240k
{
985
240k
  size_t i;
986
240k
  uint16_t idx;
987
988
240k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
989
121k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
990
121k
          x86_16_bit_eq_tbl[i].first == orig;
991
118k
         i++) {
992
118k
      if (x86_16_bit_eq_tbl[i].second == equiv)
993
116k
        return true;
994
118k
    }
995
118k
  }
996
997
124k
  return false;
998
240k
}
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
19.6k
{
1007
19.6k
  unsigned int i = find_insn(id);
1008
19.6k
  if (i != -1) {
1009
19.5k
    return insns[i].is64bit;
1010
19.5k
  }
1011
1012
  // not found??
1013
96
  return false;
1014
19.6k
}
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
1.39k
{
1050
1.39k
  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
1.39k
  switch (insn->opcodeType) {
1055
  // No one-byte opcodes have mandatory prefixes.
1056
70
  case ONEBYTE:
1057
70
    resolution = DO_NOT_RESOLVE;
1058
70
    break;
1059
1.28k
  case TWOBYTE:
1060
    // Exceptions for instructions that operate on data size-overridable
1061
    // operands.
1062
1.28k
    if (
1063
      // XADD
1064
1.28k
      (insn->opcode & 0xFE) == 0xC0
1065
1066
      // BSWAP
1067
1.16k
      || (insn->opcode & 0xF8) == 0xC8
1068
1069
      // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX
1070
636
      || (insn->opcode & 0xF8) == 0xB0
1071
1072
      // Group 16, various NOPs
1073
601
      || (insn->opcode & 0xF8) == 0x18
1074
1075
      // UD0
1076
796
      || insn->opcode == 0xFF) {
1077
796
      resolution = IGNORE_REP;
1078
796
      break;
1079
796
    }
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
490
    switch (insn->opcode & 0xf0) {
1101
1
    case 0x10:
1102
21
    case 0x50:
1103
22
    case 0x60:
1104
54
    case 0x70:
1105
116
    case 0xC0:
1106
123
    case 0xD0:
1107
127
    case 0xE0:
1108
132
    case 0xF0:
1109
132
      resolution = IGNORE_DATA_SIZE;
1110
132
      break;
1111
78
    case 0x00:
1112
100
    case 0x20:
1113
174
    case 0x30:
1114
213
    case 0x40:
1115
321
    case 0x80:
1116
339
    case 0x90:
1117
357
    case 0xA0:
1118
357
      resolution = IGNORE_REP;
1119
357
      break;
1120
1
    default: // 0xB0
1121
1
      resolution = DO_NOT_RESOLVE;
1122
1
      break;
1123
490
    }
1124
490
    break;
1125
490
  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
28
    if ((insn->opcode & 0xF0) == 0xF0) {
1130
25
      resolution = IGNORE_DATA_SIZE;
1131
25
      break;
1132
25
    }
1133
1134
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1135
    // or separately specified.
1136
3
    resolution = DO_NOT_RESOLVE;
1137
3
    break;
1138
0
  case THREEBYTE_3A:
1139
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1140
    // or separately specified.
1141
0
    resolution = DO_NOT_RESOLVE;
1142
0
    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
15
  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
15
    resolution = IGNORE_DATA_SIZE;
1154
15
    break;
1155
1.39k
  }
1156
1157
1.39k
  switch (resolution) {
1158
1.15k
  case IGNORE_REP:
1159
1.15k
    return attrMask & ~(ATTR_XD | ATTR_XS);
1160
172
  case IGNORE_DATA_SIZE:
1161
172
    return attrMask & ~ATTR_OPSIZE;
1162
0
  default:
1163
74
  case DO_NOT_RESOLVE:
1164
74
    return attrMask;
1165
1.39k
  }
1166
1.39k
}
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
798k
{
1179
798k
  uint16_t attrMask;
1180
798k
  uint16_t instructionID;
1181
1182
798k
  attrMask = ATTR_NONE;
1183
1184
798k
  if (insn->mode == MODE_64BIT)
1185
291k
    attrMask |= ATTR_64BIT;
1186
1187
798k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1188
66.2k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1189
46.5k
            ATTR_EVEX :
1190
66.2k
            ATTR_VEX;
1191
1192
66.2k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1193
46.5k
      switch (ppFromEVEX3of4(
1194
46.5k
        insn->vectorExtensionPrefix[2])) {
1195
39.1k
      case VEX_PREFIX_66:
1196
39.1k
        attrMask |= ATTR_OPSIZE;
1197
39.1k
        break;
1198
2.17k
      case VEX_PREFIX_F3:
1199
2.17k
        attrMask |= ATTR_XS;
1200
2.17k
        break;
1201
994
      case VEX_PREFIX_F2:
1202
994
        attrMask |= ATTR_XD;
1203
994
        break;
1204
46.5k
      }
1205
1206
46.5k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1207
3.82k
        attrMask |= ATTR_EVEXKZ;
1208
46.5k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1209
17.7k
        attrMask |= ATTR_EVEXB;
1210
46.5k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1211
30.2k
        attrMask |= ATTR_EVEXK;
1212
46.5k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1213
22.0k
        attrMask |= ATTR_EVEXL;
1214
46.5k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1215
19.4k
        attrMask |= ATTR_EVEXL2;
1216
46.5k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1217
5.54k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1218
4.58k
      case VEX_PREFIX_66:
1219
4.58k
        attrMask |= ATTR_OPSIZE;
1220
4.58k
        break;
1221
287
      case VEX_PREFIX_F3:
1222
287
        attrMask |= ATTR_XS;
1223
287
        break;
1224
307
      case VEX_PREFIX_F2:
1225
307
        attrMask |= ATTR_XD;
1226
307
        break;
1227
5.54k
      }
1228
1229
5.54k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1230
2.85k
        attrMask |= ATTR_VEXL;
1231
14.1k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1232
8.17k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1233
3.70k
      case VEX_PREFIX_66:
1234
3.70k
        attrMask |= ATTR_OPSIZE;
1235
3.70k
        break;
1236
819
      case VEX_PREFIX_F3:
1237
819
        attrMask |= ATTR_XS;
1238
819
        break;
1239
1.33k
      case VEX_PREFIX_F2:
1240
1.33k
        attrMask |= ATTR_XD;
1241
1.33k
        break;
1242
8.17k
      }
1243
1244
8.17k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1245
5.79k
        attrMask |= ATTR_VEXL;
1246
8.17k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1247
5.99k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1248
10
      case VEX_PREFIX_66:
1249
10
        attrMask |= ATTR_OPSIZE;
1250
10
        break;
1251
6
      case VEX_PREFIX_F3:
1252
6
        attrMask |= ATTR_XS;
1253
6
        break;
1254
9
      case VEX_PREFIX_F2:
1255
9
        attrMask |= ATTR_XD;
1256
9
        break;
1257
5.99k
      }
1258
1259
5.99k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1260
331
        attrMask |= ATTR_VEXL;
1261
5.99k
    } else {
1262
0
      return -1;
1263
0
    }
1264
732k
  } else {
1265
732k
    if (insn->hasOpSize && insn->mode != MODE_16BIT) {
1266
12.5k
      attrMask |= ATTR_OPSIZE;
1267
12.5k
    }
1268
732k
    if (insn->hasAdSize)
1269
7.53k
      attrMask |= ATTR_ADSIZE;
1270
732k
    if (insn->opcodeType == ONEBYTE) {
1271
693k
      if (insn->repeatPrefix == 0xf3 &&
1272
16.2k
          (insn->opcode == 0x90))
1273
        // Special support for PAUSE
1274
347
        attrMask |= ATTR_XS;
1275
693k
    } else {
1276
38.8k
      if (insn->repeatPrefix == 0xf2)
1277
4.54k
        attrMask |= ATTR_XD;
1278
34.3k
      else if (insn->repeatPrefix == 0xf3)
1279
5.21k
        attrMask |= ATTR_XS;
1280
38.8k
    }
1281
1282
732k
    if ((attrMask & ATTR_OPSIZE) &&
1283
12.5k
        (attrMask & (ATTR_XD | ATTR_XS))) {
1284
1.39k
      attrMask =
1285
1.39k
        resolveMandatoryPrefixConflict(insn, attrMask);
1286
1.39k
    }
1287
732k
  }
1288
1289
798k
  if (insn->rexPrefix & 0x08) {
1290
44.6k
    attrMask |= ATTR_REXW;
1291
44.6k
    attrMask &= ~ATTR_ADSIZE;
1292
44.6k
  }
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
798k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1299
229k
      insn->opcode == 0xE3)
1300
994
    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
798k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1307
10.4k
    switch (insn->opcode) {
1308
301
    case 0xE8:
1309
502
    case 0xE9:
1310
      // Take care of psubsb and other mmx instructions.
1311
502
      if (insn->opcodeType == ONEBYTE) {
1312
266
        attrMask ^= ATTR_OPSIZE;
1313
266
        insn->immediateSize = 4;
1314
266
        insn->displacementSize = 4;
1315
266
      }
1316
502
      break;
1317
78
    case 0x82:
1318
171
    case 0x83:
1319
261
    case 0x84:
1320
639
    case 0x85:
1321
876
    case 0x86:
1322
1.55k
    case 0x87:
1323
1.89k
    case 0x88:
1324
2.10k
    case 0x89:
1325
2.37k
    case 0x8A:
1326
2.60k
    case 0x8B:
1327
2.67k
    case 0x8C:
1328
2.93k
    case 0x8D:
1329
3.04k
    case 0x8E:
1330
3.24k
    case 0x8F:
1331
      // Take care of lea and three byte ops.
1332
3.24k
      if (insn->opcodeType == TWOBYTE) {
1333
165
        attrMask ^= ATTR_OPSIZE;
1334
165
        insn->immediateSize = 4;
1335
165
        insn->displacementSize = 4;
1336
165
      }
1337
3.24k
      break;
1338
10.4k
    }
1339
10.4k
  }
1340
1341
  /* The following clauses compensate for limitations of the tables. */
1342
798k
  if (insn->mode != MODE_64BIT &&
1343
507k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1344
43.1k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1345
29
      return -1;
1346
29
    }
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
43.1k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1353
30.2k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1354
25.3k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1355
3.24k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1356
23.7k
        (insn->vectorExtensionType == TYPE_XOP &&
1357
19.6k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1358
19.6k
      uint16_t instructionIDWithREXW;
1359
1360
19.6k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1361
19.6k
                attrMask | ATTR_REXW)) {
1362
12
        insn->instructionID = instructionID;
1363
12
        insn->spec = specifierForUID(instructionID);
1364
12
        return 0;
1365
12
      }
1366
1367
      // If not a 64-bit instruction. Switch the opcode.
1368
19.6k
      if (!is64Bit(instructionIDWithREXW)) {
1369
18.4k
        insn->instructionID = instructionIDWithREXW;
1370
18.4k
        insn->spec =
1371
18.4k
          specifierForUID(instructionIDWithREXW);
1372
1373
18.4k
        return 0;
1374
18.4k
      }
1375
19.6k
    }
1376
43.1k
  }
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
780k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1386
771k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1387
771k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1388
    /* Make sure we observed the prefixes in any position. */
1389
9.34k
    if (insn->hasAdSize)
1390
318
      attrMask |= ATTR_ADSIZE;
1391
1392
9.34k
    if (insn->hasOpSize)
1393
350
      attrMask |= ATTR_OPSIZE;
1394
1395
    /* In 16-bit, invert the attributes. */
1396
9.34k
    if (insn->mode == MODE_16BIT) {
1397
3.41k
      attrMask ^= ATTR_ADSIZE;
1398
1399
      /* The OpSize attribute is only valid with the absolute moves. */
1400
3.41k
      if (insn->opcodeType == ONEBYTE &&
1401
3.21k
          ((insn->opcode & 0xFC) == 0xA0))
1402
3.21k
        attrMask ^= ATTR_OPSIZE;
1403
3.41k
    }
1404
1405
9.34k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1406
4
      return -1;
1407
4
    }
1408
1409
9.33k
    insn->instructionID = instructionID;
1410
9.33k
    insn->spec = specifierForUID(instructionID);
1411
1412
9.33k
    return 0;
1413
9.34k
  }
1414
770k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1415
1.50k
    return -1;
1416
1.50k
  }
1417
1418
769k
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1419
261k
      !(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
240k
    const struct InstructionSpecifier *spec;
1428
240k
    uint16_t instructionIDWithOpsize;
1429
1430
240k
    spec = specifierForUID(instructionID);
1431
1432
240k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1433
240k
              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
240k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1445
116k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1446
115k
      insn->instructionID = instructionIDWithOpsize;
1447
115k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1448
125k
    } else {
1449
125k
      insn->instructionID = instructionID;
1450
125k
      insn->spec = spec;
1451
125k
    }
1452
1453
240k
    return 0;
1454
240k
  }
1455
1456
529k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1457
1.95k
      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
191
    const struct InstructionSpecifier *spec;
1463
191
    uint16_t instructionIDWithNewOpcode;
1464
191
    const struct InstructionSpecifier *specWithNewOpcode;
1465
1466
191
    spec = specifierForUID(instructionID);
1467
1468
    /* Borrow opcode from one of the other XCHGar opcodes */
1469
191
    insn->opcode = 0x91;
1470
1471
191
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1472
191
              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
191
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1482
1483
    /* Change back */
1484
191
    insn->opcode = 0x90;
1485
1486
191
    insn->instructionID = instructionIDWithNewOpcode;
1487
191
    insn->spec = specWithNewOpcode;
1488
1489
191
    return 0;
1490
191
  }
1491
1492
528k
  insn->instructionID = instructionID;
1493
528k
  insn->spec = specifierForUID(insn->instructionID);
1494
1495
528k
  return 0;
1496
529k
}
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
20.4k
{
1507
20.4k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1508
20.4k
  uint8_t index, base;
1509
1510
  // dbgprintf(insn, "readSIB()");
1511
1512
20.4k
  if (insn->consumedSIB)
1513
0
    return 0;
1514
1515
20.4k
  insn->consumedSIB = true;
1516
1517
20.4k
  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
9.66k
  case 4:
1522
9.66k
    insn->sibIndexBase = SIB_INDEX_EAX;
1523
9.66k
    sibBaseBase = SIB_BASE_EAX;
1524
9.66k
    break;
1525
10.8k
  case 8:
1526
10.8k
    insn->sibIndexBase = SIB_INDEX_RAX;
1527
10.8k
    sibBaseBase = SIB_BASE_RAX;
1528
10.8k
    break;
1529
20.4k
  }
1530
1531
20.4k
  if (consumeByte(insn, &insn->sib))
1532
33
    return -1;
1533
1534
20.4k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1535
1536
20.4k
  if (index == 0x4) {
1537
5.38k
    insn->sibIndex = SIB_INDEX_NONE;
1538
15.0k
  } else {
1539
15.0k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1540
15.0k
  }
1541
1542
20.4k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1543
1544
20.4k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1545
1546
20.4k
  switch (base) {
1547
2.46k
  case 0x5:
1548
3.36k
  case 0xd:
1549
3.36k
    switch (modFromModRM(insn->modRM)) {
1550
1.35k
    case 0x0:
1551
1.35k
      insn->eaDisplacement = EA_DISP_32;
1552
1.35k
      insn->sibBase = SIB_BASE_NONE;
1553
1.35k
      break;
1554
1.86k
    case 0x1:
1555
1.86k
      insn->eaDisplacement = EA_DISP_8;
1556
1.86k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1557
1.86k
      break;
1558
144
    case 0x2:
1559
144
      insn->eaDisplacement = EA_DISP_32;
1560
144
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1561
144
      break;
1562
0
    case 0x3:
1563
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1564
0
      return -1;
1565
3.36k
    }
1566
3.36k
    break;
1567
17.1k
  default:
1568
17.1k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1569
17.1k
    break;
1570
20.4k
  }
1571
1572
20.4k
  return 0;
1573
20.4k
}
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
138k
{
1584
138k
  int8_t d8;
1585
138k
  int16_t d16;
1586
138k
  int32_t d32;
1587
1588
  // dbgprintf(insn, "readDisplacement()");
1589
1590
138k
  if (insn->consumedDisplacement)
1591
0
    return 0;
1592
1593
138k
  insn->consumedDisplacement = true;
1594
138k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1595
1596
138k
  switch (insn->eaDisplacement) {
1597
38.9k
  case EA_DISP_NONE:
1598
38.9k
    insn->consumedDisplacement = false;
1599
38.9k
    break;
1600
63.8k
  case EA_DISP_8:
1601
63.8k
    if (consumeInt8(insn, &d8))
1602
140
      return -1;
1603
63.7k
    insn->displacement = d8;
1604
63.7k
    break;
1605
14.3k
  case EA_DISP_16:
1606
14.3k
    if (consumeInt16(insn, &d16))
1607
68
      return -1;
1608
14.2k
    insn->displacement = d16;
1609
14.2k
    break;
1610
21.3k
  case EA_DISP_32:
1611
21.3k
    if (consumeInt32(insn, &d32))
1612
259
      return -1;
1613
21.0k
    insn->displacement = d32;
1614
21.0k
    break;
1615
138k
  }
1616
1617
137k
  return 0;
1618
138k
}
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
1.32M
{
1629
1.32M
  uint8_t mod, rm, reg, evexrm;
1630
1631
  // dbgprintf(insn, "readModRM()");
1632
1633
1.32M
  if (insn->consumedModRM)
1634
896k
    return 0;
1635
1636
428k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1637
1638
428k
  if (consumeByte(insn, &insn->modRM))
1639
1.06k
    return -1;
1640
1641
427k
  insn->consumedModRM = true;
1642
1643
  // save original ModRM for later reference
1644
427k
  insn->orgModRM = insn->modRM;
1645
1646
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1647
427k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1648
33.1k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1649
538
    insn->modRM |= 0xC0;
1650
1651
427k
  mod = modFromModRM(insn->modRM);
1652
427k
  rm = rmFromModRM(insn->modRM);
1653
427k
  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
427k
  switch (insn->registerSize) {
1661
146k
  case 2:
1662
146k
    insn->regBase = MODRM_REG_AX;
1663
146k
    insn->eaRegBase = EA_REG_AX;
1664
146k
    break;
1665
246k
  case 4:
1666
246k
    insn->regBase = MODRM_REG_EAX;
1667
246k
    insn->eaRegBase = EA_REG_EAX;
1668
246k
    break;
1669
34.3k
  case 8:
1670
34.3k
    insn->regBase = MODRM_REG_RAX;
1671
34.3k
    insn->eaRegBase = EA_REG_RAX;
1672
34.3k
    break;
1673
427k
  }
1674
1675
427k
  reg |= rFromREX(insn->rexPrefix) << 3;
1676
427k
  rm |= bFromREX(insn->rexPrefix) << 3;
1677
1678
427k
  evexrm = 0;
1679
427k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1680
46.2k
      insn->mode == MODE_64BIT) {
1681
16.1k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1682
16.1k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1683
16.1k
  }
1684
1685
427k
  insn->reg = (Reg)(insn->regBase + reg);
1686
1687
427k
  switch (insn->addressSize) {
1688
137k
  case 2: {
1689
137k
    EABase eaBaseBase = EA_BASE_BX_SI;
1690
1691
137k
    switch (mod) {
1692
77.4k
    case 0x0:
1693
77.4k
      if (rm == 0x6) {
1694
4.26k
        insn->eaBase = EA_BASE_NONE;
1695
4.26k
        insn->eaDisplacement = EA_DISP_16;
1696
4.26k
        if (readDisplacement(insn))
1697
15
          return -1;
1698
73.2k
      } else {
1699
73.2k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1700
73.2k
        insn->eaDisplacement = EA_DISP_NONE;
1701
73.2k
      }
1702
77.4k
      break;
1703
77.4k
    case 0x1:
1704
21.2k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1705
21.2k
      insn->eaDisplacement = EA_DISP_8;
1706
21.2k
      insn->displacementSize = 1;
1707
21.2k
      if (readDisplacement(insn))
1708
37
        return -1;
1709
21.2k
      break;
1710
21.2k
    case 0x2:
1711
10.0k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1712
10.0k
      insn->eaDisplacement = EA_DISP_16;
1713
10.0k
      if (readDisplacement(insn))
1714
53
        return -1;
1715
9.99k
      break;
1716
28.4k
    case 0x3:
1717
28.4k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1718
28.4k
      if (readDisplacement(insn))
1719
0
        return -1;
1720
28.4k
      break;
1721
137k
    }
1722
137k
    break;
1723
137k
  }
1724
1725
137k
  case 4:
1726
289k
  case 8: {
1727
289k
    EABase eaBaseBase =
1728
289k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1729
1730
289k
    switch (mod) {
1731
0
    default:
1732
0
      break;
1733
154k
    case 0x0:
1734
154k
      insn->eaDisplacement =
1735
154k
        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
154k
      switch (rm & 7) {
1740
11.8k
      case 0x4: // SIB byte is present
1741
11.8k
        insn->eaBase = (insn->addressSize == 4 ?
1742
5.37k
              EA_BASE_sib :
1743
11.8k
              EA_BASE_sib64);
1744
11.8k
        if (readSIB(insn) || readDisplacement(insn))
1745
29
          return -1;
1746
11.7k
        break;
1747
11.7k
      case 0x5: // RIP-relative
1748
4.07k
        insn->eaBase = EA_BASE_NONE;
1749
4.07k
        insn->eaDisplacement = EA_DISP_32;
1750
4.07k
        if (readDisplacement(insn))
1751
55
          return -1;
1752
4.02k
        break;
1753
138k
      default:
1754
138k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1755
138k
        break;
1756
154k
      }
1757
154k
      break;
1758
154k
    case 0x1:
1759
42.5k
      insn->displacementSize = 1;
1760
      /* FALLTHROUGH */
1761
58.4k
    case 0x2:
1762
58.4k
      insn->eaDisplacement =
1763
58.4k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1764
58.4k
      switch (rm & 7) {
1765
8.68k
      case 0x4: // SIB byte is present
1766
8.68k
        insn->eaBase = EA_BASE_sib;
1767
8.68k
        if (readSIB(insn) || readDisplacement(insn))
1768
46
          return -1;
1769
8.64k
        break;
1770
49.8k
      default:
1771
49.8k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1772
49.8k
        if (readDisplacement(insn))
1773
265
          return -1;
1774
49.5k
        break;
1775
58.4k
      }
1776
58.1k
      break;
1777
76.6k
    case 0x3:
1778
76.6k
      insn->eaDisplacement = EA_DISP_NONE;
1779
76.6k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1780
76.6k
      break;
1781
289k
    }
1782
1783
289k
    break;
1784
289k
  }
1785
427k
  } /* switch (insn->addressSize) */
1786
1787
426k
  return 0;
1788
427k
}
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
471k
  { \
1794
471k
    *valid = 1; \
1795
471k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
117k
    case TYPE_Rv: \
1800
117k
      return base + index; \
1801
174k
    case TYPE_R8: \
1802
174k
      index &= mask; \
1803
174k
      if (index > 0xf) \
1804
174k
        *valid = 0; \
1805
174k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
2.18k
        return prefix##_SPL + (index - 4); \
1807
171k
      } else { \
1808
171k
        return prefix##_AL + index; \
1809
171k
      } \
1810
174k
    case TYPE_R16: \
1811
3.28k
      index &= mask; \
1812
3.28k
      if (index > 0xf) \
1813
3.28k
        *valid = 0; \
1814
3.28k
      return prefix##_AX + index; \
1815
174k
    case TYPE_R32: \
1816
1.43k
      index &= mask; \
1817
1.43k
      if (index > 0xf) \
1818
1.43k
        *valid = 0; \
1819
1.43k
      return prefix##_EAX + index; \
1820
174k
    case TYPE_R64: \
1821
13.8k
      index &= mask; \
1822
13.8k
      if (index > 0xf) \
1823
13.8k
        *valid = 0; \
1824
13.8k
      return prefix##_RAX + index; \
1825
174k
    case TYPE_ZMM: \
1826
32.1k
      return prefix##_ZMM0 + index; \
1827
174k
    case TYPE_YMM: \
1828
29.6k
      return prefix##_YMM0 + index; \
1829
174k
    case TYPE_XMM: \
1830
61.9k
      return prefix##_XMM0 + index; \
1831
174k
    case TYPE_VK: \
1832
24.4k
      index &= 0xf; \
1833
24.4k
      if (index > 7) \
1834
24.4k
        *valid = 0; \
1835
24.4k
      return prefix##_K0 + index; \
1836
174k
    case TYPE_MM64: \
1837
5.43k
      return prefix##_MM0 + (index & 0x7); \
1838
174k
    case TYPE_SEGMENTREG: \
1839
1.61k
      if ((index & 7) > 5) \
1840
1.61k
        *valid = 0; \
1841
1.61k
      return prefix##_ES + (index & 7); \
1842
174k
    case TYPE_DEBUGREG: \
1843
280
      return prefix##_DR0 + index; \
1844
174k
    case TYPE_CONTROLREG: \
1845
258
      return prefix##_CR0 + index; \
1846
174k
    case TYPE_BNDR: \
1847
6.08k
      if (index > 3) \
1848
6.08k
        *valid = 0; \
1849
6.08k
      return prefix##_BND0 + index; \
1850
174k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
174k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
174k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
471k
    } \
1857
471k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1793
371k
  { \
1794
371k
    *valid = 1; \
1795
371k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
86.1k
    case TYPE_Rv: \
1800
86.1k
      return base + index; \
1801
139k
    case TYPE_R8: \
1802
139k
      index &= mask; \
1803
139k
      if (index > 0xf) \
1804
139k
        *valid = 0; \
1805
139k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
1.55k
        return prefix##_SPL + (index - 4); \
1807
138k
      } else { \
1808
138k
        return prefix##_AL + index; \
1809
138k
      } \
1810
139k
    case TYPE_R16: \
1811
2.43k
      index &= mask; \
1812
2.43k
      if (index > 0xf) \
1813
2.43k
        *valid = 0; \
1814
2.43k
      return prefix##_AX + index; \
1815
139k
    case TYPE_R32: \
1816
736
      index &= mask; \
1817
736
      if (index > 0xf) \
1818
736
        *valid = 0; \
1819
736
      return prefix##_EAX + index; \
1820
139k
    case TYPE_R64: \
1821
7.25k
      index &= mask; \
1822
7.25k
      if (index > 0xf) \
1823
7.25k
        *valid = 0; \
1824
7.25k
      return prefix##_RAX + index; \
1825
139k
    case TYPE_ZMM: \
1826
25.5k
      return prefix##_ZMM0 + index; \
1827
139k
    case TYPE_YMM: \
1828
23.9k
      return prefix##_YMM0 + index; \
1829
139k
    case TYPE_XMM: \
1830
51.3k
      return prefix##_XMM0 + index; \
1831
139k
    case TYPE_VK: \
1832
22.7k
      index &= 0xf; \
1833
22.7k
      if (index > 7) \
1834
22.7k
        *valid = 0; \
1835
22.7k
      return prefix##_K0 + index; \
1836
139k
    case TYPE_MM64: \
1837
3.54k
      return prefix##_MM0 + (index & 0x7); \
1838
139k
    case TYPE_SEGMENTREG: \
1839
1.61k
      if ((index & 7) > 5) \
1840
1.61k
        *valid = 0; \
1841
1.61k
      return prefix##_ES + (index & 7); \
1842
139k
    case TYPE_DEBUGREG: \
1843
280
      return prefix##_DR0 + index; \
1844
139k
    case TYPE_CONTROLREG: \
1845
258
      return prefix##_CR0 + index; \
1846
139k
    case TYPE_BNDR: \
1847
5.39k
      if (index > 3) \
1848
5.39k
        *valid = 0; \
1849
5.39k
      return prefix##_BND0 + index; \
1850
139k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
139k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
139k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
371k
    } \
1857
371k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1793
100k
  { \
1794
100k
    *valid = 1; \
1795
100k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
31.1k
    case TYPE_Rv: \
1800
31.1k
      return base + index; \
1801
34.1k
    case TYPE_R8: \
1802
34.1k
      index &= mask; \
1803
34.1k
      if (index > 0xf) \
1804
34.1k
        *valid = 0; \
1805
34.1k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
639
        return prefix##_SPL + (index - 4); \
1807
33.5k
      } else { \
1808
33.5k
        return prefix##_AL + index; \
1809
33.5k
      } \
1810
34.1k
    case TYPE_R16: \
1811
851
      index &= mask; \
1812
851
      if (index > 0xf) \
1813
851
        *valid = 0; \
1814
851
      return prefix##_AX + index; \
1815
34.1k
    case TYPE_R32: \
1816
698
      index &= mask; \
1817
698
      if (index > 0xf) \
1818
698
        *valid = 0; \
1819
698
      return prefix##_EAX + index; \
1820
34.1k
    case TYPE_R64: \
1821
6.57k
      index &= mask; \
1822
6.57k
      if (index > 0xf) \
1823
6.57k
        *valid = 0; \
1824
6.57k
      return prefix##_RAX + index; \
1825
34.1k
    case TYPE_ZMM: \
1826
6.62k
      return prefix##_ZMM0 + index; \
1827
34.1k
    case TYPE_YMM: \
1828
5.74k
      return prefix##_YMM0 + index; \
1829
34.1k
    case TYPE_XMM: \
1830
10.6k
      return prefix##_XMM0 + index; \
1831
34.1k
    case TYPE_VK: \
1832
1.66k
      index &= 0xf; \
1833
1.66k
      if (index > 7) \
1834
1.66k
        *valid = 0; \
1835
1.66k
      return prefix##_K0 + index; \
1836
34.1k
    case TYPE_MM64: \
1837
1.89k
      return prefix##_MM0 + (index & 0x7); \
1838
34.1k
    case TYPE_SEGMENTREG: \
1839
0
      if ((index & 7) > 5) \
1840
0
        *valid = 0; \
1841
0
      return prefix##_ES + (index & 7); \
1842
34.1k
    case TYPE_DEBUGREG: \
1843
0
      return prefix##_DR0 + index; \
1844
34.1k
    case TYPE_CONTROLREG: \
1845
0
      return prefix##_CR0 + index; \
1846
34.1k
    case TYPE_BNDR: \
1847
683
      if (index > 3) \
1848
683
        *valid = 0; \
1849
683
      return prefix##_BND0 + index; \
1850
34.1k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
34.1k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
34.1k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
100k
    } \
1857
100k
  }
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
788k
{
1887
788k
  uint8_t valid;
1888
1889
788k
  switch ((OperandEncoding)op->encoding) {
1890
0
  default:
1891
    // debug("Expected a REG or R/M encoding in fixupReg");
1892
0
    return -1;
1893
48.3k
  case ENCODING_VVVV:
1894
48.3k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1895
48.3k
            insn->vvvv, &valid);
1896
48.3k
    if (!valid)
1897
0
      return -1;
1898
48.3k
    break;
1899
322k
  case ENCODING_REG:
1900
322k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1901
322k
                 insn->reg - insn->regBase,
1902
322k
                 &valid);
1903
322k
    if (!valid)
1904
18
      return -1;
1905
322k
    break;
1906
2.76M
CASE_ENCODING_RM:
1907
2.76M
    if (insn->eaBase >= insn->eaRegBase) {
1908
100k
      insn->eaBase = (EABase)fixupRMValue(
1909
100k
        insn, (OperandType)op->type,
1910
100k
        insn->eaBase - insn->eaRegBase, &valid);
1911
100k
      if (!valid)
1912
2
        return -1;
1913
100k
    }
1914
417k
    break;
1915
788k
  }
1916
1917
788k
  return 0;
1918
788k
}
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
90.3k
{
1933
90.3k
  if (size == 0)
1934
61.7k
    size = insn->registerSize;
1935
1936
90.3k
  switch (size) {
1937
12.2k
  case 1:
1938
12.2k
    insn->opcodeRegister =
1939
12.2k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1940
12.2k
                (insn->opcode & 7)));
1941
12.2k
    if (insn->rexPrefix &&
1942
1.10k
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1943
722
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1944
321
      insn->opcodeRegister =
1945
321
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1946
321
                   MODRM_REG_AL - 4));
1947
321
    }
1948
1949
12.2k
    break;
1950
27.6k
  case 2:
1951
27.6k
    insn->opcodeRegister =
1952
27.6k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1953
27.6k
                (insn->opcode & 7)));
1954
27.6k
    break;
1955
33.7k
  case 4:
1956
33.7k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1957
33.7k
               ((bFromREX(insn->rexPrefix) << 3) |
1958
33.7k
                (insn->opcode & 7)));
1959
33.7k
    break;
1960
16.7k
  case 8:
1961
16.7k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1962
16.7k
               ((bFromREX(insn->rexPrefix) << 3) |
1963
16.7k
                (insn->opcode & 7)));
1964
16.7k
    break;
1965
90.3k
  }
1966
1967
90.3k
  return 0;
1968
90.3k
}
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
211k
{
1981
211k
  uint8_t imm8;
1982
211k
  uint16_t imm16;
1983
211k
  uint32_t imm32;
1984
211k
  uint64_t imm64;
1985
1986
211k
  if (insn->numImmediatesConsumed == 2) {
1987
    // debug("Already consumed two immediates");
1988
0
    return -1;
1989
0
  }
1990
1991
211k
  if (size == 0)
1992
0
    size = insn->immediateSize;
1993
211k
  else
1994
211k
    insn->immediateSize = size;
1995
1996
211k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1997
1998
211k
  switch (size) {
1999
156k
  case 1:
2000
156k
    if (consumeByte(insn, &imm8))
2001
421
      return -1;
2002
2003
155k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
2004
155k
    break;
2005
31.0k
  case 2:
2006
31.0k
    if (consumeUInt16(insn, &imm16))
2007
170
      return -1;
2008
2009
30.8k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
2010
30.8k
    break;
2011
20.9k
  case 4:
2012
20.9k
    if (consumeUInt32(insn, &imm32))
2013
291
      return -1;
2014
2015
20.6k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
2016
20.6k
    break;
2017
3.16k
  case 8:
2018
3.16k
    if (consumeUInt64(insn, &imm64))
2019
78
      return -1;
2020
3.08k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
2021
3.08k
    break;
2022
211k
  }
2023
2024
210k
  insn->numImmediatesConsumed++;
2025
2026
210k
  return 0;
2027
211k
}
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
795k
{
2038
795k
  int vvvv;
2039
2040
795k
  if (insn->vectorExtensionType == TYPE_EVEX)
2041
46.2k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2042
46.2k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2043
749k
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2044
5.50k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2045
743k
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2046
8.09k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2047
735k
  else if (insn->vectorExtensionType == TYPE_XOP)
2048
5.94k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2049
729k
  else
2050
729k
    return -1;
2051
2052
65.7k
  if (insn->mode != MODE_64BIT)
2053
42.8k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2054
2055
65.7k
  insn->vvvv = (Reg)vvvv;
2056
2057
65.7k
  return 0;
2058
795k
}
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
30.4k
{
2069
30.4k
  if (insn->vectorExtensionType != TYPE_EVEX)
2070
0
    return -1;
2071
2072
30.4k
  insn->writemask =
2073
30.4k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2074
2075
30.4k
  return 0;
2076
30.4k
}
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
795k
{
2087
795k
  int hasVVVV, needVVVV;
2088
795k
  int sawRegImm = 0;
2089
795k
  int i;
2090
2091
  /* If non-zero vvvv specified, need to make sure one of the operands
2092
     uses it. */
2093
795k
  hasVVVV = !readVVVV(insn);
2094
795k
  needVVVV = hasVVVV && (insn->vvvv != 0);
2095
2096
5.56M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2097
4.76M
    const OperandSpecifier *op =
2098
4.76M
      &x86OperandSets[insn->spec->operands][i];
2099
4.76M
    switch (op->encoding) {
2100
3.38M
    case ENCODING_NONE:
2101
3.42M
    case ENCODING_SI:
2102
3.46M
    case ENCODING_DI:
2103
3.46M
      break;
2104
2105
23.1k
CASE_ENCODING_VSIB:
2106
      // VSIB can use the V2 bit so check only the other bits.
2107
23.1k
      if (needVVVV)
2108
2.35k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2109
2110
23.1k
      if (readModRM(insn))
2111
0
        return -1;
2112
2113
      // Reject if SIB wasn't used.
2114
4.47k
      if (insn->eaBase != EA_BASE_sib &&
2115
2.21k
          insn->eaBase != EA_BASE_sib64)
2116
9
        return -1;
2117
2118
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2119
4.46k
      if (insn->sibIndex == SIB_INDEX_NONE)
2120
831
        insn->sibIndex =
2121
831
          (SIBIndex)(insn->sibIndexBase + 4);
2122
2123
      // If EVEX.v2 is set this is one of the 16-31 registers.
2124
4.46k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2125
3.57k
          insn->mode == MODE_64BIT &&
2126
2.06k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2127
1.46k
        insn->sibIndex =
2128
1.46k
          (SIBIndex)(insn->sibIndex + 16);
2129
2130
      // Adjust the index register to the correct size.
2131
4.46k
      switch (op->type) {
2132
0
      default:
2133
        // debug("Unhandled VSIB index type");
2134
0
        return -1;
2135
2.68k
      case TYPE_MVSIBX:
2136
2.68k
        insn->sibIndex =
2137
2.68k
          (SIBIndex)(SIB_INDEX_XMM0 +
2138
2.68k
               (insn->sibIndex -
2139
2.68k
                insn->sibIndexBase));
2140
2.68k
        break;
2141
799
      case TYPE_MVSIBY:
2142
799
        insn->sibIndex =
2143
799
          (SIBIndex)(SIB_INDEX_YMM0 +
2144
799
               (insn->sibIndex -
2145
799
                insn->sibIndexBase));
2146
799
        break;
2147
975
      case TYPE_MVSIBZ:
2148
975
        insn->sibIndex =
2149
975
          (SIBIndex)(SIB_INDEX_ZMM0 +
2150
975
               (insn->sibIndex -
2151
975
                insn->sibIndexBase));
2152
975
        break;
2153
4.46k
      }
2154
2155
      // Apply the AVX512 compressed displacement scaling factor.
2156
4.46k
      if (op->encoding != ENCODING_REG &&
2157
4.46k
          insn->eaDisplacement == EA_DISP_8)
2158
636
        insn->displacement *=
2159
636
          1 << (op->encoding - ENCODING_VSIB);
2160
4.46k
      break;
2161
2162
322k
    case ENCODING_REG:
2163
5.02M
CASE_ENCODING_RM:
2164
5.02M
      if (readModRM(insn))
2165
0
        return -1;
2166
2167
740k
      if (fixupReg(insn, op))
2168
20
        return -1;
2169
2170
      // Apply the AVX512 compressed displacement scaling factor.
2171
740k
      if (op->encoding != ENCODING_REG &&
2172
417k
          insn->eaDisplacement == EA_DISP_8)
2173
63.0k
        insn->displacement *=
2174
63.0k
          1 << (op->encoding - ENCODING_RM);
2175
740k
      break;
2176
2177
157k
    case ENCODING_IB:
2178
157k
      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
997
        insn->immediates[insn->numImmediatesConsumed] =
2182
997
          insn->immediates
2183
997
            [insn->numImmediatesConsumed -
2184
997
             1] &
2185
997
          0xf;
2186
997
        ++insn->numImmediatesConsumed;
2187
997
        break;
2188
997
      }
2189
156k
      if (readImmediate(insn, 1))
2190
421
        return -1;
2191
155k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2192
1.25k
        sawRegImm = 1;
2193
155k
      break;
2194
2195
11.7k
    case ENCODING_IW:
2196
11.7k
      if (readImmediate(insn, 2))
2197
54
        return -1;
2198
11.7k
      break;
2199
2200
11.7k
    case ENCODING_ID:
2201
3.25k
      if (readImmediate(insn, 4))
2202
45
        return -1;
2203
3.20k
      break;
2204
2205
3.20k
    case ENCODING_IO:
2206
705
      if (readImmediate(insn, 8))
2207
19
        return -1;
2208
686
      break;
2209
2210
30.5k
    case ENCODING_Iv:
2211
30.5k
      if (readImmediate(insn, insn->immediateSize))
2212
316
        return -1;
2213
30.2k
      break;
2214
2215
30.2k
    case ENCODING_Ia:
2216
8.79k
      if (readImmediate(insn, insn->addressSize))
2217
105
        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
8.68k
      insn->displacementOffset = insn->immediateOffset;
2222
8.68k
      insn->consumedDisplacement = true;
2223
8.68k
      insn->displacementSize = insn->immediateSize;
2224
8.68k
      insn->displacement =
2225
8.68k
        insn->immediates[insn->numImmediatesConsumed -
2226
8.68k
             1];
2227
8.68k
      insn->immediateOffset = 0;
2228
8.68k
      insn->immediateSize = 0;
2229
8.68k
      break;
2230
2231
2.41k
    case ENCODING_IRC:
2232
2.41k
      insn->RC =
2233
2.41k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2234
2.41k
         << 1) |
2235
2.41k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2236
2.41k
      break;
2237
2238
12.2k
    case ENCODING_RB:
2239
12.2k
      if (readOpcodeRegister(insn, 1))
2240
0
        return -1;
2241
12.2k
      break;
2242
2243
12.2k
    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
16.3k
    case ENCODING_RO:
2254
16.3k
      if (readOpcodeRegister(insn, 8))
2255
0
        return -1;
2256
16.3k
      break;
2257
2258
61.7k
    case ENCODING_Rv:
2259
61.7k
      if (readOpcodeRegister(insn, 0))
2260
0
        return -1;
2261
61.7k
      break;
2262
2263
61.7k
    case ENCODING_FP:
2264
3.15k
      break;
2265
2266
48.3k
    case ENCODING_VVVV:
2267
48.3k
      if (!hasVVVV)
2268
0
        return -1;
2269
2270
48.3k
      needVVVV =
2271
48.3k
        0; /* Mark that we have found a VVVV operand. */
2272
2273
48.3k
      if (insn->mode != MODE_64BIT)
2274
32.4k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2275
2276
48.3k
      if (fixupReg(insn, op))
2277
0
        return -1;
2278
48.3k
      break;
2279
2280
48.3k
    case ENCODING_WRITEMASK:
2281
30.4k
      if (readMaskRegister(insn))
2282
0
        return -1;
2283
30.4k
      break;
2284
2285
171k
    case ENCODING_DUP:
2286
171k
      break;
2287
2288
0
    default:
2289
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2290
0
      return -1;
2291
4.76M
    }
2292
4.76M
  }
2293
2294
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2295
794k
  if (needVVVV)
2296
12
    return -1;
2297
2298
794k
  return 0;
2299
794k
}
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
795k
{
2305
  // LOCK prefix
2306
795k
  if (insn->hasLockPrefix) {
2307
32.7k
    switch (insn->instructionID) {
2308
217
    default:
2309
      // invalid LOCK
2310
217
      return true;
2311
2312
    // nop dword [rax]
2313
34
    case X86_NOOPL:
2314
2315
    // DEC
2316
100
    case X86_DEC16m:
2317
324
    case X86_DEC32m:
2318
424
    case X86_DEC64m:
2319
505
    case X86_DEC8m:
2320
2321
    // ADC
2322
622
    case X86_ADC16mi:
2323
979
    case X86_ADC16mi8:
2324
1.09k
    case X86_ADC16mr:
2325
1.17k
    case X86_ADC32mi:
2326
1.82k
    case X86_ADC32mi8:
2327
1.92k
    case X86_ADC32mr:
2328
2.22k
    case X86_ADC64mi32:
2329
2.33k
    case X86_ADC64mi8:
2330
2.44k
    case X86_ADC64mr:
2331
2.58k
    case X86_ADC8mi:
2332
2.65k
    case X86_ADC8mi8:
2333
3.30k
    case X86_ADC8mr:
2334
3.48k
    case X86_ADC8rm:
2335
3.78k
    case X86_ADC16rm:
2336
3.85k
    case X86_ADC32rm:
2337
3.95k
    case X86_ADC64rm:
2338
2339
    // ADD
2340
4.15k
    case X86_ADD16mi:
2341
4.43k
    case X86_ADD16mi8:
2342
4.80k
    case X86_ADD16mr:
2343
5.04k
    case X86_ADD32mi:
2344
5.36k
    case X86_ADD32mi8:
2345
5.85k
    case X86_ADD32mr:
2346
5.91k
    case X86_ADD64mi32:
2347
6.08k
    case X86_ADD64mi8:
2348
6.54k
    case X86_ADD64mr:
2349
6.74k
    case X86_ADD8mi:
2350
6.85k
    case X86_ADD8mi8:
2351
7.39k
    case X86_ADD8mr:
2352
7.88k
    case X86_ADD8rm:
2353
8.57k
    case X86_ADD16rm:
2354
8.84k
    case X86_ADD32rm:
2355
9.01k
    case X86_ADD64rm:
2356
2357
    // AND
2358
9.18k
    case X86_AND16mi:
2359
9.31k
    case X86_AND16mi8:
2360
9.56k
    case X86_AND16mr:
2361
9.89k
    case X86_AND32mi:
2362
10.0k
    case X86_AND32mi8:
2363
10.1k
    case X86_AND32mr:
2364
10.1k
    case X86_AND64mi32:
2365
10.3k
    case X86_AND64mi8:
2366
10.3k
    case X86_AND64mr:
2367
10.6k
    case X86_AND8mi:
2368
10.7k
    case X86_AND8mi8:
2369
11.0k
    case X86_AND8mr:
2370
11.2k
    case X86_AND8rm:
2371
11.4k
    case X86_AND16rm:
2372
11.7k
    case X86_AND32rm:
2373
11.8k
    case X86_AND64rm:
2374
2375
    // BTC
2376
12.0k
    case X86_BTC16mi8:
2377
12.1k
    case X86_BTC16mr:
2378
12.1k
    case X86_BTC32mi8:
2379
12.2k
    case X86_BTC32mr:
2380
12.3k
    case X86_BTC64mi8:
2381
12.4k
    case X86_BTC64mr:
2382
2383
    // BTR
2384
12.6k
    case X86_BTR16mi8:
2385
12.9k
    case X86_BTR16mr:
2386
13.0k
    case X86_BTR32mi8:
2387
13.3k
    case X86_BTR32mr:
2388
13.4k
    case X86_BTR64mi8:
2389
13.5k
    case X86_BTR64mr:
2390
2391
    // BTS
2392
13.5k
    case X86_BTS16mi8:
2393
13.7k
    case X86_BTS16mr:
2394
13.7k
    case X86_BTS32mi8:
2395
13.9k
    case X86_BTS32mr:
2396
14.1k
    case X86_BTS64mi8:
2397
14.2k
    case X86_BTS64mr:
2398
2399
    // CMPXCHG
2400
14.5k
    case X86_CMPXCHG16B:
2401
14.5k
    case X86_CMPXCHG16rm:
2402
14.6k
    case X86_CMPXCHG32rm:
2403
14.8k
    case X86_CMPXCHG64rm:
2404
15.4k
    case X86_CMPXCHG8rm:
2405
15.5k
    case X86_CMPXCHG8B:
2406
2407
    // INC
2408
15.6k
    case X86_INC16m:
2409
15.7k
    case X86_INC32m:
2410
15.7k
    case X86_INC64m:
2411
16.0k
    case X86_INC8m:
2412
2413
    // NEG
2414
16.2k
    case X86_NEG16m:
2415
16.3k
    case X86_NEG32m:
2416
16.3k
    case X86_NEG64m:
2417
16.4k
    case X86_NEG8m:
2418
2419
    // NOT
2420
16.5k
    case X86_NOT16m:
2421
16.6k
    case X86_NOT32m:
2422
16.8k
    case X86_NOT64m:
2423
17.1k
    case X86_NOT8m:
2424
2425
    // OR
2426
17.4k
    case X86_OR16mi:
2427
18.2k
    case X86_OR16mi8:
2428
18.2k
    case X86_OR16mr:
2429
18.5k
    case X86_OR32mi:
2430
18.9k
    case X86_OR32mi8:
2431
19.1k
    case X86_OR32mr:
2432
19.3k
    case X86_OR64mi32:
2433
19.4k
    case X86_OR64mi8:
2434
19.5k
    case X86_OR64mr:
2435
19.7k
    case X86_OR8mi8:
2436
20.3k
    case X86_OR8mi:
2437
20.4k
    case X86_OR8mr:
2438
20.6k
    case X86_OR8rm:
2439
20.9k
    case X86_OR16rm:
2440
21.2k
    case X86_OR32rm:
2441
21.4k
    case X86_OR64rm:
2442
2443
    // SBB
2444
21.6k
    case X86_SBB16mi:
2445
21.9k
    case X86_SBB16mi8:
2446
22.0k
    case X86_SBB16mr:
2447
22.1k
    case X86_SBB32mi:
2448
22.3k
    case X86_SBB32mi8:
2449
22.5k
    case X86_SBB32mr:
2450
22.5k
    case X86_SBB64mi32:
2451
22.8k
    case X86_SBB64mi8:
2452
22.8k
    case X86_SBB64mr:
2453
23.0k
    case X86_SBB8mi:
2454
23.2k
    case X86_SBB8mi8:
2455
23.3k
    case X86_SBB8mr:
2456
2457
    // SUB
2458
23.7k
    case X86_SUB16mi:
2459
23.7k
    case X86_SUB16mi8:
2460
23.9k
    case X86_SUB16mr:
2461
24.0k
    case X86_SUB32mi:
2462
24.2k
    case X86_SUB32mi8:
2463
24.3k
    case X86_SUB32mr:
2464
24.6k
    case X86_SUB64mi32:
2465
24.8k
    case X86_SUB64mi8:
2466
25.2k
    case X86_SUB64mr:
2467
25.4k
    case X86_SUB8mi8:
2468
25.5k
    case X86_SUB8mi:
2469
25.7k
    case X86_SUB8mr:
2470
25.9k
    case X86_SUB8rm:
2471
26.3k
    case X86_SUB16rm:
2472
26.7k
    case X86_SUB32rm:
2473
26.9k
    case X86_SUB64rm:
2474
2475
    // XADD
2476
27.1k
    case X86_XADD16rm:
2477
27.1k
    case X86_XADD32rm:
2478
27.4k
    case X86_XADD64rm:
2479
27.7k
    case X86_XADD8rm:
2480
2481
    // XCHG
2482
28.1k
    case X86_XCHG16rm:
2483
28.4k
    case X86_XCHG32rm:
2484
28.5k
    case X86_XCHG64rm:
2485
28.5k
    case X86_XCHG8rm:
2486
2487
    // XOR
2488
28.7k
    case X86_XOR16mi:
2489
29.0k
    case X86_XOR16mi8:
2490
29.2k
    case X86_XOR16mr:
2491
29.3k
    case X86_XOR32mi:
2492
29.5k
    case X86_XOR32mi8:
2493
29.6k
    case X86_XOR32mr:
2494
29.9k
    case X86_XOR64mi32:
2495
30.1k
    case X86_XOR64mi8:
2496
30.4k
    case X86_XOR64mr:
2497
30.6k
    case X86_XOR8mi8:
2498
31.0k
    case X86_XOR8mi:
2499
31.4k
    case X86_XOR8mr:
2500
31.9k
    case X86_XOR8rm:
2501
32.2k
    case X86_XOR16rm:
2502
32.4k
    case X86_XOR32rm:
2503
32.5k
    case X86_XOR64rm:
2504
2505
      // this instruction can be used with LOCK prefix
2506
32.5k
      return false;
2507
32.7k
    }
2508
32.7k
  }
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
763k
  return false;
2523
795k
}
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
799k
{
2544
799k
  insn->reader = reader;
2545
799k
  insn->readerArg = readerArg;
2546
799k
  insn->startLocation = startLoc;
2547
799k
  insn->readerCursor = startLoc;
2548
799k
  insn->mode = mode;
2549
799k
  insn->numImmediatesConsumed = 0;
2550
2551
799k
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2552
797k
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2553
5.08k
    return -1;
2554
2555
794k
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2556
2557
  // instruction length must be <= 15 to be valid
2558
794k
  if (insn->length > 15)
2559
48
    return -1;
2560
2561
794k
  if (insn->operandSize == 0)
2562
794k
    insn->operandSize = insn->registerSize;
2563
2564
794k
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2565
2566
794k
  return 0;
2567
794k
}
2568
2569
#endif