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

vm: pass correct context to CompileFunctionInContext in CompileFunction #23206

Closed
wants to merge 1 commit into from

Conversation

darahayes
Copy link

@darahayes darahayes commented Oct 1, 2018

Fixes: #23194

The ContextifyContext::CompileFunction function was accidentally passing the wrong context variable into ScriptCompiler::CompileFunctionInContext this resulted in the incorrect context being passed to the compiled function.

You can quickly test this with the following code:

const vm = require('vm')

const name = 'world'
const parsingContext = vm.createContext({ name: 'world' })

const code = `return 'hello ' + name`

const fn = vm.compileFunction(code, [], { parsingContext })

console.log(fn())

With Node v10.10.0 I get the following output:

:1
return 'hello ' + name
                  ^

ReferenceError: name is not defined
    at <anonymous>:1:19
    at Object.<anonymous> (/Users/dara/mydev/vmtest/index.js:10:13)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)

With the proposed changes, the output is hello world.

I also edited the description of the parsingContext option in the docs to explicitly use the word contextified. The docs use this word everywhere else when referring to a context object.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. vm Issues and PRs related to the vm subsystem. labels Oct 1, 2018
Copy link
Contributor

@ryzokuken ryzokuken left a comment

Choose a reason for hiding this comment

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

Looks great! Could you please add a test?

@darahayes
Copy link
Author

darahayes commented Oct 1, 2018

@ryzokuken I'm also wondering. In these lines:

https://github.com/nodejs/node/pull/23206/files#diff-0cf206672499c2f86db4ffb0cc5b668bR1041

and

https://github.com/nodejs/node/pull/23206/files#diff-0cf206672499c2f86db4ffb0cc5b668bR1052

should we also be checking against parsing_context instead of context? I'm not actually 100% sure what this check is doing but it looks like we're checking the wrong object.

Sorry for the slowness here, I've never actually written c++ before and I'm not exactly familiar with the v8 APIs. I am trying though 😄

@darahayes
Copy link
Author

@ryzokuken I'd be happy to help write a test. Is it good enough to test this from the JS layer or should the test be written in C++? Sorry for all the questions!

@devsnek
Copy link
Member

devsnek commented Oct 1, 2018

@darahayes

In these lines ... should we also be checking against parsing_context instead of context?

Nope. Those get calls should happen in whatever the current context is.

@darahayes
Copy link
Author

@devsnek thanks. Now I think understand what's happening in both of those loops. We're basically checking (by name) to see if that particular value exists in the current context, and if so, then we can add it right?

Working on some small tests now!

@darahayes
Copy link
Author

@ryzokuken just added some small test cases. Hope they're okay :)

@darahayes darahayes changed the title Pass correct context to CompileFunctionInContext in vm.compileFunction vm: pass correct context to CompileFunctionInContext in CompileFunction Oct 1, 2018
@ryzokuken
Copy link
Contributor

@darahayes

Is it good enough to test this from the JS layer or should the test be written in C++?

JS tests should be fine. Thanks.

Copy link
Contributor

@ryzokuken ryzokuken left a comment

Choose a reason for hiding this comment

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

The tests look great, can you confirm that they all pass with the changed code?

@darahayes
Copy link
Author

@ryzokuken using ./configure && make -j4 test shows everything passes.

Additionally, I was able to run the individual test file I modified with python tools/test.py -J --mode=release parallel/test-vm-basic.

Surely this would be running in the Travis build too right?

@mscdex
Copy link
Contributor

mscdex commented Oct 1, 2018

@ryzokuken
Copy link
Contributor

@darahayes It would, yes! The CI failures are probably not related to your change, and it will be fixed soon.

@darahayes
Copy link
Author

pinging @ryzokuken, just wondering is there any more work to be done on my side to get this merged? Thanks!

@lundibundi
Copy link
Member

@darahayes while we are at it could you rebase on master to make CI easier?

@darahayes
Copy link
Author

@lundibundi just rebased!

@lundibundi
Copy link
Member

@ryzokuken
Copy link
Contributor

CI is green! If nobody lands with in the next few hours, I will.

Copy link
Contributor

@ryzokuken ryzokuken left a comment

Choose a reason for hiding this comment

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

Final LG.

@ryzokuken
Copy link
Contributor

@darahayes shucks, the other two commits aren't fixups. Would you mind squashing the three commits into one?

ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: nodejs#23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction
@darahayes
Copy link
Author

@ryzokuken looks like the build failed for some unrelated reason. Some timeout. Is it possible to rerun it and see what happens?

@ryzokuken
Copy link
Contributor

@darahayes timeout in not ok 519 parallel/test-worker-memory indeed. Weird that it'd happen only on Windows. Could you check if it's a known flake?

@ryzokuken
Copy link
Contributor

@darahayes here we go: #23277

@ryzokuken
Copy link
Contributor

/cc @Trott

Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

Now that parsingContext is also the context/realm in which the function is created, the name becomes a misnomer. For v8.Script, the parsingContext option only controls behavior seen through the Inspector API, as the created script is not bound to a V8 context. I suggest we change the name to context or something similar, if we can still do so.

@Trott
Copy link
Member

Trott commented Oct 10, 2018

/cc @Trott

Hi! Are you looking for a review from me? Or is there something else you'd like/need?

@lundibundi
Copy link
Member

lundibundi commented Oct 10, 2018

Resume CI: https://ci.nodejs.org/job/node-test-pull-request/17728/

Though, @darahayes perhaps you can come up with a better name for the context (refs #23206 (review))

@lundibundi lundibundi added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Oct 10, 2018
@ryzokuken
Copy link
Contributor

@Trott pinged you because of the flaky test, thought it'd bring this particular failure to your notice plus you'll have a better idea about the flake in general 😅.

@Trott
Copy link
Member

Trott commented Oct 11, 2018

@Trott pinged you because of the flaky test, thought it'd bring this particular failure to your notice plus you'll have a better idea about the flake in general 😅.

Ah! Well, yes, that test has been observed timing out elsewhere and is reported in #23277. The Resume Build passed, so I think you can proceed.

@danbev
Copy link
Contributor

danbev commented Oct 11, 2018

Landed in 8da674d.

@danbev danbev closed this Oct 11, 2018
danbev pushed a commit that referenced this pull request Oct 11, 2018
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: #23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction

PR-URL: #23206
Fixes: #23194
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
@darahayes
Copy link
Author

Though, @darahayes perhaps you can come up with a better name for the context (refs #23206 (review))

Apologies for the delay in getting back to this. I would have preferred to get my changes right over getting them merged right now.

For v8.Script, the parsingContext option only controls behavior seen through the Inspector API, as the created script is not bound to a V8 context.

You'll have to forgive me, I'm super new to this.

  • I'm not exactly sure what the Inspector API is.
  • It might be a silly question, but is the created script not bound to the context that we pass into ScriptCompiler::CompileFunctionInContext? Or is it just compiled in that context, but at runtime, it's not bound to anything?

In any case if you folks think it's more appropriate, I'd be happy to submit a small PR that renames parsingContext to context from the JS side and the native side (and docs of course).

This would rename an option passed in from userland code, so these would be breaking changes right? Is there anything from a process or technical perspective that I need to be aware of when submitting a PR with breaking changes?

@danbev
Copy link
Contributor

danbev commented Oct 11, 2018

I would have preferred to get my changes right over getting them merged right now.

Sorry about that. I saw the author ready label and thought is was good to go.

@darahayes
Copy link
Author

Sorry about that.

It's all good. It's really my fault because I didn't respond quick enough 😄

@lundibundi
Copy link
Member

lundibundi commented Oct 11, 2018

IMO it's not like this PR is not 'ready', that part was an addition that could happen here or in a follow-up PR as this one was opened for 'pass correct context to CompileFunctionInContext in CompileFunction'.
Also, I think that change deserves it's own PR as this one was almost ready at that point and it was unlikely that any more people would look through it lessening the possibility of coming up with a better name/other improvements connected.

Edit: also, @darahayes no worries, there is no such thing as be quick enough, there are obviously some sane limits of how much time a change might take but usually a PR is either 'needs changes' and it will wait or 'ready to go'. If the change was critical people would just use 'Request changes' to make sure this doesn't get landed before they are fixed.

@darahayes
Copy link
Author

Thanks @lundibundi I'm happy to create a new issue around the naming and work on a follow up PR if you think that's the way to go 😊

targos pushed a commit that referenced this pull request Oct 12, 2018
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: #23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction

PR-URL: #23206
Fixes: #23194
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
jasnell pushed a commit that referenced this pull request Oct 17, 2018
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: #23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction

PR-URL: #23206
Fixes: #23194
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
MylesBorins pushed a commit that referenced this pull request Oct 30, 2018
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: #23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction

PR-URL: #23206
Fixes: #23194
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
@codebytere codebytere mentioned this pull request Nov 27, 2018
rvagg pushed a commit that referenced this pull request Nov 28, 2018
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: #23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction

PR-URL: #23206
Fixes: #23194
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
MylesBorins pushed a commit that referenced this pull request Nov 29, 2018
ContextifyContext::CompileFunction in src/node_contextify.cc
was incorrectly passing the context variable to
ScriptCompiler::CompileFunctionInContext

This meant that the parsingContext option in vm.compileFunction
was not being applied properly to the compiled function.

fixes: #23194

doc: clarify parsingContext option for vm.compileScript

test: usage of parsingContext in vm.compileFunction

PR-URL: #23206
Fixes: #23194
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Sakthipriyan Vairamani <[email protected]>
@codebytere codebytere mentioned this pull request Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

docs: parsingContext option for vm.compileFunction is a little unclear