Skip to content

Instantly share code, notes, and snippets.

@anvbis
Last active January 19, 2023 13:35
Embed
What would you like to do?
dataview_offset.js, wasm_mutable_globals.js
const addrof = o => {
return Sandbox.getAddressOf(o);
};
const weak_read = p => {
let reader = new Sandbox.MemoryView(p, 64);
let view = new DataView(reader);
return view.getBigUint64(0, true);
};
const weak_write = (p, x) => {
let writer = new Sandbox.MemoryView(p, 64);
let view = new DataView(writer);
view.setBigUint64(0, x, true);
};
let buf = new ArrayBuffer(128);
let view = new DataView(buf);
let heap = (weak_read(0x18) >> 32n) << 32n;
let store = heap + ((weak_read(addrof(buf) + 0x20) & 0xffffffffn) << 8n);
const strong_read = p => {
let offset = p - store;
if (p < store) {
offset = 0xffffffffffffffffn - store + p + 1n;
}
weak_write(addrof(view) + 0x10, offset);
return view.getBigUint64(0, true);
};
const strong_write = (p, x) => {
let offset = p - store;
if (p < store) {
offset = 0xffffffffffffffffn - store + p + 1n;
}
weak_write(addrof(view) + 0x10, offset);
view.setBigUint64(0, x, true);
};
let buf = new ArrayBuffer(8);
let f64 = new Float64Array(buf);
let u64 = new BigUint64Array(buf);
let i64 = new BigInt64Array(buf);
const utof = x => {
u64[0] = x;
return f64[0];
};
const itou = x => {
i64[0] = x;
return u64[0];
};
const hex = x => {
return `0x${x.toString(16)}`;
};
const addrof = o => {
return Sandbox.getAddressOf(o);
};
const weak_read = p => {
let reader = new Sandbox.MemoryView(p, 64);
let view = new DataView(reader);
return view.getBigUint64(0, true);
};
const weak_write = (p, x) => {
let writer = new Sandbox.MemoryView(p, 64);
let view = new DataView(writer);
view.setBigUint64(0, x, true);
};
const global = new WebAssembly.Global({ value: "i64", mutable: true }, 0n);
/*
(module
(global $g (import "js" "global") (mut i64))
(func (export "read") (result i64)
(global.get $g)
)
(func (export "write") (param $p i64)
(global.set $g (local.get $p))
)
)
*/
let wasm = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x00, 0x01, 0x7e, 0x60,
0x01, 0x7e, 0x00, 0x02, 0x0e, 0x01, 0x02, 0x6a, 0x73, 0x06, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c,
0x03, 0x7e, 0x01, 0x03, 0x03, 0x02, 0x00, 0x01, 0x07, 0x10, 0x02, 0x04, 0x72, 0x65, 0x61, 0x64,
0x00, 0x00, 0x05, 0x77, 0x72, 0x69, 0x74, 0x65, 0x00, 0x01, 0x0a, 0x0d, 0x02, 0x04, 0x00, 0x23,
0x00, 0x0b, 0x06, 0x00, 0x20, 0x00, 0x24, 0x00, 0x0b
]);
let module = new WebAssembly.Module(wasm);
let instance = new WebAssembly.Instance(module, {
js: { global }
});
let heap = (weak_read(0x18) >> 32n) << 32n;
let store = [1.1];
let elements = heap + (weak_read(addrof(store) + 0x8) & 0xffffffffn);
weak_write(addrof(instance) + 0x58, elements + 8n - 1n);
const strong_read = p => {
store[0] = utof(p);
return itou(instance.exports.read());
};
const strong_write = (p, x) => {
store[0] = utof(p);
instance.exports.write(x);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment