rust-mobile / rust-mobile/ndk

Weird/UB `ndk` APIs found by removing NIHisms in `android-activity`

Open
#478 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.3k
Forks
125
PR merge metrics
No merged PRs in 30d

Description

android-activity open-codes a lot of ndk-provided API, directly on top of native functions and symbols from ndk-sys. While attempting to remove most of it to simplify and clear up the implementation with safer Rust, I found that the ndk could do some things quite a bit better, even if only by documenting expectations. Perhaps this is why certain things were open-coded in the first place, besides being ported from raw C code.

Since the ndk crate is quite old by now, and has seen a lot of "Just Make it Work™", things like lifetime management and correctness when it comes to safety/UB have not always been kept in mind while writing bindings. Instead of creating a lot of separate issues (or PRs) right now, I am using this issue as a public scratch-pad to keep track of questions and ideas that need to tracked and looked into later.

  • NativeActivity returns &Paths from raw pointers: could these ever be null()/None?
  • NativeActivity::asset_manager() has no lifetime: this should be bound to &self or is it "Application-global"?
  • InputQueue has no lifetime and no Drop/Clone: document its lifetime guarantees and return borrows where necessary?
    Its lifetime is tied to the onInputQueueDestroyed callback whose docs describe that:

    You should no longer try to reference this object upon returning from this function.

  • ForeignLooper (and various other structures, see https://github.com/rust-mobile/ndk/issues/309) cannot be created without lifetime management, i.e. to temporarily borrow/handle the pointer safely while it is valid inside a callback.
    For lack of unsafe fn borrow_from_ptr(NonNull...) -> &'unknown Self (impossible to write in Rust anyway), it seems most convenient to call let looper = ManuallyDrop::new(ForeignLooper::from_ptr(looper)); now.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the NativeActivity, InputQueue, and ForeignLooper APIs described in the issue, then read the linked issue #309 and the Android onInputQueueDestroyed callback documentation. This is a scratch-pad of questions rather than a scoped change; done would require turning an agreed API or safety concern into a separate, actionable issue or PR.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, rust
Domain
mobile-dev, operating-systems
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.