/
marksweep_gc_test.cc
136 lines (98 loc) · 3.03 KB
/
marksweep_gc_test.cc
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
#include "mycpp/runtime.h"
#include "vendor/greatest.h"
TEST string_collection_test() {
Str *test_str = StrFromC("foo");
{
// NOTE(Jesse): This causes a crash when this gets compiled against the
// cheney collector w/ GC_EVERY_ALLOC. I did verify it doesn't crash with
// the marksweep allocator but didn't want to figure out how to tell the
// build system to not compile these tests against the cheney collector
//
/* ASSERT(are_equal(test_str, StrFromC("foo"))); */
StackRoots _roots({&test_str});
ASSERT(are_equal(test_str, StrFromC("foo")));
gHeap.Collect();
ASSERT(are_equal(test_str, StrFromC("foo")));
}
// NOTE(Jesse): Technically UB. If the collector hits between when the roots
// go out of scope in the above block we'll get a UAF here. ASAN should
// detect this but we currently have no way of programatically verifying that
// ASAN detects bugs. AFAIK asan is not 100% reliable, so maybe that's a
// path fraught with peril anyhow.
//
/* ASSERT(are_equal(test_str, StrFromC("foo"))); */
gHeap.Collect();
// NOTE(Jesse): ASAN detects UAF here when I tested by toggling this on
//
// ASSERT(are_equal(test_str, StrFromC("foo")));
PASS();
}
TEST list_collection_test() {
{
Str *test_str0 = nullptr;
Str *test_str1 = nullptr;
List<Str *> *test_list = nullptr;
StackRoots _roots({&test_str0, &test_str1, &test_list});
test_str0 = StrFromC("foo_0");
test_str1 = StrFromC("foo_1");
test_list = NewList<Str *>();
test_list->append(test_str0);
test_list->append(test_str1);
// Verify the list looks as we expected
{
ASSERT(are_equal(test_list->index_(0), test_str0));
ASSERT(are_equal(test_list->index_(1), test_str1));
ASSERT_EQ(test_list->index_(0), test_str0);
ASSERT_EQ(test_list->index_(1), test_str1);
ASSERT_EQ(2, len(test_list));
}
gHeap.Collect();
{
ASSERT(are_equal(test_list->index_(0), test_str0));
ASSERT(are_equal(test_list->index_(1), test_str1));
ASSERT_EQ(test_list->index_(0), test_str0);
ASSERT_EQ(test_list->index_(1), test_str1);
}
test_list->pop();
ASSERT_EQ(1, len(test_list));
}
gHeap.Collect();
PASS();
}
class Node : Obj {
public:
Node();
Node* next_;
};
constexpr uint16_t maskof_Node() {
return maskbit(offsetof(Node, next_));
}
Node::Node() :
Obj(Tag::FixedSize, maskof_Node(), sizeof(Node)),
next_(nullptr) {
}
TEST cycle_collection_test() {
// Dict<Str*, int>* d = NewDict<Str*, int>();
Node* n1 = nullptr;
Node* n2 = nullptr;
StackRoots _roots({&n1, &n2});
n1 = Alloc<Node>();
n2 = Alloc<Node>();
gHeap.Collect();
n1->next_ = n2;
n2->next_ = n1;
gHeap.Collect();
PASS();
}
GREATEST_MAIN_DEFS();
int main(int argc, char **argv) {
gHeap.Init(MiB(64));
PRINT_GC_MODE_STRING();
GREATEST_MAIN_BEGIN();
RUN_TEST(string_collection_test);
RUN_TEST(list_collection_test);
RUN_TEST(cycle_collection_test);
gHeap.Collect();
GREATEST_MAIN_END(); /* display results */
return 0;
}