Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: src/trusted/validator_x86/nc_inst_state_Tests.cc

Issue 6883091: Start unit testing for functions in nc_inst_state.c (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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 // Unit tests for code in nc_inst_state.cc (and nc_inst_state_statics.c).
Brad Chen 2011/04/25 21:47:27 What was that notation you were going to use to in
Karl 2011/06/24 18:15:00 Done.
8
9 // To turn on debugging of instruction decoding, change value of
10 // DEBUGGING to 1.
11 #define DEBUGGING 0
12
13 #include "native_client/src/trusted/validator_x86/nc_inst_state.h"
14 #include "native_client/src/include/nacl_macros.h"
15 #include "gtest/gtest.h"
16
17 // Include static functions, so that we can test.
18 extern "C" {
19 #include "native_client/src/trusted/validator_x86/nc_inst_state_statics.c"
20 #include "native_client/src/trusted/validator_x86/RexPrefixes.h"
21 };
22
23 namespace {
24
25 // Size of buffer to use to contain bytes of an instruction.
26 static const size_t kBufferSize = 24;
27
28 // Test harness for routines in nc_inst_state.c and nc_inst_state_statics.c.
29 class NcInstStateTests : public ::testing::Test {
30 protected:
31 NcInstStateTests();
32 void SetUp();
33 void TearDown();
34
35 // Plant the given byte as the next input byte in the input buffer.
36 // Uses plant_index to determine the current end of the input buffer.
37 void Plant(uint8_t byte);
38
39 // Reset test state to a cleared input buffer, and (re)initialize
40 // the instruction state.
41 void Reset();
42
43 // Routine to add dummy calls so that compilation errors are not defined
44 // for static routines we have not tested.
45 void dummy();
46
47 // Reinitializes instruction state.
48 void ResetState();
49
50 // Resets the instruction pattern, and its flags to a default initial
51 // state.
52 void ResetInstPattern();
53
54 // Fills the input buffer with unlikely bytes, and initializes
55 // the reader to the beginning of the input buffer.
56 void ResetInput();
57
58 // Fills the input buffer with unlikely bytes, and set the plant
59 // index to the beginning of the input buffer.
60 void ResetInputBuffer();
61
62 // Verify that we have consumed the given number of prefix bytes, with
63 // the given number of rex prefixes, and that the prefix mask is set
64 // to the given mask.
65 //
66 // Parameters are:
67 // num_bytes - Number of prefix bytes read.
68 // num_rex - Number of prefix bytes that were rex prefixes.
69 // mask - prefix mask that should have been generated.
70 void VerifyConsumedPrefixBytes(uint8_t num_bytes, uint8_t num_rex,
71 uint32_t mask);
72
73 // Run tests that verify that the call to NaClConsume0F38XXNaClInstBytes
74 // behaved as expected. Assumes the call was made through a call
75 // to NaClConsumeInstBytes.
76 void VerifyConsume0F38XXInstructions();
77
78 // Run tests that verify that the call to NaClConsume0F3AXXNaClInstBytes
79 // behaved as expected. Assumes the call was made through a call
80 // to NaClConsumeInstBytes.
81 void VerifyConsume0F3AXXInstructions();
82
83 // Run tests that verify that the call to NaClConsume0FXXNaClInstBytes
84 // behaved as expected. Assumes the call was made through a call
85 // to NaClConsumeInstBytes.
86 void VerifyConsume0FXXInstructions();
87
88 // Run tests that verify that the call to NaClConsumeX87NaClInstBytes
89 // behaved as expected. Assumes the call was made through a call
90 // to NaClConsumeInstBytes.
91 void VerifyConsumeX87Instructions();
92
93 // Run tests that verify that the call to NaClConsumeInstBytes consumed
94 // a single byte.
95 void VerifyConsumeOneByteInstructions();
96
97 // The instruction state to test.
98 NaClInstState* _state;
99 // The instruction iterator to use.
100 NaClInstIter* _iter;
101 // The memory segment to test.
102 NaClSegment _segment;
103 // The memory buffer in the memory segment.
104 uint8_t _buffer[kBufferSize];
105 // The instruction pattern to match against.
106 NaClInst _inst_pattern;
107 // The index of where the next planted byte should
108 // be added to the input buffer.
109 size_t _plant_index;
110 };
111
112 // Helper function to convert Bool to bool. This function is defined
113 // to get around a visual studio warning for 64-bits, which causes
114 // our trybots to fail (in our build system, compiler warnings are converted
115 // to compiler errors).
116 static inline bool Bool2bool(Bool b) {
117 return b ? true : false;
118 }
119
120 NcInstStateTests::NcInstStateTests() {
121 ResetInputBuffer();
122 NaClSegmentInitialize(_buffer, 0, kBufferSize, &_segment);
123 }
124
125 void NcInstStateTests::SetUp() {
126 _iter = NaClInstIterCreate(&_segment);
127 _state = NaClInstIterGetUndecodedState(_iter);
128 ResetInput();
129 ResetState();
130 }
131
132 void NcInstStateTests::TearDown() {
133 NaClInstIterDestroy(_iter);
134 }
135
136 void NcInstStateTests::Reset() {
137 ResetInput();
138 ResetState();
139 }
140
141 void NcInstStateTests::ResetState() {
142 NaClInstStateInit(_iter, _state);
143 ResetInstPattern();
144 }
145
146 void NcInstStateTests::ResetInstPattern() {
147 _inst_pattern.flags = NACL_EMPTY_IFLAGS;
148 _state->inst = &_inst_pattern;
149 }
150
151 void NcInstStateTests::ResetInputBuffer() {
152 // Fill input buffer with unlikely byte codes.
153 for (size_t i = 0; i < kBufferSize; ++i) {
154 _buffer[i] = 'X';
155 }
156 // Mark start point for planting data into
157 // the input buffer.
158 _plant_index = 0;
159 }
160
161 void NcInstStateTests::ResetInput() {
162 ResetInputBuffer();
163 NCInstBytesReset(&_state->bytes);
164 }
165
166 void NcInstStateTests::Plant(uint8_t byte) {
167 // TODO(Karl): Why do we get a compile time error if we use ASSERT.
168 ASSERT_LT(_plant_index, kBufferSize) <<
169 "Planted too many bytes, buffer overflow!";
170 _buffer[_plant_index++] = byte;
171 // Need to reset memory so that peek byte is set.
172 NCRemainingMemoryReset(_state->bytes.memory);
173 }
174
175 void NcInstStateTests::VerifyConsumedPrefixBytes(
176 uint8_t num_bytes, uint8_t num_rex, uint32_t mask) {
177 EXPECT_EQ(num_bytes, _state->bytes.length);
178 EXPECT_EQ(num_bytes, _state->num_prefix_bytes);
179 EXPECT_EQ(mask, _state->prefix_mask);
180 EXPECT_EQ(num_rex, _state->num_rex_prefixes);
181 }
182
183 void NcInstStateTests::VerifyConsume0F38XXInstructions() {
184 NaClInstPrefixDescriptor desc;
185 uint32_t prefix_mask = _state->prefix_mask;
186 // Note: This code assumes that the prefix mask may have
187 // other flags set before this routine is called. Hence,
188 // we must be careful when updating and checking the
189 // mask.
190
191 // Test for all possible XX.
192 for (int i = 0; i < NCDTABLESIZE; ++i) {
193 // Test successfully matching 0f38XX
194 _state->prefix_mask = prefix_mask;
195 Plant(0x0f);
196 Plant(0x38);
197 Plant(i);
198 NaClConsumeInstBytes(_state, &desc);
199 if (NaClHasBit(_state->prefix_mask, kPrefixREP)) {
200 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
201 } else if (NaClHasBit(_state->prefix_mask, kPrefixREPNE)) {
202 EXPECT_EQ(PrefixF20F38, desc.matched_prefix);
203 } else if (NaClHasBit(_state->prefix_mask, kPrefixDATA16)) {
204 EXPECT_EQ(Prefix660F38, desc.matched_prefix);
205 } else {
206 EXPECT_EQ(Prefix0F38, desc.matched_prefix);
207 }
208 EXPECT_EQ((uint8_t) i, desc.opcode_byte);
209 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
210 ResetInput();
211 ResetState();
212 }
213
214 // Now verify if that there isn't an XX byte, things short curcuit correctly.
215 _state->prefix_mask = prefix_mask;
216 Plant(0x0f);
217 Plant(0x38);
218 _state->length_limit = 2;
219 NaClConsumeInstBytes(_state, &desc);
220 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
221 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
222 ResetInput();
223 ResetState();
224 }
225
226 void NcInstStateTests::VerifyConsume0F3AXXInstructions() {
227 NaClInstPrefixDescriptor desc;
228 uint32_t prefix_mask = _state->prefix_mask;
229 // Note: This code assumes that the prefix mask may have
230 // other flags set before this routine is called. Hence,
231 // we must be careful when updating and checking the
232 // mask.
233
234 // Test for all possible XX.
235 for (int i = 0; i < NCDTABLESIZE; ++i) {
236 // Test successfully matching 0F3AXX
237 _state->prefix_mask = prefix_mask;
238 Plant(0x0f);
239 Plant(0x3a);
240 Plant(i);
241 NaClConsumeInstBytes(_state, &desc);
242 if (NaClHasBit(_state->prefix_mask, kPrefixREP) ||
243 NaClHasBit(_state->prefix_mask, kPrefixREPNE)) {
244 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
245 } else if (NaClHasBit(_state->prefix_mask, kPrefixDATA16)) {
246 EXPECT_EQ(Prefix660F3A, desc.matched_prefix);
247 } else {
248 EXPECT_EQ(Prefix0F3A, desc.matched_prefix);
249 }
250 EXPECT_EQ((uint8_t) i, desc.opcode_byte);
251 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
252 ResetInput();
253 ResetState();
254 }
255
256 // Now verify if that there isn't an XX byte, things short curcuit correctly.
257 _state->prefix_mask = prefix_mask;
258 Plant(0x0f);
259 Plant(0x3a);
260 _state->length_limit = 2;
261 NaClConsumeInstBytes(_state, &desc);
262 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
263 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
264 ResetInput();
265 ResetState();
266 }
267
268 void NcInstStateTests::VerifyConsume0FXXInstructions() {
269 NaClInstPrefixDescriptor desc;
270 uint32_t prefix_mask = _state->prefix_mask;
271 // Note: This code assumes that the prefix mask may have
272 // other flags set before this routine is called. Hence,
273 // we must be careful when updating and checking the
274 // mask.
275
276 // Test for all possible XX.
277 for (int i = 0; i < NCDTABLESIZE; ++i) {
278 if (i == 0x38 || i == 0x3a) continue; // exclude special lookup cases.
279 // Test successfully matching 0fXX
280 _state->prefix_mask = prefix_mask;
281 Plant(0x0f);
282 Plant(i);
283 NaClConsumeInstBytes(_state, &desc);
284 if (NaClHasBit(_state->prefix_mask, kPrefixREP)) {
285 if (NaClHasBit(_state->prefix_mask, kPrefixREPNE)) {
286 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
287 } else {
288 EXPECT_EQ(PrefixF30F, desc.matched_prefix);
289 }
290 } else if (NaClHasBit(_state->prefix_mask, kPrefixREPNE)) {
291 EXPECT_EQ(PrefixF20F, desc.matched_prefix);
292 } else if (NaClHasBit(_state->prefix_mask, kPrefixDATA16)) {
293 EXPECT_EQ(Prefix660F, desc.matched_prefix);
294 } else {
295 EXPECT_EQ(Prefix0F, desc.matched_prefix);
296 }
297 EXPECT_EQ((uint8_t) i, desc.opcode_byte);
298 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
299 ResetInput();
300 ResetState();
301 }
302
303 // Now verify if that there isn't an XX byte, things short curcuit correctly.
304 _state->prefix_mask = prefix_mask;
305 Plant(0x0f);
306 _state->length_limit = 1;
307 NaClConsumeInstBytes(_state, &desc);
308 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
309 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
310 ResetInput();
311 ResetState();
312 }
313
314 void NcInstStateTests::VerifyConsumeX87Instructions() {
315 NaClInstPrefixDescriptor desc;
316 uint32_t prefix_mask = _state->prefix_mask;
317 // Note: This code assumes that the prefix mask may have
318 // other flags set before this routine is called. Hence,
319 // we must be careful when updating and checking the
320 // mask.
321
322 // Try for all possible x87 initial bytes.
323 for (uint8_t byte1 = 0xD8; byte1 <= 0xDF; ++byte1) {
324 // Test for all possible XX.
325 for (int i = 0; i < NCDTABLESIZE; ++i) {
326 // Test successfully matching byte1 XX
327 _state->prefix_mask = prefix_mask;
328 Plant(byte1);
329 Plant(i);
330 NaClConsumeInstBytes(_state, &desc);
331 NaClInstPrefix prefix = (NaClInstPrefix) (PrefixD8 + (byte1 - 0xD8));
332 EXPECT_EQ(prefix, desc.matched_prefix);
333 EXPECT_EQ((uint8_t) i, desc.opcode_byte);
334 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
335 ResetInput();
336 ResetState();
337 }
338
339 // Now verify if that there isn't an XX byte, things short curcuit
340 // correctly. For this context, it should return matching a single
341 // byte instruction with no prefix.
342 _state->prefix_mask = prefix_mask;
343 Plant(byte1);
344 _state->length_limit = 1;
345 NaClConsumeInstBytes(_state, &desc);
346 EXPECT_EQ(NoPrefix, desc.matched_prefix);
347 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
348 ResetInput();
349 ResetState();
350 }
351 }
352
353 void NcInstStateTests::VerifyConsumeOneByteInstructions() {
354 NaClInstPrefixDescriptor desc;
355 uint32_t prefix_mask = _state->prefix_mask;
356 // Note: This code assumes that the prefix mask may have
357 // other flags set before this routine is called. Hence,
358 // we must be careful when updating and checking the
359 // mask.
360
361 // Test for all possible XX.
362 for (int i = 0; i < NCDTABLESIZE; ++i) {
363 // exclude special lookup cases.
364 if (i == 0x0f || (i >= 0xD8 && i <= 0xDF)) continue;
365 // Test successfully XX
366 _state->prefix_mask = prefix_mask;
367 Plant(i);
368 NaClConsumeInstBytes(_state, &desc);
369 EXPECT_EQ(NoPrefix, desc.matched_prefix);
370 EXPECT_EQ((uint8_t) i, desc.opcode_byte);
371 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
372 ResetInput();
373 ResetState();
374 }
375
376 // Now verify if that there isn't an XX byte, things short curcuit correctly.
377 _state->prefix_mask = prefix_mask;
378 _state->length_limit = 0;
379 NaClConsumeInstBytes(_state, &desc);
380 EXPECT_EQ(NaClInstPrefixEnumSize, desc.matched_prefix);
381 EXPECT_EQ((uint8_t) 0, desc.next_length_adjustment);
382 ResetInput();
383 ResetState();
384 }
385
386 void NcInstStateTests::dummy() {
387 NaClInstPrefixDescriptor prefix_desc;
388 NaClConsumeAndCheckOperandSize(_state);
389 NaClConsumeAndCheckAddressSize(_state);
390 NaClConsumeModRm(_state);
391 NaClConsumeSib(_state);
392 NaClConsumeDispBytes(_state);
393 NaClConsumeImmediateBytes(_state);
394 NaClValidatePrefixFlags(_state);
395 NaClClearInstState(_state, 0);
396 NaClGetNextInstCandidates(_state, &prefix_desc, NULL);
397 NaClConsumeOpcodeSequence(_state);
398 }
399
400 // Test function NaClExtactOpSize, which returns the expected
401 // number of bytes to represent operands.
402 TEST_F(NcInstStateTests, TestExtractOpSize) {
403 // Test 32 amd 64 bit assumptions.
404
405 // Test explicit size restrictors. Note: Only b should make a difference
406 // in matching the pattern, since v, w, and o are used as excluders rather
407 // than for matching (i.e. don't match unless operand size should be
408 // 1).
409 _inst_pattern.flags = NACL_IFLAG(OperandSize_b);
410 EXPECT_EQ(1, NaClExtractOpSize(_state)) << "bytes are of size 1\n";
411 _inst_pattern.flags = NACL_IFLAG(OperandSize_w);
412 EXPECT_EQ(4, NaClExtractOpSize(_state));
413 _inst_pattern.flags = NACL_IFLAG(OperandSize_v);
414 EXPECT_EQ(4, NaClExtractOpSize(_state));
415 _inst_pattern.flags = NACL_IFLAG(OperandSize_o);
416 EXPECT_EQ(4, NaClExtractOpSize(_state));
417 ResetState();
418
419 // See if we interpret the Data16 prefix correctly.
420 _state->prefix_mask = kPrefixDATA16;
421 EXPECT_EQ(2, NaClExtractOpSize(_state));
422 _inst_pattern.flags = NACL_IFLAG(SizeIgnoresData16);
423 EXPECT_EQ(4, NaClExtractOpSize(_state));
424 ResetState();
425
426 // Test strictly 64-bit assumptions.
427 if (NACL_TARGET_SUBARCH == 64) {
428 // Check that we return a size 64 if the REX.W bit is set.
429 for (uint8_t rex = NaClRexMin; rex <= NaClRexMax; ++rex) {
430 _state->rexprefix = rex;
431 if (NaClRexW(rex)) {
432 EXPECT_EQ(8, NaClExtractOpSize(_state));
433 } else {
434 EXPECT_EQ(4, NaClExtractOpSize(_state));
435 }
436 }
437 ResetState();
438
439 // If we force the size to 64, it returns size 64.
440 _inst_pattern.flags = NACL_IFLAG(OperandSizeForce64);
441 EXPECT_EQ(8, NaClExtractOpSize(_state));
442 ResetState();
443
444 // Now repeat the tests, but with the default size set to 64 bits,
445 // which replaces the default size of 4 with 8.
446
447 // Test explicit size restrictors. Note: Only b should make a difference
448 // in matching the pattern, since v, w, and o are used as excluders rather
449 // than for matching (i.e. don't match unless operand size matches).
450 _inst_pattern.flags =
451 NACL_IFLAG(OperandSize_b) | NACL_IFLAG(OperandSizeDefaultIs64);
452 EXPECT_EQ(1, NaClExtractOpSize(_state)) << "bytes are of size 1\n";
453 _inst_pattern.flags =
454 NACL_IFLAG(OperandSize_w) | NACL_IFLAG(OperandSizeDefaultIs64);
455 EXPECT_EQ(8, NaClExtractOpSize(_state));
456 _inst_pattern.flags =
457 NACL_IFLAG(OperandSize_v) | NACL_IFLAG(OperandSizeDefaultIs64);
458 EXPECT_EQ(8, NaClExtractOpSize(_state));
459 _inst_pattern.flags =
460 NACL_IFLAG(OperandSize_o) | NACL_IFLAG(OperandSizeDefaultIs64);
461 EXPECT_EQ(8, NaClExtractOpSize(_state));
462 ResetState();
463
464 // See if we interpret the Data16 prefix correctly.
465 _state->prefix_mask = kPrefixDATA16;
466 _inst_pattern.flags = NACL_IFLAG(OperandSizeDefaultIs64);
467 EXPECT_EQ(2, NaClExtractOpSize(_state));
468 _inst_pattern.flags =
469 NACL_IFLAG(SizeIgnoresData16) | NACL_IFLAG(OperandSizeDefaultIs64);
470 EXPECT_EQ(8, NaClExtractOpSize(_state));
471 ResetState();
472
473 // Check that we return a size 64 independent of the REX.W bit.
474 _inst_pattern.flags = NACL_IFLAG(OperandSizeDefaultIs64);
475 for (uint8_t rex = NaClRexMin; rex <= NaClRexMax; ++rex) {
476 _state->rexprefix = rex;
477 EXPECT_EQ(8, NaClExtractOpSize(_state));
478 }
479 }
480 }
481
482 // Test function NaClExtractAddressSize, which returns the expected
483 // number of bits in operands corresponding to addresses.
484 TEST_F(NcInstStateTests, TestExtractAddressSize) {
485 // Depending on whether we are in 32/64 bit mode, there are two
486 // different address sizes.
487 int small_address;
488 int large_address;
489 if (NACL_TARGET_SUBARCH == 64) {
490 small_address = 32;
491 large_address = 64;
492 } else {
493 small_address = 16;
494 large_address = 32;
495 }
496 EXPECT_EQ(large_address, NaClExtractAddressSize(_state));
497 _state->prefix_mask = kPrefixADDR16;
498 EXPECT_EQ(small_address, NaClExtractAddressSize(_state));
499 }
500
501 extern "C" {
502 // Define acceptable prefixes, and the corresponding flag that
503 // should be set (except for rex prefixes).
504 static const struct prefix_pairs {
505 uint8_t byte;
506 uint32_t mask;
507 } prefix_values[] = {
508 {kValueSEGCS, kPrefixSEGCS},
509 {kValueSEGSS, kPrefixSEGSS},
510 {kValueSEGFS, kPrefixSEGFS},
511 {kValueSEGGS, kPrefixSEGGS},
512 {kValueDATA16, kPrefixDATA16},
513 {kValueADDR16, kPrefixADDR16},
514 {kValueREPNE, kPrefixREPNE},
515 {kValueREP, kPrefixREP},
516 {kValueLOCK, kPrefixLOCK},
517 {kValueSEGES, kPrefixSEGES},
518 {kValueSEGDS, kPrefixSEGDS}
519 };
520 };
521
522 // Test function NaClConsumePrefixBytes to verify it only recognizes
523 // valid prefix values.
524 TEST_F(NcInstStateTests, ConsumesKnownPrefixBytes) {
525 for (int byte = 0; byte < NCDTABLESIZE; ++byte) {
526 bool byte_categorized = false;
527 Plant(byte);
528 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
529 if (NACL_TARGET_SUBARCH == 64 &&
530 byte >= NaClRexMin && byte <= NaClRexMax) {
531 VerifyConsumedPrefixBytes(1, 1, kPrefixREX);
532 byte_categorized = true;
533 } else {
534 for (size_t j = 0; j < NACL_ARRAY_SIZE(prefix_values); ++j) {
535 if (byte == prefix_values[j].byte) {
536 VerifyConsumedPrefixBytes(1, 0, prefix_values[j].mask);
537 byte_categorized = true;
538 }
539 }
540 }
541 if (!byte_categorized) {
542 VerifyConsumedPrefixBytes(0, 0, 0);
543 }
544 ResetInput();
545 ResetState();
546 }
547 }
548
549 // Test function NaClConsumePrefixBytes to verify it can recognize
550 // pairs of non-rex prefix bytes.
551 TEST_F(NcInstStateTests, ConsumeNonRexPrefixBytePairs) {
552 // First try some pairs within non-rex prefix bytes.
553 for (size_t i = 0; i < NACL_ARRAY_SIZE(prefix_values) - 1; ++i) {
554 Plant(prefix_values[i].byte);
555 Plant(prefix_values[i+1].byte);
556 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
557 VerifyConsumedPrefixBytes(2, 0,
558 prefix_values[i].mask | prefix_values[i+1].mask);
559 ResetInput();
560 ResetState();
561 }
562 }
563
564 // Test Function NaClConsumePrefixBytes to verify it can recognize
565 // a Rex prefix followed by a non-rex prefix.
566 TEST_F(NcInstStateTests, ConsumeRexThenNonRexPrefixPairs) {
567 if (NACL_TARGET_SUBARCH == 64) {
568 // Try some pairs where one is rex.
569 for (size_t i = 0; i < NACL_ARRAY_SIZE(prefix_values); ++i) {
570 for (uint8_t rex = NaClRexMin; rex <= NaClRexMax; ++rex) {
571 Plant(rex);
572 Plant(prefix_values[i].byte);
573 EXPECT_FALSE(Bool2bool(NaClConsumePrefixBytes(_state)));
574 VerifyConsumedPrefixBytes(2, 1, prefix_values[i].mask | kPrefixREX);
575 ResetInput();
576 ResetState();
577 }
578 }
579 }
580 }
581
582 // Test Function NaClConsumePrefixBytes to verify it can recognize
583 // a non-rex prefix, followed by a rex prefix.
584 TEST_F(NcInstStateTests, ConsumeNonRexThenRexPrefixPairs) {
585 if (NACL_TARGET_SUBARCH == 64) {
586 // Try some pairs where one is rex.
587 for (size_t i = 0; i < NACL_ARRAY_SIZE(prefix_values); ++i) {
588 for (uint8_t rex = NaClRexMin; rex <= NaClRexMax; ++rex) {
589 Plant(prefix_values[i].byte);
590 Plant(rex);
591 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
592 VerifyConsumedPrefixBytes(2, 1, prefix_values[i].mask | kPrefixREX);
593 ResetInput();
594 ResetState();
595 }
596 }
597 }
598 }
599
600 // Test function NaClConsumePrefixBytes on multiple rex prefixes.
601 TEST_F(NcInstStateTests, ConsumeMultipleRexPrefixes) {
602 if (NACL_TARGET_SUBARCH == 64) {
603 for (uint8_t rex1 = NaClRexMin; rex1 <= NaClRexMax; ++rex1) {
604 for (uint8_t rex2 = NaClRexMin; rex2 <= NaClRexMax; ++rex2) {
605 Plant(rex1);
606 Plant(rex2);
607 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
608 VerifyConsumedPrefixBytes(2, 2, kPrefixREX);
609 ResetInput();
610 ResetState();
611 }
612 }
613 }
614 }
615
616 // Test function NaClConsumePrefixBytes to see if we allow multiple
617 // copies of the same (non-rex) prefix.
618 TEST_F(NcInstStateTests, ConsumeDuplicatePrefixes) {
619 // Try with non rex prefixes.
620 for (size_t i = 0; i < NACL_ARRAY_SIZE(prefix_values); ++i) {
621 Plant(prefix_values[i].byte);
622 Plant(prefix_values[i].byte);
623 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
624 VerifyConsumedPrefixBytes(2, 0, prefix_values[i].mask);
625 ResetInput();
626 ResetState();
627 }
628 }
629
630 // Test if we can recognize 14 prefix bytes.
631 TEST_F(NcInstStateTests, Consume14PrefixBytes) {
632 for (int i = 0; i < 14; ++i) {
633 Plant(kValueDATA16);
634 }
635 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
636 VerifyConsumedPrefixBytes(14, 0, kPrefixDATA16);
637 }
638
639 // Test that we can't accept 15 prefix bytes.
640 TEST_F(NcInstStateTests, Consume15PrefixBytes) {
641 for (int i = 0; i < 15; ++i) {
642 Plant(kValueDATA16);
643 }
644 EXPECT_TRUE(Bool2bool(NaClConsumePrefixBytes(_state)));
645 EXPECT_EQ((uint8_t) 14, _state->bytes.length);
646 }
647
648 // Defines the set of prefix bytes that effect multibyte instructions
649 // (i.e. REP, REPNE, and DATA16), and all possible combinations of
650 // these prefixes.
651 static const uint32_t kMultibytePrefixes[] = {
652 0,
653 kPrefixREP,
654 kPrefixREP | kPrefixREPNE,
655 kPrefixREP | kPrefixREPNE | kPrefixDATA16,
656 kPrefixREPNE,
657 kPrefixREPNE | kPrefixDATA16,
658 kPrefixDATA16
659 };
660
661 // Test function NaClConsume0F38XXNaClInstBytes, as called through
662 // function NaClConsumeInstBytes.
663 TEST_F(NcInstStateTests, ConsumeOF38XXInstructions) {
664 // First try effects of just multibyte prefixes.
665 for (size_t i = 0; i < NACL_ARRAY_SIZE(kMultibytePrefixes); ++i) {
666 _state->prefix_mask = kMultibytePrefixes[i];
667 VerifyConsume0F38XXInstructions();
668
669 // Verify that adding a rex prefix don't effect anything.
670 _state->prefix_mask = kMultibytePrefixes[i] | kPrefixREX;
671 VerifyConsume0F38XXInstructions();
672
673 // Now try adding other possible prefixes to see if they break anything.
674 for (size_t j = 0; j < NACL_ARRAY_SIZE(prefix_values); ++j) {
675 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask;
676 VerifyConsume0F38XXInstructions();
677
678 // Verify that adding a rex prefix don't effect anything.
679 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask
680 | kPrefixREX;
681 VerifyConsume0F38XXInstructions();
682 }
683 }
684 }
685
686 // Test function NaClConsume0F3AXXNaClInstBytes, as called through
687 // function NaClConsumeInstBytes.
688 TEST_F(NcInstStateTests, ConsumeOF3AXXInstructions) {
689 // First try effects of just multibyte prefixes.
690 for (size_t i = 0; i < NACL_ARRAY_SIZE(kMultibytePrefixes); ++i) {
691 _state->prefix_mask = kMultibytePrefixes[i];
692 VerifyConsume0F3AXXInstructions();
693
694 // Verify that adding a rex prefix don't effect anything.
695 _state->prefix_mask = kMultibytePrefixes[i] | kPrefixREX;
696 VerifyConsume0F3AXXInstructions();
697
698 // Now try adding other possible prefixes to see if they break anything.
699 for (size_t j = 0; j < NACL_ARRAY_SIZE(prefix_values); ++j) {
700 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask;
701 VerifyConsume0F3AXXInstructions();
702
703 // Verify that adding a rex prefix don't effect anything.
704 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask
705 | kPrefixREX;
706 VerifyConsume0F3AXXInstructions();
707 }
708 }
709 }
710
711 // Test function NaClConsume0FXXNaClInstBytes, as called through
712 // function NaClConsumeInstBytes.
713 TEST_F(NcInstStateTests, ConsumeOFXXInstructions) {
714 // First try effects of just multibyte prefixes.
715 for (size_t i = 0; i < NACL_ARRAY_SIZE(kMultibytePrefixes); ++i) {
716 _state->prefix_mask = kMultibytePrefixes[i];
717 VerifyConsume0FXXInstructions();
718
719 // Verify that adding a rex prefix don't effect anything.
720 _state->prefix_mask = kMultibytePrefixes[i] | kPrefixREX;
721 VerifyConsume0FXXInstructions();
722
723 // Now try adding other possible prefixes to see if they break anything.
724 for (size_t j = 0; j < NACL_ARRAY_SIZE(prefix_values); ++j) {
725 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask;
726 VerifyConsume0FXXInstructions();
727
728 // Verify that adding a rex prefix don't effect anything.
729 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask
730 | kPrefixREX;
731 VerifyConsume0FXXInstructions();
732 }
733 }
734 }
735
736 // Test function NaClConsumeX87NaClInstBytes, as called through
737 // function NaClConsumeInstBytes.
738 TEST_F(NcInstStateTests, ConsumeX87Instructions) {
739 // First try effects of just multibyte prefixes.
740 for (size_t i = 0; i < NACL_ARRAY_SIZE(kMultibytePrefixes); ++i) {
741 _state->prefix_mask = kMultibytePrefixes[i];
742 VerifyConsumeX87Instructions();
743
744 // Verify that adding a rex prefix don't effect anything.
745 _state->prefix_mask = kMultibytePrefixes[i] | kPrefixREX;
746 VerifyConsumeX87Instructions();
747
748 // Now try adding other possible prefixes to see if they break anything.
749 for (size_t j = 0; j < NACL_ARRAY_SIZE(prefix_values); ++j) {
750 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask;
751 VerifyConsumeX87Instructions();
752
753 // Verify that adding a rex prefix don't effect anything.
754 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask
755 | kPrefixREX;
756 VerifyConsumeX87Instructions();
757 }
758 }
759 }
760
761 // Test function NaClConsumeInstBytes for one byte instruction values.
762 TEST_F(NcInstStateTests, ConsumeOneByteInstructions) {
763 // First try effects of just multibyte prefixes.
764 for (size_t i = 0; i < NACL_ARRAY_SIZE(kMultibytePrefixes); ++i) {
765 _state->prefix_mask = kMultibytePrefixes[i];
766 VerifyConsumeOneByteInstructions();
767
768 // Verify that adding a rex prefix don't effect anything.
769 _state->prefix_mask = kMultibytePrefixes[i] | kPrefixREX;
770 VerifyConsumeOneByteInstructions();
771
772 // Now try adding other possible prefixes to see if they break anything.
773 for (size_t j = 0; j < NACL_ARRAY_SIZE(prefix_values); ++j) {
774 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask;
775 VerifyConsumeOneByteInstructions();
776
777 // Verify that adding a rex prefix don't effect anything.
778 _state->prefix_mask = kMultibytePrefixes[i] | prefix_values[i].mask
779 | kPrefixREX;
780 VerifyConsumeOneByteInstructions();
781 }
782 }
783 }
784
785 }; // anonymous namespace
786
787 int main(int argc, char *argv[]) {
788 NaClLogModuleInit();
789 testing::InitGoogleTest(&argc, argv);
790 return RUN_ALL_TESTS();
791 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698