OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 #include <string.h> | |
8 #include "gtest/gtest.h" | |
9 | |
10 #include "native_client/src/shared/platform/nacl_log.h" | |
11 #include "native_client/src/shared/utils/types.h" | |
12 #include "native_client/src/trusted/validator/ncvalidate.h" | |
13 | |
14 #define CODE_SIZE 32 | |
15 #define NOP 0x90 | |
16 | |
17 class ValidationMovntRewriteTests : public ::testing::Test { | |
18 protected: | |
19 NaClValidationMetadata *metadata_ptr; | |
20 const struct NaClValidatorInterface *validator; | |
21 NaClCPUFeatures *cpu_features; | |
22 | |
23 unsigned char code_buffer[CODE_SIZE]; | |
24 | |
25 void SetUp() { | |
26 metadata_ptr = NULL; | |
27 validator = NaClCreateValidator(); | |
28 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); | |
29 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); | |
30 memset(cpu_features, 0, validator->CPUFeatureSize); | |
31 validator->SetAllCPUFeatures(cpu_features); | |
32 memset(code_buffer, NOP, sizeof(code_buffer)); | |
33 } | |
34 | |
35 NaClValidationStatus Validate(uint32_t flags) { | |
36 return validator->Validate(0, code_buffer, 32, | |
37 FALSE, /* stubout_mode */ | |
38 flags, | |
39 FALSE, /* readonly_test */ | |
40 cpu_features, | |
41 metadata_ptr, | |
42 NULL); | |
43 } | |
44 | |
45 void TearDown() { | |
46 free(cpu_features); | |
47 } | |
48 }; | |
49 | |
50 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { | |
51 // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) | |
52 const char *code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; | |
53 memcpy(code_buffer, code, strlen(code)); | |
54 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); | |
55 // Disable nontemporals, no rewrite. | |
56 EXPECT_EQ(NaClValidationFailed, status); | |
57 // Code should not change. | |
58 EXPECT_EQ(0, memcmp(code_buffer, code, strlen(code))); | |
59 } | |
60 | |
61 TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { | |
62 // mov %edi,%edi; prefetchnta (%r15,%rdi,1) | |
63 const char *code = "\x89\xff\x41\x0f\x18\x04\x3f"; | |
64 memcpy(code_buffer, code, strlen(code)); | |
65 NaClValidationStatus status = Validate(0); | |
66 EXPECT_EQ(NaClValidationSucceeded, status); | |
67 // mov %edi,%edi; nop; nop; nop; nop; nop | |
68 const char *expect_code = "\x89\xff\x90\x90\x90\x90\x90"; | |
69 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
70 } | |
71 | |
72 TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { | |
73 // mov %ebx,%ebx; movntps %xmm0,(%r15,%rbx,1) | |
74 const char *code = "\x89\xdb\x41\x0f\x2b\x04\x1f"; | |
75 memcpy(code_buffer, code, strlen(code)); | |
76 NaClValidationStatus status = Validate(0); | |
77 EXPECT_EQ(NaClValidationSucceeded, status); | |
78 // mov %ebx,%ebx; movaps %xmm0,(%r15,%rbx,1) | |
79 const char *expect_code = "\x89\xdb\x41\x0f\x29\x04\x1f"; | |
80 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
81 } | |
82 | |
83 TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { | |
84 // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) | |
85 const char *code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; | |
86 memcpy(code_buffer, code, strlen(code)); | |
87 NaClValidationStatus status = Validate(0); | |
88 EXPECT_EQ(NaClValidationSucceeded, status); | |
89 // mov %edi,%edi; mov %rax,0x68(%r15,%rdi,1); nop | |
90 const char *expect_code = "\x89\xff\x49\x89\x44\x3f\x68\x90"; | |
bradn
2015/08/04 22:03:30
You can also put the expected asm in the .S file.
| |
91 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
92 } | |
93 | |
94 TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { | |
95 // mov %edx,%edx; movnti %r9d,(%r15,%rdx,1) | |
96 const char *code = "\x89\xd2\x45\x0f\xc3\x0c\x17"; | |
97 memcpy(code_buffer, code, strlen(code)); | |
98 NaClValidationStatus status = Validate(0); | |
99 EXPECT_EQ(NaClValidationSucceeded, status); | |
100 // mov %edx,%edx; mov %r9d,(%r15,%rdx,1); nop | |
101 const char *expect_code = "\x89\xd2\x45\x89\x0c\x17\x90"; | |
102 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
103 } | |
104 | |
105 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { | |
106 // mov %edx,%edx; movntdq %xmm0,0x10(%r15,%rdx,1) | |
107 const char *code = "\x89\xd2\x66\x41\x0f\xe7\x44\x17\x10"; | |
108 memcpy(code_buffer, code, strlen(code)); | |
109 NaClValidationStatus status = Validate(0); | |
110 EXPECT_EQ(NaClValidationSucceeded, status); | |
111 // mov %edx,%edx; movdqa %xmm0,0x10(%r15,%rdx,1) | |
112 const char *expect_code = "\x89\xd2\x66\x41\x0f\x7f\x44\x17\x10"; | |
113 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
114 } | |
115 | |
116 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { | |
117 // mov %ecx,%ecx; movntdq %xmm15,(%r15,%rcx,1) | |
118 const char *code = "\x89\xc9\x66\x45\x0f\xe7\x3c\x0f"; | |
119 memcpy(code_buffer, code, strlen(code)); | |
120 NaClValidationStatus status = Validate(0); | |
121 EXPECT_EQ(NaClValidationSucceeded, status); | |
122 // mov %ecx,%ecx; movdqa %xmm15,(%r15,%rcx,1) | |
123 const char *expect_code = "\x89\xc9\x66\x45\x0f\x7f\x3c\x0f"; | |
124 EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); | |
125 } | |
126 | |
127 int main(int argc, char *argv[]) { | |
128 // The IllegalInst test touches the log mutex deep inside the validator. | |
129 // This causes an SEH exception to be thrown on Windows if the mutex is not | |
130 // initialized. | |
131 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | |
132 NaClLogModuleInit(); | |
133 testing::InitGoogleTest(&argc, argv); | |
134 return RUN_ALL_TESTS(); | |
135 } | |
OLD | NEW |