Coverage Report

Created: 2025-11-09 07:00

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
110k
{
79
110k
  return CONTEXTS_SYM[attrMask];
80
110k
}
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
110k
{
96
110k
  const struct OpcodeDecision *decision = NULL;
97
110k
  const uint8_t *indextable = NULL;
98
110k
  unsigned int index;
99
100
110k
  switch (type) {
101
0
  default:
102
0
    break;
103
89.1k
  case ONEBYTE:
104
89.1k
    decision = ONEBYTE_SYM;
105
89.1k
    indextable = index_x86DisassemblerOneByteOpcodes;
106
89.1k
    break;
107
10.4k
  case TWOBYTE:
108
10.4k
    decision = TWOBYTE_SYM;
109
10.4k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
10.4k
    break;
111
3.68k
  case THREEBYTE_38:
112
3.68k
    decision = THREEBYTE38_SYM;
113
3.68k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
3.68k
    break;
115
5.89k
  case THREEBYTE_3A:
116
5.89k
    decision = THREEBYTE3A_SYM;
117
5.89k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
5.89k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
1.17k
  case XOP8_MAP:
121
1.17k
    decision = XOP8_MAP_SYM;
122
1.17k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
1.17k
    break;
124
523
  case XOP9_MAP:
125
523
    decision = XOP9_MAP_SYM;
126
523
    indextable = index_x86DisassemblerXOP9Opcodes;
127
523
    break;
128
97
  case XOPA_MAP:
129
97
    decision = XOPA_MAP_SYM;
130
97
    indextable = index_x86DisassemblerXOPAOpcodes;
131
97
    break;
132
21
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
21
    return true;
135
110k
#endif
136
110k
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
110k
  index = indextable[insnContext];
140
110k
  if (index)
141
110k
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
110k
           MODRM_ONEENTRY;
143
469
  else
144
469
    return false;
145
110k
}
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
110k
{
160
110k
  const struct ModRMDecision *dec = NULL;
161
110k
  unsigned int index;
162
110k
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
110k
  switch (type) {
165
0
  default:
166
0
    break; // never reach
167
88.9k
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
88.9k
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
88.9k
    if (index)
171
88.9k
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
7
    else
173
7
      dec = &emptyDecision.modRMDecisions[opcode];
174
88.9k
    break;
175
10.4k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
10.4k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
10.4k
    if (index)
179
10.3k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
133
    else
181
133
      dec = &emptyDecision.modRMDecisions[opcode];
182
10.4k
    break;
183
3.68k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
3.68k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
3.68k
    if (index)
187
3.67k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
10
    else
189
10
      dec = &emptyDecision.modRMDecisions[opcode];
190
3.68k
    break;
191
5.88k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
5.88k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
5.88k
    if (index)
195
5.87k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
15
    else
197
15
      dec = &emptyDecision.modRMDecisions[opcode];
198
5.88k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
1.17k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
1.17k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
1.17k
    if (index)
204
1.01k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
153
    else
206
153
      dec = &emptyDecision.modRMDecisions[opcode];
207
1.17k
    break;
208
522
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
522
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
522
    if (index)
212
397
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
125
    else
214
125
      dec = &emptyDecision.modRMDecisions[opcode];
215
522
    break;
216
97
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
97
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
97
    if (index)
220
71
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
26
    else
222
26
      dec = &emptyDecision.modRMDecisions[opcode];
223
97
    break;
224
21
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
21
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
21
    if (index)
228
14
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
14
               .modRMDecisions[opcode];
230
7
    else
231
7
      dec = &emptyDecision.modRMDecisions[opcode];
232
21
    break;
233
110k
#endif
234
110k
  }
235
236
110k
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
50.1k
  case MODRM_ONEENTRY:
241
50.1k
    return modRMTable[dec->instructionIDs];
242
47.3k
  case MODRM_SPLITRM:
243
47.3k
    if (modFromModRM(modRM) == 0x3)
244
11.4k
      return modRMTable[dec->instructionIDs + 1];
245
35.8k
    return modRMTable[dec->instructionIDs];
246
11.2k
  case MODRM_SPLITREG:
247
11.2k
    if (modFromModRM(modRM) == 0x3)
248
3.16k
      return modRMTable[dec->instructionIDs +
249
3.16k
            ((modRM & 0x38) >> 3) + 8];
250
8.13k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
2.04k
  case MODRM_SPLITMISC:
252
2.04k
    if (modFromModRM(modRM) == 0x3)
253
632
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
632
            8];
255
1.41k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
110k
  }
259
110k
}
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
92.9k
{
271
92.9k
  return &INSTRUCTIONS_SYM[uid];
272
92.9k
}
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
323k
{
286
323k
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
323k
  if (!ret)
289
323k
    ++(insn->readerCursor);
290
291
323k
  return ret;
292
323k
}
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
42.5k
{
303
42.5k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
42.5k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
107k
{
308
107k
  insn->readerCursor--;
309
107k
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
15.0k
  { \
314
15.0k
    type combined = 0; \
315
15.0k
    unsigned offset; \
316
47.7k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
32.8k
      uint8_t byte; \
318
32.8k
      int ret = insn->reader(insn->readerArg, &byte, \
319
32.8k
                 insn->readerCursor + offset); \
320
32.8k
      if (ret) \
321
32.8k
        return ret; \
322
32.8k
      combined = combined | \
323
32.7k
           ((uint64_t)byte << (offset * 8)); \
324
32.7k
    } \
325
15.0k
    *ptr = combined; \
326
14.9k
    insn->readerCursor += sizeof(type); \
327
14.9k
    return 0; \
328
15.0k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
6.79k
  { \
314
6.79k
    type combined = 0; \
315
6.79k
    unsigned offset; \
316
13.5k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
6.79k
      uint8_t byte; \
318
6.79k
      int ret = insn->reader(insn->readerArg, &byte, \
319
6.79k
                 insn->readerCursor + offset); \
320
6.79k
      if (ret) \
321
6.79k
        return ret; \
322
6.79k
      combined = combined | \
323
6.77k
           ((uint64_t)byte << (offset * 8)); \
324
6.77k
    } \
325
6.79k
    *ptr = combined; \
326
6.77k
    insn->readerCursor += sizeof(type); \
327
6.77k
    return 0; \
328
6.79k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
1.41k
  { \
314
1.41k
    type combined = 0; \
315
1.41k
    unsigned offset; \
316
4.23k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
2.82k
      uint8_t byte; \
318
2.82k
      int ret = insn->reader(insn->readerArg, &byte, \
319
2.82k
                 insn->readerCursor + offset); \
320
2.82k
      if (ret) \
321
2.82k
        return ret; \
322
2.82k
      combined = combined | \
323
2.82k
           ((uint64_t)byte << (offset * 8)); \
324
2.82k
    } \
325
1.41k
    *ptr = combined; \
326
1.41k
    insn->readerCursor += sizeof(type); \
327
1.41k
    return 0; \
328
1.41k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
1.95k
  { \
314
1.95k
    type combined = 0; \
315
1.95k
    unsigned offset; \
316
9.72k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
7.78k
      uint8_t byte; \
318
7.78k
      int ret = insn->reader(insn->readerArg, &byte, \
319
7.78k
                 insn->readerCursor + offset); \
320
7.78k
      if (ret) \
321
7.78k
        return ret; \
322
7.78k
      combined = combined | \
323
7.76k
           ((uint64_t)byte << (offset * 8)); \
324
7.76k
    } \
325
1.95k
    *ptr = combined; \
326
1.93k
    insn->readerCursor += sizeof(type); \
327
1.93k
    return 0; \
328
1.95k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
2.80k
  { \
314
2.80k
    type combined = 0; \
315
2.80k
    unsigned offset; \
316
8.38k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
5.59k
      uint8_t byte; \
318
5.59k
      int ret = insn->reader(insn->readerArg, &byte, \
319
5.59k
                 insn->readerCursor + offset); \
320
5.59k
      if (ret) \
321
5.59k
        return ret; \
322
5.59k
      combined = combined | \
323
5.57k
           ((uint64_t)byte << (offset * 8)); \
324
5.57k
    } \
325
2.80k
    *ptr = combined; \
326
2.78k
    insn->readerCursor += sizeof(type); \
327
2.78k
    return 0; \
328
2.80k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
1.70k
  { \
314
1.70k
    type combined = 0; \
315
1.70k
    unsigned offset; \
316
8.40k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
6.73k
      uint8_t byte; \
318
6.73k
      int ret = insn->reader(insn->readerArg, &byte, \
319
6.73k
                 insn->readerCursor + offset); \
320
6.73k
      if (ret) \
321
6.73k
        return ret; \
322
6.73k
      combined = combined | \
323
6.70k
           ((uint64_t)byte << (offset * 8)); \
324
6.70k
    } \
325
1.70k
    *ptr = combined; \
326
1.66k
    insn->readerCursor += sizeof(type); \
327
1.66k
    return 0; \
328
1.70k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
392
  { \
314
392
    type combined = 0; \
315
392
    unsigned offset; \
316
3.45k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
3.07k
      uint8_t byte; \
318
3.07k
      int ret = insn->reader(insn->readerArg, &byte, \
319
3.07k
                 insn->readerCursor + offset); \
320
3.07k
      if (ret) \
321
3.07k
        return ret; \
322
3.07k
      combined = combined | \
323
3.06k
           ((uint64_t)byte << (offset * 8)); \
324
3.06k
    } \
325
392
    *ptr = combined; \
326
380
    insn->readerCursor += sizeof(type); \
327
380
    return 0; \
328
392
  }
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
86.2k
{
349
86.2k
  if (insn->mode == MODE_64BIT)
350
34.1k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
52.0k
  return false;
353
86.2k
}
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
18.4k
{
363
18.4k
  uint8_t nextByte;
364
365
18.4k
  switch (prefix) {
366
3.83k
  case 0xf0: // LOCK
367
3.83k
    insn->hasLockPrefix = true;
368
3.83k
    insn->repeatPrefix = 0;
369
3.83k
    break;
370
371
3.60k
  case 0xf2: // REPNE/REPNZ
372
6.87k
  case 0xf3: // REP or REPE/REPZ
373
6.87k
    if (lookAtByte(insn, &nextByte))
374
3
      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
6.87k
    if (isREX(insn, nextByte) || nextByte == 0x0f ||
382
4.65k
        nextByte == 0x66)
383
      // The last of 0xf2 /0xf3 is mandatory prefix
384
2.42k
      insn->mandatoryPrefix = prefix;
385
386
6.87k
    insn->repeatPrefix = prefix;
387
6.87k
    insn->hasLockPrefix = false;
388
6.87k
    break;
389
390
3.24k
  case 0x66:
391
3.24k
    if (lookAtByte(insn, &nextByte))
392
5
      break;
393
    // 0x66 can't overwrite existing mandatory prefix and should be ignored
394
3.23k
    if (!insn->mandatoryPrefix &&
395
2.81k
        (nextByte == 0x0f || isREX(insn, nextByte)))
396
1.35k
      insn->mandatoryPrefix = prefix;
397
3.23k
    break;
398
18.4k
  }
399
18.4k
}
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
82.4k
{
412
82.4k
  bool isPrefix = true;
413
82.4k
  uint8_t byte = 0;
414
82.4k
  uint8_t nextByte;
415
416
183k
  while (isPrefix) {
417
100k
    if (insn->mode == MODE_64BIT) {
418
      // eliminate consecutive redundant REX bytes in front
419
41.6k
      if (consumeByte(insn, &byte))
420
17
        return -1;
421
422
41.6k
      if ((byte & 0xf0) == 0x40) {
423
9.26k
        while (true) {
424
9.26k
          if (lookAtByte(
425
9.26k
                insn,
426
9.26k
                &byte)) // out of input code
427
17
            return -1;
428
9.25k
          if ((byte & 0xf0) == 0x40) {
429
            // another REX prefix, but we only remember the last one
430
846
            if (consumeByte(insn, &byte))
431
0
              return -1;
432
846
          } else
433
8.40k
            break;
434
9.25k
        }
435
436
        // recover the last REX byte if next byte is not a legacy prefix
437
8.40k
        switch (byte) {
438
109
        case 0xf2: /* REPNE/REPNZ */
439
284
        case 0xf3: /* REP or REPE/REPZ */
440
420
        case 0xf0: /* LOCK */
441
427
        case 0x2e: /* CS segment override -OR- Branch not taken */
442
564
        case 0x36: /* SS segment override -OR- Branch taken */
443
575
        case 0x3e: /* DS segment override */
444
598
        case 0x26: /* ES segment override */
445
676
        case 0x64: /* FS segment override */
446
1.66k
        case 0x65: /* GS segment override */
447
1.89k
        case 0x66: /* Operand-size override */
448
2.20k
        case 0x67: /* Address-size override */
449
2.20k
          break;
450
6.20k
        default: /* Not a prefix byte */
451
6.20k
          unconsumeByte(insn);
452
6.20k
          break;
453
8.40k
        }
454
33.1k
      } else {
455
33.1k
        unconsumeByte(insn);
456
33.1k
      }
457
41.6k
    }
458
459
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
460
100k
    if (consumeByte(insn, &byte))
461
17
      return -1;
462
463
100k
    if (insn->readerCursor - 1 == insn->startLocation &&
464
81.5k
        (byte == 0xf2 || byte == 0xf3)) {
465
      // prefix requires next byte
466
5.55k
      if (lookAtByte(insn, &nextByte))
467
10
        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
5.54k
      if (((nextByte == 0xf0) ||
477
5.39k
           ((nextByte & 0xfe) == 0x86 ||
478
5.33k
            (nextByte & 0xf8) == 0x90))) {
479
229
        insn->xAcquireRelease = byte;
480
229
      }
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
5.54k
      if (byte == 0xf3 &&
489
2.81k
          (nextByte == 0x88 || nextByte == 0x89 ||
490
2.78k
           nextByte == 0xc6 || nextByte == 0xc7)) {
491
185
        insn->xAcquireRelease = byte;
492
185
      }
493
494
5.54k
      if (isREX(insn, nextByte)) {
495
976
        uint8_t nnextByte;
496
497
        // Go to REX prefix after the current one
498
976
        if (consumeByte(insn, &nnextByte))
499
0
          return -1;
500
501
        // We should be able to read next byte after REX prefix
502
976
        if (lookAtByte(insn, &nnextByte))
503
1
          return -1;
504
505
975
        unconsumeByte(insn);
506
975
      }
507
5.54k
    }
508
509
100k
    switch (byte) {
510
3.83k
    case 0xf0: /* LOCK */
511
7.44k
    case 0xf2: /* REPNE/REPNZ */
512
10.7k
    case 0xf3: /* REP or REPE/REPZ */
513
      // only accept the last prefix
514
10.7k
      setPrefixPresent(insn, byte);
515
10.7k
      insn->prefix0 = byte;
516
10.7k
      break;
517
518
284
    case 0x2e: /* CS segment override -OR- Branch not taken */
519
634
    case 0x36: /* SS segment override -OR- Branch taken */
520
902
    case 0x3e: /* DS segment override */
521
1.10k
    case 0x26: /* ES segment override */
522
1.59k
    case 0x64: /* FS segment override */
523
2.94k
    case 0x65: /* GS segment override */
524
2.94k
      switch (byte) {
525
284
      case 0x2e:
526
284
        insn->segmentOverride = SEG_OVERRIDE_CS;
527
284
        insn->prefix1 = byte;
528
284
        break;
529
350
      case 0x36:
530
350
        insn->segmentOverride = SEG_OVERRIDE_SS;
531
350
        insn->prefix1 = byte;
532
350
        break;
533
268
      case 0x3e:
534
268
        insn->segmentOverride = SEG_OVERRIDE_DS;
535
268
        insn->prefix1 = byte;
536
268
        break;
537
200
      case 0x26:
538
200
        insn->segmentOverride = SEG_OVERRIDE_ES;
539
200
        insn->prefix1 = byte;
540
200
        break;
541
495
      case 0x64:
542
495
        insn->segmentOverride = SEG_OVERRIDE_FS;
543
495
        insn->prefix1 = byte;
544
495
        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
2.94k
      }
553
2.94k
      setPrefixPresent(insn, byte);
554
2.94k
      break;
555
556
3.24k
    case 0x66: /* Operand-size override */
557
3.24k
      insn->hasOpSize = true;
558
3.24k
      setPrefixPresent(insn, byte);
559
3.24k
      insn->prefix2 = byte;
560
3.24k
      break;
561
562
1.56k
    case 0x67: /* Address-size override */
563
1.56k
      insn->hasAdSize = true;
564
1.56k
      setPrefixPresent(insn, byte);
565
1.56k
      insn->prefix3 = byte;
566
1.56k
      break;
567
82.3k
    default: /* Not a prefix byte */
568
82.3k
      isPrefix = false;
569
82.3k
      break;
570
100k
    }
571
100k
  }
572
573
82.3k
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
574
575
82.3k
  if (byte == 0x62) {
576
7.80k
    uint8_t byte1, byte2;
577
578
7.80k
    if (consumeByte(insn, &byte1)) {
579
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
580
10
      return -1;
581
10
    }
582
583
7.79k
    if (lookAtByte(insn, &byte2)) {
584
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
585
6
      unconsumeByte(insn); /* unconsume byte1 */
586
6
      unconsumeByte(insn); /* unconsume byte  */
587
7.78k
    } else {
588
7.78k
      if ((insn->mode == MODE_64BIT ||
589
4.72k
           (byte1 & 0xc0) == 0xc0) &&
590
7.04k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
591
7.03k
        insn->vectorExtensionType = TYPE_EVEX;
592
7.03k
      } else {
593
752
        unconsumeByte(insn); /* unconsume byte1 */
594
752
        unconsumeByte(insn); /* unconsume byte  */
595
752
      }
596
7.78k
    }
597
598
7.79k
    if (insn->vectorExtensionType == TYPE_EVEX) {
599
7.03k
      insn->vectorExtensionPrefix[0] = byte;
600
7.03k
      insn->vectorExtensionPrefix[1] = byte1;
601
7.03k
      if (consumeByte(insn,
602
7.03k
          &insn->vectorExtensionPrefix[2])) {
603
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
604
0
        return -1;
605
0
      }
606
607
7.03k
      if (consumeByte(insn,
608
7.03k
          &insn->vectorExtensionPrefix[3])) {
609
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
610
5
        return -1;
611
5
      }
612
613
      /* We simulate the REX prefix for simplicity's sake */
614
7.02k
      if (insn->mode == MODE_64BIT) {
615
3.05k
        insn->rexPrefix =
616
3.05k
          0x40 |
617
3.05k
          (wFromEVEX3of4(
618
3.05k
             insn->vectorExtensionPrefix[2])
619
3.05k
           << 3) |
620
3.05k
          (rFromEVEX2of4(
621
3.05k
             insn->vectorExtensionPrefix[1])
622
3.05k
           << 2) |
623
3.05k
          (xFromEVEX2of4(
624
3.05k
             insn->vectorExtensionPrefix[1])
625
3.05k
           << 1) |
626
3.05k
          (bFromEVEX2of4(
627
3.05k
             insn->vectorExtensionPrefix[1])
628
3.05k
           << 0);
629
3.05k
      }
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
7.02k
    }
635
74.5k
  } else if (byte == 0xc4) {
636
997
    uint8_t byte1;
637
638
997
    if (lookAtByte(insn, &byte1)) {
639
      // dbgprintf(insn, "Couldn't read second byte of VEX");
640
1
      return -1;
641
1
    }
642
643
996
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
644
819
      insn->vectorExtensionType = TYPE_VEX_3B;
645
177
    else
646
177
      unconsumeByte(insn);
647
648
996
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
649
819
      insn->vectorExtensionPrefix[0] = byte;
650
819
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
651
819
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
652
653
      /* We simulate the REX prefix for simplicity's sake */
654
819
      if (insn->mode == MODE_64BIT)
655
352
        insn->rexPrefix =
656
352
          0x40 |
657
352
          (wFromVEX3of3(
658
352
             insn->vectorExtensionPrefix[2])
659
352
           << 3) |
660
352
          (rFromVEX2of3(
661
352
             insn->vectorExtensionPrefix[1])
662
352
           << 2) |
663
352
          (xFromVEX2of3(
664
352
             insn->vectorExtensionPrefix[1])
665
352
           << 1) |
666
352
          (bFromVEX2of3(
667
352
             insn->vectorExtensionPrefix[1])
668
352
           << 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
819
    }
674
73.5k
  } else if (byte == 0xc5) {
675
650
    uint8_t byte1;
676
677
650
    if (lookAtByte(insn, &byte1)) {
678
      // dbgprintf(insn, "Couldn't read second byte of VEX");
679
2
      return -1;
680
2
    }
681
682
648
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
683
541
      insn->vectorExtensionType = TYPE_VEX_2B;
684
107
    else
685
107
      unconsumeByte(insn);
686
687
648
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
688
541
      insn->vectorExtensionPrefix[0] = byte;
689
541
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
690
691
541
      if (insn->mode == MODE_64BIT)
692
93
        insn->rexPrefix =
693
93
          0x40 |
694
93
          (rFromVEX2of2(
695
93
             insn->vectorExtensionPrefix[1])
696
93
           << 2);
697
698
541
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
699
151
      default:
700
151
        break;
701
390
      case VEX_PREFIX_66:
702
390
        insn->hasOpSize = true;
703
390
        break;
704
541
      }
705
706
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
707
      //    insn->vectorExtensionPrefix[0],
708
      //    insn->vectorExtensionPrefix[1]);
709
541
    }
710
72.8k
  } else if (byte == 0x8f) {
711
947
    uint8_t byte1;
712
713
947
    if (lookAtByte(insn, &byte1)) {
714
      // dbgprintf(insn, "Couldn't read second byte of XOP");
715
2
      return -1;
716
2
    }
717
718
945
    if ((byte1 & 0x38) !=
719
945
        0x0) /* 0 in these 3 bits is a POP instruction. */
720
910
      insn->vectorExtensionType = TYPE_XOP;
721
35
    else
722
35
      unconsumeByte(insn);
723
724
945
    if (insn->vectorExtensionType == TYPE_XOP) {
725
910
      insn->vectorExtensionPrefix[0] = byte;
726
910
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
727
910
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
728
729
      /* We simulate the REX prefix for simplicity's sake */
730
910
      if (insn->mode == MODE_64BIT)
731
414
        insn->rexPrefix =
732
414
          0x40 |
733
414
          (wFromXOP3of3(
734
414
             insn->vectorExtensionPrefix[2])
735
414
           << 3) |
736
414
          (rFromXOP2of3(
737
414
             insn->vectorExtensionPrefix[1])
738
414
           << 2) |
739
414
          (xFromXOP2of3(
740
414
             insn->vectorExtensionPrefix[1])
741
414
           << 1) |
742
414
          (bFromXOP2of3(
743
414
             insn->vectorExtensionPrefix[1])
744
414
           << 0);
745
746
910
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
747
910
      default:
748
910
        break;
749
910
      case VEX_PREFIX_66:
750
0
        insn->hasOpSize = true;
751
0
        break;
752
910
      }
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
910
    }
758
71.9k
  } else if (isREX(insn, byte)) {
759
6.20k
    if (lookAtByte(insn, &nextByte))
760
0
      return -1;
761
762
6.20k
    insn->rexPrefix = byte;
763
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
764
6.20k
  } else
765
65.7k
    unconsumeByte(insn);
766
767
82.3k
  if (insn->mode == MODE_16BIT) {
768
25.3k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
769
25.3k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
770
25.3k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
771
25.3k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
772
25.3k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
773
56.9k
  } else if (insn->mode == MODE_32BIT) {
774
24.6k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
775
24.6k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
776
24.6k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
777
24.6k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
778
24.6k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
779
32.3k
  } else if (insn->mode == MODE_64BIT) {
780
32.3k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
781
5.29k
      insn->registerSize = 8;
782
5.29k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
783
5.29k
      insn->displacementSize = 4;
784
5.29k
      insn->immediateSize = 4;
785
5.29k
      insn->immSize = 4;
786
27.0k
    } else {
787
27.0k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
788
27.0k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
789
27.0k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
790
27.0k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
791
27.0k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
792
27.0k
    }
793
32.3k
  }
794
795
82.3k
  return 0;
796
82.3k
}
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
82.3k
{
809
82.3k
  uint8_t current;
810
811
  // dbgprintf(insn, "readOpcode()");
812
813
82.3k
  insn->opcodeType = ONEBYTE;
814
815
82.3k
  if (insn->vectorExtensionType == TYPE_EVEX) {
816
7.02k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
817
1
    default:
818
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
819
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
820
1
      return -1;
821
2.43k
    case VEX_LOB_0F:
822
2.43k
      insn->opcodeType = TWOBYTE;
823
2.43k
      return consumeByte(insn, &insn->opcode);
824
1.85k
    case VEX_LOB_0F38:
825
1.85k
      insn->opcodeType = THREEBYTE_38;
826
1.85k
      return consumeByte(insn, &insn->opcode);
827
2.74k
    case VEX_LOB_0F3A:
828
2.74k
      insn->opcodeType = THREEBYTE_3A;
829
2.74k
      return consumeByte(insn, &insn->opcode);
830
7.02k
    }
831
75.2k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
832
819
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
833
1
    default:
834
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
835
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
836
1
      return -1;
837
64
    case VEX_LOB_0F:
838
      //insn->twoByteEscape = 0x0f;
839
64
      insn->opcodeType = TWOBYTE;
840
64
      return consumeByte(insn, &insn->opcode);
841
536
    case VEX_LOB_0F38:
842
      //insn->twoByteEscape = 0x0f;
843
536
      insn->opcodeType = THREEBYTE_38;
844
536
      return consumeByte(insn, &insn->opcode);
845
218
    case VEX_LOB_0F3A:
846
      //insn->twoByteEscape = 0x0f;
847
218
      insn->opcodeType = THREEBYTE_3A;
848
218
      return consumeByte(insn, &insn->opcode);
849
819
    }
850
74.4k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
851
    //insn->twoByteEscape = 0x0f;
852
541
    insn->opcodeType = TWOBYTE;
853
541
    return consumeByte(insn, &insn->opcode);
854
73.9k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
855
910
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
856
1
    default:
857
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
858
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
859
1
      return -1;
860
712
    case XOP_MAP_SELECT_8:
861
712
      insn->opcodeType = XOP8_MAP;
862
712
      return consumeByte(insn, &insn->opcode);
863
171
    case XOP_MAP_SELECT_9:
864
171
      insn->opcodeType = XOP9_MAP;
865
171
      return consumeByte(insn, &insn->opcode);
866
26
    case XOP_MAP_SELECT_A:
867
26
      insn->opcodeType = XOPA_MAP;
868
26
      return consumeByte(insn, &insn->opcode);
869
910
    }
870
910
  }
871
872
73.0k
  if (consumeByte(insn, &current))
873
0
    return -1;
874
875
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
876
73.0k
  insn->firstByte = current;
877
878
73.0k
  if (current == 0x0f) {
879
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
880
4.84k
    insn->twoByteEscape = current;
881
882
4.84k
    if (consumeByte(insn, &current))
883
8
      return -1;
884
885
4.83k
    if (current == 0x38) {
886
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
887
43
      if (consumeByte(insn, &current))
888
1
        return -1;
889
890
42
      insn->opcodeType = THREEBYTE_38;
891
4.79k
    } else if (current == 0x3a) {
892
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
893
22
      if (consumeByte(insn, &current))
894
0
        return -1;
895
896
22
      insn->opcodeType = THREEBYTE_3A;
897
4.77k
    } 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
16
      if (readModRM(insn))
901
1
        return -1;
902
903
15
      if (consumeByte(insn, &current))
904
1
        return -1;
905
906
14
      insn->opcodeType = THREEDNOW_MAP;
907
4.75k
    } else {
908
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
909
4.75k
      insn->opcodeType = TWOBYTE;
910
4.75k
    }
911
68.1k
  } else if (insn->mandatoryPrefix)
912
    // The opcode with mandatory prefix must start with opcode escape.
913
    // If not it's legacy repeat prefix
914
1.37k
    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
73.0k
  insn->opcode = current;
922
923
73.0k
  return 0;
924
73.0k
}
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
110k
{
950
110k
  bool hasModRMExtension;
951
952
110k
  InstructionContext instructionClass = contextForAttrs(attrMask);
953
954
110k
  hasModRMExtension =
955
110k
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
956
957
110k
  if (hasModRMExtension) {
958
60.9k
    if (readModRM(insn))
959
211
      return -1;
960
961
60.6k
    *instructionID = decode(insn->opcodeType, instructionClass,
962
60.6k
          insn->opcode, insn->modRM);
963
60.6k
  } else {
964
50.0k
    *instructionID = decode(insn->opcodeType, instructionClass,
965
50.0k
          insn->opcode, 0);
966
50.0k
  }
967
968
110k
  return 0;
969
110k
}
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
22.7k
{
980
22.7k
  size_t i;
981
22.7k
  uint16_t idx;
982
983
22.7k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
984
11.5k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
985
11.5k
          x86_16_bit_eq_tbl[i].first == orig;
986
11.2k
         i++) {
987
11.2k
      if (x86_16_bit_eq_tbl[i].second == equiv)
988
11.0k
        return true;
989
11.2k
    }
990
11.2k
  }
991
992
11.7k
  return false;
993
22.7k
}
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
2.42k
{
1002
2.42k
  unsigned int i = find_insn(id);
1003
2.42k
  if (i != -1) {
1004
2.41k
    return insns[i].is64bit;
1005
2.41k
  }
1006
1007
  // not found??
1008
11
  return false;
1009
2.42k
}
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
82.2k
{
1022
82.2k
  uint16_t attrMask;
1023
82.2k
  uint16_t instructionID;
1024
1025
82.2k
  attrMask = ATTR_NONE;
1026
1027
82.2k
  if (insn->mode == MODE_64BIT)
1028
32.3k
    attrMask |= ATTR_64BIT;
1029
1030
82.2k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1031
9.28k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1032
7.02k
            ATTR_EVEX :
1033
9.28k
            ATTR_VEX;
1034
1035
9.28k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1036
7.02k
      switch (ppFromEVEX3of4(
1037
7.02k
        insn->vectorExtensionPrefix[2])) {
1038
5.28k
      case VEX_PREFIX_66:
1039
5.28k
        attrMask |= ATTR_OPSIZE;
1040
5.28k
        break;
1041
392
      case VEX_PREFIX_F3:
1042
392
        attrMask |= ATTR_XS;
1043
392
        break;
1044
150
      case VEX_PREFIX_F2:
1045
150
        attrMask |= ATTR_XD;
1046
150
        break;
1047
7.02k
      }
1048
1049
7.02k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1050
1.62k
        attrMask |= ATTR_EVEXKZ;
1051
7.02k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1052
2.25k
        attrMask |= ATTR_EVEXB;
1053
7.02k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1054
5.00k
        attrMask |= ATTR_EVEXK;
1055
7.02k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1056
2.79k
        attrMask |= ATTR_EVEXL;
1057
7.02k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1058
2.55k
        attrMask |= ATTR_EVEXL2;
1059
7.02k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1060
815
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1061
747
      case VEX_PREFIX_66:
1062
747
        attrMask |= ATTR_OPSIZE;
1063
747
        break;
1064
10
      case VEX_PREFIX_F3:
1065
10
        attrMask |= ATTR_XS;
1066
10
        break;
1067
40
      case VEX_PREFIX_F2:
1068
40
        attrMask |= ATTR_XD;
1069
40
        break;
1070
815
      }
1071
1072
815
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1073
207
        attrMask |= ATTR_VEXL;
1074
1.44k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1075
540
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1076
390
      case VEX_PREFIX_66:
1077
390
        attrMask |= ATTR_OPSIZE;
1078
390
        break;
1079
44
      case VEX_PREFIX_F3:
1080
44
        attrMask |= ATTR_XS;
1081
44
        break;
1082
29
      case VEX_PREFIX_F2:
1083
29
        attrMask |= ATTR_XD;
1084
29
        break;
1085
540
      }
1086
1087
540
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1088
433
        attrMask |= ATTR_VEXL;
1089
908
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1090
908
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1091
0
      case VEX_PREFIX_66:
1092
0
        attrMask |= ATTR_OPSIZE;
1093
0
        break;
1094
0
      case VEX_PREFIX_F3:
1095
0
        attrMask |= ATTR_XS;
1096
0
        break;
1097
8
      case VEX_PREFIX_F2:
1098
8
        attrMask |= ATTR_XD;
1099
8
        break;
1100
908
      }
1101
1102
908
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1103
41
        attrMask |= ATTR_VEXL;
1104
908
    } else {
1105
0
      return -1;
1106
0
    }
1107
73.0k
  } else if (!insn->mandatoryPrefix) {
1108
    // If we don't have mandatory prefix we should use legacy prefixes here
1109
70.8k
    if (insn->hasOpSize && (insn->mode != MODE_16BIT))
1110
1.37k
      attrMask |= ATTR_OPSIZE;
1111
70.8k
    if (insn->hasAdSize)
1112
1.29k
      attrMask |= ATTR_ADSIZE;
1113
70.8k
    if (insn->opcodeType == ONEBYTE) {
1114
68.1k
      if (insn->repeatPrefix == 0xf3 &&
1115
1.92k
          (insn->opcode == 0x90))
1116
        // Special support for PAUSE
1117
6
        attrMask |= ATTR_XS;
1118
68.1k
    } else {
1119
2.63k
      if (insn->repeatPrefix == 0xf2)
1120
0
        attrMask |= ATTR_XD;
1121
2.63k
      else if (insn->repeatPrefix == 0xf3)
1122
2
        attrMask |= ATTR_XS;
1123
2.63k
    }
1124
70.8k
  } else {
1125
2.20k
    switch (insn->mandatoryPrefix) {
1126
405
    case 0xf2:
1127
405
      attrMask |= ATTR_XD;
1128
405
      break;
1129
862
    case 0xf3:
1130
862
      attrMask |= ATTR_XS;
1131
862
      break;
1132
933
    case 0x66:
1133
933
      if (insn->mode != MODE_16BIT)
1134
917
        attrMask |= ATTR_OPSIZE;
1135
933
      break;
1136
0
    case 0x67:
1137
0
      attrMask |= ATTR_ADSIZE;
1138
0
      break;
1139
2.20k
    }
1140
2.20k
  }
1141
1142
82.2k
  if (insn->rexPrefix & 0x08) {
1143
5.29k
    attrMask |= ATTR_REXW;
1144
5.29k
    attrMask &= ~ATTR_ADSIZE;
1145
5.29k
  }
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
82.2k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1152
21.4k
      insn->opcode == 0xE3)
1153
311
    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
82.2k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1160
1.32k
    switch (insn->opcode) {
1161
1
    case 0xE8:
1162
18
    case 0xE9:
1163
      // Take care of psubsb and other mmx instructions.
1164
18
      if (insn->opcodeType == ONEBYTE) {
1165
0
        attrMask ^= ATTR_OPSIZE;
1166
0
        insn->immediateSize = 4;
1167
0
        insn->displacementSize = 4;
1168
0
      }
1169
18
      break;
1170
17
    case 0x82:
1171
20
    case 0x83:
1172
20
    case 0x84:
1173
34
    case 0x85:
1174
53
    case 0x86:
1175
322
    case 0x87:
1176
327
    case 0x88:
1177
330
    case 0x89:
1178
346
    case 0x8A:
1179
512
    case 0x8B:
1180
521
    case 0x8C:
1181
665
    case 0x8D:
1182
674
    case 0x8E:
1183
674
    case 0x8F:
1184
      // Take care of lea and three byte ops.
1185
674
      if (insn->opcodeType == TWOBYTE) {
1186
32
        attrMask ^= ATTR_OPSIZE;
1187
32
        insn->immediateSize = 4;
1188
32
        insn->displacementSize = 4;
1189
32
      }
1190
674
      break;
1191
1.32k
    }
1192
1.32k
  }
1193
1194
  /* The following clauses compensate for limitations of the tables. */
1195
82.2k
  if (insn->mode != MODE_64BIT &&
1196
49.9k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1197
5.37k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1198
6
      return -1;
1199
6
    }
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
5.37k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1206
3.96k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1207
3.17k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1208
466
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1209
3.09k
        (insn->vectorExtensionType == TYPE_XOP &&
1210
2.42k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1211
2.42k
      uint16_t instructionIDWithREXW;
1212
1213
2.42k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1214
2.42k
                attrMask | ATTR_REXW)) {
1215
0
        insn->instructionID = instructionID;
1216
0
        insn->spec = specifierForUID(instructionID);
1217
0
        return 0;
1218
0
      }
1219
1220
      // If not a 64-bit instruction. Switch the opcode.
1221
2.42k
      if (!is64Bit(instructionIDWithREXW)) {
1222
1.85k
        insn->instructionID = instructionIDWithREXW;
1223
1.85k
        insn->spec =
1224
1.85k
          specifierForUID(instructionIDWithREXW);
1225
1226
1.85k
        return 0;
1227
1.85k
      }
1228
2.42k
    }
1229
5.37k
  }
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
80.4k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1239
79.5k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1240
79.5k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1241
    /* Make sure we observed the prefixes in any position. */
1242
906
    if (insn->hasAdSize)
1243
6
      attrMask |= ATTR_ADSIZE;
1244
1245
906
    if (insn->hasOpSize)
1246
23
      attrMask |= ATTR_OPSIZE;
1247
1248
    /* In 16-bit, invert the attributes. */
1249
906
    if (insn->mode == MODE_16BIT) {
1250
482
      attrMask ^= ATTR_ADSIZE;
1251
1252
      /* The OpSize attribute is only valid with the absolute moves. */
1253
482
      if (insn->opcodeType == ONEBYTE &&
1254
480
          ((insn->opcode & 0xFC) == 0xA0))
1255
480
        attrMask ^= ATTR_OPSIZE;
1256
482
    }
1257
1258
906
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1259
2
      return -1;
1260
2
    }
1261
1262
904
    insn->instructionID = instructionID;
1263
904
    insn->spec = specifierForUID(instructionID);
1264
1265
904
    return 0;
1266
906
  }
1267
79.5k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1268
203
    return -1;
1269
203
  }
1270
1271
79.3k
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1272
26.4k
      !(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
22.7k
    const struct InstructionSpecifier *spec;
1281
22.7k
    uint16_t instructionIDWithOpsize;
1282
1283
22.7k
    spec = specifierForUID(instructionID);
1284
1285
22.7k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1286
22.7k
              attrMask | ATTR_OPSIZE)) {
1287
      /*
1288
       * ModRM required with OpSize but not present; give up and return version
1289
       * without OpSize set
1290
       */
1291
0
      insn->instructionID = instructionID;
1292
0
      insn->spec = spec;
1293
1294
0
      return 0;
1295
0
    }
1296
1297
22.7k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1298
11.0k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1299
10.8k
      insn->instructionID = instructionIDWithOpsize;
1300
10.8k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1301
11.9k
    } else {
1302
11.9k
      insn->instructionID = instructionID;
1303
11.9k
      insn->spec = spec;
1304
11.9k
    }
1305
1306
22.7k
    return 0;
1307
22.7k
  }
1308
1309
56.5k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1310
258
      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
2
    const struct InstructionSpecifier *spec;
1316
2
    uint16_t instructionIDWithNewOpcode;
1317
2
    const struct InstructionSpecifier *specWithNewOpcode;
1318
1319
2
    spec = specifierForUID(instructionID);
1320
1321
    /* Borrow opcode from one of the other XCHGar opcodes */
1322
2
    insn->opcode = 0x91;
1323
1324
2
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1325
2
              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
2
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1335
1336
    /* Change back */
1337
2
    insn->opcode = 0x90;
1338
1339
2
    insn->instructionID = instructionIDWithNewOpcode;
1340
2
    insn->spec = specWithNewOpcode;
1341
1342
2
    return 0;
1343
2
  }
1344
1345
56.5k
  insn->instructionID = instructionID;
1346
56.5k
  insn->spec = specifierForUID(insn->instructionID);
1347
1348
56.5k
  return 0;
1349
56.5k
}
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
2.18k
{
1360
2.18k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1361
2.18k
  uint8_t index, base;
1362
1363
  // dbgprintf(insn, "readSIB()");
1364
1365
2.18k
  if (insn->consumedSIB)
1366
0
    return 0;
1367
1368
2.18k
  insn->consumedSIB = true;
1369
1370
2.18k
  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
912
  case 4:
1375
912
    insn->sibIndexBase = SIB_INDEX_EAX;
1376
912
    sibBaseBase = SIB_BASE_EAX;
1377
912
    break;
1378
1.26k
  case 8:
1379
1.26k
    insn->sibIndexBase = SIB_INDEX_RAX;
1380
1.26k
    sibBaseBase = SIB_BASE_RAX;
1381
1.26k
    break;
1382
2.18k
  }
1383
1384
2.18k
  if (consumeByte(insn, &insn->sib))
1385
6
    return -1;
1386
1387
2.17k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1388
1389
2.17k
  if (index == 0x4) {
1390
331
    insn->sibIndex = SIB_INDEX_NONE;
1391
1.84k
  } else {
1392
1.84k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1393
1.84k
  }
1394
1395
2.17k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1396
1397
2.17k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1398
1399
2.17k
  switch (base) {
1400
107
  case 0x5:
1401
129
  case 0xd:
1402
129
    switch (modFromModRM(insn->modRM)) {
1403
66
    case 0x0:
1404
66
      insn->eaDisplacement = EA_DISP_32;
1405
66
      insn->sibBase = SIB_BASE_NONE;
1406
66
      break;
1407
60
    case 0x1:
1408
60
      insn->eaDisplacement = EA_DISP_8;
1409
60
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1410
60
      break;
1411
3
    case 0x2:
1412
3
      insn->eaDisplacement = EA_DISP_32;
1413
3
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1414
3
      break;
1415
0
    case 0x3:
1416
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1417
0
      return -1;
1418
129
    }
1419
129
    break;
1420
2.04k
  default:
1421
2.04k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1422
2.04k
    break;
1423
2.17k
  }
1424
1425
2.17k
  return 0;
1426
2.17k
}
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
14.3k
{
1437
14.3k
  int8_t d8;
1438
14.3k
  int16_t d16;
1439
14.3k
  int32_t d32;
1440
1441
  // dbgprintf(insn, "readDisplacement()");
1442
1443
14.3k
  if (insn->consumedDisplacement)
1444
0
    return 0;
1445
1446
14.3k
  insn->consumedDisplacement = true;
1447
14.3k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1448
1449
14.3k
  switch (insn->eaDisplacement) {
1450
4.14k
  case EA_DISP_NONE:
1451
4.14k
    insn->consumedDisplacement = false;
1452
4.14k
    break;
1453
6.79k
  case EA_DISP_8:
1454
6.79k
    if (consumeInt8(insn, &d8))
1455
20
      return -1;
1456
6.77k
    insn->displacement = d8;
1457
6.77k
    break;
1458
1.41k
  case EA_DISP_16:
1459
1.41k
    if (consumeInt16(insn, &d16))
1460
2
      return -1;
1461
1.41k
    insn->displacement = d16;
1462
1.41k
    break;
1463
1.95k
  case EA_DISP_32:
1464
1.95k
    if (consumeInt32(insn, &d32))
1465
27
      return -1;
1466
1.93k
    insn->displacement = d32;
1467
1.93k
    break;
1468
14.3k
  }
1469
1470
14.2k
  return 0;
1471
14.3k
}
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
140k
{
1482
140k
  uint8_t mod, rm, reg, evexrm;
1483
1484
  // dbgprintf(insn, "readModRM()");
1485
1486
140k
  if (insn->consumedModRM)
1487
95.4k
    return 0;
1488
1489
44.7k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1490
1491
44.7k
  if (consumeByte(insn, &insn->modRM))
1492
157
    return -1;
1493
1494
44.6k
  insn->consumedModRM = true;
1495
1496
  // save original ModRM for later reference
1497
44.6k
  insn->orgModRM = insn->modRM;
1498
1499
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1500
44.6k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1501
4.55k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1502
12
    insn->modRM |= 0xC0;
1503
1504
44.6k
  mod = modFromModRM(insn->modRM);
1505
44.6k
  rm = rmFromModRM(insn->modRM);
1506
44.6k
  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
44.6k
  switch (insn->registerSize) {
1514
15.6k
  case 2:
1515
15.6k
    insn->regBase = MODRM_REG_AX;
1516
15.6k
    insn->eaRegBase = EA_REG_AX;
1517
15.6k
    break;
1518
24.9k
  case 4:
1519
24.9k
    insn->regBase = MODRM_REG_EAX;
1520
24.9k
    insn->eaRegBase = EA_REG_EAX;
1521
24.9k
    break;
1522
4.05k
  case 8:
1523
4.05k
    insn->regBase = MODRM_REG_RAX;
1524
4.05k
    insn->eaRegBase = EA_REG_RAX;
1525
4.05k
    break;
1526
44.6k
  }
1527
1528
44.6k
  reg |= rFromREX(insn->rexPrefix) << 3;
1529
44.6k
  rm |= bFromREX(insn->rexPrefix) << 3;
1530
1531
44.6k
  evexrm = 0;
1532
44.6k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1533
7.00k
      insn->mode == MODE_64BIT) {
1534
3.04k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1535
3.04k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1536
3.04k
  }
1537
1538
44.6k
  insn->reg = (Reg)(insn->regBase + reg);
1539
1540
44.6k
  switch (insn->addressSize) {
1541
13.6k
  case 2: {
1542
13.6k
    EABase eaBaseBase = EA_BASE_BX_SI;
1543
1544
13.6k
    switch (mod) {
1545
7.69k
    case 0x0:
1546
7.69k
      if (rm == 0x6) {
1547
198
        insn->eaBase = EA_BASE_NONE;
1548
198
        insn->eaDisplacement = EA_DISP_16;
1549
198
        if (readDisplacement(insn))
1550
0
          return -1;
1551
7.50k
      } else {
1552
7.50k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1553
7.50k
        insn->eaDisplacement = EA_DISP_NONE;
1554
7.50k
      }
1555
7.69k
      break;
1556
7.69k
    case 0x1:
1557
1.66k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1558
1.66k
      insn->eaDisplacement = EA_DISP_8;
1559
1.66k
      insn->displacementSize = 1;
1560
1.66k
      if (readDisplacement(insn))
1561
6
        return -1;
1562
1.66k
      break;
1563
1.66k
    case 0x2:
1564
1.21k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1565
1.21k
      insn->eaDisplacement = EA_DISP_16;
1566
1.21k
      if (readDisplacement(insn))
1567
2
        return -1;
1568
1.21k
      break;
1569
3.04k
    case 0x3:
1570
3.04k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1571
3.04k
      if (readDisplacement(insn))
1572
0
        return -1;
1573
3.04k
      break;
1574
13.6k
    }
1575
13.6k
    break;
1576
13.6k
  }
1577
1578
13.6k
  case 4:
1579
30.9k
  case 8: {
1580
30.9k
    EABase eaBaseBase =
1581
30.9k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1582
1583
30.9k
    switch (mod) {
1584
0
    default:
1585
0
      break;
1586
15.7k
    case 0x0:
1587
15.7k
      insn->eaDisplacement =
1588
15.7k
        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
15.7k
      switch (rm & 7) {
1593
1.16k
      case 0x4: // SIB byte is present
1594
1.16k
        insn->eaBase = (insn->addressSize == 4 ?
1595
542
              EA_BASE_sib :
1596
1.16k
              EA_BASE_sib64);
1597
1.16k
        if (readSIB(insn) || readDisplacement(insn))
1598
2
          return -1;
1599
1.16k
        break;
1600
1.16k
      case 0x5: // RIP-relative
1601
596
        insn->eaBase = EA_BASE_NONE;
1602
596
        insn->eaDisplacement = EA_DISP_32;
1603
596
        if (readDisplacement(insn))
1604
7
          return -1;
1605
589
        break;
1606
13.9k
      default:
1607
13.9k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1608
13.9k
        break;
1609
15.7k
      }
1610
15.7k
      break;
1611
15.7k
    case 0x1:
1612
5.13k
      insn->displacementSize = 1;
1613
      /* FALLTHROUGH */
1614
6.42k
    case 0x2:
1615
6.42k
      insn->eaDisplacement =
1616
6.42k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1617
6.42k
      switch (rm & 7) {
1618
1.01k
      case 0x4: // SIB byte is present
1619
1.01k
        insn->eaBase = EA_BASE_sib;
1620
1.01k
        if (readSIB(insn) || readDisplacement(insn))
1621
7
          return -1;
1622
1.00k
        break;
1623
5.41k
      default:
1624
5.41k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1625
5.41k
        if (readDisplacement(insn))
1626
31
          return -1;
1627
5.38k
        break;
1628
6.42k
      }
1629
6.39k
      break;
1630
8.82k
    case 0x3:
1631
8.82k
      insn->eaDisplacement = EA_DISP_NONE;
1632
8.82k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1633
8.82k
      break;
1634
30.9k
    }
1635
1636
30.9k
    break;
1637
30.9k
  }
1638
44.6k
  } /* switch (insn->addressSize) */
1639
1640
44.5k
  return 0;
1641
44.6k
}
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
53.3k
  { \
1647
53.3k
    *valid = 1; \
1648
53.3k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
13.6k
    case TYPE_Rv: \
1653
13.6k
      return base + index; \
1654
13.9k
    case TYPE_R8: \
1655
13.9k
      index &= mask; \
1656
13.9k
      if (index > 0xf) \
1657
13.9k
        *valid = 0; \
1658
13.9k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
347
        return prefix##_SPL + (index - 4); \
1660
13.6k
      } else { \
1661
13.6k
        return prefix##_AL + index; \
1662
13.6k
      } \
1663
13.9k
    case TYPE_R16: \
1664
62
      index &= mask; \
1665
62
      if (index > 0xf) \
1666
62
        *valid = 0; \
1667
62
      return prefix##_AX + index; \
1668
13.9k
    case TYPE_R32: \
1669
232
      index &= mask; \
1670
232
      if (index > 0xf) \
1671
232
        *valid = 0; \
1672
232
      return prefix##_EAX + index; \
1673
13.9k
    case TYPE_R64: \
1674
1.84k
      index &= mask; \
1675
1.84k
      if (index > 0xf) \
1676
1.84k
        *valid = 0; \
1677
1.84k
      return prefix##_RAX + index; \
1678
13.9k
    case TYPE_ZMM: \
1679
5.46k
      return prefix##_ZMM0 + index; \
1680
13.9k
    case TYPE_YMM: \
1681
3.10k
      return prefix##_YMM0 + index; \
1682
13.9k
    case TYPE_XMM: \
1683
10.7k
      return prefix##_XMM0 + index; \
1684
13.9k
    case TYPE_VK: \
1685
2.86k
      index &= 0xf; \
1686
2.86k
      if (index > 7) \
1687
2.86k
        *valid = 0; \
1688
2.86k
      return prefix##_K0 + index; \
1689
13.9k
    case TYPE_MM64: \
1690
334
      return prefix##_MM0 + (index & 0x7); \
1691
13.9k
    case TYPE_SEGMENTREG: \
1692
207
      if ((index & 7) > 5) \
1693
207
        *valid = 0; \
1694
207
      return prefix##_ES + (index & 7); \
1695
13.9k
    case TYPE_DEBUGREG: \
1696
12
      return prefix##_DR0 + index; \
1697
13.9k
    case TYPE_CONTROLREG: \
1698
0
      return prefix##_CR0 + index; \
1699
13.9k
    case TYPE_BNDR: \
1700
928
      if (index > 3) \
1701
928
        *valid = 0; \
1702
928
      return prefix##_BND0 + index; \
1703
13.9k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
13.9k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
13.9k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
53.3k
    } \
1710
53.3k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1646
41.9k
  { \
1647
41.9k
    *valid = 1; \
1648
41.9k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
10.0k
    case TYPE_Rv: \
1653
10.0k
      return base + index; \
1654
11.4k
    case TYPE_R8: \
1655
11.4k
      index &= mask; \
1656
11.4k
      if (index > 0xf) \
1657
11.4k
        *valid = 0; \
1658
11.4k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
206
        return prefix##_SPL + (index - 4); \
1660
11.2k
      } else { \
1661
11.2k
        return prefix##_AL + index; \
1662
11.2k
      } \
1663
11.4k
    case TYPE_R16: \
1664
53
      index &= mask; \
1665
53
      if (index > 0xf) \
1666
53
        *valid = 0; \
1667
53
      return prefix##_AX + index; \
1668
11.4k
    case TYPE_R32: \
1669
128
      index &= mask; \
1670
128
      if (index > 0xf) \
1671
128
        *valid = 0; \
1672
128
      return prefix##_EAX + index; \
1673
11.4k
    case TYPE_R64: \
1674
1.26k
      index &= mask; \
1675
1.26k
      if (index > 0xf) \
1676
1.26k
        *valid = 0; \
1677
1.26k
      return prefix##_RAX + index; \
1678
11.4k
    case TYPE_ZMM: \
1679
4.04k
      return prefix##_ZMM0 + index; \
1680
11.4k
    case TYPE_YMM: \
1681
2.49k
      return prefix##_YMM0 + index; \
1682
11.4k
    case TYPE_XMM: \
1683
8.47k
      return prefix##_XMM0 + index; \
1684
11.4k
    case TYPE_VK: \
1685
2.73k
      index &= 0xf; \
1686
2.73k
      if (index > 7) \
1687
2.73k
        *valid = 0; \
1688
2.73k
      return prefix##_K0 + index; \
1689
11.4k
    case TYPE_MM64: \
1690
240
      return prefix##_MM0 + (index & 0x7); \
1691
11.4k
    case TYPE_SEGMENTREG: \
1692
207
      if ((index & 7) > 5) \
1693
207
        *valid = 0; \
1694
207
      return prefix##_ES + (index & 7); \
1695
11.4k
    case TYPE_DEBUGREG: \
1696
12
      return prefix##_DR0 + index; \
1697
11.4k
    case TYPE_CONTROLREG: \
1698
0
      return prefix##_CR0 + index; \
1699
11.4k
    case TYPE_BNDR: \
1700
720
      if (index > 3) \
1701
720
        *valid = 0; \
1702
720
      return prefix##_BND0 + index; \
1703
11.4k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
11.4k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
11.4k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
41.9k
    } \
1710
41.9k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1646
11.4k
  { \
1647
11.4k
    *valid = 1; \
1648
11.4k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
3.55k
    case TYPE_Rv: \
1653
3.55k
      return base + index; \
1654
2.47k
    case TYPE_R8: \
1655
2.47k
      index &= mask; \
1656
2.47k
      if (index > 0xf) \
1657
2.47k
        *valid = 0; \
1658
2.47k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
141
        return prefix##_SPL + (index - 4); \
1660
2.33k
      } else { \
1661
2.33k
        return prefix##_AL + index; \
1662
2.33k
      } \
1663
2.47k
    case TYPE_R16: \
1664
9
      index &= mask; \
1665
9
      if (index > 0xf) \
1666
9
        *valid = 0; \
1667
9
      return prefix##_AX + index; \
1668
2.47k
    case TYPE_R32: \
1669
104
      index &= mask; \
1670
104
      if (index > 0xf) \
1671
104
        *valid = 0; \
1672
104
      return prefix##_EAX + index; \
1673
2.47k
    case TYPE_R64: \
1674
584
      index &= mask; \
1675
584
      if (index > 0xf) \
1676
584
        *valid = 0; \
1677
584
      return prefix##_RAX + index; \
1678
2.47k
    case TYPE_ZMM: \
1679
1.42k
      return prefix##_ZMM0 + index; \
1680
2.47k
    case TYPE_YMM: \
1681
617
      return prefix##_YMM0 + index; \
1682
2.47k
    case TYPE_XMM: \
1683
2.28k
      return prefix##_XMM0 + index; \
1684
2.47k
    case TYPE_VK: \
1685
128
      index &= 0xf; \
1686
128
      if (index > 7) \
1687
128
        *valid = 0; \
1688
128
      return prefix##_K0 + index; \
1689
2.47k
    case TYPE_MM64: \
1690
94
      return prefix##_MM0 + (index & 0x7); \
1691
2.47k
    case TYPE_SEGMENTREG: \
1692
0
      if ((index & 7) > 5) \
1693
0
        *valid = 0; \
1694
0
      return prefix##_ES + (index & 7); \
1695
2.47k
    case TYPE_DEBUGREG: \
1696
0
      return prefix##_DR0 + index; \
1697
2.47k
    case TYPE_CONTROLREG: \
1698
0
      return prefix##_CR0 + index; \
1699
2.47k
    case TYPE_BNDR: \
1700
208
      if (index > 3) \
1701
208
        *valid = 0; \
1702
208
      return prefix##_BND0 + index; \
1703
2.47k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
2.47k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
2.47k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
11.4k
    } \
1710
11.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
85.5k
{
1740
85.5k
  uint8_t valid;
1741
1742
85.5k
  switch ((OperandEncoding)op->encoding) {
1743
0
  default:
1744
    // debug("Expected a REG or R/M encoding in fixupReg");
1745
0
    return -1;
1746
6.80k
  case ENCODING_VVVV:
1747
6.80k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1748
6.80k
            insn->vvvv, &valid);
1749
6.80k
    if (!valid)
1750
2
      return -1;
1751
6.80k
    break;
1752
35.1k
  case ENCODING_REG:
1753
35.1k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1754
35.1k
                 insn->reg - insn->regBase,
1755
35.1k
                 &valid);
1756
35.1k
    if (!valid)
1757
2
      return -1;
1758
35.1k
    break;
1759
280k
CASE_ENCODING_RM:
1760
280k
    if (insn->eaBase >= insn->eaRegBase) {
1761
11.4k
      insn->eaBase = (EABase)fixupRMValue(
1762
11.4k
        insn, (OperandType)op->type,
1763
11.4k
        insn->eaBase - insn->eaRegBase, &valid);
1764
11.4k
      if (!valid)
1765
0
        return -1;
1766
11.4k
    }
1767
43.6k
    break;
1768
85.5k
  }
1769
1770
85.5k
  return 0;
1771
85.5k
}
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
8.32k
{
1786
8.32k
  if (size == 0)
1787
5.67k
    size = insn->registerSize;
1788
1789
8.32k
  switch (size) {
1790
948
  case 1:
1791
948
    insn->opcodeRegister =
1792
948
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1793
948
                (insn->opcode & 7)));
1794
948
    if (insn->rexPrefix &&
1795
51
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1796
42
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1797
36
      insn->opcodeRegister =
1798
36
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1799
36
                   MODRM_REG_AL - 4));
1800
36
    }
1801
1802
948
    break;
1803
2.19k
  case 2:
1804
2.19k
    insn->opcodeRegister =
1805
2.19k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1806
2.19k
                (insn->opcode & 7)));
1807
2.19k
    break;
1808
3.47k
  case 4:
1809
3.47k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1810
3.47k
               ((bFromREX(insn->rexPrefix) << 3) |
1811
3.47k
                (insn->opcode & 7)));
1812
3.47k
    break;
1813
1.71k
  case 8:
1814
1.71k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1815
1.71k
               ((bFromREX(insn->rexPrefix) << 3) |
1816
1.71k
                (insn->opcode & 7)));
1817
1.71k
    break;
1818
8.32k
  }
1819
1820
8.32k
  return 0;
1821
8.32k
}
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
24.0k
{
1834
24.0k
  uint8_t imm8;
1835
24.0k
  uint16_t imm16;
1836
24.0k
  uint32_t imm32;
1837
24.0k
  uint64_t imm64;
1838
1839
24.0k
  if (insn->numImmediatesConsumed == 2) {
1840
    // debug("Already consumed two immediates");
1841
0
    return -1;
1842
0
  }
1843
1844
24.0k
  if (size == 0)
1845
0
    size = insn->immediateSize;
1846
24.0k
  else
1847
24.0k
    insn->immediateSize = size;
1848
1849
24.0k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1850
1851
24.0k
  switch (size) {
1852
19.1k
  case 1:
1853
19.1k
    if (consumeByte(insn, &imm8))
1854
64
      return -1;
1855
1856
19.0k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
1857
19.0k
    break;
1858
2.80k
  case 2:
1859
2.80k
    if (consumeUInt16(insn, &imm16))
1860
18
      return -1;
1861
1862
2.78k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
1863
2.78k
    break;
1864
1.70k
  case 4:
1865
1.70k
    if (consumeUInt32(insn, &imm32))
1866
31
      return -1;
1867
1868
1.66k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
1869
1.66k
    break;
1870
392
  case 8:
1871
392
    if (consumeUInt64(insn, &imm64))
1872
12
      return -1;
1873
380
    insn->immediates[insn->numImmediatesConsumed] = imm64;
1874
380
    break;
1875
24.0k
  }
1876
1877
23.8k
  insn->numImmediatesConsumed++;
1878
1879
23.8k
  return 0;
1880
24.0k
}
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
81.9k
{
1891
81.9k
  int vvvv;
1892
1893
81.9k
  if (insn->vectorExtensionType == TYPE_EVEX)
1894
7.00k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1895
7.00k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
1896
74.9k
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
1897
809
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
1898
74.1k
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
1899
537
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
1900
73.6k
  else if (insn->vectorExtensionType == TYPE_XOP)
1901
896
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
1902
72.7k
  else
1903
72.7k
    return -1;
1904
1905
9.24k
  if (insn->mode != MODE_64BIT)
1906
5.35k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1907
1908
9.24k
  insn->vvvv = (Reg)vvvv;
1909
1910
9.24k
  return 0;
1911
81.9k
}
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
5.15k
{
1922
5.15k
  if (insn->vectorExtensionType != TYPE_EVEX)
1923
0
    return -1;
1924
1925
5.15k
  insn->writemask =
1926
5.15k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
1927
1928
5.15k
  return 0;
1929
5.15k
}
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
81.9k
{
1940
81.9k
  int hasVVVV, needVVVV;
1941
81.9k
  int sawRegImm = 0;
1942
81.9k
  int i;
1943
1944
  /* If non-zero vvvv specified, need to make sure one of the operands
1945
     uses it. */
1946
81.9k
  hasVVVV = !readVVVV(insn);
1947
81.9k
  needVVVV = hasVVVV && (insn->vvvv != 0);
1948
1949
573k
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
1950
491k
    const OperandSpecifier *op =
1951
491k
      &x86OperandSets[insn->spec->operands][i];
1952
491k
    switch (op->encoding) {
1953
338k
    case ENCODING_NONE:
1954
343k
    case ENCODING_SI:
1955
348k
    case ENCODING_DI:
1956
348k
      break;
1957
1958
2.82k
CASE_ENCODING_VSIB:
1959
      // VSIB can use the V2 bit so check only the other bits.
1960
2.82k
      if (needVVVV)
1961
316
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
1962
1963
2.82k
      if (readModRM(insn))
1964
0
        return -1;
1965
1966
      // Reject if SIB wasn't used.
1967
492
      if (insn->eaBase != EA_BASE_sib &&
1968
143
          insn->eaBase != EA_BASE_sib64)
1969
1
        return -1;
1970
1971
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1972
491
      if (insn->sibIndex == SIB_INDEX_NONE)
1973
23
        insn->sibIndex =
1974
23
          (SIBIndex)(insn->sibIndexBase + 4);
1975
1976
      // If EVEX.v2 is set this is one of the 16-31 registers.
1977
491
      if (insn->vectorExtensionType == TYPE_EVEX &&
1978
310
          insn->mode == MODE_64BIT &&
1979
136
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1980
136
        insn->sibIndex =
1981
136
          (SIBIndex)(insn->sibIndex + 16);
1982
1983
      // Adjust the index register to the correct size.
1984
491
      switch (op->type) {
1985
0
      default:
1986
        // debug("Unhandled VSIB index type");
1987
0
        return -1;
1988
312
      case TYPE_MVSIBX:
1989
312
        insn->sibIndex =
1990
312
          (SIBIndex)(SIB_INDEX_XMM0 +
1991
312
               (insn->sibIndex -
1992
312
                insn->sibIndexBase));
1993
312
        break;
1994
17
      case TYPE_MVSIBY:
1995
17
        insn->sibIndex =
1996
17
          (SIBIndex)(SIB_INDEX_YMM0 +
1997
17
               (insn->sibIndex -
1998
17
                insn->sibIndexBase));
1999
17
        break;
2000
162
      case TYPE_MVSIBZ:
2001
162
        insn->sibIndex =
2002
162
          (SIBIndex)(SIB_INDEX_ZMM0 +
2003
162
               (insn->sibIndex -
2004
162
                insn->sibIndexBase));
2005
162
        break;
2006
491
      }
2007
2008
      // Apply the AVX512 compressed displacement scaling factor.
2009
491
      if (op->encoding != ENCODING_REG &&
2010
491
          insn->eaDisplacement == EA_DISP_8)
2011
164
        insn->displacement *=
2012
164
          1 << (op->encoding - ENCODING_VSIB);
2013
491
      break;
2014
2015
35.1k
    case ENCODING_REG:
2016
526k
CASE_ENCODING_RM:
2017
526k
      if (readModRM(insn))
2018
0
        return -1;
2019
2020
78.7k
      if (fixupReg(insn, op))
2021
2
        return -1;
2022
2023
      // Apply the AVX512 compressed displacement scaling factor.
2024
78.7k
      if (op->encoding != ENCODING_REG &&
2025
43.6k
          insn->eaDisplacement == EA_DISP_8)
2026
6.60k
        insn->displacement *=
2027
6.60k
          1 << (op->encoding - ENCODING_RM);
2028
78.7k
      break;
2029
2030
19.3k
    case ENCODING_IB:
2031
19.3k
      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
196
        insn->immediates[insn->numImmediatesConsumed] =
2035
196
          insn->immediates
2036
196
            [insn->numImmediatesConsumed -
2037
196
             1] &
2038
196
          0xf;
2039
196
        ++insn->numImmediatesConsumed;
2040
196
        break;
2041
196
      }
2042
19.1k
      if (readImmediate(insn, 1))
2043
64
        return -1;
2044
19.0k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2045
224
        sawRegImm = 1;
2046
19.0k
      break;
2047
2048
1.04k
    case ENCODING_IW:
2049
1.04k
      if (readImmediate(insn, 2))
2050
2
        return -1;
2051
1.04k
      break;
2052
2053
1.04k
    case ENCODING_ID:
2054
297
      if (readImmediate(insn, 4))
2055
8
        return -1;
2056
289
      break;
2057
2058
289
    case ENCODING_IO:
2059
158
      if (readImmediate(insn, 8))
2060
3
        return -1;
2061
155
      break;
2062
2063
2.54k
    case ENCODING_Iv:
2064
2.54k
      if (readImmediate(insn, insn->immediateSize))
2065
33
        return -1;
2066
2.50k
      break;
2067
2068
2.50k
    case ENCODING_Ia:
2069
856
      if (readImmediate(insn, insn->addressSize))
2070
15
        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
841
      insn->displacementOffset = insn->immediateOffset;
2075
841
      insn->consumedDisplacement = true;
2076
841
      insn->displacementSize = insn->immediateSize;
2077
841
      insn->displacement =
2078
841
        insn->immediates[insn->numImmediatesConsumed -
2079
841
             1];
2080
841
      insn->immediateOffset = 0;
2081
841
      insn->immediateSize = 0;
2082
841
      break;
2083
2084
474
    case ENCODING_IRC:
2085
474
      insn->RC =
2086
474
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2087
474
         << 1) |
2088
474
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2089
474
      break;
2090
2091
948
    case ENCODING_RB:
2092
948
      if (readOpcodeRegister(insn, 1))
2093
0
        return -1;
2094
948
      break;
2095
2096
948
    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
1.70k
    case ENCODING_RO:
2107
1.70k
      if (readOpcodeRegister(insn, 8))
2108
0
        return -1;
2109
1.70k
      break;
2110
2111
5.67k
    case ENCODING_Rv:
2112
5.67k
      if (readOpcodeRegister(insn, 0))
2113
0
        return -1;
2114
5.67k
      break;
2115
2116
5.67k
    case ENCODING_FP:
2117
296
      break;
2118
2119
6.80k
    case ENCODING_VVVV:
2120
6.80k
      if (!hasVVVV)
2121
0
        return -1;
2122
2123
6.80k
      needVVVV =
2124
6.80k
        0; /* Mark that we have found a VVVV operand. */
2125
2126
6.80k
      if (insn->mode != MODE_64BIT)
2127
3.83k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2128
2129
6.80k
      if (fixupReg(insn, op))
2130
2
        return -1;
2131
6.80k
      break;
2132
2133
6.80k
    case ENCODING_WRITEMASK:
2134
5.15k
      if (readMaskRegister(insn))
2135
0
        return -1;
2136
5.15k
      break;
2137
2138
18.5k
    case ENCODING_DUP:
2139
18.5k
      break;
2140
2141
0
    default:
2142
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2143
0
      return -1;
2144
491k
    }
2145
491k
  }
2146
2147
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2148
81.8k
  if (needVVVV)
2149
0
    return -1;
2150
2151
81.8k
  return 0;
2152
81.8k
}
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
81.9k
{
2158
  // LOCK prefix
2159
81.9k
  if (insn->hasLockPrefix) {
2160
3.43k
    switch (insn->instructionID) {
2161
13
    default:
2162
      // invalid LOCK
2163
13
      return true;
2164
2165
    // nop dword [rax]
2166
35
    case X86_NOOPL:
2167
2168
    // DEC
2169
47
    case X86_DEC16m:
2170
79
    case X86_DEC32m:
2171
80
    case X86_DEC64m:
2172
89
    case X86_DEC8m:
2173
2174
    // ADC
2175
91
    case X86_ADC16mi:
2176
93
    case X86_ADC16mi8:
2177
93
    case X86_ADC16mr:
2178
109
    case X86_ADC32mi:
2179
117
    case X86_ADC32mi8:
2180
136
    case X86_ADC32mr:
2181
181
    case X86_ADC64mi32:
2182
217
    case X86_ADC64mi8:
2183
346
    case X86_ADC64mr:
2184
346
    case X86_ADC8mi:
2185
354
    case X86_ADC8mi8:
2186
483
    case X86_ADC8mr:
2187
501
    case X86_ADC8rm:
2188
502
    case X86_ADC16rm:
2189
514
    case X86_ADC32rm:
2190
519
    case X86_ADC64rm:
2191
2192
    // ADD
2193
521
    case X86_ADD16mi:
2194
554
    case X86_ADD16mi8:
2195
693
    case X86_ADD16mr:
2196
693
    case X86_ADD32mi:
2197
719
    case X86_ADD32mi8:
2198
959
    case X86_ADD32mr:
2199
977
    case X86_ADD64mi32:
2200
987
    case X86_ADD64mi8:
2201
1.00k
    case X86_ADD64mr:
2202
1.01k
    case X86_ADD8mi:
2203
1.01k
    case X86_ADD8mi8:
2204
1.03k
    case X86_ADD8mr:
2205
1.04k
    case X86_ADD8rm:
2206
1.04k
    case X86_ADD16rm:
2207
1.06k
    case X86_ADD32rm:
2208
1.22k
    case X86_ADD64rm:
2209
2210
    // AND
2211
1.22k
    case X86_AND16mi:
2212
1.28k
    case X86_AND16mi8:
2213
1.30k
    case X86_AND16mr:
2214
1.30k
    case X86_AND32mi:
2215
1.30k
    case X86_AND32mi8:
2216
1.31k
    case X86_AND32mr:
2217
1.31k
    case X86_AND64mi32:
2218
1.31k
    case X86_AND64mi8:
2219
1.33k
    case X86_AND64mr:
2220
1.46k
    case X86_AND8mi:
2221
1.61k
    case X86_AND8mi8:
2222
1.65k
    case X86_AND8mr:
2223
1.66k
    case X86_AND8rm:
2224
1.66k
    case X86_AND16rm:
2225
1.66k
    case X86_AND32rm:
2226
1.66k
    case X86_AND64rm:
2227
2228
    // BTC
2229
1.66k
    case X86_BTC16mi8:
2230
1.66k
    case X86_BTC16mr:
2231
1.66k
    case X86_BTC32mi8:
2232
1.66k
    case X86_BTC32mr:
2233
1.66k
    case X86_BTC64mi8:
2234
1.66k
    case X86_BTC64mr:
2235
2236
    // BTR
2237
1.66k
    case X86_BTR16mi8:
2238
1.67k
    case X86_BTR16mr:
2239
1.67k
    case X86_BTR32mi8:
2240
1.67k
    case X86_BTR32mr:
2241
1.72k
    case X86_BTR64mi8:
2242
1.74k
    case X86_BTR64mr:
2243
2244
    // BTS
2245
1.74k
    case X86_BTS16mi8:
2246
1.88k
    case X86_BTS16mr:
2247
1.90k
    case X86_BTS32mi8:
2248
1.91k
    case X86_BTS32mr:
2249
1.92k
    case X86_BTS64mi8:
2250
1.98k
    case X86_BTS64mr:
2251
2252
    // CMPXCHG
2253
1.98k
    case X86_CMPXCHG16B:
2254
1.98k
    case X86_CMPXCHG16rm:
2255
2.00k
    case X86_CMPXCHG32rm:
2256
2.00k
    case X86_CMPXCHG64rm:
2257
2.00k
    case X86_CMPXCHG8rm:
2258
2.00k
    case X86_CMPXCHG8B:
2259
2260
    // INC
2261
2.04k
    case X86_INC16m:
2262
2.07k
    case X86_INC32m:
2263
2.07k
    case X86_INC64m:
2264
2.08k
    case X86_INC8m:
2265
2266
    // NEG
2267
2.08k
    case X86_NEG16m:
2268
2.08k
    case X86_NEG32m:
2269
2.08k
    case X86_NEG64m:
2270
2.08k
    case X86_NEG8m:
2271
2272
    // NOT
2273
2.08k
    case X86_NOT16m:
2274
2.09k
    case X86_NOT32m:
2275
2.10k
    case X86_NOT64m:
2276
2.10k
    case X86_NOT8m:
2277
2278
    // OR
2279
2.21k
    case X86_OR16mi:
2280
2.22k
    case X86_OR16mi8:
2281
2.22k
    case X86_OR16mr:
2282
2.22k
    case X86_OR32mi:
2283
2.22k
    case X86_OR32mi8:
2284
2.25k
    case X86_OR32mr:
2285
2.25k
    case X86_OR64mi32:
2286
2.27k
    case X86_OR64mi8:
2287
2.30k
    case X86_OR64mr:
2288
2.32k
    case X86_OR8mi8:
2289
2.33k
    case X86_OR8mi:
2290
2.35k
    case X86_OR8mr:
2291
2.37k
    case X86_OR8rm:
2292
2.37k
    case X86_OR16rm:
2293
2.39k
    case X86_OR32rm:
2294
2.39k
    case X86_OR64rm:
2295
2296
    // SBB
2297
2.52k
    case X86_SBB16mi:
2298
2.53k
    case X86_SBB16mi8:
2299
2.53k
    case X86_SBB16mr:
2300
2.53k
    case X86_SBB32mi:
2301
2.53k
    case X86_SBB32mi8:
2302
2.53k
    case X86_SBB32mr:
2303
2.53k
    case X86_SBB64mi32:
2304
2.54k
    case X86_SBB64mi8:
2305
2.54k
    case X86_SBB64mr:
2306
2.54k
    case X86_SBB8mi:
2307
2.56k
    case X86_SBB8mi8:
2308
2.56k
    case X86_SBB8mr:
2309
2310
    // SUB
2311
2.56k
    case X86_SUB16mi:
2312
2.56k
    case X86_SUB16mi8:
2313
2.56k
    case X86_SUB16mr:
2314
2.56k
    case X86_SUB32mi:
2315
2.56k
    case X86_SUB32mi8:
2316
2.57k
    case X86_SUB32mr:
2317
2.57k
    case X86_SUB64mi32:
2318
2.57k
    case X86_SUB64mi8:
2319
2.58k
    case X86_SUB64mr:
2320
2.58k
    case X86_SUB8mi8:
2321
2.58k
    case X86_SUB8mi:
2322
2.62k
    case X86_SUB8mr:
2323
2.64k
    case X86_SUB8rm:
2324
2.78k
    case X86_SUB16rm:
2325
2.85k
    case X86_SUB32rm:
2326
2.89k
    case X86_SUB64rm:
2327
2328
    // XADD
2329
3.02k
    case X86_XADD16rm:
2330
3.06k
    case X86_XADD32rm:
2331
3.07k
    case X86_XADD64rm:
2332
3.09k
    case X86_XADD8rm:
2333
2334
    // XCHG
2335
3.09k
    case X86_XCHG16rm:
2336
3.10k
    case X86_XCHG32rm:
2337
3.23k
    case X86_XCHG64rm:
2338
3.23k
    case X86_XCHG8rm:
2339
2340
    // XOR
2341
3.23k
    case X86_XOR16mi:
2342
3.27k
    case X86_XOR16mi8:
2343
3.29k
    case X86_XOR16mr:
2344
3.29k
    case X86_XOR32mi:
2345
3.29k
    case X86_XOR32mi8:
2346
3.29k
    case X86_XOR32mr:
2347
3.30k
    case X86_XOR64mi32:
2348
3.31k
    case X86_XOR64mi8:
2349
3.32k
    case X86_XOR64mr:
2350
3.32k
    case X86_XOR8mi8:
2351
3.32k
    case X86_XOR8mi:
2352
3.32k
    case X86_XOR8mr:
2353
3.38k
    case X86_XOR8rm:
2354
3.40k
    case X86_XOR16rm:
2355
3.40k
    case X86_XOR32rm:
2356
3.42k
    case X86_XOR64rm:
2357
2358
      // this instruction can be used with LOCK prefix
2359
3.42k
      return false;
2360
3.43k
    }
2361
3.43k
  }
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
78.5k
  return false;
2376
81.9k
}
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
82.4k
{
2397
82.4k
  insn->reader = reader;
2398
82.4k
  insn->readerArg = readerArg;
2399
82.4k
  insn->startLocation = startLoc;
2400
82.4k
  insn->readerCursor = startLoc;
2401
82.4k
  insn->mode = mode;
2402
82.4k
  insn->numImmediatesConsumed = 0;
2403
2404
82.4k
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2405
82.0k
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2406
548
    return -1;
2407
2408
81.8k
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2409
2410
  // instruction length must be <= 15 to be valid
2411
81.8k
  if (insn->length > 15)
2412
2
    return -1;
2413
2414
81.8k
  if (insn->operandSize == 0)
2415
81.8k
    insn->operandSize = insn->registerSize;
2416
2417
81.8k
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2418
2419
81.8k
  return 0;
2420
81.8k
}
2421
2422
#endif