2024-08-14 20:41:00 +02:00
|
|
|
// Copyright 2024 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2025-04-29 08:03:15 +02:00
|
|
|
// Flags: --wasm-staging --sandbox-testing
|
2024-08-14 20:41:00 +02:00
|
|
|
|
|
|
|
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.exportMemoryAs("mem0", 0);
|
|
|
|
let $mem0 = builder.addMemory(1, 1);
|
|
|
|
|
|
|
|
let $box = builder.addStruct([makeField(kWasmFuncRef, true)]);
|
|
|
|
|
|
|
|
let $sig_i_l = builder.addType(kSig_i_l);
|
|
|
|
|
|
|
|
// func0 and func1 have no other job than to have incompatible signatures.
|
|
|
|
builder.addFunction("func0", kSig_v_i).exportFunc().addBody([]);
|
|
|
|
builder.addFunction("func1", $sig_i_l).exportFunc().addBody([
|
|
|
|
kExprI32Const, 0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
builder.addFunction("get_func0", kSig_r_v).exportFunc().addBody([
|
|
|
|
kExprRefFunc, 0,
|
|
|
|
kGCPrefix, kExprStructNew, $box,
|
|
|
|
kGCPrefix, kExprExternConvertAny,
|
|
|
|
]);
|
|
|
|
builder.addFunction("get_func1", kSig_r_v).exportFunc().addBody([
|
|
|
|
kExprRefFunc, 1,
|
|
|
|
kGCPrefix, kExprStructNew, $box,
|
|
|
|
kGCPrefix, kExprExternConvertAny,
|
|
|
|
]);
|
|
|
|
builder.addFunction("boom", kSig_i_l).exportFunc().addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprRefFunc, 1,
|
|
|
|
kExprCallRef, $sig_i_l,
|
|
|
|
])
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
instance.exports.func0(0);
|
|
|
|
|
|
|
|
const kHeapObjectTag = 1;
|
|
|
|
const kStructField0Offset = 8; // 0:map, 4:hash
|
2025-01-30 16:35:04 +01:00
|
|
|
const kWasmFuncRefType = Sandbox.getInstanceTypeIdFor('WASM_FUNC_REF_TYPE');
|
|
|
|
const kWasmFuncRefInternalOffset = Sandbox.getFieldOffset(kWasmFuncRefType, 'trusted_internal');
|
2024-08-14 20:41:00 +02:00
|
|
|
|
|
|
|
let memory = new DataView(new Sandbox.MemoryView(0, 0x100000000));
|
|
|
|
|
|
|
|
function getPtr(obj) {
|
|
|
|
return Sandbox.getAddressOf(obj) + kHeapObjectTag;
|
|
|
|
}
|
|
|
|
function getField(obj, offset) {
|
|
|
|
return memory.getUint32(obj + offset - kHeapObjectTag, true);
|
|
|
|
}
|
|
|
|
function setField(obj, offset, value) {
|
|
|
|
memory.setUint32(obj + offset - kHeapObjectTag, value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let f0_box = getPtr(instance.exports.get_func0());
|
|
|
|
let f0 = getField(f0_box, kStructField0Offset);
|
2025-01-30 16:35:04 +01:00
|
|
|
let f0_int = getField(f0, kWasmFuncRefInternalOffset);
|
2024-08-14 20:41:00 +02:00
|
|
|
|
|
|
|
let f1_box = getPtr(instance.exports.get_func1());
|
|
|
|
let f1 = getField(f1_box, kStructField0Offset);
|
|
|
|
|
2025-01-30 16:35:04 +01:00
|
|
|
setField(f1, kWasmFuncRefInternalOffset, f0_int);
|
2024-08-14 20:41:00 +02:00
|
|
|
|
|
|
|
// Signature confusion should kill the process.
|
2025-01-30 16:35:04 +01:00
|
|
|
instance.exports.boom(0x41414141414141n);
|
2024-08-14 20:41:00 +02:00
|
|
|
|
|
|
|
assertUnreachable("Process should have been killed.");
|