| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| use crate::objects::{BinderObjectFlags, LocalBinderObject, SerializedBinderObject}; | |
| use starnix_core::vfs::FdNumber; | |
| use starnix_uapi::union::struct_with_union_into_bytes; | |
| use starnix_uapi::user_address::UserAddress; | |
| use starnix_uapi::{ | |
| BINDER_TYPE_BINDER, BINDER_TYPE_FD, BINDER_TYPE_FDA, BINDER_TYPE_HANDLE, BINDER_TYPE_PTR, | |
| binder_buffer_object, binder_fd_array_object, binder_fd_object, binder_object_header, | |
| binder_uintptr_t, flat_binder_object, uapi, | |
| }; | |
| use zerocopy::{FromBytes, IntoBytes}; | |
| #[fuchsia::test] | |
| fn serialize_binder_handle() { | |
| let mut output = [0u8; std::mem::size_of::<flat_binder_object>()]; | |
| SerializedBinderObject::Handle { | |
| handle: 2.into(), | |
| flags: BinderObjectFlags::from_bits_retain(42), | |
| cookie: 99, | |
| } | |
| .write_to(&mut output) | |
| .expect("write handle"); | |
| assert_eq!( | |
| struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_HANDLE, | |
| flags: 42, | |
| cookie: 99, | |
| __bindgen_anon_1.handle: 2, | |
| }), | |
| output | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn serialize_binder_object() { | |
| let mut output = [0u8; std::mem::size_of::<flat_binder_object>()]; | |
| SerializedBinderObject::Object { | |
| local: LocalBinderObject { | |
| weak_ref_addr: UserAddress::from(0xDEADBEEF), | |
| strong_ref_addr: UserAddress::from(0xDEADDEAD), | |
| }, | |
| flags: BinderObjectFlags::from_bits_retain(42), | |
| } | |
| .write_to(&mut output) | |
| .expect("write object"); | |
| assert_eq!( | |
| struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_BINDER, | |
| flags: 42, | |
| cookie: 0xDEADDEAD, | |
| __bindgen_anon_1.binder: 0xDEADBEEF, | |
| }), | |
| output | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn serialize_binder_fd() { | |
| let mut output = [0u8; std::mem::size_of::<flat_binder_object>()]; | |
| SerializedBinderObject::File { fd: FdNumber::from_raw(2), cookie: 99 } | |
| .write_to(&mut output) | |
| .expect("write fd"); | |
| let (output_fd_object, _) = | |
| binder_fd_object::read_from_prefix(&output).expect("output ought be a binder_fd_object"); | |
| assert_eq!(BINDER_TYPE_FD, output_fd_object.hdr.type_); | |
| assert_eq!(99, output_fd_object.cookie); | |
| // SAFETY: Union read. | |
| assert_eq!(2, unsafe { output_fd_object.__bindgen_anon_1.fd }); | |
| } | |
| #[fuchsia::test] | |
| fn serialize_binder_buffer() { | |
| let mut output = [0u8; std::mem::size_of::<binder_buffer_object>()]; | |
| SerializedBinderObject::Buffer { | |
| buffer: UserAddress::from(0xDEADBEEF), | |
| length: 0x100, | |
| parent: 1, | |
| parent_offset: 20, | |
| flags: 42, | |
| } | |
| .write_to(&mut output) | |
| .expect("write buffer"); | |
| assert_eq!( | |
| binder_buffer_object { | |
| hdr: binder_object_header { type_: BINDER_TYPE_PTR }, | |
| buffer: 0xDEADBEEF, | |
| length: 0x100, | |
| parent: 1, | |
| parent_offset: 20, | |
| flags: 42, | |
| } | |
| .as_bytes(), | |
| output | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn serialize_binder_fd_array() { | |
| let mut output = [0u8; std::mem::size_of::<binder_fd_array_object>()]; | |
| SerializedBinderObject::FileArray { num_fds: 2, parent: 1, parent_offset: 20 } | |
| .write_to(&mut output) | |
| .expect("write fd array"); | |
| assert_eq!( | |
| binder_fd_array_object { | |
| hdr: binder_object_header { type_: BINDER_TYPE_FDA }, | |
| num_fds: 2, | |
| parent: 1, | |
| parent_offset: 20, | |
| pad: 0, | |
| } | |
| .as_bytes(), | |
| output | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn serialize_binder_buffer_too_small() { | |
| let mut output = [0u8; std::mem::size_of::<binder_uintptr_t>()]; | |
| SerializedBinderObject::Handle { | |
| handle: 2.into(), | |
| flags: BinderObjectFlags::from_bits_retain(42), | |
| cookie: 99, | |
| } | |
| .write_to(&mut output) | |
| .expect_err("write handle should not succeed"); | |
| SerializedBinderObject::Object { | |
| local: LocalBinderObject { | |
| weak_ref_addr: UserAddress::from(0xDEADBEEF), | |
| strong_ref_addr: UserAddress::from(0xDEADDEAD), | |
| }, | |
| flags: BinderObjectFlags::from_bits_retain(42), | |
| } | |
| .write_to(&mut output) | |
| .expect_err("write object should not succeed"); | |
| SerializedBinderObject::File { fd: FdNumber::from_raw(2), cookie: 99 } | |
| .write_to(&mut output) | |
| .expect_err("write fd should not succeed"); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_binder_handle() { | |
| let input = struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_HANDLE, | |
| flags: 42, | |
| cookie: 99, | |
| __bindgen_anon_1.handle: 2, | |
| }); | |
| assert_eq!( | |
| SerializedBinderObject::from_bytes(&input).expect("read handle"), | |
| SerializedBinderObject::Handle { | |
| handle: 2.into(), | |
| flags: BinderObjectFlags::from_bits_retain(42), | |
| cookie: 99 | |
| } | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_binder_object() { | |
| let input = struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_BINDER, | |
| flags: 42, | |
| cookie: 0xDEADDEAD, | |
| __bindgen_anon_1.binder: 0xDEADBEEF, | |
| }); | |
| assert_eq!( | |
| SerializedBinderObject::from_bytes(&input).expect("read object"), | |
| SerializedBinderObject::Object { | |
| local: LocalBinderObject { | |
| weak_ref_addr: UserAddress::from(0xDEADBEEF), | |
| strong_ref_addr: UserAddress::from(0xDEADDEAD) | |
| }, | |
| flags: BinderObjectFlags::from_bits_retain(42) | |
| } | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn binder_object_retains_unknown_flags() { | |
| const UNKNOWN_BITS: u32 = 0x8000_0000; | |
| let combined = UNKNOWN_BITS | uapi::flat_binder_object_flags_FLAT_BINDER_FLAG_ACCEPTS_FDS; | |
| let flags = BinderObjectFlags::from_bits_retain(combined); | |
| assert!(flags.contains(BinderObjectFlags::ACCEPTS_FDS)); | |
| assert_eq!(flags.bits(), combined); | |
| let binder_input = struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_BINDER, | |
| flags: combined, | |
| cookie: 0x1234, | |
| __bindgen_anon_1.binder: 0x5678, | |
| }); | |
| let deserialized_object = | |
| SerializedBinderObject::from_bytes(&binder_input).expect("deserialize unknown flags"); | |
| assert_eq!( | |
| deserialized_object, | |
| SerializedBinderObject::Object { | |
| local: LocalBinderObject { | |
| weak_ref_addr: UserAddress::from(0x5678), | |
| strong_ref_addr: UserAddress::from(0x1234), | |
| }, | |
| flags: BinderObjectFlags::from_bits_retain(combined), | |
| } | |
| ); | |
| let mut binder_output = [0u8; std::mem::size_of::<flat_binder_object>()]; | |
| deserialized_object.write_to(&mut binder_output).expect("write object"); | |
| assert_eq!(binder_output, binder_input); | |
| let handle_input = struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_HANDLE, | |
| flags: combined, | |
| cookie: 0x1234, | |
| __bindgen_anon_1.handle: 7, | |
| }); | |
| let deserialized_handle = | |
| SerializedBinderObject::from_bytes(&handle_input).expect("deserialize handle flags"); | |
| assert_eq!( | |
| deserialized_handle, | |
| SerializedBinderObject::Handle { | |
| handle: 7.into(), | |
| flags: BinderObjectFlags::from_bits_retain(combined), | |
| cookie: 0x1234, | |
| } | |
| ); | |
| let mut handle_output = [0u8; std::mem::size_of::<flat_binder_object>()]; | |
| deserialized_handle.write_to(&mut handle_output).expect("write handle"); | |
| assert_eq!(handle_output, handle_input); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_binder_fd() { | |
| let input = struct_with_union_into_bytes!(binder_fd_object { | |
| hdr.type_: BINDER_TYPE_FD, | |
| pad_flags: 0xdeadbeef, | |
| cookie: 99, | |
| __bindgen_anon_1.fd: 2, | |
| }); | |
| assert_eq!( | |
| SerializedBinderObject::from_bytes(&input).expect("read handle"), | |
| SerializedBinderObject::File { fd: FdNumber::from_raw(2), cookie: 99 } | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_binder_buffer() { | |
| let input = binder_buffer_object { | |
| hdr: binder_object_header { type_: BINDER_TYPE_PTR }, | |
| buffer: 0xDEADBEEF, | |
| length: 0x100, | |
| parent: 1, | |
| parent_offset: 20, | |
| flags: 42, | |
| }; | |
| assert_eq!( | |
| SerializedBinderObject::from_bytes(input.as_bytes()).expect("read buffer"), | |
| SerializedBinderObject::Buffer { | |
| buffer: UserAddress::from(0xDEADBEEF), | |
| length: 0x100, | |
| parent: 1, | |
| parent_offset: 20, | |
| flags: 42, | |
| } | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_binder_fd_array() { | |
| let input = binder_fd_array_object { | |
| hdr: binder_object_header { type_: BINDER_TYPE_FDA }, | |
| num_fds: 2, | |
| pad: 0, | |
| parent: 1, | |
| parent_offset: 20, | |
| }; | |
| assert_eq!( | |
| SerializedBinderObject::from_bytes(input.as_bytes()).expect("read fd array"), | |
| SerializedBinderObject::FileArray { num_fds: 2, parent: 1, parent_offset: 20 } | |
| ); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_unknown_object() { | |
| let input = struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: 9001, | |
| flags: 42, | |
| cookie: 99, | |
| __bindgen_anon_1.handle: 2, | |
| }); | |
| SerializedBinderObject::from_bytes(&input).expect_err("read unknown object"); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_input_too_small() { | |
| let input = struct_with_union_into_bytes!(binder_fd_object { | |
| hdr.type_: BINDER_TYPE_FD, | |
| pad_flags: 0xdeadbeef, | |
| cookie: 99, | |
| __bindgen_anon_1.fd: 2, | |
| }); | |
| SerializedBinderObject::from_bytes(&input[..std::mem::size_of::<binder_uintptr_t>()]) | |
| .expect_err("read buffer too small"); | |
| } | |
| #[fuchsia::test] | |
| fn deserialize_unaligned() { | |
| let input = struct_with_union_into_bytes!(flat_binder_object { | |
| hdr.type_: BINDER_TYPE_HANDLE, | |
| flags: 42, | |
| cookie: 99, | |
| __bindgen_anon_1.handle: 2, | |
| }); | |
| let mut unaligned_input = vec![]; | |
| unaligned_input.push(0u8); | |
| unaligned_input.extend(input); | |
| SerializedBinderObject::from_bytes(&unaligned_input[1..]).expect("read unaligned object"); | |
| } |