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
1.51M
{
79
1.51M
  return CONTEXTS_SYM[attrMask];
80
1.51M
}
81
82
/*
83
 * modRMRequired - Reads the appropriate instruction table to determine whether
84
 *   the ModR/M byte is required to decode a particular instruction.
85
 *
86
 * @param type        - The opcode type (i.e., how many bytes it has).
87
 * @param insnContext - The context for the instruction, as returned by
88
 *                      contextForAttrs.
89
 * @param opcode      - The last byte of the instruction's opcode, not counting
90
 *                      ModR/M extensions and escapes.
91
 * @return            - true if the ModR/M byte is required, false otherwise.
92
 */
93
static int modRMRequired(OpcodeType type, InstructionContext insnContext,
94
       uint16_t opcode)
95
1.51M
{
96
1.51M
  const struct OpcodeDecision *decision = NULL;
97
1.51M
  const uint8_t *indextable = NULL;
98
1.51M
  unsigned int index;
99
100
1.51M
  switch (type) {
101
0
  default:
102
0
    return false;
103
1.30M
  case ONEBYTE:
104
1.30M
    decision = ONEBYTE_SYM;
105
1.30M
    indextable = index_x86DisassemblerOneByteOpcodes;
106
1.30M
    break;
107
119k
  case TWOBYTE:
108
119k
    decision = TWOBYTE_SYM;
109
119k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
119k
    break;
111
36.5k
  case THREEBYTE_38:
112
36.5k
    decision = THREEBYTE38_SYM;
113
36.5k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
36.5k
    break;
115
42.4k
  case THREEBYTE_3A:
116
42.4k
    decision = THREEBYTE3A_SYM;
117
42.4k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
42.4k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
11.4k
  case XOP8_MAP:
121
11.4k
    decision = XOP8_MAP_SYM;
122
11.4k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
11.4k
    break;
124
2.68k
  case XOP9_MAP:
125
2.68k
    decision = XOP9_MAP_SYM;
126
2.68k
    indextable = index_x86DisassemblerXOP9Opcodes;
127
2.68k
    break;
128
812
  case XOPA_MAP:
129
812
    decision = XOPA_MAP_SYM;
130
812
    indextable = index_x86DisassemblerXOPAOpcodes;
131
812
    break;
132
893
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
893
    return true;
135
1.51M
#endif
136
1.51M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.51M
  index = indextable[insnContext];
140
1.51M
  if (index)
141
1.50M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.50M
           MODRM_ONEENTRY;
143
4.41k
  else
144
4.41k
    return false;
145
1.51M
}
146
147
/*
148
 * decode - Reads the appropriate instruction table to obtain the unique ID of
149
 *   an instruction.
150
 *
151
 * @param type        - See modRMRequired().
152
 * @param insnContext - See modRMRequired().
153
 * @param opcode      - See modRMRequired().
154
 * @param modRM       - The ModR/M byte if required, or any value if not.
155
 * @return            - The UID of the instruction, or 0 on failure.
156
 */
157
static InstrUID decode(OpcodeType type, InstructionContext insnContext,
158
           uint8_t opcode, uint8_t modRM)
159
1.51M
{
160
1.51M
  const struct ModRMDecision *dec = NULL;
161
1.51M
  unsigned int index;
162
1.51M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.51M
  switch (type) {
165
0
  default:
166
0
    return 0;
167
1.29M
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
1.29M
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
1.29M
    if (index)
171
1.29M
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
274
    else
173
274
      dec = &emptyDecision.modRMDecisions[opcode];
174
1.29M
    break;
175
119k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
119k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
119k
    if (index)
179
117k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
1.88k
    else
181
1.88k
      dec = &emptyDecision.modRMDecisions[opcode];
182
119k
    break;
183
36.4k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
36.4k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
36.4k
    if (index)
187
36.2k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
276
    else
189
276
      dec = &emptyDecision.modRMDecisions[opcode];
190
36.4k
    break;
191
42.4k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
42.4k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
42.4k
    if (index)
195
42.2k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
220
    else
197
220
      dec = &emptyDecision.modRMDecisions[opcode];
198
42.4k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
11.4k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
11.4k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
11.4k
    if (index)
204
10.5k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
870
    else
206
870
      dec = &emptyDecision.modRMDecisions[opcode];
207
11.4k
    break;
208
2.68k
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
2.68k
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
2.68k
    if (index)
212
2.00k
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
678
    else
214
678
      dec = &emptyDecision.modRMDecisions[opcode];
215
2.68k
    break;
216
812
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
812
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
812
    if (index)
220
602
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
210
    else
222
210
      dec = &emptyDecision.modRMDecisions[opcode];
223
812
    break;
224
893
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
893
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
893
    if (index)
228
593
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
593
               .modRMDecisions[opcode];
230
300
    else
231
300
      dec = &emptyDecision.modRMDecisions[opcode];
232
893
    break;
233
1.51M
#endif
234
1.51M
  }
235
236
1.51M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
718k
  case MODRM_ONEENTRY:
241
718k
    return modRMTable[dec->instructionIDs];
242
605k
  case MODRM_SPLITRM:
243
605k
    if (modFromModRM(modRM) == 0x3)
244
132k
      return modRMTable[dec->instructionIDs + 1];
245
472k
    return modRMTable[dec->instructionIDs];
246
161k
  case MODRM_SPLITREG:
247
161k
    if (modFromModRM(modRM) == 0x3)
248
50.3k
      return modRMTable[dec->instructionIDs +
249
50.3k
            ((modRM & 0x38) >> 3) + 8];
250
110k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
28.0k
  case MODRM_SPLITMISC:
252
28.0k
    if (modFromModRM(modRM) == 0x3)
253
7.90k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
7.90k
            8];
255
20.1k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.51M
  }
259
1.51M
}
260
261
/*
262
 * specifierForUID - Given a UID, returns the name and operand specification for
263
 *   that instruction.
264
 *
265
 * @param uid - The unique ID for the instruction.  This should be returned by
266
 *              decode(); specifierForUID will not check bounds.
267
 * @return    - A pointer to the specification for that instruction.
268
 */
269
static const struct InstructionSpecifier *specifierForUID(InstrUID uid)
270
1.28M
{
271
1.28M
  return &INSTRUCTIONS_SYM[uid];
272
1.28M
}
273
274
/*
275
 * consumeByte - Uses the reader function provided by the user to consume one
276
 *   byte from the instruction's memory and advance the cursor.
277
 *
278
 * @param insn  - The instruction with the reader function to use.  The cursor
279
 *                for this instruction is advanced.
280
 * @param byte  - A pointer to a pre-allocated memory buffer to be populated
281
 *                with the data read.
282
 * @return      - 0 if the read was successful; nonzero otherwise.
283
 */
284
static int consumeByte(struct InternalInstruction *insn, uint8_t *byte)
285
3.90M
{
286
3.90M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
3.90M
  if (!ret)
289
3.90M
    ++(insn->readerCursor);
290
291
3.90M
  return ret;
292
3.90M
}
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
314k
{
303
314k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
314k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
1.27M
{
308
1.27M
  insn->readerCursor--;
309
1.27M
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
239k
  { \
314
239k
    type combined = 0; \
315
239k
    unsigned offset; \
316
785k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
547k
      uint8_t byte; \
318
547k
      int ret = insn->reader(insn->readerArg, &byte, \
319
547k
                 insn->readerCursor + offset); \
320
547k
      if (ret) \
321
547k
        return ret; \
322
547k
      combined = combined | \
323
545k
           ((uint64_t)byte << (offset * 8)); \
324
545k
    } \
325
239k
    *ptr = combined; \
326
238k
    insn->readerCursor += sizeof(type); \
327
238k
    return 0; \
328
239k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
96.8k
  { \
314
96.8k
    type combined = 0; \
315
96.8k
    unsigned offset; \
316
193k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
96.8k
      uint8_t byte; \
318
96.8k
      int ret = insn->reader(insn->readerArg, &byte, \
319
96.8k
                 insn->readerCursor + offset); \
320
96.8k
      if (ret) \
321
96.8k
        return ret; \
322
96.8k
      combined = combined | \
323
96.7k
           ((uint64_t)byte << (offset * 8)); \
324
96.7k
    } \
325
96.8k
    *ptr = combined; \
326
96.7k
    insn->readerCursor += sizeof(type); \
327
96.7k
    return 0; \
328
96.8k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
24.3k
  { \
314
24.3k
    type combined = 0; \
315
24.3k
    unsigned offset; \
316
73.0k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
48.7k
      uint8_t byte; \
318
48.7k
      int ret = insn->reader(insn->readerArg, &byte, \
319
48.7k
                 insn->readerCursor + offset); \
320
48.7k
      if (ret) \
321
48.7k
        return ret; \
322
48.7k
      combined = combined | \
323
48.6k
           ((uint64_t)byte << (offset * 8)); \
324
48.6k
    } \
325
24.3k
    *ptr = combined; \
326
24.2k
    insn->readerCursor += sizeof(type); \
327
24.2k
    return 0; \
328
24.3k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
33.9k
  { \
314
33.9k
    type combined = 0; \
315
33.9k
    unsigned offset; \
316
168k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
135k
      uint8_t byte; \
318
135k
      int ret = insn->reader(insn->readerArg, &byte, \
319
135k
                 insn->readerCursor + offset); \
320
135k
      if (ret) \
321
135k
        return ret; \
322
135k
      combined = combined | \
323
134k
           ((uint64_t)byte << (offset * 8)); \
324
134k
    } \
325
33.9k
    *ptr = combined; \
326
33.6k
    insn->readerCursor += sizeof(type); \
327
33.6k
    return 0; \
328
33.9k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
44.8k
  { \
314
44.8k
    type combined = 0; \
315
44.8k
    unsigned offset; \
316
134k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
89.6k
      uint8_t byte; \
318
89.6k
      int ret = insn->reader(insn->readerArg, &byte, \
319
89.6k
                 insn->readerCursor + offset); \
320
89.6k
      if (ret) \
321
89.6k
        return ret; \
322
89.6k
      combined = combined | \
323
89.4k
           ((uint64_t)byte << (offset * 8)); \
324
89.4k
    } \
325
44.8k
    *ptr = combined; \
326
44.6k
    insn->readerCursor += sizeof(type); \
327
44.6k
    return 0; \
328
44.8k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
34.8k
  { \
314
34.8k
    type combined = 0; \
315
34.8k
    unsigned offset; \
316
173k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
138k
      uint8_t byte; \
318
138k
      int ret = insn->reader(insn->readerArg, &byte, \
319
138k
                 insn->readerCursor + offset); \
320
138k
      if (ret) \
321
138k
        return ret; \
322
138k
      combined = combined | \
323
138k
           ((uint64_t)byte << (offset * 8)); \
324
138k
    } \
325
34.8k
    *ptr = combined; \
326
34.4k
    insn->readerCursor += sizeof(type); \
327
34.4k
    return 0; \
328
34.8k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
4.79k
  { \
314
4.79k
    type combined = 0; \
315
4.79k
    unsigned offset; \
316
42.6k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
37.9k
      uint8_t byte; \
318
37.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
37.9k
                 insn->readerCursor + offset); \
320
37.9k
      if (ret) \
321
37.9k
        return ret; \
322
37.9k
      combined = combined | \
323
37.8k
           ((uint64_t)byte << (offset * 8)); \
324
37.8k
    } \
325
4.79k
    *ptr = combined; \
326
4.69k
    insn->readerCursor += sizeof(type); \
327
4.69k
    return 0; \
328
4.79k
  }
329
330
/*
331
 * consume* - Use the reader function provided by the user to consume data
332
 *   values of various sizes from the instruction's memory and advance the
333
 *   cursor appropriately.  These readers perform endian conversion.
334
 *
335
 * @param insn    - See consumeByte().
336
 * @param ptr     - A pointer to a pre-allocated memory of appropriate size to
337
 *                  be populated with the data read.
338
 * @return        - See consumeByte().
339
 */
340
CONSUME_FUNC(consumeInt8, int8_t)
341
CONSUME_FUNC(consumeInt16, int16_t)
342
CONSUME_FUNC(consumeInt32, int32_t)
343
CONSUME_FUNC(consumeUInt16, uint16_t)
344
CONSUME_FUNC(consumeUInt32, uint32_t)
345
CONSUME_FUNC(consumeUInt64, uint64_t)
346
347
static bool isREX(struct InternalInstruction *insn, uint8_t prefix)
348
1.20M
{
349
1.20M
  if (insn->mode == MODE_64BIT)
350
483k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
725k
  return false;
353
1.20M
}
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
1.51M
{
920
1.51M
  bool hasModRMExtension;
921
922
1.51M
  InstructionContext instructionClass = contextForAttrs(attrMask);
923
924
1.51M
  hasModRMExtension =
925
1.51M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
926
927
1.51M
  if (hasModRMExtension) {
928
796k
    if (readModRM(insn))
929
2.11k
      return -1;
930
931
794k
    *instructionID = decode(insn->opcodeType, instructionClass,
932
794k
          insn->opcode, insn->modRM);
933
794k
  } else {
934
717k
    *instructionID = decode(insn->opcodeType, instructionClass,
935
717k
          insn->opcode, 0);
936
717k
  }
937
938
1.51M
  return 0;
939
1.51M
}
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
337k
{
950
337k
  size_t i;
951
337k
  uint16_t idx;
952
953
337k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
954
165k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
955
165k
          x86_16_bit_eq_tbl[i].first == orig;
956
162k
         i++) {
957
162k
      if (x86_16_bit_eq_tbl[i].second == equiv)
958
159k
        return true;
959
162k
    }
960
162k
  }
961
962
178k
  return false;
963
337k
}
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
20.4k
{
972
20.4k
  unsigned int i = find_insn(id);
973
20.4k
  if (i != -1) {
974
20.3k
    return insns[i].is64bit;
975
20.3k
  }
976
977
  // not found??
978
101
  return false;
979
20.4k
}
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
32.1k
{
1501
32.1k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1502
32.1k
  uint8_t index, base;
1503
1504
  // dbgprintf(insn, "readSIB()");
1505
1506
32.1k
  if (insn->consumedSIB)
1507
0
    return 0;
1508
1509
32.1k
  insn->consumedSIB = true;
1510
1511
32.1k
  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
12.9k
  case 4:
1516
12.9k
    insn->sibIndexBase = SIB_INDEX_EAX;
1517
12.9k
    sibBaseBase = SIB_BASE_EAX;
1518
12.9k
    break;
1519
19.1k
  case 8:
1520
19.1k
    insn->sibIndexBase = SIB_INDEX_RAX;
1521
19.1k
    sibBaseBase = SIB_BASE_RAX;
1522
19.1k
    break;
1523
32.1k
  }
1524
1525
32.1k
  if (consumeByte(insn, &insn->sib))
1526
70
    return -1;
1527
1528
32.0k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1529
1530
32.0k
  if (index == 0x4) {
1531
5.75k
    insn->sibIndex = SIB_INDEX_NONE;
1532
26.3k
  } else {
1533
26.3k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1534
26.3k
  }
1535
1536
32.0k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1537
1538
32.0k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1539
1540
32.0k
  switch (base) {
1541
2.53k
  case 0x5:
1542
3.77k
  case 0xd:
1543
3.77k
    switch (modFromModRM(insn->modRM)) {
1544
1.84k
    case 0x0:
1545
1.84k
      insn->eaDisplacement = EA_DISP_32;
1546
1.84k
      insn->sibBase = SIB_BASE_NONE;
1547
1.84k
      break;
1548
1.60k
    case 0x1:
1549
1.60k
      insn->eaDisplacement = EA_DISP_8;
1550
1.60k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1551
1.60k
      break;
1552
314
    case 0x2:
1553
314
      insn->eaDisplacement = EA_DISP_32;
1554
314
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1555
314
      break;
1556
0
    case 0x3:
1557
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1558
0
      return -1;
1559
3.77k
    }
1560
3.77k
    break;
1561
28.2k
  default:
1562
28.2k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1563
28.2k
    break;
1564
32.0k
  }
1565
1566
32.0k
  return 0;
1567
32.0k
}
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
209k
{
1578
209k
  int8_t d8;
1579
209k
  int16_t d16;
1580
209k
  int32_t d32;
1581
1582
  // dbgprintf(insn, "readDisplacement()");
1583
1584
209k
  if (insn->consumedDisplacement)
1585
0
    return 0;
1586
1587
209k
  insn->consumedDisplacement = true;
1588
209k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1589
1590
209k
  switch (insn->eaDisplacement) {
1591
53.9k
  case EA_DISP_NONE:
1592
53.9k
    insn->consumedDisplacement = false;
1593
53.9k
    break;
1594
96.8k
  case EA_DISP_8:
1595
96.8k
    if (consumeInt8(insn, &d8))
1596
180
      return -1;
1597
96.7k
    insn->displacement = d8;
1598
96.7k
    break;
1599
24.3k
  case EA_DISP_16:
1600
24.3k
    if (consumeInt16(insn, &d16))
1601
89
      return -1;
1602
24.2k
    insn->displacement = d16;
1603
24.2k
    break;
1604
33.9k
  case EA_DISP_32:
1605
33.9k
    if (consumeInt32(insn, &d32))
1606
351
      return -1;
1607
33.6k
    insn->displacement = d32;
1608
33.6k
    break;
1609
209k
  }
1610
1611
208k
  return 0;
1612
209k
}
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
1.83M
{
1623
1.83M
  uint8_t mod, rm, reg, evexrm;
1624
1625
  // dbgprintf(insn, "readModRM()");
1626
1627
1.83M
  if (insn->consumedModRM)
1628
1.23M
    return 0;
1629
1630
596k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1631
1632
596k
  if (consumeByte(insn, &insn->modRM))
1633
1.43k
    return -1;
1634
1635
594k
  insn->consumedModRM = true;
1636
1637
  // save original ModRM for later reference
1638
594k
  insn->orgModRM = insn->modRM;
1639
1640
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1641
594k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1642
46.5k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1643
1.45k
    insn->modRM |= 0xC0;
1644
1645
594k
  mod = modFromModRM(insn->modRM);
1646
594k
  rm = rmFromModRM(insn->modRM);
1647
594k
  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
594k
  switch (insn->registerSize) {
1655
196k
  case 2:
1656
196k
    insn->regBase = MODRM_REG_AX;
1657
196k
    insn->eaRegBase = EA_REG_AX;
1658
196k
    break;
1659
350k
  case 4:
1660
350k
    insn->regBase = MODRM_REG_EAX;
1661
350k
    insn->eaRegBase = EA_REG_EAX;
1662
350k
    break;
1663
47.5k
  case 8:
1664
47.5k
    insn->regBase = MODRM_REG_RAX;
1665
47.5k
    insn->eaRegBase = EA_REG_RAX;
1666
47.5k
    break;
1667
594k
  }
1668
1669
594k
  reg |= rFromREX(insn->rexPrefix) << 3;
1670
594k
  rm |= bFromREX(insn->rexPrefix) << 3;
1671
1672
594k
  evexrm = 0;
1673
594k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1674
61.0k
      insn->mode == MODE_64BIT) {
1675
27.3k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1676
27.3k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1677
27.3k
  }
1678
1679
594k
  insn->reg = (Reg)(insn->regBase + reg);
1680
1681
594k
  switch (insn->addressSize) {
1682
181k
  case 2: {
1683
181k
    EABase eaBaseBase = EA_BASE_BX_SI;
1684
1685
181k
    switch (mod) {
1686
97.4k
    case 0x0:
1687
97.4k
      if (rm == 0x6) {
1688
5.33k
        insn->eaBase = EA_BASE_NONE;
1689
5.33k
        insn->eaDisplacement = EA_DISP_16;
1690
5.33k
        if (readDisplacement(insn))
1691
20
          return -1;
1692
92.1k
      } else {
1693
92.1k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1694
92.1k
        insn->eaDisplacement = EA_DISP_NONE;
1695
92.1k
      }
1696
97.4k
      break;
1697
97.4k
    case 0x1:
1698
27.9k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1699
27.9k
      insn->eaDisplacement = EA_DISP_8;
1700
27.9k
      insn->displacementSize = 1;
1701
27.9k
      if (readDisplacement(insn))
1702
55
        return -1;
1703
27.9k
      break;
1704
27.9k
    case 0x2:
1705
19.0k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1706
19.0k
      insn->eaDisplacement = EA_DISP_16;
1707
19.0k
      if (readDisplacement(insn))
1708
69
        return -1;
1709
18.9k
      break;
1710
36.8k
    case 0x3:
1711
36.8k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1712
36.8k
      if (readDisplacement(insn))
1713
0
        return -1;
1714
36.8k
      break;
1715
181k
    }
1716
181k
    break;
1717
181k
  }
1718
1719
187k
  case 4:
1720
413k
  case 8: {
1721
413k
    EABase eaBaseBase =
1722
413k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1723
1724
413k
    switch (mod) {
1725
0
    default:
1726
0
      break;
1727
207k
    case 0x0:
1728
207k
      insn->eaDisplacement =
1729
207k
        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
207k
      switch (rm & 7) {
1734
19.0k
      case 0x4: // SIB byte is present
1735
19.0k
        insn->eaBase = (insn->addressSize == 4 ?
1736
7.60k
              EA_BASE_sib :
1737
19.0k
              EA_BASE_sib64);
1738
19.0k
        if (readSIB(insn) || readDisplacement(insn))
1739
47
          return -1;
1740
18.9k
        break;
1741
18.9k
      case 0x5: // RIP-relative
1742
5.65k
        insn->eaBase = EA_BASE_NONE;
1743
5.65k
        insn->eaDisplacement = EA_DISP_32;
1744
5.65k
        if (readDisplacement(insn))
1745
56
          return -1;
1746
5.59k
        break;
1747
183k
      default:
1748
183k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1749
183k
        break;
1750
207k
      }
1751
207k
      break;
1752
207k
    case 0x1:
1753
68.9k
      insn->displacementSize = 1;
1754
      /* FALLTHROUGH */
1755
95.4k
    case 0x2:
1756
95.4k
      insn->eaDisplacement =
1757
95.4k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1758
95.4k
      switch (rm & 7) {
1759
13.0k
      case 0x4: // SIB byte is present
1760
13.0k
        insn->eaBase = EA_BASE_sib;
1761
13.0k
        if (readSIB(insn) || readDisplacement(insn))
1762
85
          return -1;
1763
13.0k
        break;
1764
82.3k
      default:
1765
82.3k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1766
82.3k
        if (readDisplacement(insn))
1767
358
          return -1;
1768
81.9k
        break;
1769
95.4k
      }
1770
94.9k
      break;
1771
110k
    case 0x3:
1772
110k
      insn->eaDisplacement = EA_DISP_NONE;
1773
110k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1774
110k
      break;
1775
413k
    }
1776
1777
412k
    break;
1778
413k
  }
1779
594k
  } /* switch (insn->addressSize) */
1780
1781
594k
  return 0;
1782
594k
}
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
648k
  { \
1788
648k
    *valid = 1; \
1789
648k
    switch (type) { \
1790
0
    default: \
1791
0
      *valid = 0; \
1792
0
      return 0; \
1793
156k
    case TYPE_Rv: \
1794
156k
      return base + index; \
1795
246k
    case TYPE_R8: \
1796
246k
      index &= mask; \
1797
246k
      if (index > 0xf) \
1798
246k
        *valid = 0; \
1799
246k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1800
2.50k
        return prefix##_SPL + (index - 4); \
1801
243k
      } else { \
1802
243k
        return prefix##_AL + index; \
1803
243k
      } \
1804
246k
    case TYPE_R16: \
1805
4.67k
      index &= mask; \
1806
4.67k
      if (index > 0xf) \
1807
4.67k
        *valid = 0; \
1808
4.67k
      return prefix##_AX + index; \
1809
246k
    case TYPE_R32: \
1810
5.33k
      index &= mask; \
1811
5.33k
      if (index > 0xf) \
1812
5.33k
        *valid = 0; \
1813
5.33k
      return prefix##_EAX + index; \
1814
246k
    case TYPE_R64: \
1815
16.6k
      index &= mask; \
1816
16.6k
      if (index > 0xf) \
1817
16.6k
        *valid = 0; \
1818
16.6k
      return prefix##_RAX + index; \
1819
246k
    case TYPE_ZMM: \
1820
45.2k
      return prefix##_ZMM0 + index; \
1821
246k
    case TYPE_YMM: \
1822
42.8k
      return prefix##_YMM0 + index; \
1823
246k
    case TYPE_XMM: \
1824
82.7k
      return prefix##_XMM0 + index; \
1825
246k
    case TYPE_VK: \
1826
29.3k
      index &= 0xf; \
1827
29.3k
      if (index > 7) \
1828
29.3k
        *valid = 0; \
1829
29.3k
      return prefix##_K0 + index; \
1830
246k
    case TYPE_MM64: \
1831
6.18k
      return prefix##_MM0 + (index & 0x7); \
1832
246k
    case TYPE_SEGMENTREG: \
1833
2.78k
      if ((index & 7) > 5) \
1834
2.78k
        *valid = 0; \
1835
2.78k
      return prefix##_ES + (index & 7); \
1836
246k
    case TYPE_DEBUGREG: \
1837
950
      return prefix##_DR0 + index; \
1838
246k
    case TYPE_CONTROLREG: \
1839
507
      return prefix##_CR0 + index; \
1840
246k
    case TYPE_BNDR: \
1841
7.78k
      if (index > 3) \
1842
7.78k
        *valid = 0; \
1843
7.78k
      return prefix##_BND0 + index; \
1844
246k
    case TYPE_MVSIBX: \
1845
0
      return prefix##_XMM0 + index; \
1846
246k
    case TYPE_MVSIBY: \
1847
0
      return prefix##_YMM0 + index; \
1848
246k
    case TYPE_MVSIBZ: \
1849
0
      return prefix##_ZMM0 + index; \
1850
648k
    } \
1851
648k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1787
508k
  { \
1788
508k
    *valid = 1; \
1789
508k
    switch (type) { \
1790
0
    default: \
1791
0
      *valid = 0; \
1792
0
      return 0; \
1793
115k
    case TYPE_Rv: \
1794
115k
      return base + index; \
1795
202k
    case TYPE_R8: \
1796
202k
      index &= mask; \
1797
202k
      if (index > 0xf) \
1798
202k
        *valid = 0; \
1799
202k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1800
1.49k
        return prefix##_SPL + (index - 4); \
1801
200k
      } else { \
1802
200k
        return prefix##_AL + index; \
1803
200k
      } \
1804
202k
    case TYPE_R16: \
1805
3.40k
      index &= mask; \
1806
3.40k
      if (index > 0xf) \
1807
3.40k
        *valid = 0; \
1808
3.40k
      return prefix##_AX + index; \
1809
202k
    case TYPE_R32: \
1810
3.25k
      index &= mask; \
1811
3.25k
      if (index > 0xf) \
1812
3.25k
        *valid = 0; \
1813
3.25k
      return prefix##_EAX + index; \
1814
202k
    case TYPE_R64: \
1815
9.05k
      index &= mask; \
1816
9.05k
      if (index > 0xf) \
1817
9.05k
        *valid = 0; \
1818
9.05k
      return prefix##_RAX + index; \
1819
202k
    case TYPE_ZMM: \
1820
35.7k
      return prefix##_ZMM0 + index; \
1821
202k
    case TYPE_YMM: \
1822
32.9k
      return prefix##_YMM0 + index; \
1823
202k
    case TYPE_XMM: \
1824
64.5k
      return prefix##_XMM0 + index; \
1825
202k
    case TYPE_VK: \
1826
26.9k
      index &= 0xf; \
1827
26.9k
      if (index > 7) \
1828
26.9k
        *valid = 0; \
1829
26.9k
      return prefix##_K0 + index; \
1830
202k
    case TYPE_MM64: \
1831
4.18k
      return prefix##_MM0 + (index & 0x7); \
1832
202k
    case TYPE_SEGMENTREG: \
1833
2.78k
      if ((index & 7) > 5) \
1834
2.78k
        *valid = 0; \
1835
2.78k
      return prefix##_ES + (index & 7); \
1836
202k
    case TYPE_DEBUGREG: \
1837
950
      return prefix##_DR0 + index; \
1838
202k
    case TYPE_CONTROLREG: \
1839
507
      return prefix##_CR0 + index; \
1840
202k
    case TYPE_BNDR: \
1841
7.06k
      if (index > 3) \
1842
7.06k
        *valid = 0; \
1843
7.06k
      return prefix##_BND0 + index; \
1844
202k
    case TYPE_MVSIBX: \
1845
0
      return prefix##_XMM0 + index; \
1846
202k
    case TYPE_MVSIBY: \
1847
0
      return prefix##_YMM0 + index; \
1848
202k
    case TYPE_MVSIBZ: \
1849
0
      return prefix##_ZMM0 + index; \
1850
508k
    } \
1851
508k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1787
139k
  { \
1788
139k
    *valid = 1; \
1789
139k
    switch (type) { \
1790
0
    default: \
1791
0
      *valid = 0; \
1792
0
      return 0; \
1793
41.6k
    case TYPE_Rv: \
1794
41.6k
      return base + index; \
1795
44.2k
    case TYPE_R8: \
1796
44.2k
      index &= mask; \
1797
44.2k
      if (index > 0xf) \
1798
44.2k
        *valid = 0; \
1799
44.2k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1800
1.00k
        return prefix##_SPL + (index - 4); \
1801
43.2k
      } else { \
1802
43.2k
        return prefix##_AL + index; \
1803
43.2k
      } \
1804
44.2k
    case TYPE_R16: \
1805
1.26k
      index &= mask; \
1806
1.26k
      if (index > 0xf) \
1807
1.26k
        *valid = 0; \
1808
1.26k
      return prefix##_AX + index; \
1809
44.2k
    case TYPE_R32: \
1810
2.07k
      index &= mask; \
1811
2.07k
      if (index > 0xf) \
1812
2.07k
        *valid = 0; \
1813
2.07k
      return prefix##_EAX + index; \
1814
44.2k
    case TYPE_R64: \
1815
7.63k
      index &= mask; \
1816
7.63k
      if (index > 0xf) \
1817
7.63k
        *valid = 0; \
1818
7.63k
      return prefix##_RAX + index; \
1819
44.2k
    case TYPE_ZMM: \
1820
9.53k
      return prefix##_ZMM0 + index; \
1821
44.2k
    case TYPE_YMM: \
1822
9.81k
      return prefix##_YMM0 + index; \
1823
44.2k
    case TYPE_XMM: \
1824
18.1k
      return prefix##_XMM0 + index; \
1825
44.2k
    case TYPE_VK: \
1826
2.38k
      index &= 0xf; \
1827
2.38k
      if (index > 7) \
1828
2.38k
        *valid = 0; \
1829
2.38k
      return prefix##_K0 + index; \
1830
44.2k
    case TYPE_MM64: \
1831
1.99k
      return prefix##_MM0 + (index & 0x7); \
1832
44.2k
    case TYPE_SEGMENTREG: \
1833
0
      if ((index & 7) > 5) \
1834
0
        *valid = 0; \
1835
0
      return prefix##_ES + (index & 7); \
1836
44.2k
    case TYPE_DEBUGREG: \
1837
0
      return prefix##_DR0 + index; \
1838
44.2k
    case TYPE_CONTROLREG: \
1839
0
      return prefix##_CR0 + index; \
1840
44.2k
    case TYPE_BNDR: \
1841
713
      if (index > 3) \
1842
713
        *valid = 0; \
1843
713
      return prefix##_BND0 + index; \
1844
44.2k
    case TYPE_MVSIBX: \
1845
0
      return prefix##_XMM0 + index; \
1846
44.2k
    case TYPE_MVSIBY: \
1847
0
      return prefix##_YMM0 + index; \
1848
44.2k
    case TYPE_MVSIBZ: \
1849
0
      return prefix##_ZMM0 + index; \
1850
139k
    } \
1851
139k
  }
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
1.08M
{
1881
1.08M
  uint8_t valid;
1882
1883
1.08M
  switch ((OperandEncoding)op->encoding) {
1884
0
  default:
1885
    // debug("Expected a REG or R/M encoding in fixupReg");
1886
0
    return -1;
1887
61.2k
  case ENCODING_VVVV:
1888
61.2k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1889
61.2k
            insn->vvvv, &valid);
1890
61.2k
    if (!valid)
1891
2
      return -1;
1892
61.2k
    break;
1893
447k
  case ENCODING_REG:
1894
447k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1895
447k
                 insn->reg - insn->regBase,
1896
447k
                 &valid);
1897
447k
    if (!valid)
1898
24
      return -1;
1899
447k
    break;
1900
3.83M
CASE_ENCODING_RM:
1901
3.83M
    if (insn->eaBase >= insn->eaRegBase) {
1902
139k
      insn->eaBase = (EABase)fixupRMValue(
1903
139k
        insn, (OperandType)op->type,
1904
139k
        insn->eaBase - insn->eaRegBase, &valid);
1905
139k
      if (!valid)
1906
4
        return -1;
1907
139k
    }
1908
577k
    break;
1909
1.08M
  }
1910
1911
1.08M
  return 0;
1912
1.08M
}
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
129k
{
1927
129k
  if (size == 0)
1928
92.9k
    size = insn->registerSize;
1929
1930
129k
  switch (size) {
1931
15.8k
  case 1:
1932
15.8k
    insn->opcodeRegister =
1933
15.8k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1934
15.8k
                (insn->opcode & 7)));
1935
15.8k
    if (insn->rexPrefix &&
1936
956
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1937
579
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1938
118
      insn->opcodeRegister =
1939
118
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1940
118
                   MODRM_REG_AL - 4));
1941
118
    }
1942
1943
15.8k
    break;
1944
41.5k
  case 2:
1945
41.5k
    insn->opcodeRegister =
1946
41.5k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1947
41.5k
                (insn->opcode & 7)));
1948
41.5k
    break;
1949
50.8k
  case 4:
1950
50.8k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1951
50.8k
               ((bFromREX(insn->rexPrefix) << 3) |
1952
50.8k
                (insn->opcode & 7)));
1953
50.8k
    break;
1954
21.6k
  case 8:
1955
21.6k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1956
21.6k
               ((bFromREX(insn->rexPrefix) << 3) |
1957
21.6k
                (insn->opcode & 7)));
1958
21.6k
    break;
1959
129k
  }
1960
1961
129k
  return 0;
1962
129k
}
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
308k
{
1975
308k
  uint8_t imm8;
1976
308k
  uint16_t imm16;
1977
308k
  uint32_t imm32;
1978
308k
  uint64_t imm64;
1979
1980
308k
  if (insn->numImmediatesConsumed == 2) {
1981
    // debug("Already consumed two immediates");
1982
0
    return -1;
1983
0
  }
1984
1985
308k
  if (size == 0)
1986
0
    size = insn->immediateSize;
1987
308k
  else
1988
308k
    insn->immediateSize = size;
1989
1990
308k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1991
1992
308k
  switch (size) {
1993
223k
  case 1:
1994
223k
    if (consumeByte(insn, &imm8))
1995
508
      return -1;
1996
1997
223k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
1998
223k
    break;
1999
44.8k
  case 2:
2000
44.8k
    if (consumeUInt16(insn, &imm16))
2001
217
      return -1;
2002
2003
44.6k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
2004
44.6k
    break;
2005
34.8k
  case 4:
2006
34.8k
    if (consumeUInt32(insn, &imm32))
2007
439
      return -1;
2008
2009
34.4k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
2010
34.4k
    break;
2011
4.79k
  case 8:
2012
4.79k
    if (consumeUInt64(insn, &imm64))
2013
97
      return -1;
2014
4.69k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
2015
4.69k
    break;
2016
308k
  }
2017
2018
307k
  insn->numImmediatesConsumed++;
2019
2020
307k
  return 0;
2021
308k
}
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
1.12M
{
2032
1.12M
  int vvvv;
2033
2034
1.12M
  if (insn->vectorExtensionType == TYPE_EVEX)
2035
61.0k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2036
61.0k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2037
1.06M
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2038
6.28k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2039
1.05M
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2040
10.9k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2041
1.04M
  else if (insn->vectorExtensionType == TYPE_XOP)
2042
8.16k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2043
1.03M
  else
2044
1.03M
    return -1;
2045
2046
86.4k
  if (insn->mode != MODE_64BIT)
2047
49.2k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2048
2049
86.4k
  insn->vvvv = (Reg)vvvv;
2050
2051
86.4k
  return 0;
2052
1.12M
}
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
42.1k
{
2063
42.1k
  if (insn->vectorExtensionType != TYPE_EVEX)
2064
0
    return -1;
2065
2066
42.1k
  insn->writemask =
2067
42.1k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2068
2069
42.1k
  return 0;
2070
42.1k
}
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
1.12M
{
2081
1.12M
  int hasVVVV, needVVVV;
2082
1.12M
  int sawRegImm = 0;
2083
1.12M
  int i;
2084
2085
  /* If non-zero vvvv specified, need to make sure one of the operands
2086
     uses it. */
2087
1.12M
  hasVVVV = !readVVVV(insn);
2088
1.12M
  needVVVV = hasVVVV && (insn->vvvv != 0);
2089
2090
7.84M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2091
6.72M
    const OperandSpecifier *op =
2092
6.72M
      &x86OperandSets[insn->spec->operands][i];
2093
6.72M
    switch (op->encoding) {
2094
4.79M
    case ENCODING_NONE:
2095
4.83M
    case ENCODING_SI:
2096
4.89M
    case ENCODING_DI:
2097
4.89M
      break;
2098
2099
45.1k
CASE_ENCODING_VSIB:
2100
      // VSIB can use the V2 bit so check only the other bits.
2101
45.1k
      if (needVVVV)
2102
5.34k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2103
2104
45.1k
      if (readModRM(insn))
2105
0
        return -1;
2106
2107
      // Reject if SIB wasn't used.
2108
8.66k
      if (insn->eaBase != EA_BASE_sib &&
2109
5.33k
          insn->eaBase != EA_BASE_sib64)
2110
16
        return -1;
2111
2112
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2113
8.64k
      if (insn->sibIndex == SIB_INDEX_NONE)
2114
858
        insn->sibIndex =
2115
858
          (SIBIndex)(insn->sibIndexBase + 4);
2116
2117
      // If EVEX.v2 is set this is one of the 16-31 registers.
2118
8.64k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2119
6.65k
          insn->mode == MODE_64BIT &&
2120
4.40k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2121
3.33k
        insn->sibIndex =
2122
3.33k
          (SIBIndex)(insn->sibIndex + 16);
2123
2124
      // Adjust the index register to the correct size.
2125
8.64k
      switch (op->type) {
2126
0
      default:
2127
        // debug("Unhandled VSIB index type");
2128
0
        return -1;
2129
2.76k
      case TYPE_MVSIBX:
2130
2.76k
        insn->sibIndex =
2131
2.76k
          (SIBIndex)(SIB_INDEX_XMM0 +
2132
2.76k
               (insn->sibIndex -
2133
2.76k
                insn->sibIndexBase));
2134
2.76k
        break;
2135
3.36k
      case TYPE_MVSIBY:
2136
3.36k
        insn->sibIndex =
2137
3.36k
          (SIBIndex)(SIB_INDEX_YMM0 +
2138
3.36k
               (insn->sibIndex -
2139
3.36k
                insn->sibIndexBase));
2140
3.36k
        break;
2141
2.52k
      case TYPE_MVSIBZ:
2142
2.52k
        insn->sibIndex =
2143
2.52k
          (SIBIndex)(SIB_INDEX_ZMM0 +
2144
2.52k
               (insn->sibIndex -
2145
2.52k
                insn->sibIndexBase));
2146
2.52k
        break;
2147
8.64k
      }
2148
2149
      // Apply the AVX512 compressed displacement scaling factor.
2150
8.64k
      if (op->encoding != ENCODING_REG &&
2151
8.64k
          insn->eaDisplacement == EA_DISP_8)
2152
1.21k
        insn->displacement *=
2153
1.21k
          1 << (op->encoding - ENCODING_VSIB);
2154
8.64k
      break;
2155
2156
447k
    case ENCODING_REG:
2157
6.97M
CASE_ENCODING_RM:
2158
6.97M
      if (readModRM(insn))
2159
0
        return -1;
2160
2161
1.02M
      if (fixupReg(insn, op))
2162
28
        return -1;
2163
2164
      // Apply the AVX512 compressed displacement scaling factor.
2165
1.02M
      if (op->encoding != ENCODING_REG &&
2166
577k
          insn->eaDisplacement == EA_DISP_8)
2167
95.4k
        insn->displacement *=
2168
95.4k
          1 << (op->encoding - ENCODING_RM);
2169
1.02M
      break;
2170
2171
224k
    case ENCODING_IB:
2172
224k
      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
1.14k
        insn->immediates[insn->numImmediatesConsumed] =
2176
1.14k
          insn->immediates
2177
1.14k
            [insn->numImmediatesConsumed -
2178
1.14k
             1] &
2179
1.14k
          0xf;
2180
1.14k
        ++insn->numImmediatesConsumed;
2181
1.14k
        break;
2182
1.14k
      }
2183
223k
      if (readImmediate(insn, 1))
2184
508
        return -1;
2185
223k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2186
1.93k
        sawRegImm = 1;
2187
223k
      break;
2188
2189
13.8k
    case ENCODING_IW:
2190
13.8k
      if (readImmediate(insn, 2))
2191
66
        return -1;
2192
13.7k
      break;
2193
2194
13.7k
    case ENCODING_ID:
2195
7.39k
      if (readImmediate(insn, 4))
2196
77
        return -1;
2197
7.32k
      break;
2198
2199
7.32k
    case ENCODING_IO:
2200
532
      if (readImmediate(insn, 8))
2201
10
        return -1;
2202
522
      break;
2203
2204
50.2k
    case ENCODING_Iv:
2205
50.2k
      if (readImmediate(insn, insn->immediateSize))
2206
440
        return -1;
2207
49.7k
      break;
2208
2209
49.7k
    case ENCODING_Ia:
2210
12.5k
      if (readImmediate(insn, insn->addressSize))
2211
160
        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
12.3k
      insn->displacementOffset = insn->immediateOffset;
2216
12.3k
      insn->consumedDisplacement = true;
2217
12.3k
      insn->displacementSize = insn->immediateSize;
2218
12.3k
      insn->displacement =
2219
12.3k
        insn->immediates[insn->numImmediatesConsumed -
2220
12.3k
             1];
2221
12.3k
      insn->immediateOffset = 0;
2222
12.3k
      insn->immediateSize = 0;
2223
12.3k
      break;
2224
2225
3.95k
    case ENCODING_IRC:
2226
3.95k
      insn->RC =
2227
3.95k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2228
3.95k
         << 1) |
2229
3.95k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2230
3.95k
      break;
2231
2232
15.8k
    case ENCODING_RB:
2233
15.8k
      if (readOpcodeRegister(insn, 1))
2234
0
        return -1;
2235
15.8k
      break;
2236
2237
15.8k
    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
21.1k
    case ENCODING_RO:
2248
21.1k
      if (readOpcodeRegister(insn, 8))
2249
0
        return -1;
2250
21.1k
      break;
2251
2252
92.9k
    case ENCODING_Rv:
2253
92.9k
      if (readOpcodeRegister(insn, 0))
2254
0
        return -1;
2255
92.9k
      break;
2256
2257
92.9k
    case ENCODING_FP:
2258
4.89k
      break;
2259
2260
61.2k
    case ENCODING_VVVV:
2261
61.2k
      if (!hasVVVV)
2262
0
        return -1;
2263
2264
61.2k
      needVVVV =
2265
61.2k
        0; /* Mark that we have found a VVVV operand. */
2266
2267
61.2k
      if (insn->mode != MODE_64BIT)
2268
34.8k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2269
2270
61.2k
      if (fixupReg(insn, op))
2271
2
        return -1;
2272
61.2k
      break;
2273
2274
61.2k
    case ENCODING_WRITEMASK:
2275
42.1k
      if (readMaskRegister(insn))
2276
0
        return -1;
2277
42.1k
      break;
2278
2279
240k
    case ENCODING_DUP:
2280
240k
      break;
2281
2282
0
    default:
2283
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2284
0
      return -1;
2285
6.72M
    }
2286
6.72M
  }
2287
2288
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2289
1.12M
  if (needVVVV)
2290
20
    return -1;
2291
2292
1.12M
  return 0;
2293
1.12M
}
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
1.12M
{
2299
  // LOCK prefix
2300
1.12M
  if (insn->hasLockPrefix) {
2301
41.1k
    switch (insn->instructionID) {
2302
248
    default:
2303
      // invalid LOCK
2304
248
      return true;
2305
2306
    // nop dword [rax]
2307
27
    case X86_NOOPL:
2308
2309
    // DEC
2310
122
    case X86_DEC16m:
2311
506
    case X86_DEC32m:
2312
704
    case X86_DEC64m:
2313
1.04k
    case X86_DEC8m:
2314
2315
    // ADC
2316
1.44k
    case X86_ADC16mi:
2317
1.93k
    case X86_ADC16mi8:
2318
2.15k
    case X86_ADC16mr:
2319
2.34k
    case X86_ADC32mi:
2320
2.48k
    case X86_ADC32mi8:
2321
2.81k
    case X86_ADC32mr:
2322
3.16k
    case X86_ADC64mi32:
2323
3.42k
    case X86_ADC64mi8:
2324
3.63k
    case X86_ADC64mr:
2325
3.83k
    case X86_ADC8mi:
2326
4.13k
    case X86_ADC8mi8:
2327
4.47k
    case X86_ADC8mr:
2328
4.55k
    case X86_ADC8rm:
2329
4.63k
    case X86_ADC16rm:
2330
4.72k
    case X86_ADC32rm:
2331
4.94k
    case X86_ADC64rm:
2332
2333
    // ADD
2334
5.19k
    case X86_ADD16mi:
2335
5.50k
    case X86_ADD16mi8:
2336
5.76k
    case X86_ADD16mr:
2337
6.08k
    case X86_ADD32mi:
2338
6.40k
    case X86_ADD32mi8:
2339
6.80k
    case X86_ADD32mr:
2340
7.08k
    case X86_ADD64mi32:
2341
7.56k
    case X86_ADD64mi8:
2342
7.75k
    case X86_ADD64mr:
2343
7.94k
    case X86_ADD8mi:
2344
8.21k
    case X86_ADD8mi8:
2345
8.71k
    case X86_ADD8mr:
2346
8.91k
    case X86_ADD8rm:
2347
9.19k
    case X86_ADD16rm:
2348
9.38k
    case X86_ADD32rm:
2349
9.62k
    case X86_ADD64rm:
2350
2351
    // AND
2352
10.0k
    case X86_AND16mi:
2353
10.5k
    case X86_AND16mi8:
2354
11.0k
    case X86_AND16mr:
2355
11.2k
    case X86_AND32mi:
2356
11.6k
    case X86_AND32mi8:
2357
11.7k
    case X86_AND32mr:
2358
12.0k
    case X86_AND64mi32:
2359
12.3k
    case X86_AND64mi8:
2360
12.6k
    case X86_AND64mr:
2361
13.0k
    case X86_AND8mi:
2362
13.2k
    case X86_AND8mi8:
2363
13.4k
    case X86_AND8mr:
2364
13.9k
    case X86_AND8rm:
2365
14.1k
    case X86_AND16rm:
2366
14.4k
    case X86_AND32rm:
2367
14.5k
    case X86_AND64rm:
2368
2369
    // BTC
2370
14.8k
    case X86_BTC16mi8:
2371
14.9k
    case X86_BTC16mr:
2372
15.0k
    case X86_BTC32mi8:
2373
15.1k
    case X86_BTC32mr:
2374
15.2k
    case X86_BTC64mi8:
2375
15.3k
    case X86_BTC64mr:
2376
2377
    // BTR
2378
15.5k
    case X86_BTR16mi8:
2379
15.6k
    case X86_BTR16mr:
2380
15.7k
    case X86_BTR32mi8:
2381
15.9k
    case X86_BTR32mr:
2382
16.0k
    case X86_BTR64mi8:
2383
16.0k
    case X86_BTR64mr:
2384
2385
    // BTS
2386
16.5k
    case X86_BTS16mi8:
2387
16.6k
    case X86_BTS16mr:
2388
16.9k
    case X86_BTS32mi8:
2389
17.2k
    case X86_BTS32mr:
2390
17.5k
    case X86_BTS64mi8:
2391
17.8k
    case X86_BTS64mr:
2392
2393
    // CMPXCHG
2394
18.1k
    case X86_CMPXCHG16B:
2395
18.1k
    case X86_CMPXCHG16rm:
2396
18.2k
    case X86_CMPXCHG32rm:
2397
18.6k
    case X86_CMPXCHG64rm:
2398
18.9k
    case X86_CMPXCHG8rm:
2399
19.0k
    case X86_CMPXCHG8B:
2400
2401
    // INC
2402
19.3k
    case X86_INC16m:
2403
19.6k
    case X86_INC32m:
2404
19.7k
    case X86_INC64m:
2405
20.0k
    case X86_INC8m:
2406
2407
    // NEG
2408
20.2k
    case X86_NEG16m:
2409
20.3k
    case X86_NEG32m:
2410
20.4k
    case X86_NEG64m:
2411
20.6k
    case X86_NEG8m:
2412
2413
    // NOT
2414
20.8k
    case X86_NOT16m:
2415
21.5k
    case X86_NOT32m:
2416
21.6k
    case X86_NOT64m:
2417
22.3k
    case X86_NOT8m:
2418
2419
    // OR
2420
22.6k
    case X86_OR16mi:
2421
22.8k
    case X86_OR16mi8:
2422
23.4k
    case X86_OR16mr:
2423
23.9k
    case X86_OR32mi:
2424
24.1k
    case X86_OR32mi8:
2425
24.4k
    case X86_OR32mr:
2426
24.7k
    case X86_OR64mi32:
2427
24.9k
    case X86_OR64mi8:
2428
25.1k
    case X86_OR64mr:
2429
25.3k
    case X86_OR8mi8:
2430
25.3k
    case X86_OR8mi:
2431
25.6k
    case X86_OR8mr:
2432
25.9k
    case X86_OR8rm:
2433
26.2k
    case X86_OR16rm:
2434
26.6k
    case X86_OR32rm:
2435
26.9k
    case X86_OR64rm:
2436
2437
    // SBB
2438
27.1k
    case X86_SBB16mi:
2439
27.4k
    case X86_SBB16mi8:
2440
27.8k
    case X86_SBB16mr:
2441
27.9k
    case X86_SBB32mi:
2442
28.3k
    case X86_SBB32mi8:
2443
28.5k
    case X86_SBB32mr:
2444
29.0k
    case X86_SBB64mi32:
2445
29.0k
    case X86_SBB64mi8:
2446
29.3k
    case X86_SBB64mr:
2447
29.7k
    case X86_SBB8mi:
2448
30.0k
    case X86_SBB8mi8:
2449
30.1k
    case X86_SBB8mr:
2450
2451
    // SUB
2452
30.5k
    case X86_SUB16mi:
2453
30.6k
    case X86_SUB16mi8:
2454
30.8k
    case X86_SUB16mr:
2455
31.1k
    case X86_SUB32mi:
2456
31.2k
    case X86_SUB32mi8:
2457
31.4k
    case X86_SUB32mr:
2458
31.8k
    case X86_SUB64mi32:
2459
32.0k
    case X86_SUB64mi8:
2460
32.1k
    case X86_SUB64mr:
2461
32.2k
    case X86_SUB8mi8:
2462
32.3k
    case X86_SUB8mi:
2463
32.6k
    case X86_SUB8mr:
2464
32.8k
    case X86_SUB8rm:
2465
33.1k
    case X86_SUB16rm:
2466
33.3k
    case X86_SUB32rm:
2467
33.4k
    case X86_SUB64rm:
2468
2469
    // XADD
2470
33.6k
    case X86_XADD16rm:
2471
33.8k
    case X86_XADD32rm:
2472
34.4k
    case X86_XADD64rm:
2473
34.8k
    case X86_XADD8rm:
2474
2475
    // XCHG
2476
35.1k
    case X86_XCHG16rm:
2477
35.4k
    case X86_XCHG32rm:
2478
35.7k
    case X86_XCHG64rm:
2479
35.9k
    case X86_XCHG8rm:
2480
2481
    // XOR
2482
36.5k
    case X86_XOR16mi:
2483
36.8k
    case X86_XOR16mi8:
2484
36.9k
    case X86_XOR16mr:
2485
37.4k
    case X86_XOR32mi:
2486
37.8k
    case X86_XOR32mi8:
2487
38.0k
    case X86_XOR32mr:
2488
38.4k
    case X86_XOR64mi32:
2489
38.8k
    case X86_XOR64mi8:
2490
38.8k
    case X86_XOR64mr:
2491
39.0k
    case X86_XOR8mi8:
2492
39.5k
    case X86_XOR8mi:
2493
39.8k
    case X86_XOR8mr:
2494
40.0k
    case X86_XOR8rm:
2495
40.3k
    case X86_XOR16rm:
2496
40.7k
    case X86_XOR32rm:
2497
40.8k
    case X86_XOR64rm:
2498
2499
      // this instruction can be used with LOCK prefix
2500
40.8k
      return false;
2501
41.1k
    }
2502
41.1k
  }
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
1.08M
  return false;
2517
1.12M
}
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