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 /* Implement the functions common for ia32 and x86-64 architectures. */ | 7 /* Implement the functions common for ia32 and x86-64 architectures. */ |
8 #include "native_client/src/trusted/validator_ragel/dfa_validate_common.h" | 8 #include "native_client/src/trusted/validator_ragel/dfa_validate_common.h" |
9 | 9 |
10 #include <string.h> | 10 #include <string.h> |
11 | 11 |
12 #include "native_client/src/shared/platform/nacl_check.h" | 12 #include "native_client/src/shared/platform/nacl_check.h" |
13 #include "native_client/src/trusted/service_runtime/nacl_config.h" | 13 #include "native_client/src/trusted/service_runtime/nacl_config.h" |
14 #include "native_client/src/trusted/validator_ragel/validator.h" | 14 #include "native_client/src/trusted/validator_ragel/validator.h" |
15 #include "native_client/src/include/build_config.h" | |
15 | 16 |
16 /* Used as an argument to copy_func when unsupported instruction must be | 17 /* Used as an argument to copy_func when unsupported instruction must be |
17 replaced with HLTs. */ | 18 replaced with HLTs. */ |
18 static const uint8_t kStubOutMem[MAX_INSTRUCTION_LENGTH] = { | 19 static const uint8_t kStubOutMem[MAX_INSTRUCTION_LENGTH] = { |
19 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, | 20 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, |
20 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, | 21 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, |
21 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, | 22 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, |
22 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, | 23 NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, NACL_HALT_OPCODE, |
23 NACL_HALT_OPCODE | 24 NACL_HALT_OPCODE |
24 }; | 25 }; |
25 | 26 |
26 Bool NaClDfaProcessValidationError(const uint8_t *begin, const uint8_t *end, | 27 Bool NaClDfaProcessValidationError(const uint8_t *begin, const uint8_t *end, |
27 uint32_t info, void *callback_data) { | 28 uint32_t info, void *callback_data) { |
28 UNREFERENCED_PARAMETER(begin); | 29 UNREFERENCED_PARAMETER(begin); |
29 UNREFERENCED_PARAMETER(end); | 30 UNREFERENCED_PARAMETER(end); |
30 UNREFERENCED_PARAMETER(info); | 31 UNREFERENCED_PARAMETER(info); |
31 UNREFERENCED_PARAMETER(callback_data); | 32 UNREFERENCED_PARAMETER(callback_data); |
32 | 33 |
33 return FALSE; | 34 return FALSE; |
34 } | 35 } |
35 | 36 |
37 #if NACL_BUILD_SUBARCH == 64 | |
38 static Bool IsREX(uint8_t byte) { | |
39 return byte >= 0x40 && byte <= 0x4f; | |
40 } | |
41 #endif | |
42 | |
43 static Bool RewriteUnsupportedInstruction(const uint8_t *begin, | |
44 const uint8_t *end) { | |
45 uint8_t *ptr = (uint8_t *) begin; | |
46 /* We usually only check and rewrite the first few bytes without examining | |
47 * further because this function is only called when the validator tells us | |
48 * that it is an 'unsupported instruction' and there are no other validation | |
49 * failures. | |
50 */ | |
51 #if NACL_BUILD_SUBARCH == 32 | |
52 UNREFERENCED_PARAMETER(end); | |
53 if (memcmp(begin, "\x0f\xe7", 2) == 0) { | |
54 /* movntq => movq */ | |
55 ptr[1] = 0x7f; | |
56 return TRUE; | |
57 } else if (memcmp(begin, "\x66\x0f\xe7", 3) == 0) { | |
58 /* movntdq => movdqa */ | |
59 ptr[2] = 0x7f; | |
60 return TRUE; | |
61 } | |
62 #elif NACL_BUILD_SUBARCH == 64 | |
63 if (IsREX(begin[0])) { | |
64 if (memcmp(begin + 1, "\x0f\x2b", 2) == 0) { | |
Petr Hosek
2015/08/05 21:59:56
Couldn't you rewrite this using a switch statement
ruiq
2015/08/06 04:53:18
Done.
| |
65 /* movntps => movaps */ | |
66 ptr[2] = 0x29; | |
67 return TRUE; | |
68 } else if (memcmp(begin + 1, "\x0f\xc3", 2) == 0) { | |
69 /* movnti => mov */ | |
70 ptr[1] = 0x89; | |
71 memmove(ptr + 2, ptr + 3, end - begin - 3); | |
72 ptr[end - begin - 1] = 0x90; | |
73 return TRUE; | |
74 } else if (memcmp(begin + 1, "\x0f\x18", 2) == 0) { | |
75 /* prefetchnta => nop */ | |
76 memset(ptr, 0x90, end - begin); | |
77 return TRUE; | |
78 } | |
79 } else if (begin[0] == 0x66 && IsREX(begin[1]) && | |
80 memcmp(begin + 2, "\x0f\xe7", 2) == 0) { | |
81 /* movntdq => movdqa */ | |
82 ptr[3] = 0x7f; | |
83 return TRUE; | |
84 } | |
85 #endif | |
86 return FALSE; | |
87 } | |
88 | |
36 Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, | 89 Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, |
37 const uint8_t *end, | 90 const uint8_t *end, |
38 uint32_t info, | 91 uint32_t info, |
39 void *callback_data) { | 92 void *callback_data) { |
40 struct StubOutCallbackData *data = callback_data; | 93 struct StubOutCallbackData *data = callback_data; |
41 /* Stub-out instructions unsupported on this CPU, but valid on other CPUs. */ | 94 /* Stub-out instructions unsupported on this CPU, but valid on other CPUs. */ |
42 if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) { | 95 if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) { |
43 data->did_rewrite = 1; | 96 data->did_rewrite = 1; |
44 memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin); | 97 memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin); |
45 return TRUE; | 98 return TRUE; |
46 } else if ((info & VALIDATION_ERRORS_MASK) == UNSUPPORTED_INSTRUCTION) { | 99 } else if ((info & VALIDATION_ERRORS_MASK) == UNSUPPORTED_INSTRUCTION) { |
47 if (data->flags & NACL_DISABLE_NONTEMPORALS_X86) { | 100 if (data->flags & NACL_DISABLE_NONTEMPORALS_X86) { |
48 return FALSE; | 101 return FALSE; |
49 } else { | 102 } else { |
50 /* TODO(ruiq): rewrite instruction. For now, we keep the original | 103 if (RewriteUnsupportedInstruction(begin, end)) { |
Petr Hosek
2015/08/05 21:59:56
You could merge the nested if statement with the o
ruiq
2015/08/06 04:53:18
Done.
| |
51 * instruction and indicate validation success, which is consistent | 104 data->did_rewrite = 1; |
52 * with current validation results. */ | 105 return TRUE; |
53 data->did_rewrite = 0; | 106 } else { |
54 return TRUE; | 107 data->did_rewrite = 0; |
108 return FALSE; | |
109 } | |
55 } | 110 } |
56 } else { | 111 } else { |
57 return FALSE; | 112 return FALSE; |
58 } | 113 } |
59 } | 114 } |
60 | 115 |
61 Bool NaClDfaProcessCodeCopyInstruction(const uint8_t *begin_new, | 116 Bool NaClDfaProcessCodeCopyInstruction(const uint8_t *begin_new, |
62 const uint8_t *end_new, | 117 const uint8_t *end_new, |
63 uint32_t info_new, | 118 uint32_t info_new, |
64 void *callback_data) { | 119 void *callback_data) { |
(...skipping 13 matching lines...) Expand all Loading... | |
78 | 133 |
79 Bool NaClDfaCodeReplacementIsStubouted(const uint8_t *begin_existing, | 134 Bool NaClDfaCodeReplacementIsStubouted(const uint8_t *begin_existing, |
80 size_t instruction_length) { | 135 size_t instruction_length) { |
81 | 136 |
82 /* Unsupported instruction must have been replaced with HLTs. */ | 137 /* Unsupported instruction must have been replaced with HLTs. */ |
83 if (memcmp(kStubOutMem, begin_existing, instruction_length) == 0) | 138 if (memcmp(kStubOutMem, begin_existing, instruction_length) == 0) |
84 return TRUE; | 139 return TRUE; |
85 else | 140 else |
86 return FALSE; | 141 return FALSE; |
87 } | 142 } |
OLD | NEW |