OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * This is the core of ia32-mode validator. Please note that this file | 8 * This is the core of ia32-mode validator. Please note that this file |
9 * combines ragel machine description and C language actions. Please read | 9 * combines ragel machine description and C language actions. Please read |
10 * validator_internals.html first to understand how the whole thing is built: | 10 * validator_internals.html first to understand how the whole thing is built: |
11 * it explains how the byte sequences are constructed, what constructs like | 11 * it explains how the byte sequences are constructed, what constructs like |
12 * "@{}" or "REX_WRX?" mean, etc. | 12 * "@{}" or "REX_WRX?" mean, etc. |
13 */ | 13 */ |
14 | 14 |
15 #include <assert.h> | 15 #include <assert.h> |
16 #include <errno.h> | 16 #include <errno.h> |
17 #include <stddef.h> | 17 #include <stddef.h> |
18 #include <stdio.h> | 18 #include <stdio.h> |
19 #include <stdlib.h> | 19 #include <stdlib.h> |
20 #include <string.h> | 20 #include <string.h> |
21 | 21 |
22 #include "native_client/src/trusted/validator_ragel/bitmap.h" | 22 #include "native_client/src/trusted/validator_ragel/bitmap.h" |
23 #include "native_client/src/trusted/validator_ragel/unreviewed/validator_interna l.h" | 23 #include "native_client/src/trusted/validator_ragel/validator_internal.h" |
24 | 24 |
25 /* Ignore this information: it's not used by security model in IA32 mode. */ | 25 /* Ignore this information: it's not used by security model in IA32 mode. */ |
26 /* TODO(khim): change gen_dfa to remove needs for these lines. */ | |
26 #undef GET_VEX_PREFIX3 | 27 #undef GET_VEX_PREFIX3 |
27 #define GET_VEX_PREFIX3 0 | 28 #define GET_VEX_PREFIX3 0 |
28 #undef SET_VEX_PREFIX3 | 29 #undef SET_VEX_PREFIX3 |
29 #define SET_VEX_PREFIX3(P) | 30 #define SET_VEX_PREFIX3(P) |
30 | 31 |
31 %%{ | 32 %%{ |
32 machine x86_32_validator; | 33 machine x86_32_validator; |
33 alphtype unsigned char; | 34 alphtype unsigned char; |
34 variable p current_position; | 35 variable p current_position; |
35 variable pe end_of_bundle; | 36 variable pe end_of_bundle; |
(...skipping 18 matching lines...) Expand all Loading... | |
54 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 55 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
55 include immediate_fields_parsing_ia32 | 56 include immediate_fields_parsing_ia32 |
56 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 57 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
57 include relative_fields_validator_actions | 58 include relative_fields_validator_actions |
58 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 59 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
59 include relative_fields_parsing | 60 include relative_fields_parsing |
60 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 61 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
61 include cpuid_actions | 62 include cpuid_actions |
62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 63 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
63 | 64 |
64 # Action which marks last byte as not immediate. Most 3DNow! instructions, | |
65 # some AVX and XOP instructions have this property. It's referenced by | |
66 # decode_x86_32 machine in [autogenerated] "validator_x86_32_instruction.rl" | |
67 # file. | |
68 action last_byte_is_not_immediate { | |
69 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; | |
70 } | |
71 | |
72 include decode_x86_32 "validator_x86_32_instruction.rl"; | 65 include decode_x86_32 "validator_x86_32_instruction.rl"; |
73 | 66 |
74 special_instruction = | 67 special_instruction = |
75 # and $~0x1f, %eXX call %eXX | 68 # and $~0x1f, %eXX call %eXX |
76 # vvvvvvvvvv | 69 # vvvvvvvvvv |
77 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax | 70 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax |
78 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx | 71 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx |
79 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx | 72 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx |
80 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx | 73 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx |
81 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp | 74 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp |
82 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp | 75 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp |
83 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi | 76 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi |
84 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi | 77 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi |
85 # ^^^^ ^^^^ | 78 # ^^^^ ^^^^ |
86 # and $~0x1f, %eXX jmp %eXX | 79 # and $~0x1f, %eXX jmp %eXX |
87 @{ | 80 @{ |
88 UnmarkValidJumpTarget((current_position - data) - 1, valid_targets); | 81 UnmarkValidJumpTarget((current_position - data) - 1, valid_targets); |
89 instruction_begin -= 3; | 82 instruction_begin -= 3; |
90 instruction_info_collected |= SPECIAL_INSTRUCTION; | 83 instruction_info_collected |= SPECIAL_INSTRUCTION; |
91 } | | 84 } | |
92 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax | 85 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax |
93 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) | 86 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) |
94 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg | 87 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg |
95 | 88 |
96 # Check if call is properly aligned | 89 # For direct call we explicitly encode all variations. |
97 # | 90 direct_call = (data16 0xe8 rel16) | (0xe8 rel32); |
98 # For direct call we explicitly encode all variations. For indirect call | 91 |
99 # we accept all the special instructions which ends with register-addressed | 92 # For indirect call we accept only near register-addressed indirect call. |
100 # indirect call. | 93 indirect_call_register = data16? 0xff (opcode_2 & modrm_registers); |
94 | |
95 # Ragel machine that accepts one call instruction or call superinstruction and | |
96 # checks if call is properly aligned. | |
101 call_alignment = | 97 call_alignment = |
102 ((one_instruction & | 98 ((one_instruction & direct_call) |
103 # Direct call | 99 # For indirect calls we accept all the special instructions which ends with |
104 ((data16 0xe8 rel16) | | 100 # register-addressed indirect call. |
105 (0xe8 rel32))) | | 101 (special_instruction & (any* indirect_call_register))) |
106 (special_instruction & | |
107 # Indirect call | |
108 (any* data16? 0xff ((opcode_2 | opcode_3) any* & | |
109 modrm_registers)))) | |
110 # Call instruction must aligned to the end of bundle. Previously this was | 102 # Call instruction must aligned to the end of bundle. Previously this was |
111 # strict requirement, today it's just warning to aid with debugging. | 103 # strict requirement, today it's just warning to aid with debugging. |
112 @{ | 104 @{ |
113 if (((current_position - data) & kBundleMask) != kBundleMask) | 105 if (((current_position - data) & kBundleMask) != kBundleMask) |
114 instruction_info_collected |= BAD_CALL_ALIGNMENT; | 106 instruction_info_collected |= BAD_CALL_ALIGNMENT; |
115 }; | 107 }; |
116 | 108 |
117 # This action calls user's callback (if needed) and cleans up validator's | 109 # This action calls users callback (if needed) and cleans up validators |
halyavin
2013/03/19 15:03:54
user, not users
khim
2013/03/21 14:38:17
Done.
| |
118 # internal state. | 110 # internal state. |
119 # | 111 # |
120 # We call the user callback if there are validation errors or if the | 112 # We call the user callback either on validation errors or on every |
121 # CALL_USER_CALLBACK_ON_EACH_INSTRUCTION option is used. | 113 # instruction, depending on CALL_USER_CALLBACK_ON_EACH_INSTRUTION option. |
122 # | 114 # |
123 # After that we move instruction_begin and clean all the variables which | 115 # After that we move instruction_begin and clean all the variables which |
124 # only used in the processing of a single instruction (prefixes, operand | 116 # are only used in the processing of a single instruction (here it's just |
125 # states and instruction_info_collected). | 117 # instruction_info_collected, there are more state in x86-64 case). |
126 action end_of_instruction_cleanup { | 118 action end_of_instruction_cleanup { |
127 /* Mark start of this instruction as a valid target for jump. */ | 119 /* Mark start of this instruction as a valid target for jump. */ |
128 MarkValidJumpTarget(instruction_begin - data, valid_targets); | 120 MarkValidJumpTarget(instruction_begin - data, valid_targets); |
129 | 121 |
130 /* Call user-supplied callback. */ | 122 /* Call user-supplied callback. */ |
131 instruction_end = current_position + 1; | 123 instruction_end = current_position + 1; |
132 if ((instruction_info_collected & VALIDATION_ERRORS_MASK) || | 124 if ((instruction_info_collected & VALIDATION_ERRORS_MASK) || |
133 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { | 125 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { |
134 result &= user_callback(instruction_begin, instruction_end, | 126 result &= user_callback(instruction_begin, instruction_end, |
135 instruction_info_collected, callback_data); | 127 instruction_info_collected, callback_data); |
136 } | 128 } |
137 | 129 |
138 /* On successful match the instruction_begin must point to the next byte | 130 /* |
139 * to be able to report the new offset as the start of instruction | 131 * We may set instruction_begin at the first byte of the instruction instead |
140 * causing error. */ | 132 * of here but in the case of incorrect one byte instructions user callback |
133 * may be called before instruction_begin is set. | |
134 */ | |
141 instruction_begin = instruction_end; | 135 instruction_begin = instruction_end; |
142 | 136 |
143 /* Clear variables (well, one variable currently). */ | 137 /* Clear variables (well, one variable currently). */ |
144 instruction_info_collected = 0; | 138 instruction_info_collected = 0; |
145 } | 139 } |
146 | 140 |
147 # This action reports fatal error detected by DFA. | 141 # This action reports fatal error detected by DFA. |
148 action report_fatal_error { | 142 action report_fatal_error { |
149 result &= user_callback(instruction_begin, current_position, | 143 result &= user_callback(instruction_begin, current_position, |
150 UNRECOGNIZED_INSTRUCTION, callback_data); | 144 UNRECOGNIZED_INSTRUCTION, callback_data); |
151 /* | 145 /* |
152 * Process the next bundle: "continue" here is for the "for" cycle in | 146 * Process the next bundle: "continue" here is for the "for" cycle in |
153 * the ValidateChunkIA32 function. | 147 * the ValidateChunkIA32 function. |
154 * | 148 * |
155 * It does not affect the case which we really care about (when code | 149 * It does not affect the case which we really care about (when code |
156 * is validatable), but makes it possible to detect more errors in one | 150 * is validatable), but makes it possible to detect more errors in one |
157 * run in tools like ncval. | 151 * run in tools like ncval. |
158 */ | 152 */ |
159 continue; | 153 continue; |
160 } | 154 } |
161 | 155 |
162 # This is main ragel machine: it does 99% of validation work. There are only | 156 # This is main ragel machine: it does 99% of validation work. There are only |
163 # one thing to do if this machine accepts the bundles - check that direct | 157 # one thing to do if this ragel machine accepts the bundles - check that |
164 # jumps are correct. This is done in the following way: | 158 # direct jumps are correct. This is done in the following way: |
165 # * DFA fills two arrays: valid_targets and jump_dests. | 159 # * DFA fills two arrays: valid_targets and jump_dests. |
166 # * ProcessInvalidJumpTargets checks that "jump_dests & !valid_targets == 0". | 160 # * ProcessInvalidJumpTargets checks that "jump_dests & !valid_targets == 0". |
167 # All other checks are done here. | 161 # All other checks are done here. |
168 main := ((call_alignment | one_instruction | special_instruction) | 162 main := ((call_alignment | one_instruction | special_instruction) |
169 @end_of_instruction_cleanup)* | 163 @end_of_instruction_cleanup)* |
170 $!report_fatal_error; | 164 $!report_fatal_error; |
171 | 165 |
172 }%% | 166 }%% |
173 | 167 |
168 /* | |
169 * The "write data" statement causes Ragel to emit the constant static data | |
170 * needed by the ragel machine. | |
171 */ | |
174 %% write data; | 172 %% write data; |
175 | 173 |
176 | |
177 Bool ValidateChunkIA32(const uint8_t *data, size_t size, | 174 Bool ValidateChunkIA32(const uint8_t *data, size_t size, |
178 uint32_t options, | 175 uint32_t options, |
179 const NaClCPUFeaturesX86 *cpu_features, | 176 const NaClCPUFeaturesX86 *cpu_features, |
180 ValidationCallbackFunc user_callback, | 177 ValidationCallbackFunc user_callback, |
181 void *callback_data) { | 178 void *callback_data) { |
182 bitmap_word valid_targets_small; | 179 bitmap_word valid_targets_small; |
183 bitmap_word jump_dests_small; | 180 bitmap_word jump_dests_small; |
184 bitmap_word *valid_targets; | 181 bitmap_word *valid_targets; |
185 bitmap_word *jump_dests; | 182 bitmap_word *jump_dests; |
186 const uint8_t *current_position; | 183 const uint8_t *current_position; |
(...skipping 16 matching lines...) Expand all Loading... | |
203 free(jump_dests); | 200 free(jump_dests); |
204 free(valid_targets); | 201 free(valid_targets); |
205 errno = ENOMEM; | 202 errno = ENOMEM; |
206 return FALSE; | 203 return FALSE; |
207 } | 204 } |
208 } | 205 } |
209 | 206 |
210 /* | 207 /* |
211 * This option is usually used in tests: we will process the whole chunk | 208 * This option is usually used in tests: we will process the whole chunk |
212 * in one pass. Usually each bundle is processed separately which means | 209 * in one pass. Usually each bundle is processed separately which means |
213 * instructions (and super-instructions) can not cross borders of the bundle. | 210 * instructions (and "superinstructions") can not cross borders of the bundle. |
214 */ | 211 */ |
215 if (options & PROCESS_CHUNK_AS_A_CONTIGUOUS_STREAM) | 212 if (options & PROCESS_CHUNK_AS_A_CONTIGUOUS_STREAM) |
216 end_of_bundle = data + size; | 213 end_of_bundle = data + size; |
217 else | 214 else |
218 end_of_bundle = data + kBundleSize; | 215 end_of_bundle = data + kBundleSize; |
219 | 216 |
220 /* | 217 /* |
221 * Main loop. Here we process the data array bundle-after-bundle. | 218 * Main loop. Here we process the data array bundle-after-bundle. |
222 * Ragel-produced DFA does all the checks with one exception: direct jumps. | 219 * Ragel-produced DFA does all the checks with one exception: direct jumps. |
223 * It collects the two arrays: valid_targets and jump_dests which are used | 220 * It collects the two arrays: valid_targets and jump_dests which are used |
224 * to test direct jumps later. | 221 * to test direct jumps later. |
225 */ | 222 */ |
226 for (current_position = data; | 223 for (current_position = data; |
227 current_position < data + size; | 224 current_position < data + size; |
228 current_position = end_of_bundle, | 225 current_position = end_of_bundle, |
229 end_of_bundle = current_position + kBundleSize) { | 226 end_of_bundle = current_position + kBundleSize) { |
230 /* Start of the instruction being processed. */ | 227 /* Start of the instruction being processed. */ |
231 const uint8_t *instruction_begin = current_position; | 228 const uint8_t *instruction_begin = current_position; |
232 /* Only used locally in the end_of_instruction_cleanup action. */ | 229 /* Only used locally in the end_of_instruction_cleanup action. */ |
233 const uint8_t *instruction_end; | 230 const uint8_t *instruction_end; |
234 uint32_t instruction_info_collected = 0; | 231 uint32_t instruction_info_collected = 0; |
235 int current_state; | 232 int current_state; |
236 | 233 |
234 /* | |
235 * The "write init" statement causes Ragel to emit initialization code. | |
236 * This should be executed once before the ragel machine is started. | |
237 */ | |
237 %% write init; | 238 %% write init; |
239 /* | |
240 * The "write exec" statement causes Ragel to emit the ragel machine's | |
241 * execution code. | |
242 */ | |
238 %% write exec; | 243 %% write exec; |
239 } | 244 } |
240 | 245 |
241 /* | 246 /* |
242 * Check the direct jumps. All the targets from jump_dests must be in | 247 * Check the direct jumps. All the targets from jump_dests must be in |
243 * valid_targets. | 248 * valid_targets. |
244 */ | 249 */ |
245 result &= ProcessInvalidJumpTargets(data, size, valid_targets, jump_dests, | 250 result &= ProcessInvalidJumpTargets(data, size, valid_targets, jump_dests, |
246 user_callback, callback_data); | 251 user_callback, callback_data); |
247 | 252 |
248 /* We only use malloc for a large code sequences */ | 253 /* We only use malloc for a large code sequences */ |
249 if (jump_dests != &jump_dests_small) free(jump_dests); | 254 if (jump_dests != &jump_dests_small) free(jump_dests); |
250 if (valid_targets != &valid_targets_small) free(valid_targets); | 255 if (valid_targets != &valid_targets_small) free(valid_targets); |
251 if (!result) errno = EINVAL; | 256 if (!result) errno = EINVAL; |
252 return result; | 257 return result; |
253 } | 258 } |
OLD | NEW |