Coverage Report

Created: 2026-10-17 07:17

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.69M
{
79
1.69M
  return CONTEXTS_SYM[attrMask];
80
1.69M
}
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.69M
{
96
1.69M
  const struct OpcodeDecision *decision = NULL;
97
1.69M
  const uint8_t *indextable = NULL;
98
1.69M
  unsigned int index;
99
100
1.69M
  switch (type) {
101
0
  default:
102
0
    return false;
103
1.32M
  case ONEBYTE:
104
1.32M
    decision = ONEBYTE_SYM;
105
1.32M
    indextable = index_x86DisassemblerOneByteOpcodes;
106
1.32M
    break;
107
193k
  case TWOBYTE:
108
193k
    decision = TWOBYTE_SYM;
109
193k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
193k
    break;
111
65.4k
  case THREEBYTE_38:
112
65.4k
    decision = THREEBYTE38_SYM;
113
65.4k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
65.4k
    break;
115
77.6k
  case THREEBYTE_3A:
116
77.6k
    decision = THREEBYTE3A_SYM;
117
77.6k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
77.6k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
21.7k
  case XOP8_MAP:
121
21.7k
    decision = XOP8_MAP_SYM;
122
21.7k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
21.7k
    break;
124
2.66k
  case XOP9_MAP:
125
2.66k
    decision = XOP9_MAP_SYM;
126
2.66k
    indextable = index_x86DisassemblerXOP9Opcodes;
127
2.66k
    break;
128
1.60k
  case XOPA_MAP:
129
1.60k
    decision = XOPA_MAP_SYM;
130
1.60k
    indextable = index_x86DisassemblerXOPAOpcodes;
131
1.60k
    break;
132
1.95k
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
1.95k
    return true;
135
1.69M
#endif
136
1.69M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.68M
  index = indextable[insnContext];
140
1.68M
  if (index)
141
1.68M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.68M
           MODRM_ONEENTRY;
143
8.41k
  else
144
8.41k
    return false;
145
1.68M
}
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.68M
{
160
1.68M
  const struct ModRMDecision *dec = NULL;
161
1.68M
  unsigned int index;
162
1.68M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.68M
  switch (type) {
165
0
  default:
166
0
    return 0;
167
1.32M
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
1.32M
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
1.32M
    if (index)
171
1.32M
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
411
    else
173
411
      dec = &emptyDecision.modRMDecisions[opcode];
174
1.32M
    break;
175
192k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
192k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
192k
    if (index)
179
189k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
3.22k
    else
181
3.22k
      dec = &emptyDecision.modRMDecisions[opcode];
182
192k
    break;
183
65.3k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
65.3k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
65.3k
    if (index)
187
64.8k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
547
    else
189
547
      dec = &emptyDecision.modRMDecisions[opcode];
190
65.3k
    break;
191
77.6k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
77.6k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
77.6k
    if (index)
195
77.2k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
431
    else
197
431
      dec = &emptyDecision.modRMDecisions[opcode];
198
77.6k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
21.7k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
21.7k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
21.7k
    if (index)
204
19.0k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
2.72k
    else
206
2.72k
      dec = &emptyDecision.modRMDecisions[opcode];
207
21.7k
    break;
208
2.66k
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
2.66k
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
2.66k
    if (index)
212
2.00k
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
658
    else
214
658
      dec = &emptyDecision.modRMDecisions[opcode];
215
2.66k
    break;
216
1.60k
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
1.60k
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
1.60k
    if (index)
220
1.18k
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
417
    else
222
417
      dec = &emptyDecision.modRMDecisions[opcode];
223
1.60k
    break;
224
1.95k
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
1.95k
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
1.95k
    if (index)
228
1.12k
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
1.12k
               .modRMDecisions[opcode];
230
828
    else
231
828
      dec = &emptyDecision.modRMDecisions[opcode];
232
1.95k
    break;
233
1.68M
#endif
234
1.68M
  }
235
236
1.68M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
733k
  case MODRM_ONEENTRY:
241
733k
    return modRMTable[dec->instructionIDs];
242
733k
  case MODRM_SPLITRM:
243
733k
    if (modFromModRM(modRM) == 0x3)
244
179k
      return modRMTable[dec->instructionIDs + 1];
245
553k
    return modRMTable[dec->instructionIDs];
246
186k
  case MODRM_SPLITREG:
247
186k
    if (modFromModRM(modRM) == 0x3)
248
52.2k
      return modRMTable[dec->instructionIDs +
249
52.2k
            ((modRM & 0x38) >> 3) + 8];
250
133k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
35.0k
  case MODRM_SPLITMISC:
252
35.0k
    if (modFromModRM(modRM) == 0x3)
253
7.08k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
7.08k
            8];
255
28.0k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.68M
  }
259
1.68M
}
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.42M
{
271
1.42M
  return &INSTRUCTIONS_SYM[uid];
272
1.42M
}
273
274
/*
275
 * consumeByte - Uses the reader function provided by the user to consume one
276
 *   byte from the instruction's memory and advance the cursor.
277
 *
278
 * @param insn  - The instruction with the reader function to use.  The cursor
279
 *                for this instruction is advanced.
280
 * @param byte  - A pointer to a pre-allocated memory buffer to be populated
281
 *                with the data read.
282
 * @return      - 0 if the read was successful; nonzero otherwise.
283
 */
284
static int consumeByte(struct InternalInstruction *insn, uint8_t *byte)
285
4.66M
{
286
4.66M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
4.66M
  if (!ret)
289
4.65M
    ++(insn->readerCursor);
290
291
4.66M
  return ret;
292
4.66M
}
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
460k
{
303
460k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
460k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
1.35M
{
308
1.35M
  insn->readerCursor--;
309
1.35M
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
266k
  { \
314
266k
    type combined = 0; \
315
266k
    unsigned offset; \
316
831k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
567k
      uint8_t byte; \
318
567k
      int ret = insn->reader(insn->readerArg, &byte, \
319
567k
                 insn->readerCursor + offset); \
320
567k
      if (ret) \
321
567k
        return ret; \
322
567k
      combined = combined | \
323
565k
           ((uint64_t)byte << (offset * 8)); \
324
565k
    } \
325
266k
    *ptr = combined; \
326
264k
    insn->readerCursor += sizeof(type); \
327
264k
    return 0; \
328
266k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
124k
  { \
314
124k
    type combined = 0; \
315
124k
    unsigned offset; \
316
248k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
124k
      uint8_t byte; \
318
124k
      int ret = insn->reader(insn->readerArg, &byte, \
319
124k
                 insn->readerCursor + offset); \
320
124k
      if (ret) \
321
124k
        return ret; \
322
124k
      combined = combined | \
323
124k
           ((uint64_t)byte << (offset * 8)); \
324
124k
    } \
325
124k
    *ptr = combined; \
326
124k
    insn->readerCursor += sizeof(type); \
327
124k
    return 0; \
328
124k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
22.1k
  { \
314
22.1k
    type combined = 0; \
315
22.1k
    unsigned offset; \
316
66.1k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
44.1k
      uint8_t byte; \
318
44.1k
      int ret = insn->reader(insn->readerArg, &byte, \
319
44.1k
                 insn->readerCursor + offset); \
320
44.1k
      if (ret) \
321
44.1k
        return ret; \
322
44.1k
      combined = combined | \
323
44.0k
           ((uint64_t)byte << (offset * 8)); \
324
44.0k
    } \
325
22.1k
    *ptr = combined; \
326
21.9k
    insn->readerCursor += sizeof(type); \
327
21.9k
    return 0; \
328
22.1k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
33.2k
  { \
314
33.2k
    type combined = 0; \
315
33.2k
    unsigned offset; \
316
164k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
132k
      uint8_t byte; \
318
132k
      int ret = insn->reader(insn->readerArg, &byte, \
319
132k
                 insn->readerCursor + offset); \
320
132k
      if (ret) \
321
132k
        return ret; \
322
132k
      combined = combined | \
323
131k
           ((uint64_t)byte << (offset * 8)); \
324
131k
    } \
325
33.2k
    *ptr = combined; \
326
32.7k
    insn->readerCursor += sizeof(type); \
327
32.7k
    return 0; \
328
33.2k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
48.7k
  { \
314
48.7k
    type combined = 0; \
315
48.7k
    unsigned offset; \
316
145k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
97.3k
      uint8_t byte; \
318
97.3k
      int ret = insn->reader(insn->readerArg, &byte, \
319
97.3k
                 insn->readerCursor + offset); \
320
97.3k
      if (ret) \
321
97.3k
        return ret; \
322
97.3k
      combined = combined | \
323
96.9k
           ((uint64_t)byte << (offset * 8)); \
324
96.9k
    } \
325
48.7k
    *ptr = combined; \
326
48.4k
    insn->readerCursor += sizeof(type); \
327
48.4k
    return 0; \
328
48.7k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
32.1k
  { \
314
32.1k
    type combined = 0; \
315
32.1k
    unsigned offset; \
316
158k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
127k
      uint8_t byte; \
318
127k
      int ret = insn->reader(insn->readerArg, &byte, \
319
127k
                 insn->readerCursor + offset); \
320
127k
      if (ret) \
321
127k
        return ret; \
322
127k
      combined = combined | \
323
126k
           ((uint64_t)byte << (offset * 8)); \
324
126k
    } \
325
32.1k
    *ptr = combined; \
326
31.5k
    insn->readerCursor += sizeof(type); \
327
31.5k
    return 0; \
328
32.1k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
5.31k
  { \
314
5.31k
    type combined = 0; \
315
5.31k
    unsigned offset; \
316
47.1k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
41.9k
      uint8_t byte; \
318
41.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
41.9k
                 insn->readerCursor + offset); \
320
41.9k
      if (ret) \
321
41.9k
        return ret; \
322
41.9k
      combined = combined | \
323
41.7k
           ((uint64_t)byte << (offset * 8)); \
324
41.7k
    } \
325
5.31k
    *ptr = combined; \
326
5.18k
    insn->readerCursor += sizeof(type); \
327
5.18k
    return 0; \
328
5.31k
  }
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.37M
{
349
1.37M
  if (insn->mode == MODE_64BIT)
350
574k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
799k
  return false;
353
1.37M
}
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
96.4k
{
363
96.4k
  switch (prefix) {
364
41.4k
  case 0xf0: // LOCK
365
41.4k
    insn->hasLockPrefix = true;
366
41.4k
    break;
367
368
32.1k
  case 0xf2: // REPNE/REPNZ
369
54.9k
  case 0xf3: // REP or REPE/REPZ
370
54.9k
    insn->repeatPrefix = prefix;
371
54.9k
    break;
372
96.4k
  }
373
96.4k
}
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
24.6k
{
385
  // In 32-bit or 16-bit mode all segment override prefixes are used.
386
24.6k
  if (insn->mode != MODE_64BIT) {
387
11.8k
    insn->segmentOverride = prefix;
388
11.8k
    insn->prefix1 = byte;
389
11.8k
    return;
390
11.8k
  }
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
12.7k
  switch (insn->prefix1) {
396
883
  case 0x64: // FS
397
1.75k
  case 0x65: // GS
398
1.75k
    return;
399
12.7k
  }
400
401
  // If the proposed override is for FS or GS, mark it overridden.
402
  // All other segment prefixes are ignored.
403
11.0k
  switch (byte) {
404
1.71k
  case 0x64: // FS
405
4.08k
  case 0x65: // GS
406
4.08k
    insn->segmentOverride = prefix;
407
4.08k
    break;
408
11.0k
  }
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
11.0k
  insn->prefix1 = byte;
414
11.0k
}
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
725k
{
427
725k
  bool isPrefix = true;
428
725k
  uint8_t byte = 0;
429
725k
  uint8_t nextByte;
430
431
1.65M
  while (isPrefix) {
432
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
433
931k
    if (consumeByte(insn, &byte))
434
468
      return -1;
435
436
931k
    if (insn->readerCursor - 1 == insn->startLocation &&
437
725k
        (byte == 0xf2 || byte == 0xf3)) {
438
      // prefix requires next byte
439
44.3k
      if (lookAtByte(insn, &nextByte))
440
103
        return -1;
441
442
44.2k
      if (isREX(insn, nextByte)) {
443
4.05k
        uint8_t nnextByte;
444
445
        // Go to REX prefix after the current one
446
4.05k
        if (consumeByte(insn, &nnextByte))
447
0
          return -1;
448
449
        // We should be able to read next byte after REX prefix
450
4.05k
        if (lookAtByte(insn, &nnextByte))
451
4
          return -1;
452
453
4.04k
        unconsumeByte(insn);
454
4.04k
      }
455
44.2k
    }
456
457
930k
    switch (byte) {
458
41.4k
    case 0xf0: /* LOCK */
459
73.5k
    case 0xf2: /* REPNE/REPNZ */
460
96.4k
    case 0xf3: /* REP or REPE/REPZ */
461
      // only accept the last prefix
462
96.4k
      setGroup0Prefix(insn, byte);
463
96.4k
      insn->prefix0 = byte;
464
96.4k
      insn->rexPrefix = 0;
465
96.4k
      break;
466
467
4.79k
    case 0x2e: /* CS segment override -OR- Branch not taken */
468
7.71k
    case 0x36: /* SS segment override -OR- Branch taken */
469
12.1k
    case 0x3e: /* DS segment override */
470
14.3k
    case 0x26: /* ES segment override */
471
18.8k
    case 0x64: /* FS segment override */
472
24.6k
    case 0x65: /* GS segment override */
473
24.6k
      switch (byte) {
474
4.79k
      case 0x2e:
475
4.79k
        setSegmentOverride(insn, SEG_OVERRIDE_CS, byte);
476
4.79k
        break;
477
2.91k
      case 0x36:
478
2.91k
        setSegmentOverride(insn, SEG_OVERRIDE_SS, byte);
479
2.91k
        break;
480
4.39k
      case 0x3e:
481
4.39k
        setSegmentOverride(insn, SEG_OVERRIDE_DS, byte);
482
4.39k
        break;
483
2.28k
      case 0x26:
484
2.28k
        setSegmentOverride(insn, SEG_OVERRIDE_ES, byte);
485
2.28k
        break;
486
4.45k
      case 0x64:
487
4.45k
        setSegmentOverride(insn, SEG_OVERRIDE_FS, byte);
488
4.45k
        break;
489
5.79k
      case 0x65:
490
5.79k
        setSegmentOverride(insn, SEG_OVERRIDE_GS, byte);
491
5.79k
        break;
492
0
      default:
493
        // debug("Unhandled override");
494
0
        return -1;
495
24.6k
      }
496
24.6k
      insn->rexPrefix = 0;
497
24.6k
      break;
498
499
20.2k
    case 0x66: /* Operand-size override */
500
20.2k
      insn->hasOpSize = true;
501
20.2k
      insn->prefix2 = byte;
502
20.2k
      insn->rexPrefix = 0;
503
20.2k
      break;
504
505
6.73k
    case 0x67: /* Address-size override */
506
6.73k
      insn->hasAdSize = true;
507
6.73k
      insn->prefix3 = byte;
508
6.73k
      insn->rexPrefix = 0;
509
6.73k
      break;
510
782k
    default:
511
782k
      if (isREX(insn, byte)) {
512
        /* REX prefix byte */
513
57.5k
        insn->rexPrefix = byte;
514
725k
      } else {
515
        /* Not a prefix byte */
516
725k
        isPrefix = false;
517
725k
      }
518
782k
      break;
519
930k
    }
520
930k
  }
521
522
725k
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
523
524
725k
  if (byte == 0x62) {
525
58.9k
    uint8_t byte1, byte2;
526
527
58.9k
    if (consumeByte(insn, &byte1)) {
528
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
529
99
      return -1;
530
99
    }
531
532
58.8k
    if (lookAtByte(insn, &byte2)) {
533
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
534
51
      unconsumeByte(insn); /* unconsume byte1 */
535
51
      unconsumeByte(insn); /* unconsume byte  */
536
58.7k
    } else {
537
58.7k
      if ((insn->mode == MODE_64BIT ||
538
34.8k
           (byte1 & 0xc0) == 0xc0) &&
539
53.9k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
540
53.8k
        insn->vectorExtensionType = TYPE_EVEX;
541
53.8k
      } else {
542
4.89k
        unconsumeByte(insn); /* unconsume byte1 */
543
4.89k
        unconsumeByte(insn); /* unconsume byte  */
544
4.89k
      }
545
58.7k
    }
546
547
58.8k
    if (insn->vectorExtensionType == TYPE_EVEX) {
548
53.8k
      insn->vectorExtensionPrefix[0] = byte;
549
53.8k
      insn->vectorExtensionPrefix[1] = byte1;
550
53.8k
      if (consumeByte(insn,
551
53.8k
          &insn->vectorExtensionPrefix[2])) {
552
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
553
0
        return -1;
554
0
      }
555
556
53.8k
      if (consumeByte(insn,
557
53.8k
          &insn->vectorExtensionPrefix[3])) {
558
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
559
22
        return -1;
560
22
      }
561
562
      /* We simulate the REX prefix for simplicity's sake */
563
53.8k
      if (insn->mode == MODE_64BIT) {
564
23.8k
        insn->rexPrefix =
565
23.8k
          0x40 |
566
23.8k
          (wFromEVEX3of4(
567
23.8k
             insn->vectorExtensionPrefix[2])
568
23.8k
           << 3) |
569
23.8k
          (rFromEVEX2of4(
570
23.8k
             insn->vectorExtensionPrefix[1])
571
23.8k
           << 2) |
572
23.8k
          (xFromEVEX2of4(
573
23.8k
             insn->vectorExtensionPrefix[1])
574
23.8k
           << 1) |
575
23.8k
          (bFromEVEX2of4(
576
23.8k
             insn->vectorExtensionPrefix[1])
577
23.8k
           << 0);
578
23.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
53.8k
    }
584
666k
  } else if (byte == 0xc4) {
585
6.90k
    uint8_t byte1;
586
587
6.90k
    if (lookAtByte(insn, &byte1)) {
588
      // dbgprintf(insn, "Couldn't read second byte of VEX");
589
20
      return -1;
590
20
    }
591
592
6.88k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
593
5.72k
      insn->vectorExtensionType = TYPE_VEX_3B;
594
1.16k
    else
595
1.16k
      unconsumeByte(insn);
596
597
6.88k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
598
5.72k
      insn->vectorExtensionPrefix[0] = byte;
599
5.72k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
600
5.72k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
601
602
      /* We simulate the REX prefix for simplicity's sake */
603
5.72k
      if (insn->mode == MODE_64BIT)
604
2.17k
        insn->rexPrefix =
605
2.17k
          0x40 |
606
2.17k
          (wFromVEX3of3(
607
2.17k
             insn->vectorExtensionPrefix[2])
608
2.17k
           << 3) |
609
2.17k
          (rFromVEX2of3(
610
2.17k
             insn->vectorExtensionPrefix[1])
611
2.17k
           << 2) |
612
2.17k
          (xFromVEX2of3(
613
2.17k
             insn->vectorExtensionPrefix[1])
614
2.17k
           << 1) |
615
2.17k
          (bFromVEX2of3(
616
2.17k
             insn->vectorExtensionPrefix[1])
617
2.17k
           << 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
5.72k
    }
623
659k
  } else if (byte == 0xc5) {
624
11.0k
    uint8_t byte1;
625
626
11.0k
    if (lookAtByte(insn, &byte1)) {
627
      // dbgprintf(insn, "Couldn't read second byte of VEX");
628
26
      return -1;
629
26
    }
630
631
11.0k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
632
9.61k
      insn->vectorExtensionType = TYPE_VEX_2B;
633
1.41k
    else
634
1.41k
      unconsumeByte(insn);
635
636
11.0k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
637
9.61k
      insn->vectorExtensionPrefix[0] = byte;
638
9.61k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
639
640
9.61k
      if (insn->mode == MODE_64BIT)
641
2.76k
        insn->rexPrefix =
642
2.76k
          0x40 |
643
2.76k
          (rFromVEX2of2(
644
2.76k
             insn->vectorExtensionPrefix[1])
645
2.76k
           << 2);
646
647
9.61k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
648
4.17k
      default:
649
4.17k
        break;
650
5.43k
      case VEX_PREFIX_66:
651
5.43k
        insn->hasOpSize = true;
652
5.43k
        break;
653
9.61k
      }
654
655
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
656
      //    insn->vectorExtensionPrefix[0],
657
      //    insn->vectorExtensionPrefix[1]);
658
9.61k
    }
659
648k
  } else if (byte == 0x8f) {
660
7.43k
    uint8_t byte1;
661
662
7.43k
    if (lookAtByte(insn, &byte1)) {
663
      // dbgprintf(insn, "Couldn't read second byte of XOP");
664
17
      return -1;
665
17
    }
666
667
7.41k
    if ((byte1 & 0x38) !=
668
7.41k
        0x0) /* 0 in these 3 bits is a POP instruction. */
669
6.45k
      insn->vectorExtensionType = TYPE_XOP;
670
959
    else
671
959
      unconsumeByte(insn);
672
673
7.41k
    if (insn->vectorExtensionType == TYPE_XOP) {
674
6.45k
      insn->vectorExtensionPrefix[0] = byte;
675
6.45k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
676
6.45k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
677
678
      /* We simulate the REX prefix for simplicity's sake */
679
6.45k
      if (insn->mode == MODE_64BIT)
680
2.12k
        insn->rexPrefix =
681
2.12k
          0x40 |
682
2.12k
          (wFromXOP3of3(
683
2.12k
             insn->vectorExtensionPrefix[2])
684
2.12k
           << 3) |
685
2.12k
          (rFromXOP2of3(
686
2.12k
             insn->vectorExtensionPrefix[1])
687
2.12k
           << 2) |
688
2.12k
          (xFromXOP2of3(
689
2.12k
             insn->vectorExtensionPrefix[1])
690
2.12k
           << 1) |
691
2.12k
          (bFromXOP2of3(
692
2.12k
             insn->vectorExtensionPrefix[1])
693
2.12k
           << 0);
694
695
6.45k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
696
6.44k
      default:
697
6.44k
        break;
698
6.44k
      case VEX_PREFIX_66:
699
10
        insn->hasOpSize = true;
700
10
        break;
701
6.45k
      }
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
6.45k
    }
707
7.41k
  } else
708
641k
    unconsumeByte(insn);
709
710
725k
  if (insn->repeatPrefix != 0) {
711
48.6k
    if (lookAtByte(insn, &nextByte))
712
4
      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
48.6k
    if ((insn->hasLockPrefix || ((nextByte & 0xfe) == 0x86 ||
722
46.3k
               (nextByte & 0xf8) == 0x90)) &&
723
3.28k
        nextByte != 0x90) {
724
2.75k
      insn->xAcquireRelease = insn->repeatPrefix;
725
2.75k
    }
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
48.6k
    if (insn->repeatPrefix == 0xf3 &&
734
20.6k
        (nextByte == 0x88 || nextByte == 0x89 || nextByte == 0xc6 ||
735
20.0k
         nextByte == 0xc7)) {
736
731
      insn->xAcquireRelease = insn->repeatPrefix;
737
731
    }
738
48.6k
  }
739
740
725k
  if (insn->mode == MODE_16BIT) {
741
206k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
742
206k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
743
206k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
744
206k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
745
206k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
746
518k
  } else if (insn->mode == MODE_32BIT) {
747
244k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
748
244k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
749
244k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
750
244k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
751
244k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
752
274k
  } else if (insn->mode == MODE_64BIT) {
753
274k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
754
48.3k
      insn->registerSize = 8;
755
48.3k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
756
48.3k
      insn->displacementSize = 4;
757
48.3k
      insn->immediateSize = 4;
758
48.3k
      insn->immSize = 4;
759
225k
    } else {
760
225k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
761
225k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
762
225k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
763
225k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
764
225k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
765
225k
    }
766
274k
  }
767
768
725k
  return 0;
769
725k
}
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
725k
{
782
725k
  uint8_t current;
783
784
  // dbgprintf(insn, "readOpcode()");
785
786
725k
  insn->opcodeType = ONEBYTE;
787
788
725k
  if (insn->vectorExtensionType == TYPE_EVEX) {
789
53.8k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
790
3
    default:
791
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
792
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
793
3
      return -1;
794
18.8k
    case VEX_LOB_0F:
795
18.8k
      insn->opcodeType = TWOBYTE;
796
18.8k
      return consumeByte(insn, &insn->opcode);
797
18.3k
    case VEX_LOB_0F38:
798
18.3k
      insn->opcodeType = THREEBYTE_38;
799
18.3k
      return consumeByte(insn, &insn->opcode);
800
16.5k
    case VEX_LOB_0F3A:
801
16.5k
      insn->opcodeType = THREEBYTE_3A;
802
16.5k
      return consumeByte(insn, &insn->opcode);
803
53.8k
    }
804
671k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
805
5.72k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
806
20
    default:
807
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
808
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
809
20
      return -1;
810
726
    case VEX_LOB_0F:
811
      //insn->twoByteEscape = 0x0f;
812
726
      insn->opcodeType = TWOBYTE;
813
726
      return consumeByte(insn, &insn->opcode);
814
3.23k
    case VEX_LOB_0F38:
815
      //insn->twoByteEscape = 0x0f;
816
3.23k
      insn->opcodeType = THREEBYTE_38;
817
3.23k
      return consumeByte(insn, &insn->opcode);
818
1.74k
    case VEX_LOB_0F3A:
819
      //insn->twoByteEscape = 0x0f;
820
1.74k
      insn->opcodeType = THREEBYTE_3A;
821
1.74k
      return consumeByte(insn, &insn->opcode);
822
5.72k
    }
823
665k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
824
    //insn->twoByteEscape = 0x0f;
825
9.61k
    insn->opcodeType = TWOBYTE;
826
9.61k
    return consumeByte(insn, &insn->opcode);
827
655k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
828
6.45k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
829
20
    default:
830
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
831
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
832
20
      return -1;
833
5.52k
    case XOP_MAP_SELECT_8:
834
5.52k
      insn->opcodeType = XOP8_MAP;
835
5.52k
      return consumeByte(insn, &insn->opcode);
836
640
    case XOP_MAP_SELECT_9:
837
640
      insn->opcodeType = XOP9_MAP;
838
640
      return consumeByte(insn, &insn->opcode);
839
269
    case XOP_MAP_SELECT_A:
840
269
      insn->opcodeType = XOPA_MAP;
841
269
      return consumeByte(insn, &insn->opcode);
842
6.45k
    }
843
6.45k
  }
844
845
649k
  if (consumeByte(insn, &current))
846
0
    return -1;
847
848
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
849
649k
  insn->firstByte = current;
850
851
649k
  if (current == 0x0f) {
852
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
853
43.8k
    insn->twoByteEscape = current;
854
855
43.8k
    if (consumeByte(insn, &current))
856
60
      return -1;
857
858
43.7k
    if (current == 0x38) {
859
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
860
1.02k
      if (consumeByte(insn, &current))
861
2
        return -1;
862
863
1.02k
      insn->opcodeType = THREEBYTE_38;
864
42.7k
    } else if (current == 0x3a) {
865
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
866
287
      if (consumeByte(insn, &current))
867
1
        return -1;
868
869
286
      insn->opcodeType = THREEBYTE_3A;
870
42.4k
    } 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
405
      if (readModRM(insn))
874
7
        return -1;
875
876
398
      if (consumeByte(insn, &current))
877
5
        return -1;
878
879
393
      insn->opcodeType = THREEDNOW_MAP;
880
42.0k
    } else {
881
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
882
42.0k
      insn->opcodeType = TWOBYTE;
883
42.0k
    }
884
43.7k
  }
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
649k
  insn->opcode = current;
892
893
649k
  return 0;
894
649k
}
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.69M
{
920
1.69M
  bool hasModRMExtension;
921
922
1.69M
  InstructionContext instructionClass = contextForAttrs(attrMask);
923
924
1.69M
  hasModRMExtension =
925
1.69M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
926
927
1.69M
  if (hasModRMExtension) {
928
958k
    if (readModRM(insn))
929
3.12k
      return -1;
930
931
955k
    *instructionID = decode(insn->opcodeType, instructionClass,
932
955k
          insn->opcode, insn->modRM);
933
955k
  } else {
934
732k
    *instructionID = decode(insn->opcodeType, instructionClass,
935
732k
          insn->opcode, 0);
936
732k
  }
937
938
1.68M
  return 0;
939
1.69M
}
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
172k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
955
172k
          x86_16_bit_eq_tbl[i].first == orig;
956
169k
         i++) {
957
169k
      if (x86_16_bit_eq_tbl[i].second == equiv)
958
165k
        return true;
959
169k
    }
960
169k
  }
961
962
171k
  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
41.0k
{
972
41.0k
  unsigned int i = find_insn(id);
973
41.0k
  if (i != -1) {
974
40.9k
    return insns[i].is64bit;
975
40.9k
  }
976
977
  // not found??
978
99
  return false;
979
41.0k
}
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
3.09k
{
1015
3.09k
  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
3.09k
  switch (insn->opcodeType) {
1020
  // No one-byte opcodes have mandatory prefixes.
1021
227
  case ONEBYTE:
1022
227
    resolution = DO_NOT_RESOLVE;
1023
227
    break;
1024
2.55k
  case TWOBYTE:
1025
    // Exceptions for instructions that operate on data size-overridable
1026
    // operands.
1027
2.55k
    if (
1028
      // XADD
1029
2.55k
      (insn->opcode & 0xFE) == 0xC0
1030
1031
      // BSWAP
1032
2.50k
      || (insn->opcode & 0xF8) == 0xC8
1033
1034
      // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX
1035
2.15k
      || (insn->opcode & 0xF8) == 0xB0
1036
1037
      // Group 16, various NOPs
1038
2.10k
      || (insn->opcode & 0xF8) == 0x18
1039
1040
      // UD0
1041
1.90k
      || insn->opcode == 0xFF) {
1042
874
      resolution = IGNORE_REP;
1043
874
      break;
1044
874
    }
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
1.68k
    switch (insn->opcode & 0xf0) {
1066
29
    case 0x10:
1067
45
    case 0x50:
1068
45
    case 0x60:
1069
221
    case 0x70:
1070
723
    case 0xC0:
1071
729
    case 0xD0:
1072
753
    case 0xE0:
1073
753
    case 0xF0:
1074
753
      resolution = IGNORE_DATA_SIZE;
1075
753
      break;
1076
8
    case 0x00:
1077
33
    case 0x20:
1078
226
    case 0x30:
1079
241
    case 0x40:
1080
744
    case 0x80:
1081
744
    case 0x90:
1082
746
    case 0xA0:
1083
746
      resolution = IGNORE_REP;
1084
746
      break;
1085
185
    default: // 0xB0
1086
185
      resolution = DO_NOT_RESOLVE;
1087
185
      break;
1088
1.68k
    }
1089
1.68k
    break;
1090
1.68k
  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
244
    if ((insn->opcode & 0xF0) == 0xF0) {
1095
71
      resolution = IGNORE_DATA_SIZE;
1096
71
      break;
1097
71
    }
1098
1099
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1100
    // or separately specified.
1101
173
    resolution = DO_NOT_RESOLVE;
1102
173
    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
68
  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
68
    resolution = IGNORE_DATA_SIZE;
1119
68
    break;
1120
3.09k
  }
1121
1122
3.09k
  switch (resolution) {
1123
1.62k
  case IGNORE_REP:
1124
1.62k
    return attrMask & ~(ATTR_XD | ATTR_XS);
1125
892
  case IGNORE_DATA_SIZE:
1126
892
    return attrMask & ~ATTR_OPSIZE;
1127
0
  default:
1128
585
  case DO_NOT_RESOLVE:
1129
585
    return attrMask;
1130
3.09k
  }
1131
3.09k
}
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
724k
{
1144
724k
  uint16_t attrMask;
1145
724k
  uint16_t instructionID;
1146
724k
  bool rexWOverridesOpSize;
1147
1148
  /* REX.W overrides the operand-sized prefix for near RET in 64-bit mode */
1149
724k
  rexWOverridesOpSize = insn->mode == MODE_64BIT && insn->hasOpSize &&
1150
12.7k
            insn->opcodeType == ONEBYTE &&
1151
6.80k
            (insn->opcode == 0xC2 || insn->opcode == 0xC3) &&
1152
621
            (insn->rexPrefix & 0x08);
1153
1154
724k
  attrMask = ATTR_NONE;
1155
1156
724k
  if (insn->mode == MODE_64BIT)
1157
274k
    attrMask |= ATTR_64BIT;
1158
1159
724k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1160
75.5k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1161
53.8k
            ATTR_EVEX :
1162
75.5k
            ATTR_VEX;
1163
1164
75.5k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1165
53.8k
      switch (ppFromEVEX3of4(
1166
53.8k
        insn->vectorExtensionPrefix[2])) {
1167
45.7k
      case VEX_PREFIX_66:
1168
45.7k
        attrMask |= ATTR_OPSIZE;
1169
45.7k
        break;
1170
2.26k
      case VEX_PREFIX_F3:
1171
2.26k
        attrMask |= ATTR_XS;
1172
2.26k
        break;
1173
1.17k
      case VEX_PREFIX_F2:
1174
1.17k
        attrMask |= ATTR_XD;
1175
1.17k
        break;
1176
53.8k
      }
1177
1178
53.8k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1179
7.25k
        attrMask |= ATTR_EVEXKZ;
1180
53.8k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1181
21.1k
        attrMask |= ATTR_EVEXB;
1182
53.8k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1183
38.8k
        attrMask |= ATTR_EVEXK;
1184
53.8k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1185
27.9k
        attrMask |= ATTR_EVEXL;
1186
53.8k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1187
22.6k
        attrMask |= ATTR_EVEXL2;
1188
53.8k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1189
5.68k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1190
5.05k
      case VEX_PREFIX_66:
1191
5.05k
        attrMask |= ATTR_OPSIZE;
1192
5.05k
        break;
1193
218
      case VEX_PREFIX_F3:
1194
218
        attrMask |= ATTR_XS;
1195
218
        break;
1196
332
      case VEX_PREFIX_F2:
1197
332
        attrMask |= ATTR_XD;
1198
332
        break;
1199
5.68k
      }
1200
1201
5.68k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1202
3.20k
        attrMask |= ATTR_VEXL;
1203
16.0k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1204
9.59k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1205
5.43k
      case VEX_PREFIX_66:
1206
5.43k
        attrMask |= ATTR_OPSIZE;
1207
5.43k
        break;
1208
981
      case VEX_PREFIX_F3:
1209
981
        attrMask |= ATTR_XS;
1210
981
        break;
1211
1.48k
      case VEX_PREFIX_F2:
1212
1.48k
        attrMask |= ATTR_XD;
1213
1.48k
        break;
1214
9.59k
      }
1215
1216
9.59k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1217
7.72k
        attrMask |= ATTR_VEXL;
1218
9.59k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1219
6.41k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1220
5
      case VEX_PREFIX_66:
1221
5
        attrMask |= ATTR_OPSIZE;
1222
5
        break;
1223
6
      case VEX_PREFIX_F3:
1224
6
        attrMask |= ATTR_XS;
1225
6
        break;
1226
6
      case VEX_PREFIX_F2:
1227
6
        attrMask |= ATTR_XD;
1228
6
        break;
1229
6.41k
      }
1230
1231
6.41k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1232
620
        attrMask |= ATTR_VEXL;
1233
6.41k
    } else {
1234
0
      return -1;
1235
0
    }
1236
649k
  } else {
1237
649k
    if (insn->hasOpSize && insn->mode != MODE_16BIT &&
1238
15.1k
        !rexWOverridesOpSize) {
1239
14.9k
      attrMask |= ATTR_OPSIZE;
1240
14.9k
    }
1241
649k
    if (insn->hasAdSize)
1242
5.30k
      attrMask |= ATTR_ADSIZE;
1243
649k
    if (insn->opcodeType == ONEBYTE) {
1244
605k
      if (insn->repeatPrefix == 0xf3 &&
1245
14.8k
          (insn->opcode == 0x90))
1246
        // Special support for PAUSE
1247
511
        attrMask |= ATTR_XS;
1248
605k
    } else {
1249
43.7k
      if (insn->repeatPrefix == 0xf2)
1250
6.61k
        attrMask |= ATTR_XD;
1251
37.1k
      else if (insn->repeatPrefix == 0xf3)
1252
4.86k
        attrMask |= ATTR_XS;
1253
43.7k
    }
1254
1255
    /* BSF/BSR ignore F2; F3 selects TZCNT/LZCNT instead. */
1256
649k
    if (insn->opcodeType == TWOBYTE &&
1257
42.0k
        (insn->opcode == 0xBC || insn->opcode == 0xBD))
1258
275
      attrMask &= ~ATTR_XD;
1259
1260
649k
    if ((attrMask & ATTR_OPSIZE) &&
1261
14.9k
        (attrMask & (ATTR_XD | ATTR_XS))) {
1262
3.09k
      attrMask =
1263
3.09k
        resolveMandatoryPrefixConflict(insn, attrMask);
1264
3.09k
    }
1265
649k
  }
1266
1267
724k
  if (insn->rexPrefix & 0x08) {
1268
48.2k
    attrMask |= ATTR_REXW;
1269
48.2k
    attrMask &= ~ATTR_ADSIZE;
1270
48.2k
  }
1271
1272
  /*
1273
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1274
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1275
   */
1276
724k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1277
178k
      insn->opcode == 0xE3)
1278
776
    attrMask ^= ATTR_ADSIZE;
1279
1280
  /*
1281
   * CALL/JMP ignore 66 in 64-bit mode. Near Jcc preserve their previous
1282
   * behavior unless an Intel or AMD mode is selected.
1283
   */
1284
724k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1285
12.7k
    switch (insn->opcode) {
1286
358
    case 0xE8:
1287
851
    case 0xE9:
1288
      // Take care of psubsb and other mmx instructions.
1289
851
      if (insn->opcodeType == ONEBYTE) {
1290
237
        attrMask ^= ATTR_OPSIZE;
1291
237
        insn->immediateSize = 4;
1292
237
        insn->displacementSize = 4;
1293
237
      }
1294
851
      break;
1295
195
    case 0x80:
1296
522
    case 0x81:
1297
684
    case 0x82:
1298
1.11k
    case 0x83:
1299
1.28k
    case 0x84:
1300
1.36k
    case 0x85:
1301
1.56k
    case 0x86:
1302
1.77k
    case 0x87:
1303
1.99k
    case 0x88:
1304
2.19k
    case 0x89:
1305
2.25k
    case 0x8A:
1306
2.32k
    case 0x8B:
1307
2.51k
    case 0x8C:
1308
2.71k
    case 0x8D:
1309
2.94k
    case 0x8E:
1310
3.15k
    case 0x8F:
1311
      // Take care of lea and three byte ops.
1312
3.15k
      if (insn->opcodeType != TWOBYTE)
1313
2.50k
        break;
1314
1315
647
      if ((x86_has_feature(mode, CS_MODE_X86_INTEL) ||
1316
647
           x86_has_feature(mode, CS_MODE_X86_AMD)) &&
1317
0
          insn->vectorExtensionType == TYPE_NO_VEX_XOP) {
1318
0
        if ((x86_has_feature(mode, CS_MODE_X86_AMD)) &&
1319
0
            !wFromREX(insn->rexPrefix)) {
1320
0
          attrMask |= ATTR_OPSIZE;
1321
0
          insn->immediateSize = 2;
1322
0
          insn->displacementSize = 2;
1323
0
          insn->immSize = 2;
1324
0
        } else {
1325
0
          attrMask &= ~ATTR_OPSIZE;
1326
0
          insn->immediateSize = 4;
1327
0
          insn->displacementSize = 4;
1328
0
          insn->immSize = 8;
1329
0
        }
1330
647
      } else if (insn->opcode >= 0x82) {
1331
367
        attrMask ^= ATTR_OPSIZE;
1332
367
        insn->immediateSize = 4;
1333
367
        insn->displacementSize = 4;
1334
367
      }
1335
647
      break;
1336
12.7k
    }
1337
12.7k
  }
1338
1339
  /* The following clauses compensate for limitations of the tables. */
1340
724k
  if (insn->mode != MODE_64BIT &&
1341
450k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1342
44.7k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1343
16
      return -1;
1344
16
    }
1345
1346
    /*
1347
     * The tables can't distinguish between cases where the W-bit is used to
1348
     * select register size and cases where it's a required part of the opcode.
1349
     */
1350
44.6k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1351
30.0k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1352
25.2k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1353
3.52k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1354
23.4k
        (insn->vectorExtensionType == TYPE_XOP &&
1355
21.5k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1356
21.5k
      uint16_t instructionIDWithREXW;
1357
1358
21.5k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1359
21.5k
                attrMask | ATTR_REXW)) {
1360
6
        insn->instructionID = instructionID;
1361
6
        insn->spec = specifierForUID(instructionID);
1362
6
        return 0;
1363
6
      }
1364
1365
      // If not a 64-bit instruction. Switch the opcode.
1366
21.5k
      if (!is64Bit(instructionIDWithREXW)) {
1367
19.4k
        insn->instructionID = instructionIDWithREXW;
1368
19.4k
        insn->spec =
1369
19.4k
          specifierForUID(instructionIDWithREXW);
1370
1371
19.4k
        return 0;
1372
19.4k
      }
1373
21.5k
    }
1374
44.6k
  }
1375
1376
  /*
1377
   * Absolute moves, umonitor, and movdir64b need special handling.
1378
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1379
   *  inverted w.r.t.
1380
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1381
   *  any position.
1382
   */
1383
705k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1384
697k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1385
697k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1386
    /* Make sure we observed the prefixes in any position. */
1387
8.75k
    if (insn->hasAdSize)
1388
350
      attrMask |= ATTR_ADSIZE;
1389
1390
8.75k
    if (insn->hasOpSize)
1391
252
      attrMask |= ATTR_OPSIZE;
1392
1393
    /* In 16-bit, invert the attributes. */
1394
8.75k
    if (insn->mode == MODE_16BIT) {
1395
3.78k
      attrMask ^= ATTR_ADSIZE;
1396
1397
      /* The OpSize attribute is only valid with the absolute moves. */
1398
3.78k
      if (insn->opcodeType == ONEBYTE &&
1399
3.35k
          ((insn->opcode & 0xFC) == 0xA0))
1400
3.35k
        attrMask ^= ATTR_OPSIZE;
1401
3.78k
    }
1402
1403
8.75k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1404
6
      return -1;
1405
6
    }
1406
1407
8.74k
    insn->instructionID = instructionID;
1408
8.74k
    insn->spec = specifierForUID(instructionID);
1409
1410
8.74k
    return 0;
1411
8.75k
  }
1412
696k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1413
1.77k
    return -1;
1414
1.77k
  }
1415
1416
695k
  if ((insn->mode == MODE_16BIT ||
1417
499k
       (insn->hasOpSize && !rexWOverridesOpSize)) &&
1418
212k
      !(attrMask & ATTR_OPSIZE)) {
1419
    /*
1420
     * The instruction tables make no distinction between instructions that
1421
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1422
     * particular spot (i.e., many MMX operations).  In general we're
1423
     * conservative, but in the specific case where OpSize is present but not
1424
     * in the right place we check if there's a 16-bit operation.
1425
     */
1426
189k
    const struct InstructionSpecifier *spec;
1427
189k
    uint16_t instructionIDWithOpsize;
1428
1429
189k
    spec = specifierForUID(instructionID);
1430
1431
189k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1432
189k
              attrMask | ATTR_OPSIZE)) {
1433
      /*
1434
       * ModRM required with OpSize but not present; give up and return version
1435
       * without OpSize set
1436
       */
1437
6
      insn->instructionID = instructionID;
1438
6
      insn->spec = spec;
1439
1440
6
      return 0;
1441
6
    }
1442
1443
189k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1444
94.0k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1445
93.1k
      insn->instructionID = instructionIDWithOpsize;
1446
93.1k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1447
96.6k
    } else {
1448
96.6k
      insn->instructionID = instructionID;
1449
96.6k
      insn->spec = spec;
1450
96.6k
    }
1451
1452
189k
    return 0;
1453
189k
  }
1454
1455
505k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1456
2.40k
      insn->rexPrefix & 0x01) {
1457
    /*
1458
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1459
     * it should decode as XCHG %r8, %eax.
1460
     */
1461
669
    const struct InstructionSpecifier *spec;
1462
669
    uint16_t instructionIDWithNewOpcode;
1463
669
    const struct InstructionSpecifier *specWithNewOpcode;
1464
1465
669
    spec = specifierForUID(instructionID);
1466
1467
    /* Borrow opcode from one of the other XCHGar opcodes */
1468
669
    insn->opcode = 0x91;
1469
1470
669
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1471
669
              attrMask)) {
1472
0
      insn->opcode = 0x90;
1473
1474
0
      insn->instructionID = instructionID;
1475
0
      insn->spec = spec;
1476
1477
0
      return 0;
1478
0
    }
1479
1480
669
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1481
1482
    /* Change back */
1483
669
    insn->opcode = 0x90;
1484
1485
669
    insn->instructionID = instructionIDWithNewOpcode;
1486
669
    insn->spec = specWithNewOpcode;
1487
1488
669
    return 0;
1489
669
  }
1490
1491
504k
  insn->instructionID = instructionID;
1492
504k
  insn->spec = specifierForUID(insn->instructionID);
1493
1494
504k
  return 0;
1495
505k
}
1496
1497
/*
1498
 * readSIB - Consumes the SIB byte to determine addressing information for an
1499
 *   instruction.
1500
 *
1501
 * @param insn  - The instruction whose SIB byte is to be read.
1502
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1503
 */
1504
static int readSIB(struct InternalInstruction *insn)
1505
36.5k
{
1506
36.5k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1507
36.5k
  uint8_t index, base;
1508
1509
  // dbgprintf(insn, "readSIB()");
1510
1511
36.5k
  if (insn->consumedSIB)
1512
0
    return 0;
1513
1514
36.5k
  insn->consumedSIB = true;
1515
1516
36.5k
  switch (insn->addressSize) {
1517
0
  case 2:
1518
    // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1519
0
    return -1;
1520
14.0k
  case 4:
1521
14.0k
    insn->sibIndexBase = SIB_INDEX_EAX;
1522
14.0k
    sibBaseBase = SIB_BASE_EAX;
1523
14.0k
    break;
1524
22.4k
  case 8:
1525
22.4k
    insn->sibIndexBase = SIB_INDEX_RAX;
1526
22.4k
    sibBaseBase = SIB_BASE_RAX;
1527
22.4k
    break;
1528
36.5k
  }
1529
1530
36.5k
  if (consumeByte(insn, &insn->sib))
1531
92
    return -1;
1532
1533
36.4k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1534
1535
36.4k
  if (index == 0x4) {
1536
8.35k
    insn->sibIndex = SIB_INDEX_NONE;
1537
28.0k
  } else {
1538
28.0k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1539
28.0k
  }
1540
1541
36.4k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1542
1543
36.4k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1544
1545
36.4k
  switch (base) {
1546
4.41k
  case 0x5:
1547
5.67k
  case 0xd:
1548
5.67k
    switch (modFromModRM(insn->modRM)) {
1549
3.03k
    case 0x0:
1550
3.03k
      insn->eaDisplacement = EA_DISP_32;
1551
3.03k
      insn->sibBase = SIB_BASE_NONE;
1552
3.03k
      break;
1553
1.92k
    case 0x1:
1554
1.92k
      insn->eaDisplacement = EA_DISP_8;
1555
1.92k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1556
1.92k
      break;
1557
722
    case 0x2:
1558
722
      insn->eaDisplacement = EA_DISP_32;
1559
722
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1560
722
      break;
1561
0
    case 0x3:
1562
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1563
0
      return -1;
1564
5.67k
    }
1565
5.67k
    break;
1566
30.7k
  default:
1567
30.7k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1568
30.7k
    break;
1569
36.4k
  }
1570
1571
36.4k
  return 0;
1572
36.4k
}
1573
1574
/*
1575
 * readDisplacement - Consumes the displacement of an instruction.
1576
 *
1577
 * @param insn  - The instruction whose displacement is to be read.
1578
 * @return      - 0 if the displacement byte was successfully read; nonzero
1579
 *                otherwise.
1580
 */
1581
static int readDisplacement(struct InternalInstruction *insn)
1582
243k
{
1583
243k
  int8_t d8;
1584
243k
  int16_t d16;
1585
243k
  int32_t d32;
1586
1587
  // dbgprintf(insn, "readDisplacement()");
1588
1589
243k
  if (insn->consumedDisplacement)
1590
0
    return 0;
1591
1592
243k
  insn->consumedDisplacement = true;
1593
243k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1594
1595
243k
  switch (insn->eaDisplacement) {
1596
63.2k
  case EA_DISP_NONE:
1597
63.2k
    insn->consumedDisplacement = false;
1598
63.2k
    break;
1599
124k
  case EA_DISP_8:
1600
124k
    if (consumeInt8(insn, &d8))
1601
256
      return -1;
1602
124k
    insn->displacement = d8;
1603
124k
    insn->displacementSize = 1;
1604
124k
    break;
1605
22.1k
  case EA_DISP_16:
1606
22.1k
    if (consumeInt16(insn, &d16))
1607
119
      return -1;
1608
21.9k
    insn->displacement = d16;
1609
21.9k
    insn->displacementSize = 2;
1610
21.9k
    break;
1611
33.2k
  case EA_DISP_32:
1612
33.2k
    if (consumeInt32(insn, &d32))
1613
410
      return -1;
1614
32.7k
    insn->displacement = d32;
1615
32.7k
    insn->displacementSize = 4;
1616
32.7k
    break;
1617
243k
  }
1618
1619
242k
  return 0;
1620
243k
}
1621
1622
/*
1623
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1624
 *   displacement) for an instruction and interprets it.
1625
 *
1626
 * @param insn  - The instruction whose addressing information is to be read.
1627
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1628
 */
1629
static int readModRM(struct InternalInstruction *insn)
1630
2.21M
{
1631
2.21M
  uint8_t mod, rm, reg, evexrm;
1632
1633
  // dbgprintf(insn, "readModRM()");
1634
1635
2.21M
  if (insn->consumedModRM)
1636
1.49M
    return 0;
1637
1638
717k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1639
1640
717k
  if (consumeByte(insn, &insn->modRM))
1641
2.25k
    return -1;
1642
1643
715k
  insn->consumedModRM = true;
1644
1645
  // save original ModRM for later reference
1646
715k
  insn->orgModRM = insn->modRM;
1647
1648
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1649
715k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1650
74.3k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1651
1.95k
    insn->modRM |= 0xC0;
1652
1653
715k
  mod = modFromModRM(insn->modRM);
1654
715k
  rm = rmFromModRM(insn->modRM);
1655
715k
  reg = regFromModRM(insn->modRM);
1656
1657
  /*
1658
   * This goes by insn->registerSize to pick the correct register, which messes
1659
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1660
   * fixupReg().
1661
   */
1662
715k
  switch (insn->registerSize) {
1663
225k
  case 2:
1664
225k
    insn->regBase = MODRM_REG_AX;
1665
225k
    insn->eaRegBase = EA_REG_AX;
1666
225k
    break;
1667
416k
  case 4:
1668
416k
    insn->regBase = MODRM_REG_EAX;
1669
416k
    insn->eaRegBase = EA_REG_EAX;
1670
416k
    break;
1671
73.6k
  case 8:
1672
73.6k
    insn->regBase = MODRM_REG_RAX;
1673
73.6k
    insn->eaRegBase = EA_REG_RAX;
1674
73.6k
    break;
1675
715k
  }
1676
1677
715k
  reg |= rFromREX(insn->rexPrefix) << 3;
1678
715k
  rm |= bFromREX(insn->rexPrefix) << 3;
1679
1680
715k
  evexrm = 0;
1681
715k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1682
110k
      insn->mode == MODE_64BIT) {
1683
48.1k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1684
48.1k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1685
48.1k
  }
1686
1687
715k
  insn->reg = (Reg)(insn->regBase + reg);
1688
1689
715k
  switch (insn->addressSize) {
1690
204k
  case 2: {
1691
204k
    EABase eaBaseBase = EA_BASE_BX_SI;
1692
1693
204k
    switch (mod) {
1694
112k
    case 0x0:
1695
112k
      if (rm == 0x6) {
1696
6.71k
        insn->eaBase = EA_BASE_NONE;
1697
6.71k
        insn->eaDisplacement = EA_DISP_16;
1698
6.71k
        if (readDisplacement(insn))
1699
27
          return -1;
1700
106k
      } else {
1701
106k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1702
106k
        insn->eaDisplacement = EA_DISP_NONE;
1703
106k
      }
1704
112k
      break;
1705
112k
    case 0x1:
1706
32.5k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1707
32.5k
      insn->eaDisplacement = EA_DISP_8;
1708
32.5k
      insn->displacementSize = 1;
1709
32.5k
      if (readDisplacement(insn))
1710
49
        return -1;
1711
32.4k
      break;
1712
32.4k
    case 0x2:
1713
15.3k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1714
15.3k
      insn->eaDisplacement = EA_DISP_16;
1715
15.3k
      if (readDisplacement(insn))
1716
92
        return -1;
1717
15.2k
      break;
1718
43.6k
    case 0x3:
1719
43.6k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1720
43.6k
      if (readDisplacement(insn))
1721
0
        return -1;
1722
43.6k
      break;
1723
204k
    }
1724
204k
    break;
1725
204k
  }
1726
1727
230k
  case 4:
1728
510k
  case 8: {
1729
510k
    EABase eaBaseBase =
1730
510k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1731
1732
510k
    switch (mod) {
1733
0
    default:
1734
0
      break;
1735
252k
    case 0x0:
1736
252k
      insn->eaDisplacement =
1737
252k
        EA_DISP_NONE; /* readSIB may override this */
1738
      // In determining whether RIP-relative mode is used (rm=5),
1739
      // or whether a SIB byte is present (rm=4),
1740
      // the extension bits (REX.b and EVEX.x) are ignored.
1741
252k
      switch (rm & 7) {
1742
22.6k
      case 0x4: // SIB byte is present
1743
22.6k
        insn->eaBase = (insn->addressSize == 4 ?
1744
8.94k
              EA_BASE_sib :
1745
22.6k
              EA_BASE_sib64);
1746
22.6k
        if (readSIB(insn) || readDisplacement(insn))
1747
55
          return -1;
1748
22.6k
        break;
1749
22.6k
      case 0x5: // RIP-relative
1750
6.42k
        insn->eaBase = EA_BASE_NONE;
1751
6.42k
        insn->eaDisplacement = EA_DISP_32;
1752
6.42k
        if (readDisplacement(insn))
1753
63
          return -1;
1754
6.36k
        break;
1755
223k
      default:
1756
223k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1757
223k
        break;
1758
252k
      }
1759
252k
      break;
1760
252k
    case 0x1:
1761
91.9k
      insn->displacementSize = 1;
1762
      /* FALLTHROUGH */
1763
115k
    case 0x2:
1764
115k
      insn->eaDisplacement =
1765
115k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1766
115k
      switch (rm & 7) {
1767
13.8k
      case 0x4: // SIB byte is present
1768
13.8k
        insn->eaBase = EA_BASE_sib;
1769
13.8k
        if (readSIB(insn) || readDisplacement(insn))
1770
123
          return -1;
1771
13.7k
        break;
1772
101k
      default:
1773
101k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1774
101k
        if (readDisplacement(insn))
1775
468
          return -1;
1776
101k
        break;
1777
115k
      }
1778
115k
      break;
1779
142k
    case 0x3:
1780
142k
      insn->eaDisplacement = EA_DISP_NONE;
1781
142k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1782
142k
      break;
1783
510k
    }
1784
1785
510k
    break;
1786
510k
  }
1787
715k
  } /* switch (insn->addressSize) */
1788
1789
714k
  return 0;
1790
715k
}
1791
1792
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1793
  static uint16_t name(struct InternalInstruction *insn, \
1794
           OperandType type, uint8_t index, uint8_t *valid) \
1795
829k
  { \
1796
829k
    *valid = 1; \
1797
829k
    switch (type) { \
1798
0
    default: \
1799
0
      *valid = 0; \
1800
0
      return 0; \
1801
183k
    case TYPE_Rv: \
1802
183k
      return base + index; \
1803
232k
    case TYPE_R8: \
1804
232k
      index &= mask; \
1805
232k
      if (index > 0xf) \
1806
232k
        *valid = 0; \
1807
232k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1808
3.66k
        return prefix##_SPL + (index - 4); \
1809
228k
      } else { \
1810
228k
        return prefix##_AL + index; \
1811
228k
      } \
1812
232k
    case TYPE_R16: \
1813
7.24k
      index &= mask; \
1814
7.24k
      if (index > 0xf) \
1815
7.24k
        *valid = 0; \
1816
7.24k
      return prefix##_AX + index; \
1817
232k
    case TYPE_R32: \
1818
8.74k
      index &= mask; \
1819
8.74k
      if (index > 0xf) \
1820
8.74k
        *valid = 0; \
1821
8.74k
      return prefix##_EAX + index; \
1822
232k
    case TYPE_R64: \
1823
24.4k
      index &= mask; \
1824
24.4k
      if (index > 0xf) \
1825
24.4k
        *valid = 0; \
1826
24.4k
      return prefix##_RAX + index; \
1827
232k
    case TYPE_ZMM: \
1828
82.6k
      return prefix##_ZMM0 + index; \
1829
232k
    case TYPE_YMM: \
1830
71.2k
      return prefix##_YMM0 + index; \
1831
232k
    case TYPE_XMM: \
1832
143k
      return prefix##_XMM0 + index; \
1833
232k
    case TYPE_VK: \
1834
47.8k
      index &= 0xf; \
1835
47.8k
      if (index > 7) \
1836
47.8k
        *valid = 0; \
1837
47.8k
      return prefix##_K0 + index; \
1838
232k
    case TYPE_MM64: \
1839
11.3k
      return prefix##_MM0 + (index & 0x7); \
1840
232k
    case TYPE_SEGMENTREG: \
1841
2.37k
      if ((index & 7) > 5) \
1842
2.37k
        *valid = 0; \
1843
2.37k
      return prefix##_ES + (index & 7); \
1844
232k
    case TYPE_DEBUGREG: \
1845
1.12k
      return prefix##_DR0 + index; \
1846
232k
    case TYPE_CONTROLREG: \
1847
832
      return prefix##_CR0 + index; \
1848
232k
    case TYPE_BNDR: \
1849
12.5k
      if (index > 3) \
1850
12.5k
        *valid = 0; \
1851
12.5k
      return prefix##_BND0 + index; \
1852
232k
    case TYPE_MVSIBX: \
1853
0
      return prefix##_XMM0 + index; \
1854
232k
    case TYPE_MVSIBY: \
1855
0
      return prefix##_YMM0 + index; \
1856
232k
    case TYPE_MVSIBZ: \
1857
0
      return prefix##_ZMM0 + index; \
1858
829k
    } \
1859
829k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1795
650k
  { \
1796
650k
    *valid = 1; \
1797
650k
    switch (type) { \
1798
0
    default: \
1799
0
      *valid = 0; \
1800
0
      return 0; \
1801
136k
    case TYPE_Rv: \
1802
136k
      return base + index; \
1803
184k
    case TYPE_R8: \
1804
184k
      index &= mask; \
1805
184k
      if (index > 0xf) \
1806
184k
        *valid = 0; \
1807
184k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1808
2.30k
        return prefix##_SPL + (index - 4); \
1809
182k
      } else { \
1810
182k
        return prefix##_AL + index; \
1811
182k
      } \
1812
184k
    case TYPE_R16: \
1813
5.47k
      index &= mask; \
1814
5.47k
      if (index > 0xf) \
1815
5.47k
        *valid = 0; \
1816
5.47k
      return prefix##_AX + index; \
1817
184k
    case TYPE_R32: \
1818
5.25k
      index &= mask; \
1819
5.25k
      if (index > 0xf) \
1820
5.25k
        *valid = 0; \
1821
5.25k
      return prefix##_EAX + index; \
1822
184k
    case TYPE_R64: \
1823
15.3k
      index &= mask; \
1824
15.3k
      if (index > 0xf) \
1825
15.3k
        *valid = 0; \
1826
15.3k
      return prefix##_RAX + index; \
1827
184k
    case TYPE_ZMM: \
1828
66.0k
      return prefix##_ZMM0 + index; \
1829
184k
    case TYPE_YMM: \
1830
57.1k
      return prefix##_YMM0 + index; \
1831
184k
    case TYPE_XMM: \
1832
113k
      return prefix##_XMM0 + index; \
1833
184k
    case TYPE_VK: \
1834
44.0k
      index &= 0xf; \
1835
44.0k
      if (index > 7) \
1836
44.0k
        *valid = 0; \
1837
44.0k
      return prefix##_K0 + index; \
1838
184k
    case TYPE_MM64: \
1839
6.99k
      return prefix##_MM0 + (index & 0x7); \
1840
184k
    case TYPE_SEGMENTREG: \
1841
2.37k
      if ((index & 7) > 5) \
1842
2.37k
        *valid = 0; \
1843
2.37k
      return prefix##_ES + (index & 7); \
1844
184k
    case TYPE_DEBUGREG: \
1845
1.12k
      return prefix##_DR0 + index; \
1846
184k
    case TYPE_CONTROLREG: \
1847
832
      return prefix##_CR0 + index; \
1848
184k
    case TYPE_BNDR: \
1849
11.0k
      if (index > 3) \
1850
11.0k
        *valid = 0; \
1851
11.0k
      return prefix##_BND0 + index; \
1852
184k
    case TYPE_MVSIBX: \
1853
0
      return prefix##_XMM0 + index; \
1854
184k
    case TYPE_MVSIBY: \
1855
0
      return prefix##_YMM0 + index; \
1856
184k
    case TYPE_MVSIBZ: \
1857
0
      return prefix##_ZMM0 + index; \
1858
650k
    } \
1859
650k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1795
178k
  { \
1796
178k
    *valid = 1; \
1797
178k
    switch (type) { \
1798
0
    default: \
1799
0
      *valid = 0; \
1800
0
      return 0; \
1801
46.3k
    case TYPE_Rv: \
1802
46.3k
      return base + index; \
1803
47.5k
    case TYPE_R8: \
1804
47.5k
      index &= mask; \
1805
47.5k
      if (index > 0xf) \
1806
47.5k
        *valid = 0; \
1807
47.5k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1808
1.36k
        return prefix##_SPL + (index - 4); \
1809
46.1k
      } else { \
1810
46.1k
        return prefix##_AL + index; \
1811
46.1k
      } \
1812
47.5k
    case TYPE_R16: \
1813
1.76k
      index &= mask; \
1814
1.76k
      if (index > 0xf) \
1815
1.76k
        *valid = 0; \
1816
1.76k
      return prefix##_AX + index; \
1817
47.5k
    case TYPE_R32: \
1818
3.49k
      index &= mask; \
1819
3.49k
      if (index > 0xf) \
1820
3.49k
        *valid = 0; \
1821
3.49k
      return prefix##_EAX + index; \
1822
47.5k
    case TYPE_R64: \
1823
9.05k
      index &= mask; \
1824
9.05k
      if (index > 0xf) \
1825
9.05k
        *valid = 0; \
1826
9.05k
      return prefix##_RAX + index; \
1827
47.5k
    case TYPE_ZMM: \
1828
16.5k
      return prefix##_ZMM0 + index; \
1829
47.5k
    case TYPE_YMM: \
1830
14.1k
      return prefix##_YMM0 + index; \
1831
47.5k
    case TYPE_XMM: \
1832
30.2k
      return prefix##_XMM0 + index; \
1833
47.5k
    case TYPE_VK: \
1834
3.78k
      index &= 0xf; \
1835
3.78k
      if (index > 7) \
1836
3.78k
        *valid = 0; \
1837
3.78k
      return prefix##_K0 + index; \
1838
47.5k
    case TYPE_MM64: \
1839
4.35k
      return prefix##_MM0 + (index & 0x7); \
1840
47.5k
    case TYPE_SEGMENTREG: \
1841
0
      if ((index & 7) > 5) \
1842
0
        *valid = 0; \
1843
0
      return prefix##_ES + (index & 7); \
1844
47.5k
    case TYPE_DEBUGREG: \
1845
0
      return prefix##_DR0 + index; \
1846
47.5k
    case TYPE_CONTROLREG: \
1847
0
      return prefix##_CR0 + index; \
1848
47.5k
    case TYPE_BNDR: \
1849
1.46k
      if (index > 3) \
1850
1.46k
        *valid = 0; \
1851
1.46k
      return prefix##_BND0 + index; \
1852
47.5k
    case TYPE_MVSIBX: \
1853
0
      return prefix##_XMM0 + index; \
1854
47.5k
    case TYPE_MVSIBY: \
1855
0
      return prefix##_YMM0 + index; \
1856
47.5k
    case TYPE_MVSIBZ: \
1857
0
      return prefix##_ZMM0 + index; \
1858
178k
    } \
1859
178k
  }
1860
1861
/*
1862
 * fixup*Value - Consults an operand type to determine the meaning of the
1863
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1864
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1865
 *   misinterpret it as.
1866
 *
1867
 * @param insn  - The instruction containing the operand.
1868
 * @param type  - The operand type.
1869
 * @param index - The existing value of the field as reported by readModRM().
1870
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1871
 *                field is valid for the register class; 0 if not.
1872
 * @return      - The proper value.
1873
 */
1874
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1875
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1876
1877
/*
1878
 * fixupReg - Consults an operand specifier to determine which of the
1879
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1880
 *
1881
 * @param insn  - See fixup*Value().
1882
 * @param op    - The operand specifier.
1883
 * @return      - 0 if fixup was successful; -1 if the register returned was
1884
 *                invalid for its class.
1885
 */
1886
static int fixupReg(struct InternalInstruction *insn,
1887
        const struct OperandSpecifier *op)
1888
1.34M
{
1889
1.34M
  uint8_t valid;
1890
1891
1.34M
  switch ((OperandEncoding)op->encoding) {
1892
0
  default:
1893
    // debug("Expected a REG or R/M encoding in fixupReg");
1894
0
    return -1;
1895
107k
  case ENCODING_VVVV:
1896
107k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1897
107k
            insn->vvvv, &valid);
1898
107k
    if (!valid)
1899
3
      return -1;
1900
107k
    break;
1901
543k
  case ENCODING_REG:
1902
543k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1903
543k
                 insn->reg - insn->regBase,
1904
543k
                 &valid);
1905
543k
    if (!valid)
1906
26
      return -1;
1907
543k
    break;
1908
4.50M
CASE_ENCODING_RM:
1909
4.50M
    if (insn->eaBase >= insn->eaRegBase) {
1910
178k
      insn->eaBase = (EABase)fixupRMValue(
1911
178k
        insn, (OperandType)op->type,
1912
178k
        insn->eaBase - insn->eaRegBase, &valid);
1913
178k
      if (!valid)
1914
4
        return -1;
1915
178k
    }
1916
697k
    break;
1917
1.34M
  }
1918
1919
1.34M
  return 0;
1920
1.34M
}
1921
1922
/*
1923
 * readOpcodeRegister - Reads an operand from the opcode field of an
1924
 *   instruction and interprets it appropriately given the operand width.
1925
 *   Handles AddRegFrm instructions.
1926
 *
1927
 * @param insn  - the instruction whose opcode field is to be read.
1928
 * @param size  - The width (in bytes) of the register being specified.
1929
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1930
 *                RAX.
1931
 * @return      - 0 on success; nonzero otherwise.
1932
 */
1933
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
1934
118k
{
1935
118k
  if (size == 0)
1936
84.9k
    size = insn->registerSize;
1937
1938
118k
  switch (size) {
1939
14.9k
  case 1:
1940
14.9k
    insn->opcodeRegister =
1941
14.9k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1942
14.9k
                (insn->opcode & 7)));
1943
14.9k
    if (insn->rexPrefix &&
1944
1.48k
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1945
936
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1946
329
      insn->opcodeRegister =
1947
329
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1948
329
                   MODRM_REG_AL - 4));
1949
329
    }
1950
1951
14.9k
    break;
1952
33.0k
  case 2:
1953
33.0k
    insn->opcodeRegister =
1954
33.0k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1955
33.0k
                (insn->opcode & 7)));
1956
33.0k
    break;
1957
51.2k
  case 4:
1958
51.2k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1959
51.2k
               ((bFromREX(insn->rexPrefix) << 3) |
1960
51.2k
                (insn->opcode & 7)));
1961
51.2k
    break;
1962
19.5k
  case 8:
1963
19.5k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1964
19.5k
               ((bFromREX(insn->rexPrefix) << 3) |
1965
19.5k
                (insn->opcode & 7)));
1966
19.5k
    break;
1967
118k
  }
1968
1969
118k
  return 0;
1970
118k
}
1971
1972
/*
1973
 * readImmediate - Consumes an immediate operand from an instruction, given the
1974
 *   desired operand size.
1975
 *
1976
 * @param insn  - The instruction whose operand is to be read.
1977
 * @param size  - The width (in bytes) of the operand.
1978
 * @return      - 0 if the immediate was successfully consumed; nonzero
1979
 *                otherwise.
1980
 */
1981
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
1982
357k
{
1983
357k
  uint8_t imm8;
1984
357k
  uint16_t imm16;
1985
357k
  uint32_t imm32;
1986
357k
  uint64_t imm64;
1987
1988
357k
  if (insn->numImmediatesConsumed == 2) {
1989
    // debug("Already consumed two immediates");
1990
0
    return -1;
1991
0
  }
1992
1993
357k
  if (size == 0)
1994
0
    size = insn->immediateSize;
1995
357k
  else
1996
357k
    insn->immediateSize = size;
1997
1998
357k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1999
2000
357k
  switch (size) {
2001
271k
  case 1:
2002
271k
    if (consumeByte(insn, &imm8))
2003
1.05k
      return -1;
2004
2005
270k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
2006
270k
    break;
2007
48.7k
  case 2:
2008
48.7k
    if (consumeUInt16(insn, &imm16))
2009
371
      return -1;
2010
2011
48.4k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
2012
48.4k
    break;
2013
32.1k
  case 4:
2014
32.1k
    if (consumeUInt32(insn, &imm32))
2015
595
      return -1;
2016
2017
31.5k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
2018
31.5k
    break;
2019
5.31k
  case 8:
2020
5.31k
    if (consumeUInt64(insn, &imm64))
2021
135
      return -1;
2022
5.18k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
2023
5.18k
    break;
2024
357k
  }
2025
2026
355k
  insn->numImmediatesConsumed++;
2027
2028
355k
  return 0;
2029
357k
}
2030
2031
/*
2032
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
2033
 *
2034
 * @param insn  - The instruction whose operand is to be read.
2035
 * @return      - 0 if the vvvv was successfully consumed; nonzero
2036
 *                otherwise.
2037
 */
2038
static int readVVVV(struct InternalInstruction *insn)
2039
1.25M
{
2040
1.25M
  int vvvv;
2041
2042
1.25M
  if (insn->vectorExtensionType == TYPE_EVEX)
2043
110k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2044
110k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2045
1.14M
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2046
11.1k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2047
1.13M
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2048
17.9k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2049
1.11M
  else if (insn->vectorExtensionType == TYPE_XOP)
2050
12.9k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2051
1.10M
  else
2052
1.10M
    return -1;
2053
2054
152k
  if (insn->mode != MODE_64BIT)
2055
91.2k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2056
2057
152k
  insn->vvvv = (Reg)vvvv;
2058
2059
152k
  return 0;
2060
1.25M
}
2061
2062
/*
2063
 * readMaskRegister - Reads an mask register from the opcode field of an
2064
 *   instruction.
2065
 *
2066
 * @param insn    - The instruction whose opcode field is to be read.
2067
 * @return        - 0 on success; nonzero otherwise.
2068
 */
2069
static int readMaskRegister(struct InternalInstruction *insn)
2070
78.5k
{
2071
78.5k
  if (insn->vectorExtensionType != TYPE_EVEX)
2072
0
    return -1;
2073
2074
78.5k
  insn->writemask =
2075
78.5k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2076
2077
78.5k
  return 0;
2078
78.5k
}
2079
2080
/*
2081
 * readOperands - Consults the specifier for an instruction and consumes all
2082
 *   operands for that instruction, interpreting them as it goes.
2083
 *
2084
 * @param insn  - The instruction whose operands are to be read and interpreted.
2085
 * @return      - 0 if all operands could be read; nonzero otherwise.
2086
 */
2087
static int readOperands(struct InternalInstruction *insn)
2088
1.25M
{
2089
1.25M
  int hasVVVV, needVVVV;
2090
1.25M
  int sawRegImm = 0;
2091
1.25M
  int i;
2092
2093
  /* If non-zero vvvv specified, need to make sure one of the operands
2094
     uses it. */
2095
1.25M
  hasVVVV = !readVVVV(insn);
2096
1.25M
  needVVVV = hasVVVV && (insn->vvvv != 0);
2097
2098
8.76M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2099
7.51M
    const OperandSpecifier *op =
2100
7.51M
      &x86OperandSets[insn->spec->operands][i];
2101
7.51M
    switch (op->encoding) {
2102
5.17M
    case ENCODING_NONE:
2103
5.22M
    case ENCODING_SI:
2104
5.29M
    case ENCODING_DI:
2105
5.29M
      break;
2106
2107
50.6k
CASE_ENCODING_VSIB:
2108
      // VSIB can use the V2 bit so check only the other bits.
2109
50.6k
      if (needVVVV)
2110
6.66k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2111
2112
50.6k
      if (readModRM(insn))
2113
0
        return -1;
2114
2115
      // Reject if SIB wasn't used.
2116
9.78k
      if (insn->eaBase != EA_BASE_sib &&
2117
6.34k
          insn->eaBase != EA_BASE_sib64)
2118
19
        return -1;
2119
2120
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2121
9.76k
      if (insn->sibIndex == SIB_INDEX_NONE)
2122
1.15k
        insn->sibIndex =
2123
1.15k
          (SIBIndex)(insn->sibIndexBase + 4);
2124
2125
      // If EVEX.v2 is set this is one of the 16-31 registers.
2126
9.76k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2127
7.47k
          insn->mode == MODE_64BIT &&
2128
5.36k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2129
4.37k
        insn->sibIndex =
2130
4.37k
          (SIBIndex)(insn->sibIndex + 16);
2131
2132
      // Adjust the index register to the correct size.
2133
9.76k
      switch (op->type) {
2134
0
      default:
2135
        // debug("Unhandled VSIB index type");
2136
0
        return -1;
2137
3.51k
      case TYPE_MVSIBX:
2138
3.51k
        insn->sibIndex =
2139
3.51k
          (SIBIndex)(SIB_INDEX_XMM0 +
2140
3.51k
               (insn->sibIndex -
2141
3.51k
                insn->sibIndexBase));
2142
3.51k
        break;
2143
2.98k
      case TYPE_MVSIBY:
2144
2.98k
        insn->sibIndex =
2145
2.98k
          (SIBIndex)(SIB_INDEX_YMM0 +
2146
2.98k
               (insn->sibIndex -
2147
2.98k
                insn->sibIndexBase));
2148
2.98k
        break;
2149
3.26k
      case TYPE_MVSIBZ:
2150
3.26k
        insn->sibIndex =
2151
3.26k
          (SIBIndex)(SIB_INDEX_ZMM0 +
2152
3.26k
               (insn->sibIndex -
2153
3.26k
                insn->sibIndexBase));
2154
3.26k
        break;
2155
9.76k
      }
2156
2157
      // Apply the AVX512 compressed displacement scaling factor.
2158
9.76k
      if (op->encoding != ENCODING_REG &&
2159
9.76k
          insn->eaDisplacement == EA_DISP_8)
2160
1.59k
        insn->displacement *=
2161
1.59k
          1 << (op->encoding - ENCODING_VSIB);
2162
9.76k
      break;
2163
2164
543k
    case ENCODING_REG:
2165
8.31M
CASE_ENCODING_RM:
2166
8.31M
      if (readModRM(insn))
2167
0
        return -1;
2168
2169
1.24M
      if (fixupReg(insn, op))
2170
30
        return -1;
2171
2172
      // Apply the AVX512 compressed displacement scaling factor.
2173
1.24M
      if (op->encoding != ENCODING_REG &&
2174
697k
          insn->eaDisplacement == EA_DISP_8)
2175
122k
        insn->displacement *=
2176
122k
          1 << (op->encoding - ENCODING_RM);
2177
1.24M
      break;
2178
2179
274k
    case ENCODING_IB:
2180
274k
      if (sawRegImm) {
2181
        /* Saw a register immediate so don't read again and instead split the
2182
             previous immediate.  FIXME: This is a hack. */
2183
2.59k
        insn->immediates[insn->numImmediatesConsumed] =
2184
2.59k
          insn->immediates
2185
2.59k
            [insn->numImmediatesConsumed -
2186
2.59k
             1] &
2187
2.59k
          0xf;
2188
2.59k
        ++insn->numImmediatesConsumed;
2189
2.59k
        break;
2190
2.59k
      }
2191
271k
      if (readImmediate(insn, 1))
2192
1.05k
        return -1;
2193
270k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2194
4.18k
        sawRegImm = 1;
2195
270k
      break;
2196
2197
18.1k
    case ENCODING_IW:
2198
18.1k
      if (readImmediate(insn, 2))
2199
94
        return -1;
2200
18.0k
      break;
2201
2202
18.0k
    case ENCODING_ID:
2203
6.90k
      if (readImmediate(insn, 4))
2204
130
        return -1;
2205
6.77k
      break;
2206
2207
6.77k
    case ENCODING_IO:
2208
1.10k
      if (readImmediate(insn, 8))
2209
23
        return -1;
2210
1.08k
      break;
2211
2212
47.0k
    case ENCODING_Iv:
2213
47.0k
      if (readImmediate(insn, insn->immediateSize))
2214
652
        return -1;
2215
46.4k
      break;
2216
2217
46.4k
    case ENCODING_Ia:
2218
12.9k
      if (readImmediate(insn, insn->addressSize))
2219
202
        return -1;
2220
      /* Direct memory-offset (moffset) immediate will get mapped
2221
           to memory operand later. We want the encoding info to
2222
           reflect that as well. */
2223
12.7k
      insn->displacementOffset = insn->immediateOffset;
2224
12.7k
      insn->consumedDisplacement = true;
2225
12.7k
      insn->displacementSize = insn->immediateSize;
2226
12.7k
      insn->displacement =
2227
12.7k
        insn->immediates[insn->numImmediatesConsumed -
2228
12.7k
             1];
2229
12.7k
      insn->immediateOffset = 0;
2230
12.7k
      insn->immediateSize = 0;
2231
12.7k
      break;
2232
2233
6.85k
    case ENCODING_IRC:
2234
6.85k
      insn->RC =
2235
6.85k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2236
6.85k
         << 1) |
2237
6.85k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2238
6.85k
      break;
2239
2240
14.9k
    case ENCODING_RB:
2241
14.9k
      if (readOpcodeRegister(insn, 1))
2242
0
        return -1;
2243
14.9k
      break;
2244
2245
14.9k
    case ENCODING_RW:
2246
0
      if (readOpcodeRegister(insn, 2))
2247
0
        return -1;
2248
0
      break;
2249
2250
0
    case ENCODING_RD:
2251
0
      if (readOpcodeRegister(insn, 4))
2252
0
        return -1;
2253
0
      break;
2254
2255
18.9k
    case ENCODING_RO:
2256
18.9k
      if (readOpcodeRegister(insn, 8))
2257
0
        return -1;
2258
18.9k
      break;
2259
2260
84.9k
    case ENCODING_Rv:
2261
84.9k
      if (readOpcodeRegister(insn, 0))
2262
0
        return -1;
2263
84.9k
      break;
2264
2265
84.9k
    case ENCODING_FP:
2266
5.10k
      break;
2267
2268
107k
    case ENCODING_VVVV:
2269
107k
      if (!hasVVVV)
2270
0
        return -1;
2271
2272
107k
      needVVVV =
2273
107k
        0; /* Mark that we have found a VVVV operand. */
2274
2275
107k
      if (insn->mode != MODE_64BIT)
2276
63.8k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2277
2278
107k
      if (fixupReg(insn, op))
2279
3
        return -1;
2280
107k
      break;
2281
2282
107k
    case ENCODING_WRITEMASK:
2283
78.5k
      if (readMaskRegister(insn))
2284
0
        return -1;
2285
78.5k
      break;
2286
2287
285k
    case ENCODING_DUP:
2288
285k
      break;
2289
2290
0
    default:
2291
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2292
0
      return -1;
2293
7.51M
    }
2294
7.51M
  }
2295
2296
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2297
1.25M
  if (needVVVV)
2298
15
    return -1;
2299
2300
1.25M
  return 0;
2301
1.25M
}
2302
2303
// return True if instruction is illegal to use with prefixes
2304
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2305
static bool checkPrefix(struct InternalInstruction *insn)
2306
1.25M
{
2307
  // LOCK prefix
2308
1.25M
  if (insn->hasLockPrefix) {
2309
69.3k
    switch (insn->instructionID) {
2310
243
    default:
2311
      // invalid LOCK
2312
243
      return true;
2313
2314
    // nop dword [rax]
2315
384
    case X86_NOOPL:
2316
2317
    // DEC
2318
635
    case X86_DEC16m:
2319
1.23k
    case X86_DEC32m:
2320
1.84k
    case X86_DEC64m:
2321
2.32k
    case X86_DEC8m:
2322
2323
    // ADC
2324
2.71k
    case X86_ADC16mi:
2325
3.11k
    case X86_ADC16mi8:
2326
3.74k
    case X86_ADC16mr:
2327
4.17k
    case X86_ADC32mi:
2328
4.57k
    case X86_ADC32mi8:
2329
4.99k
    case X86_ADC32mr:
2330
5.29k
    case X86_ADC64mi32:
2331
5.43k
    case X86_ADC64mi8:
2332
5.69k
    case X86_ADC64mr:
2333
6.01k
    case X86_ADC8mi:
2334
6.43k
    case X86_ADC8mi8:
2335
6.88k
    case X86_ADC8mr:
2336
7.37k
    case X86_ADC8rm:
2337
7.79k
    case X86_ADC16rm:
2338
8.24k
    case X86_ADC32rm:
2339
8.55k
    case X86_ADC64rm:
2340
2341
    // ADD
2342
8.98k
    case X86_ADD16mi:
2343
9.44k
    case X86_ADD16mi8:
2344
9.97k
    case X86_ADD16mr:
2345
10.4k
    case X86_ADD32mi:
2346
10.7k
    case X86_ADD32mi8:
2347
11.3k
    case X86_ADD32mr:
2348
11.8k
    case X86_ADD64mi32:
2349
12.2k
    case X86_ADD64mi8:
2350
12.8k
    case X86_ADD64mr:
2351
13.5k
    case X86_ADD8mi:
2352
14.0k
    case X86_ADD8mi8:
2353
15.2k
    case X86_ADD8mr:
2354
15.6k
    case X86_ADD8rm:
2355
16.0k
    case X86_ADD16rm:
2356
16.5k
    case X86_ADD32rm:
2357
17.2k
    case X86_ADD64rm:
2358
2359
    // AND
2360
17.5k
    case X86_AND16mi:
2361
17.9k
    case X86_AND16mi8:
2362
18.2k
    case X86_AND16mr:
2363
18.7k
    case X86_AND32mi:
2364
19.3k
    case X86_AND32mi8:
2365
19.8k
    case X86_AND32mr:
2366
20.2k
    case X86_AND64mi32:
2367
20.5k
    case X86_AND64mi8:
2368
21.0k
    case X86_AND64mr:
2369
21.3k
    case X86_AND8mi:
2370
21.6k
    case X86_AND8mi8:
2371
22.1k
    case X86_AND8mr:
2372
22.4k
    case X86_AND8rm:
2373
23.0k
    case X86_AND16rm:
2374
23.6k
    case X86_AND32rm:
2375
24.0k
    case X86_AND64rm:
2376
2377
    // BTC
2378
24.3k
    case X86_BTC16mi8:
2379
24.5k
    case X86_BTC16mr:
2380
24.8k
    case X86_BTC32mi8:
2381
25.1k
    case X86_BTC32mr:
2382
25.5k
    case X86_BTC64mi8:
2383
26.3k
    case X86_BTC64mr:
2384
2385
    // BTR
2386
26.6k
    case X86_BTR16mi8:
2387
27.0k
    case X86_BTR16mr:
2388
27.2k
    case X86_BTR32mi8:
2389
27.6k
    case X86_BTR32mr:
2390
28.0k
    case X86_BTR64mi8:
2391
28.3k
    case X86_BTR64mr:
2392
2393
    // BTS
2394
28.8k
    case X86_BTS16mi8:
2395
29.2k
    case X86_BTS16mr:
2396
29.5k
    case X86_BTS32mi8:
2397
29.9k
    case X86_BTS32mr:
2398
30.3k
    case X86_BTS64mi8:
2399
30.8k
    case X86_BTS64mr:
2400
2401
    // CMPXCHG
2402
31.3k
    case X86_CMPXCHG16B:
2403
31.7k
    case X86_CMPXCHG16rm:
2404
32.1k
    case X86_CMPXCHG32rm:
2405
32.5k
    case X86_CMPXCHG64rm:
2406
32.9k
    case X86_CMPXCHG8rm:
2407
33.1k
    case X86_CMPXCHG8B:
2408
2409
    // INC
2410
33.7k
    case X86_INC16m:
2411
34.1k
    case X86_INC32m:
2412
34.5k
    case X86_INC64m:
2413
34.9k
    case X86_INC8m:
2414
2415
    // NEG
2416
35.3k
    case X86_NEG16m:
2417
35.7k
    case X86_NEG32m:
2418
36.0k
    case X86_NEG64m:
2419
36.3k
    case X86_NEG8m:
2420
2421
    // NOT
2422
36.7k
    case X86_NOT16m:
2423
37.3k
    case X86_NOT32m:
2424
37.6k
    case X86_NOT64m:
2425
38.1k
    case X86_NOT8m:
2426
2427
    // OR
2428
38.4k
    case X86_OR16mi:
2429
38.9k
    case X86_OR16mi8:
2430
39.3k
    case X86_OR16mr:
2431
39.7k
    case X86_OR32mi:
2432
40.2k
    case X86_OR32mi8:
2433
40.7k
    case X86_OR32mr:
2434
40.9k
    case X86_OR64mi32:
2435
41.4k
    case X86_OR64mi8:
2436
41.6k
    case X86_OR64mr:
2437
42.0k
    case X86_OR8mi8:
2438
42.4k
    case X86_OR8mi:
2439
42.9k
    case X86_OR8mr:
2440
43.4k
    case X86_OR8rm:
2441
43.9k
    case X86_OR16rm:
2442
44.4k
    case X86_OR32rm:
2443
44.9k
    case X86_OR64rm:
2444
2445
    // SBB
2446
45.3k
    case X86_SBB16mi:
2447
45.8k
    case X86_SBB16mi8:
2448
46.4k
    case X86_SBB16mr:
2449
46.8k
    case X86_SBB32mi:
2450
47.2k
    case X86_SBB32mi8:
2451
47.7k
    case X86_SBB32mr:
2452
48.1k
    case X86_SBB64mi32:
2453
48.6k
    case X86_SBB64mi8:
2454
48.8k
    case X86_SBB64mr:
2455
49.2k
    case X86_SBB8mi:
2456
49.6k
    case X86_SBB8mi8:
2457
50.1k
    case X86_SBB8mr:
2458
2459
    // SUB
2460
50.5k
    case X86_SUB16mi:
2461
50.9k
    case X86_SUB16mi8:
2462
51.3k
    case X86_SUB16mr:
2463
51.7k
    case X86_SUB32mi:
2464
52.2k
    case X86_SUB32mi8:
2465
52.8k
    case X86_SUB32mr:
2466
53.2k
    case X86_SUB64mi32:
2467
53.9k
    case X86_SUB64mi8:
2468
54.1k
    case X86_SUB64mr:
2469
54.4k
    case X86_SUB8mi8:
2470
54.7k
    case X86_SUB8mi:
2471
55.3k
    case X86_SUB8mr:
2472
55.7k
    case X86_SUB8rm:
2473
56.2k
    case X86_SUB16rm:
2474
56.7k
    case X86_SUB32rm:
2475
57.1k
    case X86_SUB64rm:
2476
2477
    // XADD
2478
57.5k
    case X86_XADD16rm:
2479
57.7k
    case X86_XADD32rm:
2480
58.3k
    case X86_XADD64rm:
2481
58.6k
    case X86_XADD8rm:
2482
2483
    // XCHG
2484
59.3k
    case X86_XCHG16rm:
2485
59.9k
    case X86_XCHG32rm:
2486
60.4k
    case X86_XCHG64rm:
2487
60.9k
    case X86_XCHG8rm:
2488
2489
    // XOR
2490
61.3k
    case X86_XOR16mi:
2491
62.2k
    case X86_XOR16mi8:
2492
62.6k
    case X86_XOR16mr:
2493
63.0k
    case X86_XOR32mi:
2494
64.0k
    case X86_XOR32mi8:
2495
64.5k
    case X86_XOR32mr:
2496
64.8k
    case X86_XOR64mi32:
2497
65.2k
    case X86_XOR64mi8:
2498
65.6k
    case X86_XOR64mr:
2499
66.0k
    case X86_XOR8mi8:
2500
66.6k
    case X86_XOR8mi:
2501
67.1k
    case X86_XOR8mr:
2502
67.6k
    case X86_XOR8rm:
2503
68.2k
    case X86_XOR16rm:
2504
68.6k
    case X86_XOR32rm:
2505
69.0k
    case X86_XOR64rm:
2506
2507
      // this instruction can be used with LOCK prefix
2508
69.0k
      return false;
2509
69.3k
    }
2510
69.3k
  }
2511
2512
#if 0
2513
  // REPNE prefix
2514
  if (insn->repeatPrefix) {
2515
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2516
    // In such a case, clear it.
2517
    if (insn->twoByteEscape == 0x0f) {
2518
      insn->prefix0 = 0;
2519
    }
2520
  }
2521
#endif
2522
2523
  // no invalid prefixes
2524
1.18M
  return false;
2525
1.25M
}
2526
2527
/*
2528
 * decodeInstruction - Reads and interprets a full instruction provided by the
2529
 *   user.
2530
 *
2531
 * @param insn      - A pointer to the instruction to be populated.  Must be
2532
 *                    pre-allocated.
2533
 * @param reader    - The function to be used to read the instruction's bytes.
2534
 * @param readerArg - A generic argument to be passed to the reader to store
2535
 *                    any internal state.
2536
 * @param startLoc  - The address (in the reader's address space) of the first
2537
 *                    byte in the instruction.
2538
 * @param mode      - Capstone mode flags.
2539
 * @return          - 0 if instruction is valid; nonzero if not.
2540
 */
2541
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader,
2542
          const void *readerArg, uint64_t startLoc, cs_mode mode)
2543
725k
{
2544
725k
  insn->reader = reader;
2545
725k
  insn->readerArg = readerArg;
2546
725k
  insn->startLocation = startLoc;
2547
725k
  insn->readerCursor = startLoc;
2548
725k
  if (x86_has_feature(mode, CS_MODE_16))
2549
206k
    insn->mode = MODE_16BIT;
2550
519k
  else if (x86_has_feature(mode, CS_MODE_32))
2551
244k
    insn->mode = MODE_32BIT;
2552
274k
  else
2553
274k
    insn->mode = MODE_64BIT;
2554
725k
  insn->numImmediatesConsumed = 0;
2555
2556
725k
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn, mode) ||
2557
723k
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2558
4.94k
    return -1;
2559
2560
720k
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2561
2562
  // instruction length must be <= 15 to be valid
2563
720k
  if (insn->length > 15)
2564
30
    return -1;
2565
2566
720k
  if (insn->operandSize == 0)
2567
720k
    insn->operandSize = insn->registerSize;
2568
2569
720k
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2570
2571
720k
  return 0;
2572
720k
}
2573
2574
#endif