Coverage Report

Created: 2025-10-12 06:32

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