1use std::fmt;
16use std::sync::atomic::{AtomicU64, Ordering};
17
18static FAULTS: AtomicU64 = AtomicU64::new(0);
20
21#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum RuntimeError {
24 PoisonedLock(&'static str),
27 Abandoned(&'static str),
36 AlreadyCollected(&'static str),
45 EntropyFailed,
47 CapIdCollision,
49 CannotFinalizeHostFlow,
51 FlowIdExhausted,
53 DuplicateFlowId,
55 LinkIdExhausted,
57 MonitorIdExhausted,
59}
60
61impl fmt::Display for RuntimeError {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match self {
64 RuntimeError::PoisonedLock(where_) => {
65 write!(f, "runtime mutex poisoned at {where_}")
66 }
67 RuntimeError::Abandoned(where_) => {
68 write!(
69 f,
70 "{where_}: flow was destroyed before producing an outcome"
71 )
72 }
73 RuntimeError::AlreadyCollected(where_) => {
74 write!(f, "{where_}: outcome was already collected")
75 }
76 RuntimeError::EntropyFailed => {
77 write!(f, "capability CSPRNG unavailable")
78 }
79 RuntimeError::CapIdCollision => {
80 write!(f, "could not allocate a unique capability id")
81 }
82 RuntimeError::CannotFinalizeHostFlow => {
83 write!(f, "cannot finalize reserved host flow")
84 }
85 RuntimeError::FlowIdExhausted => {
86 write!(f, "flow id space exhausted (counter wrapped)")
87 }
88 RuntimeError::DuplicateFlowId => {
89 write!(f, "directory already has this flow id")
90 }
91 RuntimeError::LinkIdExhausted => {
92 write!(f, "link id space exhausted (counter wrapped)")
93 }
94 RuntimeError::MonitorIdExhausted => {
95 write!(f, "monitor id space exhausted (counter wrapped)")
96 }
97 }
98 }
99}
100
101impl std::error::Error for RuntimeError {}
102
103#[derive(Debug, Clone, PartialEq, Eq)]
109pub enum LifecycleError {
110 NoSuchFlow(super::process::FlowId),
111 InvalidCapability,
112 InvalidMonitor,
113 InvalidLink,
114 NotOwner,
115 AlreadyRegistered,
116 AlreadyLinked,
117 EmptyName,
118 SelfRelation,
119 Unavailable,
120 LinkLimitReached {
122 limit: u32,
123 },
124 MonitorLimitReached {
126 limit: u32,
127 },
128 RegistryLimitReached {
130 limit: u32,
131 },
132}
133
134impl fmt::Display for LifecycleError {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 match self {
137 Self::NoSuchFlow(id) => write!(f, "no live flow {id}"),
138 Self::InvalidCapability => write!(f, "unknown or revoked capability"),
139 Self::InvalidMonitor => write!(f, "unknown monitor"),
140 Self::InvalidLink => write!(f, "unknown link"),
141 Self::NotOwner => write!(f, "caller does not own this relation"),
142 Self::AlreadyRegistered => write!(f, "registry name already taken"),
143 Self::AlreadyLinked => write!(f, "flows are already linked"),
144 Self::EmptyName => write!(f, "registry name must be non-empty"),
145 Self::SelfRelation => write!(f, "cannot link or monitor a flow to itself"),
146 Self::Unavailable => write!(f, "runtime table unavailable (poisoned lock)"),
147 Self::LinkLimitReached { limit } => {
148 write!(f, "link limit reached ({limit})")
149 }
150 Self::MonitorLimitReached { limit } => {
151 write!(f, "monitor limit reached ({limit})")
152 }
153 Self::RegistryLimitReached { limit } => {
154 write!(f, "registry name limit reached ({limit})")
155 }
156 }
157 }
158}
159
160impl std::error::Error for LifecycleError {}
161
162#[cold]
164pub fn report_fault(err: RuntimeError) {
165 FAULTS.fetch_add(1, Ordering::Relaxed);
166 eprintln!("byteflow: {err} — fail-closed");
167}
168
169pub fn fault_count() -> u64 {
171 FAULTS.load(Ordering::Relaxed)
172}
173
174#[derive(Debug, Clone, PartialEq, Eq)]
181pub enum SpawnError {
182 BadFunction {
184 index: u32,
185 table_size: u32,
186 },
187 VerifyFailed(crate::bytecode::VerifyError),
189 InvalidCapability,
191 Unavailable,
193 FlowIdExhausted,
195 FlowLimit {
197 current: usize,
198 max: u32,
199 },
200 SpawnDenied(String),
202 SpawnRateExceeded,
203 ParentCapRevoked,
204 MissingSpawnRight,
205 NameTaken {
207 name: String,
208 },
209 ThreadSpawnFailed(String),
211 Attestation(crate::AttestError),
213 VmInit(String),
216}
217
218impl fmt::Display for SpawnError {
219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220 match self {
221 SpawnError::BadFunction { index, table_size } => {
222 write!(
223 f,
224 "spawn: function index {index} out of range (table size {table_size})"
225 )
226 }
227 SpawnError::VerifyFailed(err) => write!(f, "chunk verification failed: {err}"),
228 SpawnError::InvalidCapability => {
229 write!(f, "spawn: argument capability is unknown or not held")
230 }
231 SpawnError::Unavailable => write!(f, "spawn: runtime table unavailable"),
232 SpawnError::FlowIdExhausted => {
233 write!(f, "spawn: flow id space exhausted (counter wrapped)")
234 }
235 SpawnError::FlowLimit { current, max } => {
236 write!(f, "spawn: live flow limit reached ({current}/{max})")
237 }
238 SpawnError::SpawnDenied(msg) => write!(f, "spawn: {msg}"),
239 SpawnError::SpawnRateExceeded => write!(f, "spawn: spawn rate exceeded"),
240 SpawnError::ParentCapRevoked => write!(f, "spawn: parent capability revoked"),
241 SpawnError::MissingSpawnRight => write!(f, "spawn: parent lacks SPAWN right"),
242 SpawnError::NameTaken { name } => {
243 write!(f, "spawn: registry name {name:?} already taken")
244 }
245 SpawnError::ThreadSpawnFailed(msg) => {
246 write!(f, "failed to spawn runtime thread: {msg}")
247 }
248 SpawnError::Attestation(err) => write!(f, "attestation failed: {err}"),
249 SpawnError::VmInit(msg) => write!(f, "vm init failed: {msg}"),
250 }
251 }
252}
253
254impl std::error::Error for SpawnError {}
255
256impl From<crate::vm::Fault> for SpawnError {
257 fn from(fault: crate::vm::Fault) -> Self {
258 match fault {
259 crate::vm::Fault::BadFunction { index, table_size } => {
260 SpawnError::BadFunction { index, table_size }
261 }
262 other => SpawnError::VmInit(other.to_string()),
263 }
264 }
265}