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> |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 Bool NaClDfaProcessValidationError(const uint8_t *begin, const uint8_t *end, | 26 Bool NaClDfaProcessValidationError(const uint8_t *begin, const uint8_t *end, |
| 27 uint32_t info, void *callback_data) { | 27 uint32_t info, void *callback_data) { |
| 28 UNREFERENCED_PARAMETER(begin); | 28 UNREFERENCED_PARAMETER(begin); |
| 29 UNREFERENCED_PARAMETER(end); | 29 UNREFERENCED_PARAMETER(end); |
| 30 UNREFERENCED_PARAMETER(info); | 30 UNREFERENCED_PARAMETER(info); |
| 31 UNREFERENCED_PARAMETER(callback_data); | 31 UNREFERENCED_PARAMETER(callback_data); |
| 32 | 32 |
| 33 return FALSE; | 33 return FALSE; |
| 34 } | 34 } |
| 35 | 35 |
| 36 static Bool IsREX(uint8_t byte) { | |
| 37 return byte >= 0x40 && byte <= 0x4f; | |
| 38 } | |
| 39 | |
| 40 static Bool RewriteUnsupportedInstruction(const uint8_t *begin, | |
| 41 const uint8_t *end) { | |
| 42 uint8_t *ptr = (uint8_t *) begin; | |
| 43 /* We usually only check and rewrite the first few bytes without examining | |
| 44 * further because this function is only called when the validator tells us | |
| 45 * that it is an 'unsupported instruction' and there are no other validation | |
| 46 * failures. | |
| 47 */ | |
| 48 if (memcmp(begin, "\x0f\xe7", 2) == 0) { | |
| 49 /* ia32: movntq => movq */ | |
| 50 ptr[1] = 0x7f; | |
| 51 return TRUE; | |
| 52 } else if (memcmp(begin, "\x66\x0f\xe7", 3) == 0) { | |
| 53 /* ia32: movntdq => movdqa */ | |
| 54 ptr[2] = 0x7f; | |
| 55 return TRUE; | |
| 56 } else if (IsREX(begin[0])) { | |
|
bradn
2015/08/04 22:03:30
Make this arch conditional (#ifdef), so you don't
ruiq
2015/08/05 20:11:03
Done.
| |
| 57 if (memcmp(begin + 1, "\x0f\x2b", 2) == 0) { | |
| 58 /* amd64: movntps => movaps */ | |
| 59 ptr[2] = 0x29; | |
| 60 return TRUE; | |
| 61 } else if (memcmp(begin + 1, "\x0f\xc3", 2) == 0) { | |
| 62 /* amd64: movnti => mov */ | |
| 63 ptr[1] = 0x89; | |
| 64 memmove(ptr + 2, ptr + 3, end - begin - 3); | |
| 65 ptr[end - begin - 1] = 0x90; | |
| 66 return TRUE; | |
| 67 } else if (memcmp(begin + 1, "\x0f\x18", 2) == 0) { | |
| 68 /* amd64: prefetchnta => nop */ | |
| 69 memset(ptr, 0x90, end - begin); | |
| 70 return TRUE; | |
| 71 } | |
| 72 } else if (begin[0] == 0x66 && IsREX(begin[1]) && | |
| 73 memcmp(begin + 2, "\x0f\xe7", 2) == 0) { | |
| 74 /* amd64: movntdq => movdqa */ | |
| 75 ptr[3] = 0x7f; | |
| 76 return TRUE; | |
| 77 } | |
| 78 return FALSE; | |
| 79 } | |
| 80 | |
| 36 Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, | 81 Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, |
| 37 const uint8_t *end, | 82 const uint8_t *end, |
| 38 uint32_t info, | 83 uint32_t info, |
| 39 void *callback_data) { | 84 void *callback_data) { |
| 40 struct StubOutCallbackData *data = callback_data; | 85 struct StubOutCallbackData *data = callback_data; |
| 41 /* Stub-out instructions unsupported on this CPU, but valid on other CPUs. */ | 86 /* Stub-out instructions unsupported on this CPU, but valid on other CPUs. */ |
| 42 if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) { | 87 if ((info & VALIDATION_ERRORS_MASK) == CPUID_UNSUPPORTED_INSTRUCTION) { |
| 43 data->did_rewrite = 1; | 88 data->did_rewrite = 1; |
| 44 memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin); | 89 memset((uint8_t *)begin, NACL_HALT_OPCODE, end - begin); |
| 45 return TRUE; | 90 return TRUE; |
| 46 } else if ((info & VALIDATION_ERRORS_MASK) == UNSUPPORTED_INSTRUCTION) { | 91 } else if ((info & VALIDATION_ERRORS_MASK) == UNSUPPORTED_INSTRUCTION) { |
| 47 if (data->flags & NACL_DISABLE_NONTEMPORALS_X86) { | 92 if (data->flags & NACL_DISABLE_NONTEMPORALS_X86) { |
| 48 return FALSE; | 93 return FALSE; |
| 49 } else { | 94 } else { |
| 50 /* TODO(ruiq): rewrite instruction. For now, we keep the original | 95 if (RewriteUnsupportedInstruction(begin, end)) { |
| 51 * instruction and indicate validation success, which is consistent | 96 data->did_rewrite = 1; |
| 52 * with current validation results. */ | 97 return TRUE; |
| 53 data->did_rewrite = 0; | 98 } else { |
| 54 return TRUE; | 99 data->did_rewrite = 0; |
| 100 return FALSE; | |
| 101 } | |
| 55 } | 102 } |
| 56 } else { | 103 } else { |
| 57 return FALSE; | 104 return FALSE; |
| 58 } | 105 } |
| 59 } | 106 } |
| 60 | 107 |
| 61 Bool NaClDfaProcessCodeCopyInstruction(const uint8_t *begin_new, | 108 Bool NaClDfaProcessCodeCopyInstruction(const uint8_t *begin_new, |
| 62 const uint8_t *end_new, | 109 const uint8_t *end_new, |
| 63 uint32_t info_new, | 110 uint32_t info_new, |
| 64 void *callback_data) { | 111 void *callback_data) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 78 | 125 |
| 79 Bool NaClDfaCodeReplacementIsStubouted(const uint8_t *begin_existing, | 126 Bool NaClDfaCodeReplacementIsStubouted(const uint8_t *begin_existing, |
| 80 size_t instruction_length) { | 127 size_t instruction_length) { |
| 81 | 128 |
| 82 /* Unsupported instruction must have been replaced with HLTs. */ | 129 /* Unsupported instruction must have been replaced with HLTs. */ |
| 83 if (memcmp(kStubOutMem, begin_existing, instruction_length) == 0) | 130 if (memcmp(kStubOutMem, begin_existing, instruction_length) == 0) |
| 84 return TRUE; | 131 return TRUE; |
| 85 else | 132 else |
| 86 return FALSE; | 133 return FALSE; |
| 87 } | 134 } |
| OLD | NEW |