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..be7e5e7863ab67760dfcef883c99bef31e569ff5 |
--- /dev/null |
+++ b/src/trusted/validator/validation_rewrite_64_test.cc |
@@ -0,0 +1,249 @@ |
+/* |
+ * 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 64 |
+#define NOP 0x90 |
+ |
+extern "C" { |
+ extern const char no_rewrite_code[]; |
+ extern const char no_rewrite_code_end[]; |
+ extern const char no_rewrite_code_post_rewrite[]; |
+ extern const char no_rewrite_code_post_rewrite_end[]; |
+ extern const char off_webstore_movnt_code[]; |
+ extern const char off_webstore_movnt_code_end[]; |
+ extern const char off_webstore_movnt_code_post_rewrite[]; |
+ extern const char off_webstore_movnt_code_post_rewrite_end[]; |
+ extern const char prefetchnta_code[]; |
+ extern const char prefetchnta_code_end[]; |
+ extern const char prefetchnta_code_post_rewrite[]; |
+ extern const char prefetchnta_code_post_rewrite_end[]; |
+ extern const char movntps_code[]; |
+ extern const char movntps_code_end[]; |
+ extern const char movntps_code_post_rewrite[]; |
+ extern const char movntps_code_post_rewrite_end[]; |
+ extern const char movnti_code[]; |
+ extern const char movnti_code_end[]; |
+ extern const char movnti_code_post_rewrite[]; |
+ extern const char movnti_code_post_rewrite_end[]; |
+ extern const char movnti_code2[]; |
+ extern const char movnti_code2_end[]; |
+ extern const char movnti_code2_post_rewrite[]; |
+ extern const char movnti_code2_post_rewrite_end[]; |
+ extern const char movnti_rip_relative_code[]; |
+ extern const char movnti_rip_relative_code_end[]; |
+ extern const char movnti_rip_relative_code_post_rewrite[]; |
+ extern const char movnti_rip_relative_code_post_rewrite_end[]; |
+ extern const char movntdq_code[]; |
+ extern const char movntdq_code_end[]; |
+ extern const char movntdq_code_post_rewrite[]; |
+ extern const char movntdq_code_post_rewrite_end[]; |
+ extern const char movntdq_code2[]; |
+ extern const char movntdq_code2_end[]; |
+ extern const char movntdq_code2_post_rewrite[]; |
+ extern const char movntdq_code2_post_rewrite_end[]; |
+ extern const char multiple_movnt_code[]; |
+ extern const char multiple_movnt_code_end[]; |
+ extern const char multiple_movnt_code_post_rewrite[]; |
+ extern const char multiple_movnt_code_post_rewrite_end[]; |
+ extern const char one_bundle_movnt_code[]; |
+ extern const char one_bundle_movnt_code_end[]; |
+ extern const char one_bundle_movnt_code_post_rewrite[]; |
+ extern const char one_bundle_movnt_code_post_rewrite_end[]; |
+ extern const char last_movnti_cross_bundle_by_one[]; |
+ extern const char last_movnti_cross_bundle_by_one_end[]; |
+ extern const char last_movnti_cross_bundle_by_one_post_rewrite[]; |
+ extern const char last_movnti_cross_bundle_by_one_post_rewrite_end[]; |
+} |
+ |
+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, CODE_SIZE, |
+ FALSE, /* stubout_mode */ |
+ flags, |
+ FALSE, /* readonly_test */ |
+ cpu_features, |
+ metadata_ptr, |
+ NULL); |
+ } |
+ |
+ void TearDown() { |
+ free(cpu_features); |
+ } |
+}; |
+ |
+TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
+ size_t len = no_rewrite_code_end - no_rewrite_code; |
+ size_t post_len = |
+ no_rewrite_code_post_rewrite_end - no_rewrite_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, no_rewrite_code, len); |
+ NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
+ EXPECT_EQ(NaClValidationFailed, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, no_rewrite_code_post_rewrite, post_len)); |
+} |
+ |
+/* |
+ * In this test, the non-temporal write instruction is not found in x86-64 nexes |
+ * in the webstore. Therefore, we will forbid it instead of rewriting it. |
+ */ |
+TEST_F(ValidationMovntRewriteTests, ForbidOffWebStoreMovntNoRewrite) { |
+ size_t len = off_webstore_movnt_code_end - off_webstore_movnt_code; |
+ size_t post_len = off_webstore_movnt_code_post_rewrite_end - |
+ off_webstore_movnt_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, no_rewrite_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationFailed, status); |
+ EXPECT_EQ(0, memcmp(off_webstore_movnt_code, |
+ off_webstore_movnt_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { |
+ size_t len = prefetchnta_code_end - prefetchnta_code; |
+ size_t post_len = |
+ prefetchnta_code_post_rewrite_end - prefetchnta_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, prefetchnta_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, prefetchnta_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { |
+ size_t len = movntps_code_end - movntps_code; |
+ size_t post_len = |
+ movntps_code_post_rewrite_end - movntps_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, movntps_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, movntps_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { |
+ size_t len = movnti_code_end - movnti_code; |
+ size_t post_len = |
+ movnti_code_post_rewrite_end - movnti_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, movnti_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, movnti_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { |
+ size_t len = movnti_code2_end - movnti_code2; |
+ size_t post_len = |
+ movnti_code2_post_rewrite_end - movnti_code2_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, movnti_code2, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, movnti_code2_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntiRipRelative) { |
+ size_t len = movnti_rip_relative_code_end - movnti_rip_relative_code; |
+ size_t post_len = movnti_rip_relative_code_post_rewrite_end - |
+ movnti_rip_relative_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, movnti_rip_relative_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, |
+ movnti_rip_relative_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
+ size_t len = movntdq_code_end - movntdq_code; |
+ size_t post_len = movntdq_code_post_rewrite_end - movntdq_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, movntdq_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, movntdq_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { |
+ size_t len = movntdq_code2_end - movntdq_code2; |
+ size_t post_len = movntdq_code2_post_rewrite_end - movntdq_code2_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, movntdq_code2, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, movntdq_code2_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteMultipleMovnt) { |
+ size_t len = multiple_movnt_code_end - multiple_movnt_code; |
+ size_t post_len = |
+ multiple_movnt_code_post_rewrite_end - multiple_movnt_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, multiple_movnt_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, multiple_movnt_code_post_rewrite, post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteOneBundleMovnt) { |
+ size_t len = one_bundle_movnt_code_end - one_bundle_movnt_code; |
+ size_t post_len = one_bundle_movnt_code_post_rewrite_end - |
+ one_bundle_movnt_code_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, one_bundle_movnt_code, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, one_bundle_movnt_code_post_rewrite, |
+ post_len)); |
+} |
+ |
+TEST_F(ValidationMovntRewriteTests, RewriteLastMovntiCrossBundleByOne) { |
+ size_t len = last_movnti_cross_bundle_by_one_end - |
+ last_movnti_cross_bundle_by_one; |
+ size_t post_len = last_movnti_cross_bundle_by_one_post_rewrite_end - |
+ last_movnti_cross_bundle_by_one_post_rewrite; |
+ EXPECT_EQ(len, post_len); |
+ memcpy(code_buffer, last_movnti_cross_bundle_by_one, len); |
+ NaClValidationStatus status = Validate(0); |
+ EXPECT_EQ(NaClValidationSucceeded, status); |
+ EXPECT_EQ(0, memcmp(code_buffer, last_movnti_cross_bundle_by_one_post_rewrite, |
+ post_len)); |
+} |
+ |
+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(); |
+} |