Skip to content

Commit 4ad9bd8

Browse files
aykevldeadprogram
authored andcommitted
wasm: ignore arguments and environment variables
The wasm_exec.js file copied from the main Go repository did write those values to address 4096 in linear memory, which led to memory corruption in linear memory. Remove these things for now, until they're actually supported, if support is ever added.
1 parent 2a1dd98 commit 4ad9bd8

File tree

1 file changed

+2
-42
lines changed

1 file changed

+2
-42
lines changed

targets/wasm_exec.js

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@
7373

7474
global.Go = class {
7575
constructor() {
76-
this.argv = ["js"];
77-
this.env = {};
78-
this.exit = (code) => {
79-
if (code !== 0) {
80-
console.warn("exit code:", code);
81-
}
82-
};
8376
this._callbackTimeouts = new Map();
8477
this._nextCallbackTimeoutID = 1;
8578

@@ -342,36 +335,6 @@
342335

343336
const mem = new DataView(this._inst.exports.memory.buffer)
344337

345-
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
346-
let offset = 4096;
347-
348-
const strPtr = (str) => {
349-
let ptr = offset;
350-
new Uint8Array(mem.buffer, offset, str.length + 1).set(encoder.encode(str + "\0"));
351-
offset += str.length + (8 - (str.length % 8));
352-
return ptr;
353-
};
354-
355-
const argc = this.argv.length;
356-
357-
const argvPtrs = [];
358-
this.argv.forEach((arg) => {
359-
argvPtrs.push(strPtr(arg));
360-
});
361-
362-
const keys = Object.keys(this.env).sort();
363-
argvPtrs.push(keys.length);
364-
keys.forEach((key) => {
365-
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
366-
});
367-
368-
const argv = offset;
369-
argvPtrs.forEach((ptr) => {
370-
mem.setUint32(offset, ptr, true);
371-
mem.setUint32(offset + 4, 0, true);
372-
offset += 8;
373-
});
374-
375338
while (true) {
376339
const callbackPromise = new Promise((resolve) => {
377340
this._resolveCallbackPromise = () => {
@@ -381,7 +344,7 @@
381344
setTimeout(resolve, 0); // make sure it is asynchronous
382345
};
383346
});
384-
this._inst.exports.cwa_main(argc, argv);
347+
this._inst.exports.cwa_main();
385348
if (this.exited) {
386349
break;
387350
}
@@ -413,15 +376,12 @@
413376
}
414377

415378
if (isNodeJS) {
416-
if (process.argv.length < 3) {
379+
if (process.argv.length != 3) {
417380
process.stderr.write("usage: go_js_wasm_exec [wasm binary] [arguments]\n");
418381
process.exit(1);
419382
}
420383

421384
const go = new Go();
422-
go.argv = process.argv.slice(2);
423-
go.env = process.env;
424-
go.exit = process.exit;
425385
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
426386
process.on("exit", (code) => { // Node.js exits if no callback is pending
427387
if (code === 0 && !go.exited) {

0 commit comments

Comments
 (0)