blob: 8354deb3c38bf7d27828d1bb34840815fc1f8135 [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.
//! Unified `#![no_std]` Fuchsia runtime startup API (`//sdk/vx/vx_runtime`).
//!
//! Mirrors Fuchsia's `fuchsia-runtime` startup handle and namespace APIs
//! ([`HandleType`], [`HandleInfo`], [`take_startup_handle`],
//! [`take_namespace_dir`]) without depending on `//src/vx/` or raw `vx_sys`.
//!
//! Runtime providers supply the two link-time C-ABI hooks:
//! - `__vx_take_startup_handle(info: u32) -> u32`
//! - `__vx_take_namespace_dir(path_ptr: *const u8, path_len: usize) -> u32`
//!
//! # Security Boundary
//!
//! Ambient `vmar_root_self()` is **deliberately omitted** so sandboxed
//! components cannot obtain direct access to the process's root VMAR.
#![no_std]
use core::mem::{align_of, size_of};
use vx_processargs::{
PA_CLOCK_UTC, PA_DIRECTORY_REQUEST, PA_ESCROWED_DICTIONARY, PA_FD, PA_JOB_DEFAULT,
PA_LDSVC_LOADER, PA_LIFECYCLE, PA_LOG_SINK, PA_NS_DIR, PA_PROC_SELF, PA_RESOURCE,
PA_THREAD_SELF, PA_USER0, PA_USER1, PA_USER2, PA_VMAR_LOADED, PA_VMAR_ROOT, PA_VMO_BOOTDATA,
PA_VMO_BOOTFS, PA_VMO_COMPONENT_CONFIG, PA_VMO_EXECUTABLE, PA_VMO_KERNEL_FILE, PA_VMO_STACK,
PA_VMO_VDSO, pa_hnd, pa_hnd_arg, pa_hnd_type,
};
use vx_zx::sys::ZX_HANDLE_INVALID;
pub use vx_zx::{Channel, Handle, NullableHandle, Status};
unsafe extern "C" {
fn __vx_take_startup_handle(info: u32) -> u32;
fn __vx_take_namespace_dir(path_ptr: *const u8, path_len: usize) -> u32;
}
/// Zero-sized error returned when converting an unrecognized raw `u8` into
/// [`HandleType`].
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct InvalidHandleType;
const _: () = assert!(size_of::<InvalidHandleType>() == 0);
/// Zircon `processargs` startup handle type discriminant (`PA_*`), mirroring
/// `fuchsia_runtime::HandleType`.
#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum HandleType {
/// Handle to our own process (`PA_PROC_SELF`).
ProcessSelf = PA_PROC_SELF,
/// Handle to the initial thread of our own process (`PA_THREAD_SELF`).
ThreadSelf = PA_THREAD_SELF,
/// Handle to the default job for creating child processes (`PA_JOB_DEFAULT`).
DefaultJob = PA_JOB_DEFAULT,
/// Handle to the root VMAR (`PA_VMAR_ROOT`).
///
/// Note: ambient `vmar_root_self()` is intentionally omitted so component
/// code is not given direct root VMAR access.
RootVmar = PA_VMAR_ROOT,
/// Handle to the VMAR used to load the initial image (`PA_VMAR_LOADED`).
LoadedVmar = PA_VMAR_LOADED,
/// Handle to the dynamic loader service channel (`PA_LDSVC_LOADER`).
LdsvcLoader = PA_LDSVC_LOADER,
/// Handle to the vDSO VMO (`PA_VMO_VDSO`).
VdsoVmo = PA_VMO_VDSO,
/// Handle to the initial stack VMO (`PA_VMO_STACK`).
StackVmo = PA_VMO_STACK,
/// Handle to the executable VMO (`PA_VMO_EXECUTABLE`).
ExecutableVmo = PA_VMO_EXECUTABLE,
/// Handle to the ZBI bootdata VMO (`PA_VMO_BOOTDATA`).
BootdataVmo = PA_VMO_BOOTDATA,
/// Handle to the bootfs VMO (`PA_VMO_BOOTFS`).
BootfsVmo = PA_VMO_BOOTFS,
/// Handle to a kernel file VMO (`PA_VMO_KERNEL_FILE`).
KernelFileVmo = PA_VMO_KERNEL_FILE,
/// Handle to the component's structured configuration VMO (`PA_VMO_COMPONENT_CONFIG`).
ComponentConfigVmo = PA_VMO_COMPONENT_CONFIG,
/// Handle to a namespace directory channel (`PA_NS_DIR`).
NamespaceDirectory = PA_NS_DIR,
/// Handle to a file descriptor (`PA_FD`).
FileDescriptor = PA_FD,
/// Handle to the component lifecycle channel (`PA_LIFECYCLE`).
Lifecycle = PA_LIFECYCLE,
/// Handle to the component outgoing directory request channel (`PA_DIRECTORY_REQUEST`).
DirectoryRequest = PA_DIRECTORY_REQUEST,
/// Handle to an escrowed component dictionary (`PA_ESCROWED_DICTIONARY`).
EscrowedDictionary = PA_ESCROWED_DICTIONARY,
/// Handle to the log sink (`PA_LOG_SINK`).
LogSink = PA_LOG_SINK,
/// Handle to a Zircon resource (`PA_RESOURCE`).
Resource = PA_RESOURCE,
/// Handle to the UTC clock (`PA_CLOCK_UTC`).
ClockUtc = PA_CLOCK_UTC,
/// Application-defined handle slot 0 (`PA_USER0`).
User0 = PA_USER0,
/// Application-defined handle slot 1 (`PA_USER1`).
User1 = PA_USER1,
/// Application-defined handle slot 2 (`PA_USER2`).
User2 = PA_USER2,
}
const _: () = assert!(size_of::<HandleType>() == 1);
const _: () = assert!(align_of::<HandleType>() == 1);
impl HandleType {
/// Return the raw `PA_*` type byte for this handle type.
#[inline(always)]
pub const fn as_u8(self) -> u8 {
self as u8
}
/// Decode a raw `PA_*` type byte into a known [`HandleType`].
#[inline(always)]
pub const fn from_u8(raw: u8) -> Option<Self> {
match raw {
PA_PROC_SELF => Some(Self::ProcessSelf),
PA_THREAD_SELF => Some(Self::ThreadSelf),
PA_JOB_DEFAULT => Some(Self::DefaultJob),
PA_VMAR_ROOT => Some(Self::RootVmar),
PA_VMAR_LOADED => Some(Self::LoadedVmar),
PA_LDSVC_LOADER => Some(Self::LdsvcLoader),
PA_VMO_VDSO => Some(Self::VdsoVmo),
PA_VMO_STACK => Some(Self::StackVmo),
PA_VMO_EXECUTABLE => Some(Self::ExecutableVmo),
PA_VMO_BOOTDATA => Some(Self::BootdataVmo),
PA_VMO_BOOTFS => Some(Self::BootfsVmo),
PA_VMO_KERNEL_FILE => Some(Self::KernelFileVmo),
PA_VMO_COMPONENT_CONFIG => Some(Self::ComponentConfigVmo),
PA_NS_DIR => Some(Self::NamespaceDirectory),
PA_FD => Some(Self::FileDescriptor),
PA_LIFECYCLE => Some(Self::Lifecycle),
PA_DIRECTORY_REQUEST => Some(Self::DirectoryRequest),
PA_ESCROWED_DICTIONARY => Some(Self::EscrowedDictionary),
PA_LOG_SINK => Some(Self::LogSink),
PA_RESOURCE => Some(Self::Resource),
PA_CLOCK_UTC => Some(Self::ClockUtc),
PA_USER0 => Some(Self::User0),
PA_USER1 => Some(Self::User1),
PA_USER2 => Some(Self::User2),
_ => None,
}
}
}
impl From<HandleType> for u8 {
#[inline(always)]
fn from(ht: HandleType) -> Self {
ht.as_u8()
}
}
impl TryFrom<u8> for HandleType {
type Error = InvalidHandleType;
#[inline(always)]
fn try_from(raw: u8) -> Result<Self, Self::Error> {
Self::from_u8(raw).ok_or(InvalidHandleType)
}
}
/// Packed Zircon `PA_HND(type, arg)` startup handle descriptor, mirroring
/// `fuchsia_runtime::HandleInfo`.
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct HandleInfo(u32);
const _: () = assert!(size_of::<HandleInfo>() == 4);
const _: () = assert!(align_of::<HandleInfo>() == 4);
impl HandleInfo {
/// Outgoing directory server request (`PA_HND(PA_DIRECTORY_REQUEST, 0)`).
pub const DIRECTORY_REQUEST: Self = Self::new(HandleType::DirectoryRequest, 0);
/// Component lifecycle channel (`PA_HND(PA_LIFECYCLE, 0)`).
pub const LIFECYCLE: Self = Self::new(HandleType::Lifecycle, 0);
/// Component structured configuration VMO (`PA_HND(PA_VMO_COMPONENT_CONFIG, 0)`).
pub const COMPONENT_CONFIG: Self = Self::new(HandleType::ComponentConfigVmo, 0);
/// Construct a [`HandleInfo`] from a typed [`HandleType`] and 16-bit argument.
#[inline(always)]
pub const fn new(handle_type: HandleType, arg: u16) -> Self {
Self(pa_hnd(handle_type.as_u8(), arg))
}
/// Construct a namespace directory handle descriptor (`PA_HND(PA_NS_DIR, index)`).
#[inline(always)]
pub const fn ns_dir(index: u16) -> Self {
Self::new(HandleType::NamespaceDirectory, index)
}
/// Wrap a raw `PA_HND` `u32` word.
#[inline(always)]
pub const fn from_raw(raw: u32) -> Self {
Self(raw)
}
/// Return the packed `PA_HND` `u32` word.
#[inline(always)]
pub const fn as_raw(self) -> u32 {
self.0
}
/// Decode the [`HandleType`] discriminant byte, or `None` if unknown.
#[inline(always)]
pub const fn handle_type(self) -> Option<HandleType> {
HandleType::from_u8(pa_hnd_type(self.0))
}
/// Return the raw low 8-bit `PA_*` handle type byte.
#[inline(always)]
pub const fn raw_type(self) -> u8 {
pa_hnd_type(self.0)
}
/// Return the 16-bit handle argument (`PA_HND_ARG`).
#[inline(always)]
pub const fn arg(self) -> u16 {
pa_hnd_arg(self.0)
}
}
impl From<HandleInfo> for u32 {
#[inline(always)]
fn from(info: HandleInfo) -> Self {
info.as_raw()
}
}
impl From<HandleType> for HandleInfo {
#[inline(always)]
fn from(ht: HandleType) -> Self {
Self::new(ht, 0)
}
}
/// Take ownership of the startup handle matching `info` from the active
/// runtime context.
///
/// One-shot move semantics: each handle is returned at most once; subsequent
/// calls for the same `info` return `None`.
#[curry::verified]
pub fn take_startup_handle(info: HandleInfo) -> Option<NullableHandle> {
// SAFETY: `__vx_take_startup_handle` is a link-time C-ABI runtime hook
// that either returns `ZX_HANDLE_INVALID` (0) or transfers sole ownership
// of a live Zircon handle matching `info`.
let raw = unsafe { __vx_take_startup_handle(info.as_raw()) };
if raw == ZX_HANDLE_INVALID {
None
} else {
// SAFETY: `raw != ZX_HANDLE_INVALID` and `__vx_take_startup_handle`
// transferred sole ownership of this handle to the caller.
Some(unsafe { NullableHandle::from_raw(raw) })
}
}
/// Take ownership of the namespace directory [`Channel`] mounted at `path`
/// (e.g., `"/svc"` or `"/pkg"`) from the active runtime context.
///
/// One-shot move semantics: each namespace directory channel is returned at
/// most once; subsequent calls for the same `path` (or empty/unknown paths)
/// return `None`.
#[curry::verified]
pub fn take_namespace_dir(path: &str) -> Option<Channel> {
if path.is_empty() {
return None;
}
// SAFETY: `path.as_ptr()` is valid for reading `path.len()` bytes for the
// duration of the call, and `__vx_take_namespace_dir` returns either
// `ZX_HANDLE_INVALID` (0) or sole ownership of a live directory `Channel`
// handle.
let raw = unsafe { __vx_take_namespace_dir(path.as_ptr(), path.len()) };
if raw == ZX_HANDLE_INVALID {
None
} else {
// SAFETY: `raw != ZX_HANDLE_INVALID` and `__vx_take_namespace_dir`
// transferred sole ownership of this directory channel handle.
Some(unsafe { Channel::from_raw(raw) })
}
}