Chromium Code Reviews| Index: src/trusted/validator_ragel/dfa_validate_common.c |
| diff --git a/src/trusted/validator_ragel/dfa_validate_common.c b/src/trusted/validator_ragel/dfa_validate_common.c |
| index 4972cbd654b3fcd0d884dec0099db63750991802..4f20121f79ea976cf6f79d54fb1a7a67b64aed3c 100644 |
| --- a/src/trusted/validator_ragel/dfa_validate_common.c |
| +++ b/src/trusted/validator_ragel/dfa_validate_common.c |
| @@ -12,6 +12,7 @@ |
| #include "native_client/src/shared/platform/nacl_check.h" |
| #include "native_client/src/trusted/service_runtime/nacl_config.h" |
| #include "native_client/src/trusted/validator_ragel/validator.h" |
| +#include "native_client/src/include/build_config.h" |
| /* Used as an argument to copy_func when unsupported instruction must be |
| replaced with HLTs. */ |
| @@ -33,6 +34,58 @@ Bool NaClDfaProcessValidationError(const uint8_t *begin, const uint8_t *end, |
| return FALSE; |
| } |
| +#if NACL_BUILD_SUBARCH == 64 |
| +static Bool IsREX(uint8_t byte) { |
| + return byte >= 0x40 && byte <= 0x4f; |
| +} |
| +#endif |
| + |
| +static Bool RewriteUnsupportedInstruction(const uint8_t *begin, |
| + const uint8_t *end) { |
| + uint8_t *ptr = (uint8_t *) begin; |
| + /* We usually only check and rewrite the first few bytes without examining |
| + * further because this function is only called when the validator tells us |
| + * that it is an 'unsupported instruction' and there are no other validation |
| + * failures. |
| + */ |
| +#if NACL_BUILD_SUBARCH == 32 |
| + UNREFERENCED_PARAMETER(end); |
| + if (memcmp(begin, "\x0f\xe7", 2) == 0) { |
| + /* movntq => movq */ |
| + ptr[1] = 0x7f; |
| + return TRUE; |
| + } else if (memcmp(begin, "\x66\x0f\xe7", 3) == 0) { |
| + /* movntdq => movdqa */ |
| + ptr[2] = 0x7f; |
| + return TRUE; |
| + } |
| +#elif NACL_BUILD_SUBARCH == 64 |
| + if (IsREX(begin[0])) { |
| + 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.
|
| + /* movntps => movaps */ |
| + ptr[2] = 0x29; |
| + return TRUE; |
| + } else if (memcmp(begin + 1, "\x0f\xc3", 2) == 0) { |
| + /* movnti => mov */ |
| + ptr[1] = 0x89; |
| + memmove(ptr + 2, ptr + 3, end - begin - 3); |
| + ptr[end - begin - 1] = 0x90; |
| + return TRUE; |
| + } else if (memcmp(begin + 1, "\x0f\x18", 2) == 0) { |
| + /* prefetchnta => nop */ |
| + memset(ptr, 0x90, end - begin); |
| + return TRUE; |
| + } |
| + } else if (begin[0] == 0x66 && IsREX(begin[1]) && |
| + memcmp(begin + 2, "\x0f\xe7", 2) == 0) { |
| + /* movntdq => movdqa */ |
| + ptr[3] = 0x7f; |
| + return TRUE; |
| + } |
| +#endif |
| + return FALSE; |
| +} |
| + |
| Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, |
| const uint8_t *end, |
| uint32_t info, |
| @@ -47,11 +100,13 @@ Bool NaClDfaStubOutUnsupportedInstruction(const uint8_t *begin, |
| if (data->flags & NACL_DISABLE_NONTEMPORALS_X86) { |
| return FALSE; |
| } else { |
| - /* TODO(ruiq): rewrite instruction. For now, we keep the original |
| - * instruction and indicate validation success, which is consistent |
| - * with current validation results. */ |
| - data->did_rewrite = 0; |
| - return TRUE; |
| + 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.
|
| + data->did_rewrite = 1; |
| + return TRUE; |
| + } else { |
| + data->did_rewrite = 0; |
| + return FALSE; |
| + } |
| } |
| } else { |
| return FALSE; |