-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[vm/ffi] Support .address.cast()
in leaf calls
#55971
Comments
Hey @dcharkes, so we have to change the data type of |
Hi @codesculpture! No, the |
Hi @dcharkes , i think i don't understand the problem
can you please elaborate above with an example :) |
The following function in C size_t fread( void *buffer, size_t size, size_t count,
FILE *stream ); yields the following bindings with @Native<Size Function(Pointer<Void>, Size, Size, Pointer<File>)
external int fread(Pointer<Void> buffer, int size, int count, Pointer<File> stream); But if you want to use for |
Sorry again @dcharkes , thanks for explaining the problem, i understood the problem, but what is the solution you are suggesting? Thanks |
The following code should work: @Native<Size Function(Pointer<Void>, Size, Size, Pointer<Void>)>()
external int fread(
Pointer<Void> buffer, int size, int count, Pointer<Void> stream);
void main() {
final buffer = Uint8List.fromList([1, 2, 3]);
fread(buffer.address.cast(), 3, 3, nullptr);
} Currently it is rejected because |
@dcharkes am trying to debug the https://github.com/dart-lang/sdk/blob/05d9e5bf37a68ec75f8ffc56a65a89e0e86b5a1d/pkg%2Fvm%2Flib%2Fmodular%2Ftransformations%2Fffi%2Fuse_sites.dart, but i cannot find a way to do so, i already asked a related question in dart discord server long ago, what would be ideal solution to debug the internal dart codes (now am adding print statements, that would require re building dart, for every changes) |
If you're in VSCode, you can run the CFE like this in the debugger:
|
Hey @dcharkes , are we going to allow |
Also @dcharkes, one small doubt is that |
Yes, this is already what is happening: sdk/pkg/vm/lib/modular/transformations/ffi/use_sites.dart Lines 1521 to 1530 in e9c7b2b
The sdk/runtime/vm/compiler/backend/il.cc Lines 7589 to 7599 in e9c7b2b
|
This should be allowable as well. 👍 |
this is requested here dart-lang#55971
And also i have no idea about whether to change does fileOffset is line number or character's position in the file? |
Yes, that's the right approach!
|
@dcharkes , where can i write test for this? |
You can take a look at the original PR: https://dart-review.googlesource.com/c/sdk/+/360882
You'll probably also need to change the analyzer to allow the cast:
|
Hey @dcharkes , can you explain how "--platform" argument is making possible debugging, the reason why am asking is, i need to debug the |
If you're using vscode: {
"name": "dart analyzer.dart",
"type": "dart",
"request": "launch",
"program": "pkg/analyzer_cli/bin/analyzer.dart",
"args": [
"tests/ffi/static_checks/vmspecific_static_checks_array_test.dart",
],
"toolArgs": [],
"enableAsserts": true,
"cwd": "${workspaceFolder}",
}, For the transformation tests: {
"name": "dart pkg/vm/test/transformations/ffi_test.dart",
"type": "dart",
"request": "launch",
"program": "pkg/vm/test/transformations/ffi_test.dart",
"args": [
"compound_copies",
],
"toolArgs": [
"-DupdateExpectations=true",
],
"enableAsserts": true,
"cwd": "${workspaceFolder}",
}, |
https://discord.com/channels/608014603317936148/608022273152122881/1268987825081028638 Please help if u can @dcharkes Thanks Edit: mraleph helped me. |
void main() {
final myStruct = Struct.create<MyStruct>();
myNative(
myStruct.a.address,
myStruct.b.address, // It expected to return Pointer<Int>, but it returning Pointer<Never>
);
}
@Native<
Void Function(
Pointer<Int8>,
Pointer<Void>,
)>(isLeaf: true)
external void myNative(
Pointer<Int8> pointer,
Pointer<Void> pointer2,
);
final class MyStruct extends Struct {
@Int8()
external int a;
@Int8()
external int b;
} Does address of any members from I expected analyzer throw error for above program, but it dint thrown any error. That made me to ask this question |
Seems members of |
@dcharkes , Here |
Also is the tests in |
It's a The
You can probably write the handful of tests manually. The test generators are set up to produce correct types. 😄 Just make sure to cover some obvious different cases:
No, those are written by hand. The old fashioned way! In general, I only write a test generator if I have many tests in exactly the same structure but with one thing varying. (E.g. the FFI call tests all pass complex parameters and sum all the individual ints and then return it. And the one thing varying is what the parameter list is.) |
But the tests in |
$ tools/build.py -mrelease runtime create_platform_sdk && tools/test.py -mrelease -cfasta tests/ffi/static_checks/address_of_cast.dart I created a new file called
Any idea on this @dcharkes Edit: My bad i dint added |
@dcharkes , I pushed the tests kindly review and seems you need to press some button to run the tests again ? |
Yep, I'll happily hit that button. Just ping me. Also left another round of review comments! We've got some missing test cases but I think we're almost there! 👍 |
Yup, on it. Thanks! |
Hey @dcharkes, There is 2 errors being thrown for this snippet @Native<Void Function(Pointer<Void>)>(isLeaf: true)
external void myNative(Pointer<Void> buffer);
void main() {
final typedData = Int8List.fromList([1, 2]);
myNative(typedData.address);
} Output of tests/ffi/static_checks/address_of_cast_test.dart:50:22: Error: The argument type 'Pointer<Int8>' can't be assigned to the parameter type 'Pointer<Void>'.
- 'Pointer' is from 'dart:ffi'.
- 'Int8' is from 'dart:ffi'.
- 'Void' is from 'dart:ffi'.
myNative(typedData.address);
^
tests/ffi/static_checks/address_of_cast_test.dart:50:22: Error: The '.address' expression can only be used as argument to a leaf native external call.
myNative(typedData.address); The second error is not supposed to be thrown, and its not reported by analyzer (confirmed that by running I tested this on 3.5.0-292.0.dev, seems this is a existing behavior. |
I resolved all comments except this https://dart-review.googlesource.com/c/sdk/+/378221/comment/0a497574_518e78f1/ Need your attention on the above comment. |
I've filed #56462. Just expect both errors for now and add a comment that refers to the bug. We can fix that in another PR. |
Hey @dcharkes , https://logs.chromium.org/logs/dart/buildbucket/cr-buildbucket/8739796480788876289/+/u/test_results/new_test_failures__logs_
It tells me to run above command, but it throwing error on some tests But i run this as u suugest previously Am i missing something ? :( |
Edit: Also, try rebuilding the full SDK just in case (Some command in my terminal history on Mac: |
@dcharkes , i did ran the above locally but all i got no changes in .expect files and all tests ran successfully. |
Can there be any problem that i dint synced up with latest commits(i think am too behind) comparing upstream? (I cannot find other reason for tests failing) |
This looks like there might be like a new constant somewhere in the program. That could indeed be that you are too behind. You can try rebasing/merging your PR. If that doesn't work, I can try it locally here. |
I did synced with upstream Just giving this info just in case, if i done something which makes some issue. |
@dcharkes you can vote commit queue to trigger tests, copybara synced my changes. |
I will have some questions if the tests passes after i synced with upstream, I assumed testcases and building dart are all run based on my HEAD(base) (which might not synced with upstream in my case) So my HEAD (which is here my branch), would not contain any synced source code or testcases. So how then while ci run tests and able to produce different expectations. It can produce different expectations if it the source code changed (might get synced) but i assume it cannot, is it? |
I should ran But Hoping that static_checks tests should pass now. |
Gotcha, |
I never actually ran
where Btw i ran tests with |
I now passed Wrong full snapshot version, expected xxx, found yyy I am getting this while running tests on dart2analyzer compiler locally (also am passing --use-sdk) . Any idea ? Thanks. |
What OS and hardware are you working on? Windows x64?
|
This can be closed @dcharkes |
Thanks for contributing! 🚀 I've written https://github.com/dart-lang/sdk/blob/main/runtime/docs/contributing_to_dart_ffi.md to answer some of the frequently asked questions. Maybe you can take a look at it and make a PR for the things that are missing for the next new contributor! |
Thanks for doing this documentation, looking forward to contribute more ! |
Many functions with buffers take a
void*
, for example https://en.cppreference.com/w/c/io/fread.However, using a
Uint8List
and.address
yields aPointer<Uint8>
.It would be useful to be able to pass
uint8list.address.cast()
. That would prevent having to modify the FFIgen generated function signature.Thanks for the suggestion @brianquinlan!
For anyone willing to contribute, the implementation is in:
sdk/pkg/vm/lib/modular/transformations/ffi/use_sites.dart
Lines 1489 to 1504 in 05d9e5b
The implementation should find the
cast()
invocation and use its receiver instead (effectively ignoring the cast expression as a no-op).The text was updated successfully, but these errors were encountered: