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

Side by Side Diff: src/trusted/validator_ragel/unreviewed/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
(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 * This is the core of ia32-mode validator. Please note that this file
9 * combines ragel machine description and C language actions. Please read
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
12 * "@{}" or "REX_WRX?" mean, etc.
13 */
14
15 #include <assert.h>
16 #include <errno.h>
17 #include <stddef.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "native_client/src/trusted/validator_ragel/bitmap.h"
23 #include "native_client/src/trusted/validator_ragel/unreviewed/validator_interna l.h"
24
25 /* Ignore this information: it's not used by security model in IA32 mode. */
26 #undef GET_VEX_PREFIX3
27 #define GET_VEX_PREFIX3 0
28 #undef SET_VEX_PREFIX3
29 #define SET_VEX_PREFIX3(P)
30
31 %%{
32 machine x86_32_validator;
33 alphtype unsigned char;
34 variable p current_position;
35 variable pe end_of_bundle;
36 variable eof end_of_bundle;
37 variable cs current_state;
38
39 include byte_machine "byte_machines.rl";
40
41 include prefix_actions
42 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
43 include prefixes_parsing_noaction
44 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
45 include vex_actions_ia32
46 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
47 include vex_parsing_ia32
48 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
49 include displacement_fields_actions
50 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
51 include displacement_fields_parsing
52 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
53 include modrm_parsing_ia32_noactions
54 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
55 include immediate_fields_actions
56 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
57 include immediate_fields_parsing_ia32
58 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
59 include relative_fields_validator_actions
60 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
61 include relative_fields_parsing
62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
63 include cpuid_actions
64 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl";
65
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
68 # decode_x86_32 machine in [autogenerated] "validator_x86_32_instruction.rl"
69 # file.
70 action last_byte_is_not_immediate {
71 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE;
72 }
73
74 include decode_x86_32 "validator_x86_32_instruction.rl";
75
76 special_instruction =
77 # and $~0x1f, %eXX call %eXX
78 # vvvvvvvvvv
79 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax
80 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx
81 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx
82 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx
83 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp
84 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp
85 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi
86 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi
87 # ^^^^ ^^^^
88 # and $~0x1f, %eXX jmp %eXX
89 @{
90 UnmarkValidJumpTarget((current_position - data) - 1, valid_targets);
91 instruction_begin -= 3;
92 instruction_info_collected |= SPECIAL_INSTRUCTION;
93 } |
94 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax
95 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d)
96 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg
97
98 # Check if call is properly aligned
99 #
100 # For direct call we explicitly encode all variations. For indirect call
101 # we accept all the special instructions which ends with indirect call.
102 call_alignment =
103 ((one_instruction &
104 # Direct call
105 ((data16 0xe8 rel16) |
106 (0xe8 rel32))) |
107 (special_instruction &
108 # Indirect call
109 (any* data16? 0xff ((opcode_2 | opcode_3) any* &
110 (modrm_memory | modrm_registers)))))
111 # Call instruction must aligned to the end of bundle. Previously this was
112 # strict requirement, today it's just warning to aid with debugging.
113 @{
114 if (((current_position - data) & kBundleMask) != kBundleMask)
115 instruction_info_collected |= BAD_CALL_ALIGNMENT;
116 };
117
118 # This action calls user's callback (if needed) and cleans up validator's
119 # internal state.
120 #
121 # We call the user callback if there are validation errors or if the
122 # CALL_USER_CALLBACK_ON_EACH_INSTRUCTION option is used.
123 #
124 # After that we move instruction_begin and clean all the variables which
125 # only used in the processing of a single instruction (prefixes, operand
126 # states and instruction_info_collected).
127 action end_of_instruction_cleanup {
128 /* Mark start of this instruction as a valid target for jump. */
129 MarkValidJumpTarget(instruction_begin - data, valid_targets);
130
131 /* Call user-supplied callback. */
132 instruction_end = current_position + 1;
133 if ((instruction_info_collected & VALIDATION_ERRORS_MASK) ||
134 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) {
135 result &= user_callback(instruction_begin, instruction_end,
136 instruction_info_collected, callback_data);
137 }
138
139 /* On successful match the instruction_begin must point to the next byte
140 * to be able to report the new offset as the start of instruction
141 * causing error. */
142 instruction_begin = instruction_end;
143
144 /* Clear variables (well, one variable currently). */
145 instruction_info_collected = 0;
146 }
147
148 # This action reports fatal error detected by DFA.
149 action report_fatal_error {
150 result &= user_callback(instruction_begin, current_position,
151 UNRECOGNIZED_INSTRUCTION, callback_data);
152 /*
153 * Process the next bundle: "continue" here is for the "for" cycle in
154 * the ValidateChunkIA32 function.
155 *
156 * 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 * run in tools like ncval.
159 */
160 continue;
161 }
162
163 # 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 # jumps are correct. This is done in the following way:
166 # * DFA fills two arrays: valid_targets and jump_dests.
167 # * ProcessInvalidJumpTargets checks that "jump_dests & !valid_targets == 0".
168 # All other checks are done here.
169 main := ((call_alignment | one_instruction | special_instruction)
170 @end_of_instruction_cleanup)*
171 $!report_fatal_error;
172
173 }%%
174
175 %% write data;
176
177
178 Bool ValidateChunkIA32(const uint8_t *data, size_t size,
179 uint32_t options,
180 const NaClCPUFeaturesX86 *cpu_features,
181 ValidationCallbackFunc user_callback,
182 void *callback_data) {
183 bitmap_word valid_targets_small;
184 bitmap_word jump_dests_small;
185 bitmap_word *valid_targets;
186 bitmap_word *jump_dests;
187 const uint8_t *current_position;
188 const uint8_t *end_of_bundle;
189 int result = TRUE;
190
191 CHECK(sizeof valid_targets_small == sizeof jump_dests_small);
192 CHECK(size % kBundleSize == 0);
193
194 /* For a very small sequences (one bundle) malloc is too expensive. */
195 if (size <= (sizeof valid_targets_small * 8)) {
196 valid_targets_small = 0;
197 valid_targets = &valid_targets_small;
198 jump_dests_small = 0;
199 jump_dests = &jump_dests_small;
200 } else {
201 valid_targets = BitmapAllocate(size);
202 jump_dests = BitmapAllocate(size);
203 if (!valid_targets || !jump_dests) {
204 free(jump_dests);
205 free(valid_targets);
206 errno = ENOMEM;
207 return FALSE;
208 }
209 }
210
211 /*
212 * 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
214 * instructions (and super-instructions) can not cross borders of the bundle.
215 */
216 if (options & PROCESS_CHUNK_AS_A_CONTIGUOUS_STREAM)
217 end_of_bundle = data + size;
218 else
219 end_of_bundle = data + kBundleSize;
220
221 /*
222 * Main loop. Here we process the data array bundle-after-bundle.
223 * 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
225 * to test direct jumps later.
226 */
227 for (current_position = data;
228 current_position < data + size;
229 current_position = end_of_bundle,
230 end_of_bundle = current_position + kBundleSize) {
231 /* Start of the instruction being processed. */
232 const uint8_t *instruction_begin = current_position;
233 /* Only used locally in the end_of_instruction_cleanup action. */
234 const uint8_t *instruction_end;
235 uint32_t instruction_info_collected = 0;
236 int current_state;
237
238 %% write init;
239 %% write exec;
240 }
241
242 /*
243 * Check the direct jumps. All the targets from jump_dests must be in
244 * valid_targets.
245 */
246 result &= ProcessInvalidJumpTargets(data, size, valid_targets, jump_dests,
247 user_callback, callback_data);
248
249 /* We only use malloc for a large code sequences */
250 if (jump_dests != &jump_dests_small) free(jump_dests);
251 if (valid_targets != &valid_targets_small) free(valid_targets);
252 if (!result) errno = EINVAL;
253 return result;
254 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698