Commit a80b7228 authored by Jakob Kummerow's avatar Jakob Kummerow Committed by V8 LUCI CQ

[wasm-gc] Fix initialization of ref-type fields

The NewWasmStruct/NewWasmArray factory functions didn't take pointer
compression into account; this patch fixes that.

Bug: v8:7748
Change-Id: I7a77d867971aad1df6660a3b7279ca3b2819b86a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3195873Reviewed-by: 's avatarManos Koukoutos <manoskouk@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77172}
parent f4099832
......@@ -1572,14 +1572,17 @@ Handle<WasmArray> Factory::NewWasmArray(
WasmArray result = WasmArray::cast(raw);
result.set_raw_properties_or_hash(*empty_fixed_array(), kRelaxedStore);
result.set_length(length);
for (uint32_t i = 0; i < length; i++) {
Address address = result.ElementAddress(i);
if (type->element_type().is_numeric()) {
if (type->element_type().is_numeric()) {
for (uint32_t i = 0; i < length; i++) {
Address address = result.ElementAddress(i);
elements[i]
.Packed(type->element_type())
.CopyTo(reinterpret_cast<byte*>(address));
} else {
base::WriteUnalignedValue<Object>(address, *elements[i].to_ref());
}
} else {
for (uint32_t i = 0; i < length; i++) {
int offset = result.element_offset(i);
TaggedField<Object>::store(result, offset, *elements[i].to_ref());
}
}
return handle(result, isolate());
......@@ -1594,11 +1597,13 @@ Handle<WasmStruct> Factory::NewWasmStruct(const wasm::StructType* type,
WasmStruct result = WasmStruct::cast(raw);
result.set_raw_properties_or_hash(*empty_fixed_array(), kRelaxedStore);
for (uint32_t i = 0; i < type->field_count(); i++) {
Address address = result.RawFieldAddress(type->field_offset(i));
int offset = type->field_offset(i);
if (type->field(i).is_numeric()) {
Address address = result.RawFieldAddress(offset);
args[i].Packed(type->field(i)).CopyTo(reinterpret_cast<byte*>(address));
} else {
base::WriteUnalignedValue<Object>(address, *args[i].to_ref());
offset += WasmStruct::kHeaderSize;
TaggedField<Object>::store(result, offset, *args[i].to_ref());
}
}
return handle(result, isolate());
......
......@@ -632,6 +632,22 @@ int WasmArray::SizeFor(Map map, int length) {
return kHeaderSize + RoundUp(element_size * length, kTaggedSize);
}
uint32_t WasmArray::element_offset(uint32_t index) {
DCHECK_LE(index, length());
return WasmArray::kHeaderSize +
index * type()->element_type().element_size_bytes();
}
Address WasmArray::ElementAddress(uint32_t index) {
return ptr() + element_offset(index) - kHeapObjectTag;
}
ObjectSlot WasmArray::ElementSlot(uint32_t index) {
DCHECK_LE(index, length());
DCHECK(type()->element_type().is_reference());
return RawField(kHeaderSize + kTaggedSize * index);
}
// static
Handle<Object> WasmArray::GetElement(Isolate* isolate, Handle<WasmArray> array,
uint32_t index) {
......@@ -639,9 +655,8 @@ Handle<Object> WasmArray::GetElement(Isolate* isolate, Handle<WasmArray> array,
return isolate->factory()->undefined_value();
}
wasm::ValueType element_type = array->type()->element_type();
uint32_t offset =
WasmArray::kHeaderSize + index * element_type.element_size_bytes();
return ReadValueAt(isolate, array, element_type, offset);
return ReadValueAt(isolate, array, element_type,
array->element_offset(index));
}
// static
......
......@@ -1688,18 +1688,6 @@ wasm::WasmValue WasmArray::GetElement(uint32_t index) {
}
}
ObjectSlot WasmArray::ElementSlot(uint32_t index) {
DCHECK_LE(index, length());
DCHECK(type()->element_type().is_reference());
return RawField(kHeaderSize + kTaggedSize * index);
}
Address WasmArray::ElementAddress(uint32_t index) {
DCHECK_LE(index, length());
return ptr() + WasmArray::kHeaderSize +
index * type()->element_type().element_size_bytes() - kHeapObjectTag;
}
// static
Handle<WasmTagObject> WasmTagObject::New(Isolate* isolate,
const wasm::FunctionSig* sig,
......
......@@ -925,7 +925,7 @@ class WasmArray : public TorqueGeneratedWasmArray<WasmArray, WasmObject> {
// Get the {ObjectSlot} corresponding to the element at {index}. Requires that
// this is a reference array.
ObjectSlot ElementSlot(uint32_t index);
inline ObjectSlot ElementSlot(uint32_t index);
V8_EXPORT_PRIVATE wasm::WasmValue GetElement(uint32_t index);
static inline int SizeFor(Map map, int length);
......@@ -935,8 +935,9 @@ class WasmArray : public TorqueGeneratedWasmArray<WasmArray, WasmObject> {
Handle<WasmArray> array,
uint32_t index);
// Returns the Address of the element at {index}.
Address ElementAddress(uint32_t index);
// Returns the offset/Address of the element at {index}.
inline uint32_t element_offset(uint32_t index);
inline Address ElementAddress(uint32_t index);
static int MaxLength(const wasm::ArrayType* type) {
// The total object size must fit into a Smi, for filler objects. To make
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment