Coverage Report

Created: 2025-12-05 06:11

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