OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 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 "gtest/gtest.h" | |
8 | |
9 #include "native_client/src/shared/platform/nacl_log.h" | |
10 #include "native_client/src/shared/utils/types.h" | |
11 #include "native_client/src/trusted/validator/ncvalidate.h" | |
12 | |
13 #define CODE_SIZE 32 | |
14 #define NOP 0x90 | |
15 #define MOVNTI_CODE_SIZE 8 | |
16 | |
17 // mov %edi,%edi; movnti %rax,0x68(%r15,%rdi,1) | |
18 const char* movnti_code = "\x89\xff\x49\x0f\xc3\x44\x3f\x68"; | |
19 | |
20 class ValidationPNaClModeTests : public ::testing::Test { | |
21 protected: | |
22 NaClValidationMetadata *metadata_ptr; | |
23 const struct NaClValidatorInterface *validator; | |
24 NaClCPUFeatures *cpu_features; | |
25 | |
26 unsigned char code_buffer[CODE_SIZE]; | |
27 | |
28 void SetUp() { | |
29 metadata_ptr = NULL; | |
30 validator = NaClCreateValidator(); | |
31 cpu_features = (NaClCPUFeatures *) malloc(validator->CPUFeatureSize); | |
32 EXPECT_NE(cpu_features, (NaClCPUFeatures *) NULL); | |
33 memset(cpu_features, 0, validator->CPUFeatureSize); | |
34 validator->SetAllCPUFeatures(cpu_features); | |
35 memset(code_buffer, NOP, sizeof(code_buffer)); | |
36 } | |
37 | |
38 NaClValidationStatus Validate(int pnacl_mode) { | |
39 return validator->Validate(0, code_buffer, 32, | |
40 FALSE, /* stubout_mode */ | |
41 pnacl_mode, | |
42 FALSE, /* readonly_test */ | |
43 cpu_features, | |
44 metadata_ptr, | |
45 NULL); | |
46 } | |
47 | |
48 void TearDown() { | |
49 free(cpu_features); | |
50 } | |
51 }; | |
52 | |
53 TEST_F(ValidationPNaClModeTests, movnti_non_pnacl_mode) { | |
54 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | |
55 NaClValidationStatus status = Validate(0); | |
56 // In non-pnacl mode: use the original validation rule, i.e., allow | |
57 // non-temporal writes. In future, we will do rewriting. | |
gdeepti
2015/07/28 03:08:57
Add a todo for rewriting.
ruiq
2015/07/28 04:58:55
There is a TODO in the place where we should do re
| |
58 EXPECT_EQ(NaClValidationSucceeded, status); | |
59 // Code should not change. | |
60 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | |
gdeepti
2015/07/28 03:08:58
As we are not rewriting yet, seems redundant to ch
ruiq
2015/07/28 04:58:55
This is a sanity check. It was added because the c
| |
61 } | |
62 | |
63 TEST_F(ValidationPNaClModeTests, movnti_pnacl_mode) { | |
64 memcpy(code_buffer, movnti_code, MOVNTI_CODE_SIZE); | |
65 NaClValidationStatus status = Validate(1); | |
66 // In pnacl mode: forbid non-temporal writes. | |
67 EXPECT_EQ(NaClValidationFailed, status); | |
68 // Code should not change. | |
69 EXPECT_EQ(0, memcmp(code_buffer, movnti_code, MOVNTI_CODE_SIZE)); | |
70 } | |
71 | |
72 int main(int argc, char *argv[]) { | |
73 // The IllegalInst test touches the log mutex deep inside the validator. | |
74 // This causes an SEH exception to be thrown on Windows if the mutex is not | |
75 // initialized. | |
76 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | |
77 NaClLogModuleInit(); | |
78 testing::InitGoogleTest(&argc, argv); | |
79 return RUN_ALL_TESTS(); | |
80 } | |
OLD | NEW |