Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dart:ffi asFunction / lookupFunction static function vs instance method #35903

Closed
dcharkes opened this issue Feb 11, 2019 · 4 comments
Closed
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi

Comments

@dcharkes
Copy link
Contributor

Currently Pointer.asFunction and DynamicLibrary.lookup are instance methods.
However, this enables users to call these dynamically.

Pointer<Int8 Function(Int8)> p = fromAddress(memoryAddress);
dynamic q = p;
int Function(int) f = q.asFunction();
f(123);

Unfortunately we cannot at compile-time discover this, which means that we cannot generate trampolines in AOT mode.

We have two possible solutions:

  1. throw a runtime exception on a dynamic invocation
  2. make these functions static: f = asFunction(q)

The second option is a bit less ergonomic, but safer.

@dcharkes dcharkes added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Feb 11, 2019
@dcharkes
Copy link
Contributor Author

@lrhn @mraleph

@lrhn
Copy link
Member

lrhn commented Mar 12, 2019

Let it crash? Or throw in asFunction if the function is not available?

If this was done using static extension types instead then dynamic invocation would not work. Much of the ffi functionality is compiler magic that works similar to this, so I'd not be worried if it just doesn't work when used through dynamic. It's already so very magical.

The trampoline you create, is it for a particular function, or can it be reused for multiple functions with the same type, just with different native addresses? If it was the latter, you could create it when you see Pointer<Int8 Function(Int8)>.

@sjindel-google
Copy link
Contributor

We will keep using instance methods for now and use extension methods once available, to ease migration. (Throwing an exception on a dynamic call).

@dcharkes
Copy link
Contributor Author

This should be addressed now that we have extension methods.

@dcharkes dcharkes reopened this Feb 12, 2020
dart-bot pushed a commit that referenced this issue Feb 14, 2020
This prevents them from being called dynamically.
Moreover, it prevents asFunction from being called on a non-NativeFunction type argument, simplifying the amount of manual checks.

Note that this CL had to change the CFE and analzyer, and their tests (including mock_sdk) as well.

This can potentially be a breaking change, as the extension methods are only visible when `dart:ffi` is imported, while methods on objects are always visible.

Issue: #35903

Change-Id: I1e291f154228d5d9a34b21a022088bf493f6557d
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-dartkb-linux-release-x64-abi-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,analyzer-nnbd-linux-release-try,dart2js-nnbd-linux-x64-chrome-try,ddc-nnbd-linux-release-chrome-try,front-end-nnbd-linux-release-x64-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-nnbd-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135463
Commit-Queue: Daco Harkes <[email protected]>
Reviewed-by: Martin Kustermann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi
Projects
None yet
Development

No branches or pull requests

3 participants