-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGarbageCollector.cpp
More file actions
343 lines (288 loc) · 10.4 KB
/
Copy pathGarbageCollector.cpp
File metadata and controls
343 lines (288 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "GarbageCollector.hpp"
#include "interpreter/Interpreter.hpp"
#include "interpreter/InterpreterSession.hpp"
#include "memory/Heap.hpp"
#include "runtime/PyCode.hpp"
#include "runtime/PyDict.hpp"
#include "runtime/PyFrame.hpp"
#include "runtime/PyModule.hpp"
#include "runtime/PyObject.hpp"
#include "runtime/PyType.hpp"
#include "vm/VM.hpp"
#include <csetjmp>
#include <unordered_set>
using namespace py;
MarkSweepGC::MarkSweepGC() : GarbageCollector() { set_frequency(10'000); }
namespace {
template<class To, class From>
__attribute__((no_sanitize_address))
typename std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From>
&& std::is_trivially_copyable_v<To>,
To>
bit_cast_without_sanitizer(const From &src) noexcept
{
static_assert(std::is_trivially_constructible_v<To>,
"This implementation additionally requires destination type to be trivially constructible");
To dst;
std::memcpy(&dst, &src, sizeof(To));
return dst;
}
void add_root(GarbageCollected *obj_header, std::stack<Cell *> &roots)
{
auto *cell = bit_cast<Cell *>(bit_cast<uint8_t *>(obj_header) + sizeof(GarbageCollected));
ASSERT(!obj_header->black());
if (obj_header->grey()) { return; }
ASSERT(obj_header->white());
if (cell->is_pyobject()) {
auto *obj = static_cast<PyObject *>(cell);
spdlog::debug("adding root {}@{}", obj->type()->name(), (void *)obj);
}
obj_header->mark(GarbageCollected::Color::GREY);
roots.push(cell);
}
__attribute__((no_sanitize_address)) std::stack<Cell *> collect_roots_on_the_stack(const Heap &heap,
uint8_t *stack_bottom)
{
std::stack<Cell *> roots;
// Spill all callee-saved registers into `jump_buffer`. setjmp captures
// them as part of saving the calling environment.
std::jmp_buf jump_buffer;
setjmp(jump_buffer);
auto scan_range = [&](uint8_t *begin, uint8_t *end) {
for (uint8_t *p = begin; p < end; p += sizeof(uintptr_t)) {
uint8_t *address =
bit_cast_without_sanitizer<uint8_t *>(*bit_cast_without_sanitizer<uintptr_t *>(p))
- sizeof(GarbageCollected);
spdlog::trace("checking address {}, pointer address={}", (void *)address, (void *)p);
if (heap.slab().has_address(address)) {
spdlog::trace("valid address {}", (void *)address);
auto *obj_header = bit_cast<GarbageCollected *>(address);
add_root(obj_header, roots);
}
}
};
// jump_buffer is a local variable, so it sits below the current frame
// pointer on stacks that grow downward. The frame-pointer-to-stack-bottom
// scan below therefore skips it, which would lose any GC root whose only
// live reference is in a callee-saved register at this point. Scan the
// buffer's bytes explicitly to recover those spilled values.
auto *jb_begin = bit_cast_without_sanitizer<uint8_t *>(&jump_buffer);
scan_range(jb_begin, jb_begin + sizeof(jump_buffer));
// Traverse the stack from the current frame pointer up to the recorded
// stack bottom; this covers everything in our callers' frames.
uint8_t *rsp = bit_cast_without_sanitizer<uint8_t *>(__builtin_frame_address(0));
scan_range(rsp, stack_bottom);
spdlog::debug("Done collecting roots from the stack, found {} roots", roots.size());
return roots;
}
bool is_static_memory(uint8_t *cell_start, const Heap &heap)
{
return bit_cast<uintptr_t>(cell_start) >= bit_cast<uintptr_t>(heap.static_memory())
&& bit_cast<uintptr_t>(cell_start)
< bit_cast<uintptr_t>(heap.static_memory() + heap.static_memory_size());
}
}// namespace
void GarbageCollector::remove_weakref(Heap &heap, uint8_t *obj) const
{
heap.m_weakrefs.erase(obj);
}
std::stack<Cell *> MarkSweepGC::collect_roots(const Heap &heap) const
{
if (!m_stack_bottom) { m_stack_bottom = bit_cast<uint8_t *>(heap.start_sp()); }
auto roots = collect_roots_on_the_stack(heap, m_stack_bottom);
spdlog::trace("adding objects in VM stack to roots");
for (const auto &s : VirtualMachine::the().stack_objects()) {
for (const auto &val : s) {
if (std::holds_alternative<PyObject *>(*val)) {
auto *obj = std::get<PyObject *>(*val);
if (obj) {
if (!is_static_memory(bit_cast<uint8_t *>(obj), heap)) {
auto *obj_header = bit_cast<GarbageCollected *>(
bit_cast<uint8_t *>(obj) - sizeof(GarbageCollected));
add_root(obj_header, roots);
}
}
}
}
}
if (VirtualMachine::the().has_interpreter()) {
auto &interpreter = VirtualMachine::the().interpreter();
struct AddRoot : Cell::Visitor
{
const Heap &heap_;
std::stack<Cell *> &roots_;
AddRoot(const Heap &heap, std::stack<Cell *> &roots_) : heap_(heap), roots_(roots_) {}
void visit(Cell &cell)
{
auto *obj = static_cast<PyObject *>(&cell);
if (obj) {
if (!is_static_memory(bit_cast<uint8_t *>(obj), heap_)) {
auto *obj_header = bit_cast<GarbageCollected *>(
bit_cast<uint8_t *>(obj) - sizeof(GarbageCollected));
add_root(obj_header, roots_);
}
}
}
} visitor{ heap, roots };
interpreter.visit_graph(visitor);
}
return roots;
}
struct MarkGCVisitor : Cell::Visitor
{
Heap &m_heap;
std::stack<Cell *> &m_to_visit;
// Collects the 1-hop neighbours of a given cell. visit_graph(visitor) on
// the root invokes Visitor::visit() for each thing the root references
// (which, by convention, may include the root itself for leaf cells); the
// `m_collecting` gate isolates those nested calls so only direct
// neighbours land in the vector and we do not recurse further.
struct NeighbourVisitor : Cell::Visitor
{
std::vector<Cell *> m_neighbours;
bool m_collecting{ false };
void collect_neighbours_of(Cell &root)
{
m_neighbours.clear();
m_collecting = true;
root.visit_graph(*this);
m_collecting = false;
}
void visit(Cell &cell) override
{
if (m_collecting) { m_neighbours.push_back(&cell); }
}
};
MarkGCVisitor(Heap &heap, std::stack<Cell *> &to_visit) : m_heap(heap), m_to_visit(to_visit) {}
void visit(Cell &cell)
{
uint8_t *cell_start = bit_cast<uint8_t *>(&cell);
if (!is_static_memory(cell_start, m_heap)) {
NeighbourVisitor nv{};
nv.collect_neighbours_of(cell);
auto &neighbours = nv.m_neighbours;
spdlog::trace("node: {}", static_cast<void *>(&cell));
for (auto *neighbour : neighbours) {
if (is_static_memory(bit_cast<uint8_t *>(neighbour), m_heap)) { continue; }
auto *obj_header = bit_cast<GarbageCollected *>(
bit_cast<uint8_t *>(neighbour) - sizeof(GarbageCollected));
// already visited
if (obj_header->black()) {
spdlog::trace("already visited @{}", static_cast<void *>(neighbour));
continue;
}
// already on the 'to be visited' stack
if (obj_header->grey()) {
spdlog::trace(
"already added @{} to the visited stack", static_cast<void *>(neighbour));
continue;
}
if (neighbour->is_pyobject()) {
auto *obj = static_cast<PyObject *>(neighbour);
spdlog::trace("Adding PyObject to be visited stack {}@{}",
obj->type_prototype().__name__,
(void *)obj);
}
obj_header->mark(GarbageCollected::Color::GREY);
m_to_visit.push(neighbour);
}
}
}
};
void MarkSweepGC::mark_all_cell_unreachable(Heap &heap) const
{
// NOTE: this site intentionally does NOT use Slab::for_each_block. Doing
// so produces a stack layout (lambda captures across inlining) that
// keeps a Block pointer alive in a slot the next collect_roots scan
// reads as a heap root, regressing
// TestHeap.GarbageCollectorDeallocatesGCPointersWhenStackFrameIsPopped.
// The conservative-GC fragility this exposes is the underlying issue;
// until it is replaced by a precise RootSet, keep the explicit array
// here so the test stays green. The other sweep/has_address sites are
// fine because they run after the scan, not before.
std::array blocks = {
std::reference_wrapper{ heap.slab().block_16() },
std::reference_wrapper{ heap.slab().block_32() },
std::reference_wrapper{ heap.slab().block_64() },
std::reference_wrapper{ heap.slab().block_128() },
std::reference_wrapper{ heap.slab().block_256() },
std::reference_wrapper{ heap.slab().block_512() },
std::reference_wrapper{ heap.slab().block_1024() },
std::reference_wrapper{ heap.slab().block_2048() },
};
for (const auto &block : blocks) {
for (auto &chunk : block.get()->chunks()) {
chunk.for_each_cell([](uint8_t *memory) {
auto *header = static_cast<GarbageCollected *>(static_cast<void *>(memory));
header->mark(GarbageCollected::Color::WHITE);
});
}
}
}
void MarkSweepGC::mark_all_live_objects(Heap &heap, std::stack<Cell *> &&roots) const
{
auto mark_visitor = std::make_unique<MarkGCVisitor>(heap, roots);
// mark all live objects
while (!roots.empty()) {
Cell *root = roots.top();
roots.pop();
if (is_static_memory(bit_cast<uint8_t *>(root), heap)) { continue; }
spdlog::trace("Visiting root {}", (void *)root);
auto *obj_header =
bit_cast<GarbageCollected *>(bit_cast<uint8_t *>(root) - sizeof(GarbageCollected));
ASSERT(obj_header->grey());
obj_header->mark(GarbageCollected::Color::BLACK);
mark_visitor->visit(*root);
}
spdlog::debug("Done marking all live objects");
}
void MarkSweepGC::sweep(Heap &heap) const
{
spdlog::trace("MarkSweepGC::sweep start");
heap.slab().for_each_block([this, &heap](Block &block) {
for (auto &chunk : block.chunks()) {
chunk.for_each_cell_alive([this, &chunk, &heap](uint8_t *memory) {
auto *header = bit_cast<GarbageCollected *>(memory);
if (header->white()) {
auto *cell = bit_cast<Cell *>(memory + sizeof(GarbageCollected));
if (cell->is_pyobject()) {
auto *obj = static_cast<PyObject *>(cell);
spdlog::debug("Deallocating {}@{}", obj->type()->name(), (void *)obj);
}
spdlog::debug("Calling destructor of object at {}", (void *)cell);
this->remove_weakref(heap, bit_cast<uint8_t *>(cell));
cell->~Cell();
chunk.deallocate(memory);
new (header) GarbageCollected();
}
});
}
});
spdlog::trace("MarkSweepGC::sweep done");
}
void MarkSweepGC::mark_and_sweep(Heap &heap)
{
mark_all_cell_unreachable(heap);
auto roots = collect_roots(heap);
mark_all_live_objects(heap, std::move(roots));
sweep(heap);
m_iterations_since_last_sweep = 0;
}
void MarkSweepGC::run(Heap &heap)
{
if (m_pause) { return; }
if (++m_iterations_since_last_sweep < m_frequency) { return; }
mark_and_sweep(heap);
}
void MarkSweepGC::force_run(Heap &heap) { mark_and_sweep(heap); }
void MarkSweepGC::resume()
{
ASSERT(!is_active());
m_pause = false;
}
void MarkSweepGC::pause()
{
ASSERT(is_active());
m_pause = true;
}
bool MarkSweepGC::is_active() const { return !m_pause; }