Chromium Code Reviews| 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 */ | |
|
khimg
2015/08/08 00:33:11
Why movntq is not rewritten in 64bit mode?
ruiq
2015/08/10 00:57:15
As the CL description says, we only rewrite non-te
| |
| 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]) && (begin[1] == 0x0f)) { | |
| 64 uint8_t opcode_byte2 = begin[2]; | |
| 65 switch (opcode_byte2) { | |
| 66 case 0x2b: | |
| 67 /* movntps => movaps */ | |
|
khimg
2015/08/08 00:33:11
Why movntps is not rewritten in 32bit mode?
ruiq
2015/08/10 00:57:15
ditto.
| |
| 68 ptr[2] = 0x29; | |
| 69 return TRUE; | |
| 70 case 0xc3: | |
| 71 /* movnti => mov */ | |
|
khimg
2015/08/08 00:33:11
Why movnti is not rewritten in 32bit moed?
ruiq
2015/08/10 00:57:15
ditto.
| |
| 72 ptr[1] = 0x89; | |
| 73 memmove(ptr + 2, ptr + 3, end - begin - 3); | |
|
khimg
2015/08/08 00:33:11
You can't do that. Use the following approach:
p
ruiq
2015/08/10 00:57:15
The nop has to be put at the end because before th
| |
| 74 ptr[end - begin - 1] = 0x90; | |
| 75 return TRUE; | |
| 76 case 0x18: | |
| 77 /* prefetchnta => nop */ | |
|
khimg
2015/08/08 00:33:11
Why prefetchnta is not rewritten in 32bit mode?
Yo
ruiq
2015/08/10 00:57:15
ditto.
| |
| 78 memset(ptr, 0x90, end - begin); | |
| 79 return TRUE; | |
| 80 default: | |
| 81 return FALSE; | |
| 82 } | |
| 83 } else if (begin[0] == 0x66 && IsREX(begin[1]) && | |
| 84 memcmp(begin + 2, "\x0f\xe7", 2) == 0) { | |
| 85 /* movntdq => movdqa */ | |
| 86 ptr[3] = 0x7f; | |
| 87 return TRUE; | |
| 88 } | |
| 89 #endif | |
| 90 return FALSE; | |
| 91 } | |
| 92 | |
| 36 Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, | 93 Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, |
| 37 const uint8_t *end, | 94 const uint8_t *end, |
| 38 uint32_t info, | 95 uint32_t info, |
| 39 void *callback_data) { | 96 void *callback_data) { |
| 40 struct StubOutCallbackData *data = callback_data; | 97 struct StubOutCallbackData *data = callback_data; |
| 41 /* Stub-out instructions unsupported on this CPU, but valid on other CPUs. */ | |
| 42 if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) { | 98 if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) { |
| 99 /* Stub-out instructions unsupported on this CPU, but valid on other CPUs.*/ | |
| 43 data->did_rewrite = 1; | 100 data->did_rewrite = 1; |
| 44 memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin); | 101 memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin); |
| 45 return TRUE; | 102 return TRUE; |
| 46 } else if ((info & VALIDATION_ERRORS_MASK) == UNSUPPORTED_INSTRUCTION) { | 103 } else if ((info & VALIDATION_ERRORS_MASK) == UNSUPPORTED_INSTRUCTION) { |
| 47 if (data->flags & NACL_DISABLE_NONTEMPORALS_X86) { | 104 if (!(data->flags & NACL_DISABLE_NONTEMPORALS_X86) && |
| 48 return FALSE; | 105 RewriteUnsupportedInstruction(begin, end)) { |
| 49 } else { | 106 data->did_rewrite = 1; |
| 50 /* TODO(ruiq): rewrite instruction. For now, we keep the original | |
| 51 * instruction and indicate validation success, which is consistent | |
| 52 * with current validation results. */ | |
| 53 data->did_rewrite = 0; | |
| 54 return TRUE; | 107 return TRUE; |
| 55 } | 108 } |
| 109 return FALSE; | |
| 56 } else { | 110 } else { |
| 111 /* Other validation errors the validator cannot fix. */ | |
| 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) { |
| 65 struct CodeCopyCallbackData *data = callback_data; | 120 struct CodeCopyCallbackData *data = callback_data; |
| 66 size_t instruction_length = end_new - begin_new; | 121 size_t instruction_length = end_new - begin_new; |
| (...skipping 11 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 |