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

src: fix async hooks crashing when there is no node context #19134

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
6b76017
src: fix async hooks crashing when there is no node context
xaviergonz Mar 4, 2018
84a1822
src: optimization for check for env promise hook
xaviergonz Mar 4, 2018
5e9128f
src: fixed linting errors for env.cc
xaviergonz Mar 4, 2018
9690b86
fix: updated with the latest code review
xaviergonz Mar 6, 2018
0e4fda8
fix: made the magic number smaller and a constant
xaviergonz Mar 6, 2018
c938269
fix: switched back to promise creation context
xaviergonz Mar 8, 2018
70169d3
fix: using a faster way to know if we are in a node context
xaviergonz Mar 8, 2018
9f69beb
Merge pull request #1 from nodejs/master
xaviergonz Apr 11, 2018
96a53db
Merge branch 'master' into fix-async-hooks-crash
xaviergonz Apr 11, 2018
cb3bba1
fix: rebase to latest master and use node_context_data constants
xaviergonz Apr 11, 2018
51257ed
fix: added namespace for enum
xaviergonz Apr 11, 2018
361d425
fix: storing actual pointers on the context
xaviergonz Apr 19, 2018
704b418
Merge branch 'master' into fix-async-hooks-crash
xaviergonz Apr 19, 2018
bca7fd1
src: code review
xaviergonz May 14, 2018
0577c29
fix: code review changes
xaviergonz May 14, 2018
54bf6dc
fix: code review
xaviergonz May 14, 2018
3c4d991
fix: code review changes
xaviergonz May 14, 2018
4d20b72
fix: code review
xaviergonz May 14, 2018
d9214a7
Merge branch 'master' into fix-async-hooks-crash
xaviergonz Jun 26, 2018
23fa418
src: fix async hooks crashing when there is no node context
xaviergonz Mar 4, 2018
c2b9b32
src: optimization for check for env promise hook
xaviergonz Mar 4, 2018
4437f90
src: fixed linting errors for env.cc
xaviergonz Mar 4, 2018
436919e
fix: updated with the latest code review
xaviergonz Mar 6, 2018
6b095d7
fix: made the magic number smaller and a constant
xaviergonz Mar 6, 2018
85cf945
fix: switched back to promise creation context
xaviergonz Mar 8, 2018
76df613
fix: using a faster way to know if we are in a node context
xaviergonz Mar 8, 2018
077c7a5
fix: rebase to latest master and use node_context_data constants
xaviergonz Apr 11, 2018
92ec8c1
fix: added namespace for enum
xaviergonz Apr 11, 2018
39b5c1f
fix: storing actual pointers on the context
xaviergonz Apr 19, 2018
9c72574
src: code review
xaviergonz May 14, 2018
e91cdc0
fix: code review changes
xaviergonz May 14, 2018
6ef891e
fix: code review
xaviergonz May 14, 2018
5ac8e62
fix: code review changes
xaviergonz May 14, 2018
aaa53b1
fix: code review
xaviergonz May 14, 2018
3a54578
fix: rebasing
xaviergonz Jun 26, 2018
634a63e
fix: linting errors
xaviergonz Jun 26, 2018
10b280e
fix: use actual magic number as ptr rather than ptr to int variable
xaviergonz Jun 26, 2018
e394201
fix: reinterpret cast and linting
xaviergonz Jun 26, 2018
af69d52
fix: better reinterpret cast
xaviergonz Jun 26, 2018
927d984
fix: reverting using fixed address
xaviergonz Jun 26, 2018
290b627
fix: fix for failing unit tests
xaviergonz Jun 27, 2018
93d7670
Merge branch 'master' into fix-async-hooks-crash
xaviergonz Jul 15, 2018
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
25 changes: 24 additions & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "node_buffer.h"
#include "node_platform.h"
#include "node_file.h"
#include "node_context_data.h"
#include "tracing/agent.h"

#include <stdio.h>
Expand All @@ -24,6 +25,11 @@ using v8::StackTrace;
using v8::String;
using v8::Value;

const int kNodeContextTag = 0x6e6f64;
void* kNodeContextTagPtr = const_cast<void*>(
static_cast<const void*>(&kNodeContextTag)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (the parenthesis part, the indent used looks like 2 spaces to me? or is 4 a special rule for these cases?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it’s a block/conditional body/function body, 2 spaces, otherwise 4 for statement continuations.

We try to document this at https://github.com/nodejs/node/blob/master/CPP_STYLE_GUIDE.md, if you have any suggestions for improving the clarity of that document please say so :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah thanks, missed that one. fixed!

);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: Left-leaning pointers (i.e. void* rather than void *). Also, I think the inner reinterpret_cast could just be a static_cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


IsolateData::IsolateData(Isolate* isolate,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
Expand Down Expand Up @@ -195,6 +201,10 @@ void Environment::Start(int argc,
set_process_object(process_object);

SetupProcessObject(this, argc, argv, exec_argc, exec_argv);

// Used by EnvPromiseHook to know that we are on a node context.
context()->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kContextTag, kNodeContextTagPtr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be happening in Environment::AssignToContext rather than here… that means that kNodeContextTag would have to be in a more public location (e.g. as a static private variable of Environment), but I think that’s fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that one might take a while. I'd need to clone the whole repo again, etc etc.


LoadAsyncWrapperInfo(this);

static uv_once_t init_once = UV_ONCE_INIT;
Expand Down Expand Up @@ -364,7 +374,20 @@ bool Environment::RemovePromiseHook(promise_hook_func fn, void* arg) {
void Environment::EnvPromiseHook(v8::PromiseHookType type,
v8::Local<v8::Promise> promise,
v8::Local<v8::Value> parent) {
Environment* env = Environment::GetCurrent(promise->CreationContext());
Local<v8::Context> context = promise->CreationContext();

// Grow the embedder data if necessary to make sure we are not out of bounds
// when reading the magic number.
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kContextTagBoundary, nullptr);
int* magicNumberPtr = reinterpret_cast<int*>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: magic_number

If you reinterpret_cast<uintptr_t>(...), you can define enum { kNodeContextTag = ... }; and compare against that. Arguably a little neater.

context->GetAlignedPointerFromEmbedderData(ContextEmbedderIndex::kContextTag)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: four space indentation. You can move the ) on the next line to this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (the parenthesis part, the indent used looks like 2 spaces to me? or is 4 a special rule for these cases?)

);
if (magicNumberPtr != kNodeContextTagPtr || *magicNumberPtr != kNodeContextTag) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first condition implies the second one :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be the reaaaaally tiny small chance that the pointer might actually point to the "random" memory location yet that this random location does not contain the expected value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, if magicNumberPtr == kContextTagPtr, then *magicNumberPtr == *kContextTagPtr, right? And kNodeContextTagPtr is modified nowhere, so it will always point to kNodeContextTag… or am I overlooking something?

Copy link
Contributor Author

@xaviergonz xaviergonz May 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's assuming that the context is actually created by node, maybe there could be the chance that:
a) some other program (e.g. electron) uses that field number
b) that by a (very small) chance that other program stores on that field a pointer that happens to match the expected pointer
c) and that the pointer contents are the "magic number" (extremely unlikely)

I mean, it is a failsafe, but I can just remove it and set the magic number to 0 and just rely on the pointer address if you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xaviergonz Yeah, but if they manage to get the same pointer into the same internal field, then we still won’t be able to distinguish these. :)

I mean, it is a failsafe, but I can just remove it and set the magic number to 0 and just rely on the pointer address if you want.

I think it’s good to use a magic value here so that it’s easier to recognize – just saying that the second condition here will currently never be true if the first one was false. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually upon thinking about it a bit more you are right :) The same memory location cannot be shared, so I'll just remove the second check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return;
}

Environment* env = Environment::GetCurrent(context);

for (const PromiseHookCallback& hook : env->promise_hooks_) {
hook.cb_(type, promise, parent, hook.arg_);
}
Expand Down
10 changes: 10 additions & 0 deletions src/node_context_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ namespace node {
#define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34
#endif

#ifndef NODE_CONTEXT_TAG
#define NODE_CONTEXT_TAG 35
#endif

#ifndef NODE_CONTEXT_TAG_BOUNDARY
#define NODE_CONTEXT_TAG_BOUNDARY 36
#endif

enum ContextEmbedderIndex {
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
kContextTag = NODE_CONTEXT_TAG,
kContextTagBoundary = NODE_CONTEXT_TAG_BOUNDARY,
};

} // namespace node
Expand Down