OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
8 * Internal implementation of the state associated with matching instructions. | |
9 */ | |
10 | |
11 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NC_INST_STATE_INTERNAL_H
_ | |
12 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NC_INST_STATE_INTERNAL_H
_ | |
13 | |
14 #include "native_client/src/shared/utils/types.h" | |
15 #include "native_client/src/trusted/validator/x86/decoder/ncop_exps.h" | |
16 #include "native_client/src/trusted/validator/x86/decoder/nc_inst_state.h" | |
17 #include "native_client/src/trusted/validator/x86/ncinstbuffer.h" | |
18 | |
19 EXTERN_C_BEGIN | |
20 | |
21 /* The meta model of an x86 opcode instruction. */ | |
22 struct NaClInst; | |
23 | |
24 /* Model of a code segment. */ | |
25 struct NaClSegment; | |
26 | |
27 /* Decoder tables used to decode instructions. */ | |
28 struct NaClDecodeTables; | |
29 | |
30 /* Defines the type used to align OpExprNodes when memory allocating. */ | |
31 typedef uint64_t NaClOpExpElement; | |
32 | |
33 /* Defines the decoder tables to use to decode an instruction. */ | |
34 struct NaClDecodeTables; | |
35 | |
36 /* Model data needed to decode an x86 instruction. */ | |
37 struct NaClInstState { | |
38 /* This pointer is used to access vbase, allowing instruction addresses to be | |
39 * printed in the address space that the code is being mapped to. | |
40 */ | |
41 struct NaClInstIter* iter; | |
42 /* The bytes used to parse the x86-32 instruction (may have added | |
43 * zero filler if the instruction straddles the end of the memory segment). | |
44 */ | |
45 NCInstBytes bytes; | |
46 /* The address of the instruction, relative to the beginning of the code | |
47 * segment. | |
48 */ | |
49 NaClPcAddress inst_addr; | |
50 /* Define the upper limit on how many bytes can be in the instruction. */ | |
51 uint8_t length_limit; | |
52 /* Define the number of prefix bytes processed. */ | |
53 uint8_t num_prefix_bytes; /* 0..4 */ | |
54 /* The prefix byte used to recognize the opcode, or zero if not applicable. */ | |
55 uint8_t opcode_prefix; | |
56 /* Define the number of opcode bytes processed. */ | |
57 uint8_t num_opcode_bytes; | |
58 /* If REX prefix found, its value. Otherwise zero. */ | |
59 uint8_t rexprefix; | |
60 /* Number of REX prefix bytes found. */ | |
61 uint8_t num_rex_prefixes; | |
62 /* If Mod/RM byte defined, its value. Otherwise zero. */ | |
63 uint8_t modrm; | |
64 /* True if prefix bytes are duplicated prefix bytes. */ | |
65 Bool has_prefix_duplicates; | |
66 /* True if prefix has ambiguous segment prefix bytes. */ | |
67 Bool has_ambig_segment_prefixes; | |
68 /* True only if the instruction has an SIB byte. */ | |
69 Bool has_sib; | |
70 /* If a SIB byte is defined, its value. Otherwise zero. */ | |
71 uint8_t sib; | |
72 /* Define the number of displacement bytes matched by the instruction. */ | |
73 uint8_t num_disp_bytes; | |
74 /* Define the index of the first displacement byte of the instruction, or | |
75 * zero if num_disp_bytes == 0. | |
76 */ | |
77 uint8_t first_disp_byte; | |
78 /* Define the number of immediate bytes defined by the instruction. */ | |
79 uint8_t num_imm_bytes; | |
80 /* Define the index of the first immediate byte of the instruction, or | |
81 * zero if num_imm_bytes == 0;. | |
82 */ | |
83 uint8_t first_imm_byte; | |
84 /* Define the number of bytes to the second immediate value if defined | |
85 * (defaults to zero). | |
86 */ | |
87 uint8_t num_imm2_bytes; | |
88 /* The computed (default) operand size associated with the instruction. */ | |
89 uint8_t operand_size; | |
90 /* The computed (default) address size associated with the instruction. */ | |
91 uint8_t address_size; | |
92 /* The set of prefix byte kinds associated with the instruction | |
93 * (See kPrefixXXXX #define's in ncdecode.h) | |
94 */ | |
95 uint32_t prefix_mask; | |
96 /* The (opcode) instruction pattern used to match the instruction. | |
97 * Note: If this value is NULL, we have not yet tried to match | |
98 * the current instruction with the corresponding instruction iterator. | |
99 * Note: One can assume that two instructions use the same modeled | |
100 * NaClInst iff the pointers are equals. | |
101 */ | |
102 const NaClInst* inst; | |
103 /* The corresponding expression tree denoted by the matched instruction. */ | |
104 NaClExpVector nodes; | |
105 /* Transient pointer, which is defined each time an instruction is decoded. | |
106 * It defines the decoder tables to use to decode the instruction. | |
107 */ | |
108 struct NaClDecodeTables* decoder_tables; | |
109 /* True if the instruction is unchanged while dynamically replacing code. | |
110 * False if the instruction has changed or if code replacement is not being | |
111 * performed (i.e. normal validation.) | |
112 */ | |
113 Bool unchanged; | |
114 }; | |
115 | |
116 /* Model of an instruction iterator. */ | |
117 struct NaClInstIter { | |
118 /* Defines the decoder table to use to decode the instruction. */ | |
119 struct NaClDecodeTables* decoder_tables; | |
120 /* Defines the segment to process */ | |
121 struct NaClSegment* segment; | |
122 /* Defines the remaining memory to iterate over. */ | |
123 NCRemainingMemory memory; | |
124 /* Defines the current (relative pc) index into the segment. */ | |
125 NaClMemorySize index; | |
126 /* Defines the index of the current instruction, relative to | |
127 * the beginning of the segment. | |
128 */ | |
129 NaClMemorySize inst_count; | |
130 /* The following fields define a ring buffer, where buffer_index | |
131 * is the index of the current instruction in the buffer, and | |
132 * buffer_size is the number of iterator states in the buffer. | |
133 */ | |
134 size_t buffer_size; | |
135 size_t buffer_index; | |
136 struct NaClInstState* buffer; | |
137 }; | |
138 | |
139 /* Structure holding the results of consuming the opcode bytes of the | |
140 * instruction. | |
141 */ | |
142 typedef struct { | |
143 /* The applicable prefix byte selector, or 0 if no prefix selector. */ | |
144 uint8_t opcode_prefix; | |
145 /* The (last) byte of the matched opcode. */ | |
146 uint8_t opcode_byte; | |
147 /* The most specific prefix that the opcode bytes can match | |
148 * (or OpcodePrefixEnumSize if no such patterns exist). | |
149 */ | |
150 NaClInstPrefix matched_prefix; | |
151 /* The number of bytes to subtract from the instruction length, | |
152 * the next time GetNextNaClInstCandidates is called. | |
153 */ | |
154 uint8_t next_length_adjustment; | |
155 } NaClInstPrefixDescriptor; | |
156 | |
157 /* Given the current location of the (relative) pc of the given instruction | |
158 * iterator, update the given state to hold the matched opcode | |
159 * (instruction) pattern. If no matching pattern exists, set the state | |
160 * to a matched undefined opcode (instruction) pattern. In all cases, | |
161 * update the state to hold all information on the matched bytes of the | |
162 * instruction. | |
163 */ | |
164 void NaClDecodeInst(struct NaClInstIter* iter, struct NaClInstState* state); | |
165 | |
166 /* Returns the (undecoded) instruction state of the iterator. Should only | |
167 * be used for testing. | |
168 */ | |
169 | |
170 struct NaClInstState* NaClInstIterGetUndecodedState(struct NaClInstIter* iter); | |
171 | |
172 EXTERN_C_END | |
173 | |
174 #endif /* NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_X86_DECODER_NC_INST_STATE_INTERNA
L_H_ */ | |
OLD | NEW |