OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 The Native Client Authors. All rights reserved. | 2 * Copyright 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * Unit tests for the MIPS validator | 8 * Unit tests for the MIPS validator |
9 * | 9 * |
10 * These tests use the google-test framework (gtest for short) to exercise the | 10 * These tests use the google-test framework (gtest for short) to exercise the |
(...skipping 25 matching lines...) Expand all Loading... | |
36 using std::string; | 36 using std::string; |
37 using std::ostringstream; | 37 using std::ostringstream; |
38 | 38 |
39 using nacl_mips_dec::Register; | 39 using nacl_mips_dec::Register; |
40 using nacl_mips_dec::RegisterList; | 40 using nacl_mips_dec::RegisterList; |
41 using nacl_mips_dec::Instruction; | 41 using nacl_mips_dec::Instruction; |
42 | 42 |
43 using nacl_mips_val::SfiValidator; | 43 using nacl_mips_val::SfiValidator; |
44 using nacl_mips_val::ProblemSink; | 44 using nacl_mips_val::ProblemSink; |
45 using nacl_mips_val::CodeSegment; | 45 using nacl_mips_val::CodeSegment; |
46 using nacl_mips_dec::kInstrSize; | |
47 using nacl_mips_dec::kNop; | |
46 | 48 |
47 namespace { | 49 namespace { |
48 | 50 |
49 typedef uint32_t mips_inst; | 51 typedef uint32_t mips_inst; |
50 | 52 |
51 /* | 53 /* |
52 * We use these parameters to initialize the validator, below. They are | 54 * We use these parameters to initialize the validator, below. They are |
53 * somewhat different from the parameters used in real NaCl systems, to test | 55 * somewhat different from the parameters used in real NaCl systems, to test |
54 * degrees of validator freedom that we don't currently exercise in prod. | 56 * degrees of validator freedom that we don't currently exercise in prod. |
55 */ | 57 */ |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 static const AnnotatedInstruction examples_of_safe_jump_masks[] = { | 245 static const AnnotatedInstruction examples_of_safe_jump_masks[] = { |
244 { (10 << 21 | 14 << 16 | 10 << 11 | 36), | 246 { (10 << 21 | 14 << 16 | 10 << 11 | 36), |
245 "and t2,t2,t6 : simple jump masking" }, | 247 "and t2,t2,t6 : simple jump masking" }, |
246 }; | 248 }; |
247 | 249 |
248 static const AnnotatedInstruction examples_of_safe_jumps[] = { | 250 static const AnnotatedInstruction examples_of_safe_jumps[] = { |
249 { ((10<<21| 31 << 11 |9) ), "simple jump jalr t2" }, | 251 { ((10<<21| 31 << 11 |9) ), "simple jump jalr t2" }, |
250 { (10<<21|8), "simple jump jr t2" }, | 252 { (10<<21|8), "simple jump jr t2" }, |
251 }; | 253 }; |
252 | 254 |
253 static const AnnotatedInstruction nop_instruction[] = { | |
254 { 0, "nop"}, | |
255 }; | |
256 | |
257 TEST_F(ValidatorTests, SafeMaskedJumps) { | 255 TEST_F(ValidatorTests, SafeMaskedJumps) { |
258 /* | 256 /* |
259 * Produces examples of masked jumps using the safe jump table | 257 * Produces examples of masked jumps using the safe jump table |
260 * (above) and the list of possible masking instructions (above). | 258 * (above) and the list of possible masking instructions (above). |
261 */ | 259 */ |
262 for (unsigned m = 0; m < NACL_ARRAY_SIZE(examples_of_safe_jump_masks); m++) { | 260 for (unsigned m = 0; m < NACL_ARRAY_SIZE(examples_of_safe_jump_masks); m++) { |
263 for (unsigned s = 0; s < NACL_ARRAY_SIZE(examples_of_safe_jumps); s++) { | 261 for (unsigned s = 0; s < NACL_ARRAY_SIZE(examples_of_safe_jumps); s++) { |
264 ostringstream message; | 262 ostringstream message; |
265 message << examples_of_safe_jump_masks[m].about | 263 message << examples_of_safe_jump_masks[m].about |
266 << ", " | 264 << ", " |
267 << examples_of_safe_jumps[s].about; | 265 << examples_of_safe_jumps[s].about; |
268 mips_inst program[] = { | 266 mips_inst program[] = { |
269 nop_instruction[0].inst, | 267 kNop, |
270 examples_of_safe_jump_masks[m].inst, | 268 examples_of_safe_jump_masks[m].inst, |
271 examples_of_safe_jumps[s].inst, | 269 examples_of_safe_jumps[s].inst, |
272 }; | 270 }; |
273 ValidationShouldPass(program, | 271 ValidationShouldPass(program, |
274 3, | 272 3, |
275 kDefaultBaseAddr, | 273 kDefaultBaseAddr, |
276 message.str()); | 274 message.str()); |
277 } | 275 } |
278 } | 276 } |
279 } | 277 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 */ | 348 */ |
351 for (unsigned m = 0; m < NACL_ARRAY_SIZE(examples_of_safe_stack_masks); m++) { | 349 for (unsigned m = 0; m < NACL_ARRAY_SIZE(examples_of_safe_stack_masks); m++) { |
352 for (unsigned s = 0; s < NACL_ARRAY_SIZE(examples_of_safe_stack_ops); s++) { | 350 for (unsigned s = 0; s < NACL_ARRAY_SIZE(examples_of_safe_stack_ops); s++) { |
353 ostringstream bad_message; | 351 ostringstream bad_message; |
354 bad_message << examples_of_safe_stack_masks[m].about | 352 bad_message << examples_of_safe_stack_masks[m].about |
355 << ", " | 353 << ", " |
356 << examples_of_safe_stack_ops[s].about; | 354 << examples_of_safe_stack_ops[s].about; |
357 mips_inst bad_program[] = { | 355 mips_inst bad_program[] = { |
358 examples_of_safe_stack_masks[m].inst, | 356 examples_of_safe_stack_masks[m].inst, |
359 examples_of_safe_stack_ops[s].inst, | 357 examples_of_safe_stack_ops[s].inst, |
360 nop_instruction[0].inst | 358 kNop |
361 }; | 359 }; |
362 | 360 |
363 ValidationShouldFail(bad_program, | 361 ValidationShouldFail(bad_program, |
364 3, | 362 3, |
365 kDefaultBaseAddr, | 363 kDefaultBaseAddr, |
366 bad_message.str()); | 364 bad_message.str()); |
367 } | 365 } |
368 } | 366 } |
369 } | 367 } |
370 | 368 |
369 TEST_F(ValidatorTests, UnmaskedSpUpdate) { | |
JF
2012/11/16 21:03:27
Please also add a test for nop (as in the ARM chan
petarj
2012/11/19 18:46:10
Done.
| |
370 vector<mips_inst> code(_validator.bytes_per_bundle()/kInstrSize, kNop); | |
Mark Seaborn
2012/11/16 20:08:08
Nit: please put spaces around '/'
petarj
2012/11/19 18:46:10
Done.
| |
371 for (vector<mips_inst>::size_type i = 0; i < code.size(); ++i) { | |
372 std::fill(code.begin(), code.end(), kNop); | |
373 code[i] = 0x8fbd0000; // lw $sp, 0($sp) | |
374 ValidationShouldFail(&code[0], code.size(), kDefaultBaseAddr, | |
375 "unmasked SP update"); | |
376 } | |
377 } | |
378 | |
371 /* | 379 /* |
372 * Implementation of the ValidatorTests utility methods. These are documented | 380 * Implementation of the ValidatorTests utility methods. These are documented |
373 * toward the top of this file. | 381 * toward the top of this file. |
374 */ | 382 */ |
375 ValidatorTests::ValidatorTests() | 383 ValidatorTests::ValidatorTests() |
376 : _validator(kBytesPerBundle, | 384 : _validator(kBytesPerBundle, |
377 kCodeRegionSize, | 385 kCodeRegionSize, |
378 kDataRegionSize, | 386 kDataRegionSize, |
379 nacl_mips_dec::kRegListReserved, | 387 nacl_mips_dec::kRegListReserved, |
380 nacl_mips_dec::kRegisterStack) {} | 388 nacl_mips_dec::kRegisterStack) {} |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
429 return problems; | 437 return problems; |
430 } | 438 } |
431 | 439 |
432 }; // anonymous namespace | 440 }; // anonymous namespace |
433 | 441 |
434 // Test driver function. | 442 // Test driver function. |
435 int main(int argc, char *argv[]) { | 443 int main(int argc, char *argv[]) { |
436 testing::InitGoogleTest(&argc, argv); | 444 testing::InitGoogleTest(&argc, argv); |
437 return RUN_ALL_TESTS(); | 445 return RUN_ALL_TESTS(); |
438 } | 446 } |
OLD | NEW |