[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 <mstarzinger@chromium.org>
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65236}
This commit is contained in:
Michael Starzinger 2019-11-28 14:05:12 +01:00 committed by Commit Bot
parent 5b156c5548
commit d03802c6d2

View File

@ -18,53 +18,48 @@ class WasmBuiltinsAssembler : public CodeStubAssembler {
: CodeStubAssembler(state) {}
protected:
TNode<Object> UncheckedParameter(int index) {
return UncheckedCast<Object>(Parameter(index));
}
TNode<Object> LoadInstanceFromFrame() {
return UncheckedCast<Object>(
TNode<WasmInstanceObject> LoadInstanceFromFrame() {
return CAST(
LoadFromParentFrame(WasmCompiledFrameConstants::kWasmInstanceOffset));
}
TNode<Object> LoadContextFromInstance(TNode<Object> instance) {
return UncheckedCast<Object>(
Load(MachineType::AnyTagged(), instance,
IntPtrConstant(WasmInstanceObject::kNativeContextOffset -
kHeapObjectTag)));
TNode<Context> LoadContextFromInstance(TNode<WasmInstanceObject> instance) {
return CAST(Load(MachineType::AnyTagged(), instance,
IntPtrConstant(WasmInstanceObject::kNativeContextOffset -
kHeapObjectTag)));
}
};
TF_BUILTIN(WasmStackGuard, WasmBuiltinsAssembler) {
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
TailCallRuntime(Runtime::kWasmStackGuard, context);
}
TF_BUILTIN(WasmStackOverflow, WasmBuiltinsAssembler) {
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
TailCallRuntime(Runtime::kThrowWasmStackOverflow, context);
}
TF_BUILTIN(WasmThrow, WasmBuiltinsAssembler) {
TNode<Object> exception = UncheckedParameter(Descriptor::kException);
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Object> exception = CAST(Parameter(Descriptor::kException));
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
TailCallRuntime(Runtime::kThrow, context, exception);
}
TF_BUILTIN(WasmRethrow, WasmBuiltinsAssembler) {
TNode<Object> exception = UncheckedParameter(Descriptor::kException);
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Object> exception = CAST(Parameter(Descriptor::kException));
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
TailCallRuntime(Runtime::kReThrow, context, exception);
}
TF_BUILTIN(WasmTraceMemory, WasmBuiltinsAssembler) {
TNode<Object> info = UncheckedParameter(Descriptor::kMemoryTracingInfo);
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Smi> info = CAST(Parameter(Descriptor::kMemoryTracingInfo));
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
TailCallRuntime(Runtime::kWasmTraceMemory, context, info);
}
@ -73,14 +68,14 @@ TF_BUILTIN(WasmAtomicNotify, WasmBuiltinsAssembler) {
UncheckedCast<Uint32T>(Parameter(Descriptor::kAddress));
TNode<Uint32T> count = UncheckedCast<Uint32T>(Parameter(Descriptor::kCount));
TNode<Object> instance = LoadInstanceFromFrame();
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Number> address_number = ChangeUint32ToTagged(address);
TNode<Number> count_number = ChangeUint32ToTagged(count);
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Context> context = LoadContextFromInstance(instance);
TNode<Smi> result_smi =
UncheckedCast<Smi>(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<Float64T> timeout =
UncheckedCast<Float64T>(Parameter(Descriptor::kTimeout));
TNode<Object> instance = LoadInstanceFromFrame();
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Number> address_number = ChangeUint32ToTagged(address);
TNode<Number> expected_value_number = ChangeInt32ToTagged(expected_value);
TNode<Number> timeout_number = ChangeFloat64ToTagged(timeout);
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Context> context = LoadContextFromInstance(instance);
TNode<Smi> result_smi = UncheckedCast<Smi>(
CallRuntime(Runtime::kWasmI32AtomicWait, context, instance,
address_number, expected_value_number, timeout_number));
TNode<Smi> 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<Float64T> timeout =
UncheckedCast<Float64T>(Parameter(Descriptor::kTimeout));
TNode<Object> instance = LoadInstanceFromFrame();
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Number> address_number = ChangeUint32ToTagged(address);
TNode<Number> expected_value_high_number =
ChangeUint32ToTagged(expected_value_high);
TNode<Number> expected_value_low_number =
ChangeUint32ToTagged(expected_value_low);
TNode<Number> timeout_number = ChangeFloat64ToTagged(timeout);
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Context> context = LoadContextFromInstance(instance);
TNode<Smi> result_smi = UncheckedCast<Smi>(CallRuntime(
TNode<Smi> 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<Smi> num_pages_smi = SmiFromInt32(num_pages);
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<Smi> ret_smi = UncheckedCast<Smi>(
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
TNode<Smi> 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<Int32T> entry_index =
UncheckedCast<Int32T>(Parameter(Descriptor::kEntryIndex));
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
Label entry_index_out_of_range(this, Label::kDeferred);
TNode<BoolT> 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<Smi> entry_index_smi = SmiFromInt32(entry_index);
TNode<Smi> table_index_smi =
UncheckedCast<Smi>(Parameter(Descriptor::kTableIndex));
TNode<Smi> 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<Int32T> entry_index =
UncheckedCast<Int32T>(Parameter(Descriptor::kEntryIndex));
TNode<Object> instance = LoadInstanceFromFrame();
TNode<Object> context = LoadContextFromInstance(instance);
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
TNode<Context> context = LoadContextFromInstance(instance);
Label entry_index_out_of_range(this, Label::kDeferred);
TNode<BoolT> 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<Smi> entry_index_smi = SmiFromInt32(entry_index);
TNode<Smi> table_index_smi =
UncheckedCast<Smi>(Parameter(Descriptor::kTableIndex));
TNode<Object> value = UncheckedCast<Object>(Parameter(Descriptor::kValue));
TNode<Smi> table_index_smi = CAST(Parameter(Descriptor::kTableIndex));
TNode<Object> 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<int>(message_id)));
}
#define DECLARE_ENUM(name) \
TF_BUILTIN(ThrowWasm##name, WasmBuiltinsAssembler) { \
TNode<Object> instance = LoadInstanceFromFrame(); \
TNode<Object> context = LoadContextFromInstance(instance); \
MessageTemplate message_id = \
wasm::WasmOpcodes::TrapReasonToMessageId(wasm::k##name); \
TailCallRuntime(Runtime::kThrowWasmError, context, \
SmiConstant(static_cast<int>(message_id))); \
#define DECLARE_THROW_RUNTIME_FN(name) \
TF_BUILTIN(ThrowWasm##name, WasmBuiltinsAssembler) { \
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame(); \
TNode<Context> context = LoadContextFromInstance(instance); \
MessageTemplate message_id = \
wasm::WasmOpcodes::TrapReasonToMessageId(wasm::k##name); \
TailCallRuntime(Runtime::kThrowWasmError, context, \
SmiConstant(static_cast<int>(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