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

lib: replace eval with vm.runInThisContext #18623

Closed
wants to merge 1 commit into from

Conversation

MylesBorins
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot added the tools Issues and PRs related to the tools directory. label Feb 7, 2018
@MylesBorins
Copy link
Contributor Author

@MylesBorins MylesBorins added the lib / src Issues and PRs related to general changes in the lib or src directory. label Feb 7, 2018
@@ -1,4 +1,6 @@
/* eslint-disable strict */
Copy link
Member

Choose a reason for hiding this comment

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

Can we enable strict mode, since we are no longer using eval()?

@MylesBorins
Copy link
Contributor Author

updated @TimothyGu

@bnoordhuis
Copy link
Member

bnoordhuis commented Feb 8, 2018

V8's profiler scripts weren't strict mode compatible at one point. You probably want to double-check that turning it on doesn't break anything.

edit: but they're executed as a separate script so it's probably fine.

@MylesBorins
Copy link
Contributor Author

@bnoordhuis or @nodejs/benchmarking how would I go about testing this?

@addaleax
Copy link
Member

addaleax commented Feb 8, 2018

@MylesBorins In theory python tools/test.py tick-processor should do the trick, but on master the tests seem to time out for me? You might want to try this on a release branch instead (or track down what makes things time out, if you experience that too)…

@MylesBorins
Copy link
Contributor Author

@addaleax turtles! Will report back

@MylesBorins
Copy link
Contributor Author

MylesBorins commented Feb 9, 2018

Floated this on the 8.10.0 proposal branch...

So this breaks the world 🎉

ReferenceError: require is not defined
    at evalmachine.<anonymous>:32:12
    at evalmachine.<anonymous>:3859:3
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at internal/v8_prof_processor.js:41:4
    at NativeModule.compile (bootstrap_node.js:597:7)
    at Function.NativeModule.require (bootstrap_node.js:542:18)
    at startup (bootstrap_node.js:134:20)
    at bootstrap_node.js:609:3
evalmachine.<anonymous>:32
const fs = require('fs');

/cc @nodejs/v8

edit: python tools/test.py tick-processor works before this patch is applied on v8.10.0-proposal, is broken on master though

@addaleax
Copy link
Member

addaleax commented Feb 9, 2018

@MylesBorins I guess that’s because eval would capture the require from outside and make it accessible to the evaluated script

You can probably get around that by making the evaluated script a normal function instead of an IIFE, then compile that using vm.runInThisContext and then pass the pseudo-globals it needs (in particular require) to that function, similar to what we do with the normal module wrappers.

That being said: It’s not really obvious to me why we don’t just use require() to load the scripts we want in the first place?

@MylesBorins MylesBorins force-pushed the remove-eval branch 2 times, most recently from 3e96fb8 to aca1ece Compare February 10, 2018 10:14
@MylesBorins
Copy link
Contributor Author

@addaleax removing the IIFE doesn't solve anything. Using require instead also didn't work. I tried banging my head against the wall to figure out why the behavior is different, but it doesn't seem like I can get the tests to pass if eval is switched out.

@bnoordhuis is this change totally neccessary? Could we instead check the V8 flag and bail with a clear warning if eval is disabled?

@addaleax
Copy link
Member

addaleax commented Feb 10, 2018

@MylesBorins

diff --git a/lib/internal/v8_prof_processor.js b/lib/internal/v8_prof_processor.js
index deb8d77d0acb..bb71213a8f4d 100644
--- a/lib/internal/v8_prof_processor.js
+++ b/lib/internal/v8_prof_processor.js
@@ -33,9 +33,9 @@ if (process.platform === 'darwin') {
   tickArguments.push('--windows');
 }
 tickArguments.push.apply(tickArguments, process.argv.slice(1));
-script = `(function() {
+script = `(function(require) {
   arguments = ${JSON.stringify(tickArguments)};
   function write (s) { process.stdout.write(s) }
   ${script}
-})()`;
-vm.runInThisContext(script);
+})`;
+vm.runInThisContext(script)(require);

works fine for me? (edit: on v8.x-staging)

@MylesBorins
Copy link
Contributor Author

@addaleax OHHHHHH. Got it working on my machine. I hadn't considered using runInThisContext to produce a function that I injected require into. Should we potentially include all lexically scoped variables?

specifically exports, require, module, __filename, __dirname

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

@MylesBorins
Copy link
Contributor Author

CI is being weird, one more time https://ci.nodejs.org/job/node-test-pull-request/13117/

@MylesBorins
Copy link
Contributor Author

Here's a build off master to compare to see if these failures are from this PR https://ci.nodejs.org/job/node-test-commit/16182/

@MylesBorins
Copy link
Contributor Author

@TimothyGu it looks like I regressed moving between machines 🤦🏽‍♂️

one more ci: https://ci.nodejs.org/job/node-test-pull-request/13152/

@MylesBorins
Copy link
Contributor Author

YACI (yet another CI): https://ci.nodejs.org/job/node-test-pull-request/13180/

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Feb 16, 2018
@jasnell
Copy link
Member

jasnell commented Feb 16, 2018

CI failures appear to be unrelated, but trying again on arm due to a total build bot failure: https://ci.nodejs.org/job/node-test-commit-arm-fanned/14534/

@BridgeAR
Copy link
Member

BridgeAR commented Feb 17, 2018

It came out green.

Landed in 99d693d 🎉

@BridgeAR BridgeAR closed this Feb 17, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Feb 17, 2018
PR-URL: nodejs#18623
Refs: nodejs#18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins added a commit that referenced this pull request Feb 21, 2018
PR-URL: #18623
Refs: #18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Feb 21, 2018
MylesBorins added a commit that referenced this pull request Feb 21, 2018
PR-URL: #18623
Refs: #18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins added a commit that referenced this pull request Feb 21, 2018
PR-URL: #18623
Refs: #18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax added a commit to addaleax/node that referenced this pull request Feb 28, 2018
Make the script not error out immediately because of a missing
pseudo-global.
(Note that it seems like tests are still broken on `master`.)

Fixes: nodejs#19044
Refs: nodejs#18623
addaleax added a commit that referenced this pull request Mar 5, 2018
Make the script not error out immediately because of a missing
pseudo-global.
(Note that it seems like tests are still broken on `master`.)

PR-URL: #19059
Fixes: #19044
Refs: #18623
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax added a commit to addaleax/node that referenced this pull request Mar 5, 2018
Make the script not error out immediately because of a missing
pseudo-global.
(Note that it seems like tests are still broken on `master`.)

PR-URL: nodejs#19059
Fixes: nodejs#19044
Refs: nodejs#18623
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
PR-URL: nodejs#18623
Refs: nodejs#18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
Make the script not error out immediately because of a missing
pseudo-global.
(Note that it seems like tests are still broken on `master`.)

PR-URL: nodejs#19059
Fixes: nodejs#19044
Refs: nodejs#18623
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax pushed a commit that referenced this pull request Jun 30, 2018
PR-URL: #18623
Refs: #18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
addaleax added a commit that referenced this pull request Jun 30, 2018
Make the script not error out immediately because of a missing
pseudo-global.
(Note that it seems like tests are still broken on `master`.)

PR-URL: #19059
Fixes: #19044
Refs: #18623
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Jul 9, 2018
rvagg pushed a commit that referenced this pull request Aug 16, 2018
PR-URL: #18623
Refs: #18212 (review)
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
rvagg pushed a commit that referenced this pull request Aug 16, 2018
Make the script not error out immediately because of a missing
pseudo-global.
(Note that it seems like tests are still broken on `master`.)

PR-URL: #19059
Fixes: #19044
Refs: #18623
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matheus Marchini <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
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. lib / src Issues and PRs related to general changes in the lib or src directory. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants