aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-artifact / src / resource.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use crate::error::ArtifactError; |
| 17 | |
| 18 | pub const RESOURCE_ENVELOPE_SIZE: usize = 48; |
| 19 | pub const PAGE_SIZE: u32 = 4096; |
| 20 | pub const MAX_CODE_PAGES: u32 = 64; |
| 21 | pub const MAX_DATA_PAGES: u32 = 64; |
| 22 | pub const MAX_STACK_PAGES: u32 = 16; |
| 23 | pub const MAX_IPC_MESSAGES: u32 = 1024; |
| 24 | pub const MAX_IPC_BYTES: u32 = 1_048_576; |
| 25 | pub const MAX_CPU_TICKS: u64 = 1_000_000_000; |
| 26 | pub const MAX_ELAPSED_TICKS: u64 = 1_000_000_000; |
| 27 | pub const MAX_SYSCALLS: u32 = 65_536; |
| 28 | |
| 29 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 30 | pub struct ResourceEnvelope { |
| 31 | pub code_pages: u32, |
| 32 | pub data_pages: u32, |
| 33 | pub stack_pages: u32, |
| 34 | pub max_capabilities: u16, |
| 35 | pub ipc_messages: u32, |
| 36 | pub ipc_bytes: u32, |
| 37 | pub cpu_ticks: u64, |
| 38 | pub elapsed_ticks: u64, |
| 39 | pub syscall_count: u32, |
| 40 | } |
| 41 | |
| 42 | impl ResourceEnvelope { |
| 43 | pub fn decode(bytes: &[u8], requested_capabilities: usize) -> Result<Self, ArtifactError> { |
| 44 | if bytes.len() != RESOURCE_ENVELOPE_SIZE { |
| 45 | return Err(if bytes.len() < RESOURCE_ENVELOPE_SIZE { |
| 46 | ArtifactError::Truncated |
| 47 | } else { |
| 48 | ArtifactError::WrongLength |
| 49 | }); |
| 50 | } |
| 51 | if read_u16(bytes, 14)? != 0 || read_u32(bytes, 44)? != 0 { |
| 52 | return Err(ArtifactError::ReservedNonZero); |
| 53 | } |
| 54 | let envelope = Self { |
| 55 | code_pages: read_u32(bytes, 0)?, |
| 56 | data_pages: read_u32(bytes, 4)?, |
| 57 | stack_pages: read_u32(bytes, 8)?, |
| 58 | max_capabilities: read_u16(bytes, 12)?, |
| 59 | ipc_messages: read_u32(bytes, 16)?, |
| 60 | ipc_bytes: read_u32(bytes, 20)?, |
| 61 | cpu_ticks: read_u64(bytes, 24)?, |
| 62 | elapsed_ticks: read_u64(bytes, 32)?, |
| 63 | syscall_count: read_u32(bytes, 40)?, |
| 64 | }; |
| 65 | envelope.validate(requested_capabilities)?; |
| 66 | Ok(envelope) |
| 67 | } |
| 68 | |
| 69 | pub fn validate(&self, requested_capabilities: usize) -> Result<(), ArtifactError> { |
| 70 | if self.code_pages == 0 |
| 71 | || self.code_pages > MAX_CODE_PAGES |
| 72 | || self.data_pages == 0 |
| 73 | || self.data_pages > MAX_DATA_PAGES |
| 74 | || self.stack_pages == 0 |
| 75 | || self.stack_pages > MAX_STACK_PAGES |
| 76 | || usize::from(self.max_capabilities) < requested_capabilities |
| 77 | || usize::from(self.max_capabilities) > crate::capability::MAX_CAPABILITIES |
| 78 | || self.ipc_messages > MAX_IPC_MESSAGES |
| 79 | || self.ipc_bytes > MAX_IPC_BYTES |
| 80 | || self.cpu_ticks == 0 |
| 81 | || self.cpu_ticks > MAX_CPU_TICKS |
| 82 | || self.elapsed_ticks == 0 |
| 83 | || self.elapsed_ticks > MAX_ELAPSED_TICKS |
| 84 | || self.syscall_count == 0 |
| 85 | || self.syscall_count > MAX_SYSCALLS |
| 86 | { |
| 87 | return Err(ArtifactError::ResourceLimit); |
| 88 | } |
| 89 | Ok(()) |
| 90 | } |
| 91 | |
| 92 | pub fn code_capacity_bytes(&self) -> Result<u32, ArtifactError> { |
| 93 | self.code_pages |
| 94 | .checked_mul(PAGE_SIZE) |
| 95 | .ok_or(ArtifactError::LengthOverflow) |
| 96 | } |
| 97 | |
| 98 | pub fn data_capacity_bytes(&self) -> Result<u32, ArtifactError> { |
| 99 | self.data_pages |
| 100 | .checked_mul(PAGE_SIZE) |
| 101 | .ok_or(ArtifactError::LengthOverflow) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn read_u16(bytes: &[u8], offset: usize) -> Result<u16, ArtifactError> { |
| 106 | let end = offset.checked_add(2).ok_or(ArtifactError::LengthOverflow)?; |
| 107 | let value = bytes.get(offset..end).ok_or(ArtifactError::Truncated)?; |
| 108 | Ok(u16::from_le_bytes([value[0], value[1]])) |
| 109 | } |
| 110 | |
| 111 | fn read_u32(bytes: &[u8], offset: usize) -> Result<u32, ArtifactError> { |
| 112 | let end = offset.checked_add(4).ok_or(ArtifactError::LengthOverflow)?; |
| 113 | let value = bytes.get(offset..end).ok_or(ArtifactError::Truncated)?; |
| 114 | Ok(u32::from_le_bytes([value[0], value[1], value[2], value[3]])) |
| 115 | } |
| 116 | |
| 117 | fn read_u64(bytes: &[u8], offset: usize) -> Result<u64, ArtifactError> { |
| 118 | let end = offset.checked_add(8).ok_or(ArtifactError::LengthOverflow)?; |
| 119 | let value = bytes.get(offset..end).ok_or(ArtifactError::Truncated)?; |
| 120 | Ok(u64::from_le_bytes([ |
| 121 | value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], |
| 122 | ])) |
| 123 | } |