laverdet / isolated-vm

Secure & isolated JS environments for nodejs
ISC License
2.19k stars 154 forks source link

Bus error/Segmentation fault when bumping to Node 18 #431

Closed moriaam closed 11 months ago

moriaam commented 11 months ago

Hello, I started getting failures on ivm.Isolate after bumping node from 16 to 18. I managed to isolated the issue and I'm getting the following errors sporadically:

/bin/sh: line 1:  9883 Segmentation fault: 11  node src/index.js
/bin/sh: line 1:  9929 Bus error: 10           node src/index.js

this seems to be directly related to the import I'm using:

import 'core-js/web/url'

when I replace it with other imports it doesn't reproduce.

failing with node: 18.18.2 working with node 16.17.0

macOS Sonoma 14.1.1 processor 2.4 GHz 8-Core Intel Core i9 yarn 1.22.19

package.json

{
  "name": "vm-issue",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "core-js": "^3.34.0",
    "esbuild": "^0.19.8",
    "isolated-vm": "^4.6.0"
  },
  "scripts": {
    "build": "esbuild src/myWorker.js --bundle --outfile=dist/myWorker.bundle.js",
    "start": "yarn build && node src/index.js"
  }
}

src/index.js

const fs = require('fs')
const ivm = require('isolated-vm')

const myWorkerCode = fs.readFileSync('dist/myWorker.bundle.js', 'utf-8')
const baseSnapshotScripts = [
    { code: myWorkerCode, filename: 'myWorker.js' },
]

const snapshot = ivm.Isolate.createSnapshot(baseSnapshotScripts)

new ivm.Isolate({ snapshot })

src/myWorker.js

import 'core-js/web/url'
laverdet commented 11 months ago

Snapshots which affect global state (in this case core-js is applying a bunch of polyfills) tend to crash v8. The feature is more made for defining a bunch of functions, as far as I can tell. If you can, try something like export const init = () => await import('core-js/web/url') and then invoke await init() when you start the snapshot.

moriaam commented 11 months ago

thank you, that did the trick and solved the issue.