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; | |
49 using nacl_mips_dec::kRegListReserved; | |
50 | 48 |
51 namespace { | 49 namespace { |
52 | 50 |
53 typedef uint32_t mips_inst; | 51 typedef uint32_t mips_inst; |
54 | 52 |
55 /* | 53 /* |
56 * We use these parameters to initialize the validator, below. They are | 54 * We use these parameters to initialize the validator, below. They are |
57 * 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 |
58 * 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. |
59 */ | 57 */ |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 }; | 135 }; |
138 | 136 |
139 | 137 |
140 /* | 138 /* |
141 * Primitive tests checking various constructor properties. Any of these | 139 * Primitive tests checking various constructor properties. Any of these |
142 * failing would be a very bad sign indeed. | 140 * failing would be a very bad sign indeed. |
143 */ | 141 */ |
144 | 142 |
145 TEST_F(ValidatorTests, RecognizesDataAddressRegisters) { | 143 TEST_F(ValidatorTests, RecognizesDataAddressRegisters) { |
146 /* | 144 /* |
147 * Note that the logic below needs to be kept in sync with the definition | 145 * Note that the logic below needs to be kept in sync with the implementation |
148 * of kAbiDataAddrRegisters at the top of this file. | 146 * of RegisterList::DataAddrRegs(). |
149 * | 147 * |
150 * This test is pretty trivial -- we can exercise the data_address_register | 148 * This test is pretty trivial -- we can exercise the data_address_register |
151 * functionality more deeply with pattern tests below. | 149 * functionality more deeply with pattern tests below. |
152 */ | 150 */ |
153 for (int i = 0; i < 31; i++) { | 151 for (int i = 0; i < 31; i++) { |
154 Register reg(i); | 152 Register reg(i); |
155 if (reg.Equals(nacl_mips_dec::kRegisterStack)) { | 153 if (reg.Equals(Register::Sp()) || reg.Equals(Register::Tls())) { |
156 EXPECT_TRUE(_validator.IsDataAddressRegister(reg)) | 154 EXPECT_TRUE(_validator.IsDataAddressRegister(reg)) |
157 << "Stack pointer must be a data address register."; | 155 << "Stack pointer and TLS register must be data address registers."; |
158 } else { | 156 } else { |
159 EXPECT_FALSE(_validator.IsDataAddressRegister(reg)) | 157 EXPECT_FALSE(_validator.IsDataAddressRegister(reg)) |
160 << "Only the stack pointer must be a data address register."; | 158 << "Only the stack pointer and TLS register are data " |
| 159 "address registers."; |
161 } | 160 } |
162 } | 161 } |
163 } | 162 } |
164 | 163 |
165 /* | 164 /* |
166 * Code validation tests | 165 * Code validation tests |
167 */ | 166 */ |
168 | 167 |
169 // This is where untrusted code starts. Most tests use this. | 168 // This is where untrusted code starts. Most tests use this. |
170 static const uint32_t kDefaultBaseAddr = 0x20000; | 169 static const uint32_t kDefaultBaseAddr = 0x20000; |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 } | 396 } |
398 | 397 |
399 /* | 398 /* |
400 * Implementation of the ValidatorTests utility methods. These are documented | 399 * Implementation of the ValidatorTests utility methods. These are documented |
401 * toward the top of this file. | 400 * toward the top of this file. |
402 */ | 401 */ |
403 ValidatorTests::ValidatorTests() | 402 ValidatorTests::ValidatorTests() |
404 : _validator(kBytesPerBundle, | 403 : _validator(kBytesPerBundle, |
405 kCodeRegionSize, | 404 kCodeRegionSize, |
406 kDataRegionSize, | 405 kDataRegionSize, |
407 kRegListReserved, | 406 RegisterList::ReservedRegs(), |
408 RegisterList(kRegisterStack)) {} | 407 RegisterList::DataAddrRegs()) {} |
409 | 408 |
410 bool ValidatorTests::Validate(const mips_inst *pattern, | 409 bool ValidatorTests::Validate(const mips_inst *pattern, |
411 size_t inst_count, | 410 size_t inst_count, |
412 uint32_t start_addr, | 411 uint32_t start_addr, |
413 ProblemSink *sink) { | 412 ProblemSink *sink) { |
414 // We think in instructions; CodeSegment thinks in bytes. | 413 // We think in instructions; CodeSegment thinks in bytes. |
415 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(pattern); | 414 const uint8_t *bytes = reinterpret_cast<const uint8_t *>(pattern); |
416 CodeSegment segment(bytes, start_addr, inst_count * sizeof(mips_inst)); | 415 CodeSegment segment(bytes, start_addr, inst_count * sizeof(mips_inst)); |
417 | 416 |
418 vector<CodeSegment> segments; | 417 vector<CodeSegment> segments; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 return problems; | 456 return problems; |
458 } | 457 } |
459 | 458 |
460 }; // anonymous namespace | 459 }; // anonymous namespace |
461 | 460 |
462 // Test driver function. | 461 // Test driver function. |
463 int main(int argc, char *argv[]) { | 462 int main(int argc, char *argv[]) { |
464 testing::InitGoogleTest(&argc, argv); | 463 testing::InitGoogleTest(&argc, argv); |
465 return RUN_ALL_TESTS(); | 464 return RUN_ALL_TESTS(); |
466 } | 465 } |
OLD | NEW |