Flutter Engine
The Flutter Engine
become_test.cc
Go to the documentation of this file.
1// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
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.
4
5#include "platform/assert.h"
6#include "platform/globals.h"
7
8#include "vm/globals.h"
9#include "vm/heap/become.h"
10#include "vm/heap/heap.h"
11#include "vm/unit_test.h"
12
13namespace dart {
14
15void TestBecomeForward(Heap::Space before_space, Heap::Space after_space) {
16 // Allocate the container in old space to test the remembered set.
17 const Array& container = Array::Handle(Array::New(1, Heap::kOld));
18
19 const String& before_obj = String::Handle(String::New("old", before_space));
20 const String& after_obj = String::Handle(String::New("new", after_space));
21 container.SetAt(0, before_obj);
22 EXPECT(before_obj.ptr() != after_obj.ptr());
23
24 Become become;
25 become.Add(before_obj, after_obj);
26 become.Forward();
27
28 EXPECT(before_obj.ptr() == after_obj.ptr());
29 EXPECT(container.At(0) == after_obj.ptr());
30
32
33 EXPECT(before_obj.ptr() == after_obj.ptr());
34 EXPECT(container.At(0) == after_obj.ptr());
35}
36
37ISOLATE_UNIT_TEST_CASE(BecomeForwardOldToOld) {
39}
40
41ISOLATE_UNIT_TEST_CASE(BecomeForwardNewToNew) {
43}
44
45ISOLATE_UNIT_TEST_CASE(BecomeForwardOldToNew) {
47}
48
49ISOLATE_UNIT_TEST_CASE(BecomeForwardNewToOld) {
51}
52
53ISOLATE_UNIT_TEST_CASE(BecomeForwardPeer) {
54 Heap* heap = IsolateGroup::Current()->heap();
55
56 const Array& before_obj = Array::Handle(Array::New(0, Heap::kOld));
57 const Array& after_obj = Array::Handle(Array::New(0, Heap::kOld));
58 EXPECT(before_obj.ptr() != after_obj.ptr());
59
60 void* peer = reinterpret_cast<void*>(42);
61 void* no_peer = reinterpret_cast<void*>(0);
62 heap->SetPeer(before_obj.ptr(), peer);
63 EXPECT_EQ(peer, heap->GetPeer(before_obj.ptr()));
64 EXPECT_EQ(no_peer, heap->GetPeer(after_obj.ptr()));
65
66 Become become;
67 become.Add(before_obj, after_obj);
68 become.Forward();
69
70 EXPECT(before_obj.ptr() == after_obj.ptr());
71 EXPECT_EQ(peer, heap->GetPeer(before_obj.ptr()));
72 EXPECT_EQ(peer, heap->GetPeer(after_obj.ptr()));
73}
74
75ISOLATE_UNIT_TEST_CASE(BecomeForwardObjectId) {
76 Heap* heap = IsolateGroup::Current()->heap();
77
78 const Array& before_obj = Array::Handle(Array::New(0, Heap::kOld));
79 const Array& after_obj = Array::Handle(Array::New(0, Heap::kOld));
80 EXPECT(before_obj.ptr() != after_obj.ptr());
81
82 intptr_t id = 42;
83 intptr_t no_id = 0;
84 heap->SetObjectId(before_obj.ptr(), id);
85 EXPECT_EQ(id, heap->GetObjectId(before_obj.ptr()));
86 EXPECT_EQ(no_id, heap->GetObjectId(after_obj.ptr()));
87
88 Become become;
89 become.Add(before_obj, after_obj);
90 become.Forward();
91
92 EXPECT(before_obj.ptr() == after_obj.ptr());
93 EXPECT_EQ(id, heap->GetObjectId(before_obj.ptr()));
94 EXPECT_EQ(id, heap->GetObjectId(after_obj.ptr()));
95}
96
97ISOLATE_UNIT_TEST_CASE(BecomeForwardMessageId) {
98 Isolate* isolate = Isolate::Current();
99 isolate->set_forward_table_new(new WeakTable());
100 isolate->set_forward_table_old(new WeakTable());
101
102 const Array& before_obj = Array::Handle(Array::New(0, Heap::kOld));
103 const Array& after_obj = Array::Handle(Array::New(0, Heap::kOld));
104 EXPECT(before_obj.ptr() != after_obj.ptr());
105
106 intptr_t id = 42;
107 intptr_t no_id = 0;
108 isolate->forward_table_old()->SetValueExclusive(before_obj.ptr(), id);
109 EXPECT_EQ(id,
110 isolate->forward_table_old()->GetValueExclusive(before_obj.ptr()));
111 EXPECT_EQ(no_id,
112 isolate->forward_table_old()->GetValueExclusive(after_obj.ptr()));
113
114 Become become;
115 become.Add(before_obj, after_obj);
116 become.Forward();
117
118 EXPECT(before_obj.ptr() == after_obj.ptr());
119 EXPECT_EQ(id,
120 isolate->forward_table_old()->GetValueExclusive(before_obj.ptr()));
121 EXPECT_EQ(id,
122 isolate->forward_table_old()->GetValueExclusive(after_obj.ptr()));
123
124 isolate->set_forward_table_new(nullptr);
125 isolate->set_forward_table_old(nullptr);
126}
127
128ISOLATE_UNIT_TEST_CASE(BecomeForwardRememberedObject) {
129 const String& new_element = String::Handle(String::New("new", Heap::kNew));
130 const String& old_element = String::Handle(String::New("old", Heap::kOld));
131 const Array& before_obj = Array::Handle(Array::New(1, Heap::kOld));
132 const Array& after_obj = Array::Handle(Array::New(1, Heap::kOld));
133 before_obj.SetAt(0, new_element);
134 after_obj.SetAt(0, old_element);
135 EXPECT(before_obj.ptr()->untag()->IsRemembered());
136 EXPECT(!after_obj.ptr()->untag()->IsRemembered());
137
138 EXPECT(before_obj.ptr() != after_obj.ptr());
139
140 Become become;
141 become.Add(before_obj, after_obj);
142 become.Forward();
143
144 EXPECT(before_obj.ptr() == after_obj.ptr());
145 EXPECT(!after_obj.ptr()->untag()->IsRemembered());
146
148
149 EXPECT(before_obj.ptr() == after_obj.ptr());
150}
151
152ISOLATE_UNIT_TEST_CASE(BecomeForwardRememberedCards) {
155 const Array& card_remembered_array = Array::Handle(Array::New(length));
156 EXPECT(card_remembered_array.ptr()->untag()->IsCardRemembered());
157 EXPECT(!card_remembered_array.ptr()->untag()->IsRemembered());
158
159 const String& old_element = String::Handle(String::New("old", Heap::kOld));
160 const String& new_element = String::Handle(String::New("new", Heap::kNew));
161 card_remembered_array.SetAt(0, old_element);
162
163 {
164 HANDLESCOPE(thread);
165 EXPECT_STREQ("old",
166 Object::Handle(card_remembered_array.At(0)).ToCString());
167 }
168
169 Become become;
170 become.Add(old_element, new_element);
171 become.Forward();
172
173 EXPECT(old_element.ptr() == new_element.ptr());
174 EXPECT(old_element.ptr()->IsNewObject());
175 EXPECT(card_remembered_array.ptr()->untag()->IsCardRemembered());
176 EXPECT(!card_remembered_array.ptr()->untag()->IsRemembered());
177
178 {
179 HANDLESCOPE(thread);
180 EXPECT_STREQ("new",
181 Object::Handle(card_remembered_array.At(0)).ToCString());
182 }
183
185
186 EXPECT(old_element.ptr() == new_element.ptr());
187 EXPECT(card_remembered_array.ptr()->untag()->IsCardRemembered());
188 EXPECT(!card_remembered_array.ptr()->untag()->IsRemembered());
189
190 {
191 HANDLESCOPE(thread);
192 EXPECT_STREQ("new",
193 Object::Handle(card_remembered_array.At(0)).ToCString());
194 }
195}
196
197} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
static ArrayPtr New(intptr_t len, Heap::Space space=Heap::kNew)
Definition: object.h:10959
static constexpr intptr_t kBytesPerElement
Definition: object.h:10923
static constexpr bool UseCardMarkingForAllocation(const intptr_t array_length)
Definition: object.h:10818
ObjectPtr At(intptr_t index) const
Definition: object.h:10875
void SetAt(intptr_t index, const Object &value) const
Definition: object.h:10880
void Add(const Object &before, const Object &after)
Definition: become.cc:231
void Forward()
Definition: become.cc:275
@ kNew
Definition: heap.h:38
@ kOld
Definition: heap.h:39
void * GetPeer(ObjectPtr raw_obj) const
Definition: heap.h:167
void SetPeer(ObjectPtr raw_obj, void *peer)
Definition: heap.h:164
intptr_t GetObjectId(ObjectPtr raw_obj) const
Definition: heap.h:197
void SetObjectId(ObjectPtr raw_obj, intptr_t object_id)
Definition: heap.h:193
Heap * heap() const
Definition: isolate.h:296
static IsolateGroup * Current()
Definition: isolate.h:539
void set_forward_table_old(WeakTable *table)
Definition: isolate.cc:2552
static Isolate * Current()
Definition: isolate.h:986
void set_forward_table_new(WeakTable *table)
Definition: isolate.cc:2548
WeakTable * forward_table_old()
Definition: isolate.h:1461
UntaggedObject * untag() const
ObjectPtr ptr() const
Definition: object.h:332
virtual const char * ToCString() const
Definition: object.h:366
static Object & Handle()
Definition: object.h:407
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition: object.cc:23698
bool IsCardRemembered() const
Definition: raw_object.h:385
bool IsRemembered() const
Definition: raw_object.h:361
void SetValueExclusive(ObjectPtr key, intptr_t val)
Definition: weak_table.cc:33
intptr_t GetValueExclusive(ObjectPtr key) const
Definition: weak_table.h:109
#define ASSERT(E)
#define HANDLESCOPE(thread)
Definition: handles.h:321
size_t length
Definition: dart_vm.cc:33
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction)
static void CollectAllGarbage(Thread *thread, JSONStream *js)
Definition: service.cc:4559
void TestBecomeForward(Heap::Space before_space, Heap::Space after_space)
Definition: become_test.cc:15
static constexpr intptr_t kNewAllocatableSize
Definition: spaces.h:54