1 : /*
2 : * Copyright (c) 2011 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 : /*
8 : * ncdecode_tests.c - simple unit tests for NaCl decoder
9 : */
10 :
11 : #ifndef NACL_TRUSTED_BUT_NOT_TCB
12 : #error("This file is not meant for use in the TCB")
13 : #endif
14 :
15 : #include <stdio.h>
16 : #include "native_client/src/shared/gio/gio.h"
17 : #include "native_client/src/shared/platform/nacl_log.h"
18 : #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncdecode.h"
19 : #include "native_client/src/trusted/validator/x86/ncval_seg_sfi/ncdecode_verbose.h"
20 :
21 : struct NCDecodeTestCase {
22 : char *description;
23 : size_t testsize;
24 : uint8_t *testbytes;
25 : };
26 :
27 : struct NCDecodeTestCase NCDecoderTests[] = {
28 : {
29 : "test 1",
30 : 25,
31 : (uint8_t *)"\x55"
32 : "\x89\xe5"
33 : "\x83\xec\x08"
34 : "\xe8\x81\x00\x00\x00"
35 : "\xe8\xd3\x00\x00\x00"
36 : "\xe8\xf3\x04\x00\x00"
37 : "\xc9"
38 : "\xc3"
39 : "\x00\x00",
40 : },
41 : {
42 : "test2",
43 : 341,
44 : (uint8_t *)"\x8d\x4c\x24\x04"
45 : "\x83\xe4\xf0"
46 : "\xff\x71\xfc"
47 : "\x55"
48 : "\x89\xe5"
49 : "\x51"
50 : "\x66\x90"
51 : "\x83\xec\x24"
52 : "\x89\x4d\xe8"
53 : "\xc7\x45\xf4\x0a\x00\x00\x00"
54 : "\x8b\x45\xe8"
55 : "\x83\x38\x01"
56 : "\x7f\x2b"
57 : "\x8b\x55\xe8"
58 : "\x8b\x42\x04"
59 : "\x8b\x00"
60 : "\x8d\x76\x00"
61 : "\x89\x44\x24\x04"
62 : "\xc7\x04\x24\x54\x14\x00\x08"
63 : "\xe8\xc0\x02\x00\x00"
64 : "\xc7\x04\x24\x01\x00\x00\x00"
65 : "\x8d\x74\x26\x00"
66 : "\xe8\xc0\x01\x00\x00"
67 : "\x8b\x55\xe8"
68 : "\x8b\x42\x04"
69 : "\x83\xc0\x04"
70 : "\x8b\x00"
71 : "\x89\x04\x24"
72 : "\x66\x90"
73 : "\x8d\x74\x26\x00"
74 : "\x8d\xbc\x27\x00\x00\x00\x00"
75 : "\xe8\x90\x09\x00\x00"
76 : "\x89\x45\xf8"
77 : "\x8b\x45\xe8"
78 : "\x83\x38\x02"
79 : "\x7e\x25"
80 : "\x8b\x55\xe8"
81 : "\x66\x90"
82 : "\x8b\x42\x04"
83 : "\x83\xc0\x08"
84 : "\x8b\x00"
85 : "\x89\x04\x24"
86 : "\xe8\x70\x09\x00\x00"
87 : "\x89\x45\xf4"
88 : "\x8d\xb6\x00\x00\x00\x00"
89 : "\x8d\xbc\x27\x00\x00\x00\x00"
90 : "\x8b\x45\xf4"
91 : "\xa3\x28\x2f\x00\x08"
92 : "\xeb\x26"
93 : "\x8d\xb6\x00\x00\x00\x00"
94 : "\xc7\x44\x24\x08\x03\x00\x00\x00, "
95 : "\xc7\x44\x24\x04\x01\x00\x00\x00, "
96 : "\x8b\x45\xf4"
97 : "\x89\x04\x24"
98 : "\x90"
99 : "\x8d\x74\x26\x00"
100 : "\xe8\x20\x00\x00\x00"
101 : "\x83\x7d\xf8\x00"
102 : "\x0f\x9f\xc0"
103 : "\x83\x6d\xf8\x01"
104 : "\x84\xc0"
105 : "\x8d\x76\x00"
106 : "\x75\xce"
107 : "\xc7\x04\x24\x00\x00\x00\x00"
108 : "\x66\x90"
109 : "\xe8\x20\x01\x00\x00"
110 : "\x55"
111 : "\x89\xe5"
112 : "\x83\xec\x1c"
113 : "\x83\x7d\x08\x01"
114 : "\x75\x44"
115 : "\x8b\x55\x0c"
116 : "\x90"
117 : "\x8b\x04\x95\x24\x2f\x00\x08"
118 : "\x83\xe8\x01"
119 : "\x8d\xb6\x00\x00\x00\x00"
120 : "\x89\x04\x95\x24\x2f\x00\x08"
121 : "\x8b\x55\x10"
122 : "\x8d\xb6\x00\x00\x00\x00"
123 : "\x8b\x04\x95\x24\x2f\x00\x08"
124 : "\x83\xc0\x01"
125 : "\x8d\xb6\x00\x00\x00\x00"
126 : "\x89\x04\x95\x24\x2f\x00\x08"
127 : "\xeb\x77"
128 : "\x8d\xb4\x26\x00\x00\x00\x00"
129 : "\x8b\x45\x10"
130 : "\x8b\x55\x0c"
131 : "\x01\xc2"
132 : "\xb8\x06\x00\x00\x00"
133 : "\x29\xd0"
134 : "\x90"
135 : },
136 : {
137 : "test 3",
138 : 9,
139 : (uint8_t *)"\x3c\x25"
140 : "\x90\x90\x90\x90\x90\x90\x90"
141 : },
142 : { "test4",
143 : 13,
144 : (uint8_t *)"\xc1\xf9\x1f\x89\x4d\xe4"
145 : "\x90\x90\x90\x90\x90\x90\x90"
146 : },
147 : {
148 : "test5",
149 : 12,
150 : (uint8_t *)"\xc6\x44\x05\xd6\x00"
151 : "\x90\x90\x90\x90\x90\x90\x90"
152 : },
153 : {
154 : "test6",
155 : 14,
156 : (uint8_t *)"\xff\x24\x95\xc8\x6e\x05\x08"
157 : "\x90\x90\x90\x90\x90\x90\x90"
158 : },
159 : {
160 : "test7",
161 : 15,
162 : (uint8_t *)"\x0f\xab\x14\x85\x40\xfb\x27\x08"
163 : "\x90\x90\x90\x90\x90\x90\x90"
164 : },
165 : {
166 : "test8",
167 : 11,
168 : (uint8_t *)"\x66\xbf\x08\x00"
169 : "\x90\x90\x90\x90\x90\x90\x90"
170 : },
171 : {
172 : "test9",
173 : 12,
174 : (uint8_t *)"\x66\x0f\xbe\x04\x10"
175 : "\x90\x90\x90\x90\x90\x90\x90"
176 : },
177 : /* ldmxcsr, stmxcsr */
178 : {
179 : "test10",
180 : 15,
181 : (uint8_t *)"\x90\x0f\xae\x10\x90\x0f\xae\x18"
182 : "\x90\x90\x90\x90\x90\x90\x90"
183 : },
184 : /* invalid */
185 : {
186 : "test11",
187 : 11,
188 : (uint8_t *)"\x90\x0f\xae\x21"
189 : "\x90\x90\x90\x90\x90\x90\x90"
190 : },
191 : /* lfence */
192 : {
193 : "test12",
194 : 11,
195 : (uint8_t *)"\x90\x0f\xae\x28"
196 : "\x90\x90\x90\x90\x90\x90\x90"
197 : },
198 : {
199 : "test13",
200 : 9,
201 : (uint8_t *)"\xf0\x0f\xb1\x8f\xa8\x01\x00\x00"
202 : "\x90"
203 : },
204 : {
205 : "test14",
206 : 17,
207 : (uint8_t *)"\x84\xd4\x04\x53\xa0\x04\x6a\x5a\x20\xcc\xb8\x48\x03"
208 : "\x2b\x96\x11\xf4"
209 : },
210 : {
211 : "test15",
212 : 17,
213 : (uint8_t *)"\xb0\xb9\xd8\x3d\x60\xe4\x2c\x15\x43\x61\x4e\x03\x39"
214 : "\xfc\x79\x4b\xf4"
215 : },
216 : {
217 : "test16",
218 : 17,
219 : (uint8_t *)"\x66\x66\x66\x66\x0f\x1f\x0d\x1a\xed\x25\x37\x27\x0a"
220 : "\xff\xc6\x1a\xf4"
221 : },
222 : {
223 : "test17",
224 : 18,
225 : (uint8_t *)"\x66\xeb\x1b\x31\x51\x3d\xef\xcc\x2f\x36\x48\x6e\x44"
226 : "\x2e\xcc\x14\x00\xf4"
227 : },
228 : {
229 : "test18",
230 : 17,
231 : (uint8_t *)"\x67\x8d\x1d\x22\xa0\x05\xe3\x7b\x9c\xdb\x08\x04\xb1"
232 : "\x90\xed\x12\xf4"
233 : },
234 : {
235 : "test 19",
236 : 20,
237 : (uint8_t *)"\x45\x7f\x89\x58\x94\x04\x24\x1b\xc3\xe2\x6f\x1a\x94\x87\x8f\x0b\x00\x00\x90\xf4",
238 : },
239 : {
240 : "test 20",
241 : 8,
242 : (uint8_t *)"\x66\x0f\x3a\x0e\xd0\xc0\x90\xf4"
243 : },
244 : {
245 : "test 21: palignr (SSSE3)",
246 : 8,
247 : (uint8_t *)"\x66\x0f\x3a\x0f\xd0\xc0\x90\xf4"
248 : },
249 : {
250 : "test 22: SSSE3",
251 : 8,
252 : (uint8_t *)"\x66\x0f\x38\x00\x00\x00\x90\xf4"
253 : },
254 : {
255 : "test 23: 3DNow",
256 : 7,
257 : (uint8_t *)"\x0f\x0f\x46\x01\xbf\x90\xf4"
258 : },
259 : {
260 : "test 24: SSSE3 error",
261 : 8,
262 : (uint8_t *)"\x66\x0f\x3a\x0e\xd0\xc0\x90\xf4"
263 : },
264 : {
265 : "test 25: lzcnt",
266 : 6,
267 : (uint8_t *)"\xf3\x0f\xbd\x00\x90\xf4",
268 : },
269 : {
270 : "test 26: 3dnow",
271 : 6,
272 : (uint8_t *)"\x0f\x0f\x01\x0c\x90\xf4",
273 : },
274 : {
275 : "test 27: addr16 variants",
276 : 17,
277 : (uint8_t *)"\x67\x01\x00"
278 : "\x67\x01\x40\x00"
279 : "\x67\x01\x80\x00\x90"
280 : "\x67\x01\xc0"
281 : "\x90\xf4",
282 : },
283 : {
284 : "test 28: lock cmpxchg8b",
285 : 6,
286 : (uint8_t *)"\xf0\x0f\xc7\010\x90\xf4"
287 : },
288 : {
289 : "test 29: SSE3",
290 : 8,
291 : (uint8_t *)"\x66\x0f\x7d\x00\x90\x90\x90\xf4"
292 : },
293 : {
294 : "test 30: MMXSSE2",
295 : 10,
296 : (uint8_t *)"\x66\x0f\x71\xf6\x00\x90\x90\x90\x90\xf4",
297 : },
298 : {
299 : "test 31: call %esp",
300 : 7,
301 : /* 11 100 000 */
302 : (uint8_t *)"\x83\xe4\xf0\xff\xd4\x90\xf4",
303 : },
304 : {
305 : "test 32: roundss",
306 : 9,
307 : (uint8_t *)"\x66\x0f\x3a\x0a\xc0\x00"
308 : "\x90\x90"
309 : "\xf4",
310 : },
311 : {
312 : "test 33: crc32",
313 : 8,
314 : (uint8_t *)"\xf2\x0f\x38\xf1\xc3"
315 : "\x90\x90"
316 : "\xf4",
317 : },
318 : {
319 : "test 34: SSE4",
320 : 8,
321 : (uint8_t *)"\x66\x0f\x38\x0a\xd0\x90\x90\xf4"
322 : }
323 : };
324 :
325 : #define sizeofA(array) (sizeof(array)/sizeof(array[0]))
326 1 : void ncdecode_unittests(void) {
327 : size_t i;
328 :
329 1 : for (i = 0; i < sizeofA(NCDecoderTests); i++) {
330 1 : printf("==========================================================\n");
331 1 : printf("%s\n", NCDecoderTests[i].description);
332 : NCDecodeSegment(NCDecoderTests[i].testbytes,
333 : (uint32_t)(NCDecoderTests[i].testbytes),
334 1 : NCDecoderTests[i].testsize);
335 1 : }
336 1 : }
337 :
338 1 : int main(void) {
339 : struct GioFile gio_out_stream;
340 1 : struct Gio *gout = (struct Gio*) &gio_out_stream;
341 1 : if (!GioFileRefCtor(&gio_out_stream, stdout)) {
342 0 : fprintf(stderr, "Unable to create gio file for stdout!\n");
343 0 : return 1;
344 : }
345 1 : NaClLogModuleInitExtended(LOG_INFO, gout);
346 1 : ncdecode_unittests();
347 1 : printf("PASSED\n");
348 1 : GioFileDtor(gout);
349 1 : return 0;
350 1 : }
|