Skip to content

Commit 3765c63

Browse files
legendecasruyadorno
authored andcommitted
src: extract common context embedder tag checks
Extract common context embedder tag checks to ContextEmbedderTag so that verifying if a context is a node context doesn't need to access private members of node::Environment. PR-URL: #44258 Refs: #44179 Refs: #44252 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent e8441a2 commit 3765c63

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

src/env-inl.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,7 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
177177
}
178178

179179
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180-
if (UNLIKELY(context.IsEmpty())) {
181-
return nullptr;
182-
}
183-
if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184-
ContextEmbedderIndex::kContextTag)) {
185-
return nullptr;
186-
}
187-
if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188-
ContextEmbedderIndex::kContextTag) !=
189-
Environment::kNodeContextTagPtr)) {
180+
if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
190181
return nullptr;
191182
}
192183
return static_cast<Environment*>(

src/env.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ using v8::WeakCallbackInfo;
6161
using v8::WeakCallbackType;
6262
using worker::Worker;
6363

64-
int const Environment::kNodeContextTag = 0x6e6f64;
65-
void* const Environment::kNodeContextTagPtr = const_cast<void*>(
66-
static_cast<const void*>(&Environment::kNodeContextTag));
64+
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
65+
void* const ContextEmbedderTag::kNodeContextTagPtr = const_cast<void*>(
66+
static_cast<const void*>(&ContextEmbedderTag::kNodeContextTag));
6767

6868
void AsyncHooks::SetJSPromiseHooks(Local<Function> init,
6969
Local<Function> before,
@@ -508,11 +508,9 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
508508

509509
void Environment::AssignToContext(Local<v8::Context> context,
510510
const ContextInfo& info) {
511+
ContextEmbedderTag::TagNodeContext(context);
511512
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
512513
this);
513-
// Used by Environment::GetCurrent to know that we are on a node context.
514-
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kContextTag,
515-
Environment::kNodeContextTagPtr);
516514
// Used to retrieve bindings
517515
context->SetAlignedPointerInEmbedderData(
518516
ContextEmbedderIndex::kBindingListIndex, &(this->bindings_));

src/env.h

-3
Original file line numberDiff line numberDiff line change
@@ -1540,9 +1540,6 @@ class Environment : public MemoryRetainer {
15401540
uint64_t thread_id_;
15411541
std::unordered_set<worker::Worker*> sub_worker_contexts_;
15421542

1543-
static void* const kNodeContextTagPtr;
1544-
static int const kNodeContextTag;
1545-
15461543
#if HAVE_INSPECTOR
15471544
std::unique_ptr<inspector::Agent> inspector_agent_;
15481545
bool is_in_inspector_console_call_ = false;

src/node_context_data.h

+47-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include "util.h"
7+
#include "v8.h"
8+
69
namespace node {
710

811
// Pick an index that's hopefully out of the way when we're embedded inside
@@ -21,26 +24,62 @@ namespace node {
2124
#define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34
2225
#endif
2326

24-
#ifndef NODE_CONTEXT_TAG
25-
#define NODE_CONTEXT_TAG 35
26-
#endif
27-
2827
#ifndef NODE_BINDING_LIST
29-
#define NODE_BINDING_LIST_INDEX 36
28+
#define NODE_BINDING_LIST_INDEX 35
3029
#endif
3130

3231
#ifndef NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
33-
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 37
32+
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36
33+
#endif
34+
35+
// NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
36+
// check on the number of embedder data fields can assure the presence of all
37+
// embedder indexes.
38+
#ifndef NODE_CONTEXT_TAG
39+
#define NODE_CONTEXT_TAG 37
3440
#endif
3541

3642
enum ContextEmbedderIndex {
3743
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
3844
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
3945
kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
40-
kContextTag = NODE_CONTEXT_TAG,
4146
kBindingListIndex = NODE_BINDING_LIST_INDEX,
4247
kAllowCodeGenerationFromStrings =
43-
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
48+
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
49+
kContextTag = NODE_CONTEXT_TAG,
50+
};
51+
52+
class ContextEmbedderTag {
53+
public:
54+
static inline void TagNodeContext(v8::Local<v8::Context> context) {
55+
// Used by ContextEmbedderTag::IsNodeContext to know that we are on a node
56+
// context.
57+
context->SetAlignedPointerInEmbedderData(
58+
ContextEmbedderIndex::kContextTag,
59+
ContextEmbedderTag::kNodeContextTagPtr);
60+
}
61+
62+
static inline bool IsNodeContext(v8::Local<v8::Context> context) {
63+
if (UNLIKELY(context.IsEmpty())) {
64+
return false;
65+
}
66+
if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
67+
ContextEmbedderIndex::kContextTag)) {
68+
return false;
69+
}
70+
if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
71+
ContextEmbedderIndex::kContextTag) !=
72+
ContextEmbedderTag::kNodeContextTagPtr)) {
73+
return false;
74+
}
75+
return true;
76+
}
77+
78+
private:
79+
static void* const kNodeContextTagPtr;
80+
static int const kNodeContextTag;
81+
82+
ContextEmbedderTag() = delete;
4483
};
4584

4685
} // namespace node

0 commit comments

Comments
 (0)