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 |
| 9 #include "gtest/gtest.h" |
| 10 |
| 11 #include "native_client/src/shared/platform/nacl_log.h" |
| 12 #include "native_client/src/shared/utils/types.h" |
| 13 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 14 |
| 15 #define CODE_SIZE 64 |
| 16 #define NOP 0x90 |
| 17 |
| 18 extern "C" { |
| 19 extern const char no_rewrite_code[]; |
| 20 extern const char no_rewrite_code_end[]; |
| 21 extern const char no_rewrite_code_post_rewrite[]; |
| 22 extern const char no_rewrite_code_post_rewrite_end[]; |
| 23 extern const char off_webstore_movnt_code[]; |
| 24 extern const char off_webstore_movnt_code_end[]; |
| 25 extern const char off_webstore_movnt_code_post_rewrite[]; |
| 26 extern const char off_webstore_movnt_code_post_rewrite_end[]; |
| 27 extern const char prefetchnta_code[]; |
| 28 extern const char prefetchnta_code_end[]; |
| 29 extern const char prefetchnta_code_post_rewrite[]; |
| 30 extern const char prefetchnta_code_post_rewrite_end[]; |
| 31 extern const char movntps_code[]; |
| 32 extern const char movntps_code_end[]; |
| 33 extern const char movntps_code_post_rewrite[]; |
| 34 extern const char movntps_code_post_rewrite_end[]; |
| 35 extern const char movnti_code[]; |
| 36 extern const char movnti_code_end[]; |
| 37 extern const char movnti_code_post_rewrite[]; |
| 38 extern const char movnti_code_post_rewrite_end[]; |
| 39 extern const char movnti_code2[]; |
| 40 extern const char movnti_code2_end[]; |
| 41 extern const char movnti_code2_post_rewrite[]; |
| 42 extern const char movnti_code2_post_rewrite_end[]; |
| 43 extern const char movnti_rip_relative_code[]; |
| 44 extern const char movnti_rip_relative_code_end[]; |
| 45 extern const char movnti_rip_relative_code_post_rewrite[]; |
| 46 extern const char movnti_rip_relative_code_post_rewrite_end[]; |
| 47 extern const char movntdq_code[]; |
| 48 extern const char movntdq_code_end[]; |
| 49 extern const char movntdq_code_post_rewrite[]; |
| 50 extern const char movntdq_code_post_rewrite_end[]; |
| 51 extern const char movntdq_code2[]; |
| 52 extern const char movntdq_code2_end[]; |
| 53 extern const char movntdq_code2_post_rewrite[]; |
| 54 extern const char movntdq_code2_post_rewrite_end[]; |
| 55 extern const char multiple_movnt_code[]; |
| 56 extern const char multiple_movnt_code_end[]; |
| 57 extern const char multiple_movnt_code_post_rewrite[]; |
| 58 extern const char multiple_movnt_code_post_rewrite_end[]; |
| 59 extern const char one_bundle_movnt_code[]; |
| 60 extern const char one_bundle_movnt_code_end[]; |
| 61 extern const char one_bundle_movnt_code_post_rewrite[]; |
| 62 extern const char one_bundle_movnt_code_post_rewrite_end[]; |
| 63 extern const char last_movnti_cross_bundle_by_one[]; |
| 64 extern const char last_movnti_cross_bundle_by_one_end[]; |
| 65 extern const char last_movnti_cross_bundle_by_one_post_rewrite[]; |
| 66 extern const char last_movnti_cross_bundle_by_one_post_rewrite_end[]; |
| 67 } |
| 68 |
| 69 class ValidationMovntRewriteTests : public ::testing::Test { |
| 70 protected: |
| 71 NaClValidationMetadata *metadata_ptr; |
| 72 const struct NaClValidatorInterface *validator; |
| 73 NaClCPUFeatures *cpu_features; |
| 74 |
| 75 unsigned char code_buffer[CODE_SIZE]; |
| 76 |
| 77 void SetUp() { |
| 78 metadata_ptr = NULL; |
| 79 validator = NaClCreateValidator(); |
| 80 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); |
| 81 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); |
| 82 memset(cpu_features, 0, validator->CPUFeatureSize); |
| 83 validator->SetAllCPUFeatures(cpu_features); |
| 84 memset(code_buffer, NOP, sizeof(code_buffer)); |
| 85 } |
| 86 |
| 87 NaClValidationStatus Validate(uint32_t flags) { |
| 88 return validator->Validate(0, code_buffer, CODE_SIZE, |
| 89 FALSE, /* stubout_mode */ |
| 90 flags, |
| 91 FALSE, /* readonly_test */ |
| 92 cpu_features, |
| 93 metadata_ptr, |
| 94 NULL); |
| 95 } |
| 96 |
| 97 void TearDown() { |
| 98 free(cpu_features); |
| 99 } |
| 100 }; |
| 101 |
| 102 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
| 103 size_t len = no_rewrite_code_end - no_rewrite_code; |
| 104 size_t post_len = |
| 105 no_rewrite_code_post_rewrite_end - no_rewrite_code_post_rewrite; |
| 106 EXPECT_EQ(len, post_len); |
| 107 memcpy(code_buffer, no_rewrite_code, len); |
| 108 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
| 109 EXPECT_EQ(NaClValidationFailed, status); |
| 110 EXPECT_EQ(0, memcmp(code_buffer, no_rewrite_code_post_rewrite, post_len)); |
| 111 } |
| 112 |
| 113 /* |
| 114 * In this test, the non-temporal write instruction is not found in x86-64 nexes |
| 115 * in the webstore. Therefore, we will forbid it instead of rewriting it. |
| 116 */ |
| 117 TEST_F(ValidationMovntRewriteTests, ForbidOffWebStoreMovntNoRewrite) { |
| 118 size_t len = off_webstore_movnt_code_end - off_webstore_movnt_code; |
| 119 size_t post_len = off_webstore_movnt_code_post_rewrite_end - |
| 120 off_webstore_movnt_code_post_rewrite; |
| 121 EXPECT_EQ(len, post_len); |
| 122 memcpy(code_buffer, no_rewrite_code, len); |
| 123 NaClValidationStatus status = Validate(0); |
| 124 EXPECT_EQ(NaClValidationFailed, status); |
| 125 EXPECT_EQ(0, memcmp(off_webstore_movnt_code, |
| 126 off_webstore_movnt_code_post_rewrite, post_len)); |
| 127 } |
| 128 |
| 129 TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { |
| 130 size_t len = prefetchnta_code_end - prefetchnta_code; |
| 131 size_t post_len = |
| 132 prefetchnta_code_post_rewrite_end - prefetchnta_code_post_rewrite; |
| 133 EXPECT_EQ(len, post_len); |
| 134 memcpy(code_buffer, prefetchnta_code, len); |
| 135 NaClValidationStatus status = Validate(0); |
| 136 EXPECT_EQ(NaClValidationSucceeded, status); |
| 137 EXPECT_EQ(0, memcmp(code_buffer, prefetchnta_code_post_rewrite, post_len)); |
| 138 } |
| 139 |
| 140 TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { |
| 141 size_t len = movntps_code_end - movntps_code; |
| 142 size_t post_len = |
| 143 movntps_code_post_rewrite_end - movntps_code_post_rewrite; |
| 144 EXPECT_EQ(len, post_len); |
| 145 memcpy(code_buffer, movntps_code, len); |
| 146 NaClValidationStatus status = Validate(0); |
| 147 EXPECT_EQ(NaClValidationSucceeded, status); |
| 148 EXPECT_EQ(0, memcmp(code_buffer, movntps_code_post_rewrite, post_len)); |
| 149 } |
| 150 |
| 151 TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { |
| 152 size_t len = movnti_code_end - movnti_code; |
| 153 size_t post_len = |
| 154 movnti_code_post_rewrite_end - movnti_code_post_rewrite; |
| 155 EXPECT_EQ(len, post_len); |
| 156 memcpy(code_buffer, movnti_code, len); |
| 157 NaClValidationStatus status = Validate(0); |
| 158 EXPECT_EQ(NaClValidationSucceeded, status); |
| 159 EXPECT_EQ(0, memcmp(code_buffer, movnti_code_post_rewrite, post_len)); |
| 160 } |
| 161 |
| 162 TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { |
| 163 size_t len = movnti_code2_end - movnti_code2; |
| 164 size_t post_len = |
| 165 movnti_code2_post_rewrite_end - movnti_code2_post_rewrite; |
| 166 EXPECT_EQ(len, post_len); |
| 167 memcpy(code_buffer, movnti_code2, len); |
| 168 NaClValidationStatus status = Validate(0); |
| 169 EXPECT_EQ(NaClValidationSucceeded, status); |
| 170 EXPECT_EQ(0, memcmp(code_buffer, movnti_code2_post_rewrite, post_len)); |
| 171 } |
| 172 |
| 173 TEST_F(ValidationMovntRewriteTests, RewriteMovntiRipRelative) { |
| 174 size_t len = movnti_rip_relative_code_end - movnti_rip_relative_code; |
| 175 size_t post_len = movnti_rip_relative_code_post_rewrite_end - |
| 176 movnti_rip_relative_code_post_rewrite; |
| 177 EXPECT_EQ(len, post_len); |
| 178 memcpy(code_buffer, movnti_rip_relative_code, len); |
| 179 NaClValidationStatus status = Validate(0); |
| 180 EXPECT_EQ(NaClValidationSucceeded, status); |
| 181 EXPECT_EQ(0, memcmp(code_buffer, |
| 182 movnti_rip_relative_code_post_rewrite, post_len)); |
| 183 } |
| 184 |
| 185 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 186 size_t len = movntdq_code_end - movntdq_code; |
| 187 size_t post_len = movntdq_code_post_rewrite_end - movntdq_code_post_rewrite; |
| 188 EXPECT_EQ(len, post_len); |
| 189 memcpy(code_buffer, movntdq_code, len); |
| 190 NaClValidationStatus status = Validate(0); |
| 191 EXPECT_EQ(NaClValidationSucceeded, status); |
| 192 EXPECT_EQ(0, memcmp(code_buffer, movntdq_code_post_rewrite, post_len)); |
| 193 } |
| 194 |
| 195 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { |
| 196 size_t len = movntdq_code2_end - movntdq_code2; |
| 197 size_t post_len = movntdq_code2_post_rewrite_end - movntdq_code2_post_rewrite; |
| 198 EXPECT_EQ(len, post_len); |
| 199 memcpy(code_buffer, movntdq_code2, len); |
| 200 NaClValidationStatus status = Validate(0); |
| 201 EXPECT_EQ(NaClValidationSucceeded, status); |
| 202 EXPECT_EQ(0, memcmp(code_buffer, movntdq_code2_post_rewrite, post_len)); |
| 203 } |
| 204 |
| 205 TEST_F(ValidationMovntRewriteTests, RewriteMultipleMovnt) { |
| 206 size_t len = multiple_movnt_code_end - multiple_movnt_code; |
| 207 size_t post_len = |
| 208 multiple_movnt_code_post_rewrite_end - multiple_movnt_code_post_rewrite; |
| 209 EXPECT_EQ(len, post_len); |
| 210 memcpy(code_buffer, multiple_movnt_code, len); |
| 211 NaClValidationStatus status = Validate(0); |
| 212 EXPECT_EQ(NaClValidationSucceeded, status); |
| 213 EXPECT_EQ(0, memcmp(code_buffer, multiple_movnt_code_post_rewrite, post_len)); |
| 214 } |
| 215 |
| 216 TEST_F(ValidationMovntRewriteTests, RewriteOneBundleMovnt) { |
| 217 size_t len = one_bundle_movnt_code_end - one_bundle_movnt_code; |
| 218 size_t post_len = one_bundle_movnt_code_post_rewrite_end - |
| 219 one_bundle_movnt_code_post_rewrite; |
| 220 EXPECT_EQ(len, post_len); |
| 221 memcpy(code_buffer, one_bundle_movnt_code, len); |
| 222 NaClValidationStatus status = Validate(0); |
| 223 EXPECT_EQ(NaClValidationSucceeded, status); |
| 224 EXPECT_EQ(0, memcmp(code_buffer, one_bundle_movnt_code_post_rewrite, |
| 225 post_len)); |
| 226 } |
| 227 |
| 228 TEST_F(ValidationMovntRewriteTests, RewriteLastMovntiCrossBundleByOne) { |
| 229 size_t len = last_movnti_cross_bundle_by_one_end - |
| 230 last_movnti_cross_bundle_by_one; |
| 231 size_t post_len = last_movnti_cross_bundle_by_one_post_rewrite_end - |
| 232 last_movnti_cross_bundle_by_one_post_rewrite; |
| 233 EXPECT_EQ(len, post_len); |
| 234 memcpy(code_buffer, last_movnti_cross_bundle_by_one, len); |
| 235 NaClValidationStatus status = Validate(0); |
| 236 EXPECT_EQ(NaClValidationSucceeded, status); |
| 237 EXPECT_EQ(0, memcmp(code_buffer, last_movnti_cross_bundle_by_one_post_rewrite, |
| 238 post_len)); |
| 239 } |
| 240 |
| 241 int main(int argc, char *argv[]) { |
| 242 // The IllegalInst test touches the log mutex deep inside the validator. |
| 243 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 244 // initialized. |
| 245 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 246 NaClLogModuleInit(); |
| 247 testing::InitGoogleTest(&argc, argv); |
| 248 return RUN_ALL_TESTS(); |
| 249 } |
OLD | NEW |