Index: src/trusted/validator/validation_rewrite_64_test.cc |
diff --git a/src/trusted/validator/validation_rewrite_64_test.cc b/src/trusted/validator/validation_rewrite_64_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2bc83b1e3a8f02dab1aedfd8a893d19ae199b8e4 |
--- /dev/null |
+++ b/src/trusted/validator/validation_rewrite_64_test.cc |
@@ -0,0 +1,135 @@ |
+/* |
+ * Copyright 2015 The Native Client Authors. All rights reserved. |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include <string.h> |
+#include "gtest/gtest.h" |
+ |
+#include "native_client/src/shared/platform/nacl_log.h" |
+#include "native_client/src/shared/utils/types.h" |
+#include "native_client/src/trusted/validator/ncvalidate.h" |
+ |
+#define CODE_SIZE 32 |
+#define NOP 0x90 |
+ |
+class ValidationMovntRewriteTests : public ::testing::Test { |
+ protected: |
+ NaClValidationMetadata *metadata_ptr; |
+ const struct NaClValidatorInterface *validator; |
+ NaClCPUFeatures *cpu_features; |
+ |
+ unsigned char code_buffer[CODE_SIZE]; |
+ |
+ void SetUp() { |
+ metadata_ptr = NULL; |
+ validator = NaClCreateValidator(); |
+ cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); |
+ EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); |
+ memset(cpu_features, 0, validator->CPUFeatureSize); |
+ validator->SetAllCPUFeatures(cpu_features); |
+ memset(code_buffer, NOP, sizeof(code_buffer)); |
+ } |
+ |
+ NaClValidationStatus Validate(uint32_t flags) { |
+ return validator->Validate(0, code_buffer, 32, |
+ FALSE, /* stubout_mode */ |
+ flags, |
+ FALSE, /* readonly_test */ |
+ cpu_features, |
+ metadata_ptr, |
+ NULL); |
+ } |
+ |
+ void TearDown() { |
+ free(cpu_features); |
+ } |
+}; |
+ |
+TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
+ // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) |
+ const char *code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
+ // Disable nontemporals, no rewrite. |
+ EXPECT_EQ(NaClValidationFailed, status); |
+ // Code should not change. |
+ EXPECT_EQ(0, memcmp(code_buffer, code, strlen(code))); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { |
+ // mov %edi,%edi; prefetchnta (%r15,%rdi,1) |
+ const char *code = "\x89\xff\x41\x0f\x18\x04\x3f"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ // mov %edi,%edi; nop; nop; nop; nop; nop |
+ const char *expect_code = "\x89\xff\x90\x90\x90\x90\x90"; |
+ EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { |
+ // mov %ebx,%ebx; movntps %xmm0,(%r15,%rbx,1) |
+ const char *code = "\x89\xdb\x41\x0f\x2b\x04\x1f"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ // mov %ebx,%ebx; movaps %xmm0,(%r15,%rbx,1) |
+ const char *expect_code = "\x89\xdb\x41\x0f\x29\x04\x1f"; |
+ EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { |
+ // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) |
+ const char *code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ // mov %edi,%edi; mov %rax,0x68(%r15,%rdi,1); nop |
+ 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.
|
+ EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { |
+ // mov %edx,%edx; movnti %r9d,(%r15,%rdx,1) |
+ const char *code = "\x89\xd2\x45\x0f\xc3\x0c\x17"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ // mov %edx,%edx; mov %r9d,(%r15,%rdx,1); nop |
+ const char *expect_code = "\x89\xd2\x45\x89\x0c\x17\x90"; |
+ EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
+ // mov %edx,%edx; movntdq %xmm0,0x10(%r15,%rdx,1) |
+ const char *code = "\x89\xd2\x66\x41\x0f\xe7\x44\x17\x10"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ // mov %edx,%edx; movdqa %xmm0,0x10(%r15,%rdx,1) |
+ const char *expect_code = "\x89\xd2\x66\x41\x0f\x7f\x44\x17\x10"; |
+ EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { |
+ // mov %ecx,%ecx; movntdq %xmm15,(%r15,%rcx,1) |
+ const char *code = "\x89\xc9\x66\x45\x0f\xe7\x3c\x0f"; |
+ memcpy(code_buffer, code, strlen(code)); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ // mov %ecx,%ecx; movdqa %xmm15,(%r15,%rcx,1) |
+ const char *expect_code = "\x89\xc9\x66\x45\x0f\x7f\x3c\x0f"; |
+ EXPECT_EQ(0, memcmp(code_buffer, expect_code, strlen(expect_code))); |
+} |
+ |
+int main(int argc, char *argv[]) { |
+ // The IllegalInst test touches the log mutex deep inside the validator. |
+ // This causes an SEH exception to be thrown on Windows if the mutex is not |
+ // initialized. |
+ // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
+ NaClLogModuleInit(); |
+ testing::InitGoogleTest(&argc, argv); |
+ return RUN_ALL_TESTS(); |
+} |