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

Add workaround for JNA bug when passing small structs by value. #335

Merged
merged 1 commit into from
Oct 26, 2020

Conversation

rfk
Copy link
Collaborator

@rfk rfk commented Oct 26, 2020

The current version of JNA appears to have a bug when passing small structs
as arguments by value, on arm64 platforms, when the function call has too
many arguments to pass the struct in registers. It turns out that our generated
code does this a lot thanks to the way we pass around RustBuffer structs.

This commit adds a temporary workaround for the issue, by padding our structs
to be larger than 16 bytes, which causes them to be passed as pointers rather
than by value.

See mozilla/application-services#3646 for investigation
of the JNA issue, and #334 to track
the work of removing this workaround once a fix is released in upstream JNA.

The current version of JNA appears to have a bug when passing small structs
as arguments by value, on arm64 platforms, when the function call has too
many arguments to pass the struct in registers. It turns out that our generated
code does this a lot thanks to the way we pass around `RustBuffer` structs.

This commit adds a temporary workaround for the issue, by padding our structs
to be larger than 16 bytes, which causes them to be passed as pointers rather
than by value.

See mozilla/application-services#3646 for investigation
of the JNA issue, and #334 to track
the work of removing this workaround once a fix is released in upstream JNA.
@rfk rfk force-pushed the jna-arm64-workaround branch from 2f03a37 to ab7a447 Compare October 26, 2020 04:05
@rfk
Copy link
Collaborator Author

rfk commented Oct 26, 2020

@travis79 I made a local build of the glean+nimbus sample app using this patched version of uniffi, and AFAICT it resolves the arm64 bug I was seeing with invalid memory reads. Any chance you could try this out on your device that was also experiencing a crasher, and see if it works for you? You'd need to:

  • Configure your nimbus-sdk rust project to use this version of uniffi, e.g. using a git commit dependency.
  • Install this version as uniffi-bindgen in your path (so it can be found by the gradle script to generate Kotlin bindings.

I'm confident enough that this resolves a real issue that I think we should proceed with merging and cutting a new uniffi release that includes it, but it would be worth getting confirmation from your device as well for completeness.

@rfk rfk marked this pull request as ready for review October 26, 2020 05:17
@rfk rfk requested a review from eoger October 26, 2020 05:17
@rfk
Copy link
Collaborator Author

rfk commented Oct 26, 2020

Here's the concrete repro of the bug, for context: https://github.com/rfk/arm64-jna-structbug

@rfk
Copy link
Collaborator Author

rfk commented Oct 26, 2020

Also for context, the upstream bug report: java-native-access/jna#1259

Copy link
Contributor

@eoger eoger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man this is a pretty bad bug, good job investigating it!

@travis79
Copy link
Member

@travis79 I made a local build of the glean+nimbus sample app using this patched version of uniffi, and AFAICT it resolves the arm64 bug I was seeing with invalid memory reads. Any chance you could try this out on your device that was also experiencing a crasher, and see if it works for you? You'd need to:

* Configure your nimbus-sdk rust project to use this version of uniffi, e.g. using a [git commit dependency](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories).

* Install this version as `uniffi-bindgen` in your path (so it can be found by the gradle script to generate Kotlin bindings.

I'm confident enough that this resolves a real issue that I think we should proceed with merging and cutting a new uniffi release that includes it, but it would be worth getting confirmation from your device as well for completeness.

This is a little non-trivial to test locally because the application I was testing this in was depending on the nimbus in the appservices megazord. Let me see if I can build and publish the megazord locally. I trust your judgement that this resolves the issue if we want to go ahead and merge this, it might make it a little easier for me to confirm if it percolated down to the prototype megazord with nimbus in it.

@rfk rfk merged commit 937f9b7 into main Oct 26, 2020
@rfk rfk deleted the jna-arm64-workaround branch October 26, 2020 22:25
@rfk rfk mentioned this pull request Oct 26, 2020
@rfk
Copy link
Collaborator Author

rfk commented Oct 27, 2020

@travis79 FYI the 0.4.0 release is out with this fix included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants