| // 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. | |
| //! Pure `#![no_std]` Zircon `processargs` protocol constants, handle-info | |
| //! packing (`PA_HND`), and wire header validation library. | |
| //! | |
| //! Mirrors `zircon/system/public/zircon/processargs.h`. Verified in Lean 4 | |
| //! (`//sdk/vx/vx_processargs/proofs`) directly over compiled AArch64 machine | |
| //! code for arbitrary linker placements. | |
| #![no_std] | |
| pub const ZX_PROCARGS_PROTOCOL: u32 = 0x4150_585d; | |
| pub const ZX_PROCARGS_VERSION: u32 = 0x0000_1000; | |
| pub const ZX_PROCARGS_HEADER_SIZE: u32 = 36; | |
| pub const PA_PROC_SELF: u8 = 0x01; | |
| pub const PA_THREAD_SELF: u8 = 0x02; | |
| pub const PA_JOB_DEFAULT: u8 = 0x03; | |
| pub const PA_VMAR_ROOT: u8 = 0x04; | |
| pub const PA_VMAR_LOADED: u8 = 0x05; | |
| pub const PA_LDSVC_LOADER: u8 = 0x10; | |
| pub const PA_VMO_VDSO: u8 = 0x11; | |
| pub const PA_VMO_STACK: u8 = 0x13; | |
| pub const PA_VMO_EXECUTABLE: u8 = 0x14; | |
| pub const PA_VMO_BOOTDATA: u8 = 0x1A; | |
| pub const PA_VMO_BOOTFS: u8 = 0x1B; | |
| pub const PA_VMO_KERNEL_FILE: u8 = 0x1C; | |
| pub const PA_VMO_COMPONENT_CONFIG: u8 = 0x1D; | |
| pub const PA_NS_DIR: u8 = 0x20; | |
| pub const PA_FD: u8 = 0x30; | |
| pub const PA_LIFECYCLE: u8 = 0x3A; | |
| pub const PA_DIRECTORY_REQUEST: u8 = 0x3B; | |
| pub const PA_ESCROWED_DICTIONARY: u8 = 0x3C; | |
| pub const PA_LOG_SINK: u8 = 0x3D; | |
| pub const PA_RESOURCE: u8 = 0x3F; | |
| pub const PA_CLOCK_UTC: u8 = 0x40; | |
| pub const PA_USER0: u8 = 0xF0; | |
| pub const PA_USER1: u8 = 0xF1; | |
| pub const PA_USER2: u8 = 0xF2; | |
| #[inline(always)] | |
| pub const fn pa_hnd(hnd_type: u8, arg: u16) -> u32 { | |
| (hnd_type as u32) | ((arg as u32) << 16) | |
| } | |
| #[inline(always)] | |
| pub const fn pa_hnd_type(info: u32) -> u8 { | |
| (info & 0xFF) as u8 | |
| } | |
| #[inline(always)] | |
| pub const fn pa_hnd_arg(info: u32) -> u16 { | |
| ((info >> 16) & 0xFFFF) as u16 | |
| } | |
| /// Validate the header fields `(protocol, version, handle_info_off)` of a | |
| /// `zx_proc_args_t` message of length `nbytes` with `nhandles` transferred handles. | |
| /// | |
| /// Returns `Some(handle_info_off)` (guaranteed `>= 36`, 4-byte aligned, and | |
| /// `handle_info_off + 4 * nhandles <= nbytes`) when valid, or `None` otherwise. | |
| #[curry::verified] | |
| pub fn validate_header_fields( | |
| protocol: u32, | |
| version: u32, | |
| handle_info_off: u32, | |
| nbytes: usize, | |
| nhandles: usize, | |
| ) -> Option<usize> { | |
| let hdr = ZX_PROCARGS_HEADER_SIZE as usize; | |
| let hi = handle_info_off as usize; | |
| if nbytes < hdr | |
| || protocol != ZX_PROCARGS_PROTOCOL | |
| || version != ZX_PROCARGS_VERSION | |
| || hi < hdr | |
| || (hi & 3) != 0 | |
| || hi > nbytes | |
| || ((nbytes - hi) >> 2) < nhandles | |
| { | |
| None | |
| } else { | |
| Some(hi) | |
| } | |
| } |