Coverage Report

Created: 2025-10-10 06:20

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
319k
{
79
319k
  return CONTEXTS_SYM[attrMask];
80
319k
}
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
319k
{
96
319k
  const struct OpcodeDecision *decision = NULL;
97
319k
  const uint8_t *indextable = NULL;
98
319k
  unsigned int index;
99
100
319k
  switch (type) {
101
0
  default:
102
0
    break;
103
259k
  case ONEBYTE:
104
259k
    decision = ONEBYTE_SYM;
105
259k
    indextable = index_x86DisassemblerOneByteOpcodes;
106
259k
    break;
107
28.7k
  case TWOBYTE:
108
28.7k
    decision = TWOBYTE_SYM;
109
28.7k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
28.7k
    break;
111
9.51k
  case THREEBYTE_38:
112
9.51k
    decision = THREEBYTE38_SYM;
113
9.51k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
9.51k
    break;
115
16.7k
  case THREEBYTE_3A:
116
16.7k
    decision = THREEBYTE3A_SYM;
117
16.7k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
16.7k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
4.33k
  case XOP8_MAP:
121
4.33k
    decision = XOP8_MAP_SYM;
122
4.33k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
4.33k
    break;
124
631
  case XOP9_MAP:
125
631
    decision = XOP9_MAP_SYM;
126
631
    indextable = index_x86DisassemblerXOP9Opcodes;
127
631
    break;
128
100
  case XOPA_MAP:
129
100
    decision = XOPA_MAP_SYM;
130
100
    indextable = index_x86DisassemblerXOPAOpcodes;
131
100
    break;
132
336
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
336
    return true;
135
319k
#endif
136
319k
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
319k
  index = indextable[insnContext];
140
319k
  if (index)
141
317k
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
317k
           MODRM_ONEENTRY;
143
1.76k
  else
144
1.76k
    return false;
145
319k
}
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
318k
{
160
318k
  const struct ModRMDecision *dec = NULL;
161
318k
  unsigned int index;
162
318k
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
318k
  switch (type) {
165
0
  default:
166
0
    break; // never reach
167
258k
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
258k
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
258k
    if (index)
171
258k
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
25
    else
173
25
      dec = &emptyDecision.modRMDecisions[opcode];
174
258k
    break;
175
28.7k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
28.7k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
28.7k
    if (index)
179
28.3k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
372
    else
181
372
      dec = &emptyDecision.modRMDecisions[opcode];
182
28.7k
    break;
183
9.50k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
9.50k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
9.50k
    if (index)
187
9.30k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
204
    else
189
204
      dec = &emptyDecision.modRMDecisions[opcode];
190
9.50k
    break;
191
16.7k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
16.7k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
16.7k
    if (index)
195
16.5k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
121
    else
197
121
      dec = &emptyDecision.modRMDecisions[opcode];
198
16.7k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
4.33k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
4.33k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
4.33k
    if (index)
204
3.40k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
923
    else
206
923
      dec = &emptyDecision.modRMDecisions[opcode];
207
4.33k
    break;
208
631
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
631
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
631
    if (index)
212
537
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
94
    else
214
94
      dec = &emptyDecision.modRMDecisions[opcode];
215
631
    break;
216
100
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
100
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
100
    if (index)
220
70
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
30
    else
222
30
      dec = &emptyDecision.modRMDecisions[opcode];
223
100
    break;
224
336
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
336
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
336
    if (index)
228
268
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
268
               .modRMDecisions[opcode];
230
68
    else
231
68
      dec = &emptyDecision.modRMDecisions[opcode];
232
336
    break;
233
318k
#endif
234
318k
  }
235
236
318k
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
144k
  case MODRM_ONEENTRY:
241
144k
    return modRMTable[dec->instructionIDs];
242
133k
  case MODRM_SPLITRM:
243
133k
    if (modFromModRM(modRM) == 0x3)
244
31.9k
      return modRMTable[dec->instructionIDs + 1];
245
101k
    return modRMTable[dec->instructionIDs];
246
34.4k
  case MODRM_SPLITREG:
247
34.4k
    if (modFromModRM(modRM) == 0x3)
248
9.83k
      return modRMTable[dec->instructionIDs +
249
9.83k
            ((modRM & 0x38) >> 3) + 8];
250
24.5k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
6.35k
  case MODRM_SPLITMISC:
252
6.35k
    if (modFromModRM(modRM) == 0x3)
253
1.72k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
1.72k
            8];
255
4.63k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
318k
  }
259
318k
}
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
267k
{
271
267k
  return &INSTRUCTIONS_SYM[uid];
272
267k
}
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
886k
{
286
886k
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
886k
  if (!ret)
289
885k
    ++(insn->readerCursor);
290
291
886k
  return ret;
292
886k
}
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
108k
{
303
108k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
108k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
294k
{
308
294k
  insn->readerCursor--;
309
294k
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
46.4k
  { \
314
46.4k
    type combined = 0; \
315
46.4k
    unsigned offset; \
316
144k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
98.2k
      uint8_t byte; \
318
98.2k
      int ret = insn->reader(insn->readerArg, &byte, \
319
98.2k
                 insn->readerCursor + offset); \
320
98.2k
      if (ret) \
321
98.2k
        return ret; \
322
98.2k
      combined = combined | \
323
97.8k
           ((uint64_t)byte << (offset * 8)); \
324
97.8k
    } \
325
46.4k
    *ptr = combined; \
326
46.0k
    insn->readerCursor += sizeof(type); \
327
46.0k
    return 0; \
328
46.4k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
22.0k
  { \
314
22.0k
    type combined = 0; \
315
22.0k
    unsigned offset; \
316
43.9k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
22.0k
      uint8_t byte; \
318
22.0k
      int ret = insn->reader(insn->readerArg, &byte, \
319
22.0k
                 insn->readerCursor + offset); \
320
22.0k
      if (ret) \
321
22.0k
        return ret; \
322
22.0k
      combined = combined | \
323
21.9k
           ((uint64_t)byte << (offset * 8)); \
324
21.9k
    } \
325
22.0k
    *ptr = combined; \
326
21.9k
    insn->readerCursor += sizeof(type); \
327
21.9k
    return 0; \
328
22.0k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
4.49k
  { \
314
4.49k
    type combined = 0; \
315
4.49k
    unsigned offset; \
316
13.4k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
8.98k
      uint8_t byte; \
318
8.98k
      int ret = insn->reader(insn->readerArg, &byte, \
319
8.98k
                 insn->readerCursor + offset); \
320
8.98k
      if (ret) \
321
8.98k
        return ret; \
322
8.98k
      combined = combined | \
323
8.96k
           ((uint64_t)byte << (offset * 8)); \
324
8.96k
    } \
325
4.49k
    *ptr = combined; \
326
4.47k
    insn->readerCursor += sizeof(type); \
327
4.47k
    return 0; \
328
4.49k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
5.04k
  { \
314
5.04k
    type combined = 0; \
315
5.04k
    unsigned offset; \
316
24.9k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
20.0k
      uint8_t byte; \
318
20.0k
      int ret = insn->reader(insn->readerArg, &byte, \
319
20.0k
                 insn->readerCursor + offset); \
320
20.0k
      if (ret) \
321
20.0k
        return ret; \
322
20.0k
      combined = combined | \
323
19.9k
           ((uint64_t)byte << (offset * 8)); \
324
19.9k
    } \
325
5.04k
    *ptr = combined; \
326
4.95k
    insn->readerCursor += sizeof(type); \
327
4.95k
    return 0; \
328
5.04k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
7.93k
  { \
314
7.93k
    type combined = 0; \
315
7.93k
    unsigned offset; \
316
23.6k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
15.8k
      uint8_t byte; \
318
15.8k
      int ret = insn->reader(insn->readerArg, &byte, \
319
15.8k
                 insn->readerCursor + offset); \
320
15.8k
      if (ret) \
321
15.8k
        return ret; \
322
15.8k
      combined = combined | \
323
15.7k
           ((uint64_t)byte << (offset * 8)); \
324
15.7k
    } \
325
7.93k
    *ptr = combined; \
326
7.86k
    insn->readerCursor += sizeof(type); \
327
7.86k
    return 0; \
328
7.93k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
5.96k
  { \
314
5.96k
    type combined = 0; \
315
5.96k
    unsigned offset; \
316
29.4k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
23.6k
      uint8_t byte; \
318
23.6k
      int ret = insn->reader(insn->readerArg, &byte, \
319
23.6k
                 insn->readerCursor + offset); \
320
23.6k
      if (ret) \
321
23.6k
        return ret; \
322
23.6k
      combined = combined | \
323
23.5k
           ((uint64_t)byte << (offset * 8)); \
324
23.5k
    } \
325
5.96k
    *ptr = combined; \
326
5.85k
    insn->readerCursor += sizeof(type); \
327
5.85k
    return 0; \
328
5.96k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
986
  { \
314
986
    type combined = 0; \
315
986
    unsigned offset; \
316
8.76k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
7.79k
      uint8_t byte; \
318
7.79k
      int ret = insn->reader(insn->readerArg, &byte, \
319
7.79k
                 insn->readerCursor + offset); \
320
7.79k
      if (ret) \
321
7.79k
        return ret; \
322
7.79k
      combined = combined | \
323
7.77k
           ((uint64_t)byte << (offset * 8)); \
324
7.77k
    } \
325
986
    *ptr = combined; \
326
963
    insn->readerCursor += sizeof(type); \
327
963
    return 0; \
328
986
  }
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
246k
{
349
246k
  if (insn->mode == MODE_64BIT)
350
84.4k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
162k
  return false;
353
246k
}
354
355
/*
356
 * setPrefixPresent - Marks that a particular prefix is present as mandatory
357
 *
358
 * @param insn      - The instruction to be marked as having the prefix.
359
 * @param prefix    - The prefix that is present.
360
 */
361
static void setPrefixPresent(struct InternalInstruction *insn, uint8_t prefix)
362
51.0k
{
363
51.0k
  uint8_t nextByte;
364
365
51.0k
  switch (prefix) {
366
11.7k
  case 0xf0: // LOCK
367
11.7k
    insn->hasLockPrefix = true;
368
11.7k
    insn->repeatPrefix = 0;
369
11.7k
    break;
370
371
9.52k
  case 0xf2: // REPNE/REPNZ
372
21.0k
  case 0xf3: // REP or REPE/REPZ
373
21.0k
    if (lookAtByte(insn, &nextByte))
374
19
      break;
375
    // TODO:
376
    //  1. There could be several 0x66
377
    //  2. if (nextByte == 0x66) and nextNextByte != 0x0f then
378
    //      it's not mandatory prefix
379
    //  3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
380
    //     0x0f exactly after it to be mandatory prefix
381
21.0k
    if (isREX(insn, nextByte) || nextByte == 0x0f ||
382
15.8k
        nextByte == 0x66)
383
      // The last of 0xf2 /0xf3 is mandatory prefix
384
5.58k
      insn->mandatoryPrefix = prefix;
385
386
21.0k
    insn->repeatPrefix = prefix;
387
21.0k
    insn->hasLockPrefix = false;
388
21.0k
    break;
389
390
6.21k
  case 0x66:
391
6.21k
    if (lookAtByte(insn, &nextByte))
392
18
      break;
393
    // 0x66 can't overwrite existing mandatory prefix and should be ignored
394
6.20k
    if (!insn->mandatoryPrefix &&
395
5.59k
        (nextByte == 0x0f || isREX(insn, nextByte)))
396
1.99k
      insn->mandatoryPrefix = prefix;
397
6.20k
    break;
398
51.0k
  }
399
51.0k
}
400
401
/*
402
 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
403
 *   instruction as having them.  Also sets the instruction's default operand,
404
 *   address, and other relevant data sizes to report operands correctly.
405
 *
406
 * @param insn  - The instruction whose prefixes are to be read.
407
 * @return      - 0 if the instruction could be read until the end of the prefix
408
 *                bytes, and no prefixes conflicted; nonzero otherwise.
409
 */
410
static int readPrefixes(struct InternalInstruction *insn)
411
233k
{
412
233k
  bool isPrefix = true;
413
233k
  uint8_t byte = 0;
414
233k
  uint8_t nextByte;
415
416
517k
  while (isPrefix) {
417
284k
    if (insn->mode == MODE_64BIT) {
418
      // eliminate consecutive redundant REX bytes in front
419
98.5k
      if (consumeByte(insn, &byte))
420
50
        return -1;
421
422
98.5k
      if ((byte & 0xf0) == 0x40) {
423
20.2k
        while (true) {
424
20.2k
          if (lookAtByte(
425
20.2k
                insn,
426
20.2k
                &byte)) // out of input code
427
36
            return -1;
428
20.1k
          if ((byte & 0xf0) == 0x40) {
429
            // another REX prefix, but we only remember the last one
430
3.36k
            if (consumeByte(insn, &byte))
431
0
              return -1;
432
3.36k
          } else
433
16.8k
            break;
434
20.1k
        }
435
436
        // recover the last REX byte if next byte is not a legacy prefix
437
16.8k
        switch (byte) {
438
242
        case 0xf2: /* REPNE/REPNZ */
439
1.21k
        case 0xf3: /* REP or REPE/REPZ */
440
1.96k
        case 0xf0: /* LOCK */
441
2.15k
        case 0x2e: /* CS segment override -OR- Branch not taken */
442
2.17k
        case 0x36: /* SS segment override -OR- Branch taken */
443
2.36k
        case 0x3e: /* DS segment override */
444
2.37k
        case 0x26: /* ES segment override */
445
2.58k
        case 0x64: /* FS segment override */
446
2.61k
        case 0x65: /* GS segment override */
447
2.84k
        case 0x66: /* Operand-size override */
448
3.27k
        case 0x67: /* Address-size override */
449
3.27k
          break;
450
13.5k
        default: /* Not a prefix byte */
451
13.5k
          unconsumeByte(insn);
452
13.5k
          break;
453
16.8k
        }
454
81.6k
      } else {
455
81.6k
        unconsumeByte(insn);
456
81.6k
      }
457
98.5k
    }
458
459
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
460
284k
    if (consumeByte(insn, &byte))
461
75
      return -1;
462
463
284k
    if (insn->readerCursor - 1 == insn->startLocation &&
464
231k
        (byte == 0xf2 || byte == 0xf3)) {
465
      // prefix requires next byte
466
16.8k
      if (lookAtByte(insn, &nextByte))
467
40
        return -1;
468
469
      /*
470
       * If the byte is 0xf2 or 0xf3, and any of the following conditions are
471
       * met:
472
       * - it is followed by a LOCK (0xf0) prefix
473
       * - it is followed by an xchg instruction
474
       * then it should be disassembled as a xacquire/xrelease not repne/rep.
475
       */
476
16.8k
      if (((nextByte == 0xf0) ||
477
16.1k
           ((nextByte & 0xfe) == 0x86 ||
478
15.7k
            (nextByte & 0xf8) == 0x90))) {
479
1.13k
        insn->xAcquireRelease = byte;
480
1.13k
      }
481
482
      /*
483
       * Also if the byte is 0xf3, and the following condition is met:
484
       * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
485
       *                       "mov mem, imm" (opcode 0xc6/0xc7) instructions.
486
       * then it should be disassembled as an xrelease not rep.
487
       */
488
16.8k
      if (byte == 0xf3 &&
489
9.25k
          (nextByte == 0x88 || nextByte == 0x89 ||
490
9.08k
           nextByte == 0xc6 || nextByte == 0xc7)) {
491
188
        insn->xAcquireRelease = byte;
492
188
      }
493
494
16.8k
      if (isREX(insn, nextByte)) {
495
2.52k
        uint8_t nnextByte;
496
497
        // Go to REX prefix after the current one
498
2.52k
        if (consumeByte(insn, &nnextByte))
499
0
          return -1;
500
501
        // We should be able to read next byte after REX prefix
502
2.52k
        if (lookAtByte(insn, &nnextByte))
503
1
          return -1;
504
505
2.52k
        unconsumeByte(insn);
506
2.52k
      }
507
16.8k
    }
508
509
284k
    switch (byte) {
510
11.7k
    case 0xf0: /* LOCK */
511
21.2k
    case 0xf2: /* REPNE/REPNZ */
512
32.7k
    case 0xf3: /* REP or REPE/REPZ */
513
      // only accept the last prefix
514
32.7k
      setPrefixPresent(insn, byte);
515
32.7k
      insn->prefix0 = byte;
516
32.7k
      break;
517
518
3.78k
    case 0x2e: /* CS segment override -OR- Branch not taken */
519
4.38k
    case 0x36: /* SS segment override -OR- Branch taken */
520
5.29k
    case 0x3e: /* DS segment override */
521
6.65k
    case 0x26: /* ES segment override */
522
7.90k
    case 0x64: /* FS segment override */
523
9.26k
    case 0x65: /* GS segment override */
524
9.26k
      switch (byte) {
525
3.78k
      case 0x2e:
526
3.78k
        insn->segmentOverride = SEG_OVERRIDE_CS;
527
3.78k
        insn->prefix1 = byte;
528
3.78k
        break;
529
599
      case 0x36:
530
599
        insn->segmentOverride = SEG_OVERRIDE_SS;
531
599
        insn->prefix1 = byte;
532
599
        break;
533
909
      case 0x3e:
534
909
        insn->segmentOverride = SEG_OVERRIDE_DS;
535
909
        insn->prefix1 = byte;
536
909
        break;
537
1.36k
      case 0x26:
538
1.36k
        insn->segmentOverride = SEG_OVERRIDE_ES;
539
1.36k
        insn->prefix1 = byte;
540
1.36k
        break;
541
1.25k
      case 0x64:
542
1.25k
        insn->segmentOverride = SEG_OVERRIDE_FS;
543
1.25k
        insn->prefix1 = byte;
544
1.25k
        break;
545
1.35k
      case 0x65:
546
1.35k
        insn->segmentOverride = SEG_OVERRIDE_GS;
547
1.35k
        insn->prefix1 = byte;
548
1.35k
        break;
549
0
      default:
550
        // debug("Unhandled override");
551
0
        return -1;
552
9.26k
      }
553
9.26k
      setPrefixPresent(insn, byte);
554
9.26k
      break;
555
556
6.21k
    case 0x66: /* Operand-size override */
557
6.21k
      insn->hasOpSize = true;
558
6.21k
      setPrefixPresent(insn, byte);
559
6.21k
      insn->prefix2 = byte;
560
6.21k
      break;
561
562
2.73k
    case 0x67: /* Address-size override */
563
2.73k
      insn->hasAdSize = true;
564
2.73k
      setPrefixPresent(insn, byte);
565
2.73k
      insn->prefix3 = byte;
566
2.73k
      break;
567
233k
    default: /* Not a prefix byte */
568
233k
      isPrefix = false;
569
233k
      break;
570
284k
    }
571
284k
  }
572
573
233k
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
574
575
233k
  if (byte == 0x62) {
576
18.8k
    uint8_t byte1, byte2;
577
578
18.8k
    if (consumeByte(insn, &byte1)) {
579
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
580
21
      return -1;
581
21
    }
582
583
18.8k
    if (lookAtByte(insn, &byte2)) {
584
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
585
26
      unconsumeByte(insn); /* unconsume byte1 */
586
26
      unconsumeByte(insn); /* unconsume byte  */
587
18.7k
    } else {
588
18.7k
      if ((insn->mode == MODE_64BIT ||
589
11.8k
           (byte1 & 0xc0) == 0xc0) &&
590
17.1k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
591
17.1k
        insn->vectorExtensionType = TYPE_EVEX;
592
17.1k
      } else {
593
1.65k
        unconsumeByte(insn); /* unconsume byte1 */
594
1.65k
        unconsumeByte(insn); /* unconsume byte  */
595
1.65k
      }
596
18.7k
    }
597
598
18.8k
    if (insn->vectorExtensionType == TYPE_EVEX) {
599
17.1k
      insn->vectorExtensionPrefix[0] = byte;
600
17.1k
      insn->vectorExtensionPrefix[1] = byte1;
601
17.1k
      if (consumeByte(insn,
602
17.1k
          &insn->vectorExtensionPrefix[2])) {
603
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
604
0
        return -1;
605
0
      }
606
607
17.1k
      if (consumeByte(insn,
608
17.1k
          &insn->vectorExtensionPrefix[3])) {
609
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
610
6
        return -1;
611
6
      }
612
613
      /* We simulate the REX prefix for simplicity's sake */
614
17.1k
      if (insn->mode == MODE_64BIT) {
615
6.90k
        insn->rexPrefix =
616
6.90k
          0x40 |
617
6.90k
          (wFromEVEX3of4(
618
6.90k
             insn->vectorExtensionPrefix[2])
619
6.90k
           << 3) |
620
6.90k
          (rFromEVEX2of4(
621
6.90k
             insn->vectorExtensionPrefix[1])
622
6.90k
           << 2) |
623
6.90k
          (xFromEVEX2of4(
624
6.90k
             insn->vectorExtensionPrefix[1])
625
6.90k
           << 1) |
626
6.90k
          (bFromEVEX2of4(
627
6.90k
             insn->vectorExtensionPrefix[1])
628
6.90k
           << 0);
629
6.90k
      }
630
631
      // dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
632
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
633
      //    insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
634
17.1k
    }
635
214k
  } else if (byte == 0xc4) {
636
3.06k
    uint8_t byte1;
637
638
3.06k
    if (lookAtByte(insn, &byte1)) {
639
      // dbgprintf(insn, "Couldn't read second byte of VEX");
640
1
      return -1;
641
1
    }
642
643
3.06k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
644
2.63k
      insn->vectorExtensionType = TYPE_VEX_3B;
645
429
    else
646
429
      unconsumeByte(insn);
647
648
3.06k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
649
2.63k
      insn->vectorExtensionPrefix[0] = byte;
650
2.63k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
651
2.63k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
652
653
      /* We simulate the REX prefix for simplicity's sake */
654
2.63k
      if (insn->mode == MODE_64BIT)
655
810
        insn->rexPrefix =
656
810
          0x40 |
657
810
          (wFromVEX3of3(
658
810
             insn->vectorExtensionPrefix[2])
659
810
           << 3) |
660
810
          (rFromVEX2of3(
661
810
             insn->vectorExtensionPrefix[1])
662
810
           << 2) |
663
810
          (xFromVEX2of3(
664
810
             insn->vectorExtensionPrefix[1])
665
810
           << 1) |
666
810
          (bFromVEX2of3(
667
810
             insn->vectorExtensionPrefix[1])
668
810
           << 0);
669
670
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
671
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
672
      //    insn->vectorExtensionPrefix[2]);
673
2.63k
    }
674
211k
  } else if (byte == 0xc5) {
675
3.64k
    uint8_t byte1;
676
677
3.64k
    if (lookAtByte(insn, &byte1)) {
678
      // dbgprintf(insn, "Couldn't read second byte of VEX");
679
6
      return -1;
680
6
    }
681
682
3.64k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
683
2.79k
      insn->vectorExtensionType = TYPE_VEX_2B;
684
847
    else
685
847
      unconsumeByte(insn);
686
687
3.64k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
688
2.79k
      insn->vectorExtensionPrefix[0] = byte;
689
2.79k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
690
691
2.79k
      if (insn->mode == MODE_64BIT)
692
439
        insn->rexPrefix =
693
439
          0x40 |
694
439
          (rFromVEX2of2(
695
439
             insn->vectorExtensionPrefix[1])
696
439
           << 2);
697
698
2.79k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
699
1.46k
      default:
700
1.46k
        break;
701
1.46k
      case VEX_PREFIX_66:
702
1.32k
        insn->hasOpSize = true;
703
1.32k
        break;
704
2.79k
      }
705
706
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
707
      //    insn->vectorExtensionPrefix[0],
708
      //    insn->vectorExtensionPrefix[1]);
709
2.79k
    }
710
207k
  } else if (byte == 0x8f) {
711
2.88k
    uint8_t byte1;
712
713
2.88k
    if (lookAtByte(insn, &byte1)) {
714
      // dbgprintf(insn, "Couldn't read second byte of XOP");
715
1
      return -1;
716
1
    }
717
718
2.88k
    if ((byte1 & 0x38) !=
719
2.88k
        0x0) /* 0 in these 3 bits is a POP instruction. */
720
2.40k
      insn->vectorExtensionType = TYPE_XOP;
721
482
    else
722
482
      unconsumeByte(insn);
723
724
2.88k
    if (insn->vectorExtensionType == TYPE_XOP) {
725
2.40k
      insn->vectorExtensionPrefix[0] = byte;
726
2.40k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
727
2.40k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
728
729
      /* We simulate the REX prefix for simplicity's sake */
730
2.40k
      if (insn->mode == MODE_64BIT)
731
817
        insn->rexPrefix =
732
817
          0x40 |
733
817
          (wFromXOP3of3(
734
817
             insn->vectorExtensionPrefix[2])
735
817
           << 3) |
736
817
          (rFromXOP2of3(
737
817
             insn->vectorExtensionPrefix[1])
738
817
           << 2) |
739
817
          (xFromXOP2of3(
740
817
             insn->vectorExtensionPrefix[1])
741
817
           << 1) |
742
817
          (bFromXOP2of3(
743
817
             insn->vectorExtensionPrefix[1])
744
817
           << 0);
745
746
2.40k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
747
2.39k
      default:
748
2.39k
        break;
749
2.39k
      case VEX_PREFIX_66:
750
6
        insn->hasOpSize = true;
751
6
        break;
752
2.40k
      }
753
754
      // dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
755
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
756
      //    insn->vectorExtensionPrefix[2]);
757
2.40k
    }
758
204k
  } else if (isREX(insn, byte)) {
759
13.5k
    if (lookAtByte(insn, &nextByte))
760
0
      return -1;
761
762
13.5k
    insn->rexPrefix = byte;
763
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
764
13.5k
  } else
765
191k
    unconsumeByte(insn);
766
767
233k
  if (insn->mode == MODE_16BIT) {
768
76.8k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
769
76.8k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
770
76.8k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
771
76.8k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
772
76.8k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
773
156k
  } else if (insn->mode == MODE_32BIT) {
774
78.1k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
775
78.1k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
776
78.1k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
777
78.1k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
778
78.1k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
779
78.1k
  } else if (insn->mode == MODE_64BIT) {
780
78.0k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
781
14.3k
      insn->registerSize = 8;
782
14.3k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
783
14.3k
      insn->displacementSize = 4;
784
14.3k
      insn->immediateSize = 4;
785
14.3k
      insn->immSize = 4;
786
63.6k
    } else {
787
63.6k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
788
63.6k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
789
63.6k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
790
63.6k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
791
63.6k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
792
63.6k
    }
793
78.0k
  }
794
795
233k
  return 0;
796
233k
}
797
798
static int readModRM(struct InternalInstruction *insn);
799
800
/*
801
 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
802
 *   extended or escape opcodes).
803
 *
804
 * @param insn  - The instruction whose opcode is to be read.
805
 * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
806
 */
807
static int readOpcode(struct InternalInstruction *insn)
808
233k
{
809
233k
  uint8_t current;
810
811
  // dbgprintf(insn, "readOpcode()");
812
813
233k
  insn->opcodeType = ONEBYTE;
814
815
233k
  if (insn->vectorExtensionType == TYPE_EVEX) {
816
17.1k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
817
2
    default:
818
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
819
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
820
2
      return -1;
821
4.62k
    case VEX_LOB_0F:
822
4.62k
      insn->opcodeType = TWOBYTE;
823
4.62k
      return consumeByte(insn, &insn->opcode);
824
4.82k
    case VEX_LOB_0F38:
825
4.82k
      insn->opcodeType = THREEBYTE_38;
826
4.82k
      return consumeByte(insn, &insn->opcode);
827
7.67k
    case VEX_LOB_0F3A:
828
7.67k
      insn->opcodeType = THREEBYTE_3A;
829
7.67k
      return consumeByte(insn, &insn->opcode);
830
17.1k
    }
831
215k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
832
2.63k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
833
5
    default:
834
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
835
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
836
5
      return -1;
837
216
    case VEX_LOB_0F:
838
      //insn->twoByteEscape = 0x0f;
839
216
      insn->opcodeType = TWOBYTE;
840
216
      return consumeByte(insn, &insn->opcode);
841
1.23k
    case VEX_LOB_0F38:
842
      //insn->twoByteEscape = 0x0f;
843
1.23k
      insn->opcodeType = THREEBYTE_38;
844
1.23k
      return consumeByte(insn, &insn->opcode);
845
1.17k
    case VEX_LOB_0F3A:
846
      //insn->twoByteEscape = 0x0f;
847
1.17k
      insn->opcodeType = THREEBYTE_3A;
848
1.17k
      return consumeByte(insn, &insn->opcode);
849
2.63k
    }
850
213k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
851
    //insn->twoByteEscape = 0x0f;
852
2.79k
    insn->opcodeType = TWOBYTE;
853
2.79k
    return consumeByte(insn, &insn->opcode);
854
210k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
855
2.40k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
856
12
    default:
857
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
858
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
859
12
      return -1;
860
2.04k
    case XOP_MAP_SELECT_8:
861
2.04k
      insn->opcodeType = XOP8_MAP;
862
2.04k
      return consumeByte(insn, &insn->opcode);
863
317
    case XOP_MAP_SELECT_9:
864
317
      insn->opcodeType = XOP9_MAP;
865
317
      return consumeByte(insn, &insn->opcode);
866
27
    case XOP_MAP_SELECT_A:
867
27
      insn->opcodeType = XOPA_MAP;
868
27
      return consumeByte(insn, &insn->opcode);
869
2.40k
    }
870
2.40k
  }
871
872
208k
  if (consumeByte(insn, &current))
873
0
    return -1;
874
875
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
876
208k
  insn->firstByte = current;
877
878
208k
  if (current == 0x0f) {
879
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
880
12.9k
    insn->twoByteEscape = current;
881
882
12.9k
    if (consumeByte(insn, &current))
883
36
      return -1;
884
885
12.9k
    if (current == 0x38) {
886
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
887
342
      if (consumeByte(insn, &current))
888
0
        return -1;
889
890
342
      insn->opcodeType = THREEBYTE_38;
891
12.5k
    } else if (current == 0x3a) {
892
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
893
165
      if (consumeByte(insn, &current))
894
0
        return -1;
895
896
165
      insn->opcodeType = THREEBYTE_3A;
897
12.4k
    } else if (current == 0x0f) {
898
      // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
899
      // Consume operands before the opcode to comply with the 3DNow encoding
900
175
      if (readModRM(insn))
901
2
        return -1;
902
903
173
      if (consumeByte(insn, &current))
904
1
        return -1;
905
906
172
      insn->opcodeType = THREEDNOW_MAP;
907
12.2k
    } else {
908
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
909
12.2k
      insn->opcodeType = TWOBYTE;
910
12.2k
    }
911
195k
  } else if (insn->mandatoryPrefix)
912
    // The opcode with mandatory prefix must start with opcode escape.
913
    // If not it's legacy repeat prefix
914
2.86k
    insn->mandatoryPrefix = 0;
915
916
  /*
917
   * At this point we have consumed the full opcode.
918
   * Anything we consume from here on must be unconsumed.
919
   */
920
921
208k
  insn->opcode = current;
922
923
208k
  return 0;
924
208k
}
925
926
// Hacky for FEMMS
927
#define GET_INSTRINFO_ENUM
928
#ifndef CAPSTONE_X86_REDUCE
929
#include "X86GenInstrInfo.inc"
930
#else
931
#include "X86GenInstrInfo_reduce.inc"
932
#endif
933
934
/*
935
 * getIDWithAttrMask - Determines the ID of an instruction, consuming
936
 *   the ModR/M byte as appropriate for extended and escape opcodes,
937
 *   and using a supplied attribute mask.
938
 *
939
 * @param instructionID - A pointer whose target is filled in with the ID of the
940
 *                        instruction.
941
 * @param insn          - The instruction whose ID is to be determined.
942
 * @param attrMask      - The attribute mask to search.
943
 * @return              - 0 if the ModR/M could be read when needed or was not
944
 *                        needed; nonzero otherwise.
945
 */
946
static int getIDWithAttrMask(uint16_t *instructionID,
947
           struct InternalInstruction *insn,
948
           uint16_t attrMask)
949
319k
{
950
319k
  bool hasModRMExtension;
951
952
319k
  InstructionContext instructionClass = contextForAttrs(attrMask);
953
954
319k
  hasModRMExtension =
955
319k
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
956
957
319k
  if (hasModRMExtension) {
958
174k
    if (readModRM(insn))
959
596
      return -1;
960
961
174k
    *instructionID = decode(insn->opcodeType, instructionClass,
962
174k
          insn->opcode, insn->modRM);
963
174k
  } else {
964
144k
    *instructionID = decode(insn->opcodeType, instructionClass,
965
144k
          insn->opcode, 0);
966
144k
  }
967
968
318k
  return 0;
969
319k
}
970
971
/*
972
 * is16BitEquivalent - Determines whether two instruction names refer to
973
 * equivalent instructions but one is 16-bit whereas the other is not.
974
 *
975
 * @param orig  - The instruction ID that is not 16-bit
976
 * @param equiv - The instruction ID that is 16-bit
977
 */
978
static bool is16BitEquivalent(unsigned orig, unsigned equiv)
979
70.0k
{
980
70.0k
  size_t i;
981
70.0k
  uint16_t idx;
982
983
70.0k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
984
36.8k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
985
36.8k
          x86_16_bit_eq_tbl[i].first == orig;
986
36.1k
         i++) {
987
36.1k
      if (x86_16_bit_eq_tbl[i].second == equiv)
988
35.4k
        return true;
989
36.1k
    }
990
36.1k
  }
991
992
34.6k
  return false;
993
70.0k
}
994
995
/*
996
 * is64Bit - Determines whether this instruction is a 64-bit instruction.
997
 *
998
 * @param name - The instruction that is not 16-bit
999
 */
1000
static bool is64Bit(uint16_t id)
1001
6.47k
{
1002
6.47k
  unsigned int i = find_insn(id);
1003
6.47k
  if (i != -1) {
1004
6.45k
    return insns[i].is64bit;
1005
6.45k
  }
1006
1007
  // not found??
1008
20
  return false;
1009
6.47k
}
1010
1011
/*
1012
 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
1013
 *   appropriate for extended and escape opcodes.  Determines the attributes and
1014
 *   context for the instruction before doing so.
1015
 *
1016
 * @param insn  - The instruction whose ID is to be determined.
1017
 * @return      - 0 if the ModR/M could be read when needed or was not needed;
1018
 *                nonzero otherwise.
1019
 */
1020
static int getID(struct InternalInstruction *insn)
1021
233k
{
1022
233k
  uint16_t attrMask;
1023
233k
  uint16_t instructionID;
1024
1025
233k
  attrMask = ATTR_NONE;
1026
1027
233k
  if (insn->mode == MODE_64BIT)
1028
77.9k
    attrMask |= ATTR_64BIT;
1029
1030
233k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1031
24.9k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1032
17.1k
            ATTR_EVEX :
1033
24.9k
            ATTR_VEX;
1034
1035
24.9k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1036
17.1k
      switch (ppFromEVEX3of4(
1037
17.1k
        insn->vectorExtensionPrefix[2])) {
1038
14.3k
      case VEX_PREFIX_66:
1039
14.3k
        attrMask |= ATTR_OPSIZE;
1040
14.3k
        break;
1041
611
      case VEX_PREFIX_F3:
1042
611
        attrMask |= ATTR_XS;
1043
611
        break;
1044
548
      case VEX_PREFIX_F2:
1045
548
        attrMask |= ATTR_XD;
1046
548
        break;
1047
17.1k
      }
1048
1049
17.1k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1050
1.92k
        attrMask |= ATTR_EVEXKZ;
1051
17.1k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1052
6.47k
        attrMask |= ATTR_EVEXB;
1053
17.1k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1054
12.2k
        attrMask |= ATTR_EVEXK;
1055
17.1k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1056
7.14k
        attrMask |= ATTR_EVEXL;
1057
17.1k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1058
7.23k
        attrMask |= ATTR_EVEXL2;
1059
17.1k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1060
2.62k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1061
2.40k
      case VEX_PREFIX_66:
1062
2.40k
        attrMask |= ATTR_OPSIZE;
1063
2.40k
        break;
1064
168
      case VEX_PREFIX_F3:
1065
168
        attrMask |= ATTR_XS;
1066
168
        break;
1067
40
      case VEX_PREFIX_F2:
1068
40
        attrMask |= ATTR_XD;
1069
40
        break;
1070
2.62k
      }
1071
1072
2.62k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1073
1.14k
        attrMask |= ATTR_VEXL;
1074
5.17k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1075
2.78k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1076
1.32k
      case VEX_PREFIX_66:
1077
1.32k
        attrMask |= ATTR_OPSIZE;
1078
1.32k
        break;
1079
219
      case VEX_PREFIX_F3:
1080
219
        attrMask |= ATTR_XS;
1081
219
        break;
1082
364
      case VEX_PREFIX_F2:
1083
364
        attrMask |= ATTR_XD;
1084
364
        break;
1085
2.78k
      }
1086
1087
2.78k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1088
1.89k
        attrMask |= ATTR_VEXL;
1089
2.78k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1090
2.38k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1091
5
      case VEX_PREFIX_66:
1092
5
        attrMask |= ATTR_OPSIZE;
1093
5
        break;
1094
2
      case VEX_PREFIX_F3:
1095
2
        attrMask |= ATTR_XS;
1096
2
        break;
1097
6
      case VEX_PREFIX_F2:
1098
6
        attrMask |= ATTR_XD;
1099
6
        break;
1100
2.38k
      }
1101
1102
2.38k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1103
107
        attrMask |= ATTR_VEXL;
1104
2.38k
    } else {
1105
0
      return -1;
1106
0
    }
1107
208k
  } else if (!insn->mandatoryPrefix) {
1108
    // If we don't have mandatory prefix we should use legacy prefixes here
1109
203k
    if (insn->hasOpSize && (insn->mode != MODE_16BIT))
1110
2.91k
      attrMask |= ATTR_OPSIZE;
1111
203k
    if (insn->hasAdSize)
1112
1.87k
      attrMask |= ATTR_ADSIZE;
1113
203k
    if (insn->opcodeType == ONEBYTE) {
1114
195k
      if (insn->repeatPrefix == 0xf3 &&
1115
6.67k
          (insn->opcode == 0x90))
1116
        // Special support for PAUSE
1117
27
        attrMask |= ATTR_XS;
1118
195k
    } else {
1119
8.80k
      if (insn->repeatPrefix == 0xf2)
1120
169
        attrMask |= ATTR_XD;
1121
8.63k
      else if (insn->repeatPrefix == 0xf3)
1122
333
        attrMask |= ATTR_XS;
1123
8.80k
    }
1124
203k
  } else {
1125
4.12k
    switch (insn->mandatoryPrefix) {
1126
1.15k
    case 0xf2:
1127
1.15k
      attrMask |= ATTR_XD;
1128
1.15k
      break;
1129
1.42k
    case 0xf3:
1130
1.42k
      attrMask |= ATTR_XS;
1131
1.42k
      break;
1132
1.54k
    case 0x66:
1133
1.54k
      if (insn->mode != MODE_16BIT)
1134
1.01k
        attrMask |= ATTR_OPSIZE;
1135
1.54k
      break;
1136
0
    case 0x67:
1137
0
      attrMask |= ATTR_ADSIZE;
1138
0
      break;
1139
4.12k
    }
1140
4.12k
  }
1141
1142
233k
  if (insn->rexPrefix & 0x08) {
1143
14.3k
    attrMask |= ATTR_REXW;
1144
14.3k
    attrMask &= ~ATTR_ADSIZE;
1145
14.3k
  }
1146
1147
  /*
1148
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1149
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1150
   */
1151
233k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1152
64.8k
      insn->opcode == 0xE3)
1153
872
    attrMask ^= ATTR_ADSIZE;
1154
1155
  /*
1156
   * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
1157
   * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
1158
   */
1159
233k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1160
2.99k
    switch (insn->opcode) {
1161
38
    case 0xE8:
1162
78
    case 0xE9:
1163
      // Take care of psubsb and other mmx instructions.
1164
78
      if (insn->opcodeType == ONEBYTE) {
1165
38
        attrMask ^= ATTR_OPSIZE;
1166
38
        insn->immediateSize = 4;
1167
38
        insn->displacementSize = 4;
1168
38
      }
1169
78
      break;
1170
26
    case 0x82:
1171
71
    case 0x83:
1172
105
    case 0x84:
1173
337
    case 0x85:
1174
337
    case 0x86:
1175
748
    case 0x87:
1176
776
    case 0x88:
1177
793
    case 0x89:
1178
1.04k
    case 0x8A:
1179
1.09k
    case 0x8B:
1180
1.11k
    case 0x8C:
1181
1.21k
    case 0x8D:
1182
1.26k
    case 0x8E:
1183
1.39k
    case 0x8F:
1184
      // Take care of lea and three byte ops.
1185
1.39k
      if (insn->opcodeType == TWOBYTE) {
1186
78
        attrMask ^= ATTR_OPSIZE;
1187
78
        insn->immediateSize = 4;
1188
78
        insn->displacementSize = 4;
1189
78
      }
1190
1.39k
      break;
1191
2.99k
    }
1192
2.99k
  }
1193
1194
  /* The following clauses compensate for limitations of the tables. */
1195
233k
  if (insn->mode != MODE_64BIT &&
1196
155k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1197
15.9k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1198
10
      return -1;
1199
10
    }
1200
1201
    /*
1202
     * The tables can't distinguish between cases where the W-bit is used to
1203
     * select register size and cases where it's a required part of the opcode.
1204
     */
1205
15.9k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1206
10.2k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1207
10.0k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1208
1.82k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1209
9.59k
        (insn->vectorExtensionType == TYPE_XOP &&
1210
6.48k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1211
6.48k
      uint16_t instructionIDWithREXW;
1212
1213
6.48k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1214
6.48k
                attrMask | ATTR_REXW)) {
1215
2
        insn->instructionID = instructionID;
1216
2
        insn->spec = specifierForUID(instructionID);
1217
2
        return 0;
1218
2
      }
1219
1220
      // If not a 64-bit instruction. Switch the opcode.
1221
6.47k
      if (!is64Bit(instructionIDWithREXW)) {
1222
6.10k
        insn->instructionID = instructionIDWithREXW;
1223
6.10k
        insn->spec =
1224
6.10k
          specifierForUID(instructionIDWithREXW);
1225
1226
6.10k
        return 0;
1227
6.10k
      }
1228
6.47k
    }
1229
15.9k
  }
1230
1231
  /*
1232
   * Absolute moves, umonitor, and movdir64b need special handling.
1233
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1234
   *  inverted w.r.t.
1235
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1236
   *  any position.
1237
   */
1238
226k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1239
224k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1240
224k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1241
    /* Make sure we observed the prefixes in any position. */
1242
2.63k
    if (insn->hasAdSize)
1243
12
      attrMask |= ATTR_ADSIZE;
1244
1245
2.63k
    if (insn->hasOpSize)
1246
61
      attrMask |= ATTR_OPSIZE;
1247
1248
    /* In 16-bit, invert the attributes. */
1249
2.63k
    if (insn->mode == MODE_16BIT) {
1250
1.03k
      attrMask ^= ATTR_ADSIZE;
1251
1252
      /* The OpSize attribute is only valid with the absolute moves. */
1253
1.03k
      if (insn->opcodeType == ONEBYTE &&
1254
909
          ((insn->opcode & 0xFC) == 0xA0))
1255
909
        attrMask ^= ATTR_OPSIZE;
1256
1.03k
    }
1257
1258
2.63k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1259
2
      return -1;
1260
2
    }
1261
1262
2.63k
    insn->instructionID = instructionID;
1263
2.63k
    insn->spec = specifierForUID(instructionID);
1264
1265
2.63k
    return 0;
1266
2.63k
  }
1267
224k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1268
580
    return -1;
1269
580
  }
1270
1271
223k
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1272
77.8k
      !(attrMask & ATTR_OPSIZE)) {
1273
    /*
1274
     * The instruction tables make no distinction between instructions that
1275
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1276
     * particular spot (i.e., many MMX operations).  In general we're
1277
     * conservative, but in the specific case where OpSize is present but not
1278
     * in the right place we check if there's a 16-bit operation.
1279
     */
1280
70.0k
    const struct InstructionSpecifier *spec;
1281
70.0k
    uint16_t instructionIDWithOpsize;
1282
1283
70.0k
    spec = specifierForUID(instructionID);
1284
1285
70.0k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1286
70.0k
              attrMask | ATTR_OPSIZE)) {
1287
      /*
1288
       * ModRM required with OpSize but not present; give up and return version
1289
       * without OpSize set
1290
       */
1291
2
      insn->instructionID = instructionID;
1292
2
      insn->spec = spec;
1293
1294
2
      return 0;
1295
2
    }
1296
1297
70.0k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1298
35.4k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1299
35.1k
      insn->instructionID = instructionIDWithOpsize;
1300
35.1k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1301
35.1k
    } else {
1302
34.8k
      insn->instructionID = instructionID;
1303
34.8k
      insn->spec = spec;
1304
34.8k
    }
1305
1306
70.0k
    return 0;
1307
70.0k
  }
1308
1309
153k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1310
953
      insn->rexPrefix & 0x01) {
1311
    /*
1312
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1313
     * it should decode as XCHG %r8, %eax.
1314
     */
1315
63
    const struct InstructionSpecifier *spec;
1316
63
    uint16_t instructionIDWithNewOpcode;
1317
63
    const struct InstructionSpecifier *specWithNewOpcode;
1318
1319
63
    spec = specifierForUID(instructionID);
1320
1321
    /* Borrow opcode from one of the other XCHGar opcodes */
1322
63
    insn->opcode = 0x91;
1323
1324
63
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1325
63
              attrMask)) {
1326
0
      insn->opcode = 0x90;
1327
1328
0
      insn->instructionID = instructionID;
1329
0
      insn->spec = spec;
1330
1331
0
      return 0;
1332
0
    }
1333
1334
63
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1335
1336
    /* Change back */
1337
63
    insn->opcode = 0x90;
1338
1339
63
    insn->instructionID = instructionIDWithNewOpcode;
1340
63
    insn->spec = specWithNewOpcode;
1341
1342
63
    return 0;
1343
63
  }
1344
1345
153k
  insn->instructionID = instructionID;
1346
153k
  insn->spec = specifierForUID(insn->instructionID);
1347
1348
153k
  return 0;
1349
153k
}
1350
1351
/*
1352
 * readSIB - Consumes the SIB byte to determine addressing information for an
1353
 *   instruction.
1354
 *
1355
 * @param insn  - The instruction whose SIB byte is to be read.
1356
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1357
 */
1358
static int readSIB(struct InternalInstruction *insn)
1359
6.44k
{
1360
6.44k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1361
6.44k
  uint8_t index, base;
1362
1363
  // dbgprintf(insn, "readSIB()");
1364
1365
6.44k
  if (insn->consumedSIB)
1366
0
    return 0;
1367
1368
6.44k
  insn->consumedSIB = true;
1369
1370
6.44k
  switch (insn->addressSize) {
1371
0
  case 2:
1372
    // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1373
0
    return -1;
1374
3.38k
  case 4:
1375
3.38k
    insn->sibIndexBase = SIB_INDEX_EAX;
1376
3.38k
    sibBaseBase = SIB_BASE_EAX;
1377
3.38k
    break;
1378
3.06k
  case 8:
1379
3.06k
    insn->sibIndexBase = SIB_INDEX_RAX;
1380
3.06k
    sibBaseBase = SIB_BASE_RAX;
1381
3.06k
    break;
1382
6.44k
  }
1383
1384
6.44k
  if (consumeByte(insn, &insn->sib))
1385
16
    return -1;
1386
1387
6.42k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1388
1389
6.42k
  if (index == 0x4) {
1390
1.23k
    insn->sibIndex = SIB_INDEX_NONE;
1391
5.19k
  } else {
1392
5.19k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1393
5.19k
  }
1394
1395
6.42k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1396
1397
6.42k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1398
1399
6.42k
  switch (base) {
1400
700
  case 0x5:
1401
750
  case 0xd:
1402
750
    switch (modFromModRM(insn->modRM)) {
1403
278
    case 0x0:
1404
278
      insn->eaDisplacement = EA_DISP_32;
1405
278
      insn->sibBase = SIB_BASE_NONE;
1406
278
      break;
1407
428
    case 0x1:
1408
428
      insn->eaDisplacement = EA_DISP_8;
1409
428
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1410
428
      break;
1411
44
    case 0x2:
1412
44
      insn->eaDisplacement = EA_DISP_32;
1413
44
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1414
44
      break;
1415
0
    case 0x3:
1416
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1417
0
      return -1;
1418
750
    }
1419
750
    break;
1420
5.67k
  default:
1421
5.67k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1422
5.67k
    break;
1423
6.42k
  }
1424
1425
6.42k
  return 0;
1426
6.42k
}
1427
1428
/*
1429
 * readDisplacement - Consumes the displacement of an instruction.
1430
 *
1431
 * @param insn  - The instruction whose displacement is to be read.
1432
 * @return      - 0 if the displacement byte was successfully read; nonzero
1433
 *                otherwise.
1434
 */
1435
static int readDisplacement(struct InternalInstruction *insn)
1436
43.1k
{
1437
43.1k
  int8_t d8;
1438
43.1k
  int16_t d16;
1439
43.1k
  int32_t d32;
1440
1441
  // dbgprintf(insn, "readDisplacement()");
1442
1443
43.1k
  if (insn->consumedDisplacement)
1444
0
    return 0;
1445
1446
43.1k
  insn->consumedDisplacement = true;
1447
43.1k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1448
1449
43.1k
  switch (insn->eaDisplacement) {
1450
11.5k
  case EA_DISP_NONE:
1451
11.5k
    insn->consumedDisplacement = false;
1452
11.5k
    break;
1453
22.0k
  case EA_DISP_8:
1454
22.0k
    if (consumeInt8(insn, &d8))
1455
58
      return -1;
1456
21.9k
    insn->displacement = d8;
1457
21.9k
    break;
1458
4.49k
  case EA_DISP_16:
1459
4.49k
    if (consumeInt16(insn, &d16))
1460
20
      return -1;
1461
4.47k
    insn->displacement = d16;
1462
4.47k
    break;
1463
5.04k
  case EA_DISP_32:
1464
5.04k
    if (consumeInt32(insn, &d32))
1465
96
      return -1;
1466
4.95k
    insn->displacement = d32;
1467
4.95k
    break;
1468
43.1k
  }
1469
1470
42.9k
  return 0;
1471
43.1k
}
1472
1473
/*
1474
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1475
 *   displacement) for an instruction and interprets it.
1476
 *
1477
 * @param insn  - The instruction whose addressing information is to be read.
1478
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1479
 */
1480
static int readModRM(struct InternalInstruction *insn)
1481
397k
{
1482
397k
  uint8_t mod, rm, reg, evexrm;
1483
1484
  // dbgprintf(insn, "readModRM()");
1485
1486
397k
  if (insn->consumedModRM)
1487
268k
    return 0;
1488
1489
128k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1490
1491
128k
  if (consumeByte(insn, &insn->modRM))
1492
408
    return -1;
1493
1494
127k
  insn->consumedModRM = true;
1495
1496
  // save original ModRM for later reference
1497
127k
  insn->orgModRM = insn->modRM;
1498
1499
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1500
127k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1501
11.7k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1502
63
    insn->modRM |= 0xC0;
1503
1504
127k
  mod = modFromModRM(insn->modRM);
1505
127k
  rm = rmFromModRM(insn->modRM);
1506
127k
  reg = regFromModRM(insn->modRM);
1507
1508
  /*
1509
   * This goes by insn->registerSize to pick the correct register, which messes
1510
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1511
   * fixupReg().
1512
   */
1513
127k
  switch (insn->registerSize) {
1514
43.4k
  case 2:
1515
43.4k
    insn->regBase = MODRM_REG_AX;
1516
43.4k
    insn->eaRegBase = EA_REG_AX;
1517
43.4k
    break;
1518
73.5k
  case 4:
1519
73.5k
    insn->regBase = MODRM_REG_EAX;
1520
73.5k
    insn->eaRegBase = EA_REG_EAX;
1521
73.5k
    break;
1522
10.7k
  case 8:
1523
10.7k
    insn->regBase = MODRM_REG_RAX;
1524
10.7k
    insn->eaRegBase = EA_REG_RAX;
1525
10.7k
    break;
1526
127k
  }
1527
1528
127k
  reg |= rFromREX(insn->rexPrefix) << 3;
1529
127k
  rm |= bFromREX(insn->rexPrefix) << 3;
1530
1531
127k
  evexrm = 0;
1532
127k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1533
17.0k
      insn->mode == MODE_64BIT) {
1534
6.88k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1535
6.88k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1536
6.88k
  }
1537
1538
127k
  insn->reg = (Reg)(insn->regBase + reg);
1539
1540
127k
  switch (insn->addressSize) {
1541
39.8k
  case 2: {
1542
39.8k
    EABase eaBaseBase = EA_BASE_BX_SI;
1543
1544
39.8k
    switch (mod) {
1545
21.9k
    case 0x0:
1546
21.9k
      if (rm == 0x6) {
1547
995
        insn->eaBase = EA_BASE_NONE;
1548
995
        insn->eaDisplacement = EA_DISP_16;
1549
995
        if (readDisplacement(insn))
1550
6
          return -1;
1551
20.9k
      } else {
1552
20.9k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1553
20.9k
        insn->eaDisplacement = EA_DISP_NONE;
1554
20.9k
      }
1555
21.9k
      break;
1556
21.9k
    case 0x1:
1557
5.43k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1558
5.43k
      insn->eaDisplacement = EA_DISP_8;
1559
5.43k
      insn->displacementSize = 1;
1560
5.43k
      if (readDisplacement(insn))
1561
17
        return -1;
1562
5.41k
      break;
1563
5.41k
    case 0x2:
1564
3.50k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1565
3.50k
      insn->eaDisplacement = EA_DISP_16;
1566
3.50k
      if (readDisplacement(insn))
1567
14
        return -1;
1568
3.48k
      break;
1569
8.98k
    case 0x3:
1570
8.98k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1571
8.98k
      if (readDisplacement(insn))
1572
0
        return -1;
1573
8.98k
      break;
1574
39.8k
    }
1575
39.8k
    break;
1576
39.8k
  }
1577
1578
43.0k
  case 4:
1579
87.9k
  case 8: {
1580
87.9k
    EABase eaBaseBase =
1581
87.9k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1582
1583
87.9k
    switch (mod) {
1584
0
    default:
1585
0
      break;
1586
44.0k
    case 0x0:
1587
44.0k
      insn->eaDisplacement =
1588
44.0k
        EA_DISP_NONE; /* readSIB may override this */
1589
      // In determining whether RIP-relative mode is used (rm=5),
1590
      // or whether a SIB byte is present (rm=4),
1591
      // the extension bits (REX.b and EVEX.x) are ignored.
1592
44.0k
      switch (rm & 7) {
1593
2.85k
      case 0x4: // SIB byte is present
1594
2.85k
        insn->eaBase = (insn->addressSize == 4 ?
1595
1.32k
              EA_BASE_sib :
1596
2.85k
              EA_BASE_sib64);
1597
2.85k
        if (readSIB(insn) || readDisplacement(insn))
1598
11
          return -1;
1599
2.84k
        break;
1600
2.84k
      case 0x5: // RIP-relative
1601
773
        insn->eaBase = EA_BASE_NONE;
1602
773
        insn->eaDisplacement = EA_DISP_32;
1603
773
        if (readDisplacement(insn))
1604
17
          return -1;
1605
756
        break;
1606
40.4k
      default:
1607
40.4k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1608
40.4k
        break;
1609
44.0k
      }
1610
44.0k
      break;
1611
44.0k
    case 0x1:
1612
16.5k
      insn->displacementSize = 1;
1613
      /* FALLTHROUGH */
1614
20.5k
    case 0x2:
1615
20.5k
      insn->eaDisplacement =
1616
20.5k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1617
20.5k
      switch (rm & 7) {
1618
3.58k
      case 0x4: // SIB byte is present
1619
3.58k
        insn->eaBase = EA_BASE_sib;
1620
3.58k
        if (readSIB(insn) || readDisplacement(insn))
1621
20
          return -1;
1622
3.56k
        break;
1623
17.0k
      default:
1624
17.0k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1625
17.0k
        if (readDisplacement(insn))
1626
105
          return -1;
1627
16.8k
        break;
1628
20.5k
      }
1629
20.4k
      break;
1630
23.2k
    case 0x3:
1631
23.2k
      insn->eaDisplacement = EA_DISP_NONE;
1632
23.2k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1633
23.2k
      break;
1634
87.9k
    }
1635
1636
87.7k
    break;
1637
87.9k
  }
1638
127k
  } /* switch (insn->addressSize) */
1639
1640
127k
  return 0;
1641
127k
}
1642
1643
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1644
  static uint16_t name(struct InternalInstruction *insn, \
1645
           OperandType type, uint8_t index, uint8_t *valid) \
1646
145k
  { \
1647
145k
    *valid = 1; \
1648
145k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
34.4k
    case TYPE_Rv: \
1653
34.4k
      return base + index; \
1654
43.7k
    case TYPE_R8: \
1655
43.7k
      index &= mask; \
1656
43.7k
      if (index > 0xf) \
1657
43.7k
        *valid = 0; \
1658
43.7k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
558
        return prefix##_SPL + (index - 4); \
1660
43.2k
      } else { \
1661
43.2k
        return prefix##_AL + index; \
1662
43.2k
      } \
1663
43.7k
    case TYPE_R16: \
1664
949
      index &= mask; \
1665
949
      if (index > 0xf) \
1666
949
        *valid = 0; \
1667
949
      return prefix##_AX + index; \
1668
43.7k
    case TYPE_R32: \
1669
778
      index &= mask; \
1670
778
      if (index > 0xf) \
1671
778
        *valid = 0; \
1672
778
      return prefix##_EAX + index; \
1673
43.7k
    case TYPE_R64: \
1674
3.75k
      index &= mask; \
1675
3.75k
      if (index > 0xf) \
1676
3.75k
        *valid = 0; \
1677
3.75k
      return prefix##_RAX + index; \
1678
43.7k
    case TYPE_ZMM: \
1679
12.5k
      return prefix##_ZMM0 + index; \
1680
43.7k
    case TYPE_YMM: \
1681
11.3k
      return prefix##_YMM0 + index; \
1682
43.7k
    case TYPE_XMM: \
1683
24.6k
      return prefix##_XMM0 + index; \
1684
43.7k
    case TYPE_VK: \
1685
9.27k
      index &= 0xf; \
1686
9.27k
      if (index > 7) \
1687
9.27k
        *valid = 0; \
1688
9.27k
      return prefix##_K0 + index; \
1689
43.7k
    case TYPE_MM64: \
1690
1.35k
      return prefix##_MM0 + (index & 0x7); \
1691
43.7k
    case TYPE_SEGMENTREG: \
1692
403
      if ((index & 7) > 5) \
1693
403
        *valid = 0; \
1694
403
      return prefix##_ES + (index & 7); \
1695
43.7k
    case TYPE_DEBUGREG: \
1696
18
      return prefix##_DR0 + index; \
1697
43.7k
    case TYPE_CONTROLREG: \
1698
45
      return prefix##_CR0 + index; \
1699
43.7k
    case TYPE_BNDR: \
1700
2.00k
      if (index > 3) \
1701
2.00k
        *valid = 0; \
1702
2.00k
      return prefix##_BND0 + index; \
1703
43.7k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
43.7k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
43.7k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
145k
    } \
1710
145k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1646
114k
  { \
1647
114k
    *valid = 1; \
1648
114k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
25.6k
    case TYPE_Rv: \
1653
25.6k
      return base + index; \
1654
35.3k
    case TYPE_R8: \
1655
35.3k
      index &= mask; \
1656
35.3k
      if (index > 0xf) \
1657
35.3k
        *valid = 0; \
1658
35.3k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
367
        return prefix##_SPL + (index - 4); \
1660
35.0k
      } else { \
1661
35.0k
        return prefix##_AL + index; \
1662
35.0k
      } \
1663
35.3k
    case TYPE_R16: \
1664
721
      index &= mask; \
1665
721
      if (index > 0xf) \
1666
721
        *valid = 0; \
1667
721
      return prefix##_AX + index; \
1668
35.3k
    case TYPE_R32: \
1669
651
      index &= mask; \
1670
651
      if (index > 0xf) \
1671
651
        *valid = 0; \
1672
651
      return prefix##_EAX + index; \
1673
35.3k
    case TYPE_R64: \
1674
2.52k
      index &= mask; \
1675
2.52k
      if (index > 0xf) \
1676
2.52k
        *valid = 0; \
1677
2.52k
      return prefix##_RAX + index; \
1678
35.3k
    case TYPE_ZMM: \
1679
10.0k
      return prefix##_ZMM0 + index; \
1680
35.3k
    case TYPE_YMM: \
1681
8.97k
      return prefix##_YMM0 + index; \
1682
35.3k
    case TYPE_XMM: \
1683
19.5k
      return prefix##_XMM0 + index; \
1684
35.3k
    case TYPE_VK: \
1685
8.20k
      index &= 0xf; \
1686
8.20k
      if (index > 7) \
1687
8.20k
        *valid = 0; \
1688
8.20k
      return prefix##_K0 + index; \
1689
35.3k
    case TYPE_MM64: \
1690
1.10k
      return prefix##_MM0 + (index & 0x7); \
1691
35.3k
    case TYPE_SEGMENTREG: \
1692
403
      if ((index & 7) > 5) \
1693
403
        *valid = 0; \
1694
403
      return prefix##_ES + (index & 7); \
1695
35.3k
    case TYPE_DEBUGREG: \
1696
18
      return prefix##_DR0 + index; \
1697
35.3k
    case TYPE_CONTROLREG: \
1698
45
      return prefix##_CR0 + index; \
1699
35.3k
    case TYPE_BNDR: \
1700
1.64k
      if (index > 3) \
1701
1.64k
        *valid = 0; \
1702
1.64k
      return prefix##_BND0 + index; \
1703
35.3k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
35.3k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
35.3k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
114k
    } \
1710
114k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1646
30.4k
  { \
1647
30.4k
    *valid = 1; \
1648
30.4k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
8.77k
    case TYPE_Rv: \
1653
8.77k
      return base + index; \
1654
8.38k
    case TYPE_R8: \
1655
8.38k
      index &= mask; \
1656
8.38k
      if (index > 0xf) \
1657
8.38k
        *valid = 0; \
1658
8.38k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
191
        return prefix##_SPL + (index - 4); \
1660
8.19k
      } else { \
1661
8.19k
        return prefix##_AL + index; \
1662
8.19k
      } \
1663
8.38k
    case TYPE_R16: \
1664
228
      index &= mask; \
1665
228
      if (index > 0xf) \
1666
228
        *valid = 0; \
1667
228
      return prefix##_AX + index; \
1668
8.38k
    case TYPE_R32: \
1669
127
      index &= mask; \
1670
127
      if (index > 0xf) \
1671
127
        *valid = 0; \
1672
127
      return prefix##_EAX + index; \
1673
8.38k
    case TYPE_R64: \
1674
1.22k
      index &= mask; \
1675
1.22k
      if (index > 0xf) \
1676
1.22k
        *valid = 0; \
1677
1.22k
      return prefix##_RAX + index; \
1678
8.38k
    case TYPE_ZMM: \
1679
2.44k
      return prefix##_ZMM0 + index; \
1680
8.38k
    case TYPE_YMM: \
1681
2.41k
      return prefix##_YMM0 + index; \
1682
8.38k
    case TYPE_XMM: \
1683
5.12k
      return prefix##_XMM0 + index; \
1684
8.38k
    case TYPE_VK: \
1685
1.06k
      index &= 0xf; \
1686
1.06k
      if (index > 7) \
1687
1.06k
        *valid = 0; \
1688
1.06k
      return prefix##_K0 + index; \
1689
8.38k
    case TYPE_MM64: \
1690
255
      return prefix##_MM0 + (index & 0x7); \
1691
8.38k
    case TYPE_SEGMENTREG: \
1692
0
      if ((index & 7) > 5) \
1693
0
        *valid = 0; \
1694
0
      return prefix##_ES + (index & 7); \
1695
8.38k
    case TYPE_DEBUGREG: \
1696
0
      return prefix##_DR0 + index; \
1697
8.38k
    case TYPE_CONTROLREG: \
1698
0
      return prefix##_CR0 + index; \
1699
8.38k
    case TYPE_BNDR: \
1700
361
      if (index > 3) \
1701
361
        *valid = 0; \
1702
361
      return prefix##_BND0 + index; \
1703
8.38k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
8.38k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
8.38k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
30.4k
    } \
1710
30.4k
  }
1711
1712
/*
1713
 * fixup*Value - Consults an operand type to determine the meaning of the
1714
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1715
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1716
 *   misinterpret it as.
1717
 *
1718
 * @param insn  - The instruction containing the operand.
1719
 * @param type  - The operand type.
1720
 * @param index - The existing value of the field as reported by readModRM().
1721
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1722
 *                field is valid for the register class; 0 if not.
1723
 * @return      - The proper value.
1724
 */
1725
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1726
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1727
1728
/*
1729
 * fixupReg - Consults an operand specifier to determine which of the
1730
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1731
 *
1732
 * @param insn  - See fixup*Value().
1733
 * @param op    - The operand specifier.
1734
 * @return      - 0 if fixup was successful; -1 if the register returned was
1735
 *                invalid for its class.
1736
 */
1737
static int fixupReg(struct InternalInstruction *insn,
1738
        const struct OperandSpecifier *op)
1739
239k
{
1740
239k
  uint8_t valid;
1741
1742
239k
  switch ((OperandEncoding)op->encoding) {
1743
0
  default:
1744
    // debug("Expected a REG or R/M encoding in fixupReg");
1745
0
    return -1;
1746
18.2k
  case ENCODING_VVVV:
1747
18.2k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1748
18.2k
            insn->vvvv, &valid);
1749
18.2k
    if (!valid)
1750
1
      return -1;
1751
18.2k
    break;
1752
96.6k
  case ENCODING_REG:
1753
96.6k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1754
96.6k
                 insn->reg - insn->regBase,
1755
96.6k
                 &valid);
1756
96.6k
    if (!valid)
1757
4
      return -1;
1758
96.6k
    break;
1759
811k
CASE_ENCODING_RM:
1760
811k
    if (insn->eaBase >= insn->eaRegBase) {
1761
30.4k
      insn->eaBase = (EABase)fixupRMValue(
1762
30.4k
        insn, (OperandType)op->type,
1763
30.4k
        insn->eaBase - insn->eaRegBase, &valid);
1764
30.4k
      if (!valid)
1765
2
        return -1;
1766
30.4k
    }
1767
124k
    break;
1768
239k
  }
1769
1770
239k
  return 0;
1771
239k
}
1772
1773
/*
1774
 * readOpcodeRegister - Reads an operand from the opcode field of an
1775
 *   instruction and interprets it appropriately given the operand width.
1776
 *   Handles AddRegFrm instructions.
1777
 *
1778
 * @param insn  - the instruction whose opcode field is to be read.
1779
 * @param size  - The width (in bytes) of the register being specified.
1780
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1781
 *                RAX.
1782
 * @return      - 0 on success; nonzero otherwise.
1783
 */
1784
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
1785
24.2k
{
1786
24.2k
  if (size == 0)
1787
18.9k
    size = insn->registerSize;
1788
1789
24.2k
  switch (size) {
1790
2.86k
  case 1:
1791
2.86k
    insn->opcodeRegister =
1792
2.86k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1793
2.86k
                (insn->opcode & 7)));
1794
2.86k
    if (insn->rexPrefix &&
1795
388
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1796
314
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1797
148
      insn->opcodeRegister =
1798
148
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1799
148
                   MODRM_REG_AL - 4));
1800
148
    }
1801
1802
2.86k
    break;
1803
8.72k
  case 2:
1804
8.72k
    insn->opcodeRegister =
1805
8.72k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1806
8.72k
                (insn->opcode & 7)));
1807
8.72k
    break;
1808
10.2k
  case 4:
1809
10.2k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1810
10.2k
               ((bFromREX(insn->rexPrefix) << 3) |
1811
10.2k
                (insn->opcode & 7)));
1812
10.2k
    break;
1813
2.40k
  case 8:
1814
2.40k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1815
2.40k
               ((bFromREX(insn->rexPrefix) << 3) |
1816
2.40k
                (insn->opcode & 7)));
1817
2.40k
    break;
1818
24.2k
  }
1819
1820
24.2k
  return 0;
1821
24.2k
}
1822
1823
/*
1824
 * readImmediate - Consumes an immediate operand from an instruction, given the
1825
 *   desired operand size.
1826
 *
1827
 * @param insn  - The instruction whose operand is to be read.
1828
 * @param size  - The width (in bytes) of the operand.
1829
 * @return      - 0 if the immediate was successfully consumed; nonzero
1830
 *                otherwise.
1831
 */
1832
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
1833
64.8k
{
1834
64.8k
  uint8_t imm8;
1835
64.8k
  uint16_t imm16;
1836
64.8k
  uint32_t imm32;
1837
64.8k
  uint64_t imm64;
1838
1839
64.8k
  if (insn->numImmediatesConsumed == 2) {
1840
    // debug("Already consumed two immediates");
1841
0
    return -1;
1842
0
  }
1843
1844
64.8k
  if (size == 0)
1845
0
    size = insn->immediateSize;
1846
64.8k
  else
1847
64.8k
    insn->immediateSize = size;
1848
1849
64.8k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1850
1851
64.8k
  switch (size) {
1852
49.9k
  case 1:
1853
49.9k
    if (consumeByte(insn, &imm8))
1854
205
      return -1;
1855
1856
49.7k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
1857
49.7k
    break;
1858
7.93k
  case 2:
1859
7.93k
    if (consumeUInt16(insn, &imm16))
1860
72
      return -1;
1861
1862
7.86k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
1863
7.86k
    break;
1864
5.96k
  case 4:
1865
5.96k
    if (consumeUInt32(insn, &imm32))
1866
109
      return -1;
1867
1868
5.85k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
1869
5.85k
    break;
1870
986
  case 8:
1871
986
    if (consumeUInt64(insn, &imm64))
1872
23
      return -1;
1873
963
    insn->immediates[insn->numImmediatesConsumed] = imm64;
1874
963
    break;
1875
64.8k
  }
1876
1877
64.4k
  insn->numImmediatesConsumed++;
1878
1879
64.4k
  return 0;
1880
64.8k
}
1881
1882
/*
1883
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
1884
 *
1885
 * @param insn  - The instruction whose operand is to be read.
1886
 * @return      - 0 if the vvvv was successfully consumed; nonzero
1887
 *                otherwise.
1888
 */
1889
static int readVVVV(struct InternalInstruction *insn)
1890
232k
{
1891
232k
  int vvvv;
1892
1893
232k
  if (insn->vectorExtensionType == TYPE_EVEX)
1894
17.0k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1895
17.0k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
1896
214k
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
1897
2.60k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
1898
212k
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
1899
2.76k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
1900
209k
  else if (insn->vectorExtensionType == TYPE_XOP)
1901
2.36k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
1902
207k
  else
1903
207k
    return -1;
1904
1905
24.8k
  if (insn->mode != MODE_64BIT)
1906
15.8k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1907
1908
24.8k
  insn->vvvv = (Reg)vvvv;
1909
1910
24.8k
  return 0;
1911
232k
}
1912
1913
/*
1914
 * readMaskRegister - Reads an mask register from the opcode field of an
1915
 *   instruction.
1916
 *
1917
 * @param insn    - The instruction whose opcode field is to be read.
1918
 * @return        - 0 on success; nonzero otherwise.
1919
 */
1920
static int readMaskRegister(struct InternalInstruction *insn)
1921
12.3k
{
1922
12.3k
  if (insn->vectorExtensionType != TYPE_EVEX)
1923
0
    return -1;
1924
1925
12.3k
  insn->writemask =
1926
12.3k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
1927
1928
12.3k
  return 0;
1929
12.3k
}
1930
1931
/*
1932
 * readOperands - Consults the specifier for an instruction and consumes all
1933
 *   operands for that instruction, interpreting them as it goes.
1934
 *
1935
 * @param insn  - The instruction whose operands are to be read and interpreted.
1936
 * @return      - 0 if all operands could be read; nonzero otherwise.
1937
 */
1938
static int readOperands(struct InternalInstruction *insn)
1939
232k
{
1940
232k
  int hasVVVV, needVVVV;
1941
232k
  int sawRegImm = 0;
1942
232k
  int i;
1943
1944
  /* If non-zero vvvv specified, need to make sure one of the operands
1945
     uses it. */
1946
232k
  hasVVVV = !readVVVV(insn);
1947
232k
  needVVVV = hasVVVV && (insn->vvvv != 0);
1948
1949
1.62M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
1950
1.39M
    const OperandSpecifier *op =
1951
1.39M
      &x86OperandSets[insn->spec->operands][i];
1952
1.39M
    switch (op->encoding) {
1953
968k
    case ENCODING_NONE:
1954
979k
    case ENCODING_SI:
1955
992k
    case ENCODING_DI:
1956
992k
      break;
1957
1958
6.21k
CASE_ENCODING_VSIB:
1959
      // VSIB can use the V2 bit so check only the other bits.
1960
6.21k
      if (needVVVV)
1961
773
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
1962
1963
6.21k
      if (readModRM(insn))
1964
0
        return -1;
1965
1966
      // Reject if SIB wasn't used.
1967
1.16k
      if (insn->eaBase != EA_BASE_sib &&
1968
776
          insn->eaBase != EA_BASE_sib64)
1969
3
        return -1;
1970
1971
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1972
1.16k
      if (insn->sibIndex == SIB_INDEX_NONE)
1973
55
        insn->sibIndex =
1974
55
          (SIBIndex)(insn->sibIndexBase + 4);
1975
1976
      // If EVEX.v2 is set this is one of the 16-31 registers.
1977
1.16k
      if (insn->vectorExtensionType == TYPE_EVEX &&
1978
731
          insn->mode == MODE_64BIT &&
1979
475
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1980
344
        insn->sibIndex =
1981
344
          (SIBIndex)(insn->sibIndex + 16);
1982
1983
      // Adjust the index register to the correct size.
1984
1.16k
      switch (op->type) {
1985
0
      default:
1986
        // debug("Unhandled VSIB index type");
1987
0
        return -1;
1988
677
      case TYPE_MVSIBX:
1989
677
        insn->sibIndex =
1990
677
          (SIBIndex)(SIB_INDEX_XMM0 +
1991
677
               (insn->sibIndex -
1992
677
                insn->sibIndexBase));
1993
677
        break;
1994
205
      case TYPE_MVSIBY:
1995
205
        insn->sibIndex =
1996
205
          (SIBIndex)(SIB_INDEX_YMM0 +
1997
205
               (insn->sibIndex -
1998
205
                insn->sibIndexBase));
1999
205
        break;
2000
278
      case TYPE_MVSIBZ:
2001
278
        insn->sibIndex =
2002
278
          (SIBIndex)(SIB_INDEX_ZMM0 +
2003
278
               (insn->sibIndex -
2004
278
                insn->sibIndexBase));
2005
278
        break;
2006
1.16k
      }
2007
2008
      // Apply the AVX512 compressed displacement scaling factor.
2009
1.16k
      if (op->encoding != ENCODING_REG &&
2010
1.16k
          insn->eaDisplacement == EA_DISP_8)
2011
132
        insn->displacement *=
2012
132
          1 << (op->encoding - ENCODING_VSIB);
2013
1.16k
      break;
2014
2015
96.6k
    case ENCODING_REG:
2016
1.48M
CASE_ENCODING_RM:
2017
1.48M
      if (readModRM(insn))
2018
0
        return -1;
2019
2020
221k
      if (fixupReg(insn, op))
2021
6
        return -1;
2022
2023
      // Apply the AVX512 compressed displacement scaling factor.
2024
221k
      if (op->encoding != ENCODING_REG &&
2025
124k
          insn->eaDisplacement == EA_DISP_8)
2026
21.8k
        insn->displacement *=
2027
21.8k
          1 << (op->encoding - ENCODING_RM);
2028
221k
      break;
2029
2030
50.8k
    case ENCODING_IB:
2031
50.8k
      if (sawRegImm) {
2032
        /* Saw a register immediate so don't read again and instead split the
2033
             previous immediate.  FIXME: This is a hack. */
2034
896
        insn->immediates[insn->numImmediatesConsumed] =
2035
896
          insn->immediates
2036
896
            [insn->numImmediatesConsumed -
2037
896
             1] &
2038
896
          0xf;
2039
896
        ++insn->numImmediatesConsumed;
2040
896
        break;
2041
896
      }
2042
49.9k
      if (readImmediate(insn, 1))
2043
205
        return -1;
2044
49.7k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2045
981
        sawRegImm = 1;
2046
49.7k
      break;
2047
2048
2.00k
    case ENCODING_IW:
2049
2.00k
      if (readImmediate(insn, 2))
2050
15
        return -1;
2051
1.99k
      break;
2052
2053
1.99k
    case ENCODING_ID:
2054
1.23k
      if (readImmediate(insn, 4))
2055
18
        return -1;
2056
1.21k
      break;
2057
2058
1.21k
    case ENCODING_IO:
2059
199
      if (readImmediate(insn, 8))
2060
1
        return -1;
2061
198
      break;
2062
2063
8.99k
    case ENCODING_Iv:
2064
8.99k
      if (readImmediate(insn, insn->immediateSize))
2065
127
        return -1;
2066
8.87k
      break;
2067
2068
8.87k
    case ENCODING_Ia:
2069
2.44k
      if (readImmediate(insn, insn->addressSize))
2070
43
        return -1;
2071
      /* Direct memory-offset (moffset) immediate will get mapped
2072
           to memory operand later. We want the encoding info to
2073
           reflect that as well. */
2074
2.40k
      insn->displacementOffset = insn->immediateOffset;
2075
2.40k
      insn->consumedDisplacement = true;
2076
2.40k
      insn->displacementSize = insn->immediateSize;
2077
2.40k
      insn->displacement =
2078
2.40k
        insn->immediates[insn->numImmediatesConsumed -
2079
2.40k
             1];
2080
2.40k
      insn->immediateOffset = 0;
2081
2.40k
      insn->immediateSize = 0;
2082
2.40k
      break;
2083
2084
755
    case ENCODING_IRC:
2085
755
      insn->RC =
2086
755
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2087
755
         << 1) |
2088
755
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2089
755
      break;
2090
2091
2.86k
    case ENCODING_RB:
2092
2.86k
      if (readOpcodeRegister(insn, 1))
2093
0
        return -1;
2094
2.86k
      break;
2095
2096
2.86k
    case ENCODING_RW:
2097
0
      if (readOpcodeRegister(insn, 2))
2098
0
        return -1;
2099
0
      break;
2100
2101
0
    case ENCODING_RD:
2102
0
      if (readOpcodeRegister(insn, 4))
2103
0
        return -1;
2104
0
      break;
2105
2106
2.40k
    case ENCODING_RO:
2107
2.40k
      if (readOpcodeRegister(insn, 8))
2108
0
        return -1;
2109
2.40k
      break;
2110
2111
18.9k
    case ENCODING_Rv:
2112
18.9k
      if (readOpcodeRegister(insn, 0))
2113
0
        return -1;
2114
18.9k
      break;
2115
2116
18.9k
    case ENCODING_FP:
2117
1.44k
      break;
2118
2119
18.2k
    case ENCODING_VVVV:
2120
18.2k
      if (!hasVVVV)
2121
0
        return -1;
2122
2123
18.2k
      needVVVV =
2124
18.2k
        0; /* Mark that we have found a VVVV operand. */
2125
2126
18.2k
      if (insn->mode != MODE_64BIT)
2127
12.2k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2128
2129
18.2k
      if (fixupReg(insn, op))
2130
1
        return -1;
2131
18.2k
      break;
2132
2133
18.2k
    case ENCODING_WRITEMASK:
2134
12.3k
      if (readMaskRegister(insn))
2135
0
        return -1;
2136
12.3k
      break;
2137
2138
52.3k
    case ENCODING_DUP:
2139
52.3k
      break;
2140
2141
0
    default:
2142
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2143
0
      return -1;
2144
1.39M
    }
2145
1.39M
  }
2146
2147
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2148
231k
  if (needVVVV)
2149
4
    return -1;
2150
2151
231k
  return 0;
2152
231k
}
2153
2154
// return True if instruction is illegal to use with prefixes
2155
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2156
static bool checkPrefix(struct InternalInstruction *insn)
2157
232k
{
2158
  // LOCK prefix
2159
232k
  if (insn->hasLockPrefix) {
2160
10.4k
    switch (insn->instructionID) {
2161
50
    default:
2162
      // invalid LOCK
2163
50
      return true;
2164
2165
    // nop dword [rax]
2166
5
    case X86_NOOPL:
2167
2168
    // DEC
2169
20
    case X86_DEC16m:
2170
30
    case X86_DEC32m:
2171
64
    case X86_DEC64m:
2172
123
    case X86_DEC8m:
2173
2174
    // ADC
2175
191
    case X86_ADC16mi:
2176
223
    case X86_ADC16mi8:
2177
404
    case X86_ADC16mr:
2178
543
    case X86_ADC32mi:
2179
572
    case X86_ADC32mi8:
2180
610
    case X86_ADC32mr:
2181
654
    case X86_ADC64mi32:
2182
736
    case X86_ADC64mi8:
2183
773
    case X86_ADC64mr:
2184
784
    case X86_ADC8mi:
2185
788
    case X86_ADC8mi8:
2186
964
    case X86_ADC8mr:
2187
994
    case X86_ADC8rm:
2188
1.04k
    case X86_ADC16rm:
2189
1.11k
    case X86_ADC32rm:
2190
1.26k
    case X86_ADC64rm:
2191
2192
    // ADD
2193
1.27k
    case X86_ADD16mi:
2194
1.31k
    case X86_ADD16mi8:
2195
1.33k
    case X86_ADD16mr:
2196
1.47k
    case X86_ADD32mi:
2197
1.64k
    case X86_ADD32mi8:
2198
1.86k
    case X86_ADD32mr:
2199
1.94k
    case X86_ADD64mi32:
2200
1.96k
    case X86_ADD64mi8:
2201
1.99k
    case X86_ADD64mr:
2202
2.04k
    case X86_ADD8mi:
2203
2.17k
    case X86_ADD8mi8:
2204
2.40k
    case X86_ADD8mr:
2205
2.40k
    case X86_ADD8rm:
2206
2.46k
    case X86_ADD16rm:
2207
2.53k
    case X86_ADD32rm:
2208
2.71k
    case X86_ADD64rm:
2209
2210
    // AND
2211
2.90k
    case X86_AND16mi:
2212
3.10k
    case X86_AND16mi8:
2213
3.17k
    case X86_AND16mr:
2214
3.18k
    case X86_AND32mi:
2215
3.32k
    case X86_AND32mi8:
2216
3.46k
    case X86_AND32mr:
2217
3.46k
    case X86_AND64mi32:
2218
3.47k
    case X86_AND64mi8:
2219
3.52k
    case X86_AND64mr:
2220
3.54k
    case X86_AND8mi:
2221
3.58k
    case X86_AND8mi8:
2222
3.67k
    case X86_AND8mr:
2223
3.73k
    case X86_AND8rm:
2224
3.78k
    case X86_AND16rm:
2225
3.82k
    case X86_AND32rm:
2226
3.84k
    case X86_AND64rm:
2227
2228
    // BTC
2229
4.02k
    case X86_BTC16mi8:
2230
4.03k
    case X86_BTC16mr:
2231
4.19k
    case X86_BTC32mi8:
2232
4.20k
    case X86_BTC32mr:
2233
4.20k
    case X86_BTC64mi8:
2234
4.21k
    case X86_BTC64mr:
2235
2236
    // BTR
2237
4.29k
    case X86_BTR16mi8:
2238
4.29k
    case X86_BTR16mr:
2239
4.34k
    case X86_BTR32mi8:
2240
4.38k
    case X86_BTR32mr:
2241
4.42k
    case X86_BTR64mi8:
2242
4.44k
    case X86_BTR64mr:
2243
2244
    // BTS
2245
4.45k
    case X86_BTS16mi8:
2246
4.45k
    case X86_BTS16mr:
2247
4.58k
    case X86_BTS32mi8:
2248
4.59k
    case X86_BTS32mr:
2249
4.60k
    case X86_BTS64mi8:
2250
4.65k
    case X86_BTS64mr:
2251
2252
    // CMPXCHG
2253
4.70k
    case X86_CMPXCHG16B:
2254
4.70k
    case X86_CMPXCHG16rm:
2255
4.71k
    case X86_CMPXCHG32rm:
2256
4.73k
    case X86_CMPXCHG64rm:
2257
4.76k
    case X86_CMPXCHG8rm:
2258
4.82k
    case X86_CMPXCHG8B:
2259
2260
    // INC
2261
4.84k
    case X86_INC16m:
2262
5.03k
    case X86_INC32m:
2263
5.05k
    case X86_INC64m:
2264
5.19k
    case X86_INC8m:
2265
2266
    // NEG
2267
5.33k
    case X86_NEG16m:
2268
5.52k
    case X86_NEG32m:
2269
5.52k
    case X86_NEG64m:
2270
5.63k
    case X86_NEG8m:
2271
2272
    // NOT
2273
5.68k
    case X86_NOT16m:
2274
5.72k
    case X86_NOT32m:
2275
5.74k
    case X86_NOT64m:
2276
5.88k
    case X86_NOT8m:
2277
2278
    // OR
2279
6.02k
    case X86_OR16mi:
2280
6.10k
    case X86_OR16mi8:
2281
6.10k
    case X86_OR16mr:
2282
6.11k
    case X86_OR32mi:
2283
6.15k
    case X86_OR32mi8:
2284
6.19k
    case X86_OR32mr:
2285
6.20k
    case X86_OR64mi32:
2286
6.23k
    case X86_OR64mi8:
2287
6.24k
    case X86_OR64mr:
2288
6.28k
    case X86_OR8mi8:
2289
6.34k
    case X86_OR8mi:
2290
6.36k
    case X86_OR8mr:
2291
6.41k
    case X86_OR8rm:
2292
6.57k
    case X86_OR16rm:
2293
6.63k
    case X86_OR32rm:
2294
6.67k
    case X86_OR64rm:
2295
2296
    // SBB
2297
6.96k
    case X86_SBB16mi:
2298
7.13k
    case X86_SBB16mi8:
2299
7.14k
    case X86_SBB16mr:
2300
7.33k
    case X86_SBB32mi:
2301
7.39k
    case X86_SBB32mi8:
2302
7.40k
    case X86_SBB32mr:
2303
7.57k
    case X86_SBB64mi32:
2304
7.57k
    case X86_SBB64mi8:
2305
7.73k
    case X86_SBB64mr:
2306
7.74k
    case X86_SBB8mi:
2307
7.75k
    case X86_SBB8mi8:
2308
7.84k
    case X86_SBB8mr:
2309
2310
    // SUB
2311
7.86k
    case X86_SUB16mi:
2312
7.87k
    case X86_SUB16mi8:
2313
7.91k
    case X86_SUB16mr:
2314
7.92k
    case X86_SUB32mi:
2315
7.95k
    case X86_SUB32mi8:
2316
7.97k
    case X86_SUB32mr:
2317
7.99k
    case X86_SUB64mi32:
2318
8.00k
    case X86_SUB64mi8:
2319
8.01k
    case X86_SUB64mr:
2320
8.05k
    case X86_SUB8mi8:
2321
8.18k
    case X86_SUB8mi:
2322
8.24k
    case X86_SUB8mr:
2323
8.30k
    case X86_SUB8rm:
2324
8.32k
    case X86_SUB16rm:
2325
8.36k
    case X86_SUB32rm:
2326
8.36k
    case X86_SUB64rm:
2327
2328
    // XADD
2329
8.52k
    case X86_XADD16rm:
2330
8.53k
    case X86_XADD32rm:
2331
8.54k
    case X86_XADD64rm:
2332
8.71k
    case X86_XADD8rm:
2333
2334
    // XCHG
2335
8.73k
    case X86_XCHG16rm:
2336
9.09k
    case X86_XCHG32rm:
2337
9.10k
    case X86_XCHG64rm:
2338
9.14k
    case X86_XCHG8rm:
2339
2340
    // XOR
2341
9.15k
    case X86_XOR16mi:
2342
9.17k
    case X86_XOR16mi8:
2343
9.18k
    case X86_XOR16mr:
2344
9.19k
    case X86_XOR32mi:
2345
9.39k
    case X86_XOR32mi8:
2346
9.61k
    case X86_XOR32mr:
2347
9.61k
    case X86_XOR64mi32:
2348
9.79k
    case X86_XOR64mi8:
2349
9.96k
    case X86_XOR64mr:
2350
10.1k
    case X86_XOR8mi8:
2351
10.1k
    case X86_XOR8mi:
2352
10.1k
    case X86_XOR8mr:
2353
10.2k
    case X86_XOR8rm:
2354
10.2k
    case X86_XOR16rm:
2355
10.3k
    case X86_XOR32rm:
2356
10.3k
    case X86_XOR64rm:
2357
2358
      // this instruction can be used with LOCK prefix
2359
10.3k
      return false;
2360
10.4k
    }
2361
10.4k
  }
2362
2363
#if 0
2364
  // REPNE prefix
2365
  if (insn->repeatPrefix) {
2366
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2367
    // In such a case, clear it.
2368
    if (insn->twoByteEscape == 0x0f) {
2369
      insn->prefix0 = 0;
2370
    }
2371
  }
2372
#endif
2373
2374
  // no invalid prefixes
2375
221k
  return false;
2376
232k
}
2377
2378
/*
2379
 * decodeInstruction - Reads and interprets a full instruction provided by the
2380
 *   user.
2381
 *
2382
 * @param insn      - A pointer to the instruction to be populated.  Must be
2383
 *                    pre-allocated.
2384
 * @param reader    - The function to be used to read the instruction's bytes.
2385
 * @param readerArg - A generic argument to be passed to the reader to store
2386
 *                    any internal state.
2387
 * @param startLoc  - The address (in the reader's address space) of the first
2388
 *                    byte in the instruction.
2389
 * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
2390
 *                    decode the instruction in.
2391
 * @return          - 0 if instruction is valid; nonzero if not.
2392
 */
2393
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader,
2394
          const void *readerArg, uint64_t startLoc,
2395
          DisassemblerMode mode)
2396
233k
{
2397
233k
  insn->reader = reader;
2398
233k
  insn->readerArg = readerArg;
2399
233k
  insn->startLocation = startLoc;
2400
233k
  insn->readerCursor = startLoc;
2401
233k
  insn->mode = mode;
2402
233k
  insn->numImmediatesConsumed = 0;
2403
2404
233k
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2405
232k
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2406
1.70k
    return -1;
2407
2408
231k
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2409
2410
  // instruction length must be <= 15 to be valid
2411
231k
  if (insn->length > 15)
2412
8
    return -1;
2413
2414
231k
  if (insn->operandSize == 0)
2415
231k
    insn->operandSize = insn->registerSize;
2416
2417
231k
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2418
2419
231k
  return 0;
2420
231k
}
2421
2422
#endif