From d03802c6d29f7136e80cdf8426de1898ff16fe23 Mon Sep 17 00:00:00 2001 From: Michael Starzinger Date: Thu, 28 Nov 2019 14:05:12 +0100 Subject: [PATCH] [wasm] Use checked casts for builtins where possible. This makes sure that proper checked casts (i.e. {CAST} instead of the unsafe {UncheckedCast} before) are used for all WebAssembly runtime stubs. This is possible because the corresponding builtins are embedded and hence can use proper assertion mechanisms. R=clemensb@chromium.org BUG=v8:10021 Change-Id: I344ae8ba7dd4c5d3f5dc2467f7d58b93517d9af7 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1940264 Commit-Queue: Michael Starzinger Reviewed-by: Clemens Backes Cr-Commit-Position: refs/heads/master@{#65236} --- src/builtins/builtins-wasm-gen.cc | 109 ++++++++++++++---------------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/src/builtins/builtins-wasm-gen.cc b/src/builtins/builtins-wasm-gen.cc index a6b69176a2..0f5d86e646 100644 --- a/src/builtins/builtins-wasm-gen.cc +++ b/src/builtins/builtins-wasm-gen.cc @@ -18,53 +18,48 @@ class WasmBuiltinsAssembler : public CodeStubAssembler { : CodeStubAssembler(state) {} protected: - TNode UncheckedParameter(int index) { - return UncheckedCast(Parameter(index)); - } - - TNode LoadInstanceFromFrame() { - return UncheckedCast( + TNode LoadInstanceFromFrame() { + return CAST( LoadFromParentFrame(WasmCompiledFrameConstants::kWasmInstanceOffset)); } - TNode LoadContextFromInstance(TNode instance) { - return UncheckedCast( - Load(MachineType::AnyTagged(), instance, - IntPtrConstant(WasmInstanceObject::kNativeContextOffset - - kHeapObjectTag))); + TNode LoadContextFromInstance(TNode instance) { + return CAST(Load(MachineType::AnyTagged(), instance, + IntPtrConstant(WasmInstanceObject::kNativeContextOffset - + kHeapObjectTag))); } }; TF_BUILTIN(WasmStackGuard, WasmBuiltinsAssembler) { - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); TailCallRuntime(Runtime::kWasmStackGuard, context); } TF_BUILTIN(WasmStackOverflow, WasmBuiltinsAssembler) { - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); TailCallRuntime(Runtime::kThrowWasmStackOverflow, context); } TF_BUILTIN(WasmThrow, WasmBuiltinsAssembler) { - TNode exception = UncheckedParameter(Descriptor::kException); - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode exception = CAST(Parameter(Descriptor::kException)); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); TailCallRuntime(Runtime::kThrow, context, exception); } TF_BUILTIN(WasmRethrow, WasmBuiltinsAssembler) { - TNode exception = UncheckedParameter(Descriptor::kException); - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode exception = CAST(Parameter(Descriptor::kException)); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); TailCallRuntime(Runtime::kReThrow, context, exception); } TF_BUILTIN(WasmTraceMemory, WasmBuiltinsAssembler) { - TNode info = UncheckedParameter(Descriptor::kMemoryTracingInfo); - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode info = CAST(Parameter(Descriptor::kMemoryTracingInfo)); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); TailCallRuntime(Runtime::kWasmTraceMemory, context, info); } @@ -73,14 +68,14 @@ TF_BUILTIN(WasmAtomicNotify, WasmBuiltinsAssembler) { UncheckedCast(Parameter(Descriptor::kAddress)); TNode count = UncheckedCast(Parameter(Descriptor::kCount)); - TNode instance = LoadInstanceFromFrame(); + TNode instance = LoadInstanceFromFrame(); TNode address_number = ChangeUint32ToTagged(address); TNode count_number = ChangeUint32ToTagged(count); - TNode context = LoadContextFromInstance(instance); + TNode context = LoadContextFromInstance(instance); TNode result_smi = - UncheckedCast(CallRuntime(Runtime::kWasmAtomicNotify, context, - instance, address_number, count_number)); + CAST(CallRuntime(Runtime::kWasmAtomicNotify, context, instance, + address_number, count_number)); Return(Unsigned(SmiToInt32(result_smi))); } @@ -92,15 +87,15 @@ TF_BUILTIN(WasmI32AtomicWait, WasmBuiltinsAssembler) { TNode timeout = UncheckedCast(Parameter(Descriptor::kTimeout)); - TNode instance = LoadInstanceFromFrame(); + TNode instance = LoadInstanceFromFrame(); TNode address_number = ChangeUint32ToTagged(address); TNode expected_value_number = ChangeInt32ToTagged(expected_value); TNode timeout_number = ChangeFloat64ToTagged(timeout); - TNode context = LoadContextFromInstance(instance); + TNode context = LoadContextFromInstance(instance); - TNode result_smi = UncheckedCast( - CallRuntime(Runtime::kWasmI32AtomicWait, context, instance, - address_number, expected_value_number, timeout_number)); + TNode result_smi = + CAST(CallRuntime(Runtime::kWasmI32AtomicWait, context, instance, + address_number, expected_value_number, timeout_number)); Return(Unsigned(SmiToInt32(result_smi))); } @@ -114,16 +109,16 @@ TF_BUILTIN(WasmI64AtomicWait, WasmBuiltinsAssembler) { TNode timeout = UncheckedCast(Parameter(Descriptor::kTimeout)); - TNode instance = LoadInstanceFromFrame(); + TNode instance = LoadInstanceFromFrame(); TNode address_number = ChangeUint32ToTagged(address); TNode expected_value_high_number = ChangeUint32ToTagged(expected_value_high); TNode expected_value_low_number = ChangeUint32ToTagged(expected_value_low); TNode timeout_number = ChangeFloat64ToTagged(timeout); - TNode context = LoadContextFromInstance(instance); + TNode context = LoadContextFromInstance(instance); - TNode result_smi = UncheckedCast(CallRuntime( + TNode result_smi = CAST(CallRuntime( Runtime::kWasmI64AtomicWait, context, instance, address_number, expected_value_high_number, expected_value_low_number, timeout_number)); Return(Unsigned(SmiToInt32(result_smi))); @@ -139,9 +134,9 @@ TF_BUILTIN(WasmMemoryGrow, WasmBuiltinsAssembler) { GotoIfNot(num_pages_fits_in_smi, &num_pages_out_of_range); TNode num_pages_smi = SmiFromInt32(num_pages); - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); - TNode ret_smi = UncheckedCast( + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); + TNode ret_smi = CAST( CallRuntime(Runtime::kWasmMemoryGrow, context, instance, num_pages_smi)); Return(SmiToInt32(ret_smi)); @@ -152,8 +147,8 @@ TF_BUILTIN(WasmMemoryGrow, WasmBuiltinsAssembler) { TF_BUILTIN(WasmTableGet, WasmBuiltinsAssembler) { TNode entry_index = UncheckedCast(Parameter(Descriptor::kEntryIndex)); - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); Label entry_index_out_of_range(this, Label::kDeferred); TNode entry_index_fits_in_smi = @@ -161,8 +156,7 @@ TF_BUILTIN(WasmTableGet, WasmBuiltinsAssembler) { GotoIfNot(entry_index_fits_in_smi, &entry_index_out_of_range); TNode entry_index_smi = SmiFromInt32(entry_index); - TNode table_index_smi = - UncheckedCast(Parameter(Descriptor::kTableIndex)); + TNode table_index_smi = CAST(Parameter(Descriptor::kTableIndex)); TailCallRuntime(Runtime::kWasmFunctionTableGet, context, instance, table_index_smi, entry_index_smi); @@ -177,8 +171,8 @@ TF_BUILTIN(WasmTableGet, WasmBuiltinsAssembler) { TF_BUILTIN(WasmTableSet, WasmBuiltinsAssembler) { TNode entry_index = UncheckedCast(Parameter(Descriptor::kEntryIndex)); - TNode instance = LoadInstanceFromFrame(); - TNode context = LoadContextFromInstance(instance); + TNode instance = LoadInstanceFromFrame(); + TNode context = LoadContextFromInstance(instance); Label entry_index_out_of_range(this, Label::kDeferred); TNode entry_index_fits_in_smi = @@ -186,9 +180,8 @@ TF_BUILTIN(WasmTableSet, WasmBuiltinsAssembler) { GotoIfNot(entry_index_fits_in_smi, &entry_index_out_of_range); TNode entry_index_smi = SmiFromInt32(entry_index); - TNode table_index_smi = - UncheckedCast(Parameter(Descriptor::kTableIndex)); - TNode value = UncheckedCast(Parameter(Descriptor::kValue)); + TNode table_index_smi = CAST(Parameter(Descriptor::kTableIndex)); + TNode value = CAST(Parameter(Descriptor::kValue)); TailCallRuntime(Runtime::kWasmFunctionTableSet, context, instance, table_index_smi, entry_index_smi, value); @@ -199,17 +192,17 @@ TF_BUILTIN(WasmTableSet, WasmBuiltinsAssembler) { SmiConstant(static_cast(message_id))); } -#define DECLARE_ENUM(name) \ - TF_BUILTIN(ThrowWasm##name, WasmBuiltinsAssembler) { \ - TNode instance = LoadInstanceFromFrame(); \ - TNode context = LoadContextFromInstance(instance); \ - MessageTemplate message_id = \ - wasm::WasmOpcodes::TrapReasonToMessageId(wasm::k##name); \ - TailCallRuntime(Runtime::kThrowWasmError, context, \ - SmiConstant(static_cast(message_id))); \ +#define DECLARE_THROW_RUNTIME_FN(name) \ + TF_BUILTIN(ThrowWasm##name, WasmBuiltinsAssembler) { \ + TNode instance = LoadInstanceFromFrame(); \ + TNode context = LoadContextFromInstance(instance); \ + MessageTemplate message_id = \ + wasm::WasmOpcodes::TrapReasonToMessageId(wasm::k##name); \ + TailCallRuntime(Runtime::kThrowWasmError, context, \ + SmiConstant(static_cast(message_id))); \ } -FOREACH_WASM_TRAPREASON(DECLARE_ENUM) -#undef DECLARE_ENUM +FOREACH_WASM_TRAPREASON(DECLARE_THROW_RUNTIME_FN) +#undef DECLARE_THROW_RUNTIME_FN } // namespace internal } // namespace v8