From b5646f4e7427f5276d459a02f01ec3cf57a07ae5 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 14 May 2021 14:56:43 -0500 Subject: [PATCH 1/4] refactor(xsnap): use application layer access to array buffer Use application programming interface (xs* macros) rather than platform programming interface (the fx* functions) to access array buffer. Taking the address of slot is against best practices at the application layer. --- packages/xsnap/src/xsnap.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/xsnap/src/xsnap.c b/packages/xsnap/src/xsnap.c index 277bec2c213..bf63405556b 100644 --- a/packages/xsnap/src/xsnap.c +++ b/packages/xsnap/src/xsnap.c @@ -1473,12 +1473,11 @@ static void fx_issueCommand(xsMachine *the) size_t length; char* buf = NULL; - txSlot* arrayBuffer = &xsArg(0); - length = fxGetArrayBufferLength(the, arrayBuffer); + length = xsGetArrayBufferLength(xsArg(0)); buf = malloc(length); - fxGetArrayBufferData(the, arrayBuffer, 0, buf, length); + xsGetArrayBufferData(xsArg(0), 0, buf, length); int writeError = fxWriteNetString(toParent, "?", buf, length); free(buf); From 67d25812985ce590cda10e2774be885b16fa67fb Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 14 May 2021 15:02:46 -0500 Subject: [PATCH 2/4] fix(xsnap): handle malloc() failure --- packages/xsnap/src/xsnap.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/xsnap/src/xsnap.c b/packages/xsnap/src/xsnap.c index bf63405556b..1749199d59c 100644 --- a/packages/xsnap/src/xsnap.c +++ b/packages/xsnap/src/xsnap.c @@ -1476,6 +1476,9 @@ static void fx_issueCommand(xsMachine *the) length = xsGetArrayBufferLength(xsArg(0)); buf = malloc(length); + if (!buf) { + fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT); + } xsGetArrayBufferData(xsArg(0), 0, buf, length); int writeError = fxWriteNetString(toParent, "?", buf, length); From ee3344cb2114118a7d7afc087b3dd7e639a0353c Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Thu, 13 May 2021 14:14:54 -0500 Subject: [PATCH 3/4] test(xsnap): importing from test-xsnap had side-effects --- packages/xsnap/test/test-xs-perf.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/xsnap/test/test-xs-perf.js b/packages/xsnap/test/test-xs-perf.js index ff442e72887..9cf903bad51 100644 --- a/packages/xsnap/test/test-xs-perf.js +++ b/packages/xsnap/test/test-xs-perf.js @@ -1,7 +1,27 @@ // eslint-disable-next-line import/no-extraneous-dependencies import test from 'ava'; +import * as childProcess from 'child_process'; +import * as os from 'os'; import { xsnap } from '../src/xsnap'; -import { options } from './test-xsnap'; + +const decoder = new TextDecoder(); + +const xsnapOptions = { + name: 'xsnap test worker', + spawn: childProcess.spawn, + os: os.type(), + stderr: 'inherit', + stdout: 'inherit', +}; + +export function options() { + const messages = []; + async function handleCommand(message) { + messages.push(decoder.decode(message)); + return new Uint8Array(); + } + return { ...xsnapOptions, handleCommand, messages }; +} test('meter details', async t => { const opts = options(); From 127e58ac45bc9ea316733bbe6790936ba1b28f56 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 14 May 2021 16:27:18 -0500 Subject: [PATCH 4/4] fix(xsnap): free netstring in issueCommand() - set *dest to 0 on failure --- packages/xsnap/src/xsnap.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/xsnap/src/xsnap.c b/packages/xsnap/src/xsnap.c index 1749199d59c..4e33b97c20c 100644 --- a/packages/xsnap/src/xsnap.c +++ b/packages/xsnap/src/xsnap.c @@ -1389,10 +1389,12 @@ static int fxReadNetString(FILE *inStream, char** dest, size_t* len) } else { *(buf + *len) = 0; } - if (code != 0) { + if (code == 0) { + *dest = buf; + } else { + *dest = 0; free(buf); } - *dest = buf; } return code; } @@ -1497,6 +1499,7 @@ static void fx_issueCommand(xsMachine *the) } xsResult = xsArrayBuffer(buf, len); + free(buf); }