OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Redistribution and use in source and binary forms, with or without |
3 // found in the LICENSE file. | 3 // modification, are permitted provided that the following conditions are |
4 | 4 // met: |
5 // Test embedded constant pool builder code. | 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 // Test constant pool array code. |
6 | 29 |
7 #include "src/v8.h" | 30 #include "src/v8.h" |
8 | 31 |
9 #include "src/assembler.h" | 32 #include "src/factory.h" |
| 33 #include "src/objects.h" |
10 #include "test/cctest/cctest.h" | 34 #include "test/cctest/cctest.h" |
11 | 35 |
12 using namespace v8::internal; | 36 using namespace v8::internal; |
13 | 37 |
14 const ConstantPoolEntry::Type kPtrType = ConstantPoolEntry::INTPTR; | 38 static ConstantPoolArray::Type kTypes[] = { ConstantPoolArray::INT64, |
15 const ConstantPoolEntry::Type kDblType = ConstantPoolEntry::DOUBLE; | 39 ConstantPoolArray::CODE_PTR, |
16 const ConstantPoolEntry::Access kRegAccess = ConstantPoolEntry::REGULAR; | 40 ConstantPoolArray::HEAP_PTR, |
17 const ConstantPoolEntry::Access kOvflAccess = ConstantPoolEntry::OVERFLOWED; | 41 ConstantPoolArray::INT32 }; |
18 | 42 static ConstantPoolArray::LayoutSection kSmall = |
19 const int kReachBits = 6; // Use reach of 64-bytes to test overflow. | 43 ConstantPoolArray::SMALL_SECTION; |
20 const int kReach = 1 << kReachBits; | 44 static ConstantPoolArray::LayoutSection kExtended = |
21 | 45 ConstantPoolArray::EXTENDED_SECTION; |
22 | 46 |
23 TEST(ConstantPoolPointers) { | 47 Code* DummyCode(LocalContext* context) { |
24 ConstantPoolBuilder builder(kReachBits, kReachBits); | 48 CompileRun("function foo() {};"); |
25 const int kRegularCount = kReach / kPointerSize; | 49 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( |
26 ConstantPoolEntry::Access access; | 50 *v8::Local<v8::Function>::Cast( |
27 int pos = 0; | 51 (*context)->Global()->Get(v8_str("foo")))); |
28 intptr_t value = 0; | 52 return fun->code(); |
29 bool sharing_ok = true; | 53 } |
30 | 54 |
31 CHECK(builder.IsEmpty()); | 55 |
32 while (builder.NextAccess(kPtrType) == kRegAccess) { | 56 TEST(ConstantPoolSmall) { |
33 access = builder.AddEntry(pos++, value++, sharing_ok); | 57 LocalContext context; |
34 CHECK_EQ(access, kRegAccess); | 58 Isolate* isolate = CcTest::i_isolate(); |
35 } | 59 Factory* factory = isolate->factory(); |
36 CHECK(!builder.IsEmpty()); | 60 v8::HandleScope scope(context->GetIsolate()); |
37 CHECK_EQ(pos, kRegularCount); | 61 |
38 | 62 // Check construction. |
39 access = builder.AddEntry(pos, value, sharing_ok); | 63 ConstantPoolArray::NumberOfEntries small(3, 1, 2, 1); |
40 CHECK_EQ(access, kOvflAccess); | 64 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
41 } | 65 |
42 | 66 int expected_counts[] = { 3, 1, 2, 1 }; |
43 | 67 int expected_first_idx[] = { 0, 3, 4, 6 }; |
44 TEST(ConstantPoolDoubles) { | 68 int expected_last_idx[] = { 2, 3, 5, 6 }; |
45 ConstantPoolBuilder builder(kReachBits, kReachBits); | 69 for (int i = 0; i < 4; i++) { |
46 const int kRegularCount = kReach / kDoubleSize; | 70 CHECK_EQ(expected_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
47 ConstantPoolEntry::Access access; | 71 CHECK_EQ(expected_first_idx[i], array->first_index(kTypes[i], kSmall)); |
48 int pos = 0; | 72 CHECK_EQ(expected_last_idx[i], array->last_index(kTypes[i], kSmall)); |
49 double value = 0.0; | 73 } |
50 | 74 CHECK(!array->is_extended_layout()); |
51 CHECK(builder.IsEmpty()); | 75 |
52 while (builder.NextAccess(kDblType) == kRegAccess) { | 76 // Check getters and setters. |
53 access = builder.AddEntry(pos++, value); | 77 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
54 value += 0.5; | 78 Handle<Object> object = factory->NewHeapNumber(4.0, IMMUTABLE, TENURED); |
55 CHECK_EQ(access, kRegAccess); | 79 Code* code = DummyCode(&context); |
56 } | 80 array->set(0, big_number); |
57 CHECK(!builder.IsEmpty()); | 81 array->set(1, 0.5); |
58 CHECK_EQ(pos, kRegularCount); | 82 array->set(2, 3e-24); |
59 | 83 array->set(3, code->entry()); |
60 access = builder.AddEntry(pos, value); | 84 array->set(4, code); |
61 CHECK_EQ(access, kOvflAccess); | 85 array->set(5, *object); |
62 } | 86 array->set(6, 50); |
63 | 87 CHECK_EQ(big_number, array->get_int64_entry(0)); |
64 | 88 CHECK_EQ(0.5, array->get_int64_entry_as_double(1)); |
65 TEST(ConstantPoolMixedTypes) { | 89 CHECK_EQ(3e-24, array->get_int64_entry_as_double(2)); |
66 ConstantPoolBuilder builder(kReachBits, kReachBits); | 90 CHECK_EQ(code->entry(), array->get_code_ptr_entry(3)); |
67 const int kRegularCount = (((kReach / (kDoubleSize + kPointerSize)) * 2) + | 91 CHECK_EQ(code, array->get_heap_ptr_entry(4)); |
68 ((kPointerSize < kDoubleSize) ? 1 : 0)); | 92 CHECK_EQ(*object, array->get_heap_ptr_entry(5)); |
69 ConstantPoolEntry::Type type = kPtrType; | 93 CHECK_EQ(50, array->get_int32_entry(6)); |
70 ConstantPoolEntry::Access access; | 94 } |
71 int pos = 0; | 95 |
72 intptr_t ptrValue = 0; | 96 |
73 double dblValue = 0.0; | 97 TEST(ConstantPoolExtended) { |
74 bool sharing_ok = true; | 98 LocalContext context; |
75 | 99 Isolate* isolate = CcTest::i_isolate(); |
76 CHECK(builder.IsEmpty()); | 100 Factory* factory = isolate->factory(); |
77 while (builder.NextAccess(type) == kRegAccess) { | 101 v8::HandleScope scope(context->GetIsolate()); |
78 if (type == kPtrType) { | 102 |
79 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | 103 // Check construction. |
80 type = kDblType; | 104 ConstantPoolArray::NumberOfEntries small(1, 2, 3, 4); |
| 105 ConstantPoolArray::NumberOfEntries extended(5, 6, 7, 8); |
| 106 Handle<ConstantPoolArray> array = |
| 107 factory->NewExtendedConstantPoolArray(small, extended); |
| 108 |
| 109 // Check small section. |
| 110 int small_counts[] = { 1, 2, 3, 4 }; |
| 111 int small_first_idx[] = { 0, 1, 3, 6 }; |
| 112 int small_last_idx[] = { 0, 2, 5, 9 }; |
| 113 for (int i = 0; i < 4; i++) { |
| 114 CHECK_EQ(small_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
| 115 CHECK_EQ(small_first_idx[i], array->first_index(kTypes[i], kSmall)); |
| 116 CHECK_EQ(small_last_idx[i], array->last_index(kTypes[i], kSmall)); |
| 117 } |
| 118 |
| 119 // Check extended layout. |
| 120 CHECK(array->is_extended_layout()); |
| 121 int extended_counts[] = { 5, 6, 7, 8 }; |
| 122 int extended_first_idx[] = { 10, 15, 21, 28 }; |
| 123 int extended_last_idx[] = { 14, 20, 27, 35 }; |
| 124 for (int i = 0; i < 4; i++) { |
| 125 CHECK_EQ(extended_counts[i], |
| 126 array->number_of_entries(kTypes[i], kExtended)); |
| 127 CHECK_EQ(extended_first_idx[i], array->first_index(kTypes[i], kExtended)); |
| 128 CHECK_EQ(extended_last_idx[i], array->last_index(kTypes[i], kExtended)); |
| 129 } |
| 130 |
| 131 // Check small and large section's don't overlap. |
| 132 int64_t small_section_int64 = V8_2PART_UINT64_C(0x56781234, DEF09ABC); |
| 133 Code* small_section_code_ptr = DummyCode(&context); |
| 134 Handle<Object> small_section_heap_ptr = |
| 135 factory->NewHeapNumber(4.0, IMMUTABLE, TENURED); |
| 136 int32_t small_section_int32 = 0xab12cd45; |
| 137 |
| 138 int64_t extended_section_int64 = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
| 139 Code* extended_section_code_ptr = DummyCode(&context); |
| 140 Handle<Object> extended_section_heap_ptr = |
| 141 factory->NewHeapNumber(5.0, IMMUTABLE, TENURED); |
| 142 int32_t extended_section_int32 = 0xef67ab89; |
| 143 |
| 144 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
| 145 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
| 146 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
| 147 array->set(i, small_section_int64); |
| 148 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
| 149 array->set(i, small_section_code_ptr->entry()); |
| 150 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
| 151 array->set(i, *small_section_heap_ptr); |
81 } else { | 152 } else { |
82 access = builder.AddEntry(pos++, dblValue); | 153 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kSmall)); |
83 dblValue += 0.5; | 154 array->set(i, small_section_int32); |
84 type = kPtrType; | |
85 } | 155 } |
86 CHECK_EQ(access, kRegAccess); | 156 } |
87 } | 157 for (int i = array->first_index(ConstantPoolArray::INT64, kExtended); |
88 CHECK(!builder.IsEmpty()); | 158 i <= array->last_index(ConstantPoolArray::INT32, kExtended); i++) { |
89 CHECK_EQ(pos, kRegularCount); | 159 if (i <= array->last_index(ConstantPoolArray::INT64, kExtended)) { |
90 | 160 array->set(i, extended_section_int64); |
91 access = builder.AddEntry(pos++, ptrValue, sharing_ok); | 161 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
92 CHECK_EQ(access, kOvflAccess); | 162 array->set(i, extended_section_code_ptr->entry()); |
93 access = builder.AddEntry(pos, dblValue); | 163 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
94 CHECK_EQ(access, kOvflAccess); | 164 array->set(i, *extended_section_heap_ptr); |
95 } | |
96 | |
97 | |
98 TEST(ConstantPoolMixedReach) { | |
99 const int ptrReachBits = kReachBits + 2; | |
100 const int ptrReach = 1 << ptrReachBits; | |
101 const int dblReachBits = kReachBits; | |
102 const int dblReach = kReach; | |
103 const int dblRegularCount = | |
104 Min(dblReach / kDoubleSize, ptrReach / (kDoubleSize + kPointerSize)); | |
105 const int ptrRegularCount = | |
106 ((ptrReach - (dblRegularCount * (kDoubleSize + kPointerSize))) / | |
107 kPointerSize) + | |
108 dblRegularCount; | |
109 ConstantPoolBuilder builder(ptrReachBits, dblReachBits); | |
110 ConstantPoolEntry::Access access; | |
111 int pos = 0; | |
112 intptr_t ptrValue = 0; | |
113 double dblValue = 0.0; | |
114 bool sharing_ok = true; | |
115 int ptrCount = 0; | |
116 int dblCount = 0; | |
117 | |
118 CHECK(builder.IsEmpty()); | |
119 while (builder.NextAccess(kDblType) == kRegAccess) { | |
120 access = builder.AddEntry(pos++, dblValue); | |
121 dblValue += 0.5; | |
122 dblCount++; | |
123 CHECK_EQ(access, kRegAccess); | |
124 | |
125 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | |
126 ptrCount++; | |
127 CHECK_EQ(access, kRegAccess); | |
128 } | |
129 CHECK(!builder.IsEmpty()); | |
130 CHECK_EQ(dblCount, dblRegularCount); | |
131 | |
132 while (ptrCount < ptrRegularCount) { | |
133 access = builder.AddEntry(pos++, dblValue); | |
134 dblValue += 0.5; | |
135 CHECK_EQ(access, kOvflAccess); | |
136 | |
137 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | |
138 ptrCount++; | |
139 CHECK_EQ(access, kRegAccess); | |
140 } | |
141 CHECK_EQ(builder.NextAccess(kPtrType), kOvflAccess); | |
142 | |
143 access = builder.AddEntry(pos++, ptrValue, sharing_ok); | |
144 CHECK_EQ(access, kOvflAccess); | |
145 access = builder.AddEntry(pos, dblValue); | |
146 CHECK_EQ(access, kOvflAccess); | |
147 } | |
148 | |
149 | |
150 TEST(ConstantPoolSharing) { | |
151 ConstantPoolBuilder builder(kReachBits, kReachBits); | |
152 const int kRegularCount = (((kReach / (kDoubleSize + kPointerSize)) * 2) + | |
153 ((kPointerSize < kDoubleSize) ? 1 : 0)); | |
154 ConstantPoolEntry::Access access; | |
155 | |
156 CHECK(builder.IsEmpty()); | |
157 | |
158 ConstantPoolEntry::Type type = kPtrType; | |
159 int pos = 0; | |
160 intptr_t ptrValue = 0; | |
161 double dblValue = 0.0; | |
162 bool sharing_ok = true; | |
163 while (builder.NextAccess(type) == kRegAccess) { | |
164 if (type == kPtrType) { | |
165 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | |
166 type = kDblType; | |
167 } else { | 165 } else { |
168 access = builder.AddEntry(pos++, dblValue); | 166 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
169 dblValue += 0.5; | 167 array->set(i, extended_section_int32); |
170 type = kPtrType; | |
171 } | 168 } |
172 CHECK_EQ(access, kRegAccess); | 169 } |
173 } | 170 |
174 CHECK(!builder.IsEmpty()); | 171 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
175 CHECK_EQ(pos, kRegularCount); | 172 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
176 | 173 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
177 type = kPtrType; | 174 CHECK_EQ(small_section_int64, array->get_int64_entry(i)); |
178 ptrValue = 0; | 175 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
179 dblValue = 0.0; | 176 CHECK_EQ(small_section_code_ptr->entry(), array->get_code_ptr_entry(i)); |
180 while (pos < kRegularCount * 2) { | 177 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
181 if (type == kPtrType) { | 178 CHECK_EQ(*small_section_heap_ptr, array->get_heap_ptr_entry(i)); |
182 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | |
183 type = kDblType; | |
184 } else { | 179 } else { |
185 access = builder.AddEntry(pos++, dblValue); | 180 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kSmall)); |
186 dblValue += 0.5; | 181 CHECK_EQ(small_section_int32, array->get_int32_entry(i)); |
187 type = kPtrType; | |
188 } | 182 } |
189 CHECK_EQ(access, kRegAccess); | 183 } |
190 } | 184 for (int i = array->first_index(ConstantPoolArray::INT64, kExtended); |
191 | 185 i <= array->last_index(ConstantPoolArray::INT32, kExtended); i++) { |
192 access = builder.AddEntry(pos++, ptrValue, sharing_ok); | 186 if (i <= array->last_index(ConstantPoolArray::INT64, kExtended)) { |
193 CHECK_EQ(access, kOvflAccess); | 187 CHECK_EQ(extended_section_int64, array->get_int64_entry(i)); |
194 access = builder.AddEntry(pos, dblValue); | 188 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
195 CHECK_EQ(access, kOvflAccess); | 189 CHECK_EQ(extended_section_code_ptr->entry(), |
196 } | 190 array->get_code_ptr_entry(i)); |
197 | 191 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
198 | 192 CHECK_EQ(*extended_section_heap_ptr, array->get_heap_ptr_entry(i)); |
199 TEST(ConstantPoolNoSharing) { | |
200 ConstantPoolBuilder builder(kReachBits, kReachBits); | |
201 const int kRegularCount = (((kReach / (kDoubleSize + kPointerSize)) * 2) + | |
202 ((kPointerSize < kDoubleSize) ? 1 : 0)); | |
203 ConstantPoolEntry::Access access; | |
204 | |
205 CHECK(builder.IsEmpty()); | |
206 | |
207 ConstantPoolEntry::Type type = kPtrType; | |
208 int pos = 0; | |
209 intptr_t ptrValue = 0; | |
210 double dblValue = 0.0; | |
211 bool sharing_ok = false; | |
212 while (builder.NextAccess(type) == kRegAccess) { | |
213 if (type == kPtrType) { | |
214 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | |
215 type = kDblType; | |
216 } else { | 193 } else { |
217 access = builder.AddEntry(pos++, dblValue); | 194 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
218 dblValue += 0.5; | 195 CHECK_EQ(extended_section_int32, array->get_int32_entry(i)); |
219 type = kPtrType; | |
220 } | 196 } |
221 CHECK_EQ(access, kRegAccess); | 197 } |
222 } | 198 } |
223 CHECK(!builder.IsEmpty()); | 199 |
224 CHECK_EQ(pos, kRegularCount); | 200 |
225 | 201 static void CheckIterator(Handle<ConstantPoolArray> array, |
226 type = kPtrType; | 202 ConstantPoolArray::Type type, |
227 ptrValue = 0; | 203 int expected_indexes[], |
228 dblValue = 0.0; | 204 int count) { |
229 sharing_ok = true; | 205 int i = 0; |
230 while (pos < kRegularCount * 2) { | 206 ConstantPoolArray::Iterator iter(*array, type); |
231 if (type == kPtrType) { | 207 while (!iter.is_finished()) { |
232 access = builder.AddEntry(pos++, ptrValue++, sharing_ok); | 208 CHECK_EQ(expected_indexes[i++], iter.next_index()); |
233 type = kDblType; | 209 } |
234 CHECK_EQ(access, kOvflAccess); | 210 CHECK_EQ(count, i); |
235 } else { | 211 } |
236 access = builder.AddEntry(pos++, dblValue); | 212 |
237 dblValue += 0.5; | 213 |
238 type = kPtrType; | 214 TEST(ConstantPoolIteratorSmall) { |
239 CHECK_EQ(access, kRegAccess); | 215 LocalContext context; |
240 } | 216 Isolate* isolate = CcTest::i_isolate(); |
241 } | 217 Factory* factory = isolate->factory(); |
242 | 218 v8::HandleScope scope(context->GetIsolate()); |
243 access = builder.AddEntry(pos++, ptrValue, sharing_ok); | 219 |
244 CHECK_EQ(access, kOvflAccess); | 220 ConstantPoolArray::NumberOfEntries small(1, 5, 2, 0); |
245 access = builder.AddEntry(pos, dblValue); | 221 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
246 CHECK_EQ(access, kOvflAccess); | 222 |
247 } | 223 int expected_int64_indexs[] = { 0 }; |
| 224 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 1); |
| 225 int expected_code_indexs[] = { 1, 2, 3, 4, 5 }; |
| 226 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 5); |
| 227 int expected_heap_indexs[] = { 6, 7 }; |
| 228 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 2); |
| 229 int expected_int32_indexs[1]; |
| 230 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 0); |
| 231 } |
| 232 |
| 233 |
| 234 TEST(ConstantPoolIteratorExtended) { |
| 235 LocalContext context; |
| 236 Isolate* isolate = CcTest::i_isolate(); |
| 237 Factory* factory = isolate->factory(); |
| 238 v8::HandleScope scope(context->GetIsolate()); |
| 239 |
| 240 ConstantPoolArray::NumberOfEntries small(1, 0, 0, 4); |
| 241 ConstantPoolArray::NumberOfEntries extended(5, 0, 3, 0); |
| 242 Handle<ConstantPoolArray> array = |
| 243 factory->NewExtendedConstantPoolArray(small, extended); |
| 244 |
| 245 int expected_int64_indexs[] = { 0, 5, 6, 7, 8, 9 }; |
| 246 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 6); |
| 247 int expected_code_indexs[1]; |
| 248 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 0); |
| 249 int expected_heap_indexs[] = { 10, 11, 12 }; |
| 250 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 3); |
| 251 int expected_int32_indexs[] = { 1, 2, 3, 4 }; |
| 252 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 4); |
| 253 } |
| 254 |
| 255 |
| 256 TEST(ConstantPoolPreciseGC) { |
| 257 LocalContext context; |
| 258 Isolate* isolate = CcTest::i_isolate(); |
| 259 Heap* heap = isolate->heap(); |
| 260 Factory* factory = isolate->factory(); |
| 261 v8::HandleScope scope(context->GetIsolate()); |
| 262 |
| 263 ConstantPoolArray::NumberOfEntries small(1, 0, 0, 1); |
| 264 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
| 265 |
| 266 // Check that the store buffer knows which entries are pointers and which are |
| 267 // not. To do this, make non-pointer entries which look like new space |
| 268 // pointers but are actually invalid and ensure the GC doesn't try to move |
| 269 // them. |
| 270 Handle<HeapObject> object = factory->NewHeapNumber(4.0); |
| 271 Object* raw_ptr = *object; |
| 272 // If interpreted as a pointer, this should be right inside the heap number |
| 273 // which will cause a crash when trying to lookup the 'map' pointer. |
| 274 intptr_t invalid_ptr = reinterpret_cast<intptr_t>(raw_ptr) + kInt32Size; |
| 275 int32_t invalid_ptr_int32 = static_cast<int32_t>(invalid_ptr); |
| 276 int64_t invalid_ptr_int64 = static_cast<int64_t>(invalid_ptr); |
| 277 array->set(0, invalid_ptr_int64); |
| 278 array->set(1, invalid_ptr_int32); |
| 279 |
| 280 // Ensure we perform a scan on scavenge for the constant pool's page. |
| 281 MemoryChunk::FromAddress(array->address())->set_scan_on_scavenge(true); |
| 282 heap->CollectGarbage(NEW_SPACE); |
| 283 |
| 284 // Check the object was moved by GC. |
| 285 CHECK_NE(*object, raw_ptr); |
| 286 |
| 287 // Check the non-pointer entries weren't changed. |
| 288 CHECK_EQ(invalid_ptr_int64, array->get_int64_entry(0)); |
| 289 CHECK_EQ(invalid_ptr_int32, array->get_int32_entry(1)); |
| 290 } |
| 291 |
| 292 |
| 293 TEST(ConstantPoolCompacting) { |
| 294 if (i::FLAG_never_compact) return; |
| 295 i::FLAG_always_compact = true; |
| 296 LocalContext context; |
| 297 Isolate* isolate = CcTest::i_isolate(); |
| 298 Heap* heap = isolate->heap(); |
| 299 Factory* factory = isolate->factory(); |
| 300 v8::HandleScope scope(context->GetIsolate()); |
| 301 |
| 302 ConstantPoolArray::NumberOfEntries small(0, 0, 1, 0); |
| 303 ConstantPoolArray::NumberOfEntries extended(0, 0, 1, 0); |
| 304 Handle<ConstantPoolArray> array = |
| 305 factory->NewExtendedConstantPoolArray(small, extended); |
| 306 |
| 307 // Start a second old-space page so that the heap pointer added to the |
| 308 // constant pool array ends up on the an evacuation candidate page. |
| 309 Page* first_page = heap->old_space()->anchor()->next_page(); |
| 310 { |
| 311 HandleScope scope(isolate); |
| 312 int dummy_array_size = Page::kMaxRegularHeapObjectSize - 92 * KB; |
| 313 Handle<HeapObject> temp = |
| 314 factory->NewFixedDoubleArray(dummy_array_size / kDoubleSize, TENURED); |
| 315 CHECK(heap->InOldSpace(temp->address())); |
| 316 Handle<HeapObject> heap_ptr = |
| 317 factory->NewHeapNumber(5.0, IMMUTABLE, TENURED); |
| 318 CHECK(heap->InOldSpace(heap_ptr->address())); |
| 319 CHECK(!first_page->Contains(heap_ptr->address())); |
| 320 array->set(0, *heap_ptr); |
| 321 array->set(1, *heap_ptr); |
| 322 } |
| 323 |
| 324 // Check heap pointers are correctly updated on GC. |
| 325 Object* old_ptr = array->get_heap_ptr_entry(0); |
| 326 Handle<Object> object(old_ptr, isolate); |
| 327 CHECK_EQ(old_ptr, *object); |
| 328 CHECK_EQ(old_ptr, array->get_heap_ptr_entry(1)); |
| 329 |
| 330 // Force compacting garbage collection. |
| 331 CHECK(FLAG_always_compact); |
| 332 heap->CollectAllGarbage(); |
| 333 |
| 334 CHECK_NE(old_ptr, *object); |
| 335 CHECK_EQ(*object, array->get_heap_ptr_entry(0)); |
| 336 CHECK_EQ(*object, array->get_heap_ptr_entry(1)); |
| 337 } |
OLD | NEW |