summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/heap/heap_thread_test.cc
blob: b1f86ce0cee918f63f9af01022798c2f9ef8852f (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
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
// Copyright 2018 The Chromium 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 "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/heap_test_utilities.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"

namespace blink {
namespace heap_thread_test {

static Mutex& ActiveThreadMutex() {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, active_thread_mutex, ());
  return active_thread_mutex;
}

static ThreadCondition& ActiveThreadCondition() {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadCondition, active_thread_condition,
                                  (ActiveThreadMutex()));
  return active_thread_condition;
}

enum ActiveThreadState {
  kNoThreadActive,
  kMainThreadActive,
  kWorkerThreadActive,
};

static ActiveThreadState& ActiveThread() {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(ActiveThreadState, active_thread,
                                  (kNoThreadActive));
  return active_thread;
}

static void WakeMainThread() {
  ActiveThread() = kMainThreadActive;
  ActiveThreadCondition().Signal();
}

static void WakeWorkerThread() {
  ActiveThread() = kWorkerThreadActive;
  ActiveThreadCondition().Signal();
}

static void ParkMainThread() {
  while (ActiveThread() != kMainThreadActive) {
    ActiveThreadCondition().Wait();
  }
}

static void ParkWorkerThread() {
  while (ActiveThread() != kWorkerThreadActive) {
    ActiveThreadCondition().Wait();
  }
}

class Object : public GarbageCollected<Object> {
 public:
  Object() {}
  ~Object() {}
  void Trace(blink::Visitor* visitor) {}
};

class AlternatingThreadTester {
 public:
  void Test() {
    MutexLocker locker(ActiveThreadMutex());
    ActiveThread() = kMainThreadActive;

    std::unique_ptr<Thread> worker_thread = Platform::Current()->CreateThread(
        ThreadCreationParams(WebThreadType::kTestThread)
            .SetThreadNameForTest("Test Worker Thread"));
    PostCrossThreadTask(
        *worker_thread->GetTaskRunner(), FROM_HERE,
        CrossThreadBind(&AlternatingThreadTester::StartWorkerThread,
                        CrossThreadUnretained(this)));

    MainThreadMain();
  }

  void SwitchToWorkerThread() {
    WakeWorkerThread();
    ParkMainThread();
  }

  void SwitchToMainThread() {
    WakeMainThread();
    ParkWorkerThread();
  }

 protected:
  // Override with code you want to execute on the main thread.
  virtual void MainThreadMain() = 0;
  // Override with code you want to execute on the worker thread. At the end,
  // the ThreadState is detached and we switch back to the main thread
  // automatically.
  virtual void WorkerThreadMain() = 0;

 private:
  void StartWorkerThread() {
    ThreadState::AttachCurrentThread();

    MutexLocker locker(ActiveThreadMutex());

    WorkerThreadMain();

    ThreadState::DetachCurrentThread();
    WakeMainThread();
  }
};

class MemberSameThreadCheckTester : public AlternatingThreadTester {
 private:
  void MainThreadMain() override { SwitchToWorkerThread(); }

  void WorkerThreadMain() override {
    // Setting an object created on the worker thread to a Member allocated on
    // the main thread is not allowed.
    object_ = MakeGarbageCollected<Object>();
  }

  Member<Object> object_;
};

#if DCHECK_IS_ON()
// TODO(keishi) This test is flaky on mac_chromium_rel_ng bot.
// crbug.com/709069
#if !defined(OS_MACOSX)
TEST(HeapDeathTest, MemberSameThreadCheck) {
  EXPECT_DEATH(MemberSameThreadCheckTester().Test(), "");
}
#endif
#endif

class PersistentSameThreadCheckTester : public AlternatingThreadTester {
 private:
  void MainThreadMain() override { SwitchToWorkerThread(); }

  void WorkerThreadMain() override {
    // Setting an object created on the worker thread to a Persistent allocated
    // on the main thread is not allowed.
    object_ = MakeGarbageCollected<Object>();
  }

  Persistent<Object> object_;
};

#if DCHECK_IS_ON()
// TODO(keishi) This test is flaky on mac_chromium_rel_ng bot.
// crbug.com/709069
#if !defined(OS_MACOSX)
TEST(HeapDeathTest, PersistentSameThreadCheck) {
  EXPECT_DEATH(PersistentSameThreadCheckTester().Test(), "");
}
#endif
#endif

class MarkingSameThreadCheckTester : public AlternatingThreadTester {
 private:
  class MainThreadObject : public GarbageCollectedFinalized<MainThreadObject> {
   public:
    void Trace(blink::Visitor* visitor) { visitor->Trace(set_); }
    void AddToSet(Object* object) { set_.insert(42, object); }

   private:
    HeapHashMap<int, Member<Object>> set_;
  };

  void MainThreadMain() override {
    main_thread_object_ = MakeGarbageCollected<MainThreadObject>();

    SwitchToWorkerThread();

    // This will try to mark MainThreadObject when it tries to mark Object
    // it should crash.
    PreciselyCollectGarbage();
  }

  void WorkerThreadMain() override {
    // Adding a reference to an object created on the worker thread to a
    // HeapHashMap created on the main thread is not allowed.
    main_thread_object_->AddToSet(MakeGarbageCollected<Object>());
  }

  CrossThreadPersistent<MainThreadObject> main_thread_object_;
};

#if DCHECK_IS_ON()
// TODO(keishi) This test is flaky on mac_chromium_rel_ng bot.
// crbug.com/709069
#if !defined(OS_MACOSX)
TEST(HeapDeathTest, MarkingSameThreadCheck) {
  // This will crash during marking, at the DCHECK in Visitor::markHeader() or
  // earlier.
  EXPECT_DEATH(MarkingSameThreadCheckTester().Test(), "");
}
#endif
#endif

class DestructorLockingObject
    : public GarbageCollectedFinalized<DestructorLockingObject> {
 public:
  static DestructorLockingObject* Create() {
    return MakeGarbageCollected<DestructorLockingObject>();
  }

  DestructorLockingObject() = default;
  virtual ~DestructorLockingObject() { ++destructor_calls_; }

  static int destructor_calls_;
  void Trace(blink::Visitor* visitor) {}
};

int DestructorLockingObject::destructor_calls_ = 0;

class CrossThreadWeakPersistentTester : public AlternatingThreadTester {
 private:
  void MainThreadMain() override {
    // Create an object in the worker thread, have a CrossThreadWeakPersistent
    // pointing to it on the main thread, run a GC in the worker thread, and see
    // if the CrossThreadWeakPersistent is cleared.

    DestructorLockingObject::destructor_calls_ = 0;

    // Step 1: Initiate a worker thread, and wait for |Object| to get allocated
    // on the worker thread.
    SwitchToWorkerThread();

    // Step 3: Set up a CrossThreadWeakPersistent.
    ASSERT_TRUE(object_);
    EXPECT_EQ(0, DestructorLockingObject::destructor_calls_);

    // Pretend we have no pointers on stack during the step 4.
    SwitchToWorkerThread();

    // Step 5: Make sure the weak persistent is cleared.
    EXPECT_FALSE(object_.Get());
    EXPECT_EQ(1, DestructorLockingObject::destructor_calls_);

    SwitchToWorkerThread();
  }

  void WorkerThreadMain() override {
    // Step 2: Create an object and store the pointer.
    object_ = DestructorLockingObject::Create();
    SwitchToMainThread();

    // Step 4: Run a GC.
    ThreadState::Current()->CollectGarbage(
        BlinkGC::kNoHeapPointersOnStack, BlinkGC::kAtomicMarking,
        BlinkGC::kEagerSweeping, BlinkGC::GCReason::kForcedGC);
    SwitchToMainThread();
  }

  CrossThreadWeakPersistent<DestructorLockingObject> object_;
};

TEST(HeapThreadTest, CrossThreadWeakPersistent) {
  CrossThreadWeakPersistentTester().Test();
}

}  // namespace heap_thread_test
}  // namespace blink