From 5e2ebfb9d71b6be631f2d6279fdba19050142938 Mon Sep 17 00:00:00 2001 From: Ren-Pei Zeng Date: Wed, 23 Oct 2024 08:31:33 +0000 Subject: Unset FDSan tag when converting handle into other types Bug: 359100544 Test: atest libnativewindow_rs-internal_test Change-Id: Ib80d14277d9f695615fa0f07459d5c0bce4aeab9 --- libs/nativewindow/rust/src/handle.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libs/nativewindow/rust/src/handle.rs b/libs/nativewindow/rust/src/handle.rs index ee70e3f538..2b08c1bcb9 100644 --- a/libs/nativewindow/rust/src/handle.rs +++ b/libs/nativewindow/rust/src/handle.rs @@ -85,6 +85,12 @@ impl NativeHandle { /// Destroys the `NativeHandle`, taking ownership of the file descriptors it contained. pub fn into_fds(self) -> Vec { + // Unset FDSan tag since this `native_handle_t` is no longer the owner of the file + // descriptors after this function. + // SAFETY: Our wrapped `native_handle_t` pointer is always valid. + unsafe { + ffi::native_handle_unset_fdsan_tag(self.as_ref()); + } let fds = self.data()[..self.fd_count()] .iter() .map(|fd| { @@ -260,6 +266,29 @@ mod test { drop(cloned); } + #[test] + fn to_fds() { + let file = File::open("/dev/null").unwrap(); + let original = NativeHandle::new(vec![file.into()], &[42]).unwrap(); + assert_eq!(original.ints(), &[42]); + assert_eq!(original.fds().len(), 1); + + let fds = original.into_fds(); + assert_eq!(fds.len(), 1); + } + + #[test] + fn to_aidl() { + let file = File::open("/dev/null").unwrap(); + let original = NativeHandle::new(vec![file.into()], &[42]).unwrap(); + assert_eq!(original.ints(), &[42]); + assert_eq!(original.fds().len(), 1); + + let aidl = AidlNativeHandle::from(original); + assert_eq!(&aidl.ints, &[42]); + assert_eq!(aidl.fds.len(), 1); + } + #[test] fn to_from_aidl() { let file = File::open("/dev/null").unwrap(); -- cgit v1.2.3-59-g8ed1b