Commit a3b58252 authored by Manos Koukoutos's avatar Manos Koukoutos Committed by Commit Bot

[wasm][gc][refactor] Decode gc types with immediates consistently.

Motivation:
There were three versions of type decoding for wasm in the codebase.
Not all of them decoded gc types with immediates (reference types)
correctly.

Changes:
- Refactor the wasm binary decoder for unify type decoding.
- Update BranchTypeImmediate and SelectTypeImmediate to handle
  reference types.

Reference: https://github.com/WebAssembly/gc

R=jkummerow@chromium.org
Bug: v8:7748

Change-Id: I33b38c911d366570ca6ef2723ded5205698e1979
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2179003
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67614}
parent 2cb1e2ef
This diff is collapsed.
...@@ -187,6 +187,7 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body, ...@@ -187,6 +187,7 @@ bool PrintRawWasmCode(AccountingAllocator* allocator, const FunctionBody& body,
opcode == kExprTry) { opcode == kExprTry) {
DCHECK_EQ(2, length); DCHECK_EQ(2, length);
// TODO(7748) Update this for gc and ref types if needed
switch (i.pc()[1]) { switch (i.pc()[1]) {
#define CASE_LOCAL_TYPE(local_name, type_name) \ #define CASE_LOCAL_TYPE(local_name, type_name) \
case kLocal##local_name: \ case kLocal##local_name: \
......
...@@ -1722,67 +1722,14 @@ class ModuleDecoderImpl : public Decoder { ...@@ -1722,67 +1722,14 @@ class ModuleDecoderImpl : public Decoder {
return val != 0; return val != 0;
} }
// Reads a single 8-bit integer, interpreting it as a local type.
ValueType consume_value_type() { ValueType consume_value_type() {
byte val = consume_u8("value type"); ValueType result;
ValueTypeCode t = static_cast<ValueTypeCode>(val); uint32_t type_length = value_type_reader::read_value_type<kValidate>(
switch (t) { this, this->pc(), &result,
case kLocalI32: origin_ == kWasmOrigin ? enabled_features_ : WasmFeatures::None());
return kWasmI32; if (type_length == 0) error(pc_, "invalid value type");
case kLocalI64: consume_bytes(type_length);
return kWasmI64; return result;
case kLocalF32:
return kWasmF32;
case kLocalF64:
return kWasmF64;
default:
if (origin_ == kWasmOrigin) {
switch (t) {
case kLocalS128:
if (enabled_features_.has_simd()) return kWasmS128;
break;
case kLocalFuncRef:
if (enabled_features_.has_anyref()) return kWasmFuncRef;
break;
case kLocalAnyRef:
if (enabled_features_.has_anyref()) return kWasmAnyRef;
break;
case kLocalNullRef:
if (enabled_features_.has_anyref()) return kWasmNullRef;
break;
case kLocalExnRef:
if (enabled_features_.has_eh()) return kWasmExnRef;
break;
case kLocalRef:
if (enabled_features_.has_gc()) {
uint32_t type_index = consume_u32v("type index");
return ValueType(ValueType::kRef, type_index);
}
break;
case kLocalOptRef:
if (enabled_features_.has_gc()) {
uint32_t type_index = consume_u32v("type index");
return ValueType(ValueType::kOptRef, type_index);
}
break;
case kLocalEqRef:
if (enabled_features_.has_gc()) {
return ValueType(ValueType::kEqRef);
}
break;
case kLocalI31Ref:
case kLocalRttRef:
if (enabled_features_.has_gc()) {
UNIMPLEMENTED(); // TODO(7748): implement.
}
break;
default:
break;
}
}
error(pc_ - 1, "invalid local type");
return kWasmStmt;
}
} }
// Reads a single 8-bit integer, interpreting it as a reference type. // Reads a single 8-bit integer, interpreting it as a reference type.
......
...@@ -3183,7 +3183,8 @@ class ThreadImpl { ...@@ -3183,7 +3183,8 @@ class ThreadImpl {
break; break;
} }
case kExprSelectWithType: { case kExprSelectWithType: {
SelectTypeImmediate<Decoder::kNoValidate> imm(&decoder, code->at(pc)); SelectTypeImmediate<Decoder::kNoValidate> imm(WasmFeatures::All(),
&decoder, code->at(pc));
len = 1 + imm.length; len = 1 + imm.length;
V8_FALLTHROUGH; V8_FALLTHROUGH;
} }
......
...@@ -395,5 +395,5 @@ function testErrorPosition(bytes, pos, message) { ...@@ -395,5 +395,5 @@ function testErrorPosition(bytes, pos, message) {
]); ]);
let pos = bytes.length - 1 - 1; let pos = bytes.length - 1 - 1;
testErrorPositionAsyncOnly(bytes, pos, 'invalid local type'); testErrorPositionAsyncOnly(bytes, pos, 'invalid value type');
})(); })();
...@@ -2399,6 +2399,7 @@ TEST_F(FunctionBodyDecoderTest, Break_TypeCheck) { ...@@ -2399,6 +2399,7 @@ TEST_F(FunctionBodyDecoderTest, Break_TypeCheck) {
} }
TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll1) { TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll1) {
WASM_FEATURE_SCOPE(anyref);
for (size_t i = 0; i < arraysize(kValueTypes); i++) { for (size_t i = 0; i < arraysize(kValueTypes); i++) {
for (size_t j = 0; j < arraysize(kValueTypes); j++) { for (size_t j = 0; j < arraysize(kValueTypes); j++) {
ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]}; ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
...@@ -2413,6 +2414,7 @@ TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll1) { ...@@ -2413,6 +2414,7 @@ TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll1) {
} }
TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll2) { TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll2) {
WASM_FEATURE_SCOPE(anyref);
for (size_t i = 0; i < arraysize(kValueTypes); i++) { for (size_t i = 0; i < arraysize(kValueTypes); i++) {
for (size_t j = 0; j < arraysize(kValueTypes); j++) { for (size_t j = 0; j < arraysize(kValueTypes); j++) {
ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]}; ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
...@@ -2427,6 +2429,7 @@ TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll2) { ...@@ -2427,6 +2429,7 @@ TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll2) {
} }
TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll3) { TEST_F(FunctionBodyDecoderTest, Break_TypeCheckAll3) {
WASM_FEATURE_SCOPE(anyref);
for (size_t i = 0; i < arraysize(kValueTypes); i++) { for (size_t i = 0; i < arraysize(kValueTypes); i++) {
for (size_t j = 0; j < arraysize(kValueTypes); j++) { for (size_t j = 0; j < arraysize(kValueTypes); j++) {
ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]}; ValueType storage[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
...@@ -2457,6 +2460,7 @@ TEST_F(FunctionBodyDecoderTest, Break_Unify) { ...@@ -2457,6 +2460,7 @@ TEST_F(FunctionBodyDecoderTest, Break_Unify) {
} }
TEST_F(FunctionBodyDecoderTest, BreakIf_cond_type) { TEST_F(FunctionBodyDecoderTest, BreakIf_cond_type) {
WASM_FEATURE_SCOPE(anyref);
for (size_t i = 0; i < arraysize(kValueTypes); i++) { for (size_t i = 0; i < arraysize(kValueTypes); i++) {
for (size_t j = 0; j < arraysize(kValueTypes); j++) { for (size_t j = 0; j < arraysize(kValueTypes); j++) {
ValueType types[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]}; ValueType types[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j]};
...@@ -2470,6 +2474,7 @@ TEST_F(FunctionBodyDecoderTest, BreakIf_cond_type) { ...@@ -2470,6 +2474,7 @@ TEST_F(FunctionBodyDecoderTest, BreakIf_cond_type) {
} }
TEST_F(FunctionBodyDecoderTest, BreakIf_val_type) { TEST_F(FunctionBodyDecoderTest, BreakIf_val_type) {
WASM_FEATURE_SCOPE(anyref);
for (size_t i = 0; i < arraysize(kValueTypes); i++) { for (size_t i = 0; i < arraysize(kValueTypes); i++) {
for (size_t j = 0; j < arraysize(kValueTypes); j++) { for (size_t j = 0; j < arraysize(kValueTypes); j++) {
ValueType types[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j], ValueType types[] = {kValueTypes[i], kValueTypes[i], kValueTypes[j],
......
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