OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/unit_test.h" | 6 #include "vm/unit_test.h" |
7 #include "vm/verified_memory.h" | 7 #include "vm/verified_memory.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 void Init() { | 11 void Init() { |
12 #if defined(DEBUG) | 12 #if defined(DEBUG) |
13 FLAG_verified_mem = true; | 13 FLAG_verified_mem = true; |
14 #endif | 14 #endif |
15 } | 15 } |
16 | 16 |
17 | 17 |
| 18 void Shutdown() { |
| 19 #if defined(DEBUG) |
| 20 FLAG_verified_mem = false; |
| 21 #endif |
| 22 } |
| 23 |
| 24 |
18 UNIT_TEST_CASE(VerifiedMemoryReserve) { | 25 UNIT_TEST_CASE(VerifiedMemoryReserve) { |
19 Init(); | 26 Init(); |
20 const intptr_t kReservationSize = 64 * KB; | 27 const intptr_t kReservationSize = 64 * KB; |
21 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 28 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
22 EXPECT_EQ(kReservationSize, vm->size()); | 29 EXPECT_EQ(kReservationSize, vm->size()); |
23 delete vm; | 30 delete vm; |
| 31 Shutdown(); |
24 } | 32 } |
25 | 33 |
26 | 34 |
27 UNIT_TEST_CASE(VerifiedMemoryCommit) { | 35 UNIT_TEST_CASE(VerifiedMemoryCommit) { |
28 Init(); | 36 Init(); |
29 const intptr_t kReservationSize = 64 * KB; | 37 const intptr_t kReservationSize = 64 * KB; |
30 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 38 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
31 EXPECT_EQ(kReservationSize, vm->size()); | 39 EXPECT_EQ(kReservationSize, vm->size()); |
32 vm->Commit(false); | 40 vm->Commit(false); |
33 delete vm; | 41 delete vm; |
| 42 Shutdown(); |
34 } | 43 } |
35 | 44 |
36 | 45 |
37 UNIT_TEST_CASE(VerifiedMemoryBasic) { | 46 UNIT_TEST_CASE(VerifiedMemoryBasic) { |
38 Init(); | 47 Init(); |
39 const intptr_t kReservationSize = 64 * KB; | 48 const intptr_t kReservationSize = 64 * KB; |
40 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 49 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
41 EXPECT_EQ(kReservationSize, vm->size()); | 50 EXPECT_EQ(kReservationSize, vm->size()); |
42 vm->Commit(false); | 51 vm->Commit(false); |
43 double* addr = reinterpret_cast<double*>(vm->address()); | 52 double* addr = reinterpret_cast<double*>(vm->address()); |
44 VerifiedMemory::Write(&addr[0], 0.5); | 53 VerifiedMemory::Write(&addr[0], 0.5); |
45 EXPECT_EQ(0.5, addr[0]); | 54 EXPECT_EQ(0.5, addr[0]); |
46 VerifiedMemory::Write(&addr[1], 1.5); | 55 VerifiedMemory::Write(&addr[1], 1.5); |
47 VerifiedMemory::Write(&addr[2], 2.5); | 56 VerifiedMemory::Write(&addr[2], 2.5); |
48 VerifiedMemory::Write(&addr[0], 0.25); | 57 VerifiedMemory::Write(&addr[0], 0.25); |
49 static const double kNaN = NAN; | 58 static const double kNaN = NAN; |
50 VerifiedMemory::Write(&addr[0], kNaN); // Bitwise comparison should be used. | 59 VerifiedMemory::Write(&addr[0], kNaN); // Bitwise comparison should be used. |
51 VerifiedMemory::Write(&addr[0], 0.5); | 60 VerifiedMemory::Write(&addr[0], 0.5); |
52 int64_t* unverified = reinterpret_cast<int64_t*>(&addr[3]); | 61 int64_t* unverified = reinterpret_cast<int64_t*>(&addr[3]); |
53 *unverified = 123; | 62 *unverified = 123; |
54 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); | 63 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); |
55 delete vm; | 64 delete vm; |
| 65 Shutdown(); |
56 } | 66 } |
57 | 67 |
58 | 68 |
59 UNIT_TEST_CASE(VerifiedMemoryAccept) { | 69 UNIT_TEST_CASE(VerifiedMemoryAccept) { |
60 Init(); | 70 Init(); |
61 const intptr_t kReservationSize = 64 * KB; | 71 const intptr_t kReservationSize = 64 * KB; |
62 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 72 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
63 EXPECT_EQ(kReservationSize, vm->size()); | 73 EXPECT_EQ(kReservationSize, vm->size()); |
64 vm->Commit(false); | 74 vm->Commit(false); |
65 double* addr = reinterpret_cast<double*>(vm->address()); | 75 double* addr = reinterpret_cast<double*>(vm->address()); |
66 VerifiedMemory::Write(&addr[0], 0.5); | 76 VerifiedMemory::Write(&addr[0], 0.5); |
67 VerifiedMemory::Write(&addr[1], 1.5); | 77 VerifiedMemory::Write(&addr[1], 1.5); |
68 VerifiedMemory::Write(&addr[2], 2.5); | 78 VerifiedMemory::Write(&addr[2], 2.5); |
69 VerifiedMemory::Write(&addr[0], 0.25); | 79 VerifiedMemory::Write(&addr[0], 0.25); |
70 // Unverified write followed by Accept ("I know what I'm doing"). | 80 // Unverified write followed by Accept ("I know what I'm doing"). |
71 memset(addr, 0xf3, 2 * sizeof(double)); | 81 memset(addr, 0xf3, 2 * sizeof(double)); |
72 VerifiedMemory::Accept(reinterpret_cast<uword>(addr), 2 * sizeof(double)); | 82 VerifiedMemory::Accept(reinterpret_cast<uword>(addr), 2 * sizeof(double)); |
73 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); | 83 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); |
74 delete vm; | 84 delete vm; |
| 85 Shutdown(); |
75 } | 86 } |
76 | 87 |
77 | 88 |
78 // Negative tests below. | 89 // Negative tests below. |
79 | 90 |
80 UNIT_TEST_CASE(VerifyImplicit_Crash) { | 91 UNIT_TEST_CASE(VerifyImplicit_Crash) { |
81 Init(); | 92 Init(); |
82 const intptr_t kReservationSize = 64 * KB; | 93 const intptr_t kReservationSize = 64 * KB; |
83 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 94 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
84 EXPECT_EQ(kReservationSize, vm->size()); | 95 EXPECT_EQ(kReservationSize, vm->size()); |
85 vm->Commit(false); | 96 vm->Commit(false); |
86 double* addr = reinterpret_cast<double*>(vm->address()); | 97 double* addr = reinterpret_cast<double*>(vm->address()); |
87 addr[0] = 0.5; // Forget to use Write. | 98 addr[0] = 0.5; // Forget to use Write. |
88 VerifiedMemory::Write(&addr[0], 1.5); | 99 VerifiedMemory::Write(&addr[0], 1.5); |
| 100 Shutdown(); |
89 } | 101 } |
90 | 102 |
91 | 103 |
92 UNIT_TEST_CASE(VerifyExplicit_Crash) { | 104 UNIT_TEST_CASE(VerifyExplicit_Crash) { |
93 Init(); | 105 Init(); |
94 const intptr_t kReservationSize = 64 * KB; | 106 const intptr_t kReservationSize = 64 * KB; |
95 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 107 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
96 EXPECT_EQ(kReservationSize, vm->size()); | 108 EXPECT_EQ(kReservationSize, vm->size()); |
97 vm->Commit(false); | 109 vm->Commit(false); |
98 double* addr = reinterpret_cast<double*>(vm->address()); | 110 double* addr = reinterpret_cast<double*>(vm->address()); |
99 VerifiedMemory::Write(&addr[0], 0.5); | 111 VerifiedMemory::Write(&addr[0], 0.5); |
100 VerifiedMemory::Write(&addr[1], 1.5); | 112 VerifiedMemory::Write(&addr[1], 1.5); |
101 addr[1] = 3.5; // Forget to use Write. | 113 addr[1] = 3.5; // Forget to use Write. |
102 VerifiedMemory::Write(&addr[2], 2.5); | 114 VerifiedMemory::Write(&addr[2], 2.5); |
103 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); | 115 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); |
| 116 Shutdown(); |
104 } | 117 } |
105 | 118 |
106 } // namespace dart | 119 } // namespace dart |
OLD | NEW |