Coverage Report

Created: 2025-11-11 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/X86/X86DisassemblerDecoder.c
Line
Count
Source
1
/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
2
 *
3
 *                     The LLVM Compiler Infrastructure
4
 *
5
 * This file is distributed under the University of Illinois Open Source
6
 * License. See LICENSE.TXT for details.
7
 *
8
 *===----------------------------------------------------------------------===*
9
 *
10
 * This file is part of the X86 Disassembler.
11
 * It contains the implementation of the instruction decoder.
12
 * Documentation for the disassembler can be found in X86Disassembler.h.
13
 *
14
 *===----------------------------------------------------------------------===*/
15
16
/* Capstone Disassembly Engine */
17
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
18
19
#ifdef CAPSTONE_HAS_X86
20
21
#include <stdarg.h> /* for va_*()       */
22
#if defined(CAPSTONE_HAS_OSXKERNEL)
23
#include <libkern/libkern.h>
24
#else
25
#include <stdlib.h> /* for exit()       */
26
#endif
27
28
#include <string.h>
29
30
#include "../../cs_priv.h"
31
#include "../../utils.h"
32
33
#include "X86DisassemblerDecoder.h"
34
#include "X86Mapping.h"
35
36
/// Specifies whether a ModR/M byte is needed and (if so) which
37
/// instruction each possible value of the ModR/M byte corresponds to.  Once
38
/// this information is known, we have narrowed down to a single instruction.
39
struct ModRMDecision {
40
  uint8_t modrm_type;
41
  uint16_t instructionIDs;
42
};
43
44
/// Specifies which set of ModR/M->instruction tables to look at
45
/// given a particular opcode.
46
struct OpcodeDecision {
47
  struct ModRMDecision modRMDecisions[256];
48
};
49
50
/// Specifies which opcode->instruction tables to look at given
51
/// a particular context (set of attributes).  Since there are many possible
52
/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
53
/// applies given a specific set of attributes.  Hence there are only IC_max
54
/// entries in this table, rather than 2^(ATTR_max).
55
struct ContextDecision {
56
  struct OpcodeDecision opcodeDecisions[IC_max];
57
};
58
59
#ifdef CAPSTONE_X86_REDUCE
60
#include "X86GenDisassemblerTables_reduce.inc"
61
#include "X86GenDisassemblerTables_reduce2.inc"
62
#include "X86Lookup16_reduce.inc"
63
#else
64
#include "X86GenDisassemblerTables.inc"
65
#include "X86GenDisassemblerTables2.inc"
66
#include "X86Lookup16.inc"
67
#endif
68
69
/*
70
 * contextForAttrs - Client for the instruction context table.  Takes a set of
71
 *   attributes and returns the appropriate decode context.
72
 *
73
 * @param attrMask  - Attributes, from the enumeration attributeBits.
74
 * @return          - The InstructionContext to use when looking up an
75
 *                    an instruction with these attributes.
76
 */
77
static InstructionContext contextForAttrs(uint16_t attrMask)
78
1.86M
{
79
1.86M
  return CONTEXTS_SYM[attrMask];
80
1.86M
}
81
82
/*
83
 * modRMRequired - Reads the appropriate instruction table to determine whether
84
 *   the ModR/M byte is required to decode a particular instruction.
85
 *
86
 * @param type        - The opcode type (i.e., how many bytes it has).
87
 * @param insnContext - The context for the instruction, as returned by
88
 *                      contextForAttrs.
89
 * @param opcode      - The last byte of the instruction's opcode, not counting
90
 *                      ModR/M extensions and escapes.
91
 * @return            - true if the ModR/M byte is required, false otherwise.
92
 */
93
static int modRMRequired(OpcodeType type, InstructionContext insnContext,
94
       uint16_t opcode)
95
1.86M
{
96
1.86M
  const struct OpcodeDecision *decision = NULL;
97
1.86M
  const uint8_t *indextable = NULL;
98
1.86M
  unsigned int index;
99
100
1.86M
  switch (type) {
101
0
  default:
102
0
    break;
103
1.52M
  case ONEBYTE:
104
1.52M
    decision = ONEBYTE_SYM;
105
1.52M
    indextable = index_x86DisassemblerOneByteOpcodes;
106
1.52M
    break;
107
171k
  case TWOBYTE:
108
171k
    decision = TWOBYTE_SYM;
109
171k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
171k
    break;
111
57.6k
  case THREEBYTE_38:
112
57.6k
    decision = THREEBYTE38_SYM;
113
57.6k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
57.6k
    break;
115
81.1k
  case THREEBYTE_3A:
116
81.1k
    decision = THREEBYTE3A_SYM;
117
81.1k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
81.1k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
22.9k
  case XOP8_MAP:
121
22.9k
    decision = XOP8_MAP_SYM;
122
22.9k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
22.9k
    break;
124
1.60k
  case XOP9_MAP:
125
1.60k
    decision = XOP9_MAP_SYM;
126
1.60k
    indextable = index_x86DisassemblerXOP9Opcodes;
127
1.60k
    break;
128
1.51k
  case XOPA_MAP:
129
1.51k
    decision = XOPA_MAP_SYM;
130
1.51k
    indextable = index_x86DisassemblerXOPAOpcodes;
131
1.51k
    break;
132
2.63k
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
2.63k
    return true;
135
1.86M
#endif
136
1.86M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.85M
  index = indextable[insnContext];
140
1.85M
  if (index)
141
1.84M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.84M
           MODRM_ONEENTRY;
143
11.2k
  else
144
11.2k
    return false;
145
1.85M
}
146
147
/*
148
 * decode - Reads the appropriate instruction table to obtain the unique ID of
149
 *   an instruction.
150
 *
151
 * @param type        - See modRMRequired().
152
 * @param insnContext - See modRMRequired().
153
 * @param opcode      - See modRMRequired().
154
 * @param modRM       - The ModR/M byte if required, or any value if not.
155
 * @return            - The UID of the instruction, or 0 on failure.
156
 */
157
static InstrUID decode(OpcodeType type, InstructionContext insnContext,
158
           uint8_t opcode, uint8_t modRM)
159
1.85M
{
160
1.85M
  const struct ModRMDecision *dec = NULL;
161
1.85M
  unsigned int index;
162
1.85M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.85M
  switch (type) {
165
0
  default:
166
0
    break; // never reach
167
1.51M
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
1.51M
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
1.51M
    if (index)
171
1.51M
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
1.02k
    else
173
1.02k
      dec = &emptyDecision.modRMDecisions[opcode];
174
1.51M
    break;
175
171k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
171k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
171k
    if (index)
179
168k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
2.75k
    else
181
2.75k
      dec = &emptyDecision.modRMDecisions[opcode];
182
171k
    break;
183
57.6k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
57.6k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
57.6k
    if (index)
187
56.4k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
1.23k
    else
189
1.23k
      dec = &emptyDecision.modRMDecisions[opcode];
190
57.6k
    break;
191
81.1k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
81.1k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
81.1k
    if (index)
195
80.7k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
374
    else
197
374
      dec = &emptyDecision.modRMDecisions[opcode];
198
81.1k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
22.9k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
22.9k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
22.9k
    if (index)
204
17.8k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
5.09k
    else
206
5.09k
      dec = &emptyDecision.modRMDecisions[opcode];
207
22.9k
    break;
208
1.60k
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
1.60k
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
1.60k
    if (index)
212
1.22k
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
375
    else
214
375
      dec = &emptyDecision.modRMDecisions[opcode];
215
1.60k
    break;
216
1.51k
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
1.51k
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
1.51k
    if (index)
220
1.12k
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
387
    else
222
387
      dec = &emptyDecision.modRMDecisions[opcode];
223
1.51k
    break;
224
2.63k
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
2.63k
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
2.63k
    if (index)
228
1.93k
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
1.93k
               .modRMDecisions[opcode];
230
696
    else
231
696
      dec = &emptyDecision.modRMDecisions[opcode];
232
2.63k
    break;
233
1.85M
#endif
234
1.85M
  }
235
236
1.85M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
874k
  case MODRM_ONEENTRY:
241
874k
    return modRMTable[dec->instructionIDs];
242
742k
  case MODRM_SPLITRM:
243
742k
    if (modFromModRM(modRM) == 0x3)
244
171k
      return modRMTable[dec->instructionIDs + 1];
245
570k
    return modRMTable[dec->instructionIDs];
246
203k
  case MODRM_SPLITREG:
247
203k
    if (modFromModRM(modRM) == 0x3)
248
68.2k
      return modRMTable[dec->instructionIDs +
249
68.2k
            ((modRM & 0x38) >> 3) + 8];
250
135k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
36.9k
  case MODRM_SPLITMISC:
252
36.9k
    if (modFromModRM(modRM) == 0x3)
253
8.98k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
8.98k
            8];
255
27.9k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.85M
  }
259
1.85M
}
260
261
/*
262
 * specifierForUID - Given a UID, returns the name and operand specification for
263
 *   that instruction.
264
 *
265
 * @param uid - The unique ID for the instruction.  This should be returned by
266
 *              decode(); specifierForUID will not check bounds.
267
 * @return    - A pointer to the specification for that instruction.
268
 */
269
static const struct InstructionSpecifier *specifierForUID(InstrUID uid)
270
1.54M
{
271
1.54M
  return &INSTRUCTIONS_SYM[uid];
272
1.54M
}
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
5.14M
{
286
5.14M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
5.14M
  if (!ret)
289
5.14M
    ++(insn->readerCursor);
290
291
5.14M
  return ret;
292
5.14M
}
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
608k
{
303
608k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
608k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
1.75M
{
308
1.75M
  insn->readerCursor--;
309
1.75M
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
269k
  { \
314
269k
    type combined = 0; \
315
269k
    unsigned offset; \
316
866k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
598k
      uint8_t byte; \
318
598k
      int ret = insn->reader(insn->readerArg, &byte, \
319
598k
                 insn->readerCursor + offset); \
320
598k
      if (ret) \
321
598k
        return ret; \
322
598k
      combined = combined | \
323
597k
           ((uint64_t)byte << (offset * 8)); \
324
597k
    } \
325
269k
    *ptr = combined; \
326
267k
    insn->readerCursor += sizeof(type); \
327
267k
    return 0; \
328
269k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
113k
  { \
314
113k
    type combined = 0; \
315
113k
    unsigned offset; \
316
226k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
113k
      uint8_t byte; \
318
113k
      int ret = insn->reader(insn->readerArg, &byte, \
319
113k
                 insn->readerCursor + offset); \
320
113k
      if (ret) \
321
113k
        return ret; \
322
113k
      combined = combined | \
323
113k
           ((uint64_t)byte << (offset * 8)); \
324
113k
    } \
325
113k
    *ptr = combined; \
326
113k
    insn->readerCursor += sizeof(type); \
327
113k
    return 0; \
328
113k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
24.9k
  { \
314
24.9k
    type combined = 0; \
315
24.9k
    unsigned offset; \
316
74.5k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
49.7k
      uint8_t byte; \
318
49.7k
      int ret = insn->reader(insn->readerArg, &byte, \
319
49.7k
                 insn->readerCursor + offset); \
320
49.7k
      if (ret) \
321
49.7k
        return ret; \
322
49.7k
      combined = combined | \
323
49.6k
           ((uint64_t)byte << (offset * 8)); \
324
49.6k
    } \
325
24.9k
    *ptr = combined; \
326
24.7k
    insn->readerCursor += sizeof(type); \
327
24.7k
    return 0; \
328
24.9k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
34.4k
  { \
314
34.4k
    type combined = 0; \
315
34.4k
    unsigned offset; \
316
171k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
137k
      uint8_t byte; \
318
137k
      int ret = insn->reader(insn->readerArg, &byte, \
319
137k
                 insn->readerCursor + offset); \
320
137k
      if (ret) \
321
137k
        return ret; \
322
137k
      combined = combined | \
323
136k
           ((uint64_t)byte << (offset * 8)); \
324
136k
    } \
325
34.4k
    *ptr = combined; \
326
34.0k
    insn->readerCursor += sizeof(type); \
327
34.0k
    return 0; \
328
34.4k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
54.5k
  { \
314
54.5k
    type combined = 0; \
315
54.5k
    unsigned offset; \
316
162k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
108k
      uint8_t byte; \
318
108k
      int ret = insn->reader(insn->readerArg, &byte, \
319
108k
                 insn->readerCursor + offset); \
320
108k
      if (ret) \
321
108k
        return ret; \
322
108k
      combined = combined | \
323
108k
           ((uint64_t)byte << (offset * 8)); \
324
108k
    } \
325
54.5k
    *ptr = combined; \
326
54.1k
    insn->readerCursor += sizeof(type); \
327
54.1k
    return 0; \
328
54.5k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
36.6k
  { \
314
36.6k
    type combined = 0; \
315
36.6k
    unsigned offset; \
316
181k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
145k
      uint8_t byte; \
318
145k
      int ret = insn->reader(insn->readerArg, &byte, \
319
145k
                 insn->readerCursor + offset); \
320
145k
      if (ret) \
321
145k
        return ret; \
322
145k
      combined = combined | \
323
144k
           ((uint64_t)byte << (offset * 8)); \
324
144k
    } \
325
36.6k
    *ptr = combined; \
326
36.0k
    insn->readerCursor += sizeof(type); \
327
36.0k
    return 0; \
328
36.6k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
5.58k
  { \
314
5.58k
    type combined = 0; \
315
5.58k
    unsigned offset; \
316
49.5k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
44.0k
      uint8_t byte; \
318
44.0k
      int ret = insn->reader(insn->readerArg, &byte, \
319
44.0k
                 insn->readerCursor + offset); \
320
44.0k
      if (ret) \
321
44.0k
        return ret; \
322
44.0k
      combined = combined | \
323
43.9k
           ((uint64_t)byte << (offset * 8)); \
324
43.9k
    } \
325
5.58k
    *ptr = combined; \
326
5.45k
    insn->readerCursor += sizeof(type); \
327
5.45k
    return 0; \
328
5.58k
  }
329
330
/*
331
 * consume* - Use the reader function provided by the user to consume data
332
 *   values of various sizes from the instruction's memory and advance the
333
 *   cursor appropriately.  These readers perform endian conversion.
334
 *
335
 * @param insn    - See consumeByte().
336
 * @param ptr     - A pointer to a pre-allocated memory of appropriate size to
337
 *                  be populated with the data read.
338
 * @return        - See consumeByte().
339
 */
340
CONSUME_FUNC(consumeInt8, int8_t)
341
CONSUME_FUNC(consumeInt16, int16_t)
342
CONSUME_FUNC(consumeInt32, int32_t)
343
CONSUME_FUNC(consumeUInt16, uint16_t)
344
CONSUME_FUNC(consumeUInt32, uint32_t)
345
CONSUME_FUNC(consumeUInt64, uint64_t)
346
347
static bool isREX(struct InternalInstruction *insn, uint8_t prefix)
348
1.43M
{
349
1.43M
  if (insn->mode == MODE_64BIT)
350
526k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
909k
  return false;
353
1.43M
}
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
278k
{
363
278k
  uint8_t nextByte;
364
365
278k
  switch (prefix) {
366
69.9k
  case 0xf0: // LOCK
367
69.9k
    insn->hasLockPrefix = true;
368
69.9k
    insn->repeatPrefix = 0;
369
69.9k
    break;
370
371
61.2k
  case 0xf2: // REPNE/REPNZ
372
117k
  case 0xf3: // REP or REPE/REPZ
373
117k
    if (lookAtByte(insn, &nextByte))
374
62
      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
117k
    if (isREX(insn, nextByte) || nextByte == 0x0f ||
382
87.5k
        nextByte == 0x66)
383
      // The last of 0xf2 /0xf3 is mandatory prefix
384
31.5k
      insn->mandatoryPrefix = prefix;
385
386
117k
    insn->repeatPrefix = prefix;
387
117k
    insn->hasLockPrefix = false;
388
117k
    break;
389
390
35.0k
  case 0x66:
391
35.0k
    if (lookAtByte(insn, &nextByte))
392
100
      break;
393
    // 0x66 can't overwrite existing mandatory prefix and should be ignored
394
34.9k
    if (!insn->mandatoryPrefix &&
395
32.3k
        (nextByte == 0x0f || isREX(insn, nextByte)))
396
11.6k
      insn->mandatoryPrefix = prefix;
397
34.9k
    break;
398
278k
  }
399
278k
}
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
1.35M
{
412
1.35M
  bool isPrefix = true;
413
1.35M
  uint8_t byte = 0;
414
1.35M
  uint8_t nextByte;
415
416
2.99M
  while (isPrefix) {
417
1.63M
    if (insn->mode == MODE_64BIT) {
418
      // eliminate consecutive redundant REX bytes in front
419
611k
      if (consumeByte(insn, &byte))
420
244
        return -1;
421
422
610k
      if ((byte & 0xf0) == 0x40) {
423
111k
        while (true) {
424
111k
          if (lookAtByte(
425
111k
                insn,
426
111k
                &byte)) // out of input code
427
221
            return -1;
428
110k
          if ((byte & 0xf0) == 0x40) {
429
            // another REX prefix, but we only remember the last one
430
12.4k
            if (consumeByte(insn, &byte))
431
0
              return -1;
432
12.4k
          } else
433
98.4k
            break;
434
110k
        }
435
436
        // recover the last REX byte if next byte is not a legacy prefix
437
98.4k
        switch (byte) {
438
2.58k
        case 0xf2: /* REPNE/REPNZ */
439
5.01k
        case 0xf3: /* REP or REPE/REPZ */
440
7.98k
        case 0xf0: /* LOCK */
441
8.51k
        case 0x2e: /* CS segment override -OR- Branch not taken */
442
9.16k
        case 0x36: /* SS segment override -OR- Branch taken */
443
9.55k
        case 0x3e: /* DS segment override */
444
10.0k
        case 0x26: /* ES segment override */
445
10.6k
        case 0x64: /* FS segment override */
446
10.9k
        case 0x65: /* GS segment override */
447
12.1k
        case 0x66: /* Operand-size override */
448
14.0k
        case 0x67: /* Address-size override */
449
14.0k
          break;
450
84.3k
        default: /* Not a prefix byte */
451
84.3k
          unconsumeByte(insn);
452
84.3k
          break;
453
98.4k
        }
454
512k
      } else {
455
512k
        unconsumeByte(insn);
456
512k
      }
457
610k
    }
458
459
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
460
1.63M
    if (consumeByte(insn, &byte))
461
263
      return -1;
462
463
1.63M
    if (insn->readerCursor - 1 == insn->startLocation &&
464
1.34M
        (byte == 0xf2 || byte == 0xf3)) {
465
      // prefix requires next byte
466
94.1k
      if (lookAtByte(insn, &nextByte))
467
170
        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
93.9k
      if (((nextByte == 0xf0) ||
477
90.3k
           ((nextByte & 0xfe) == 0x86 ||
478
89.1k
            (nextByte & 0xf8) == 0x90))) {
479
6.59k
        insn->xAcquireRelease = byte;
480
6.59k
      }
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
93.9k
      if (byte == 0xf3 &&
489
45.4k
          (nextByte == 0x88 || nextByte == 0x89 ||
490
44.7k
           nextByte == 0xc6 || nextByte == 0xc7)) {
491
1.76k
        insn->xAcquireRelease = byte;
492
1.76k
      }
493
494
93.9k
      if (isREX(insn, nextByte)) {
495
10.2k
        uint8_t nnextByte;
496
497
        // Go to REX prefix after the current one
498
10.2k
        if (consumeByte(insn, &nnextByte))
499
0
          return -1;
500
501
        // We should be able to read next byte after REX prefix
502
10.2k
        if (lookAtByte(insn, &nnextByte))
503
17
          return -1;
504
505
10.2k
        unconsumeByte(insn);
506
10.2k
      }
507
93.9k
    }
508
509
1.63M
    switch (byte) {
510
69.9k
    case 0xf0: /* LOCK */
511
131k
    case 0xf2: /* REPNE/REPNZ */
512
187k
    case 0xf3: /* REP or REPE/REPZ */
513
      // only accept the last prefix
514
187k
      setPrefixPresent(insn, byte);
515
187k
      insn->prefix0 = byte;
516
187k
      break;
517
518
7.85k
    case 0x2e: /* CS segment override -OR- Branch not taken */
519
11.7k
    case 0x36: /* SS segment override -OR- Branch taken */
520
18.6k
    case 0x3e: /* DS segment override */
521
25.7k
    case 0x26: /* ES segment override */
522
34.6k
    case 0x64: /* FS segment override */
523
39.7k
    case 0x65: /* GS segment override */
524
39.7k
      switch (byte) {
525
7.85k
      case 0x2e:
526
7.85k
        insn->segmentOverride = SEG_OVERRIDE_CS;
527
7.85k
        insn->prefix1 = byte;
528
7.85k
        break;
529
3.93k
      case 0x36:
530
3.93k
        insn->segmentOverride = SEG_OVERRIDE_SS;
531
3.93k
        insn->prefix1 = byte;
532
3.93k
        break;
533
6.85k
      case 0x3e:
534
6.85k
        insn->segmentOverride = SEG_OVERRIDE_DS;
535
6.85k
        insn->prefix1 = byte;
536
6.85k
        break;
537
7.08k
      case 0x26:
538
7.08k
        insn->segmentOverride = SEG_OVERRIDE_ES;
539
7.08k
        insn->prefix1 = byte;
540
7.08k
        break;
541
8.97k
      case 0x64:
542
8.97k
        insn->segmentOverride = SEG_OVERRIDE_FS;
543
8.97k
        insn->prefix1 = byte;
544
8.97k
        break;
545
5.03k
      case 0x65:
546
5.03k
        insn->segmentOverride = SEG_OVERRIDE_GS;
547
5.03k
        insn->prefix1 = byte;
548
5.03k
        break;
549
0
      default:
550
        // debug("Unhandled override");
551
0
        return -1;
552
39.7k
      }
553
39.7k
      setPrefixPresent(insn, byte);
554
39.7k
      break;
555
556
35.0k
    case 0x66: /* Operand-size override */
557
35.0k
      insn->hasOpSize = true;
558
35.0k
      setPrefixPresent(insn, byte);
559
35.0k
      insn->prefix2 = byte;
560
35.0k
      break;
561
562
16.7k
    case 0x67: /* Address-size override */
563
16.7k
      insn->hasAdSize = true;
564
16.7k
      setPrefixPresent(insn, byte);
565
16.7k
      insn->prefix3 = byte;
566
16.7k
      break;
567
1.35M
    default: /* Not a prefix byte */
568
1.35M
      isPrefix = false;
569
1.35M
      break;
570
1.63M
    }
571
1.63M
  }
572
573
1.35M
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
574
575
1.35M
  if (byte == 0x62) {
576
113k
    uint8_t byte1, byte2;
577
578
113k
    if (consumeByte(insn, &byte1)) {
579
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
580
106
      return -1;
581
106
    }
582
583
113k
    if (lookAtByte(insn, &byte2)) {
584
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
585
116
      unconsumeByte(insn); /* unconsume byte1 */
586
116
      unconsumeByte(insn); /* unconsume byte  */
587
112k
    } else {
588
112k
      if ((insn->mode == MODE_64BIT ||
589
70.8k
           (byte1 & 0xc0) == 0xc0) &&
590
101k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
591
101k
        insn->vectorExtensionType = TYPE_EVEX;
592
101k
      } else {
593
11.8k
        unconsumeByte(insn); /* unconsume byte1 */
594
11.8k
        unconsumeByte(insn); /* unconsume byte  */
595
11.8k
      }
596
112k
    }
597
598
113k
    if (insn->vectorExtensionType == TYPE_EVEX) {
599
101k
      insn->vectorExtensionPrefix[0] = byte;
600
101k
      insn->vectorExtensionPrefix[1] = byte1;
601
101k
      if (consumeByte(insn,
602
101k
          &insn->vectorExtensionPrefix[2])) {
603
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
604
0
        return -1;
605
0
      }
606
607
101k
      if (consumeByte(insn,
608
101k
          &insn->vectorExtensionPrefix[3])) {
609
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
610
39
        return -1;
611
39
      }
612
613
      /* We simulate the REX prefix for simplicity's sake */
614
101k
      if (insn->mode == MODE_64BIT) {
615
41.8k
        insn->rexPrefix =
616
41.8k
          0x40 |
617
41.8k
          (wFromEVEX3of4(
618
41.8k
             insn->vectorExtensionPrefix[2])
619
41.8k
           << 3) |
620
41.8k
          (rFromEVEX2of4(
621
41.8k
             insn->vectorExtensionPrefix[1])
622
41.8k
           << 2) |
623
41.8k
          (xFromEVEX2of4(
624
41.8k
             insn->vectorExtensionPrefix[1])
625
41.8k
           << 1) |
626
41.8k
          (bFromEVEX2of4(
627
41.8k
             insn->vectorExtensionPrefix[1])
628
41.8k
           << 0);
629
41.8k
      }
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
101k
    }
635
1.24M
  } else if (byte == 0xc4) {
636
11.8k
    uint8_t byte1;
637
638
11.8k
    if (lookAtByte(insn, &byte1)) {
639
      // dbgprintf(insn, "Couldn't read second byte of VEX");
640
20
      return -1;
641
20
    }
642
643
11.7k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
644
10.5k
      insn->vectorExtensionType = TYPE_VEX_3B;
645
1.24k
    else
646
1.24k
      unconsumeByte(insn);
647
648
11.7k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
649
10.5k
      insn->vectorExtensionPrefix[0] = byte;
650
10.5k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
651
10.5k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
652
653
      /* We simulate the REX prefix for simplicity's sake */
654
10.5k
      if (insn->mode == MODE_64BIT)
655
6.34k
        insn->rexPrefix =
656
6.34k
          0x40 |
657
6.34k
          (wFromVEX3of3(
658
6.34k
             insn->vectorExtensionPrefix[2])
659
6.34k
           << 3) |
660
6.34k
          (rFromVEX2of3(
661
6.34k
             insn->vectorExtensionPrefix[1])
662
6.34k
           << 2) |
663
6.34k
          (xFromVEX2of3(
664
6.34k
             insn->vectorExtensionPrefix[1])
665
6.34k
           << 1) |
666
6.34k
          (bFromVEX2of3(
667
6.34k
             insn->vectorExtensionPrefix[1])
668
6.34k
           << 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
10.5k
    }
674
1.23M
  } else if (byte == 0xc5) {
675
19.4k
    uint8_t byte1;
676
677
19.4k
    if (lookAtByte(insn, &byte1)) {
678
      // dbgprintf(insn, "Couldn't read second byte of VEX");
679
26
      return -1;
680
26
    }
681
682
19.3k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
683
16.4k
      insn->vectorExtensionType = TYPE_VEX_2B;
684
2.94k
    else
685
2.94k
      unconsumeByte(insn);
686
687
19.3k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
688
16.4k
      insn->vectorExtensionPrefix[0] = byte;
689
16.4k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
690
691
16.4k
      if (insn->mode == MODE_64BIT)
692
3.74k
        insn->rexPrefix =
693
3.74k
          0x40 |
694
3.74k
          (rFromVEX2of2(
695
3.74k
             insn->vectorExtensionPrefix[1])
696
3.74k
           << 2);
697
698
16.4k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
699
6.72k
      default:
700
6.72k
        break;
701
9.70k
      case VEX_PREFIX_66:
702
9.70k
        insn->hasOpSize = true;
703
9.70k
        break;
704
16.4k
      }
705
706
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
707
      //    insn->vectorExtensionPrefix[0],
708
      //    insn->vectorExtensionPrefix[1]);
709
16.4k
    }
710
1.21M
  } else if (byte == 0x8f) {
711
12.3k
    uint8_t byte1;
712
713
12.3k
    if (lookAtByte(insn, &byte1)) {
714
      // dbgprintf(insn, "Couldn't read second byte of XOP");
715
18
      return -1;
716
18
    }
717
718
12.3k
    if ((byte1 & 0x38) !=
719
12.3k
        0x0) /* 0 in these 3 bits is a POP instruction. */
720
11.0k
      insn->vectorExtensionType = TYPE_XOP;
721
1.29k
    else
722
1.29k
      unconsumeByte(insn);
723
724
12.3k
    if (insn->vectorExtensionType == TYPE_XOP) {
725
11.0k
      insn->vectorExtensionPrefix[0] = byte;
726
11.0k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
727
11.0k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
728
729
      /* We simulate the REX prefix for simplicity's sake */
730
11.0k
      if (insn->mode == MODE_64BIT)
731
2.36k
        insn->rexPrefix =
732
2.36k
          0x40 |
733
2.36k
          (wFromXOP3of3(
734
2.36k
             insn->vectorExtensionPrefix[2])
735
2.36k
           << 3) |
736
2.36k
          (rFromXOP2of3(
737
2.36k
             insn->vectorExtensionPrefix[1])
738
2.36k
           << 2) |
739
2.36k
          (xFromXOP2of3(
740
2.36k
             insn->vectorExtensionPrefix[1])
741
2.36k
           << 1) |
742
2.36k
          (bFromXOP2of3(
743
2.36k
             insn->vectorExtensionPrefix[1])
744
2.36k
           << 0);
745
746
11.0k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
747
11.0k
      default:
748
11.0k
        break;
749
11.0k
      case VEX_PREFIX_66:
750
28
        insn->hasOpSize = true;
751
28
        break;
752
11.0k
      }
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
11.0k
    }
758
1.19M
  } else if (isREX(insn, byte)) {
759
84.3k
    if (lookAtByte(insn, &nextByte))
760
0
      return -1;
761
762
84.3k
    insn->rexPrefix = byte;
763
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
764
84.3k
  } else
765
1.11M
    unconsumeByte(insn);
766
767
1.35M
  if (insn->mode == MODE_16BIT) {
768
455k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
769
455k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
770
455k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
771
455k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
772
455k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
773
900k
  } else if (insn->mode == MODE_32BIT) {
774
414k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
775
414k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
776
414k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
777
414k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
778
414k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
779
486k
  } else if (insn->mode == MODE_64BIT) {
780
486k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
781
90.1k
      insn->registerSize = 8;
782
90.1k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
783
90.1k
      insn->displacementSize = 4;
784
90.1k
      insn->immediateSize = 4;
785
90.1k
      insn->immSize = 4;
786
396k
    } else {
787
396k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
788
396k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
789
396k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
790
396k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
791
396k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
792
396k
    }
793
486k
  }
794
795
1.35M
  return 0;
796
1.35M
}
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
1.35M
{
809
1.35M
  uint8_t current;
810
811
  // dbgprintf(insn, "readOpcode()");
812
813
1.35M
  insn->opcodeType = ONEBYTE;
814
815
1.35M
  if (insn->vectorExtensionType == TYPE_EVEX) {
816
101k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
817
15
    default:
818
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
819
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
820
15
      return -1;
821
27.1k
    case VEX_LOB_0F:
822
27.1k
      insn->opcodeType = TWOBYTE;
823
27.1k
      return consumeByte(insn, &insn->opcode);
824
31.2k
    case VEX_LOB_0F38:
825
31.2k
      insn->opcodeType = THREEBYTE_38;
826
31.2k
      return consumeByte(insn, &insn->opcode);
827
42.6k
    case VEX_LOB_0F3A:
828
42.6k
      insn->opcodeType = THREEBYTE_3A;
829
42.6k
      return consumeByte(insn, &insn->opcode);
830
101k
    }
831
1.25M
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
832
10.5k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
833
47
    default:
834
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
835
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
836
47
      return -1;
837
2.11k
    case VEX_LOB_0F:
838
      //insn->twoByteEscape = 0x0f;
839
2.11k
      insn->opcodeType = TWOBYTE;
840
2.11k
      return consumeByte(insn, &insn->opcode);
841
5.75k
    case VEX_LOB_0F38:
842
      //insn->twoByteEscape = 0x0f;
843
5.75k
      insn->opcodeType = THREEBYTE_38;
844
5.75k
      return consumeByte(insn, &insn->opcode);
845
2.62k
    case VEX_LOB_0F3A:
846
      //insn->twoByteEscape = 0x0f;
847
2.62k
      insn->opcodeType = THREEBYTE_3A;
848
2.62k
      return consumeByte(insn, &insn->opcode);
849
10.5k
    }
850
1.24M
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
851
    //insn->twoByteEscape = 0x0f;
852
16.4k
    insn->opcodeType = TWOBYTE;
853
16.4k
    return consumeByte(insn, &insn->opcode);
854
1.22M
  } else if (insn->vectorExtensionType == TYPE_XOP) {
855
11.0k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
856
74
    default:
857
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
858
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
859
74
      return -1;
860
10.0k
    case XOP_MAP_SELECT_8:
861
10.0k
      insn->opcodeType = XOP8_MAP;
862
10.0k
      return consumeByte(insn, &insn->opcode);
863
518
    case XOP_MAP_SELECT_9:
864
518
      insn->opcodeType = XOP9_MAP;
865
518
      return consumeByte(insn, &insn->opcode);
866
390
    case XOP_MAP_SELECT_A:
867
390
      insn->opcodeType = XOPA_MAP;
868
390
      return consumeByte(insn, &insn->opcode);
869
11.0k
    }
870
11.0k
  }
871
872
1.21M
  if (consumeByte(insn, &current))
873
0
    return -1;
874
875
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
876
1.21M
  insn->firstByte = current;
877
878
1.21M
  if (current == 0x0f) {
879
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
880
80.0k
    insn->twoByteEscape = current;
881
882
80.0k
    if (consumeByte(insn, &current))
883
108
      return -1;
884
885
79.9k
    if (current == 0x38) {
886
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
887
1.76k
      if (consumeByte(insn, &current))
888
2
        return -1;
889
890
1.76k
      insn->opcodeType = THREEBYTE_38;
891
78.1k
    } else if (current == 0x3a) {
892
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
893
729
      if (consumeByte(insn, &current))
894
2
        return -1;
895
896
727
      insn->opcodeType = THREEBYTE_3A;
897
77.4k
    } else if (current == 0x0f) {
898
      // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
899
      // Consume operands before the opcode to comply with the 3DNow encoding
900
1.36k
      if (readModRM(insn))
901
8
        return -1;
902
903
1.35k
      if (consumeByte(insn, &current))
904
4
        return -1;
905
906
1.34k
      insn->opcodeType = THREEDNOW_MAP;
907
76.0k
    } else {
908
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
909
76.0k
      insn->opcodeType = TWOBYTE;
910
76.0k
    }
911
1.13M
  } else if (insn->mandatoryPrefix)
912
    // The opcode with mandatory prefix must start with opcode escape.
913
    // If not it's legacy repeat prefix
914
14.6k
    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
1.21M
  insn->opcode = current;
922
923
1.21M
  return 0;
924
1.21M
}
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
1.86M
{
950
1.86M
  bool hasModRMExtension;
951
952
1.86M
  InstructionContext instructionClass = contextForAttrs(attrMask);
953
954
1.86M
  hasModRMExtension =
955
1.86M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
956
957
1.86M
  if (hasModRMExtension) {
958
986k
    if (readModRM(insn))
959
2.78k
      return -1;
960
961
984k
    *instructionID = decode(insn->opcodeType, instructionClass,
962
984k
          insn->opcode, insn->modRM);
963
984k
  } else {
964
873k
    *instructionID = decode(insn->opcodeType, instructionClass,
965
873k
          insn->opcode, 0);
966
873k
  }
967
968
1.85M
  return 0;
969
1.86M
}
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
417k
{
980
417k
  size_t i;
981
417k
  uint16_t idx;
982
983
417k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
984
206k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
985
206k
          x86_16_bit_eq_tbl[i].first == orig;
986
200k
         i++) {
987
200k
      if (x86_16_bit_eq_tbl[i].second == equiv)
988
194k
        return true;
989
200k
    }
990
200k
  }
991
992
223k
  return false;
993
417k
}
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
32.9k
{
1002
32.9k
  unsigned int i = find_insn(id);
1003
32.9k
  if (i != -1) {
1004
32.7k
    return insns[i].is64bit;
1005
32.7k
  }
1006
1007
  // not found??
1008
177
  return false;
1009
32.9k
}
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
1.35M
{
1022
1.35M
  uint16_t attrMask;
1023
1.35M
  uint16_t instructionID;
1024
1025
1.35M
  attrMask = ATTR_NONE;
1026
1027
1.35M
  if (insn->mode == MODE_64BIT)
1028
486k
    attrMask |= ATTR_64BIT;
1029
1030
1.35M
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1031
138k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1032
100k
            ATTR_EVEX :
1033
138k
            ATTR_VEX;
1034
1035
138k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1036
100k
      switch (ppFromEVEX3of4(
1037
100k
        insn->vectorExtensionPrefix[2])) {
1038
86.7k
      case VEX_PREFIX_66:
1039
86.7k
        attrMask |= ATTR_OPSIZE;
1040
86.7k
        break;
1041
3.42k
      case VEX_PREFIX_F3:
1042
3.42k
        attrMask |= ATTR_XS;
1043
3.42k
        break;
1044
1.78k
      case VEX_PREFIX_F2:
1045
1.78k
        attrMask |= ATTR_XD;
1046
1.78k
        break;
1047
100k
      }
1048
1049
100k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1050
9.61k
        attrMask |= ATTR_EVEXKZ;
1051
100k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1052
34.7k
        attrMask |= ATTR_EVEXB;
1053
100k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1054
67.1k
        attrMask |= ATTR_EVEXK;
1055
100k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1056
46.1k
        attrMask |= ATTR_EVEXL;
1057
100k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1058
45.5k
        attrMask |= ATTR_EVEXL2;
1059
100k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1060
10.4k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1061
8.58k
      case VEX_PREFIX_66:
1062
8.58k
        attrMask |= ATTR_OPSIZE;
1063
8.58k
        break;
1064
406
      case VEX_PREFIX_F3:
1065
406
        attrMask |= ATTR_XS;
1066
406
        break;
1067
394
      case VEX_PREFIX_F2:
1068
394
        attrMask |= ATTR_XD;
1069
394
        break;
1070
10.4k
      }
1071
1072
10.4k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1073
4.47k
        attrMask |= ATTR_VEXL;
1074
27.3k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1075
16.4k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1076
9.68k
      case VEX_PREFIX_66:
1077
9.68k
        attrMask |= ATTR_OPSIZE;
1078
9.68k
        break;
1079
2.71k
      case VEX_PREFIX_F3:
1080
2.71k
        attrMask |= ATTR_XS;
1081
2.71k
        break;
1082
827
      case VEX_PREFIX_F2:
1083
827
        attrMask |= ATTR_XD;
1084
827
        break;
1085
16.4k
      }
1086
1087
16.4k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1088
11.8k
        attrMask |= ATTR_VEXL;
1089
16.4k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1090
10.9k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1091
13
      case VEX_PREFIX_66:
1092
13
        attrMask |= ATTR_OPSIZE;
1093
13
        break;
1094
16
      case VEX_PREFIX_F3:
1095
16
        attrMask |= ATTR_XS;
1096
16
        break;
1097
28
      case VEX_PREFIX_F2:
1098
28
        attrMask |= ATTR_XD;
1099
28
        break;
1100
10.9k
      }
1101
1102
10.9k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1103
382
        attrMask |= ATTR_VEXL;
1104
10.9k
    } else {
1105
0
      return -1;
1106
0
    }
1107
1.21M
  } else if (!insn->mandatoryPrefix) {
1108
    // If we don't have mandatory prefix we should use legacy prefixes here
1109
1.18M
    if (insn->hasOpSize && (insn->mode != MODE_16BIT))
1110
17.6k
      attrMask |= ATTR_OPSIZE;
1111
1.18M
    if (insn->hasAdSize)
1112
12.5k
      attrMask |= ATTR_ADSIZE;
1113
1.18M
    if (insn->opcodeType == ONEBYTE) {
1114
1.13M
      if (insn->repeatPrefix == 0xf3 &&
1115
32.5k
          (insn->opcode == 0x90))
1116
        // Special support for PAUSE
1117
1.10k
        attrMask |= ATTR_XS;
1118
1.13M
    } else {
1119
52.6k
      if (insn->repeatPrefix == 0xf2)
1120
1.81k
        attrMask |= ATTR_XD;
1121
50.8k
      else if (insn->repeatPrefix == 0xf3)
1122
1.19k
        attrMask |= ATTR_XS;
1123
52.6k
    }
1124
1.18M
  } else {
1125
27.2k
    switch (insn->mandatoryPrefix) {
1126
9.55k
    case 0xf2:
1127
9.55k
      attrMask |= ATTR_XD;
1128
9.55k
      break;
1129
9.97k
    case 0xf3:
1130
9.97k
      attrMask |= ATTR_XS;
1131
9.97k
      break;
1132
7.70k
    case 0x66:
1133
7.70k
      if (insn->mode != MODE_16BIT)
1134
6.67k
        attrMask |= ATTR_OPSIZE;
1135
7.70k
      break;
1136
0
    case 0x67:
1137
0
      attrMask |= ATTR_ADSIZE;
1138
0
      break;
1139
27.2k
    }
1140
27.2k
  }
1141
1142
1.35M
  if (insn->rexPrefix & 0x08) {
1143
90.1k
    attrMask |= ATTR_REXW;
1144
90.1k
    attrMask &= ~ATTR_ADSIZE;
1145
90.1k
  }
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
1.35M
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1152
390k
      insn->opcode == 0xE3)
1153
3.21k
    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
1.35M
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1160
18.5k
    switch (insn->opcode) {
1161
364
    case 0xE8:
1162
769
    case 0xE9:
1163
      // Take care of psubsb and other mmx instructions.
1164
769
      if (insn->opcodeType == ONEBYTE) {
1165
420
        attrMask ^= ATTR_OPSIZE;
1166
420
        insn->immediateSize = 4;
1167
420
        insn->displacementSize = 4;
1168
420
      }
1169
769
      break;
1170
472
    case 0x82:
1171
998
    case 0x83:
1172
1.32k
    case 0x84:
1173
1.81k
    case 0x85:
1174
2.31k
    case 0x86:
1175
3.47k
    case 0x87:
1176
3.75k
    case 0x88:
1177
4.06k
    case 0x89:
1178
4.34k
    case 0x8A:
1179
4.67k
    case 0x8B:
1180
4.97k
    case 0x8C:
1181
5.69k
    case 0x8D:
1182
6.21k
    case 0x8E:
1183
6.57k
    case 0x8F:
1184
      // Take care of lea and three byte ops.
1185
6.57k
      if (insn->opcodeType == TWOBYTE) {
1186
733
        attrMask ^= ATTR_OPSIZE;
1187
733
        insn->immediateSize = 4;
1188
733
        insn->displacementSize = 4;
1189
733
      }
1190
6.57k
      break;
1191
18.5k
    }
1192
18.5k
  }
1193
1194
  /* The following clauses compensate for limitations of the tables. */
1195
1.35M
  if (insn->mode != MODE_64BIT &&
1196
869k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1197
84.6k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1198
33
      return -1;
1199
33
    }
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
84.6k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1206
59.1k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1207
54.1k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1208
4.17k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1209
52.3k
        (insn->vectorExtensionType == TYPE_XOP &&
1210
32.9k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1211
32.9k
      uint16_t instructionIDWithREXW;
1212
1213
32.9k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1214
32.9k
                attrMask | ATTR_REXW)) {
1215
3
        insn->instructionID = instructionID;
1216
3
        insn->spec = specifierForUID(instructionID);
1217
3
        return 0;
1218
3
      }
1219
1220
      // If not a 64-bit instruction. Switch the opcode.
1221
32.9k
      if (!is64Bit(instructionIDWithREXW)) {
1222
31.2k
        insn->instructionID = instructionIDWithREXW;
1223
31.2k
        insn->spec =
1224
31.2k
          specifierForUID(instructionIDWithREXW);
1225
1226
31.2k
        return 0;
1227
31.2k
      }
1228
32.9k
    }
1229
84.6k
  }
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
1.32M
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1239
1.30M
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1240
1.30M
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1241
    /* Make sure we observed the prefixes in any position. */
1242
16.7k
    if (insn->hasAdSize)
1243
915
      attrMask |= ATTR_ADSIZE;
1244
1245
16.7k
    if (insn->hasOpSize)
1246
570
      attrMask |= ATTR_OPSIZE;
1247
1248
    /* In 16-bit, invert the attributes. */
1249
16.7k
    if (insn->mode == MODE_16BIT) {
1250
7.28k
      attrMask ^= ATTR_ADSIZE;
1251
1252
      /* The OpSize attribute is only valid with the absolute moves. */
1253
7.28k
      if (insn->opcodeType == ONEBYTE &&
1254
6.41k
          ((insn->opcode & 0xFC) == 0xA0))
1255
6.41k
        attrMask ^= ATTR_OPSIZE;
1256
7.28k
    }
1257
1258
16.7k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1259
4
      return -1;
1260
4
    }
1261
1262
16.7k
    insn->instructionID = instructionID;
1263
16.7k
    insn->spec = specifierForUID(instructionID);
1264
1265
16.7k
    return 0;
1266
16.7k
  }
1267
1.30M
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1268
2.73k
    return -1;
1269
2.73k
  }
1270
1271
1.30M
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1272
464k
      !(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
417k
    const struct InstructionSpecifier *spec;
1281
417k
    uint16_t instructionIDWithOpsize;
1282
1283
417k
    spec = specifierForUID(instructionID);
1284
1285
417k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1286
417k
              attrMask | ATTR_OPSIZE)) {
1287
      /*
1288
       * ModRM required with OpSize but not present; give up and return version
1289
       * without OpSize set
1290
       */
1291
6
      insn->instructionID = instructionID;
1292
6
      insn->spec = spec;
1293
1294
6
      return 0;
1295
6
    }
1296
1297
417k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1298
194k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1299
192k
      insn->instructionID = instructionIDWithOpsize;
1300
192k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1301
225k
    } else {
1302
225k
      insn->instructionID = instructionID;
1303
225k
      insn->spec = spec;
1304
225k
    }
1305
1306
417k
    return 0;
1307
417k
  }
1308
1309
886k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1310
3.13k
      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
422
    const struct InstructionSpecifier *spec;
1316
422
    uint16_t instructionIDWithNewOpcode;
1317
422
    const struct InstructionSpecifier *specWithNewOpcode;
1318
1319
422
    spec = specifierForUID(instructionID);
1320
1321
    /* Borrow opcode from one of the other XCHGar opcodes */
1322
422
    insn->opcode = 0x91;
1323
1324
422
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1325
422
              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
422
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1335
1336
    /* Change back */
1337
422
    insn->opcode = 0x90;
1338
1339
422
    insn->instructionID = instructionIDWithNewOpcode;
1340
422
    insn->spec = specWithNewOpcode;
1341
1342
422
    return 0;
1343
422
  }
1344
1345
886k
  insn->instructionID = instructionID;
1346
886k
  insn->spec = specifierForUID(insn->instructionID);
1347
1348
886k
  return 0;
1349
886k
}
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
36.9k
{
1360
36.9k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1361
36.9k
  uint8_t index, base;
1362
1363
  // dbgprintf(insn, "readSIB()");
1364
1365
36.9k
  if (insn->consumedSIB)
1366
0
    return 0;
1367
1368
36.9k
  insn->consumedSIB = true;
1369
1370
36.9k
  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
15.2k
  case 4:
1375
15.2k
    insn->sibIndexBase = SIB_INDEX_EAX;
1376
15.2k
    sibBaseBase = SIB_BASE_EAX;
1377
15.2k
    break;
1378
21.6k
  case 8:
1379
21.6k
    insn->sibIndexBase = SIB_INDEX_RAX;
1380
21.6k
    sibBaseBase = SIB_BASE_RAX;
1381
21.6k
    break;
1382
36.9k
  }
1383
1384
36.9k
  if (consumeByte(insn, &insn->sib))
1385
94
    return -1;
1386
1387
36.8k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1388
1389
36.8k
  if (index == 0x4) {
1390
7.68k
    insn->sibIndex = SIB_INDEX_NONE;
1391
29.1k
  } else {
1392
29.1k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1393
29.1k
  }
1394
1395
36.8k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1396
1397
36.8k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1398
1399
36.8k
  switch (base) {
1400
4.10k
  case 0x5:
1401
5.25k
  case 0xd:
1402
5.25k
    switch (modFromModRM(insn->modRM)) {
1403
2.80k
    case 0x0:
1404
2.80k
      insn->eaDisplacement = EA_DISP_32;
1405
2.80k
      insn->sibBase = SIB_BASE_NONE;
1406
2.80k
      break;
1407
2.01k
    case 0x1:
1408
2.01k
      insn->eaDisplacement = EA_DISP_8;
1409
2.01k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1410
2.01k
      break;
1411
434
    case 0x2:
1412
434
      insn->eaDisplacement = EA_DISP_32;
1413
434
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1414
434
      break;
1415
0
    case 0x3:
1416
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1417
0
      return -1;
1418
5.25k
    }
1419
5.25k
    break;
1420
31.6k
  default:
1421
31.6k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1422
31.6k
    break;
1423
36.8k
  }
1424
1425
36.8k
  return 0;
1426
36.8k
}
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
242k
{
1437
242k
  int8_t d8;
1438
242k
  int16_t d16;
1439
242k
  int32_t d32;
1440
1441
  // dbgprintf(insn, "readDisplacement()");
1442
1443
242k
  if (insn->consumedDisplacement)
1444
0
    return 0;
1445
1446
242k
  insn->consumedDisplacement = true;
1447
242k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1448
1449
242k
  switch (insn->eaDisplacement) {
1450
69.4k
  case EA_DISP_NONE:
1451
69.4k
    insn->consumedDisplacement = false;
1452
69.4k
    break;
1453
113k
  case EA_DISP_8:
1454
113k
    if (consumeInt8(insn, &d8))
1455
238
      return -1;
1456
113k
    insn->displacement = d8;
1457
113k
    break;
1458
24.9k
  case EA_DISP_16:
1459
24.9k
    if (consumeInt16(insn, &d16))
1460
107
      return -1;
1461
24.7k
    insn->displacement = d16;
1462
24.7k
    break;
1463
34.4k
  case EA_DISP_32:
1464
34.4k
    if (consumeInt32(insn, &d32))
1465
383
      return -1;
1466
34.0k
    insn->displacement = d32;
1467
34.0k
    break;
1468
242k
  }
1469
1470
241k
  return 0;
1471
242k
}
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
2.24M
{
1482
2.24M
  uint8_t mod, rm, reg, evexrm;
1483
1484
  // dbgprintf(insn, "readModRM()");
1485
1486
2.24M
  if (insn->consumedModRM)
1487
1.51M
    return 0;
1488
1489
727k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1490
1491
727k
  if (consumeByte(insn, &insn->modRM))
1492
1.96k
    return -1;
1493
1494
725k
  insn->consumedModRM = true;
1495
1496
  // save original ModRM for later reference
1497
725k
  insn->orgModRM = insn->modRM;
1498
1499
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1500
725k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1501
72.1k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1502
832
    insn->modRM |= 0xC0;
1503
1504
725k
  mod = modFromModRM(insn->modRM);
1505
725k
  rm = rmFromModRM(insn->modRM);
1506
725k
  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
725k
  switch (insn->registerSize) {
1514
247k
  case 2:
1515
247k
    insn->regBase = MODRM_REG_AX;
1516
247k
    insn->eaRegBase = EA_REG_AX;
1517
247k
    break;
1518
405k
  case 4:
1519
405k
    insn->regBase = MODRM_REG_EAX;
1520
405k
    insn->eaRegBase = EA_REG_EAX;
1521
405k
    break;
1522
72.0k
  case 8:
1523
72.0k
    insn->regBase = MODRM_REG_RAX;
1524
72.0k
    insn->eaRegBase = EA_REG_RAX;
1525
72.0k
    break;
1526
725k
  }
1527
1528
725k
  reg |= rFromREX(insn->rexPrefix) << 3;
1529
725k
  rm |= bFromREX(insn->rexPrefix) << 3;
1530
1531
725k
  evexrm = 0;
1532
725k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1533
100k
      insn->mode == MODE_64BIT) {
1534
41.6k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1535
41.6k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1536
41.6k
  }
1537
1538
725k
  insn->reg = (Reg)(insn->regBase + reg);
1539
1540
725k
  switch (insn->addressSize) {
1541
229k
  case 2: {
1542
229k
    EABase eaBaseBase = EA_BASE_BX_SI;
1543
1544
229k
    switch (mod) {
1545
127k
    case 0x0:
1546
127k
      if (rm == 0x6) {
1547
6.51k
        insn->eaBase = EA_BASE_NONE;
1548
6.51k
        insn->eaDisplacement = EA_DISP_16;
1549
6.51k
        if (readDisplacement(insn))
1550
30
          return -1;
1551
120k
      } else {
1552
120k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1553
120k
        insn->eaDisplacement = EA_DISP_NONE;
1554
120k
      }
1555
127k
      break;
1556
127k
    case 0x1:
1557
33.1k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1558
33.1k
      insn->eaDisplacement = EA_DISP_8;
1559
33.1k
      insn->displacementSize = 1;
1560
33.1k
      if (readDisplacement(insn))
1561
55
        return -1;
1562
33.0k
      break;
1563
33.0k
    case 0x2:
1564
18.3k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1565
18.3k
      insn->eaDisplacement = EA_DISP_16;
1566
18.3k
      if (readDisplacement(insn))
1567
77
        return -1;
1568
18.3k
      break;
1569
50.2k
    case 0x3:
1570
50.2k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1571
50.2k
      if (readDisplacement(insn))
1572
0
        return -1;
1573
50.2k
      break;
1574
229k
    }
1575
229k
    break;
1576
229k
  }
1577
1578
229k
  case 4:
1579
496k
  case 8: {
1580
496k
    EABase eaBaseBase =
1581
496k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1582
1583
496k
    switch (mod) {
1584
0
    default:
1585
0
      break;
1586
251k
    case 0x0:
1587
251k
      insn->eaDisplacement =
1588
251k
        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
251k
      switch (rm & 7) {
1593
22.0k
      case 0x4: // SIB byte is present
1594
22.0k
        insn->eaBase = (insn->addressSize == 4 ?
1595
9.10k
              EA_BASE_sib :
1596
22.0k
              EA_BASE_sib64);
1597
22.0k
        if (readSIB(insn) || readDisplacement(insn))
1598
40
          return -1;
1599
21.9k
        break;
1600
21.9k
      case 0x5: // RIP-relative
1601
6.15k
        insn->eaBase = EA_BASE_NONE;
1602
6.15k
        insn->eaDisplacement = EA_DISP_32;
1603
6.15k
        if (readDisplacement(insn))
1604
56
          return -1;
1605
6.10k
        break;
1606
222k
      default:
1607
222k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1608
222k
        break;
1609
251k
      }
1610
250k
      break;
1611
250k
    case 0x1:
1612
80.4k
      insn->displacementSize = 1;
1613
      /* FALLTHROUGH */
1614
105k
    case 0x2:
1615
105k
      insn->eaDisplacement =
1616
105k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1617
105k
      switch (rm & 7) {
1618
14.9k
      case 0x4: // SIB byte is present
1619
14.9k
        insn->eaBase = EA_BASE_sib;
1620
14.9k
        if (readSIB(insn) || readDisplacement(insn))
1621
109
          return -1;
1622
14.8k
        break;
1623
91.0k
      default:
1624
91.0k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1625
91.0k
        if (readDisplacement(insn))
1626
455
          return -1;
1627
90.5k
        break;
1628
105k
      }
1629
105k
      break;
1630
139k
    case 0x3:
1631
139k
      insn->eaDisplacement = EA_DISP_NONE;
1632
139k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1633
139k
      break;
1634
496k
    }
1635
1636
495k
    break;
1637
496k
  }
1638
725k
  } /* switch (insn->addressSize) */
1639
1640
724k
  return 0;
1641
725k
}
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
823k
  { \
1647
823k
    *valid = 1; \
1648
823k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
186k
    case TYPE_Rv: \
1653
186k
      return base + index; \
1654
249k
    case TYPE_R8: \
1655
249k
      index &= mask; \
1656
249k
      if (index > 0xf) \
1657
249k
        *valid = 0; \
1658
249k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
4.25k
        return prefix##_SPL + (index - 4); \
1660
244k
      } else { \
1661
244k
        return prefix##_AL + index; \
1662
244k
      } \
1663
249k
    case TYPE_R16: \
1664
6.16k
      index &= mask; \
1665
6.16k
      if (index > 0xf) \
1666
6.16k
        *valid = 0; \
1667
6.16k
      return prefix##_AX + index; \
1668
249k
    case TYPE_R32: \
1669
3.72k
      index &= mask; \
1670
3.72k
      if (index > 0xf) \
1671
3.72k
        *valid = 0; \
1672
3.72k
      return prefix##_EAX + index; \
1673
249k
    case TYPE_R64: \
1674
26.5k
      index &= mask; \
1675
26.5k
      if (index > 0xf) \
1676
26.5k
        *valid = 0; \
1677
26.5k
      return prefix##_RAX + index; \
1678
249k
    case TYPE_ZMM: \
1679
76.5k
      return prefix##_ZMM0 + index; \
1680
249k
    case TYPE_YMM: \
1681
58.3k
      return prefix##_YMM0 + index; \
1682
249k
    case TYPE_XMM: \
1683
133k
      return prefix##_XMM0 + index; \
1684
249k
    case TYPE_VK: \
1685
53.6k
      index &= 0xf; \
1686
53.6k
      if (index > 7) \
1687
53.6k
        *valid = 0; \
1688
53.6k
      return prefix##_K0 + index; \
1689
249k
    case TYPE_MM64: \
1690
11.6k
      return prefix##_MM0 + (index & 0x7); \
1691
249k
    case TYPE_SEGMENTREG: \
1692
3.06k
      if ((index & 7) > 5) \
1693
3.06k
        *valid = 0; \
1694
3.06k
      return prefix##_ES + (index & 7); \
1695
249k
    case TYPE_DEBUGREG: \
1696
498
      return prefix##_DR0 + index; \
1697
249k
    case TYPE_CONTROLREG: \
1698
333
      return prefix##_CR0 + index; \
1699
249k
    case TYPE_BNDR: \
1700
12.8k
      if (index > 3) \
1701
12.8k
        *valid = 0; \
1702
12.8k
      return prefix##_BND0 + index; \
1703
249k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
249k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
249k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
823k
    } \
1710
823k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1646
642k
  { \
1647
642k
    *valid = 1; \
1648
642k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
136k
    case TYPE_Rv: \
1653
136k
      return base + index; \
1654
199k
    case TYPE_R8: \
1655
199k
      index &= mask; \
1656
199k
      if (index > 0xf) \
1657
199k
        *valid = 0; \
1658
199k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
2.95k
        return prefix##_SPL + (index - 4); \
1660
196k
      } else { \
1661
196k
        return prefix##_AL + index; \
1662
196k
      } \
1663
199k
    case TYPE_R16: \
1664
4.78k
      index &= mask; \
1665
4.78k
      if (index > 0xf) \
1666
4.78k
        *valid = 0; \
1667
4.78k
      return prefix##_AX + index; \
1668
199k
    case TYPE_R32: \
1669
1.97k
      index &= mask; \
1670
1.97k
      if (index > 0xf) \
1671
1.97k
        *valid = 0; \
1672
1.97k
      return prefix##_EAX + index; \
1673
199k
    case TYPE_R64: \
1674
15.7k
      index &= mask; \
1675
15.7k
      if (index > 0xf) \
1676
15.7k
        *valid = 0; \
1677
15.7k
      return prefix##_RAX + index; \
1678
199k
    case TYPE_ZMM: \
1679
58.6k
      return prefix##_ZMM0 + index; \
1680
199k
    case TYPE_YMM: \
1681
46.2k
      return prefix##_YMM0 + index; \
1682
199k
    case TYPE_XMM: \
1683
107k
      return prefix##_XMM0 + index; \
1684
199k
    case TYPE_VK: \
1685
49.4k
      index &= 0xf; \
1686
49.4k
      if (index > 7) \
1687
49.4k
        *valid = 0; \
1688
49.4k
      return prefix##_K0 + index; \
1689
199k
    case TYPE_MM64: \
1690
7.42k
      return prefix##_MM0 + (index & 0x7); \
1691
199k
    case TYPE_SEGMENTREG: \
1692
3.06k
      if ((index & 7) > 5) \
1693
3.06k
        *valid = 0; \
1694
3.06k
      return prefix##_ES + (index & 7); \
1695
199k
    case TYPE_DEBUGREG: \
1696
498
      return prefix##_DR0 + index; \
1697
199k
    case TYPE_CONTROLREG: \
1698
333
      return prefix##_CR0 + index; \
1699
199k
    case TYPE_BNDR: \
1700
11.0k
      if (index > 3) \
1701
11.0k
        *valid = 0; \
1702
11.0k
      return prefix##_BND0 + index; \
1703
199k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
199k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
199k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
642k
    } \
1710
642k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1646
180k
  { \
1647
180k
    *valid = 1; \
1648
180k
    switch (type) { \
1649
0
    default: \
1650
0
      *valid = 0; \
1651
0
      return 0; \
1652
49.8k
    case TYPE_Rv: \
1653
49.8k
      return base + index; \
1654
49.9k
    case TYPE_R8: \
1655
49.9k
      index &= mask; \
1656
49.9k
      if (index > 0xf) \
1657
49.9k
        *valid = 0; \
1658
49.9k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1659
1.30k
        return prefix##_SPL + (index - 4); \
1660
48.6k
      } else { \
1661
48.6k
        return prefix##_AL + index; \
1662
48.6k
      } \
1663
49.9k
    case TYPE_R16: \
1664
1.38k
      index &= mask; \
1665
1.38k
      if (index > 0xf) \
1666
1.38k
        *valid = 0; \
1667
1.38k
      return prefix##_AX + index; \
1668
49.9k
    case TYPE_R32: \
1669
1.75k
      index &= mask; \
1670
1.75k
      if (index > 0xf) \
1671
1.75k
        *valid = 0; \
1672
1.75k
      return prefix##_EAX + index; \
1673
49.9k
    case TYPE_R64: \
1674
10.7k
      index &= mask; \
1675
10.7k
      if (index > 0xf) \
1676
10.7k
        *valid = 0; \
1677
10.7k
      return prefix##_RAX + index; \
1678
49.9k
    case TYPE_ZMM: \
1679
17.9k
      return prefix##_ZMM0 + index; \
1680
49.9k
    case TYPE_YMM: \
1681
12.0k
      return prefix##_YMM0 + index; \
1682
49.9k
    case TYPE_XMM: \
1683
26.7k
      return prefix##_XMM0 + index; \
1684
49.9k
    case TYPE_VK: \
1685
4.24k
      index &= 0xf; \
1686
4.24k
      if (index > 7) \
1687
4.24k
        *valid = 0; \
1688
4.24k
      return prefix##_K0 + index; \
1689
49.9k
    case TYPE_MM64: \
1690
4.23k
      return prefix##_MM0 + (index & 0x7); \
1691
49.9k
    case TYPE_SEGMENTREG: \
1692
0
      if ((index & 7) > 5) \
1693
0
        *valid = 0; \
1694
0
      return prefix##_ES + (index & 7); \
1695
49.9k
    case TYPE_DEBUGREG: \
1696
0
      return prefix##_DR0 + index; \
1697
49.9k
    case TYPE_CONTROLREG: \
1698
0
      return prefix##_CR0 + index; \
1699
49.9k
    case TYPE_BNDR: \
1700
1.79k
      if (index > 3) \
1701
1.79k
        *valid = 0; \
1702
1.79k
      return prefix##_BND0 + index; \
1703
49.9k
    case TYPE_MVSIBX: \
1704
0
      return prefix##_XMM0 + index; \
1705
49.9k
    case TYPE_MVSIBY: \
1706
0
      return prefix##_YMM0 + index; \
1707
49.9k
    case TYPE_MVSIBZ: \
1708
0
      return prefix##_ZMM0 + index; \
1709
180k
    } \
1710
180k
  }
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
1.34M
{
1740
1.34M
  uint8_t valid;
1741
1742
1.34M
  switch ((OperandEncoding)op->encoding) {
1743
0
  default:
1744
    // debug("Expected a REG or R/M encoding in fixupReg");
1745
0
    return -1;
1746
99.6k
  case ENCODING_VVVV:
1747
99.6k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1748
99.6k
            insn->vvvv, &valid);
1749
99.6k
    if (!valid)
1750
2
      return -1;
1751
99.6k
    break;
1752
542k
  case ENCODING_REG:
1753
542k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1754
542k
                 insn->reg - insn->regBase,
1755
542k
                 &valid);
1756
542k
    if (!valid)
1757
43
      return -1;
1758
542k
    break;
1759
4.58M
CASE_ENCODING_RM:
1760
4.58M
    if (insn->eaBase >= insn->eaRegBase) {
1761
180k
      insn->eaBase = (EABase)fixupRMValue(
1762
180k
        insn, (OperandType)op->type,
1763
180k
        insn->eaBase - insn->eaRegBase, &valid);
1764
180k
      if (!valid)
1765
6
        return -1;
1766
180k
    }
1767
705k
    break;
1768
1.34M
  }
1769
1770
1.34M
  return 0;
1771
1.34M
}
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
139k
{
1786
139k
  if (size == 0)
1787
97.7k
    size = insn->registerSize;
1788
1789
139k
  switch (size) {
1790
19.4k
  case 1:
1791
19.4k
    insn->opcodeRegister =
1792
19.4k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1793
19.4k
                (insn->opcode & 7)));
1794
19.4k
    if (insn->rexPrefix &&
1795
1.77k
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1796
1.20k
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1797
520
      insn->opcodeRegister =
1798
520
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1799
520
                   MODRM_REG_AL - 4));
1800
520
    }
1801
1802
19.4k
    break;
1803
45.4k
  case 2:
1804
45.4k
    insn->opcodeRegister =
1805
45.4k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1806
45.4k
                (insn->opcode & 7)));
1807
45.4k
    break;
1808
51.7k
  case 4:
1809
51.7k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1810
51.7k
               ((bFromREX(insn->rexPrefix) << 3) |
1811
51.7k
                (insn->opcode & 7)));
1812
51.7k
    break;
1813
22.5k
  case 8:
1814
22.5k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1815
22.5k
               ((bFromREX(insn->rexPrefix) << 3) |
1816
22.5k
                (insn->opcode & 7)));
1817
22.5k
    break;
1818
139k
  }
1819
1820
139k
  return 0;
1821
139k
}
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
396k
{
1834
396k
  uint8_t imm8;
1835
396k
  uint16_t imm16;
1836
396k
  uint32_t imm32;
1837
396k
  uint64_t imm64;
1838
1839
396k
  if (insn->numImmediatesConsumed == 2) {
1840
    // debug("Already consumed two immediates");
1841
0
    return -1;
1842
0
  }
1843
1844
396k
  if (size == 0)
1845
0
    size = insn->immediateSize;
1846
396k
  else
1847
396k
    insn->immediateSize = size;
1848
1849
396k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1850
1851
396k
  switch (size) {
1852
299k
  case 1:
1853
299k
    if (consumeByte(insn, &imm8))
1854
859
      return -1;
1855
1856
298k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
1857
298k
    break;
1858
54.5k
  case 2:
1859
54.5k
    if (consumeUInt16(insn, &imm16))
1860
325
      return -1;
1861
1862
54.1k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
1863
54.1k
    break;
1864
36.6k
  case 4:
1865
36.6k
    if (consumeUInt32(insn, &imm32))
1866
585
      return -1;
1867
1868
36.0k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
1869
36.0k
    break;
1870
5.58k
  case 8:
1871
5.58k
    if (consumeUInt64(insn, &imm64))
1872
135
      return -1;
1873
5.45k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
1874
5.45k
    break;
1875
396k
  }
1876
1877
394k
  insn->numImmediatesConsumed++;
1878
1879
394k
  return 0;
1880
396k
}
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
1.35M
{
1891
1.35M
  int vvvv;
1892
1893
1.35M
  if (insn->vectorExtensionType == TYPE_EVEX)
1894
100k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1895
100k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
1896
1.24M
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
1897
10.3k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
1898
1.23M
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
1899
16.2k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
1900
1.22M
  else if (insn->vectorExtensionType == TYPE_XOP)
1901
10.8k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
1902
1.21M
  else
1903
1.21M
    return -1;
1904
1905
138k
  if (insn->mode != MODE_64BIT)
1906
84.1k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1907
1908
138k
  insn->vvvv = (Reg)vvvv;
1909
1910
138k
  return 0;
1911
1.35M
}
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
68.5k
{
1922
68.5k
  if (insn->vectorExtensionType != TYPE_EVEX)
1923
0
    return -1;
1924
1925
68.5k
  insn->writemask =
1926
68.5k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
1927
1928
68.5k
  return 0;
1929
68.5k
}
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
1.35M
{
1940
1.35M
  int hasVVVV, needVVVV;
1941
1.35M
  int sawRegImm = 0;
1942
1.35M
  int i;
1943
1944
  /* If non-zero vvvv specified, need to make sure one of the operands
1945
     uses it. */
1946
1.35M
  hasVVVV = !readVVVV(insn);
1947
1.35M
  needVVVV = hasVVVV && (insn->vvvv != 0);
1948
1949
9.43M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
1950
8.09M
    const OperandSpecifier *op =
1951
8.09M
      &x86OperandSets[insn->spec->operands][i];
1952
8.09M
    switch (op->encoding) {
1953
5.67M
    case ENCODING_NONE:
1954
5.74M
    case ENCODING_SI:
1955
5.81M
    case ENCODING_DI:
1956
5.81M
      break;
1957
1958
53.4k
CASE_ENCODING_VSIB:
1959
      // VSIB can use the V2 bit so check only the other bits.
1960
53.4k
      if (needVVVV)
1961
5.96k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
1962
1963
53.4k
      if (readModRM(insn))
1964
0
        return -1;
1965
1966
      // Reject if SIB wasn't used.
1967
10.2k
      if (insn->eaBase != EA_BASE_sib &&
1968
6.34k
          insn->eaBase != EA_BASE_sib64)
1969
17
        return -1;
1970
1971
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1972
10.2k
      if (insn->sibIndex == SIB_INDEX_NONE)
1973
1.05k
        insn->sibIndex =
1974
1.05k
          (SIBIndex)(insn->sibIndexBase + 4);
1975
1976
      // If EVEX.v2 is set this is one of the 16-31 registers.
1977
10.2k
      if (insn->vectorExtensionType == TYPE_EVEX &&
1978
7.70k
          insn->mode == MODE_64BIT &&
1979
5.45k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1980
3.64k
        insn->sibIndex =
1981
3.64k
          (SIBIndex)(insn->sibIndex + 16);
1982
1983
      // Adjust the index register to the correct size.
1984
10.2k
      switch (op->type) {
1985
0
      default:
1986
        // debug("Unhandled VSIB index type");
1987
0
        return -1;
1988
3.70k
      case TYPE_MVSIBX:
1989
3.70k
        insn->sibIndex =
1990
3.70k
          (SIBIndex)(SIB_INDEX_XMM0 +
1991
3.70k
               (insn->sibIndex -
1992
3.70k
                insn->sibIndexBase));
1993
3.70k
        break;
1994
3.30k
      case TYPE_MVSIBY:
1995
3.30k
        insn->sibIndex =
1996
3.30k
          (SIBIndex)(SIB_INDEX_YMM0 +
1997
3.30k
               (insn->sibIndex -
1998
3.30k
                insn->sibIndexBase));
1999
3.30k
        break;
2000
3.21k
      case TYPE_MVSIBZ:
2001
3.21k
        insn->sibIndex =
2002
3.21k
          (SIBIndex)(SIB_INDEX_ZMM0 +
2003
3.21k
               (insn->sibIndex -
2004
3.21k
                insn->sibIndexBase));
2005
3.21k
        break;
2006
10.2k
      }
2007
2008
      // Apply the AVX512 compressed displacement scaling factor.
2009
10.2k
      if (op->encoding != ENCODING_REG &&
2010
10.2k
          insn->eaDisplacement == EA_DISP_8)
2011
1.53k
        insn->displacement *=
2012
1.53k
          1 << (op->encoding - ENCODING_VSIB);
2013
10.2k
      break;
2014
2015
542k
    case ENCODING_REG:
2016
8.37M
CASE_ENCODING_RM:
2017
8.37M
      if (readModRM(insn))
2018
0
        return -1;
2019
2020
1.24M
      if (fixupReg(insn, op))
2021
49
        return -1;
2022
2023
      // Apply the AVX512 compressed displacement scaling factor.
2024
1.24M
      if (op->encoding != ENCODING_REG &&
2025
705k
          insn->eaDisplacement == EA_DISP_8)
2026
111k
        insn->displacement *=
2027
111k
          1 << (op->encoding - ENCODING_RM);
2028
1.24M
      break;
2029
2030
301k
    case ENCODING_IB:
2031
301k
      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
1.70k
        insn->immediates[insn->numImmediatesConsumed] =
2035
1.70k
          insn->immediates
2036
1.70k
            [insn->numImmediatesConsumed -
2037
1.70k
             1] &
2038
1.70k
          0xf;
2039
1.70k
        ++insn->numImmediatesConsumed;
2040
1.70k
        break;
2041
1.70k
      }
2042
299k
      if (readImmediate(insn, 1))
2043
859
        return -1;
2044
298k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2045
2.80k
        sawRegImm = 1;
2046
298k
      break;
2047
2048
18.6k
    case ENCODING_IW:
2049
18.6k
      if (readImmediate(insn, 2))
2050
74
        return -1;
2051
18.5k
      break;
2052
2053
18.5k
    case ENCODING_ID:
2054
7.00k
      if (readImmediate(insn, 4))
2055
129
        return -1;
2056
6.87k
      break;
2057
2058
6.87k
    case ENCODING_IO:
2059
951
      if (readImmediate(insn, 8))
2060
15
        return -1;
2061
936
      break;
2062
2063
54.4k
    case ENCODING_Iv:
2064
54.4k
      if (readImmediate(insn, insn->immediateSize))
2065
620
        return -1;
2066
53.8k
      break;
2067
2068
53.8k
    case ENCODING_Ia:
2069
15.6k
      if (readImmediate(insn, insn->addressSize))
2070
207
        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
15.4k
      insn->displacementOffset = insn->immediateOffset;
2075
15.4k
      insn->consumedDisplacement = true;
2076
15.4k
      insn->displacementSize = insn->immediateSize;
2077
15.4k
      insn->displacement =
2078
15.4k
        insn->immediates[insn->numImmediatesConsumed -
2079
15.4k
             1];
2080
15.4k
      insn->immediateOffset = 0;
2081
15.4k
      insn->immediateSize = 0;
2082
15.4k
      break;
2083
2084
3.76k
    case ENCODING_IRC:
2085
3.76k
      insn->RC =
2086
3.76k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2087
3.76k
         << 1) |
2088
3.76k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2089
3.76k
      break;
2090
2091
19.4k
    case ENCODING_RB:
2092
19.4k
      if (readOpcodeRegister(insn, 1))
2093
0
        return -1;
2094
19.4k
      break;
2095
2096
19.4k
    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
22.0k
    case ENCODING_RO:
2107
22.0k
      if (readOpcodeRegister(insn, 8))
2108
0
        return -1;
2109
22.0k
      break;
2110
2111
97.7k
    case ENCODING_Rv:
2112
97.7k
      if (readOpcodeRegister(insn, 0))
2113
0
        return -1;
2114
97.7k
      break;
2115
2116
97.7k
    case ENCODING_FP:
2117
6.45k
      break;
2118
2119
99.6k
    case ENCODING_VVVV:
2120
99.6k
      if (!hasVVVV)
2121
0
        return -1;
2122
2123
99.6k
      needVVVV =
2124
99.6k
        0; /* Mark that we have found a VVVV operand. */
2125
2126
99.6k
      if (insn->mode != MODE_64BIT)
2127
60.4k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2128
2129
99.6k
      if (fixupReg(insn, op))
2130
2
        return -1;
2131
99.6k
      break;
2132
2133
99.6k
    case ENCODING_WRITEMASK:
2134
68.5k
      if (readMaskRegister(insn))
2135
0
        return -1;
2136
68.5k
      break;
2137
2138
299k
    case ENCODING_DUP:
2139
299k
      break;
2140
2141
0
    default:
2142
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2143
0
      return -1;
2144
8.09M
    }
2145
8.09M
  }
2146
2147
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2148
1.34M
  if (needVVVV)
2149
26
    return -1;
2150
2151
1.34M
  return 0;
2152
1.34M
}
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
1.35M
{
2158
  // LOCK prefix
2159
1.35M
  if (insn->hasLockPrefix) {
2160
61.7k
    switch (insn->instructionID) {
2161
389
    default:
2162
      // invalid LOCK
2163
389
      return true;
2164
2165
    // nop dword [rax]
2166
263
    case X86_NOOPL:
2167
2168
    // DEC
2169
637
    case X86_DEC16m:
2170
1.04k
    case X86_DEC32m:
2171
1.33k
    case X86_DEC64m:
2172
1.60k
    case X86_DEC8m:
2173
2174
    // ADC
2175
1.92k
    case X86_ADC16mi:
2176
2.24k
    case X86_ADC16mi8:
2177
2.57k
    case X86_ADC16mr:
2178
2.87k
    case X86_ADC32mi:
2179
3.14k
    case X86_ADC32mi8:
2180
3.56k
    case X86_ADC32mr:
2181
4.48k
    case X86_ADC64mi32:
2182
4.70k
    case X86_ADC64mi8:
2183
5.36k
    case X86_ADC64mr:
2184
5.63k
    case X86_ADC8mi:
2185
5.97k
    case X86_ADC8mi8:
2186
6.60k
    case X86_ADC8mr:
2187
6.91k
    case X86_ADC8rm:
2188
7.23k
    case X86_ADC16rm:
2189
7.55k
    case X86_ADC32rm:
2190
7.78k
    case X86_ADC64rm:
2191
2192
    // ADD
2193
8.25k
    case X86_ADD16mi:
2194
8.60k
    case X86_ADD16mi8:
2195
9.09k
    case X86_ADD16mr:
2196
9.39k
    case X86_ADD32mi:
2197
9.70k
    case X86_ADD32mi8:
2198
10.5k
    case X86_ADD32mr:
2199
10.7k
    case X86_ADD64mi32:
2200
11.0k
    case X86_ADD64mi8:
2201
11.3k
    case X86_ADD64mr:
2202
11.7k
    case X86_ADD8mi:
2203
12.0k
    case X86_ADD8mi8:
2204
13.5k
    case X86_ADD8mr:
2205
13.8k
    case X86_ADD8rm:
2206
14.1k
    case X86_ADD16rm:
2207
14.4k
    case X86_ADD32rm:
2208
15.0k
    case X86_ADD64rm:
2209
2210
    // AND
2211
15.4k
    case X86_AND16mi:
2212
15.8k
    case X86_AND16mi8:
2213
16.3k
    case X86_AND16mr:
2214
16.5k
    case X86_AND32mi:
2215
16.8k
    case X86_AND32mi8:
2216
17.2k
    case X86_AND32mr:
2217
17.4k
    case X86_AND64mi32:
2218
17.7k
    case X86_AND64mi8:
2219
18.2k
    case X86_AND64mr:
2220
18.7k
    case X86_AND8mi:
2221
19.4k
    case X86_AND8mi8:
2222
20.0k
    case X86_AND8mr:
2223
20.3k
    case X86_AND8rm:
2224
20.6k
    case X86_AND16rm:
2225
21.0k
    case X86_AND32rm:
2226
21.5k
    case X86_AND64rm:
2227
2228
    // BTC
2229
21.8k
    case X86_BTC16mi8:
2230
22.0k
    case X86_BTC16mr:
2231
22.3k
    case X86_BTC32mi8:
2232
22.8k
    case X86_BTC32mr:
2233
23.0k
    case X86_BTC64mi8:
2234
23.2k
    case X86_BTC64mr:
2235
2236
    // BTR
2237
23.3k
    case X86_BTR16mi8:
2238
23.9k
    case X86_BTR16mr:
2239
24.2k
    case X86_BTR32mi8:
2240
24.5k
    case X86_BTR32mr:
2241
24.7k
    case X86_BTR64mi8:
2242
24.9k
    case X86_BTR64mr:
2243
2244
    // BTS
2245
25.2k
    case X86_BTS16mi8:
2246
25.6k
    case X86_BTS16mr:
2247
26.2k
    case X86_BTS32mi8:
2248
26.5k
    case X86_BTS32mr:
2249
26.9k
    case X86_BTS64mi8:
2250
27.1k
    case X86_BTS64mr:
2251
2252
    // CMPXCHG
2253
27.8k
    case X86_CMPXCHG16B:
2254
28.1k
    case X86_CMPXCHG16rm:
2255
28.6k
    case X86_CMPXCHG32rm:
2256
28.7k
    case X86_CMPXCHG64rm:
2257
29.6k
    case X86_CMPXCHG8rm:
2258
30.0k
    case X86_CMPXCHG8B:
2259
2260
    // INC
2261
30.4k
    case X86_INC16m:
2262
30.7k
    case X86_INC32m:
2263
31.1k
    case X86_INC64m:
2264
31.4k
    case X86_INC8m:
2265
2266
    // NEG
2267
31.8k
    case X86_NEG16m:
2268
32.1k
    case X86_NEG32m:
2269
32.5k
    case X86_NEG64m:
2270
32.6k
    case X86_NEG8m:
2271
2272
    // NOT
2273
33.1k
    case X86_NOT16m:
2274
33.5k
    case X86_NOT32m:
2275
33.8k
    case X86_NOT64m:
2276
34.1k
    case X86_NOT8m:
2277
2278
    // OR
2279
34.6k
    case X86_OR16mi:
2280
34.9k
    case X86_OR16mi8:
2281
35.5k
    case X86_OR16mr:
2282
35.8k
    case X86_OR32mi:
2283
36.1k
    case X86_OR32mi8:
2284
36.5k
    case X86_OR32mr:
2285
37.2k
    case X86_OR64mi32:
2286
37.5k
    case X86_OR64mi8:
2287
37.8k
    case X86_OR64mr:
2288
38.2k
    case X86_OR8mi8:
2289
38.6k
    case X86_OR8mi:
2290
38.9k
    case X86_OR8mr:
2291
39.3k
    case X86_OR8rm:
2292
39.8k
    case X86_OR16rm:
2293
40.1k
    case X86_OR32rm:
2294
40.4k
    case X86_OR64rm:
2295
2296
    // SBB
2297
41.1k
    case X86_SBB16mi:
2298
41.6k
    case X86_SBB16mi8:
2299
41.9k
    case X86_SBB16mr:
2300
42.3k
    case X86_SBB32mi:
2301
42.6k
    case X86_SBB32mi8:
2302
43.0k
    case X86_SBB32mr:
2303
43.2k
    case X86_SBB64mi32:
2304
43.7k
    case X86_SBB64mi8:
2305
44.3k
    case X86_SBB64mr:
2306
44.6k
    case X86_SBB8mi:
2307
44.9k
    case X86_SBB8mi8:
2308
45.2k
    case X86_SBB8mr:
2309
2310
    // SUB
2311
45.5k
    case X86_SUB16mi:
2312
45.8k
    case X86_SUB16mi8:
2313
46.1k
    case X86_SUB16mr:
2314
46.4k
    case X86_SUB32mi:
2315
46.7k
    case X86_SUB32mi8:
2316
47.0k
    case X86_SUB32mr:
2317
47.3k
    case X86_SUB64mi32:
2318
47.9k
    case X86_SUB64mi8:
2319
48.3k
    case X86_SUB64mr:
2320
49.1k
    case X86_SUB8mi8:
2321
49.4k
    case X86_SUB8mi:
2322
50.0k
    case X86_SUB8mr:
2323
50.4k
    case X86_SUB8rm:
2324
50.7k
    case X86_SUB16rm:
2325
51.1k
    case X86_SUB32rm:
2326
51.5k
    case X86_SUB64rm:
2327
2328
    // XADD
2329
51.8k
    case X86_XADD16rm:
2330
52.1k
    case X86_XADD32rm:
2331
52.3k
    case X86_XADD64rm:
2332
52.5k
    case X86_XADD8rm:
2333
2334
    // XCHG
2335
53.0k
    case X86_XCHG16rm:
2336
53.6k
    case X86_XCHG32rm:
2337
54.0k
    case X86_XCHG64rm:
2338
54.3k
    case X86_XCHG8rm:
2339
2340
    // XOR
2341
54.6k
    case X86_XOR16mi:
2342
55.0k
    case X86_XOR16mi8:
2343
55.5k
    case X86_XOR16mr:
2344
55.9k
    case X86_XOR32mi:
2345
56.2k
    case X86_XOR32mi8:
2346
56.7k
    case X86_XOR32mr:
2347
57.0k
    case X86_XOR64mi32:
2348
57.2k
    case X86_XOR64mi8:
2349
57.5k
    case X86_XOR64mr:
2350
57.8k
    case X86_XOR8mi8:
2351
58.1k
    case X86_XOR8mi:
2352
58.9k
    case X86_XOR8mr:
2353
59.9k
    case X86_XOR8rm:
2354
60.2k
    case X86_XOR16rm:
2355
60.7k
    case X86_XOR32rm:
2356
61.3k
    case X86_XOR64rm:
2357
2358
      // this instruction can be used with LOCK prefix
2359
61.3k
      return false;
2360
61.7k
    }
2361
61.7k
  }
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
1.28M
  return false;
2376
1.35M
}
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
1.35M
{
2397
1.35M
  insn->reader = reader;
2398
1.35M
  insn->readerArg = readerArg;
2399
1.35M
  insn->startLocation = startLoc;
2400
1.35M
  insn->readerCursor = startLoc;
2401
1.35M
  insn->mode = mode;
2402
1.35M
  insn->numImmediatesConsumed = 0;
2403
2404
1.35M
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2405
1.35M
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2406
9.13k
    return -1;
2407
2408
1.34M
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2409
2410
  // instruction length must be <= 15 to be valid
2411
1.34M
  if (insn->length > 15)
2412
52
    return -1;
2413
2414
1.34M
  if (insn->operandSize == 0)
2415
1.34M
    insn->operandSize = insn->registerSize;
2416
2417
1.34M
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2418
2419
1.34M
  return 0;
2420
1.34M
}
2421
2422
#endif