Coverage Report

Created: 2026-09-01 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/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.95M
{
79
1.95M
  return CONTEXTS_SYM[attrMask];
80
1.95M
}
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,
94
    InstructionContext insnContext,
95
    uint16_t opcode)
96
1.95M
{
97
1.95M
  const struct OpcodeDecision *decision = NULL;
98
1.95M
  const uint8_t *indextable = NULL;
99
1.95M
  unsigned int index;
100
101
1.95M
  switch (type) {
102
0
    default:
103
0
      return false;
104
1.68M
    case ONEBYTE:
105
1.68M
      decision = ONEBYTE_SYM;
106
1.68M
      indextable = index_x86DisassemblerOneByteOpcodes;
107
1.68M
      break;
108
151k
    case TWOBYTE:
109
151k
      decision = TWOBYTE_SYM;
110
151k
      indextable = index_x86DisassemblerTwoByteOpcodes;
111
151k
      break;
112
40.0k
    case THREEBYTE_38:
113
40.0k
      decision = THREEBYTE38_SYM;
114
40.0k
      indextable = index_x86DisassemblerThreeByte38Opcodes;
115
40.0k
      break;
116
50.3k
    case THREEBYTE_3A:
117
50.3k
      decision = THREEBYTE3A_SYM;
118
50.3k
      indextable = index_x86DisassemblerThreeByte3AOpcodes;
119
50.3k
      break;
120
0
#ifndef CAPSTONE_X86_REDUCE
121
18.9k
    case XOP8_MAP:
122
18.9k
      decision = XOP8_MAP_SYM;
123
18.9k
      indextable = index_x86DisassemblerXOP8Opcodes;
124
18.9k
      break;
125
2.98k
    case XOP9_MAP:
126
2.98k
      decision = XOP9_MAP_SYM;
127
2.98k
      indextable = index_x86DisassemblerXOP9Opcodes;
128
2.98k
      break;
129
874
    case XOPA_MAP:
130
874
      decision = XOPA_MAP_SYM;
131
874
      indextable = index_x86DisassemblerXOPAOpcodes;
132
874
      break;
133
856
    case THREEDNOW_MAP:
134
      // 3DNow instructions always have ModRM byte
135
856
      return true;
136
1.95M
#endif
137
1.95M
  }
138
139
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
140
1.95M
  index = indextable[insnContext];
141
1.95M
  if (index)
142
1.94M
    return decision[index - 1].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
143
8.57k
  else
144
8.57k
    return false;
145
1.95M
}
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,
158
                       InstructionContext insnContext,
159
                       uint8_t opcode,
160
                       uint8_t modRM)
161
1.95M
{
162
1.95M
  const struct ModRMDecision *dec = NULL;
163
1.95M
  unsigned int index;
164
1.95M
  static const struct OpcodeDecision emptyDecision = { 0 };
165
166
1.95M
  switch (type) {
167
0
    default:
168
0
      return 0;
169
1.68M
    case ONEBYTE:
170
      // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
171
1.68M
      index = index_x86DisassemblerOneByteOpcodes[insnContext];
172
1.68M
      if (index)
173
1.68M
        dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
174
571
      else
175
571
        dec = &emptyDecision.modRMDecisions[opcode];
176
1.68M
      break;
177
151k
    case TWOBYTE:
178
      //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
179
151k
      index = index_x86DisassemblerTwoByteOpcodes[insnContext];
180
151k
      if (index)
181
148k
        dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
182
3.06k
      else
183
3.06k
        dec = &emptyDecision.modRMDecisions[opcode];
184
151k
      break;
185
40.0k
    case THREEBYTE_38:
186
      // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
187
40.0k
      index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
188
40.0k
      if (index)
189
39.7k
        dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
190
222
      else
191
222
        dec = &emptyDecision.modRMDecisions[opcode];
192
40.0k
      break;
193
50.3k
    case THREEBYTE_3A:
194
      //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
195
50.3k
      index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
196
50.3k
      if (index)
197
49.8k
        dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
198
497
      else
199
497
        dec = &emptyDecision.modRMDecisions[opcode];
200
50.3k
      break;
201
0
#ifndef CAPSTONE_X86_REDUCE
202
18.9k
    case XOP8_MAP:
203
      // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
204
18.9k
      index = index_x86DisassemblerXOP8Opcodes[insnContext];
205
18.9k
      if (index)
206
15.6k
        dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
207
3.20k
      else
208
3.20k
        dec = &emptyDecision.modRMDecisions[opcode];
209
18.9k
      break;
210
2.98k
    case XOP9_MAP:
211
      // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
212
2.98k
      index = index_x86DisassemblerXOP9Opcodes[insnContext];
213
2.98k
      if (index)
214
2.20k
        dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
215
778
      else
216
778
        dec = &emptyDecision.modRMDecisions[opcode];
217
2.98k
      break;
218
874
    case XOPA_MAP:
219
      // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
220
874
      index = index_x86DisassemblerXOPAOpcodes[insnContext];
221
874
      if (index)
222
646
        dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
223
228
      else
224
228
        dec = &emptyDecision.modRMDecisions[opcode];
225
874
      break;
226
856
    case THREEDNOW_MAP:
227
      // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
228
856
      index = index_x86Disassembler3DNowOpcodes[insnContext];
229
856
      if (index)
230
556
        dec = &THREEDNOW_MAP_SYM[index - 1].modRMDecisions[opcode];
231
300
      else
232
300
        dec = &emptyDecision.modRMDecisions[opcode];
233
856
      break;
234
1.95M
#endif
235
1.95M
  }
236
237
1.95M
  switch (dec->modrm_type) {
238
0
    default:
239
      // debug("Corrupt table!  Unknown modrm_type");
240
0
      return 0;
241
939k
    case MODRM_ONEENTRY:
242
939k
      return modRMTable[dec->instructionIDs];
243
766k
    case MODRM_SPLITRM:
244
766k
      if (modFromModRM(modRM) == 0x3)
245
166k
        return modRMTable[dec->instructionIDs + 1];
246
599k
      return modRMTable[dec->instructionIDs];
247
204k
    case MODRM_SPLITREG:
248
204k
      if (modFromModRM(modRM) == 0x3)
249
66.1k
        return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3) + 8];
250
138k
      return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
251
39.6k
    case MODRM_SPLITMISC:
252
39.6k
      if (modFromModRM(modRM) == 0x3)
253
12.9k
        return modRMTable[dec->instructionIDs+(modRM & 0x3f) + 8];
254
26.6k
      return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
255
0
    case MODRM_FULL:
256
0
      return modRMTable[dec->instructionIDs+modRM];
257
1.95M
  }
258
1.95M
}
259
260
/*
261
 * specifierForUID - Given a UID, returns the name and operand specification for
262
 *   that instruction.
263
 *
264
 * @param uid - The unique ID for the instruction.  This should be returned by
265
 *              decode(); specifierForUID will not check bounds.
266
 * @return    - A pointer to the specification for that instruction.
267
 */
268
static const struct InstructionSpecifier *specifierForUID(InstrUID uid)
269
1.64M
{
270
1.64M
  return &INSTRUCTIONS_SYM[uid];
271
1.64M
}
272
273
/*
274
 * consumeByte - Uses the reader function provided by the user to consume one
275
 *   byte from the instruction's memory and advance the cursor.
276
 *
277
 * @param insn  - The instruction with the reader function to use.  The cursor
278
 *                for this instruction is advanced.
279
 * @param byte  - A pointer to a pre-allocated memory buffer to be populated
280
 *                with the data read.
281
 * @return      - 0 if the read was successful; nonzero otherwise.
282
 */
283
static int consumeByte(struct InternalInstruction* insn, uint8_t* byte)
284
4.87M
{
285
4.87M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
286
287
4.87M
  if (!ret)
288
4.86M
    ++(insn->readerCursor);
289
290
4.87M
  return ret;
291
4.87M
}
292
293
/*
294
 * lookAtByte - Like consumeByte, but does not advance the cursor.
295
 *
296
 * @param insn  - See consumeByte().
297
 * @param byte  - See consumeByte().
298
 * @return      - See consumeByte().
299
 */
300
static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte)
301
382k
{
302
382k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
303
382k
}
304
305
static void unconsumeByte(struct InternalInstruction* insn)
306
1.57M
{
307
1.57M
  insn->readerCursor--;
308
1.57M
}
309
310
#define CONSUME_FUNC(name, type)                                  \
311
302k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
302k
    type combined = 0;                                            \
313
302k
    unsigned offset;                                              \
314
989k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
689k
      uint8_t byte;                                               \
316
689k
      int ret = insn->reader(insn->readerArg,                     \
317
689k
                             &byte,                               \
318
689k
                             insn->readerCursor + offset);        \
319
689k
      if (ret)                                                    \
320
689k
        return ret;                                               \
321
689k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
687k
    }                                                             \
323
302k
    *ptr = combined;                                              \
324
300k
    insn->readerCursor += sizeof(type);                           \
325
300k
    return 0;                                                     \
326
302k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
311
120k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
120k
    type combined = 0;                                            \
313
120k
    unsigned offset;                                              \
314
240k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
120k
      uint8_t byte;                                               \
316
120k
      int ret = insn->reader(insn->readerArg,                     \
317
120k
                             &byte,                               \
318
120k
                             insn->readerCursor + offset);        \
319
120k
      if (ret)                                                    \
320
120k
        return ret;                                               \
321
120k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
119k
    }                                                             \
323
120k
    *ptr = combined;                                              \
324
119k
    insn->readerCursor += sizeof(type);                           \
325
119k
    return 0;                                                     \
326
120k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
311
30.1k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
30.1k
    type combined = 0;                                            \
313
30.1k
    unsigned offset;                                              \
314
90.1k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
60.1k
      uint8_t byte;                                               \
316
60.1k
      int ret = insn->reader(insn->readerArg,                     \
317
60.1k
                             &byte,                               \
318
60.1k
                             insn->readerCursor + offset);        \
319
60.1k
      if (ret)                                                    \
320
60.1k
        return ret;                                               \
321
60.1k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
60.0k
    }                                                             \
323
30.1k
    *ptr = combined;                                              \
324
29.9k
    insn->readerCursor += sizeof(type);                           \
325
29.9k
    return 0;                                                     \
326
30.1k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
311
44.0k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
44.0k
    type combined = 0;                                            \
313
44.0k
    unsigned offset;                                              \
314
218k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
175k
      uint8_t byte;                                               \
316
175k
      int ret = insn->reader(insn->readerArg,                     \
317
175k
                             &byte,                               \
318
175k
                             insn->readerCursor + offset);        \
319
175k
      if (ret)                                                    \
320
175k
        return ret;                                               \
321
175k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
174k
    }                                                             \
323
44.0k
    *ptr = combined;                                              \
324
43.5k
    insn->readerCursor += sizeof(type);                           \
325
43.5k
    return 0;                                                     \
326
44.0k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
311
58.7k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
58.7k
    type combined = 0;                                            \
313
58.7k
    unsigned offset;                                              \
314
175k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
117k
      uint8_t byte;                                               \
316
117k
      int ret = insn->reader(insn->readerArg,                     \
317
117k
                             &byte,                               \
318
117k
                             insn->readerCursor + offset);        \
319
117k
      if (ret)                                                    \
320
117k
        return ret;                                               \
321
117k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
117k
    }                                                             \
323
58.7k
    *ptr = combined;                                              \
324
58.4k
    insn->readerCursor += sizeof(type);                           \
325
58.4k
    return 0;                                                     \
326
58.7k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
311
43.9k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
43.9k
    type combined = 0;                                            \
313
43.9k
    unsigned offset;                                              \
314
218k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
175k
      uint8_t byte;                                               \
316
175k
      int ret = insn->reader(insn->readerArg,                     \
317
175k
                             &byte,                               \
318
175k
                             insn->readerCursor + offset);        \
319
175k
      if (ret)                                                    \
320
175k
        return ret;                                               \
321
175k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
174k
    }                                                             \
323
43.9k
    *ptr = combined;                                              \
324
43.4k
    insn->readerCursor += sizeof(type);                           \
325
43.4k
    return 0;                                                     \
326
43.9k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
311
5.18k
  static int name(struct InternalInstruction* insn, type* ptr) {  \
312
5.18k
    type combined = 0;                                            \
313
5.18k
    unsigned offset;                                              \
314
46.1k
    for (offset = 0; offset < sizeof(type); ++offset) {           \
315
41.1k
      uint8_t byte;                                               \
316
41.1k
      int ret = insn->reader(insn->readerArg,                     \
317
41.1k
                             &byte,                               \
318
41.1k
                             insn->readerCursor + offset);        \
319
41.1k
      if (ret)                                                    \
320
41.1k
        return ret;                                               \
321
41.1k
      combined = combined | ((uint64_t)byte << (offset * 8));     \
322
41.0k
    }                                                             \
323
5.18k
    *ptr = combined;                                              \
324
5.08k
    insn->readerCursor += sizeof(type);                           \
325
5.08k
    return 0;                                                     \
326
5.18k
  }
327
328
/*
329
 * consume* - Use the reader function provided by the user to consume data
330
 *   values of various sizes from the instruction's memory and advance the
331
 *   cursor appropriately.  These readers perform endian conversion.
332
 *
333
 * @param insn    - See consumeByte().
334
 * @param ptr     - A pointer to a pre-allocated memory of appropriate size to
335
 *                  be populated with the data read.
336
 * @return        - See consumeByte().
337
 */
338
CONSUME_FUNC(consumeInt8, int8_t)
339
CONSUME_FUNC(consumeInt16, int16_t)
340
CONSUME_FUNC(consumeInt32, int32_t)
341
CONSUME_FUNC(consumeUInt16, uint16_t)
342
CONSUME_FUNC(consumeUInt32, uint32_t)
343
CONSUME_FUNC(consumeUInt64, uint64_t)
344
345
static bool isREX(struct InternalInstruction *insn, uint8_t prefix)
346
1.54M
{
347
1.54M
  if (insn->mode == MODE_64BIT)
348
568k
    return prefix >= 0x40 && prefix <= 0x4f;
349
350
976k
  return false;
351
1.54M
}
352
353
/*
354
 * setPrefixPresent - Marks that a particular prefix is present as mandatory
355
 *
356
 * @param insn      - The instruction to be marked as having the prefix.
357
 * @param prefix    - The prefix that is present.
358
 */
359
static void setPrefixPresent(struct InternalInstruction *insn, uint8_t prefix)
360
115k
{
361
115k
  uint8_t nextByte;
362
363
115k
  switch (prefix) {
364
29.6k
    case 0xf0:  // LOCK
365
29.6k
      insn->hasLockPrefix = true;
366
29.6k
      insn->repeatPrefix = 0;
367
29.6k
      break;
368
369
22.8k
    case 0xf2:  // REPNE/REPNZ
370
43.7k
    case 0xf3:  // REP or REPE/REPZ
371
43.7k
      if (lookAtByte(insn, &nextByte))
372
21
        break;
373
      // TODO:
374
      //  1. There could be several 0x66
375
      //  2. if (nextByte == 0x66) and nextNextByte != 0x0f then
376
      //      it's not mandatory prefix
377
      //  3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
378
      //     0x0f exactly after it to be mandatory prefix
379
43.6k
      if (isREX(insn, nextByte) || nextByte == 0x0f || nextByte == 0x66)
380
        // The last of 0xf2 /0xf3 is mandatory prefix
381
9.84k
        insn->mandatoryPrefix = prefix;
382
383
43.6k
      insn->repeatPrefix = prefix;
384
43.6k
      insn->hasLockPrefix = false;
385
43.6k
      break;
386
387
14.2k
    case 0x66:
388
14.2k
      if (lookAtByte(insn, &nextByte))
389
35
        break;
390
      // 0x66 can't overwrite existing mandatory prefix and should be ignored
391
14.1k
      if (!insn->mandatoryPrefix && (nextByte == 0x0f || isREX(insn, nextByte)))
392
3.98k
        insn->mandatoryPrefix = prefix;
393
14.1k
      break;
394
115k
  }
395
115k
}
396
397
/*
398
 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
399
 *   instruction as having them.  Also sets the instruction's default operand,
400
 *   address, and other relevant data sizes to report operands correctly.
401
 *
402
 * @param insn  - The instruction whose prefixes are to be read.
403
 * @return      - 0 if the instruction could be read until the end of the prefix
404
 *                bytes, and no prefixes conflicted; nonzero otherwise.
405
 */
406
static int readPrefixes(struct InternalInstruction* insn)
407
584k
{
408
584k
  bool isPrefix = true;
409
584k
  uint8_t byte = 0;
410
584k
  uint8_t nextByte;
411
412
1.28M
  while (isPrefix) {
413
700k
    if (insn->mode == MODE_64BIT) {
414
      // eliminate consecutive redundant REX bytes in front
415
276k
      if (consumeByte(insn, &byte))
416
84
        return -1;
417
418
276k
      if ((byte & 0xf0) == 0x40) {
419
49.9k
        while(true) {
420
49.9k
          if (lookAtByte(insn, &byte))  // out of input code
421
62
            return -1;
422
49.9k
          if ((byte & 0xf0) == 0x40) {
423
            // another REX prefix, but we only remember the last one
424
9.18k
            if (consumeByte(insn, &byte))
425
0
              return -1;
426
9.18k
          } else
427
40.7k
            break;
428
49.9k
        }
429
430
        // recover the last REX byte if next byte is not a legacy prefix
431
40.7k
        switch (byte) {
432
1.92k
          case 0xf2:  /* REPNE/REPNZ */
433
3.29k
          case 0xf3:  /* REP or REPE/REPZ */
434
4.19k
          case 0xf0:  /* LOCK */
435
4.40k
          case 0x2e:  /* CS segment override -OR- Branch not taken */
436
4.57k
          case 0x36:  /* SS segment override -OR- Branch taken */
437
4.88k
          case 0x3e:  /* DS segment override */
438
5.22k
          case 0x26:  /* ES segment override */
439
5.40k
          case 0x64:  /* FS segment override */
440
5.61k
          case 0x65:  /* GS segment override */
441
6.32k
          case 0x66:  /* Operand-size override */
442
6.81k
          case 0x67:  /* Address-size override */
443
6.81k
            break;
444
33.9k
          default:    /* Not a prefix byte */
445
33.9k
            unconsumeByte(insn);
446
33.9k
            break;
447
40.7k
        }
448
235k
      } else {
449
235k
        unconsumeByte(insn);
450
235k
      }
451
276k
    }
452
453
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
454
699k
    if (consumeByte(insn, &byte))
455
99
      return -1;
456
457
699k
    if (insn->readerCursor - 1 == insn->startLocation
458
577k
        && (byte == 0xf2 || byte == 0xf3)) {
459
      // prefix requires next byte
460
33.7k
      if (lookAtByte(insn, &nextByte))
461
49
        return -1;
462
463
      /*
464
       * If the byte is 0xf2 or 0xf3, and any of the following conditions are
465
       * met:
466
       * - it is followed by a LOCK (0xf0) prefix
467
       * - it is followed by an xchg instruction
468
       * then it should be disassembled as a xacquire/xrelease not repne/rep.
469
       */
470
33.6k
      if (((nextByte == 0xf0) ||
471
32.9k
        ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) {
472
1.72k
        insn->xAcquireRelease = byte;
473
1.72k
      }
474
475
      /*
476
       * Also if the byte is 0xf3, and the following condition is met:
477
       * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
478
       *                       "mov mem, imm" (opcode 0xc6/0xc7) instructions.
479
       * then it should be disassembled as an xrelease not rep.
480
       */
481
33.6k
      if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 ||
482
16.6k
            nextByte == 0xc6 || nextByte == 0xc7)) {
483
501
        insn->xAcquireRelease = byte;
484
501
      }
485
486
33.6k
      if (isREX(insn, nextByte)) {
487
3.78k
        uint8_t nnextByte;
488
489
        // Go to REX prefix after the current one
490
3.78k
        if (consumeByte(insn, &nnextByte))
491
0
          return -1;
492
493
        // We should be able to read next byte after REX prefix
494
3.78k
        if (lookAtByte(insn, &nnextByte))
495
2
          return -1;
496
497
3.78k
        unconsumeByte(insn);
498
3.78k
      }
499
33.6k
    }
500
501
699k
    switch (byte) {
502
29.6k
      case 0xf0:  /* LOCK */
503
52.4k
      case 0xf2:  /* REPNE/REPNZ */
504
73.3k
      case 0xf3:  /* REP or REPE/REPZ */
505
        // only accept the last prefix
506
73.3k
        setPrefixPresent(insn, byte);
507
73.3k
        insn->prefix0 = byte;
508
73.3k
        break;
509
510
7.16k
      case 0x2e:  /* CS segment override -OR- Branch not taken */
511
8.58k
      case 0x36:  /* SS segment override -OR- Branch taken */
512
12.2k
      case 0x3e:  /* DS segment override */
513
14.8k
      case 0x26:  /* ES segment override */
514
17.9k
      case 0x64:  /* FS segment override */
515
20.6k
      case 0x65:  /* GS segment override */
516
20.6k
        switch (byte) {
517
7.16k
          case 0x2e:
518
7.16k
            insn->segmentOverride = SEG_OVERRIDE_CS;
519
7.16k
            insn->prefix1 = byte;
520
7.16k
            break;
521
1.42k
          case 0x36:
522
1.42k
            insn->segmentOverride = SEG_OVERRIDE_SS;
523
1.42k
            insn->prefix1 = byte;
524
1.42k
            break;
525
3.64k
          case 0x3e:
526
3.64k
            insn->segmentOverride = SEG_OVERRIDE_DS;
527
3.64k
            insn->prefix1 = byte;
528
3.64k
            break;
529
2.57k
          case 0x26:
530
2.57k
            insn->segmentOverride = SEG_OVERRIDE_ES;
531
2.57k
            insn->prefix1 = byte;
532
2.57k
            break;
533
3.17k
          case 0x64:
534
3.17k
            insn->segmentOverride = SEG_OVERRIDE_FS;
535
3.17k
            insn->prefix1 = byte;
536
3.17k
            break;
537
2.66k
          case 0x65:
538
2.66k
            insn->segmentOverride = SEG_OVERRIDE_GS;
539
2.66k
            insn->prefix1 = byte;
540
2.66k
            break;
541
0
          default:
542
            // debug("Unhandled override");
543
0
            return -1;
544
20.6k
        }
545
20.6k
        setPrefixPresent(insn, byte);
546
20.6k
        break;
547
548
14.2k
      case 0x66:  /* Operand-size override */
549
14.2k
        insn->hasOpSize = true;
550
14.2k
        setPrefixPresent(insn, byte);
551
14.2k
        insn->prefix2 = byte;
552
14.2k
        break;
553
554
7.20k
      case 0x67:  /* Address-size override */
555
7.20k
        insn->hasAdSize = true;
556
7.20k
        setPrefixPresent(insn, byte);
557
7.20k
        insn->prefix3 = byte;
558
7.20k
        break;
559
584k
      default:    /* Not a prefix byte */
560
584k
        isPrefix = false;
561
584k
        break;
562
699k
    }
563
699k
  }
564
565
584k
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
566
567
584k
  if (byte == 0x62) {
568
40.9k
    uint8_t byte1, byte2;
569
570
40.9k
    if (consumeByte(insn, &byte1)) {
571
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
572
37
      return -1;
573
37
    }
574
575
40.9k
    if (lookAtByte(insn, &byte2)) {
576
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
577
41
      unconsumeByte(insn); /* unconsume byte1 */
578
41
      unconsumeByte(insn); /* unconsume byte  */
579
40.8k
    } else {
580
40.8k
      if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
581
36.0k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
582
36.0k
        insn->vectorExtensionType = TYPE_EVEX;
583
36.0k
      } else {
584
4.88k
        unconsumeByte(insn); /* unconsume byte1 */
585
4.88k
        unconsumeByte(insn); /* unconsume byte  */
586
4.88k
      }
587
40.8k
    }
588
589
40.9k
    if (insn->vectorExtensionType == TYPE_EVEX) {
590
36.0k
      insn->vectorExtensionPrefix[0] = byte;
591
36.0k
      insn->vectorExtensionPrefix[1] = byte1;
592
36.0k
      if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
593
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
594
0
        return -1;
595
0
      }
596
597
36.0k
      if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
598
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
599
15
        return -1;
600
15
      }
601
602
      /* We simulate the REX prefix for simplicity's sake */
603
35.9k
      if (insn->mode == MODE_64BIT) {
604
14.5k
        insn->rexPrefix = 0x40
605
14.5k
          | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
606
14.5k
          | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
607
14.5k
          | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
608
14.5k
          | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
609
14.5k
      }
610
611
      // dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
612
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
613
      //    insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
614
35.9k
    }
615
543k
  } else if (byte == 0xc4) {
616
4.43k
    uint8_t byte1;
617
618
4.43k
    if (lookAtByte(insn, &byte1)) {
619
      // dbgprintf(insn, "Couldn't read second byte of VEX");
620
10
      return -1;
621
10
    }
622
623
4.42k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
624
4.03k
      insn->vectorExtensionType = TYPE_VEX_3B;
625
394
    else
626
394
      unconsumeByte(insn);
627
628
4.42k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
629
4.03k
      insn->vectorExtensionPrefix[0] = byte;
630
4.03k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
631
4.03k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
632
633
      /* We simulate the REX prefix for simplicity's sake */
634
4.03k
      if (insn->mode == MODE_64BIT)
635
2.69k
        insn->rexPrefix = 0x40
636
2.69k
          | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
637
2.69k
          | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
638
2.69k
          | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
639
2.69k
          | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
640
641
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
642
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
643
      //    insn->vectorExtensionPrefix[2]);
644
4.03k
    }
645
538k
  } else if (byte == 0xc5) {
646
6.39k
    uint8_t byte1;
647
648
6.39k
    if (lookAtByte(insn, &byte1)) {
649
      // dbgprintf(insn, "Couldn't read second byte of VEX");
650
7
      return -1;
651
7
    }
652
653
6.38k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
654
5.18k
      insn->vectorExtensionType = TYPE_VEX_2B;
655
1.20k
    else
656
1.20k
      unconsumeByte(insn);
657
658
6.38k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
659
5.18k
      insn->vectorExtensionPrefix[0] = byte;
660
5.18k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
661
662
5.18k
      if (insn->mode == MODE_64BIT)
663
974
        insn->rexPrefix = 0x40
664
974
          | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
665
666
5.18k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
667
2.71k
        default:
668
2.71k
          break;
669
2.71k
        case VEX_PREFIX_66:
670
2.47k
          insn->hasOpSize = true;
671
2.47k
          break;
672
5.18k
      }
673
674
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
675
      //    insn->vectorExtensionPrefix[0],
676
      //    insn->vectorExtensionPrefix[1]);
677
5.18k
    }
678
532k
  } else if (byte == 0x8f) {
679
6.16k
    uint8_t byte1;
680
681
6.16k
    if (lookAtByte(insn, &byte1)) {
682
      // dbgprintf(insn, "Couldn't read second byte of XOP");
683
7
      return -1;
684
7
    }
685
686
6.15k
    if ((byte1 & 0x38) != 0x0) /* 0 in these 3 bits is a POP instruction. */
687
5.76k
      insn->vectorExtensionType = TYPE_XOP;
688
391
    else
689
391
      unconsumeByte(insn);
690
691
6.15k
    if (insn->vectorExtensionType == TYPE_XOP) {
692
5.76k
      insn->vectorExtensionPrefix[0] = byte;
693
5.76k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
694
5.76k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
695
696
      /* We simulate the REX prefix for simplicity's sake */
697
5.76k
      if (insn->mode == MODE_64BIT)
698
2.89k
        insn->rexPrefix = 0x40
699
2.89k
          | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
700
2.89k
          | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
701
2.89k
          | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
702
2.89k
          | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
703
704
5.76k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
705
5.76k
        default:
706
5.76k
          break;
707
5.76k
        case VEX_PREFIX_66:
708
2
          insn->hasOpSize = true;
709
2
          break;
710
5.76k
      }
711
712
      // dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
713
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
714
      //    insn->vectorExtensionPrefix[2]);
715
5.76k
    }
716
526k
  } else if (isREX(insn, byte)) {
717
33.9k
    if (lookAtByte(insn, &nextByte))
718
0
      return -1;
719
720
33.9k
    insn->rexPrefix = byte;
721
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
722
33.9k
  } else
723
492k
    unconsumeByte(insn);
724
725
584k
  if (insn->mode == MODE_16BIT) {
726
199k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
727
199k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
728
199k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
729
199k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
730
199k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
731
384k
  } else if (insn->mode == MODE_32BIT) {
732
160k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
733
160k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
734
160k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
735
160k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
736
160k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
737
224k
  } else if (insn->mode == MODE_64BIT) {
738
224k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
739
34.8k
      insn->registerSize       = 8;
740
34.8k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
741
34.8k
      insn->displacementSize   = 4;
742
34.8k
      insn->immediateSize      = 4;
743
34.8k
      insn->immSize      = 4;
744
189k
    } else {
745
189k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
746
189k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
747
189k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
748
189k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
749
189k
      insn->immSize      = (insn->hasOpSize ? 4 : 8);
750
189k
    }
751
224k
  }
752
753
584k
  return 0;
754
584k
}
755
756
static int readModRM(struct InternalInstruction* insn);
757
758
/*
759
 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
760
 *   extended or escape opcodes).
761
 *
762
 * @param insn  - The instruction whose opcode is to be read.
763
 * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
764
 */
765
static int readOpcode(struct InternalInstruction* insn)
766
584k
{
767
584k
  uint8_t current;
768
769
  // dbgprintf(insn, "readOpcode()");
770
771
584k
  insn->opcodeType = ONEBYTE;
772
773
584k
  if (insn->vectorExtensionType == TYPE_EVEX) {
774
35.9k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
775
2
      default:
776
        // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
777
        //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
778
2
        return -1;
779
10.5k
      case VEX_LOB_0F:
780
10.5k
        insn->opcodeType = TWOBYTE;
781
10.5k
        return consumeByte(insn, &insn->opcode);
782
10.1k
      case VEX_LOB_0F38:
783
10.1k
        insn->opcodeType = THREEBYTE_38;
784
10.1k
        return consumeByte(insn, &insn->opcode);
785
15.3k
      case VEX_LOB_0F3A:
786
15.3k
        insn->opcodeType = THREEBYTE_3A;
787
15.3k
        return consumeByte(insn, &insn->opcode);
788
35.9k
    }
789
548k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
790
4.03k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
791
16
      default:
792
        // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
793
        //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
794
16
        return -1;
795
1.11k
      case VEX_LOB_0F:
796
        //insn->twoByteEscape = 0x0f;
797
1.11k
        insn->opcodeType = TWOBYTE;
798
1.11k
        return consumeByte(insn, &insn->opcode);
799
1.81k
      case VEX_LOB_0F38:
800
        //insn->twoByteEscape = 0x0f;
801
1.81k
        insn->opcodeType = THREEBYTE_38;
802
1.81k
        return consumeByte(insn, &insn->opcode);
803
1.08k
      case VEX_LOB_0F3A:
804
        //insn->twoByteEscape = 0x0f;
805
1.08k
        insn->opcodeType = THREEBYTE_3A;
806
1.08k
        return consumeByte(insn, &insn->opcode);
807
4.03k
    }
808
544k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
809
    //insn->twoByteEscape = 0x0f;
810
5.18k
    insn->opcodeType = TWOBYTE;
811
5.18k
    return consumeByte(insn, &insn->opcode);
812
539k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
813
5.76k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
814
14
      default:
815
        // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
816
        //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
817
14
        return -1;
818
5.26k
      case XOP_MAP_SELECT_8:
819
5.26k
        insn->opcodeType = XOP8_MAP;
820
5.26k
        return consumeByte(insn, &insn->opcode);
821
421
      case XOP_MAP_SELECT_9:
822
421
        insn->opcodeType = XOP9_MAP;
823
421
        return consumeByte(insn, &insn->opcode);
824
65
      case XOP_MAP_SELECT_A:
825
65
        insn->opcodeType = XOPA_MAP;
826
65
        return consumeByte(insn, &insn->opcode);
827
5.76k
    }
828
5.76k
  }
829
830
533k
  if (consumeByte(insn, &current))
831
0
    return -1;
832
833
    // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
834
533k
    insn->firstByte = current;
835
836
533k
  if (current == 0x0f) {
837
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
838
29.0k
    insn->twoByteEscape = current;
839
840
29.0k
    if (consumeByte(insn, &current))
841
41
      return -1;
842
843
29.0k
    if (current == 0x38) {
844
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
845
733
      if (consumeByte(insn, &current))
846
1
        return -1;
847
848
732
      insn->opcodeType = THREEBYTE_38;
849
28.2k
    } else if (current == 0x3a) {
850
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
851
215
      if (consumeByte(insn, &current))
852
1
        return -1;
853
854
214
      insn->opcodeType = THREEBYTE_3A;
855
28.0k
    } else if (current == 0x0f) {
856
      // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
857
      // Consume operands before the opcode to comply with the 3DNow encoding
858
183
      if (readModRM(insn))
859
2
        return -1;
860
861
181
      if (consumeByte(insn, &current))
862
2
        return -1;
863
864
179
      insn->opcodeType = THREEDNOW_MAP;
865
27.8k
    } else {
866
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
867
27.8k
      insn->opcodeType = TWOBYTE;
868
27.8k
    }
869
504k
  } else if (insn->mandatoryPrefix)
870
    // The opcode with mandatory prefix must start with opcode escape.
871
    // If not it's legacy repeat prefix
872
5.19k
    insn->mandatoryPrefix = 0;
873
874
  /*
875
   * At this point we have consumed the full opcode.
876
   * Anything we consume from here on must be unconsumed.
877
   */
878
879
533k
  insn->opcode = current;
880
881
533k
  return 0;
882
533k
}
883
884
// Hacky for FEMMS
885
#define GET_INSTRINFO_ENUM
886
#ifndef CAPSTONE_X86_REDUCE
887
#include "X86GenInstrInfo.inc"
888
#else
889
#include "X86GenInstrInfo_reduce.inc"
890
#endif
891
892
/*
893
 * getIDWithAttrMask - Determines the ID of an instruction, consuming
894
 *   the ModR/M byte as appropriate for extended and escape opcodes,
895
 *   and using a supplied attribute mask.
896
 *
897
 * @param instructionID - A pointer whose target is filled in with the ID of the
898
 *                        instruction.
899
 * @param insn          - The instruction whose ID is to be determined.
900
 * @param attrMask      - The attribute mask to search.
901
 * @return              - 0 if the ModR/M could be read when needed or was not
902
 *                        needed; nonzero otherwise.
903
 */
904
static int getIDWithAttrMask(uint16_t *instructionID,
905
                             struct InternalInstruction* insn,
906
                             uint16_t attrMask)
907
1.95M
{
908
1.95M
  bool hasModRMExtension;
909
910
1.95M
  InstructionContext instructionClass = contextForAttrs(attrMask);
911
912
1.95M
  hasModRMExtension = modRMRequired(insn->opcodeType,
913
1.95M
      instructionClass,
914
1.95M
      insn->opcode);
915
916
1.95M
  if (hasModRMExtension) {
917
1.01M
    if (readModRM(insn))
918
2.37k
      return -1;
919
920
1.01M
    *instructionID = decode(insn->opcodeType,
921
1.01M
        instructionClass,
922
1.01M
        insn->opcode,
923
1.01M
        insn->modRM);
924
1.01M
  } else {
925
938k
    *instructionID = decode(insn->opcodeType,
926
938k
        instructionClass,
927
938k
        insn->opcode,
928
938k
        0);
929
938k
  }
930
931
1.95M
  return 0;
932
1.95M
}
933
934
/*
935
 * is16BitEquivalent - Determines whether two instruction names refer to
936
 * equivalent instructions but one is 16-bit whereas the other is not.
937
 *
938
 * @param orig  - The instruction ID that is not 16-bit
939
 * @param equiv - The instruction ID that is 16-bit
940
 */
941
static bool is16BitEquivalent(unsigned orig, unsigned equiv)
942
460k
{
943
460k
  size_t i;
944
460k
  uint16_t idx;
945
946
460k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
947
229k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) && x86_16_bit_eq_tbl[i].first == orig; i++) {
948
224k
      if (x86_16_bit_eq_tbl[i].second == equiv)
949
220k
        return true;
950
224k
    }
951
224k
  }
952
953
240k
  return false;
954
460k
}
955
956
/*
957
 * is64Bit - Determines whether this instruction is a 64-bit instruction.
958
 *
959
 * @param name - The instruction that is not 16-bit
960
 */
961
static bool is64Bit(uint16_t id)
962
24.0k
{
963
24.0k
  unsigned int i = find_insn(id);
964
24.0k
  if (i != -1) {
965
23.9k
    return insns[i].is64bit;
966
23.9k
  }
967
968
  // not found??
969
134
  return false;
970
24.0k
}
971
972
/*
973
 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
974
 *   appropriate for extended and escape opcodes.  Determines the attributes and
975
 *   context for the instruction before doing so.
976
 *
977
 * @param insn  - The instruction whose ID is to be determined.
978
 * @return      - 0 if the ModR/M could be read when needed or was not needed;
979
 *                nonzero otherwise.
980
 */
981
static int getID(struct InternalInstruction *insn)
982
584k
{
983
584k
  uint16_t attrMask;
984
584k
  uint16_t instructionID;
985
986
584k
  attrMask = ATTR_NONE;
987
988
584k
  if (insn->mode == MODE_64BIT)
989
224k
    attrMask |= ATTR_64BIT;
990
991
584k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
992
50.8k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
993
994
50.8k
    if (insn->vectorExtensionType == TYPE_EVEX) {
995
35.9k
      switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
996
30.5k
        case VEX_PREFIX_66:
997
30.5k
          attrMask |= ATTR_OPSIZE;
998
30.5k
          break;
999
656
        case VEX_PREFIX_F3:
1000
656
          attrMask |= ATTR_XS;
1001
656
          break;
1002
683
        case VEX_PREFIX_F2:
1003
683
          attrMask |= ATTR_XD;
1004
683
          break;
1005
35.9k
      }
1006
1007
35.9k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1008
5.20k
        attrMask |= ATTR_EVEXKZ;
1009
35.9k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1010
13.6k
        attrMask |= ATTR_EVEXB;
1011
35.9k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1012
22.7k
        attrMask |= ATTR_EVEXK;
1013
35.9k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1014
20.7k
        attrMask |= ATTR_EVEXL;
1015
35.9k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1016
14.8k
        attrMask |= ATTR_EVEXL2;
1017
35.9k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1018
4.00k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1019
3.08k
        case VEX_PREFIX_66:
1020
3.08k
          attrMask |= ATTR_OPSIZE;
1021
3.08k
          break;
1022
287
        case VEX_PREFIX_F3:
1023
287
          attrMask |= ATTR_XS;
1024
287
          break;
1025
312
        case VEX_PREFIX_F2:
1026
312
          attrMask |= ATTR_XD;
1027
312
          break;
1028
4.00k
      }
1029
1030
4.00k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1031
2.09k
        attrMask |= ATTR_VEXL;
1032
10.9k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1033
5.17k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1034
2.47k
        case VEX_PREFIX_66:
1035
2.47k
          attrMask |= ATTR_OPSIZE;
1036
2.47k
          break;
1037
447
        case VEX_PREFIX_F3:
1038
447
          attrMask |= ATTR_XS;
1039
447
          break;
1040
813
        case VEX_PREFIX_F2:
1041
813
          attrMask |= ATTR_XD;
1042
813
          break;
1043
5.17k
      }
1044
1045
5.17k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1046
3.15k
        attrMask |= ATTR_VEXL;
1047
5.73k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1048
5.73k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1049
2
        case VEX_PREFIX_66:
1050
2
          attrMask |= ATTR_OPSIZE;
1051
2
          break;
1052
4
        case VEX_PREFIX_F3:
1053
4
          attrMask |= ATTR_XS;
1054
4
          break;
1055
6
        case VEX_PREFIX_F2:
1056
6
          attrMask |= ATTR_XD;
1057
6
          break;
1058
5.73k
      }
1059
1060
5.73k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1061
406
        attrMask |= ATTR_VEXL;
1062
5.73k
    } else {
1063
0
      return -1;
1064
0
    }
1065
533k
  } else if (!insn->mandatoryPrefix) {
1066
    // If we don't have mandatory prefix we should use legacy prefixes here
1067
524k
    if (insn->hasOpSize && (insn->mode != MODE_16BIT))
1068
7.82k
      attrMask |= ATTR_OPSIZE;
1069
524k
    if (insn->hasAdSize)
1070
5.27k
      attrMask |= ATTR_ADSIZE;
1071
524k
    if (insn->opcodeType == ONEBYTE) {
1072
504k
      if (insn->repeatPrefix == 0xf3 && (insn->opcode == 0x90))
1073
        // Special support for PAUSE
1074
344
        attrMask |= ATTR_XS;
1075
504k
    } else {
1076
20.5k
      if (insn->repeatPrefix == 0xf2)
1077
1.28k
        attrMask |= ATTR_XD;
1078
19.2k
      else if (insn->repeatPrefix == 0xf3)
1079
166
        attrMask |= ATTR_XS;
1080
20.5k
    }
1081
524k
  } else {
1082
8.50k
    switch (insn->mandatoryPrefix) {
1083
2.98k
      case 0xf2:
1084
2.98k
        attrMask |= ATTR_XD;
1085
2.98k
        break;
1086
3.08k
      case 0xf3:
1087
3.08k
        attrMask |= ATTR_XS;
1088
3.08k
        break;
1089
2.43k
      case 0x66:
1090
2.43k
        if (insn->mode != MODE_16BIT)
1091
1.90k
          attrMask |= ATTR_OPSIZE;
1092
2.43k
        break;
1093
0
      case 0x67:
1094
0
        attrMask |= ATTR_ADSIZE;
1095
0
        break;
1096
8.50k
    }
1097
1098
8.50k
  }
1099
1100
584k
  if (insn->rexPrefix & 0x08) {
1101
34.8k
    attrMask |= ATTR_REXW;
1102
34.8k
    attrMask &= ~ATTR_ADSIZE;
1103
34.8k
  }
1104
1105
  /*
1106
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1107
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1108
   */
1109
584k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1110
176k
      insn->opcode == 0xE3)
1111
747
    attrMask ^= ATTR_ADSIZE;
1112
1113
  /*
1114
   * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
1115
   * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
1116
   */
1117
584k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1118
7.55k
    switch (insn->opcode) {
1119
95
      case 0xE8:
1120
147
      case 0xE9:
1121
        // Take care of psubsb and other mmx instructions.
1122
147
        if (insn->opcodeType == ONEBYTE) {
1123
96
          attrMask ^= ATTR_OPSIZE;
1124
96
          insn->immediateSize = 4;
1125
96
          insn->displacementSize = 4;
1126
96
        }
1127
147
        break;
1128
62
      case 0x82:
1129
563
      case 0x83:
1130
641
      case 0x84:
1131
893
      case 0x85:
1132
1.08k
      case 0x86:
1133
1.89k
      case 0x87:
1134
1.98k
      case 0x88:
1135
2.19k
      case 0x89:
1136
2.32k
      case 0x8A:
1137
2.46k
      case 0x8B:
1138
2.87k
      case 0x8C:
1139
2.88k
      case 0x8D:
1140
2.95k
      case 0x8E:
1141
3.00k
      case 0x8F:
1142
        // Take care of lea and three byte ops.
1143
3.00k
        if (insn->opcodeType == TWOBYTE) {
1144
73
          attrMask ^= ATTR_OPSIZE;
1145
73
          insn->immediateSize = 4;
1146
73
          insn->displacementSize = 4;
1147
73
        }
1148
3.00k
        break;
1149
7.55k
    }
1150
7.55k
  }
1151
1152
  /* The following clauses compensate for limitations of the tables. */
1153
584k
  if (insn->mode != MODE_64BIT &&
1154
359k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1155
29.7k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1156
16
      return -1;
1157
16
    }
1158
1159
    /*
1160
     * The tables can't distinquish between cases where the W-bit is used to
1161
     * select register size and cases where its a required part of the opcode.
1162
     */
1163
29.7k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1164
21.3k
          wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1165
19.5k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1166
1.32k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1167
18.7k
        (insn->vectorExtensionType == TYPE_XOP &&
1168
11.0k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1169
11.0k
      uint16_t instructionIDWithREXW;
1170
1171
11.0k
      if (getIDWithAttrMask(&instructionIDWithREXW,
1172
11.0k
            insn, attrMask | ATTR_REXW)) {
1173
2
        insn->instructionID = instructionID;
1174
2
        insn->spec = specifierForUID(instructionID);
1175
2
        return 0;
1176
2
      }
1177
1178
      // If not a 64-bit instruction. Switch the opcode.
1179
11.0k
      if (!is64Bit(instructionIDWithREXW)) {
1180
10.9k
        insn->instructionID = instructionIDWithREXW;
1181
10.9k
        insn->spec = specifierForUID(instructionIDWithREXW);
1182
1183
10.9k
        return 0;
1184
10.9k
      }
1185
11.0k
    }
1186
29.7k
  }
1187
1188
  /*
1189
   * Absolute moves, umonitor, and movdir64b need special handling.
1190
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1191
   *  inverted w.r.t.
1192
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1193
   *  any position.
1194
   */
1195
573k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1196
567k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1197
566k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1198
    /* Make sure we observed the prefixes in any position. */
1199
6.65k
    if (insn->hasAdSize)
1200
191
      attrMask |= ATTR_ADSIZE;
1201
1202
6.65k
    if (insn->hasOpSize)
1203
161
      attrMask |= ATTR_OPSIZE;
1204
1205
    /* In 16-bit, invert the attributes. */
1206
6.65k
    if (insn->mode == MODE_16BIT) {
1207
2.89k
      attrMask ^= ATTR_ADSIZE;
1208
1209
      /* The OpSize attribute is only valid with the absolute moves. */
1210
2.89k
      if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0))
1211
2.21k
        attrMask ^= ATTR_OPSIZE;
1212
2.89k
    }
1213
1214
6.65k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1215
1
      return -1;
1216
1
    }
1217
1218
6.64k
    insn->instructionID = instructionID;
1219
6.64k
    insn->spec = specifierForUID(instructionID);
1220
1221
6.64k
    return 0;
1222
6.65k
  }
1223
566k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1224
1.01k
    return -1;
1225
1.01k
  }
1226
1227
565k
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1228
203k
      !(attrMask & ATTR_OPSIZE)) {
1229
    /*
1230
     * The instruction tables make no distinction between instructions that
1231
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1232
     * particular spot (i.e., many MMX operations).  In general we're
1233
     * conservative, but in the specific case where OpSize is present but not
1234
     * in the right place we check if there's a 16-bit operation.
1235
     */
1236
187k
    const struct InstructionSpecifier *spec;
1237
187k
    uint16_t instructionIDWithOpsize;
1238
1239
187k
    spec = specifierForUID(instructionID);
1240
1241
187k
    if (getIDWithAttrMask(&instructionIDWithOpsize,
1242
187k
          insn,
1243
187k
          attrMask | ATTR_OPSIZE)) {
1244
      /*
1245
       * ModRM required with OpSize but not present; give up and return version
1246
       * without OpSize set
1247
       */
1248
4
      insn->instructionID = instructionID;
1249
4
      insn->spec = spec;
1250
1251
4
      return 0;
1252
4
    }
1253
1254
187k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1255
87.6k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1256
86.8k
      insn->instructionID = instructionIDWithOpsize;
1257
86.8k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1258
100k
    } else {
1259
100k
      insn->instructionID = instructionID;
1260
100k
      insn->spec = spec;
1261
100k
    }
1262
1263
187k
    return 0;
1264
187k
  }
1265
1266
378k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1267
927
      insn->rexPrefix & 0x01) {
1268
    /*
1269
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1270
     * it should decode as XCHG %r8, %eax.
1271
     */
1272
138
    const struct InstructionSpecifier *spec;
1273
138
    uint16_t instructionIDWithNewOpcode;
1274
138
    const struct InstructionSpecifier *specWithNewOpcode;
1275
1276
138
    spec = specifierForUID(instructionID);
1277
1278
    /* Borrow opcode from one of the other XCHGar opcodes */
1279
138
    insn->opcode = 0x91;
1280
1281
138
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn, attrMask)) {
1282
0
      insn->opcode = 0x90;
1283
1284
0
      insn->instructionID = instructionID;
1285
0
      insn->spec = spec;
1286
1287
0
      return 0;
1288
0
    }
1289
1290
138
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1291
1292
    /* Change back */
1293
138
    insn->opcode = 0x90;
1294
1295
138
    insn->instructionID = instructionIDWithNewOpcode;
1296
138
    insn->spec = specWithNewOpcode;
1297
1298
138
    return 0;
1299
138
  }
1300
1301
378k
  insn->instructionID = instructionID;
1302
378k
  insn->spec = specifierForUID(insn->instructionID);
1303
1304
378k
  return 0;
1305
378k
}
1306
1307
/*
1308
 * readSIB - Consumes the SIB byte to determine addressing information for an
1309
 *   instruction.
1310
 *
1311
 * @param insn  - The instruction whose SIB byte is to be read.
1312
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1313
 */
1314
static int readSIB(struct InternalInstruction* insn)
1315
34.9k
{
1316
34.9k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1317
34.9k
  uint8_t index, base;
1318
1319
  // dbgprintf(insn, "readSIB()");
1320
1321
34.9k
  if (insn->consumedSIB)
1322
0
    return 0;
1323
1324
34.9k
  insn->consumedSIB = true;
1325
1326
34.9k
  switch (insn->addressSize) {
1327
0
    case 2:
1328
      // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1329
0
      return -1;
1330
15.8k
    case 4:
1331
15.8k
      insn->sibIndexBase = SIB_INDEX_EAX;
1332
15.8k
      sibBaseBase = SIB_BASE_EAX;
1333
15.8k
      break;
1334
19.1k
    case 8:
1335
19.1k
      insn->sibIndexBase = SIB_INDEX_RAX;
1336
19.1k
      sibBaseBase = SIB_BASE_RAX;
1337
19.1k
      break;
1338
34.9k
  }
1339
1340
34.9k
  if (consumeByte(insn, &insn->sib))
1341
66
    return -1;
1342
1343
34.8k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1344
1345
34.8k
  if (index == 0x4) {
1346
6.64k
    insn->sibIndex = SIB_INDEX_NONE;
1347
28.2k
  } else {
1348
28.2k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1349
28.2k
  }
1350
1351
34.8k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1352
1353
34.8k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1354
1355
34.8k
  switch (base) {
1356
3.42k
    case 0x5:
1357
4.25k
    case 0xd:
1358
4.25k
      switch (modFromModRM(insn->modRM)) {
1359
2.41k
        case 0x0:
1360
2.41k
          insn->eaDisplacement = EA_DISP_32;
1361
2.41k
          insn->sibBase = SIB_BASE_NONE;
1362
2.41k
          break;
1363
1.28k
        case 0x1:
1364
1.28k
          insn->eaDisplacement = EA_DISP_8;
1365
1.28k
          insn->sibBase = (SIBBase)(sibBaseBase + base);
1366
1.28k
          break;
1367
548
        case 0x2:
1368
548
          insn->eaDisplacement = EA_DISP_32;
1369
548
          insn->sibBase = (SIBBase)(sibBaseBase + base);
1370
548
          break;
1371
0
        case 0x3:
1372
          // debug("Cannot have Mod = 0b11 and a SIB byte");
1373
0
          return -1;
1374
4.25k
      }
1375
4.25k
      break;
1376
30.6k
    default:
1377
30.6k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1378
30.6k
      break;
1379
34.8k
  }
1380
1381
34.8k
  return 0;
1382
34.8k
}
1383
1384
/*
1385
 * readDisplacement - Consumes the displacement of an instruction.
1386
 *
1387
 * @param insn  - The instruction whose displacement is to be read.
1388
 * @return      - 0 if the displacement byte was successfully read; nonzero
1389
 *                otherwise.
1390
 */
1391
static int readDisplacement(struct InternalInstruction* insn)
1392
263k
{
1393
263k
  int8_t d8;
1394
263k
  int16_t d16;
1395
263k
  int32_t d32;
1396
1397
  // dbgprintf(insn, "readDisplacement()");
1398
1399
263k
  if (insn->consumedDisplacement)
1400
0
    return 0;
1401
1402
263k
  insn->consumedDisplacement = true;
1403
263k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1404
1405
263k
  switch (insn->eaDisplacement) {
1406
69.1k
    case EA_DISP_NONE:
1407
69.1k
      insn->consumedDisplacement = false;
1408
69.1k
      break;
1409
120k
    case EA_DISP_8:
1410
120k
      if (consumeInt8(insn, &d8))
1411
228
        return -1;
1412
119k
      insn->displacement = d8;
1413
119k
      break;
1414
30.1k
    case EA_DISP_16:
1415
30.1k
      if (consumeInt16(insn, &d16))
1416
145
        return -1;
1417
29.9k
      insn->displacement = d16;
1418
29.9k
      break;
1419
44.0k
    case EA_DISP_32:
1420
44.0k
      if (consumeInt32(insn, &d32))
1421
422
        return -1;
1422
43.5k
      insn->displacement = d32;
1423
43.5k
      break;
1424
263k
  }
1425
1426
1427
262k
  return 0;
1428
263k
}
1429
1430
/*
1431
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1432
 *   displacement) for an instruction and interprets it.
1433
 *
1434
 * @param insn  - The instruction whose addressing information is to be read.
1435
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1436
 */
1437
static int readModRM(struct InternalInstruction* insn)
1438
2.30M
{
1439
2.30M
  uint8_t mod, rm, reg, evexrm;
1440
1441
  // dbgprintf(insn, "readModRM()");
1442
1443
2.30M
  if (insn->consumedModRM)
1444
1.56M
    return 0;
1445
1446
748k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1447
1448
748k
  if (consumeByte(insn, &insn->modRM))
1449
1.52k
    return -1;
1450
1451
747k
  insn->consumedModRM = true;
1452
1453
  // save original ModRM for later reference
1454
747k
  insn->orgModRM = insn->modRM;
1455
1456
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1457
747k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1458
59.2k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23 ))
1459
1.54k
    insn->modRM |= 0xC0;
1460
1461
747k
  mod = modFromModRM(insn->modRM);
1462
747k
  rm  = rmFromModRM(insn->modRM);
1463
747k
  reg = regFromModRM(insn->modRM);
1464
1465
  /*
1466
   * This goes by insn->registerSize to pick the correct register, which messes
1467
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1468
   * fixupReg().
1469
   */
1470
747k
  switch (insn->registerSize) {
1471
256k
    case 2:
1472
256k
      insn->regBase = MODRM_REG_AX;
1473
256k
      insn->eaRegBase = EA_REG_AX;
1474
256k
      break;
1475
434k
    case 4:
1476
434k
      insn->regBase = MODRM_REG_EAX;
1477
434k
      insn->eaRegBase = EA_REG_EAX;
1478
434k
      break;
1479
56.3k
    case 8:
1480
56.3k
      insn->regBase = MODRM_REG_RAX;
1481
56.3k
      insn->eaRegBase = EA_REG_RAX;
1482
56.3k
      break;
1483
747k
  }
1484
1485
747k
  reg |= rFromREX(insn->rexPrefix) << 3;
1486
747k
  rm  |= bFromREX(insn->rexPrefix) << 3;
1487
1488
747k
  evexrm = 0;
1489
747k
  if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT) {
1490
29.9k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1491
29.9k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1492
29.9k
  }
1493
1494
747k
  insn->reg = (Reg)(insn->regBase + reg);
1495
1496
747k
  switch (insn->addressSize) {
1497
239k
    case 2: {
1498
239k
      EABase eaBaseBase = EA_BASE_BX_SI;
1499
1500
239k
      switch (mod) {
1501
130k
        case 0x0:
1502
130k
          if (rm == 0x6) {
1503
7.25k
            insn->eaBase = EA_BASE_NONE;
1504
7.25k
            insn->eaDisplacement = EA_DISP_16;
1505
7.25k
            if (readDisplacement(insn))
1506
27
              return -1;
1507
123k
          } else {
1508
123k
            insn->eaBase = (EABase)(eaBaseBase + rm);
1509
123k
            insn->eaDisplacement = EA_DISP_NONE;
1510
123k
          }
1511
130k
          break;
1512
130k
        case 0x1:
1513
34.3k
          insn->eaBase = (EABase)(eaBaseBase + rm);
1514
34.3k
          insn->eaDisplacement = EA_DISP_8;
1515
34.3k
          insn->displacementSize = 1;
1516
34.3k
          if (readDisplacement(insn))
1517
63
            return -1;
1518
34.2k
          break;
1519
34.2k
        case 0x2:
1520
22.8k
          insn->eaBase = (EABase)(eaBaseBase + rm);
1521
22.8k
          insn->eaDisplacement = EA_DISP_16;
1522
22.8k
          if (readDisplacement(insn))
1523
118
            return -1;
1524
22.7k
          break;
1525
52.0k
        case 0x3:
1526
52.0k
          insn->eaBase = (EABase)(insn->eaRegBase + rm);
1527
52.0k
          if (readDisplacement(insn))
1528
0
            return -1;
1529
52.0k
          break;
1530
239k
      }
1531
239k
      break;
1532
239k
    }
1533
1534
239k
    case 4:
1535
507k
    case 8: {
1536
507k
      EABase eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1537
1538
507k
      switch (mod) {
1539
0
        default: break;
1540
252k
        case 0x0:
1541
252k
          insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1542
          // In determining whether RIP-relative mode is used (rm=5),
1543
          // or whether a SIB byte is present (rm=4),
1544
          // the extension bits (REX.b and EVEX.x) are ignored.
1545
252k
          switch (rm & 7) {
1546
19.6k
            case 0x4: // SIB byte is present
1547
19.6k
              insn->eaBase = (insn->addressSize == 4 ?
1548
10.4k
                  EA_BASE_sib : EA_BASE_sib64);
1549
19.6k
              if (readSIB(insn) || readDisplacement(insn))
1550
42
                return -1;
1551
19.5k
              break;
1552
19.5k
            case 0x5: // RIP-relative
1553
6.08k
              insn->eaBase = EA_BASE_NONE;
1554
6.08k
              insn->eaDisplacement = EA_DISP_32;
1555
6.08k
              if (readDisplacement(insn))
1556
64
                return -1;
1557
6.02k
              break;
1558
226k
            default:
1559
226k
              insn->eaBase = (EABase)(eaBaseBase + rm);
1560
226k
              break;
1561
252k
          }
1562
251k
          break;
1563
251k
        case 0x1:
1564
85.8k
          insn->displacementSize = 1;
1565
          /* FALLTHROUGH */
1566
121k
        case 0x2:
1567
121k
          insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1568
121k
          switch (rm & 7) {
1569
15.3k
            case 0x4: // SIB byte is present
1570
15.3k
              insn->eaBase = EA_BASE_sib;
1571
15.3k
              if (readSIB(insn) || readDisplacement(insn))
1572
88
                return -1;
1573
15.2k
              break;
1574
106k
            default:
1575
106k
              insn->eaBase = (EABase)(eaBaseBase + rm);
1576
106k
              if (readDisplacement(insn))
1577
459
                return -1;
1578
105k
              break;
1579
121k
          }
1580
120k
          break;
1581
134k
        case 0x3:
1582
134k
          insn->eaDisplacement = EA_DISP_NONE;
1583
134k
          insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1584
134k
          break;
1585
507k
      }
1586
1587
506k
      break;
1588
507k
    }
1589
747k
  } /* switch (insn->addressSize) */
1590
1591
746k
  return 0;
1592
747k
}
1593
1594
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask)      \
1595
  static uint16_t name(struct InternalInstruction *insn,  \
1596
                       OperandType type,                  \
1597
                       uint8_t index,                     \
1598
811k
                       uint8_t *valid) {                  \
1599
811k
    *valid = 1;                                           \
1600
811k
    switch (type) {                                       \
1601
0
    default:                                              \
1602
0
      *valid = 0;                                         \
1603
0
      return 0;                                           \
1604
201k
    case TYPE_Rv:                                         \
1605
201k
      return base + index;                                \
1606
312k
    case TYPE_R8:                                         \
1607
312k
      index &= mask;                                      \
1608
312k
      if (index > 0xf)                                    \
1609
312k
        *valid = 0;                                       \
1610
312k
      if (insn->rexPrefix &&                              \
1611
312k
         index >= 4 && index <= 7) {                      \
1612
2.97k
        return prefix##_SPL + (index - 4);                \
1613
309k
      } else {                                            \
1614
309k
        return prefix##_AL + index;                       \
1615
309k
      }                                                   \
1616
312k
    case TYPE_R16:                                        \
1617
6.88k
      index &= mask;                                      \
1618
6.88k
      if (index > 0xf)                                    \
1619
6.88k
        *valid = 0;                                       \
1620
6.88k
      return prefix##_AX + index;                         \
1621
312k
    case TYPE_R32:                                        \
1622
5.58k
      index &= mask;                                      \
1623
5.58k
      if (index > 0xf)                                    \
1624
5.58k
        *valid = 0;                                       \
1625
5.58k
      return prefix##_EAX + index;                        \
1626
312k
    case TYPE_R64:                                        \
1627
20.8k
      index &= mask;                                      \
1628
20.8k
      if (index > 0xf)                                    \
1629
20.8k
        *valid = 0;                                       \
1630
20.8k
      return prefix##_RAX + index;                        \
1631
312k
    case TYPE_ZMM:                                        \
1632
49.3k
      return prefix##_ZMM0 + index;                       \
1633
312k
    case TYPE_YMM:                                        \
1634
48.3k
      return prefix##_YMM0 + index;                       \
1635
312k
    case TYPE_XMM:                                        \
1636
108k
      return prefix##_XMM0 + index;                       \
1637
312k
    case TYPE_VK:                                         \
1638
36.3k
      index &= 0xf;                                       \
1639
36.3k
      if (index > 7)                                      \
1640
36.3k
        *valid = 0;                                       \
1641
36.3k
      return prefix##_K0 + index;                         \
1642
312k
    case TYPE_MM64:                                       \
1643
8.47k
      return prefix##_MM0 + (index & 0x7);                \
1644
312k
    case TYPE_SEGMENTREG:                                 \
1645
2.97k
      if ((index & 7) > 5)                                \
1646
2.97k
        *valid = 0;                                       \
1647
2.97k
      return prefix##_ES + (index & 7);                   \
1648
312k
    case TYPE_DEBUGREG:                                   \
1649
1.03k
      return prefix##_DR0 + index;                        \
1650
312k
    case TYPE_CONTROLREG:                                 \
1651
510
      return prefix##_CR0 + index;                        \
1652
312k
    case TYPE_BNDR:                                       \
1653
8.23k
      if (index > 3)                                      \
1654
8.23k
        *valid = 0;                                       \
1655
8.23k
      return prefix##_BND0 + index;                       \
1656
312k
    case TYPE_MVSIBX:                                     \
1657
0
      return prefix##_XMM0 + index;                       \
1658
312k
    case TYPE_MVSIBY:                                     \
1659
0
      return prefix##_YMM0 + index;                       \
1660
312k
    case TYPE_MVSIBZ:                                     \
1661
0
      return prefix##_ZMM0 + index;                       \
1662
811k
    }                                                     \
1663
811k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1598
637k
                       uint8_t *valid) {                  \
1599
637k
    *valid = 1;                                           \
1600
637k
    switch (type) {                                       \
1601
0
    default:                                              \
1602
0
      *valid = 0;                                         \
1603
0
      return 0;                                           \
1604
147k
    case TYPE_Rv:                                         \
1605
147k
      return base + index;                                \
1606
254k
    case TYPE_R8:                                         \
1607
254k
      index &= mask;                                      \
1608
254k
      if (index > 0xf)                                    \
1609
254k
        *valid = 0;                                       \
1610
254k
      if (insn->rexPrefix &&                              \
1611
254k
         index >= 4 && index <= 7) {                      \
1612
2.04k
        return prefix##_SPL + (index - 4);                \
1613
252k
      } else {                                            \
1614
252k
        return prefix##_AL + index;                       \
1615
252k
      }                                                   \
1616
254k
    case TYPE_R16:                                        \
1617
5.33k
      index &= mask;                                      \
1618
5.33k
      if (index > 0xf)                                    \
1619
5.33k
        *valid = 0;                                       \
1620
5.33k
      return prefix##_AX + index;                         \
1621
254k
    case TYPE_R32:                                        \
1622
3.92k
      index &= mask;                                      \
1623
3.92k
      if (index > 0xf)                                    \
1624
3.92k
        *valid = 0;                                       \
1625
3.92k
      return prefix##_EAX + index;                        \
1626
254k
    case TYPE_R64:                                        \
1627
11.2k
      index &= mask;                                      \
1628
11.2k
      if (index > 0xf)                                    \
1629
11.2k
        *valid = 0;                                       \
1630
11.2k
      return prefix##_RAX + index;                        \
1631
254k
    case TYPE_ZMM:                                        \
1632
38.8k
      return prefix##_ZMM0 + index;                       \
1633
254k
    case TYPE_YMM:                                        \
1634
37.8k
      return prefix##_YMM0 + index;                       \
1635
254k
    case TYPE_XMM:                                        \
1636
86.5k
      return prefix##_XMM0 + index;                       \
1637
254k
    case TYPE_VK:                                         \
1638
33.6k
      index &= 0xf;                                       \
1639
33.6k
      if (index > 7)                                      \
1640
33.6k
        *valid = 0;                                       \
1641
33.6k
      return prefix##_K0 + index;                         \
1642
254k
    case TYPE_MM64:                                       \
1643
5.34k
      return prefix##_MM0 + (index & 0x7);                \
1644
254k
    case TYPE_SEGMENTREG:                                 \
1645
2.97k
      if ((index & 7) > 5)                                \
1646
2.97k
        *valid = 0;                                       \
1647
2.97k
      return prefix##_ES + (index & 7);                   \
1648
254k
    case TYPE_DEBUGREG:                                   \
1649
1.03k
      return prefix##_DR0 + index;                        \
1650
254k
    case TYPE_CONTROLREG:                                 \
1651
510
      return prefix##_CR0 + index;                        \
1652
254k
    case TYPE_BNDR:                                       \
1653
7.65k
      if (index > 3)                                      \
1654
7.65k
        *valid = 0;                                       \
1655
7.65k
      return prefix##_BND0 + index;                       \
1656
254k
    case TYPE_MVSIBX:                                     \
1657
0
      return prefix##_XMM0 + index;                       \
1658
254k
    case TYPE_MVSIBY:                                     \
1659
0
      return prefix##_YMM0 + index;                       \
1660
254k
    case TYPE_MVSIBZ:                                     \
1661
0
      return prefix##_ZMM0 + index;                       \
1662
637k
    }                                                     \
1663
637k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1598
174k
                       uint8_t *valid) {                  \
1599
174k
    *valid = 1;                                           \
1600
174k
    switch (type) {                                       \
1601
0
    default:                                              \
1602
0
      *valid = 0;                                         \
1603
0
      return 0;                                           \
1604
54.0k
    case TYPE_Rv:                                         \
1605
54.0k
      return base + index;                                \
1606
57.9k
    case TYPE_R8:                                         \
1607
57.9k
      index &= mask;                                      \
1608
57.9k
      if (index > 0xf)                                    \
1609
57.9k
        *valid = 0;                                       \
1610
57.9k
      if (insn->rexPrefix &&                              \
1611
57.9k
         index >= 4 && index <= 7) {                      \
1612
931
        return prefix##_SPL + (index - 4);                \
1613
57.0k
      } else {                                            \
1614
57.0k
        return prefix##_AL + index;                       \
1615
57.0k
      }                                                   \
1616
57.9k
    case TYPE_R16:                                        \
1617
1.55k
      index &= mask;                                      \
1618
1.55k
      if (index > 0xf)                                    \
1619
1.55k
        *valid = 0;                                       \
1620
1.55k
      return prefix##_AX + index;                         \
1621
57.9k
    case TYPE_R32:                                        \
1622
1.65k
      index &= mask;                                      \
1623
1.65k
      if (index > 0xf)                                    \
1624
1.65k
        *valid = 0;                                       \
1625
1.65k
      return prefix##_EAX + index;                        \
1626
57.9k
    case TYPE_R64:                                        \
1627
9.63k
      index &= mask;                                      \
1628
9.63k
      if (index > 0xf)                                    \
1629
9.63k
        *valid = 0;                                       \
1630
9.63k
      return prefix##_RAX + index;                        \
1631
57.9k
    case TYPE_ZMM:                                        \
1632
10.5k
      return prefix##_ZMM0 + index;                       \
1633
57.9k
    case TYPE_YMM:                                        \
1634
10.5k
      return prefix##_YMM0 + index;                       \
1635
57.9k
    case TYPE_XMM:                                        \
1636
22.3k
      return prefix##_XMM0 + index;                       \
1637
57.9k
    case TYPE_VK:                                         \
1638
2.65k
      index &= 0xf;                                       \
1639
2.65k
      if (index > 7)                                      \
1640
2.65k
        *valid = 0;                                       \
1641
2.65k
      return prefix##_K0 + index;                         \
1642
57.9k
    case TYPE_MM64:                                       \
1643
3.13k
      return prefix##_MM0 + (index & 0x7);                \
1644
57.9k
    case TYPE_SEGMENTREG:                                 \
1645
0
      if ((index & 7) > 5)                                \
1646
0
        *valid = 0;                                       \
1647
0
      return prefix##_ES + (index & 7);                   \
1648
57.9k
    case TYPE_DEBUGREG:                                   \
1649
0
      return prefix##_DR0 + index;                        \
1650
57.9k
    case TYPE_CONTROLREG:                                 \
1651
0
      return prefix##_CR0 + index;                        \
1652
57.9k
    case TYPE_BNDR:                                       \
1653
579
      if (index > 3)                                      \
1654
579
        *valid = 0;                                       \
1655
579
      return prefix##_BND0 + index;                       \
1656
57.9k
    case TYPE_MVSIBX:                                     \
1657
0
      return prefix##_XMM0 + index;                       \
1658
57.9k
    case TYPE_MVSIBY:                                     \
1659
0
      return prefix##_YMM0 + index;                       \
1660
57.9k
    case TYPE_MVSIBZ:                                     \
1661
0
      return prefix##_ZMM0 + index;                       \
1662
174k
    }                                                     \
1663
174k
  }
1664
1665
/*
1666
 * fixup*Value - Consults an operand type to determine the meaning of the
1667
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1668
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1669
 *   misinterpret it as.
1670
 *
1671
 * @param insn  - The instruction containing the operand.
1672
 * @param type  - The operand type.
1673
 * @param index - The existing value of the field as reported by readModRM().
1674
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1675
 *                field is valid for the register class; 0 if not.
1676
 * @return      - The proper value.
1677
 */
1678
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1679
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1680
1681
/*
1682
 * fixupReg - Consults an operand specifier to determine which of the
1683
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1684
 *
1685
 * @param insn  - See fixup*Value().
1686
 * @param op    - The operand specifier.
1687
 * @return      - 0 if fixup was successful; -1 if the register returned was
1688
 *                invalid for its class.
1689
 */
1690
static int fixupReg(struct InternalInstruction *insn,
1691
                    const struct OperandSpecifier *op)
1692
1.36M
{
1693
1.36M
  uint8_t valid;
1694
1695
1.36M
  switch ((OperandEncoding)op->encoding) {
1696
0
    default:
1697
      // debug("Expected a REG or R/M encoding in fixupReg");
1698
0
      return -1;
1699
76.9k
    case ENCODING_VVVV:
1700
76.9k
      insn->vvvv = (Reg)fixupRegValue(insn,
1701
76.9k
          (OperandType)op->type,
1702
76.9k
          insn->vvvv,
1703
76.9k
          &valid);
1704
76.9k
      if (!valid)
1705
1
        return -1;
1706
76.9k
      break;
1707
560k
    case ENCODING_REG:
1708
560k
      insn->reg = (Reg)fixupRegValue(insn,
1709
560k
          (OperandType)op->type,
1710
560k
          insn->reg - insn->regBase,
1711
560k
          &valid);
1712
560k
      if (!valid)
1713
43
        return -1;
1714
560k
      break;
1715
4.85M
    CASE_ENCODING_RM:
1716
4.85M
      if (insn->eaBase >= insn->eaRegBase) {
1717
174k
        insn->eaBase = (EABase)fixupRMValue(insn,
1718
174k
            (OperandType)op->type,
1719
174k
            insn->eaBase - insn->eaRegBase,
1720
174k
            &valid);
1721
174k
        if (!valid)
1722
4
          return -1;
1723
174k
      }
1724
727k
      break;
1725
1.36M
  }
1726
1727
1.36M
  return 0;
1728
1.36M
}
1729
1730
/*
1731
 * readOpcodeRegister - Reads an operand from the opcode field of an
1732
 *   instruction and interprets it appropriately given the operand width.
1733
 *   Handles AddRegFrm instructions.
1734
 *
1735
 * @param insn  - the instruction whose opcode field is to be read.
1736
 * @param size  - The width (in bytes) of the register being specified.
1737
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1738
 *                RAX.
1739
 * @return      - 0 on success; nonzero otherwise.
1740
 */
1741
static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size)
1742
167k
{
1743
167k
  if (size == 0)
1744
122k
    size = insn->registerSize;
1745
1746
167k
  switch (size) {
1747
24.7k
    case 1:
1748
24.7k
      insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
1749
24.7k
            | (insn->opcode & 7)));
1750
24.7k
      if (insn->rexPrefix &&
1751
1.13k
          insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1752
751
          insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1753
185
        insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1754
185
            + (insn->opcodeRegister - MODRM_REG_AL - 4));
1755
185
      }
1756
1757
24.7k
      break;
1758
57.8k
    case 2:
1759
57.8k
      insn->opcodeRegister = (Reg)(MODRM_REG_AX
1760
57.8k
          + ((bFromREX(insn->rexPrefix) << 3)
1761
57.8k
            | (insn->opcode & 7)));
1762
57.8k
      break;
1763
64.0k
    case 4:
1764
64.0k
      insn->opcodeRegister = (Reg)(MODRM_REG_EAX
1765
64.0k
          + ((bFromREX(insn->rexPrefix) << 3)
1766
64.0k
            | (insn->opcode & 7)));
1767
64.0k
      break;
1768
20.8k
    case 8:
1769
20.8k
      insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1770
20.8k
          + ((bFromREX(insn->rexPrefix) << 3)
1771
20.8k
            | (insn->opcode & 7)));
1772
20.8k
      break;
1773
167k
  }
1774
1775
167k
  return 0;
1776
167k
}
1777
1778
/*
1779
 * readImmediate - Consumes an immediate operand from an instruction, given the
1780
 *   desired operand size.
1781
 *
1782
 * @param insn  - The instruction whose operand is to be read.
1783
 * @param size  - The width (in bytes) of the operand.
1784
 * @return      - 0 if the immediate was successfully consumed; nonzero
1785
 *                otherwise.
1786
 */
1787
static int readImmediate(struct InternalInstruction* insn, uint8_t size)
1788
396k
{
1789
396k
  uint8_t imm8;
1790
396k
  uint16_t imm16;
1791
396k
  uint32_t imm32;
1792
396k
  uint64_t imm64;
1793
1794
396k
  if (insn->numImmediatesConsumed == 2) {
1795
    // debug("Already consumed two immediates");
1796
0
    return -1;
1797
0
  }
1798
1799
396k
  if (size == 0)
1800
0
    size = insn->immediateSize;
1801
396k
  else
1802
396k
    insn->immediateSize = size;
1803
1804
396k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1805
1806
396k
  switch (size) {
1807
288k
    case 1:
1808
288k
      if (consumeByte(insn, &imm8))
1809
638
        return -1;
1810
1811
287k
      insn->immediates[insn->numImmediatesConsumed] = imm8;
1812
287k
      break;
1813
58.7k
    case 2:
1814
58.7k
      if (consumeUInt16(insn, &imm16))
1815
230
        return -1;
1816
1817
58.4k
      insn->immediates[insn->numImmediatesConsumed] = imm16;
1818
58.4k
      break;
1819
43.9k
    case 4:
1820
43.9k
      if (consumeUInt32(insn, &imm32))
1821
513
        return -1;
1822
1823
43.4k
      insn->immediates[insn->numImmediatesConsumed] = imm32;
1824
43.4k
      break;
1825
5.18k
    case 8:
1826
5.18k
      if (consumeUInt64(insn, &imm64))
1827
97
        return -1;
1828
5.08k
      insn->immediates[insn->numImmediatesConsumed] = imm64;
1829
5.08k
      break;
1830
396k
  }
1831
1832
394k
  insn->numImmediatesConsumed++;
1833
1834
394k
  return 0;
1835
396k
}
1836
1837
/*
1838
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
1839
 *
1840
 * @param insn  - The instruction whose operand is to be read.
1841
 * @return      - 0 if the vvvv was successfully consumed; nonzero
1842
 *                otherwise.
1843
 */
1844
static int readVVVV(struct InternalInstruction* insn)
1845
1.42M
{
1846
1.42M
  int vvvv;
1847
1848
1.42M
  if (insn->vectorExtensionType == TYPE_EVEX)
1849
69.4k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1850
69.4k
        vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
1851
1.35M
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
1852
7.05k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
1853
1.34M
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
1854
14.1k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
1855
1.33M
  else if (insn->vectorExtensionType == TYPE_XOP)
1856
11.1k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
1857
1.32M
  else
1858
1.32M
    return -1;
1859
1860
101k
  if (insn->mode != MODE_64BIT)
1861
62.8k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1862
1863
101k
  insn->vvvv = (Reg)vvvv;
1864
1865
101k
  return 0;
1866
1.42M
}
1867
1868
/*
1869
 * readMaskRegister - Reads an mask register from the opcode field of an
1870
 *   instruction.
1871
 *
1872
 * @param insn    - The instruction whose opcode field is to be read.
1873
 * @return        - 0 on success; nonzero otherwise.
1874
 */
1875
static int readMaskRegister(struct InternalInstruction* insn)
1876
46.1k
{
1877
46.1k
  if (insn->vectorExtensionType != TYPE_EVEX)
1878
0
    return -1;
1879
1880
46.1k
  insn->writemask = (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
1881
1882
46.1k
  return 0;
1883
46.1k
}
1884
1885
/*
1886
 * readOperands - Consults the specifier for an instruction and consumes all
1887
 *   operands for that instruction, interpreting them as it goes.
1888
 *
1889
 * @param insn  - The instruction whose operands are to be read and interpreted.
1890
 * @return      - 0 if all operands could be read; nonzero otherwise.
1891
 */
1892
static int readOperands(struct InternalInstruction* insn)
1893
1.42M
{
1894
1.42M
  int hasVVVV, needVVVV;
1895
1.42M
  int sawRegImm = 0;
1896
1.42M
  int i;
1897
1898
  /* If non-zero vvvv specified, need to make sure one of the operands
1899
     uses it. */
1900
1.42M
  hasVVVV = !readVVVV(insn);
1901
1.42M
  needVVVV = hasVVVV && (insn->vvvv != 0);
1902
1903
9.94M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
1904
8.52M
    const OperandSpecifier *op = &x86OperandSets[insn->spec->operands][i];
1905
8.52M
    switch (op->encoding) {
1906
6.08M
      case ENCODING_NONE:
1907
6.15M
      case ENCODING_SI:
1908
6.23M
      case ENCODING_DI:
1909
6.23M
        break;
1910
1911
36.4k
      CASE_ENCODING_VSIB:
1912
        // VSIB can use the V2 bit so check only the other bits.
1913
36.4k
        if (needVVVV)
1914
4.04k
          needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
1915
1916
36.4k
        if (readModRM(insn))
1917
0
          return -1;
1918
1919
        // Reject if SIB wasn't used.
1920
6.97k
        if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64)
1921
12
          return -1;
1922
1923
        // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1924
6.96k
        if (insn->sibIndex == SIB_INDEX_NONE)
1925
585
          insn->sibIndex = (SIBIndex)(insn->sibIndexBase + 4);
1926
1927
        // If EVEX.v2 is set this is one of the 16-31 registers.
1928
6.96k
        if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT &&
1929
3.07k
            v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1930
2.37k
          insn->sibIndex = (SIBIndex)(insn->sibIndex + 16);
1931
1932
        // Adjust the index register to the correct size.
1933
6.96k
        switch (op->type) {
1934
0
          default:
1935
            // debug("Unhandled VSIB index type");
1936
0
            return -1;
1937
2.36k
          case TYPE_MVSIBX:
1938
2.36k
            insn->sibIndex = (SIBIndex)(SIB_INDEX_XMM0 +
1939
2.36k
                (insn->sibIndex - insn->sibIndexBase));
1940
2.36k
            break;
1941
2.44k
          case TYPE_MVSIBY:
1942
2.44k
            insn->sibIndex = (SIBIndex)(SIB_INDEX_YMM0 +
1943
2.44k
                (insn->sibIndex - insn->sibIndexBase));
1944
2.44k
            break;
1945
2.15k
          case TYPE_MVSIBZ:
1946
2.15k
            insn->sibIndex = (SIBIndex)(SIB_INDEX_ZMM0 +
1947
2.15k
                (insn->sibIndex - insn->sibIndexBase));
1948
2.15k
            break;
1949
6.96k
        }
1950
1951
        // Apply the AVX512 compressed displacement scaling factor.
1952
6.96k
        if (op->encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1953
890
          insn->displacement *= 1 << (op->encoding - ENCODING_VSIB);
1954
6.96k
        break;
1955
1956
560k
      case ENCODING_REG:
1957
8.77M
      CASE_ENCODING_RM:
1958
8.77M
        if (readModRM(insn))
1959
0
          return -1;
1960
1961
1.28M
        if (fixupReg(insn, op))
1962
47
          return -1;
1963
1964
        // Apply the AVX512 compressed displacement scaling factor.
1965
1.28M
        if (op->encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1966
118k
          insn->displacement *= 1 << (op->encoding - ENCODING_RM);
1967
1.28M
        break;
1968
1969
288k
      case ENCODING_IB:
1970
288k
        if (sawRegImm) {
1971
          /* Saw a register immediate so don't read again and instead split the
1972
             previous immediate.  FIXME: This is a hack. */
1973
823
          insn->immediates[insn->numImmediatesConsumed] =
1974
823
            insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1975
823
          ++insn->numImmediatesConsumed;
1976
823
          break;
1977
823
        }
1978
288k
        if (readImmediate(insn, 1))
1979
638
          return -1;
1980
287k
        if (op->type == TYPE_XMM || op->type == TYPE_YMM)
1981
1.81k
          sawRegImm = 1;
1982
287k
        break;
1983
1984
19.5k
      case ENCODING_IW:
1985
19.5k
        if (readImmediate(insn, 2))
1986
82
          return -1;
1987
19.4k
        break;
1988
1989
19.4k
      case ENCODING_ID:
1990
7.99k
        if (readImmediate(insn, 4))
1991
80
          return -1;
1992
7.91k
        break;
1993
1994
7.91k
      case ENCODING_IO:
1995
707
        if (readImmediate(insn, 8))
1996
18
          return -1;
1997
689
        break;
1998
1999
65.2k
      case ENCODING_Iv:
2000
65.2k
        if (readImmediate(insn, insn->immediateSize))
2001
520
          return -1;
2002
64.7k
        break;
2003
2004
64.7k
      case ENCODING_Ia:
2005
14.4k
        if (readImmediate(insn, insn->addressSize))
2006
140
          return -1;
2007
        /* Direct memory-offset (moffset) immediate will get mapped
2008
           to memory operand later. We want the encoding info to
2009
           reflect that as well. */
2010
14.2k
        insn->displacementOffset = insn->immediateOffset;
2011
14.2k
        insn->consumedDisplacement = true;
2012
14.2k
        insn->displacementSize = insn->immediateSize;
2013
14.2k
        insn->displacement = insn->immediates[insn->numImmediatesConsumed - 1];
2014
14.2k
        insn->immediateOffset = 0;
2015
14.2k
        insn->immediateSize = 0;
2016
14.2k
        break;
2017
2018
4.12k
      case ENCODING_IRC:
2019
4.12k
        insn->RC = (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 1) |
2020
4.12k
          lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2021
4.12k
        break;
2022
2023
24.7k
      case ENCODING_RB:
2024
24.7k
        if (readOpcodeRegister(insn, 1))
2025
0
          return -1;
2026
24.7k
        break;
2027
2028
24.7k
      case ENCODING_RW:
2029
0
        if (readOpcodeRegister(insn, 2))
2030
0
          return -1;
2031
0
        break;
2032
2033
0
      case ENCODING_RD:
2034
0
        if (readOpcodeRegister(insn, 4))
2035
0
          return -1;
2036
0
        break;
2037
2038
20.7k
      case ENCODING_RO:
2039
20.7k
        if (readOpcodeRegister(insn, 8))
2040
0
          return -1;
2041
20.7k
        break;
2042
2043
122k
      case ENCODING_Rv:
2044
122k
        if (readOpcodeRegister(insn, 0))
2045
0
          return -1;
2046
122k
        break;
2047
2048
122k
      case ENCODING_FP:
2049
7.90k
        break;
2050
2051
76.9k
      case ENCODING_VVVV:
2052
76.9k
        if (!hasVVVV)
2053
0
          return -1;
2054
2055
76.9k
        needVVVV = 0; /* Mark that we have found a VVVV operand. */
2056
2057
76.9k
        if (insn->mode != MODE_64BIT)
2058
46.1k
          insn->vvvv = (Reg)(insn->vvvv & 0x7);
2059
2060
76.9k
        if (fixupReg(insn, op))
2061
1
          return -1;
2062
76.9k
        break;
2063
2064
76.9k
      case ENCODING_WRITEMASK:
2065
46.1k
        if (readMaskRegister(insn))
2066
0
          return -1;
2067
46.1k
        break;
2068
2069
298k
      case ENCODING_DUP:
2070
298k
        break;
2071
2072
0
      default:
2073
        // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2074
0
        return -1;
2075
8.52M
    }
2076
8.52M
  }
2077
2078
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2079
1.42M
  if (needVVVV)
2080
13
    return -1;
2081
2082
1.42M
  return 0;
2083
1.42M
}
2084
2085
// return True if instruction is illegal to use with prefixes
2086
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2087
static bool checkPrefix(struct InternalInstruction *insn)
2088
1.42M
{
2089
  // LOCK prefix
2090
1.42M
  if (insn->hasLockPrefix) {
2091
52.9k
    switch(insn->instructionID) {
2092
318
      default:
2093
        // invalid LOCK
2094
318
        return true;
2095
2096
      // nop dword [rax]
2097
125
      case X86_NOOPL:
2098
2099
      // DEC
2100
210
      case X86_DEC16m:
2101
755
      case X86_DEC32m:
2102
1.24k
      case X86_DEC64m:
2103
1.57k
      case X86_DEC8m:
2104
2105
      // ADC
2106
1.90k
      case X86_ADC16mi:
2107
2.33k
      case X86_ADC16mi8:
2108
2.68k
      case X86_ADC16mr:
2109
2.97k
      case X86_ADC32mi:
2110
3.39k
      case X86_ADC32mi8:
2111
3.59k
      case X86_ADC32mr:
2112
3.77k
      case X86_ADC64mi32:
2113
4.19k
      case X86_ADC64mi8:
2114
4.40k
      case X86_ADC64mr:
2115
4.95k
      case X86_ADC8mi:
2116
5.32k
      case X86_ADC8mi8:
2117
5.75k
      case X86_ADC8mr:
2118
6.03k
      case X86_ADC8rm:
2119
6.37k
      case X86_ADC16rm:
2120
6.58k
      case X86_ADC32rm:
2121
6.86k
      case X86_ADC64rm:
2122
2123
      // ADD
2124
7.18k
      case X86_ADD16mi:
2125
7.67k
      case X86_ADD16mi8:
2126
8.02k
      case X86_ADD16mr:
2127
8.37k
      case X86_ADD32mi:
2128
8.91k
      case X86_ADD32mi8:
2129
9.61k
      case X86_ADD32mr:
2130
9.96k
      case X86_ADD64mi32:
2131
10.3k
      case X86_ADD64mi8:
2132
10.6k
      case X86_ADD64mr:
2133
11.1k
      case X86_ADD8mi:
2134
11.4k
      case X86_ADD8mi8:
2135
12.1k
      case X86_ADD8mr:
2136
12.4k
      case X86_ADD8rm:
2137
12.9k
      case X86_ADD16rm:
2138
13.0k
      case X86_ADD32rm:
2139
13.5k
      case X86_ADD64rm:
2140
2141
      // AND
2142
13.8k
      case X86_AND16mi:
2143
14.1k
      case X86_AND16mi8:
2144
14.4k
      case X86_AND16mr:
2145
14.8k
      case X86_AND32mi:
2146
15.1k
      case X86_AND32mi8:
2147
15.5k
      case X86_AND32mr:
2148
15.6k
      case X86_AND64mi32:
2149
15.9k
      case X86_AND64mi8:
2150
16.2k
      case X86_AND64mr:
2151
17.1k
      case X86_AND8mi:
2152
17.2k
      case X86_AND8mi8:
2153
17.9k
      case X86_AND8mr:
2154
18.4k
      case X86_AND8rm:
2155
18.8k
      case X86_AND16rm:
2156
19.1k
      case X86_AND32rm:
2157
19.2k
      case X86_AND64rm:
2158
2159
      // BTC
2160
19.6k
      case X86_BTC16mi8:
2161
19.8k
      case X86_BTC16mr:
2162
19.9k
      case X86_BTC32mi8:
2163
20.1k
      case X86_BTC32mr:
2164
20.3k
      case X86_BTC64mi8:
2165
20.4k
      case X86_BTC64mr:
2166
2167
      // BTR
2168
20.6k
      case X86_BTR16mi8:
2169
20.8k
      case X86_BTR16mr:
2170
20.9k
      case X86_BTR32mi8:
2171
21.1k
      case X86_BTR32mr:
2172
21.2k
      case X86_BTR64mi8:
2173
21.3k
      case X86_BTR64mr:
2174
2175
      // BTS
2176
21.8k
      case X86_BTS16mi8:
2177
22.1k
      case X86_BTS16mr:
2178
22.3k
      case X86_BTS32mi8:
2179
22.6k
      case X86_BTS32mr:
2180
22.7k
      case X86_BTS64mi8:
2181
23.0k
      case X86_BTS64mr:
2182
2183
      // CMPXCHG
2184
23.4k
      case X86_CMPXCHG16B:
2185
23.7k
      case X86_CMPXCHG16rm:
2186
23.9k
      case X86_CMPXCHG32rm:
2187
24.1k
      case X86_CMPXCHG64rm:
2188
24.8k
      case X86_CMPXCHG8rm:
2189
25.0k
      case X86_CMPXCHG8B:
2190
2191
      // INC
2192
25.3k
      case X86_INC16m:
2193
25.8k
      case X86_INC32m:
2194
26.0k
      case X86_INC64m:
2195
26.3k
      case X86_INC8m:
2196
2197
      // NEG
2198
26.5k
      case X86_NEG16m:
2199
26.8k
      case X86_NEG32m:
2200
26.9k
      case X86_NEG64m:
2201
27.0k
      case X86_NEG8m:
2202
2203
      // NOT
2204
27.3k
      case X86_NOT16m:
2205
27.7k
      case X86_NOT32m:
2206
28.0k
      case X86_NOT64m:
2207
28.5k
      case X86_NOT8m:
2208
2209
      // OR
2210
28.9k
      case X86_OR16mi:
2211
29.9k
      case X86_OR16mi8:
2212
30.1k
      case X86_OR16mr:
2213
30.5k
      case X86_OR32mi:
2214
31.0k
      case X86_OR32mi8:
2215
31.6k
      case X86_OR32mr:
2216
31.7k
      case X86_OR64mi32:
2217
31.9k
      case X86_OR64mi8:
2218
32.1k
      case X86_OR64mr:
2219
32.4k
      case X86_OR8mi8:
2220
32.6k
      case X86_OR8mi:
2221
32.8k
      case X86_OR8mr:
2222
33.2k
      case X86_OR8rm:
2223
33.5k
      case X86_OR16rm:
2224
33.9k
      case X86_OR32rm:
2225
34.4k
      case X86_OR64rm:
2226
2227
      // SBB
2228
34.8k
      case X86_SBB16mi:
2229
35.2k
      case X86_SBB16mi8:
2230
35.7k
      case X86_SBB16mr:
2231
35.9k
      case X86_SBB32mi:
2232
36.1k
      case X86_SBB32mi8:
2233
36.7k
      case X86_SBB32mr:
2234
37.1k
      case X86_SBB64mi32:
2235
37.3k
      case X86_SBB64mi8:
2236
37.5k
      case X86_SBB64mr:
2237
37.9k
      case X86_SBB8mi:
2238
38.4k
      case X86_SBB8mi8:
2239
38.6k
      case X86_SBB8mr:
2240
2241
      // SUB
2242
39.0k
      case X86_SUB16mi:
2243
39.5k
      case X86_SUB16mi8:
2244
39.7k
      case X86_SUB16mr:
2245
40.1k
      case X86_SUB32mi:
2246
40.8k
      case X86_SUB32mi8:
2247
41.0k
      case X86_SUB32mr:
2248
41.2k
      case X86_SUB64mi32:
2249
41.7k
      case X86_SUB64mi8:
2250
41.9k
      case X86_SUB64mr:
2251
42.3k
      case X86_SUB8mi8:
2252
42.4k
      case X86_SUB8mi:
2253
42.8k
      case X86_SUB8mr:
2254
43.2k
      case X86_SUB8rm:
2255
43.4k
      case X86_SUB16rm:
2256
43.6k
      case X86_SUB32rm:
2257
43.8k
      case X86_SUB64rm:
2258
2259
      // XADD
2260
43.9k
      case X86_XADD16rm:
2261
44.0k
      case X86_XADD32rm:
2262
44.5k
      case X86_XADD64rm:
2263
45.0k
      case X86_XADD8rm:
2264
2265
      // XCHG
2266
45.4k
      case X86_XCHG16rm:
2267
45.6k
      case X86_XCHG32rm:
2268
46.1k
      case X86_XCHG64rm:
2269
46.6k
      case X86_XCHG8rm:
2270
2271
      // XOR
2272
47.3k
      case X86_XOR16mi:
2273
48.0k
      case X86_XOR16mi8:
2274
48.1k
      case X86_XOR16mr:
2275
48.3k
      case X86_XOR32mi:
2276
48.7k
      case X86_XOR32mi8:
2277
49.1k
      case X86_XOR32mr:
2278
49.4k
      case X86_XOR64mi32:
2279
49.6k
      case X86_XOR64mi8:
2280
49.8k
      case X86_XOR64mr:
2281
50.0k
      case X86_XOR8mi8:
2282
50.9k
      case X86_XOR8mi:
2283
51.5k
      case X86_XOR8mr:
2284
51.9k
      case X86_XOR8rm:
2285
52.2k
      case X86_XOR16rm:
2286
52.5k
      case X86_XOR32rm:
2287
52.6k
      case X86_XOR64rm:
2288
2289
        // this instruction can be used with LOCK prefix
2290
52.6k
        return false;
2291
52.9k
    }
2292
52.9k
  }
2293
2294
#if 0
2295
  // REPNE prefix
2296
  if (insn->repeatPrefix) {
2297
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2298
    // In such a case, clear it.
2299
    if (insn->twoByteEscape == 0x0f) {
2300
      insn->prefix0 = 0;
2301
    }
2302
  }
2303
#endif
2304
2305
  // no invalid prefixes
2306
1.36M
  return false;
2307
1.42M
}
2308
2309
/*
2310
 * decodeInstruction - Reads and interprets a full instruction provided by the
2311
 *   user.
2312
 *
2313
 * @param insn      - A pointer to the instruction to be populated.  Must be
2314
 *                    pre-allocated.
2315
 * @param reader    - The function to be used to read the instruction's bytes.
2316
 * @param readerArg - A generic argument to be passed to the reader to store
2317
 *                    any internal state.
2318
 * @param startLoc  - The address (in the reader's address space) of the first
2319
 *                    byte in the instruction.
2320
 * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
2321
 *                    decode the instruction in.
2322
 * @return          - 0 if instruction is valid; nonzero if not.
2323
 */
2324
int decodeInstruction(struct InternalInstruction *insn,
2325
    byteReader_t reader,
2326
    const void *readerArg,
2327
    uint64_t startLoc,
2328
    DisassemblerMode mode)
2329
1.42M
{
2330
1.42M
  insn->reader = reader;
2331
1.42M
  insn->readerArg = readerArg;
2332
1.42M
  insn->startLocation = startLoc;
2333
1.42M
  insn->readerCursor = startLoc;
2334
1.42M
  insn->mode = mode;
2335
1.42M
  insn->numImmediatesConsumed = 0;
2336
2337
1.42M
  if (readPrefixes(insn) ||
2338
1.42M
      readOpcode(insn) ||
2339
1.42M
      getID(insn) ||
2340
1.42M
      insn->instructionID == 0 ||
2341
1.42M
      checkPrefix(insn) ||
2342
1.42M
      readOperands(insn))
2343
7.64k
    return -1;
2344
2345
1.42M
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2346
2347
  // instruction length must be <= 15 to be valid
2348
1.42M
  if (insn->length > 15)
2349
72
    return -1;
2350
2351
1.42M
  if (insn->operandSize == 0)
2352
1.42M
    insn->operandSize = insn->registerSize;
2353
2354
1.42M
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2355
2356
1.42M
  return 0;
2357
1.42M
}
2358
2359
#endif
2360