blob: e68d8ce4800bc21746b8b138768ac39e9393b6bc [file]
// 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.
mod command_read;
mod freeze_and_death;
mod handles_and_refs;
mod objects_serialization;
mod resource_accessor;
mod shared_memory;
mod threads;
mod transaction_translation;
mod transactions;
pub use resource_accessor::run_process_accessor;
use crate::binder::{BinderDevice, BinderDriver, OperationContext};
use crate::objects::{
BinderObject, BinderObjectFlags, Handle, LocalBinderObject, RefCountActions, StrongRefGuard,
};
use crate::process::BinderProcess;
use crate::resource_accessor::ResourceAccessor;
use crate::shared_memory::SharedMemory;
use crate::thread::BinderThread;
use starnix_core::mm::{
DesiredAddress, MappingName, MappingOptions, MemoryAccessor, ProtectionFlags,
};
use starnix_core::task::{CurrentTask, ExitStatus, Kernel};
use starnix_core::testing::*;
use starnix_core::vfs::{FdFlags, FdNumber, FileHandle};
use starnix_types::ownership::{OwnedRef, Releasable, release_after};
use starnix_uapi::errors::Errno;
use starnix_uapi::user_address::UserAddress;
use starnix_uapi::{
binder_transaction_data, binder_transaction_data__bindgen_ty_1, binder_transaction_data_sg,
};
use std::ops::Deref;
use std::sync::{Arc, Weak};
const BASE_ADDR: UserAddress = UserAddress::const_from(0x0000000000000100);
const VMO_LENGTH: usize = 4096;
impl ResourceAccessor for AutoReleasableTask {
fn close_files(&self, fds: Vec<FdNumber>) -> Result<(), Errno> {
self.deref().close_files(fds)
}
fn get_files_with_flags(
&self,
current_task: &CurrentTask,
fds: Vec<FdNumber>,
) -> Result<Vec<(FileHandle, FdFlags)>, Errno> {
self.deref().get_files_with_flags(current_task, fds)
}
fn add_files_with_flags(
&self,
current_task: &CurrentTask,
files: Vec<(FileHandle, FdFlags)>,
add_action: &mut dyn FnMut(FdNumber),
) -> Result<Vec<FdNumber>, Errno> {
self.deref().add_files_with_flags(current_task, files, add_action)
}
fn as_memory_accessor(&self) -> Option<&dyn MemoryAccessor> {
Some(self.deref())
}
}
struct BinderProcessFixture {
device: Weak<BinderDriver>,
proc: OwnedRef<BinderProcess>,
thread: OwnedRef<BinderThread>,
kernel: Arc<Kernel>,
task: Option<AutoReleasableTask>,
}
impl BinderProcessFixture {
fn new(current_task: &CurrentTask, device: &BinderDevice) -> Self {
let task = create_task(current_task.kernel(), "task");
let (proc, thread) = device.create_process_and_thread(task.pid.clone(), &task.task);
mmap_shared_memory(&device, &task, &proc);
Self {
device: Arc::downgrade(device),
proc,
thread,
kernel: current_task.kernel().clone(),
task: Some(task),
}
}
fn new_current(current_task: &CurrentTask, device: &BinderDevice) -> Self {
let (proc, thread) =
device.create_process_and_thread(current_task.pid.clone(), &current_task.task);
mmap_shared_memory(&device, current_task, &proc);
Self {
device: Arc::downgrade(device),
proc,
thread,
kernel: current_task.kernel().clone(),
task: None,
}
}
/// Returns a fixture for the current task without shared memory, as for a remote process
/// that has not mapped its VMO yet.
fn new_current_without_shared_memory(
current_task: &CurrentTask,
device: &BinderDevice,
) -> Self {
let (proc, thread) =
device.create_process_and_thread(current_task.pid.clone(), &current_task.task);
Self {
device: Arc::downgrade(device),
proc,
thread,
kernel: current_task.kernel().clone(),
task: None,
}
}
fn lock_shared_memory(&self) -> starnix_sync::MappedLockDepGuard<'_, SharedMemory> {
starnix_sync::LockDepGuard::map(self.proc.shared_memory.lock(), |value| {
value.as_mut().unwrap()
})
}
fn task(&self) -> &CurrentTask {
&self.task.as_ref().unwrap()
}
fn context<'a>(&'a self, current_task: &'a CurrentTask) -> OperationContext<'a> {
let current_task = if let Some(task) = self.task.as_ref() { &task } else { current_task };
OperationContext {
current_task,
binder_proc: &self.proc,
binder_thread: &self.thread,
memory_accessor: current_task.as_memory_accessor().expect("as_memory_accessor"),
}
}
/// Registers `local` in the process object table with a live strong reference, without
/// enqueuing any command. Refcount commands for such an object are not transient, so they
/// are not lazily cancelled by `handle_thread_read`. The returned guard must be released.
fn register_live_object(&self, local: LocalBinderObject) -> StrongRefGuard {
let (object, guard) = BinderObject::new(&self.proc, local, BinderObjectFlags::empty());
self.proc.lock().objects.insert(local.weak_ref_addr, object);
guard
}
}
impl Drop for BinderProcessFixture {
fn drop(&mut self) {
OwnedRef::take(&mut self.thread).release(&self.kernel);
if let Some(device) = self.device.upgrade() {
device.procs.write().remove(&self.proc.identifier()).release(&self.kernel);
}
OwnedRef::take(&mut self.proc).release(&self.kernel);
if let Some(task) = self.task.as_ref() {
task.write().set_exiting(ExitStatus::Exit(0));
}
}
}
/// Simulates an mmap call on the binder driver, setting up shared memory between the driver and
/// `proc`.
fn mmap_shared_memory(driver: &BinderDriver, current_task: &CurrentTask, proc: &BinderProcess) {
let prot_flags = ProtectionFlags::READ;
driver
.mmap(
current_task,
proc,
DesiredAddress::Any,
VMO_LENGTH,
prot_flags,
MappingOptions::empty(),
MappingName::None,
)
.expect("mmap");
}
/// Registers a binder object to `owner`.
fn register_binder_object(
owner: &BinderProcess,
weak_ref_addr: UserAddress,
strong_ref_addr: UserAddress,
) -> (Arc<BinderObject>, StrongRefGuard) {
let (object, guard) = BinderObject::new(
owner,
LocalBinderObject { weak_ref_addr, strong_ref_addr },
BinderObjectFlags::empty(),
);
owner.lock().objects.insert(weak_ref_addr, object.clone());
(object, guard)
}
fn handle_refcount_operation(
proc: &BinderProcess,
command: starnix_uapi::binder_driver_command_protocol,
handle: Handle,
) -> Result<(), Errno> {
let mut actions = RefCountActions::default();
release_after!(actions, (), { proc.handle_refcount_operation(command, handle, &mut actions) })
}
fn handle_refcount_operation_done(
proc: &BinderProcess,
command: starnix_uapi::binder_driver_command_protocol,
object: LocalBinderObject,
) -> Result<(), Errno> {
let mut actions = RefCountActions::default();
release_after!(actions, (), {
proc.handle_refcount_operation_done(command, object, &mut actions)
})
}
/// Sends a two-way transaction from `sender` to a binder object owned by `receiver`. On return,
/// `sender`'s thread has a `TwoWayTransactionComplete` queued and a pending
/// `TransactionRole::Sender`.
fn send_two_way_transaction(
current_task: &CurrentTask,
device: &BinderDevice,
sender: &BinderProcessFixture,
receiver: &BinderProcessFixture,
) {
const OBJECT_ADDR: UserAddress = UserAddress::const_from(0x01);
let (_, guard) =
register_binder_object(&receiver.proc, OBJECT_ADDR, (OBJECT_ADDR + 1u64).unwrap());
let handle = sender
.proc
.lock()
.handles
.insert_for_transaction(guard, &mut RefCountActions::default_released());
let transaction = binder_transaction_data_sg {
transaction_data: binder_transaction_data {
code: 42,
target: binder_transaction_data__bindgen_ty_1 { handle: handle.into() },
..binder_transaction_data::default()
},
buffers_size: 0,
};
device
.handle_transaction(&sender.context(current_task), &mut Vec::new(), transaction)
.expect("failed to handle the transaction");
}