OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2012 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/trusted/validator/ncvalidate.h" | |
11 #include "native_client/src/trusted/validator/validation_cache.h" | |
12 | |
13 #if NACL_WINDOWS | |
14 # define UNREFERENCED_PARAMETER(P) (P) | |
Mark Seaborn
2012/03/01 00:26:20
Why is this here? Use src/include/nacl_compiler_a
Nick Bray (chromium)
2012/03/01 01:25:16
Done.
| |
15 #else | |
16 # define UNREFERENCED_PARAMETER(P) do { (void) P; } while (0) | |
17 #endif | |
18 | |
19 const char nop[33] = | |
20 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | |
21 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; | |
22 | |
23 // ret | |
24 const char ret[33] = | |
25 "\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | |
26 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; | |
27 | |
28 // pblendw $0xc0,%xmm0,%xmm2 | |
29 const char sse41[33] = | |
30 "\x66\x0f\x3a\x0e\xd0\xc0\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | |
31 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; | |
32 | |
33 const int code_size = 32; | |
34 | |
35 struct MockContext { | |
36 /* Sanity check that we're getting the right object. */ | |
37 int marker; | |
38 int query_result; | |
39 int set_validates_expected; | |
40 int query_destroyed; | |
41 }; | |
42 | |
43 struct MockQuery { | |
44 /* Sanity check that we're getting the right object. */ | |
45 int marker; | |
46 int state; | |
47 int add_count; | |
48 MockContext *context; | |
49 }; | |
50 | |
51 void *MockCreateQuery(void *context) { | |
52 MockContext *mcontext = (MockContext *) context; | |
53 MockQuery *mquery = (MockQuery *) malloc(sizeof(MockQuery)); | |
54 EXPECT_EQ(31, mcontext->marker); | |
55 mquery->marker = 37; | |
56 mquery->state = 0; | |
57 mquery->add_count = 0; | |
58 mquery->context = mcontext; | |
59 return mquery; | |
60 } | |
61 | |
62 void MockAddData(void *query, const unsigned char *data, size_t length) { | |
63 MockQuery *mquery = (MockQuery *) query; | |
64 ASSERT_EQ(37, mquery->marker); | |
65 UNREFERENCED_PARAMETER(data); | |
66 EXPECT_EQ(0, mquery->state); | |
67 /* Small data is supicious. */ | |
68 EXPECT_LE((size_t) 2, length); | |
69 mquery->add_count += 1; | |
70 } | |
71 | |
72 int MockDoQuery(void *query) { | |
73 MockQuery *mquery = (MockQuery *) query; | |
74 EXPECT_EQ(37, mquery->marker); | |
75 EXPECT_EQ(0, mquery->state); | |
76 /* Less than two pieces of data is suspicious. */ | |
77 EXPECT_LE(2, mquery->add_count); | |
78 mquery->state = 1; | |
79 return mquery->context->query_result; | |
80 } | |
81 | |
82 void MockSetValidates(void *query) { | |
83 MockQuery *mquery = (MockQuery *) query; | |
84 ASSERT_EQ(37, mquery->marker); | |
85 EXPECT_EQ(1, mquery->state); | |
86 EXPECT_EQ(1, mquery->context->set_validates_expected); | |
87 mquery->state = 2; | |
88 } | |
89 | |
90 void MockDestroyQuery(void *query) { | |
91 MockQuery *mquery = (MockQuery *) query; | |
92 ASSERT_EQ(37, mquery->marker); | |
93 EXPECT_EQ(mquery->context->set_validates_expected ? 2 : 1, mquery->state); | |
94 mquery->context->query_destroyed = 1; | |
95 free(mquery); | |
96 } | |
97 | |
98 class ValidationCachingInterfaceTests : public ::testing::Test { | |
99 protected: | |
100 MockContext context; | |
101 NaClValidationCache cache; | |
102 NaClCPUFeatures cpu_features; | |
103 int bundle_size; | |
104 | |
105 unsigned char code_buffer[32]; | |
106 | |
107 void SetUp() { | |
108 context.marker = 31; | |
109 context.query_result = 1; | |
110 context.set_validates_expected = 0; | |
111 context.query_destroyed = 0; | |
112 | |
113 cache.context = &context; | |
114 cache.CreateQuery = MockCreateQuery; | |
115 cache.AddData = MockAddData; | |
116 cache.DoQuery = MockDoQuery; | |
117 cache.SetValidates = MockSetValidates; | |
118 cache.DestroyQuery = MockDestroyQuery; | |
119 | |
120 NaClSetAllCPUFeatures(&cpu_features); | |
121 | |
122 bundle_size = 32; | |
Mark Seaborn
2012/03/01 00:26:20
Rename the global code_size to bundle_size?
Nick Bray (chromium)
2012/03/01 01:25:16
bundle_size and code_size only happen to be the sa
| |
123 | |
124 memset(code_buffer, 0x90, 32); | |
125 } | |
126 | |
127 NaClValidationStatus Validate() { | |
128 return NACL_SUBARCH_NAME(ApplyValidator, | |
129 NACL_TARGET_ARCH, | |
130 NACL_TARGET_SUBARCH)( | |
131 NACL_SB_DEFAULT, | |
132 NaClApplyCodeValidation, | |
133 0, code_buffer, 32, | |
134 bundle_size, &cpu_features, | |
135 &cache); | |
136 } | |
137 }; | |
138 | |
139 TEST_F(ValidationCachingInterfaceTests, Sanity) { | |
140 void *query = cache.CreateQuery(cache.context); | |
141 cache.AddData(query, 0, 6); | |
142 cache.AddData(query, 0, 128); | |
143 EXPECT_EQ(1, cache.DoQuery(query)); | |
144 cache.DestroyQuery(query); | |
145 EXPECT_EQ(1, context.query_destroyed); | |
146 } | |
147 | |
148 TEST_F(ValidationCachingInterfaceTests, NoCache) { | |
149 NaClValidationStatus status; | |
150 status = NACL_SUBARCH_NAME(ApplyValidator, | |
151 NACL_TARGET_ARCH, | |
152 NACL_TARGET_SUBARCH)( | |
153 NACL_SB_DEFAULT, | |
154 NaClApplyCodeValidation, | |
155 0, code_buffer, code_size, | |
156 bundle_size, &cpu_features, | |
157 NULL); | |
158 EXPECT_EQ(NaClValidationSucceeded, status); | |
159 } | |
160 | |
161 TEST_F(ValidationCachingInterfaceTests, CacheHit) { | |
Mark Seaborn
2012/03/01 00:26:20
You can check cache hit more thoroughly by passing
Nick Bray (chromium)
2012/03/01 01:25:16
Done.
| |
162 NaClValidationStatus status; | |
163 status = Validate(); | |
164 EXPECT_EQ(NaClValidationSucceeded, status); | |
165 EXPECT_EQ(1, context.query_destroyed); | |
166 } | |
167 | |
168 TEST_F(ValidationCachingInterfaceTests, CacheMiss) { | |
169 NaClValidationStatus status; | |
170 context.query_result = 0; | |
171 context.set_validates_expected = 1; | |
172 status = Validate(); | |
173 EXPECT_EQ(NaClValidationSucceeded, status); | |
174 EXPECT_EQ(1, context.query_destroyed); | |
175 } | |
176 | |
177 TEST_F(ValidationCachingInterfaceTests, SSE4Allowed) { | |
178 NaClValidationStatus status; | |
179 memcpy(code_buffer, sse41, 32); | |
180 context.query_result = 0; | |
181 context.set_validates_expected = 1; | |
182 status = Validate(); | |
183 EXPECT_EQ(NaClValidationSucceeded, status); | |
184 EXPECT_EQ(1, context.query_destroyed); | |
185 } | |
186 | |
187 TEST_F(ValidationCachingInterfaceTests, SSE4Stubout) { | |
188 NaClValidationStatus status; | |
189 memcpy(code_buffer, sse41, 32); | |
190 context.query_result = 0; | |
191 NaClSetCPUFeature(&cpu_features, NaClCPUFeature_SSE41, 0); | |
192 status = Validate(); | |
193 EXPECT_EQ(NaClValidationSucceeded, status); | |
194 EXPECT_EQ(1, context.query_destroyed); | |
195 } | |
196 | |
197 TEST_F(ValidationCachingInterfaceTests, IllegalInst) { | |
198 NaClValidationStatus status; | |
199 memcpy(code_buffer, ret, 32); | |
Mark Seaborn
2012/03/01 00:26:20
32 -> code_size?
Nick Bray (chromium)
2012/03/01 01:25:16
Done.
| |
200 context.query_result = 0; | |
201 status = Validate(); | |
Mark Seaborn
2012/03/01 00:26:20
This is C++ so you can write
NaClValidationStatus
Nick Bray (chromium)
2012/03/01 01:25:16
Done.
| |
202 EXPECT_EQ(NaClValidationFailed, status); | |
203 EXPECT_EQ(1, context.query_destroyed); | |
Mark Seaborn
2012/03/01 00:26:20
More conventional is:
EXPECT_EQ(context.query_dest
Nick Bray (chromium)
2012/03/01 01:25:16
No. EXPECT_EQ(EXPECTED, ACTUAL);
| |
204 } | |
205 | |
206 // Test driver function. | |
207 int main(int argc, char *argv[]) { | |
208 // The IllegalInst test touches the log mutex deep inside the validator. | |
209 // This causes an SEH exception to be thrown on Windows if the mutex is not | |
Mark Seaborn
2012/03/01 00:26:20
Unnecessary detail? Just say: "Initialise the log
Nick Bray (chromium)
2012/03/01 01:25:16
Detail is good in this case, it may provide breadc
| |
210 // initialized. | |
211 // http://code.google.com/p/nativeclient/issues/detail?id=1696 | |
212 NaClLogModuleInit(); | |
213 testing::InitGoogleTest(&argc, argv); | |
214 return RUN_ALL_TESTS(); | |
215 } | |
OLD | NEW |