diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 5cfae82f47b1..95ce32434a06 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -402,24 +402,33 @@ Maybe ParseGeneralReply(Environment* env, return Just(ARES_SUCCESS); } -Maybe ParseMxReply(Environment* env, - const unsigned char* buf, - int len, - Local ret, - bool need_type = false) { - HandleScope handle_scope(env->isolate()); - - auto tmpl = env->mx_record_template(); +// Returns the per-isolate dictionary template for a record type, creating and +// caching it on first use. +template +Local GetRecordTemplate( + Environment* env, + Local (Environment::*getter)() const, + void (Environment::*setter)(Local), + const std::string_view (&names)[N]) { + Local tmpl = (env->*getter)(); if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "exchange", - "priority", - "type", - }; tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_mx_record_template(tmpl); + (env->*setter)(tmpl); } + return tmpl; +} +// Parses a DNS reply and calls `fn(rr, index)` for each answer record of the +// given type, where `index` is the record's position in the answer section. +// `fn` returns false if a JS exception is pending. Returns the parse status, +// ARES_ENODATA if no answer has the requested type and `allow_empty` is false, +// or Nothing if `fn` failed. +template +Maybe ForEachAnswer(const unsigned char* buf, + int len, + ares_dns_rec_type_t type, + Fn&& fn, + bool allow_empty = false) { ares_dns_record_t* dnsrec = nullptr; int status = ares_dns_parse(buf, len, 0, &dnsrec); if (status != ARES_SUCCESS) { @@ -429,177 +438,153 @@ Maybe ParseMxReply(Environment* env, DeleteFnPtr free_me(dnsrec); const size_t rr_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); - uint32_t offset = ret->Length(); - uint32_t count = 0; - - MaybeLocal values[] = { - Undefined(env->isolate()), // exchange - Undefined(env->isolate()), // priority - Undefined(env->isolate()), // type - }; - + bool found = false; for (size_t i = 0; i < rr_count; i++) { const ares_dns_rr_t* rr = ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i); - if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_MX) continue; - - const char* exchange = ares_dns_rr_get_str(rr, ARES_RR_MX_EXCHANGE); - unsigned short priority = // NOLINT(runtime/int) - ares_dns_rr_get_u16(rr, ARES_RR_MX_PREFERENCE); - values[0] = OneByteString(env->isolate(), exchange); - values[1] = Integer::New(env->isolate(), priority); - values[2] = env->dns_mx_string(); - Local record; - if (!NewDictionaryInstance(env->context(), tmpl, values).ToLocal(&record) || - ret->Set(env->context(), offset + count++, record).IsNothing()) { - return Nothing(); - } + if (ares_dns_rr_get_type(rr) != type) continue; + found = true; + if (!fn(rr, i)) return Nothing(); } - if (count == 0) return Just(ARES_ENODATA); + if (!found && !allow_empty) return Just(ARES_ENODATA); return Just(ARES_SUCCESS); } +// Creates a record object from `tmpl` and `values` and appends it to `ret` at +// `index`. Returns false if a JS exception is pending. +template +bool AppendRecord(Environment* env, + Local tmpl, + MaybeLocal (&values)[N], + Local ret, + uint32_t index) { + Local record; + return NewDictionaryInstance(env->context(), tmpl, values).ToLocal(&record) && + ret->Set(env->context(), index, record).IsJust(); +} + +Maybe ParseMxReply(Environment* env, + const unsigned char* buf, + int len, + Local ret, + bool need_type = false) { + HandleScope handle_scope(env->isolate()); + static constexpr std::string_view names[] = { + "exchange", + "priority", + "type", + }; + auto tmpl = GetRecordTemplate(env, + &Environment::mx_record_template, + &Environment::set_mx_record_template, + names); + + const uint32_t offset = ret->Length(); + uint32_t count = 0; + return ForEachAnswer( + buf, len, ARES_REC_TYPE_MX, [&](const ares_dns_rr_t* rr, size_t) { + const char* exchange = ares_dns_rr_get_str(rr, ARES_RR_MX_EXCHANGE); + unsigned short priority = // NOLINT(runtime/int) + ares_dns_rr_get_u16(rr, ARES_RR_MX_PREFERENCE); + MaybeLocal values[] = { + OneByteString(env->isolate(), exchange), + Integer::New(env->isolate(), priority), + env->dns_mx_string(), + }; + return AppendRecord(env, tmpl, values, ret, offset + count++); + }); +} + Maybe ParseCaaReply(Environment* env, const unsigned char* buf, int len, Local ret, bool need_type = false) { HandleScope handle_scope(env->isolate()); - - auto tmpl = env->caa_record_template(); - if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "critical", - "type", - }; - tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_caa_record_template(tmpl); - } - - ares_dns_record_t* dnsrec = nullptr; - int status = ares_dns_parse(buf, len, 0, &dnsrec); - if (status != ARES_SUCCESS) { - ares_dns_record_destroy(dnsrec); - return Just(status); - } - DeleteFnPtr free_me(dnsrec); - - MaybeLocal values[] = { - Undefined(env->isolate()), // critical - Undefined(env->isolate()), // type + static constexpr std::string_view names[] = { + "critical", + "type", }; + auto tmpl = GetRecordTemplate(env, + &Environment::caa_record_template, + &Environment::set_caa_record_template, + names); - const size_t rr_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); - uint32_t offset = ret->Length(); + const uint32_t offset = ret->Length(); uint32_t count = 0; - for (size_t i = 0; i < rr_count; i++) { - const ares_dns_rr_t* rr = - ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i); - if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_CAA) continue; - - unsigned char critical = ares_dns_rr_get_u8(rr, ARES_RR_CAA_CRITICAL); - values[0] = Integer::New(env->isolate(), critical); - values[1] = env->dns_caa_string(); - Local caa_record; - if (!NewDictionaryInstance(env->context(), tmpl, values) - .ToLocal(&caa_record)) { - return Nothing(); - } + return ForEachAnswer( + buf, len, ARES_REC_TYPE_CAA, [&](const ares_dns_rr_t* rr, size_t) { + unsigned char critical = ares_dns_rr_get_u8(rr, ARES_RR_CAA_CRITICAL); + MaybeLocal values[] = { + Integer::New(env->isolate(), critical), + env->dns_caa_string(), + }; + Local caa_record; + if (!NewDictionaryInstance(env->context(), tmpl, values) + .ToLocal(&caa_record)) { + return false; + } - const char* tag = ares_dns_rr_get_str(rr, ARES_RR_CAA_TAG); - size_t value_len = 0; - const unsigned char* value = - ares_dns_rr_get_bin(rr, ARES_RR_CAA_VALUE, &value_len); - - // This additional property is not part of the template as it is - // variable based on the record. - if (caa_record - ->Set(env->context(), - OneByteString(env->isolate(), tag), - OneByteString(env->isolate(), value, value_len)) - .IsNothing()) { - return Nothing(); - } + const char* tag = ares_dns_rr_get_str(rr, ARES_RR_CAA_TAG); + size_t value_len = 0; + const unsigned char* value = + ares_dns_rr_get_bin(rr, ARES_RR_CAA_VALUE, &value_len); - if (ret->Set(env->context(), offset + count++, caa_record).IsNothing()) { - return Nothing(); - } - } - - if (count == 0) return Just(ARES_ENODATA); - return Just(ARES_SUCCESS); + // This additional property is not part of the template as it is + // variable based on the record. + return caa_record + ->Set(env->context(), + OneByteString(env->isolate(), tag), + OneByteString(env->isolate(), value, value_len)) + .IsJust() && + ret->Set(env->context(), offset + count++, caa_record).IsJust(); + }); } Maybe ParseTlsaReply(Environment* env, unsigned char* buf, int len, Local ret) { - EscapableHandleScope handle_scope(env->isolate()); - - auto tmpl = env->tlsa_record_template(); - if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "certUsage", - "selector", - "match", - "data", - }; - tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_tlsa_record_template(tmpl); - } - - ares_dns_record_t* dnsrec = nullptr; - - int status = ares_dns_parse(buf, len, 0, &dnsrec); - if (status != ARES_SUCCESS) { - ares_dns_record_destroy(dnsrec); - return Just(status); - } - - DeleteFnPtr free_me(dnsrec); - - uint32_t offset = ret->Length(); - size_t rr_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); - - MaybeLocal values[] = { - Undefined(env->isolate()), // certUsage - Undefined(env->isolate()), // selector - Undefined(env->isolate()), // match - Undefined(env->isolate()), // data + HandleScope handle_scope(env->isolate()); + static constexpr std::string_view names[] = { + "certUsage", + "selector", + "match", + "data", }; + auto tmpl = GetRecordTemplate(env, + &Environment::tlsa_record_template, + &Environment::set_tlsa_record_template, + names); - for (size_t i = 0; i < rr_count; i++) { - const ares_dns_rr_t* rr = - ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i); - - if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_TLSA) continue; - - unsigned char certusage = ares_dns_rr_get_u8(rr, ARES_RR_TLSA_CERT_USAGE); - unsigned char selector = ares_dns_rr_get_u8(rr, ARES_RR_TLSA_SELECTOR); - unsigned char match = ares_dns_rr_get_u8(rr, ARES_RR_TLSA_MATCH); - size_t data_len; - const unsigned char* data = - ares_dns_rr_get_bin(rr, ARES_RR_TLSA_DATA, &data_len); - if (!data || data_len == 0) continue; - - Local data_ab = ArrayBuffer::New(env->isolate(), data_len); - memcpy(data_ab->Data(), data, data_len); - - values[0] = Integer::NewFromUnsigned(env->isolate(), certusage); - values[1] = Integer::NewFromUnsigned(env->isolate(), selector); - values[2] = Integer::NewFromUnsigned(env->isolate(), match); - values[3] = data_ab; - - Local tlsa_rec; - if (!NewDictionaryInstance(env->context(), tmpl, values) - .ToLocal(&tlsa_rec) || - ret->Set(env->context(), offset + i, tlsa_rec).IsNothing()) { - return Nothing(); - } - } - - return Just(ARES_SUCCESS); + const uint32_t offset = ret->Length(); + return ForEachAnswer( + buf, + len, + ARES_REC_TYPE_TLSA, + [&](const ares_dns_rr_t* rr, size_t i) { + unsigned char certusage = + ares_dns_rr_get_u8(rr, ARES_RR_TLSA_CERT_USAGE); + unsigned char selector = ares_dns_rr_get_u8(rr, ARES_RR_TLSA_SELECTOR); + unsigned char match = ares_dns_rr_get_u8(rr, ARES_RR_TLSA_MATCH); + size_t data_len; + const unsigned char* data = + ares_dns_rr_get_bin(rr, ARES_RR_TLSA_DATA, &data_len); + if (!data || data_len == 0) return true; + + Local data_ab = ArrayBuffer::New(env->isolate(), data_len); + memcpy(data_ab->Data(), data, data_len); + + MaybeLocal values[] = { + Integer::NewFromUnsigned(env->isolate(), certusage), + Integer::NewFromUnsigned(env->isolate(), selector), + Integer::NewFromUnsigned(env->isolate(), match), + data_ab, + }; + return AppendRecord(env, tmpl, values, ret, offset + i); + }, + /* allow_empty */ true); } Maybe ParseTxtReply(Environment* env, @@ -608,68 +593,39 @@ Maybe ParseTxtReply(Environment* env, Local ret, bool need_type = false) { HandleScope handle_scope(env->isolate()); - - auto tmpl = env->txt_record_template(); - if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "entries", - "type", - }; - tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_txt_record_template(tmpl); - } - - ares_dns_record_t* dnsrec = nullptr; - int status = ares_dns_parse(buf, len, 0, &dnsrec); - if (status != ARES_SUCCESS) { - ares_dns_record_destroy(dnsrec); - return Just(status); - } - DeleteFnPtr free_me(dnsrec); - - const size_t rr_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); - uint32_t offset = ret->Length(); - uint32_t count = 0; - - MaybeLocal values[] = { - Undefined(env->isolate()), // entries - Undefined(env->isolate()), // type + static constexpr std::string_view names[] = { + "entries", + "type", }; + auto tmpl = GetRecordTemplate(env, + &Environment::txt_record_template, + &Environment::set_txt_record_template, + names); - for (size_t i = 0; i < rr_count; i++) { - const ares_dns_rr_t* rr = - ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i); - if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_TXT) continue; - - // Each TXT record is a chunk consisting of one or more character-strings. - LocalVector chunks(env->isolate()); - size_t str_count = ares_dns_rr_get_abin_cnt(rr, ARES_RR_TXT_DATA); - chunks.reserve(str_count); - for (size_t j = 0; j < str_count; j++) { - size_t str_len = 0; - const unsigned char* str = - ares_dns_rr_get_abin(rr, ARES_RR_TXT_DATA, j, &str_len); - chunks.push_back(OneByteString(env->isolate(), str, str_len)); - } - - Local txt_chunk = - Array::New(env->isolate(), chunks.data(), chunks.size()); - if (need_type) { - values[0] = txt_chunk; - values[1] = env->dns_txt_string(); - Local elem; - if (!NewDictionaryInstance(env->context(), tmpl, values).ToLocal(&elem) || - ret->Set(env->context(), offset + count++, elem).IsNothing()) { - return Nothing(); - } - } else if (ret->Set(env->context(), offset + count++, txt_chunk) - .IsNothing()) { - return Nothing(); - } - } + const uint32_t offset = ret->Length(); + uint32_t count = 0; + return ForEachAnswer( + buf, len, ARES_REC_TYPE_TXT, [&](const ares_dns_rr_t* rr, size_t) { + // Each TXT record is a chunk consisting of one or more + // character-strings. + LocalVector chunks(env->isolate()); + size_t str_count = ares_dns_rr_get_abin_cnt(rr, ARES_RR_TXT_DATA); + chunks.reserve(str_count); + for (size_t j = 0; j < str_count; j++) { + size_t str_len = 0; + const unsigned char* str = + ares_dns_rr_get_abin(rr, ARES_RR_TXT_DATA, j, &str_len); + chunks.push_back(OneByteString(env->isolate(), str, str_len)); + } - if (count == 0) return Just(ARES_ENODATA); - return Just(ARES_SUCCESS); + Local txt_chunk = + Array::New(env->isolate(), chunks.data(), chunks.size()); + if (need_type) { + MaybeLocal values[] = {txt_chunk, env->dns_txt_string()}; + return AppendRecord(env, tmpl, values, ret, offset + count++); + } + return ret->Set(env->context(), offset + count++, txt_chunk).IsJust(); + }); } Maybe ParseSrvReply(Environment* env, @@ -678,64 +634,35 @@ Maybe ParseSrvReply(Environment* env, Local ret, bool need_type = false) { HandleScope handle_scope(env->isolate()); - - auto tmpl = env->srv_record_template(); - if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "name", - "port", - "priority", - "weight", - "type", - }; - tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_srv_record_template(tmpl); - } - - ares_dns_record_t* dnsrec = nullptr; - int status = ares_dns_parse(buf, len, 0, &dnsrec); - if (status != ARES_SUCCESS) { - ares_dns_record_destroy(dnsrec); - return Just(status); - } - DeleteFnPtr free_me(dnsrec); - - MaybeLocal values[] = { - Undefined(env->isolate()), // name - Undefined(env->isolate()), // port - Undefined(env->isolate()), // priority - Undefined(env->isolate()), // weight - Undefined(env->isolate()), // type + static constexpr std::string_view names[] = { + "name", + "port", + "priority", + "weight", + "type", }; + auto tmpl = GetRecordTemplate(env, + &Environment::srv_record_template, + &Environment::set_srv_record_template, + names); - const size_t rr_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); - uint32_t offset = ret->Length(); + const uint32_t offset = ret->Length(); uint32_t count = 0; - for (size_t i = 0; i < rr_count; i++) { - const ares_dns_rr_t* rr = - ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i); - if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_SRV) continue; - - const char* target = ares_dns_rr_get_str(rr, ARES_RR_SRV_TARGET); - values[0] = OneByteString(env->isolate(), target); - values[1] = - Integer::New(env->isolate(), ares_dns_rr_get_u16(rr, ARES_RR_SRV_PORT)); - values[2] = Integer::New(env->isolate(), - ares_dns_rr_get_u16(rr, ARES_RR_SRV_PRIORITY)); - values[3] = Integer::New(env->isolate(), - ares_dns_rr_get_u16(rr, ARES_RR_SRV_WEIGHT)); - values[4] = env->dns_srv_string(); - - Local srv_record; - if (!NewDictionaryInstance(env->context(), tmpl, values) - .ToLocal(&srv_record) || - ret->Set(env->context(), offset + count++, srv_record).IsNothing()) { - return Nothing(); - } - } - - if (count == 0) return Just(ARES_ENODATA); - return Just(ARES_SUCCESS); + return ForEachAnswer( + buf, len, ARES_REC_TYPE_SRV, [&](const ares_dns_rr_t* rr, size_t) { + const char* target = ares_dns_rr_get_str(rr, ARES_RR_SRV_TARGET); + MaybeLocal values[] = { + OneByteString(env->isolate(), target), + Integer::New(env->isolate(), + ares_dns_rr_get_u16(rr, ARES_RR_SRV_PORT)), + Integer::New(env->isolate(), + ares_dns_rr_get_u16(rr, ARES_RR_SRV_PRIORITY)), + Integer::New(env->isolate(), + ares_dns_rr_get_u16(rr, ARES_RR_SRV_WEIGHT)), + env->dns_srv_string(), + }; + return AppendRecord(env, tmpl, values, ret, offset + count++); + }); } Maybe ParseNaptrReply(Environment* env, @@ -744,93 +671,59 @@ Maybe ParseNaptrReply(Environment* env, Local ret, bool need_type = false) { HandleScope handle_scope(env->isolate()); - - auto tmpl = env->naptr_record_template(); - if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "flags", - "service", - "regexp", - "replacement", - "order", - "preference", - "type", - }; - tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_naptr_record_template(tmpl); - } - - ares_dns_record_t* dnsrec = nullptr; - int status = ares_dns_parse(buf, len, 0, &dnsrec); - if (status != ARES_SUCCESS) { - ares_dns_record_destroy(dnsrec); - return Just(status); - } - DeleteFnPtr free_me(dnsrec); - - MaybeLocal values[] = { - Undefined(env->isolate()), // flags - Undefined(env->isolate()), // service - Undefined(env->isolate()), // regexp - Undefined(env->isolate()), // replacement - Undefined(env->isolate()), // order - Undefined(env->isolate()), // preference - Undefined(env->isolate()), // type + static constexpr std::string_view names[] = { + "flags", + "service", + "regexp", + "replacement", + "order", + "preference", + "type", }; + auto tmpl = GetRecordTemplate(env, + &Environment::naptr_record_template, + &Environment::set_naptr_record_template, + names); - const size_t rr_count = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); - uint32_t offset = ret->Length(); + const uint32_t offset = ret->Length(); uint32_t count = 0; - for (size_t i = 0; i < rr_count; i++) { - const ares_dns_rr_t* rr = - ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i); - if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_NAPTR) continue; - - values[0] = OneByteString(env->isolate(), - ares_dns_rr_get_str(rr, ARES_RR_NAPTR_FLAGS)); - values[1] = OneByteString(env->isolate(), - ares_dns_rr_get_str(rr, ARES_RR_NAPTR_SERVICES)); - values[2] = OneByteString(env->isolate(), - ares_dns_rr_get_str(rr, ARES_RR_NAPTR_REGEXP)); - values[3] = OneByteString( - env->isolate(), ares_dns_rr_get_str(rr, ARES_RR_NAPTR_REPLACEMENT)); - values[4] = Integer::New(env->isolate(), - ares_dns_rr_get_u16(rr, ARES_RR_NAPTR_ORDER)); - values[5] = Integer::New(env->isolate(), - ares_dns_rr_get_u16(rr, ARES_RR_NAPTR_PREFERENCE)); - if (need_type) { - values[6] = env->dns_naptr_string(); - } - - Local naptr_record; - if (!NewDictionaryInstance(env->context(), tmpl, values) - .ToLocal(&naptr_record) || - ret->Set(env->context(), offset + count++, naptr_record).IsNothing()) { - return Nothing(); - } - } - - if (count == 0) return Just(ARES_ENODATA); - return Just(ARES_SUCCESS); + return ForEachAnswer( + buf, len, ARES_REC_TYPE_NAPTR, [&](const ares_dns_rr_t* rr, size_t) { + MaybeLocal values[] = { + OneByteString(env->isolate(), + ares_dns_rr_get_str(rr, ARES_RR_NAPTR_FLAGS)), + OneByteString(env->isolate(), + ares_dns_rr_get_str(rr, ARES_RR_NAPTR_SERVICES)), + OneByteString(env->isolate(), + ares_dns_rr_get_str(rr, ARES_RR_NAPTR_REGEXP)), + OneByteString(env->isolate(), + ares_dns_rr_get_str(rr, ARES_RR_NAPTR_REPLACEMENT)), + Integer::New(env->isolate(), + ares_dns_rr_get_u16(rr, ARES_RR_NAPTR_ORDER)), + Integer::New(env->isolate(), + ares_dns_rr_get_u16(rr, ARES_RR_NAPTR_PREFERENCE)), + need_type ? MaybeLocal(env->dns_naptr_string()) + : MaybeLocal(Undefined(env->isolate())), + }; + return AppendRecord(env, tmpl, values, ret, offset + count++); + }); } Local getSoaRecordTemplate(Environment* env) { - auto tmpl = env->soa_record_template(); - if (tmpl.IsEmpty()) { - static constexpr std::string_view names[] = { - "nsname", - "hostmaster", - "serial", - "refresh", - "retry", - "expire", - "minttl", - "type", - }; - tmpl = DictionaryTemplate::New(env->isolate(), names); - env->set_soa_record_template(tmpl); - } - return tmpl; + static constexpr std::string_view names[] = { + "nsname", + "hostmaster", + "serial", + "refresh", + "retry", + "expire", + "minttl", + "type", + }; + return GetRecordTemplate(env, + &Environment::soa_record_template, + &Environment::set_soa_record_template, + names); } Maybe ParseSoaReply(Environment* env, diff --git a/src/encoding_binding.cc b/src/encoding_binding.cc index 8a445088b54a..5b8f8f0bcdc2 100644 --- a/src/encoding_binding.cc +++ b/src/encoding_binding.cc @@ -20,7 +20,6 @@ using v8::BackingStoreInitializationMode; using v8::BackingStoreOnFailureMode; using v8::Context; using v8::FunctionCallbackInfo; -using v8::HandleScope; using v8::Isolate; using v8::Local; using v8::Object; @@ -67,10 +66,7 @@ bool BindingData::PrepareForSerialization(Local context, } InternalFieldInfoBase* BindingData::Serialize(int index) { - DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = internal_field_info_; - internal_field_info_ = nullptr; - return info; + return TakeInternalFieldInfo(index, &internal_field_info_); } // The following code is adapted from Cloudflare workers. @@ -185,13 +181,8 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - // Recreate the buffer in the constructor. - InternalFieldInfo* casted_info = static_cast(info); - BindingData* binding = - realm->AddBindingData(holder, casted_info); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData( + context, holder, static_cast(info)); } void BindingData::EncodeInto(const FunctionCallbackInfo& args) { diff --git a/src/glob/node_glob.cc b/src/glob/node_glob.cc index 9337775c79da..2109ae246189 100644 --- a/src/glob/node_glob.cc +++ b/src/glob/node_glob.cc @@ -178,10 +178,7 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - Realm* realm = Realm::GetCurrent(context); - HandleScope scope(realm->isolate()); - BindingData* binding = realm->AddBindingData(holder, info); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData(context, holder, info); } void BindingData::MemoryInfo(MemoryTracker* tracker) const { diff --git a/src/node_blob.cc b/src/node_blob.cc index ab4267fb81f0..bdc10ab2ad79 100644 --- a/src/node_blob.cc +++ b/src/node_blob.cc @@ -584,10 +584,7 @@ void BlobBindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - BlobBindingData* binding = realm->AddBindingData(holder); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData(context, holder); } bool BlobBindingData::PrepareForSerialization(Local context, @@ -600,9 +597,7 @@ bool BlobBindingData::PrepareForSerialization(Local context, InternalFieldInfoBase* BlobBindingData::Serialize(int index) { DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = - InternalFieldInfoBase::New(type()); - return info; + return InternalFieldInfoBase::New(type()); } void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) { diff --git a/src/node_diagnostics_channel.cc b/src/node_diagnostics_channel.cc index 2cc3309542ff..e7afa2557cd0 100644 --- a/src/node_diagnostics_channel.cc +++ b/src/node_diagnostics_channel.cc @@ -125,10 +125,7 @@ bool BindingData::PrepareForSerialization(Local context, } InternalFieldInfoBase* BindingData::Serialize(int index) { - DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = internal_field_info_; - internal_field_info_ = nullptr; - return info; + return TakeInternalFieldInfo(index, &internal_field_info_); } void BindingData::Deserialize(Local context, @@ -136,11 +133,8 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - BindingData* binding = realm->AddBindingData( - holder, static_cast(info)); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData( + context, holder, static_cast(info)); } void BindingData::SetChannelStatusCallback(uint32_t index, diff --git a/src/node_file.cc b/src/node_file.cc index b96771217b83..b6fbf26c7343 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -103,6 +103,18 @@ constexpr char kPathSeparator = '/'; const char* const kPathSeparator = "\\/"; #endif +// Returns the request wrap of an asynchronous fs call, which bindings receive +// as the argument at `index`, or nullptr for a synchronous call (when fewer +// arguments were passed). +static FSReqBase* GetAsyncReqWrap(const FunctionCallbackInfo& args, + int index, + bool use_bigint = false) { + if (args.Length() <= index) return nullptr; + FSReqBase* req_wrap_async = GetReqWrap(args, index, use_bigint); + CHECK_NOT_NULL(req_wrap_async); + return req_wrap_async; +} + inline int64_t GetOffset(Local value) { return IsSafeJsInt(value) ? value.As()->Value() : -1; } @@ -155,6 +167,20 @@ static const char* get_fs_func_name_by_type(uv_fs_type req_type) { #define GET_TRACE_ENABLED \ (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \ TRACING_CATEGORY_NODE2(fs, sync)) != 0) +// Checks a permission for an fs binding. A denial is reported through +// `req_wrap_async` for an asynchronous call, or thrown for a synchronous call +// (when `req_wrap_async` is nullptr). +#define FS_THROW_IF_INSUFFICIENT_PERMISSIONS( \ + env, req_wrap_async, perm, resource) \ + do { \ + if ((req_wrap_async) != nullptr) { \ + ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( \ + env, req_wrap_async, perm, resource); \ + } else { \ + THROW_IF_INSUFFICIENT_PERMISSIONS(env, perm, resource); \ + } \ + } while (0) + #define FS_SYNC_TRACE_BEGIN(syscall, ...) \ if (GET_TRACE_ENABLED) \ TRACE_EVENT_BEGIN( \ @@ -1040,9 +1066,8 @@ void Access(const FunctionCallbackInfo& args) { CHECK_NOT_NULL(*path); ToNamespacedPath(env, &path); - if (argc > 2) { // access(path, mode, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + if (req_wrap_async != nullptr) { // access(path, mode, req) ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( env, req_wrap_async, @@ -1081,9 +1106,8 @@ void Close(const FunctionCallbackInfo& args) { } env->RemoveUnmanagedFd(fd); - if (argc > 1) { // close(fd, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 1); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1); + if (req_wrap_async != nullptr) { // close(fd, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_CLOSE, req_wrap_async) AsyncCall( env, req_wrap_async, args, "close", UTF8, AfterNoArgs, uv_fs_close, fd); @@ -1173,16 +1197,16 @@ static void Stat(const FunctionCallbackInfo& args) { ToNamespacedPath(env, &path); bool use_bigint = args[1]->IsTrue(); - if (!args[2]->IsUndefined()) { // stat(path, use_bigint, req, - // do_not_throw_if_no_entry) + FSReqBase* req_wrap_async = + !args[2]->IsUndefined() ? GetAsyncReqWrap(args, 2, use_bigint) : nullptr; + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + path.ToStringView()); + if (req_wrap_async != nullptr) { // stat(path, use_bigint, req, + // do_not_throw_if_no_entry) bool do_not_throw_if_no_entry = args[3]->IsFalse(); - FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - path.ToStringView()); FS_ASYNC_TRACE_BEGIN1( UV_FS_STAT, req_wrap_async, "path", TRACE_STR_COPY(*path)) if (do_not_throw_if_no_entry) { @@ -1205,8 +1229,6 @@ static void Stat(const FunctionCallbackInfo& args) { *path); } } else { // stat(path, use_bigint, undefined, do_not_throw_if_no_entry) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, path.ToStringView()); bool do_not_throw_if_no_entry = args[3]->IsFalse(); FSReqWrapSync req_wrap_sync("stat", *path); FS_SYNC_TRACE_BEGIN(stat); @@ -1337,14 +1359,13 @@ static void StatFs(const FunctionCallbackInfo& args) { ToNamespacedPath(env, &path); bool use_bigint = args[1]->IsTrue(); - if (argc > 2) { // statfs(path, use_bigint, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2, use_bigint); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + path.ToStringView()); + if (req_wrap_async != nullptr) { // statfs(path, use_bigint, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_STATFS, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -1356,8 +1377,6 @@ static void StatFs(const FunctionCallbackInfo& args) { uv_fs_statfs, *path); } else { // statfs(path, use_bigint) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, path.ToStringView()); FSReqWrapSync req_wrap_sync("statfs", *path); FS_SYNC_TRACE_BEGIN(statfs); int result = @@ -1400,9 +1419,8 @@ static void Symlink(const FunctionCallbackInfo& args) { CHECK(args[2]->IsInt32()); int flags = args[2].As()->Value(); - if (argc > 3) { // symlink(target, path, flags, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // symlink(target, path, flags, req) FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK, req_wrap_async, "target", @@ -1449,26 +1467,25 @@ static void Link(const FunctionCallbackInfo& args) { const auto dest_view = dest.ToStringView(); - if (argc > 2) { // link(src, dest, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); - // To avoid bypass the link target should be allowed to read and write - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - src_view); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - src_view); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + // To avoid bypass the link target should be allowed to read and write + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + src_view); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + src_view); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - dest_view); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + dest_view); + if (req_wrap_async != nullptr) { // link(src, dest, req) FS_ASYNC_TRACE_BEGIN2(UV_FS_LINK, req_wrap_async, "src", @@ -1487,14 +1504,6 @@ static void Link(const FunctionCallbackInfo& args) { *src, *dest); } else { // link(src, dest) - // To avoid bypass the link target should be allowed to read and write - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, src_view); - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemWrite, src_view); - - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemWrite, dest_view); FSReqWrapSync req_wrap_sync("link", *src, *dest); FS_SYNC_TRACE_BEGIN(link); SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_link, *src, *dest); @@ -1517,9 +1526,8 @@ static void ReadLink(const FunctionCallbackInfo& args) { const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - if (argc > 2) { // readlink(path, encoding, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + if (req_wrap_async != nullptr) { // readlink(path, encoding, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_READLINK, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -1564,24 +1572,23 @@ static void Rename(const FunctionCallbackInfo& args) { CHECK_NOT_NULL(*new_path); ToNamespacedPath(env, &new_path); - if (argc > 2) { // rename(old_path, new_path, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - view_old_path); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - view_old_path); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - new_path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + view_old_path); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + view_old_path); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + new_path.ToStringView()); + if (req_wrap_async != nullptr) { // rename(old_path, new_path, req) FS_ASYNC_TRACE_BEGIN2(UV_FS_RENAME, req_wrap_async, "old_path", @@ -1600,14 +1607,6 @@ static void Rename(const FunctionCallbackInfo& args) { *old_path, *new_path); } else { // rename(old_path, new_path) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, view_old_path); - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemWrite, view_old_path); - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - permission::PermissionScope::kFileSystemWrite, - new_path.ToStringView()); FSReqWrapSync req_wrap_sync("rename", *old_path, *new_path); FS_SYNC_TRACE_BEGIN(rename); SyncCallAndThrowOnError( @@ -1630,9 +1629,8 @@ static void FTruncate(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[1])); const int64_t len = args[1].As()->Value(); - if (argc > 2) { // ftruncate(fd, len, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + if (req_wrap_async != nullptr) { // ftruncate(fd, len, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_FTRUNCATE, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -1662,9 +1660,8 @@ static void Fdatasync(const FunctionCallbackInfo& args) { return; } - if (argc > 1) { // fdatasync(fd, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 1); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1); + if (req_wrap_async != nullptr) { // fdatasync(fd, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_FDATASYNC, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -1693,9 +1690,8 @@ static void Fsync(const FunctionCallbackInfo& args) { return; } - if (argc > 1) { - FSReqBase* req_wrap_async = GetReqWrap(args, 1); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1); + if (req_wrap_async != nullptr) { FS_ASYNC_TRACE_BEGIN0(UV_FS_FSYNC, req_wrap_async) AsyncCall( env, req_wrap_async, args, "fsync", UTF8, AfterNoArgs, uv_fs_fsync, fd); @@ -1717,14 +1713,13 @@ static void Unlink(const FunctionCallbackInfo& args) { CHECK_NOT_NULL(*path); ToNamespacedPath(env, &path); - if (argc > 1) { // unlink(path, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 1); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + path.ToStringView()); + if (req_wrap_async != nullptr) { // unlink(path, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_UNLINK, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -1736,10 +1731,6 @@ static void Unlink(const FunctionCallbackInfo& args) { uv_fs_unlink, *path); } else { // unlink(path) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - permission::PermissionScope::kFileSystemWrite, - path.ToStringView()); FSReqWrapSync req_wrap_sync("unlink", *path); FS_SYNC_TRACE_BEGIN(unlink); SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_unlink, *path); @@ -1759,9 +1750,8 @@ static void RMDir(const FunctionCallbackInfo& args) { THROW_IF_INSUFFICIENT_PERMISSIONS( env, permission::PermissionScope::kFileSystemWrite, path.ToStringView()); - if (argc > 1) { - FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req) - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 1); + if (req_wrap_async != nullptr) { // rmdir(path, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_RMDIR, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -2143,9 +2133,8 @@ static void MKDir(const FunctionCallbackInfo& args) { CHECK(args[2]->IsBoolean()); bool mkdirp = args[2]->IsTrue(); - if (argc > 3) { // mkdir(path, mode, recursive, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // mkdir(path, mode, recursive, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_UNLINK, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -2196,14 +2185,13 @@ static void RealPath(const FunctionCallbackInfo& args) { const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - if (argc > 2) { // realpath(path, encoding, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + path.ToStringView()); + if (req_wrap_async != nullptr) { // realpath(path, encoding, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_REALPATH, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -2215,8 +2203,6 @@ static void RealPath(const FunctionCallbackInfo& args) { uv_fs_realpath, *path); } else { // realpath(path, encoding, undefined, ctx) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, path.ToStringView()); FSReqWrapSync req_wrap_sync("realpath", *path); FS_SYNC_TRACE_BEGIN(realpath); int err = @@ -2271,14 +2257,13 @@ static void ReadDir(const FunctionCallbackInfo& args) { bool with_types = args[2]->IsTrue(); - if (argc > 3) { // readdir(path, encoding, withTypes, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + path.ToStringView()); + if (req_wrap_async != nullptr) { // readdir(path, encoding, withTypes, req) req_wrap_async->set_with_file_types(with_types); FS_ASYNC_TRACE_BEGIN1( UV_FS_SCANDIR, req_wrap_async, "path", TRACE_STR_COPY(*path)) @@ -2292,8 +2277,6 @@ static void ReadDir(const FunctionCallbackInfo& args) { *path, 0 /*flags*/); } else { // readdir(path, encoding, withTypes) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, path.ToStringView()); FSReqWrapSync req_wrap_sync("scandir", *path); FS_SYNC_TRACE_BEGIN(readdir); int err = SyncCallAndThrowOnError( @@ -2779,9 +2762,9 @@ static void ReadDirRecursive(const FunctionCallbackInfo& args) { // can do those: lib walks the tree in JS when the permission model is on. CHECK(!env->permission()->enabled()); - if (argc > 3) { // readdirRecursive(path, encoding, withTypes, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != + nullptr) { // readdirRecursive(path, encoding, withTypes, req) req_wrap_async->Init("scandir", nullptr, 0, encoding); FS_ASYNC_TRACE_BEGIN1( UV_FS_SCANDIR, req_wrap_async, "path", TRACE_STR_COPY(*path)) @@ -2894,9 +2877,8 @@ static void Open(const FunctionCallbackInfo& args) { CHECK(args[2]->IsInt32()); const int mode = args[2].As()->Value(); - if (argc > 3) { // open(path, flags, mode, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // open(path, flags, mode, req) if (AsyncCheckOpenPermissions(env, req_wrap_async, path, flags).IsNothing()) return; req_wrap_async->set_is_plain_open(true); @@ -3006,19 +2988,18 @@ static void CopyFile(const FunctionCallbackInfo& args) { CHECK_NOT_NULL(*dest); ToNamespacedPath(env, &dest); - if (argc > 3) { // copyFile(src, dest, flags, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemRead, - src.ToStringView()); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - dest.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemRead, + src.ToStringView()); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + dest.ToStringView()); + if (req_wrap_async != nullptr) { // copyFile(src, dest, flags, req) FS_ASYNC_TRACE_BEGIN2(UV_FS_COPYFILE, req_wrap_async, "src", @@ -3038,12 +3019,6 @@ static void CopyFile(const FunctionCallbackInfo& args) { *dest, flags); } else { // copyFile(src, dest, flags) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, permission::PermissionScope::kFileSystemRead, src.ToStringView()); - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - permission::PermissionScope::kFileSystemWrite, - dest.ToStringView()); FSReqWrapSync req_wrap_sync("copyfile", *src, *dest); FS_SYNC_TRACE_BEGIN(copyfile); SyncCallAndThrowOnError( @@ -3163,9 +3138,8 @@ static void WriteBuffers(const FunctionCallbackInfo& args) { iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk)); } - if (argc > 3) { // writeBuffers(fd, chunks, pos, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // writeBuffers(fd, chunks, pos, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_WRITE, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -3421,9 +3395,8 @@ static void Read(const FunctionCallbackInfo& args) { char* buf = buffer_data + off; uv_buf_t uvbuf = uv_buf_init(buf, len); - if (argc > 5) { // read(fd, buffer, offset, len, pos, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 5); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 5); + if (req_wrap_async != nullptr) { // read(fd, buffer, offset, len, pos, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_READ, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -4053,9 +4026,8 @@ static void ReadBuffers(const FunctionCallbackInfo& args) { iovs[i] = uv_buf_init(Buffer::Data(buffer), Buffer::Length(buffer)); } - if (argc > 3) { // readBuffers(fd, buffers, pos, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // readBuffers(fd, buffers, pos, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_READ, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -4099,9 +4071,8 @@ static void Chmod(const FunctionCallbackInfo& args) { CHECK(args[1]->IsInt32()); int mode = args[1].As()->Value(); - if (argc > 2) { // chmod(path, mode, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + if (req_wrap_async != nullptr) { // chmod(path, mode, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_CHMOD, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -4138,9 +4109,8 @@ static void FChmod(const FunctionCallbackInfo& args) { CHECK(args[1]->IsInt32()); const int mode = args[1].As()->Value(); - if (argc > 2) { // fchmod(fd, mode, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + if (req_wrap_async != nullptr) { // fchmod(fd, mode, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_FCHMOD, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -4178,14 +4148,13 @@ static void Chown(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[2])); const auto gid = FromV8Value(args[2]); - if (argc > 3) { // chown(path, uid, gid, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + path.ToStringView()); + if (req_wrap_async != nullptr) { // chown(path, uid, gid, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_CHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -4199,10 +4168,6 @@ static void Chown(const FunctionCallbackInfo& args) { uid, gid); } else { // chown(path, uid, gid) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - permission::PermissionScope::kFileSystemWrite, - path.ToStringView()); FSReqWrapSync req_wrap_sync("chown", *path); FS_SYNC_TRACE_BEGIN(chown); SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chown, *path, uid, gid); @@ -4230,9 +4195,8 @@ static void FChown(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[2])); const auto gid = FromV8Value(args[2]); - if (argc > 3) { // fchown(fd, uid, gid, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // fchown(fd, uid, gid, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_FCHOWN, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -4268,14 +4232,13 @@ static void LChown(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[2])); const auto gid = FromV8Value(args[2]); - if (argc > 3) { // lchown(path, uid, gid, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - path.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + path.ToStringView()); + if (req_wrap_async != nullptr) { // lchown(path, uid, gid, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_LCHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -4289,10 +4252,6 @@ static void LChown(const FunctionCallbackInfo& args) { uid, gid); } else { // lchown(path, uid, gid) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - permission::PermissionScope::kFileSystemWrite, - path.ToStringView()); FSReqWrapSync req_wrap_sync("lchown", *path); FS_SYNC_TRACE_BEGIN(lchown); SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_lchown, *path, uid, gid); @@ -4318,9 +4277,8 @@ static void UTimes(const FunctionCallbackInfo& args) { CHECK(args[2]->IsNumber()); const double mtime = args[2].As()->Value(); - if (argc > 3) { // utimes(path, atime, mtime, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // utimes(path, atime, mtime, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_UTIME, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -4359,9 +4317,8 @@ static void FUTimes(const FunctionCallbackInfo& args) { CHECK(args[2]->IsNumber()); const double mtime = args[2].As()->Value(); - if (argc > 3) { // futimes(fd, atime, mtime, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // futimes(fd, atime, mtime, req) FS_ASYNC_TRACE_BEGIN0(UV_FS_FUTIME, req_wrap_async) AsyncCall(env, req_wrap_async, @@ -4400,9 +4357,8 @@ static void LUTimes(const FunctionCallbackInfo& args) { CHECK(args[2]->IsNumber()); const double mtime = args[2].As()->Value(); - if (argc > 3) { // lutimes(path, atime, mtime, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 3); + if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_LUTIME, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, @@ -4447,14 +4403,13 @@ static void Mkdtemp(const FunctionCallbackInfo& args) { const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - if (argc > 2) { // mkdtemp(tmpl, encoding, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); - ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - req_wrap_async, - permission::PermissionScope::kFileSystemWrite, - tmpl.ToStringView()); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + FS_THROW_IF_INSUFFICIENT_PERMISSIONS( + env, + req_wrap_async, + permission::PermissionScope::kFileSystemWrite, + tmpl.ToStringView()); + if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req) FS_ASYNC_TRACE_BEGIN1( UV_FS_MKDTEMP, req_wrap_async, "path", TRACE_STR_COPY(*tmpl)) AsyncCall(env, @@ -4466,10 +4421,6 @@ static void Mkdtemp(const FunctionCallbackInfo& args) { uv_fs_mkdtemp, *tmpl); } else { // mkdtemp(tmpl, encoding) - THROW_IF_INSUFFICIENT_PERMISSIONS( - env, - permission::PermissionScope::kFileSystemWrite, - tmpl.ToStringView()); FSReqWrapSync req_wrap_sync("mkdtemp", *tmpl); FS_SYNC_TRACE_BEGIN(mkdtemp); int result = @@ -4504,9 +4455,8 @@ static void Mkstemp(const FunctionCallbackInfo& args) { const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - if (argc > 2) { // mkstemp(tmpl, encoding, req) - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - CHECK_NOT_NULL(req_wrap_async); + FSReqBase* req_wrap_async = GetAsyncReqWrap(args, 2); + if (req_wrap_async != nullptr) { // mkstemp(tmpl, encoding, req) if (AsyncCheckOpenPermissions(env, req_wrap_async, tmpl, kMkstempFlags) .IsNothing()) { return; @@ -5632,12 +5582,8 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - InternalFieldInfo* casted_info = static_cast(info); - BindingData* binding = - realm->AddBindingData(holder, casted_info); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData( + context, holder, static_cast(info)); } bool BindingData::PrepareForSerialization(Local context, @@ -5659,10 +5605,7 @@ bool BindingData::PrepareForSerialization(Local context, } InternalFieldInfoBase* BindingData::Serialize(int index) { - DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = internal_field_info_; - internal_field_info_ = nullptr; - return info; + return TakeInternalFieldInfo(index, &internal_field_info_); } #ifdef _WIN32 diff --git a/src/node_modules.cc b/src/node_modules.cc index ee9be4557ac1..f32abb90d9db 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -67,9 +67,7 @@ bool BindingData::PrepareForSerialization(v8::Local context, InternalFieldInfoBase* BindingData::Serialize(int index) { DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = - InternalFieldInfoBase::New(type()); - return info; + return InternalFieldInfoBase::New(type()); } void BindingData::Deserialize(v8::Local context, @@ -77,10 +75,7 @@ void BindingData::Deserialize(v8::Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - BindingData* binding = realm->AddBindingData(holder); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData(context, holder); } Local BindingData::PackageConfig::Serialize(Realm* realm) const { diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 6a35e5c5e4c4..2db5e3678b52 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -752,10 +752,7 @@ bool BindingData::PrepareForSerialization(Local context, } InternalFieldInfoBase* BindingData::Serialize(int index) { - DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = internal_field_info_; - internal_field_info_ = nullptr; - return info; + return TakeInternalFieldInfo(index, &internal_field_info_); } void BindingData::Deserialize(Local context, @@ -763,13 +760,8 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - v8::HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - // Recreate the buffer in the constructor. - InternalFieldInfo* casted_info = static_cast(info); - BindingData* binding = - realm->AddBindingData(holder, casted_info); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData( + context, holder, static_cast(info)); } static void SetEmitWarningSync(const FunctionCallbackInfo& args) { diff --git a/src/node_realm-inl.h b/src/node_realm-inl.h index 62f77384a387..61a09b6ee2c8 100644 --- a/src/node_realm-inl.h +++ b/src/node_realm-inl.h @@ -108,6 +108,15 @@ inline T* Realm::AddBindingData(v8::Local target, Args&&... args) { return item.get(); } +template T, typename... Args> +void Realm::DeserializeBindingData(v8::Local context, + v8::Local holder, + Args&&... args) { + v8::HandleScope scope(v8::Isolate::GetCurrent()); + Realm* realm = Realm::GetCurrent(context); + CHECK_NOT_NULL(realm->AddBindingData(holder, std::forward(args)...)); +} + inline BindingDataStore* Realm::binding_data_store() { return &binding_data_store_; } diff --git a/src/node_realm.h b/src/node_realm.h index 8bba26f50a92..810d48c8e3ee 100644 --- a/src/node_realm.h +++ b/src/node_realm.h @@ -128,6 +128,13 @@ class Realm : public MemoryRetainer { // GetBindingData(args) later. template T, typename... Args> T* AddBindingData(v8::Local target, Args&&... args); + // Recreates binding data of type T on `holder` in the realm of `context` + // when deserializing a snapshot. Used to implement the static Deserialize() + // method of snapshotable binding data. + template T, typename... Args> + static void DeserializeBindingData(v8::Local context, + v8::Local holder, + Args&&... args); template static inline T* GetBindingData(const v8::PropertyCallbackInfo& info); template diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index faf95133bf47..c2ac34e662ce 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -1701,10 +1701,7 @@ bool BindingData::PrepareForSerialization(Local context, } InternalFieldInfoBase* BindingData::Serialize(int index) { - DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = internal_field_info_; - internal_field_info_ = nullptr; - return info; + return TakeInternalFieldInfo(index, &internal_field_info_); } void BindingData::Deserialize(Local context, @@ -1712,13 +1709,8 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - v8::HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - // Recreate the buffer in the constructor. - InternalFieldInfo* casted_info = static_cast(info); - BindingData* binding = - realm->AddBindingData(holder, casted_info); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData( + context, holder, static_cast(info)); } void BindingData::MemoryInfo(MemoryTracker* tracker) const { diff --git a/src/node_snapshotable.h b/src/node_snapshotable.h index a9050877a04d..95efa51b5276 100644 --- a/src/node_snapshotable.h +++ b/src/node_snapshotable.h @@ -8,6 +8,8 @@ #include "base_object.h" #include "util.h" +#include + namespace node { class Environment; @@ -140,6 +142,15 @@ void SerializeSnapshotableObjects(Realm* realm, #define DCHECK_IS_SNAPSHOT_SLOT(index) DCHECK_EQ(index, BaseObject::kSlot) +// Implements SnapshotableObject::Serialize() for objects that allocate their +// InternalFieldInfo in PrepareForSerialization(): ownership of `*info` is +// handed over to the caller and the field is reset. +template +InternalFieldInfoBase* TakeInternalFieldInfo(int index, Info** info) { + DCHECK_IS_SNAPSHOT_SLOT(index); + return std::exchange(*info, nullptr); +} + namespace mksnapshot { class BindingData : public SnapshotableObject { public: diff --git a/src/node_url.cc b/src/node_url.cc index 0c5c9e65136a..d8a710b7427b 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -97,9 +97,7 @@ bool BindingData::PrepareForSerialization(Local context, InternalFieldInfoBase* BindingData::Serialize(int index) { DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = - InternalFieldInfoBase::New(type()); - return info; + return InternalFieldInfoBase::New(type()); } void BindingData::Deserialize(Local context, @@ -107,10 +105,7 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - BindingData* binding = realm->AddBindingData(holder); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData(context, holder); } #ifndef LARGEST_ASCII_CHAR_CODE_TO_ENCODE diff --git a/src/node_v8.cc b/src/node_v8.cc index 14b74f4b6d42..48ebb4828625 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -166,20 +166,12 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - // Recreate the buffer in the constructor. - InternalFieldInfo* casted_info = static_cast(info); - BindingData* binding = - realm->AddBindingData(holder, casted_info); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData( + context, holder, static_cast(info)); } InternalFieldInfoBase* BindingData::Serialize(int index) { - DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = internal_field_info_; - internal_field_info_ = nullptr; - return info; + return TakeInternalFieldInfo(index, &internal_field_info_); } void BindingData::MemoryInfo(MemoryTracker* tracker) const { diff --git a/src/timers.cc b/src/timers.cc index 03f0910926f3..055597ecb9eb 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -104,9 +104,7 @@ bool BindingData::PrepareForSerialization(Local context, InternalFieldInfoBase* BindingData::Serialize(int index) { DCHECK_IS_SNAPSHOT_SLOT(index); - InternalFieldInfo* info = - InternalFieldInfoBase::New(type()); - return info; + return InternalFieldInfoBase::New(type()); } void BindingData::Deserialize(Local context, @@ -114,11 +112,7 @@ void BindingData::Deserialize(Local context, int index, InternalFieldInfoBase* info) { DCHECK_IS_SNAPSHOT_SLOT(index); - v8::HandleScope scope(Isolate::GetCurrent()); - Realm* realm = Realm::GetCurrent(context); - // Recreate the buffer in the constructor. - BindingData* binding = realm->AddBindingData(holder); - CHECK_NOT_NULL(binding); + Realm::DeserializeBindingData(context, holder); } v8::CFunction BindingData::fast_get_libuv_now_(