isolated-vm
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "isolated-vm", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Access to multiple isolates", | ||
@@ -5,0 +5,0 @@ "main": "isolated-vm.js", |
@@ -124,2 +124,19 @@ [](https://www.npmjs.com/package/isolated-vm) [](https://github.com/laverdet/isolated-vm/blob/master/LICENSE) [](https://travis-ci.org/laverdet/isolated-vm) | ||
##### `isolate.cpuTime` *[Array]* | ||
##### `isolate.wallTime` *[Array]* | ||
The total CPU and wall time spent in this isolate. CPU time is the amount of time the isolate has | ||
spent actively doing work on the CPU. Wall time is the amount of time the isolate has been running, | ||
including passive time spent waiting (think "wall" like a clock on the wall). For instance, if an | ||
isolate makes a call into another isolate, wall time will continue increasing while CPU time will | ||
remain the same. | ||
The return format is `[ seconds, nanoseconds ]`, which is the same as the nodejs method | ||
`[process.hrtime](https://nodejs.org/api/process.html#process_process_hrtime_time)`. To convert this | ||
value to milliseconds you could do something like: `(ret[0] + ret[1] / 1e9) * 1000`. Some precision | ||
is lost in this conversion but for most applications it's probably not a big deal. | ||
Note that CPU time may vary drastically if there is contention for the CPU. This could occur if | ||
other processes are trying to do work, or if you have more than `require('os').cpus().length` | ||
isolates currently doing work in the same nodejs process. | ||
### Class: `Context` *[transferable]* | ||
@@ -126,0 +143,0 @@ A context is a sandboxed execution environment within an isolate. Each context contains its own |
@@ -7,2 +7,3 @@ #!/usr/bin/env node | ||
let ret = 0; | ||
let passCount = 0, failCount = 0; | ||
function runTest(test, cb) { | ||
@@ -44,2 +45,3 @@ // Copy env variables | ||
if (stdout !== 'pass\n' || stderr !== '') { | ||
++failCount; | ||
ret = 1; | ||
@@ -53,5 +55,7 @@ console.error( | ||
} else if (code !== 0) { | ||
++failCount; | ||
ret = 1; | ||
console.error(`fail (${code})`); | ||
} else { | ||
++passCount; | ||
console.log(`pass`); | ||
@@ -64,2 +68,3 @@ } | ||
let cb = function() { | ||
console.log('\nCompleted: '+ passCount+ ' passed, '+ failCount+ ' failed.'); | ||
process.exit(ret); | ||
@@ -66,0 +71,0 @@ }; |
@@ -62,1 +62,7 @@ 'use strict'; | ||
isolate.compileScriptSync('slowPromise.applySyncPromise(undefined, [])').run(context, { timeout: 5 }).catch(() => 0); | ||
// Test dead promise (This causes a memory leak! Don't do this!) | ||
global.setSync('deadPromise', new ivm.Reference(function() { | ||
return new Promise(() => {}); | ||
})); | ||
isolate.compileScriptSync('deadPromise.applySyncPromise(undefined, [])').run(context, { timeout: 5 }).catch(() => 0); |
'use strict'; | ||
let ivm = require('isolated-vm'); | ||
function bisect(fn, ii) { | ||
// Phase 1, search upwards | ||
let min = ii; | ||
let max = ii * 2; | ||
while (!fn(max)) { | ||
max *= 2; | ||
} | ||
// Phase 2, binary search | ||
do { | ||
let mid = (min + max) >> 1; | ||
if (fn(mid)) { | ||
if (mid - 1 <= min) { | ||
return mid; | ||
} | ||
max = mid - 1; | ||
} else { | ||
if (mid + 1 >= max) { | ||
return mid; | ||
} | ||
min = mid + 1; | ||
} | ||
} while (true); | ||
} | ||
// Detect isolate failure limit | ||
let memoryLimit = 16; | ||
let failure = bisect(function(ii) { | ||
let isolate = new ivm.Isolate({ memoryLimit }); | ||
let context = isolate.createContextSync(); | ||
isolate.compileScriptSync(''+ function load(num) { | ||
let store = []; | ||
while (num--) { | ||
store.push({}); | ||
} | ||
}).runSync(context); | ||
try { | ||
context.global.getSync('load').applySync(null, [ ii ]); | ||
return false; | ||
} catch (err) { | ||
return true; | ||
} | ||
}, 100000); | ||
// Try to kill an isolate with an array buffer | ||
let overflow = Array(1024).fill().map(() => {}); | ||
overflow.push(new ArrayBuffer(1024)); | ||
let isolate = new ivm.Isolate({ memoryLimit }); | ||
let isolate = new ivm.Isolate({ memoryLimit: 16 }); | ||
let context = isolate.createContextSync(); | ||
isolate.compileScriptSync(''+ function loadAndCopy(num, copy) { | ||
let store = []; | ||
while (num--) { | ||
store.push({}); | ||
} | ||
copy.copy(); | ||
}).runSync(context); | ||
context.global.setSync('copy', new ivm.ExternalCopy([ new ArrayBuffer(1024 * 1024 * 16) ])); | ||
try { | ||
context.global.getSync('loadAndCopy').applySync(null, [ failure - 1024, new ivm.ExternalCopy(overflow) ]); | ||
isolate.compileScriptSync('copy.copy()').runSync(context); | ||
console.log('did not fail?'); | ||
@@ -63,0 +10,0 @@ } catch (err) { |
@@ -9,6 +9,6 @@ 'use strict'; | ||
global.setSync('global', global.derefInto()); | ||
isolate.compileScriptSync('global.run = () => { for(;;); }').runSync(context); | ||
isolate.compileScriptSync('global.run = () => { function the_stack() { for(;;); }; the_stack(); }').runSync(context); | ||
let run = global.getSync('run'); | ||
let uhoh = false; | ||
setTimeout(function() { | ||
let timeout = setTimeout(function() { | ||
uhoh = true; | ||
@@ -18,4 +18,7 @@ }, 500); | ||
run.applySync(undefined, [], { timeout: 20 }); | ||
console.log('what?'); | ||
} catch (err) {} | ||
} catch (err) { | ||
if (!/the_stack/.test(err.stack)) { | ||
console.log('missing stack'); | ||
} | ||
} | ||
@@ -26,4 +29,8 @@ try { | ||
} catch (err) { | ||
if (!/the_stack/.test(err.stack)) { | ||
console.log('missing stack'); | ||
} | ||
console.log(uhoh ? 'uhoh' : 'pass'); | ||
clearTimeout(timeout); | ||
} | ||
})().catch(console.error); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
312142
102
1576
467