Coverage Report

Created: 2023-02-22 06:51

/src/hermes/lib/VM/CheckHeapWellFormedAcceptor.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#include "hermes/VM/CheckHeapWellFormedAcceptor.h"
9
10
#include "hermes/VM/GC.h"
11
#include "hermes/VM/SmallHermesValue-inline.h"
12
13
namespace hermes {
14
namespace vm {
15
16
#ifdef HERMES_SLOW_DEBUG
17
18
CheckHeapWellFormedAcceptor::CheckHeapWellFormedAcceptor(GCBase &gc)
19
    : RootAndSlotAcceptorDefault(gc.getPointerBase()),
20
      WeakAcceptorDefault(gc.getPointerBase()),
21
745
      gc(gc) {}
22
23
10.7M
void CheckHeapWellFormedAcceptor::accept(GCCell *&ptr) {
24
10.7M
  accept(static_cast<const GCCell *>(ptr));
25
10.7M
}
26
27
11.8M
void CheckHeapWellFormedAcceptor::accept(const GCCell *ptr) {
28
11.8M
  assert(
29
11.8M
      (!ptr || gc.validPointer(ptr)) &&
30
11.8M
      "A pointer is pointing outside of the valid region");
31
11.8M
}
32
33
451k
void CheckHeapWellFormedAcceptor::acceptWeak(GCCell *&ptr) {
34
  // A weak pointer has the same well-formed-ness checks as a normal pointer.
35
451k
  accept(ptr);
36
451k
}
37
38
340k
void CheckHeapWellFormedAcceptor::acceptHV(HermesValue &hv) {
39
340k
  assert(!hv.isInvalid() && "HermesValue with InvalidTag encountered by GC.");
40
340k
  if (hv.isPointer()) {
41
309k
    GCCell *cell = static_cast<GCCell *>(hv.getPointer());
42
309k
    accept(cell);
43
309k
  } else if (hv.isSymbol()) {
44
95
    acceptSym(hv.getSymbol());
45
95
  }
46
340k
}
47
48
6.34M
void CheckHeapWellFormedAcceptor::acceptSHV(SmallHermesValue &hv) {
49
6.34M
  if (hv.isPointer()) {
50
4.75M
    GCCell *cell = static_cast<GCCell *>(hv.getPointer(pointerBase_));
51
4.75M
    accept(cell);
52
4.75M
  } else if (hv.isSymbol()) {
53
7.47k
    acceptSym(hv.getSymbol());
54
7.47k
  }
55
6.34M
}
56
57
1.05M
void CheckHeapWellFormedAcceptor::acceptSym(SymbolID sym) {
58
1.05M
  if (!sym.isValid()) {
59
749
    return;
60
749
  }
61
1.05M
  assert(
62
1.05M
      gc.getCallbacks().isSymbolLive(sym) &&
63
1.05M
      "Symbol is marked but is not live");
64
  // Check that the string used by this symbol is valid.
65
0
  accept(
66
1.05M
      static_cast<const GCCell *>(gc.getCallbacks().getStringForSymbol(sym)));
67
1.05M
}
68
69
423k
void CheckHeapWellFormedAcceptor::accept(WeakRefBase &wr) {
70
  // Cannot check if the weak ref is valid, since it is allowed to mark an
71
  // empty weak ref.
72
423k
  const WeakRefSlot *slot = wr.unsafeGetSlot();
73
  // If the weak value is a pointer, check that it's within the valid region.
74
423k
  if (slot->state() != WeakSlotState::Free && slot->hasValue()) {
75
422k
    GCCell *cell = slot->getNoBarrierUnsafe(gc.getPointerBase());
76
422k
    accept(cell);
77
422k
  }
78
423k
}
79
80
#endif
81
82
} // namespace vm
83
} // namespace hermes