Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The Native Client Authors. All rights reserved. | 2 * Copyright 2015 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 #include <string.h> | |
| 7 #include "gtest/gtest.h" | 8 #include "gtest/gtest.h" |
| 8 | 9 |
| 9 #include "native_client/src/shared/platform/nacl_log.h" | 10 #include "native_client/src/shared/platform/nacl_log.h" |
| 10 #include "native_client/src/shared/utils/types.h" | 11 #include "native_client/src/shared/utils/types.h" |
| 11 #include "native_client/src/trusted/validator/ncvalidate.h" | 12 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 12 | 13 |
| 13 #define CODE_SIZE 32 | 14 #define CODE_SIZE 32 |
| 14 #define NOP 0x90 | 15 #define NOP 0x90 |
| 15 #define MOVNTI_CODE_SIZE 8 | |
| 16 | 16 |
| 17 // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) | 17 class ValidationMovntRewriteTests : public ::testing::Test { |
| 18 const char* movnti_code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; | |
| 19 | |
| 20 class ValidationDisableNonTemporalsTests : public ::testing::Test { | |
| 21 protected: | 18 protected: |
| 22 NaClValidationMetadata *metadata_ptr; | 19 NaClValidationMetadata *metadata_ptr; |
| 23 const struct NaClValidatorInterface *validator; | 20 const struct NaClValidatorInterface *validator; |
| 24 NaClCPUFeatures *cpu_features; | 21 NaClCPUFeatures *cpu_features; |
| 25 | 22 |
| 26 unsigned char code_buffer[CODE_SIZE]; | 23 unsigned char code_buffer[CODE_SIZE]; |
| 27 | 24 |
| 28 void SetUp() { | 25 void SetUp() { |
| 29 metadata_ptr = NULL; | 26 metadata_ptr = NULL; |
| 30 validator = NaClCreateValidator(); | 27 validator = NaClCreateValidator(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 43 cpu_features, | 40 cpu_features, |
| 44 metadata_ptr, | 41 metadata_ptr, |
| 45 NULL); | 42 NULL); |
| 46 } | 43 } |
| 47 | 44 |
| 48 void TearDown() { | 45 void TearDown() { |
| 49 free(cpu_features); | 46 free(cpu_features); |
| 50 } | 47 } |
| 51 }; | 48 }; |
| 52 | 49 |
| 53 TEST_F(ValidationDisableNonTemporalsTests, NotDisableNonTemporals) { | 50 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
| 54 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | 51 // movntq %mm0, (%ebx) |
| 55 NaClValidationStatus status = Validate(0); | 52 const char *code = "\x0f\xe7\x03"; |
|
bradn
2015/08/04 22:03:30
Could you add a .S file with the asm for each of t
ruiq
2015/08/05 20:11:02
Done.
| |
| 56 // If we are not disabling non-temporal instructions, use the original | 53 memcpy(code_buffer, code, strlen(code)); |
| 57 // validation rule, i.e., allow them. In future, we will do rewriting. | 54 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
| 58 EXPECT_EQ(NaClValidationSucceeded, status); | 55 // Disable nontemporals, no rewrite. |
| 56 EXPECT_EQ(NaClValidationFailed, status); | |
| 59 // Code should not change. | 57 // Code should not change. |
| 60 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | 58 EXPECT_EQ(0, memcmp(code_buffer, code, strlen(code))); |
| 61 } | 59 } |
| 62 | 60 |
| 63 TEST_F(ValidationDisableNonTemporalsTests, DisableNonTemporals) { | 61 TEST_F(ValidationMovntRewriteTests, RewriteMovntq) { |
| 64 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | 62 // movntq %mm0, (%ebx) |
| 65 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); | 63 const char *code = "\x0f\xe7\x03"; |
| 66 // Disable non-temporal instructions. | 64 memcpy(code_buffer, code, strlen(code)); |
| 67 EXPECT_EQ(NaClValidationFailed, status); | 65 NaClValidationStatus status = Validate(0); |
| 68 // Code should not change. | 66 EXPECT_EQ(NaClValidationSucceeded, status); |
| 69 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | 67 // movq %mm0, (%ebx) |
| 68 const char *expect_code = "\x0f\x7f\x03"; | |
| 69 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
| 70 } | |
| 71 | |
| 72 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { | |
| 73 // movntdq %xmm0, (%edx) | |
| 74 const char *code = "\x66\x0f\xe7\x02"; | |
| 75 memcpy(code_buffer, code, strlen(code)); | |
| 76 NaClValidationStatus status = Validate(0); | |
| 77 EXPECT_EQ(NaClValidationSucceeded, status); | |
| 78 // movdqa %xmm0, (%edx) | |
| 79 const char *expect_code = "\x66\x0f\x7f\x02"; | |
| 80 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
| 70 } | 81 } |
| 71 | 82 |
| 72 int main(int argc, char *argv[]) { | 83 int main(int argc, char *argv[]) { |
| 73 // The IllegalInst test touches the log mutex deep inside the validator. | 84 // The IllegalInst test touches the log mutex deep inside the validator. |
| 74 // This causes an SEH exception to be thrown on Windows if the mutex is not | 85 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 75 // initialized. | 86 // initialized. |
| 76 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | 87 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 77 NaClLogModuleInit(); | 88 NaClLogModuleInit(); |
| 78 testing::InitGoogleTest(&argc, argv); | 89 testing::InitGoogleTest(&argc, argv); |
| 79 return RUN_ALL_TESTS(); | 90 return RUN_ALL_TESTS(); |
| 80 } | 91 } |
| OLD | NEW |