feat(diagnostics): preserve Registry and resource fault context for PLN-0176 step 28

This commit is contained in:
2026-10-07 21:13:59 +01:00
parent 74c7522dd1
commit 63e371955a
24 changed files with 1085 additions and 85 deletions
@@ -30,6 +30,7 @@ pub fn check_local_slot(
message: format!("Local slot {} out of bounds for function (limit {})", slot, limit),
pc,
span: None,
resource: None,
})
}
}
@@ -1,21 +1,9 @@
//! Session accounting shared by owners, with reservations refunded on failure/drop.
//! These quotas account VM domains, not process RSS or allocator metadata.
use std::collections::VecDeque;
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(usize)]
pub enum RuntimeResource {
GuestBytes,
HandleSlots,
AuxiliaryBytes,
TemporaryBytes,
StringBytes,
StringAllocations,
IndividualStringBytes,
CallDepth,
}
pub use prometeu_bytecode::{ResourceFailure, ResourceReason, RuntimeResource};
const COUNT: usize = 8;
const MIB: u64 = 1_048_576;
const CEILINGS: [u64; COUNT] =
@@ -51,30 +39,6 @@ impl RuntimeLimits {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceReason {
Limit,
SizeOverflow,
AllocationFailed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResourceFailure {
pub resource: RuntimeResource,
pub reason: ResourceReason,
pub requested: u64,
pub limit: u64,
}
impl fmt::Display for ResourceFailure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:?}: {:?}, requested {}, limit {}",
self.resource, self.reason, self.requested, self.limit
)
}
}
impl std::error::Error for ResourceFailure {}
#[derive(Debug)]
struct State {
limits: RuntimeLimits,
@@ -1264,6 +1264,9 @@ impl VirtualMachine {
self.operand_stack.truncate(stack_height_before);
}
native_result.map_err(|fault| match fault {
VmFault::ResourceLimit(resource) => {
self.resource_trap(resource, opcode, pc_at_syscall)
}
VmFault::Trap(code, msg) => {
self.trap(code, OpCode::Syscall as u16, msg, pc_at_syscall)
}
@@ -18,7 +18,10 @@ impl VirtualMachine {
opcode: OpCode,
pc: u32,
) -> LogicalFrameEndingReason {
self.trap(TRAP_RESOURCE_LIMIT, opcode as u16, error.to_string(), pc)
let mut trap =
self.program.create_trap(TRAP_RESOURCE_LIMIT, opcode as u16, error.to_string(), pc);
trap.resource = Some(error);
LogicalFrameEndingReason::Trap(trap)
}
pub fn trap(
@@ -7,6 +7,9 @@ pub(super) struct SessionStringAllocator<'a> {
pub budget: RuntimeBudget,
}
impl prometeu_hal::host_return::GuestStringAllocator for SessionStringAllocator<'_> {
fn byte_limit(&self) -> u64 {
self.budget.limit(RuntimeResource::StringBytes)
}
fn build(
&mut self,
length: usize,
@@ -24,7 +27,7 @@ impl prometeu_hal::host_return::GuestStringAllocator for SessionStringAllocator<
}
}
fn resource_fault(resource: ResourceFailure) -> VmFault {
VmFault::Trap(TRAP_RESOURCE_LIMIT, resource.to_string())
VmFault::ResourceLimit(resource)
}
impl VirtualMachine {
@@ -527,7 +527,7 @@ fn native_string_failures_cannot_publish_partial_results_even_if_handler_ignores
// This synthetic native implementation tests the bounded writer, not the
// has-cart handler's ordinary Boolean semantic contract.
assert!(
matches!(report.reason,LogicalFrameEndingReason::Trap(info) if info.code==TRAP_RESOURCE_LIMIT && info.pc==0)
matches!(report.reason,LogicalFrameEndingReason::Trap(info) if info.code==TRAP_RESOURCE_LIMIT && info.pc==0 && info.resource.is_some_and(|resource|resource.resource==RuntimeResource::StringBytes && resource.requested==3 && resource.limit==2))
);
assert!(vm.operand_stack.is_empty());
assert_eq!(vm.budget.used(RuntimeResource::StringBytes), 0);