diff options
author | Gus Caplan <me@gus.host> | 2018-02-26 14:29:41 -0600 |
---|---|---|
committer | Gus Caplan <me@gus.host> | 2018-03-12 14:39:21 -0500 |
commit | cb5f358ee7461f191db8c88e0af26023558f5232 (patch) | |
tree | f23e34d68a09fb05d044f00bd795575db63a9370 /test/parallel/test-vm-codegen.js | |
parent | a03c90b661f69200f124718b56b55b0cb3506c71 (diff) | |
download | android-node-v8-cb5f358ee7461f191db8c88e0af26023558f5232.tar.gz android-node-v8-cb5f358ee7461f191db8c88e0af26023558f5232.tar.bz2 android-node-v8-cb5f358ee7461f191db8c88e0af26023558f5232.zip |
vm: add code generation options
Adds options to a VM Context to disable code generation from strings
(such as eval or new Function) and WASM code generation
(WebAssembly.compile).
PR-URL: https://github.com/nodejs/node/pull/19016
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Diffstat (limited to 'test/parallel/test-vm-codegen.js')
-rw-r--r-- | test/parallel/test-vm-codegen.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/test/parallel/test-vm-codegen.js b/test/parallel/test-vm-codegen.js new file mode 100644 index 0000000000..ebafe30c07 --- /dev/null +++ b/test/parallel/test-vm-codegen.js @@ -0,0 +1,110 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); + +const { createContext, runInContext, runInNewContext } = require('vm'); + +const WASM_BYTES = Buffer.from( + [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); + + +function expectsError(fn, type) { + try { + fn(); + assert.fail('expected fn to error'); + } catch (err) { + if (typeof type === 'string') + assert.strictEqual(err.name, type); + else + assert(err instanceof type); + } +} + +{ + const ctx = createContext({ WASM_BYTES }); + const test = 'eval(""); new WebAssembly.Module(WASM_BYTES);'; + runInContext(test, ctx); + + runInNewContext(test, { WASM_BYTES }, { + contextCodeGeneration: undefined, + }); +} + +{ + const ctx = createContext({}, { + codeGeneration: { + strings: false, + }, + }); + + const EvalError = runInContext('EvalError', ctx); + expectsError(() => { + runInContext('eval("x")', ctx); + }, EvalError); +} + +{ + const ctx = createContext({ WASM_BYTES }, { + codeGeneration: { + wasm: false, + }, + }); + + const CompileError = runInContext('WebAssembly.CompileError', ctx); + expectsError(() => { + runInContext('new WebAssembly.Module(WASM_BYTES)', ctx); + }, CompileError); +} + +expectsError(() => { + runInNewContext('eval("x")', {}, { + contextCodeGeneration: { + strings: false, + }, + }); +}, 'EvalError'); + +expectsError(() => { + runInNewContext('new WebAssembly.Module(WASM_BYTES)', { WASM_BYTES }, { + contextCodeGeneration: { + wasm: false, + }, + }); +}, 'CompileError'); + +common.expectsError(() => { + createContext({}, { + codeGeneration: { + strings: 0, + }, + }); +}, { + code: 'ERR_INVALID_ARG_TYPE', +}); + +common.expectsError(() => { + runInNewContext('eval("x")', {}, { + contextCodeGeneration: { + wasm: 1, + }, + }); +}, { + code: 'ERR_INVALID_ARG_TYPE' +}); + +common.expectsError(() => { + createContext({}, { + codeGeneration: 1, + }); +}, { + code: 'ERR_INVALID_ARG_TYPE', +}); + +common.expectsError(() => { + createContext({}, { + codeGeneration: null, + }); +}, { + code: 'ERR_INVALID_ARG_TYPE', +}); |