From 28fbd737fa975bdccf67b5838337d2450cc2b03a Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sun, 4 Feb 2018 11:48:21 -0500 Subject: [PATCH 1/4] src: move GetNow to Environment --- src/env.cc | 15 +++++++++++++++ src/env.h | 2 ++ src/timer_wrap.cc | 16 ++-------------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/env.cc b/src/env.cc index b05f0bec81df4f..dbd2da579791ac 100644 --- a/src/env.cc +++ b/src/env.cc @@ -12,13 +12,16 @@ namespace node { using v8::Context; using v8::FunctionTemplate; using v8::HandleScope; +using v8::Integer; using v8::Isolate; using v8::Local; using v8::Message; +using v8::Number; using v8::Private; using v8::StackFrame; using v8::StackTrace; using v8::String; +using v8::Value; IsolateData::IsolateData(Isolate* isolate, uv_loop_t* event_loop, @@ -341,6 +344,18 @@ void Environment::ToggleImmediateRef(bool ref) { } +Local Environment::GetNow() { + uv_update_time(event_loop()); + uint64_t now = uv_now(event_loop()); + CHECK(now >= timer_base()); + now -= timer_base(); + if (now <= 0xfffffff) + return Integer::New(isolate(), static_cast(now)); + else + return Number::New(isolate(), static_cast(now)); +} + + void CollectExceptionInfo(Environment* env, v8::Local obj, int errorno, diff --git a/src/env.h b/src/env.h index 7e0152916161bf..5b15178704b407 100644 --- a/src/env.h +++ b/src/env.h @@ -726,6 +726,8 @@ class Environment { static inline Environment* ForAsyncHooks(AsyncHooks* hooks); + v8::Local GetNow(); + private: inline void CreateImmediate(native_immediate_callback cb, void* data, diff --git a/src/timer_wrap.cc b/src/timer_wrap.cc index f1b423d3669ed5..441974ae77b5db 100644 --- a/src/timer_wrap.cc +++ b/src/timer_wrap.cc @@ -37,7 +37,6 @@ using v8::FunctionTemplate; using v8::HandleScope; using v8::Integer; using v8::Local; -using v8::Number; using v8::Object; using v8::String; using v8::Value; @@ -142,7 +141,7 @@ class TimerWrap : public HandleWrap { Local ret; Local args[1]; do { - args[0] = GetNow(env); + args[0] = env->GetNow(); ret = wrap->MakeCallback(kOnTimeout, 1, args).ToLocalChecked(); } while (ret->IsUndefined() && !env->tick_info()->has_thrown() && @@ -153,18 +152,7 @@ class TimerWrap : public HandleWrap { static void Now(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - args.GetReturnValue().Set(GetNow(env)); - } - - static Local GetNow(Environment* env) { - uv_update_time(env->event_loop()); - uint64_t now = uv_now(env->event_loop()); - CHECK(now >= env->timer_base()); - now -= env->timer_base(); - if (now <= 0xfffffff) - return Integer::New(env->isolate(), static_cast(now)); - else - return Number::New(env->isolate(), static_cast(now)); + args.GetReturnValue().Set(env->GetNow()); } uv_timer_t handle_; From f24c57041be20916d816a2b2cf6ba9f2f925be3b Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sun, 4 Feb 2018 11:51:18 -0500 Subject: [PATCH 2/4] tls: provide now value from C++ Instead of separately calling into C++ from JS to retrieve the Timer.now() value, pass it in as an argument. --- lib/_tls_wrap.js | 8 +++----- src/tls_wrap.cc | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 16d8b24c2979d4..fcd447bb5901fe 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -32,7 +32,6 @@ const common = require('_tls_common'); const { StreamWrap } = require('_stream_wrap'); const { Buffer } = require('buffer'); const debug = util.debuglog('tls'); -const { Timer } = process.binding('timer_wrap'); const tls_wrap = process.binding('tls_wrap'); const { TCP, constants: TCPConstants } = process.binding('tcp_wrap'); const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap'); @@ -49,14 +48,13 @@ const kSNICallback = Symbol('snicallback'); const noop = () => {}; -function onhandshakestart() { +function onhandshakestart(now) { debug('onhandshakestart'); - const owner = this.owner; - const now = Timer.now(); - assert(now >= this.lastHandshakeTime); + const owner = this.owner; + if ((now - this.lastHandshakeTime) >= tls.CLIENT_RENEG_WINDOW * 1000) { this.handshakes = 0; } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index 971dbb857f77f5..0cba1898fbaaad 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -230,7 +230,8 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) { if (where & SSL_CB_HANDSHAKE_START) { Local callback = object->Get(env->onhandshakestart_string()); if (callback->IsFunction()) { - c->MakeCallback(callback.As(), 0, nullptr); + Local argv[] = { env->GetNow() }; + c->MakeCallback(callback.As(), arraysize(argv), argv); } } From 18b06b8656a0880be5f81f6a6a827e976c17c1f5 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sun, 4 Feb 2018 14:18:56 -0500 Subject: [PATCH 3/4] fixup: too many f's to count --- src/env.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/env.cc b/src/env.cc index dbd2da579791ac..0c1480d7f01ad4 100644 --- a/src/env.cc +++ b/src/env.cc @@ -349,7 +349,7 @@ Local Environment::GetNow() { uint64_t now = uv_now(event_loop()); CHECK(now >= timer_base()); now -= timer_base(); - if (now <= 0xfffffff) + if (now <= 0xffffffff) return Integer::New(isolate(), static_cast(now)); else return Number::New(isolate(), static_cast(now)); From 6748de457f05a112a7becd71def20e6a65f0ee63 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 7 Feb 2018 11:59:03 -0500 Subject: [PATCH 4/4] fixup: bnoordhuis comment --- src/env.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/env.cc b/src/env.cc index 0c1480d7f01ad4..28e61dce4d95e8 100644 --- a/src/env.cc +++ b/src/env.cc @@ -347,7 +347,7 @@ void Environment::ToggleImmediateRef(bool ref) { Local Environment::GetNow() { uv_update_time(event_loop()); uint64_t now = uv_now(event_loop()); - CHECK(now >= timer_base()); + CHECK_GE(now, timer_base()); now -= timer_base(); if (now <= 0xffffffff) return Integer::New(isolate(), static_cast(now));