Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls: pass in Timer.now() value straight from C++ #18562

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 3 additions & 5 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -341,6 +344,18 @@ void Environment::ToggleImmediateRef(bool ref) {
}


Local<Value> Environment::GetNow() {
uv_update_time(event_loop());
uint64_t now = uv_now(event_loop());
CHECK_GE(now, timer_base());
now -= timer_base();
if (now <= 0xffffffff)
return Integer::New(isolate(), static_cast<uint32_t>(now));
else
return Number::New(isolate(), static_cast<double>(now));
}


void CollectExceptionInfo(Environment* env,
v8::Local<v8::Object> obj,
int errorno,
Expand Down
2 changes: 2 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,8 @@ class Environment {

static inline Environment* ForAsyncHooks(AsyncHooks* hooks);

v8::Local<v8::Value> GetNow();

private:
inline void CreateImmediate(native_immediate_callback cb,
void* data,
Expand Down
16 changes: 2 additions & 14 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -142,7 +141,7 @@ class TimerWrap : public HandleWrap {
Local<Value> ret;
Local<Value> 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() &&
Expand All @@ -153,18 +152,7 @@ class TimerWrap : public HandleWrap {

static void Now(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
args.GetReturnValue().Set(GetNow(env));
}

static Local<Value> 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<uint32_t>(now));
else
return Number::New(env->isolate(), static_cast<double>(now));
args.GetReturnValue().Set(env->GetNow());
}

uv_timer_t handle_;
Expand Down
3 changes: 2 additions & 1 deletion src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ void TLSWrap::SSLInfoCallback(const SSL* ssl_, int where, int ret) {
if (where & SSL_CB_HANDSHAKE_START) {
Local<Value> callback = object->Get(env->onhandshakestart_string());
if (callback->IsFunction()) {
c->MakeCallback(callback.As<Function>(), 0, nullptr);
Local<Value> argv[] = { env->GetNow() };
c->MakeCallback(callback.As<Function>(), arraysize(argv), argv);
}
}

Expand Down