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

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

Powered by Google App Engine
This is Rietveld 408576698