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 extern "C" { |
18 const char* movnti_code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; | 18 extern const char no_rewrite_code[]; |
| 19 extern const char no_rewrite_code_post_rewrite[]; |
| 20 extern const char movntq_code[]; |
| 21 extern const char movntq_code_post_rewrite[]; |
| 22 extern const char movntdq_code[]; |
| 23 extern const char movntdq_code_post_rewrite[]; |
| 24 } |
19 | 25 |
20 class ValidationDisableNonTemporalsTests : public ::testing::Test { | 26 class ValidationMovntRewriteTests : public ::testing::Test { |
21 protected: | 27 protected: |
22 NaClValidationMetadata *metadata_ptr; | 28 NaClValidationMetadata *metadata_ptr; |
23 const struct NaClValidatorInterface *validator; | 29 const struct NaClValidatorInterface *validator; |
24 NaClCPUFeatures *cpu_features; | 30 NaClCPUFeatures *cpu_features; |
25 | 31 |
26 unsigned char code_buffer[CODE_SIZE]; | 32 unsigned char code_buffer[CODE_SIZE]; |
27 | 33 |
28 void SetUp() { | 34 void SetUp() { |
29 metadata_ptr = NULL; | 35 metadata_ptr = NULL; |
30 validator = NaClCreateValidator(); | 36 validator = NaClCreateValidator(); |
(...skipping 12 matching lines...) Expand all Loading... |
43 cpu_features, | 49 cpu_features, |
44 metadata_ptr, | 50 metadata_ptr, |
45 NULL); | 51 NULL); |
46 } | 52 } |
47 | 53 |
48 void TearDown() { | 54 void TearDown() { |
49 free(cpu_features); | 55 free(cpu_features); |
50 } | 56 } |
51 }; | 57 }; |
52 | 58 |
53 TEST_F(ValidationDisableNonTemporalsTests, NotDisableNonTemporals) { | 59 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
54 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | 60 memcpy(code_buffer, no_rewrite_code, strlen(no_rewrite_code)); |
55 NaClValidationStatus status = Validate(0); | 61 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
56 // If we are not disabling non-temporal instructions, use the original | 62 EXPECT_EQ(NaClValidationFailed, status); |
57 // validation rule, i.e., allow them. In future, we will do rewriting. | 63 EXPECT_EQ(0, memcmp(code_buffer, |
58 EXPECT_EQ(NaClValidationSucceeded, status); | 64 no_rewrite_code_post_rewrite, |
59 // Code should not change. | 65 strlen(no_rewrite_code_post_rewrite))); |
60 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | |
61 } | 66 } |
62 | 67 |
63 TEST_F(ValidationDisableNonTemporalsTests, DisableNonTemporals) { | 68 TEST_F(ValidationMovntRewriteTests, RewriteMovntq) { |
64 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | 69 memcpy(code_buffer, movntq_code, strlen(movntq_code)); |
65 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); | 70 NaClValidationStatus status = Validate(0); |
66 // Disable non-temporal instructions. | 71 EXPECT_EQ(NaClValidationSucceeded, status); |
67 EXPECT_EQ(NaClValidationFailed, status); | 72 EXPECT_EQ(0, memcmp(code_buffer, |
68 // Code should not change. | 73 movntq_code_post_rewrite, |
69 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | 74 strlen(movntq_code_post_rewrite))); |
| 75 } |
| 76 |
| 77 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 78 memcpy(code_buffer, movntdq_code, strlen(movntdq_code)); |
| 79 NaClValidationStatus status = Validate(0); |
| 80 EXPECT_EQ(NaClValidationSucceeded, status); |
| 81 EXPECT_EQ(0, memcmp(code_buffer, |
| 82 movntdq_code_post_rewrite, |
| 83 strlen(movntdq_code_post_rewrite))); |
70 } | 84 } |
71 | 85 |
72 int main(int argc, char *argv[]) { | 86 int main(int argc, char *argv[]) { |
73 // The IllegalInst test touches the log mutex deep inside the validator. | 87 // 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 | 88 // This causes an SEH exception to be thrown on Windows if the mutex is not |
75 // initialized. | 89 // initialized. |
76 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | 90 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
77 NaClLogModuleInit(); | 91 NaClLogModuleInit(); |
78 testing::InitGoogleTest(&argc, argv); | 92 testing::InitGoogleTest(&argc, argv); |
79 return RUN_ALL_TESTS(); | 93 return RUN_ALL_TESTS(); |
80 } | 94 } |
OLD | NEW |