dart-sdk/tests/ffi
Daco Harkes b113fffb65 Reland "[vm/ffi] Throw on returning Error in Handle"
Fix: Check handle contents for Smi.
Closes: https://github.com/flutter/flutter/issues/112726

Orignal CL description:

Makes `Dart_Handle` FFI returns behave as the following snippet:

```
Dart_Handle ExampleSnippet() {
  Dart_Handle result = ...;
  if (Dart_IsError(result)) {
    Dart_PropagateError(result);
  }
  return result;
}
```

Also makes FFI consistent with Dart_NativeFunctions, which will
automatically throw upon return if Dart_SetReturnValue set the result
to an error.

`UnhandledExceptions` cannot flow out into Dart generated code. So,
the implementation needs to be in `FfiCallInstr::EmitNativeCode`.

Using `Dart_IsError` is slow compared to a machine code class id
check. So, we should do the handle unwrapping and class id check in
machine code.

Unwrapping Handles in machine code is only safe when the GC is
guaranteed to not run: Either (1) in `kThreadInGenerated`, or (2) in
`kThreadInNative`, but only when transitioned into safepoint. So, the
handle cannot be unwrapped immediately after the FFI call in machine code. We first need to transition back to generated.

This means we need to transition again to native to do the actual
`Dart_PropagateError` call. We can do so without the stub in JIT
because we never return with normal control flow.

Performance impact of this change is within benchmark noise in both
JIT and AOT.
Size impact is 42 bytes on x64, which is 10% in AOT and 12% in JIT.

For more numbers see: go/dart-ffi-handle-error

TEST=runtime/bin/ffi_test/ffi_test_functions_vmspecific.cc
TEST=tests/ffi/vmspecific_handle_test.dart

Closes: https://github.com/dart-lang/sdk/issues/49936
Change-Id: Id8edfd841a7d6246438386007d83747868a0a151
Cq-Include-Trybots: luci.dart.try:vm-canary-linux-debug-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-debug-arm-try,vm-kernel-gcc-linux-try,vm-kernel-linux-debug-x64-try,vm-kernel-linux-debug-x64c-try,vm-kernel-msvc-windows-try,vm-kernel-optcounter-threshold-linux-release-x64-try,vm-kernel-precomp-asan-linux-release-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-tsan-linux-release-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-precomp-ffi-qemu-linux-release-riscv64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-mac-release-arm64-try,vm-kernel-precomp-win-debug-x64c-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/262342
Reviewed-by: Martin Kustermann <kustermann@google.com>
Auto-Submit: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
2022-10-03 12:25:08 +00:00
..
generator Fixed various typos in a lot of files 2022-07-25 12:21:59 +00:00
abi_specific_int_incomplete_aot_test.dart
abi_specific_int_incomplete_jit_test.dart
abi_specific_int_test.dart
abi_test.dart
aliasing_test.dart
all_positive.dart
allocator_test.dart
analysis_options.yaml
bool_test.dart
c_types_test.dart
callback_tests_utils.dart
calloc_test.dart
coordinate.dart
coordinate_nnbd_workaround.dart
data_not_asan_test.dart
data_test.dart
dylib_isolates_test.dart
dylib_utils.dart
expando_test.dart
extension_methods_test.dart
external_typed_data_test.dart
ffi.status [vm, compiler] Inform MSan about parameters coming from FFI. 2022-09-23 21:31:14 +00:00
ffi_native_test.dart [cfe/ffi] Transform FfiNatives in a single pass 2022-07-08 12:36:09 +00:00
ffi_test_helpers.dart
function_callbacks_many_test.dart
function_callbacks_structs_by_value_generated_test.dart
function_callbacks_structs_by_value_test.dart
function_callbacks_test.dart [vm] Support serialization of FFI callbacks in IL serialization 2022-08-26 17:37:21 +00:00
function_callbacks_very_many_test.dart
function_structs_by_value_generated_compounds.dart
function_structs_by_value_generated_leaf_test.dart
function_structs_by_value_generated_test.dart
function_structs_test.dart Fixed various typos in a lot of files 2022-07-25 12:21:59 +00:00
function_test.dart
function_very_many_test.dart
hardfp_test.dart
has_symbol_test.dart [vm/ffi] Support DynamicLibrary.process() on Windows 2022-09-27 08:42:03 +00:00
inline_array_multi_dimensional_test.dart
inline_array_test.dart
native_effect_test.dart
negative_function_test.dart
regress_37254_test.dart
regress_39044_test.dart
regress_39063_test.dart
regress_39885_test.dart
regress_40537_test.dart Fixed various typos in a lot of files 2022-07-25 12:21:59 +00:00
regress_43016_test.dart
regress_43693_test.dart
regress_44985_test.dart
regress_44986_test.dart
regress_45189_test.dart
regress_45198_test.dart
regress_45507_test.dart
regress_45988_test.dart
regress_46004_test.dart
regress_46085_test.dart
regress_46127_test.dart
regress_47594_test.dart
regress_47673_2_test.dart
regress_47673_test.dart
regress_49402_test.dart [vm/ffi] Fix constant Finalizables 2022-07-07 08:39:21 +00:00
regress_49684_test.dart [analyzer/ffi] Fix FfiNative Pointer params 2022-09-27 15:58:16 +00:00
regress_flutter79441_test.dart
regress_flutter97301_test.dart
regress_jump_to_frame_test.dart
sizeof_test.dart
snapshot_test.dart
stacktrace_regress_37910_test.dart
structs_nested_test.dart
structs_nnbd_workaround_test.dart
structs_packed_test.dart
structs_test.dart
unaligned_test.dart
variance_function_test.dart Fix typos 2022-09-16 19:35:00 +00:00
very_large_struct.dart
vmspecific_dynamic_library_test.dart [vm/ffi] Support DynamicLibrary.process() on Windows 2022-09-27 08:42:03 +00:00
vmspecific_enable_ffi_test.dart
vmspecific_ffi_native_test.dart [vm/ffi] NativeFieldWrapper FfiNative check receivers for nullptr 2022-08-11 17:39:57 +00:00
vmspecific_function_callbacks_exit_test.dart
vmspecific_function_callbacks_negative_test.dart
vmspecific_function_callbacks_test.dart
vmspecific_function_gc_test.dart
vmspecific_function_test.dart
vmspecific_handle_dynamically_linked_test.dart
vmspecific_handle_test.dart Reland "[vm/ffi] Throw on returning Error in Handle" 2022-10-03 12:25:08 +00:00
vmspecific_highmem_32bit_test.dart
vmspecific_leaf_call_test.dart
vmspecific_native_finalizer_2_test.dart
vmspecific_native_finalizer_isolate_groups_test.dart
vmspecific_native_finalizer_isolates_test.dart
vmspecific_native_finalizer_test.dart
vmspecific_object_gc_test.dart
vmspecific_regress_37100_test.dart
vmspecific_regress_37511_callbacks_test.dart
vmspecific_regress_37511_test.dart
vmspecific_regress_37780_test.dart
vmspecific_regress_38993_test.dart
vmspecific_send_port_id_test.dart
vmspecific_static_checks_ffinative_test.dart Reland "[cfe/ffi] Improve FFI call mismatched types compile errors" 2022-08-01 16:09:03 +00:00
vmspecific_static_checks_test.dart Reland "[cfe/ffi] Improve FFI call mismatched types compile errors" 2022-08-01 16:09:03 +00:00
vmspecific_variance_function_checks_test.dart Fix typos 2022-09-16 19:35:00 +00:00