Coverage Report

Created: 2026-11-12 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zydis/include/Zydis/Internal/DecoderData.h
Line
Count
Source
1
/***************************************************************************************************
2
3
  Zyan Disassembler Library (Zydis)
4
5
  Original Author : Florian Bernd
6
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
25
***************************************************************************************************/
26
27
#ifndef ZYDIS_INTERNAL_DECODERDATA_H
28
#define ZYDIS_INTERNAL_DECODERDATA_H
29
30
#include <Zycore/Defines.h>
31
#include <Zycore/Types.h>
32
#include <Zydis/Defines.h>
33
34
#ifdef __cplusplus
35
extern "C" {
36
#endif
37
38
/* ============================================================================================== */
39
/* Enums and types                                                                                */
40
/* ============================================================================================== */
41
42
// MSVC does not like types other than (un-)signed int for bit-fields
43
#ifdef ZYAN_MSVC
44
#   pragma warning(push)
45
#   pragma warning(disable:4214)
46
#endif
47
48
#pragma pack(push, 1)
49
50
/* ---------------------------------------------------------------------------------------------- */
51
/* Decoder tree                                                                                   */
52
/* ---------------------------------------------------------------------------------------------- */
53
54
/**
55
 * Defines the `ZydisDecoderTreeNode` data-type.
56
 */
57
typedef ZyanU16 ZydisDecoderTreeNode;
58
59
/* ---------------------------------------------------------------------------------------------- */
60
61
/**
62
 * Values that represent Zydis decoder tree node types.
63
 */
64
typedef enum ZydisDecoderTreeNodeType_
65
{
66
    ZYDIS_NODETYPE_INVALID = 0,
67
    /**
68
     * Reference to an instruction-definition.
69
     */
70
    ZYDIS_NODETYPE_DEFINITION,
71
    /**
72
     * A decoder tree node that instructs the decoder to switch to a different opcode table.
73
     * The `arg0` value of the node header is the id of the opcode table to switch to.
74
     */
75
    ZYDIS_NODETYPE_SWITCH_TABLE,
76
    /**
77
     * Reference to an XOP-map selector.
78
     */
79
    ZYDIS_NODETYPE_SWITCH_TABLE_XOP,
80
    /**
81
     * Reference to an VEX-map selector.
82
     */
83
    ZYDIS_NODETYPE_SWITCH_TABLE_VEX,
84
    /**
85
     * Reference to an EVEX/MVEX-map selector.
86
     */
87
    ZYDIS_NODETYPE_SWITCH_TABLE_EMVEX,
88
    /**
89
     * Reference to a REX2-map selector.
90
     */
91
    ZYDIS_NODETYPE_SWITCH_TABLE_REX2,
92
    /**
93
     * Reference to an opcode selector.
94
     */
95
    ZYDIS_NODETYPE_OPCODE_TABLE,
96
    /**
97
     * Reference to an instruction-mode selector.
98
     */
99
    ZYDIS_NODETYPE_MODE,
100
    /**
101
     * Reference to an compacted instruction-mode selector.
102
     */
103
    ZYDIS_NODETYPE_MODE_COMPACT,
104
    /**
105
     * Reference to a ModRM.mod selector.
106
     */
107
    ZYDIS_NODETYPE_MODRM_MOD,
108
    /**
109
     * Reference to a compacted ModRM.mod selector.
110
     */
111
    ZYDIS_NODETYPE_MODRM_MOD_COMPACT,
112
    /**
113
     * Reference to a ModRM.reg selector.
114
     */
115
    ZYDIS_NODETYPE_MODRM_REG,
116
    /**
117
     * Reference to a ModRM.rm selector.
118
     */
119
    ZYDIS_NODETYPE_MODRM_RM,
120
    /**
121
     * Reference to a PrefixGroup1 selector.
122
     */
123
    ZYDIS_NODETYPE_PREFIX_GROUP1,
124
    /**
125
     * Reference to a PrefixGroup3 selector.
126
     */
127
    ZYDIS_NODETYPE_PREFIX_GROUP3,
128
    /**
129
     * Reference to a mandatory-prefix selector.
130
     */
131
    ZYDIS_NODETYPE_MANDATORY_PREFIX,
132
    /**
133
     * Reference to an operand-size selector.
134
     */
135
    ZYDIS_NODETYPE_OPERAND_SIZE,
136
    /**
137
     * Reference to an address-size selector.
138
     */
139
    ZYDIS_NODETYPE_ADDRESS_SIZE,
140
    /**
141
     * Reference to a vector-length selector.
142
     */
143
    ZYDIS_NODETYPE_VECTOR_LENGTH,
144
    /**
145
     * Reference to an REX/VEX/EVEX.W selector.
146
     */
147
    ZYDIS_NODETYPE_REX_W,
148
    /**
149
     * Reference to an REX/VEX/EVEX.B selector.
150
     */
151
    ZYDIS_NODETYPE_REX_B,
152
    /**
153
     * Reference to an EVEX.b selector.
154
     */
155
    ZYDIS_NODETYPE_EVEX_B,
156
    /**
157
     * Reference to an MVEX.E selector.
158
     */
159
    ZYDIS_NODETYPE_MVEX_E,
160
    /**
161
     * Reference to a AMD-mode selector.
162
     */
163
    ZYDIS_NODETYPE_MODE_AMD,
164
    /**
165
     * Reference to a KNC-mode selector.
166
     */
167
    ZYDIS_NODETYPE_MODE_KNC,
168
    /**
169
     * Reference to a MPX-mode selector.
170
     */
171
    ZYDIS_NODETYPE_MODE_MPX,
172
    /**
173
     * Reference to a CET-mode selector.
174
     */
175
    ZYDIS_NODETYPE_MODE_CET,
176
    /**
177
     * Reference to a LZCNT-mode selector.
178
     */
179
    ZYDIS_NODETYPE_MODE_LZCNT,
180
    /**
181
     * Reference to a TZCNT-mode selector.
182
     */
183
    ZYDIS_NODETYPE_MODE_TZCNT,
184
    /**
185
     * Reference to a WBNOINVD-mode selector.
186
     */
187
    ZYDIS_NODETYPE_MODE_WBNOINVD,
188
    /**
189
     * Reference to a CLDEMOTE-mode selector.
190
     */
191
    ZYDIS_NODETYPE_MODE_CLDEMOTE,
192
    /**
193
     * Reference to a IPREFETCH-mode selector.
194
     */
195
    ZYDIS_NODETYPE_MODE_IPREFETCH,
196
    /**
197
     * Reference to a UD0_COMPAT-mode selector.
198
     */
199
    ZYDIS_NODETYPE_MODE_UD0_COMPAT,
200
    /**
201
     * Reference to an EVEX.nd selector.
202
     */
203
    ZYDIS_NODETYPE_EVEX_ND,
204
    /**
205
     * Reference to an EVEX.nf selector.
206
     */
207
    ZYDIS_NODETYPE_EVEX_NF,
208
    /**
209
     * Reference to an EVEX.scc selector.
210
     */
211
    ZYDIS_NODETYPE_EVEX_SCC,
212
    /**
213
     * Reference to a REX2-prefix selector.
214
     */
215
    ZYDIS_NODETYPE_REX_2,
216
    /**
217
     * Reference to a EVEX.U selector.
218
     */
219
    ZYDIS_NODETYPE_EVEX_U,
220
    /**
221
     * Reference to a IBHF-mode selector.
222
     */
223
    ZYDIS_NODETYPE_MODE_IBHF,
224
    /**
225
     * Reference to a PREFETCHRST-mode selector.
226
     */
227
    ZYDIS_NODETYPE_MODE_PREFETCHRST,
228
229
    /**
230
     * Maximum value of this enum.
231
     */
232
    ZYDIS_NODETYPE_MAX_VALUE = ZYDIS_NODETYPE_MODE_PREFETCHRST,
233
    /**
234
     * The minimum number of bits required to represent all values of this enum.
235
     */
236
    ZYDIS_NODETYPE_REQUIRED_BITS = ZYAN_BITS_TO_REPRESENT(ZYDIS_NODETYPE_MAX_VALUE)
237
} ZydisDecoderTreeNodeType;
238
239
/* ---------------------------------------------------------------------------------------------- */
240
/* Opcode tables                                                                                  */
241
/* ---------------------------------------------------------------------------------------------- */
242
243
// TODO: Auto generate this.
244
245
13.3k
#define ZYDIS_OPCODE_TABLE_PRIMARY 0x00
246
#define ZYDIS_OPCODE_TABLE_0F      0x01
247
#define ZYDIS_OPCODE_TABLE_0F38    0x02
248
#define ZYDIS_OPCODE_TABLE_0F3A    0x03
249
#define ZYDIS_OPCODE_TABLE_VEX     0x04
250
#define ZYDIS_OPCODE_TABLE_EVEX    0x1C
251
#define ZYDIS_OPCODE_TABLE_MVEX    0x3C
252
#define ZYDIS_OPCODE_TABLE_XOP     0x4C
253
39
#define ZYDIS_OPCODE_TABLE_3DNOW   0x4F
254
255
/* ---------------------------------------------------------------------------------------------- */
256
257
#pragma pack(pop)
258
259
#ifdef ZYAN_MSVC
260
#   pragma warning(pop)
261
#endif
262
263
/* ---------------------------------------------------------------------------------------------- */
264
/* Physical instruction encoding info                                                             */
265
/* ---------------------------------------------------------------------------------------------- */
266
267
/**
268
 * Defines the `ZydisInstructionEncodingFlags` data-type.
269
 */
270
typedef ZyanU8 ZydisInstructionEncodingFlags;
271
272
/**
273
 * The instruction has an optional modrm byte.
274
 */
275
13.0k
#define ZYDIS_INSTR_ENC_FLAG_HAS_MODRM      0x01
276
277
/**
278
 * The instruction has an optional displacement value.
279
 */
280
13.0k
#define ZYDIS_INSTR_ENC_FLAG_HAS_DISP       0x02
281
282
/**
283
 * The instruction has an optional immediate value.
284
 */
285
12.9k
#define ZYDIS_INSTR_ENC_FLAG_HAS_IMM0       0x04
286
287
/**
288
 * The instruction has a second optional immediate value.
289
 */
290
12.9k
#define ZYDIS_INSTR_ENC_FLAG_HAS_IMM1       0x08
291
292
/**
293
 * The instruction ignores the value of `modrm.mod` and always assumes `modrm.mod == 3`
294
 *          ("reg, reg" - form).
295
 *
296
 *          Instructions with this flag can't have a SIB byte or a displacement value.
297
 */
298
12.2k
#define ZYDIS_INSTR_ENC_FLAG_FORCE_REG_FORM 0x10
299
300
/**
301
 * Defines the `ZydisInstructionEncodingInfo` struct.
302
 */
303
typedef struct ZydisInstructionEncodingInfo_
304
{
305
    /**
306
     * Contains flags with information about the physical instruction-encoding.
307
     */
308
    ZydisInstructionEncodingFlags flags;
309
    /**
310
     * Displacement info.
311
     */
312
    struct
313
    {
314
        /**
315
         * The size of the displacement value.
316
         */
317
        ZyanU8 size[3];
318
    } disp;
319
    /**
320
     * Immediate info.
321
     */
322
    struct
323
    {
324
        /**
325
         * The size of the immediate value.
326
         */
327
        ZyanU8 size[3];
328
        /**
329
         * Signals, if the value is signed.
330
         */
331
        ZyanBool is_signed;
332
        /**
333
         * Signals, if the value is an address.
334
         */
335
        ZyanBool is_address;
336
        /**
337
         * Signals, if the value is a relative offset.
338
         */
339
        ZyanBool is_relative;
340
    } imm[2];
341
} ZydisInstructionEncodingInfo;
342
343
/* ---------------------------------------------------------------------------------------------- */
344
345
/* ============================================================================================== */
346
/* Functions                                                                                      */
347
/* ============================================================================================== */
348
349
/* ---------------------------------------------------------------------------------------------- */
350
/* Decoder tree                                                                                   */
351
/* ---------------------------------------------------------------------------------------------- */
352
353
/**
354
 * Gets the type of the given decoder tree node.
355
 *
356
 * @param node  The decoder tree node.
357
 *
358
 * Encoding: [15..8] = ARG0, [7..0] = TYPE.
359
 */
360
#define ZYDIS_DT_GET_TYPE(node) \
361
84.9k
    ((ZydisDecoderTreeNodeType)((*(node)) & 0xFF))
362
363
/**
364
 * Gets the first argument of the given decoder tree node.
365
 *
366
 * @param node  The decoder tree node.
367
 *
368
 * Encoding: [15..8] = ARG0, [7..0] = TYPE.
369
 */
370
#define ZYDIS_DT_GET_ARG0(node) \
371
17.6k
    ((ZyanU8)(((*(node)) >> 8) & 0xFF))
372
373
/**
374
 * Gets the value at index `index` of the given decoder tree node.
375
 *
376
 * @param node  The decoder tree node.
377
 * @param index The index of the value to retrieve.
378
 */
379
#define ZYDIS_DT_GET_VALUE(node, index) \
380
58.4k
    ((ZyanU16)(*((node) + 1 + (index))))
381
382
extern const ZydisInstructionEncodingInfo INSTR_ENCODINGS[];
383
extern const ZydisDecoderTreeNode* const OPCODE_TABLE_TREES[];
384
385
/**
386
 * Returns the root node of the opcode table with the given `opcode_table_id`.
387
 *
388
 * @param opcode_table_id   The id of the opcode table to retrieve.
389
 * @return The root node of the opcode table with the given `opcode_table_id`.
390
 */
391
ZYAN_INLINE const ZydisDecoderTreeNode* ZydisGetOpcodeTableRootNode(ZyanU8 opcode_table_id)
392
17.9k
{
393
17.9k
    return OPCODE_TABLE_TREES[opcode_table_id];
394
17.9k
}
Decoder.c:ZydisGetOpcodeTableRootNode
Line
Count
Source
392
17.9k
{
393
17.9k
    return OPCODE_TABLE_TREES[opcode_table_id];
394
17.9k
}
Unexecuted instantiation: DecoderData.c:ZydisGetOpcodeTableRootNode
395
396
/**
397
 * Returns information about optional instruction parts (like modrm, displacement or
398
 * immediates) for the instruction that is linked to the given `node`.
399
 *
400
 * @param   node    The instruction definition node.
401
 * @param   info    A pointer to the `ZydisInstructionParts` struct.
402
 */
403
ZYAN_INLINE void ZydisGetInstructionEncodingInfo(const ZydisDecoderTreeNode* node,
404
    const ZydisInstructionEncodingInfo** info)
405
13.0k
{
406
13.0k
    ZYAN_ASSERT(ZYDIS_DT_GET_TYPE(node) == ZYDIS_NODETYPE_DEFINITION);
407
13.0k
    *info = &INSTR_ENCODINGS[ZYDIS_DT_GET_ARG0(node)];
408
13.0k
}
Decoder.c:ZydisGetInstructionEncodingInfo
Line
Count
Source
405
13.0k
{
406
13.0k
    ZYAN_ASSERT(ZYDIS_DT_GET_TYPE(node) == ZYDIS_NODETYPE_DEFINITION);
407
13.0k
    *info = &INSTR_ENCODINGS[ZYDIS_DT_GET_ARG0(node)];
408
13.0k
}
Unexecuted instantiation: DecoderData.c:ZydisGetInstructionEncodingInfo
409
410
/* ---------------------------------------------------------------------------------------------- */
411
412
/* ============================================================================================== */
413
414
#ifdef __cplusplus
415
}
416
#endif
417
418
#endif /* ZYDIS_INTERNAL_DECODERDATA_H */