[wasm] Fix reference type global initialization by index.

This fixes initialization of reference type global variables (i.e.
anyref and except_ref) based on an index of another global. It extends
the existing support to exception types, fixes the logic, and also fixes
a missing write barrier.

R=ahaas@chromium.org
TEST=mjsunit/wasm/exceptions-global
BUG=v8:8091

Change-Id: Ia91f1ea03be24fadf3023a5acdd073badb8dcd93
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1539581
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60525}
This commit is contained in:
Michael Starzinger 2019-03-29 13:07:57 +01:00 committed by Commit Bot
parent dd5f07a542
commit 4902e91a82
2 changed files with 26 additions and 14 deletions

View File

@ -1199,25 +1199,21 @@ void InstanceBuilder::InitGlobals() {
SKIP_WRITE_BARRIER);
break;
case WasmInitExpr::kGlobalIndex: {
if (global.type == ValueType::kWasmAnyRef) {
DCHECK(enabled_.anyref);
int other_offset =
module_->globals[global.init.val.global_index].offset;
tagged_globals_->set(global.offset,
tagged_globals_->get(other_offset),
SKIP_WRITE_BARRIER);
}
// Initialize with another global.
uint32_t new_offset = global.offset;
uint32_t old_offset =
module_->globals[global.init.val.global_index].offset;
TRACE("init [globals+%u] = [globals+%d]\n", global.offset, old_offset);
size_t size = (global.type == kWasmI64 || global.type == kWasmF64)
? sizeof(double)
: sizeof(int32_t);
memcpy(raw_buffer_ptr(untagged_globals_, new_offset),
raw_buffer_ptr(untagged_globals_, old_offset), size);
if (ValueTypes::IsReferenceType(global.type)) {
DCHECK(enabled_.anyref || enabled_.eh);
tagged_globals_->set(new_offset, tagged_globals_->get(old_offset));
} else {
size_t size = (global.type == kWasmI64 || global.type == kWasmF64)
? sizeof(double)
: sizeof(int32_t);
memcpy(raw_buffer_ptr(untagged_globals_, new_offset),
raw_buffer_ptr(untagged_globals_, old_offset), size);
}
break;
}
case WasmInitExpr::kNone:

View File

@ -136,3 +136,19 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
let exception2 = mutable_global.value = "an even fancier exception";
assertThrowsEquals(() => instance.exports.rethrow_except_ref(), exception2);
})();*/
// Test custom initialization index for a global "except_ref" variable.
(function TestGlobalExceptRefInitIndex() {
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
let g1_index = builder.addImportedGlobal("m", "exn", kWasmExceptRef);
let g2 = builder.addGlobal(kWasmExceptRef);
g2.init_index = g1_index; // Initialize {g2} to equal {g1}.
builder.addFunction('push_and_return_except_ref', kSig_e_v)
.addBody([kExprGetGlobal, g2.index])
.exportFunc();
let exception = { x: "my fancy exception" };
let instance = builder.instantiate({ "m": { "exn": exception }});
assertSame(exception, instance.exports.push_and_return_except_ref());
})();