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 64 |
| 15 #define NOP 0x90 |
| 16 |
| 17 extern "C" { |
| 18 extern const char no_rewrite_code[]; |
| 19 extern const char no_rewrite_code_post_rewrite[]; |
| 20 extern const char prefetchnta_code[]; |
| 21 extern const char prefetchnta_code_post_rewrite[]; |
| 22 extern const char movntps_code[]; |
| 23 extern const char movntps_code_post_rewrite[]; |
| 24 extern const char movnti_code[]; |
| 25 extern const char movnti_code_post_rewrite[]; |
| 26 extern const char movnti_code2[]; |
| 27 extern const char movnti_code2_post_rewrite[]; |
| 28 extern const char movnti_rip_relative_code[]; |
| 29 extern const char movnti_rip_relative_code_post_rewrite[]; |
| 30 extern const char movntdq_code[]; |
| 31 extern const char movntdq_code_post_rewrite[]; |
| 32 extern const char movntdq_code2[]; |
| 33 extern const char movntdq_code2_post_rewrite[]; |
| 34 extern const char multiple_movnt_code[]; |
| 35 extern const char multiple_movnt_code_post_rewrite[]; |
| 36 extern const char one_bundle_movnt_code[]; |
| 37 extern const char one_bundle_movnt_code_post_rewrite[]; |
| 38 extern const char last_movnti_cross_bundle_by_one[]; |
| 39 extern const char last_movnti_cross_bundle_by_one_post_rewrite[]; |
| 40 } |
| 41 |
| 42 class ValidationMovntRewriteTests : public ::testing::Test { |
| 43 protected: |
| 44 NaClValidationMetadata *metadata_ptr; |
| 45 const struct NaClValidatorInterface *validator; |
| 46 NaClCPUFeatures *cpu_features; |
| 47 |
| 48 unsigned char code_buffer[CODE_SIZE]; |
| 49 |
| 50 void SetUp() { |
| 51 metadata_ptr = NULL; |
| 52 validator = NaClCreateValidator(); |
| 53 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); |
| 54 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); |
| 55 memset(cpu_features, 0, validator->CPUFeatureSize); |
| 56 validator->SetAllCPUFeatures(cpu_features); |
| 57 memset(code_buffer, NOP, sizeof(code_buffer)); |
| 58 } |
| 59 |
| 60 NaClValidationStatus Validate(uint32_t flags) { |
| 61 return validator->Validate(0, code_buffer, CODE_SIZE, |
| 62 FALSE, /* stubout_mode */ |
| 63 flags, |
| 64 FALSE, /* readonly_test */ |
| 65 cpu_features, |
| 66 metadata_ptr, |
| 67 NULL); |
| 68 } |
| 69 |
| 70 void TearDown() { |
| 71 free(cpu_features); |
| 72 } |
| 73 }; |
| 74 |
| 75 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
| 76 memcpy(code_buffer, no_rewrite_code, strlen(no_rewrite_code)); |
| 77 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
| 78 EXPECT_EQ(NaClValidationFailed, status); |
| 79 EXPECT_EQ(0, memcmp(code_buffer, |
| 80 no_rewrite_code_post_rewrite, |
| 81 strlen(no_rewrite_code_post_rewrite))); |
| 82 } |
| 83 |
| 84 TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { |
| 85 memcpy(code_buffer, prefetchnta_code, strlen(prefetchnta_code)); |
| 86 NaClValidationStatus status = Validate(0); |
| 87 EXPECT_EQ(NaClValidationSucceeded, status); |
| 88 EXPECT_EQ(0, memcmp(code_buffer, |
| 89 prefetchnta_code_post_rewrite, |
| 90 strlen(prefetchnta_code_post_rewrite))); |
| 91 } |
| 92 |
| 93 TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { |
| 94 memcpy(code_buffer, movntps_code, strlen(movntps_code)); |
| 95 NaClValidationStatus status = Validate(0); |
| 96 EXPECT_EQ(NaClValidationSucceeded, status); |
| 97 EXPECT_EQ(0, memcmp(code_buffer, |
| 98 movntps_code_post_rewrite, |
| 99 strlen(movntps_code_post_rewrite))); |
| 100 } |
| 101 |
| 102 TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { |
| 103 memcpy(code_buffer, movnti_code, strlen(movnti_code)); |
| 104 NaClValidationStatus status = Validate(0); |
| 105 EXPECT_EQ(NaClValidationSucceeded, status); |
| 106 EXPECT_EQ(0, memcmp(code_buffer, |
| 107 movnti_code_post_rewrite, |
| 108 strlen(movnti_code_post_rewrite))); |
| 109 } |
| 110 |
| 111 TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { |
| 112 memcpy(code_buffer, movnti_code2, strlen(movnti_code2)); |
| 113 NaClValidationStatus status = Validate(0); |
| 114 EXPECT_EQ(NaClValidationSucceeded, status); |
| 115 EXPECT_EQ(0, memcmp(code_buffer, |
| 116 movnti_code2_post_rewrite, |
| 117 strlen(movnti_code2_post_rewrite))); |
| 118 } |
| 119 |
| 120 TEST_F(ValidationMovntRewriteTests, RewriteMovntiRipRelative) { |
| 121 memcpy(code_buffer, movnti_rip_relative_code, |
| 122 strlen(movnti_rip_relative_code)); |
| 123 NaClValidationStatus status = Validate(0); |
| 124 EXPECT_EQ(NaClValidationSucceeded, status); |
| 125 EXPECT_EQ(0, memcmp(code_buffer, |
| 126 movnti_rip_relative_code_post_rewrite, |
| 127 strlen(movnti_rip_relative_code_post_rewrite))); |
| 128 } |
| 129 |
| 130 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 131 memcpy(code_buffer, movntdq_code, strlen(movntdq_code)); |
| 132 NaClValidationStatus status = Validate(0); |
| 133 EXPECT_EQ(NaClValidationSucceeded, status); |
| 134 EXPECT_EQ(0, memcmp(code_buffer, |
| 135 movntdq_code_post_rewrite, |
| 136 strlen(movntdq_code_post_rewrite))); |
| 137 } |
| 138 |
| 139 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { |
| 140 memcpy(code_buffer, movntdq_code2, strlen(movntdq_code2)); |
| 141 NaClValidationStatus status = Validate(0); |
| 142 EXPECT_EQ(NaClValidationSucceeded, status); |
| 143 EXPECT_EQ(0, memcmp(code_buffer, |
| 144 movntdq_code2_post_rewrite, |
| 145 strlen(movntdq_code2_post_rewrite))); |
| 146 } |
| 147 |
| 148 TEST_F(ValidationMovntRewriteTests, RewriteMultipleMovnt) { |
| 149 memcpy(code_buffer, multiple_movnt_code, strlen(multiple_movnt_code)); |
| 150 NaClValidationStatus status = Validate(0); |
| 151 EXPECT_EQ(NaClValidationSucceeded, status); |
| 152 EXPECT_EQ(0, memcmp(code_buffer, |
| 153 multiple_movnt_code_post_rewrite, |
| 154 strlen(multiple_movnt_code_post_rewrite))); |
| 155 } |
| 156 |
| 157 TEST_F(ValidationMovntRewriteTests, RewriteOneBundleMovnt) { |
| 158 memcpy(code_buffer, one_bundle_movnt_code, strlen(one_bundle_movnt_code)); |
| 159 NaClValidationStatus status = Validate(0); |
| 160 EXPECT_EQ(NaClValidationSucceeded, status); |
| 161 EXPECT_EQ(0, memcmp(code_buffer, |
| 162 one_bundle_movnt_code_post_rewrite, |
| 163 strlen(one_bundle_movnt_code_post_rewrite))); |
| 164 } |
| 165 |
| 166 TEST_F(ValidationMovntRewriteTests, RewriteLastMovntiCrossBundleByOne) { |
| 167 memcpy(code_buffer, last_movnti_cross_bundle_by_one, |
| 168 strlen(last_movnti_cross_bundle_by_one)); |
| 169 NaClValidationStatus status = Validate(0); |
| 170 EXPECT_EQ(NaClValidationSucceeded, status); |
| 171 EXPECT_EQ(0, memcmp(code_buffer, |
| 172 last_movnti_cross_bundle_by_one_post_rewrite, |
| 173 strlen(last_movnti_cross_bundle_by_one_post_rewrite))); |
| 174 } |
| 175 |
| 176 int main(int argc, char *argv[]) { |
| 177 // The IllegalInst test touches the log mutex deep inside the validator. |
| 178 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 179 // initialized. |
| 180 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 181 NaClLogModuleInit(); |
| 182 testing::InitGoogleTest(&argc, argv); |
| 183 return RUN_ALL_TESTS(); |
| 184 } |
OLD | NEW |