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 /* | |
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 | |
7 #include <assert.h> | 15 #include <assert.h> |
8 #include <errno.h> | 16 #include <errno.h> |
9 #include <stddef.h> | 17 #include <stddef.h> |
10 #include <stdio.h> | 18 #include <stdio.h> |
11 #include <stdlib.h> | 19 #include <stdlib.h> |
12 #include <string.h> | 20 #include <string.h> |
13 | 21 |
14 #include "native_client/src/trusted/validator_ragel/unreviewed/validator_interna l.h" | 22 #include "native_client/src/trusted/validator_ragel/validator_internal.h" |
15 | 23 |
16 /* Ignore this information: it's not used by security model in IA32 mode. */ | 24 /* Ignore this information: it's not used by security model in IA32 mode. */ |
17 #undef GET_VEX_PREFIX3 | 25 #undef GET_VEX_PREFIX3 |
18 #define GET_VEX_PREFIX3 0 | 26 #define GET_VEX_PREFIX3 0 |
19 #undef SET_VEX_PREFIX3 | 27 #undef SET_VEX_PREFIX3 |
20 #define SET_VEX_PREFIX3(P) | 28 #define SET_VEX_PREFIX3(P) |
21 | 29 |
22 %%{ | 30 %%{ |
23 machine x86_32_validator; | 31 machine x86_32_validator; |
24 alphtype unsigned char; | 32 alphtype unsigned char; |
(...skipping 19 matching lines...) Expand all Loading... | |
44 include modrm_parsing_ia32_noactions | 52 include modrm_parsing_ia32_noactions |
45 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 53 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
46 include immediate_fields_actions | 54 include immediate_fields_actions |
47 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 55 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
48 include immediate_fields_parsing_ia32 | 56 include immediate_fields_parsing_ia32 |
49 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 57 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
50 action rel8_operand { | 58 action rel8_operand { |
51 rel8_operand(current_position + 1, data, jump_dests, size, | 59 rel8_operand(current_position + 1, data, jump_dests, size, |
52 &instruction_info_collected); | 60 &instruction_info_collected); |
53 } | 61 } |
54 action rel16_operand { | 62 action rel16_operand { |
Brad Chen
2012/10/05 16:47:21
Can you add comments to remind readers what kind o
khim
2012/10/15 16:38:57
I'm not so sure. I doubt you can ever understand t
Brad Chen
2012/10/16 00:16:55
I'm not sure either but I'd like you to try. Even
| |
55 #error rel16_operand should never be used in nacl | 63 #error rel16_operand should never be used in nacl |
56 } | 64 } |
57 action rel32_operand { | 65 action rel32_operand { |
58 rel32_operand(current_position + 1, data, jump_dests, size, | 66 rel32_operand(current_position + 1, data, jump_dests, size, |
59 &instruction_info_collected); | 67 &instruction_info_collected); |
60 } | 68 } |
61 include relative_fields_parsing | 69 include relative_fields_parsing |
62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 70 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
63 include cpuid_actions | 71 include cpuid_actions |
64 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 72 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
65 | 73 |
66 action last_byte_is_not_immediate { | 74 action last_byte_is_not_immediate { |
67 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; | 75 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; |
68 } | 76 } |
69 | 77 |
70 include decode_x86_32 "validator_x86_32_instruction.rl"; | 78 include decode_x86_32 "validator_x86_32_instruction.rl"; |
71 | 79 |
72 special_instruction = | 80 special_instruction = |
73 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax | 81 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax |
74 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx | 82 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx |
75 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx | 83 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx |
76 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx | 84 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx |
77 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp | 85 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp |
78 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp | 86 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp |
79 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi | 87 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi |
80 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi | 88 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi |
Brad Chen
2012/10/05 16:47:21
Note: This is a case where repetition seems okay t
khim
2012/10/15 16:38:57
Even so: I've added one minor change to separate a
| |
81 @{ | 89 @{ |
82 BitmapClearBit(valid_targets, (current_position - data) - 1); | 90 MakeInvalidJumpTarget((current_position - data) - 1, valid_targets); |
83 instruction_start -= 3; | 91 instruction_start -= 3; |
84 instruction_info_collected |= SPECIAL_INSTRUCTION; | 92 instruction_info_collected |= SPECIAL_INSTRUCTION; |
85 } | | 93 } | |
86 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax | 94 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax |
87 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) | 95 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) |
88 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg | 96 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg |
89 | 97 |
90 # Check if call is properly aligned | 98 # Check if call is properly aligned |
91 call_alignment = | 99 call_alignment = |
92 ((one_instruction & | 100 ((one_instruction & |
93 # Direct call | 101 # Direct call |
94 ((data16 0xe8 rel16) | | 102 ((data16 0xe8 rel16) | |
Brad Chen
2012/10/05 16:47:21
This looks like this file would benefit from the s
khim
2012/10/15 16:38:57
All the pieces in this file have analogues in vali
Brad Chen
2012/10/16 00:16:55
While what you state is true, it doesn't change th
| |
95 (0xe8 rel32))) | | 103 (0xe8 rel32))) | |
96 (special_instruction & | 104 (special_instruction & |
97 # Indirect call | 105 # Indirect call |
98 (any* data16? 0xff ((opcode_2 | opcode_3) any* & | 106 (any* data16? 0xff ((opcode_2 | opcode_3) any* & |
99 (modrm_memory | modrm_registers))))) | 107 (modrm_memory | modrm_registers))))) |
100 @{ | 108 @{ |
101 if (((current_position - data) & kBundleMask) != kBundleMask) | 109 if (((current_position - data) & kBundleMask) != kBundleMask) |
102 instruction_info_collected |= BAD_CALL_ALIGNMENT; | 110 instruction_info_collected |= BAD_CALL_ALIGNMENT; |
103 }; | 111 }; |
104 | 112 |
105 main := ((call_alignment | one_instruction | special_instruction) | 113 main := ((call_alignment | one_instruction | special_instruction) |
106 >{ | 114 >{ |
107 BitmapSetBit(valid_targets, current_position - data); | 115 MakeJumpTargetValid(current_position - data, valid_targets); |
108 } | 116 } |
109 @{ | 117 @{ |
110 if ((instruction_info_collected & | 118 if ((instruction_info_collected & |
111 (VALIDATION_ERRORS_MASK | BAD_CALL_ALIGNMENT)) || | 119 (VALIDATION_ERRORS_MASK | BAD_CALL_ALIGNMENT)) || |
Brad Chen
2012/10/05 16:47:21
This doesn't look like standard indentation. Boole
khim
2012/10/15 16:38:57
This was the goal :-(
But this is moot point sinc
| |
112 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { | 120 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { |
113 result &= user_callback(instruction_start, current_position, | 121 result &= user_callback(instruction_start, current_position, |
114 instruction_info_collected, callback_data); | 122 instruction_info_collected, callback_data); |
115 } | 123 } |
116 /* On successful match the instruction start must point to the next byte | 124 /* On successful match the instruction start must point to the next byte |
117 * to be able to report the new offset as the start of instruction | 125 * to be able to report the new offset as the start of instruction |
118 * causing error. */ | 126 * causing error. */ |
119 instruction_start = current_position + 1; | 127 instruction_start = current_position + 1; |
120 instruction_info_collected = 0; | 128 instruction_info_collected = 0; |
121 })* | 129 })* |
122 $err{ | 130 $err{ |
123 result &= user_callback(instruction_start, current_position, | 131 result &= user_callback(instruction_start, current_position, |
124 UNRECOGNIZED_INSTRUCTION, callback_data); | 132 UNRECOGNIZED_INSTRUCTION, callback_data); |
125 continue; | 133 continue; |
126 }; | 134 }; |
127 | 135 |
128 }%% | 136 }%% |
129 | 137 |
130 %% write data; | 138 %% write data; |
131 | 139 |
132 | 140 |
133 Bool ValidateChunkIA32(const uint8_t *data, size_t size, | 141 Bool ValidateChunkIA32(const uint8_t *data, size_t size, |
134 enum validation_options options, | 142 enum validation_options options, |
135 const NaClCPUFeaturesX86 *cpu_features, | 143 const NaClCPUFeaturesX86 *cpu_features, |
136 validation_callback_func user_callback, | 144 ValidationCallbackFunc user_callback, |
137 void *callback_data) { | 145 void *callback_data) { |
138 bitmap_word valid_targets_small; | 146 bitmap_word valid_targets_small; |
139 bitmap_word jump_dests_small; | 147 bitmap_word jump_dests_small; |
140 bitmap_word *valid_targets; | 148 bitmap_word *valid_targets; |
141 bitmap_word *jump_dests; | 149 bitmap_word *jump_dests; |
142 const uint8_t *current_position; | 150 const uint8_t *current_position; |
143 const uint8_t *end_of_bundle; | 151 const uint8_t *end_of_bundle; |
144 int result = TRUE; | 152 int result = TRUE; |
145 | 153 |
146 CHECK(sizeof valid_targets_small == sizeof jump_dests_small); | 154 CHECK(sizeof valid_targets_small == sizeof jump_dests_small); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 user_callback, callback_data); | 193 user_callback, callback_data); |
186 | 194 |
187 /* We only use malloc for a large code sequences */ | 195 /* We only use malloc for a large code sequences */ |
188 if (size > sizeof valid_targets_small) { | 196 if (size > sizeof valid_targets_small) { |
189 free(jump_dests); | 197 free(jump_dests); |
190 free(valid_targets); | 198 free(valid_targets); |
191 } | 199 } |
192 if (!result) errno = EINVAL; | 200 if (!result) errno = EINVAL; |
193 return result; | 201 return result; |
194 } | 202 } |
OLD | NEW |