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 15 matching lines...) Expand all Loading... | |
40 include displacement_fields_actions | 48 include displacement_fields_actions |
41 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 49 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
42 include displacement_fields_parsing | 50 include displacement_fields_parsing |
43 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 51 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
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"; |
58 # rel8 actions are used in relative jumps with 8-bit offset. | |
50 action rel8_operand { | 59 action rel8_operand { |
51 rel8_operand(current_position + 1, data, jump_dests, size, | 60 rel8_operand(current_position + 1, data, jump_dests, size, |
52 &instruction_info_collected); | 61 &instruction_info_collected); |
53 } | 62 } |
63 # rel16 actions are used in relative jums with 16-bit offset. | |
64 # | |
65 # Such instructions should be included in the validator's DFA, but we can not | |
66 # just exlude them because they are refenced in relative_fields_parsing ragel | |
67 # machine. Ensure compilations error in case of usage. | |
54 action rel16_operand { | 68 action rel16_operand { |
55 #error rel16_operand should never be used in nacl | 69 #error rel16_operand should never be used in nacl |
56 } | 70 } |
71 # rel32 actions are used in relative calls and jumps with 32-bit offset. | |
57 action rel32_operand { | 72 action rel32_operand { |
58 rel32_operand(current_position + 1, data, jump_dests, size, | 73 rel32_operand(current_position + 1, data, jump_dests, size, |
59 &instruction_info_collected); | 74 &instruction_info_collected); |
60 } | 75 } |
61 include relative_fields_parsing | 76 include relative_fields_parsing |
62 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 77 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
63 include cpuid_actions | 78 include cpuid_actions |
64 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; | 79 "native_client/src/trusted/validator_ragel/unreviewed/parse_instruction.rl"; |
65 | 80 |
81 # Action which marks last byte as not immediate. Most 3DNow! instructions, | |
82 # some AVX and XOP instructions have this proerty. It's referenced by | |
83 # decode_x86_32 machine in [autogenerated] “validator_x86_32_instruction.rl” | |
84 # file. | |
66 action last_byte_is_not_immediate { | 85 action last_byte_is_not_immediate { |
67 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; | 86 instruction_info_collected |= LAST_BYTE_IS_NOT_IMMEDIATE; |
68 } | 87 } |
69 | 88 |
70 include decode_x86_32 "validator_x86_32_instruction.rl"; | 89 include decode_x86_32 "validator_x86_32_instruction.rl"; |
71 | 90 |
72 special_instruction = | 91 special_instruction = |
73 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax | 92 # and $~0x1f, %eXX call %eXX |
74 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx | 93 # ↓↓↓↓↓↓↓↓↓↓ |
Brad Chen
2012/10/22 21:29:05
What character are you using to generate the up an
khim
2013/03/08 17:59:53
Done.
| |
75 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx | 94 (0x83 0xe0 0xe0 0xff (0xd0|0xe0) | # naclcall/jmp %eax |
76 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx | 95 0x83 0xe1 0xe0 0xff (0xd1|0xe1) | # naclcall/jmp %ecx |
77 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp | 96 0x83 0xe2 0xe0 0xff (0xd2|0xe2) | # naclcall/jmp %edx |
78 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp | 97 0x83 0xe3 0xe0 0xff (0xd3|0xe3) | # naclcall/jmp %ebx |
79 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi | 98 0x83 0xe4 0xe0 0xff (0xd4|0xe4) | # naclcall/jmp %esp |
80 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi | 99 0x83 0xe5 0xe0 0xff (0xd5|0xe5) | # naclcall/jmp %ebp |
100 0x83 0xe6 0xe0 0xff (0xd6|0xe6) | # naclcall/jmp %esi | |
101 0x83 0xe7 0xe0 0xff (0xd7|0xe7)) # naclcall/jmp %edi | |
102 # ↑↑↑↑ ↑↑↑↑ | |
103 # and $~0x1f, %eXX jmp %eXX | |
81 @{ | 104 @{ |
82 BitmapClearBit(valid_targets, (current_position - data) - 1); | 105 MakeInvalidJumpTarget((current_position - data) - 1, valid_targets); |
83 instruction_start -= 3; | 106 instruction_start -= 3; |
84 instruction_info_collected |= SPECIAL_INSTRUCTION; | 107 instruction_info_collected |= SPECIAL_INSTRUCTION; |
85 } | | 108 } | |
86 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax | 109 (0x65 0xa1 (0x00|0x04) 0x00 0x00 0x00 | # mov %gs:0x0/0x4,%eax |
87 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) | 110 0x65 0x8b (0x05|0x0d|0x015|0x1d|0x25|0x2d|0x35|0x3d) |
88 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg | 111 (0x00|0x04) 0x00 0x00 0x00); # mov %gs:0x0/0x4,%reg |
89 | 112 |
90 # Check if call is properly aligned | 113 # Check if call is properly aligned |
114 # | |
115 # For direct call we explicitly encode all variations. For indirect call | |
116 # we accept all the special instructions which ends with indirect call. | |
91 call_alignment = | 117 call_alignment = |
92 ((one_instruction & | 118 ((one_instruction & |
93 # Direct call | 119 # Direct call |
94 ((data16 0xe8 rel16) | | 120 ((data16 0xe8 rel16) | |
95 (0xe8 rel32))) | | 121 (0xe8 rel32))) | |
96 (special_instruction & | 122 (special_instruction & |
97 # Indirect call | 123 # Indirect call |
98 (any* data16? 0xff ((opcode_2 | opcode_3) any* & | 124 (any* data16? 0xff ((opcode_2 | opcode_3) any* & |
99 (modrm_memory | modrm_registers))))) | 125 (modrm_memory | modrm_registers))))) |
126 # Call instruction must aligned to the end of bundle. Previously this was | |
127 # strict requirement, today it's just warning to aid with debugging. | |
100 @{ | 128 @{ |
101 if (((current_position - data) & kBundleMask) != kBundleMask) | 129 if (((current_position - data) & kBundleMask) != kBundleMask) |
102 instruction_info_collected |= BAD_CALL_ALIGNMENT; | 130 instruction_info_collected |= BAD_CALL_ALIGNMENT; |
103 }; | 131 }; |
104 | 132 |
105 main := ((call_alignment | one_instruction | special_instruction) | 133 main := ((call_alignment | one_instruction | special_instruction) |
134 # Beginning of the instruction is always valid target for jump. If this | |
135 # instruction is, in fact, part of the superinstruction then we'll clear | |
136 # that bit later. | |
106 >{ | 137 >{ |
107 BitmapSetBit(valid_targets, current_position - data); | 138 MakeJumpTargetValid(current_position - data, valid_targets); |
108 } | 139 } |
140 # Here we call the user callback if there are validation errors or if the | |
141 # CALL_USER_CALLBACK_ON_EACH_INSTRUCTION option is used. | |
142 # | |
143 # After that we move instruction_start and clean all the variables which | |
144 # only used in the processing of a single instruction (prefixes, operand | |
145 # states and instruction_info_collected). | |
109 @{ | 146 @{ |
110 if ((instruction_info_collected & VALIDATION_ERRORS_MASK) || | 147 if ((instruction_info_collected & VALIDATION_ERRORS_MASK) || |
111 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { | 148 (options & CALL_USER_CALLBACK_ON_EACH_INSTRUCTION)) { |
112 result &= user_callback(instruction_start, current_position, | 149 result &= user_callback(instruction_start, current_position, |
113 instruction_info_collected, callback_data); | 150 instruction_info_collected, callback_data); |
114 } | 151 } |
115 /* On successful match the instruction start must point to the next byte | 152 /* On successful match the instruction start must point to the next byte |
116 * to be able to report the new offset as the start of instruction | 153 * to be able to report the new offset as the start of instruction |
117 * causing error. */ | 154 * causing error. */ |
118 instruction_start = current_position + 1; | 155 instruction_start = current_position + 1; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 user_callback, callback_data); | 221 user_callback, callback_data); |
185 | 222 |
186 /* We only use malloc for a large code sequences */ | 223 /* We only use malloc for a large code sequences */ |
187 if (size > sizeof valid_targets_small) { | 224 if (size > sizeof valid_targets_small) { |
188 free(jump_dests); | 225 free(jump_dests); |
189 free(valid_targets); | 226 free(valid_targets); |
190 } | 227 } |
191 if (!result) errno = EINVAL; | 228 if (!result) errno = EINVAL; |
192 return result; | 229 return result; |
193 } | 230 } |
OLD | NEW |