OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 |
| 9 #include "gtest/gtest.h" |
| 10 |
| 11 #include "native_client/src/include/build_config.h" |
| 12 #include "native_client/src/shared/platform/nacl_log.h" |
| 13 #include "native_client/src/shared/utils/types.h" |
| 14 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 15 |
| 16 #define CODE_SIZE 64 |
| 17 #define NOP 0x90 |
| 18 |
| 19 struct TestCode { |
| 20 const char *before_template; |
| 21 const char *before_template_end; |
| 22 const char *after_template; |
| 23 const char *after_template_end; |
| 24 }; |
| 25 |
| 26 #define DECLARE_TEMPLATE(template_name) \ |
| 27 extern "C" const char template_name[]; \ |
| 28 extern "C" const char template_name ## _end[]; \ |
| 29 extern "C" const char template_name ## _post_rewrite[]; \ |
| 30 extern "C" const char template_name ## _post_rewrite_end[]; \ |
| 31 static const TestCode t_ ## template_name = { \ |
| 32 template_name, \ |
| 33 template_name ## _end, \ |
| 34 template_name ## _post_rewrite, \ |
| 35 template_name ## _post_rewrite_end, \ |
| 36 }; |
| 37 |
| 38 DECLARE_TEMPLATE(no_rewrite_code) |
| 39 #if NACL_BUILD_SUBARCH == 32 |
| 40 DECLARE_TEMPLATE(movntq_code) |
| 41 DECLARE_TEMPLATE(movntdq_code) |
| 42 #else |
| 43 DECLARE_TEMPLATE(off_webstore_movnt_code) |
| 44 DECLARE_TEMPLATE(prefetchnta_code) |
| 45 DECLARE_TEMPLATE(movntps_code) |
| 46 DECLARE_TEMPLATE(movnti_code) |
| 47 DECLARE_TEMPLATE(movnti_code2) |
| 48 DECLARE_TEMPLATE(movnti_rip_relative_code) |
| 49 DECLARE_TEMPLATE(movntdq_code) |
| 50 DECLARE_TEMPLATE(movntdq_code2) |
| 51 DECLARE_TEMPLATE(multiple_movnt_code) |
| 52 DECLARE_TEMPLATE(one_bundle_movnt_code) |
| 53 DECLARE_TEMPLATE(last_movnti_cross_bundle_by_one) |
| 54 #endif |
| 55 |
| 56 class ValidationMovntRewriteTests : public ::testing::Test { |
| 57 protected: |
| 58 const struct NaClValidatorInterface *validator; |
| 59 NaClCPUFeatures *cpu_features; |
| 60 |
| 61 unsigned char code_buffer[CODE_SIZE]; |
| 62 |
| 63 void SetUp() { |
| 64 validator = NaClCreateValidator(); |
| 65 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); |
| 66 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); |
| 67 memset(cpu_features, 0, validator->CPUFeatureSize); |
| 68 validator->SetAllCPUFeatures(cpu_features); |
| 69 memset(code_buffer, NOP, sizeof(code_buffer)); |
| 70 } |
| 71 |
| 72 NaClValidationStatus Validate(uint32_t flags) { |
| 73 return validator->Validate(0, code_buffer, CODE_SIZE, |
| 74 FALSE, /* stubout_mode */ |
| 75 flags, |
| 76 FALSE, /* readonly_test */ |
| 77 cpu_features, |
| 78 NULL, |
| 79 NULL); |
| 80 } |
| 81 |
| 82 void TestTemplate(const TestCode *code, |
| 83 uint32_t flags, |
| 84 NaClValidationStatus expected_status) { |
| 85 size_t before_length = code->before_template_end - code->before_template; |
| 86 size_t after_length = code->after_template_end - code->after_template; |
| 87 EXPECT_EQ(before_length, after_length); |
| 88 memcpy(code_buffer, code->before_template, before_length); |
| 89 NaClValidationStatus status = Validate(flags); |
| 90 EXPECT_EQ(expected_status, status); |
| 91 EXPECT_EQ(0, memcmp(code_buffer, code->after_template, after_length)); |
| 92 } |
| 93 |
| 94 void TestRewrite(const TestCode *code) { |
| 95 TestTemplate(code, 0, NaClValidationSucceeded); |
| 96 } |
| 97 |
| 98 void TearDown() { |
| 99 free(cpu_features); |
| 100 } |
| 101 }; |
| 102 |
| 103 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
| 104 TestTemplate(&t_no_rewrite_code, NACL_DISABLE_NONTEMPORALS_X86, |
| 105 NaClValidationFailed); |
| 106 } |
| 107 |
| 108 #if NACL_BUILD_SUBARCH == 32 |
| 109 |
| 110 TEST_F(ValidationMovntRewriteTests, RewriteMovntq) { |
| 111 TestRewrite(&t_movntq_code); |
| 112 } |
| 113 |
| 114 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 115 TestRewrite(&t_movntdq_code); |
| 116 } |
| 117 |
| 118 #else |
| 119 |
| 120 // In this test, the non-temporal write instruction is not found in x86-64 |
| 121 // nexes in the webstore. Therefore, we will forbid it instead of |
| 122 // rewriting it. |
| 123 TEST_F(ValidationMovntRewriteTests, ForbidOffWebStoreMovntNoRewrite) { |
| 124 TestTemplate(&t_off_webstore_movnt_code, 0, NaClValidationFailed); |
| 125 } |
| 126 |
| 127 TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { |
| 128 TestRewrite(&t_prefetchnta_code); |
| 129 } |
| 130 |
| 131 TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { |
| 132 TestRewrite(&t_movntps_code); |
| 133 } |
| 134 |
| 135 TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { |
| 136 TestRewrite(&t_movnti_code); |
| 137 } |
| 138 |
| 139 TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { |
| 140 TestRewrite(&t_movnti_code2); |
| 141 } |
| 142 |
| 143 TEST_F(ValidationMovntRewriteTests, RewriteMovntiRipRelative) { |
| 144 TestRewrite(&t_movnti_rip_relative_code); |
| 145 } |
| 146 |
| 147 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 148 TestRewrite(&t_movntdq_code); |
| 149 } |
| 150 |
| 151 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { |
| 152 TestRewrite(&t_movntdq_code2); |
| 153 } |
| 154 |
| 155 TEST_F(ValidationMovntRewriteTests, RewriteMultipleMovnt) { |
| 156 TestRewrite(&t_multiple_movnt_code); |
| 157 } |
| 158 |
| 159 TEST_F(ValidationMovntRewriteTests, RewriteOneBundleMovnt) { |
| 160 TestRewrite(&t_one_bundle_movnt_code); |
| 161 } |
| 162 |
| 163 TEST_F(ValidationMovntRewriteTests, RewriteLastMovntiCrossBundleByOne) { |
| 164 TestRewrite(&t_last_movnti_cross_bundle_by_one); |
| 165 } |
| 166 |
| 167 #endif |
| 168 |
| 169 int main(int argc, char *argv[]) { |
| 170 NaClLogModuleInit(); |
| 171 testing::InitGoogleTest(&argc, argv); |
| 172 return RUN_ALL_TESTS(); |
| 173 } |
OLD | NEW |