Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * 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 27 matching lines...) Expand all Loading... | |
| 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; | 46 using nacl_mips_dec::kInstrSize; |
| 47 using nacl_mips_dec::kNop; | 47 using nacl_mips_dec::kNop; |
| 48 using nacl_mips_dec::kRegisterStack; | 48 using nacl_mips_dec::kRegListDataAddr; |
| 49 using nacl_mips_dec::kRegListReserved; | 49 using nacl_mips_dec::kRegListReserved; |
| 50 | 50 |
| 51 namespace { | 51 namespace { |
| 52 | 52 |
| 53 typedef uint32_t mips_inst; | 53 typedef uint32_t mips_inst; |
| 54 | 54 |
| 55 /* | 55 /* |
| 56 * We use these parameters to initialize the validator, below. They are | 56 * We use these parameters to initialize the validator, below. They are |
| 57 * somewhat different from the parameters used in real NaCl systems, to test | 57 * somewhat different from the parameters used in real NaCl systems, to test |
| 58 * degrees of validator freedom that we don't currently exercise in prod. | 58 * degrees of validator freedom that we don't currently exercise in prod. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 | 138 |
| 139 | 139 |
| 140 /* | 140 /* |
| 141 * Primitive tests checking various constructor properties. Any of these | 141 * Primitive tests checking various constructor properties. Any of these |
| 142 * failing would be a very bad sign indeed. | 142 * failing would be a very bad sign indeed. |
| 143 */ | 143 */ |
| 144 | 144 |
| 145 TEST_F(ValidatorTests, RecognizesDataAddressRegisters) { | 145 TEST_F(ValidatorTests, RecognizesDataAddressRegisters) { |
| 146 /* | 146 /* |
| 147 * Note that the logic below needs to be kept in sync with the definition | 147 * Note that the logic below needs to be kept in sync with the definition |
| 148 * of kAbiDataAddrRegisters at the top of this file. | 148 * of kRegListDataAddr at the top of this file. |
|
Mark Seaborn
2013/01/15 16:51:36
Update comment? kRegListDataAddr isn't defined at
petarj
2013/01/22 22:57:30
Done.
| |
| 149 * | 149 * |
| 150 * This test is pretty trivial -- we can exercise the data_address_register | 150 * This test is pretty trivial -- we can exercise the data_address_register |
| 151 * functionality more deeply with pattern tests below. | 151 * functionality more deeply with pattern tests below. |
| 152 */ | 152 */ |
| 153 for (int i = 0; i < 31; i++) { | 153 for (int i = 0; i < 31; i++) { |
| 154 Register reg(i); | 154 Register reg(i); |
| 155 if (reg.Equals(nacl_mips_dec::kRegisterStack)) { | 155 if (reg.Equals(nacl_mips_dec::kRegisterStack) |
| 156 || reg.Equals(nacl_mips_dec::kRegisterTls)) { | |
| 156 EXPECT_TRUE(_validator.IsDataAddressRegister(reg)) | 157 EXPECT_TRUE(_validator.IsDataAddressRegister(reg)) |
| 157 << "Stack pointer must be a data address register."; | 158 << "Stack pointer and TLS register must be data address registers."; |
| 158 } else { | 159 } else { |
| 159 EXPECT_FALSE(_validator.IsDataAddressRegister(reg)) | 160 EXPECT_FALSE(_validator.IsDataAddressRegister(reg)) |
| 160 << "Only the stack pointer must be a data address register."; | 161 << "Only the stack pointer and TLS register are data " |
| 162 "address registers."; | |
| 161 } | 163 } |
| 162 } | 164 } |
| 163 } | 165 } |
| 164 | 166 |
| 165 /* | 167 /* |
| 166 * Code validation tests | 168 * Code validation tests |
| 167 */ | 169 */ |
| 168 | 170 |
| 169 // This is where untrusted code starts. Most tests use this. | 171 // This is where untrusted code starts. Most tests use this. |
| 170 static const uint32_t kDefaultBaseAddr = 0x20000; | 172 static const uint32_t kDefaultBaseAddr = 0x20000; |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 | 400 |
| 399 /* | 401 /* |
| 400 * Implementation of the ValidatorTests utility methods. These are documented | 402 * Implementation of the ValidatorTests utility methods. These are documented |
| 401 * toward the top of this file. | 403 * toward the top of this file. |
| 402 */ | 404 */ |
| 403 ValidatorTests::ValidatorTests() | 405 ValidatorTests::ValidatorTests() |
| 404 : _validator(kBytesPerBundle, | 406 : _validator(kBytesPerBundle, |
| 405 kCodeRegionSize, | 407 kCodeRegionSize, |
| 406 kDataRegionSize, | 408 kDataRegionSize, |
| 407 kRegListReserved, | 409 kRegListReserved, |
| 408 RegisterList(kRegisterStack)) {} | 410 kRegListDataAddr) {} |
| 409 | 411 |
| 410 bool ValidatorTests::Validate(const mips_inst *pattern, | 412 bool ValidatorTests::Validate(const mips_inst *pattern, |
| 411 size_t inst_count, | 413 size_t inst_count, |
| 412 uint32_t start_addr, | 414 uint32_t start_addr, |
| 413 ProblemSink *sink) { | 415 ProblemSink *sink) { |
| 414 // We think in instructions; CodeSegment thinks in bytes. | 416 // We think in instructions; CodeSegment thinks in bytes. |
| 415 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(pattern); | 417 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(pattern); |
| 416 CodeSegment segment(bytes, start_addr, inst_count * sizeof(mips_inst)); | 418 CodeSegment segment(bytes, start_addr, inst_count * sizeof(mips_inst)); |
| 417 | 419 |
| 418 vector<CodeSegment> segments; | 420 vector<CodeSegment> segments; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 457 return problems; | 459 return problems; |
| 458 } | 460 } |
| 459 | 461 |
| 460 }; // anonymous namespace | 462 }; // anonymous namespace |
| 461 | 463 |
| 462 // Test driver function. | 464 // Test driver function. |
| 463 int main(int argc, char *argv[]) { | 465 int main(int argc, char *argv[]) { |
| 464 testing::InitGoogleTest(&argc, argv); | 466 testing::InitGoogleTest(&argc, argv); |
| 465 return RUN_ALL_TESTS(); | 467 return RUN_ALL_TESTS(); |
| 466 } | 468 } |
| OLD | NEW |