Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
quic: add NgTcp2CallbackScope/NgHttp3CallbackScope
  • Loading branch information
jasnell committed Apr 10, 2023
commit 2fa2cdbfbcca3e727b3463241fc6ad4527b0985d
32 changes: 32 additions & 0 deletions src/quic/bindingdata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ void BindingData::FlushPacketFreelist(
state.packet_freelist.clear();
}

NgTcp2CallbackScope::NgTcp2CallbackScope(Environment* env) : env(env) {
auto& binding = BindingData::Get(env);
CHECK(!binding.in_ngtcp2_callback_scope);
binding.in_ngtcp2_callback_scope = true;
}

NgTcp2CallbackScope::~NgTcp2CallbackScope() {
auto& binding = BindingData::Get(env);
binding.in_ngtcp2_callback_scope = false;
}

bool NgTcp2CallbackScope::in_ngtcp2_callback(Environment* env) {
auto& binding = BindingData::Get(env);
return binding.in_ngtcp2_callback_scope;
}

NgHttp3CallbackScope::NgHttp3CallbackScope(Environment* env) : env(env) {
auto& binding = BindingData::Get(env);
CHECK(!binding.in_nghttp3_callback_scope);
binding.in_nghttp3_callback_scope = true;
}

NgHttp3CallbackScope::~NgHttp3CallbackScope() {
auto& binding = BindingData::Get(env);
binding.in_nghttp3_callback_scope = false;
}

bool NgHttp3CallbackScope::in_nghttp3_callback(Environment* env) {
auto& binding = BindingData::Get(env);
return binding.in_nghttp3_callback_scope;
}

void IllegalConstructor(const FunctionCallbackInfo<Value>& args) {
THROW_ERR_ILLEGAL_CONSTRUCTOR(Environment::GetCurrent(args));
}
Expand Down
20 changes: 20 additions & 0 deletions src/quic/bindingdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ class BindingData final
static void FlushPacketFreelist(
const v8::FunctionCallbackInfo<v8::Value>& args);

bool in_ngtcp2_callback_scope = false;
bool in_nghttp3_callback_scope = false;

// The following set up various storage and accessors for common strings,
// construction templates, and callbacks stored on the BindingData. These
// are all defined in defs.h
Expand Down Expand Up @@ -183,6 +186,23 @@ class BindingData final

void IllegalConstructor(const v8::FunctionCallbackInfo<v8::Value>& args);

// The ngtcp2 and nghttp3 callbacks have certain restrictions
// that forbid re-entry. We provide the following scopes for
// use in those to help protect against it.
struct NgTcp2CallbackScope {
Environment* env;
NgTcp2CallbackScope(Environment* env);
~NgTcp2CallbackScope();
static bool in_ngtcp2_callback(Environment* env);
};

struct NgHttp3CallbackScope {
Environment* env;
NgHttp3CallbackScope(Environment* env);
~NgHttp3CallbackScope();
static bool in_nghttp3_callback(Environment* env);
};

} // namespace quic
} // namespace node

Expand Down