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/trusted/validator/ncvalidate.h" | |
10 #include "native_client/src/trusted/validator/validation_cache.h" | |
11 | |
12 #if NACL_WINDOWS | |
13 # define UNREFERENCED_PARAMETER(P) (P) | |
14 #else | |
15 # define UNREFERENCED_PARAMETER(P) do { (void) P; } while (0) | |
16 #endif | |
17 | |
18 const char * nop = | |
Mark Seaborn
2012/02/29 21:33:34
Spacing style. Use an array: nops[32] = "..."
Nick Bray (chromium)
2012/02/29 22:58:09
Done.
| |
19 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | |
20 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; | |
21 | |
22 // ret | |
23 const char * ret = | |
24 "\xc3\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | |
25 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; | |
26 | |
27 // pblendw $0xc0,%xmm0,%xmm2 | |
28 const char * sse4 = | |
Mark Seaborn
2012/02/29 21:33:34
Is this SSE4 or SSE4.1?
Nick Bray (chromium)
2012/02/29 22:58:09
Done.
| |
29 "\x66\x0f\x3a\x0e\xd0\xc0\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" | |
30 "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"; | |
31 | |
32 const int code_size = 32; | |
33 | |
34 struct MockContext { | |
35 /* Sanity check that we're getting the right object. */ | |
36 int marker; | |
37 int query_result; | |
38 int set_validates_expected; | |
39 int query_destroyed; | |
40 }; | |
41 | |
42 struct MockQuery { | |
43 /* Sanity check that we're getting the right object. */ | |
44 int marker; | |
45 int state; | |
46 int add_count; | |
47 MockContext *context; | |
48 }; | |
49 | |
50 void *MockCreateQuery(void *context) { | |
51 MockContext *mcontext = (MockContext *) context; | |
52 MockQuery *mquery = (MockQuery *) malloc(sizeof(MockQuery)); | |
53 EXPECT_EQ(31, mcontext->marker); | |
54 mquery->marker = 37; | |
55 mquery->state = 0; | |
56 mquery->add_count = 0; | |
57 mquery->context = mcontext; | |
58 return mquery; | |
59 } | |
60 | |
61 void MockAddData(void *query, const unsigned char *data, size_t length) { | |
62 MockQuery *mquery = (MockQuery *) query; | |
63 ASSERT_EQ(37, mquery->marker); | |
64 UNREFERENCED_PARAMETER(data); | |
65 EXPECT_EQ(0, mquery->state); | |
66 /* Small data is supicious. */ | |
67 EXPECT_LE((size_t) 2, length); | |
68 mquery->add_count += 1; | |
69 } | |
70 | |
71 int MockDoQuery(void *query) { | |
72 MockQuery *mquery = (MockQuery *) query; | |
73 EXPECT_EQ(37, mquery->marker); | |
74 EXPECT_EQ(0, mquery->state); | |
75 /* Less than two pieces of data is suspicious. */ | |
76 EXPECT_LE(2, mquery->add_count); | |
77 mquery->state = 1; | |
78 return mquery->context->query_result; | |
79 } | |
80 | |
81 void MockSetValidates(void *query) { | |
82 MockQuery *mquery = (MockQuery *) query; | |
83 ASSERT_EQ(37, mquery->marker); | |
84 EXPECT_EQ(1, mquery->state); | |
85 EXPECT_EQ(1, mquery->context->set_validates_expected); | |
86 mquery->state = 2; | |
87 } | |
88 | |
89 void MockDestroyQuery(void *query) { | |
90 MockQuery *mquery = (MockQuery *) query; | |
91 ASSERT_EQ(37, mquery->marker); | |
92 EXPECT_EQ(mquery->context->set_validates_expected ? 2 : 1, mquery->state); | |
93 mquery->context->query_destroyed = 1; | |
94 free(mquery); | |
95 } | |
96 | |
97 class ValidationCachingInterfaceTests : public ::testing::Test { | |
98 protected: | |
99 MockContext context; | |
100 NaClValidationCache cache; | |
101 NaClCPUFeatures cpu_features; | |
102 int bundle_size; | |
103 | |
104 unsigned char code_buffer[32]; | |
105 | |
106 void SetUp() { | |
107 context.marker = 31; | |
108 context.query_result = 1; | |
109 context.set_validates_expected = 0; | |
110 context.query_destroyed = 0; | |
111 | |
112 cache.create_query = MockCreateQuery; | |
113 cache.add_data = MockAddData; | |
114 cache.do_query = MockDoQuery; | |
115 cache.set_validates = MockSetValidates; | |
116 cache.destroy_query = MockDestroyQuery; | |
117 | |
118 NaClSetAllCPUFeatures(&cpu_features); | |
119 | |
120 bundle_size = 32; | |
121 | |
122 memset(code_buffer, 0x90, 32); | |
123 } | |
124 | |
125 NaClValidationStatus Validate() { | |
126 return NACL_SUBARCH_NAME(ApplyValidator, | |
127 NACL_TARGET_ARCH, | |
128 NACL_TARGET_SUBARCH)( | |
129 NACL_SB_DEFAULT, | |
130 NaClApplyCodeValidation, | |
131 0, code_buffer, 32, | |
132 bundle_size, &cpu_features, | |
133 &cache, &context); | |
134 } | |
135 }; | |
136 | |
137 TEST_F(ValidationCachingInterfaceTests, Sanity) { | |
138 void *query = cache.create_query(&context); | |
139 cache.add_data(query, 0, 6); | |
140 cache.add_data(query, 0, 128); | |
141 EXPECT_EQ(1, cache.do_query(query)); | |
142 cache.destroy_query(query); | |
143 EXPECT_EQ(1, context.query_destroyed); | |
144 } | |
145 | |
146 TEST_F(ValidationCachingInterfaceTests, NoCache) { | |
147 NaClValidationStatus status; | |
148 status = NACL_SUBARCH_NAME(ApplyValidator, | |
149 NACL_TARGET_ARCH, | |
150 NACL_TARGET_SUBARCH)( | |
151 NACL_SB_DEFAULT, | |
152 NaClApplyCodeValidation, | |
153 0, code_buffer, code_size, | |
154 bundle_size, &cpu_features, | |
155 NULL, NULL); | |
156 EXPECT_EQ(NaClValidationSucceeded, status); | |
157 } | |
158 | |
159 TEST_F(ValidationCachingInterfaceTests, CacheHit) { | |
160 NaClValidationStatus status; | |
161 status = Validate(); | |
162 EXPECT_EQ(NaClValidationSucceeded, status); | |
163 EXPECT_EQ(1, context.query_destroyed); | |
164 } | |
165 | |
166 TEST_F(ValidationCachingInterfaceTests, CacheMiss) { | |
167 NaClValidationStatus status; | |
168 context.query_result = 0; | |
169 context.set_validates_expected = 1; | |
170 status = Validate(); | |
171 EXPECT_EQ(NaClValidationSucceeded, status); | |
172 EXPECT_EQ(1, context.query_destroyed); | |
173 } | |
174 | |
175 TEST_F(ValidationCachingInterfaceTests, SSE4Allowed) { | |
176 NaClValidationStatus status; | |
177 memcpy(code_buffer, sse4, 32); | |
178 context.query_result = 0; | |
179 context.set_validates_expected = 1; | |
180 status = Validate(); | |
181 EXPECT_EQ(NaClValidationSucceeded, status); | |
182 EXPECT_EQ(1, context.query_destroyed); | |
183 } | |
184 | |
185 TEST_F(ValidationCachingInterfaceTests, SSE4Stubout) { | |
186 NaClValidationStatus status; | |
187 memcpy(code_buffer, sse4, 32); | |
188 context.query_result = 0; | |
189 NaClSetCPUFeature(&cpu_features, NaClCPUFeature_SSE41, 0); | |
190 status = Validate(); | |
191 EXPECT_EQ(NaClValidationSucceeded, status); | |
192 EXPECT_EQ(1, context.query_destroyed); | |
193 } | |
194 | |
195 TEST_F(ValidationCachingInterfaceTests, IllegalInst) { | |
196 NaClValidationStatus status; | |
197 memcpy(code_buffer, ret, 32); | |
198 context.query_result = 0; | |
199 status = Validate(); | |
200 EXPECT_EQ(NaClValidationFailed, status); | |
201 EXPECT_EQ(1, context.query_destroyed); | |
202 } | |
203 | |
204 // Test driver function. | |
205 int main(int argc, char *argv[]) { | |
206 testing::InitGoogleTest(&argc, argv); | |
207 return RUN_ALL_TESTS(); | |
208 } | |
OLD | NEW |