Skip to content

Add support for target_os = android#25

Merged
lucab merged 1 commit into
lucab:masterfrom
flxo:pr-android
Sep 6, 2021
Merged

Add support for target_os = android#25
lucab merged 1 commit into
lucab:masterfrom
flxo:pr-android

Conversation

@flxo

@flxo flxo commented Jul 28, 2021

Copy link
Copy Markdown
Contributor

Android supports memfds. Add target_os = android to the conditional compilation flags. Replace the use of raw libc::syscallwith a more genericlibc::fcntl` calls.

Test are fine on a AOSP Android 9.0:

~/memfd-rs ‹pr-android*› cargo test --target aarch64-linux-android                                     
   Compiling memfd v0.4.1-alpha.0 (/home/felix/memfd-rs)
    Finished test [unoptimized + debuginfo] target(s) in 0.49s
     Running unittests (target/aarch64-linux-android/debug/deps/memfd-9f7fbfff9cfff8d3)
remount succeeded
+ adb push /home/felix/memfd-rs/target/aarch64-linux-android/debug/deps/memfd-9f7fbfff9cfff8d3 /data
/home/felix/memfd-rs/target/aarch64-linux-android/debug/deps/memfd-9f7fbfff9cfff8d3: 1 file pushed. 21.4 MB/s (5790168 bytes in 0.258s)
+ adb shell /data/memfd-9f7fbfff9cfff8d3

running 1 test
test errors::error_send_sync ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

+ adb shell rm /data/memfd-9f7fbfff9cfff8d3
     Running tests/memfd.rs (target/aarch64-linux-android/debug/deps/memfd-18901d5b48e24950)
remount succeeded
+ adb push /home/felix/memfd-rs/target/aarch64-linux-android/debug/deps/memfd-18901d5b48e24950 /data
/home/felix/memfd-rs/target/aarch64-linux-android/debug/deps/memfd-18901d5b48e24950: 1 file pushed. 21.6 MB/s (6180424 bytes in 0.273s)
+ adb shell /data/memfd-18901d5b48e24950

running 4 tests
test test_memfd_default ... ok
test test_memfd_from_into ... ok
test test_memfd_multi ... ok
test test_memfd_no_cloexec ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

+ adb shell rm /data/memfd-18901d5b48e24950
     Running tests/sealing.rs (target/aarch64-linux-android/debug/deps/sealing-a99958c7b2daecde)
remount succeeded
+ adb push /home/felix/memfd-rs/target/aarch64-linux-android/debug/deps/sealing-a99958c7b2daecde /data
/home/felix/memfd-rs/target/aarch64-linux-android/debug/deps/sealing-a99958c7b2daecde: 1 file pushed. 21.2 MB/s (6377448 bytes in 0.287s)
+ adb shell /data/sealing-a99958c7b2daecde

running 4 tests
test test_sealing_default ... ok
test test_sealing_add ... ok
test test_sealing_unsealed ... ok
test test_sealing_resize ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

+ adb shell rm /data/sealing-a99958c7b2daecde

@flxo flxo requested review from lucab and nagisa as code owners July 28, 2021 13:14
Comment thread src/memfd.rs Outdated
let flags = sealing::seals_to_bitflags(seals);
// UNSAFE(lucab): required syscall.
let r = unsafe { libc::syscall(libc::SYS_fcntl, fd, libc::F_ADD_SEALS, flags) };
let r = unsafe { libc::fcntl(fd, libc::F_ADD_SEALS, flags) };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I recall there being a reason to use the syscall interface. Not sure what it was, however.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok. Do you remember? Using libc::fcntl feel more portable than the explicit sys call name. I replaced syscall with fnctl because I had build errors for aarch64-linux-android with the SYS_fcntl arg which is not exposed by libc. I haven't digged which syscall name to use instead which would also be an option with a cfg(target_os = ... guard.

@lucab lucab Jul 29, 2021

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It looks like I wrote it that way initially, but I don't remember exactly why. Trying to retro-guess myself, it's likely one of those reasons:

  • fcntl() is a muxed wrapper in most libc implementations, and I didn't see a real reason to go through an additional opaque layer (and to make writing seccomp filters easier)
  • I was possibly playing with this on a platform where libc-rs was missing the function binding (uclibc? musl?) and so I went for the syscall directly
  • I was playing with the idea of making this work just with sc and without libc-rs at all, but I never walked that path till the end

Or possibly a mix of all of them. The only interesting one nowadays is the first, but portability is likely a better argument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@lucab Thanks for your reply. Which way would you like to go? I can try to refactor this patch to use libc::syscall withtarget_os (and maybe target_arch) specific guards. Otherwise we can also check which targets are affected at all.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@flxo if you are not in a hurry, it may be worth to just dig a bit more into libc-rs. If it is just missing a few SYS_fcntl definitions, it would be good to add it there; they have a self-service release process so it shouldn't add to much delay before being able to consume it here.
Instead, if the reason turns out to be more complex than that and we don't find any blocker for switching to the libc muxed wrapper, we can freely do that as a fallback.
In any case, thanks for looking into this :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@lucab I'll have a look why my first attempt with the direct use of libc::syscall didn't work for aarch64-linux-android.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@lucab Turned out that SYS_fcntl is missing in aarch64-linux-android in libc. I opened #2308

The flags F_ADD_SEALS and F_GET_SEALS are already included in libc for this target.
I'd propose to wait for the PR unless you're fine with the use of libc::fcntl...

bors added a commit to rust-lang/libc that referenced this pull request Aug 10, 2021
Bump to 0.2.99

I'd like to get a new release published, since I want to make use of #2308 in lucab/memfd-rs#25
bors added a commit to rust-lang/libc that referenced this pull request Aug 10, 2021
Bump to 0.2.99

I'd like to get a new release published, since I want to make use of #2308 in lucab/memfd-rs#25
@flxo

flxo commented Sep 1, 2021

Copy link
Copy Markdown
Contributor Author

@lucab Hi. I'm back from vacation :-)

Looks like the merge of the libc patch did the trick.

@flxo flxo requested a review from nagisa September 2, 2021 09:31
@lucab

lucab commented Sep 6, 2021

Copy link
Copy Markdown
Owner

@flxo thanks, patch looks fine. It would be good to add an explicit version to the libc dependency in cargo manifest to make sure consumers are meeting the minimum requirement.

Add `target_os = android` to the conditional compilation flags.
Add targets `aarch64-linux-android` and `arm-linux-androideabi` with the
use of `cross` the the GH action.
@lucab lucab merged commit a7d11c2 into lucab:master Sep 6, 2021
@flxo flxo deleted the pr-android branch September 6, 2021 11:59
@flxo

flxo commented Sep 6, 2021

Copy link
Copy Markdown
Contributor Author

@lucab Many thanks! Would you mind publishing a 0.4.1? There doesn't seem to be anything in the queue and I'd love to get rid of another git dependency.

@lucab lucab mentioned this pull request Jan 27, 2022
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