dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.57k forks source link

[vm/ffi] Support `.address.cast()` in leaf calls #55971

Closed dcharkes closed 2 months ago

dcharkes commented 4 months ago

Many functions with buffers take a void*, for example https://en.cppreference.com/w/c/io/fread.

However, using a Uint8List and .address yields a Pointer<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: https://github.com/dart-lang/sdk/blob/05d9e5bf37a68ec75f8ffc56a65a89e0e86b5a1d/pkg/vm/lib/modular/transformations/ffi/use_sites.dart#L1489-L1504 The implementation should find the cast() invocation and use its receiver instead (effectively ignoring the cast expression as a no-op).

codesculpture commented 2 months ago

I should rantransformations/ffi_test.dart to see whether it producing different .expect files with latest commits from upstream before submitting to gerrit. Sorry @dcharkes , am doing that now.

But Hoping that static_checks tests should pass now.

codesculpture commented 2 months ago

Gotcha, .expect file changed with latest changes. Pushed the changes. But now there is a one more bad boy remaining, tests/ffi/static_checks/address_of_cast_test is failing.

codesculpture commented 2 months ago

I never actually ran $ tools/test.py -mrelease -cdart2analyzer tests/ffi/static_checks/address_of_cast_test.dart Because it simply throws

Error: ProcessException: The system cannot find the file specified.
Command: sdk\bin\dart.exe --packages=.dart_tool/package_config.json pkg/analyzer_cli/bin/analyzer.dart --use-analysis-driver-memory-byte-store --dart-sdk=<location>/dart/sdk/sdk --batch

where --dart-sdk=<location>/dart/sdk/sdk sdk is repeated twice, seems that the reason it cannot find file ?

Btw i ran tests with -cfasta and those are passed locally.
i think i need to make the tests pass with dart2analyzer locally and i might find the reason for the issue ?

@dcharkes

codesculpture commented 2 months ago

I never actually ran $ tools/test.py -mrelease -cdart2analyzer tests/ffi/static_checks/address_of_cast_test.dart Because it simply throws

Error: ProcessException: The system cannot find the file specified.
Command: sdk\bin\dart.exe --packages=.dart_tool/package_config.json pkg/analyzer_cli/bin/analyzer.dart --use-analysis-driver-memory-byte-store --dart-sdk=<location>/dart/sdk/sdk --batch

where --dart-sdk=<location>/dart/sdk/sdk sdk is repeated twice, seems that the reason it cannot find file ?

Btw i ran tests with -cfasta and those are passed locally.
i think i need to make the tests pass with dart2analyzer locally and i might find the reason for the issue ?

@dcharkes

I now passed --use-sdk flag to locate the dart sdk in the right path which no longer the above error occurs. But now facing this error which i posted on here

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.

codesculpture commented 2 months ago

@dcharkes Need help here https://dart-review.googlesource.com/c/sdk/+/378221/comment/7a60c8c5_6570eaf1/

dcharkes commented 2 months ago

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?

$ python3 tools/build.py -mrelease -ax64 create_sdk runtime should ensure everything is built (note create_sdk can take quite a while). You can delete the whole out/ReleaseX64/ directory to ensure it's a clean build.

codesculpture commented 2 months ago

This can be closed @dcharkes Thanks a very lot for literally answering my every silly questions, thanks for your time. Really appreciate it.

dcharkes commented 2 months ago

This can be closed @dcharkes Thanks a very lot for literally answering my every silly questions, thanks for your time. Really appreciate it.

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!

codesculpture commented 2 months ago

Thanks for doing this documentation, looking forward to contribute more !