summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/heap/cppgc/weak-container-unittest.cc
blob: 0aee3287ef00a1ab784880a593e01340af081ab2 (plain)
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
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "include/cppgc/allocation.h"
#include "src/heap/cppgc/marker.h"
#include "src/heap/cppgc/marking-visitor.h"
#include "src/heap/cppgc/stats-collector.h"
#include "test/unittests/heap/cppgc/tests.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cppgc {
namespace internal {

namespace {
class WeakContainerTest : public testing::TestWithHeap {
 public:
  using Config = Marker::MarkingConfig;

  void StartMarking() {
    Config config = {Config::CollectionType::kMajor,
                     Config::StackState::kNoHeapPointers,
                     Config::MarkingType::kIncremental};
    GetMarkerRef() = MarkerFactory::CreateAndStartMarking<Marker>(
        Heap::From(GetHeap())->AsBase(), GetPlatformHandle().get(), config);
  }

  void FinishMarking(Config::StackState stack_state) {
    GetMarkerRef()->FinishMarking(stack_state);
    GetMarkerRef().reset();
    Heap::From(GetHeap())->stats_collector()->NotifySweepingCompleted();
  }
};

class TraceableGCed : public GarbageCollected<TraceableGCed> {
 public:
  void Trace(cppgc::Visitor*) const { n_trace_calls++; }
  static size_t n_trace_calls;
};
size_t TraceableGCed::n_trace_calls = 0u;

class NonTraceableGCed : public GarbageCollected<NonTraceableGCed> {
 public:
  void Trace(cppgc::Visitor*) const { n_trace_calls++; }
  static size_t n_trace_calls;
};
size_t NonTraceableGCed::n_trace_calls = 0u;

void EmptyWeakCallback(const LivenessBroker&, const void*) {}

template <typename T>
V8_NOINLINE T access(volatile const T& t) {
  return t;
}

}  // namespace

}  // namespace internal

template <>
struct TraceTrait<internal::TraceableGCed>
    : public internal::TraceTraitBase<internal::TraceableGCed> {
  static TraceDescriptor GetWeakTraceDescriptor(const void* self) {
    return {self, Trace};
  }
};

template <>
struct TraceTrait<internal::NonTraceableGCed>
    : public internal::TraceTraitBase<internal::NonTraceableGCed> {
  static TraceDescriptor GetWeakTraceDescriptor(const void* self) {
    return {self, nullptr};
  }
};

namespace internal {

TEST_F(WeakContainerTest, TraceableGCedTraced) {
  TraceableGCed* obj =
      MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
  TraceableGCed::n_trace_calls = 0u;
  StartMarking();
  GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
  FinishMarking(Config::StackState::kNoHeapPointers);
  EXPECT_NE(0u, TraceableGCed::n_trace_calls);
  access(obj);
}

TEST_F(WeakContainerTest, NonTraceableGCedNotTraced) {
  NonTraceableGCed* obj =
      MakeGarbageCollected<NonTraceableGCed>(GetAllocationHandle());
  NonTraceableGCed::n_trace_calls = 0u;
  StartMarking();
  GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
  FinishMarking(Config::StackState::kNoHeapPointers);
  EXPECT_EQ(0u, NonTraceableGCed::n_trace_calls);
  access(obj);
}

TEST_F(WeakContainerTest, NonTraceableGCedNotTracedConservatively) {
  NonTraceableGCed* obj =
      MakeGarbageCollected<NonTraceableGCed>(GetAllocationHandle());
  NonTraceableGCed::n_trace_calls = 0u;
  StartMarking();
  GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
  FinishMarking(Config::StackState::kMayContainHeapPointers);
  EXPECT_NE(0u, NonTraceableGCed::n_trace_calls);
  access(obj);
}

TEST_F(WeakContainerTest, ConservativeGCTracesWeakContainer) {
  size_t trace_count_without_conservative;
  {
    TraceableGCed* obj =
        MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
    TraceableGCed::n_trace_calls = 0u;
    StartMarking();
    GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback,
                                                 nullptr);
    FinishMarking(Config::StackState::kNoHeapPointers);
    trace_count_without_conservative = TraceableGCed::n_trace_calls;
    access(obj);
  }
  {
    TraceableGCed* obj =
        MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
    TraceableGCed::n_trace_calls = 0u;
    StartMarking();
    GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback,
                                                 nullptr);
    FinishMarking(Config::StackState::kMayContainHeapPointers);
    EXPECT_LT(trace_count_without_conservative, TraceableGCed::n_trace_calls);
    access(obj);
  }
}

TEST_F(WeakContainerTest, ConservativeGCTracesWeakContainerOnce) {
  NonTraceableGCed* obj =
      MakeGarbageCollected<NonTraceableGCed>(GetAllocationHandle());
  NonTraceableGCed* copy_obj = obj;
  USE(copy_obj);
  NonTraceableGCed* another_copy_obj = obj;
  USE(another_copy_obj);
  NonTraceableGCed::n_trace_calls = 0u;
  StartMarking();
  GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
  FinishMarking(Config::StackState::kMayContainHeapPointers);
  EXPECT_EQ(1u, NonTraceableGCed::n_trace_calls);
  access(obj);
}

namespace {

struct WeakCallback {
  static void callback(const LivenessBroker&, const void* data) {
    n_callback_called++;
    obj = data;
  }
  static size_t n_callback_called;
  static const void* obj;
};
size_t WeakCallback::n_callback_called = 0u;
const void* WeakCallback::obj = nullptr;

}  // namespace

TEST_F(WeakContainerTest, WeakContainerWeakCallbackCalled) {
  TraceableGCed* obj =
      MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
  WeakCallback::n_callback_called = 0u;
  WeakCallback::obj = nullptr;
  StartMarking();
  GetMarkerRef()->Visitor().TraceWeakContainer(obj, WeakCallback::callback,
                                               obj);
  FinishMarking(Config::StackState::kMayContainHeapPointers);
  EXPECT_NE(0u, WeakCallback::n_callback_called);
  EXPECT_EQ(obj, WeakCallback::obj);
}

}  // namespace internal
}  // namespace cppgc