Skip to content

Commit 5e2ebfb

Browse files
author
Ren-Pei Zeng
committed
Unset FDSan tag when converting handle into other types
Bug: 359100544 Test: atest libnativewindow_rs-internal_test Change-Id: Ib80d14277d9f695615fa0f07459d5c0bce4aeab9
1 parent 5da35fa commit 5e2ebfb

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

libs/nativewindow/rust/src/handle.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ impl NativeHandle {
8585

8686
/// Destroys the `NativeHandle`, taking ownership of the file descriptors it contained.
8787
pub fn into_fds(self) -> Vec<OwnedFd> {
88+
// Unset FDSan tag since this `native_handle_t` is no longer the owner of the file
89+
// descriptors after this function.
90+
// SAFETY: Our wrapped `native_handle_t` pointer is always valid.
91+
unsafe {
92+
ffi::native_handle_unset_fdsan_tag(self.as_ref());
93+
}
8894
let fds = self.data()[..self.fd_count()]
8995
.iter()
9096
.map(|fd| {
@@ -260,6 +266,29 @@ mod test {
260266
drop(cloned);
261267
}
262268

269+
#[test]
270+
fn to_fds() {
271+
let file = File::open("/dev/null").unwrap();
272+
let original = NativeHandle::new(vec![file.into()], &[42]).unwrap();
273+
assert_eq!(original.ints(), &[42]);
274+
assert_eq!(original.fds().len(), 1);
275+
276+
let fds = original.into_fds();
277+
assert_eq!(fds.len(), 1);
278+
}
279+
280+
#[test]
281+
fn to_aidl() {
282+
let file = File::open("/dev/null").unwrap();
283+
let original = NativeHandle::new(vec![file.into()], &[42]).unwrap();
284+
assert_eq!(original.ints(), &[42]);
285+
assert_eq!(original.fds().len(), 1);
286+
287+
let aidl = AidlNativeHandle::from(original);
288+
assert_eq!(&aidl.ints, &[42]);
289+
assert_eq!(aidl.fds.len(), 1);
290+
}
291+
263292
#[test]
264293
fn to_from_aidl() {
265294
let file = File::open("/dev/null").unwrap();

0 commit comments

Comments
 (0)