26 Commits
Author SHA1 Message Date
bquarkz f1adf86dd5 feat(runtime): support Studio Play and separate integration gates
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Add the windowless Studio host with authenticated binary control/frame channels,
managed input, pause/resume/stop, frame ownership and save-aware shutdown.
Preserve deterministic headless execution and add protocol/process fixtures.
Separate unit CI and coverage from opt-in integration/socket qualification,
retaining the prior combined coverage floors in the configuration history.

Validation: tooling make verify passed runtime ci-domains and integration.
2026-11-19 17:32:02 +00:00
bquarkz 2d5735cb13 chore: retire Discussion Framework workflow and retain lessons
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
2026-11-16 16:45:34 +00:00
bquarkz dccf4d0775 fix(ci): declare llvm-tools-preview in pinned toolchain
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
2026-11-16 10:00:49 +00:00
bquarkz c3589be1ad fix(headless): wait for memcard bootstrap IO progress
Intrepid/Prometeu/Runtime/pipeline/head There was a failure building this commit
Coordinate bounded native publication progress during frame advance while preserving watchdogs, guest budgets and resource ownership. Add controlled and packaged reopen regressions, clarify the runtime specs, and retain qualification evidence.
2026-11-10 23:26:40 +00:00
bquarkz 49d7fca0b7 chore(toolchain): pin Rust to 1.98.1
Intrepid/Prometeu/Runtime/pipeline/head There was a failure building this commit
2026-11-10 22:39:19 +00:00
bquarkz c649e02816 feat(headless): expose negotiated native log observation 2026-11-10 21:57:34 +00:00
bquarkz 09d8778f04 fix(runtime): support older stable Rust atomic updates
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
2026-11-05 09:00:19 +00:00
bquarkz 1aa939eca8 fix(runtime): replace deprecated atomic fetch_update calls
Intrepid/Prometeu/Runtime/pipeline/head There was a failure building this commit
2026-11-03 00:01:58 +00:00
bquarkz a4bec5bc45 feat(host): expose memcard controls and qualify process persistence
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
2026-10-30 10:10:07 +00:00
bquarkz dcf6561ea7 refactor(vm)!: remove publication and logical guard opcodes
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Remove INDEX_GUARD, ARRAY_CHECK_LENGTH, STORAGE_PUBLISH and FIELDS_PUBLISH
from decoding, metadata, tooling, verification and execution. Keep ARRAY_LEN
and overlap-safe ARRAY_COPY with complete-group ownership and early charging
of calculable copy work, including refused attempts.

Update specifications, producer handoff, qualification evidence and lessons.
Share collection test support and remove redundant Copy clones for Clippy.

Validation: make ci-domains passed with 1,095 workspace tests, five separately
executed debugger socket tests and all 15 unchanged coverage domain gates.
VM release, resource diagnostics and layer/quality suites also passed.

BREAKING CHANGE: PBX using opcodes 0xA7, 0xA8, 0xAA or 0xAB is rejected,
including unreachable instructions. Consumers must adapt their emission and
collection algorithms or operational guarantees.
2026-10-23 23:37:56 +01:00
bquarkz 69ad1931de feat(vm): add reference identity and deterministic value hashing 2026-10-23 14:29:00 +01:00
Nilton Constantino 57992cf214 feat(vm): publish typed fields across multiple structs atomically
Add schema-driven FIELDS_PUBLISH without collection-specific runtime behavior. Validate every nullable destination and complete field group before allocation-free publication, with neutral ownership, malformed-input and quota coverage.
2026-10-23 09:53:40 +01:00
bquarkz d9b9eeb171 Add generic logical guards and atomic storage publication 2026-10-22 22:24:28 +01:00
bquarkz f7d7c16753 docs: include Random in registry handoff builtin list
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
2026-10-14 15:04:07 +01:00
bquarkz 07b69314b1 Merge pull request 'Dev/runtime improvements for games' (#42) from dev/runtime-improvements-for-games into master
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Reviewed-on: #42
2026-10-14 13:58:56 +00:00
bquarkz 4669ced98e fix(ci): accept installed llvm-tools component name
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
2026-10-14 14:43:09 +01:00
bquarkz a2275932a5 Keep PBX at v0 and simplify Jenkins quality gate
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
2026-10-14 14:35:44 +01:00
bquarkz 39468740c3 docs: close runtime quality review with consolidated lesson
Intrepid/Prometeu/Runtime/pipeline/pr-master There was a failure building this commit
2026-10-14 14:24:14 +01:00
bquarkz 3f9d235546 Index string provenance with metered image and session tables 2026-10-14 13:55:16 +01:00
bquarkz bb6f212779 Share asset decoding validation and reject truncated payloads 2026-10-14 13:08:34 +01:00
bquarkz 418e574ac8 ci: enforce a shared strict repository quality gate 2026-10-14 12:14:11 +01:00
bquarkz 461b554c1a perf(audio): reclaim callback owners through bounded return packets 2026-10-14 11:59:47 +01:00
bquarkz 46bbad19c2 perf(vm): eliminate recurring stack and return allocations 2026-10-14 10:54:11 +01:00
bquarkz e09f1197bc fix(audio): preserve full sample boundaries for one-shot playback 2026-10-14 10:46:03 +01:00
bquarkz ff73f8ad39 fix(coverage): restore specialized domain ownership and floors 2026-10-14 10:35:41 +01:00
bquarkz bad942fb25 fix(memcard): preserve watchdog timeout reasons across completion 2026-10-14 10:26:30 +01:00
223 changed files with 23849 additions and 881 deletions
+19 -9
View File
@@ -13,17 +13,27 @@ jobs:
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Install repository Rust toolchain
run: |
toolchain=$(python3 -c 'import tomllib; print(tomllib.load(open("rust-toolchain.toml", "rb"))["toolchain"]["channel"])')
rustup toolchain install "$toolchain" --profile minimal
- uses: Swatinem/rust-cache@v2
- name: Format check
run: cargo fmt -- --check
- name: Discard reports from previous runs
run: rm -rf target/llvm-cov
- name: Clippy
run: cargo clippy --workspace --all-features
- name: Provision quality-gate tools and native dependencies
run: bash scripts/provision-quality-gate.sh
- name: Test
run: cargo test --workspace --all-targets --all-features --no-fail-fast
- name: Repository quality gate
run: make ci-domains
- name: Publish gate evidence (including failures)
if: always()
uses: actions/upload-artifact@v4
with:
name: quality-gate-${{ runner.os }}-${{ github.sha }}
path: |
target/llvm-cov/**
if-no-files-found: warn
-4
View File
@@ -66,7 +66,3 @@ AGENTS.md
.python-version
.venv
discussion/*
!discussion/lessons/
!discussion/lessons/**
+24 -11
View File
@@ -1,25 +1,32 @@
.PHONY: fmt fmt-check clippy test-local test-debugger-socket clean coverage coverage-xml coverage-json coverage-report-json coverage-domain-list coverage-domain-evidence coverage-domain-audit coverage-domain-gate test ci ci-domains cobertura
.PHONY: fmt fmt-check clippy test-unit test-local test-integration test-debugger-socket clean coverage coverage-xml coverage-json coverage-report-json coverage-domain-list coverage-domain-evidence coverage-domain-audit coverage-domain-gate test ci ci-domains cobertura
fmt:
cargo fmt
cargo fmt --all
fmt-check:
cargo fmt -- --check
cargo fmt --all -- --check
clippy:
cargo clippy --workspace --all-features
cargo clippy --workspace --all-targets --all-features -- -D warnings
test-local:
cargo test --workspace --all-targets --all-features --no-fail-fast
test-unit:
cargo test --workspace --lib --bins --all-features --no-fail-fast
# Compatibility entry point: repository-local verification is unit-only.
test-local: test-unit
# Explicit opt-in, orchestrated by ../tooling; test sources stay in this repository.
test-integration:
cargo test --workspace --tests --all-features --no-fail-fast
test-debugger-socket:
cargo test -p prometeu-host-desktop-winit --lib -- --ignored
cargo test -p prometeu-host-desktop-winit --lib runner::tests -- --ignored
clean:
cargo llvm-cov clean --workspace
coverage:
cargo llvm-cov --workspace --all-features --html --output-dir target/llvm-cov
cargo llvm-cov --workspace --lib --bins --all-features --html --output-dir target/llvm-cov
coverage-report-json:
cargo llvm-cov report --json --output-path target/llvm-cov/report.json
@@ -46,7 +53,13 @@ coverage-domain-evidence:
coverage-domain-gate:
./scripts/coverage-domain-evidence.sh --check-all
test: fmt-check clippy test-local test-debugger-socket
ci: clean fmt-check clippy coverage coverage-report-json
ci-domains: clean fmt-check clippy coverage coverage-report-json coverage-xml coverage-json coverage-domain-gate
test: fmt-check clippy test-unit
# Local and GitHub entry point; Jenkins exposes the same targets as stages.
CI_DOMAIN_STEPS = clean fmt-check clippy test-unit coverage coverage-report-json coverage-xml coverage-json coverage-domain-audit coverage-domain-gate
ci: ci-domains
ci-domains:
rm -rf target/llvm-cov
+@set -e; for step in $(CI_DOMAIN_STEPS); do $(MAKE) --no-print-directory -j1 $$step; done
cobertura: coverage-xml coverage-json
+40 -25
View File
@@ -1,14 +1,14 @@
# PROMETEU Runtime
PROMETEU is an educational and experimental fantasy handheld / fantasy console.
This repository contains the Rust runtime workspace for the machine: VM-facing components, virtual peripherals, firmware/system services, host integration, CLI entrypoints, development utilities, specs, and discussion artifacts.
This repository contains the Rust runtime workspace for the machine: VM-facing components, virtual peripherals, firmware/system services, host integration, CLI entrypoints, development utilities, specs, and engineering lessons.
The VM is only one subsystem of the machine. The canonical docs in this repository intentionally distinguish:
- machine-level specs;
- VM/runtime internal architecture;
- implementation crates;
- discussion workflow and lessons learned.
- engineering lessons learned.
## What This Repository Contains
@@ -19,7 +19,7 @@ The VM is only one subsystem of the machine. The canonical docs in this reposito
- `docs/specs/runtime/`: canonical PROMETEU machine specs
- `docs/vm-arch/`: canonical VM/runtime architecture and ISA references
- `devtools/`: debugger protocol material
- `discussion/`: agendas, decisions, plans, and lessons for architectural work
- `discussion/lessons/`: published engineering lessons and supporting assets
- `test-cartridges/`: cartridge fixtures used for validation and manual runs
## Canonical Documentation
@@ -29,7 +29,11 @@ Use these entrypoints instead of inferring the model from isolated source files:
- [Machine specs](docs/specs/runtime/README.md): authoritative contract for the PROMETEU machine, peripherals, firmware, cartridge format, timing, and host ABI
- [VM architecture](docs/vm-arch/ARCHITECTURE.md): authoritative internal architecture for VM/runtime invariants
- [ISA reference](docs/vm-arch/ISA_CORE.md): bytecode-level instruction set authority
- [Discussion workflow](discussion/index.ndjson): architectural agenda, decision, and execution traceability
- [Engineering lessons](discussion/lessons/): published results and rationale
Engineering discussions, decisions, plans and execution evidence are maintained in
the enclosing [Prometeu Engineering workspace](../README.md) using
[AI Engineering](../.ai-engineering/README.md).
## Workspace Layout
@@ -60,11 +64,21 @@ Other ecosystem commands, such as build, pack, and verification tooling, are not
## Requirements
- Rust toolchain from [rust-toolchain.toml](rust-toolchain.toml)
- Rust **1.98.1**, pinned in [rust-toolchain.toml](rust-toolchain.toml)
- `rustup` for toolchain installation and management
- `cargo-llvm-cov` for coverage targets such as `make coverage` and `make ci-domains`
- a local graphical environment for desktop runtime commands
From the repository root, install and verify the pinned toolchain:
```bash
rustup toolchain install 1.98.1 --profile minimal --component rustfmt,clippy
rustc --version
```
Rustup selects the pinned version automatically for commands run in this repository.
CI installs the toolchain from the same file.
## Quick Start
Build the workspace:
@@ -107,19 +121,26 @@ Format, lint, and test the workspace:
make test
```
Run the CI-oriented non-domain gate:
Run the shared strict CI gate (`make ci` is an alias):
```bash
make ci
```
Run the CI-oriented domain coverage gate:
The authoritative target used by both GitHub and Jenkins is:
```bash
make ci-domains
```
The debugger socket tests bind localhost sockets and are marked ignored in Rust test metadata; `make test` includes them through the dedicated `test-debugger-socket` target.
`make test-unit` selects library and binary unit-test harnesses with
`--lib --bins --all-features`; `test-local` remains an alias. Both `make test`
and `make ci-domains` run only this suite. Integration targets under `tests/`,
installed-process qualification and the five ignored debugger socket tests are
excluded from the pipeline. They remain available explicitly through
`make test-integration` and `make test-debugger-socket`, orchestrated by
`../tooling`. Tests and fixtures stay in this repository. Physical audio and manual probes remain opt-in. See
[quality-gate prerequisites and commands](docs/vm-arch/COVERAGE_DOMAINS.md#commands-and-ci).
## Coverage Governance
@@ -128,20 +149,14 @@ The repository uses a two-layer runtime-edge coverage model:
- a mandatory CI gate based on canonical runtime-edge domains;
- domain-oriented review evidence for the canonical runtime-edge domains.
Current canonical domains:
- `system/runtime`
- `fs`
- `asset/bank`
- `firmware`
- `host-dependent`
- `host/headless`
The fifteen canonical domains and fixed floors are defined in
[the repository configuration](files/config/runtime-edge-coverage-domains.json).
Use `make coverage-domain-list` or the [ownership documentation](docs/vm-arch/COVERAGE_DOMAINS.md)
to inspect the current map.
Global coverage artifacts:
```bash
make ci
make cobertura
make ci-domains
```
@@ -161,12 +176,12 @@ make coverage-domain-evidence DOMAIN=host-dependent
make coverage-domain-evidence DOMAIN=host/headless
```
Per-domain baselines currently start around `60`.
That does not waive test obligations. It means the project is adopting explicit domain evidence immediately while tightening quantitative expectations incrementally over time.
`make ci-domains` is the CI entrypoint used by Jenkins.
It runs the standard coverage pipeline, produces the HTML/XML/JSON artifacts, and fails when any canonical domain falls below its configured `baseline_percent`.
The CI gate currently compares domain `lines` coverage against each domain baseline.
`make ci-domains` runs formatting, strict workspace/all-target/all-feature Clippy,
library/binary unit tests, fresh unit-only coverage and every domain
ownership/evidence gate. Both CI providers archive available evidence on failure.
A failing run never lowers a configured floor; local and CI checks use the same
repository-owned commands. Reports identify their revision, source/configuration
and actual execution platform.
When a PR changes the observable contract of a canonical domain, it is expected to:
@@ -179,7 +194,7 @@ Improving the global percentage alone is not sufficient review evidence when the
The project is still in active architectural and implementation convergence.
- the machine contract is being clarified through the specs and discussion workflow;
- the machine contract is being clarified through the specs and AI Engineering;
- the workspace already contains concrete runtime and host code;
- the CLI dispatcher currently exposes the runtime paths that are backed by binaries in this workspace.
@@ -179,7 +179,7 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
emit_u16(CoreOpCode::Ret as u16, &mut out);
}
"YIELD" | "SLEEP" | "SPAWN" => {
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()))
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()));
}
"FRAME_SYNC" => {
emit_u16(CoreOpCode::FrameSync as u16, &mut out);
@@ -345,7 +345,7 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
emit_u32(ty, &mut out);
emit_u32(field, &mut out);
}
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" => {
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" | "ARRAY_COPY" => {
if ops.is_empty() {
return Err(AsmError::MissingOperand(line.into()));
}
@@ -354,23 +354,32 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
"ARRAY_NEW" => CoreOpCode::ArrayNew,
"ARRAY_LEN" => CoreOpCode::ArrayLen,
"ARRAY_GET" => CoreOpCode::ArrayGet,
"ARRAY_COPY" => CoreOpCode::ArrayCopy,
_ => CoreOpCode::ArraySet,
};
emit_u16(op as u16, &mut out);
emit_u32(parse_u32_any(ops)?, &mut out);
}
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" => {
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" | "REF_IDENTITY_HASH" => {
if ops.is_empty() {
return Err(AsmError::MissingOperand(line.into()));
}
let opcode = match mn {
"DYN_PACK" => CoreOpCode::DynPack,
"DYN_UNPACK" => CoreOpCode::DynUnpack,
"REF_IDENTITY_HASH" => CoreOpCode::RefIdentityHash,
_ => CoreOpCode::DynCanUnpack,
};
emit_u16(opcode as u16, &mut out);
emit_u32(parse_u32_any(ops)?, &mut out);
}
"REF_IDENTITY_EQ" => {
let (a, b) = parse_keyvals(ops)?;
let (left, right) = parse_pair(a, "left", b, "right")?;
emit_u16(CoreOpCode::RefIdentityEq as u16, &mut out);
emit_u32(left, &mut out);
emit_u32(right, &mut out);
}
"REF_IS_EXACT" | "REF_IMPLEMENTS" | "REF_TO_TRAIT" | "REF_CAST" => {
if ops.is_empty() {
return Err(AsmError::MissingOperand(line.into()));
@@ -65,12 +65,14 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
| CoreOpCode::Hostcall
| CoreOpCode::DynPack
| CoreOpCode::DynUnpack
| CoreOpCode::RefIdentityHash
| CoreOpCode::DynCanUnpack
| CoreOpCode::StructNew
| CoreOpCode::ArrayNew
| CoreOpCode::ArrayLen
| CoreOpCode::ArrayGet
| CoreOpCode::ArraySet => {
| CoreOpCode::ArraySet
| CoreOpCode::ArrayCopy => {
let v = u32::from_le_bytes(imm.try_into().unwrap());
format!("{}", v)
}
@@ -79,6 +81,11 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
let field = u32::from_le_bytes(imm[4..].try_into().unwrap());
format!("type={}, field={}", ty, field)
}
CoreOpCode::RefIdentityEq => {
let left = u32::from_le_bytes(imm[..4].try_into().unwrap());
let right = u32::from_le_bytes(imm[4..].try_into().unwrap());
format!("left={}, right={}", left, right)
}
CoreOpCode::RefIsExact
| CoreOpCode::RefImplements
| CoreOpCode::RefToTrait
@@ -1,4 +1,4 @@
//! Shared PBX v1 resource limits and structured validation failures.
//! Shared PBX v0 resource limits and structured validation failures.
//! Counters are deterministic; a failed reservation never changes usage.
use serde::{Deserialize, Serialize};
use std::fmt;
@@ -192,6 +192,10 @@ pub enum OpCode {
RefToTrait = 0x95,
/// REF_CAST: source:TypeId, target:TypeId; requires validated Registry metadata.
RefCast = 0x96,
/// Query a complete typed reference identity; source TypeId.
RefIdentityHash = 0x97,
/// Compare two complete typed references; left/right TypeIds.
RefIdentityEq = 0x98,
// Registry-backed collection operations. cycles() is their base component.
StructNew = 0xA0,
@@ -201,6 +205,8 @@ pub enum OpCode {
ArrayLen = 0xA4,
ArrayGet = 0xA5,
ArraySet = 0xA6,
/// Complete stored groups, overlap-safe; Array TypeId:u32.
ArrayCopy = 0xA9,
}
impl TryFrom<u16> for OpCode {
@@ -267,6 +273,8 @@ impl TryFrom<u16> for OpCode {
0x94 => Ok(OpCode::RefImplements),
0x95 => Ok(OpCode::RefToTrait),
0x96 => Ok(OpCode::RefCast),
0x97 => Ok(OpCode::RefIdentityHash),
0x98 => Ok(OpCode::RefIdentityEq),
0xA0 => Ok(OpCode::StructNew),
0xA1 => Ok(OpCode::StructGet),
0xA2 => Ok(OpCode::StructSet),
@@ -274,6 +282,7 @@ impl TryFrom<u16> for OpCode {
0xA4 => Ok(OpCode::ArrayLen),
0xA5 => Ok(OpCode::ArrayGet),
0xA6 => Ok(OpCode::ArraySet),
0xA9 => Ok(OpCode::ArrayCopy),
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
}
@@ -344,6 +353,8 @@ impl OpCode {
OpCode::RefImplements => 4,
OpCode::RefToTrait => 5,
OpCode::RefCast => 4,
OpCode::RefIdentityHash => 8,
OpCode::RefIdentityEq => 4,
OpCode::StructNew => 8,
OpCode::StructGet => 3,
OpCode::StructSet => 4,
@@ -351,6 +362,7 @@ impl OpCode {
OpCode::ArrayLen => 3,
OpCode::ArrayGet => 3,
OpCode::ArraySet => 4,
OpCode::ArrayCopy => 4,
}
}
}
@@ -596,6 +596,26 @@ impl OpCodeSpecExt for OpCode {
may_trap: true,
is_safepoint: false,
},
OpCode::RefIdentityHash => OpcodeSpec {
name: "REF_IDENTITY_HASH",
imm_bytes: 4,
pops: None,
pushes: None,
is_branch: false,
is_terminator: false,
may_trap: true,
is_safepoint: false,
},
OpCode::RefIdentityEq => OpcodeSpec {
name: "REF_IDENTITY_EQ",
imm_bytes: 8,
pops: None,
pushes: None,
is_branch: false,
is_terminator: false,
may_trap: true,
is_safepoint: false,
},
OpCode::StructNew => OpcodeSpec {
name: "STRUCT_NEW",
imm_bytes: 4,
@@ -666,6 +686,16 @@ impl OpCodeSpecExt for OpCode {
may_trap: true,
is_safepoint: false,
},
OpCode::ArrayCopy => OpcodeSpec {
name: "ARRAY_COPY",
imm_bytes: 4,
pops: None,
pushes: None,
is_branch: false,
is_terminator: false,
may_trap: true,
is_safepoint: false,
},
OpCode::FrameSync => OpcodeSpec {
name: "FRAME_SYNC",
imm_bytes: 0,
@@ -52,6 +52,19 @@ pub enum TypeKind {
Trait(Vec<MethodDecl>),
Fn(Signature),
}
impl TypeKind {
/// Categories supported by identity queries, not by legacy REF_* conversions.
pub fn is_identity_reference(&self) -> bool {
matches!(
self,
Self::Struct(_)
| Self::Array(_)
| Self::Fn(_)
| Self::Trait(_)
| Self::Builtin(BuiltinId::RANDOM)
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeDecl {
pub name: Option<String>,
@@ -1,4 +1,4 @@
//! Strict PBX v1 envelope and Registry sections. No legacy fallback.
//! Strict PBX v0 envelope and Registry sections. No legacy fallback.
//! Envelope parsing is structural only: non-Registry sections and instructions
//! must be validated by their consumers before an executable image is published.
use crate::load_limits::{
@@ -32,7 +32,7 @@ impl<'a> PbxEnvelope<'a> {
if &bytes[..4] != b"PBX\0" {
return Err(E::new(C::InvalidFormat, R::Magic));
}
if bytes[4..6] != [1, 0] {
if bytes[4..6] != [0, 0] {
return Err(E::new(C::UnsupportedFormat, R::Version));
}
if bytes[6] != 0 {
@@ -314,14 +314,14 @@ impl<'a, 'b> Reader<'a, 'b> {
}
}
/// Raw section input for a v1 envelope writer. Non-Registry section bytes must
/// Raw section input for a v0 envelope writer. Non-Registry section bytes must
/// come from their own validated writers; this API never invents defaults.
#[derive(Clone, Copy)]
pub struct SectionData<'a> {
pub kind: u32,
pub bytes: &'a [u8],
}
/// Assemble strict v1 sections. The output buffer is charged to temporary load storage.
/// Assemble strict v0 sections. The output buffer is charged to temporary load storage.
pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Result<Vec<u8>, E> {
b.transaction(|b| {
let mut by_kind = [None; 9];
@@ -349,7 +349,7 @@ pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Resu
b.work(total)?;
let mut out = b.vector(total, Q::TemporaryBytes)?;
out.extend_from_slice(b"PBX\0");
out.extend_from_slice(&[1, 0, 0, 0]);
out.extend_from_slice(&[0, 0, 0, 0]);
out.extend_from_slice(&(count as u32).to_le_bytes());
out.extend_from_slice(&[0; 20]);
let mut offset = directory_end;
@@ -6,7 +6,7 @@ use crate::load_limits::IdDomain;
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
use std::cmp::Ordering;
/// Owned, bounded PBX v1 tables for the executable loader. This is not an
/// Owned, bounded PBX v0 tables for the executable loader. This is not an
/// executable image: bindings and instruction/type verification are still due.
#[derive(Debug)]
pub struct OwnedPbx {
@@ -1,5 +1,5 @@
//! Producer-side serialization of a complete typed module. This writer emits
//! only v1; executable consumers still need bindings and code verification.
//! only v0; executable consumers still need bindings and code verification.
use super::*;
use crate::load_limits::IdDomain;
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
@@ -86,7 +86,7 @@ impl DecodedInstr<'_> {
)
}
Op::GetCapture => {
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex))
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex));
}
Op::CallTrait => {
let (ty, method) = self.imm_u32x2().map_err(|_| malformed())?;
@@ -133,6 +133,29 @@ impl DecodedInstr<'_> {
};
(source_layout.storage.slots, pushes)
}
Op::RefIdentityHash | Op::RefIdentityEq => {
let (left, right) = if self.opcode == Op::RefIdentityHash {
(self.imm_u32().map_err(|_| malformed())?, None)
} else {
let (a, b) = self.imm_u32x2().map_err(|_| malformed())?;
(a, Some(b))
};
let mut pops = 0;
for id in std::iter::once(left).chain(right) {
let (decl, layout) = type_info(id)?;
if !decl.kind.is_identity_reference() {
return Err(bad_category(id));
}
pops += layout.storage.slots;
}
(pops, 1)
}
Op::ArrayCopy => {
let array = self.imm_u32().map_err(|_| malformed())?;
let (_, layout) = type_info(array)?;
layout.array.ok_or_else(|| bad_category(array))?;
(5, 0)
}
Op::StructNew
| Op::StructGet
| Op::StructSet
@@ -34,6 +34,8 @@ fn all_typed_instructions_match_independent_bytes_and_costs() {
("REF_IMPLEMENTS source=16909060, target=84281096", &[0x94, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
("REF_TO_TRAIT source=16909060, target=84281096", &[0x95, 0, 4, 3, 2, 1, 8, 7, 6, 5], 5),
("REF_CAST source=16909060, target=84281096", &[0x96, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
("REF_IDENTITY_HASH 16909060", &[0x97, 0, 4, 3, 2, 1], 8),
("REF_IDENTITY_EQ left=16909060, right=84281096", &[0x98, 0, 4, 3, 2, 1, 8, 7, 6, 5], 4),
];
for &(text, bytes, cycles) in cases {
assert_eq!(assemble(text).unwrap(), bytes);
@@ -49,8 +51,8 @@ fn all_typed_instructions_match_independent_bytes_and_costs() {
}
}
assert!(matches!(
decode_next(0, &[0x97, 0]),
Err(DecodeError::UnknownOpcode { opcode: 0x97, .. })
decode_next(0, &[0x99, 0]),
Err(DecodeError::UnknownOpcode { opcode: 0x99, .. })
));
}
#[test]
@@ -68,6 +70,8 @@ fn slot_effects_resolve_semantic_widths_and_reference_categories() {
("REF_TO_TRAIT source=4, target=6", 1, 2),
("REF_TO_TRAIT source=6, target=6", 2, 2),
("REF_CAST source=6, target=4", 2, 1),
("REF_IDENTITY_HASH 6", 2, 1),
("REF_IDENTITY_EQ left=6, right=4", 3, 1),
] {
let bytes = assemble(text).unwrap();
assert_eq!(
@@ -94,6 +98,9 @@ fn invalid_type_immediates_are_contextual_load_errors() {
"REF_IS_EXACT source=4, target=6",
"REF_TO_TRAIT source=4, target=0",
"REF_IMPLEMENTS source=0, target=6",
"REF_IDENTITY_HASH 0",
"REF_IDENTITY_HASH 3",
"REF_IDENTITY_EQ left=4, right=1",
] {
let bytes = assemble(text).unwrap();
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(&registry).unwrap_err();
@@ -114,6 +121,10 @@ fn assembler_rejects_missing_overflowing_or_ambiguous_type_ids() {
"REF_CAST source=4",
"REF_CAST source=4, target=5, target=6",
"REF_CAST source=4, trait=6",
"REF_IDENTITY_HASH",
"REF_IDENTITY_HASH -1",
"REF_IDENTITY_EQ left=4",
"REF_IDENTITY_EQ left=4, target=5",
] {
assert!(assemble(text).is_err(), "{text}");
}
@@ -204,3 +215,62 @@ fn collection_effects_and_cached_initialization_width_are_exact() {
);
}
}
#[test]
fn removed_storage_encodings_and_mnemonics_are_unsupported() {
for opcode in [0xa7u16, 0xa8, 0xaa, 0xab] {
assert!(Op::try_from(opcode).is_err());
let bytes = opcode.to_le_bytes();
assert!(
matches!(decode_next(0,&bytes),Err(DecodeError::UnknownOpcode{pc:0,opcode: actual}) if actual == opcode)
);
assert!(disassemble(&bytes).is_err());
}
for text in
["INDEX_GUARD", "ARRAY_CHECK_LENGTH 1", "STORAGE_PUBLISH 1,2,0,1", "FIELDS_PUBLISH 1"]
{
assert!(assemble(text).is_err(), "{text}");
}
}
#[test]
fn range_copy_encoding_effects_and_registry_categories_are_independent_oracles() {
let bytes = [0xa9, 0, 4, 3, 2, 1];
assert_eq!(assemble("ARRAY_COPY 16909060").unwrap(), bytes);
assert_eq!(disassemble(&bytes).unwrap(), "ARRAY_COPY 16909060");
let instruction = decode_next(0, &bytes).unwrap();
assert_eq!(instruction.opcode, Op::ArrayCopy);
assert_eq!(instruction.opcode.cycles(), 4);
let spec = instruction.opcode.spec();
assert_eq!((spec.imm_bytes, spec.pops, spec.pushes), (4, None, None));
assert!(instruction.opcode.requires_registry() && spec.may_trap);
assert!(!spec.is_safepoint && !spec.is_branch && !spec.is_terminator);
for end in 0..6 {
assert!(decode_next(0, &bytes[..end]).is_err());
}
let registry = registry();
let bytes = assemble("ARRAY_COPY 5").unwrap();
assert_eq!(
decode_next(0, &bytes).unwrap().registry_stack_effect(&registry).unwrap(),
Some(RegistryStackEffect { pops: 5, pushes: 0 })
);
for (id, reason) in [
(0, LoadReason::Category),
(4, LoadReason::Category),
(6, LoadReason::Category),
(99, LoadReason::TypeId),
] {
let bytes = assemble(&format!("ARRAY_COPY {id}")).unwrap();
let error = decode_next(0, &bytes).unwrap().registry_stack_effect(&registry).unwrap_err();
assert_eq!(
(error.code, error.reason, error.pc),
(LoadCode::InvalidReference, reason, Some(0))
);
}
for text in ["ARRAY_COPY", "ARRAY_COPY -1", "ARRAY_COPY 4294967296", "ARRAY_COPY 1 2"] {
assert!(assemble(text).is_err());
}
// These are legal immediate bytes, not instruction boundaries.
let bytes = assemble("PUSH_I64 18735214094965159\nHALT").unwrap();
assert!(disassemble(&bytes).is_ok());
}
@@ -172,7 +172,7 @@ fn hex(s: &str) -> Vec<u8> {
// syscalls=168/4, types=172/10, implementations=182/4, captures=186/8. Total=194.
fn minimal_bytes() -> Vec<u8> {
hex("
50 42 58 00 01 00 00 00 08 00 00 00
50 42 58 00 00 00 00 00 08 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 80 00 00 00 04 00 00 00
01 00 00 00 84 00 00 00 1e 00 00 00
@@ -241,7 +241,7 @@ fn writer_matches_independent_minimal_envelope() {
}
#[test]
fn owned_v1_decoder_accepts_independent_bytes_without_publishing_an_image() {
fn owned_v0_decoder_accepts_independent_bytes_without_publishing_an_image() {
let mut b = budget();
let owned = OwnedPbx::parse(&minimal_bytes(), &mut b).unwrap();
assert_eq!(owned.registry.declarations(), minimal_registry().declarations());
@@ -536,7 +536,7 @@ fn every_truncated_envelope_is_rejected_without_panic() {
fn envelope_mutations_have_typed_reasons() {
for (offset, value, code, reason) in [
(0, 0, LoadCode::InvalidFormat, LoadReason::Magic),
(4, 0, LoadCode::UnsupportedFormat, LoadReason::Version),
(4, 1, LoadCode::UnsupportedFormat, LoadReason::Version),
(6, 1, LoadCode::InvalidFormat, LoadReason::Endianness),
(7, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
(31, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
+26 -226
View File
@@ -1,4 +1,5 @@
#![allow(clippy::collapsible_if)]
mod decode;
mod resumable;
use crate::memory_banks::{GlyphBankPoolInstaller, SceneBankPoolInstaller, SoundBankPoolInstaller};
use prometeu_hal::AssetBridge;
@@ -634,62 +635,13 @@ impl AssetManager {
fn decode_glyph_bank_layout(
entry: &AssetEntry,
) -> Result<(TileSize, usize, usize, usize, usize, usize), String> {
let meta = entry.metadata_as_glyph_bank()?;
let tile_size = match meta.tile_size {
8 => TileSize::Size8,
16 => TileSize::Size16,
32 => TileSize::Size32,
_ => return Err(format!("Invalid tile_size: {}", meta.tile_size)),
};
let palette_count = meta.palette_count as usize;
if !(1..=GLYPH_BANK_MAX_PALETTE_COUNT_V1).contains(&palette_count) {
return Err(format!("Invalid palette_count: {}", meta.palette_count));
}
let width = meta.width as usize;
let height = meta.height as usize;
let logical_pixels = width.checked_mul(height).ok_or("GlyphBank dimensions overflow")?;
let serialized_pixel_bytes = logical_pixels.div_ceil(2);
let palette_bytes = palette_count
.checked_mul(GLYPH_BANK_PALETTE_BYTES_PER_PALETTE)
.ok_or("GlyphBank palette size overflow")?;
let serialized_size = serialized_pixel_bytes
.checked_add(palette_bytes)
.ok_or("GlyphBank serialized size overflow")?;
let decoded_size =
logical_pixels.checked_add(palette_bytes).ok_or("GlyphBank decoded size overflow")?;
if entry.size != serialized_size as u64 {
return Err(format!(
"Invalid GLYPHBANK serialized size: expected {}, got {}",
serialized_size, entry.size
));
}
if entry.decoded_size != decoded_size as u64 {
return Err(format!(
"Invalid GLYPHBANK decoded_size: expected {}, got {}",
decoded_size, entry.decoded_size
));
}
Ok((tile_size, width, height, serialized_pixel_bytes, palette_count, palette_bytes))
decode::glyph_layout(entry)
}
fn unpack_glyph_bank_pixels(packed_pixels: &[u8], logical_pixels: usize) -> Vec<u8> {
let mut pixel_indices = Vec::with_capacity(logical_pixels);
for &packed in packed_pixels {
if pixel_indices.len() < logical_pixels {
pixel_indices.push(packed >> 4);
}
if pixel_indices.len() < logical_pixels {
pixel_indices.push(packed & 0x0f);
}
}
pixel_indices
let mut pixels = Vec::with_capacity(logical_pixels);
decode::append_pixels(&mut pixels, packed_pixels, logical_pixels);
pixels
}
fn op_mode_for(entry: &AssetEntry) -> Result<AssetOpMode, String> {
@@ -1260,17 +1212,7 @@ impl AssetManager {
let palette_data = &buffer[packed_pixel_bytes..packed_pixel_bytes + palette_bytes];
let mut palettes = vec![[Color::BLACK; GLYPH_BANK_COLORS_PER_PALETTE]; palette_count];
for (p, pal) in palettes.iter_mut().enumerate() {
for (c, slot) in pal.iter_mut().enumerate() {
let offset = (p * 16 + c) * 4;
*slot = Color::rgba(
palette_data[offset],
palette_data[offset + 1],
palette_data[offset + 2],
palette_data[offset + 3],
);
}
}
decode::write_palettes(&mut palettes, 0, palette_data);
Ok(GlyphBank { tile_size, width, height, pixel_indices, palettes })
}
@@ -1295,17 +1237,7 @@ impl AssetManager {
.map_err(|_| "Buffer too small for GLYPHBANK".to_string())?;
let mut palettes = vec![[Color::BLACK; GLYPH_BANK_COLORS_PER_PALETTE]; palette_count];
for (p, pal) in palettes.iter_mut().enumerate() {
for (c, slot) in pal.iter_mut().enumerate() {
let offset = (p * 16 + c) * 4;
*slot = Color::rgba(
palette_data[offset],
palette_data[offset + 1],
palette_data[offset + 2],
palette_data[offset + 3],
);
}
}
decode::write_palettes(&mut palettes, 0, &palette_data);
Ok(GlyphBank { tile_size, width, height, pixel_indices, palettes })
}
@@ -1340,7 +1272,7 @@ impl AssetManager {
entry: &AssetEntry,
assets_data: Arc<RwLock<AssetsPayloadSource>>,
) -> Result<SceneBank, String> {
let _ = entry.metadata_as_scene_bank()?;
decode::scene_metadata(entry)?;
let op_mode = Self::op_mode_for(entry)?;
let slice = {
let assets_data = assets_data.read().unwrap();
@@ -1367,152 +1299,25 @@ impl AssetManager {
entry: &AssetEntry,
buffer: &[u8],
) -> Result<SceneBank, String> {
let _ = entry.metadata_as_scene_bank()?;
if buffer.len() < SCENE_HEADER_BYTES_V1 {
return Err("Buffer too small for SCENE".to_string());
}
if buffer[0..4] != SCENE_PAYLOAD_MAGIC_V1 {
return Err("Invalid SCENE magic".to_string());
}
let version = u16::from_le_bytes([buffer[4], buffer[5]]);
if version != SCENE_PAYLOAD_VERSION_V1 {
return Err(format!("Unsupported SCENE version: {}", version));
}
let layer_count = u16::from_le_bytes([buffer[6], buffer[7]]) as usize;
if layer_count != SCENE_LAYER_COUNT_V1 {
return Err(format!("Invalid SCENE layer count: {}", layer_count));
}
decode::scene_metadata(entry)?;
decode::payload_size(entry, buffer.len())?;
decode::scene_header(buffer)?;
let mut offset = SCENE_HEADER_BYTES_V1;
let mut decoded_size = 0_usize;
let layers = std::array::from_fn(|_| SceneLayer {
active: false,
glyph_asset_id: 0,
tile_size: TileSize::Size8,
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
tilemap: TileMap { width: 0, height: 0, tiles: Vec::new() },
});
let mut layers = layers;
let mut decoded = 0;
let mut layers = decode::empty_layers();
for layer in &mut layers {
let header_end = offset
let end = offset
.checked_add(SCENE_LAYER_HEADER_BYTES_V1)
.ok_or("SCENE layer header offset overflow")?;
if header_end > buffer.len() {
return Err("Buffer too small for SCENE layer header".to_string());
}
let flags = buffer[offset];
let glyph_asset_id = i32::from_le_bytes([
buffer[offset + 1],
buffer[offset + 2],
buffer[offset + 3],
buffer[offset + 4],
]);
let tile_size_raw = buffer[offset + 5];
let tile_size = match tile_size_raw {
8 => TileSize::Size8,
16 => TileSize::Size16,
32 => TileSize::Size32,
other => return Err(format!("Invalid SCENE tile size: {}", other)),
};
let parallax_factor_x = f32::from_le_bytes([
buffer[offset + 8],
buffer[offset + 9],
buffer[offset + 10],
buffer[offset + 11],
]);
let parallax_factor_y = f32::from_le_bytes([
buffer[offset + 12],
buffer[offset + 13],
buffer[offset + 14],
buffer[offset + 15],
]);
if !parallax_factor_x.is_finite() || !parallax_factor_y.is_finite() {
return Err("Invalid SCENE parallax_factor".to_string());
}
let width = u32::from_le_bytes([
buffer[offset + 16],
buffer[offset + 17],
buffer[offset + 18],
buffer[offset + 19],
]) as usize;
let height = u32::from_le_bytes([
buffer[offset + 20],
buffer[offset + 21],
buffer[offset + 22],
buffer[offset + 23],
]) as usize;
let tile_count = u32::from_le_bytes([
buffer[offset + 24],
buffer[offset + 25],
buffer[offset + 26],
buffer[offset + 27],
]) as usize;
let expected_tile_count =
width.checked_mul(height).ok_or("SCENE tile count overflow")?;
if tile_count != expected_tile_count {
return Err(format!(
"Invalid SCENE tile count for layer: expected {}, got {}",
expected_tile_count, tile_count
));
}
offset = header_end;
let tile_bytes = tile_count
.checked_mul(SCENE_TILE_RECORD_BYTES_V1)
.ok_or("SCENE tile payload overflow")?;
let tiles_end = offset.checked_add(tile_bytes).ok_or("SCENE payload overflow")?;
if tiles_end > buffer.len() {
return Err("Buffer too small for SCENE tile data".to_string());
}
let mut tiles = Vec::with_capacity(tile_count);
for _ in 0..tile_count {
let tile_flags = buffer[offset];
let palette_id = buffer[offset + 1];
let glyph_id = u16::from_le_bytes([buffer[offset + 2], buffer[offset + 3]]);
tiles.push(Tile {
active: (tile_flags & 0b0000_0001) != 0,
glyph: Glyph { glyph_id, palette_id },
flip_x: (tile_flags & 0b0000_0010) != 0,
flip_y: (tile_flags & 0b0000_0100) != 0,
});
offset += SCENE_TILE_RECORD_BYTES_V1;
}
decoded_size = decoded_size
.checked_add(SCENE_DECODED_LAYER_OVERHEAD_BYTES_V1)
.and_then(|size| size.checked_add(tile_count * size_of::<Tile>()))
.ok_or("SCENE decoded_size overflow")?;
*layer = SceneLayer {
active: (flags & 0b0000_0001) != 0,
glyph_asset_id,
tile_size,
parallax_factor: ParallaxFactor { x: parallax_factor_x, y: parallax_factor_y },
tilemap: TileMap { width, height, tiles },
};
.ok_or("SCENE header offset overflow")?;
let header =
buffer.get(offset..end).ok_or("Buffer too small for SCENE layer header")?;
*layer = decode::scene_layer(header, end, entry.size, &mut decoded)?;
let tile_bytes =
layer.tilemap.width * layer.tilemap.height * SCENE_TILE_RECORD_BYTES_V1;
offset = end + tile_bytes; // checked by scene_layer before allocation
decode::append_tiles(&mut layer.tilemap.tiles, &buffer[end..offset]);
}
if offset != buffer.len() {
return Err("Trailing bytes in SCENE payload".to_string());
}
if entry.decoded_size != decoded_size as u64 {
return Err(format!(
"Invalid SCENE decoded_size: expected {}, got {}",
decoded_size, entry.decoded_size
));
}
decode::scene_finish(entry, offset, decoded)?;
Ok(SceneBank { layers })
}
@@ -1529,15 +1334,10 @@ impl AssetManager {
entry: &AssetEntry,
buffer: &[u8],
) -> Result<SoundBank, String> {
let meta = entry.metadata_as_sound_bank()?;
let sample_rate = meta.sample_rate;
let sample_rate = decode::sound_rate(entry)?;
decode::payload_size(entry, buffer.len())?;
let mut data = Vec::with_capacity(buffer.len() / 2);
for i in (0..buffer.len()).step_by(2) {
if i + 1 < buffer.len() {
data.push(i16::from_le_bytes([buffer[i], buffer[i + 1]]));
}
}
decode::append_pcm(&mut data, buffer);
let sample = Arc::new(Sample::new(sample_rate, data));
Ok(SoundBank::new(vec![sample]))
@@ -0,0 +1,187 @@
//! Format authority shared by complete-buffer and bounded resumable decoding.
use super::*;
pub(super) fn glyph_layout(
entry: &AssetEntry,
) -> Result<(TileSize, usize, usize, usize, usize, usize), String> {
let meta = entry.metadata_as_glyph_bank()?;
let tile_size = match meta.tile_size {
8 => TileSize::Size8,
16 => TileSize::Size16,
32 => TileSize::Size32,
_ => return Err(format!("Invalid tile_size: {}", meta.tile_size)),
};
let palette_count = meta.palette_count as usize;
if !(1..=GLYPH_BANK_MAX_PALETTE_COUNT_V1).contains(&palette_count) {
return Err(format!("Invalid palette_count: {}", meta.palette_count));
}
let width = meta.width as usize;
let height = meta.height as usize;
let logical_pixels = width.checked_mul(height).ok_or("GlyphBank dimensions overflow")?;
let serialized_pixel_bytes = logical_pixels.div_ceil(2);
let palette_bytes = palette_count
.checked_mul(GLYPH_BANK_PALETTE_BYTES_PER_PALETTE)
.ok_or("GlyphBank palette size overflow")?;
let serialized_size = serialized_pixel_bytes
.checked_add(palette_bytes)
.ok_or("GlyphBank serialized size overflow")?;
let decoded_size =
logical_pixels.checked_add(palette_bytes).ok_or("GlyphBank decoded size overflow")?;
if entry.size != serialized_size as u64 {
return Err(format!(
"Invalid GLYPHBANK serialized size: expected {}, got {}",
serialized_size, entry.size
));
}
if entry.decoded_size != decoded_size as u64 {
return Err(format!(
"Invalid GLYPHBANK decoded_size: expected {}, got {}",
decoded_size, entry.decoded_size
));
}
Ok((tile_size, width, height, serialized_pixel_bytes, palette_count, palette_bytes))
}
pub(super) fn append_pixels(pixels: &mut Vec<u8>, bytes: &[u8], logical: usize) {
for &packed in bytes {
if pixels.len() < logical {
pixels.push(packed >> 4);
}
if pixels.len() < logical {
pixels.push(packed & 15);
}
}
}
pub(super) fn write_palettes(
palettes: &mut [[Color; GLYPH_BANK_COLORS_PER_PALETTE]],
position: usize,
bytes: &[u8],
) {
for (i, rgba) in bytes.as_chunks::<4>().0.iter().enumerate() {
let at = position / 4 + i;
palettes[at / 16][at % 16] = Color::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
}
}
pub(super) fn sound_rate(entry: &AssetEntry) -> Result<u32, String> {
Ok(entry.metadata_as_sound_bank()?.sample_rate)
}
// Callers split on even chunk boundaries. Only the final odd byte is ignored.
pub(super) fn append_pcm(data: &mut Vec<i16>, bytes: &[u8]) {
data.extend(bytes.as_chunks::<2>().0.iter().map(|p| i16::from_le_bytes(*p)));
}
pub(super) fn payload_size(entry: &AssetEntry, actual: usize) -> Result<(), String> {
if actual as u64 != entry.size {
return Err("Invalid asset payload size".into());
}
Ok(())
}
pub(super) fn scene_metadata(entry: &AssetEntry) -> Result<(), String> {
entry.metadata_as_scene_bank().map(|_| ())
}
pub(super) fn empty_layers() -> [SceneLayer; SCENE_LAYER_COUNT_V1] {
std::array::from_fn(|_| SceneLayer {
active: false,
glyph_asset_id: 0,
tile_size: TileSize::Size8,
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
tilemap: TileMap { width: 0, height: 0, tiles: Vec::new() },
})
}
pub(super) fn scene_header(bytes: &[u8]) -> Result<(), String> {
if bytes.len() < SCENE_HEADER_BYTES_V1 {
return Err("Buffer too small for SCENE".into());
}
if bytes[..4] != SCENE_PAYLOAD_MAGIC_V1 {
return Err("Invalid SCENE magic".into());
}
let version = u16::from_le_bytes([bytes[4], bytes[5]]);
if version != SCENE_PAYLOAD_VERSION_V1 {
return Err(format!("Unsupported SCENE version: {version}"));
}
let count = u16::from_le_bytes([bytes[6], bytes[7]]) as usize;
if count != SCENE_LAYER_COUNT_V1 {
return Err(format!("Invalid SCENE layer count: {count}"));
}
Ok(())
}
pub(super) fn scene_layer(
bytes: &[u8],
position: usize,
payload_len: u64,
decoded: &mut usize,
) -> Result<SceneLayer, String> {
if bytes.len() < SCENE_LAYER_HEADER_BYTES_V1 {
return Err("Buffer too small for SCENE layer header".into());
}
let u32at = |at| u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap());
let tile_size = match bytes[5] {
8 => TileSize::Size8,
16 => TileSize::Size16,
32 => TileSize::Size32,
other => return Err(format!("Invalid SCENE tile size: {other}")),
};
let x = f32::from_le_bytes(bytes[8..12].try_into().unwrap());
let y = f32::from_le_bytes(bytes[12..16].try_into().unwrap());
if !x.is_finite() || !y.is_finite() {
return Err("Invalid SCENE parallax_factor".into());
}
let width = u32at(16) as usize;
let height = u32at(20) as usize;
let count = u32at(24) as usize;
let expected = width.checked_mul(height).ok_or("SCENE tile count overflow")?;
if count != expected {
return Err(format!(
"Invalid SCENE tile count for layer: expected {expected}, got {count}"
));
}
let end = count
.checked_mul(SCENE_TILE_RECORD_BYTES_V1)
.and_then(|n| position.checked_add(n))
.ok_or("SCENE payload overflow")?;
if end as u64 > payload_len {
return Err("Buffer too small for SCENE tile data".into());
}
*decoded = decoded
.checked_add(SCENE_DECODED_LAYER_OVERHEAD_BYTES_V1)
.and_then(|n| count.checked_mul(size_of::<Tile>()).and_then(|b| n.checked_add(b)))
.ok_or("SCENE decoded_size overflow")?;
Ok(SceneLayer {
active: bytes[0] & 1 != 0,
glyph_asset_id: i32::from_le_bytes(bytes[1..5].try_into().unwrap()),
tile_size,
parallax_factor: ParallaxFactor { x, y },
tilemap: TileMap { width, height, tiles: Vec::with_capacity(count) },
})
}
pub(super) fn append_tiles(tiles: &mut Vec<Tile>, bytes: &[u8]) {
tiles.extend(bytes.as_chunks::<SCENE_TILE_RECORD_BYTES_V1>().0.iter().map(|b| Tile {
active: b[0] & 1 != 0,
glyph: Glyph { glyph_id: u16::from_le_bytes([b[2], b[3]]), palette_id: b[1] },
flip_x: b[0] & 2 != 0,
flip_y: b[0] & 4 != 0,
}));
}
pub(super) fn scene_finish(
entry: &AssetEntry,
position: usize,
decoded: usize,
) -> Result<(), String> {
payload_size(entry, position)?;
if decoded as u64 != entry.decoded_size {
return Err(format!(
"Invalid SCENE decoded_size: expected {decoded}, got {}",
entry.decoded_size
));
}
Ok(())
}
#[cfg(test)]
pub(super) mod tests;
@@ -0,0 +1,432 @@
use super::*;
use serde_json::{Value, json};
struct FileFixture(std::path::PathBuf);
impl FileFixture {
fn new(name: &str, bytes: &[u8]) -> Self {
let path = std::env::temp_dir()
.join(format!("prometeu-conformance-{}-{name}", std::process::id()));
std::fs::write(&path, bytes).unwrap();
Self(path)
}
fn source(&self, declared_len: u64) -> Arc<RwLock<AssetsPayloadSource>> {
Arc::new(RwLock::new(AssetsPayloadSource::from_file(self.0.clone(), 0, declared_len)))
}
}
impl Drop for FileFixture {
fn drop(&mut self) {
std::fs::remove_file(&self.0).unwrap();
}
}
fn pcm_entry(size: u64) -> AssetEntry {
AssetEntry {
asset_id: 1,
asset_name: "pcm".into(),
bank_type: BankType::SOUNDS,
offset: 0,
size,
decoded_size: size,
codec: AssetCodec::None,
metadata: json!({"sample_rate": 44100, "channels": 1}),
}
}
#[test]
fn pcm_chunk_edges_and_odd_tails_have_explicit_baseline_outputs() {
for len in [0, 1, 2, 3, 4095, 4096, 4097, 8191, 8192, 8193] {
let bytes: Vec<u8> = (0..len).map(|i| if i % 2 == 0 { 0x34 } else { 0x12 }).collect();
let entry = pcm_entry(len as u64);
let expected = json!([[vec![0x1234_i16; len / 2], 44100, null, null]]);
let buffer = AssetManager::decode_sound_bank_from_buffer(&entry, &bytes).unwrap();
let reader = AssetManager::decode_sound_bank_from_reader(&entry, &mut &bytes[..]).unwrap();
let source = Arc::new(RwLock::new(AssetsPayloadSource::from_bytes(bytes)));
let production = AssetManager::perform_load_sound_bank(&entry, source.clone()).unwrap();
assert_eq!(sound_snapshot(&buffer), expected, "buffer len={len}");
assert_eq!(sound_snapshot(&reader), expected, "reader len={len}");
assert_eq!(sound_snapshot(&production), expected, "legacy load len={len}");
assert_eq!(
resumable::conformance_result(&entry, &source).unwrap(),
expected,
"resumable len={len}"
);
}
}
#[test]
fn physically_truncated_pcm_file_is_rejected_by_both_modes() {
// Two actual bytes, three declared bytes: the missing byte is an odd tail.
// The approved rule rejects missing declared bytes, including an odd tail.
let fixture = FileFixture::new("truncated-pcm", &[0x34, 0x12]);
let entry = pcm_entry(3);
let source = fixture.source(3);
assert!(AssetManager::perform_load_sound_bank(&entry, source.clone()).is_err());
assert!(resumable::conformance_result(&entry, &source).is_err());
// With the declared byte physically present, both accept and ignore it.
std::fs::write(&fixture.0, [0x34, 0x12, 0xff]).unwrap();
let expected = json!([[[0x1234], 44100, null, null]]);
assert_eq!(resumable::conformance_result(&entry, &source).unwrap(), expected);
assert_eq!(
sound_snapshot(&AssetManager::perform_load_sound_bank(&entry, source.clone()).unwrap()),
expected
);
// Memory carriers reject the range before either decoder can accept it.
let memory = Arc::new(RwLock::new(AssetsPayloadSource::from_bytes(vec![0x34, 0x12])));
assert!(AssetManager::perform_load_sound_bank(&entry, memory.clone()).is_err());
assert!(resumable::conformance_result(&entry, &memory).is_err());
}
#[test]
fn physically_short_scene_file_is_rejected_by_both_modes() {
// Minimal scene: four empty layers, 12-byte header + 4 * 32-byte headers.
let mut bytes = vec![0_u8; 140];
bytes[..8].copy_from_slice(&[b'S', b'C', b'N', b'E', 1, 0, 4, 0]);
for start in [12, 44, 76, 108] {
bytes[start + 5] = 8;
}
let fixture = FileFixture::new("short-scene", &bytes);
let mut entry = AssetEntry {
asset_id: 2,
asset_name: "scene".into(),
bank_type: BankType::SCENE,
offset: 0,
size: 141,
decoded_size: 64,
codec: AssetCodec::None,
metadata: json!({}),
};
let expected = json!(vec![json!([false, 0, 8, 0.0, 0.0, 0, 0, []]); 4]);
let source = fixture.source(141);
assert!(AssetManager::perform_load_scene_bank(&entry, source.clone()).is_err());
assert!(resumable::conformance_result(&entry, &source).is_err());
// Exact physical/declared size is accepted by both.
entry.size = 140;
assert_eq!(resumable::conformance_result(&entry, &source).unwrap(), expected);
assert_eq!(
scene_snapshot(&AssetManager::perform_load_scene_bank(&entry, source.clone()).unwrap()),
expected
);
// An actual trailing byte is rejected by both.
entry.size = 141;
bytes.push(0);
std::fs::write(&fixture.0, bytes).unwrap();
assert!(AssetManager::perform_load_scene_bank(&entry, source.clone()).is_err());
assert!(resumable::conformance_result(&entry, &source).is_err());
}
// Every mode sees the same declared slice, just as it does in production.
fn check_case(name: &str, entry: &AssetEntry, bytes: Vec<u8>, expected: Option<Value>) {
let source = Arc::new(RwLock::new(AssetsPayloadSource::from_bytes(bytes)));
for mode in 0..4 {
let result = (|| -> Result<Value, String> {
if mode == 3 {
return resumable::conformance_result(entry, &source);
}
if mode == 2 {
return match entry.bank_type {
BankType::GLYPH => AssetManager::perform_load_glyph_bank(entry, source.clone())
.map(|b| glyph_snapshot(&b)),
BankType::SOUNDS => {
AssetManager::perform_load_sound_bank(entry, source.clone())
.map(|b| sound_snapshot(&b))
}
BankType::SCENE => AssetManager::perform_load_scene_bank(entry, source.clone())
.map(|b| scene_snapshot(&b)),
};
}
let slice = source
.read()
.unwrap()
.open_slice(entry.offset, entry.size)
.map_err(|e| e.to_string())?;
let bytes = slice.read_all().map_err(|e| e.to_string())?;
let mut reader = &bytes[..];
match (entry.bank_type, mode) {
(BankType::GLYPH, 0) => AssetManager::decode_glyph_bank_from_buffer(entry, &bytes)
.map(|b| glyph_snapshot(&b)),
(BankType::GLYPH, _) => {
AssetManager::decode_glyph_bank_from_reader(entry, &mut reader)
.map(|b| glyph_snapshot(&b))
}
(BankType::SOUNDS, 0) => AssetManager::decode_sound_bank_from_buffer(entry, &bytes)
.map(|b| sound_snapshot(&b)),
(BankType::SOUNDS, _) => {
AssetManager::decode_sound_bank_from_reader(entry, &mut reader)
.map(|b| sound_snapshot(&b))
}
(BankType::SCENE, 0) => AssetManager::decode_scene_bank_from_buffer(entry, &bytes)
.map(|b| scene_snapshot(&b)),
(BankType::SCENE, _) => {
AssetManager::decode_scene_bank_from_reader(entry, &mut reader)
.map(|b| scene_snapshot(&b))
}
}
})();
assert_eq!(result.ok(), expected, "{name}, mode={mode}");
}
}
#[test]
fn glyph_layout_palette_nibbles_and_chunk_boundaries() {
for pixels in [0_usize, 1, 2, 8191, 8192, 8193, 16385] {
for palettes in [1, 3, 64] {
let mut bytes = vec![0x12; pixels.div_ceil(2)];
bytes.extend(std::iter::repeat_n([0x12, 0x34, 0x56, 0x78], palettes * 16).flatten());
let entry = AssetEntry {
asset_id: 1,
asset_name: "glyph".into(),
bank_type: BankType::GLYPH,
offset: 0,
size: bytes.len() as u64,
decoded_size: (pixels + palettes * 64) as u64,
codec: AssetCodec::None,
metadata: json!({"tile_size":16,"width":pixels,"height":1,"palette_count":palettes,"palette_authored":palettes}),
};
let expected = json!([
16,
pixels,
1,
(0..pixels).map(|i| 1 + i % 2).collect::<Vec<_>>(),
vec![vec![0x12345678_u32; 16]; palettes]
]);
check_case("glyph-valid", &entry, bytes.clone(), Some(expected));
for (key, value) in [
("tile_size", json!(7)),
("palette_count", json!(0)),
("palette_count", json!(65)),
("width", json!(-1)),
("height", json!("bad")),
] {
let mut invalid = entry.clone();
invalid.metadata[key] = value;
check_case(key, &invalid, bytes.clone(), None);
}
let mut invalid = entry.clone();
invalid.decoded_size += 1;
check_case("glyph-decoded-size", &invalid, bytes.clone(), None);
let mut invalid = entry.clone();
invalid.size += 1;
let mut extra = bytes.clone();
extra.push(0);
check_case("glyph-trailing", &invalid, extra, None);
bytes.pop();
check_case("glyph-truncated", &entry, bytes, None);
}
}
}
fn empty_scene() -> (AssetEntry, Vec<u8>, Value) {
let mut bytes = vec![0_u8; 140];
bytes[..8].copy_from_slice(&[b'S', b'C', b'N', b'E', 1, 0, 4, 0]);
for start in [12, 44, 76, 108] {
bytes[start + 5] = 8;
}
let entry = AssetEntry {
asset_id: 2,
asset_name: "scene".into(),
bank_type: BankType::SCENE,
offset: 0,
size: 140,
decoded_size: 64,
codec: AssetCodec::None,
metadata: json!({}),
};
(entry, bytes, json!(vec![json!([false, 0, 8, 0.0, 0.0, 0, 0, []]); 4]))
}
#[test]
fn scene_header_ranges_nonfinite_and_truncation_corpus() {
let (entry, bytes, expected) = empty_scene();
check_case("empty-scene", &entry, bytes.clone(), Some(expected));
for end in 0..bytes.len() {
let mut e = entry.clone();
e.size = end as u64;
check_case("scene-every-truncation", &e, bytes[..end].to_vec(), None);
}
for (offset, value) in [(0, 0xff), (4, 2), (6, 3), (17, 7), (36, 1)] {
let mut invalid = bytes.clone();
invalid[offset] = value;
check_case("scene-invalid-header-field", &entry, invalid, None);
}
for offset in [20, 24] {
for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
let mut invalid = bytes.clone();
invalid[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
check_case("scene-nonfinite", &entry, invalid, None);
}
}
let mut invalid = bytes.clone();
for offset in [28, 32, 36] {
invalid[offset..offset + 4].copy_from_slice(&u32::MAX.to_le_bytes());
}
check_case("scene-dimensions-overflow-or-count-mismatch", &entry, invalid, None);
let mut invalid = bytes.clone();
for offset in [28, 32, 36] {
invalid[offset..offset + 4].copy_from_slice(&1_u32.to_le_bytes());
}
// Four layer headers no longer fit after the declared tile consumes bytes.
check_case("scene-tile-range", &entry, invalid, None);
let mut sequence_metadata = entry.clone();
sequence_metadata.metadata = json!([]);
check_case(
"scene-empty-sequence-metadata",
&sequence_metadata,
bytes.clone(),
Some(empty_scene().2),
);
let mut e = entry.clone();
e.decoded_size += 1;
check_case("scene-decoded-size", &e, bytes.clone(), None);
e = entry.clone();
e.size += 1;
let mut trailing = bytes.clone();
trailing.push(0);
check_case("scene-trailing", &e, trailing, None);
for metadata in [json!(null), json!("bad")] {
e = entry.clone();
e.metadata = metadata;
check_case("scene-metadata", &e, bytes.clone(), None);
}
}
#[test]
fn scene_tile_chunk_edges_flags_and_reserved_bytes() {
for count in [1_usize, 1023, 1024, 1025, 2049] {
let (mut entry, mut bytes, mut expected) = empty_scene();
bytes[12] = 0xff; // Only the active flag is meaningful.
bytes[13..17].copy_from_slice(&(-7_i32).to_le_bytes());
bytes[17] = 32;
bytes[18..20].copy_from_slice(&[0xff; 2]); // Reserved fields are ignored.
bytes[20..24].copy_from_slice(&(-0.5_f32).to_le_bytes());
bytes[24..28].copy_from_slice(&2.0_f32.to_le_bytes());
bytes[28..32].copy_from_slice(&(count as u32).to_le_bytes());
bytes[32..36].copy_from_slice(&1_u32.to_le_bytes());
bytes[36..40].copy_from_slice(&(count as u32).to_le_bytes());
bytes.splice(44..44, std::iter::repeat_n([0xff, 255, 0x34, 0x12], count).flatten());
entry.size = bytes.len() as u64;
entry.decoded_size += (count * size_of::<Tile>()) as u64;
expected[0] = json!([
true,
-7,
32,
-0.5,
2.0,
count,
1,
vec![(true, 0x1234, 255, true, true); count]
]);
check_case("scene-chunks", &entry, bytes, Some(expected));
}
}
#[test]
fn pcm_metadata_and_source_range_corpus() {
for metadata in [
json!(null),
json!({}),
json!({"sample_rate":-1,"channels":1}),
json!({"sample_rate":44100,"channels":"bad"}),
] {
let mut e = pcm_entry(2);
e.metadata = metadata;
check_case("pcm-metadata", &e, vec![0x34, 0x12], None);
}
// These fields are currently descriptive, not extra PCM layout constraints.
let mut e = pcm_entry(2);
e.metadata = json!({"sample_rate":0,"channels":0});
e.decoded_size = 999;
check_case(
"pcm-permissive-metadata",
&e,
vec![0x34, 0x12],
Some(json!([[[0x1234], 0, null, null]])),
);
for offset in [1, u64::MAX] {
let mut e = pcm_entry(2);
e.offset = offset;
check_case("source-range", &e, vec![0x34, 0x12], None);
}
let mut e = pcm_entry(2);
e.size = u64::MAX;
check_case("source-size-overflow", &e, vec![0x34, 0x12], None);
}
pub(in crate::asset) fn glyph_snapshot(bank: &GlyphBank) -> Value {
json!([
bank.tile_size as u8,
bank.width,
bank.height,
bank.pixel_indices,
bank.palettes
.iter()
.map(|p| p.iter().map(|c| c.raw()).collect::<Vec<_>>())
.collect::<Vec<_>>()
])
}
pub(in crate::asset) fn sound_snapshot(bank: &SoundBank) -> Value {
json!(
bank.samples
.iter()
.map(|s| (&s.data, s.sample_rate, s.loop_start, s.loop_end))
.collect::<Vec<_>>()
)
}
pub(in crate::asset) fn scene_snapshot(bank: &SceneBank) -> Value {
json!(
bank.layers
.iter()
.map(|l| json!([
l.active,
l.glyph_asset_id,
l.tile_size as u8,
l.parallax_factor.x,
l.parallax_factor.y,
l.tilemap.width,
l.tilemap.height,
l.tilemap
.tiles
.iter()
.map(|t| (t.active, t.glyph.glyph_id, t.glyph.palette_id, t.flip_x, t.flip_y))
.collect::<Vec<_>>()
]))
.collect::<Vec<_>>()
)
}
#[test]
fn shared_scene_arithmetic_rejects_overflow_before_allocating() {
let (_, bytes, _) = empty_scene();
let mut header = bytes[12..44].to_vec();
header[16..20].copy_from_slice(&1_u32.to_le_bytes());
header[20..24].copy_from_slice(&1_u32.to_le_bytes());
header[24..28].copy_from_slice(&1_u32.to_le_bytes());
assert!(scene_layer(&header, usize::MAX, u64::MAX, &mut 0).is_err());
let mut decoded = usize::MAX;
assert!(scene_layer(&header, 44, 48, &mut decoded).is_err());
assert!(scene_layer(&header, 44, 47, &mut 0).is_err());
assert!(scene_layer(&header[..31], 44, 48, &mut 0).is_err());
}
#[test]
fn physically_truncated_glyph_palette_is_rejected_by_both_modes() {
let fixture = FileFixture::new("truncated-glyph", &[0; 63]);
let entry = AssetEntry {
asset_id: 3,
asset_name: "empty-glyph".into(),
bank_type: BankType::GLYPH,
offset: 0,
size: 64,
decoded_size: 64,
codec: AssetCodec::None,
metadata: json!({"tile_size":8,"width":0,"height":0,"palette_count":1,"palette_authored":1}),
};
let source = fixture.source(64);
assert!(AssetManager::perform_load_glyph_bank(&entry, source.clone()).is_err());
assert!(resumable::conformance_result(&entry, &source).is_err());
std::fs::write(&fixture.0, [0; 64]).unwrap();
let expected = json!([8, 0, 0, [], vec![vec![0; 16]]]);
assert_eq!(
glyph_snapshot(&AssetManager::perform_load_glyph_bank(&entry, source.clone()).unwrap()),
expected
);
assert_eq!(resumable::conformance_result(&entry, &source).unwrap(), expected);
}
@@ -246,7 +246,7 @@ impl Decoder {
}
}
BankType::SOUNDS => {
let sample_rate = entry.metadata_as_sound_bank()?.sample_rate;
let sample_rate = decode::sound_rate(entry)?;
State::Sound {
sample_rate,
data: Vec::with_capacity(entry.size as usize / 2),
@@ -254,15 +254,9 @@ impl Decoder {
}
}
BankType::SCENE => {
entry.metadata_as_scene_bank()?;
decode::scene_metadata(entry)?;
State::Scene {
layers: std::array::from_fn(|_| SceneLayer {
active: false,
glyph_asset_id: 0,
tile_size: TileSize::Size8,
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
tilemap: TileMap { width: 0, height: 0, tiles: Vec::new() },
}),
layers: decode::empty_layers(),
layer: 0,
tiles_left: 0,
header: false,
@@ -279,25 +273,18 @@ impl Decoder {
if *position < *packed {
let n = (*packed - *position).min(bytes.len());
self.reader.read_exact(&mut bytes[..n]).map_err(|e| e.to_string())?;
for &v in &bytes[..n] {
if bank.pixel_indices.len() < bank.width * bank.height {
bank.pixel_indices.push(v >> 4)
}
if bank.pixel_indices.len() < bank.width * bank.height {
bank.pixel_indices.push(v & 15)
}
}
decode::append_pixels(
&mut bank.pixel_indices,
&bytes[..n],
bank.width * bank.height,
);
*position += n;
return Ok(None);
}
if *palette_position < *palette_bytes {
let n = (*palette_bytes - *palette_position).min(bytes.len());
self.reader.read_exact(&mut bytes[..n]).map_err(|e| e.to_string())?;
for (i, rgba) in bytes[..n].as_chunks::<4>().0.iter().enumerate() {
let at = *palette_position / 4 + i;
bank.palettes[at / 16][at % 16] =
Color::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
}
decode::write_palettes(&mut bank.palettes, *palette_position, &bytes[..n]);
*palette_position += n;
return Ok(None);
}
@@ -311,9 +298,7 @@ impl Decoder {
if *remaining > 0 {
let n = (*remaining).min(bytes.len());
self.reader.read_exact(&mut bytes[..n]).map_err(|e| e.to_string())?;
for pair in bytes[..n].as_chunks::<2>().0 {
data.push(i16::from_le_bytes([pair[0], pair[1]]));
}
decode::append_pcm(data, &bytes[..n]);
*remaining -= n;
return Ok(None);
}
@@ -327,12 +312,7 @@ impl Decoder {
self.reader
.read_exact(&mut bytes[..SCENE_HEADER_BYTES_V1])
.map_err(|e| e.to_string())?;
if bytes[..4] != SCENE_PAYLOAD_MAGIC_V1
|| u16::from_le_bytes([bytes[4], bytes[5]]) != SCENE_PAYLOAD_VERSION_V1
|| u16::from_le_bytes([bytes[6], bytes[7]]) as usize != SCENE_LAYER_COUNT_V1
{
return Err("Invalid scene header".into());
}
decode::scene_header(&bytes[..SCENE_HEADER_BYTES_V1])?;
self.position += SCENE_HEADER_BYTES_V1;
*header = true;
return Ok(None);
@@ -341,17 +321,7 @@ impl Decoder {
let n = (*tiles_left).min(bytes.len() / SCENE_TILE_RECORD_BYTES_V1);
let count = n * SCENE_TILE_RECORD_BYTES_V1;
self.reader.read_exact(&mut bytes[..count]).map_err(|e| e.to_string())?;
for b in bytes[..count].as_chunks::<SCENE_TILE_RECORD_BYTES_V1>().0 {
layers[*layer - 1].tilemap.tiles.push(Tile {
active: b[0] & 1 != 0,
glyph: Glyph {
glyph_id: u16::from_le_bytes([b[2], b[3]]),
palette_id: b[1],
},
flip_x: b[0] & 2 != 0,
flip_y: b[0] & 4 != 0,
});
}
decode::append_tiles(&mut layers[*layer - 1].tilemap.tiles, &bytes[..count]);
*tiles_left -= n;
self.position += count;
return Ok(None);
@@ -361,50 +331,18 @@ impl Decoder {
.read_exact(&mut bytes[..SCENE_LAYER_HEADER_BYTES_V1])
.map_err(|e| e.to_string())?;
self.position += SCENE_LAYER_HEADER_BYTES_V1;
let u32at = |at| u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap());
let width = u32at(16) as usize;
let height = u32at(20) as usize;
let count = u32at(24) as usize;
let tile_size = match bytes[5] {
8 => TileSize::Size8,
16 => TileSize::Size16,
32 => TileSize::Size32,
_ => return Err("Invalid scene tile size".into()),
};
let x = f32::from_le_bytes(bytes[8..12].try_into().unwrap());
let y = f32::from_le_bytes(bytes[12..16].try_into().unwrap());
if !x.is_finite()
|| !y.is_finite()
|| width.checked_mul(height) != Some(count)
|| count
.checked_mul(SCENE_TILE_RECORD_BYTES_V1)
.and_then(|n| n.checked_add(self.position))
.is_none_or(|n| n as u64 > self.entry.size)
{
return Err("Invalid scene layer range".into());
}
*decoded = decoded
.checked_add(SCENE_DECODED_LAYER_OVERHEAD_BYTES_V1)
.and_then(|n| {
count.checked_mul(size_of::<Tile>()).and_then(|b| n.checked_add(b))
})
.ok_or("Scene size overflow")?;
layers[*layer] = SceneLayer {
active: bytes[0] & 1 != 0,
glyph_asset_id: i32::from_le_bytes(bytes[1..5].try_into().unwrap()),
tile_size,
parallax_factor: ParallaxFactor { x, y },
tilemap: TileMap { width, height, tiles: Vec::with_capacity(count) },
};
layers[*layer] = decode::scene_layer(
&bytes[..SCENE_LAYER_HEADER_BYTES_V1],
self.position,
self.entry.size,
decoded,
)?;
let count = layers[*layer].tilemap.width * layers[*layer].tilemap.height;
*layer += 1;
*tiles_left = count;
return Ok(None);
}
if self.position as u64 != self.entry.size
|| *decoded as u64 != self.entry.decoded_size
{
return Err("Invalid scene payload size".into());
}
decode::scene_finish(&self.entry, self.position, *decoded)?;
let State::Scene { layers, .. } = std::mem::replace(&mut self.state, State::Done)
else {
unreachable!()
@@ -416,6 +354,30 @@ impl Decoder {
}
}
// Test-only access to the production state machine; no alternate decode rules.
#[cfg(test)]
pub(super) fn conformance_result(
entry: &AssetEntry,
source: &Arc<RwLock<AssetsPayloadSource>>,
) -> Result<serde_json::Value, String> {
use std::io::Seek;
let mut decoder = Decoder::new(entry, source)?;
for _ in 0..100 {
let before = decoder.reader.stream_position().unwrap();
let result = decoder.step();
let after = decoder.reader.stream_position().unwrap();
assert!(after >= before && after - before <= 4096, "unbounded or rewound decode step");
if let Some(bank) = result? {
return Ok(match bank {
Decoded::Glyph(bank) => super::decode::tests::glyph_snapshot(&bank),
Decoded::Sound(bank) => super::decode::tests::sound_snapshot(&bank),
Decoded::Scene(bank) => super::decode::tests::scene_snapshot(&bank),
});
}
}
panic!("conformance fixture exceeded its step budget")
}
#[cfg(test)]
mod tests {
use super::*;
@@ -556,4 +518,55 @@ mod tests {
assert_eq!(manager.preloads_ready(), Ok(true));
assert_eq!(manager.slot_info(SlotRef::audio(0)).asset_id, Some(1));
}
#[test]
fn canceled_and_superseded_partial_decoders_never_publish() {
use crate::memory_banks::SoundBankPoolAccess;
let bytes = vec![0x12; 16384];
let entry = AssetEntry {
asset_id: 1,
asset_name: "pcm".into(),
bank_type: BankType::SOUNDS,
offset: 0,
size: bytes.len() as u64,
decoded_size: bytes.len() as u64,
codec: AssetCodec::None,
metadata: serde_json::json!({"sample_rate":44100,"channels":1}),
};
let mut newer = entry.clone();
newer.asset_id = 2;
let banks = Arc::new(crate::memory_banks::MemoryBanks::new());
let mut manager = AssetManager::new(
vec![entry, newer],
AssetsPayloadSource::from_bytes(bytes),
banks.clone(),
banks.clone(),
banks.clone(),
);
let lane = Arc::new(ManualLane::default());
assert!(manager.bind_background_work(lane.clone()));
let handle = manager.load(1, 0).unwrap();
assert!(lane.step());
assert!(lane.step());
assert_eq!(manager.status(handle), LoadStatus::LOADING);
assert_eq!(manager.cancel(handle), AssetOpStatus::Ok);
while lane.step() {}
assert_eq!(manager.status(handle), LoadStatus::CANCELED);
assert!(manager.sound_policy.staging.read().unwrap().is_empty());
manager.apply_commits();
assert!(banks.sound_bank_slot(0).is_none());
let old = manager.load(1, 0).unwrap();
assert!(lane.step());
assert!(lane.step());
let new = manager.load(2, 0).unwrap();
assert_eq!(old, new); // Stable slot handle, different request generation.
assert!(lane.step()); // Discard the in-flight old decoder.
assert!(manager.sound_policy.staging.read().unwrap().is_empty());
while lane.step() {}
assert_eq!(manager.status(new), LoadStatus::READY);
assert!(banks.sound_bank_slot(0).is_none());
assert_eq!(manager.commit(new), AssetOpStatus::Ok);
manager.apply_commits();
assert_eq!(manager.slot_info(SlotRef::audio(0)).asset_id, Some(2));
assert_eq!(banks.sound_bank_slot(0).unwrap().samples[0].data, vec![0x1212; 8192]);
}
}
+3 -2
View File
@@ -203,7 +203,8 @@ impl AudioBridge for Audio {
fn begin_frame(&mut self) { /* Staging may already contain debugger steps. */
}
fn finish_frame(&mut self) {
self.transport.publish(std::mem::take(&mut self.commands), false);
let commands = std::mem::replace(&mut self.commands, self.transport.take_buffer());
self.transport.publish(commands, false);
self.frame_commands_admitted = 0;
}
fn abort_frame(&mut self) {
@@ -221,7 +222,7 @@ impl AudioBridge for Audio {
if success {
// Publish the earlier frame prefix first so a callback stop cannot
// be undone by an old play on resume. Do not replenish frame quota.
let mut commands = std::mem::take(&mut self.commands);
let mut commands = std::mem::replace(&mut self.commands, self.transport.take_buffer());
commands.append(&mut self.lifecycle_commands);
self.transport.publish(commands, true);
} else {
@@ -14,6 +14,7 @@ struct Mailbox {
paused: bool,
frame: Option<Vec<AudioCommand>>,
lifecycle: Option<Vec<AudioCommand>>,
recycled: [Option<Vec<AudioCommand>>; 4],
}
pub struct AudioDelivery {
@@ -36,7 +37,31 @@ impl AudioTransport {
}
pub fn consume_silently(&self) {
let _ = self.try_take();
if let Some(delivery) = self.try_take() {
for batch in [delivery.frame, delivery.lifecycle].into_iter().flatten() {
self.recycle_commands(batch);
}
}
}
/// Main/service lane only: drop command owners and retain bounded empty storage.
pub fn recycle_commands(&self, mut commands: Vec<AudioCommand>) {
commands.clear();
let mut state = self.0.lock().expect("audio mailbox");
if let Some(slot) = state.recycled.iter_mut().find(|slot| slot.is_none()) {
*slot = Some(commands);
}
// Excess empty buffers are destroyed here, never in the data callback.
}
pub(super) fn take_buffer(&self) -> Vec<AudioCommand> {
self.0
.lock()
.expect("audio mailbox")
.recycled
.iter_mut()
.find_map(Option::take)
.unwrap_or_default()
}
pub fn ready(&self) -> bool {
@@ -84,3 +109,36 @@ mod tests {
assert!(transport.try_take().is_some());
}
}
#[cfg(test)]
mod reuse_tests {
use super::*;
#[test]
fn service_recycles_storage_and_releases_sample_owners() {
use prometeu_hal::{LoopMode, sample::Sample};
let transport = AudioTransport::default();
let sample = Arc::new(Sample::new(48000, vec![1]));
let weak = Arc::downgrade(&sample);
let mut batch = Vec::with_capacity(1024);
let allocation = batch.as_ptr();
batch.push(AudioCommand::Play {
sample,
voice_id: 0,
volume: 255,
pan: 0,
pitch: 1.0,
priority: 0,
loop_mode: LoopMode::Off,
});
transport.recycle_commands(batch);
assert!(weak.upgrade().is_none());
let reused = transport.take_buffer();
assert_eq!(reused.as_ptr(), allocation);
assert_eq!(reused.capacity(), 1024);
assert!(reused.is_empty());
for _ in 0..8 {
transport.recycle_commands(Vec::with_capacity(16));
}
assert_eq!(transport.0.lock().unwrap().recycled.iter().flatten().count(), 4);
}
}
+14 -3
View File
@@ -13,7 +13,8 @@ fn io_error(e: std::io::Error) -> Error {
use std::io::ErrorKind as K;
match e.kind() {
K::NotFound => Error::Unavailable,
K::PermissionDenied => Error::ReadOnly,
K::PermissionDenied => Error::AccessDenied,
K::AlreadyExists => Error::AlreadyExists,
K::StorageFull => Error::NoSpace,
K::InvalidInput | K::UnexpectedEof => Error::InvalidRange,
K::Unsupported => Error::Unsupported,
@@ -21,8 +22,15 @@ fn io_error(e: std::io::Error) -> Error {
}
}
impl FileMemcard {
fn lock(file: &File) -> Result<(), Error> {
file.try_lock().map_err(|error| match error {
std::fs::TryLockError::WouldBlock => Error::Busy,
std::fs::TryLockError::Error(error) => io_error(error),
})
}
pub fn open(path: &Path, read_only: bool) -> Result<Self, Error> {
let file = OpenOptions::new().read(true).write(!read_only).open(path).map_err(io_error)?;
Self::lock(&file)?;
let length = file.metadata().map_err(io_error)?.len();
Ok(Self { file, length, read_only })
}
@@ -47,7 +55,9 @@ impl FileMemcard {
.create_new(true)
.open(&temporary)
.map_err(io_error)?;
let mut published = false;
let result = (|| {
Self::lock(&file)?;
let mut card = Self { file, length, read_only: false };
let zeros = [0u8; 65_536];
let mut offset = 0;
@@ -64,6 +74,7 @@ impl FileMemcard {
card.sync()?;
// hard_link publishes without replacing a user file; parent sync makes the name durable.
std::fs::hard_link(&temporary, path).map_err(io_error)?;
published = true;
File::open(parent).and_then(|f| f.sync_all()).map_err(io_error)?;
Ok(card)
})();
@@ -71,10 +82,10 @@ impl FileMemcard {
std::fs::remove_file(&temporary).and_then(|()| File::open(parent)?.sync_all());
match result {
Ok(card) => {
cleanup.map_err(io_error)?;
cleanup.map_err(|_| Error::Indeterminate)?;
Ok(card)
}
Err(e) => Err(e),
Err(e) => Err(if published { Error::Indeterminate } else { e }),
}
}
#[cfg(not(unix))]
@@ -18,6 +18,8 @@ impl ControlledWallClock {
}
/// Advance civil time explicitly, saturating at the largest timestamp.
// `try_update` requires Rust 1.95; keep older stable CI toolchains compatible.
#[allow(deprecated)]
pub fn advance(&self, seconds: u64) {
// Harness control may retry on a concurrent update; reads remain a single load.
let _ = self.seconds.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
@@ -48,6 +48,25 @@ impl LoadCartridgeStep {
pub fn needs_memcard_choice(&self) -> bool {
self.awaiting_memcard && !self.preparing_memcard
}
/// Typed host and UI admission share this single policy boundary.
pub fn choose_memcard(
&mut self,
os: &mut prometeu_system::SystemOS,
choice: prometeu_system::memcard::admission::Choice,
created_at: u64,
) -> prometeu_system::memcard::image::Result<()> {
if !self.needs_memcard_choice() {
return Err(prometeu_system::memcard::image::Error::InvalidState);
}
os.memcard().choose_for_game(
self.cartridge.app_id,
self.cartridge.memcard_slots.unwrap() as usize,
choice,
created_at,
)?;
self.preparing_memcard = true;
Ok(())
}
pub fn new_game_switch(cartridge: Cartridge) -> Self {
Self { failure_recovery: LoadCartridgeFailureRecovery::ReturnToHub, ..Self::new(cartridge) }
@@ -128,13 +147,8 @@ impl LoadCartridgeStep {
Choice::MemoryExisting
};
let created_at = ctx.platform.wall_clock().utc_unix_seconds();
match ctx.os.memcard().choose_for_game(
self.cartridge.app_id,
self.cartridge.memcard_slots.unwrap() as usize,
choice,
created_at,
) {
Ok(()) => self.preparing_memcard = true,
match self.choose_memcard(ctx.os, choice, created_at) {
Ok(()) => {}
Err(error) => self.memcard_message = Self::preparation_message(error),
}
}
@@ -57,6 +57,21 @@ impl LogService {
self.events.iter().filter(|e| e.seq > seq).cloned().collect()
}
/// Non-destructive page in native sequence order. A null cursor includes zero.
pub fn get_page(&self, after: Option<u64>, limit: usize) -> Vec<LogEvent> {
self.events
.iter()
.filter(|event| after.is_none_or(|seq| event.seq > seq))
.take(limit)
.cloned()
.collect()
}
/// Retained bounds, independent of any reader's progress.
pub fn sequence_bounds(&self) -> (Option<u64>, Option<u64>) {
(self.events.front().map(|event| event.seq), self.last_seq())
}
pub fn last_seq(&self) -> Option<u64> {
self.events.back().map(|e| e.seq)
}
@@ -107,4 +122,35 @@ mod tests {
assert_eq!(after[0].msg, "Log 3");
assert_eq!(after[1].msg, "Log 4");
}
#[test]
fn pages_preserve_zero_requery_and_retained_bounds() {
let mut service = LogService::new(3);
assert_eq!(service.sequence_bounds(), (None, None));
assert!(service.get_page(None, 2).is_empty());
for i in 0..3 {
service.log(i, i, LogLevel::Info, LogSource::Pos, 7, format!("{i}"));
}
assert_eq!(service.sequence_bounds(), (Some(0), Some(2)));
for _ in 0..2 {
let page = service.get_page(None, 2);
assert_eq!(page.iter().map(|event| event.seq).collect::<Vec<_>>(), vec![0, 1]);
}
assert_eq!(service.get_page(Some(1), 2)[0].seq, 2);
assert!(service.get_page(Some(2), 2).is_empty());
service.log(3, 3, LogLevel::Info, LogSource::Pos, 7, "next".into());
assert_eq!(service.sequence_bounds(), (Some(1), Some(3)));
assert_eq!(service.get_page(Some(2), 2)[0].seq, 3);
assert_eq!(service.get_page(None, 10).len(), 3);
}
#[test]
fn page_cursors_preserve_large_unsigned_sequences() {
let mut service = LogService::new(3);
service.next_seq = u64::MAX - 2;
service.log(0, 0, LogLevel::Info, LogSource::Pos, 0, "first".into());
service.log(0, 0, LogLevel::Info, LogSource::Pos, 0, "second".into());
assert_eq!(service.get_page(Some(u64::MAX - 2), 1)[0].seq, u64::MAX - 1);
assert!(service.get_page(Some(u64::MAX), 1).is_empty());
}
}
@@ -6,6 +6,10 @@ pub enum MemcardIoError {
InvalidRange,
NoSpace,
Unsupported,
Busy,
AccessDenied,
AlreadyExists,
Indeterminate,
IoError,
}
@@ -64,6 +64,14 @@ impl SystemOS {
pub fn memcard(&mut self) -> &mut MemcardService {
&mut self.memcard
}
/// A successful return confirms the shared worker has stopped and joined.
pub fn stop_background_io(&mut self) -> Result<(), crate::AsyncWorkLaneError> {
if let Some(controller) = &mut self.io_controller {
controller.stop()?;
}
self.io_controller = None;
Ok(())
}
/// Host/player operations are admitted only while gameplay is suspended.
pub fn replace_memcard(
@@ -162,6 +170,16 @@ impl SystemOS {
self.log_service.get_after(seq)
}
/// Coherent non-destructive page and retained bounds; does not execute the VM.
pub fn log_snapshot(
&self,
after: Option<u64>,
limit: usize,
) -> (Vec<LogEvent>, Option<u64>, Option<u64>) {
let (oldest, newest) = self.log_service.sequence_bounds();
(self.log_service.get_page(after, limit), oldest, newest)
}
pub fn frame_index(&mut self) -> u64 {
self.vm().logical_frame_index()
}
@@ -2,6 +2,7 @@
//! shared IO worker owns storage during a publication. Legacy per-slot files are
//! intentionally neither read nor migrated by this service.
pub mod admission;
pub mod diagnostics;
pub mod guest;
pub mod image;
pub mod session;
@@ -37,6 +37,7 @@ pub struct Admission {
pub state: PreparationState,
pub snapshot_budget: usize,
faulted: bool,
replacement_observation: Option<super::diagnostics::SessionDiagnostics>,
}
impl Default for Admission {
fn default() -> Self {
@@ -48,10 +49,26 @@ impl Default for Admission {
state: PreparationState::Idle,
snapshot_budget: DEFAULT_SNAPSHOT_BUDGET,
faulted: false,
replacement_observation: None,
}
}
}
impl MemcardService {
pub fn session_diagnostics(&self) -> super::diagnostics::SessionDiagnostics {
self.admission.replacement_observation.clone().unwrap_or_else(|| self.session.diagnostics())
}
pub fn preparation_active(&self) -> bool {
self.admission.receiver.is_some()
}
pub fn installed_card_id(&self) -> Option<[u8; 16]> {
self.admission.installed.as_ref().map(|c| c.current().card_id)
}
pub fn cancel_pending(&mut self) {
self.session.cancel_pending();
if let Some(control) = &self.admission.control {
control.cancel();
}
}
pub fn host_faulted(&self) -> bool {
self.admission.faulted || self.session.host_faulted()
}
@@ -60,7 +77,11 @@ impl MemcardService {
}
pub fn installed_health(&self) -> Option<(bool, bool, bool)> {
self.admission.installed.as_ref().map(|card| {
(card.recovered, card.degraded, card.write_blocked || card.storage_read_only())
(
card.recovered,
card.degraded,
card.write_blocked || card.storage_read_only() || card.uncertain,
)
})
}
pub fn card_has_game(&self, app_id: u32) -> bool {
@@ -135,6 +156,9 @@ impl MemcardService {
let owned =
card.catalog.games.iter().find(|g| g.app_id == app_id).map_or(0, |g| g.slots.len());
if choice == Choice::Persistent {
if card.uncertain {
return Err(Error::Indeterminate);
}
if card.storage_read_only() || card.write_blocked {
return Err(Error::ReadOnly);
}
@@ -171,6 +195,10 @@ impl MemcardService {
}
Choice::Persistent => {
let mut card = card.take().unwrap();
if card.uncertain {
session.retain_idle_card(card);
return Err(Error::Indeterminate);
}
if card.storage_read_only() || card.write_blocked {
session.retain_idle_card(card);
return Err(Error::ReadOnly);
@@ -256,9 +284,24 @@ impl MemcardService {
&mut self,
storage: Box<dyn MemcardStorage>,
load_existing: bool,
) -> Result<()> {
self.replace_card_job(move |session, control| {
session.replace_storage(storage, load_existing, control)
})
}
pub fn reconcile_card_with(
&mut self,
open: impl FnOnce() -> Result<Box<dyn MemcardStorage>> + Send + 'static,
) -> Result<()> {
self.replace_card_job(move |session, control| session.reconcile_storage_with(open, control))
}
fn replace_card_job(
&mut self,
operation: impl FnOnce(&mut Session, &PublicationControl) -> Result<()> + Send + 'static,
) -> Result<()> {
if self.admission.receiver.is_some()
|| self.session.has_in_flight()
|| self.session.io_active()
|| self.session.has_deferred_write()
|| self.session.is_boot_active()
{
return Err(Error::Busy);
@@ -266,6 +309,7 @@ impl MemcardService {
if self.admission.faulted {
return Err(Error::ReadOnly);
}
self.admission.replacement_observation = Some(self.session.diagnostics());
let handoff = Arc::new(Mutex::new(Some(std::mem::take(&mut self.session))));
let worker = handoff.clone();
let control = Arc::new(PublicationControl::default());
@@ -275,13 +319,14 @@ impl MemcardService {
.lane
.submit_memcard_commit_write(move |_| {
let mut session = worker.lock().unwrap().take().unwrap();
let result = session.replace_storage(storage, load_existing, &worker_control);
let result = operation(&mut session, &worker_control);
let _ = tx.send(Prepared::Replacement(session, result));
crate::AsyncWorkJobOutcome::Completed
})
.is_err()
{
self.session = handoff.lock().unwrap().take().unwrap();
self.admission.replacement_observation = None;
return Err(Error::InvalidState);
}
self.admission.control = Some(control);
@@ -331,6 +376,7 @@ impl MemcardService {
}
self.admission.receiver = None;
self.admission.control = None;
self.admission.replacement_observation = None;
}
Err(mpsc::TryRecvError::Disconnected) => {
self.admission.state = PreparationState::Failed(Error::InvalidState);
@@ -361,3 +407,37 @@ impl MemcardService {
}
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
use prometeu_drivers::memcard::FileMemcard;
use prometeu_hal::memcard::MemcardIoError;
#[test]
fn idle_uncertain_media_remains_blocked_before_another_persistent_admission() {
let path = std::env::temp_dir().join(format!(
"memcard-uncertain-idle-{}-{}",
std::process::id(),
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
));
let io = FileMemcard::create(&path, IMAGE_BYTES, |io, id| {
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
})
.unwrap();
let mut card = Card::mount(Box::new(io)).unwrap();
card.uncertain = true;
let mut service = MemcardService::new();
// An ended session returns its backend to the same installed-card state.
service.admission.installed = Some(card);
assert_eq!(service.installed_health(), Some((false, false, true)));
assert_eq!(
service.choose_for_game(42, 1, Choice::Persistent, 0),
Err(Error::Indeterminate)
);
assert!(service.card_installed());
assert_eq!(service.lane.pending_depth(), 0);
service.uninstall_card().unwrap();
std::fs::remove_file(path).unwrap();
}
}
@@ -0,0 +1,31 @@
//! Read-only native observations. These do not consume the guest request entry.
use super::{
image::Error,
session::{Mode, RequestResult},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RequestDiagnostics {
pub result: RequestResult,
pub storage_error: Option<Error>,
pub slot: Option<usize>,
pub operation: &'static str,
pub retained: bool,
pub io_active: bool,
pub deferred: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionDiagnostics {
pub app_id: Option<u32>,
pub card_id: Option<[u8; 16]>,
pub epoch: u64,
pub mode: Option<Mode>,
pub health: (bool, bool, bool),
pub dirty_slots: Vec<usize>,
pub request: Option<RequestDiagnostics>,
pub io_active: bool,
pub removal_pending: bool,
pub reconciliation_required: bool,
pub restart_required: bool,
}
@@ -31,12 +31,14 @@ pub fn status(e: Error) -> i64 {
use prometeu_hal::memcard::MemcardIoError as I;
match e {
Error::NoSpace | Error::Io(I::NoSpace) => 3,
Error::AccessDenied => 4,
Error::AccessDenied
| Error::IdentityMismatch
| Error::Io(I::AccessDenied | I::AlreadyExists) => 4,
Error::Corrupt => 5,
Error::Indeterminate => 6,
Error::Indeterminate | Error::Io(I::Indeterminate) => 6,
Error::Io(I::Unavailable) => 7,
Error::InvalidState | Error::Canceled => 8,
Error::Busy => 9,
Error::Busy | Error::Io(I::Busy) => 9,
Error::ReadOnly | Error::CounterExhausted | Error::Io(I::ReadOnly) => 10,
Error::InvalidRange | Error::Io(I::InvalidRange) => 11,
Error::Unsupported | Error::Io(I::Unsupported) => 12,
@@ -29,6 +29,7 @@ pub enum Error {
OutOfMemory,
AccessDenied,
InvalidState,
IdentityMismatch,
}
impl From<MemcardIoError> for Error {
fn from(e: MemcardIoError) -> Self {
@@ -7,6 +7,8 @@ use std::time::{Duration, Instant};
pub const GUEST_BYTES: usize = 4096;
static NEXT_CONTEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
// `try_update` requires Rust 1.95; keep older stable CI toolchains compatible.
#[allow(deprecated)]
fn next_context() -> Result<u64> {
NEXT_CONTEXT
.fetch_update(
@@ -65,16 +67,20 @@ struct Request {
control: Arc<PublicationControl>,
receiver: mpsc::Receiver<Completion>,
admitted: Instant,
watchdog_canceled_at: Option<Instant>,
worker_error: Option<Error>,
result: RequestResult,
job: AsyncWorkJobId,
released: bool,
saves_payload: bool,
worker_returned: bool,
}
/// Main-lane control; the Card moves into the single worker while its source is protected.
#[derive(Default)]
pub struct Session {
card_id: Option<[u8; 16]>,
uncertain_target: Option<Map>,
known_card_exclusions: Option<([u8; 16], [bool; BLOCK_COUNT])>,
health: (bool, bool, bool),
context: u64,
epoch: u64,
@@ -98,6 +104,82 @@ pub struct Session {
deferred: Option<(u64, usize, u64, Instant)>,
}
impl Session {
/// IO ownership, independent of a completed result retained for guest release.
pub fn io_active(&self) -> bool {
self.request.as_ref().is_some_and(|r| !r.worker_returned)
}
pub fn has_deferred_write(&self) -> bool {
self.deferred.is_some()
}
pub fn cancel_pending(&mut self) {
if let Some(r) = &self.request {
r.control.cancel();
}
if let Some((id, local, ..)) = self.deferred.take() {
self.slots[local].protected = false;
self.memory_result = Some(RequestResult {
id,
state: RequestState::Failure,
reason: RequestReason::Storage(Error::Canceled),
});
}
}
pub fn diagnostics(&self) -> super::diagnostics::SessionDiagnostics {
use super::diagnostics::{RequestDiagnostics, SessionDiagnostics};
let request = if let Some((id, local, ..)) = self.deferred {
Some(RequestDiagnostics {
result: RequestResult {
id,
state: RequestState::Pending,
reason: RequestReason::None,
},
storage_error: None,
slot: Some(local),
operation: "commit",
retained: true,
io_active: false,
deferred: true,
})
} else if let Some(r) = &self.request {
Some(RequestDiagnostics {
result: r.result,
storage_error: r.worker_error,
slot: r.saves_payload.then_some(r.local),
operation: if r.saves_payload { "commit" } else { "presentation" },
retained: !r.released,
io_active: self.io_active(),
deferred: false,
})
} else {
self.memory_result.map(|result| RequestDiagnostics {
result,
storage_error: None,
slot: None,
operation: "commit",
retained: true,
io_active: false,
deferred: false,
})
};
SessionDiagnostics {
app_id: self.mode.map(|_| self.app_id),
card_id: self.card_id,
epoch: self.epoch,
mode: self.mode,
health: self.health(),
dirty_slots: self
.slots
.iter()
.enumerate()
.filter_map(|(i, s)| s.data.read().ok().filter(|s| s.dirty).map(|_| i))
.collect(),
request,
io_active: self.io_active(),
removal_pending: self.removal_pending,
reconciliation_required: self.reconcile_blocked,
restart_required: self.faulted,
}
}
pub fn context(&self) -> u64 {
self.context
}
@@ -523,10 +605,13 @@ impl Session {
control,
receiver,
admitted,
watchdog_canceled_at: None,
worker_error: None,
result: RequestResult { id, state: RequestState::Pending, reason: RequestReason::None },
job,
released: !saves_payload,
saves_payload,
worker_returned: false,
});
Ok(())
}
@@ -664,10 +749,13 @@ impl Session {
control,
receiver,
admitted: now,
watchdog_canceled_at: None,
worker_error: None,
result: RequestResult { id, state: RequestState::Pending, reason: RequestReason::None },
job,
released: false,
saves_payload: true,
worker_returned: false,
});
Ok(id)
}
@@ -686,27 +774,30 @@ impl Session {
let Some(r) = &mut self.request else { return };
match r.receiver.try_recv() {
Ok(completion) => {
r.worker_returned = true;
r.worker_error = completion.result.error;
let current =
self.mode.is_some() && r.session == self.context && r.epoch == self.epoch;
if current {
self.slots[r.local].protected = false;
if r.result.reason == RequestReason::None {
if r.result.state == RequestState::Pending {
r.result.state = match completion.result.durable {
DurableResult::Success => RequestState::Success,
DurableResult::Failure => RequestState::Failure,
DurableResult::Indeterminate => RequestState::Indeterminate,
};
r.result.reason = completion
.result
.error
.map_or(RequestReason::None, RequestReason::Storage);
r.result.reason = if r.watchdog_canceled_at.is_some() {
RequestReason::Timeout
} else {
r.worker_error.map_or(RequestReason::None, RequestReason::Storage)
};
if completion.result.durable == DurableResult::Success && r.saves_payload {
let mut s = r.source.write().unwrap();
s.descriptor =
completion.card.current().slots[self.logical[r.local] as usize];
s.dirty = false;
}
} // A timeout result is immutable; returned card requires explicit reconciliation.
} // A restart escalation is immutable; returned card requires explicit reconciliation.
if self.provisional && completion.result.durable != DurableResult::Success {
self.boot_error =
Some(completion.result.error.unwrap_or(Error::InvalidState));
@@ -755,7 +846,10 @@ impl Session {
return;
}
let elapsed = now.saturating_duration_since(r.admitted);
if elapsed >= Duration::from_millis(500) {
if elapsed >= Duration::from_millis(500) && !r.control.canceled() {
// Main-lane cancellation cause is independent of the worker's durable
// outcome. Do not relabel an earlier removal or termination cancel.
r.watchdog_canceled_at = Some(now);
r.control.cancel();
}
if elapsed >= Duration::from_secs(1) {
@@ -788,6 +882,12 @@ impl Session {
.map(|r| r.result)
.ok_or(Error::InvalidRange)
}
/// Original worker error for native diagnostics, independent of guest deadline reason.
/// Pending and RAM-only requests have no worker error. Retention follows query/release.
pub fn request_storage_error(&self, context: u64, id: u64) -> Result<Option<Error>> {
self.query(context, id)?;
Ok(self.request.as_ref().filter(|r| r.id == id).and_then(|r| r.worker_error))
}
pub fn release_request(&mut self, context: u64, id: u64) -> Result<()> {
self.query(context, id)?;
if self.deferred.is_some() {
@@ -802,7 +902,7 @@ impl Session {
return Err(Error::Busy);
}
r.released = true;
if self.card.is_some() {
if r.worker_returned {
self.request = None;
}
Ok(())
@@ -816,7 +916,7 @@ impl Session {
r.control.cancel();
r.released = true;
}
if self.card.is_some() {
if !self.io_active() {
self.request = None;
}
self.removal_pending = false;
@@ -852,7 +952,23 @@ impl Session {
load_existing: bool,
control: &PublicationControl,
) -> Result<()> {
if self.has_in_flight() || self.boot_active || self.mode.is_none() {
self.replace_storage_with(move || Ok(storage), load_existing, false, control)
}
pub(super) fn reconcile_storage_with(
&mut self,
open: impl FnOnce() -> Result<Box<dyn prometeu_hal::memcard::MemcardStorage>>,
control: &PublicationControl,
) -> Result<()> {
self.replace_storage_with(open, false, true, control)
}
fn replace_storage_with(
&mut self,
open: impl FnOnce() -> Result<Box<dyn prometeu_hal::memcard::MemcardStorage>>,
load_existing: bool,
same_only: bool,
control: &PublicationControl,
) -> Result<()> {
if self.io_active() || self.deferred.is_some() || self.boot_active || self.mode.is_none() {
return Err(Error::Busy);
}
if self.icon.is_empty()
@@ -865,11 +981,18 @@ impl Session {
{
self.uncertain_target = card.attempted_publication.clone();
}
let known = self.card.as_ref().map(|c| (c.current().card_id, c.known_exclusions()));
if let Some(card) = &self.card {
self.known_card_exclusions = Some((card.current().card_id, card.known_exclusions()));
}
let known = self.known_card_exclusions;
self.card.take(); // Release the old scratch before mounting another image.
self.scratch = None;
self.reconcile_blocked = true;
self.card = Some(Card::mount(storage)?);
let candidate = Card::mount(open()?)?;
if same_only && self.card_id != Some(candidate.current().card_id) {
return Err(Error::IdentityMismatch);
}
self.card = Some(candidate);
let card = self.card.as_mut().unwrap();
let same = self.card_id == Some(card.current().card_id);
if let Some((id, known)) = known
@@ -956,14 +1079,15 @@ impl Session {
Ok(())
}
pub fn detach(&mut self) -> Result<()> {
if self.deferred.is_some()
|| self
.request
.as_ref()
.is_some_and(|r| r.result.state == RequestState::Pending || self.card.is_none())
{
if self.deferred.is_some() || self.io_active() {
return Err(Error::Busy);
}
if self.card.is_none() && self.scratch.is_none() {
let mut scratch = Vec::new();
scratch.try_reserve_exact(SLOT_BYTES).map_err(|_| Error::OutOfMemory)?;
scratch.resize(SLOT_BYTES, 0);
self.scratch = Some(scratch);
}
self.mode = Some(Mode::MemoryOnly);
self.removal_pending = false;
self.reconcile_blocked = false;
@@ -16,6 +16,8 @@ impl PublicationControl {
pub fn publication_attempted(&self) -> bool {
self.0.load(Ordering::SeqCst) & 1 != 0
}
// `try_update` requires Rust 1.95; keep older stable CI toolchains compatible.
#[allow(deprecated)]
fn enter(&self) -> Result<()> {
self.0
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |state| {
@@ -280,8 +280,157 @@ fn async_commit_protects_only_source_and_retains_result_without_occupying_lane()
controller.stop().unwrap();
}
// The sentinel executes after the save on the single lane. Wall time only bounds
// a broken fixture; all watchdog observations use explicit simulated instants.
fn finish_queued_save(lane: Arc<crate::AsyncWorkLane>) {
let (tx, rx) = std::sync::mpsc::channel();
lane.submit_asset_load(move |_| {
tx.send(()).unwrap();
crate::services::async_work::AsyncWorkJobOutcome::Completed
})
.unwrap();
let mut controller =
crate::AsyncWorkLaneController::start(crate::AsyncWorkLaneConfig::default(), lane);
rx.recv_timeout(std::time::Duration::from_secs(5)).unwrap();
controller.stop().unwrap();
}
fn assert_guest_query(
session: &mut super::super::session::Session,
id: u64,
state: i64,
reason: i64,
) {
use prometeu_bytecode::Value;
use prometeu_hal::{
HostReturn, host_return::GuestStringAllocator, syscalls::Syscall, vm_fault::VmFault,
};
struct NoStrings;
impl GuestStringAllocator for NoStrings {
fn byte_limit(&self) -> u64 {
0
}
fn build(
&mut self,
_: usize,
_: &mut dyn FnMut(&mut dyn std::fmt::Write) -> std::fmt::Result,
) -> std::result::Result<Arc<str>, VmFault> {
panic!("MemQuery must not allocate strings")
}
}
let mut service = super::super::MemcardService::new();
std::mem::swap(session, &mut service.session);
let args = [Value::Int64(service.session.context() as i64), Value::Int64(id as i64)];
let mut values = Vec::with_capacity(Syscall::MemQuery.results_count());
let mut strings = NoStrings;
let mut ret = HostReturn::new(&mut values, Syscall::MemQuery.results_count(), &mut strings);
service.guest(Syscall::MemQuery, &args, &mut ret, 4, 0, true).unwrap();
assert!(!ret.overflowed());
assert!(ret.failure().is_none());
assert_eq!(values, vec![Value::Int64(0), Value::Int64(state), Value::Int64(reason)]);
std::mem::swap(session, &mut service.session);
}
#[test]
fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
fn queued_memcard_watchdog_preserves_timeout_and_guest_reason() {
use super::super::session::{RequestReason, RequestState, Session};
use std::time::{Duration, Instant};
for bootstrap in [false, true] {
let (mut card, disk) = formatted();
add(&mut card, 4, 1);
let before = card.current().slots[0];
let mut s = Session::default();
let c = s.load_card(card, 4, 1, Session::required_bytes(1).unwrap()).unwrap();
s.write(c, 0, 0, b"pending").unwrap();
let lane = Arc::new(crate::AsyncWorkLane::default());
let now = Instant::now();
if bootstrap {
s.begin_boot();
}
let id = s.commit(c, 0, 7, now, &lane).unwrap();
if bootstrap {
s.finish_boot(true, &lane, now).unwrap();
}
s.poll(now + Duration::from_millis(499));
assert_guest_query(&mut s, id, 0, 0);
for _ in 0..2 {
s.poll(now + Duration::from_millis(500));
}
assert_guest_query(&mut s, id, 0, 0);
assert_eq!(s.release_request(c, id), Err(Error::Busy));
assert_eq!(s.clear(c, 0), Err(Error::Busy));
finish_queued_save(lane);
s.poll(now + Duration::from_millis(501));
let result = s.query(c, id).unwrap();
assert_eq!(result.state, RequestState::Failure);
assert_eq!(result.reason, RequestReason::Timeout);
assert_eq!(s.request_storage_error(c, id), Ok(Some(Error::Canceled)));
assert_guest_query(&mut s, id, 2, 15);
assert!(s.unsaved());
assert_eq!(reboot(&disk).current().slots[0], before);
assert_eq!(s.query(c + 1, id), Err(Error::AccessDenied));
s.release_request(c, id).unwrap();
assert_eq!(s.query(c, id), Err(Error::InvalidRange));
s.clear(c, 0).unwrap();
}
}
#[test]
fn lifecycle_cancellation_is_not_a_watchdog_timeout() {
use super::super::session::{RequestReason, RequestState, Session};
use std::time::{Duration, Instant};
for end in [false, true] {
let (mut card, _) = formatted();
add(&mut card, 4, 1);
let mut s = Session::default();
let c = s.load_card(card, 4, 1, Session::required_bytes(1).unwrap()).unwrap();
let lane = Arc::new(crate::AsyncWorkLane::default());
let now = Instant::now();
let id = s.commit(c, 0, 7, now, &lane).unwrap();
if end {
s.end_session();
} else {
s.notify_removal();
}
s.poll(now + Duration::from_millis(500));
finish_queued_save(lane);
s.poll(now + Duration::from_millis(501));
if end {
assert_eq!(s.query(c, id), Err(Error::AccessDenied));
let card = s.take_idle_card().unwrap().unwrap();
let new = s.load_card(card, 4, 1, Session::required_bytes(1).unwrap()).unwrap();
assert_ne!(new, c);
assert_eq!(s.query(new, id), Err(Error::InvalidRange));
} else {
assert_eq!(s.query(c, id).unwrap().state, RequestState::Failure);
assert_eq!(s.query(c, id).unwrap().reason, RequestReason::Storage(Error::Canceled));
assert_guest_query(&mut s, id, 2, 8);
}
}
}
#[test]
fn deferred_boot_memcard_deadline_stays_timeout() {
use super::super::session::{RequestReason, Session};
use std::time::{Duration, Instant};
let (mut card, _) = formatted();
add(&mut card, 4, 1);
let mut s = Session::default();
let c = s.load_card(card, 4, 1, Session::required_bytes(1).unwrap()).unwrap();
s.begin_boot();
let lane = crate::AsyncWorkLane::default();
let now = Instant::now();
let id = s.commit(c, 0, 7, now, &lane).unwrap();
s.poll(now + Duration::from_millis(500));
assert_eq!(s.query(c, id).unwrap().reason, RequestReason::Timeout);
assert_guest_query(&mut s, id, 2, 15);
assert_eq!(lane.pending_depth(), 0);
s.clear(c, 0).unwrap();
s.release_request(c, id).unwrap();
}
#[test]
fn blocked_host_memcard_watchdog_preserves_outcome_and_escalation() {
use crate::services::async_work::{
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
};
@@ -290,6 +439,7 @@ fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
struct Blocked {
store: Box<dyn MemcardStorage>,
publication: bool,
fail_publication: bool,
entered: std::sync::mpsc::Sender<()>,
resume: std::sync::mpsc::Receiver<()>,
}
@@ -310,12 +460,24 @@ fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
if if self.publication { b.starts_with(b"PMMAP1\0\0") } else { o >= DATA_OFFSET } {
self.entered.send(()).unwrap();
self.resume.recv().unwrap();
if self.fail_publication {
return Err(MemcardIoError::IoError);
}
}
self.store.write_at(o, b)
}
}
for publication in [false, true] {
let (mut card, _) = formatted();
for (publication, fail_publication, escalate, release_early) in [
(false, false, false, false),
(true, false, false, false),
(true, true, false, false),
(false, false, true, false),
(true, false, true, false),
(true, true, true, false),
(false, false, true, true),
(true, false, true, true),
] {
let (mut card, disk) = formatted();
add(&mut card, 4, 1);
let (et, er) = std::sync::mpsc::channel();
let (rt, rr) = std::sync::mpsc::channel();
@@ -324,6 +486,7 @@ fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
entered: et,
resume: rr,
publication,
fail_publication,
}))
.unwrap();
let mut s = Session::default();
@@ -336,22 +499,84 @@ fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
let id = s.commit(c, 0, 7, admitted, &lane).unwrap();
er.recv_timeout(Duration::from_secs(1)).unwrap();
s.poll(admitted + Duration::from_millis(500));
s.poll(admitted + Duration::from_secs(1));
let result = s.query(c, id).unwrap();
assert_eq!(
result.state,
if publication { RequestState::Indeterminate } else { RequestState::Failure }
);
assert_eq!(result.reason, RequestReason::RestartRequired);
assert!(s.host_faulted());
s.release_request(c, id).unwrap();
assert_guest_query(&mut s, id, 0, 0);
let terminal = if escalate {
s.poll(admitted + Duration::from_secs(1));
let result = s.query(c, id).unwrap();
assert_eq!(
result.state,
if publication { RequestState::Indeterminate } else { RequestState::Failure }
);
assert_eq!(result.reason, RequestReason::RestartRequired);
assert!(s.host_faulted());
if release_early {
s.release_request(c, id).unwrap();
}
Some(result)
} else {
None
};
assert_eq!(s.write(c, 0, 0, b"unsafe"), Err(Error::Busy));
assert_eq!(s.commit(c, 0, 7, Instant::now(), &lane), Err(Error::Busy));
assert_eq!(s.commit(c, 0, 7, admitted, &lane), Err(Error::Busy));
rt.send(()).unwrap();
controller.stop().unwrap();
s.poll(Instant::now());
assert!(s.unsaved());
assert!(s.host_faulted());
s.poll(admitted + Duration::from_millis(if escalate { 1001 } else { 501 }));
if let Some(terminal) = terminal {
if release_early {
assert_eq!(s.query(c, id), Err(Error::InvalidRange));
} else {
assert_eq!(s.query(c, id), Ok(terminal));
assert_guest_query(&mut s, id, if publication { 3 } else { 2 }, 15);
s.release_request(c, id).unwrap();
}
assert!(s.unsaved());
assert!(s.host_faulted());
assert_eq!(s.commit(c, 0, 7, admitted, &lane), Err(Error::ReadOnly));
} else {
let expected = if fail_publication {
RequestState::Indeterminate
} else if publication {
RequestState::Success
} else {
RequestState::Failure
};
assert_eq!(s.query(c, id).unwrap().state, expected);
assert_eq!(s.query(c, id).unwrap().reason, RequestReason::Timeout);
assert_guest_query(
&mut s,
id,
if fail_publication {
3
} else if publication {
1
} else {
2
},
15,
);
assert_eq!(
s.request_storage_error(c, id),
Ok(if fail_publication {
Some(Error::Io(MemcardIoError::IoError))
} else if publication {
None
} else {
Some(Error::Canceled)
})
);
assert_eq!(s.unsaved(), expected != RequestState::Success);
assert_eq!(s.removal_pending(), fail_publication);
assert!(!s.host_faulted());
s.release_request(c, id).unwrap();
}
let mut durable = reboot(&disk);
let mut bytes = [0; 7];
let n = durable.read_slot(0, &mut bytes).unwrap();
if publication && !fail_publication {
assert_eq!(&bytes[..n], b"pending");
} else {
assert_eq!(n, 0);
}
}
}
@@ -660,3 +885,232 @@ fn ending_a_session_releases_a_completed_unconsumed_request() {
assert!(s.take_idle_card().unwrap().is_some());
controller.stop().unwrap();
}
#[test]
fn persistent_save_preempts_real_multichunk_asset_decoders() {
use crate::memcard::session::{RequestState, Session};
use crate::{AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController};
use prometeu_drivers::{
AssetManager, GlyphBankPoolAccess, MemoryBanks, SceneBankPoolAccess, SoundBankPoolAccess,
};
use prometeu_hal::{
AssetBridge,
asset::{AssetCodec, AssetEntry, AssetOpStatus, BankType, LoadStatus},
background_work::{AssetWork, BackgroundWork, WorkUnavailable},
cartridge::AssetsPayloadSource,
};
use std::{
sync::mpsc,
thread,
time::{Duration, Instant},
};
type Events = Arc<Mutex<Vec<(&'static str, thread::ThreadId)>>>;
struct ObservedLane {
lane: Arc<AsyncWorkLane>,
events: Events,
paused: mpsc::Sender<()>,
resume: Mutex<Option<mpsc::Receiver<()>>>,
}
impl BackgroundWork for ObservedLane {
fn submit_asset(&self, mut work: AssetWork) -> std::result::Result<(), WorkUnavailable> {
let events = self.events.clone();
let paused = self.paused.clone();
let resume = self.resume.lock().unwrap().take().unwrap();
let mut step = 0;
self.lane.submit_asset(Box::new(move |canceled| {
let result = work(canceled);
step += 1;
events.lock().unwrap().push(("asset", thread::current().id()));
if step == 4 {
paused.send(()).unwrap();
resume.recv_timeout(Duration::from_secs(10)).unwrap();
}
result
}))
}
}
struct ObservedStore {
inner: Store,
events: Events,
}
impl MemcardStorage for ObservedStore {
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
self.inner.len()
}
fn read_only(&self) -> bool {
false
}
fn read_at(
&mut self,
offset: u64,
out: &mut [u8],
) -> std::result::Result<(), MemcardIoError> {
self.inner.read_at(offset, out)
}
fn write_at(
&mut self,
offset: u64,
bytes: &[u8],
) -> std::result::Result<(), MemcardIoError> {
self.events.lock().unwrap().push(("save write", thread::current().id()));
self.inner.write_at(offset, bytes)
}
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
self.events.lock().unwrap().push(("save sync", thread::current().id()));
self.inner.sync()
}
}
for kind in [BankType::GLYPH, BankType::SOUNDS, BankType::SCENE] {
let (card, disk) = formatted();
drop(card);
let events: Events = Arc::default();
let mut card = Card::mount(Box::new(ObservedStore {
inner: Store(disk.clone()),
events: events.clone(),
}))
.unwrap();
add(&mut card, 1, 1);
let mut session = Session::default();
let context = session.load_card(card, 1, 1, 1024 * 1024).unwrap();
session.write(context, 0, 0, b"persistent under asset pressure").unwrap();
events.lock().unwrap().clear();
let (bytes, decoded_size, metadata) = match kind {
BankType::GLYPH => {
let mut bytes = vec![0x12; 16384];
bytes.extend([255; 64]);
(
bytes,
32768 + 64,
serde_json::json!({"tile_size":8,"width":256,"height":128,"palette_count":1,"palette_authored":1}),
)
}
BankType::SOUNDS => {
(vec![0x12; 16385], 16385, serde_json::json!({"sample_rate":44100,"channels":1}))
}
BankType::SCENE => {
let mut bytes = vec![0; 140];
bytes[..8].copy_from_slice(&[b'S', b'C', b'N', b'E', 1, 0, 4, 0]);
for start in [12, 44, 76, 108] {
bytes[start + 5] = 8;
}
bytes[12] = 1;
bytes[28..32].copy_from_slice(&4097_u32.to_le_bytes());
bytes[32..36].copy_from_slice(&1_u32.to_le_bytes());
bytes[36..40].copy_from_slice(&4097_u32.to_le_bytes());
bytes.splice(44..44, std::iter::repeat_n([7, 3, 0x34, 0x12], 4097).flatten());
(
bytes,
64 + 4097 * size_of::<prometeu_hal::tile::Tile>() as u64,
serde_json::json!({}),
)
}
};
let entry = AssetEntry {
asset_id: 1,
asset_name: "pressure".into(),
bank_type: kind,
offset: 0,
size: bytes.len() as u64,
decoded_size,
codec: AssetCodec::None,
metadata,
};
let banks = Arc::new(MemoryBanks::new());
let mut manager = AssetManager::new(
vec![entry],
AssetsPayloadSource::from_bytes(bytes),
banks.clone(),
banks.clone(),
banks.clone(),
);
let lane = Arc::new(AsyncWorkLane::default());
let (paused_tx, paused_rx) = mpsc::channel();
let (resume_tx, resume_rx) = mpsc::channel();
let observed = Arc::new(ObservedLane {
lane: lane.clone(),
events: events.clone(),
paused: paused_tx,
resume: Mutex::new(Some(resume_rx)),
});
assert!(manager.bind_background_work(observed));
let mut controller =
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
let handle = manager.load(1, 0).unwrap();
paused_rx.recv_timeout(Duration::from_secs(10)).unwrap();
assert_eq!(manager.status(handle), LoadStatus::LOADING);
assert!(
banks.glyph_bank_slot(0).is_none()
&& banks.sound_bank_slot(0).is_none()
&& banks.scene_bank_slot(0).is_none()
);
let request = session.commit(context, 0, 7, Instant::now(), &lane).unwrap();
resume_tx.send(()).unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
loop {
session.poll(Instant::now());
if session.query(context, request).unwrap().state != RequestState::Pending
&& manager.status(handle) == LoadStatus::READY
{
break;
}
assert!(Instant::now() < deadline, "save/asset did not complete for {kind:?}");
thread::yield_now();
}
assert_eq!(session.query(context, request).unwrap().state, RequestState::Success);
assert!(
banks.glyph_bank_slot(0).is_none()
&& banks.sound_bank_slot(0).is_none()
&& banks.scene_bank_slot(0).is_none()
);
assert_eq!(manager.commit(handle), AssetOpStatus::Ok);
manager.apply_commits();
match kind {
BankType::GLYPH => {
let bank = banks.glyph_bank_slot(0).unwrap();
assert_eq!((bank.width, bank.height), (256, 128));
assert_eq!(
bank.pixel_indices,
(0..32768).map(|i| 1 + (i % 2) as u8).collect::<Vec<_>>()
);
assert!(bank.palettes[0].iter().all(|c| c.raw() == 0xffffffff));
}
BankType::SOUNDS => {
let bank = banks.sound_bank_slot(0).unwrap();
assert_eq!(bank.samples[0].sample_rate, 44100);
assert_eq!(bank.samples[0].data, vec![0x1212; 8192]);
}
BankType::SCENE => {
let bank = banks.scene_bank_slot(0).unwrap();
assert_eq!(
(bank.layers[0].tilemap.width, bank.layers[0].tilemap.height),
(4097, 1)
);
assert!(bank.layers[0].active);
assert_eq!(bank.layers[0].tilemap.tiles.len(), 4097);
assert!(bank.layers[0].tilemap.tiles.iter().all(|t| t.active
&& t.flip_x
&& t.flip_y
&& t.glyph.glyph_id == 0x1234
&& t.glyph.palette_id == 3));
assert!(bank.layers[1..].iter().all(|l| l.tilemap.tiles.is_empty()));
}
}
controller.stop().unwrap();
let observed = events.lock().unwrap();
assert_eq!(observed[..4].iter().map(|e| e.0).collect::<Vec<_>>(), vec!["asset"; 4]);
assert_eq!(observed[4].0, "save write");
let resumed = observed[4..].iter().position(|e| e.0 == "asset").unwrap() + 4;
assert_eq!(observed[resumed - 1].0, "save sync");
assert!(observed[resumed..].iter().all(|e| e.0 == "asset"));
let worker = observed[0].1;
assert_ne!(worker, thread::current().id());
assert!(observed.iter().all(|e| e.1 == worker));
drop(observed);
drop(session);
let mut recovered = Session::default();
let c = recovered.load_card(reboot(&disk), 1, 1, 1024 * 1024).unwrap();
let mut saved = [0; 64];
let n = recovered.read(c, 0, 0, &mut saved).unwrap();
assert_eq!(&saved[..n], b"persistent under asset pressure");
}
}
@@ -40,6 +40,19 @@ impl LatestRenderFrameStore {
pub fn latest_frame(&self) -> Option<OwnedRgba8888Frame> {
self.latest.lock().unwrap().clone()
}
/// Copy only a different complete publication; paused observers need not copy pixels.
pub fn latest_frame_after(
&self,
previous: Option<(RenderOwnership, FrameId)>,
) -> Option<OwnedRgba8888Frame> {
self.latest
.lock()
.unwrap()
.as_ref()
.filter(|frame| previous != Some((frame.ownership, frame.frame_id)))
.cloned()
}
}
impl RenderWorkerFrameSink for LatestRenderFrameStore {
+4
View File
@@ -18,3 +18,7 @@ fpmath = { version = "=0.1.1", features = ["soft-float"] }
[dev-dependencies]
prometeu-test-support = { path = "../../dev/prometeu-test-support" }
[[example]]
name = "string_provenance_benchmark"
required-features = ["resource-diagnostics"]
@@ -0,0 +1,83 @@
//! Run in release with --features resource-diagnostics; query setup is excluded.
#[allow(dead_code)]
#[path = "../tests/support/callables.rs"]
mod callables;
use prometeu_bytecode::model::ConstantPoolEntry;
use prometeu_vm::string_provenance_qualification::Fixture;
use std::{
alloc::{GlobalAlloc, Layout, System},
sync::atomic::{AtomicUsize, Ordering},
time::Instant,
};
struct Counting;
static ALLOC: AtomicUsize = AtomicUsize::new(0);
static REALLOC: AtomicUsize = AtomicUsize::new(0);
static FREE: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
ALLOC.fetch_add(1, Ordering::Relaxed);
unsafe { System.alloc(l) }
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
FREE.fetch_add(1, Ordering::Relaxed);
unsafe { System.dealloc(p, l) }
}
unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
REALLOC.fetch_add(1, Ordering::Relaxed);
unsafe { System.realloc(p, l, n) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn counts() -> [usize; 3] {
[ALLOC.load(Ordering::Relaxed), REALLOC.load(Ordering::Relaxed), FREE.load(Ordering::Relaxed)]
}
fn main() {
for n in [1024, 16384, 65536] {
let constants = vec![ConstantPoolEntry::String("same".into()); n];
let start = Instant::now();
let image = callables::image_constants(
&[("RET", callables::sig(&[], &[]), vec![])],
vec![],
&constants,
)
.unwrap();
let image_ns = start.elapsed().as_nanos();
let payload = image.payload_bytes();
let before = counts();
let start = Instant::now();
let mut fixture = Fixture::new(image, n);
let creation_ns = start.elapsed().as_nanos();
let after = counts();
println!(
"setup n={n} image_load_ns={image_ns} image_payload_bytes={payload} session_ns={creation_ns} session_allocs={} auxiliary_bytes={} work={:?}",
after[0] - before[0],
fixture.auxiliary_bytes(),
fixture.work()
);
for name in [
"image_hit",
"image_miss",
"session_hit",
"session_miss",
"import_hit",
"snapshot_mixed",
"import_miss",
] {
let before = counts();
let start = Instant::now();
for _ in 0..1000 {
fixture.run(name);
}
let ns = start.elapsed().as_nanos();
let after = counts();
let delta = std::array::from_fn::<_, 3, _>(|i| after[i] - before[i]);
if name.ends_with("hit") || name == "image_miss" || name == "session_miss" {
assert_eq!(delta, [0; 3]);
}
println!(
"query n={n} name={name} iterations=1000 ns={ns} alloc_realloc_free={delta:?}"
);
}
}
}
+103 -14
View File
@@ -235,12 +235,13 @@ pub type IntrinsicHostImplementation =
pub enum IntrinsicImplementation {
Host(IntrinsicHostImplementation),
VmOwned,
BorrowedInline(crate::IntrinsicInlineImplementation),
}
impl IntrinsicImplementation {
pub fn host_callback(self) -> Option<IntrinsicHostImplementation> {
match self {
Self::Host(f) => Some(f),
Self::VmOwned => None,
Self::VmOwned | Self::BorrowedInline(_) => None,
}
}
}
@@ -249,12 +250,14 @@ impl IntrinsicImplementation {
pub enum IntrinsicCostModel {
Fixed,
RandomRejectionV1,
ArgumentUtf8BytesV1,
}
impl IntrinsicCostModel {
pub const fn name(self) -> &'static str {
match self {
Self::Fixed => "fixed",
Self::RandomRejectionV1 => "random_rejection_v1",
Self::ArgumentUtf8BytesV1 => "argument_utf8_bytes_v1",
}
}
}
@@ -266,11 +269,14 @@ pub struct IntrinsicMeta {
pub name: &'static str,
pub version: u16,
pub arg_layout: &'static [AbiType],
pub arg_nullable: &'static [bool],
pub ret_layout: &'static [AbiType],
pub parameter_names: &'static [&'static str],
/// Full fixed cost, or base cost for a variable model.
pub cost_cycles: u64,
pub cost_model: IntrinsicCostModel,
pub cost_arg_index: Option<usize>,
pub cost_per_byte: u64,
pub attempt_cost_cycles: u64,
pub max_attempts: u32,
pub mutates_guest_state: bool,
@@ -300,7 +306,16 @@ impl IntrinsicMeta {
&self,
values: &[Value],
) -> Result<(), IntrinsicExecutionError> {
Self::validate_values(values, self.arg_layout)
Self::validate_values(values, self.arg_layout)?;
for (index, (value, nullable)) in values.iter().zip(self.arg_nullable).enumerate() {
if matches!(value, Value::Null) && !nullable {
return Err(IntrinsicExecutionError::TypeMismatch {
index,
expected: self.arg_layout[index],
});
}
}
Ok(())
}
fn validate_values(
@@ -563,10 +578,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "dot",
version: 1,
arg_layout: &VEC2_DOT_ARGS,
arg_nullable: &[false; VEC2_DOT_ARGS.len()],
ret_layout: &SINGLE_FLOAT_RET,
parameter_names: &["ax", "ay", "bx", "by"],
cost_cycles: 10,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -580,10 +598,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "length",
version: 1,
arg_layout: &VEC2_LENGTH_ARGS,
arg_nullable: &[false; VEC2_LENGTH_ARGS.len()],
ret_layout: &SINGLE_FLOAT_RET,
parameter_names: &["x", "y"],
cost_cycles: 40,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -597,10 +618,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "from_raw",
version: 1,
arg_layout: &INT_RET,
arg_nullable: &[false; INT_RET.len()],
ret_layout: &COLOR_RET,
parameter_names: &["raw"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -614,10 +638,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "rgb",
version: 1,
arg_layout: &COLOR_RGB_ARGS,
arg_nullable: &[false; COLOR_RGB_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &["r", "g", "b"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -631,10 +658,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "rgba",
version: 1,
arg_layout: &COLOR_RGBA_ARGS,
arg_nullable: &[false; COLOR_RGBA_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &["r", "g", "b", "a"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -648,10 +678,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "html_rgba",
version: 1,
arg_layout: &COLOR_HTML_RGBA_ARGS,
arg_nullable: &[false; COLOR_HTML_RGBA_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &["value"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -665,10 +698,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "gray_scale",
version: 1,
arg_layout: &INT_RET,
arg_nullable: &[false; INT_RET.len()],
ret_layout: &COLOR_RET,
parameter_names: &["value"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -682,10 +718,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "hex",
version: 1,
arg_layout: &COLOR_ARGS,
arg_nullable: &[false; COLOR_ARGS.len()],
ret_layout: &INT_RET,
parameter_names: &["color"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -699,10 +738,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "alpha",
version: 1,
arg_layout: &COLOR_ARGS,
arg_nullable: &[false; COLOR_ARGS.len()],
ret_layout: &INT_RET,
parameter_names: &["color"],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -716,10 +758,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "black",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -733,10 +778,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "white",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -750,10 +798,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "red",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -767,10 +818,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "green",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -784,10 +838,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "blue",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -801,10 +858,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "yellow",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -818,10 +878,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "orange",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -835,10 +898,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "indigo",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -852,10 +918,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "gray",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -869,10 +938,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "cyan",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -886,10 +958,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "magenta",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -903,10 +978,13 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
name: "transparent",
version: 1,
arg_layout: &NO_ARGS,
arg_nullable: &[false; NO_ARGS.len()],
ret_layout: &COLOR_RET,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -944,6 +1022,7 @@ pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'stati
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
}
@@ -953,6 +1032,7 @@ pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
.find(|meta| meta.id == id)
}
@@ -1254,15 +1334,36 @@ pub(crate) fn metered_lookup_intrinsic(
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
{
budget.work(1)?;
if meta.id == id {
budget.work(meta.arg_slots() + 1)?;
meta.validate_descriptor().map_err(|_| {
prometeu_bytecode::load_limits::LoadFailure::new(
prometeu_bytecode::load_limits::LoadCode::InvalidCode,
prometeu_bytecode::load_limits::LoadReason::NativeId,
)
})?;
return Ok(Some(meta));
}
}
Ok(None)
}
/// Runtime-owned catalogue, including input and scalar math.
pub fn intrinsic_catalog() -> impl Iterator<Item = &'static IntrinsicMeta> {
INTRINSICS
.iter()
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
}
pub fn builtin_constant_catalog() -> impl Iterator<Item = &'static BuiltinConstMeta> {
BUILTIN_CONSTS.iter()
}
#[cfg(test)]
mod tests {
use super::*;
@@ -1432,15 +1533,3 @@ mod tests {
assert_eq!(pixel.field("color").map(|field| field.start_slot), Some(2));
}
}
/// Runtime-owned catalogue, including input and scalar math.
pub fn intrinsic_catalog() -> impl Iterator<Item = &'static IntrinsicMeta> {
INTRINSICS
.iter()
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
}
pub fn builtin_constant_catalog() -> impl Iterator<Item = &'static BuiltinConstMeta> {
BUILTIN_CONSTS.iter()
}
+10 -3
View File
@@ -16,14 +16,15 @@ fn atom(t: AbiType) -> String {
}
pub fn intrinsic_catalog_csv() -> String {
let mut out = String::from(
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts\n",
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts,schema_version,arg_nullable,invocation_model,scratch_model,context_access,cost_arg_index,cost_per_byte\n",
);
let mut entries: Vec<_> = intrinsic_catalog().collect();
entries.sort_by_key(|m| m.id);
for m in entries {
m.validate_descriptor().expect("valid canonical intrinsic descriptor");
writeln!(
&mut out,
"0x{:04X},{},{}.{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
"0x{:04X},{},{}.{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},2,{},{},{},{},{},{}",
m.id,
m.id,
m.owner,
@@ -42,7 +43,13 @@ pub fn intrinsic_catalog_csv() -> String {
m.mutates_guest_state,
m.cost_model.name(),
m.attempt_cost_cycles,
m.max_attempts
m.max_attempts,
m.arg_nullable.iter().map(bool::to_string).collect::<Vec<_>>().join("|"),
m.invocation_model(),
m.scratch_model(),
m.context_access(),
m.cost_arg_index.map_or_else(String::new, |i| i.to_string()),
m.cost_per_byte
)
.unwrap();
}
@@ -0,0 +1,66 @@
use crate::{
AbiType, BuiltinScalarType as S, IntrinsicCostModel as C, IntrinsicExecutionError as E,
IntrinsicImplementation as I, IntrinsicMeta, IntrinsicResults,
};
use prometeu_bytecode::Value;
const fn entry(
id: u32,
name: &'static str,
abi: AbiType,
nullable: bool,
handler: crate::IntrinsicInlineImplementation,
) -> IntrinsicMeta {
IntrinsicMeta {
id,
owner: "hash",
name,
version: 1,
arg_layout: match abi {
AbiType::Scalar(S::Int64) => &[AbiType::Scalar(S::Int64)],
AbiType::Scalar(S::Float) => &[AbiType::Scalar(S::Float)],
AbiType::Scalar(S::String) => &[AbiType::Scalar(S::String)],
_ => panic!("unsupported hash input ABI"),
},
ret_layout: &[AbiType::Scalar(S::Int64)],
arg_nullable: if nullable { &[true] } else { &[false] },
parameter_names: &["value"],
cost_cycles: 8,
cost_model: if matches!(abi, AbiType::Scalar(S::String)) {
C::ArgumentUtf8BytesV1
} else {
C::Fixed
},
cost_arg_index: if matches!(abi, AbiType::Scalar(S::String)) { Some(0) } else { None },
cost_per_byte: if matches!(abi, AbiType::Scalar(S::String)) { 1 } else { 0 },
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
deterministic: true,
may_allocate: false,
implementation: I::BorrowedInline(handler),
}
}
pub(crate) const HASH_INTRINSICS: [IntrinsicMeta; 3] = [
entry(0x4100, "i64", AbiType::Scalar(S::Int64), false, hash_i64),
entry(0x4101, "float", AbiType::Scalar(S::Float), false, hash_float),
entry(0x4102, "string", AbiType::Scalar(S::String), true, hash_string),
];
fn hash_i64(args: &[Value], out: &mut IntrinsicResults) -> Result<(), E> {
let Value::Int64(value) = args[0] else { unreachable!("exact validated Int64") };
out.push(Value::Int64(crate::hashing::xxh64(&value.to_le_bytes()) as i64))
}
fn hash_float(args: &[Value], out: &mut IntrinsicResults) -> Result<(), E> {
let Value::Float(value) = args[0] else { unreachable!("exact validated Float") };
crate::numeric::check_inputs(&[value])?;
let bits = if value == 0.0 { 0 } else { value.to_bits() };
out.push(Value::Int64(crate::hashing::xxh64(&bits.to_le_bytes()) as i64))
}
fn hash_string(args: &[Value], out: &mut IntrinsicResults) -> Result<(), E> {
let hash = match &args[0] {
Value::Null => 0,
Value::String(s) => crate::hashing::xxh64(s.as_bytes()) as i64,
_ => unreachable!("exact validated nullable String"),
};
out.push(Value::Int64(hash))
}
+131
View File
@@ -0,0 +1,131 @@
//! Hash-v1: portable XXH64, seed zero, over explicit machine byte encodings.
use prometeu_bytecode::HeapRef;
const P1: u64 = 0x9E3779B185EBCA87;
const P2: u64 = 0xC2B2AE3D27D4EB4F;
const P3: u64 = 0x165667B19E3779F9;
const P4: u64 = 0x85EBCA77C2B2AE63;
const P5: u64 = 0x27D4EB2F165667C5;
fn word(bytes: &[u8]) -> u64 {
u64::from_le_bytes(bytes[..8].try_into().expect("complete XXH64 lane"))
}
fn round(acc: u64, lane: u64) -> u64 {
acc.wrapping_add(lane.wrapping_mul(P2)).rotate_left(31).wrapping_mul(P1)
}
fn merge(acc: u64, lane: u64) -> u64 {
(acc ^ round(0, lane)).wrapping_mul(P1).wrapping_add(P4)
}
pub(crate) fn xxh64(mut bytes: &[u8]) -> u64 {
let length = bytes.len() as u64;
let mut hash = if bytes.len() >= 32 {
let mut acc = [P1.wrapping_add(P2), P2, 0, 0u64.wrapping_sub(P1)];
while bytes.len() >= 32 {
for (index, value) in acc.iter_mut().enumerate() {
*value = round(*value, word(&bytes[index * 8..]));
}
bytes = &bytes[32..];
}
let mut hash = acc[0]
.rotate_left(1)
.wrapping_add(acc[1].rotate_left(7))
.wrapping_add(acc[2].rotate_left(12))
.wrapping_add(acc[3].rotate_left(18));
for value in acc {
hash = merge(hash, value);
}
hash
} else {
P5
};
hash = hash.wrapping_add(length);
while bytes.len() >= 8 {
hash ^= round(0, word(bytes));
hash = hash.rotate_left(27).wrapping_mul(P1).wrapping_add(P4);
bytes = &bytes[8..];
}
if bytes.len() >= 4 {
hash ^= u64::from(u32::from_le_bytes(bytes[..4].try_into().unwrap())).wrapping_mul(P1);
hash = hash.rotate_left(23).wrapping_mul(P2).wrapping_add(P3);
bytes = &bytes[4..];
}
for byte in bytes {
hash ^= u64::from(*byte).wrapping_mul(P5);
hash = hash.rotate_left(11).wrapping_mul(P1);
}
hash ^= hash >> 33;
hash = hash.wrapping_mul(P2);
hash ^= hash >> 29;
hash = hash.wrapping_mul(P3);
hash ^ (hash >> 32)
}
pub(crate) fn reference(value: Option<HeapRef>) -> i64 {
value.map_or(0, |reference| {
let mut bytes = [0; 8];
bytes[..4].copy_from_slice(&reference.index.to_le_bytes());
bytes[4..].copy_from_slice(&reference.generation.to_le_bytes());
xxh64(&bytes) as i64
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_all_independent_c_reference_vectors() {
let mut count = 0;
for row in include_str!("../tests/fixtures/hash-conformance.tsv").lines().skip(1) {
let f: Vec<_> = row.split('\t').collect();
let expected = u64::from_str_radix(f[2], 16).unwrap();
let actual = match f[0] {
"bytes" | "string" => {
let bytes: Vec<_> = (0..f[1].len())
.step_by(2)
.map(|i| u8::from_str_radix(&f[1][i..i + 2], 16).unwrap())
.collect();
xxh64(&bytes)
}
"i64" => xxh64(&f[1].parse::<i64>().unwrap().to_le_bytes()),
"float" => {
let bits = u64::from_str_radix(f[1], 16).unwrap();
let bits = if f64::from_bits(bits) == 0.0 { 0 } else { bits };
xxh64(&bits.to_le_bytes())
}
"reference" => {
let (index, generation) = f[1].split_once(',').unwrap();
reference(Some(HeapRef {
index: index.parse().unwrap(),
generation: generation.parse().unwrap(),
})) as u64
}
"null" => reference(None) as u64,
kind => panic!("unknown vector {kind}"),
};
assert_eq!(actual, expected, "{row}");
count += 1;
}
assert_eq!(count, 118);
}
#[test]
fn sequential_handles_spread_and_bucket_collisions_do_not_imply_identity() {
let mut buckets = [0; 256];
let mut first = [None; 256];
let mut collisions = 0;
for index in 1..=16384 {
let handle = HeapRef { index, generation: 1 };
let bucket = reference(Some(handle)) as u64 as usize & 255;
buckets[bucket] += 1;
if let Some(previous) = first[bucket] {
assert_ne!(previous, handle);
collisions += 1;
} else {
first[bucket] = Some(handle);
}
}
assert!(buckets.iter().all(|&count| (32..=100).contains(&count)), "{buckets:?}");
assert_eq!(collisions, 16384 - 256);
}
}
+40
View File
@@ -428,6 +428,34 @@ impl Heap {
self.allocation_count.load(Ordering::Relaxed)
}
/// Host-side observations only, outside guest execution and atomic commits.
#[cfg(feature = "resource-diagnostics")]
pub fn diagnostic_typed_storage(&self) -> Vec<crate::resource_diagnostics::StorageObject> {
use crate::resource_diagnostics::{StorageObject, StorageValue};
self.objects
.iter()
.enumerate()
.filter_map(|(index, object)| {
let storage = object.as_ref()?.typed.as_ref()?;
let groups = (0..storage.len())
.map(|i| {
storage
.read(i)
.expect("initialized typed storage")
.as_slice()
.iter()
.map(StorageValue::observe)
.collect()
})
.collect();
Some(StorageObject {
reference: HeapRef { index: index as u32, generation: self.generations[index] },
type_id: storage.type_id(),
groups,
})
})
.collect()
}
#[cfg(feature = "resource-diagnostics")]
pub(crate) fn diagnostic_handles(&self) -> (usize, usize) {
(self.free_list.len(), self.objects.len() - self.free_list.len() - self.len())
@@ -520,6 +548,18 @@ impl Heap {
value_contract::validate(registry, self, storage.slot_type(index)?, values.as_slice())?;
Ok(values)
}
/// Caller has checked all receivers, bounds and groups for the entire attempt.
/// This commit path performs no allocation, provenance lookup or guest callback.
pub(crate) fn commit_typed(&mut self, r: HeapRef, index: usize, values: &[Value]) {
self.objects[r.index as usize]
.as_mut()
.unwrap()
.typed
.as_mut()
.unwrap()
.write_validated(index, values)
.expect("prevalidated storage publication");
}
pub(crate) fn store_typed(
&mut self,
registry: &Registry,
@@ -20,10 +20,13 @@ const fn query(
name,
version: 1,
arg_layout: &[],
arg_nullable: &[],
ret_layout,
parameter_names: &[],
cost_cycles: 1,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -0,0 +1,143 @@
//! The native handler variant and descriptor jointly define the invocation contract.
use crate::{
AbiType, BuiltinScalarType, IntrinsicCostModel, IntrinsicExecutionError as E,
IntrinsicImplementation as I, IntrinsicMeta,
};
use prometeu_bytecode::Value;
pub type IntrinsicInlineImplementation = fn(&[Value], &mut IntrinsicResults) -> Result<(), E>;
const INLINE_SLOTS: usize = 3;
/// Fixed native staging only; no heap buffer or mutable execution context.
#[derive(Debug)]
pub struct IntrinsicResults {
values: [Value; INLINE_SLOTS],
len: usize,
limit: usize,
overflowed: bool,
}
impl IntrinsicResults {
pub(crate) fn new(limit: usize) -> Self {
assert!(limit <= INLINE_SLOTS);
Self { values: [const { Value::Null }; INLINE_SLOTS], len: 0, limit, overflowed: false }
}
pub fn push(&mut self, value: Value) -> Result<(), E> {
if self.len >= self.limit {
self.overflowed = true;
return Err(E::ArityMismatch { expected: self.limit, got: self.len + 1 });
}
self.values[self.len] = value;
self.len += 1;
Ok(())
}
pub(crate) fn validate(&self, meta: &IntrinsicMeta) -> Result<(), E> {
if self.overflowed {
return Err(E::ArityMismatch { expected: self.limit, got: self.limit + 1 });
}
meta.validate_result_values(&self.values[..self.len])
}
pub(crate) fn as_slice(&self) -> &[Value] {
&self.values[..self.len]
}
pub(crate) fn into_values(self) -> impl Iterator<Item = Value> {
self.values.into_iter().take(self.len)
}
}
impl IntrinsicMeta {
pub fn invocation_model(&self) -> &'static str {
match self.implementation {
I::Host(_) => "host_buffered_v1",
I::VmOwned => "vm_owned_v1",
I::BorrowedInline(_) => "borrowed_inline_v1",
}
}
pub fn scratch_model(&self) -> &'static str {
match self.implementation {
I::BorrowedInline(_) => "none",
_ => "existing_contract_v1",
}
}
pub fn context_access(&self) -> &'static str {
match self.implementation {
I::Host(_) => "host",
I::VmOwned => "vm_state",
I::BorrowedInline(_) => "none",
}
}
/// Derivation from the handler variant makes incompatible profile claims unrepresentable.
pub fn validate_descriptor(&self) -> Result<(), &'static str> {
if self.arg_nullable.len() != self.arg_slots()
|| self.parameter_names.len() != self.arg_slots()
{
return Err("argument metadata widths disagree");
}
for abi in self.arg_layout.iter().chain(self.ret_layout) {
if !matches!(abi, AbiType::Scalar(_))
&& !matches!(abi, AbiType::Builtin(key) if (key.name == "Random" || key.name == "color") && key.version == 1)
{
return Err("unsupported intrinsic ABI layout");
}
}
for (abi, nullable) in self.arg_layout.iter().zip(self.arg_nullable) {
let permits_null = matches!(abi, AbiType::Scalar(BuiltinScalarType::String))
|| matches!(abi, AbiType::Builtin(key) if key.name == "Random" && key.version == 1);
if *nullable && !permits_null {
return Err("non-nullable ABI carrier declared nullable");
}
}
match self.cost_model {
IntrinsicCostModel::Fixed => {
if self.cost_arg_index.is_some()
|| self.cost_per_byte != 0
|| self.attempt_cost_cycles != 0
|| self.max_attempts != 0
{
return Err("fixed cost has variable parameters");
}
}
IntrinsicCostModel::RandomRejectionV1 => {
if !matches!(self.implementation, I::VmOwned)
|| self.cost_arg_index.is_some()
|| self.cost_per_byte != 0
|| self.attempt_cost_cycles == 0
|| self.max_attempts == 0
{
return Err("invalid random rejection cost parameters");
}
}
IntrinsicCostModel::ArgumentUtf8BytesV1 => {
if !matches!(self.implementation, I::BorrowedInline(_))
|| self.cost_arg_index.and_then(|i| self.arg_layout.get(i))
!= Some(&AbiType::Scalar(BuiltinScalarType::String))
|| self.cost_per_byte == 0
|| self.attempt_cost_cycles != 0
|| self.max_attempts != 0
{
return Err("invalid String byte-cost parameters");
}
}
}
if matches!(self.implementation, I::BorrowedInline(_))
&& (self.ret_slots() > INLINE_SLOTS
|| self.ret_slots() > self.arg_slots()
|| self.may_allocate
|| self.mutates_guest_state
|| !self.deterministic)
{
return Err("handler violates bounded invocation capabilities");
}
Ok(())
}
/// None is checked cost overflow. Wrong/missing carriers have no inferred length.
pub(crate) fn attempted_cost(&self, args: &[Value]) -> Option<u64> {
if self.cost_model != IntrinsicCostModel::ArgumentUtf8BytesV1 {
return Some(self.cost_cycles);
}
let bytes = match self.cost_arg_index.and_then(|i| args.get(i)) {
Some(Value::String(value)) => value.len() as u64,
_ => 0,
};
bytes.checked_mul(self.cost_per_byte)?.checked_add(self.cost_cycles)
}
}
+8
View File
@@ -2,7 +2,11 @@
extern crate self as prometeu_vm;
mod builtins;
mod catalog;
mod hash_intrinsics;
mod hashing;
mod intrinsic_contract;
pub use catalog::{builtin_constant_catalog_csv, intrinsic_catalog_csv};
pub use intrinsic_contract::{IntrinsicInlineImplementation, IntrinsicResults};
mod call_frame;
mod input_intrinsics;
mod math_intrinsics;
@@ -25,6 +29,7 @@ mod program_image;
mod registry_verifier;
pub use program_image::{ImageStringLease, ValidatedProgramImage};
// Image/session creation and native provenance consume this in step 19.
mod string_identity_index;
#[allow(dead_code)]
mod string_ledger;
// Registry-backed consumers are integrated with executable images in PLN-0176.
@@ -52,3 +57,6 @@ pub use virtual_machine::{BudgetReport, LogicalFrameEndingReason, VirtualMachine
pub use vm_init_error::{LoaderPatchError, VmInitError};
pub use verifier::VerifierError;
#[cfg(feature = "resource-diagnostics")]
pub use virtual_machine::string_provenance_qualification;
@@ -20,10 +20,17 @@ const fn entry<const ID: u32>(
name,
version: 1,
arg_layout,
arg_nullable: match arg_layout.len() {
1 => &[false],
2 => &[false, false],
_ => &[false, false, false],
},
ret_layout,
parameter_names,
cost_cycles,
cost_model: crate::IntrinsicCostModel::Fixed,
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: 0,
max_attempts: 0,
mutates_guest_state: false,
@@ -1,5 +1,6 @@
//! PBX v1 publication: immutable metadata and shared payload, after bindings
//! PBX v0 publication: immutable metadata and shared payload, after bindings
//! and bounded whole-program verification. No public unchecked constructor.
use crate::string_identity_index::Identity;
use crate::{LoaderPatchError, VmInitError};
use prometeu_bytecode::isa::core::CoreOpCode as Op;
use prometeu_bytecode::load_limits::{
@@ -107,8 +108,30 @@ impl ValidatedProgramImage {
Some(ImageStringLease { content: content.clone(), domain: self.strings.clone() })
}
pub fn retains_string(&self, content: &Arc<str>) -> bool {
self.lookup_string(content, || {})
}
fn lookup_string(&self, content: &Arc<str>, mut compare: impl FnMut()) -> bool {
let strings = self.strings.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
strings.records.iter().any(|(weak, _)| std::ptr::eq(weak.as_ptr(), Arc::as_ptr(content)))
let target = Identity::of(content);
let mut low = 0;
let mut high = strings.records.len();
while low < high {
let mid = low + (high - low) / 2;
compare();
match record_identity(&strings.records[mid]).cmp(&target) {
std::cmp::Ordering::Less => low = mid + 1,
std::cmp::Ordering::Greater => high = mid,
std::cmp::Ordering::Equal => return true,
}
}
false
}
#[cfg(any(test, feature = "resource-diagnostics"))]
pub(crate) fn lookup_work(&self, content: &Arc<str>) -> (bool, usize) {
let mut work = 0;
let found = self.lookup_string(content, || work += 1);
(found, work)
}
pub fn constant_content(&self) -> (u64, usize) {
let mut strings = self.strings.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
@@ -205,6 +228,7 @@ impl ValidatedProgramImage {
};
values.push(value);
}
sort_string_records(&mut records, b)?;
b.refund(Q::TemporaryBytes, constants_capacity);
// Binding no longer needs source declarations. They were validated,
// used exactly, and can be released before final payload publication.
@@ -256,6 +280,48 @@ impl ValidatedProgramImage {
}
}
fn record_identity(record: &(Weak<str>, usize)) -> Identity {
Identity { address: record.0.as_ptr() as *const u8 as usize, length: record.1 }
}
// In-place heapsort: every record comparison (four field reads + comparison)
// and swap (two record reads + writes) is precharged. No scratch allocation.
fn sort_string_records(records: &mut [(Weak<str>, usize)], b: &mut LoadBudget) -> Result<(), E> {
fn sift(
records: &mut [(Weak<str>, usize)],
mut root: usize,
end: usize,
b: &mut LoadBudget,
) -> Result<(), E> {
while root < end / 2 {
let mut child = root * 2 + 1;
if child + 1 < end {
b.work(5)?;
if record_identity(&records[child]) < record_identity(&records[child + 1]) {
child += 1;
}
}
b.work(5)?;
if record_identity(&records[root]) >= record_identity(&records[child]) {
break;
}
b.work(4)?;
records.swap(root, child);
root = child;
}
Ok(())
}
for root in (0..records.len() / 2).rev() {
sift(records, root, records.len(), b)?;
}
for end in (1..records.len()).rev() {
b.work(4)?;
records.swap(0, end);
sift(records, 0, end, b)?;
}
Ok(())
}
pub(crate) fn scratch<T>(count: usize, b: &mut LoadBudget) -> Result<Vec<T>, E> {
let bytes = count
.checked_mul(size_of::<T>())
@@ -309,3 +309,65 @@ fn sealed_image_load_probe() {
elapsed
);
}
#[test]
fn string_identity_binary_search_preserves_constant_order_and_lease_indices() {
let constants = (0..1024).map(|i| K::String(format!("owner-{i}"))).collect::<Vec<_>>();
let bytes = fixture(&[1, 0], 0, &constants, None, &[]);
let image = ValidatedProgramImage::load(&bytes, &mut budget(), caps::NONE).unwrap();
for (i, value) in image.constants().iter().enumerate() {
let Value::String(text) = value else { panic!() };
assert_eq!(&**text, format!("owner-{i}"));
let (found, work) = image.lookup_work(text);
assert!(found && work <= 11, "binary work={work}");
assert!(Arc::ptr_eq(text, image.lease_constant(i).unwrap().content()));
assert!(!image.lookup_work(&Arc::from(text.as_ref())).0);
}
let lease = image.lease_constant(1023).unwrap();
drop(image);
assert_eq!(lease.retained_content(), (10, 1));
let domain = lease.domain.lock().unwrap();
assert_eq!(record_identity(&domain.records[0]), Identity::of(lease.content()));
}
#[test]
fn string_heap_sort_precharges_each_comparison_and_swap_without_scratch() {
let owners = (0..16).map(|_| Arc::<str>::from("same")).collect::<Vec<_>>();
let source = owners.iter().rev().map(|v| (Arc::downgrade(v), v.len())).collect::<Vec<_>>();
let mut full = budget();
let mut records = source.clone();
sort_string_records(&mut records, &mut full).unwrap();
assert!(records.windows(2).all(|v| record_identity(&v[0]) < record_identity(&v[1])));
assert_eq!(full.used(Q::TemporaryBytes), 0);
for limit in 0..full.used(Q::Work) {
let mut b = LoadBudget::new(LoadLimits { work: limit, ..Default::default() });
let mut records = source.clone();
let error = sort_string_records(&mut records, &mut b).unwrap_err();
assert_eq!(error.resource, Some(Q::Work));
assert!(b.used(Q::Work) <= limit);
assert_eq!(b.used(Q::TemporaryBytes), 0);
// Failed sort still owns exactly the original Weak identities.
records.sort_unstable_by_key(record_identity);
let mut expected = source.clone();
expected.sort_unstable_by_key(record_identity);
assert!(records.iter().zip(&expected).all(|(a, b)| Weak::ptr_eq(&a.0, &b.0)));
}
}
#[test]
fn string_sort_exhaustion_rolls_back_sealed_load_domains() {
let constants = vec![K::String("same".into()); 2];
let bytes = fixture(&[1, 0], 0, &constants, None, &[]);
let mut full = budget();
ValidatedProgramImage::load(&bytes, &mut full, caps::NONE).unwrap();
// Post-sort sharing costs 2 + 2 + 1 work units. Sorting two records
// costs 9 or 13 units, depending on their addresses. Removing another
// five leaves at most eight: failure is inside sorting for either order.
let mut b =
LoadBudget::new(LoadLimits { work: full.used(Q::Work) - 5 - 5, ..Default::default() });
let error = ValidatedProgramImage::load(&bytes, &mut b, caps::NONE).unwrap_err();
assert!(matches!(error,VmInitError::RegistryLoadFailed(e) if e.resource==Some(Q::Work)));
for resource in DOMAINS {
assert_eq!(b.used(resource), 0, "{resource:?}");
}
}
@@ -21,6 +21,13 @@ const fn entry(
name,
version: 1,
arg_layout: args,
arg_nullable: if id == 0x4000 {
&[false]
} else if rejection {
&[true, false, false]
} else {
&[true]
},
ret_layout: ret,
parameter_names: names,
cost_cycles: cost,
@@ -29,6 +36,8 @@ const fn entry(
} else {
IntrinsicCostModel::Fixed
},
cost_arg_index: None,
cost_per_byte: 0,
attempt_cost_cycles: if rejection { 8 } else { 0 },
max_attempts: if rejection { crate::random::MAX_ATTEMPTS } else { 0 },
deterministic: true,
@@ -485,6 +485,10 @@ fn transfer(
group(&mut working[base..], registry, ty, (instruction.pc as u64) << 32, budget)?;
}
}
Op::RefIdentityHash => {
budget.work(1)?;
working[base] = Fact::Carrier(I64);
}
Op::RefCast | Op::RefToTrait => {
group(
&mut working[base..],
@@ -503,6 +507,7 @@ fn transfer(
| Op::And
| Op::Or
| Op::Not
| Op::RefIdentityEq
| Op::RefIsExact
| Op::RefImplements
| Op::DynCanUnpack => {
@@ -705,6 +710,15 @@ fn check(
}
}
Op::ArrayNew => require(working[end - 1], I64, 0, registry, budget)?,
Op::ArrayCopy => {
let array = TypeId(instruction.imm_u32().unwrap());
let base = end - 5;
check_group(&working[base..base + 1], array, registry, budget)?;
check_group(&working[base + 2..base + 3], array, registry, budget)?;
for i in [1, 3, 4] {
require(working[base + i], I64, i, registry, budget)?;
}
}
Op::StructGet | Op::StructSet | Op::ArrayLen | Op::ArrayGet | Op::ArraySet => {
budget.work(3)?;
let (id, field) = if matches!(instruction.opcode, Op::StructGet | Op::StructSet) {
@@ -760,6 +774,21 @@ fn check(
return Err(mismatch(1));
}
}
Op::RefIdentityHash | Op::RefIdentityEq => {
let (left, right) = if instruction.opcode == Op::RefIdentityHash {
(instruction.imm_u32().unwrap(), None)
} else {
let (a, b) = instruction.imm_u32x2().unwrap();
(a, Some(b))
};
let lw = registry.type_layout(TypeId(left)).unwrap().storage.slots as usize;
let rw = right
.map_or(0, |id| registry.type_layout(TypeId(id)).unwrap().storage.slots as usize);
check_group(&working[end - lw - rw..end - rw], TypeId(left), registry, budget)?;
if let Some(id) = right {
check_group(&working[end - rw..end], TypeId(id), registry, budget)?;
}
}
Op::RefCast | Op::RefToTrait | Op::RefIsExact | Op::RefImplements => {
budget.work(1)?;
let ty = TypeId(instruction.imm_u32x2().unwrap().0);
@@ -772,7 +801,12 @@ fn check(
.unwrap();
let base = end - native.arg_slots();
for (part, abi) in native.arg_layout.iter().enumerate() {
require(working[base + part], abi_mask(*abi), part, registry, budget)?;
let accepted = if native.arg_nullable[part] {
abi_mask(*abi) | NULL
} else {
abi_mask(*abi) & !NULL
};
require(working[base + part], accepted, part, registry, budget)?;
}
}
Op::JmpIfTrue | Op::JmpIfFalse | Op::Not => {
@@ -385,6 +385,10 @@ impl<T> BudgetVec<T> {
let bytes = b.bytes::<T>(capacity, q)?;
let mut extra = b.reserve(q, bytes - self.charge.amount)?;
let temporary = b.reserve(RuntimeResource::TemporaryBytes, self.charge.amount)?;
#[cfg(test)]
if FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.replace(false)) {
return Err(b.failure(q, ResourceReason::AllocationFailed, bytes));
}
let mut candidate = Vec::new();
candidate
.try_reserve_exact(capacity)
@@ -405,6 +409,21 @@ impl<T> BudgetVec<T> {
self.charge.absorb(growth.extra);
}
}
/// Rebuild a table with all fallible capacity/coexistence admission completed
/// before moving any old entry. The relocation callback must not allocate.
pub(crate) fn rebuild_with(
&mut self,
len: usize,
vacant: impl FnMut() -> T,
relocate: impl FnOnce(&mut [T], &mut [T]),
) -> Result<(), ResourceFailure> {
let mut growth = self.prepare_capacity(len.max(self.data.capacity()))?;
growth.candidate.resize_with(len, vacant);
relocate(&mut self.data, &mut growth.candidate);
self.data = growth.candidate;
self.charge.absorb(growth.extra);
Ok(())
}
pub fn reserve(&mut self, additional: usize) -> Result<(), ResourceFailure> {
let growth = self.prepare_growth(additional)?;
self.commit_growth(growth);
@@ -445,9 +464,6 @@ impl<T> BudgetVec<T> {
pub fn clear(&mut self) {
self.data.clear();
}
pub(crate) fn retain(&mut self, keep: impl FnMut(&T) -> bool) {
self.data.retain(keep);
}
pub(crate) fn extend_from_slice(&mut self, values: &[T]) -> Result<(), ResourceFailure>
where
T: Clone,
@@ -509,3 +525,8 @@ pub(crate) struct VecGrowth<T> {
_temporary: Reservation,
old_capacity: usize,
}
#[cfg(test)]
thread_local! {
pub(crate) static FAIL_NEXT_VEC_ALLOCATION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
@@ -87,3 +87,39 @@ mod tests {
assert_eq!(h.max_ns, 99);
}
}
/// Non-owning storage observations for engineering qualification. These values
/// never retain a guest object or a native string owner and cannot write storage.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StorageValue {
Null,
Int32(i32),
Int64(i64),
FloatBits(u64),
Boolean(bool),
Reference(prometeu_bytecode::HeapRef),
StringIdentity { address: usize, bytes: usize },
}
impl StorageValue {
pub(crate) fn observe(value: &prometeu_bytecode::Value) -> Self {
use prometeu_bytecode::Value;
match value {
Value::Null => Self::Null,
Value::Int32(n) => Self::Int32(*n),
Value::Int64(n) => Self::Int64(*n),
Value::Float(n) => Self::FloatBits(n.to_bits()),
Value::Boolean(n) => Self::Boolean(*n),
Value::HeapRef(r) => Self::Reference(*r),
Value::String(s) => Self::StringIdentity {
address: std::sync::Arc::as_ptr(s) as *const () as usize,
bytes: s.len(),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StorageObject {
pub reference: prometeu_bytecode::HeapRef,
pub type_id: prometeu_bytecode::registry::TypeId,
pub groups: Vec<Vec<StorageValue>>,
}
@@ -0,0 +1,123 @@
//! Private allocation identity index. Values retain Weak ownership, never content.
use crate::resource_budget::{
BudgetVec, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
};
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct Identity {
pub(crate) address: usize,
pub(crate) length: usize,
}
impl Identity {
pub(crate) fn of(content: &Arc<str>) -> Self {
Self { address: Arc::as_ptr(content) as *const u8 as usize, length: content.len() }
}
fn hash(self) -> usize {
// Mix aligned addresses and lengths; collisions still compare both fields.
let mut n = (self.address as u64) ^ (self.length as u64).wrapping_mul(0x9e3779b97f4a7c15);
n = (n ^ (n >> 30)).wrapping_mul(0xbf58476d1ce4e5b9);
n = (n ^ (n >> 27)).wrapping_mul(0x94d049bb133111eb);
(n ^ (n >> 31)) as usize
}
}
#[derive(Debug)]
enum Bucket<T> {
Empty,
Tombstone,
Occupied(Identity, T),
}
#[derive(Debug)]
pub(crate) struct IdentityIndex<T> {
buckets: BudgetVec<Bucket<T>>,
live: usize,
tombstones: usize,
}
impl<T> IdentityIndex<T> {
pub(crate) fn new(budget: &RuntimeBudget) -> Self {
Self { buckets: BudgetVec::new(budget, Q::AuxiliaryBytes), live: 0, tombstones: 0 }
}
pub(crate) fn len(&self) -> usize {
self.live
}
pub(crate) fn contains(&self, key: Identity, mut probe: impl FnMut()) -> bool {
let size = self.buckets.len();
if size == 0 {
return false;
}
let mut at = key.hash() & (size - 1);
loop {
probe();
match &self.buckets[at] {
Bucket::Empty => return false,
Bucket::Occupied(found, _) if *found == key => return true,
_ => at = (at + 1) & (size - 1),
}
}
}
pub(crate) fn prepare(&mut self, budget: &RuntimeBudget) -> Result<(), ResourceFailure> {
let size = self.buckets.len();
let next = self.live + 1;
let capacity = if size == 0 {
8
} else if next > size / 2 {
size.checked_mul(2).ok_or_else(|| {
budget.failure(Q::AuxiliaryBytes, ResourceReason::SizeOverflow, u64::MAX)
})?
} else if next + self.tombstones > size / 2 {
size
} else {
return Ok(());
};
self.buckets.rebuild_with(
capacity,
|| Bucket::Empty,
|old, new| {
for slot in old {
if let Bucket::Occupied(key, value) = std::mem::replace(slot, Bucket::Empty) {
Self::place(new, key, value);
}
}
},
)?;
self.tombstones = 0;
Ok(())
}
fn place(buckets: &mut [Bucket<T>], key: Identity, value: T) -> bool {
let mut at = key.hash() & (buckets.len() - 1);
loop {
match &buckets[at] {
Bucket::Empty | Bucket::Tombstone => {
let reused = matches!(buckets[at], Bucket::Tombstone);
buckets[at] = Bucket::Occupied(key, value);
return reused;
}
Bucket::Occupied(found, _) => {
assert_ne!(*found, key, "duplicate allocation identity")
}
}
at = (at + 1) & (buckets.len() - 1);
}
}
pub(crate) fn insert(&mut self, key: Identity, value: T) {
assert!(self.live < self.buckets.len() / 2, "prepare must precede publication");
if Self::place(&mut self.buckets, key, value) {
self.tombstones -= 1;
}
self.live += 1;
}
pub(crate) fn retain(&mut self, mut keep: impl FnMut(&T) -> bool) {
for slot in &mut *self.buckets {
if matches!(slot, Bucket::Occupied(_, value) if !keep(value)) {
// Erase the key before releasing the Weak/reservations. Its
// allocation address cannot be recycled while Weak survives.
let removed = std::mem::replace(slot, Bucket::Tombstone);
self.live -= 1;
self.tombstones += 1;
drop(removed);
}
}
}
}
#[cfg(test)]
mod tests;
@@ -0,0 +1,72 @@
use super::*;
#[test]
fn collision_chains_distinguish_full_identity_and_survive_tombstones() {
let b = RuntimeBudget::default();
let mut table = IdentityIndex::new(&b);
let keys: Vec<_> = (0..1000)
.map(|address| Identity { address, length: 4 })
.filter(|k| k.hash() & 7 == 0)
.take(4)
.collect();
for (i, key) in keys.iter().enumerate() {
table.prepare(&b).unwrap();
table.insert(*key, i);
}
for key in &keys {
assert!(table.contains(*key, || {}));
}
table.retain(|i| *i != 1);
assert!(!table.contains(keys[1], || {}));
assert!(table.contains(keys[3], || {}));
assert!(!table.contains(Identity { length: 5, ..keys[3] }, || {}));
table.prepare(&b).unwrap(); // Rebuild removes tombstones at half occupancy.
table.insert(keys[1], 1);
assert_eq!(table.len(), 4);
assert_eq!(table.tombstones, 0);
}
#[test]
fn tombstone_reuse_keeps_collision_successors_and_length_is_part_of_key() {
let b = RuntimeBudget::default();
let mut t = IdentityIndex::new(&b);
let key = Identity { address: 16, length: 0 };
t.prepare(&b).unwrap();
t.insert(key, 1);
t.prepare(&b).unwrap();
t.insert(Identity { length: 1, ..key }, 2);
t.retain(|v| *v != 1);
assert_eq!(t.tombstones, 1);
t.prepare(&b).unwrap();
t.insert(key, 3);
assert_eq!(t.tombstones, 0);
assert!(t.contains(key, || {}));
assert!(t.contains(Identity { length: 1, ..key }, || {}));
}
#[test]
fn controlled_probe_counts_and_half_load_survive_growth_and_rebuild() {
let b = RuntimeBudget::default();
let mut table = IdentityIndex::new(&b);
for n in 0..1024 {
table.prepare(&b).unwrap();
table.insert(Identity { address: n * 16, length: n % 7 }, n);
assert!(table.live * 2 <= table.buckets.len());
}
let mut max_probes = 0;
for n in 0..2048 {
let mut probes = 0;
assert_eq!(
table.contains(Identity { address: n * 16, length: n % 7 }, || probes += 1),
n < 1024
);
max_probes = max_probes.max(probes);
}
// A reproducible synthetic-key bound for this fixture, not a hash-table
// worst-case claim or a constraint on allocator-selected real addresses.
assert!(max_probes <= 32, "fixture probes={max_probes}");
table.retain(|n| n % 2 == 0);
table.prepare(&b).unwrap();
for n in 0..1024 {
assert_eq!(table.contains(Identity { address: n * 16, length: n % 7 }, || {}), n % 2 == 0);
}
}
+23 -11
View File
@@ -4,6 +4,7 @@
use crate::resource_budget::{
BudgetVec, Reservation, ResourceFailure, ResourceReason, RuntimeBudget, RuntimeResource as Q,
};
use crate::string_identity_index::{Identity, IdentityIndex};
use prometeu_bytecode::Value;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
@@ -17,7 +18,7 @@ struct Entry {
#[derive(Debug)]
struct Domain {
budget: RuntimeBudget,
entries: BudgetVec<Entry>,
entries: IdentityIndex<Entry>,
_state: Reservation,
}
@@ -49,7 +50,7 @@ impl StringLedger {
Q::AuxiliaryBytes,
budget.bytes::<Mutex<Domain>>(1, Q::AuxiliaryBytes)? + 2 * size_of::<usize>() as u64,
)?;
let entries = BudgetVec::new(&budget, Q::AuxiliaryBytes);
let entries = IdentityIndex::new(&budget);
Ok(Self(Arc::new(Mutex::new(Domain { budget, entries, _state: state }))))
}
fn domain(&self) -> MutexGuard<'_, Domain> {
@@ -57,7 +58,7 @@ impl StringLedger {
}
/// Safepoint reconciliation is bounded by the accepted allocation count.
/// Retain compacts in place; unused capacity remains charged until drop.
/// Dead buckets become tombstones; retained capacity stays charged until drop.
pub(crate) fn reconcile(&self) -> usize {
self.domain().reconcile()
}
@@ -65,9 +66,19 @@ impl StringLedger {
self.domain().entries.len()
}
pub(crate) fn retains(&self, content: &Arc<str>) -> bool {
let address = Arc::as_ptr(content);
self.domain().entries.iter().any(|entry| std::ptr::eq(entry.content.as_ptr(), address))
self.lookup(content, || {})
}
fn lookup(&self, content: &Arc<str>, mut probe: impl FnMut()) -> bool {
self.domain().entries.contains(Identity::of(content), &mut probe)
}
#[cfg(any(test, feature = "resource-diagnostics"))]
pub(crate) fn lookup_work(&self, content: &Arc<str>) -> (bool, usize) {
let mut work = 0;
let found = self.lookup(content, || work += 1);
(found, work)
}
pub(crate) fn lease(&self, content: &Arc<str>) -> Option<StringLease> {
self.retains(content)
.then(|| StringLease { content: content.clone(), domain: self.clone() })
@@ -239,23 +250,24 @@ impl Domain {
}
Err(failure) => return Err(failure),
};
if let Err(failure) = self.entries.reserve(1) {
if let Err(failure) = self.entries.prepare(&self.budget) {
if failure.reason != ResourceReason::Limit {
return Err(failure);
}
self.reconcile();
self.entries.reserve(1)?;
self.entries.prepare(&self.budget)?;
}
Ok(charges)
}
fn publish(&mut self, content: &Arc<str>, charges: Charges) {
self.entries
.push(Entry {
self.entries.insert(
Identity::of(content),
Entry {
content: Arc::downgrade(content),
_bytes: charges.bytes,
_allocation: charges.allocation,
})
.expect("prepare reserved one ledger record before materialization");
},
);
}
}
@@ -259,3 +259,95 @@ fn formatting_refuses_result_and_temporary_limits_with_no_content_publication()
assert_eq!(budget.used(Q::StringAllocations), 0);
assert_eq!(ledger.tracked_allocations(), 0);
}
#[test]
fn index_growth_refuses_auxiliary_temporary_and_allocator_before_materialization() {
for failure in [0, 1, 2] {
let (budget, ledger) = fixture(&[]);
let kept = (0..4).map(|_| ledger.import("kept").unwrap()).collect::<Vec<_>>();
let auxiliary = budget.used(Q::AuxiliaryBytes);
match failure {
0 => budget.test_lower_limit(Q::AuxiliaryBytes, auxiliary),
1 => budget.test_lower_limit(Q::TemporaryBytes, 0),
_ => crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true)),
}
let mut called = false;
let result = ledger.build(4, &mut |w| {
called = true;
w.write_str("next")
});
let Err(MaterializeError::Resource(error)) = result else {
panic!("expected prepublication refusal")
};
assert!(!called);
assert_eq!(
error.resource,
if failure == 1 { Q::TemporaryBytes } else { Q::AuxiliaryBytes }
);
assert_eq!(
error.reason,
if failure == 2 { ResourceReason::AllocationFailed } else { ResourceReason::Limit }
);
assert_eq!(ledger.tracked_allocations(), 4);
assert_eq!(budget.used(Q::StringBytes), 16);
assert_eq!(budget.used(Q::StringAllocations), 4);
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
assert_eq!(budget.used(Q::TemporaryBytes), 0);
for value in &kept {
assert!(ledger.retains(value));
}
}
}
#[test]
fn index_initial_allocation_failure_and_rebuild_failure_refund_reservations() {
let (budget, ledger) = fixture(&[]);
let initial = budget.used(Q::AuxiliaryBytes);
crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true));
assert_eq!(ledger.import("x").unwrap_err().reason, ResourceReason::AllocationFailed);
assert_eq!(ledger.tracked_allocations(), 0);
assert_eq!(budget.used(Q::AuxiliaryBytes), initial);
assert_eq!(budget.used(Q::StringBytes), 0);
assert_eq!(budget.used(Q::StringAllocations), 0);
let mut kept = (0..4).map(|_| ledger.import("same").unwrap()).collect::<Vec<_>>();
let old = Arc::downgrade(&kept.pop().unwrap());
assert_eq!(ledger.reconcile(), 1);
let auxiliary = budget.used(Q::AuxiliaryBytes);
budget.test_lower_limit(Q::TemporaryBytes, 0);
assert_eq!(ledger.import("new").unwrap_err().resource, Q::TemporaryBytes);
assert_eq!(ledger.tracked_allocations(), 3);
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
assert_eq!(budget.used(Q::TemporaryBytes), 0);
assert_eq!(budget.used(Q::StringBytes), 12);
for value in &kept {
assert_eq!(Arc::strong_count(value), 1);
assert!(ledger.retains(value));
}
assert!(old.upgrade().is_none());
// Rebuild under a restored test budget; no production ceiling changes.
budget.test_lower_limit(Q::TemporaryBytes, 64 * 1024 * 1024);
let newer = ledger.import("same").unwrap();
assert!(ledger.retains(&newer));
assert!(!ledger.retains(&Arc::from("same")));
assert!(ledger.lookup_work(&newer).0);
}
#[test]
fn tombstone_rebuild_allocator_refusal_preserves_live_keys_and_charges() {
let (budget, ledger) = fixture(&[]);
let mut owners = (0..4).map(|_| ledger.import("same").unwrap()).collect::<Vec<_>>();
owners.pop();
assert_eq!(ledger.reconcile(), 1);
let auxiliary = budget.used(Q::AuxiliaryBytes);
crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true));
assert_eq!(ledger.import("new").unwrap_err().reason, ResourceReason::AllocationFailed);
assert_eq!(ledger.tracked_allocations(), 3);
assert_eq!(budget.used(Q::StringBytes), 12);
assert_eq!(budget.used(Q::StringAllocations), 3);
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
assert_eq!(budget.used(Q::TemporaryBytes), 0);
assert!(owners.iter().all(|v| ledger.retains(v)));
let new = ledger.import("new").unwrap();
assert!(ledger.retains(&new));
assert_eq!(budget.used(Q::AuxiliaryBytes), auxiliary);
}
@@ -221,6 +221,24 @@ pub(crate) fn unpack_dynamic(
// All reference operations validate complete source values before querying the
// underlying concrete object. Invalid handles/pairs never become false or null.
pub(crate) fn identity_reference(
registry: &Registry,
heap: &Heap,
source: TypeId,
values: &[Value],
) -> Result<Option<HeapRef>, ValueError> {
let kind = registry.declarations().types.get(source.0 as usize).ok_or(ValueError::Type)?;
if !kind.kind.is_identity_reference() {
return Err(ValueError::Type);
}
validate(registry, heap, source, values)?;
match values.first() {
Some(Value::Null) => Ok(None),
Some(Value::HeapRef(reference)) => Ok(Some(*reference)),
_ => Err(ValueError::Type),
}
}
fn reference(
registry: &Registry,
heap: &Heap,
@@ -1,12 +1,20 @@
use crate::numeric;
use crate::resource_budget::BudgetVec;
mod array_copy;
#[cfg(test)]
mod array_copy_tests;
mod callable;
#[cfg(test)]
#[path = "../tests/support/collections.rs"]
mod collection_test_support;
mod collections;
#[cfg(test)]
mod collections_tests;
mod control;
mod dynamic;
mod gc;
mod identity;
mod intrinsic;
mod lifecycle;
mod loader;
mod native_arrays;
@@ -327,8 +335,10 @@ impl VirtualMachine {
}
let charged_math = if opcode == OpCode::Intrinsic {
lookup_intrinsic_by_id(instr.imm_u32().unwrap())
.filter(|m| m.owner == "vec2" || m.owner == "mathf" || m.owner.starts_with("math."))
lookup_intrinsic_by_id(instr.imm_u32().unwrap()).filter(|m| {
matches!(m.implementation, crate::IntrinsicImplementation::Host(_))
&& (m.owner == "vec2" || m.owner == "mathf" || m.owner.starts_with("math."))
})
} else {
None
};
@@ -952,6 +962,12 @@ impl VirtualMachine {
)
})?;
if matches!(
intrinsic.implementation,
crate::IntrinsicImplementation::BorrowedInline(_)
) {
return self.execute_inline_intrinsic(intrinsic, pc_at_intrinsic);
}
if matches!(intrinsic.implementation, crate::IntrinsicImplementation::VmOwned) {
return self.execute_random(intrinsic, pc_at_intrinsic);
}
@@ -1083,6 +1099,18 @@ impl VirtualMachine {
let (source, target) = instr.imm_u32x2().unwrap();
return self.execute_reference(opcode, source, target, start_pc as u32);
}
OpCode::RefIdentityHash | OpCode::RefIdentityEq => {
let (left, right) = if opcode == OpCode::RefIdentityHash {
(instr.imm_u32().unwrap(), None)
} else {
let (left, right) = instr.imm_u32x2().unwrap();
(left, Some(right))
};
return self.execute_identity(opcode, left, right, start_pc as u32);
}
OpCode::ArrayCopy => {
return self.execute_array_copy(instr.imm_u32().unwrap(), start_pc as u32);
}
OpCode::StructNew
| OpCode::StructGet
| OpCode::StructSet
@@ -2485,7 +2513,7 @@ mod tests {
let mut vm = VirtualMachine::default();
let mut header = vec![0u8; 32];
header[0..4].copy_from_slice(&prometeu_bytecode::PBX_MAGIC);
header[4..6].copy_from_slice(&0u16.to_le_bytes()); // retired version
header[4..6].copy_from_slice(&1u16.to_le_bytes()); // unsupported version
let res = vm.initialize(header);
assert!(
@@ -2494,11 +2522,11 @@ mod tests {
}
#[test]
fn test_loader_hardening_malformed_pbx_v1() {
fn test_loader_hardening_malformed_pbx_v0() {
let mut vm = VirtualMachine::default();
let mut header = vec![0u8; 32];
header[0..4].copy_from_slice(&prometeu_bytecode::PBX_MAGIC);
header[4..6].copy_from_slice(&1u16.to_le_bytes());
header[4..6].copy_from_slice(&0u16.to_le_bytes());
header[8..12].copy_from_slice(&1u32.to_le_bytes()); // missing directory
let res = vm.initialize(header);
@@ -3890,3 +3918,10 @@ mod tests {
#[cfg(test)]
#[path = "virtual_machine/callable_tests.rs"]
mod callable_tests;
#[cfg(feature = "resource-diagnostics")]
pub mod string_provenance_qualification;
#[cfg(test)]
#[path = "virtual_machine/identity_tests.rs"]
mod identity_tests;
@@ -0,0 +1,110 @@
//! Complete-group snapshot copy: charge, validate, prepare, then commit.
use super::*;
use crate::value_contract::{ValueError, ValueGroup};
use prometeu_bytecode::registry::TypeId;
fn planned_cost(count: u32, width: u64) -> Option<u64> {
u64::from(count).checked_mul(2)?.checked_mul(width)?.checked_add(4)
}
impl VirtualMachine {
pub(super) fn execute_array_copy(
&mut self,
array_id: u32,
pc: u32,
) -> Result<(), LogicalFrameEndingReason> {
let opcode = OpCode::ArrayCopy;
self.cycles += opcode.cycles();
let image = self.validated_image.clone().expect("validated Array TypeId");
let registry = image.registry();
let array_ty = TypeId(array_id);
let element = registry.type_layout(array_ty).unwrap().array.unwrap().element_type_id;
let width = registry.type_layout(element).unwrap().storage.slots;
if self.temporary_slots() < 5 {
return Err(self.trap(
TRAP_STACK_UNDERFLOW,
opcode as u16,
"Incomplete array copy operands".into(),
pc,
));
}
let base = self.operand_stack.len() - 5;
// Inspect count only to select cost, without changing ordinary fault order.
if let Value::Int64(n) = self.operand_stack[base + 4]
&& let Ok(n) = u32::try_from(n)
{
let cost = planned_cost(n, u64::from(width))
.ok_or_else(|| self.collection_size_trap(opcode, pc, u64::MAX))?;
self.cycles += cost - opcode.cycles();
}
let receiver = |offset: usize| {
let r = match self.operand_stack[base + offset] {
Value::HeapRef(r) => r,
Value::Null => return Err(self.value_trap(ValueError::NullReference, opcode, pc)),
_ => return Err(self.value_trap(ValueError::Type, opcode, pc)),
};
if self.heap.concrete_type(r, registry).map_err(|e| self.value_trap(e, opcode, pc))?
!= array_ty
{
return Err(self.value_trap(ValueError::Type, opcode, pc));
}
Ok(r)
};
let source = receiver(0)?;
let target = receiver(2)?;
let integer = |offset: usize| match self.operand_stack[base + offset] {
Value::Int64(n) => Ok(n),
_ => Err(self.value_trap(ValueError::Type, opcode, pc)),
};
let source_start = integer(1)?;
let target_start = integer(3)?;
let count = integer(4)?;
let range = |r, start: i64| {
let len = self
.heap
.typed_array_len(r, registry)
.map_err(|e| self.value_trap(e, opcode, pc))?;
if start < 0 || count < 0 || start > len as i64 || count > len as i64 - start {
return Err(self.value_trap(ValueError::Bounds, opcode, pc));
}
Ok(start as usize)
};
let source_start = range(source, source_start)?;
let target_start = range(target, target_start)?;
let count = count as usize;
// Operands remain roots; every fallible operation precedes shared writes.
let mut prepared =
BudgetVec::<ValueGroup>::new(&self.budget, RuntimeResource::TemporaryBytes);
prepared.reserve(count).map_err(|e| self.resource_trap(e, opcode, pc))?;
for i in 0..count {
let group = self
.heap
.load_typed(registry, source, source_start + i)
.map_err(|e| self.value_trap(e, opcode, pc))?;
prepared.push(group).expect("complete scratch reservation");
}
// Snapshot captured. No allocation, validation, GC, callback or scheduler.
for (i, group) in prepared.iter().enumerate() {
self.heap.commit_typed(target, target_start + i, group.as_slice());
}
self.operand_stack.truncate(base);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::planned_cost;
#[test]
fn checked_cost_covers_the_full_sum_without_wrap_or_saturation() {
assert_eq!(planned_cost(10, 1), Some(24));
assert_eq!(planned_cost(10, 3), Some(64));
assert_eq!(planned_cost(0, u64::MAX), Some(4));
assert_eq!(planned_cost(u32::MAX, 3), Some(25_769_803_774));
// Synthetic widths exercise defensive overflow, not valid guest layouts.
assert_eq!(planned_cost(1, u64::MAX), None);
assert_eq!(planned_cost(1, u64::MAX / 2), None);
assert_eq!(planned_cost(u32::MAX, u64::MAX), None);
}
}
@@ -0,0 +1,487 @@
use super::collection_test_support as support;
use super::*;
use crate::ValidatedProgramImage;
use prometeu_bytecode::{assemble, registry::TypeId};
use support::Native;
fn machine(instruction: &str, count: usize) -> VirtualMachine {
let mut text = String::new();
for i in 0..count {
text += &format!("GET_GLOBAL {i}\n");
}
text += instruction;
text += "\nHALT";
let mut vm = VirtualMachine::from_image(
support::load(&assemble(&text).unwrap()).unwrap(),
RuntimeLimits::default(),
);
vm.prepare_boot_call().unwrap();
vm
}
fn operands(vm: &mut VirtualMachine, values: &[Value]) {
vm.globals.extend_from_slice(values).unwrap();
for _ in values {
vm.step(&mut Native, &mut HostContext::new(None)).unwrap();
}
}
fn step(vm: &mut VirtualMachine) -> Result<(), LogicalFrameEndingReason> {
vm.step(&mut Native, &mut HostContext::new(None))
}
#[test]
fn raw_copy_preserves_absence_and_box_identity_without_heap_allocations() {
let mut vm = machine("ARRAY_COPY 15", 5);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
let a = vm.heap.allocate_typed_array(registry.clone(), TypeId(15), 4).unwrap();
let boxed = vm
.heap
.allocate_dynamic_box(registry.clone(), TypeId(5), &[Value::Float(1.), Value::Float(2.)])
.unwrap();
let present = [Value::Int64(5), Value::HeapRef(boxed)];
vm.heap.store_typed(&registry, a, 0, &present).unwrap();
operands(
&mut vm,
&[Value::HeapRef(a), Value::Int64(0), Value::HeapRef(a), Value::Int64(1), Value::Int64(3)],
);
let before = vm.heap.allocation_count();
vm.budget
.test_lower_limit(RuntimeResource::GuestBytes, vm.budget.used(RuntimeResource::GuestBytes));
step(&mut vm).unwrap();
assert_eq!(vm.heap.allocation_count(), before);
assert_eq!(vm.heap.load_typed(&registry, a, 1).unwrap().as_slice(), present);
for i in [2, 3] {
assert_eq!(
vm.heap.load_typed(&registry, a, i).unwrap().as_slice(),
[Value::Null, Value::Null]
);
}
}
fn pair(ty: u32, len: u32) -> (VirtualMachine, HeapRef, HeapRef) {
let mut vm = machine(&format!("ARRAY_COPY {ty}"), 5);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
let source = vm.heap.allocate_typed_array(registry.clone(), TypeId(ty), len).unwrap();
let target = vm.heap.allocate_typed_array(registry, TypeId(ty), len).unwrap();
(vm, source, target)
}
fn copy_inputs(source: HeapRef, target: HeapRef, count: i64) -> [Value; 5] {
[
Value::HeapRef(source),
Value::Int64(0),
Value::HeapRef(target),
Value::Int64(0),
Value::Int64(count),
]
}
fn refused(vm: &mut VirtualMachine, code: u32, cost: u64) {
let inputs = vm.operand_stack.as_slice().to_vec();
let (pc, cycles, temporary, objects) =
(vm.pc, vm.cycles, vm.budget.used(RuntimeResource::TemporaryBytes), vm.heap.len());
let LogicalFrameEndingReason::Trap(info) = step(vm).unwrap_err() else { panic!() };
assert_eq!((info.code, info.opcode, info.pc), (code, 0xa9, pc as u32));
assert_eq!(vm.cycles - cycles, cost);
assert_eq!(vm.operand_stack.as_slice(), inputs);
assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), temporary);
assert_eq!(vm.heap.len(), objects);
}
#[test]
fn calculable_work_is_precharged_on_receivers_carriers_and_ranges_in_fault_order() {
for case in 0..20 {
let (mut vm, source, target) = pair(11, 10);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
let wrong = vm.heap.allocate_typed_struct(registry.clone(), TypeId(19)).unwrap();
let foreign = support::load(&assemble("HALT").unwrap()).unwrap();
let foreign_ref =
vm.heap.allocate_typed_array(foreign.registry().clone(), TypeId(11), 10).unwrap();
let stale = HeapRef { index: source.index, generation: source.generation + 1 };
let mut inputs = copy_inputs(source, target, 10);
let (offset, value, code) = match case {
0 => (0, Value::Null, TRAP_NULL_REFERENCE),
1 => (2, Value::Null, TRAP_NULL_REFERENCE),
2 => (0, Value::HeapRef(stale), TRAP_INVALID_HANDLE),
3 => (2, Value::HeapRef(stale), TRAP_INVALID_HANDLE),
4 => (0, Value::HeapRef(wrong), TRAP_TYPE),
5 => (2, Value::HeapRef(wrong), TRAP_TYPE),
6 => (0, Value::HeapRef(foreign_ref), TRAP_TYPE),
7 => (2, Value::Int32(0), TRAP_TYPE),
8 => (1, Value::Int32(0), TRAP_TYPE),
9 => (3, Value::Null, TRAP_TYPE),
10 => (1, Value::Int64(-1), TRAP_OOB),
11 => (3, Value::Int64(-1), TRAP_OOB),
12 => (1, Value::Int64(1), TRAP_OOB),
13 => (3, Value::Int64(1), TRAP_OOB),
14 => {
inputs[1] = Value::Int32(0); // Receiver fault precedes offset carrier.
(2, Value::Null, TRAP_NULL_REFERENCE)
}
15 => {
inputs[1] = Value::Int64(-1); // All carriers precede source range.
(3, Value::Int32(0), TRAP_TYPE)
}
16 => (0, Value::Int32(0), TRAP_TYPE),
17 => {
let wrong_array =
vm.heap.allocate_typed_array(registry.clone(), TypeId(22), 10).unwrap();
(2, Value::HeapRef(wrong_array), TRAP_TYPE)
}
18 => (1, Value::Int64(i64::MAX), TRAP_OOB),
_ => (3, Value::Int64(i64::MAX), TRAP_OOB),
};
inputs[offset] = value;
operands(&mut vm, &inputs);
// Quota refusal must not mask earlier faults, even with full cost selected.
vm.budget.test_lower_limit(
RuntimeResource::TemporaryBytes,
vm.budget.used(RuntimeResource::TemporaryBytes),
);
refused(&mut vm, code, 24);
for i in 0..10 {
assert_eq!(
vm.heap.load_typed(&registry, target, i).unwrap().as_slice(),
[Value::Int32(0)]
);
}
}
}
#[test]
fn invalid_count_selects_only_base_without_reordering_ordinary_faults() {
for count in [
Value::Int32(10),
Value::Null,
Value::Int64(-1),
Value::Int64(u32::MAX as i64 + 1),
Value::Int64(i64::MAX),
] {
for bad_receiver in [false, true] {
let (mut vm, source, target) = pair(11, 10);
let mut inputs = copy_inputs(source, target, 0);
inputs[4] = count.clone();
if bad_receiver {
inputs[0] = Value::Null;
}
operands(&mut vm, &inputs);
let code = if bad_receiver {
TRAP_NULL_REFERENCE
} else if matches!(count, Value::Int64(_)) {
TRAP_OOB
} else {
TRAP_TYPE
};
refused(&mut vm, code, 4);
}
}
}
#[test]
fn incomplete_temporary_operands_never_read_frame_locals_for_cost() {
let code = assemble(
"GET_GLOBAL 0\nGET_GLOBAL 1\nGET_GLOBAL 2\nGET_GLOBAL 3\nGET_GLOBAL 4\nARRAY_COPY 11\nHALT",
)
.unwrap();
for available in 0..5 {
let mut declarations = support::declarations(code.len());
declarations.functions[0].local_slots = 5;
let image = ValidatedProgramImage::load(
&support::bytes_with(&code, declarations),
&mut prometeu_bytecode::load_limits::LoadBudget::new(Default::default()),
prometeu_hal::syscalls::caps::NONE,
)
.unwrap();
let mut vm = VirtualMachine::from_image(image, RuntimeLimits::default());
vm.prepare_boot_call().unwrap();
// Internal defense: skip the verified producers, leaving only locals and
// an incomplete temporary group. This is not an admitted bad program.
vm.operand_stack[4] = Value::Int64(10);
vm.operand_stack.extend_from_slice(&vec![Value::Int64(10); available]).unwrap();
vm.pc = 30;
refused(&mut vm, TRAP_STACK_UNDERFLOW, 4);
}
}
#[test]
fn overlap_snapshots_complete_multislot_groups_and_has_no_identity_discount() {
for (source_start, target_start, count) in [(0, 1, 3), (1, 0, 3), (0, 0, 4)] {
for refuse in [false, true] {
let mut vm = machine("ARRAY_COPY 12", 5);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
let array = vm.heap.allocate_typed_array(registry.clone(), TypeId(12), 4).unwrap();
for i in 0..4 {
vm.heap
.store_typed(
&registry,
array,
i,
&[Value::Float(i as f64), Value::Float(i as f64 + 10.)],
)
.unwrap();
}
let before: Vec<_> = (0..4)
.map(|i| vm.heap.load_typed(&registry, array, i).unwrap().as_slice().to_vec())
.collect();
operands(
&mut vm,
&[
Value::HeapRef(array),
Value::Int64(source_start),
Value::HeapRef(array),
Value::Int64(target_start),
Value::Int64(count),
],
);
let temporary = vm.budget.used(RuntimeResource::TemporaryBytes);
if refuse {
vm.budget.test_lower_limit(RuntimeResource::TemporaryBytes, temporary);
refused(&mut vm, TRAP_RESOURCE_LIMIT, if count == 3 { 16 } else { 20 });
} else {
let cycles = vm.cycles;
step(&mut vm).unwrap();
assert_eq!(vm.cycles - cycles, if count == 3 { 16 } else { 20 });
assert!(vm.operand_stack.is_empty());
}
for i in 0..4 {
let expected =
if !refuse && i >= target_start as usize && i < (target_start + count) as usize
{
&before[(i as i64 - target_start + source_start) as usize]
} else {
&before[i]
};
assert_eq!(vm.heap.load_typed(&registry, array, i).unwrap().as_slice(), expected);
}
assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), temporary);
}
}
}
#[test]
fn scratch_quota_and_allocator_refusals_preserve_all_destinations_and_work() {
for allocation_failure in [false, true] {
let (mut vm, source, target) = pair(11, 10);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
for i in 0..10 {
vm.heap.store_typed(&registry, source, i, &[Value::Int32(i as i32 + 1)]).unwrap();
}
operands(&mut vm, &copy_inputs(source, target, 10));
if allocation_failure {
crate::resource_budget::FAIL_NEXT_VEC_ALLOCATION.with(|fail| fail.set(true));
} else {
vm.budget.test_lower_limit(
RuntimeResource::TemporaryBytes,
vm.budget.used(RuntimeResource::TemporaryBytes),
);
}
let (cycles, temporary) = (vm.cycles, vm.budget.used(RuntimeResource::TemporaryBytes));
let LogicalFrameEndingReason::Trap(info) = step(&mut vm).unwrap_err() else { panic!() };
assert_eq!((info.code, info.opcode, info.pc), (TRAP_RESOURCE_LIMIT, 0xa9, 30));
let resource = info.resource.unwrap();
assert_eq!(resource.resource, RuntimeResource::TemporaryBytes);
assert_eq!(
resource.reason,
if allocation_failure {
crate::resource_budget::ResourceReason::AllocationFailed
} else {
crate::resource_budget::ResourceReason::Limit
}
);
assert_eq!(vm.cycles - cycles, 24);
assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), temporary);
assert_eq!(vm.operand_stack.as_slice(), copy_inputs(source, target, 10));
for i in 0..10 {
assert_eq!(
vm.heap.load_typed(&registry, target, i).unwrap().as_slice(),
[Value::Int32(0)]
);
}
}
}
#[test]
fn late_invalid_source_group_releases_partial_preparation_without_any_write() {
let (mut vm, source, target) = pair(17, 10);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
let node = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap();
vm.heap.store_typed(&registry, source, 0, &[Value::HeapRef(node)]).unwrap();
// Internal corruption of the last stored handle exercises a late read fault.
vm.heap.commit_typed(
source,
9,
&[Value::HeapRef(HeapRef { index: node.index, generation: node.generation + 1 })],
);
operands(&mut vm, &copy_inputs(source, target, 10));
refused(&mut vm, TRAP_INVALID_HANDLE, 24);
for i in 0..10 {
assert_eq!(vm.heap.load_typed(&registry, target, i).unwrap().as_slice(), [Value::Null]);
}
}
#[test]
fn empty_copy_validates_both_receivers_and_strict_ranges_without_scratch() {
for case in 0..5 {
let (mut vm, source, target) = pair(11, 2);
let mut inputs = copy_inputs(source, target, 0);
inputs[1] = Value::Int64(2);
inputs[3] = Value::Int64(2);
let code = match case {
0 => None,
1 => {
inputs[0] = Value::Null;
Some(TRAP_NULL_REFERENCE)
}
2 => {
inputs[2] = Value::Null;
Some(TRAP_NULL_REFERENCE)
}
3 => {
inputs[1] = Value::Int64(3);
Some(TRAP_OOB)
}
_ => {
inputs[3] = Value::Int64(-1);
Some(TRAP_OOB)
}
};
operands(&mut vm, &inputs);
vm.budget.test_lower_limit(
RuntimeResource::TemporaryBytes,
vm.budget.used(RuntimeResource::TemporaryBytes),
);
if let Some(code) = code {
refused(&mut vm, code, 4);
} else {
let cycles = vm.cycles;
step(&mut vm).unwrap();
assert_eq!(vm.cycles - cycles, 4);
assert!(vm.operand_stack.is_empty());
}
}
}
#[test]
fn zero_budget_and_positive_overshoot_preserve_trap_priority_and_step_count() {
for fail in [false, true] {
let (mut vm, source, target) = pair(11, 10);
operands(&mut vm, &copy_inputs(source, target, 10));
if fail {
vm.budget.test_lower_limit(
RuntimeResource::TemporaryBytes,
vm.budget.used(RuntimeResource::TemporaryBytes),
);
}
let (pc, cycles, inputs) = (vm.pc, vm.cycles, vm.operand_stack.as_slice().to_vec());
let report = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((report.cycles_used, report.steps_executed), (0, 0));
assert_eq!((vm.pc, vm.cycles), (pc, cycles));
assert_eq!(vm.operand_stack.as_slice(), inputs);
let report = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((report.cycles_used, report.steps_executed), (24, if fail { 0 } else { 1 }));
if fail {
let LogicalFrameEndingReason::Trap(info) = report.reason else { panic!() };
assert_eq!((info.code, info.opcode, info.pc), (TRAP_RESOURCE_LIMIT, 0xa9, pc as u32));
assert_eq!(vm.operand_stack.as_slice(), inputs);
} else {
assert_eq!(report.reason, LogicalFrameEndingReason::BudgetExhausted);
}
}
}
#[test]
fn copied_groups_preserve_owners_and_references_through_collection_without_guest_growth() {
for ty in [11, 12, 13, 14, 15, 16, 17] {
let (mut vm, source, target) = pair(ty, 1);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
let node = vm.heap.allocate_typed_struct(registry.clone(), TypeId(9)).unwrap();
vm.push(Value::String(std::sync::Arc::from("copy owner"))).unwrap();
let string = vm.pop().unwrap();
let group = match ty {
11 => vec![Value::Int32(7)],
12 => vec![Value::Float(1.), Value::Float(2.)],
13 => vec![Value::Int64(1), Value::Int64(2), Value::Int64(3)],
14 => vec![string.clone()],
15 => vec![Value::Int64(4), string.clone()],
16 => vec![Value::HeapRef(node), Value::Int64(0)],
_ => vec![Value::HeapRef(node)],
};
vm.heap.store_typed(&registry, source, 0, &group).unwrap();
operands(&mut vm, &copy_inputs(source, target, 1));
let objects = vm.heap.allocation_count();
let guest = vm.budget.used(RuntimeResource::GuestBytes);
for q in [
RuntimeResource::GuestBytes,
RuntimeResource::HandleSlots,
RuntimeResource::AuxiliaryBytes,
] {
vm.budget.test_lower_limit(q, vm.budget.used(q));
}
let cycles = vm.cycles;
step(&mut vm).unwrap();
assert_eq!(
vm.cycles - cycles,
match ty {
12 | 15 | 16 => 8,
13 => 10,
_ => 6,
}
);
assert_eq!(vm.heap.allocation_count(), objects);
assert_eq!(vm.budget.used(RuntimeResource::GuestBytes), guest);
assert_eq!(vm.heap.load_typed(&registry, target, 0).unwrap().as_slice(), group);
drop(group);
drop(string);
vm.globals.clear();
vm.globals.push(Value::HeapRef(target)).unwrap();
vm.gc_alloc_threshold = 1;
vm.handle_safepoint().unwrap();
assert!(!vm.heap.is_valid(source));
assert!(vm.heap.is_valid(target));
if [16, 17].contains(&ty) {
assert!(vm.heap.is_valid(node));
}
if [14, 15].contains(&ty) {
assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 1);
}
// Remove all roots and collect using the actual session root traversal.
vm.globals.clear();
vm.last_gc_live_count = 0;
vm.handle_safepoint().unwrap();
assert_eq!(vm.heap.len(), 0);
assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 0);
}
}
#[test]
fn refused_copy_keeps_source_and_previous_target_native_owners_rooted() {
let (mut vm, source, target) = pair(14, 1);
let registry = vm.validated_image.as_ref().unwrap().registry().clone();
for (array, text) in [(source, "candidate owner"), (target, "previous owner")] {
vm.push(Value::String(std::sync::Arc::from(text))).unwrap();
let value = vm.pop().unwrap();
vm.heap.store_typed(&registry, array, 0, &[value]).unwrap();
}
operands(&mut vm, &copy_inputs(source, target, 1));
let temporary = vm.budget.used(RuntimeResource::TemporaryBytes);
vm.budget.test_lower_limit(RuntimeResource::TemporaryBytes, temporary);
refused(&mut vm, TRAP_RESOURCE_LIMIT, 6);
assert_eq!(
vm.heap.load_typed(&registry, source, 0).unwrap().as_slice(),
[Value::String(std::sync::Arc::from("candidate owner"))]
);
assert_eq!(
vm.heap.load_typed(&registry, target, 0).unwrap().as_slice(),
[Value::String(std::sync::Arc::from("previous owner"))]
);
vm.budget.test_lower_limit(
RuntimeResource::TemporaryBytes,
RuntimeLimits::default().get(RuntimeResource::TemporaryBytes),
);
vm.globals.clear(); // Failed operands are now the only session roots.
vm.gc_alloc_threshold = 1;
vm.handle_safepoint().unwrap();
assert!(vm.heap.is_valid(source) && vm.heap.is_valid(target));
assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 2);
vm.operand_stack.clear();
vm.last_gc_live_count = 0;
vm.handle_safepoint().unwrap();
assert_eq!(vm.heap.len(), 0);
assert_eq!(vm.budget.used(RuntimeResource::StringAllocations), 0);
}
@@ -331,3 +331,45 @@ fn recursive_fn_calls_use_bounded_ordinary_frames() {
);
assert_eq!(v.call_stack.len(), 4);
}
#[test]
fn root_return_scratch_refusal_keeps_transferred_roots_without_partial_sweep() {
let mut v = vm(&[
("MAKE_CLOSURE fn=1,type=4\nPOP\nMAKE_CLOSURE fn=1,type=4\nRET", sig(&[], &[4]), vec![]),
("PUSH_I32 55\nRET", sig(&[], &[0]), vec![]),
]);
v.gc_alloc_threshold = 1;
for _ in 0..3 {
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
}
let Value::HeapRef(owner) = *v.operand_stack.last().unwrap() else { panic!() };
let live = v.heap.len();
v.budget.test_lower_limit(RuntimeResource::TemporaryBytes, 0);
assert!(matches!(v.step(&mut Native, &mut HostContext::new(None)),
Err(LogicalFrameEndingReason::Trap(t)) if t.code == TRAP_RESOURCE_LIMIT));
assert!(v.call_stack.is_empty());
assert_eq!(v.operand_stack.as_slice(), &[Value::HeapRef(owner)]);
assert!(v.heap.is_valid(owner));
assert_eq!(v.heap.len(), live);
v.budget.test_lower_limit(RuntimeResource::TemporaryBytes, 1024 * 1024);
v.handle_safepoint().unwrap();
assert_eq!(v.heap.len(), 1);
assert!(v.heap.is_valid(owner));
}
#[test]
fn bad_return_height_preserves_frame_and_owners() {
let mut v = vm(&[
("MAKE_CLOSURE fn=1,type=4\nRET", sig(&[], &[4]), vec![]),
("PUSH_I32 55\nRET", sig(&[], &[0]), vec![]),
]);
v.step(&mut Native, &mut HostContext::new(None)).unwrap();
v.operand_stack.push(Value::Int32(9)).unwrap();
let before = v.operand_stack.as_slice().to_vec();
assert!(matches!(v.step(&mut Native, &mut HostContext::new(None)),
Err(LogicalFrameEndingReason::Trap(t)) if t.code == prometeu_bytecode::TRAP_BAD_RET_SLOTS));
assert_eq!(v.call_stack.len(), 1);
assert_eq!(v.operand_stack.as_slice(), before);
let Value::HeapRef(owner) = before[before.len() - 2] else { panic!() };
assert!(v.heap.is_valid(owner));
}
@@ -175,7 +175,7 @@ impl VirtualMachine {
.expect("result capacity reserved before publication");
Ok(())
}
fn collection_size_trap(
pub(super) fn collection_size_trap(
&self,
opcode: OpCode,
pc: u32,
@@ -1,6 +1,5 @@
use super::collection_test_support as support;
use super::*;
#[path = "../../tests/support/collections.rs"]
mod support;
use crate::value_contract::ValueError;
use prometeu_bytecode::assemble;
use support::Native;
@@ -46,21 +46,13 @@ impl VirtualMachine {
)
.map_err(|(_, error)| self.value_trap(error, opcode, pc))?;
// Copy return values (preserving order: pop return_slots values, then reverse to push back)
let mut return_vals = BudgetVec::new(&self.budget, RuntimeResource::TemporaryBytes);
return_vals.reserve(return_slots).map_err(|e| self.resource_trap(e, opcode, pc))?;
for _ in 0..return_slots {
return_vals
.push(self.pop().map_err(LogicalFrameEndingReason::Panic)?)
.map_err(|e| self.resource_trap(e, opcode, pc))?;
// Validation is complete. Move the ordered result suffix ahead of this
// frame's parameters/locals without cloning owners or reserving scratch.
if return_slots != 0 {
self.operand_stack[frame.stack_base..].rotate_right(return_slots);
}
return_vals.reverse();
self.operand_stack.truncate(frame.stack_base + return_slots);
self.call_stack.pop();
self.operand_stack.truncate(frame.stack_base);
for val in return_vals.drain(..) {
self.push_trap(val, opcode, pc)?;
}
self.pc = frame.return_pc as usize;
if self.call_stack.is_empty() {
self.handle_safepoint().map_err(|e| self.resource_trap(e, opcode, pc))?;
@@ -0,0 +1,59 @@
use super::*;
use crate::value_contract;
use prometeu_bytecode::registry::TypeId;
impl VirtualMachine {
pub(super) fn execute_identity(
&mut self,
opcode: OpCode,
left: u32,
right: Option<u32>,
pc: u32,
) -> Result<(), LogicalFrameEndingReason> {
let image = self.validated_image.clone().ok_or_else(|| {
self.trap(
prometeu_bytecode::TRAP_ILLEGAL_INSTRUCTION,
opcode as u16,
"Identity instruction requires a validated Registry".into(),
pc,
)
})?;
let registry = image.registry();
let left = TypeId(left);
let right = right.map(TypeId);
let lw = registry.type_layout(left).unwrap().storage.slots as usize;
let rw = right.map_or(0, |ty| registry.type_layout(ty).unwrap().storage.slots as usize);
self.cycles += opcode.cycles() + (lw + rw) as u64;
if self.temporary_slots() < lw + rw {
return Err(self.trap(
TRAP_STACK_UNDERFLOW,
opcode as u16,
"Incomplete identity operands".into(),
pc,
));
}
let base = self.operand_stack.len() - lw - rw;
let first = value_contract::identity_reference(
registry,
&self.heap,
left,
&self.operand_stack[base..base + lw],
)
.map_err(|e| self.value_trap(e, opcode, pc))?;
let result = if let Some(ty) = right {
let second = value_contract::identity_reference(
registry,
&self.heap,
ty,
&self.operand_stack[base + lw..],
)
.map_err(|e| self.value_trap(e, opcode, pc))?;
Value::Boolean(first == second)
} else {
Value::Int64(crate::hashing::reference(first))
};
self.operand_stack.truncate(base);
self.operand_stack.push(result).expect("identity result replaces existing inputs");
Ok(())
}
}
@@ -0,0 +1,236 @@
use super::*;
#[path = "../../tests/support/identity.rs"]
mod support;
use crate::{
IntrinsicExecutionError, IntrinsicImplementation, IntrinsicMeta, IntrinsicResults,
hash_intrinsics::HASH_INTRINSICS,
};
use prometeu_bytecode::{HeapRef, RuntimeResource as Q, registry::TypeId};
use std::sync::Arc;
use support::*;
fn empty() -> VirtualMachine {
vm(&[("RET", sig(&[], &[]))], &[])
}
fn push(v: &mut VirtualMachine, values: &[Value]) {
for value in values {
v.operand_stack.push(value.clone()).unwrap();
}
}
fn usage(v: &VirtualMachine) -> Vec<u64> {
[
Q::GuestBytes,
Q::HandleSlots,
Q::AuxiliaryBytes,
Q::TemporaryBytes,
Q::StringBytes,
Q::StringAllocations,
]
.map(|q| v.budget.used(q))
.to_vec()
}
fn fault(result: Result<(), LogicalFrameEndingReason>, expected: u32) {
assert!(
matches!(result,Err(LogicalFrameEndingReason::Trap(ref t)) if t.code==expected),
"{result:?}"
);
}
fn hash(v: &mut VirtualMachine, ty: u32, values: &[Value]) -> i64 {
push(v, values);
v.execute_identity(OpCode::RefIdentityHash, ty, None, 0).unwrap();
let Value::Int64(value) = v.operand_stack.pop().unwrap() else { panic!() };
value
}
#[test]
fn identity_faults_preserve_complete_groups_and_charge_once_in_left_to_right_order() {
let mut v = empty();
let registry = v.validated_image.as_ref().unwrap().registry().clone();
let live = v.heap.allocate_typed_struct(registry.clone(), TypeId(5)).unwrap();
let wrong = v.heap.allocate_typed_struct(registry, TypeId(11)).unwrap();
let stale = HeapRef { index: 999999, generation: 1 };
for (left, right, values, code) in [
(5, None, vec![Value::HeapRef(stale)], TRAP_INVALID_HANDLE),
(5, None, vec![Value::HeapRef(wrong)], TRAP_TYPE),
(5, None, vec![Value::HeapRef(HeapRef::NULL)], TRAP_TYPE),
(6, None, vec![Value::HeapRef(live)], TRAP_TYPE),
(9, None, vec![Value::HeapRef(HeapRef::NULL)], TRAP_TYPE),
(10, None, vec![Value::HeapRef(HeapRef::NULL)], TRAP_INVALID_HANDLE),
(7, None, vec![Value::Null, Value::Int64(0)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Null], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Int64(1)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Int64(-1)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(live), Value::Int64(999)], TRAP_TYPE),
(7, None, vec![Value::HeapRef(stale), Value::Int64(0)], TRAP_INVALID_HANDLE),
// A null left side still validates the complete right group.
(5, Some(7), vec![Value::Null, Value::HeapRef(live), Value::Int64(1)], TRAP_TYPE),
(5, Some(11), vec![Value::Null, Value::HeapRef(stale)], TRAP_INVALID_HANDLE),
// Both invalid: left TYPE takes precedence over right INVALID_HANDLE.
(5, Some(5), vec![Value::HeapRef(wrong), Value::HeapRef(stale)], TRAP_TYPE),
] {
v.operand_stack.truncate(8); // Entry's eight reserved local slots.
push(&mut v, &values);
let operands = v.operand_stack.as_slice().to_vec();
let before = usage(&v);
let cycles = v.cycles;
let op = if right.is_some() { OpCode::RefIdentityEq } else { OpCode::RefIdentityHash };
let width = if left == 7 { 2 } else { 1 } + right.map_or(0, |r| if r == 7 { 2 } else { 1 });
fault(v.execute_identity(op, left, right, 0), code);
assert_eq!(v.operand_stack.as_slice(), operands);
assert_eq!(usage(&v), before);
assert_eq!(v.cycles - cycles, op.cycles() + width);
}
v.operand_stack.truncate(8);
let before = v.cycles;
fault(v.execute_identity(OpCode::RefIdentityEq, 7, Some(7), 0), TRAP_STACK_UNDERFLOW);
assert_eq!(v.cycles - before, 8);
}
#[test]
fn gc_preserves_live_aliases_but_hashes_do_not_root_collected_handles() {
let mut v = empty();
let registry = v.validated_image.as_ref().unwrap().registry().clone();
let live = v.heap.allocate_typed_struct(registry.clone(), TypeId(5)).unwrap();
let dead = v.heap.allocate_typed_struct(registry.clone(), TypeId(5)).unwrap();
let live_hash = hash(&mut v, 5, &[Value::HeapRef(live)]);
let dead_hash = hash(&mut v, 5, &[Value::HeapRef(dead)]);
v.globals.push(Value::HeapRef(live)).unwrap();
v.globals.push(Value::Int64(dead_hash)).unwrap();
v.gc_alloc_threshold = 1;
v.handle_safepoint().unwrap();
assert!(v.heap.is_valid(live));
assert!(!v.heap.is_valid(dead));
assert_eq!(hash(&mut v, 7, &[Value::HeapRef(live), Value::Int64(0)]), live_hash);
let replacement = v.heap.allocate_typed_struct(registry, TypeId(5)).unwrap();
assert_eq!(replacement.index, dead.index);
assert_ne!(replacement.generation, dead.generation);
push(&mut v, &[Value::HeapRef(dead), Value::HeapRef(replacement)]);
fault(v.execute_identity(OpCode::RefIdentityEq, 5, Some(5), 0), TRAP_INVALID_HANDLE);
}
#[test]
fn new_queries_ignore_exhausted_guest_and_scratch_quotas_and_do_not_collect() {
let mut v = empty();
let registry = v.validated_image.as_ref().unwrap().registry().clone();
let live = v.heap.allocate_typed_struct(registry, TypeId(5)).unwrap();
push(&mut v, &[Value::HeapRef(live)]);
for q in [
Q::GuestBytes,
Q::HandleSlots,
Q::AuxiliaryBytes,
Q::TemporaryBytes,
Q::StringBytes,
Q::StringAllocations,
] {
v.budget.test_lower_limit(q, v.budget.used(q));
}
let before = usage(&v);
let allocations = v.heap.allocation_count();
v.gc_alloc_threshold = 1;
v.execute_identity(OpCode::RefIdentityHash, 5, None, 0).unwrap();
v.execute_inline_intrinsic(&HASH_INTRINSICS[0], 0).unwrap();
assert_eq!(v.heap.allocation_count(), allocations);
assert!(v.heap.is_valid(live)); // No root remains, but neither operation is a safepoint.
assert_eq!(usage(&v), before);
}
#[test]
fn inline_faults_preserve_float_bits_owners_and_attempted_cost() {
let mut v = empty();
for bits in [f64::INFINITY.to_bits(), f64::NEG_INFINITY.to_bits(), 0x7ff8000000001234] {
push(&mut v, &[Value::Float(f64::from_bits(bits))]);
let before = usage(&v);
let cycles = v.cycles;
fault(
v.execute_inline_intrinsic(&HASH_INTRINSICS[1], 0),
prometeu_bytecode::TRAP_NON_FINITE,
);
assert!(matches!(v.operand_stack.last(),Some(Value::Float(f)) if f.to_bits()==bits));
assert_eq!(usage(&v), before);
assert_eq!(v.cycles - cycles, 8);
v.operand_stack.pop();
}
let foreign: Arc<str> = Arc::from("owner outside image and session");
push(&mut v, &[Value::String(foreign.clone())]);
let refs = Arc::strong_count(&foreign);
let before = usage(&v);
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0), TRAP_TYPE);
assert_eq!(Arc::strong_count(&foreign), refs);
assert_eq!(usage(&v), before);
assert_eq!(v.cycles - cycles, 8 + foreign.len() as u64);
let mut overflow = HASH_INTRINSICS[2];
overflow.cost_per_byte = u64::MAX;
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&overflow, 0), TRAP_RESOURCE_LIMIT);
assert_eq!(v.cycles - cycles, 8);
assert_eq!(Arc::strong_count(&foreign), refs);
v.operand_stack.pop();
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0), TRAP_STACK_UNDERFLOW);
assert_eq!(v.cycles - cycles, 8);
push(&mut v, &[Value::Int64(7)]);
let cycles = v.cycles;
fault(v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0), TRAP_TYPE);
assert_eq!(v.cycles - cycles, 8);
assert_eq!(v.operand_stack.last(), Some(&Value::Int64(7)));
}
#[test]
fn equal_string_content_hashes_across_image_session_and_import_owners() {
use prometeu_bytecode::model::ConstantPoolEntry as K;
let mut v = vm(
&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))],
&[K::String("same\0π".into())],
);
step(&mut v).unwrap();
let Value::String(image_owner) = v.operand_stack.last().unwrap().clone() else { panic!() };
v.strings = Some(crate::string_ledger::StringLedger::new(v.budget.clone()).unwrap());
let session = v.strings.as_ref().unwrap().concat("same", "\0π").unwrap();
let imported = v.strings.as_ref().unwrap().import("same\0π").unwrap();
assert!(!Arc::ptr_eq(&session, &imported));
let mut hashes = vec![];
v.operand_stack.pop();
for owner in [&image_owner, &session, &imported] {
push(&mut v, &[Value::String(owner.clone())]);
let before = usage(&v);
let count = Arc::strong_count(owner);
v.execute_inline_intrinsic(&HASH_INTRINSICS[2], 0).unwrap();
assert_eq!(usage(&v), before);
assert_eq!(Arc::strong_count(owner), count - 1);
hashes.push(v.operand_stack.pop().unwrap());
}
assert!(hashes.windows(2).all(|pair| pair[0] == pair[1]));
}
fn missing(_: &[Value], _: &mut IntrinsicResults) -> Result<(), IntrinsicExecutionError> {
Ok(())
}
fn wrong(_: &[Value], out: &mut IntrinsicResults) -> Result<(), IntrinsicExecutionError> {
out.push(Value::Boolean(true))
}
fn excess(_: &[Value], out: &mut IntrinsicResults) -> Result<(), IntrinsicExecutionError> {
out.push(Value::Int64(1))?;
let _ = out.push(Value::Int64(2)); // Ignoring the error cannot publish a partial result.
Ok(())
}
#[test]
fn shared_invocation_rejects_bad_outputs_without_consuming_original_string_owner() {
let mut v = empty();
let Value::String(content) = v.import_string_value(&Value::String(Arc::from("kept"))).unwrap()
else {
panic!()
};
push(&mut v, &[Value::String(content.clone())]);
let count = Arc::strong_count(&content);
let before = usage(&v);
for handler in [missing, wrong, excess] {
let mut meta: IntrinsicMeta = HASH_INTRINSICS[2];
meta.owner = "test"; // The shared executor is independent of the hash namespace.
meta.implementation = IntrinsicImplementation::BorrowedInline(handler);
fault(v.execute_inline_intrinsic(&meta, 0), TRAP_INVALID_INTRINSIC);
assert_eq!(Arc::strong_count(&content), count);
assert_eq!(usage(&v), before);
assert!(matches!(v.operand_stack.last(),Some(Value::String(s)) if Arc::ptr_eq(s,&content)));
}
}
@@ -0,0 +1,113 @@
//! Shared borrowed invocation; algorithms receive no host/VM context.
use super::*;
use crate::{
AbiType, IntrinsicExecutionError as E, IntrinsicImplementation as I, IntrinsicMeta,
IntrinsicResults,
};
impl VirtualMachine {
fn inline_values(
&self,
values: &[Value],
layout: &[AbiType],
) -> Result<(), crate::value_contract::ValueError> {
use crate::value_contract::ValueError;
for (value, abi) in values.iter().zip(layout) {
match value {
Value::String(content) if self.validated_image.is_some() => {
if !self.validated_image.as_ref().unwrap().retains_string(content)
&& !self.strings.as_ref().is_some_and(|domain| domain.retains(content))
{
return Err(ValueError::Type);
}
}
Value::HeapRef(reference) if matches!(abi, AbiType::Builtin(key) if key.name == "Random") =>
{
self.heap.random_state(*reference)?;
}
_ => {}
}
}
Ok(())
}
pub(super) fn execute_inline_intrinsic(
&mut self,
meta: &IntrinsicMeta,
pc: u32,
) -> Result<(), LogicalFrameEndingReason> {
let op = OpCode::Intrinsic;
let consumed = meta.arg_slots();
let enough = self.temporary_slots() >= consumed;
let args =
if enough { &self.operand_stack[self.operand_stack.len() - consumed..] } else { &[] };
let Some(cost) = meta.attempted_cost(args) else {
self.cycles += meta.cost_cycles;
return Err(self.trap(
TRAP_RESOURCE_LIMIT,
op as u16,
"Intrinsic attempted cost overflow".into(),
pc,
));
};
self.cycles += cost;
meta.validate_descriptor()
.map_err(|message| self.trap(TRAP_INVALID_INTRINSIC, op as u16, message.into(), pc))?;
if !enough {
return Err(self.trap(
TRAP_STACK_UNDERFLOW,
op as u16,
"Incomplete intrinsic operands".into(),
pc,
));
}
let I::BorrowedInline(handler) = meta.implementation else {
return Err(self.trap(
TRAP_INVALID_INTRINSIC,
op as u16,
"Wrong invocation profile".into(),
pc,
));
};
let base = self.operand_stack.len() - consumed;
let args = &self.operand_stack[base..];
meta.validate_argument_values(args).map_err(|_| {
self.trap(TRAP_TYPE, op as u16, "Invalid intrinsic argument carrier".into(), pc)
})?;
self.inline_values(args, meta.arg_layout)
.map_err(|error| self.value_trap(error, op, pc))?;
let mut out = IntrinsicResults::new(meta.ret_slots());
handler(args, &mut out).map_err(|error| match error {
E::Numeric(error) => {
self.trap(error.code, op as u16, format!("Intrinsic numeric fault: {error:?}"), pc)
}
_ => self.trap(
TRAP_INVALID_INTRINSIC,
op as u16,
format!("Invalid intrinsic handler output: {error:?}"),
pc,
),
})?;
out.validate(meta).map_err(|error| {
self.trap(
TRAP_INVALID_INTRINSIC,
op as u16,
format!("Invalid intrinsic result: {error:?}"),
pc,
)
})?;
self.inline_values(out.as_slice(), meta.ret_layout).map_err(|_| {
self.trap(
TRAP_INVALID_INTRINSIC,
op as u16,
"Invalid intrinsic result ownership".into(),
pc,
)
})?;
self.operand_stack.truncate(base);
for value in out.into_values() {
self.operand_stack.push(value).expect("bounded result replaces admitted inputs");
}
Ok(())
}
}
@@ -62,7 +62,7 @@ impl VirtualMachine {
if self.temporary_slots() == 0 {
return Err("Stack underflow".into());
}
self.operand_stack.pop().ok_or("Stack underflow".into())
self.operand_stack.pop().ok_or_else(|| "Stack underflow".into())
}
pub fn pop_number(&mut self) -> Result<f64, String> {
@@ -82,7 +82,7 @@ impl VirtualMachine {
if self.temporary_slots() == 0 {
return Err("Stack underflow".into());
}
self.operand_stack.last().ok_or("Stack underflow".into())
self.operand_stack.last().ok_or_else(|| "Stack underflow".into())
}
pub(super) fn pop_trap<S: Into<String>>(
@@ -112,8 +112,22 @@ impl VirtualMachine {
where
F: FnOnce(Value, Value) -> Result<Value, OpError>,
{
let b = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
let a = self.pop_trap(opcode, start_pc, format!("{:?} requires two operands", opcode))?;
let b = self.pop().map_err(|_| {
self.trap(
TRAP_STACK_UNDERFLOW,
opcode as u16,
format!("{:?} requires two operands", opcode),
start_pc,
)
})?;
let a = self.pop().map_err(|_| {
self.trap(
TRAP_STACK_UNDERFLOW,
opcode as u16,
format!("{:?} requires two operands", opcode),
start_pc,
)
})?;
match f(a, b) {
Ok(res) => {
self.push_trap(res, opcode, start_pc)?;
@@ -0,0 +1,81 @@
//! Opt-in diagnostics for the actual native provenance boundaries.
use super::*;
use crate::ValidatedProgramImage;
use std::{hint::black_box, sync::Arc};
pub struct Fixture {
vm: VirtualMachine,
owners: Vec<Arc<str>>,
image_hit: Arc<str>,
miss: Arc<str>,
}
impl Fixture {
pub fn new(image: Arc<ValidatedProgramImage>, count: usize) -> Self {
let Value::String(image_hit) = image.constants().last().unwrap() else { panic!() };
let image_hit = image_hit.clone();
let mut vm = VirtualMachine::from_image(image, Default::default());
let ledger = crate::string_ledger::StringLedger::new(vm.budget.clone()).unwrap();
let owners = (0..count).map(|_| ledger.import("same").unwrap()).collect::<Vec<_>>();
vm.strings = Some(ledger);
vm.operand_stack.push(Value::String(image_hit.clone())).unwrap();
vm.operand_stack.push(Value::String(owners.last().unwrap().clone())).unwrap();
Self { vm, owners, image_hit, miss: Arc::from("same") }
}
pub fn auxiliary_bytes(&self) -> u64 {
self.vm.budget.used(RuntimeResource::AuxiliaryBytes)
}
pub fn work(&self) -> [usize; 4] {
let image = self.vm.validated_image.as_ref().unwrap();
let session = self.vm.strings.as_ref().unwrap();
let (ih, iw) = image.lookup_work(&self.image_hit);
let (im, mw) = image.lookup_work(&self.miss);
let (sh, sw) = session.lookup_work(self.owners.last().unwrap());
let (sm, tw) = session.lookup_work(&self.miss);
assert!(ih && sh && !im && !sm);
[iw, mw, sw, tw]
}
pub fn run(&mut self, workload: &str) {
match workload {
"image_hit" => {
black_box(
self.vm
.validated_image
.as_ref()
.unwrap()
.retains_string(black_box(&self.image_hit)),
);
}
"image_miss" => {
black_box(
self.vm.validated_image.as_ref().unwrap().retains_string(black_box(&self.miss)),
);
}
"session_hit" => {
black_box(
self.vm
.strings
.as_ref()
.unwrap()
.retains(black_box(self.owners.last().unwrap())),
);
}
"session_miss" => {
black_box(self.vm.strings.as_ref().unwrap().retains(black_box(&self.miss)));
}
"import_hit" => {
black_box(
self.vm
.import_string_value(&Value::String(self.owners.last().unwrap().clone()))
.unwrap(),
);
}
"import_miss" => {
black_box(self.vm.import_string_value(&Value::String(self.miss.clone())).unwrap());
}
"snapshot_mixed" => {
black_box(self.vm.operand_stack_top(2));
}
_ => panic!("unknown workload"),
}
}
}
@@ -699,28 +699,28 @@ fn ordered_multi_group_return_preserves_native_owners_and_receiver_identity() {
}
#[test]
fn unknown_return_and_late_return_resource_failures_keep_current_frame_and_operands() {
fn unknown_return_refusal_keeps_frame_but_valid_return_needs_no_scratch() {
let mut code = vec![];
arg(&mut code, OpCode::GetGlobal, 0);
op(&mut code, OpCode::Ret);
let image = image(&code, vec![function(0, code.len(), &[], &[0], 0, 1)]);
for (value, limits, expected) in [
(Value::Boolean(true), RuntimeLimits::default(), TRAP_TYPE),
(
Value::Int32(3),
RuntimeLimits::default().lowered(RuntimeResource::TemporaryBytes, 0).unwrap(),
TRAP_RESOURCE_LIMIT,
),
] {
for value in [Value::Boolean(true), Value::Int32(3)] {
let limits = RuntimeLimits::default().lowered(RuntimeResource::TemporaryBytes, 0).unwrap();
let mut vm = VirtualMachine::from_image(image.clone(), limits);
vm.prepare_boot_call().unwrap();
vm.globals.push(value.clone()).unwrap();
let result = run(&mut vm);
assert!(
matches!(result,LogicalFrameEndingReason::Trap(info) if info.code==expected && info.pc==6)
);
assert_eq!(vm.call_stack.len(), 1);
if matches!(value, Value::Boolean(_)) {
assert!(
matches!(result, LogicalFrameEndingReason::Trap(info) if info.code == TRAP_TYPE && info.pc == 6)
);
assert_eq!(vm.call_stack.len(), 1);
} else {
assert_eq!(result, LogicalFrameEndingReason::EndOfRom);
assert!(vm.call_stack.is_empty());
}
assert_eq!(vm.operand_stack.as_slice(), &[value]);
assert_eq!(vm.budget.used(RuntimeResource::TemporaryBytes), 0);
}
}
@@ -132,3 +132,39 @@ fn collection_load_limits_and_analysis_refusals_keep_context_and_refund_scratch(
assert_eq!(budget.used(LoadResource::RegistryBytes), 0);
}
}
#[test]
fn removed_storage_opcodes_reject_even_in_unreachable_instruction_positions() {
use prometeu_bytecode::load_limits::{LoadCode, LoadReason};
use prometeu_vm::VmInitError;
for opcode in [0xa7u16, 0xa8, 0xaa, 0xab] {
for dead in [false, true] {
let mut code = if dead { vec![1, 0] } else { vec![] };
let pc = code.len() as u32;
code.extend_from_slice(&opcode.to_le_bytes());
let VmInitError::RegistryLoadFailed(error) = load(&code).unwrap_err() else { panic!() };
assert_eq!(error.code, LoadCode::InvalidCode);
assert_eq!(error.reason, LoadReason::UnknownOpcode);
assert_eq!((error.function, error.pc), (Some(0), Some(pc)));
}
}
// The same bytes are accepted as an Int64 immediate, never decoded as opcodes.
let code = [0x14, 0, 0xa7, 0, 0xa8, 0, 0xaa, 0, 0xab, 0, 1, 0];
assert_eq!(
run(&code),
[Value::Int64(i64::from_le_bytes([0xa7, 0, 0xa8, 0, 0xaa, 0, 0xab, 0]))]
);
}
#[test]
fn neutral_pbx_copy_moves_a_range_and_preserves_physical_length() {
let code=assemble("PUSH_I64 2\nARRAY_NEW 11\nSET_GLOBAL 0\nGET_GLOBAL 0\nPUSH_I64 0\nPUSH_I32 42\nARRAY_SET 11\nGET_GLOBAL 0\nPUSH_I64 0\nGET_GLOBAL 0\nPUSH_I64 1\nPUSH_I64 1\nARRAY_COPY 11\nGET_GLOBAL 0\nPUSH_I64 1\nARRAY_GET 11\nGET_GLOBAL 0\nARRAY_LEN 11\nHALT").unwrap();
assert_eq!(run(&code), [Value::Int64(2), Value::Int32(42)]);
for text in [
"HALT\nARRAY_COPY 9",
"HALT\nARRAY_COPY 99",
"PUSH_I64 1\nARRAY_NEW 11\nPUSH_I64 0\nPUSH_I64 1\nARRAY_NEW 11\nPUSH_I64 0\nPUSH_BOOL 1\nARRAY_COPY 11\nHALT",
] {
assert!(load(&assemble(text).unwrap()).is_err(), "{text}");
}
}
@@ -0,0 +1,229 @@
//! Count only this thread's warmed instruction region, including reallocations.
use prometeu_bytecode::{assemble, load_limits::LoadBudget, registry::*};
use prometeu_hal::{HostContext, syscalls::caps};
use prometeu_test_support::program::{TestProgram, int64_signature};
use prometeu_vm::{ValidatedProgramImage, VirtualMachine};
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
struct Native;
impl prometeu_hal::NativeInterface for Native {
fn syscall(
&mut self,
_: u32,
_: &[prometeu_bytecode::Value],
_: &mut prometeu_hal::HostReturn,
_: &mut HostContext,
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
panic!("no syscalls in allocation fixtures")
}
}
thread_local! {
static COUNTS: Cell<Option<(usize, usize)>> = const { Cell::new(None) };
}
struct Counter;
fn count(alloc: usize, free: usize) {
let _ = COUNTS.try_with(|c| {
if let Some((a, d)) = c.get() {
c.set(Some((a + alloc, d + free)));
}
});
}
unsafe impl GlobalAlloc for Counter {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
count(1, 0);
unsafe { System.alloc(l) }
}
unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 {
count(1, 0);
unsafe { System.alloc_zeroed(l) }
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
count(0, 1);
unsafe { System.dealloc(p, l) }
}
unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
count(1, 1);
unsafe { System.realloc(p, l, n) }
}
}
#[global_allocator]
static ALLOCATOR: Counter = Counter;
#[test]
fn warmed_instruction_regions_do_not_allocate() {
let mut observations = vec![];
for (name, caller, callee, results, steps) in [
("scalar", "CALL 1\nPOP\nJMP 0", "PUSH_I64 42\nRET", 1, 5),
("void", "CALL 1\nJMP 0", "RET", 0, 3),
("multi", "CALL 1\nPOP\nPOP\nJMP 0", "PUSH_I64 11\nPUSH_I64 22\nRET", 2, 7),
("arithmetic_peek", "PUSH_I64 3\nDUP\nADD\nPOP\nJMP 0", "RET", 0, 5),
] {
let mut code = assemble(caller).unwrap();
let offset = code.len();
let body = assemble(callee).unwrap();
let mut program = TestProgram::single(code.clone(), int64_signature(0, 0), 4, 8);
program.functions.push(FunctionDecl {
signature: int64_signature(0, results),
code_offset: offset as u32,
code_len: body.len() as u32,
local_slots: 4,
max_stack_slots: 8,
..Default::default()
});
code.extend(body);
program.code = code;
let image = ValidatedProgramImage::load(
&program.serialize(),
&mut LoadBudget::new(Default::default()),
caps::NONE,
)
.unwrap();
let mut vm = VirtualMachine::from_image(image, Default::default());
vm.prepare_boot_call().unwrap();
let mut host = HostContext::new(None);
for _ in 0..steps * 10 {
vm.step(&mut Native, &mut host).unwrap();
}
COUNTS.with(|c| c.set(Some((0, 0))));
for _ in 0..steps * 1000 {
vm.step(&mut Native, &mut host).unwrap();
}
let counts = COUNTS.with(|c| c.replace(None).unwrap());
println!("{name}: 1000 iterations, {counts:?} allocations/deallocations");
observations.push((name, counts));
}
for (name, counts) in observations {
assert_eq!(counts, (0, 0), "{name}");
}
}
#[cfg(feature = "resource-diagnostics")]
#[allow(dead_code)]
#[path = "support/callables.rs"]
mod provenance_callables;
#[cfg(feature = "resource-diagnostics")]
#[test]
fn provenance_queries_allocate_nothing_at_all_qualification_sizes() {
use prometeu_bytecode::model::ConstantPoolEntry;
use prometeu_vm::string_provenance_qualification::Fixture;
for count in [1024_usize, 16384, 65536] {
let constants = vec![ConstantPoolEntry::String("same".into()); count];
let image = provenance_callables::image_constants(
&[("RET", provenance_callables::sig(&[], &[]), vec![])],
vec![],
&constants,
)
.unwrap();
let mut fixture = Fixture::new(image, count);
let work = fixture.work();
let bound = count.ilog2() as usize + 1;
assert!(work[0] <= bound && work[1] <= bound);
for workload in ["image_hit", "image_miss", "session_hit", "session_miss", "import_hit"] {
COUNTS.with(|c| c.set(Some((0, 0))));
for _ in 0..100 {
fixture.run(workload);
}
let counts = COUNTS.with(|c| c.replace(None)).unwrap();
assert_eq!(counts, (0, 0), "{workload}, owners={count}");
}
COUNTS.with(|c| c.set(Some((0, 0))));
fixture.run("snapshot_mixed");
let counts = COUNTS.with(|c| c.replace(None)).unwrap();
assert_eq!(counts, (1, 1), "only snapshot Value storage allocates");
}
}
#[path = "support/identity.rs"]
mod identity_support;
#[test]
fn identity_and_borrowed_hash_instruction_regions_do_not_allocate() {
use prometeu_bytecode::model::ConstantPoolEntry as K;
for (name, initialize, operand, operation, steps, closure) in [
("hash_i64", "PUSH_I64 -73\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4100", 4, false),
("hash_float", "PUSH_CONST 0\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4101", 4, false),
("hash_string", "PUSH_CONST 1\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4102", 4, false),
(
"struct_hash",
"STRUCT_NEW 5\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 5",
4,
false,
),
(
"array_hash",
"PUSH_I64 0\nARRAY_NEW 6\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 6",
4,
false,
),
(
"fn_hash",
"MAKE_CLOSURE fn=1,type=9\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 9",
4,
true,
),
(
"random_hash",
"PUSH_I64 1\nINTRINSIC 0x4000\nSET_LOCAL 0",
"GET_LOCAL 0",
"REF_IDENTITY_HASH 10",
4,
false,
),
(
"trait_hash",
"STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nSET_LOCAL 1\nSET_LOCAL 0",
"GET_LOCAL 0\nGET_LOCAL 1",
"REF_IDENTITY_HASH 7",
5,
false,
),
(
"struct_eq",
"STRUCT_NEW 5\nSET_LOCAL 0",
"GET_LOCAL 0\nGET_LOCAL 0",
"REF_IDENTITY_EQ left=5,right=5",
5,
false,
),
(
"trait_eq",
"STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nSET_LOCAL 1\nSET_LOCAL 0",
"GET_LOCAL 0\nGET_LOCAL 1\nGET_LOCAL 0\nGET_LOCAL 1",
"REF_IDENTITY_EQ left=7,right=7",
7,
false,
),
] {
let start = assemble(initialize).unwrap().len();
let text = format!("{initialize}\n{operand}\n{operation}\nPOP\nJMP {start}");
let mut bodies = vec![(text.as_str(), identity_support::sig(&[], &[]))];
if closure {
bodies.push(("RET", identity_support::sig(&[], &[])));
}
let mut vm =
identity_support::vm(&bodies, &[K::Float64(-0.0), K::String("π\0".repeat(512))]);
let mut host = HostContext::new(None);
while vm.pc() != start {
vm.step(&mut identity_support::Native, &mut host).unwrap();
}
for _ in 0..steps * 10 {
vm.step(&mut identity_support::Native, &mut host).unwrap();
}
let heap_allocations = vm.heap().allocation_count();
COUNTS.with(|c| c.set(Some((0, 0))));
for _ in 0..steps * 1000 {
vm.step(&mut identity_support::Native, &mut host).unwrap();
}
let counts = COUNTS.with(|c| c.replace(None).unwrap());
assert_eq!(counts, (0, 0), "{name}: allocations and deallocations");
assert_eq!(vm.heap().allocation_count(), heap_allocations, "{name}: guest allocations");
}
}
@@ -216,3 +216,47 @@ fn integer_division_and_remainder_overflow_wrap_without_host_panic() {
assert_eq!(machine.operand_stack_top(1), vec![Value::Int64(expected)]);
}
}
#[test]
fn maximum_width_mixed_return_preserves_order_and_caller_locals() {
// TestProgram catalog: Int64, Vec2, Float = 0, 6, 2.
let mut caller = String::from("PUSH_I64 73\nSET_LOCAL 0\nCALL 1\n");
caller.push_str(&"POP\n".repeat(1024));
caller.push_str("GET_LOCAL 0\nRET");
let mut callee = String::new();
let mut results = vec![];
let mut expected = vec![];
for i in 0..256 {
callee.push_str(&format!("PUSH_I64 {i}\nPUSH_F64 f64:0x3ff0000000000000\nPUSH_F64 f64:0x4000000000000000\nPUSH_F64 f64:0x4008000000000000\n"));
results.extend([TypeId(0), TypeId(6), TypeId(2)]);
expected.extend([Value::Int64(i), Value::Float(1.), Value::Float(2.), Value::Float(3.)]);
}
callee.push_str("RET");
let caller_code = assemble(&caller).unwrap();
let body = assemble(&callee).unwrap();
let mut program = TestProgram::single(caller_code.clone(), int64_signature(0, 1), 1, 1024);
program.functions.push(FunctionDecl {
signature: Signature { parameters: vec![], results },
code_offset: caller_code.len() as u32,
code_len: body.len() as u32,
local_slots: 3,
max_stack_slots: 1024,
..Default::default()
});
program.code.extend(body);
let image = ValidatedProgramImage::load(
&program.serialize(),
&mut LoadBudget::new(Default::default()),
caps::NONE,
)
.unwrap();
let mut machine = VirtualMachine::from_image(image, Default::default());
machine.prepare_boot_call().unwrap();
for _ in 0..3 + 1024 + 1 {
machine.step(&mut Native, &mut HostContext::new(None)).unwrap();
}
expected.reverse();
assert_eq!(machine.operand_stack_top(1024), expected);
assert_eq!(run(&mut machine, 10000).reason, LogicalFrameEndingReason::EndOfRom);
assert_eq!(machine.operand_stack_top(1), vec![Value::Int64(73)]);
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,23 @@
# Hash-v1 oracle provenance
`hash-conformance.tsv` is generated by `hash-oracle.py` against the official C
XXH64 reference, not the Rust VM implementation. Ordinary tests read the checked-in
vectors and need no library, network or Python dependency.
Reference: https://github.com/Cyan4973/xxHash/blob/v0.8.3/xxhash.h
Header SHA-256: `17973c0dc49d9854ca26caa191f0e12f7a424b68858d9a78de3860d959d85e4b`.
The generator verifies both the header digest and `XXH_versionNumber()==803`.
Example reproduction on macOS, after downloading that pinned header:
```sh
clang -dynamiclib -O2 -x c -DXXH_IMPLEMENTATION -DXXH_STATIC_LINKING_ONLY xxhash.h -o libxxhash-oracle.dylib
python3 hash-oracle.py --header xxhash.h --library ./libxxhash-oracle.dylib
```
On Linux, use `-shared -fPIC` instead of `-dynamiclib`. The generator explicitly
encodes little-endian integers/reference components and finite Float bit patterns,
canonicalizes signed zeros, and uses seed zero. The null row is the machine
contract's direct zero result, not a hash of an empty message. Arbitrary `bytes`
rows exercise the internal algorithm; they do not expose guest byte strings.
@@ -0,0 +1,51 @@
"""Regenerate hash-v1 vectors using upstream XXH64, never the Rust executor."""
import argparse
import ctypes
import hashlib
from pathlib import Path
import struct
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--library", required=True, type=Path)
parser.add_argument("--header", required=True, type=Path)
parser.add_argument("--output", type=Path, default=Path(__file__).with_name("hash-conformance.tsv"))
args = parser.parse_args()
expected = "17973c0dc49d9854ca26caa191f0e12f7a424b68858d9a78de3860d959d85e4b"
assert hashlib.sha256(args.header.read_bytes()).hexdigest() == expected
library = ctypes.CDLL(str(args.library.resolve()))
assert library.XXH_versionNumber() == 803
digest = library.XXH64
digest.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_uint64]
digest.restype = ctypes.c_uint64
rows = ["kind\tinput\texpected_hash_hex"]
def emit(kind, value, data):
buffer = ctypes.create_string_buffer(data)
result = digest(ctypes.cast(buffer, ctypes.c_void_p), len(data), 0)
rows.append(f"{kind}\t{value}\t{result:016X}")
for size in list(range(71)) + [95, 96, 97, 127, 128, 129, 255, 256, 257, 1024, 4097]:
data = bytes((i * 131 + 17) & 255 for i in range(size))
emit("bytes", data.hex(), data)
for value in [0, 1, -1, 2, -2, -(2**63), 2**63 - 1, 2**53 + 1, 2**32]:
emit("i64", value, struct.pack("<q", value))
for bits in [0, 0x8000000000000000, 0x3FF0000000000000, 0xBFF0000000000000,
0x3FE0000000000000, 0xBFE0000000000000, 1, 0x8000000000000001,
0x000FFFFFFFFFFFFF, 0x0010000000000000,
0x7FEFFFFFFFFFFFFF, 0xFFEFFFFFFFFFFFFF, 0x4340000000000000]:
canonical = 0 if bits & 0x7FFFFFFFFFFFFFFF == 0 else bits
emit("float", f"{bits:016X}", struct.pack("<Q", canonical))
for value in ["", "hello", "\0", "ação 😀", "a" * 31, "a" * 32, "a" * 33, "z" * 1025]:
data = value.encode("utf-8")
emit("string", data.hex(), data)
for index, generation in [(0, 1), (1, 1), (1, 2), (1024, 1), (2**32-1, 2**32-1)]:
emit("reference", f"{index},{generation}", struct.pack("<II", index, generation))
rows.append("null\tnull\t0000000000000000")
args.output.write_text("\n".join(rows) + "\n", encoding="utf-8")
print(f"Generated {len(rows)-1} vectors with upstream XXH64 v0.8.3, seed 0.")
if __name__ == "__main__":
main()
@@ -0,0 +1,152 @@
#[path = "support/identity.rs"]
mod support;
use prometeu_bytecode::{Value, model::ConstantPoolEntry as K};
use prometeu_vm::{HostContext, LogicalFrameEndingReason as End};
use support::*;
#[test]
fn scalar_pbx_execution_matches_independent_c_reference_vectors() {
let mut count = 0;
for row in include_str!("fixtures/hash-conformance.tsv").lines().skip(1) {
let fields: Vec<_> = row.split('\t').collect();
let expected = u64::from_str_radix(fields[2], 16).unwrap() as i64;
let (value, id, bytes) = match fields[0] {
"i64" => (K::Int64(fields[1].parse().unwrap()), 0x4100, 0),
"float" => {
(K::Float64(f64::from_bits(u64::from_str_radix(fields[1], 16).unwrap())), 0x4101, 0)
}
"string" => {
let bytes = hex_bytes(fields[1]);
let size = bytes.len();
(K::String(String::from_utf8(bytes).unwrap()), 0x4102, size)
}
"null" => (K::Null, 0x4102, 0),
_ => continue,
};
let source = format!("PUSH_CONST 0\nINTRINSIC {id}\nRET");
let mut vm = vm(&[(&source, sig(&[], &[0]))], &[value]);
step(&mut vm).unwrap();
let report = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(report.reason, End::BudgetExhausted);
assert_eq!(report.cycles_used, 8 + bytes as u64, "{row}");
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int64(expected)], "{row}");
assert_eq!(run(&mut vm).reason, End::EndOfRom);
count += 1;
}
assert_eq!(count, 31);
}
#[test]
fn identities_survive_mutation_and_views_validate_implementation_zero() {
let source = "STRUCT_NEW 5\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 5\nSET_LOCAL 1\nGET_LOCAL 0\nPUSH_I64 73\nSTRUCT_SET type=5,field=0\nGET_LOCAL 0\nREF_IDENTITY_HASH 5\nGET_LOCAL 1\nEQ\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=7\nREF_IDENTITY_HASH 7\nGET_LOCAL 1\nEQ\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=7\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=8\nREF_IDENTITY_EQ left=7,right=8\nGET_LOCAL 0\nSTRUCT_NEW 5\nREF_IDENTITY_EQ left=5,right=5\nRET";
let mut vm = vm(&[(source, sig(&[], &[2, 2, 2, 2]))], &[]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(
&*vm.operand_stack_top(4),
&[Value::Boolean(false), Value::Boolean(true), Value::Boolean(true), Value::Boolean(true)]
);
}
#[test]
fn array_closure_and_random_have_instance_identity() {
for (source, bodies) in [
(
"PUSH_I64 1\nARRAY_NEW 6\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 6\nSET_LOCAL 1\nGET_LOCAL 0\nPUSH_I64 0\nPUSH_I64 42\nARRAY_SET 6\nGET_LOCAL 0\nREF_IDENTITY_HASH 6\nGET_LOCAL 1\nEQ\nRET",
false,
),
(
"PUSH_I64 42\nINTRINSIC 0x4000\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 10\nSET_LOCAL 1\nGET_LOCAL 0\nINTRINSIC 0x4001\nPOP\nGET_LOCAL 0\nREF_IDENTITY_HASH 10\nGET_LOCAL 1\nEQ\nRET",
false,
),
(
"MAKE_CLOSURE fn=1,type=9\nSET_LOCAL 0\nGET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_EQ left=9,right=9\nGET_LOCAL 0\nMAKE_CLOSURE fn=1,type=9\nREF_IDENTITY_EQ left=9,right=9\nNOT\nAND\nRET",
true,
),
] {
let mut functions = vec![(source, sig(&[], &[2]))];
if bodies {
functions.push(("RET", sig(&[], &[])));
}
let mut vm = vm(&functions, &[]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(&*vm.operand_stack_top(1), &[Value::Boolean(true)]);
}
}
#[test]
fn canonical_null_groups_compare_and_hash_without_instance_faults() {
let source = "PUSH_CONST 0\nPUSH_CONST 0\nREF_IDENTITY_HASH 7\nPUSH_CONST 0\nPUSH_CONST 0\nPUSH_CONST 0\nREF_IDENTITY_EQ left=6,right=7\nRET";
let mut vm = vm(&[(source, sig(&[], &[0, 2]))], &[K::Null]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(&*vm.operand_stack_top(2), &[Value::Boolean(true), Value::Int64(0)]);
}
#[test]
fn admission_rejects_ineligible_sources_and_bad_native_ids_even_when_dead() {
for instruction in [
"REF_IDENTITY_HASH 0",
"REF_IDENTITY_HASH 3",
"REF_IDENTITY_HASH 4",
"REF_IDENTITY_HASH 12",
"REF_IDENTITY_HASH 13",
"REF_IDENTITY_HASH 14",
"REF_IDENTITY_HASH 9999",
"REF_IDENTITY_EQ left=5,right=3",
"INTRINSIC 0x41FF",
] {
let text = format!("HALT\n{instruction}");
assert!(image(&[(&text, sig(&[], &[]))], &[]).is_err(), "{instruction}");
}
assert!(image(&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))], &[K::Null]).is_ok());
for id in [0x1103, 0x4100, 0x4101] {
let text = format!("PUSH_CONST 0\nINTRINSIC {id}\nPOP\nRET");
assert!(image(&[(&text, sig(&[], &[]))], &[K::Null]).is_err(), "{id}");
}
assert!(image(&[("PUSH_I64 3\nREF_IDENTITY_HASH 5\nRET", sig(&[], &[0]))], &[]).is_err());
}
#[test]
fn whole_instruction_budget_admission_preserves_zero_and_overshoot() {
let mut vm = vm(
&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))],
&[K::String("x".repeat(65))],
);
step(&mut vm).unwrap();
let before = vm.pc();
let zero = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(zero.cycles_used, 0);
assert_eq!(vm.pc(), before);
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(one.reason, End::BudgetExhausted);
assert_eq!(one.cycles_used, 73);
assert_eq!(one.steps_executed, 1);
}
#[test]
fn every_eligible_absence_has_zero_hash_and_exact_identity_instruction_cost() {
for (ty, width) in [(5, 1), (6, 1), (7, 2), (9, 1), (10, 1)] {
let source = format!("{}REF_IDENTITY_HASH {ty}\nRET", "PUSH_CONST 0\n".repeat(width));
let mut vm = vm(&[(&source, sig(&[], &[0]))], &[K::Null]);
for _ in 0..width {
step(&mut vm).unwrap();
}
let before = vm.pc();
let zero = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((zero.cycles_used, zero.steps_executed), (0, 0));
assert_eq!(vm.pc(), before);
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((one.cycles_used, one.steps_executed), (8 + width as u64, 1));
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int64(0)]);
}
for source in [
"PUSH_CONST 0\nSTRUCT_NEW 5\nREF_IDENTITY_EQ left=6,right=5\nRET",
"STRUCT_NEW 5\nSTRUCT_NEW 11\nREF_IDENTITY_EQ left=5,right=11\nRET",
] {
let mut vm = vm(&[(source, sig(&[], &[2]))], &[K::Null]);
step(&mut vm).unwrap();
step(&mut vm).unwrap();
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((one.cycles_used, one.steps_executed), (6, 1));
assert_eq!(&*vm.operand_stack_top(1), &[Value::Boolean(false)]);
}
}
@@ -0,0 +1,105 @@
use prometeu_bytecode::Value;
use prometeu_vm::{
AbiType, BuiltinScalarType as S, IntrinsicCostModel as C, intrinsic_catalog,
intrinsic_catalog_csv, lookup_intrinsic_by_id,
};
use std::collections::BTreeSet;
#[test]
fn schema_two_has_exact_columns_profiles_and_per_slot_nullable_lists() {
let csv = intrinsic_catalog_csv();
assert_eq!(csv, include_str!("../../../../docs/vm-arch/INTRINSICS.csv"));
let mut rows = csv.lines();
assert_eq!(
rows.next().unwrap(),
"final_id_hex,final_id_dec,canonical_name,canonical_version,owner,name,arg_slots,ret_slots,arg_layout,ret_layout,deterministic,may_allocate,parameter_names,cost_cycles,mutates_guest_state,cost_model,attempt_cost_cycles,max_attempts,schema_version,arg_nullable,invocation_model,scratch_model,context_access,cost_arg_index,cost_per_byte"
);
let mut ids = BTreeSet::new();
let mut keys = BTreeSet::new();
let mut previous = 0;
for row in rows {
let f: Vec<_> = row.split(',').collect();
assert_eq!(f.len(), 25);
assert_eq!(f[18], "2");
let id = f[1].parse::<u32>().unwrap();
assert!(id > previous);
previous = id;
assert!(ids.insert(id));
assert!(keys.insert((f[2], f[3])));
let meta = lookup_intrinsic_by_id(id).unwrap();
meta.validate_descriptor().unwrap();
assert_eq!(f[19].split('|').filter(|s| !s.is_empty()).count(), meta.arg_slots());
assert_eq!(f[20], meta.invocation_model());
assert_eq!(f[21], meta.scratch_model());
assert_eq!(f[22], meta.context_access());
if id < 0x4100 {
assert_eq!(f[21], "existing_contract_v1");
assert_eq!(f[23], "");
assert_eq!(f[24], "0");
assert_eq!(f[20], if f[4] == "random" { "vm_owned_v1" } else { "host_buffered_v1" });
for (abi, nullable) in meta.arg_layout.iter().zip(meta.arg_nullable) {
assert_eq!(*nullable, matches!(abi,AbiType::Builtin(key) if key.name=="Random"));
}
} else {
assert_eq!(&f[20..23], &["borrowed_inline_v1", "none", "none"]);
assert_eq!(meta.cost_cycles, 8);
assert_eq!(meta.ret_layout, &[AbiType::Scalar(S::Int64)]);
assert_eq!(meta.arg_nullable, &[id == 0x4102]);
}
}
assert_eq!(ids.len(), 108);
assert_eq!(intrinsic_catalog().count(), 108);
}
#[test]
fn incompatible_descriptors_fail_instead_of_gaining_implicit_capabilities() {
let original = *lookup_intrinsic_by_id(0x4102).unwrap();
for variant in 0..14 {
let mut meta = original;
match variant {
0 => meta.arg_nullable = &[],
1 => meta.parameter_names = &[],
2 => meta.cost_arg_index = None,
3 => meta.cost_arg_index = Some(1),
4 => meta.cost_per_byte = 0,
5 => meta.attempt_cost_cycles = 1,
6 => meta.max_attempts = 1,
7 => meta.may_allocate = true,
8 => meta.mutates_guest_state = true,
9 => meta.deterministic = false,
10 => meta.ret_layout = &[AbiType::Scalar(S::Int64); 4],
11 => meta.ret_layout = &[AbiType::Scalar(S::Int64); 2],
12 => meta.ret_layout = &[AbiType::DynamicPayload],
13 => meta.arg_layout = &[AbiType::DynamicTag],
_ => unreachable!(),
}
assert!(meta.validate_descriptor().is_err(), "variant {variant}");
}
let mut meta = *lookup_intrinsic_by_id(0x4100).unwrap();
meta.arg_nullable = &[true];
assert!(meta.validate_descriptor().is_err());
meta.arg_nullable = &[false];
meta.cost_arg_index = Some(0);
assert!(meta.validate_descriptor().is_err());
meta.cost_model = C::ArgumentUtf8BytesV1;
meta.cost_per_byte = 1;
assert!(meta.validate_descriptor().is_err());
meta.cost_model = C::RandomRejectionV1;
meta.cost_arg_index = None;
meta.cost_per_byte = 0;
meta.attempt_cost_cycles = 8;
meta.max_attempts = 64;
assert!(meta.validate_descriptor().is_err());
}
#[test]
fn nullability_does_not_relax_existing_string_or_numeric_arguments() {
for id in [0x4100, 0x4101, 0x1103] {
assert!(
lookup_intrinsic_by_id(id).unwrap().validate_argument_values(&[Value::Null]).is_err()
);
}
assert!(
lookup_intrinsic_by_id(0x4102).unwrap().validate_argument_values(&[Value::Null]).is_ok()
);
}
@@ -61,7 +61,9 @@ fn integer_limits_are_exact_and_abs_does_not_wrap() {
] {
assert_eq!(call(base, &[b.clone(), a.clone()]).unwrap(), a);
assert_eq!(call(base + 1, &[a.clone(), b.clone()]).unwrap(), b);
assert!(matches!(call(base+3,&[a.clone()]),Err(E::Numeric(e)) if e.code==RANGE));
assert!(
matches!(call(base+3,std::slice::from_ref(&a)),Err(E::Numeric(e)) if e.code==RANGE)
);
assert!(matches!(call(base+2,&[b.clone(),b,a]),Err(E::Numeric(e)) if e.code==DOMAIN));
}
assert_eq!(
@@ -45,7 +45,7 @@ fn independent_words_signed_and_float_bits_for_all_seed_patterns() {
});
}
code.push_str("RET");
let mut v = vm(&[(&code, sig(&[], &vec![if floating { 2 } else { 0 }; 8]), vec![], 1)]);
let mut v = vm(&[(&code, sig(&[], &[if floating { 2 } else { 0 }; 8]), vec![], 1)]);
let r = run(&mut v, 10000);
assert_eq!(r.reason, End::EndOfRom);
let expected: Vec<_> = vectors(seed)
@@ -9,7 +9,7 @@ use prometeu_vm::{
// Independent executable oracle: 32-byte header, eight directory entries,
// payload at 128, one Bool import, one zero-slot HALT function; total 194.
fn minimal() -> Vec<u8> {
"50 42 58 00 01 00 00 00 08 00 00 00
"50 42 58 00 00 00 00 00 08 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 80 00 00 00 04 00 00 00
01 00 00 00 84 00 00 00 1e 00 00 00
@@ -46,7 +46,7 @@ impl NativeInterface for Native {
}
#[test]
fn independent_v1_bytes_load_publish_and_execute_through_the_public_sealed_boundary() {
fn independent_v0_bytes_load_publish_and_execute_through_the_public_sealed_boundary() {
let bytes = minimal();
assert_eq!(bytes.len(), 194);
let mut load = LoadBudget::new(Default::default());
@@ -64,7 +64,7 @@ fn independent_v1_bytes_load_publish_and_execute_through_the_public_sealed_bound
#[test]
fn single_violation_oracles_reject_before_image_publication_with_typed_context() {
for (offset, replacement, code, reason, pc) in [
(4, 0, LoadCode::UnsupportedFormat, LoadReason::Version, None),
(4, 1, LoadCode::UnsupportedFormat, LoadReason::Version, None),
(178, 0, LoadCode::UnsupportedFormat, LoadReason::Builtin, None),
(162, 0xff, LoadCode::InvalidCode, LoadReason::UnknownOpcode, Some(0)),
(162, 0x11, LoadCode::InvalidCode, LoadReason::StackUnderflow, Some(0)),
@@ -0,0 +1,114 @@
#![allow(dead_code)]
use prometeu_bytecode::{
assemble, load_limits::LoadBudget, model::ConstantPoolEntry, registry::*,
registry_codec::PbxModule,
};
use prometeu_vm::{
HostContext, HostReturn, LogicalFrameEndingReason, NativeInterface, ValidatedProgramImage,
VirtualMachine, VmInitError,
};
use std::sync::Arc;
pub struct Native;
impl NativeInterface for Native {
fn syscall(
&mut self,
_: u32,
_: &[prometeu_bytecode::Value],
_: &mut HostReturn,
_: &mut HostContext,
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
panic!("identity corpus uses no host syscall")
}
}
pub fn sig(parameters: &[u32], results: &[u32]) -> Signature {
Signature {
parameters: parameters
.iter()
.map(|&id| NamedType { type_id: TypeId(id), name: None })
.collect(),
results: results.iter().copied().map(TypeId).collect(),
}
}
pub fn types() -> Vec<TypeDecl> {
let field = || NamedType { type_id: TypeId(0), name: None };
vec![
TypeKind::Builtin(BuiltinId::INT64),
TypeKind::Builtin(BuiltinId::FLOAT),
TypeKind::Builtin(BuiltinId::BOOL),
TypeKind::Builtin(BuiltinId::STRING),
TypeKind::Builtin(BuiltinId::DYNAMIC),
TypeKind::Struct(vec![field()]),
TypeKind::Array(TypeId(0)),
TypeKind::Trait(vec![]),
TypeKind::Trait(vec![]),
TypeKind::Fn(sig(&[], &[])),
TypeKind::Builtin(BuiltinId::RANDOM),
TypeKind::Struct(vec![field()]),
TypeKind::Builtin(BuiltinId::COLOR),
TypeKind::Builtin(BuiltinId::VEC2),
TypeKind::Builtin(BuiltinId::PIXEL),
TypeKind::Builtin(BuiltinId::INT32),
]
.into_iter()
.map(|kind| TypeDecl { name: None, kind })
.collect()
}
pub fn image(
bodies: &[(&str, Signature)],
constants: &[ConstantPoolEntry],
) -> Result<Arc<ValidatedProgramImage>, VmInitError> {
let mut code = vec![];
let mut functions = vec![];
for (source, signature) in bodies {
let body = assemble(source).unwrap();
functions.push(FunctionDecl {
signature: signature.clone(),
local_slots: 8,
max_stack_slots: 64,
code_offset: code.len() as u32,
code_len: body.len() as u32,
..Default::default()
});
code.extend(body);
}
let mut budget = LoadBudget::new(Default::default());
let registry = Registry::resolve(
RegistryDeclarations {
types: types(),
functions,
implementations: vec![
ImplementationDecl { owner: TypeId(5), trait_id: TypeId(7), methods: vec![] },
ImplementationDecl { owner: TypeId(5), trait_id: TypeId(8), methods: vec![] },
],
},
code.len(),
&mut budget,
)
.unwrap();
let bytes = PbxModule {
registry: &registry,
code: &code,
constants,
debug: None,
exports: &[],
syscalls: &[],
}
.serialize(&mut budget)
.unwrap();
ValidatedProgramImage::load(&bytes, &mut LoadBudget::new(Default::default()), 0)
}
pub fn vm(bodies: &[(&str, Signature)], constants: &[ConstantPoolEntry]) -> VirtualMachine {
let mut vm = VirtualMachine::from_image(image(bodies, constants).unwrap(), Default::default());
vm.prepare_boot_call().unwrap();
vm
}
pub fn run(vm: &mut VirtualMachine) -> prometeu_vm::BudgetReport {
vm.run_budget(1_000_000, &mut Native, &mut HostContext::new(None)).unwrap()
}
pub fn step(vm: &mut VirtualMachine) -> Result<(), LogicalFrameEndingReason> {
vm.step(&mut Native, &mut HostContext::new(None))
}
pub fn hex_bytes(value: &str) -> Vec<u8> {
(0..value.len()).step_by(2).map(|i| u8::from_str_radix(&value[i..i + 2], 16).unwrap()).collect()
}
@@ -114,6 +114,111 @@ fn repository_sources_have_exactly_one_coverage_owner() {
);
}
fn repository_policy(script: &str) -> Output {
Command::new("python3")
.arg("-B")
.arg("-c")
.arg(format!(
r#"
import importlib.util
import json
import sys
from pathlib import Path
root = Path(sys.argv[1])
spec = importlib.util.spec_from_file_location("coverage_auditor", root / "scripts/audit-coverage-domains.py")
auditor = importlib.util.module_from_spec(spec)
spec.loader.exec_module(auditor)
config = json.loads((root / "files/config/runtime-edge-coverage-domains.json").read_text())
domains = config["domains"]
trees = [
("crates/console/prometeu-system/src/services/memcard", "system/fs", "system/core"),
("crates/console/prometeu-drivers/src/asset", "asset/bank", "drivers/core"),
]
{script}
"#
))
.arg(repository())
.output()
.unwrap()
}
#[test]
fn specialized_entry_files_and_all_descendants_have_the_expected_owner() {
success(repository_policy(
r#"
for stem, expected, residual in trees:
names = [stem + ".rs"]
descendants = sorted((root / stem).rglob("*.rs"))
assert descendants, stem
names += [path.relative_to(root).as_posix() for path in descendants]
names += [stem + "/future.rs", stem + "/future/nested.rs"]
for name in names:
actual = auditor.owners(name, domains)
assert actual == [expected], (name, expected, actual)
# Exclusive residual fallback would pass the generic audit but is wrong.
import copy
fallback = copy.deepcopy(domains)
fallback[expected]["paths"].remove(stem + "/")
fallback[residual]["exclude_paths"].remove(stem + "/")
name = names[-1]
assert auditor.owners(name, fallback) == [residual]
assert auditor.owners(name, fallback) != [expected]
overlap = copy.deepcopy(domains)
overlap[residual]["exclude_paths"].remove(stem + "/")
assert set(auditor.owners(name, overlap)) == {expected, residual}
"#,
));
}
#[test]
fn specialized_ownership_survives_absent_and_zero_instrumentation() {
success(repository_policy(
r#"
import tempfile
with tempfile.TemporaryDirectory(prefix="prometeu-domain-trees-") as directory:
fixture = Path(directory)
records = []
# Use the real map and source layout, with independent synthetic evidence.
for source in (root / "crates").glob("**/src/**/*.rs"):
name = source.relative_to(root).as_posix()
path = fixture / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("// ownership fixture\n")
records.append({"filename": name, "summary": {"lines": {"count": 1, "covered": 1}}})
for stem, expected, residual in trees:
for suffix in ["future/absent.rs", "future/zero.rs"]:
path = fixture / stem / suffix
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("// no executable lines\n")
records.append({"filename": stem + "/future/zero.rs", "summary": {"lines": {"count": 0, "covered": 0}}})
config_path = fixture / "config.json"
config_path.write_text(json.dumps(config))
report_path = fixture / "report.json"
report_path.write_text(json.dumps({"data": [{"files": records}]}))
before = auditor.audit(root, root / "files/config/runtime-edge-coverage-domains.json")
result = auditor.audit(fixture, config_path, report_path)
assert len(result["domains"]) == 15
assert all(domain["gate_passed"] for domain in result["domains"].values())
for stem, expected, residual in trees:
domain = result["domains"][expected]
assert domain["source_files"] == before["domains"][expected]["source_files"] + 2
assert stem + "/future/zero.rs" in domain["files"]
assert stem + "/future/absent.rs" not in domain["files"]
assert result["domains"][residual]["source_files"] == before["domains"][residual]["source_files"]
# Missing residual exclusions must fail even for sources absent from reports.
for stem, expected, residual in trees:
domains[residual]["exclude_paths"].remove(stem + "/")
config_path.write_text(json.dumps(config))
try:
auditor.audit(fixture, config_path, report_path)
except auditor.AuditError as error:
assert "expected exactly one domain" in str(error), error
else:
raise AssertionError("overlapping module trees passed ownership audit")
"#,
));
}
#[test]
fn exclusions_and_absolute_relative_report_names_have_exclusive_ownership() {
let fixture = Fixture::new();
@@ -107,4 +107,5 @@ mod tests {
}
}
pub mod memcard_fixture;
pub mod program;
@@ -0,0 +1,146 @@
//! Neutral PBX/ABI fixture. No source-language frontend or compiler is involved.
use crate::program::{TestProgram, scalar_catalog};
use prometeu_bytecode::{
assembler::assemble,
model::{ConstantPoolEntry, SyscallDecl},
registry::*,
};
use std::collections::BTreeMap;
#[derive(Clone, Copy)]
pub enum MemcardFixture {
Write,
Read,
ReadWithPresentation,
Dirty,
}
#[derive(Default)]
struct Code {
bytes: Vec<u8>,
labels: BTreeMap<&'static str, u32>,
fixups: Vec<(usize, &'static str)>,
}
impl Code {
fn emit(&mut self, text: &str) {
self.bytes.extend(assemble(text).expect("fixture assembly"));
}
fn mark(&mut self, name: &'static str) {
self.labels.insert(name, self.bytes.len() as u32);
}
fn jump(&mut self, op: &str, name: &'static str) {
self.fixups.push((self.bytes.len() + 2, name));
self.emit(&format!("{op} 0"));
}
fn check(&mut self, value: i64, int32: bool) {
self.emit(&format!("PUSH_{} {value}\nEQ", if int32 { "I32" } else { "I64" }));
let resume = self.bytes.len() + 8; // conditional jump followed by a terminal trap
self.emit(&format!("JMP_IF_TRUE {resume}\nTRAP"));
}
fn finish(mut self) -> Vec<u8> {
for (offset, label) in self.fixups {
self.bytes[offset..offset + 4].copy_from_slice(&self.labels[label].to_le_bytes());
}
self.bytes
}
}
/// Stack oracle at a completed frame: 100 pending, 101 durable SUCCESS,
/// 104 MEMORY_ONLY, 201 exact existing readback, 301 dirty/uncommitted RAM.
/// Read never writes or submits a commit; a missing/wrong payload traps.
pub fn memcard_program(kind: MemcardFixture) -> Vec<u8> {
let reads = matches!(kind, MemcardFixture::Read | MemcardFixture::ReadWithPresentation);
let mut main = Code::default();
main.emit("HOSTCALL 0");
main.check(0, true);
main.emit("PUSH_I64 4\nARRAY_NEW 9\nSET_GLOBAL 1");
if reads {
let read_import = if matches!(kind, MemcardFixture::Read) { 1 } else { 2 };
main.emit(&format!("GET_GLOBAL 0\nPUSH_I32 0\nPUSH_I32 0\nGET_GLOBAL 1\nPUSH_I32 0\nPUSH_I32 4\nHOSTCALL {read_import}"));
main.check(4, false);
main.check(0, false);
for (i, value) in [0, 127, 128, 255].into_iter().enumerate() {
main.emit(&format!("GET_GLOBAL 1\nPUSH_I64 {i}\nARRAY_GET 9"));
main.check(value, true);
}
main.mark("done");
main.emit("PUSH_I64 201\nFRAME_SYNC\nPOP_N 1");
main.jump("JMP", "done");
} else {
for (i, value) in [0, 127, 128, 255].into_iter().enumerate() {
main.emit(&format!("GET_GLOBAL 1\nPUSH_I64 {i}\nPUSH_I32 {value}\nARRAY_SET 9"));
}
main.emit("GET_GLOBAL 0\nPUSH_I32 0\nPUSH_I32 0\nGET_GLOBAL 1\nPUSH_I32 0\nPUSH_I32 4\nHOSTCALL 2");
main.check(4, false);
main.check(0, false);
if matches!(kind, MemcardFixture::Dirty) {
main.mark("done");
main.emit("PUSH_I64 301\nFRAME_SYNC\nPOP_N 1");
main.jump("JMP", "done");
} else {
main.emit("GET_GLOBAL 0\nPUSH_I32 0\nHOSTCALL 3\nSET_GLOBAL 2");
main.check(0, false);
main.mark("query");
main.emit("GET_GLOBAL 0\nGET_GLOBAL 2\nHOSTCALL 4\nPOP_N 1\nSET_GLOBAL 3");
main.check(0, false);
main.emit("GET_GLOBAL 3\nPUSH_I64 100\nADD\nFRAME_SYNC\nPOP_N 1");
main.jump("JMP", "query");
}
}
main.mark("error");
main.emit("TRAP");
let mut code = main.finish();
let main_len = code.len() as u32;
let hook = if matches!(kind, MemcardFixture::Read) {
assemble("GET_LOCAL 0\nSET_GLOBAL 0\nPUSH_I32 0\nRET").unwrap()
} else {
assemble("GET_LOCAL 0\nSET_GLOBAL 0\nPUSH_I64 560\nARRAY_NEW 9\nSET_GLOBAL 1\nGET_LOCAL 0\nPUSH_CONST 0\nPUSH_I64 1\nPUSH_I32 1\nPUSH_I32 1\nGET_GLOBAL 1\nPUSH_I32 0\nPUSH_I32 560\nHOSTCALL 1\nPOP_N 1\nPUSH_I32 0\nRET").unwrap()
};
let hook_len = hook.len() as u32;
code.extend(hook);
let mut types = scalar_catalog();
types.push(TypeDecl { name: None, kind: TypeKind::Array(TypeId(1)) });
let mut imports = vec![("bootstrap", 0, 1)];
if !matches!(kind, MemcardFixture::Read) {
imports.push(("presentation", 8, 1));
}
match kind {
MemcardFixture::Read | MemcardFixture::ReadWithPresentation => {
imports.push(("slot_read", 6, 2));
}
MemcardFixture::Dirty => imports.push(("slot_write", 6, 2)),
MemcardFixture::Write => {
imports.extend([("slot_write", 6, 2), ("slot_commit", 2, 2), ("query", 2, 3)])
}
}
let syscalls = imports
.into_iter()
.map(|(name, arg_slots, ret_slots)| SyscallDecl {
module: "mem".into(),
name: name.into(),
version: 1,
arg_slots,
ret_slots,
})
.collect();
TestProgram {
const_pool: vec![ConstantPoolEntry::String("Neutral persistence fixture".into())],
code,
functions: vec![
FunctionDecl { code_len: main_len, max_stack_slots: 32, ..Default::default() },
FunctionDecl {
lifecycle_role: LifecycleRole::MemcardBoot,
code_offset: main_len,
code_len: hook_len,
max_stack_slots: 32,
signature: Signature {
parameters: vec![NamedType { type_id: TypeId(0), name: None }],
results: vec![TypeId(1)],
},
..Default::default()
},
],
syscalls,
..Default::default()
}
.serialize_with_types(types)
}
@@ -63,10 +63,13 @@ impl TestProgram {
}
}
pub fn serialize(&self) -> Vec<u8> {
self.serialize_with_types(scalar_catalog())
}
pub fn serialize_with_types(&self, types: Vec<TypeDecl>) -> Vec<u8> {
let mut budget = LoadBudget::new(Default::default());
let registry = Registry::resolve(
RegistryDeclarations {
types: scalar_catalog(),
types,
functions: self.functions.clone(),
implementations: vec![],
},
@@ -1,3 +1,7 @@
mod reclamation;
pub use reclamation::AudioReclaimer;
use reclamation::{CallbackLane, Packet};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use prometeu_drivers::{AudioCommand, Channel, MAX_CHANNELS, OUTPUT_SAMPLE_RATE};
use prometeu_drivers::{AudioDelivery, AudioTransport};
@@ -10,9 +14,15 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};
trait AudioStreamHandle {}
trait AudioStreamHandle {
fn stop(&self) {}
}
impl AudioStreamHandle for cpal::Stream {}
impl AudioStreamHandle for cpal::Stream {
fn stop(&self) {
let _ = self.pause();
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HostAudioError {
@@ -97,6 +107,7 @@ pub struct HostAudio {
last_progress: Instant,
pub perf_consumer: Option<ringbuf::wrap::CachingCons<Arc<HeapRb<u64>>>>,
_stream: Option<Box<dyn AudioStreamHandle>>,
reclaimer: Option<AudioReclaimer>,
}
impl Default for HostAudio {
@@ -115,6 +126,7 @@ impl HostAudio {
last_progress: Instant::now(),
perf_consumer: None,
_stream: None,
reclaimer: None,
}
}
@@ -129,7 +141,7 @@ impl HostAudio {
self.last_heartbeat = 0;
self.last_progress = Instant::now();
self.perf_consumer = None;
self._stream = None;
self.stop_stream();
let device = runtime.default_output_device().ok_or(HostAudioError::NoOutputDevice)?;
@@ -143,7 +155,7 @@ impl HostAudio {
let failed = self.failed.clone();
let heartbeat = self.heartbeat.clone();
let mut mixer = AudioMixer::new();
let (mut callback, reclaimer) = AudioCallback::new();
// To pass performance data from the audio thread to the main thread
let audio_perf_rb = HeapRb::<u64>::new(64);
@@ -154,13 +166,9 @@ impl HostAudio {
&config,
move |data: &mut [f32]| {
heartbeat.fetch_add(1, Ordering::Relaxed);
if let Some(delivery) = transport.try_take() {
mixer.apply_delivery(delivery);
}
// Mixes audio
mixer.fill_buffer(data);
callback.render(&transport, data);
// Sends processing time in microseconds
let _ = perf_prod.try_push(mixer.last_processing_time.as_micros() as u64);
let _ = perf_prod.try_push(callback.mixer.last_processing_time.as_micros() as u64);
},
move |err| {
failed.store(true, Ordering::Relaxed);
@@ -169,6 +177,7 @@ impl HostAudio {
)?;
runtime.play_stream(&stream)?;
self.reclaimer = Some(reclaimer);
self.perf_consumer = Some(perf_cons);
self._stream = Some(Box::new(stream));
Ok(())
@@ -177,6 +186,9 @@ impl HostAudio {
/// Called even while guest execution is backpressured. A failed or stalled
/// device cannot retain the only publication slot indefinitely.
pub fn service(&mut self) {
if let Some(reclaimer) = &mut self.reclaimer {
reclaimer.service(&self.transport);
}
let heartbeat = self.heartbeat.load(Ordering::Relaxed);
if heartbeat != self.last_heartbeat {
self.last_heartbeat = heartbeat;
@@ -187,7 +199,7 @@ impl HostAudio {
|| self.last_progress.elapsed() >= Duration::from_secs(2))
{
eprintln!("[HostAudio] Output unavailable/stalled; consuming audio silently.");
self._stream = None;
self.stop_stream();
self.perf_consumer = None;
}
if self._stream.is_none() {
@@ -195,6 +207,16 @@ impl HostAudio {
}
}
fn stop_stream(&mut self) {
if let Some(stream) = self._stream.take() {
stream.stop();
drop(stream); // Stop/drop the callback before any silent consumer takes over.
}
if let Some(mut reclaimer) = self.reclaimer.take() {
reclaimer.service(&self.transport);
}
}
pub fn update_stats(&mut self, stats: &mut crate::stats::HostStats) {
if let Some(cons) = &mut self.perf_consumer {
while let Some(us) = cons.try_pop() {
@@ -204,6 +226,40 @@ impl HostAudio {
}
}
impl Drop for HostAudio {
fn drop(&mut self) {
self.stop_stream();
self.transport.consume_silently();
}
}
/// The same bounded runtime-owned data path used by CPAL and allocator tests.
pub struct AudioCallback {
mixer: AudioMixer,
lane: CallbackLane,
}
impl AudioCallback {
pub fn new() -> (Self, AudioReclaimer) {
let (lane, reclaimer) = reclamation::pool();
(Self { mixer: AudioMixer::new(), lane }, reclaimer)
}
pub fn render(&mut self, transport: &AudioTransport, data: &mut [f32]) {
let mut packet = self.lane.take();
if let Some(packet) = &mut packet {
// Reserve reclamation before acquiring any publication ownership.
self.mixer.retire_inactive(packet);
if let Some(delivery) = transport.try_take() {
self.mixer.apply_delivery_into(delivery, packet);
}
}
self.mixer.mix(data);
if let Some(mut packet) = packet {
self.mixer.retire_inactive(&mut packet);
self.lane.give_back(packet);
}
}
}
#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod tests {
@@ -347,27 +403,59 @@ impl AudioMixer {
}
}
pub fn apply_delivery(&mut self, delivery: AudioDelivery) {
fn apply_delivery_into(&mut self, delivery: AudioDelivery, packet: &mut Packet) {
if delivery.epoch < self.epoch {
packet.batches = [delivery.frame, delivery.lifecycle];
return;
}
if delivery.epoch != self.epoch {
self.voices = Default::default();
for voice in &mut self.voices {
packet.retire(voice.sample.take());
*voice = Channel::default();
}
self.epoch = delivery.epoch;
}
// A frame pending before a lifecycle hook is always applied first.
for batch in [delivery.frame, delivery.lifecycle].into_iter().flatten() {
for command in batch {
self.process_command(command);
for (slot, batch) in packet.batches.iter_mut().zip([delivery.frame, delivery.lifecycle]) {
*slot = batch;
}
// Move commands out of slots, keeping each vector allocation in its packet.
for index in 0..2 {
if let Some(mut batch) = packet.batches[index].take() {
for command in &mut batch {
let command =
std::mem::replace(command, AudioCommand::Stop { voice_id: MAX_CHANNELS });
self.process_command(command, packet);
}
packet.batches[index] = Some(batch);
}
}
self.paused = delivery.paused;
}
pub fn process_command(&mut self, cmd: AudioCommand) {
fn retire_inactive(&mut self, packet: &mut Packet) {
for voice in &mut self.voices {
if !voice.active {
packet.retire(voice.sample.take());
}
}
}
// Signal-only tests use synchronous reclamation outside a data callback.
#[cfg(test)]
fn apply_delivery(&mut self, delivery: AudioDelivery) {
self.apply_delivery_into(delivery, &mut Packet::new());
}
#[cfg(test)]
fn fill_buffer(&mut self, buffer: &mut [f32]) {
self.mix(buffer);
self.retire_inactive(&mut Packet::new());
}
fn process_command(&mut self, cmd: AudioCommand, packet: &mut Packet) {
match cmd {
AudioCommand::Play { sample, voice_id, volume, pan, pitch, priority, loop_mode } => {
if voice_id < MAX_CHANNELS {
packet.retire(self.voices[voice_id].sample.take());
self.voices[voice_id] = Channel {
sample: Some(sample),
active: true,
@@ -378,12 +466,14 @@ impl AudioMixer {
loop_mode,
priority,
};
} else {
packet.retire(Some(sample));
}
}
AudioCommand::Stop { voice_id } => {
if voice_id < MAX_CHANNELS {
self.voices[voice_id].active = false;
self.voices[voice_id].sample = None;
packet.retire(self.voices[voice_id].sample.take());
}
}
AudioCommand::SetVolume { voice_id, volume } => {
@@ -404,7 +494,7 @@ impl AudioMixer {
}
}
pub fn fill_buffer(&mut self, buffer: &mut [f32]) {
fn mix(&mut self, buffer: &mut [f32]) {
let start = std::time::Instant::now();
// Zeroes the buffer (stereo)
for sample in buffer.iter_mut() {
@@ -417,17 +507,19 @@ impl AudioMixer {
}
for voice in self.voices.iter_mut() {
if !voice.active {
continue;
}
let Some(sample) = voice.sample.as_ref() else {
continue;
};
let len = sample.data.len();
if len == 0 {
voice.active = false;
voice.sample = None;
continue;
}
let (loop_start, end) = match (sample.loop_start, sample.loop_end) {
(Some(start), Some(end)) if start < end && end as usize <= len => {
let (loop_start, end) = match (voice.loop_mode, sample.loop_start, sample.loop_end) {
(LoopMode::On, Some(start), Some(end)) if start < end && end as usize <= len => {
(start as f64, end as f64)
}
_ => (0.0, len as f64),
@@ -441,7 +533,6 @@ impl AudioMixer {
let pos = voice.pos as usize;
if pos >= len {
voice.active = false;
voice.sample = None;
break;
}
let s1 = sample.data[pos] as f32 / 32768.0;
@@ -460,7 +551,6 @@ impl AudioMixer {
if voice.pitch >= (end - voice.pos) / ratio {
if voice.loop_mode == LoopMode::Off {
voice.active = false;
voice.sample = None;
break;
}
let length = end - loop_start;
@@ -512,6 +602,168 @@ mod control_tests {
);
}
#[test]
fn one_shot_ignores_markers_and_loop_preserves_intro() {
for looping in [false, true] {
let (mut audio, mut mixer) = pair();
let sample =
Sample::new(48_000, vec![1024, 2048, 4096, 8192, 12288, 16384, 20480, 24576])
.with_loop(2, 4);
play(&mut audio, Arc::new(sample), 1.0, looping);
audio.finish_frame();
apply(&audio, &mut mixer);
let mut pcm = [0.0; 20];
mixer.fill_buffer(&mut pcm);
let expected = if looping {
[1024, 2048, 4096, 8192, 4096, 8192, 4096, 8192, 4096, 8192]
} else {
[1024, 2048, 4096, 8192, 12288, 16384, 20480, 24576, 0, 0]
};
for (frame, value) in pcm.as_chunks::<2>().0.iter().zip(expected) {
assert_eq!(*frame, [value as f32 / 32768.0, 0.0]);
}
assert_eq!(mixer.voices[0].active, looping);
assert_eq!(mixer.voices[0].sample.is_some(), looping);
}
}
#[test]
fn absent_partial_and_invalid_markers_fall_back_to_full_sample() {
for (start, end) in [
(None, None),
(Some(1), None),
(None, Some(2)),
(Some(2), Some(2)),
(Some(3), Some(1)),
(Some(0), Some(5)),
(Some(u32::MAX), Some(u32::MAX)),
(Some(0), Some(0)),
] {
for looping in [false, true] {
let (mut audio, mut mixer) = pair();
let mut sample = Sample::new(48_000, vec![4096, 8192, 12288, 16384]);
sample.loop_start = start;
sample.loop_end = end;
play(&mut audio, Arc::new(sample), 1.0, looping);
audio.finish_frame();
apply(&audio, &mut mixer);
let mut pcm = [0.0; 16];
mixer.fill_buffer(&mut pcm);
for (i, frame) in pcm.as_chunks::<2>().0.iter().enumerate() {
let value = if looping || i < 4 { (i % 4 + 1) as f32 / 8.0 } else { 0.0 };
assert_eq!(*frame, [value, 0.0], "markers {start:?}/{end:?}, loop {looping}");
}
assert_eq!(mixer.voices[0].active, looping);
}
}
}
#[test]
fn fractional_interpolation_uses_loop_seam_or_actual_tail() {
// Both rate conversion and fractional pitch produce a half-frame step.
for (rate, pitch) in [(48_000, 0.5), (24_000, 1.0), (96_000, 0.25)] {
for looping in [false, true] {
let (mut audio, mut mixer) = pair();
play(
&mut audio,
Arc::new(Sample::new(rate, vec![4096, 8192, 16384, 24576]).with_loop(1, 3)),
pitch,
looping,
);
audio.finish_frame();
apply(&audio, &mut mixer);
let mut pcm = [0.0; 18];
mixer.fill_buffer(&mut pcm);
let expected = if looping {
[0.125, 0.1875, 0.25, 0.375, 0.5, 0.375, 0.25, 0.375, 0.5]
} else {
[0.125, 0.1875, 0.25, 0.375, 0.5, 0.625, 0.75, 0.375, 0.0]
};
for (frame, value) in pcm.as_chunks::<2>().0.iter().zip(expected) {
assert!((frame[0] - value).abs() <= 1e-6);
assert_eq!(frame[1], 0.0);
}
assert_eq!(mixer.voices[0].active, looping);
}
}
}
#[test]
fn empty_samples_terminate_without_indexing() {
for looping in [false, true] {
for marked in [false, true] {
let (mut audio, mut mixer) = pair();
let sample = Sample::new(48_000, vec![]);
let sample = if marked { sample.with_loop(0, 1) } else { sample };
play(&mut audio, Arc::new(sample), f64::MAX, looping);
audio.finish_frame();
apply(&audio, &mut mixer);
let mut pcm = [1.0; 8];
mixer.fill_buffer(&mut pcm);
assert_eq!(pcm, [0.0; 8]);
assert!(!mixer.voices[0].active);
assert!(mixer.voices[0].sample.is_none());
}
}
}
#[test]
fn multiple_wrap_pitch_preserves_selected_interval_and_signal() {
for (rate, pitch) in [(48_000, 5.0), (96_000, 2.5)] {
let (mut audio, mut mixer) = pair();
play(
&mut audio,
Arc::new(Sample::new(rate, vec![4096, 8192, 16384, 24576]).with_loop(1, 3)),
pitch,
true,
);
audio.finish_frame();
apply(&audio, &mut mixer);
let mut pcm = [0.0; 10];
mixer.fill_buffer(&mut pcm);
assert_eq!(pcm, [0.125, 0.0, 0.25, 0.0, 0.5, 0.0, 0.25, 0.0, 0.5, 0.0]);
assert_eq!(mixer.voices[0].pos, 1.0);
}
}
#[test]
fn extreme_pitch_and_source_rates_keep_positions_finite() {
for rate in [0, 1, 44_100, 48_000, 96_000, u32::MAX] {
for looping in [false, true] {
let (mut audio, mut mixer) = pair();
play(
&mut audio,
Arc::new(Sample::new(rate, vec![4096, 8192, 16384, 24576]).with_loop(1, 3)),
f64::MAX,
looping,
);
audio.finish_frame();
apply(&audio, &mut mixer);
let mut pcm = [0.0; 128];
mixer.fill_buffer(&mut pcm);
assert!(pcm.iter().all(|value| value.is_finite()));
assert!(mixer.voices[0].pos.is_finite());
if rate == 0 {
assert_eq!(mixer.voices[0].pos, 0.0);
assert!(pcm.as_chunks::<2>().0.iter().all(|frame| *frame == [0.125, 0.0]));
} else if looping {
assert!((1.0..3.0).contains(&mixer.voices[0].pos));
assert!(
pcm.as_chunks::<2>()
.0
.iter()
.skip(1)
.all(|frame| (0.25..=0.5).contains(&frame[0]))
);
} else {
assert_eq!(&pcm[..2], &[0.125, 0.0]);
assert!(pcm[2..].iter().all(|value| *value == 0.0));
}
assert_eq!(mixer.voices[0].active, looping || rate == 0);
}
}
}
#[test]
fn pcm_controls_preserve_position_and_stop_is_idempotent() {
let (mut audio, mut mixer) = pair();
@@ -632,7 +884,7 @@ mod control_tests {
let mut pcm = [0.0; 128];
mixer.fill_buffer(&mut pcm);
assert!(pcm.iter().all(|v| v.is_finite()));
assert!(pcm.chunks_exact(2).all(|frame| frame == [0.5, 0.0]));
assert!(pcm.as_chunks::<2>().0.iter().all(|frame| *frame == [0.5, 0.0]));
assert!(mixer.voices[0].active);
assert!(mixer.voices[0].pos >= 1.0 && mixer.voices[0].pos < 3.0);
}
@@ -705,6 +957,7 @@ mod control_tests {
audio.finish_frame();
let deadline = Instant::now() + Duration::from_secs(2);
while !audio.ready() && Instant::now() < deadline {
host.service();
std::thread::sleep(Duration::from_millis(10));
}
assert!(audio.ready());
@@ -713,12 +966,119 @@ mod control_tests {
audio.set_pan(0, 255);
audio.set_pitch(0, 1.5);
audio.finish_frame();
std::thread::sleep(Duration::from_millis(100));
for _ in 0..10 {
host.service();
std::thread::sleep(Duration::from_millis(10));
}
assert!(audio.ready());
audio.stop(0);
audio.finish_frame();
std::thread::sleep(Duration::from_millis(100));
for _ in 0..10 {
host.service();
std::thread::sleep(Duration::from_millis(10));
}
assert!(audio.ready());
assert!(!host.failed.load(Ordering::Relaxed));
}
}
#[cfg(test)]
mod reclamation_tests {
use super::*;
use prometeu_drivers::{Audio, MemoryBanks};
use prometeu_hal::{AudioBridge, sample::Sample};
#[test]
fn stale_delivery_moves_whole_vectors_to_service_without_changing_playback() {
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let sample = Arc::new(Sample::new(48000, vec![1000; 16]));
let weak = Arc::downgrade(&sample);
audio.play_sample(sample, 0, 255, 0, 1.0, 0, LoopMode::On);
audio.finish_frame();
let stale = audio.transport().try_take().unwrap();
let allocation = stale.frame.as_ref().unwrap().as_ptr();
audio.reset();
let mut mixer = AudioMixer::new();
mixer.apply_delivery(audio.transport().try_take().unwrap());
let mut packet = Packet::new();
mixer.apply_delivery_into(stale, &mut packet);
assert_eq!(packet.batches[0].as_ref().unwrap().as_ptr(), allocation);
assert!(weak.upgrade().is_some());
mixer.mix(&mut [0.0; 32]);
assert!(mixer.voices.iter().all(|voice| !voice.active));
drop(packet); // Main/service thread, including stale command owners.
assert!(weak.upgrade().is_none());
}
type Callback = Box<dyn FnMut(&mut [f32]) + Send>;
struct ManualStream(Arc<std::sync::Mutex<Option<Callback>>>);
impl AudioStreamHandle for ManualStream {}
impl Drop for ManualStream {
fn drop(&mut self) {
self.0.lock().unwrap().take();
}
}
struct ManualRuntime(Arc<std::sync::Mutex<Option<Callback>>>);
impl AudioRuntime for ManualRuntime {
type Device = ();
type Stream = ManualStream;
fn default_output_device(&self) -> Option<()> {
Some(())
}
fn build_output_stream<D, E>(
&self,
_: &(),
_: &cpal::StreamConfig,
data: D,
_: E,
) -> Result<ManualStream, HostAudioError>
where
D: FnMut(&mut [f32]) + Send + 'static,
E: FnMut(String) + Send + 'static,
{
*self.0.lock().unwrap() = Some(Box::new(data));
Ok(ManualStream(self.0.clone()))
}
fn play_stream(&self, _: &ManualStream) -> Result<(), HostAudioError> {
Ok(())
}
}
#[test]
fn failure_stall_and_teardown_release_callback_and_pending_owners() {
for mode in 0..3 {
let callbacks = Arc::new(std::sync::Mutex::new(None));
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let mut host = HostAudio::new();
host.transport = audio.transport();
host.init_with_runtime(&ManualRuntime(callbacks.clone())).unwrap();
let mut owners = vec![];
for _ in 0..2 {
let sample = Arc::new(Sample::new(48000, vec![1000; 16]));
owners.push(Arc::downgrade(&sample));
audio.play_sample(sample, 0, 255, 0, 1.0, 0, LoopMode::On);
audio.finish_frame();
callbacks.lock().unwrap().as_mut().unwrap()(&mut []);
}
let sample = Arc::new(Sample::new(48000, vec![1000; 16]));
owners.push(Arc::downgrade(&sample));
audio.play_sample(sample, 0, 255, 0, 1.0, 0, LoopMode::On);
audio.finish_frame();
assert!(!audio.ready());
if mode == 0 {
host.failed.store(true, Ordering::Relaxed);
}
if mode == 1 {
host.last_progress = Instant::now() - Duration::from_secs(3);
host.last_heartbeat = host.heartbeat.load(Ordering::Relaxed);
}
if mode < 2 {
host.service();
assert!(host._stream.is_none());
}
drop(host);
assert!(audio.ready());
assert!(callbacks.lock().unwrap().is_none());
assert!(owners.iter().all(|owner| owner.upgrade().is_none()));
}
}
}
@@ -0,0 +1,100 @@
//! Two packets circulate free -> callback -> returned -> service -> free.
//! While the callback owns a packet, at most one packet can occupy the return
//! lane, so its capacity-two enqueue cannot fail. Neither lane grows.
use prometeu_drivers::{AudioCommand, AudioTransport, MAX_CHANNELS, MAX_COMMANDS_PER_FRAME};
use prometeu_hal::sample::Sample;
use ringbuf::{
HeapRb,
traits::{Consumer, Producer, Split},
wrap::{CachingCons, CachingProd},
};
use std::sync::Arc;
pub(super) const RETIRED_CAPACITY: usize = 3 * MAX_COMMANDS_PER_FRAME + MAX_CHANNELS;
pub(super) struct Packet {
retired: Vec<Arc<Sample>>,
pub batches: [Option<Vec<AudioCommand>>; 2],
}
impl Packet {
pub(super) fn new() -> Self {
Self { retired: Vec::with_capacity(RETIRED_CAPACITY), batches: [None, None] }
}
pub(super) fn retire(&mut self, sample: Option<Arc<Sample>>) {
if let Some(sample) = sample {
// Every owner comes from at most 3072 Play commands or 16 slots.
assert!(self.retired.len() < RETIRED_CAPACITY, "audio retirement bound");
self.retired.push(sample);
}
}
fn recycle(&mut self, transport: &AudioTransport) {
self.retired.clear();
for batch in &mut self.batches {
if let Some(batch) = batch.take() {
transport.recycle_commands(batch);
}
}
}
}
type Prod = CachingProd<Arc<HeapRb<Packet>>>;
type Cons = CachingCons<Arc<HeapRb<Packet>>>;
pub(super) struct CallbackLane {
free: Cons,
returned: Prod,
}
/// Main-thread endpoint. Service independently of guest progress or pause.
pub struct AudioReclaimer {
free: Prod,
returned: Cons,
}
pub(super) fn pool() -> (CallbackLane, AudioReclaimer) {
let (mut free, free_cons) = HeapRb::new(2).split();
let (returned, returned_cons) = HeapRb::new(2).split();
for _ in 0..2 {
assert!(free.try_push(Packet::new()).is_ok());
}
(CallbackLane { free: free_cons, returned }, AudioReclaimer { free, returned: returned_cons })
}
impl CallbackLane {
pub(super) fn take(&mut self) -> Option<Packet> {
self.free.try_pop()
}
pub(super) fn give_back(&mut self, packet: Packet) {
// Conservation proves capacity; there is no overflow/drop fallback.
assert!(self.returned.try_push(packet).is_ok(), "audio packet conservation");
}
}
impl AudioReclaimer {
pub fn service(&mut self, transport: &AudioTransport) {
while let Some(mut packet) = self.returned.try_pop() {
packet.recycle(transport);
assert!(self.free.try_push(packet).is_ok(), "audio packet conservation");
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn two_packets_conserve_capacity_across_service_starvation() {
let (mut callback, mut service) = pool();
let first = callback.take().unwrap();
let second = callback.take().unwrap();
assert!(callback.take().is_none());
callback.give_back(first);
callback.give_back(second);
assert!(callback.take().is_none());
let transport = AudioTransport::default();
for _ in 0..1000 {
service.service(&transport);
for _ in 0..2 {
let packet = callback.take().unwrap();
assert_eq!(packet.retired.capacity(), RETIRED_CAPACITY);
assert!(packet.retired.is_empty());
assert!(packet.batches.iter().all(Option::is_none));
callback.give_back(packet);
}
assert!(callback.take().is_none());
}
}
}
@@ -6,6 +6,7 @@ pub mod input;
pub mod log_sink;
pub mod runner;
pub mod stats;
pub mod studio;
pub mod utilities;
pub mod wall_clock;
@@ -18,6 +19,9 @@ use winit::event_loop::EventLoop;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Run an interactive Studio session without creating a desktop window.
#[arg(long, requires = "run", conflicts_with = "debug")]
studio: bool,
/// Run a cartridge
#[arg(long, value_name = "PATH", conflicts_with = "debug")]
run: Option<String>,
@@ -54,6 +58,23 @@ struct Cli {
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
let cli = Cli::parse();
if cli.studio {
let bootstrap = studio::wire::Bootstrap::read(std::io::stdin().lock())?;
let mut runner =
HostRunner::new(cli.fs_root, cli.cap.as_ref().and_then(|path| load_cap_config(path)));
if let Some(path) = cli.memcard {
runner
.install_memcard(path, cli.create_memcard)
.map_err(|e| format!("Cannot install memory card: {e:?}"))?;
}
runner.set_boot_target(BootTarget::Cartridge {
path: cli.run.expect("--studio requires --run"),
debug: false,
debug_port: 0,
});
return studio::run(runner, bootstrap).map_err(Into::into);
}
let fs_root = cli.fs_root;
let cap_config = cli.cap.as_ref().and_then(|path| load_cap_config(path));
@@ -151,6 +151,7 @@ pub struct HostRunner {
render_frame_store: LatestRenderFrameStore,
/// Tracks whether a new logical frame or host-only surface invalidation requires presentation.
presentation: PresentationState,
studio_capture: bool,
}
impl HostRunner {
@@ -238,9 +239,161 @@ impl HostRunner {
audio: HostAudio::new(),
render_frame_store,
presentation: PresentationState::default(),
studio_capture: false,
}
}
/// One canonical interactive slice, shared by desktop and Studio pacing.
fn tick_machine(&mut self) {
self.audio.service();
if !self.debugger.waiting_for_start {
if self.studio_capture {
let mut platform = crate::studio::platform::StudioPlatform {
hardware: &mut self.hardware,
store: &self.render_frame_store,
};
self.firmware.tick(&self.input.signals, &mut platform);
} else {
self.firmware.tick(&self.input.signals, &mut self.hardware);
}
self.stats.record_host_cpu_time(self.firmware.os.vm().last_frame_cpu_time_us());
}
self.hardware.audio.set_paused(
self.firmware.game_lifecycle_audio_paused()
|| self.firmware.os.vm().paused()
|| self.debugger.waiting_for_start,
);
self.audio.service();
}
pub(crate) fn studio_audio_init(&mut self) {
if let Err(error) = self.audio.init(self.hardware.audio.transport()) {
eprintln!("[HostAudio] Disabled: {error}");
}
}
pub(crate) fn studio_boot_step(&mut self, disposable: bool) -> Result<bool, String> {
self.studio_capture = true;
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
return Ok(true);
}
if matches!(self.firmware.state, FirmwareState::AppCrashes(_)) {
return Err("Cartridge failed during Studio boot".into());
}
self.firmware.os.memcard().poll(Instant::now());
if let FirmwareState::LoadCartridge(step) = &mut self.firmware.state
&& step.needs_memcard_choice()
&& !self.firmware.os.memcard().preparation_active()
{
let choice = if disposable {
prometeu_system::memcard::admission::Choice::MemoryFresh
} else {
prometeu_system::memcard::admission::Choice::Persistent
};
step.choose_memcard(
&mut self.firmware.os,
choice,
self.hardware.wall_clock().utc_unix_seconds(),
)
.map_err(|e| format!("Studio save selection failed: {e:?}"))?;
}
self.tick_machine();
self.studio_logs();
Ok(matches!(self.firmware.state, FirmwareState::GameRunning(_)))
}
pub(crate) fn studio_step(
&mut self,
signals: prometeu_hal::InputSignals,
) -> Result<(), String> {
self.input.signals = signals;
self.tick_machine();
self.studio_logs();
if matches!(self.firmware.state, FirmwareState::AppCrashes(_)) {
return Err("Cartridge faulted during Studio Play".into());
}
Ok(())
}
pub(crate) fn studio_pause(&mut self, paused: bool) {
self.input.clear_guest_signals();
self.firmware.os.vm().set_paused(paused);
self.hardware.audio.set_paused(paused);
self.audio.service();
}
pub(crate) fn studio_dimensions(&self) -> (usize, usize) {
self.hardware.display_size()
}
pub(crate) fn studio_tick_index(&mut self) -> u64 {
self.firmware.os.frame_index()
}
pub(crate) fn studio_frame(
&mut self,
previous: Option<(RenderOwnership, prometeu_hal::FrameId)>,
) -> Option<OwnedRgba8888Frame> {
let active = self.firmware.os.vm().active_render_ownership();
self.render_frame_store
.latest_frame_after(previous)
.filter(|frame| should_present_worker_frame(&self.firmware.state, frame, active))
}
fn studio_logs(&mut self) {
let last = self.log_sink.last_seq();
let events = match last {
Some(seq) => self.firmware.os.logs_after(seq),
None => self.firmware.os.recent_logs(4096),
};
self.log_sink.process_events(events);
}
pub(crate) fn studio_stop(&mut self, disposable: bool) -> Result<(), String> {
self.studio_pause(true);
let deadline = Instant::now() + Duration::from_secs(2);
loop {
let service = self.firmware.os.memcard();
service.poll(Instant::now());
if !service.session.io_active()
&& !service.session.has_deferred_write()
&& !service.preparation_active()
{
break;
}
if Instant::now() >= deadline {
return Err("Save IO has not finished; retry Stop".into());
}
let lane = service.lane.clone();
lane.wait_for_progress(lane.progress_stamp(), Duration::from_millis(5));
}
if self.firmware.os.memcard().session.unsaved() && !disposable {
return Err("Unsaved game data; resume and save before stopping".into());
}
self.firmware
.os
.stop_background_io()
.map_err(|e| format!("Save shutdown failed: {e:?}"))?;
self.firmware.os.memcard().session.end_session();
self.firmware.os.memcard().poll(Instant::now());
self.firmware
.os
.memcard()
.uninstall_card()
.map_err(|e| format!("Save detach failed: {e:?}"))?;
self.firmware
.os
.vm()
.stop_render_worker()
.map_err(|e| format!("Render shutdown failed: {e:?}"))?;
Ok(())
}
pub(crate) fn studio_abort(&mut self) {
self.studio_pause(true);
let _ = self.firmware.os.vm().stop_render_worker();
let _ = self.firmware.os.stop_background_io();
}
fn window(&self) -> &'static Window {
self.window.expect("window not created yet")
}
@@ -433,17 +586,9 @@ impl ApplicationHandler for HostRunner {
// Consume time in exact 60Hz slices.
while self.accumulator >= self.frame_target_dt {
// Unless the debugger is waiting for a 'start' command, advance the system.
if !self.debugger.waiting_for_start {
self.firmware.tick(&self.input.signals, &mut self.hardware);
self.stats.record_host_cpu_time(self.firmware.os.vm().last_frame_cpu_time_us());
}
let is_paused = self.firmware.game_lifecycle_audio_paused()
|| self.firmware.os.vm().paused()
|| self.debugger.waiting_for_start;
self.hardware.audio.set_paused(is_paused);
self.audio.service();
// Reclaim returned audio owners before another guest attempt, even
// when the previous attempt paused or stopped at backpressure.
self.tick_machine();
self.accumulator -= self.frame_target_dt;
self.stats.record_frame();
@@ -497,6 +642,73 @@ impl ApplicationHandler for HostRunner {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn studio_can_boot_and_publish_without_a_window() {
let cart = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../test-cartridges/dummy-boy");
let mut host = HostRunner::new(None, None);
host.set_boot_target(BootTarget::Cartridge {
path: cart.to_string_lossy().into_owned(),
debug: false,
debug_port: 0,
});
let deadline = Instant::now() + Duration::from_secs(5);
while !host.studio_boot_step(true).unwrap() {
assert!(Instant::now() < deadline);
std::thread::sleep(Duration::from_millis(1));
}
host.studio_pause(true);
host.studio_pause(false);
for _ in 0..60 {
host.studio_step(Default::default()).unwrap();
std::thread::sleep(Duration::from_millis(2));
}
let frame = host.studio_frame(None);
let published = host.render_frame_store.latest_frame().map(|f| (f.frame_id, f.ownership));
let active = host.firmware.os.vm().active_render_ownership();
assert!(
frame.is_some(),
"store {published:?}, owner {active:?}, state {:?}, frame {}",
host.firmware.state,
host.firmware.os.frame_index()
);
let frame = frame.unwrap();
assert!(host.studio_frame(Some((frame.ownership, frame.frame_id))).is_none());
// Exercise the same audio transport/callback through Studio lifecycle,
// without requiring a physical output device in this contract test.
use prometeu_hal::{AudioBridge, LoopMode, sample::Sample};
let (mut callback, mut reclaimer) = crate::audio::AudioCallback::new();
assert_eq!(
host.hardware.audio.play_sample(
Arc::new(Sample::new(48000, vec![1000; 32])),
0,
255,
0,
1.0,
0,
LoopMode::On,
),
prometeu_hal::AudioOpStatus::Ok
);
host.hardware.audio.finish_frame();
let transport = host.hardware.audio.transport();
let mut pcm = [0.0; 64];
callback.render(&transport, &mut pcm);
assert!(pcm.iter().any(|sample| *sample != 0.0));
reclaimer.service(&transport);
host.studio_pause(true);
callback.render(&transport, &mut pcm);
assert!(pcm.iter().all(|sample| *sample == 0.0));
reclaimer.service(&transport);
host.studio_pause(false);
callback.render(&transport, &mut pcm);
assert!(pcm.iter().any(|sample| *sample != 0.0));
reclaimer.service(&transport);
host.studio_stop(true).unwrap();
callback.render(&transport, &mut pcm);
assert!(pcm.iter().all(|sample| *sample == 0.0));
}
use prometeu_firmware::BootTarget;
use prometeu_firmware::firmware::firmware_state::{
GameRunningStep, HubHomeStep, ShellRunningStep,
@@ -834,7 +1046,7 @@ mod tests {
// real socket bind/connect coordination. Deterministic debugger state changes
// are covered below the host layer in `debugger.rs`.
#[test]
#[ignore = "requires localhost TCP bind/connect; run via `cargo test -p prometeu-host-desktop-winit --lib -- --ignored`"]
#[ignore = "requires localhost TCP bind/connect; run via `make test-debugger-socket`"]
fn test_debug_port_opens() {
let mut runner = HostRunner::new(None, None);
let port = 9999;
@@ -894,7 +1106,7 @@ mod tests {
}
#[test]
#[ignore = "requires localhost TCP bind/connect; run via `cargo test -p prometeu-host-desktop-winit --lib -- --ignored`"]
#[ignore = "requires localhost TCP bind/connect; run via `make test-debugger-socket`"]
fn test_debug_reconnection() {
let mut runner = HostRunner::new(None, None);
let port = 9998;
@@ -937,7 +1149,7 @@ mod tests {
}
#[test]
#[ignore = "requires localhost TCP bind/connect; run via `cargo test -p prometeu-host-desktop-winit --lib -- --ignored`"]
#[ignore = "requires localhost TCP bind/connect; run via `make test-debugger-socket`"]
fn test_debug_refuse_second_connection() {
let mut runner = HostRunner::new(None, None);
let port = 9997;
@@ -973,7 +1185,7 @@ mod tests {
}
#[test]
#[ignore = "requires localhost TCP bind/connect; run via `cargo test -p prometeu-host-desktop-winit --lib -- --ignored`"]
#[ignore = "requires localhost TCP bind/connect; run via `make test-debugger-socket`"]
fn test_get_state_returns_response() {
let mut runner = HostRunner::new(None, None);
let port = 9996;
@@ -1018,7 +1230,7 @@ mod tests {
}
#[test]
#[ignore = "requires localhost TCP bind/connect; run via `cargo test -p prometeu-host-desktop-winit --lib -- --ignored`"]
#[ignore = "requires localhost TCP bind/connect; run via `make test-debugger-socket`"]
fn test_debug_resume_on_disconnect() {
let mut runner = HostRunner::new(None, None);
let port = 9995;
@@ -0,0 +1,255 @@
//! Interactive Play integration, separate from the automation host.
pub(crate) mod platform;
mod transport;
pub mod wire;
use crate::runner::HostRunner;
use prometeu_hal::InputSignals;
use std::{
io,
time::{Duration, Instant},
};
use transport::Transport;
use wire::*;
fn number64(p: &[u8]) -> u64 {
u64::from_le_bytes(p.try_into().unwrap())
}
fn result(
transport: &Transport,
command: u16,
request: u64,
status: u16,
epoch: u64,
tick: u64,
text: &str,
) -> io::Result<()> {
let mut p = Vec::with_capacity(20 + text.len());
p.extend_from_slice(&command.to_le_bytes());
p.extend_from_slice(&status.to_le_bytes());
p.extend_from_slice(&epoch.to_le_bytes());
p.extend_from_slice(&tick.to_le_bytes());
p.extend_from_slice(text.as_bytes());
transport.send(RESULT, request, p)
}
fn input(p: &[u8], width: usize, height: usize) -> io::Result<(u64, InputSignals)> {
if p.len() != 20 {
return Err(invalid("Invalid Studio input length"));
}
let mask = u16::from_le_bytes(p[8..10].try_into().unwrap());
if mask & !0x1fff != 0 || p[10..12] != [0, 0] {
return Err(invalid("Invalid Studio input bits"));
}
let x = i32::from_le_bytes(p[12..16].try_into().unwrap());
let y = i32::from_le_bytes(p[16..20].try_into().unwrap());
if mask & 0x1000 != 0 && (x < 0 || y < 0 || x as usize >= width || y as usize >= height) {
return Err(invalid("Studio touch outside display"));
}
let bit = |n: u16| mask & (1u16 << n) != 0;
Ok((
number64(&p[..8]),
InputSignals {
up_signal: bit(0),
down_signal: bit(1),
left_signal: bit(2),
right_signal: bit(3),
a_signal: bit(4),
b_signal: bit(5),
x_signal: bit(6),
y_signal: bit(7),
l_signal: bit(8),
r_signal: bit(9),
start_signal: bit(10),
select_signal: bit(11),
f_signal: bit(12),
x_pos: x,
y_pos: y,
},
))
}
pub(crate) fn run(mut runner: HostRunner, boot: Bootstrap) -> io::Result<()> {
let transport = Transport::connect(&boot)?;
runner.studio_audio_init();
let (width, height) = runner.studio_dimensions();
frame_bytes(width as u32, height as u32, width as u32)?;
let mut ready = false;
let mut started = false;
let mut state = 0u32;
let mut epoch = 1u64;
let mut signals = InputSignals::default();
let mut next_tick = Instant::now();
let boot_deadline = Instant::now() + Duration::from_secs(10);
let slice = Duration::from_nanos(1_000_000_000 / 60);
let mut last_request = 0;
let mut last_frame = None;
let outcome = (|| -> io::Result<()> {
loop {
if let Some(error) = transport.failure() {
return Err(invalid(&error));
}
if ready && transport.idle() > Duration::from_secs(5) {
return Err(invalid("Studio control heartbeat expired"));
}
for _ in 0..64 {
let Some(message) = transport.next() else {
break;
};
let h = message.header;
if h.kind == INPUT {
let (received, input) = input(&message.payload, width, height)?;
if received == epoch && state == 1 {
signals = input;
}
continue;
}
if h.kind == PING {
if !message.payload.is_empty() {
return Err(invalid("Invalid PING payload"));
}
let mut p = Vec::with_capacity(20);
p.extend_from_slice(&epoch.to_le_bytes());
p.extend_from_slice(&runner.studio_tick_index().to_le_bytes());
p.extend_from_slice(&state.to_le_bytes());
transport.send(PONG, h.request, p)?;
continue;
}
if !matches!(h.kind, START | PAUSE | RESUME | STOP)
|| !message.payload.is_empty()
|| h.request == 0
{
return Err(invalid("Invalid Studio control command"));
}
if h.request <= last_request {
result(
&transport,
h.kind,
h.request,
1,
epoch,
runner.studio_tick_index(),
"Stale or duplicate request",
)?;
continue;
}
last_request = h.request;
if h.kind == STOP {
signals = InputSignals::default();
state = 2;
epoch = epoch.checked_add(1).ok_or_else(|| invalid("epoch exhausted"))?;
match runner.studio_stop(boot.disposable) {
Ok(()) => {
result(
&transport,
STOP,
h.request,
0,
epoch,
runner.studio_tick_index(),
"",
)?;
transport.finish(h.request)?;
return Ok(());
}
Err(error) => {
result(
&transport,
STOP,
h.request,
2,
epoch,
runner.studio_tick_index(),
&error,
)?;
continue;
}
}
}
if !ready {
result(
&transport,
h.kind,
h.request,
1,
epoch,
runner.studio_tick_index(),
"Cartridge is not ready",
)?;
continue;
}
match h.kind {
START if state == 0 => {
started = true;
state = 1;
runner.studio_pause(false);
next_tick = Instant::now();
}
START if state == 2 && !started => {
started = true;
}
PAUSE if state == 1 || state == 0 => {
state = 2;
epoch += 1;
signals = InputSignals::default();
runner.studio_pause(true);
}
RESUME if state == 2 && started => {
state = 1;
epoch += 1;
signals = InputSignals::default();
runner.studio_pause(false);
next_tick = Instant::now();
}
PAUSE if state == 2 => {}
RESUME if state == 1 => {}
_ => {
result(
&transport,
h.kind,
h.request,
1,
epoch,
runner.studio_tick_index(),
"Invalid execution state",
)?;
continue;
}
}
result(&transport, h.kind, h.request, 0, epoch, runner.studio_tick_index(), "")?;
}
if !ready {
if Instant::now() > boot_deadline {
return Err(invalid("Studio cartridge boot timed out"));
}
ready = runner.studio_boot_step(boot.disposable).map_err(|e| invalid(&e))?;
if ready {
runner.studio_pause(true);
let mut p = Vec::new();
for n in [width as u32, height as u32, 60, RGBA8] {
p.extend_from_slice(&n.to_le_bytes());
}
p.extend_from_slice(env!("CARGO_PKG_VERSION").as_bytes());
transport.send(READY, 0, p)?;
}
} else if state == 1 && Instant::now() >= next_tick {
runner.studio_step(signals).map_err(|e| invalid(&e))?;
next_tick += slice;
if next_tick + Duration::from_millis(100) < Instant::now() {
next_tick = Instant::now();
}
}
// Completion is asynchronous and can arrive after Pause stopped logical ticks.
if ready && let Some(frame) = runner.studio_frame(last_frame) {
last_frame = Some((frame.ownership, frame.frame_id));
transport.publish(epoch, frame);
}
std::thread::sleep(Duration::from_millis(1));
}
})();
if let Err(error) = &outcome {
eprintln!("[Studio] {error}");
let _ = transport.send(ERROR, 0, error.to_string().into_bytes());
runner.studio_abort();
}
outcome
}
@@ -0,0 +1,69 @@
//! Observe complete synchronous submissions without changing machine/render semantics.
use prometeu_drivers::hardware::Hardware;
use prometeu_hal::*;
use prometeu_system::LatestRenderFrameStore;
pub(crate) struct StudioPlatform<'a> {
pub hardware: &'a mut Hardware,
pub store: &'a LatestRenderFrameStore,
}
impl RenderSubmissionSink for StudioPlatform<'_> {
fn submit_render_submission(
&mut self,
submission: RenderSubmission,
) -> Result<(), RenderSubmitError> {
let marker = (submission.frame_id, submission.ownership);
self.hardware.submit_render_submission(submission)?;
if marker.1.app_mode == app_mode::AppMode::Game {
let (w, h) = self.hardware.display_size();
let frame = OwnedRgba8888Frame::packed(
marker.0,
marker.1,
w,
h,
self.hardware.gfx.front_buffer().to_vec(),
)
.map_err(|_| RenderSubmitError::SubmissionRejected)?;
self.store.publish_frame(frame);
}
Ok(())
}
}
impl RuntimePlatform for StudioPlatform<'_> {
fn wall_clock(&self) -> &dyn WallClock {
self.hardware.wall_clock()
}
fn display_size(&self) -> (usize, usize) {
self.hardware.display_size()
}
fn render_submission_sink(&mut self) -> &mut dyn RenderSubmissionSink {
self
}
fn render_backend(&mut self) -> &mut dyn RenderBackend {
self.hardware
}
fn game2d_frame_composer(&mut self) -> &mut dyn Game2DFrameComposer {
self.hardware
}
fn audio(&self) -> &dyn AudioBridge {
self.hardware.audio()
}
fn audio_mut(&mut self) -> &mut dyn AudioBridge {
self.hardware.audio_mut()
}
fn input(&self) -> &dyn InputPlatform {
self.hardware.input()
}
fn input_mut(&mut self) -> &mut dyn InputPlatform {
self.hardware.input_mut()
}
fn assets(&self) -> &dyn AssetBridge {
self.hardware.assets()
}
fn assets_mut(&mut self) -> &mut dyn AssetBridge {
self.hardware.assets_mut()
}
fn telemetry(&self) -> &dyn TelemetryPlatform {
self.hardware.telemetry()
}
}
@@ -0,0 +1,260 @@
use super::wire::*;
use prometeu_hal::OwnedRgba8888Frame;
use std::io::{self, Read};
use std::net::{Ipv4Addr, Shutdown, SocketAddr, TcpStream};
use std::sync::{
Arc, Condvar, Mutex,
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver, SyncSender},
};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
struct Publication {
epoch: u64,
frame: Arc<OwnedRgba8888Frame>,
}
#[derive(Default)]
struct Video {
latest: Option<Publication>,
flight: Option<(u64, u64)>,
}
struct Shared {
video: Mutex<Video>,
wake: Condvar,
closed: AtomicBool,
failure: Mutex<Option<String>>,
activity: Mutex<Instant>,
}
impl Shared {
fn fail(&self, message: impl ToString) {
if self.closed.load(Ordering::Acquire) {
return;
}
let mut failure = self.failure.lock().unwrap();
if failure.is_none() {
*failure = Some(message.to_string());
}
self.wake.notify_all();
}
fn stopped(&self) -> bool {
self.closed.load(Ordering::Acquire) || self.failure.lock().unwrap().is_some()
}
}
struct Outbound {
kind: u16,
request: u64,
payload: Vec<u8>,
flushed: Option<SyncSender<()>>,
}
pub(super) struct Transport {
shared: Arc<Shared>,
outgoing: SyncSender<Outbound>,
incoming: Receiver<Message>,
control: TcpStream,
video: TcpStream,
workers: Vec<JoinHandle<()>>,
}
impl Transport {
pub fn connect(boot: &Bootstrap) -> io::Result<Self> {
fn connect(port: u16) -> io::Result<TcpStream> {
let socket = TcpStream::connect_timeout(
&SocketAddr::from((Ipv4Addr::LOCALHOST, port)),
Duration::from_secs(5),
)?;
socket.set_nodelay(true)?;
socket.set_write_timeout(Some(Duration::from_secs(2)))?;
Ok(socket)
}
let control = connect(boot.control_port)?;
let video = connect(boot.video_port)?;
let mut writer = Writer::new(control.try_clone()?);
writer.send(HELLO, 0, &boot.hello(1))?;
let mut video_writer = Writer::new(video.try_clone()?);
video_writer.send(HELLO, 0, &boot.hello(2))?;
let shared = Arc::new(Shared {
video: Mutex::new(Video::default()),
wake: Condvar::new(),
closed: AtomicBool::new(false),
failure: Mutex::new(None),
activity: Mutex::new(Instant::now()),
});
let (tx, rx) = mpsc::sync_channel::<Outbound>(256);
let (commands, incoming) = mpsc::sync_channel(256);
let mut workers = Vec::new();
let state = shared.clone();
workers.push(std::thread::Builder::new().name("studio-control-write".into()).spawn(
move || {
while !state.stopped() {
let out = match rx.recv_timeout(Duration::from_millis(100)) {
Ok(out) => out,
Err(mpsc::RecvTimeoutError::Timeout) => continue,
Err(_) => break,
};
if let Err(error) = writer.send(out.kind, out.request, &out.payload) {
state.fail(error);
break;
}
if let Some(done) = out.flushed {
let _ = done.send(());
}
}
},
)?);
let state = shared.clone();
let socket = control.try_clone()?;
workers.push(std::thread::Builder::new().name("studio-control-read".into()).spawn(
move || {
let mut reader = Reader::new(socket);
while !state.stopped() {
let message = match reader.receive_control() {
Ok(m) => m,
Err(e) => {
state.fail(format!("Studio control lost: {e}"));
break;
}
};
*state.activity.lock().unwrap() = Instant::now();
if message.header.kind == FRAME_RECEIVED {
if message.payload.len() != 16 || message.header.request != 0 {
state.fail("Invalid frame acknowledgement");
break;
}
let p = &message.payload;
let ack = (
u64::from_le_bytes(p[..8].try_into().unwrap()),
u64::from_le_bytes(p[8..].try_into().unwrap()),
);
let mut frames = state.video.lock().unwrap();
if frames.flight != Some(ack) {
drop(frames);
state.fail("Unexpected frame acknowledgement");
break;
}
frames.flight = None;
state.wake.notify_all();
} else if commands.try_send(message).is_err() {
state.fail("Studio control queue saturated");
break;
}
}
},
)?);
let state = shared.clone();
let session = boot.session;
workers.push(std::thread::Builder::new().name("studio-video-write".into()).spawn(
move || {
let mut previous = None;
let mut payload = Vec::new();
while !state.stopped() {
let (epoch, frame) = {
let mut frames = state.video.lock().unwrap();
loop {
if state.stopped() {
return;
}
if frames.flight.is_none()
&& let Some(p) = &frames.latest
{
let marker = (p.epoch, p.frame.frame_id.get());
if previous != Some(marker) {
let result = (p.epoch, p.frame.clone());
frames.flight = Some(marker);
previous = Some(marker);
break result;
}
}
frames = state
.wake
.wait_timeout(frames, Duration::from_millis(100))
.unwrap()
.0;
}
};
let size = match frame_bytes(
frame.width as u32,
frame.height as u32,
frame.width as u32,
) {
Ok(n) => n,
Err(e) => {
state.fail(e);
break;
}
};
payload.clear();
payload.reserve(FRAME_METADATA_BYTES + size);
payload.extend_from_slice(&session);
payload.extend_from_slice(&epoch.to_le_bytes());
payload.extend_from_slice(&frame.frame_id.get().to_le_bytes());
for n in [frame.width as u32, frame.height as u32, frame.width as u32, RGBA8] {
payload.extend_from_slice(&n.to_le_bytes());
}
for y in 0..frame.height {
for pixel in &frame.pixels
[y * frame.stride_pixels..y * frame.stride_pixels + frame.width]
{
payload.extend_from_slice(&pixel.to_be_bytes());
}
}
if let Err(e) = video_writer.send(FRAME, 0, &payload) {
state.fail(format!("Studio video lost: {e}"));
break;
}
}
},
)?);
let state = shared.clone();
let mut watch = video.try_clone()?;
workers.push(std::thread::Builder::new().name("studio-video-watch".into()).spawn(
move || {
let mut byte = [0];
match watch.read(&mut byte) {
Ok(0) => state.fail("Studio video connection closed"),
Ok(_) => state.fail("Unexpected data on video channel"),
Err(e) => state.fail(format!("Studio video connection failed: {e}")),
}
},
)?);
Ok(Self { shared, outgoing: tx, incoming, control, video, workers })
}
pub fn send(&self, kind: u16, request: u64, payload: Vec<u8>) -> io::Result<()> {
self.outgoing
.try_send(Outbound { kind, request, payload, flushed: None })
.map_err(|_| invalid("Studio output queue unavailable"))
}
pub fn finish(&self, request: u64) -> io::Result<()> {
let (tx, rx) = mpsc::sync_channel(1);
self.outgoing
.try_send(Outbound { kind: STOPPED, request, payload: Vec::new(), flushed: Some(tx) })
.map_err(|_| invalid("Studio output queue unavailable"))?;
rx.recv_timeout(Duration::from_secs(2))
.map_err(|_| invalid("Studio shutdown acknowledgement failed"))
}
pub fn next(&self) -> Option<Message> {
self.incoming.try_recv().ok()
}
pub fn failure(&self) -> Option<String> {
self.shared.failure.lock().unwrap().clone()
}
pub fn idle(&self) -> Duration {
self.shared.activity.lock().unwrap().elapsed()
}
pub fn publish(&self, epoch: u64, frame: OwnedRgba8888Frame) {
self.shared.video.lock().unwrap().latest =
Some(Publication { epoch, frame: Arc::new(frame) });
self.shared.wake.notify_one();
}
}
impl Drop for Transport {
fn drop(&mut self) {
self.shared.closed.store(true, Ordering::SeqCst);
self.shared.wake.notify_all();
let _ = self.control.shutdown(Shutdown::Both);
let _ = self.video.shutdown(Shutdown::Both);
for worker in self.workers.drain(..) {
let _ = worker.join();
}
}
}
@@ -0,0 +1,227 @@
//! Bounded, explicitly little-endian Studio Play messages.
use std::io::{self, Read, Write};
pub const HEADER_BYTES: usize = 32;
pub const CONTROL_LIMIT: usize = 64 * 1024;
pub const FRAME_LIMIT: usize = 16 * 1024 * 1024;
pub const FRAME_METADATA_BYTES: usize = 48;
pub const HELLO: u16 = 1;
pub const READY: u16 = 2;
pub const START: u16 = 3;
pub const PAUSE: u16 = 4;
pub const RESUME: u16 = 5;
pub const STOP: u16 = 6;
pub const RESULT: u16 = 7;
pub const INPUT: u16 = 8;
pub const FRAME: u16 = 9;
pub const FRAME_RECEIVED: u16 = 10;
pub const PING: u16 = 11;
pub const PONG: u16 = 12;
pub const ERROR: u16 = 13;
pub const STOPPED: u16 = 14;
pub const BOOTSTRAP: u16 = 15;
pub const RGBA8: u32 = 1;
pub fn invalid(message: &str) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Header {
pub kind: u16,
pub length: usize,
pub sequence: u64,
pub request: u64,
}
impl Header {
fn validate(self) -> io::Result<()> {
if !(HELLO..=BOOTSTRAP).contains(&self.kind) || self.sequence == 0 {
return Err(invalid("invalid Studio message type/sequence"));
}
if self.length > if self.kind == FRAME { FRAME_LIMIT } else { CONTROL_LIMIT } {
return Err(invalid("Studio payload exceeds limit"));
}
Ok(())
}
pub fn encode(self) -> io::Result<[u8; HEADER_BYTES]> {
self.validate()?;
let mut b = [0; HEADER_BYTES];
b[..4].copy_from_slice(b"PST1");
b[4..6].copy_from_slice(&1u16.to_le_bytes());
b[8..10].copy_from_slice(&self.kind.to_le_bytes());
b[12..16].copy_from_slice(&(self.length as u32).to_le_bytes());
b[16..24].copy_from_slice(&self.sequence.to_le_bytes());
b[24..32].copy_from_slice(&self.request.to_le_bytes());
Ok(b)
}
pub fn decode(b: &[u8; HEADER_BYTES]) -> io::Result<Self> {
if &b[..4] != b"PST1" || b[4..8] != [1, 0, 0, 0] || b[10..12] != [0, 0] {
return Err(invalid("invalid Studio magic/version/flags"));
}
let h = Self {
kind: u16::from_le_bytes(b[8..10].try_into().unwrap()),
length: u32::from_le_bytes(b[12..16].try_into().unwrap()) as usize,
sequence: u64::from_le_bytes(b[16..24].try_into().unwrap()),
request: u64::from_le_bytes(b[24..32].try_into().unwrap()),
};
h.validate()?;
Ok(h)
}
}
#[derive(Debug)]
pub struct Message {
pub header: Header,
pub payload: Vec<u8>,
}
pub struct Reader<R> {
input: R,
sequence: u64,
}
impl<R: Read> Reader<R> {
pub fn new(input: R) -> Self {
Self { input, sequence: 0 }
}
pub fn receive(&mut self) -> io::Result<Message> {
self.receive_bounded(FRAME_LIMIT)
}
pub fn receive_control(&mut self) -> io::Result<Message> {
self.receive_bounded(CONTROL_LIMIT)
}
fn receive_bounded(&mut self, limit: usize) -> io::Result<Message> {
let mut bytes = [0; HEADER_BYTES];
self.input.read_exact(&mut bytes)?;
let header = Header::decode(&bytes)?;
if header.length > limit {
return Err(invalid("payload exceeds channel limit"));
}
if header.sequence <= self.sequence {
return Err(invalid("non-monotonic Studio sequence"));
}
self.sequence = header.sequence;
let mut payload = vec![0; header.length];
self.input.read_exact(&mut payload)?;
Ok(Message { header, payload })
}
}
pub struct Writer<W> {
output: W,
sequence: u64,
}
impl<W: Write> Writer<W> {
pub fn new(output: W) -> Self {
Self { output, sequence: 0 }
}
pub fn send(&mut self, kind: u16, request: u64, payload: &[u8]) -> io::Result<()> {
self.sequence =
self.sequence.checked_add(1).ok_or_else(|| invalid("sequence exhausted"))?;
self.output.write_all(
&Header { kind, request, sequence: self.sequence, length: payload.len() }.encode()?,
)?;
self.output.write_all(payload)?;
self.output.flush()
}
}
#[derive(Debug)]
pub struct Bootstrap {
pub session: [u8; 16],
pub token: [u8; 32],
pub control_port: u16,
pub video_port: u16,
pub disposable: bool,
}
impl Bootstrap {
pub fn read(input: impl Read) -> io::Result<Self> {
let m = Reader::new(input).receive()?;
let p = &m.payload;
if m.header.kind != BOOTSTRAP || m.header.request != 0 || p.len() != 53 || p[52] > 1 {
return Err(invalid("invalid Studio bootstrap"));
}
let b = Self {
session: p[..16].try_into().unwrap(),
token: p[16..48].try_into().unwrap(),
control_port: u16::from_le_bytes(p[48..50].try_into().unwrap()),
video_port: u16::from_le_bytes(p[50..52].try_into().unwrap()),
disposable: p[52] != 0,
};
if b.control_port == 0 || b.video_port == 0 || b.control_port == b.video_port {
return Err(invalid("invalid Studio ports"));
}
Ok(b)
}
pub fn hello(&self, role: u8) -> Vec<u8> {
let mut p = Vec::with_capacity(49);
p.extend_from_slice(&self.session);
p.extend_from_slice(&self.token);
p.push(role);
p
}
}
pub fn frame_bytes(width: u32, height: u32, stride: u32) -> io::Result<usize> {
if width == 0 || height == 0 || stride != width {
return Err(invalid("invalid frame dimensions/stride"));
}
(stride as usize)
.checked_mul(height as usize)
.and_then(|n| n.checked_mul(4))
.filter(|n| *n <= FRAME_LIMIT - FRAME_METADATA_BYTES)
.ok_or_else(|| invalid("frame exceeds limit"))
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture() -> Vec<u8> {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../devtools/studio-protocol/fixtures/pause.hex");
std::fs::read_to_string(root)
.unwrap()
.split_whitespace()
.map(|s| u8::from_str_radix(s, 16).unwrap())
.collect()
}
#[test]
fn header_matches_shared_fixture() {
let expected = fixture();
let actual = Header { kind: PAUSE, length: 0, sequence: 1, request: 42 }.encode().unwrap();
assert_eq!(actual.as_slice(), expected);
assert_eq!(Header::decode(&actual).unwrap().request, 42);
}
#[test]
fn truncation_replay_and_invalid_header_fail() {
let bytes = fixture();
assert!(Reader::new(&bytes[..31]).receive().is_err());
let repeated = [bytes.as_slice(), bytes.as_slice()].concat();
let mut r = Reader::new(repeated.as_slice());
assert!(r.receive().is_ok());
assert!(r.receive().is_err());
for offset in [0, 4, 6, 8, 10] {
let mut bad: [u8; 32] = bytes.clone().try_into().unwrap();
bad[offset] = 255;
assert!(Header::decode(&bad).is_err());
}
assert!(
Header { kind: INPUT, length: CONTROL_LIMIT + 1, sequence: 1, request: 0 }
.encode()
.is_err()
);
assert!(frame_bytes(u32::MAX, u32::MAX, u32::MAX).is_err());
assert!(frame_bytes(480, 270, 481).is_err());
assert_eq!(frame_bytes(480, 270, 480).unwrap(), 518400);
}
#[test]
fn fragmented_reads_and_writes_preserve_messages() {
struct Short<'a>(&'a [u8]);
impl Read for Short<'_> {
fn read(&mut self, b: &mut [u8]) -> io::Result<usize> {
let n = b.len().min(3).min(self.0.len());
b[..n].copy_from_slice(&self.0[..n]);
self.0 = &self.0[n..];
Ok(n)
}
}
let mut bytes = Vec::new();
Writer::new(&mut bytes).send(INPUT, 9, &[1, 2, 3]).unwrap();
let m = Reader::new(Short(&bytes)).receive().unwrap();
assert_eq!(m.payload, [1, 2, 3]);
assert_eq!(m.header.request, 9);
assert!(Reader::new(&bytes[..bytes.len() - 1]).receive().is_err());
}
}
@@ -0,0 +1,347 @@
use prometeu_drivers::{Audio, MemoryBanks};
use prometeu_hal::{AudioBridge, LoopMode, sample::Sample};
use prometeu_host_desktop_winit::audio::AudioCallback;
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::Arc;
thread_local! {
static COUNTS: Cell<Option<(usize, usize)>> = const { Cell::new(None) };
}
struct Counter;
fn count(alloc: usize, free: usize) {
let _ = COUNTS.try_with(|c| {
if let Some((a, d)) = c.get() {
c.set(Some((a + alloc, d + free)));
}
});
}
unsafe impl GlobalAlloc for Counter {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
count(1, 0);
unsafe { System.alloc(l) }
}
unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 {
count(1, 0);
unsafe { System.alloc_zeroed(l) }
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
count(0, 1);
unsafe { System.dealloc(p, l) }
}
unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 {
count(1, 1);
unsafe { System.realloc(p, l, n) }
}
}
#[global_allocator]
static ALLOCATOR: Counter = Counter;
fn measured(f: impl FnOnce()) -> (usize, usize) {
COUNTS.with(|c| c.set(Some((0, 0))));
f();
COUNTS.with(|c| c.replace(None).unwrap())
}
fn play(audio: &mut Audio, voice: usize, looping: bool) -> std::sync::Weak<Sample> {
let sample = Arc::new(Sample::new(48000, vec![1000; 4]));
let weak = Arc::downgrade(&sample);
assert_eq!(
audio.play_sample(
sample,
voice,
255,
0,
1.0,
0,
if looping { LoopMode::On } else { LoopMode::Off }
),
prometeu_hal::AudioOpStatus::Ok
);
weak
}
fn render(callback: &mut AudioCallback, audio: &Audio, pcm: &mut [f32], label: &str) {
let counts = measured(|| callback.render(&audio.transport(), pcm));
println!("{label}: {counts:?}");
assert_eq!(counts, (0, 0), "{label}");
}
#[test]
fn callback_owners_do_not_use_allocator() {
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let (mut callback, mut reclaimer) = AudioCallback::new();
let mut pcm = [0.0; 32];
let weak = play(&mut audio, 0, false);
audio.finish_frame();
render(&mut callback, &audio, &mut pcm, "natural_completion_and_batch");
assert!(weak.upgrade().is_some(), "owner must await service");
reclaimer.service(&audio.transport());
assert!(weak.upgrade().is_none());
for _ in 0..8 {
play(&mut audio, 0, true);
audio.stop(0);
let replacement = play(&mut audio, 0, true);
audio.finish_frame();
render(&mut callback, &audio, &mut pcm, "replacement_stop_fifo");
assert!(pcm.iter().step_by(2).all(|&v| v == 1000.0 / 32768.0));
reclaimer.service(&audio.transport());
audio.set_paused(true);
render(&mut callback, &audio, &mut pcm, "pause");
assert!(pcm.iter().all(|&v| v == 0.0));
reclaimer.service(&audio.transport());
audio.reset();
render(&mut callback, &audio, &mut pcm, "epoch_reset");
assert!(replacement.upgrade().is_some());
reclaimer.service(&audio.transport());
assert!(replacement.upgrade().is_none());
}
audio.stop(0);
audio.finish_frame();
render(&mut callback, &audio, &mut pcm, "control_batch");
}
#[test]
fn stalled_service_bounds_retention_and_recovers_pending_delivery() {
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let (mut callback, mut reclaimer) = AudioCallback::new();
let owners: Vec<_> = (0..16).map(|voice| play(&mut audio, voice, false)).collect();
audio.finish_frame();
// Keep all voices playing until both packets have been returned.
render(&mut callback, &audio, &mut [], "start_16");
render(&mut callback, &audio, &mut [], "exhaust_pool");
let mut pcm = [0.0; 32];
render(&mut callback, &audio, &mut pcm, "finish_16_without_packet");
assert!(owners.iter().all(|owner| owner.upgrade().is_some()));
let next = play(&mut audio, 0, true);
audio.finish_frame();
for _ in 0..10 {
render(&mut callback, &audio, &mut pcm, "starved_silent_pending");
assert!(!audio.ready());
assert!(pcm.iter().all(|&v| v == 0.0));
}
reclaimer.service(&audio.transport());
render(&mut callback, &audio, &mut pcm, "recover");
assert!(audio.ready());
reclaimer.service(&audio.transport());
assert!(owners.iter().all(|owner| owner.upgrade().is_none()));
assert!(next.upgrade().is_some());
drop(callback); // Teardown belongs to the service/main thread.
drop(reclaimer);
assert!(next.upgrade().is_none());
}
#[test]
fn full_frame_and_combined_lifecycle_fit_one_packet() {
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let (mut callback, mut reclaimer) = AudioCallback::new();
let mut owners = Vec::new();
for voice in 0..16 {
owners.push(play(&mut audio, voice, false));
}
audio.finish_frame();
render(&mut callback, &audio, &mut [], "initial_16");
reclaimer.service(&audio.transport());
// The published frame, interrupted next-frame prefix and lifecycle each
// admit 1024 commands. Every command replaces an owner on the same voice.
for _ in 0..1024 {
owners.push(play(&mut audio, 0, false));
}
audio.finish_frame();
for _ in 0..1024 {
owners.push(play(&mut audio, 0, false));
}
audio.begin_lifecycle();
for _ in 0..1024 {
owners.push(play(&mut audio, 0, false));
}
audio.finish_lifecycle(true);
let mut pcm = [0.0; 32];
render(&mut callback, &audio, &mut pcm, "3072_commands_plus_completion");
reclaimer.service(&audio.transport());
assert!(owners.iter().skip(16).all(|owner| owner.upgrade().is_none()));
audio.reset();
render(&mut callback, &audio, &mut pcm, "reset_remaining_voices");
reclaimer.service(&audio.transport());
assert!(owners.iter().all(|owner| owner.upgrade().is_none()));
}
/// Physical observations, deliberately excluded from ordinary CI. The pressure
/// fixture reads the checked-in asset pack and writes a temporary host save;
/// it does not certify asset decoding or guest memcard scheduling.
#[test]
#[ignore = "requires physical audio output; 16 quiet voices and temporary file I/O"]
fn physical_callback_duration_16_voices_with_io_pressure() {
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use prometeu_host_desktop_winit::fs_backend::HostDirBackend;
use prometeu_system::fs::FsBackend;
use ringbuf::{
HeapRb,
traits::{Consumer, Producer, Split},
};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::time::{Duration, Instant};
let device = cpal::default_host().default_output_device().expect("physical output");
let device_name = device.name().unwrap();
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let (mut callback, mut reclaimer) = AudioCallback::new();
let mut owners = vec![];
for voice in 0..16 {
let sample = Arc::new(Sample::new(48000, vec![32; 4800]));
owners.push(Arc::downgrade(&sample));
audio.play_sample(sample, voice, 8, 127, 1.0, 0, LoopMode::On);
}
audio.finish_frame();
let config = cpal::StreamConfig {
channels: 2,
sample_rate: cpal::SampleRate(48000),
buffer_size: cpal::BufferSize::Fixed(256),
};
let transport = audio.transport();
let (mut timings, mut observations) = HeapRb::<(u64, usize)>::new(4096).split();
let allocator_calls = Arc::new(AtomicUsize::new(0));
let failed = Arc::new(AtomicBool::new(false));
let counter = allocator_calls.clone();
let error = failed.clone();
let stream = device
.build_output_stream(
&config,
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
let start = Instant::now();
let (alloc, free) = measured(|| callback.render(&transport, data));
counter.fetch_add(alloc + free, Ordering::Relaxed);
let _ = timings.try_push((start.elapsed().as_nanos() as u64, data.len() / 2));
},
move |_| {
error.store(true, Ordering::Relaxed);
},
None,
)
.unwrap();
let root =
std::env::temp_dir().join(format!("prometeu-audio-qualification-{}", std::process::id()));
std::fs::create_dir_all(&root).unwrap();
let mut filesystem = HostDirBackend::new(root.clone());
let asset = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../test-cartridges/stress-console/assets.pa");
let save = vec![0x5a; 1024 * 1024];
stream.play().unwrap();
let deadline = Instant::now() + Duration::from_secs(2);
let mut io_cycles = 0;
while Instant::now() < deadline {
let payload = std::fs::read(&asset).unwrap();
assert!(!payload.is_empty());
filesystem.write_file("/user/audio-pressure.bin", &save).unwrap();
reclaimer.service(&audio.transport());
io_cycles += 1;
std::thread::sleep(Duration::from_millis(2));
}
stream.pause().unwrap();
let live_before = owners.iter().filter(|owner| owner.upgrade().is_some()).count();
drop(stream);
reclaimer.service(&audio.transport());
let live_after = owners.iter().filter(|owner| owner.upgrade().is_some()).count();
let mut samples = vec![];
let mut frames = vec![];
while let Some((time, count)) = observations.try_pop() {
samples.push(time);
frames.push(count);
}
filesystem.delete("/user/audio-pressure.bin").unwrap();
std::fs::remove_dir(root.join("user")).unwrap();
std::fs::remove_dir(root).unwrap();
samples.sort_unstable();
assert!(!samples.is_empty(), "physical stream delivered no callbacks");
println!(
"device={device_name:?}, callbacks={}, io_cycles={io_cycles}, frames_min={}, frames_max={}, p50_ns={}, p95_ns={}, p99_ns={}, max_ns={}, allocator_calls={}, owners_before={live_before}, owners_after={live_after}",
samples.len(),
frames.iter().min().unwrap(),
frames.iter().max().unwrap(),
samples[samples.len() / 2],
samples[samples.len() * 95 / 100],
samples[samples.len() * 99 / 100],
samples.last().unwrap(),
allocator_calls.load(Ordering::Relaxed)
);
assert!(!failed.load(Ordering::Relaxed));
assert_eq!(allocator_calls.load(Ordering::Relaxed), 0);
assert_eq!((live_before, live_after), (16, 0));
}
#[test]
#[ignore = "release timing observation with temporary asset-read/save-write pressure"]
fn callback_duration_16_voices_with_io_pressure() {
use prometeu_host_desktop_winit::fs_backend::HostDirBackend;
use prometeu_system::fs::FsBackend;
use std::time::Instant;
let mut audio = Audio::new(Arc::new(MemoryBanks::new()));
let (mut callback, mut reclaimer) = AudioCallback::new();
let owners: Vec<_> = (0..16).map(|voice| play(&mut audio, voice, true)).collect();
audio.finish_frame();
let mut pcm = [0.0; 512]; // 256 stereo frames: nominal 5.333 ms at 48 kHz.
callback.render(&audio.transport(), &mut pcm);
reclaimer.service(&audio.transport());
let root = std::env::temp_dir().join(format!("prometeu-audio-timing-{}", std::process::id()));
std::fs::create_dir_all(&root).unwrap();
let mut filesystem = HostDirBackend::new(root.clone());
let asset = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../test-cartridges/stress-console/assets.pa");
let save = vec![0x5a; 1024 * 1024];
let mut samples = Vec::with_capacity(1000);
let mut counts = (0, 0);
for _ in 0..1000 {
assert!(!std::fs::read(&asset).unwrap().is_empty());
filesystem.write_file("/user/audio-pressure.bin", &save).unwrap();
let start = Instant::now();
let measured = measured(|| callback.render(&audio.transport(), &mut pcm));
samples.push(start.elapsed().as_nanos());
counts.0 += measured.0;
counts.1 += measured.1;
reclaimer.service(&audio.transport());
}
samples.sort_unstable();
let before = owners.iter().filter(|owner| owner.upgrade().is_some()).count();
drop(callback);
drop(reclaimer);
let after = owners.iter().filter(|owner| owner.upgrade().is_some()).count();
filesystem.delete("/user/audio-pressure.bin").unwrap();
std::fs::remove_dir(root.join("user")).unwrap();
std::fs::remove_dir(root).unwrap();
println!(
"synthetic callbacks=1000, frames=256, nominal_period_ns=5333333, io_cycles=1000, p50_ns={}, p95_ns={}, p99_ns={}, max_ns={}, alloc_free={counts:?}, owners_before={before}, owners_after={after}",
samples[500], samples[950], samples[990], samples[999]
);
assert_eq!(counts, (0, 0));
assert_eq!((before, after), (16, 0));
}
#[test]
fn unloaded_banks_empty_samples_and_controls_reclaim_on_service() {
use prometeu_drivers::SoundBankPoolInstaller;
use prometeu_hal::sound_bank::SoundBank;
let banks = Arc::new(MemoryBanks::new());
let sample = Arc::new(Sample::new(48000, vec![1000; 16]));
let weak = Arc::downgrade(&sample);
banks.install_sound_bank(0, Arc::new(SoundBank::new(vec![sample])));
let mut audio = Audio::new(banks.clone());
let (mut callback, mut reclaimer) = AudioCallback::new();
audio.play(0, 0, 0, 255, 0, 1.0, 0, LoopMode::On);
audio.finish_frame();
banks.clear_sound_banks();
let mut pcm = [0.0; 16];
render(&mut callback, &audio, &mut pcm, "unloaded_bank");
assert!(weak.upgrade().is_some());
reclaimer.service(&audio.transport());
audio.set_volume(0, 127);
audio.set_pan(0, 255);
audio.set_pitch(0, 0.5);
audio.stop(0);
let empty = Arc::new(Sample::new(48000, vec![]));
let weak_empty = Arc::downgrade(&empty);
audio.play_sample(empty, 1, 255, 0, 1.0, 0, LoopMode::Off);
audio.finish_frame();
render(&mut callback, &audio, &mut pcm, "controls_stop_empty");
assert!(pcm.iter().all(|&sample| sample == 0.0));
assert!(weak.upgrade().is_some() && weak_empty.upgrade().is_some());
reclaimer.service(&audio.transport());
assert!(weak.upgrade().is_none() && weak_empty.upgrade().is_none());
}
@@ -0,0 +1,215 @@
//! Real process qualification: no window, slow consumer, pause and broken IPC.
use prometeu_host_desktop_winit::studio::wire::*;
use std::io::{self, BufRead, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
struct Session {
child: Child,
control: Reader<TcpStream>,
writer: Writer<TcpStream>,
video: Reader<TcpStream>,
control_socket: TcpStream,
video_socket: TcpStream,
logs: Arc<Mutex<Vec<String>>>,
}
impl Session {
fn start() -> Self {
let control = TcpListener::bind("127.0.0.1:0").unwrap();
let video = TcpListener::bind("127.0.0.1:0").unwrap();
let cart = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../test-cartridges/dummy-boy");
let child = Command::new(env!("CARGO_BIN_EXE_prometeu-host-desktop-winit"))
.args(["--studio", "--run"])
.arg(cart)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let logs = Arc::new(Mutex::new(Vec::new()));
let mut session_child = child;
for stream in [
Box::new(session_child.stdout.take().unwrap()) as Box<dyn io::Read + Send>,
Box::new(session_child.stderr.take().unwrap()) as Box<dyn io::Read + Send>,
] {
let log = logs.clone();
std::thread::spawn(move || {
for line in io::BufReader::new(stream).lines().map_while(Result::ok) {
let mut lines = log.lock().unwrap();
if lines.len() == 200 {
lines.remove(0);
}
lines.push(line);
}
});
}
let mut p = vec![3; 16];
p.extend_from_slice(&[7; 32]);
p.extend_from_slice(&control.local_addr().unwrap().port().to_le_bytes());
p.extend_from_slice(&video.local_addr().unwrap().port().to_le_bytes());
p.push(1);
Writer::new(session_child.stdin.take().unwrap()).send(BOOTSTRAP, 0, &p).unwrap();
fn accept(listener: TcpListener) -> TcpStream {
listener.set_nonblocking(true).unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
loop {
match listener.accept() {
Ok((s, _)) => {
s.set_nonblocking(false).unwrap();
s.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
return s;
}
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
assert!(Instant::now() < deadline, "child did not connect");
std::thread::sleep(Duration::from_millis(5));
}
Err(e) => panic!("accept: {e}"),
}
}
}
let control = accept(control);
let video = accept(video);
let mut c = Reader::new(control.try_clone().unwrap());
let mut v = Reader::new(video.try_clone().unwrap());
for (message, role) in [(c.receive().unwrap(), 1), (v.receive().unwrap(), 2)] {
assert_eq!(message.header.kind, HELLO);
assert_eq!(&message.payload[..16], &[3; 16]);
assert_eq!(&message.payload[16..48], &[7; 32]);
assert_eq!(message.payload[48], role);
}
let control_socket = control.try_clone().unwrap();
let mut session = Self {
child: session_child,
control: c,
writer: Writer::new(control),
video: v,
control_socket,
video_socket: video,
logs,
};
let ready = session.control.receive().unwrap();
assert_eq!(ready.header.kind, READY, "{:?}", session.logs.lock().unwrap());
assert_eq!(u32::from_le_bytes(ready.payload[..4].try_into().unwrap()), 480);
session
}
fn command(&mut self, kind: u16, request: u64) -> Message {
self.writer.send(kind, request, &[]).unwrap();
let r = self.control.receive().unwrap();
assert_eq!(r.header.kind, RESULT, "{:?}", self.logs.lock().unwrap());
assert_eq!(r.header.request, request);
assert_eq!(u16::from_le_bytes(r.payload[2..4].try_into().unwrap()), 0, "{:?}", r.payload);
r
}
fn ping(&mut self) -> (u64, u64, u32) {
self.writer.send(PING, 0, &[]).unwrap();
let m = self.control.receive().unwrap();
assert_eq!(m.header.kind, PONG);
(
u64::from_le_bytes(m.payload[..8].try_into().unwrap()),
u64::from_le_bytes(m.payload[8..16].try_into().unwrap()),
u32::from_le_bytes(m.payload[16..20].try_into().unwrap()),
)
}
fn frame(&mut self) -> Message {
let m = self
.video
.receive()
.unwrap_or_else(|e| panic!("video: {e}; logs: {:?}", self.logs.lock().unwrap()));
assert_eq!(m.header.kind, FRAME);
assert_eq!(m.payload.len(), 48 + 480 * 270 * 4);
m
}
fn ack(&mut self, frame: &Message) {
self.writer.send(FRAME_RECEIVED, 0, &frame.payload[16..32]).unwrap();
}
fn exit(&mut self) -> ExitStatus {
let deadline = Instant::now() + Duration::from_secs(4);
loop {
if let Some(status) = self.child.try_wait().unwrap() {
return status;
}
assert!(
Instant::now() < deadline,
"runtime did not exit: {:?}",
self.logs.lock().unwrap()
);
std::thread::sleep(Duration::from_millis(10));
}
}
}
impl Drop for Session {
fn drop(&mut self) {
let _ = self.child.kill();
let _ = self.child.wait();
}
}
#[test]
fn slow_video_does_not_stop_ticks_and_pause_resumes_same_process() {
let mut s = Session::start();
let pid = s.child.id();
s.command(START, 1);
let first = s.frame();
let initial = s.ping();
std::thread::sleep(Duration::from_millis(150));
let later = s.ping();
assert!(later.1 > initial.1);
// No credit was returned while the runtime kept publishing. The next frame skips old presentations.
s.ack(&first);
let recent = s.frame();
assert!(
u64::from_le_bytes(recent.payload[24..32].try_into().unwrap())
> u64::from_le_bytes(first.payload[24..32].try_into().unwrap()) + 1
);
s.command(PAUSE, 2);
let paused = s.ping();
assert_eq!(paused.2, 2);
std::thread::sleep(Duration::from_millis(80));
assert_eq!(s.ping().1, paused.1);
s.command(RESUME, 3);
std::thread::sleep(Duration::from_millis(60));
assert!(s.ping().1 > paused.1);
assert_eq!(s.child.id(), pid);
s.command(STOP, 4);
assert_eq!(s.control.receive().unwrap().header.kind, STOPPED);
assert!(s.exit().success());
}
#[test]
fn broken_video_ends_runtime_even_when_paused() {
let mut s = Session::start();
s.command(START, 1);
s.command(PAUSE, 2);
s.video_socket.shutdown(Shutdown::Both).unwrap();
assert!(!s.exit().success());
}
#[test]
fn startup_focus_loss_arms_game_without_advancing_until_manual_resume() {
let mut s = Session::start();
let initial = s.ping().1;
s.command(PAUSE, 1);
s.command(START, 2);
std::thread::sleep(Duration::from_millis(50));
let paused = s.ping();
assert_eq!(paused.1, initial);
assert_eq!(paused.2, 2);
s.command(RESUME, 3);
let _ = s.frame();
assert!(s.ping().1 > initial);
s.command(STOP, 4);
assert_eq!(s.control.receive().unwrap().header.kind, STOPPED);
assert!(s.exit().success());
}
#[test]
fn truncated_control_ends_only_its_owned_runtime() {
let mut s = Session::start();
s.command(START, 1);
s.control_socket.write_all(b"PST").unwrap();
s.control_socket.shutdown(Shutdown::Write).unwrap();
assert!(!s.exit().success());
}
@@ -0,0 +1,38 @@
use prometeu_host_desktop_winit::studio::wire::*;
#[test]
fn rgba_fixture_pins_channel_order_and_metadata() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../../devtools/studio-protocol/fixtures/rgba.hex");
let bytes: Vec<u8> = std::fs::read_to_string(path)
.unwrap()
.split_whitespace()
.map(|s| u8::from_str_radix(s, 16).unwrap())
.collect();
let m = Reader::new(bytes.as_slice()).receive().unwrap();
assert_eq!(m.header.kind, FRAME);
assert_eq!(m.header.length, 56);
assert_eq!(&m.payload[..16], &(0u8..16).collect::<Vec<_>>());
assert_eq!(
&m.payload[48..],
&[0xff0000ffu32.to_be_bytes(), 0x0000ff80u32.to_be_bytes()].concat()
);
assert_eq!(frame_bytes(2, 1, 2).unwrap() + FRAME_METADATA_BYTES, m.payload.len());
}
#[test]
fn bootstrap_rejects_wrong_roles_lengths_and_ports() {
let mut p = vec![0; 53];
p[48..50].copy_from_slice(&1234u16.to_le_bytes());
p[50..52].copy_from_slice(&1235u16.to_le_bytes());
p[52] = 1;
let mut bytes = Vec::new();
Writer::new(&mut bytes).send(BOOTSTRAP, 0, &p).unwrap();
let boot = Bootstrap::read(bytes.as_slice()).unwrap();
assert!(boot.disposable);
assert_eq!(boot.hello(1).len(), 49);
p[50..52].copy_from_slice(&1234u16.to_le_bytes());
bytes.clear();
Writer::new(&mut bytes).send(BOOTSTRAP, 0, &p).unwrap();
assert!(Bootstrap::read(bytes.as_slice()).is_err());
}
@@ -2,7 +2,7 @@
mod wire;
pub use wire::Limits;
use crate::{AdvanceFrameOutcome, HeadlessHost};
use crate::{AdvanceFrameOutcome, HeadlessHost, PendingWrites, TeardownPolicy};
use serde_json::{Value, json};
use std::io::{self, BufRead, Write};
use std::sync::{Arc, Mutex, mpsc};
@@ -16,7 +16,8 @@ struct Control {
}
enum Job {
Work(String, Command),
Work(String, Command, bool),
Shutdown(String, TeardownPolicy, bool, mpsc::Sender<bool>),
Stop(Option<String>),
ReadError(io::Error),
}
@@ -104,6 +105,8 @@ fn read_commands<R: BufRead, W: Write>(
limits: &Limits,
) -> io::Result<()> {
let mut initialized = false;
let mut memcard = false;
let mut log_read = false;
loop {
let bytes = match read_line(&mut input, limits.max_command_bytes)? {
Line::Eof => break,
@@ -142,11 +145,36 @@ fn read_commands<R: BufRead, W: Write>(
continue;
}
};
if let Command::Shutdown = command {
let mut c = control.lock().unwrap();
c.canceled = true;
sender.send(Job::Stop(Some(id))).map_err(|_| io::Error::other("execution stopped"))?;
return Ok(());
if matches!(command, Command::GetLogs(_)) && !log_read {
output.reply(
Some(&id),
Err(WireError::new(
"unsupported_capability",
"negotiate minor 1.2 and require log_read first",
)),
)?;
continue;
}
if command.needs_memcard() && !memcard {
output.reply(
Some(&id),
Err(WireError::new(
"unsupported_capability",
"negotiate minor 1.1 and require memcard first",
)),
)?;
continue;
}
if let Command::Shutdown(params) = command {
control.lock().unwrap().canceled = true;
let (tx, rx) = mpsc::channel();
sender
.send(Job::Shutdown(id, params.policy(), memcard, tx))
.map_err(|_| io::Error::other("execution stopped"))?;
if rx.recv().map_err(|_| io::Error::other("execution stopped"))? {
return Ok(());
}
continue;
}
if let Command::Initialize(params) = command {
let result = if initialized {
@@ -159,9 +187,23 @@ fn read_commands<R: BufRead, W: Write>(
.any(|c| !CAPABILITIES.contains(&c.as_str()))
{
Err(WireError::new("unsupported_capability", "required capability unavailable"))
} else if params.protocol_version.minor == 0
&& params.required_capabilities.iter().any(|c| c == "memcard")
{
Err(WireError::new("incompatible_version", "memcard requires protocol minor >= 1"))
} else if params.protocol_version.minor < 2
&& params.required_capabilities.iter().any(|c| c == "log_read")
{
Err(WireError::new("incompatible_version", "log_read requires protocol minor >= 2"))
} else {
initialized = true;
Ok(json!({"protocolVersion":{"major":1,"minor":0},"capabilities":CAPABILITIES}))
memcard = params.protocol_version.minor >= 1
&& params.required_capabilities.iter().any(|c| c == "memcard");
log_read = params.protocol_version.minor >= 2
&& params.required_capabilities.iter().any(|c| c == "log_read");
Ok(
json!({"protocolVersion":{"major":1,"minor":params.protocol_version.minor.min(2)},"capabilities":CAPABILITIES}),
)
};
output.reply(Some(&id), result)?;
continue;
@@ -197,7 +239,9 @@ fn read_commands<R: BufRead, W: Write>(
c.active = Some(id.clone());
c.advancing = matches!(command, Command::Advance(_));
c.canceled = false;
sender.send(Job::Work(id, command)).map_err(|_| io::Error::other("execution stopped"))?;
sender
.send(Job::Work(id, command, memcard))
.map_err(|_| io::Error::other("execution stopped"))?;
}
let mut c = control.lock().unwrap();
c.canceled = true;
@@ -210,14 +254,39 @@ fn execute<W: Write>(
id: &str,
output: &Output<W>,
control: &Mutex<Control>,
memcard: bool,
) -> io::Result<Result<Value, WireError>> {
Ok(match command {
Command::Boot(p) => {
host.boot(p.path).map(|()| wire::state(host.get_state())).map_err(wire::host_error)
let result = if let Some(choice) = p.memcard_choice {
host.boot_with_memcard_choice(p.path, choice.into())
} else {
host.boot(p.path)
};
result.map(|()| wire::state(host.get_state())).map_err(wire::host_error)
}
Command::Reset => {
host.reset();
Ok(wire::state(host.get_state()))
Command::Reset(params) => host
.reset_with_policy(params.policy())
.map(|snapshot| {
let mut state = wire::state(host.get_state());
if memcard {
state["memcard"] = json!(snapshot);
}
state
})
.map_err(wire::host_error),
Command::MemcardMount(p) => {
host.mount_memcard(p).map(|s| json!(s)).map_err(wire::host_error)
}
Command::MemcardUnmount => {
host.unmount_memcard().map(|s| json!(s)).map_err(wire::host_error)
}
Command::GetMemcardState => Ok(json!(host.get_memcard_state())),
Command::MemcardReconcile => {
host.reconcile_memcard().map(|s| json!(s)).map_err(wire::host_error)
}
Command::MemcardContinueInMemory => {
host.continue_memcard_in_memory().map(|s| json!(s)).map_err(wire::host_error)
}
Command::SetInput(p) => {
host.set_input(p.into());
@@ -229,6 +298,7 @@ fn execute<W: Write>(
}
Command::GetState => Ok(wire::state(host.get_state())),
Command::GetFault => Ok(host.get_fault().as_ref().map(wire::fault).unwrap_or(Value::Null)),
Command::GetLogs(params) => wire::logs(host, params, &output.session),
Command::Capture => host.capture_frame().map_err(wire::host_error).and_then(wire::capture),
Command::Advance(p) => {
let mut result = json!({"requested":p.frames,"completed":0,"stop":"completed"});
@@ -254,6 +324,10 @@ fn execute<W: Write>(
result["stop"] = json!("terminated");
break;
}
Ok(AdvanceFrameOutcome::ActionRequired) => {
result["stop"] = json!("action_required");
break;
}
Err(error) => {
result["stop"] = json!("host_error");
result["error"] = serde_json::to_value(wire::host_error(error))?;
@@ -283,7 +357,7 @@ pub fn serve<R: BufRead + Send + 'static, W: Write + Send + 'static>(
let output =
Arc::new(Output { writer: Mutex::new(writer), session, limit: limits.max_response_bytes });
output.send(
json!({"type":"hello","sessionId":output.session,"protocolVersion":{"major":1,"minor":0},
json!({"type":"hello","sessionId":output.session,"protocolVersion":{"major":1,"minor":2},
"runtimeVersion":runtime_version,"capabilities":CAPABILITIES,"limits":limits}),
)?;
let control = Arc::new(Mutex::new(Control::default()));
@@ -298,18 +372,58 @@ pub fn serve<R: BufRead + Send + 'static, W: Write + Send + 'static>(
}
});
let mut host = HeadlessHost::new();
while let Ok(job) = receiver.recv() {
loop {
host.poll_memcard();
let job = match receiver.recv_timeout(std::time::Duration::from_millis(5)) {
Ok(job) => job,
Err(mpsc::RecvTimeoutError::Timeout) => continue,
Err(mpsc::RecvTimeoutError::Disconnected) => break,
};
match job {
Job::Work(id, command) => {
let result = execute(&mut host, command, &id, &output, &control)?;
Job::Work(id, command, memcard) => {
let result = match execute(&mut host, command, &id, &output, &control, memcard) {
Ok(result) => result,
Err(error) => {
cleanup_after_loss(&mut host);
return Err(error);
}
};
// Release authority under the same lock as response publication:
// a client reacting to this reply cannot race a stale busy flag.
let mut c = control.lock().unwrap();
output.reply(Some(&id), result)?;
if let Err(error) = output.reply(Some(&id), result) {
drop(c);
cleanup_after_loss(&mut host);
return Err(error);
}
c.active = None;
c.advancing = false;
}
Job::Shutdown(id, policy, memcard, ack) => match host.shutdown(policy) {
Ok(snapshot) => {
drop(host);
let mut value = json!({"closed":true});
if memcard {
value["memcard"] = json!(snapshot);
}
output.reply(Some(&id), Ok(value))?;
let _ = ack.send(true);
reader.join().map_err(|_| io::Error::other("input reader panicked"))?;
return Ok(());
}
Err(error) => {
if let Err(error) = output.reply(Some(&id), Err(wire::host_error(error))) {
cleanup_after_loss(&mut host);
return Err(error);
}
let _ = ack.send(false);
}
},
Job::Stop(id) => {
let _ = host.shutdown(TeardownPolicy {
discard_unsaved: true,
pending_writes: PendingWrites::Cancel,
});
drop(host);
if let Some(id) = id {
output.reply(Some(&id), Ok(json!({"closed":true})))?;
@@ -317,13 +431,24 @@ pub fn serve<R: BufRead + Send + 'static, W: Write + Send + 'static>(
reader.join().map_err(|_| io::Error::other("input reader panicked"))?;
return Ok(());
}
Job::ReadError(error) => return Err(error),
Job::ReadError(error) => {
let _ = host.shutdown(TeardownPolicy {
discard_unsaved: true,
pending_writes: PendingWrites::Cancel,
});
return Err(error);
}
}
}
reader.join().map_err(|_| io::Error::other("input reader panicked"))?;
Ok(())
}
fn cleanup_after_loss(host: &mut HeadlessHost) {
let _ = host
.shutdown(TeardownPolicy { discard_unsaved: true, pending_writes: PendingWrites::Cancel });
}
#[cfg(test)]
mod tests {
use super::*;
@@ -369,7 +494,7 @@ mod tests {
"../../../../../devtools/headless-protocol/requests.json"
))
.unwrap();
assert_eq!(requests.len(), 11);
assert!(!requests.is_empty());
for request in requests {
wire::parse(serde_json::from_value(request).unwrap()).unwrap();
}

Some files were not shown because too many files have changed in this diff Show More