forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildMetadata.cpp
More file actions
76 lines (66 loc) · 2.19 KB
/
BuildMetadata.cpp
File metadata and controls
76 lines (66 loc) · 2.19 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
/*
* 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 "hermes/VM/BuildMetadata.h"
#include "hermes/VM/CellKind.h"
#include "llvh/Support/Debug.h"
#include <cstdint>
#define DEBUG_TYPE "metadata"
namespace hermes {
namespace vm {
static size_t getNumCellKinds() {
// This embeds the same value as the CellKind enum, but adds one more at the
// end to know how many values it contains.
enum CellKinds {
#define CELL_KIND(name) name,
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
numKinds,
};
return numKinds;
}
/// Creates and populates the storage for the metadata of the classes.
/// The caller takes ownership of the memory.
static const Metadata *buildStorage() {
// For each class of object, initialize its metadata
// Only run this once per class, not once per Runtime instantiation.
Metadata *storage = new Metadata[getNumCellKinds()];
size_t i = 0;
#define CELL_KIND(name) \
storage[i++] = buildMetadata(CellKind::name##Kind, name##BuildMeta);
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
assert(i == getNumCellKinds() && "Incorrect number of metadata populated");
return storage;
}
Metadata buildMetadata(CellKind kind, BuildMetadataCallback *builder) {
const GCCell *base;
#ifdef HERMES_UBSAN
// Using members on a nullptr is UB, but using a pointer to static memory is
// not.
static const int64_t buf[128]{};
base = reinterpret_cast<const GCCell *>(&buf);
#else
// Use nullptr when not building with UBSAN to ensure fast failures.
base = nullptr;
#endif
// This memory should not be read or written to
Metadata::Builder mb(base);
builder(base, mb);
Metadata meta = mb.build();
LLVM_DEBUG(
llvh::dbgs() << "Initialized metadata for cell kind " << cellKindStr(kind)
<< ": " << meta << "\n");
return meta;
}
MetadataTable getMetadataTable() {
// We intentionally leak memory here in order to avoid any static destructors
// running at exit time.
static const Metadata *storage = buildStorage();
return MetadataTable(storage, getNumCellKinds());
}
} // namespace vm
} // namespace hermes