]> Cypherpunks repositories - gostls13.git/commitdiff
misc/wasm: make sure sp is unsigned
authorRichard Musiol <mail@richard-musiol.de>
Sun, 11 Oct 2020 11:33:53 +0000 (13:33 +0200)
committerRichard Musiol <neelance@gmail.com>
Wed, 21 Oct 2020 09:34:39 +0000 (09:34 +0000)
An i32 passed from WebAssembly to JavaScript is always read as a signed
integer. Use the bitshift operator to turn it into an unsigned integer.

Fixes #40923

Change-Id: Ia91ed2145dd2fc3071e2fc22b86ebfcb3c1e9f4f
Reviewed-on: https://go-review.googlesource.com/c/go/+/261358
Trust: Richard Musiol <neelance@gmail.com>
Run-TryBot: Richard Musiol <neelance@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
misc/wasm/wasm_exec.js

index ef97c4e311dcc16119306b3bf0db0dfa7f500301..06b6062a2e0cdd4d5c81935862bf84778e8c4f56 100644 (file)
 
                                        // func wasmExit(code int32)
                                        "runtime.wasmExit": (sp) => {
+                                               sp >>>= 0;
                                                const code = this.mem.getInt32(sp + 8, true);
                                                this.exited = true;
                                                delete this._inst;
 
                                        // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
                                        "runtime.wasmWrite": (sp) => {
+                                               sp >>>= 0;
                                                const fd = getInt64(sp + 8);
                                                const p = getInt64(sp + 16);
                                                const n = this.mem.getInt32(sp + 24, true);
 
                                        // func resetMemoryDataView()
                                        "runtime.resetMemoryDataView": (sp) => {
+                                               sp >>>= 0;
                                                this.mem = new DataView(this._inst.exports.mem.buffer);
                                        },
 
                                        // func nanotime1() int64
                                        "runtime.nanotime1": (sp) => {
+                                               sp >>>= 0;
                                                setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
                                        },
 
                                        // func walltime1() (sec int64, nsec int32)
                                        "runtime.walltime1": (sp) => {
+                                               sp >>>= 0;
                                                const msec = (new Date).getTime();
                                                setInt64(sp + 8, msec / 1000);
                                                this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
 
                                        // func scheduleTimeoutEvent(delay int64) int32
                                        "runtime.scheduleTimeoutEvent": (sp) => {
+                                               sp >>>= 0;
                                                const id = this._nextCallbackTimeoutID;
                                                this._nextCallbackTimeoutID++;
                                                this._scheduledTimeouts.set(id, setTimeout(
 
                                        // func clearTimeoutEvent(id int32)
                                        "runtime.clearTimeoutEvent": (sp) => {
+                                               sp >>>= 0;
                                                const id = this.mem.getInt32(sp + 8, true);
                                                clearTimeout(this._scheduledTimeouts.get(id));
                                                this._scheduledTimeouts.delete(id);
 
                                        // func getRandomData(r []byte)
                                        "runtime.getRandomData": (sp) => {
+                                               sp >>>= 0;
                                                crypto.getRandomValues(loadSlice(sp + 8));
                                        },
 
                                        // func finalizeRef(v ref)
                                        "syscall/js.finalizeRef": (sp) => {
+                                               sp >>>= 0;
                                                const id = this.mem.getUint32(sp + 8, true);
                                                this._goRefCounts[id]--;
                                                if (this._goRefCounts[id] === 0) {
 
                                        // func stringVal(value string) ref
                                        "syscall/js.stringVal": (sp) => {
+                                               sp >>>= 0;
                                                storeValue(sp + 24, loadString(sp + 8));
                                        },
 
                                        // func valueGet(v ref, p string) ref
                                        "syscall/js.valueGet": (sp) => {
+                                               sp >>>= 0;
                                                const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
-                                               sp = this._inst.exports.getsp(); // see comment above
+                                               sp = this._inst.exports.getsp() >>> 0; // see comment above
                                                storeValue(sp + 32, result);
                                        },
 
                                        // func valueSet(v ref, p string, x ref)
                                        "syscall/js.valueSet": (sp) => {
+                                               sp >>>= 0;
                                                Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
                                        },
 
                                        // func valueDelete(v ref, p string)
                                        "syscall/js.valueDelete": (sp) => {
+                                               sp >>>= 0;
                                                Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
                                        },
 
                                        // func valueIndex(v ref, i int) ref
                                        "syscall/js.valueIndex": (sp) => {
+                                               sp >>>= 0;
                                                storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
                                        },
 
                                        // valueSetIndex(v ref, i int, x ref)
                                        "syscall/js.valueSetIndex": (sp) => {
+                                               sp >>>= 0;
                                                Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
                                        },
 
                                        // func valueCall(v ref, m string, args []ref) (ref, bool)
                                        "syscall/js.valueCall": (sp) => {
+                                               sp >>>= 0;
                                                try {
                                                        const v = loadValue(sp + 8);
                                                        const m = Reflect.get(v, loadString(sp + 16));
                                                        const args = loadSliceOfValues(sp + 32);
                                                        const result = Reflect.apply(m, v, args);
-                                                       sp = this._inst.exports.getsp(); // see comment above
+                                                       sp = this._inst.exports.getsp() >>> 0; // see comment above
                                                        storeValue(sp + 56, result);
                                                        this.mem.setUint8(sp + 64, 1);
                                                } catch (err) {
 
                                        // func valueInvoke(v ref, args []ref) (ref, bool)
                                        "syscall/js.valueInvoke": (sp) => {
+                                               sp >>>= 0;
                                                try {
                                                        const v = loadValue(sp + 8);
                                                        const args = loadSliceOfValues(sp + 16);
                                                        const result = Reflect.apply(v, undefined, args);
-                                                       sp = this._inst.exports.getsp(); // see comment above
+                                                       sp = this._inst.exports.getsp() >>> 0; // see comment above
                                                        storeValue(sp + 40, result);
                                                        this.mem.setUint8(sp + 48, 1);
                                                } catch (err) {
 
                                        // func valueNew(v ref, args []ref) (ref, bool)
                                        "syscall/js.valueNew": (sp) => {
+                                               sp >>>= 0;
                                                try {
                                                        const v = loadValue(sp + 8);
                                                        const args = loadSliceOfValues(sp + 16);
                                                        const result = Reflect.construct(v, args);
-                                                       sp = this._inst.exports.getsp(); // see comment above
+                                                       sp = this._inst.exports.getsp() >>> 0; // see comment above
                                                        storeValue(sp + 40, result);
                                                        this.mem.setUint8(sp + 48, 1);
                                                } catch (err) {
 
                                        // func valueLength(v ref) int
                                        "syscall/js.valueLength": (sp) => {
+                                               sp >>>= 0;
                                                setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
                                        },
 
                                        // valuePrepareString(v ref) (ref, int)
                                        "syscall/js.valuePrepareString": (sp) => {
+                                               sp >>>= 0;
                                                const str = encoder.encode(String(loadValue(sp + 8)));
                                                storeValue(sp + 16, str);
                                                setInt64(sp + 24, str.length);
 
                                        // valueLoadString(v ref, b []byte)
                                        "syscall/js.valueLoadString": (sp) => {
+                                               sp >>>= 0;
                                                const str = loadValue(sp + 8);
                                                loadSlice(sp + 16).set(str);
                                        },
 
                                        // func valueInstanceOf(v ref, t ref) bool
                                        "syscall/js.valueInstanceOf": (sp) => {
+                                               sp >>>= 0;
                                                this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
                                        },
 
                                        // func copyBytesToGo(dst []byte, src ref) (int, bool)
                                        "syscall/js.copyBytesToGo": (sp) => {
+                                               sp >>>= 0;
                                                const dst = loadSlice(sp + 8);
                                                const src = loadValue(sp + 32);
                                                if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
 
                                        // func copyBytesToJS(dst ref, src []byte) (int, bool)
                                        "syscall/js.copyBytesToJS": (sp) => {
+                                               sp >>>= 0;
                                                const dst = loadValue(sp + 8);
                                                const src = loadSlice(sp + 16);
                                                if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {