forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCMarkWeakTest.cpp
More file actions
80 lines (68 loc) · 2.28 KB
/
Copy pathGCMarkWeakTest.cpp
File metadata and controls
80 lines (68 loc) · 2.28 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TestHelpers.h"
#include "gtest/gtest.h"
#include "hermes/VM/AllocResult.h"
#include "hermes/VM/DummyObject.h"
#include "hermes/VM/GC.h"
#include "hermes/VM/GCPointer-inline.h"
#include <vector>
using namespace hermes::vm;
/// Testing weak marking support in whichever GC implementation the tests are
/// configured with.
///
/// Test uses GC::countUsedWeakRefs which doesn't exist in opt mode
#ifndef NDEBUG
namespace {
// Hades doesn't call markWeak the same number of times as other GCs.
#if !defined(HERMESVM_GC_HADES) && !defined(HERMESVM_GC_RUNTIME)
using testhelpers::DummyObject;
static DummyObject *createWithMarkWeakCount(GC *gc, int *numMarkWeakCalls) {
auto *obj = DummyObject::create(gc);
obj->markWeakCallback = std::make_unique<DummyObject::MarkWeakCallback>(
[numMarkWeakCalls](GCCell *, WeakRefAcceptor &) mutable {
(*numMarkWeakCalls)++;
});
return obj;
}
TEST(GCMarkWeakTest, MarkWeak) {
constexpr int checkHeapOn =
#ifdef HERMES_SLOW_DEBUG
// In slow debug modes, there are calls to check that weak refs are valid
// before and after a collection.
1
#else
0
#endif
;
int numMarkWeakCalls = 0;
auto runtime = DummyRuntime::create(kTestGCConfigSmall);
DummyRuntime &rt = *runtime;
auto &gc = rt.getHeap();
// Probably zero, but we only care about the increase/decrease.
const int initUsedWeak = gc.countUsedWeakRefs();
{
GCScope scope{&rt};
auto t = rt.makeHandle(createWithMarkWeakCount(&gc, &numMarkWeakCalls));
rt.collect();
WeakRefLock lk{gc.weakRefMutex()};
ASSERT_TRUE(t->weak.isValid());
EXPECT_EQ(*t, getNoHandle(t->weak, &gc));
// Exactly one call to _markWeakImpl
EXPECT_EQ(1 + 2 * checkHeapOn, numMarkWeakCalls);
EXPECT_EQ(initUsedWeak + 1, gc.countUsedWeakRefs());
}
rt.collect();
// The weak ref is live at the beginning of the collection, but not by the
// end, so the call in updateReferences isn't run, nor the second
// checkHeapWellFormed.
EXPECT_EQ(1 + 3 * checkHeapOn, numMarkWeakCalls);
EXPECT_EQ(initUsedWeak, gc.countUsedWeakRefs());
}
#endif
} // namespace
#endif