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 | |
25 UNIT_TEST_CASE(VerifiedMemoryReserve) { | 18 UNIT_TEST_CASE(VerifiedMemoryReserve) { |
26 Init(); | 19 Init(); |
27 const intptr_t kReservationSize = 64 * KB; | 20 const intptr_t kReservationSize = 64 * KB; |
28 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 21 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
29 EXPECT_EQ(kReservationSize, vm->size()); | 22 EXPECT_EQ(kReservationSize, vm->size()); |
30 delete vm; | 23 delete vm; |
31 Shutdown(); | |
32 } | 24 } |
33 | 25 |
34 | 26 |
35 UNIT_TEST_CASE(VerifiedMemoryCommit) { | 27 UNIT_TEST_CASE(VerifiedMemoryCommit) { |
36 Init(); | 28 Init(); |
37 const intptr_t kReservationSize = 64 * KB; | 29 const intptr_t kReservationSize = 64 * KB; |
38 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 30 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
39 EXPECT_EQ(kReservationSize, vm->size()); | 31 EXPECT_EQ(kReservationSize, vm->size()); |
40 vm->Commit(false); | 32 vm->Commit(false); |
41 delete vm; | 33 delete vm; |
42 Shutdown(); | |
43 } | 34 } |
44 | 35 |
45 | 36 |
46 UNIT_TEST_CASE(VerifiedMemoryBasic) { | 37 UNIT_TEST_CASE(VerifiedMemoryBasic) { |
47 Init(); | 38 Init(); |
48 const intptr_t kReservationSize = 64 * KB; | 39 const intptr_t kReservationSize = 64 * KB; |
49 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 40 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
50 EXPECT_EQ(kReservationSize, vm->size()); | 41 EXPECT_EQ(kReservationSize, vm->size()); |
51 vm->Commit(false); | 42 vm->Commit(false); |
52 double* addr = reinterpret_cast<double*>(vm->address()); | 43 double* addr = reinterpret_cast<double*>(vm->address()); |
53 VerifiedMemory::Write(&addr[0], 0.5); | 44 VerifiedMemory::Write(&addr[0], 0.5); |
54 EXPECT_EQ(0.5, addr[0]); | 45 EXPECT_EQ(0.5, addr[0]); |
55 VerifiedMemory::Write(&addr[1], 1.5); | 46 VerifiedMemory::Write(&addr[1], 1.5); |
56 VerifiedMemory::Write(&addr[2], 2.5); | 47 VerifiedMemory::Write(&addr[2], 2.5); |
57 VerifiedMemory::Write(&addr[0], 0.25); | 48 VerifiedMemory::Write(&addr[0], 0.25); |
58 static const double kNaN = NAN; | 49 static const double kNaN = NAN; |
59 VerifiedMemory::Write(&addr[0], kNaN); // Bitwise comparison should be used. | 50 VerifiedMemory::Write(&addr[0], kNaN); // Bitwise comparison should be used. |
60 VerifiedMemory::Write(&addr[0], 0.5); | 51 VerifiedMemory::Write(&addr[0], 0.5); |
61 int64_t* unverified = reinterpret_cast<int64_t*>(&addr[3]); | 52 int64_t* unverified = reinterpret_cast<int64_t*>(&addr[3]); |
62 *unverified = 123; | 53 *unverified = 123; |
63 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); | 54 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); |
64 delete vm; | 55 delete vm; |
65 Shutdown(); | |
66 } | 56 } |
67 | 57 |
68 | 58 |
69 UNIT_TEST_CASE(VerifiedMemoryAccept) { | 59 UNIT_TEST_CASE(VerifiedMemoryAccept) { |
70 Init(); | 60 Init(); |
71 const intptr_t kReservationSize = 64 * KB; | 61 const intptr_t kReservationSize = 64 * KB; |
72 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 62 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
73 EXPECT_EQ(kReservationSize, vm->size()); | 63 EXPECT_EQ(kReservationSize, vm->size()); |
74 vm->Commit(false); | 64 vm->Commit(false); |
75 double* addr = reinterpret_cast<double*>(vm->address()); | 65 double* addr = reinterpret_cast<double*>(vm->address()); |
76 VerifiedMemory::Write(&addr[0], 0.5); | 66 VerifiedMemory::Write(&addr[0], 0.5); |
77 VerifiedMemory::Write(&addr[1], 1.5); | 67 VerifiedMemory::Write(&addr[1], 1.5); |
78 VerifiedMemory::Write(&addr[2], 2.5); | 68 VerifiedMemory::Write(&addr[2], 2.5); |
79 VerifiedMemory::Write(&addr[0], 0.25); | 69 VerifiedMemory::Write(&addr[0], 0.25); |
80 // Unverified write followed by Accept ("I know what I'm doing"). | 70 // Unverified write followed by Accept ("I know what I'm doing"). |
81 memset(addr, 0xf3, 2 * sizeof(double)); | 71 memset(addr, 0xf3, 2 * sizeof(double)); |
82 VerifiedMemory::Accept(reinterpret_cast<uword>(addr), 2 * sizeof(double)); | 72 VerifiedMemory::Accept(reinterpret_cast<uword>(addr), 2 * sizeof(double)); |
83 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); | 73 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); |
84 delete vm; | 74 delete vm; |
85 Shutdown(); | |
86 } | 75 } |
87 | 76 |
88 | 77 |
89 // Negative tests below. | 78 // Negative tests below. |
90 | 79 |
91 UNIT_TEST_CASE(VerifyImplicit_Crash) { | 80 UNIT_TEST_CASE(VerifyImplicit_Crash) { |
92 Init(); | 81 Init(); |
93 const intptr_t kReservationSize = 64 * KB; | 82 const intptr_t kReservationSize = 64 * KB; |
94 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 83 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
95 EXPECT_EQ(kReservationSize, vm->size()); | 84 EXPECT_EQ(kReservationSize, vm->size()); |
96 vm->Commit(false); | 85 vm->Commit(false); |
97 double* addr = reinterpret_cast<double*>(vm->address()); | 86 double* addr = reinterpret_cast<double*>(vm->address()); |
98 addr[0] = 0.5; // Forget to use Write. | 87 addr[0] = 0.5; // Forget to use Write. |
99 VerifiedMemory::Write(&addr[0], 1.5); | 88 VerifiedMemory::Write(&addr[0], 1.5); |
100 Shutdown(); | |
101 } | 89 } |
102 | 90 |
103 | 91 |
104 UNIT_TEST_CASE(VerifyExplicit_Crash) { | 92 UNIT_TEST_CASE(VerifyExplicit_Crash) { |
105 Init(); | 93 Init(); |
106 const intptr_t kReservationSize = 64 * KB; | 94 const intptr_t kReservationSize = 64 * KB; |
107 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); | 95 VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize); |
108 EXPECT_EQ(kReservationSize, vm->size()); | 96 EXPECT_EQ(kReservationSize, vm->size()); |
109 vm->Commit(false); | 97 vm->Commit(false); |
110 double* addr = reinterpret_cast<double*>(vm->address()); | 98 double* addr = reinterpret_cast<double*>(vm->address()); |
111 VerifiedMemory::Write(&addr[0], 0.5); | 99 VerifiedMemory::Write(&addr[0], 0.5); |
112 VerifiedMemory::Write(&addr[1], 1.5); | 100 VerifiedMemory::Write(&addr[1], 1.5); |
113 addr[1] = 3.5; // Forget to use Write. | 101 addr[1] = 3.5; // Forget to use Write. |
114 VerifiedMemory::Write(&addr[2], 2.5); | 102 VerifiedMemory::Write(&addr[2], 2.5); |
115 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); | 103 VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double)); |
116 Shutdown(); | |
117 } | 104 } |
118 | 105 |
119 } // namespace dart | 106 } // namespace dart |
OLD | NEW |