Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: src/trusted/validator_ragel/validator_x86_32.rl

Issue 11000033: Move validator_x86_XX.rl out of unreviewed. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #undef GET_VEX_PREFIX3 26 #undef GET_VEX_PREFIX3
27 #define GET_VEX_PREFIX3 0 27 #define GET_VEX_PREFIX3 0
28 #undef SET_VEX_PREFIX3 28 #undef SET_VEX_PREFIX3
29 #define SET_VEX_PREFIX3(P) 29 #define SET_VEX_PREFIX3(P)
30 30
31 %%{ 31 %%{
32 machine x86_32_validator; 32 machine x86_32_validator;
33 alphtype unsigned char; 33 alphtype unsigned char;
(...skipping 23 matching lines...) Expand all
57 include immediate_fields_parsing_ia32 57 include immediate_fields_parsing_ia32
58 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; 58 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
59 include relative_fields_validator_actions 59 include relative_fields_validator_actions
60 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; 60 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
61 include relative_fields_parsing 61 include relative_fields_parsing
62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; 62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
63 include cpuid_actions 63 include cpuid_actions
64 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; 64 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
65 65
66 # Action which marks last byte as not immediate. Most 3DNow! instructions, 66 # Action which marks last byte as not immediate. Most 3DNow! instructions,
67 # some AVX and XOP instructions have this property. It's referenced by 67 # some AVX and XOP instructions have this property.
68 # decode_x86_32 machine in [autogenerated] "validator_x86_32_instruction.rl" 68 #
69 # file. 69 # This action is referenced by decode_x86_32 ragel machine in [autogenerated]
70 # "validator_x86_32_instruction.rl" file.
70 action last_byte_is_not_immediate { 71 action last_byte_is_not_immediate {
71 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; 72 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE;
72 } 73 }
73 74
74 include decode_x86_32 "validator_x86_32_instruction.rl"; 75 include decode_x86_32 "validator_x86_32_instruction.rl";
75 76
76 special_instruction = 77 special_instruction =
77 # and $~0x1f, %eXX call %eXX 78 # and $~0x1f, %eXX call %eXX
78 # vvvvvvvvvv 79 # vvvvvvvvvv
79 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax 80 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax
80 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx 81 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx
81 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx 82 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx
82 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx 83 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx
83 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp 84 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp
84 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp 85 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp
85 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi 86 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi
86 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi 87 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi
87 # ^^^^ ^^^^ 88 # ^^^^ ^^^^
88 # and $~0x1f, %eXX jmp %eXX 89 # and $~0x1f, %eXX jmp %eXX
89 @{ 90 @{
90 UnmarkValidJumpTarget((current_position - data) - 1, valid_targets); 91 UnmarkValidJumpTarget((current_position - data) - 1, valid_targets);
91 instruction_begin -= 3; 92 instruction_begin -= 3;
92 instruction_info_collected |= SPECIAL_INSTRUCTION; 93 instruction_info_collected |= SPECIAL_INSTRUCTION;
93 } | 94 } |
94 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax 95 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax
95 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) 96 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d)
96 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg 97 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg
97 98
98 # Check if call is properly aligned 99 # Ragel machine which checks if call is properly aligned.
99 # 100 #
100 # For direct call we explicitly encode all variations. For indirect call 101 # For direct call we explicitly encode all variations. For indirect call
101 # we accept all the special instructions which ends with indirect call. 102 # we accept all the special instructions which ends with indirect call.
102 call_alignment = 103 call_alignment =
103 ((one_instruction & 104 ((one_instruction &
104 # Direct call 105 # Direct call
105 ((data16 0xe8 rel16) | 106 ((data16 0xe8 rel16) |
106 (0xe8 rel32))) | 107 (0xe8 rel32))) |
107 (special_instruction & 108 (special_instruction &
108 # Indirect call 109 # Indirect call
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 * the ValidateChunkIA32 function. 155 * the ValidateChunkIA32 function.
155 * 156 *
156 * It does not affect the case which we really care about (when code 157 * It does not affect the case which we really care about (when code
157 * is validatable), but makes it possible to detect more errors in one 158 * is validatable), but makes it possible to detect more errors in one
158 * run in tools like ncval. 159 * run in tools like ncval.
159 */ 160 */
160 continue; 161 continue;
161 } 162 }
162 163
163 # This is main ragel machine: it does 99% of validation work. There are only 164 # This is main ragel machine: it does 99% of validation work. There are only
164 # one thing to do if this machine accepts the bundles - check that direct 165 # one thing to do if this ragel machine accepts the bundles - check that
165 # jumps are correct. This is done in the following way: 166 # direct jumps are correct. This is done in the following way:
166 # * DFA fills two arrays: valid_targets and jump_dests. 167 # * DFA fills two arrays: valid_targets and jump_dests.
167 # * ProcessInvalidJumpTargets checks that "jump_dests & !valid_targets == 0". 168 # * ProcessInvalidJumpTargets checks that "jump_dests & !valid_targets == 0".
168 # All other checks are done here. 169 # All other checks are done here.
169 main := ((call_alignment | one_instruction | special_instruction) 170 main := ((call_alignment | one_instruction | special_instruction)
170 @end_of_instruction_cleanup)* 171 @end_of_instruction_cleanup)*
171 $!report_fatal_error; 172 $!report_fatal_error;
172 173
173 }%% 174 }%%
174 175
176 /*
177 * The "write data" statement causes Ragel to emit the constant static data
178 * needed by the ragel machine.
179 */
175 %% write data; 180 %% write data;
176 181
177
178 Bool ValidateChunkIA32(const uint8_t *data, size_t size, 182 Bool ValidateChunkIA32(const uint8_t *data, size_t size,
179 uint32_t options, 183 uint32_t options,
180 const NaClCPUFeaturesX86 *cpu_features, 184 const NaClCPUFeaturesX86 *cpu_features,
181 ValidationCallbackFunc user_callback, 185 ValidationCallbackFunc user_callback,
182 void *callback_data) { 186 void *callback_data) {
183 bitmap_word valid_targets_small; 187 bitmap_word valid_targets_small;
184 bitmap_word jump_dests_small; 188 bitmap_word jump_dests_small;
185 bitmap_word *valid_targets; 189 bitmap_word *valid_targets;
186 bitmap_word *jump_dests; 190 bitmap_word *jump_dests;
187 const uint8_t *current_position; 191 const uint8_t *current_position;
(...skipping 16 matching lines...) Expand all
204 free(jump_dests); 208 free(jump_dests);
205 free(valid_targets); 209 free(valid_targets);
206 errno = ENOMEM; 210 errno = ENOMEM;
207 return FALSE; 211 return FALSE;
208 } 212 }
209 } 213 }
210 214
211 /* 215 /*
212 * This option is usually used in tests: we will process the whole chunk 216 * This option is usually used in tests: we will process the whole chunk
213 * in one pass. Usually each bundle is processed separately which means 217 * in one pass. Usually each bundle is processed separately which means
214 * instructions (and super-instructions) can not cross borders of the bundle. 218 * instructions (and "superinstructions") can not cross borders of the bundle.
215 */ 219 */
216 if (options & PROCESS_CHUNK_AS_A_CONTIGUOUS_STREAM) 220 if (options & PROCESS_CHUNK_AS_A_CONTIGUOUS_STREAM)
217 end_of_bundle = data + size; 221 end_of_bundle = data + size;
218 else 222 else
219 end_of_bundle = data + kBundleSize; 223 end_of_bundle = data + kBundleSize;
220 224
221 /* 225 /*
222 * Main loop. Here we process the data array bundle-after-bundle. 226 * Main loop. Here we process the data array bundle-after-bundle.
223 * Ragel-produced DFA does all the checks with one exception: direct jumps. 227 * Ragel-produced DFA does all the checks with one exception: direct jumps.
224 * It collects the two arrays: valid_targets and jump_dests which are used 228 * It collects the two arrays: valid_targets and jump_dests which are used
225 * to test direct jumps later. 229 * to test direct jumps later.
226 */ 230 */
227 for (current_position = data; 231 for (current_position = data;
228 current_position < data + size; 232 current_position < data + size;
229 current_position = end_of_bundle, 233 current_position = end_of_bundle,
230 end_of_bundle = current_position + kBundleSize) { 234 end_of_bundle = current_position + kBundleSize) {
231 /* Start of the instruction being processed. */ 235 /* Start of the instruction being processed. */
232 const uint8_t *instruction_begin = current_position; 236 const uint8_t *instruction_begin = current_position;
233 /* Only used locally in the end_of_instruction_cleanup action. */ 237 /* Only used locally in the end_of_instruction_cleanup action. */
234 const uint8_t *instruction_end; 238 const uint8_t *instruction_end;
235 uint32_t instruction_info_collected = 0; 239 uint32_t instruction_info_collected = 0;
236 int current_state; 240 int current_state;
237 241
242 /*
243 * The "write init" statement causes Ragel to emit initialization code.
244 * This should be executed once before the ragel machine is started.
245 */
238 %% write init; 246 %% write init;
247 /*
248 * The "write exec" statement causes Ragel to emit the ragel machine's
249 * execution code.
250 */
239 %% write exec; 251 %% write exec;
240 } 252 }
241 253
242 /* 254 /*
243 * Check the direct jumps. All the targets from jump_dests must be in 255 * Check the direct jumps. All the targets from jump_dests must be in
244 * valid_targets. 256 * valid_targets.
245 */ 257 */
246 result &= ProcessInvalidJumpTargets(data, size, valid_targets, jump_dests, 258 result &= ProcessInvalidJumpTargets(data, size, valid_targets, jump_dests,
247 user_callback, callback_data); 259 user_callback, callback_data);
248 260
249 /* We only use malloc for a large code sequences */ 261 /* We only use malloc for a large code sequences */
250 if (jump_dests != &jump_dests_small) free(jump_dests); 262 if (jump_dests != &jump_dests_small) free(jump_dests);
251 if (valid_targets != &valid_targets_small) free(valid_targets); 263 if (valid_targets != &valid_targets_small) free(valid_targets);
252 if (!result) errno = EINVAL; 264 if (!result) errno = EINVAL;
253 return result; 265 return result;
254 } 266 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698