Coverage Report

Created: 2026-09-28 06:34

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
813k
{
79
813k
  return CONTEXTS_SYM[attrMask];
80
813k
}
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
813k
{
96
813k
  const struct OpcodeDecision *decision = NULL;
97
813k
  const uint8_t *indextable = NULL;
98
813k
  unsigned int index;
99
100
813k
  switch (type) {
101
0
  default:
102
0
    return false;
103
713k
  case ONEBYTE:
104
713k
    decision = ONEBYTE_SYM;
105
713k
    indextable = index_x86DisassemblerOneByteOpcodes;
106
713k
    break;
107
59.5k
  case TWOBYTE:
108
59.5k
    decision = TWOBYTE_SYM;
109
59.5k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
59.5k
    break;
111
14.1k
  case THREEBYTE_38:
112
14.1k
    decision = THREEBYTE38_SYM;
113
14.1k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
14.1k
    break;
115
18.3k
  case THREEBYTE_3A:
116
18.3k
    decision = THREEBYTE3A_SYM;
117
18.3k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
18.3k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
6.36k
  case XOP8_MAP:
121
6.36k
    decision = XOP8_MAP_SYM;
122
6.36k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
6.36k
    break;
124
577
  case XOP9_MAP:
125
577
    decision = XOP9_MAP_SYM;
126
577
    indextable = index_x86DisassemblerXOP9Opcodes;
127
577
    break;
128
110
  case XOPA_MAP:
129
110
    decision = XOPA_MAP_SYM;
130
110
    indextable = index_x86DisassemblerXOPAOpcodes;
131
110
    break;
132
228
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
228
    return true;
135
813k
#endif
136
813k
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
812k
  index = indextable[insnContext];
140
812k
  if (index)
141
811k
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
811k
           MODRM_ONEENTRY;
143
1.65k
  else
144
1.65k
    return false;
145
812k
}
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
812k
{
160
812k
  const struct ModRMDecision *dec = NULL;
161
812k
  unsigned int index;
162
812k
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
812k
  switch (type) {
165
0
  default:
166
0
    return 0;
167
712k
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
712k
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
712k
    if (index)
171
712k
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
151
    else
173
151
      dec = &emptyDecision.modRMDecisions[opcode];
174
712k
    break;
175
59.4k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
59.4k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
59.4k
    if (index)
179
58.7k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
736
    else
181
736
      dec = &emptyDecision.modRMDecisions[opcode];
182
59.4k
    break;
183
14.1k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
14.1k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
14.1k
    if (index)
187
13.9k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
144
    else
189
144
      dec = &emptyDecision.modRMDecisions[opcode];
190
14.1k
    break;
191
18.3k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
18.3k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
18.3k
    if (index)
195
18.1k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
185
    else
197
185
      dec = &emptyDecision.modRMDecisions[opcode];
198
18.3k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
6.36k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
6.36k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
6.36k
    if (index)
204
6.05k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
315
    else
206
315
      dec = &emptyDecision.modRMDecisions[opcode];
207
6.36k
    break;
208
577
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
577
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
577
    if (index)
212
483
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
94
    else
214
94
      dec = &emptyDecision.modRMDecisions[opcode];
215
577
    break;
216
110
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
110
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
110
    if (index)
220
78
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
32
    else
222
32
      dec = &emptyDecision.modRMDecisions[opcode];
223
110
    break;
224
228
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
228
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
228
    if (index)
228
158
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
158
               .modRMDecisions[opcode];
230
70
    else
231
70
      dec = &emptyDecision.modRMDecisions[opcode];
232
228
    break;
233
812k
#endif
234
812k
  }
235
236
812k
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
396k
  case MODRM_ONEENTRY:
241
396k
    return modRMTable[dec->instructionIDs];
242
313k
  case MODRM_SPLITRM:
243
313k
    if (modFromModRM(modRM) == 0x3)
244
66.2k
      return modRMTable[dec->instructionIDs + 1];
245
246k
    return modRMTable[dec->instructionIDs];
246
87.5k
  case MODRM_SPLITREG:
247
87.5k
    if (modFromModRM(modRM) == 0x3)
248
30.5k
      return modRMTable[dec->instructionIDs +
249
30.5k
            ((modRM & 0x38) >> 3) + 8];
250
57.0k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
14.5k
  case MODRM_SPLITMISC:
252
14.5k
    if (modFromModRM(modRM) == 0x3)
253
4.79k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
4.79k
            8];
255
9.79k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
812k
  }
259
812k
}
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
690k
{
271
690k
  return &INSTRUCTIONS_SYM[uid];
272
690k
}
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
1.91M
{
286
1.91M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
1.91M
  if (!ret)
289
1.91M
    ++(insn->readerCursor);
290
291
1.91M
  return ret;
292
1.91M
}
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
99.9k
{
303
99.9k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
99.9k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
574k
{
308
574k
  insn->readerCursor--;
309
574k
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
123k
  { \
314
123k
    type combined = 0; \
315
123k
    unsigned offset; \
316
414k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
291k
      uint8_t byte; \
318
291k
      int ret = insn->reader(insn->readerArg, &byte, \
319
291k
                 insn->readerCursor + offset); \
320
291k
      if (ret) \
321
291k
        return ret; \
322
291k
      combined = combined | \
323
291k
           ((uint64_t)byte << (offset * 8)); \
324
291k
    } \
325
123k
    *ptr = combined; \
326
122k
    insn->readerCursor += sizeof(type); \
327
122k
    return 0; \
328
123k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
45.6k
  { \
314
45.6k
    type combined = 0; \
315
45.6k
    unsigned offset; \
316
91.1k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
45.6k
      uint8_t byte; \
318
45.6k
      int ret = insn->reader(insn->readerArg, &byte, \
319
45.6k
                 insn->readerCursor + offset); \
320
45.6k
      if (ret) \
321
45.6k
        return ret; \
322
45.6k
      combined = combined | \
323
45.5k
           ((uint64_t)byte << (offset * 8)); \
324
45.5k
    } \
325
45.6k
    *ptr = combined; \
326
45.5k
    insn->readerCursor += sizeof(type); \
327
45.5k
    return 0; \
328
45.6k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
13.1k
  { \
314
13.1k
    type combined = 0; \
315
13.1k
    unsigned offset; \
316
39.5k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
26.3k
      uint8_t byte; \
318
26.3k
      int ret = insn->reader(insn->readerArg, &byte, \
319
26.3k
                 insn->readerCursor + offset); \
320
26.3k
      if (ret) \
321
26.3k
        return ret; \
322
26.3k
      combined = combined | \
323
26.3k
           ((uint64_t)byte << (offset * 8)); \
324
26.3k
    } \
325
13.1k
    *ptr = combined; \
326
13.1k
    insn->readerCursor += sizeof(type); \
327
13.1k
    return 0; \
328
13.1k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
18.9k
  { \
314
18.9k
    type combined = 0; \
315
18.9k
    unsigned offset; \
316
94.4k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
75.6k
      uint8_t byte; \
318
75.6k
      int ret = insn->reader(insn->readerArg, &byte, \
319
75.6k
                 insn->readerCursor + offset); \
320
75.6k
      if (ret) \
321
75.6k
        return ret; \
322
75.6k
      combined = combined | \
323
75.4k
           ((uint64_t)byte << (offset * 8)); \
324
75.4k
    } \
325
18.9k
    *ptr = combined; \
326
18.8k
    insn->readerCursor += sizeof(type); \
327
18.8k
    return 0; \
328
18.9k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
23.2k
  { \
314
23.2k
    type combined = 0; \
315
23.2k
    unsigned offset; \
316
69.5k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
46.4k
      uint8_t byte; \
318
46.4k
      int ret = insn->reader(insn->readerArg, &byte, \
319
46.4k
                 insn->readerCursor + offset); \
320
46.4k
      if (ret) \
321
46.4k
        return ret; \
322
46.4k
      combined = combined | \
323
46.3k
           ((uint64_t)byte << (offset * 8)); \
324
46.3k
    } \
325
23.2k
    *ptr = combined; \
326
23.1k
    insn->readerCursor += sizeof(type); \
327
23.1k
    return 0; \
328
23.2k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
19.2k
  { \
314
19.2k
    type combined = 0; \
315
19.2k
    unsigned offset; \
316
95.8k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
76.7k
      uint8_t byte; \
318
76.7k
      int ret = insn->reader(insn->readerArg, &byte, \
319
76.7k
                 insn->readerCursor + offset); \
320
76.7k
      if (ret) \
321
76.7k
        return ret; \
322
76.7k
      combined = combined | \
323
76.5k
           ((uint64_t)byte << (offset * 8)); \
324
76.5k
    } \
325
19.2k
    *ptr = combined; \
326
19.0k
    insn->readerCursor += sizeof(type); \
327
19.0k
    return 0; \
328
19.2k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
2.67k
  { \
314
2.67k
    type combined = 0; \
315
2.67k
    unsigned offset; \
316
23.7k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
21.1k
      uint8_t byte; \
318
21.1k
      int ret = insn->reader(insn->readerArg, &byte, \
319
21.1k
                 insn->readerCursor + offset); \
320
21.1k
      if (ret) \
321
21.1k
        return ret; \
322
21.1k
      combined = combined | \
323
21.0k
           ((uint64_t)byte << (offset * 8)); \
324
21.0k
    } \
325
2.67k
    *ptr = combined; \
326
2.61k
    insn->readerCursor += sizeof(type); \
327
2.61k
    return 0; \
328
2.67k
  }
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
671k
{
349
671k
  if (insn->mode == MODE_64BIT)
350
265k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
405k
  return false;
353
671k
}
354
355
/*
356
 * setGroup0Prefix - Updates the decoded instruction according to the group 0-prefix.
357
 *
358
 * @param insn      - The instruction to be updated.
359
 * @param prefix    - The group 0 prefix that is present.
360
 */
361
static void setGroup0Prefix(struct InternalInstruction *insn, uint8_t prefix)
362
58.0k
{
363
58.0k
  switch (prefix) {
364
22.8k
  case 0xf0: // LOCK
365
22.8k
    insn->hasLockPrefix = true;
366
22.8k
    break;
367
368
20.5k
  case 0xf2: // REPNE/REPNZ
369
35.2k
  case 0xf3: // REP or REPE/REPZ
370
35.2k
    insn->repeatPrefix = prefix;
371
35.2k
    break;
372
58.0k
  }
373
58.0k
}
374
375
/*
376
 * setSegmentOverride - Overrides an instruction's prefix1 based on CPU mode.
377
 *
378
 * @param insn      - The instruction to be overridden.
379
 * @param prefix    - The segment override to use.
380
 * @param byte      - The current decoded prefix byte. Must be a segment override.
381
 */
382
static void setSegmentOverride(struct InternalInstruction *insn,
383
             SegmentOverride prefix, uint8_t byte)
384
19.6k
{
385
  // In 32-bit or 16-bit mode all segment override prefixes are used.
386
19.6k
  if (insn->mode != MODE_64BIT) {
387
10.6k
    insn->segmentOverride = prefix;
388
10.6k
    insn->prefix1 = byte;
389
10.6k
    return;
390
10.6k
  }
391
392
  // In 64-bit mode, the ES/CS/SS/DS segment overrides should be ignored.
393
  // In the case there are multiple segment overrides, do not override
394
  // an existing FS or GS segment prefix.
395
8.97k
  switch (insn->prefix1) {
396
2.27k
  case 0x64: // FS
397
2.87k
  case 0x65: // GS
398
2.87k
    return;
399
8.97k
  }
400
401
  // If the proposed override is for FS or GS, mark it overridden.
402
  // All other segment prefixes are ignored.
403
6.09k
  switch (byte) {
404
1.13k
  case 0x64: // FS
405
2.61k
  case 0x65: // GS
406
2.61k
    insn->segmentOverride = prefix;
407
2.61k
    break;
408
6.09k
  }
409
410
  // `prefix1` may later be used to decode the `notrack` prefix.
411
  // The `notrack` prefix reuses the DS segment override, so we
412
  // need to store the prefix even if it is ignored for the segment overrides.
413
6.09k
  insn->prefix1 = byte;
414
6.09k
}
415
416
/*
417
 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
418
 *   instruction as having them.  Also sets the instruction's default operand,
419
 *   address, and other relevant data sizes to report operands correctly.
420
 *
421
 * @param insn  - The instruction whose prefixes are to be read.
422
 * @return      - 0 if the instruction could be read until the end of the prefix
423
 *                bytes, and no prefixes conflicted; nonzero otherwise.
424
 */
425
static int readPrefixes(struct InternalInstruction *insn)
426
605k
{
427
605k
  bool isPrefix = true;
428
605k
  uint8_t byte = 0;
429
605k
  uint8_t nextByte;
430
431
1.34M
  while (isPrefix) {
432
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
433
744k
    if (consumeByte(insn, &byte))
434
196
      return -1;
435
436
744k
    if (insn->readerCursor - 1 == insn->startLocation &&
437
605k
        (byte == 0xf2 || byte == 0xf3)) {
438
      // prefix requires next byte
439
25.7k
      if (lookAtByte(insn, &nextByte))
440
31
        return -1;
441
442
25.6k
      if (isREX(insn, nextByte)) {
443
2.32k
        uint8_t nnextByte;
444
445
        // Go to REX prefix after the current one
446
2.32k
        if (consumeByte(insn, &nnextByte))
447
0
          return -1;
448
449
        // We should be able to read next byte after REX prefix
450
2.32k
        if (lookAtByte(insn, &nnextByte))
451
6
          return -1;
452
453
2.31k
        unconsumeByte(insn);
454
2.31k
      }
455
25.6k
    }
456
457
744k
    switch (byte) {
458
22.8k
    case 0xf0: /* LOCK */
459
43.4k
    case 0xf2: /* REPNE/REPNZ */
460
58.0k
    case 0xf3: /* REP or REPE/REPZ */
461
      // only accept the last prefix
462
58.0k
      setGroup0Prefix(insn, byte);
463
58.0k
      insn->prefix0 = byte;
464
58.0k
      insn->rexPrefix = 0;
465
58.0k
      break;
466
467
3.79k
    case 0x2e: /* CS segment override -OR- Branch not taken */
468
6.10k
    case 0x36: /* SS segment override -OR- Branch taken */
469
8.25k
    case 0x3e: /* DS segment override */
470
12.0k
    case 0x26: /* ES segment override */
471
15.6k
    case 0x64: /* FS segment override */
472
19.6k
    case 0x65: /* GS segment override */
473
19.6k
      switch (byte) {
474
3.79k
      case 0x2e:
475
3.79k
        setSegmentOverride(insn, SEG_OVERRIDE_CS, byte);
476
3.79k
        break;
477
2.31k
      case 0x36:
478
2.31k
        setSegmentOverride(insn, SEG_OVERRIDE_SS, byte);
479
2.31k
        break;
480
2.15k
      case 0x3e:
481
2.15k
        setSegmentOverride(insn, SEG_OVERRIDE_DS, byte);
482
2.15k
        break;
483
3.77k
      case 0x26:
484
3.77k
        setSegmentOverride(insn, SEG_OVERRIDE_ES, byte);
485
3.77k
        break;
486
3.59k
      case 0x64:
487
3.59k
        setSegmentOverride(insn, SEG_OVERRIDE_FS, byte);
488
3.59k
        break;
489
4.03k
      case 0x65:
490
4.03k
        setSegmentOverride(insn, SEG_OVERRIDE_GS, byte);
491
4.03k
        break;
492
0
      default:
493
        // debug("Unhandled override");
494
0
        return -1;
495
19.6k
      }
496
19.6k
      insn->rexPrefix = 0;
497
19.6k
      break;
498
499
15.4k
    case 0x66: /* Operand-size override */
500
15.4k
      insn->hasOpSize = true;
501
15.4k
      insn->prefix2 = byte;
502
15.4k
      insn->rexPrefix = 0;
503
15.4k
      break;
504
505
5.08k
    case 0x67: /* Address-size override */
506
5.08k
      insn->hasAdSize = true;
507
5.08k
      insn->prefix3 = byte;
508
5.08k
      insn->rexPrefix = 0;
509
5.08k
      break;
510
645k
    default:
511
645k
      if (isREX(insn, byte)) {
512
        /* REX prefix byte */
513
40.3k
        insn->rexPrefix = byte;
514
605k
      } else {
515
        /* Not a prefix byte */
516
605k
        isPrefix = false;
517
605k
      }
518
645k
      break;
519
744k
    }
520
744k
  }
521
522
605k
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
523
524
605k
  if (byte == 0x62) {
525
27.4k
    uint8_t byte1, byte2;
526
527
27.4k
    if (consumeByte(insn, &byte1)) {
528
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
529
41
      return -1;
530
41
    }
531
532
27.4k
    if (lookAtByte(insn, &byte2)) {
533
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
534
28
      unconsumeByte(insn); /* unconsume byte1 */
535
28
      unconsumeByte(insn); /* unconsume byte  */
536
27.4k
    } else {
537
27.4k
      if ((insn->mode == MODE_64BIT ||
538
16.5k
           (byte1 & 0xc0) == 0xc0) &&
539
24.4k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
540
24.3k
        insn->vectorExtensionType = TYPE_EVEX;
541
24.3k
      } else {
542
3.05k
        unconsumeByte(insn); /* unconsume byte1 */
543
3.05k
        unconsumeByte(insn); /* unconsume byte  */
544
3.05k
      }
545
27.4k
    }
546
547
27.4k
    if (insn->vectorExtensionType == TYPE_EVEX) {
548
24.3k
      insn->vectorExtensionPrefix[0] = byte;
549
24.3k
      insn->vectorExtensionPrefix[1] = byte1;
550
24.3k
      if (consumeByte(insn,
551
24.3k
          &insn->vectorExtensionPrefix[2])) {
552
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
553
0
        return -1;
554
0
      }
555
556
24.3k
      if (consumeByte(insn,
557
24.3k
          &insn->vectorExtensionPrefix[3])) {
558
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
559
13
        return -1;
560
13
      }
561
562
      /* We simulate the REX prefix for simplicity's sake */
563
24.3k
      if (insn->mode == MODE_64BIT) {
564
10.8k
        insn->rexPrefix =
565
10.8k
          0x40 |
566
10.8k
          (wFromEVEX3of4(
567
10.8k
             insn->vectorExtensionPrefix[2])
568
10.8k
           << 3) |
569
10.8k
          (rFromEVEX2of4(
570
10.8k
             insn->vectorExtensionPrefix[1])
571
10.8k
           << 2) |
572
10.8k
          (xFromEVEX2of4(
573
10.8k
             insn->vectorExtensionPrefix[1])
574
10.8k
           << 1) |
575
10.8k
          (bFromEVEX2of4(
576
10.8k
             insn->vectorExtensionPrefix[1])
577
10.8k
           << 0);
578
10.8k
      }
579
580
      // dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
581
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
582
      //    insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
583
24.3k
    }
584
577k
  } else if (byte == 0xc4) {
585
3.04k
    uint8_t byte1;
586
587
3.04k
    if (lookAtByte(insn, &byte1)) {
588
      // dbgprintf(insn, "Couldn't read second byte of VEX");
589
9
      return -1;
590
9
    }
591
592
3.03k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
593
2.15k
      insn->vectorExtensionType = TYPE_VEX_3B;
594
880
    else
595
880
      unconsumeByte(insn);
596
597
3.03k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
598
2.15k
      insn->vectorExtensionPrefix[0] = byte;
599
2.15k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
600
2.15k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
601
602
      /* We simulate the REX prefix for simplicity's sake */
603
2.15k
      if (insn->mode == MODE_64BIT)
604
851
        insn->rexPrefix =
605
851
          0x40 |
606
851
          (wFromVEX3of3(
607
851
             insn->vectorExtensionPrefix[2])
608
851
           << 3) |
609
851
          (rFromVEX2of3(
610
851
             insn->vectorExtensionPrefix[1])
611
851
           << 2) |
612
851
          (xFromVEX2of3(
613
851
             insn->vectorExtensionPrefix[1])
614
851
           << 1) |
615
851
          (bFromVEX2of3(
616
851
             insn->vectorExtensionPrefix[1])
617
851
           << 0);
618
619
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
620
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
621
      //    insn->vectorExtensionPrefix[2]);
622
2.15k
    }
623
574k
  } else if (byte == 0xc5) {
624
7.24k
    uint8_t byte1;
625
626
7.24k
    if (lookAtByte(insn, &byte1)) {
627
      // dbgprintf(insn, "Couldn't read second byte of VEX");
628
26
      return -1;
629
26
    }
630
631
7.21k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
632
6.04k
      insn->vectorExtensionType = TYPE_VEX_2B;
633
1.17k
    else
634
1.17k
      unconsumeByte(insn);
635
636
7.21k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
637
6.04k
      insn->vectorExtensionPrefix[0] = byte;
638
6.04k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
639
640
6.04k
      if (insn->mode == MODE_64BIT)
641
1.56k
        insn->rexPrefix =
642
1.56k
          0x40 |
643
1.56k
          (rFromVEX2of2(
644
1.56k
             insn->vectorExtensionPrefix[1])
645
1.56k
           << 2);
646
647
6.04k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
648
2.95k
      default:
649
2.95k
        break;
650
3.08k
      case VEX_PREFIX_66:
651
3.08k
        insn->hasOpSize = true;
652
3.08k
        break;
653
6.04k
      }
654
655
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
656
      //    insn->vectorExtensionPrefix[0],
657
      //    insn->vectorExtensionPrefix[1]);
658
6.04k
    }
659
567k
  } else if (byte == 0x8f) {
660
4.37k
    uint8_t byte1;
661
662
4.37k
    if (lookAtByte(insn, &byte1)) {
663
      // dbgprintf(insn, "Couldn't read second byte of XOP");
664
11
      return -1;
665
11
    }
666
667
4.36k
    if ((byte1 & 0x38) !=
668
4.36k
        0x0) /* 0 in these 3 bits is a POP instruction. */
669
4.10k
      insn->vectorExtensionType = TYPE_XOP;
670
259
    else
671
259
      unconsumeByte(insn);
672
673
4.36k
    if (insn->vectorExtensionType == TYPE_XOP) {
674
4.10k
      insn->vectorExtensionPrefix[0] = byte;
675
4.10k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
676
4.10k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
677
678
      /* We simulate the REX prefix for simplicity's sake */
679
4.10k
      if (insn->mode == MODE_64BIT)
680
1.54k
        insn->rexPrefix =
681
1.54k
          0x40 |
682
1.54k
          (wFromXOP3of3(
683
1.54k
             insn->vectorExtensionPrefix[2])
684
1.54k
           << 3) |
685
1.54k
          (rFromXOP2of3(
686
1.54k
             insn->vectorExtensionPrefix[1])
687
1.54k
           << 2) |
688
1.54k
          (xFromXOP2of3(
689
1.54k
             insn->vectorExtensionPrefix[1])
690
1.54k
           << 1) |
691
1.54k
          (bFromXOP2of3(
692
1.54k
             insn->vectorExtensionPrefix[1])
693
1.54k
           << 0);
694
695
4.10k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
696
4.09k
      default:
697
4.09k
        break;
698
4.09k
      case VEX_PREFIX_66:
699
9
        insn->hasOpSize = true;
700
9
        break;
701
4.10k
      }
702
703
      // dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
704
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
705
      //    insn->vectorExtensionPrefix[2]);
706
4.10k
    }
707
4.36k
  } else
708
563k
    unconsumeByte(insn);
709
710
605k
  if (insn->repeatPrefix != 0) {
711
29.8k
    if (lookAtByte(insn, &nextByte))
712
2
      return -1;
713
714
    /*
715
    * REP prefix is present, and any of the following conditions are
716
    * met:
717
    * - it is followed by a LOCK (0xf0) prefix
718
    * - it is followed by an xchg instruction (except for 0x90 - NOP/PAUSE)
719
    * then it should be disassembled as a xacquire/xrelease not repne/rep.
720
    */
721
29.8k
    if ((insn->hasLockPrefix || ((nextByte & 0xfe) == 0x86 ||
722
29.3k
               (nextByte & 0xf8) == 0x90)) &&
723
1.18k
        nextByte != 0x90) {
724
653
      insn->xAcquireRelease = insn->repeatPrefix;
725
653
    }
726
727
    /*
728
    * Also if the REP prefix is 0xf3, and the following condition is met:
729
    * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
730
    *                       "mov mem, imm" (opcode 0xc6/0xc7) instructions.
731
    * then it should be disassembled as an xrelease not rep.
732
    */
733
29.8k
    if (insn->repeatPrefix == 0xf3 &&
734
13.6k
        (nextByte == 0x88 || nextByte == 0x89 || nextByte == 0xc6 ||
735
13.2k
         nextByte == 0xc7)) {
736
460
      insn->xAcquireRelease = insn->repeatPrefix;
737
460
    }
738
29.8k
  }
739
740
605k
  if (insn->mode == MODE_16BIT) {
741
193k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
742
193k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
743
193k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
744
193k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
745
193k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
746
411k
  } else if (insn->mode == MODE_32BIT) {
747
195k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
748
195k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
749
195k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
750
195k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
751
195k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
752
216k
  } else if (insn->mode == MODE_64BIT) {
753
216k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
754
28.2k
      insn->registerSize = 8;
755
28.2k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
756
28.2k
      insn->displacementSize = 4;
757
28.2k
      insn->immediateSize = 4;
758
28.2k
      insn->immSize = 4;
759
187k
    } else {
760
187k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
761
187k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
762
187k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
763
187k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
764
187k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
765
187k
    }
766
216k
  }
767
768
605k
  return 0;
769
605k
}
770
771
static int readModRM(struct InternalInstruction *insn);
772
773
/*
774
 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
775
 *   extended or escape opcodes).
776
 *
777
 * @param insn  - The instruction whose opcode is to be read.
778
 * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
779
 */
780
static int readOpcode(struct InternalInstruction *insn)
781
605k
{
782
605k
  uint8_t current;
783
784
  // dbgprintf(insn, "readOpcode()");
785
786
605k
  insn->opcodeType = ONEBYTE;
787
788
605k
  if (insn->vectorExtensionType == TYPE_EVEX) {
789
24.3k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
790
2
    default:
791
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
792
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
793
2
      return -1;
794
7.03k
    case VEX_LOB_0F:
795
7.03k
      insn->opcodeType = TWOBYTE;
796
7.03k
      return consumeByte(insn, &insn->opcode);
797
7.90k
    case VEX_LOB_0F38:
798
7.90k
      insn->opcodeType = THREEBYTE_38;
799
7.90k
      return consumeByte(insn, &insn->opcode);
800
9.39k
    case VEX_LOB_0F3A:
801
9.39k
      insn->opcodeType = THREEBYTE_3A;
802
9.39k
      return consumeByte(insn, &insn->opcode);
803
24.3k
    }
804
580k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
805
2.15k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
806
21
    default:
807
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
808
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
809
21
      return -1;
810
83
    case VEX_LOB_0F:
811
      //insn->twoByteEscape = 0x0f;
812
83
      insn->opcodeType = TWOBYTE;
813
83
      return consumeByte(insn, &insn->opcode);
814
1.20k
    case VEX_LOB_0F38:
815
      //insn->twoByteEscape = 0x0f;
816
1.20k
      insn->opcodeType = THREEBYTE_38;
817
1.20k
      return consumeByte(insn, &insn->opcode);
818
844
    case VEX_LOB_0F3A:
819
      //insn->twoByteEscape = 0x0f;
820
844
      insn->opcodeType = THREEBYTE_3A;
821
844
      return consumeByte(insn, &insn->opcode);
822
2.15k
    }
823
578k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
824
    //insn->twoByteEscape = 0x0f;
825
6.04k
    insn->opcodeType = TWOBYTE;
826
6.04k
    return consumeByte(insn, &insn->opcode);
827
572k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
828
4.10k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
829
19
    default:
830
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
831
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
832
19
      return -1;
833
3.79k
    case XOP_MAP_SELECT_8:
834
3.79k
      insn->opcodeType = XOP8_MAP;
835
3.79k
      return consumeByte(insn, &insn->opcode);
836
255
    case XOP_MAP_SELECT_9:
837
255
      insn->opcodeType = XOP9_MAP;
838
255
      return consumeByte(insn, &insn->opcode);
839
33
    case XOP_MAP_SELECT_A:
840
33
      insn->opcodeType = XOPA_MAP;
841
33
      return consumeByte(insn, &insn->opcode);
842
4.10k
    }
843
4.10k
  }
844
845
568k
  if (consumeByte(insn, &current))
846
0
    return -1;
847
848
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
849
568k
  insn->firstByte = current;
850
851
568k
  if (current == 0x0f) {
852
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
853
28.1k
    insn->twoByteEscape = current;
854
855
28.1k
    if (consumeByte(insn, &current))
856
52
      return -1;
857
858
28.1k
    if (current == 0x38) {
859
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
860
1.00k
      if (consumeByte(insn, &current))
861
2
        return -1;
862
863
1.00k
      insn->opcodeType = THREEBYTE_38;
864
27.1k
    } else if (current == 0x3a) {
865
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
866
207
      if (consumeByte(insn, &current))
867
0
        return -1;
868
869
207
      insn->opcodeType = THREEBYTE_3A;
870
26.9k
    } else if (current == 0x0f) {
871
      // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
872
      // Consume operands before the opcode to comply with the 3DNow encoding
873
134
      if (readModRM(insn))
874
5
        return -1;
875
876
129
      if (consumeByte(insn, &current))
877
3
        return -1;
878
879
126
      insn->opcodeType = THREEDNOW_MAP;
880
26.7k
    } else {
881
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
882
26.7k
      insn->opcodeType = TWOBYTE;
883
26.7k
    }
884
28.1k
  }
885
886
  /*
887
   * At this point we have consumed the full opcode.
888
   * Anything we consume from here on must be unconsumed.
889
   */
890
891
568k
  insn->opcode = current;
892
893
568k
  return 0;
894
568k
}
895
896
// Hacky for FEMMS
897
#define GET_INSTRINFO_ENUM
898
#ifndef CAPSTONE_X86_REDUCE
899
#include "X86GenInstrInfo.inc"
900
#else
901
#include "X86GenInstrInfo_reduce.inc"
902
#endif
903
904
/*
905
 * getIDWithAttrMask - Determines the ID of an instruction, consuming
906
 *   the ModR/M byte as appropriate for extended and escape opcodes,
907
 *   and using a supplied attribute mask.
908
 *
909
 * @param instructionID - A pointer whose target is filled in with the ID of the
910
 *                        instruction.
911
 * @param insn          - The instruction whose ID is to be determined.
912
 * @param attrMask      - The attribute mask to search.
913
 * @return              - 0 if the ModR/M could be read when needed or was not
914
 *                        needed; nonzero otherwise.
915
 */
916
static int getIDWithAttrMask(uint16_t *instructionID,
917
           struct InternalInstruction *insn,
918
           uint16_t attrMask)
919
813k
{
920
813k
  bool hasModRMExtension;
921
922
813k
  InstructionContext instructionClass = contextForAttrs(attrMask);
923
924
813k
  hasModRMExtension =
925
813k
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
926
927
813k
  if (hasModRMExtension) {
928
416k
    if (readModRM(insn))
929
1.01k
      return -1;
930
931
415k
    *instructionID = decode(insn->opcodeType, instructionClass,
932
415k
          insn->opcode, insn->modRM);
933
415k
  } else {
934
396k
    *instructionID = decode(insn->opcodeType, instructionClass,
935
396k
          insn->opcode, 0);
936
396k
  }
937
938
812k
  return 0;
939
813k
}
940
941
/*
942
 * is16BitEquivalent - Determines whether two instruction names refer to
943
 * equivalent instructions but one is 16-bit whereas the other is not.
944
 *
945
 * @param orig  - The instruction ID that is not 16-bit
946
 * @param equiv - The instruction ID that is 16-bit
947
 */
948
static bool is16BitEquivalent(unsigned orig, unsigned equiv)
949
185k
{
950
185k
  size_t i;
951
185k
  uint16_t idx;
952
953
185k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
954
90.9k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
955
90.9k
          x86_16_bit_eq_tbl[i].first == orig;
956
88.8k
         i++) {
957
88.8k
      if (x86_16_bit_eq_tbl[i].second == equiv)
958
86.6k
        return true;
959
88.8k
    }
960
88.8k
  }
961
962
98.3k
  return false;
963
185k
}
964
965
/*
966
 * is64Bit - Determines whether this instruction is a 64-bit instruction.
967
 *
968
 * @param name - The instruction that is not 16-bit
969
 */
970
static bool is64Bit(uint16_t id)
971
8.78k
{
972
8.78k
  unsigned int i = find_insn(id);
973
8.78k
  if (i != -1) {
974
8.72k
    return insns[i].is64bit;
975
8.72k
  }
976
977
  // not found??
978
55
  return false;
979
8.78k
}
980
981
typedef enum {
982
  DO_NOT_RESOLVE = 0,
983
  IGNORE_REP = 1,
984
  IGNORE_DATA_SIZE = 2,
985
} MandatoryPrefixResolution;
986
987
/*
988
 * shouldResolveMandatoryPrefixConflict - Resolves conflicts between the 
989
 * data size override prefix and the REP/REPNZ prefixes in the attribute 
990
 * mask when needed.
991
 *
992
 * We need to resolve these conflicts, because the TableGen lookups we 
993
 * perform distinguish between instructions with and without REP.
994
 * For example, there may be an entry for SHLD with a DATA16 data size 
995
 * override prefix, but no entry for REP + DATA16.
996
 * These entries are split, because in some cases the REP and DATA16
997
 * prefixes are used as mandatory prefixes.
998
 * When both are mandatory prefixes, the effect of prefixing both 
999
 * to an instruction at the same time is not specified by
1000
 * reference manuals.
1001
 *
1002
 * Conflicts are resolved by one of these three resolutions:
1003
 *   - If conflicts should not be resolved, take no action.
1004
 *   - If conflicts should be resolved and the instruction has no 
1005
 *     mandatory prefixes, resolves in favor of data size override.
1006
 *   - If conflicts should be resolved and the instruction has mandatory 
1007
 *     prefixes, resolves in favor of REP/REPNZ.
1008
 * 
1009
 * @param insn - The instruction
1010
 * @param attrMask - The current attribute mask.
1011
 */
1012
static uint16_t resolveMandatoryPrefixConflict(struct InternalInstruction *insn,
1013
                 uint16_t attrMask)
1014
1.71k
{
1015
1.71k
  MandatoryPrefixResolution resolution = DO_NOT_RESOLVE;
1016
1017
  // We inspect the opcode map and opcode to determine how we need to resolve
1018
  // a mandatory prefix conflict.
1019
1.71k
  switch (insn->opcodeType) {
1020
  // No one-byte opcodes have mandatory prefixes.
1021
239
  case ONEBYTE:
1022
239
    resolution = DO_NOT_RESOLVE;
1023
239
    break;
1024
1.21k
  case TWOBYTE:
1025
    // Exceptions for instructions that operate on data size-overridable
1026
    // operands.
1027
1.21k
    if (
1028
      // XADD
1029
1.21k
      (insn->opcode & 0xFE) == 0xC0
1030
1031
      // BSWAP
1032
1.05k
      || (insn->opcode & 0xF8) == 0xC8
1033
1034
      // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX
1035
801
      || (insn->opcode & 0xF8) == 0xB0
1036
1037
      // Group 16, various NOPs
1038
717
      || (insn->opcode & 0xF8) == 0x18
1039
1040
      // UD0
1041
695
      || insn->opcode == 0xFF) {
1042
612
      resolution = IGNORE_REP;
1043
612
      break;
1044
612
    }
1045
1046
    // We inspect the instruction to determine if it operates on xmm
1047
    // registers or general-purpose registers.
1048
    //
1049
    // If it operates on general purpose registers, the data size override
1050
    // prefix is not a mandatory prefix and should not be ignored.
1051
    // In most cases, this also means that the REP prefix is not a mandatory
1052
    // prefix and should be ignored.
1053
    //
1054
    // If the instruction operates on xmm registers, the data size override
1055
    // is used to select the operation type (SS, SD, PS, or PD).
1056
    // In this case, the REP prefixes take priority over the data size [1]
1057
    // override prefixes, and when both are present the data size override
1058
    // prefix should be ignored.
1059
    //
1060
    // The exception is 0xB0, where the REP prefixes are mandatory prefixes
1061
    // but the data size override prefix should still be respected.
1062
    // For this case we return DO_NOT_RESOLVE, which returns attrMask as-is.
1063
    //
1064
    // [1]: https://stackoverflow.com/a/7197365
1065
602
    switch (insn->opcode & 0xf0) {
1066
55
    case 0x10:
1067
75
    case 0x50:
1068
175
    case 0x60:
1069
192
    case 0x70:
1070
211
    case 0xC0:
1071
221
    case 0xD0:
1072
224
    case 0xE0:
1073
228
    case 0xF0:
1074
228
      resolution = IGNORE_DATA_SIZE;
1075
228
      break;
1076
29
    case 0x00:
1077
31
    case 0x20:
1078
146
    case 0x30:
1079
156
    case 0x40:
1080
282
    case 0x80:
1081
285
    case 0x90:
1082
364
    case 0xA0:
1083
364
      resolution = IGNORE_REP;
1084
364
      break;
1085
10
    default: // 0xB0
1086
10
      resolution = DO_NOT_RESOLVE;
1087
10
      break;
1088
602
    }
1089
602
    break;
1090
602
  case THREEBYTE_38:
1091
    // Exception: the ADOX and CRC32 instructions.
1092
    // These ignore the data size override prefix even though they
1093
    // operate on general-purpose registers.
1094
201
    if ((insn->opcode & 0xF0) == 0xF0) {
1095
127
      resolution = IGNORE_DATA_SIZE;
1096
127
      break;
1097
127
    }
1098
1099
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1100
    // or separately specified.
1101
74
    resolution = DO_NOT_RESOLVE;
1102
74
    break;
1103
0
  case THREEBYTE_3A:
1104
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1105
    // or separately specified.
1106
0
    resolution = DO_NOT_RESOLVE;
1107
0
    break;
1108
0
  case XOP8_MAP:
1109
0
  case XOP9_MAP:
1110
0
  case XOPA_MAP:
1111
    // These instructions do not appear to operate on XMM/SSE registers,
1112
    // so the REP prefixes can be safely ignored.
1113
0
    resolution = IGNORE_REP;
1114
0
    break;
1115
59
  case THREEDNOW_MAP:
1116
    // AMD Reference Manual Volume 3, Section 1.2.1, states that all
1117
    // 3DNow! instructions ignore the data size override prefix.
1118
59
    resolution = IGNORE_DATA_SIZE;
1119
59
    break;
1120
1.71k
  }
1121
1122
1.71k
  switch (resolution) {
1123
976
  case IGNORE_REP:
1124
976
    return attrMask & ~(ATTR_XD | ATTR_XS);
1125
414
  case IGNORE_DATA_SIZE:
1126
414
    return attrMask & ~ATTR_OPSIZE;
1127
0
  default:
1128
323
  case DO_NOT_RESOLVE:
1129
323
    return attrMask;
1130
1.71k
  }
1131
1.71k
}
1132
1133
/*
1134
 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
1135
 *   appropriate for extended and escape opcodes.  Determines the attributes and
1136
 *   context for the instruction before doing so.
1137
 *
1138
 * @param insn  - The instruction whose ID is to be determined.
1139
 * @return      - 0 if the ModR/M could be read when needed or was not needed;
1140
 *                nonzero otherwise.
1141
 */
1142
static int getID(struct InternalInstruction *insn, cs_mode mode)
1143
605k
{
1144
605k
  uint16_t attrMask;
1145
605k
  uint16_t instructionID;
1146
605k
  bool rexWOverridesOpSize;
1147
1148
  /* REX.W overrides the operand-sized prefix for near RET in 64-bit mode */
1149
605k
  rexWOverridesOpSize = insn->mode == MODE_64BIT && insn->hasOpSize &&
1150
8.97k
            insn->opcodeType == ONEBYTE &&
1151
5.87k
            (insn->opcode == 0xC2 || insn->opcode == 0xC3) &&
1152
135
            (insn->rexPrefix & 0x08);
1153
1154
605k
  attrMask = ATTR_NONE;
1155
1156
605k
  if (insn->mode == MODE_64BIT)
1157
216k
    attrMask |= ATTR_64BIT;
1158
1159
605k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1160
36.5k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1161
24.3k
            ATTR_EVEX :
1162
36.5k
            ATTR_VEX;
1163
1164
36.5k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1165
24.3k
      switch (ppFromEVEX3of4(
1166
24.3k
        insn->vectorExtensionPrefix[2])) {
1167
21.1k
      case VEX_PREFIX_66:
1168
21.1k
        attrMask |= ATTR_OPSIZE;
1169
21.1k
        break;
1170
1.24k
      case VEX_PREFIX_F3:
1171
1.24k
        attrMask |= ATTR_XS;
1172
1.24k
        break;
1173
507
      case VEX_PREFIX_F2:
1174
507
        attrMask |= ATTR_XD;
1175
507
        break;
1176
24.3k
      }
1177
1178
24.3k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1179
2.77k
        attrMask |= ATTR_EVEXKZ;
1180
24.3k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1181
8.11k
        attrMask |= ATTR_EVEXB;
1182
24.3k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1183
16.8k
        attrMask |= ATTR_EVEXK;
1184
24.3k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1185
12.5k
        attrMask |= ATTR_EVEXL;
1186
24.3k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1187
10.3k
        attrMask |= ATTR_EVEXL2;
1188
24.3k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1189
2.12k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1190
2.00k
      case VEX_PREFIX_66:
1191
2.00k
        attrMask |= ATTR_OPSIZE;
1192
2.00k
        break;
1193
60
      case VEX_PREFIX_F3:
1194
60
        attrMask |= ATTR_XS;
1195
60
        break;
1196
16
      case VEX_PREFIX_F2:
1197
16
        attrMask |= ATTR_XD;
1198
16
        break;
1199
2.12k
      }
1200
1201
2.12k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1202
1.19k
        attrMask |= ATTR_VEXL;
1203
10.1k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1204
6.03k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1205
3.08k
      case VEX_PREFIX_66:
1206
3.08k
        attrMask |= ATTR_OPSIZE;
1207
3.08k
        break;
1208
654
      case VEX_PREFIX_F3:
1209
654
        attrMask |= ATTR_XS;
1210
654
        break;
1211
1.55k
      case VEX_PREFIX_F2:
1212
1.55k
        attrMask |= ATTR_XD;
1213
1.55k
        break;
1214
6.03k
      }
1215
1216
6.03k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1217
5.37k
        attrMask |= ATTR_VEXL;
1218
6.03k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1219
4.07k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1220
4
      case VEX_PREFIX_66:
1221
4
        attrMask |= ATTR_OPSIZE;
1222
4
        break;
1223
4
      case VEX_PREFIX_F3:
1224
4
        attrMask |= ATTR_XS;
1225
4
        break;
1226
7
      case VEX_PREFIX_F2:
1227
7
        attrMask |= ATTR_XD;
1228
7
        break;
1229
4.07k
      }
1230
1231
4.07k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1232
247
        attrMask |= ATTR_VEXL;
1233
4.07k
    } else {
1234
0
      return -1;
1235
0
    }
1236
568k
  } else {
1237
568k
    if (insn->hasOpSize && insn->mode != MODE_16BIT &&
1238
11.0k
        !rexWOverridesOpSize) {
1239
11.0k
      attrMask |= ATTR_OPSIZE;
1240
11.0k
    }
1241
568k
    if (insn->hasAdSize)
1242
3.62k
      attrMask |= ATTR_ADSIZE;
1243
568k
    if (insn->opcodeType == ONEBYTE) {
1244
540k
      if (insn->repeatPrefix == 0xf3 &&
1245
9.27k
          (insn->opcode == 0x90))
1246
        // Special support for PAUSE
1247
528
        attrMask |= ATTR_XS;
1248
540k
    } else {
1249
28.1k
      if (insn->repeatPrefix == 0xf2)
1250
3.38k
        attrMask |= ATTR_XD;
1251
24.7k
      else if (insn->repeatPrefix == 0xf3)
1252
3.81k
        attrMask |= ATTR_XS;
1253
28.1k
    }
1254
1255
568k
    if ((attrMask & ATTR_OPSIZE) &&
1256
11.0k
        (attrMask & (ATTR_XD | ATTR_XS))) {
1257
1.71k
      attrMask =
1258
1.71k
        resolveMandatoryPrefixConflict(insn, attrMask);
1259
1.71k
    }
1260
568k
  }
1261
1262
605k
  if (insn->rexPrefix & 0x08) {
1263
28.2k
    attrMask |= ATTR_REXW;
1264
28.2k
    attrMask &= ~ATTR_ADSIZE;
1265
28.2k
  }
1266
1267
  /*
1268
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1269
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1270
   */
1271
605k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1272
175k
      insn->opcode == 0xE3)
1273
696
    attrMask ^= ATTR_ADSIZE;
1274
1275
  /*
1276
   * CALL/JMP ignore 66 in 64-bit mode. Near Jcc preserve their previous
1277
   * behavior unless an Intel or AMD mode is selected.
1278
   */
1279
605k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1280
8.97k
    switch (insn->opcode) {
1281
468
    case 0xE8:
1282
836
    case 0xE9:
1283
      // Take care of psubsb and other mmx instructions.
1284
836
      if (insn->opcodeType == ONEBYTE) {
1285
130
        attrMask ^= ATTR_OPSIZE;
1286
130
        insn->immediateSize = 4;
1287
130
        insn->displacementSize = 4;
1288
130
      }
1289
836
      break;
1290
25
    case 0x80:
1291
148
    case 0x81:
1292
214
    case 0x82:
1293
481
    case 0x83:
1294
693
    case 0x84:
1295
721
    case 0x85:
1296
780
    case 0x86:
1297
1.41k
    case 0x87:
1298
1.61k
    case 0x88:
1299
1.70k
    case 0x89:
1300
1.73k
    case 0x8A:
1301
1.79k
    case 0x8B:
1302
1.84k
    case 0x8C:
1303
1.88k
    case 0x8D:
1304
2.15k
    case 0x8E:
1305
2.25k
    case 0x8F:
1306
      // Take care of lea and three byte ops.
1307
2.25k
      if (insn->opcodeType != TWOBYTE)
1308
2.06k
        break;
1309
1310
190
      if ((x86_has_feature(mode, CS_MODE_X86_INTEL) ||
1311
190
           x86_has_feature(mode, CS_MODE_X86_AMD)) &&
1312
0
          insn->vectorExtensionType == TYPE_NO_VEX_XOP) {
1313
0
        if ((x86_has_feature(mode, CS_MODE_X86_AMD)) &&
1314
0
            !wFromREX(insn->rexPrefix)) {
1315
0
          attrMask |= ATTR_OPSIZE;
1316
0
          insn->immediateSize = 2;
1317
0
          insn->displacementSize = 2;
1318
0
          insn->immSize = 2;
1319
0
        } else {
1320
0
          attrMask &= ~ATTR_OPSIZE;
1321
0
          insn->immediateSize = 4;
1322
0
          insn->displacementSize = 4;
1323
0
          insn->immSize = 8;
1324
0
        }
1325
190
      } else if (insn->opcode >= 0x82) {
1326
101
        attrMask ^= ATTR_OPSIZE;
1327
101
        insn->immediateSize = 4;
1328
101
        insn->displacementSize = 4;
1329
101
      }
1330
190
      break;
1331
8.97k
    }
1332
8.97k
  }
1333
1334
  /* The following clauses compensate for limitations of the tables. */
1335
605k
  if (insn->mode != MODE_64BIT &&
1336
389k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1337
21.8k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1338
8
      return -1;
1339
8
    }
1340
1341
    /*
1342
     * The tables can't distinguish between cases where the W-bit is used to
1343
     * select register size and cases where it's a required part of the opcode.
1344
     */
1345
21.8k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1346
13.5k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1347
13.5k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1348
1.28k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1349
13.0k
        (insn->vectorExtensionType == TYPE_XOP &&
1350
8.78k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1351
8.78k
      uint16_t instructionIDWithREXW;
1352
1353
8.78k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1354
8.78k
                attrMask | ATTR_REXW)) {
1355
1
        insn->instructionID = instructionID;
1356
1
        insn->spec = specifierForUID(instructionID);
1357
1
        return 0;
1358
1
      }
1359
1360
      // If not a 64-bit instruction. Switch the opcode.
1361
8.78k
      if (!is64Bit(instructionIDWithREXW)) {
1362
8.39k
        insn->instructionID = instructionIDWithREXW;
1363
8.39k
        insn->spec =
1364
8.39k
          specifierForUID(instructionIDWithREXW);
1365
1366
8.39k
        return 0;
1367
8.39k
      }
1368
8.78k
    }
1369
21.8k
  }
1370
1371
  /*
1372
   * Absolute moves, umonitor, and movdir64b need special handling.
1373
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1374
   *  inverted w.r.t.
1375
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1376
   *  any position.
1377
   */
1378
596k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1379
590k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1380
589k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1381
    /* Make sure we observed the prefixes in any position. */
1382
7.19k
    if (insn->hasAdSize)
1383
71
      attrMask |= ATTR_ADSIZE;
1384
1385
7.19k
    if (insn->hasOpSize)
1386
116
      attrMask |= ATTR_OPSIZE;
1387
1388
    /* In 16-bit, invert the attributes. */
1389
7.19k
    if (insn->mode == MODE_16BIT) {
1390
3.23k
      attrMask ^= ATTR_ADSIZE;
1391
1392
      /* The OpSize attribute is only valid with the absolute moves. */
1393
3.23k
      if (insn->opcodeType == ONEBYTE &&
1394
2.65k
          ((insn->opcode & 0xFC) == 0xA0))
1395
2.65k
        attrMask ^= ATTR_OPSIZE;
1396
3.23k
    }
1397
1398
7.19k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1399
3
      return -1;
1400
3
    }
1401
1402
7.19k
    insn->instructionID = instructionID;
1403
7.19k
    insn->spec = specifierForUID(instructionID);
1404
1405
7.19k
    return 0;
1406
7.19k
  }
1407
589k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1408
996
    return -1;
1409
996
  }
1410
1411
588k
  if ((insn->mode == MODE_16BIT ||
1412
401k
       (insn->hasOpSize && !rexWOverridesOpSize)) &&
1413
200k
      !(attrMask & ATTR_OPSIZE)) {
1414
    /*
1415
     * The instruction tables make no distinction between instructions that
1416
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1417
     * particular spot (i.e., many MMX operations).  In general we're
1418
     * conservative, but in the specific case where OpSize is present but not
1419
     * in the right place we check if there's a 16-bit operation.
1420
     */
1421
185k
    const struct InstructionSpecifier *spec;
1422
185k
    uint16_t instructionIDWithOpsize;
1423
1424
185k
    spec = specifierForUID(instructionID);
1425
1426
185k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1427
185k
              attrMask | ATTR_OPSIZE)) {
1428
      /*
1429
       * ModRM required with OpSize but not present; give up and return version
1430
       * without OpSize set
1431
       */
1432
2
      insn->instructionID = instructionID;
1433
2
      insn->spec = spec;
1434
1435
2
      return 0;
1436
2
    }
1437
1438
185k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1439
86.6k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1440
85.5k
      insn->instructionID = instructionIDWithOpsize;
1441
85.5k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1442
99.4k
    } else {
1443
99.4k
      insn->instructionID = instructionID;
1444
99.4k
      insn->spec = spec;
1445
99.4k
    }
1446
1447
185k
    return 0;
1448
185k
  }
1449
1450
403k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1451
1.58k
      insn->rexPrefix & 0x01) {
1452
    /*
1453
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1454
     * it should decode as XCHG %r8, %eax.
1455
     */
1456
765
    const struct InstructionSpecifier *spec;
1457
765
    uint16_t instructionIDWithNewOpcode;
1458
765
    const struct InstructionSpecifier *specWithNewOpcode;
1459
1460
765
    spec = specifierForUID(instructionID);
1461
1462
    /* Borrow opcode from one of the other XCHGar opcodes */
1463
765
    insn->opcode = 0x91;
1464
1465
765
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1466
765
              attrMask)) {
1467
0
      insn->opcode = 0x90;
1468
1469
0
      insn->instructionID = instructionID;
1470
0
      insn->spec = spec;
1471
1472
0
      return 0;
1473
0
    }
1474
1475
765
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1476
1477
    /* Change back */
1478
765
    insn->opcode = 0x90;
1479
1480
765
    insn->instructionID = instructionIDWithNewOpcode;
1481
765
    insn->spec = specWithNewOpcode;
1482
1483
765
    return 0;
1484
765
  }
1485
1486
402k
  insn->instructionID = instructionID;
1487
402k
  insn->spec = specifierForUID(insn->instructionID);
1488
1489
402k
  return 0;
1490
403k
}
1491
1492
/*
1493
 * readSIB - Consumes the SIB byte to determine addressing information for an
1494
 *   instruction.
1495
 *
1496
 * @param insn  - The instruction whose SIB byte is to be read.
1497
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1498
 */
1499
static int readSIB(struct InternalInstruction *insn)
1500
13.9k
{
1501
13.9k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1502
13.9k
  uint8_t index, base;
1503
1504
  // dbgprintf(insn, "readSIB()");
1505
1506
13.9k
  if (insn->consumedSIB)
1507
0
    return 0;
1508
1509
13.9k
  insn->consumedSIB = true;
1510
1511
13.9k
  switch (insn->addressSize) {
1512
0
  case 2:
1513
    // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1514
0
    return -1;
1515
6.23k
  case 4:
1516
6.23k
    insn->sibIndexBase = SIB_INDEX_EAX;
1517
6.23k
    sibBaseBase = SIB_BASE_EAX;
1518
6.23k
    break;
1519
7.74k
  case 8:
1520
7.74k
    insn->sibIndexBase = SIB_INDEX_RAX;
1521
7.74k
    sibBaseBase = SIB_BASE_RAX;
1522
7.74k
    break;
1523
13.9k
  }
1524
1525
13.9k
  if (consumeByte(insn, &insn->sib))
1526
35
    return -1;
1527
1528
13.9k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1529
1530
13.9k
  if (index == 0x4) {
1531
2.62k
    insn->sibIndex = SIB_INDEX_NONE;
1532
11.3k
  } else {
1533
11.3k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1534
11.3k
  }
1535
1536
13.9k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1537
1538
13.9k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1539
1540
13.9k
  switch (base) {
1541
1.22k
  case 0x5:
1542
1.61k
  case 0xd:
1543
1.61k
    switch (modFromModRM(insn->modRM)) {
1544
968
    case 0x0:
1545
968
      insn->eaDisplacement = EA_DISP_32;
1546
968
      insn->sibBase = SIB_BASE_NONE;
1547
968
      break;
1548
426
    case 0x1:
1549
426
      insn->eaDisplacement = EA_DISP_8;
1550
426
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1551
426
      break;
1552
225
    case 0x2:
1553
225
      insn->eaDisplacement = EA_DISP_32;
1554
225
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1555
225
      break;
1556
0
    case 0x3:
1557
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1558
0
      return -1;
1559
1.61k
    }
1560
1.61k
    break;
1561
12.3k
  default:
1562
12.3k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1563
12.3k
    break;
1564
13.9k
  }
1565
1566
13.9k
  return 0;
1567
13.9k
}
1568
1569
/*
1570
 * readDisplacement - Consumes the displacement of an instruction.
1571
 *
1572
 * @param insn  - The instruction whose displacement is to be read.
1573
 * @return      - 0 if the displacement byte was successfully read; nonzero
1574
 *                otherwise.
1575
 */
1576
static int readDisplacement(struct InternalInstruction *insn)
1577
104k
{
1578
104k
  int8_t d8;
1579
104k
  int16_t d16;
1580
104k
  int32_t d32;
1581
1582
  // dbgprintf(insn, "readDisplacement()");
1583
1584
104k
  if (insn->consumedDisplacement)
1585
0
    return 0;
1586
1587
104k
  insn->consumedDisplacement = true;
1588
104k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1589
1590
104k
  switch (insn->eaDisplacement) {
1591
26.7k
  case EA_DISP_NONE:
1592
26.7k
    insn->consumedDisplacement = false;
1593
26.7k
    break;
1594
45.6k
  case EA_DISP_8:
1595
45.6k
    if (consumeInt8(insn, &d8))
1596
86
      return -1;
1597
45.5k
    insn->displacement = d8;
1598
45.5k
    break;
1599
13.1k
  case EA_DISP_16:
1600
13.1k
    if (consumeInt16(insn, &d16))
1601
40
      return -1;
1602
13.1k
    insn->displacement = d16;
1603
13.1k
    break;
1604
18.9k
  case EA_DISP_32:
1605
18.9k
    if (consumeInt32(insn, &d32))
1606
193
      return -1;
1607
18.8k
    insn->displacement = d32;
1608
18.8k
    break;
1609
104k
  }
1610
1611
104k
  return 0;
1612
104k
}
1613
1614
/*
1615
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1616
 *   displacement) for an instruction and interprets it.
1617
 *
1618
 * @param insn  - The instruction whose addressing information is to be read.
1619
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1620
 */
1621
static int readModRM(struct InternalInstruction *insn)
1622
954k
{
1623
954k
  uint8_t mod, rm, reg, evexrm;
1624
1625
  // dbgprintf(insn, "readModRM()");
1626
1627
954k
  if (insn->consumedModRM)
1628
642k
    return 0;
1629
1630
312k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1631
1632
312k
  if (consumeByte(insn, &insn->modRM))
1633
661
    return -1;
1634
1635
311k
  insn->consumedModRM = true;
1636
1637
  // save original ModRM for later reference
1638
311k
  insn->orgModRM = insn->modRM;
1639
1640
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1641
311k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1642
23.5k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1643
1.26k
    insn->modRM |= 0xC0;
1644
1645
311k
  mod = modFromModRM(insn->modRM);
1646
311k
  rm = rmFromModRM(insn->modRM);
1647
311k
  reg = regFromModRM(insn->modRM);
1648
1649
  /*
1650
   * This goes by insn->registerSize to pick the correct register, which messes
1651
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1652
   * fixupReg().
1653
   */
1654
311k
  switch (insn->registerSize) {
1655
102k
  case 2:
1656
102k
    insn->regBase = MODRM_REG_AX;
1657
102k
    insn->eaRegBase = EA_REG_AX;
1658
102k
    break;
1659
188k
  case 4:
1660
188k
    insn->regBase = MODRM_REG_EAX;
1661
188k
    insn->eaRegBase = EA_REG_EAX;
1662
188k
    break;
1663
20.3k
  case 8:
1664
20.3k
    insn->regBase = MODRM_REG_RAX;
1665
20.3k
    insn->eaRegBase = EA_REG_RAX;
1666
20.3k
    break;
1667
311k
  }
1668
1669
311k
  reg |= rFromREX(insn->rexPrefix) << 3;
1670
311k
  rm |= bFromREX(insn->rexPrefix) << 3;
1671
1672
311k
  evexrm = 0;
1673
311k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1674
24.1k
      insn->mode == MODE_64BIT) {
1675
10.7k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1676
10.7k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1677
10.7k
  }
1678
1679
311k
  insn->reg = (Reg)(insn->regBase + reg);
1680
1681
311k
  switch (insn->addressSize) {
1682
94.8k
  case 2: {
1683
94.8k
    EABase eaBaseBase = EA_BASE_BX_SI;
1684
1685
94.8k
    switch (mod) {
1686
50.9k
    case 0x0:
1687
50.9k
      if (rm == 0x6) {
1688
2.41k
        insn->eaBase = EA_BASE_NONE;
1689
2.41k
        insn->eaDisplacement = EA_DISP_16;
1690
2.41k
        if (readDisplacement(insn))
1691
10
          return -1;
1692
48.5k
      } else {
1693
48.5k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1694
48.5k
        insn->eaDisplacement = EA_DISP_NONE;
1695
48.5k
      }
1696
50.9k
      break;
1697
50.9k
    case 0x1:
1698
13.3k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1699
13.3k
      insn->eaDisplacement = EA_DISP_8;
1700
13.3k
      insn->displacementSize = 1;
1701
13.3k
      if (readDisplacement(insn))
1702
25
        return -1;
1703
13.3k
      break;
1704
13.3k
    case 0x2:
1705
10.7k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1706
10.7k
      insn->eaDisplacement = EA_DISP_16;
1707
10.7k
      if (readDisplacement(insn))
1708
30
        return -1;
1709
10.7k
      break;
1710
19.7k
    case 0x3:
1711
19.7k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1712
19.7k
      if (readDisplacement(insn))
1713
0
        return -1;
1714
19.7k
      break;
1715
94.8k
    }
1716
94.7k
    break;
1717
94.8k
  }
1718
1719
104k
  case 4:
1720
216k
  case 8: {
1721
216k
    EABase eaBaseBase =
1722
216k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1723
1724
216k
    switch (mod) {
1725
0
    default:
1726
0
      break;
1727
110k
    case 0x0:
1728
110k
      insn->eaDisplacement =
1729
110k
        EA_DISP_NONE; /* readSIB may override this */
1730
      // In determining whether RIP-relative mode is used (rm=5),
1731
      // or whether a SIB byte is present (rm=4),
1732
      // the extension bits (REX.b and EVEX.x) are ignored.
1733
110k
      switch (rm & 7) {
1734
8.05k
      case 0x4: // SIB byte is present
1735
8.05k
        insn->eaBase = (insn->addressSize == 4 ?
1736
3.88k
              EA_BASE_sib :
1737
8.05k
              EA_BASE_sib64);
1738
8.05k
        if (readSIB(insn) || readDisplacement(insn))
1739
23
          return -1;
1740
8.03k
        break;
1741
8.03k
      case 0x5: // RIP-relative
1742
2.91k
        insn->eaBase = EA_BASE_NONE;
1743
2.91k
        insn->eaDisplacement = EA_DISP_32;
1744
2.91k
        if (readDisplacement(insn))
1745
35
          return -1;
1746
2.87k
        break;
1747
99.8k
      default:
1748
99.8k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1749
99.8k
        break;
1750
110k
      }
1751
110k
      break;
1752
110k
    case 0x1:
1753
32.3k
      insn->displacementSize = 1;
1754
      /* FALLTHROUGH */
1755
47.4k
    case 0x2:
1756
47.4k
      insn->eaDisplacement =
1757
47.4k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1758
47.4k
      switch (rm & 7) {
1759
5.93k
      case 0x4: // SIB byte is present
1760
5.93k
        insn->eaBase = EA_BASE_sib;
1761
5.93k
        if (readSIB(insn) || readDisplacement(insn))
1762
55
          return -1;
1763
5.87k
        break;
1764
41.4k
      default:
1765
41.4k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1766
41.4k
        if (readDisplacement(insn))
1767
176
          return -1;
1768
41.3k
        break;
1769
47.4k
      }
1770
47.1k
      break;
1771
58.6k
    case 0x3:
1772
58.6k
      insn->eaDisplacement = EA_DISP_NONE;
1773
58.6k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1774
58.6k
      break;
1775
216k
    }
1776
1777
216k
    break;
1778
216k
  }
1779
311k
  } /* switch (insn->addressSize) */
1780
1781
311k
  return 0;
1782
311k
}
1783
1784
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1785
  static uint16_t name(struct InternalInstruction *insn, \
1786
           OperandType type, uint8_t index, uint8_t *valid) \
1787
332k
  { \
1788
332k
    *valid = 1; \
1789
332k
    switch (type) { \
1790
0
    default: \
1791
0
      *valid = 0; \
1792
0
      return 0; \
1793
86.3k
    case TYPE_Rv: \
1794
86.3k
      return base + index; \
1795
139k
    case TYPE_R8: \
1796
139k
      index &= mask; \
1797
139k
      if (index > 0xf) \
1798
139k
        *valid = 0; \
1799
139k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1800
1.14k
        return prefix##_SPL + (index - 4); \
1801
138k
      } else { \
1802
138k
        return prefix##_AL + index; \
1803
138k
      } \
1804
139k
    case TYPE_R16: \
1805
1.49k
      index &= mask; \
1806
1.49k
      if (index > 0xf) \
1807
1.49k
        *valid = 0; \
1808
1.49k
      return prefix##_AX + index; \
1809
139k
    case TYPE_R32: \
1810
1.86k
      index &= mask; \
1811
1.86k
      if (index > 0xf) \
1812
1.86k
        *valid = 0; \
1813
1.86k
      return prefix##_EAX + index; \
1814
139k
    case TYPE_R64: \
1815
7.14k
      index &= mask; \
1816
7.14k
      if (index > 0xf) \
1817
7.14k
        *valid = 0; \
1818
7.14k
      return prefix##_RAX + index; \
1819
139k
    case TYPE_ZMM: \
1820
20.2k
      return prefix##_ZMM0 + index; \
1821
139k
    case TYPE_YMM: \
1822
16.7k
      return prefix##_YMM0 + index; \
1823
139k
    case TYPE_XMM: \
1824
38.2k
      return prefix##_XMM0 + index; \
1825
139k
    case TYPE_VK: \
1826
11.1k
      index &= 0xf; \
1827
11.1k
      if (index > 7) \
1828
11.1k
        *valid = 0; \
1829
11.1k
      return prefix##_K0 + index; \
1830
139k
    case TYPE_MM64: \
1831
3.28k
      return prefix##_MM0 + (index & 0x7); \
1832
139k
    case TYPE_SEGMENTREG: \
1833
1.36k
      if ((index & 7) > 5) \
1834
1.36k
        *valid = 0; \
1835
1.36k
      return prefix##_ES + (index & 7); \
1836
139k
    case TYPE_DEBUGREG: \
1837
786
      return prefix##_DR0 + index; \
1838
139k
    case TYPE_CONTROLREG: \
1839
481
      return prefix##_CR0 + index; \
1840
139k
    case TYPE_BNDR: \
1841
3.41k
      if (index > 3) \
1842
3.41k
        *valid = 0; \
1843
3.41k
      return prefix##_BND0 + index; \
1844
139k
    case TYPE_MVSIBX: \
1845
0
      return prefix##_XMM0 + index; \
1846
139k
    case TYPE_MVSIBY: \
1847
0
      return prefix##_YMM0 + index; \
1848
139k
    case TYPE_MVSIBZ: \
1849
0
      return prefix##_ZMM0 + index; \
1850
332k
    } \
1851
332k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1787
257k
  { \
1788
257k
    *valid = 1; \
1789
257k
    switch (type) { \
1790
0
    default: \
1791
0
      *valid = 0; \
1792
0
      return 0; \
1793
61.4k
    case TYPE_Rv: \
1794
61.4k
      return base + index; \
1795
114k
    case TYPE_R8: \
1796
114k
      index &= mask; \
1797
114k
      if (index > 0xf) \
1798
114k
        *valid = 0; \
1799
114k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1800
733
        return prefix##_SPL + (index - 4); \
1801
113k
      } else { \
1802
113k
        return prefix##_AL + index; \
1803
113k
      } \
1804
114k
    case TYPE_R16: \
1805
1.17k
      index &= mask; \
1806
1.17k
      if (index > 0xf) \
1807
1.17k
        *valid = 0; \
1808
1.17k
      return prefix##_AX + index; \
1809
114k
    case TYPE_R32: \
1810
1.02k
      index &= mask; \
1811
1.02k
      if (index > 0xf) \
1812
1.02k
        *valid = 0; \
1813
1.02k
      return prefix##_EAX + index; \
1814
114k
    case TYPE_R64: \
1815
3.11k
      index &= mask; \
1816
3.11k
      if (index > 0xf) \
1817
3.11k
        *valid = 0; \
1818
3.11k
      return prefix##_RAX + index; \
1819
114k
    case TYPE_ZMM: \
1820
15.9k
      return prefix##_ZMM0 + index; \
1821
114k
    case TYPE_YMM: \
1822
12.8k
      return prefix##_YMM0 + index; \
1823
114k
    case TYPE_XMM: \
1824
29.5k
      return prefix##_XMM0 + index; \
1825
114k
    case TYPE_VK: \
1826
10.2k
      index &= 0xf; \
1827
10.2k
      if (index > 7) \
1828
10.2k
        *valid = 0; \
1829
10.2k
      return prefix##_K0 + index; \
1830
114k
    case TYPE_MM64: \
1831
2.13k
      return prefix##_MM0 + (index & 0x7); \
1832
114k
    case TYPE_SEGMENTREG: \
1833
1.36k
      if ((index & 7) > 5) \
1834
1.36k
        *valid = 0; \
1835
1.36k
      return prefix##_ES + (index & 7); \
1836
114k
    case TYPE_DEBUGREG: \
1837
786
      return prefix##_DR0 + index; \
1838
114k
    case TYPE_CONTROLREG: \
1839
481
      return prefix##_CR0 + index; \
1840
114k
    case TYPE_BNDR: \
1841
3.28k
      if (index > 3) \
1842
3.28k
        *valid = 0; \
1843
3.28k
      return prefix##_BND0 + index; \
1844
114k
    case TYPE_MVSIBX: \
1845
0
      return prefix##_XMM0 + index; \
1846
114k
    case TYPE_MVSIBY: \
1847
0
      return prefix##_YMM0 + index; \
1848
114k
    case TYPE_MVSIBZ: \
1849
0
      return prefix##_ZMM0 + index; \
1850
257k
    } \
1851
257k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1787
74.4k
  { \
1788
74.4k
    *valid = 1; \
1789
74.4k
    switch (type) { \
1790
0
    default: \
1791
0
      *valid = 0; \
1792
0
      return 0; \
1793
24.8k
    case TYPE_Rv: \
1794
24.8k
      return base + index; \
1795
25.2k
    case TYPE_R8: \
1796
25.2k
      index &= mask; \
1797
25.2k
      if (index > 0xf) \
1798
25.2k
        *valid = 0; \
1799
25.2k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1800
409
        return prefix##_SPL + (index - 4); \
1801
24.8k
      } else { \
1802
24.8k
        return prefix##_AL + index; \
1803
24.8k
      } \
1804
25.2k
    case TYPE_R16: \
1805
317
      index &= mask; \
1806
317
      if (index > 0xf) \
1807
317
        *valid = 0; \
1808
317
      return prefix##_AX + index; \
1809
25.2k
    case TYPE_R32: \
1810
841
      index &= mask; \
1811
841
      if (index > 0xf) \
1812
841
        *valid = 0; \
1813
841
      return prefix##_EAX + index; \
1814
25.2k
    case TYPE_R64: \
1815
4.03k
      index &= mask; \
1816
4.03k
      if (index > 0xf) \
1817
4.03k
        *valid = 0; \
1818
4.03k
      return prefix##_RAX + index; \
1819
25.2k
    case TYPE_ZMM: \
1820
4.32k
      return prefix##_ZMM0 + index; \
1821
25.2k
    case TYPE_YMM: \
1822
3.90k
      return prefix##_YMM0 + index; \
1823
25.2k
    case TYPE_XMM: \
1824
8.71k
      return prefix##_XMM0 + index; \
1825
25.2k
    case TYPE_VK: \
1826
860
      index &= 0xf; \
1827
860
      if (index > 7) \
1828
860
        *valid = 0; \
1829
860
      return prefix##_K0 + index; \
1830
25.2k
    case TYPE_MM64: \
1831
1.15k
      return prefix##_MM0 + (index & 0x7); \
1832
25.2k
    case TYPE_SEGMENTREG: \
1833
0
      if ((index & 7) > 5) \
1834
0
        *valid = 0; \
1835
0
      return prefix##_ES + (index & 7); \
1836
25.2k
    case TYPE_DEBUGREG: \
1837
0
      return prefix##_DR0 + index; \
1838
25.2k
    case TYPE_CONTROLREG: \
1839
0
      return prefix##_CR0 + index; \
1840
25.2k
    case TYPE_BNDR: \
1841
128
      if (index > 3) \
1842
128
        *valid = 0; \
1843
128
      return prefix##_BND0 + index; \
1844
25.2k
    case TYPE_MVSIBX: \
1845
0
      return prefix##_XMM0 + index; \
1846
25.2k
    case TYPE_MVSIBY: \
1847
0
      return prefix##_YMM0 + index; \
1848
25.2k
    case TYPE_MVSIBZ: \
1849
0
      return prefix##_ZMM0 + index; \
1850
74.4k
    } \
1851
74.4k
  }
1852
1853
/*
1854
 * fixup*Value - Consults an operand type to determine the meaning of the
1855
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1856
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1857
 *   misinterpret it as.
1858
 *
1859
 * @param insn  - The instruction containing the operand.
1860
 * @param type  - The operand type.
1861
 * @param index - The existing value of the field as reported by readModRM().
1862
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1863
 *                field is valid for the register class; 0 if not.
1864
 * @return      - The proper value.
1865
 */
1866
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1867
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1868
1869
/*
1870
 * fixupReg - Consults an operand specifier to determine which of the
1871
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1872
 *
1873
 * @param insn  - See fixup*Value().
1874
 * @param op    - The operand specifier.
1875
 * @return      - 0 if fixup was successful; -1 if the register returned was
1876
 *                invalid for its class.
1877
 */
1878
static int fixupReg(struct InternalInstruction *insn,
1879
        const struct OperandSpecifier *op)
1880
562k
{
1881
562k
  uint8_t valid;
1882
1883
562k
  switch ((OperandEncoding)op->encoding) {
1884
0
  default:
1885
    // debug("Expected a REG or R/M encoding in fixupReg");
1886
0
    return -1;
1887
26.5k
  case ENCODING_VVVV:
1888
26.5k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1889
26.5k
            insn->vvvv, &valid);
1890
26.5k
    if (!valid)
1891
1
      return -1;
1892
26.5k
    break;
1893
231k
  case ENCODING_REG:
1894
231k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1895
231k
                 insn->reg - insn->regBase,
1896
231k
                 &valid);
1897
231k
    if (!valid)
1898
15
      return -1;
1899
231k
    break;
1900
2.04M
CASE_ENCODING_RM:
1901
2.04M
    if (insn->eaBase >= insn->eaRegBase) {
1902
74.4k
      insn->eaBase = (EABase)fixupRMValue(
1903
74.4k
        insn, (OperandType)op->type,
1904
74.4k
        insn->eaBase - insn->eaRegBase, &valid);
1905
74.4k
      if (!valid)
1906
2
        return -1;
1907
74.4k
    }
1908
304k
    break;
1909
562k
  }
1910
1911
562k
  return 0;
1912
562k
}
1913
1914
/*
1915
 * readOpcodeRegister - Reads an operand from the opcode field of an
1916
 *   instruction and interprets it appropriately given the operand width.
1917
 *   Handles AddRegFrm instructions.
1918
 *
1919
 * @param insn  - the instruction whose opcode field is to be read.
1920
 * @param size  - The width (in bytes) of the register being specified.
1921
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1922
 *                RAX.
1923
 * @return      - 0 on success; nonzero otherwise.
1924
 */
1925
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
1926
73.9k
{
1927
73.9k
  if (size == 0)
1928
51.4k
    size = insn->registerSize;
1929
1930
73.9k
  switch (size) {
1931
10.5k
  case 1:
1932
10.5k
    insn->opcodeRegister =
1933
10.5k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1934
10.5k
                (insn->opcode & 7)));
1935
10.5k
    if (insn->rexPrefix &&
1936
547
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1937
313
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1938
67
      insn->opcodeRegister =
1939
67
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1940
67
                   MODRM_REG_AL - 4));
1941
67
    }
1942
1943
10.5k
    break;
1944
23.2k
  case 2:
1945
23.2k
    insn->opcodeRegister =
1946
23.2k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1947
23.2k
                (insn->opcode & 7)));
1948
23.2k
    break;
1949
28.0k
  case 4:
1950
28.0k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1951
28.0k
               ((bFromREX(insn->rexPrefix) << 3) |
1952
28.0k
                (insn->opcode & 7)));
1953
28.0k
    break;
1954
12.0k
  case 8:
1955
12.0k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1956
12.0k
               ((bFromREX(insn->rexPrefix) << 3) |
1957
12.0k
                (insn->opcode & 7)));
1958
12.0k
    break;
1959
73.9k
  }
1960
1961
73.9k
  return 0;
1962
73.9k
}
1963
1964
/*
1965
 * readImmediate - Consumes an immediate operand from an instruction, given the
1966
 *   desired operand size.
1967
 *
1968
 * @param insn  - The instruction whose operand is to be read.
1969
 * @param size  - The width (in bytes) of the operand.
1970
 * @return      - 0 if the immediate was successfully consumed; nonzero
1971
 *                otherwise.
1972
 */
1973
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
1974
160k
{
1975
160k
  uint8_t imm8;
1976
160k
  uint16_t imm16;
1977
160k
  uint32_t imm32;
1978
160k
  uint64_t imm64;
1979
1980
160k
  if (insn->numImmediatesConsumed == 2) {
1981
    // debug("Already consumed two immediates");
1982
0
    return -1;
1983
0
  }
1984
1985
160k
  if (size == 0)
1986
0
    size = insn->immediateSize;
1987
160k
  else
1988
160k
    insn->immediateSize = size;
1989
1990
160k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1991
1992
160k
  switch (size) {
1993
114k
  case 1:
1994
114k
    if (consumeByte(insn, &imm8))
1995
240
      return -1;
1996
1997
114k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
1998
114k
    break;
1999
23.2k
  case 2:
2000
23.2k
    if (consumeUInt16(insn, &imm16))
2001
108
      return -1;
2002
2003
23.1k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
2004
23.1k
    break;
2005
19.2k
  case 4:
2006
19.2k
    if (consumeUInt32(insn, &imm32))
2007
196
      return -1;
2008
2009
19.0k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
2010
19.0k
    break;
2011
2.67k
  case 8:
2012
2.67k
    if (consumeUInt64(insn, &imm64))
2013
54
      return -1;
2014
2.61k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
2015
2.61k
    break;
2016
160k
  }
2017
2018
159k
  insn->numImmediatesConsumed++;
2019
2020
159k
  return 0;
2021
160k
}
2022
2023
/*
2024
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
2025
 *
2026
 * @param insn  - The instruction whose operand is to be read.
2027
 * @return      - 0 if the vvvv was successfully consumed; nonzero
2028
 *                otherwise.
2029
 */
2030
static int readVVVV(struct InternalInstruction *insn)
2031
603k
{
2032
603k
  int vvvv;
2033
2034
603k
  if (insn->vectorExtensionType == TYPE_EVEX)
2035
24.1k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2036
24.1k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2037
578k
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2038
2.10k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2039
576k
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2040
5.99k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2041
570k
  else if (insn->vectorExtensionType == TYPE_XOP)
2042
4.04k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2043
566k
  else
2044
566k
    return -1;
2045
2046
36.3k
  if (insn->mode != MODE_64BIT)
2047
21.6k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2048
2049
36.3k
  insn->vvvv = (Reg)vvvv;
2050
2051
36.3k
  return 0;
2052
603k
}
2053
2054
/*
2055
 * readMaskRegister - Reads an mask register from the opcode field of an
2056
 *   instruction.
2057
 *
2058
 * @param insn    - The instruction whose opcode field is to be read.
2059
 * @return        - 0 on success; nonzero otherwise.
2060
 */
2061
static int readMaskRegister(struct InternalInstruction *insn)
2062
17.0k
{
2063
17.0k
  if (insn->vectorExtensionType != TYPE_EVEX)
2064
0
    return -1;
2065
2066
17.0k
  insn->writemask =
2067
17.0k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2068
2069
17.0k
  return 0;
2070
17.0k
}
2071
2072
/*
2073
 * readOperands - Consults the specifier for an instruction and consumes all
2074
 *   operands for that instruction, interpreting them as it goes.
2075
 *
2076
 * @param insn  - The instruction whose operands are to be read and interpreted.
2077
 * @return      - 0 if all operands could be read; nonzero otherwise.
2078
 */
2079
static int readOperands(struct InternalInstruction *insn)
2080
603k
{
2081
603k
  int hasVVVV, needVVVV;
2082
603k
  int sawRegImm = 0;
2083
603k
  int i;
2084
2085
  /* If non-zero vvvv specified, need to make sure one of the operands
2086
     uses it. */
2087
603k
  hasVVVV = !readVVVV(insn);
2088
603k
  needVVVV = hasVVVV && (insn->vvvv != 0);
2089
2090
4.21M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2091
3.61M
    const OperandSpecifier *op =
2092
3.61M
      &x86OperandSets[insn->spec->operands][i];
2093
3.61M
    switch (op->encoding) {
2094
2.60M
    case ENCODING_NONE:
2095
2.63M
    case ENCODING_SI:
2096
2.66M
    case ENCODING_DI:
2097
2.66M
      break;
2098
2099
14.8k
CASE_ENCODING_VSIB:
2100
      // VSIB can use the V2 bit so check only the other bits.
2101
14.8k
      if (needVVVV)
2102
1.67k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2103
2104
14.8k
      if (readModRM(insn))
2105
0
        return -1;
2106
2107
      // Reject if SIB wasn't used.
2108
2.96k
      if (insn->eaBase != EA_BASE_sib &&
2109
1.47k
          insn->eaBase != EA_BASE_sib64)
2110
3
        return -1;
2111
2112
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2113
2.96k
      if (insn->sibIndex == SIB_INDEX_NONE)
2114
366
        insn->sibIndex =
2115
366
          (SIBIndex)(insn->sibIndexBase + 4);
2116
2117
      // If EVEX.v2 is set this is one of the 16-31 registers.
2118
2.96k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2119
2.42k
          insn->mode == MODE_64BIT &&
2120
1.44k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2121
1.13k
        insn->sibIndex =
2122
1.13k
          (SIBIndex)(insn->sibIndex + 16);
2123
2124
      // Adjust the index register to the correct size.
2125
2.96k
      switch (op->type) {
2126
0
      default:
2127
        // debug("Unhandled VSIB index type");
2128
0
        return -1;
2129
1.34k
      case TYPE_MVSIBX:
2130
1.34k
        insn->sibIndex =
2131
1.34k
          (SIBIndex)(SIB_INDEX_XMM0 +
2132
1.34k
               (insn->sibIndex -
2133
1.34k
                insn->sibIndexBase));
2134
1.34k
        break;
2135
775
      case TYPE_MVSIBY:
2136
775
        insn->sibIndex =
2137
775
          (SIBIndex)(SIB_INDEX_YMM0 +
2138
775
               (insn->sibIndex -
2139
775
                insn->sibIndexBase));
2140
775
        break;
2141
847
      case TYPE_MVSIBZ:
2142
847
        insn->sibIndex =
2143
847
          (SIBIndex)(SIB_INDEX_ZMM0 +
2144
847
               (insn->sibIndex -
2145
847
                insn->sibIndexBase));
2146
847
        break;
2147
2.96k
      }
2148
2149
      // Apply the AVX512 compressed displacement scaling factor.
2150
2.96k
      if (op->encoding != ENCODING_REG &&
2151
2.96k
          insn->eaDisplacement == EA_DISP_8)
2152
517
        insn->displacement *=
2153
517
          1 << (op->encoding - ENCODING_VSIB);
2154
2.96k
      break;
2155
2156
231k
    case ENCODING_REG:
2157
3.66M
CASE_ENCODING_RM:
2158
3.66M
      if (readModRM(insn))
2159
0
        return -1;
2160
2161
535k
      if (fixupReg(insn, op))
2162
17
        return -1;
2163
2164
      // Apply the AVX512 compressed displacement scaling factor.
2165
535k
      if (op->encoding != ENCODING_REG &&
2166
304k
          insn->eaDisplacement == EA_DISP_8)
2167
44.9k
        insn->displacement *=
2168
44.9k
          1 << (op->encoding - ENCODING_RM);
2169
535k
      break;
2170
2171
115k
    case ENCODING_IB:
2172
115k
      if (sawRegImm) {
2173
        /* Saw a register immediate so don't read again and instead split the
2174
             previous immediate.  FIXME: This is a hack. */
2175
619
        insn->immediates[insn->numImmediatesConsumed] =
2176
619
          insn->immediates
2177
619
            [insn->numImmediatesConsumed -
2178
619
             1] &
2179
619
          0xf;
2180
619
        ++insn->numImmediatesConsumed;
2181
619
        break;
2182
619
      }
2183
114k
      if (readImmediate(insn, 1))
2184
240
        return -1;
2185
114k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2186
681
        sawRegImm = 1;
2187
114k
      break;
2188
2189
6.73k
    case ENCODING_IW:
2190
6.73k
      if (readImmediate(insn, 2))
2191
36
        return -1;
2192
6.69k
      break;
2193
2194
6.69k
    case ENCODING_ID:
2195
3.21k
      if (readImmediate(insn, 4))
2196
37
        return -1;
2197
3.17k
      break;
2198
2199
3.17k
    case ENCODING_IO:
2200
396
      if (readImmediate(insn, 8))
2201
6
        return -1;
2202
390
      break;
2203
2204
28.3k
    case ENCODING_Iv:
2205
28.3k
      if (readImmediate(insn, insn->immediateSize))
2206
203
        return -1;
2207
28.1k
      break;
2208
2209
28.1k
    case ENCODING_Ia:
2210
6.53k
      if (readImmediate(insn, insn->addressSize))
2211
76
        return -1;
2212
      /* Direct memory-offset (moffset) immediate will get mapped
2213
           to memory operand later. We want the encoding info to
2214
           reflect that as well. */
2215
6.46k
      insn->displacementOffset = insn->immediateOffset;
2216
6.46k
      insn->consumedDisplacement = true;
2217
6.46k
      insn->displacementSize = insn->immediateSize;
2218
6.46k
      insn->displacement =
2219
6.46k
        insn->immediates[insn->numImmediatesConsumed -
2220
6.46k
             1];
2221
6.46k
      insn->immediateOffset = 0;
2222
6.46k
      insn->immediateSize = 0;
2223
6.46k
      break;
2224
2225
1.70k
    case ENCODING_IRC:
2226
1.70k
      insn->RC =
2227
1.70k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2228
1.70k
         << 1) |
2229
1.70k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2230
1.70k
      break;
2231
2232
10.5k
    case ENCODING_RB:
2233
10.5k
      if (readOpcodeRegister(insn, 1))
2234
0
        return -1;
2235
10.5k
      break;
2236
2237
10.5k
    case ENCODING_RW:
2238
0
      if (readOpcodeRegister(insn, 2))
2239
0
        return -1;
2240
0
      break;
2241
2242
0
    case ENCODING_RD:
2243
0
      if (readOpcodeRegister(insn, 4))
2244
0
        return -1;
2245
0
      break;
2246
2247
11.8k
    case ENCODING_RO:
2248
11.8k
      if (readOpcodeRegister(insn, 8))
2249
0
        return -1;
2250
11.8k
      break;
2251
2252
51.4k
    case ENCODING_Rv:
2253
51.4k
      if (readOpcodeRegister(insn, 0))
2254
0
        return -1;
2255
51.4k
      break;
2256
2257
51.4k
    case ENCODING_FP:
2258
2.67k
      break;
2259
2260
26.5k
    case ENCODING_VVVV:
2261
26.5k
      if (!hasVVVV)
2262
0
        return -1;
2263
2264
26.5k
      needVVVV =
2265
26.5k
        0; /* Mark that we have found a VVVV operand. */
2266
2267
26.5k
      if (insn->mode != MODE_64BIT)
2268
16.4k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2269
2270
26.5k
      if (fixupReg(insn, op))
2271
1
        return -1;
2272
26.5k
      break;
2273
2274
26.5k
    case ENCODING_WRITEMASK:
2275
17.0k
      if (readMaskRegister(insn))
2276
0
        return -1;
2277
17.0k
      break;
2278
2279
125k
    case ENCODING_DUP:
2280
125k
      break;
2281
2282
0
    default:
2283
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2284
0
      return -1;
2285
3.61M
    }
2286
3.61M
  }
2287
2288
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2289
602k
  if (needVVVV)
2290
9
    return -1;
2291
2292
602k
  return 0;
2293
602k
}
2294
2295
// return True if instruction is illegal to use with prefixes
2296
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2297
static bool checkPrefix(struct InternalInstruction *insn)
2298
603k
{
2299
  // LOCK prefix
2300
603k
  if (insn->hasLockPrefix) {
2301
17.9k
    switch (insn->instructionID) {
2302
130
    default:
2303
      // invalid LOCK
2304
130
      return true;
2305
2306
    // nop dword [rax]
2307
14
    case X86_NOOPL:
2308
2309
    // DEC
2310
74
    case X86_DEC16m:
2311
267
    case X86_DEC32m:
2312
441
    case X86_DEC64m:
2313
452
    case X86_DEC8m:
2314
2315
    // ADC
2316
800
    case X86_ADC16mi:
2317
1.10k
    case X86_ADC16mi8:
2318
1.19k
    case X86_ADC16mr:
2319
1.27k
    case X86_ADC32mi:
2320
1.32k
    case X86_ADC32mi8:
2321
1.57k
    case X86_ADC32mr:
2322
1.75k
    case X86_ADC64mi32:
2323
1.76k
    case X86_ADC64mi8:
2324
1.90k
    case X86_ADC64mr:
2325
1.93k
    case X86_ADC8mi:
2326
2.13k
    case X86_ADC8mi8:
2327
2.40k
    case X86_ADC8mr:
2328
2.44k
    case X86_ADC8rm:
2329
2.48k
    case X86_ADC16rm:
2330
2.50k
    case X86_ADC32rm:
2331
2.57k
    case X86_ADC64rm:
2332
2333
    // ADD
2334
2.65k
    case X86_ADD16mi:
2335
2.70k
    case X86_ADD16mi8:
2336
2.77k
    case X86_ADD16mr:
2337
2.92k
    case X86_ADD32mi:
2338
3.14k
    case X86_ADD32mi8:
2339
3.44k
    case X86_ADD32mr:
2340
3.49k
    case X86_ADD64mi32:
2341
3.73k
    case X86_ADD64mi8:
2342
3.81k
    case X86_ADD64mr:
2343
3.88k
    case X86_ADD8mi:
2344
3.92k
    case X86_ADD8mi8:
2345
4.16k
    case X86_ADD8mr:
2346
4.19k
    case X86_ADD8rm:
2347
4.26k
    case X86_ADD16rm:
2348
4.29k
    case X86_ADD32rm:
2349
4.36k
    case X86_ADD64rm:
2350
2351
    // AND
2352
4.51k
    case X86_AND16mi:
2353
4.56k
    case X86_AND16mi8:
2354
4.63k
    case X86_AND16mr:
2355
4.73k
    case X86_AND32mi:
2356
4.89k
    case X86_AND32mi8:
2357
4.94k
    case X86_AND32mr:
2358
5.02k
    case X86_AND64mi32:
2359
5.05k
    case X86_AND64mi8:
2360
5.10k
    case X86_AND64mr:
2361
5.34k
    case X86_AND8mi:
2362
5.38k
    case X86_AND8mi8:
2363
5.56k
    case X86_AND8mr:
2364
5.64k
    case X86_AND8rm:
2365
5.69k
    case X86_AND16rm:
2366
5.84k
    case X86_AND32rm:
2367
5.86k
    case X86_AND64rm:
2368
2369
    // BTC
2370
5.95k
    case X86_BTC16mi8:
2371
6.03k
    case X86_BTC16mr:
2372
6.10k
    case X86_BTC32mi8:
2373
6.15k
    case X86_BTC32mr:
2374
6.16k
    case X86_BTC64mi8:
2375
6.17k
    case X86_BTC64mr:
2376
2377
    // BTR
2378
6.26k
    case X86_BTR16mi8:
2379
6.29k
    case X86_BTR16mr:
2380
6.34k
    case X86_BTR32mi8:
2381
6.40k
    case X86_BTR32mr:
2382
6.45k
    case X86_BTR64mi8:
2383
6.46k
    case X86_BTR64mr:
2384
2385
    // BTS
2386
6.73k
    case X86_BTS16mi8:
2387
6.78k
    case X86_BTS16mr:
2388
7.02k
    case X86_BTS32mi8:
2389
7.22k
    case X86_BTS32mr:
2390
7.42k
    case X86_BTS64mi8:
2391
7.50k
    case X86_BTS64mr:
2392
2393
    // CMPXCHG
2394
7.57k
    case X86_CMPXCHG16B:
2395
7.63k
    case X86_CMPXCHG16rm:
2396
7.69k
    case X86_CMPXCHG32rm:
2397
7.86k
    case X86_CMPXCHG64rm:
2398
7.91k
    case X86_CMPXCHG8rm:
2399
7.92k
    case X86_CMPXCHG8B:
2400
2401
    // INC
2402
8.00k
    case X86_INC16m:
2403
8.16k
    case X86_INC32m:
2404
8.24k
    case X86_INC64m:
2405
8.28k
    case X86_INC8m:
2406
2407
    // NEG
2408
8.37k
    case X86_NEG16m:
2409
8.45k
    case X86_NEG32m:
2410
8.47k
    case X86_NEG64m:
2411
8.57k
    case X86_NEG8m:
2412
2413
    // NOT
2414
8.62k
    case X86_NOT16m:
2415
9.09k
    case X86_NOT32m:
2416
9.18k
    case X86_NOT64m:
2417
9.58k
    case X86_NOT8m:
2418
2419
    // OR
2420
9.61k
    case X86_OR16mi:
2421
9.68k
    case X86_OR16mi8:
2422
9.74k
    case X86_OR16mr:
2423
10.1k
    case X86_OR32mi:
2424
10.2k
    case X86_OR32mi8:
2425
10.3k
    case X86_OR32mr:
2426
10.4k
    case X86_OR64mi32:
2427
10.4k
    case X86_OR64mi8:
2428
10.5k
    case X86_OR64mr:
2429
10.5k
    case X86_OR8mi8:
2430
10.6k
    case X86_OR8mi:
2431
10.6k
    case X86_OR8mr:
2432
10.7k
    case X86_OR8rm:
2433
10.7k
    case X86_OR16rm:
2434
10.9k
    case X86_OR32rm:
2435
11.0k
    case X86_OR64rm:
2436
2437
    // SBB
2438
11.1k
    case X86_SBB16mi:
2439
11.3k
    case X86_SBB16mi8:
2440
11.5k
    case X86_SBB16mr:
2441
11.6k
    case X86_SBB32mi:
2442
11.6k
    case X86_SBB32mi8:
2443
11.7k
    case X86_SBB32mr:
2444
12.1k
    case X86_SBB64mi32:
2445
12.2k
    case X86_SBB64mi8:
2446
12.2k
    case X86_SBB64mr:
2447
12.4k
    case X86_SBB8mi:
2448
12.5k
    case X86_SBB8mi8:
2449
12.6k
    case X86_SBB8mr:
2450
2451
    // SUB
2452
12.7k
    case X86_SUB16mi:
2453
12.7k
    case X86_SUB16mi8:
2454
12.9k
    case X86_SUB16mr:
2455
13.0k
    case X86_SUB32mi:
2456
13.1k
    case X86_SUB32mi8:
2457
13.2k
    case X86_SUB32mr:
2458
13.2k
    case X86_SUB64mi32:
2459
13.2k
    case X86_SUB64mi8:
2460
13.3k
    case X86_SUB64mr:
2461
13.3k
    case X86_SUB8mi8:
2462
13.4k
    case X86_SUB8mi:
2463
13.7k
    case X86_SUB8mr:
2464
13.7k
    case X86_SUB8rm:
2465
13.9k
    case X86_SUB16rm:
2466
14.0k
    case X86_SUB32rm:
2467
14.0k
    case X86_SUB64rm:
2468
2469
    // XADD
2470
14.0k
    case X86_XADD16rm:
2471
14.2k
    case X86_XADD32rm:
2472
14.3k
    case X86_XADD64rm:
2473
14.5k
    case X86_XADD8rm:
2474
2475
    // XCHG
2476
14.7k
    case X86_XCHG16rm:
2477
14.8k
    case X86_XCHG32rm:
2478
15.0k
    case X86_XCHG64rm:
2479
15.2k
    case X86_XCHG8rm:
2480
2481
    // XOR
2482
15.3k
    case X86_XOR16mi:
2483
15.5k
    case X86_XOR16mi8:
2484
15.6k
    case X86_XOR16mr:
2485
15.9k
    case X86_XOR32mi:
2486
16.2k
    case X86_XOR32mi8:
2487
16.3k
    case X86_XOR32mr:
2488
16.5k
    case X86_XOR64mi32:
2489
16.7k
    case X86_XOR64mi8:
2490
16.8k
    case X86_XOR64mr:
2491
16.8k
    case X86_XOR8mi8:
2492
17.0k
    case X86_XOR8mi:
2493
17.2k
    case X86_XOR8mr:
2494
17.3k
    case X86_XOR8rm:
2495
17.4k
    case X86_XOR16rm:
2496
17.7k
    case X86_XOR32rm:
2497
17.8k
    case X86_XOR64rm:
2498
2499
      // this instruction can be used with LOCK prefix
2500
17.8k
      return false;
2501
17.9k
    }
2502
17.9k
  }
2503
2504
#if 0
2505
  // REPNE prefix
2506
  if (insn->repeatPrefix) {
2507
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2508
    // In such a case, clear it.
2509
    if (insn->twoByteEscape == 0x0f) {
2510
      insn->prefix0 = 0;
2511
    }
2512
  }
2513
#endif
2514
2515
  // no invalid prefixes
2516
585k
  return false;
2517
603k
}
2518
2519
/*
2520
 * decodeInstruction - Reads and interprets a full instruction provided by the
2521
 *   user.
2522
 *
2523
 * @param insn      - A pointer to the instruction to be populated.  Must be
2524
 *                    pre-allocated.
2525
 * @param reader    - The function to be used to read the instruction's bytes.
2526
 * @param readerArg - A generic argument to be passed to the reader to store
2527
 *                    any internal state.
2528
 * @param startLoc  - The address (in the reader's address space) of the first
2529
 *                    byte in the instruction.
2530
 * @param mode      - Capstone mode flags.
2531
 * @return          - 0 if instruction is valid; nonzero if not.
2532
 */
2533
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader,
2534
          const void *readerArg, uint64_t startLoc, cs_mode mode)
2535
605k
{
2536
605k
  insn->reader = reader;
2537
605k
  insn->readerArg = readerArg;
2538
605k
  insn->startLocation = startLoc;
2539
605k
  insn->readerCursor = startLoc;
2540
605k
  if (x86_has_feature(mode, CS_MODE_16))
2541
193k
    insn->mode = MODE_16BIT;
2542
411k
  else if (x86_has_feature(mode, CS_MODE_32))
2543
195k
    insn->mode = MODE_32BIT;
2544
216k
  else
2545
216k
    insn->mode = MODE_64BIT;
2546
605k
  insn->numImmediatesConsumed = 0;
2547
2548
605k
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn, mode) ||
2549
604k
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2550
3.14k
    return -1;
2551
2552
602k
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2553
2554
  // instruction length must be <= 15 to be valid
2555
602k
  if (insn->length > 15)
2556
41
    return -1;
2557
2558
602k
  if (insn->operandSize == 0)
2559
602k
    insn->operandSize = insn->registerSize;
2560
2561
602k
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2562
2563
602k
  return 0;
2564
602k
}
2565
2566
#endif