Skip to content

Commit

Permalink
Print error on unexpected bootstrap errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jun 16, 2023
1 parent 66dacc4 commit 1a488fd
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ function wrapWithActivateUvLoop(func) {
// Redirect Node to execute from current ASAR archive, using a virtual
// "asar" directory as root.
return require('path').join(execPath, 'asar')
} catch (e) {
} catch (error) {
// Not an ASAR archive, continue to Node's default routine.
if (error.message != 'Not an ASAR archive')
throw error
}
})
10 changes: 5 additions & 5 deletions src/pickle.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var PickleIterator = (function () {
if (method != null) {
return method.call(this.payload, readPayloadOffset, length)
} else {
return this.payload.slice(readPayloadOffset, readPayloadOffset + length)
return this.payload.subarray(readPayloadOffset, readPayloadOffset + length)
}
}

Expand Down Expand Up @@ -116,7 +116,7 @@ var Pickle = (function () {
}

Pickle.prototype.initEmpty = function () {
this.header = new Buffer(0)
this.header = Buffer.alloc(0)
this.headerSize = SIZE_UINT32
this.capacityAfterHeader = 0
this.writeOffset = 0
Expand All @@ -136,7 +136,7 @@ var Pickle = (function () {
this.headerSize = 0
}
if (this.headerSize === 0) {
this.header = new Buffer(0)
this.header = Buffer.alloc(0)
}
}

Expand All @@ -145,7 +145,7 @@ var Pickle = (function () {
}

Pickle.prototype.toBuffer = function () {
return this.header.slice(0, this.headerSize + this.getPayloadSize())
return this.header.subarray(0, this.headerSize + this.getPayloadSize())
}

Pickle.prototype.writeBool = function (value) {
Expand Down Expand Up @@ -212,7 +212,7 @@ var Pickle = (function () {

Pickle.prototype.resize = function (newCapacity) {
newCapacity = alignInt(newCapacity, PAYLOAD_UNIT)
this.header = Buffer.concat([this.header, new Buffer(newCapacity)])
this.header = Buffer.concat([this.header, Buffer.alloc(newCapacity)])
this.capacityAfterHeader = newCapacity
}

Expand Down
4 changes: 4 additions & 0 deletions src/yode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#include <stdlib.h>

#include "node/src/env-inl.h"
#include "node/src/node_errors.h"
#include "src/node_integration.h"

using node::errors::TryCatchScope;

namespace yode {

// Generated from js files.
Expand Down Expand Up @@ -59,6 +62,7 @@ void Bootstrap(const v8::FunctionCallbackInfo<v8::Value>& args) {
for (int i = 0; i < args.Length(); ++i)
bootstrap_args[i] = args[i];
bootstrap_args.push_back(ToV8(env, env->exec_path().c_str()));
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
v8::MaybeLocal<v8::Value> ret =
bootstrap->Call(env->context(), exports,
bootstrap_args.size(), bootstrap_args.data());
Expand Down
85 changes: 85 additions & 0 deletions test/mac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const fs = require('node:fs/promises')

// Implementation taken from https://github.com/vercel/pkg/pull/1164.
//
// The MIT License (MIT)
//
// Copyright (c) 2021 Vercel, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
function parseCStr(buf) {
for (let i = 0; i < buf.length; i += 1) {
if (buf[i] === 0) {
return buf.slice(0, i).toString();
}
}
}

function patchCommand(type, buf, file) {
// segment_64
if (type === 0x19) {
const name = parseCStr(buf.slice(0, 16));

if (name === '__LINKEDIT') {
const fileoff = buf.readBigUInt64LE(32);
const vmsize_patched = BigInt(file.length) - fileoff;
const filesize_patched = vmsize_patched;

buf.writeBigUInt64LE(vmsize_patched, 24);
buf.writeBigUInt64LE(filesize_patched, 40);
}
}

// symtab
if (type === 0x2) {
const stroff = buf.readUInt32LE(8);
const strsize_patched = file.length - stroff;

buf.writeUInt32LE(strsize_patched, 12);
}
}

async function extendStringTableSize(target) {
const file = await fs.readFile(target)

const align = 8;
const hsize = 32;

const ncmds = file.readUInt32LE(16);
const buf = file.slice(hsize);

for (let offset = 0, i = 0; i < ncmds; i += 1) {
const type = buf.readUInt32LE(offset);

offset += 4;
const size = buf.readUInt32LE(offset) - 8;

offset += 4;
patchCommand(type, buf.slice(offset, offset + size), file);

offset += size;
if (offset & align) {
offset += align - (offset & align);
}
}

await fs.writeFile(target, file)
}

module.exports = {extendStringTableSize}
19 changes: 14 additions & 5 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if (require.main == module) {
}

const assert = require('assert')
const cp = require('child_process')
const path = require('path')
const fs = require('fs')
const asar = require('../deps/asar')
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('node', function() {
fs.unlinkSync(p)
})

const child = require('child_process').fork(p)
const child = cp.fork(p)
let sent = false
child.on('message', (msg) => {
assert.equal(msg, 'ok')
Expand All @@ -91,7 +92,7 @@ describe('node', function() {
fs.unlinkSync(p)
})

const child = require('child_process').fork(p)
const child = cp.fork(p)
child.on('exit', (code) => {
assert.equal(code, 123)
done()
Expand All @@ -110,7 +111,8 @@ describe('node', function() {
assert.equal(result.stdout.toString().trim(), p)
})

it('start with asar with offset', async () => {
it('start with asar with offset', async function() {
this.timeout(10 * 1000)
const result = await packageAndRun('fs_async', changeOffset)
assert.equal(result.status, 0)
assert.ok(result.stdout.toString().includes('fs.readFile(__filename'))
Expand Down Expand Up @@ -144,12 +146,19 @@ async function packageAndRun(filename, modifyBinary = null) {
await asar.createPackage(path.join(__dirname, 'asar_' + filename), a)
const p = path.join(__dirname, '..', `${filename}_${path.basename(process.execPath)}`)
fs.writeFileSync(p, fs.readFileSync(process.execPath))
if (modifyBinary)
if (modifyBinary) {
if (process.platform == 'darwin')
cp.execSync(`codesign --remove-signature ${p}`)
modifyBinary(p)
}
fs.appendFileSync(p, fs.readFileSync(a))
appendMeta(p, a)
fs.chmodSync(p, 0o755)
const result = require('child_process').spawnSync(p)
if (modifyBinary && process.platform == 'darwin') {
await require('./mac').extendStringTableSize(p)
cp.execSync(`codesign --sign - ${p}`)
}
const result = cp.spawnSync(p)
if (result.status !== null) {
// Will be left for debugging if failed to run.
fs.unlinkSync(a)
Expand Down

0 comments on commit 1a488fd

Please sign in to comment.