| 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> |
| 8 |
| 7 #include "gtest/gtest.h" | 9 #include "gtest/gtest.h" |
| 8 | 10 |
| 9 #include "native_client/src/shared/platform/nacl_log.h" | 11 #include "native_client/src/shared/platform/nacl_log.h" |
| 10 #include "native_client/src/shared/utils/types.h" | 12 #include "native_client/src/shared/utils/types.h" |
| 11 #include "native_client/src/trusted/validator/ncvalidate.h" | 13 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 12 | 14 |
| 13 #define CODE_SIZE 32 | 15 #define CODE_SIZE 32 |
| 14 #define NOP 0x90 | 16 #define NOP 0x90 |
| 15 #define MOVNTI_CODE_SIZE 8 | |
| 16 | 17 |
| 17 // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) | 18 extern "C" { |
| 18 const char* movnti_code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; | 19 extern const char no_rewrite_code[]; |
| 20 extern const char no_rewrite_code_end[]; |
| 21 extern const char no_rewrite_code_post_rewrite[]; |
| 22 extern const char no_rewrite_code_post_rewrite_end[]; |
| 23 extern const char movntq_code[]; |
| 24 extern const char movntq_code_end[]; |
| 25 extern const char movntq_code_post_rewrite[]; |
| 26 extern const char movntq_code_post_rewrite_end[]; |
| 27 extern const char movntdq_code[]; |
| 28 extern const char movntdq_code_end[]; |
| 29 extern const char movntdq_code_post_rewrite[]; |
| 30 extern const char movntdq_code_post_rewrite_end[]; |
| 31 } |
| 19 | 32 |
| 20 class ValidationDisableNonTemporalsTests : public ::testing::Test { | 33 class ValidationMovntRewriteTests : public ::testing::Test { |
| 21 protected: | 34 protected: |
| 22 NaClValidationMetadata *metadata_ptr; | 35 NaClValidationMetadata *metadata_ptr; |
| 23 const struct NaClValidatorInterface *validator; | 36 const struct NaClValidatorInterface *validator; |
| 24 NaClCPUFeatures *cpu_features; | 37 NaClCPUFeatures *cpu_features; |
| 25 | 38 |
| 26 unsigned char code_buffer[CODE_SIZE]; | 39 unsigned char code_buffer[CODE_SIZE]; |
| 27 | 40 |
| 28 void SetUp() { | 41 void SetUp() { |
| 29 metadata_ptr = NULL; | 42 metadata_ptr = NULL; |
| 30 validator = NaClCreateValidator(); | 43 validator = NaClCreateValidator(); |
| 31 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); | 44 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); |
| 32 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); | 45 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); |
| 33 memset(cpu_features, 0, validator->CPUFeatureSize); | 46 memset(cpu_features, 0, validator->CPUFeatureSize); |
| 34 validator->SetAllCPUFeatures(cpu_features); | 47 validator->SetAllCPUFeatures(cpu_features); |
| 35 memset(code_buffer, NOP, sizeof(code_buffer)); | 48 memset(code_buffer, NOP, sizeof(code_buffer)); |
| 36 } | 49 } |
| 37 | 50 |
| 38 NaClValidationStatus Validate(uint32_t flags) { | 51 NaClValidationStatus Validate(uint32_t flags) { |
| 39 return validator->Validate(0, code_buffer, 32, | 52 return validator->Validate(0, code_buffer, CODE_SIZE, |
| 40 FALSE, /* stubout_mode */ | 53 FALSE, /* stubout_mode */ |
| 41 flags, | 54 flags, |
| 42 FALSE, /* readonly_test */ | 55 FALSE, /* readonly_test */ |
| 43 cpu_features, | 56 cpu_features, |
| 44 metadata_ptr, | 57 metadata_ptr, |
| 45 NULL); | 58 NULL); |
| 46 } | 59 } |
| 47 | 60 |
| 48 void TearDown() { | 61 void TearDown() { |
| 49 free(cpu_features); | 62 free(cpu_features); |
| 50 } | 63 } |
| 51 }; | 64 }; |
| 52 | 65 |
| 53 TEST_F(ValidationDisableNonTemporalsTests, NotDisableNonTemporals) { | 66 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
| 54 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | 67 size_t len = no_rewrite_code_end - no_rewrite_code; |
| 55 NaClValidationStatus status = Validate(0); | 68 size_t post_len = |
| 56 // If we are not disabling non-temporal instructions, use the original | 69 no_rewrite_code_post_rewrite_end - no_rewrite_code_post_rewrite; |
| 57 // validation rule, i.e., allow them. In future, we will do rewriting. | 70 EXPECT_EQ(len, post_len); |
| 58 EXPECT_EQ(NaClValidationSucceeded, status); | 71 memcpy(code_buffer, no_rewrite_code, len); |
| 59 // Code should not change. | 72 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
| 60 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | 73 EXPECT_EQ(NaClValidationFailed, status); |
| 74 EXPECT_EQ(0, memcmp(code_buffer, no_rewrite_code_post_rewrite, post_len)); |
| 61 } | 75 } |
| 62 | 76 |
| 63 TEST_F(ValidationDisableNonTemporalsTests, DisableNonTemporals) { | 77 TEST_F(ValidationMovntRewriteTests, RewriteMovntq) { |
| 64 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | 78 size_t len = movntq_code_end - movntq_code; |
| 65 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); | 79 size_t post_len = movntq_code_post_rewrite_end - movntq_code_post_rewrite; |
| 66 // Disable non-temporal instructions. | 80 EXPECT_EQ(len, post_len); |
| 67 EXPECT_EQ(NaClValidationFailed, status); | 81 memcpy(code_buffer, movntq_code, len); |
| 68 // Code should not change. | 82 NaClValidationStatus status = Validate(0); |
| 69 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | 83 EXPECT_EQ(NaClValidationSucceeded, status); |
| 84 EXPECT_EQ(0, memcmp(code_buffer, movntq_code_post_rewrite, post_len)); |
| 85 } |
| 86 |
| 87 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 88 size_t len = movntdq_code_end - movntdq_code; |
| 89 size_t post_len = movntdq_code_post_rewrite_end - movntdq_code_post_rewrite; |
| 90 EXPECT_EQ(len, post_len); |
| 91 memcpy(code_buffer, movntdq_code, len); |
| 92 NaClValidationStatus status = Validate(0); |
| 93 EXPECT_EQ(NaClValidationSucceeded, status); |
| 94 EXPECT_EQ(0, memcmp(code_buffer, movntdq_code_post_rewrite, post_len)); |
| 70 } | 95 } |
| 71 | 96 |
| 72 int main(int argc, char *argv[]) { | 97 int main(int argc, char *argv[]) { |
| 73 // The IllegalInst test touches the log mutex deep inside the validator. | 98 // 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 | 99 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 75 // initialized. | 100 // initialized. |
| 76 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | 101 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 77 NaClLogModuleInit(); | 102 NaClLogModuleInit(); |
| 78 testing::InitGoogleTest(&argc, argv); | 103 testing::InitGoogleTest(&argc, argv); |
| 79 return RUN_ALL_TESTS(); | 104 return RUN_ALL_TESTS(); |
| 80 } | 105 } |
| OLD | NEW |