From 80df03e31f679478fdc8a6d387e56dda3981a3ed Mon Sep 17 00:00:00 2001 From: Andreas Haas Date: Thu, 22 Mar 2018 10:43:44 +0100 Subject: [PATCH] [wasm][anyref] Add support for AnyRef in signature decoding R=titzer@chromium.org Bug: v8:7581 Change-Id: I51c585aafa71cbc20565974929cba8327d7f8427 Reviewed-on: https://chromium-review.googlesource.com/973305 Commit-Queue: Andreas Haas Reviewed-by: Ben Titzer Cr-Commit-Position: refs/heads/master@{#52133} --- src/flag-definitions.h | 2 ++ src/wasm/function-body-decoder-impl.h | 3 ++ src/wasm/module-decoder.cc | 8 ++++-- src/wasm/wasm-constants.h | 3 +- src/wasm/wasm-opcodes.h | 3 ++ .../unittests/wasm/module-decoder-unittest.cc | 28 +++++++++++++++++-- 6 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/flag-definitions.h b/src/flag-definitions.h index 8532d2caaa..21cd9b2d3c 100644 --- a/src/flag-definitions.h +++ b/src/flag-definitions.h @@ -588,6 +588,8 @@ DEFINE_BOOL(experimental_wasm_sat_f2i_conversions, false, "enable non-trapping float-to-int conversions for wasm") DEFINE_BOOL(experimental_wasm_se, false, "enable prototype sign extension opcodes for wasm") +DEFINE_BOOL(experimental_wasm_anyref, false, + "enable prototype anyref support for wasm") DEFINE_BOOL(wasm_opt, false, "enable wasm optimization") DEFINE_BOOL(wasm_no_bounds_checks, false, diff --git a/src/wasm/function-body-decoder-impl.h b/src/wasm/function-body-decoder-impl.h index 050d42fe52..7627197149 100644 --- a/src/wasm/function-body-decoder-impl.h +++ b/src/wasm/function-body-decoder-impl.h @@ -241,6 +241,9 @@ struct BlockTypeOperand { case kLocalS128: *result = kWasmS128; return true; + case kLocalAnyRef: + *result = kWasmAnyRef; + return true; default: *result = kWasmVar; return false; diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc index 3f2f8bb451..b033dae18b 100644 --- a/src/wasm/module-decoder.cc +++ b/src/wasm/module-decoder.cc @@ -1244,10 +1244,14 @@ class ModuleDecoderImpl : public Decoder { case kLocalF64: return kWasmF64; default: - if (IsWasm() && FLAG_experimental_wasm_simd) { + if (IsWasm()) { switch (t) { case kLocalS128: - return kWasmS128; + if (FLAG_experimental_wasm_simd) return kWasmS128; + break; + case kLocalAnyRef: + if (FLAG_experimental_wasm_anyref) return kWasmAnyRef; + break; default: break; } diff --git a/src/wasm/wasm-constants.h b/src/wasm/wasm-constants.h index 932501d776..dc6f1ee675 100644 --- a/src/wasm/wasm-constants.h +++ b/src/wasm/wasm-constants.h @@ -20,7 +20,8 @@ enum ValueTypeCode : uint8_t { kLocalI64 = 0x7e, kLocalF32 = 0x7d, kLocalF64 = 0x7c, - kLocalS128 = 0x7b + kLocalS128 = 0x7b, + kLocalAnyRef = 0x6f }; // Binary encoding of other types. constexpr uint8_t kWasmFunctionTypeCode = 0x60; diff --git a/src/wasm/wasm-opcodes.h b/src/wasm/wasm-opcodes.h index e734e55ad1..856529c3cb 100644 --- a/src/wasm/wasm-opcodes.h +++ b/src/wasm/wasm-opcodes.h @@ -23,6 +23,7 @@ constexpr ValueType kWasmI32 = MachineRepresentation::kWord32; constexpr ValueType kWasmI64 = MachineRepresentation::kWord64; constexpr ValueType kWasmF32 = MachineRepresentation::kFloat32; constexpr ValueType kWasmF64 = MachineRepresentation::kFloat64; +constexpr ValueType kWasmAnyRef = MachineRepresentation::kTaggedPointer; constexpr ValueType kWasmS128 = MachineRepresentation::kSimd128; constexpr ValueType kWasmVar = MachineRepresentation::kTagged; @@ -749,6 +750,8 @@ class V8_EXPORT_PRIVATE WasmOpcodes { return kLocalF64; case kWasmS128: return kLocalS128; + case kWasmAnyRef: + return kLocalAnyRef; case kWasmStmt: return kLocalVoid; default: diff --git a/test/unittests/wasm/module-decoder-unittest.cc b/test/unittests/wasm/module-decoder-unittest.cc index 6d4f714ebd..a8c1bccea3 100644 --- a/test/unittests/wasm/module-decoder-unittest.cc +++ b/test/unittests/wasm/module-decoder-unittest.cc @@ -137,7 +137,8 @@ struct ValueTypePair { } kValueTypes[] = {{kLocalI32, kWasmI32}, {kLocalI64, kWasmI64}, {kLocalF32, kWasmF32}, - {kLocalF64, kWasmF64}}; + {kLocalF64, kWasmF64}, + {kLocalAnyRef, kWasmAnyRef}}; class WasmModuleVerifyTest : public TestWithIsolateAndZone { public: @@ -894,7 +895,17 @@ TEST_F(WasmModuleVerifyTest, IndirectFunctionInvalidIndex) { EXPECT_FAILURE(data); } -class WasmSignatureDecodeTest : public TestWithZone {}; +class WasmSignatureDecodeTest : public TestWithZone { + public: + WasmSignatureDecodeTest() + // In the following tests we turn on support for AnyRef by default. There + // is a test (Fail_anyref_without_flag) which explicitly turns off support + // for AnyRef. + : flag_scope(&FLAG_experimental_wasm_anyref, true) {} + + private: + FlagScope flag_scope; +}; TEST_F(WasmSignatureDecodeTest, Ok_v_v) { static const byte data[] = {SIG_ENTRY_v_v}; @@ -1010,6 +1021,19 @@ TEST_F(WasmSignatureDecodeTest, Fail_off_end) { } } +TEST_F(WasmSignatureDecodeTest, Fail_anyref_without_flag) { + // Disable AnyRef support and check that decoding fails. + FlagScope flag_scope(&FLAG_experimental_wasm_anyref, false); + byte kInvalidType = kLocalAnyRef; + for (size_t i = 0; i < SIZEOF_SIG_ENTRY_x_xx; i++) { + byte data[] = {SIG_ENTRY_x_xx(kLocalI32, kLocalI32, kLocalI32)}; + data[i] = kInvalidType; + FunctionSig* sig = + DecodeWasmSignatureForTesting(zone(), data, data + sizeof(data)); + EXPECT_EQ(nullptr, sig); + } +} + TEST_F(WasmSignatureDecodeTest, Fail_invalid_type) { byte kInvalidType = 76; for (size_t i = 0; i < SIZEOF_SIG_ENTRY_x_xx; i++) {