Coverage Report

Created: 2025-08-29 06:29

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