|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include "tests/test_suites/GcTestSuite.hpp" |
| 4 | + |
| 5 | +#include <memory> |
| 6 | +#include <stdexcept> |
| 7 | +#include <unordered_set> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#include "lib/execution_tree/ExecutionResult.hpp" |
| 11 | +#include "lib/execution_tree/FunctionRepository.hpp" |
| 12 | +#include "lib/execution_tree/PassedExecutionData.hpp" |
| 13 | +#include "lib/runtime/MemoryManager.hpp" |
| 14 | +#include "lib/runtime/ObjectDescriptor.hpp" |
| 15 | +#include "lib/runtime/ObjectRepository.hpp" |
| 16 | +#include "lib/runtime/VirtualTable.hpp" |
| 17 | +#include "lib/runtime/VirtualTableRepository.hpp" |
| 18 | +#include "lib/runtime/gc/MarkAndSweepGC.hpp" |
| 19 | +#include "lib/runtime/gc/reference_scanners/ArrayReferenceScanner.hpp" |
| 20 | +#include "lib/runtime/gc/reference_scanners/DefaultReferenceScanner.hpp" |
| 21 | + |
| 22 | +TEST_F(GcTestSuite, UnreachableObjectCollected) { |
| 23 | + auto data = MakeFreshData(); |
| 24 | + |
| 25 | + void* obj = AllocateTestObject("Simple", data); |
| 26 | + |
| 27 | + auto before = SnapshotRepo(mm_.GetRepository()); |
| 28 | + ASSERT_EQ(before.size(), 1u); |
| 29 | + ASSERT_TRUE(RepoContains(mm_.GetRepository(), obj)); |
| 30 | + |
| 31 | + CollectGarbage(data); |
| 32 | + |
| 33 | + auto after = SnapshotRepo(mm_.GetRepository()); |
| 34 | + EXPECT_EQ(after.size(), 0u); |
| 35 | + EXPECT_FALSE(RepoContains(mm_.GetRepository(), obj)); |
| 36 | +} |
| 37 | + |
| 38 | +TEST_F(GcTestSuite, RootInGlobalVariablesSurvives) { |
| 39 | + auto data = MakeFreshData(); |
| 40 | + |
| 41 | + void* obj = AllocateTestObject("Simple", data); |
| 42 | + data.memory.global_variables.emplace_back(obj); |
| 43 | + |
| 44 | + CollectGarbage(data); |
| 45 | + |
| 46 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), obj)); |
| 47 | + EXPECT_EQ(SnapshotRepo(mm_.GetRepository()).size(), 1u); |
| 48 | +} |
| 49 | + |
| 50 | +TEST_F(GcTestSuite, RootInMachineStackSurvives) { |
| 51 | + auto data = MakeFreshData(); |
| 52 | + |
| 53 | + void* obj = AllocateTestObject("Simple", data); |
| 54 | + data.memory.machine_stack.emplace(obj); |
| 55 | + |
| 56 | + CollectGarbage(data); |
| 57 | + |
| 58 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), obj)); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(GcTestSuite, TransitiveReachabilityThroughField) { |
| 62 | + auto data = MakeFreshData(); |
| 63 | + |
| 64 | + void* root = AllocateTestObject("WithRef", data); |
| 65 | + void* child = AllocateTestObject("Simple", data); |
| 66 | + |
| 67 | + SetRef(root, child); |
| 68 | + |
| 69 | + data.memory.global_variables.emplace_back(root); |
| 70 | + |
| 71 | + CollectGarbage(data); |
| 72 | + |
| 73 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), root)); |
| 74 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), child)); |
| 75 | + EXPECT_EQ(SnapshotRepo(mm_.GetRepository()).size(), 2u); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(GcTestSuite, TransitiveReachabilityThroughArray) { |
| 79 | + auto data = MakeFreshData(); |
| 80 | + |
| 81 | + void* arr = AllocateTestObject("Array", data); |
| 82 | + InitArray(arr); |
| 83 | + |
| 84 | + void* child1 = AllocateTestObject("Simple", data); |
| 85 | + void* child2 = AllocateTestObject("Simple", data); |
| 86 | + |
| 87 | + AddToArray(arr, child1); |
| 88 | + AddToArray(arr, child2); |
| 89 | + |
| 90 | + data.memory.global_variables.emplace_back(arr); |
| 91 | + |
| 92 | + CollectGarbage(data); |
| 93 | + |
| 94 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), arr)); |
| 95 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), child1)); |
| 96 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), child2)); |
| 97 | + EXPECT_EQ(SnapshotRepo(mm_.GetRepository()).size(), 3u); |
| 98 | +} |
| 99 | + |
| 100 | +TEST_F(GcTestSuite, CycleWithoutRootsCollected) { |
| 101 | + auto data = MakeFreshData(); |
| 102 | + |
| 103 | + void* objA = AllocateTestObject("WithRef", data); |
| 104 | + void* objB = AllocateTestObject("WithRef", data); |
| 105 | + |
| 106 | + SetRef(objA, objB); |
| 107 | + SetRef(objB, objA); |
| 108 | + |
| 109 | + CollectGarbage(data); |
| 110 | + |
| 111 | + EXPECT_FALSE(RepoContains(mm_.GetRepository(), objA)); |
| 112 | + EXPECT_FALSE(RepoContains(mm_.GetRepository(), objB)); |
| 113 | + EXPECT_EQ(SnapshotRepo(mm_.GetRepository()).size(), 0u); |
| 114 | +} |
| 115 | + |
| 116 | +TEST_F(GcTestSuite, CycleWithRootPreserved) { |
| 117 | + auto data = MakeFreshData(); |
| 118 | + |
| 119 | + void* objA = AllocateTestObject("WithRef", data); |
| 120 | + void* objB = AllocateTestObject("WithRef", data); |
| 121 | + |
| 122 | + SetRef(objA, objB); |
| 123 | + SetRef(objB, objA); |
| 124 | + |
| 125 | + data.memory.global_variables.emplace_back(objA); |
| 126 | + |
| 127 | + CollectGarbage(data); |
| 128 | + |
| 129 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), objA)); |
| 130 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), objB)); |
| 131 | + EXPECT_EQ(SnapshotRepo(mm_.GetRepository()).size(), 2u); |
| 132 | +} |
| 133 | + |
| 134 | +TEST_F(GcTestSuite, MultipleRootsDifferentPlaces) { |
| 135 | + auto data = MakeFreshData(); |
| 136 | + |
| 137 | + void* global = AllocateTestObject("Simple", data); |
| 138 | + void* stack = AllocateTestObject("Simple", data); |
| 139 | + void* local = AllocateTestObject("Simple", data); |
| 140 | + |
| 141 | + data.memory.global_variables.emplace_back(global); |
| 142 | + data.memory.machine_stack.emplace(stack); |
| 143 | + |
| 144 | + ovum::vm::runtime::StackFrame frame; |
| 145 | + frame.local_variables.emplace_back(local); |
| 146 | + data.memory.stack_frames.push(std::move(frame)); |
| 147 | + |
| 148 | + CollectGarbage(data); |
| 149 | + |
| 150 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), global)); |
| 151 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), stack)); |
| 152 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), local)); |
| 153 | + EXPECT_EQ(SnapshotRepo(mm_.GetRepository()).size(), 3u); |
| 154 | +} |
| 155 | + |
| 156 | +TEST_F(GcTestSuite, SmallThresholdFrequentAllocations) { |
| 157 | + auto data = MakeFreshData(3); |
| 158 | + |
| 159 | + std::vector<void*> objects; |
| 160 | + std::vector<void*> should_survive; |
| 161 | + |
| 162 | + const int k_iterations = 15; |
| 163 | + |
| 164 | + for (int i = 0; i < k_iterations; ++i) { |
| 165 | + void* obj = AllocateTestObject("Simple", data); |
| 166 | + objects.push_back(obj); |
| 167 | + |
| 168 | + if (i % 3 == 0) { |
| 169 | + data.memory.global_variables.emplace_back(obj); |
| 170 | + should_survive.push_back(obj); |
| 171 | + } |
| 172 | + |
| 173 | + auto gc_res = data.memory_manager.CollectGarbageIfRequired(data); |
| 174 | + ASSERT_TRUE(gc_res.has_value()); |
| 175 | + } |
| 176 | + |
| 177 | + CollectGarbage(data); |
| 178 | + |
| 179 | + auto snapshot = SnapshotRepo(mm_.GetRepository()); |
| 180 | + EXPECT_GE(snapshot.size(), should_survive.size()); |
| 181 | + |
| 182 | + for (void* obj : should_survive) { |
| 183 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), obj)); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +TEST_F(GcTestSuite, NullReferencesNotCrashing) { |
| 188 | + auto data = MakeFreshData(); |
| 189 | + |
| 190 | + void* root = AllocateTestObject("WithRef", data); |
| 191 | + SetRef(root, nullptr); |
| 192 | + |
| 193 | + data.memory.global_variables.emplace_back(root); |
| 194 | + |
| 195 | + ASSERT_NO_FATAL_FAILURE(CollectGarbage(data)); |
| 196 | + |
| 197 | + EXPECT_TRUE(RepoContains(mm_.GetRepository(), root)); |
| 198 | +} |
0 commit comments