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 32 |
| 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 movntdq_code[]; |
| 29 extern const char movntdq_code_post_rewrite[]; |
| 30 extern const char movntdq_code2[]; |
| 31 extern const char movntdq_code2_post_rewrite[]; |
| 32 } |
| 33 |
| 34 class ValidationMovntRewriteTests : public ::testing::Test { |
| 35 protected: |
| 36 NaClValidationMetadata *metadata_ptr; |
| 37 const struct NaClValidatorInterface *validator; |
| 38 NaClCPUFeatures *cpu_features; |
| 39 |
| 40 unsigned char code_buffer[CODE_SIZE]; |
| 41 |
| 42 void SetUp() { |
| 43 metadata_ptr = NULL; |
| 44 validator = NaClCreateValidator(); |
| 45 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); |
| 46 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); |
| 47 memset(cpu_features, 0, validator->CPUFeatureSize); |
| 48 validator->SetAllCPUFeatures(cpu_features); |
| 49 memset(code_buffer, NOP, sizeof(code_buffer)); |
| 50 } |
| 51 |
| 52 NaClValidationStatus Validate(uint32_t flags) { |
| 53 return validator->Validate(0, code_buffer, 32, |
| 54 FALSE, /* stubout_mode */ |
| 55 flags, |
| 56 FALSE, /* readonly_test */ |
| 57 cpu_features, |
| 58 metadata_ptr, |
| 59 NULL); |
| 60 } |
| 61 |
| 62 void TearDown() { |
| 63 free(cpu_features); |
| 64 } |
| 65 }; |
| 66 |
| 67 TEST_F(ValidationMovntRewriteTests, DisableNonTemporalsNoRewrite) { |
| 68 memcpy(code_buffer, no_rewrite_code, strlen(no_rewrite_code)); |
| 69 NaClValidationStatus status = Validate(NACL_DISABLE_NONTEMPORALS_X86); |
| 70 EXPECT_EQ(NaClValidationFailed, status); |
| 71 EXPECT_EQ(0, memcmp(code_buffer, |
| 72 no_rewrite_code_post_rewrite, |
| 73 strlen(no_rewrite_code_post_rewrite))); |
| 74 } |
| 75 |
| 76 TEST_F(ValidationMovntRewriteTests, RewritePrefetchnta) { |
| 77 memcpy(code_buffer, prefetchnta_code, strlen(prefetchnta_code)); |
| 78 NaClValidationStatus status = Validate(0); |
| 79 EXPECT_EQ(NaClValidationSucceeded, status); |
| 80 EXPECT_EQ(0, memcmp(code_buffer, |
| 81 prefetchnta_code_post_rewrite, |
| 82 strlen(prefetchnta_code_post_rewrite))); |
| 83 } |
| 84 |
| 85 TEST_F(ValidationMovntRewriteTests, RewriteMovntps) { |
| 86 memcpy(code_buffer, movntps_code, strlen(movntps_code)); |
| 87 NaClValidationStatus status = Validate(0); |
| 88 EXPECT_EQ(NaClValidationSucceeded, status); |
| 89 EXPECT_EQ(0, memcmp(code_buffer, |
| 90 movntps_code_post_rewrite, |
| 91 strlen(movntps_code_post_rewrite))); |
| 92 } |
| 93 |
| 94 TEST_F(ValidationMovntRewriteTests, RewriteMovnti) { |
| 95 memcpy(code_buffer, movnti_code, strlen(movnti_code)); |
| 96 NaClValidationStatus status = Validate(0); |
| 97 EXPECT_EQ(NaClValidationSucceeded, status); |
| 98 EXPECT_EQ(0, memcmp(code_buffer, |
| 99 movnti_code_post_rewrite, |
| 100 strlen(movnti_code_post_rewrite))); |
| 101 } |
| 102 |
| 103 TEST_F(ValidationMovntRewriteTests, RewriteMovnti2) { |
| 104 memcpy(code_buffer, movnti_code2, strlen(movnti_code2)); |
| 105 NaClValidationStatus status = Validate(0); |
| 106 EXPECT_EQ(NaClValidationSucceeded, status); |
| 107 EXPECT_EQ(0, memcmp(code_buffer, |
| 108 movnti_code2_post_rewrite, |
| 109 strlen(movnti_code2_post_rewrite))); |
| 110 } |
| 111 |
| 112 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq) { |
| 113 memcpy(code_buffer, movntdq_code, strlen(movntdq_code)); |
| 114 NaClValidationStatus status = Validate(0); |
| 115 EXPECT_EQ(NaClValidationSucceeded, status); |
| 116 EXPECT_EQ(0, memcmp(code_buffer, |
| 117 movntdq_code_post_rewrite, |
| 118 strlen(movntdq_code_post_rewrite))); |
| 119 } |
| 120 |
| 121 TEST_F(ValidationMovntRewriteTests, RewriteMovntdq2) { |
| 122 memcpy(code_buffer, movntdq_code2, strlen(movntdq_code2)); |
| 123 NaClValidationStatus status = Validate(0); |
| 124 EXPECT_EQ(NaClValidationSucceeded, status); |
| 125 EXPECT_EQ(0, memcmp(code_buffer, |
| 126 movntdq_code2_post_rewrite, |
| 127 strlen(movntdq_code2_post_rewrite))); |
| 128 } |
| 129 |
| 130 int main(int argc, char *argv[]) { |
| 131 // The IllegalInst test touches the log mutex deep inside the validator. |
| 132 // This causes an SEH exception to be thrown on Windows if the mutex is not |
| 133 // initialized. |
| 134 // http://code.google.com/p/nativeclient/issues/detail?id=1696 |
| 135 NaClLogModuleInit(); |
| 136 testing::InitGoogleTest(&argc, argv); |
| 137 return RUN_ALL_TESTS(); |
| 138 } |
OLD | NEW |