Compare commits
1
Commits
master
..
22769aa438
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22769aa438
|
@@ -13,15 +13,14 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- name: Install repository Rust toolchain
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
run: |
|
with:
|
||||||
toolchain=$(python3 -c 'import tomllib; print(tomllib.load(open("rust-toolchain.toml", "rb"))["toolchain"]["channel"])')
|
components: rustfmt, clippy, llvm-tools-preview
|
||||||
rustup toolchain install "$toolchain" --profile minimal
|
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
|
||||||
- name: Discard reports from previous runs
|
- name: Discard reports from previous runs
|
||||||
run: rm -rf target/llvm-cov
|
run: rm -rf target/quality-gate target/llvm-cov
|
||||||
|
|
||||||
- name: Provision quality-gate tools and native dependencies
|
- name: Provision quality-gate tools and native dependencies
|
||||||
run: bash scripts/provision-quality-gate.sh
|
run: bash scripts/provision-quality-gate.sh
|
||||||
@@ -35,5 +34,6 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
name: quality-gate-${{ runner.os }}-${{ github.sha }}
|
name: quality-gate-${{ runner.os }}-${{ github.sha }}
|
||||||
path: |
|
path: |
|
||||||
|
target/quality-gate/**
|
||||||
target/llvm-cov/**
|
target/llvm-cov/**
|
||||||
if-no-files-found: warn
|
if-no-files-found: warn
|
||||||
|
|||||||
@@ -66,3 +66,7 @@ AGENTS.md
|
|||||||
|
|
||||||
.python-version
|
.python-version
|
||||||
.venv
|
.venv
|
||||||
|
|
||||||
|
discussion/*
|
||||||
|
!discussion/lessons/
|
||||||
|
!discussion/lessons/**
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
.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
|
.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 test-quality-gate cobertura
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
cargo fmt --all
|
cargo fmt --all
|
||||||
@@ -9,15 +9,8 @@ fmt-check:
|
|||||||
clippy:
|
clippy:
|
||||||
cargo clippy --workspace --all-targets --all-features -- -D warnings
|
cargo clippy --workspace --all-targets --all-features -- -D warnings
|
||||||
|
|
||||||
test-unit:
|
test-local:
|
||||||
cargo test --workspace --lib --bins --all-features --no-fail-fast
|
cargo test --workspace --all-targets --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:
|
test-debugger-socket:
|
||||||
cargo test -p prometeu-host-desktop-winit --lib runner::tests -- --ignored
|
cargo test -p prometeu-host-desktop-winit --lib runner::tests -- --ignored
|
||||||
@@ -26,7 +19,7 @@ clean:
|
|||||||
cargo llvm-cov clean --workspace
|
cargo llvm-cov clean --workspace
|
||||||
|
|
||||||
coverage:
|
coverage:
|
||||||
cargo llvm-cov --workspace --lib --bins --all-features --html --output-dir target/llvm-cov
|
cargo llvm-cov --workspace --all-features --html --output-dir target/llvm-cov
|
||||||
|
|
||||||
coverage-report-json:
|
coverage-report-json:
|
||||||
cargo llvm-cov report --json --output-path target/llvm-cov/report.json
|
cargo llvm-cov report --json --output-path target/llvm-cov/report.json
|
||||||
@@ -53,13 +46,15 @@ coverage-domain-evidence:
|
|||||||
coverage-domain-gate:
|
coverage-domain-gate:
|
||||||
./scripts/coverage-domain-evidence.sh --check-all
|
./scripts/coverage-domain-evidence.sh --check-all
|
||||||
|
|
||||||
test: fmt-check clippy test-unit
|
test: fmt-check clippy test-local test-debugger-socket
|
||||||
# Local and GitHub entry point; Jenkins exposes the same targets as stages.
|
# Keep the step list here: CI providers consume the repository contract.
|
||||||
CI_DOMAIN_STEPS = clean fmt-check clippy test-unit coverage coverage-report-json coverage-xml coverage-json coverage-domain-audit coverage-domain-gate
|
# The runner executes each step serially even if the caller supplies make -j.
|
||||||
|
CI_DOMAIN_STEPS = clean fmt-check clippy test-quality-gate test-local test-debugger-socket coverage coverage-report-json coverage-xml coverage-json coverage-domain-audit coverage-domain-gate
|
||||||
|
|
||||||
ci: ci-domains
|
ci: ci-domains
|
||||||
ci-domains:
|
ci-domains:
|
||||||
rm -rf target/llvm-cov
|
python3 scripts/run-quality-gate.py $(CI_DOMAIN_STEPS)
|
||||||
+@set -e; for step in $(CI_DOMAIN_STEPS); do $(MAKE) --no-print-directory -j1 $$step; done
|
|
||||||
|
|
||||||
|
test-quality-gate:
|
||||||
|
python3 -B -m unittest discover -s scripts/tests -p 'test_quality_gate.py'
|
||||||
cobertura: coverage-xml coverage-json
|
cobertura: coverage-xml coverage-json
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
# PROMETEU Runtime
|
# PROMETEU Runtime
|
||||||
|
|
||||||
PROMETEU is an educational and experimental fantasy handheld / fantasy console.
|
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 engineering lessons.
|
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.
|
||||||
|
|
||||||
The VM is only one subsystem of the machine. The canonical docs in this repository intentionally distinguish:
|
The VM is only one subsystem of the machine. The canonical docs in this repository intentionally distinguish:
|
||||||
|
|
||||||
- machine-level specs;
|
- machine-level specs;
|
||||||
- VM/runtime internal architecture;
|
- VM/runtime internal architecture;
|
||||||
- implementation crates;
|
- implementation crates;
|
||||||
- engineering lessons learned.
|
- discussion workflow and lessons learned.
|
||||||
|
|
||||||
## What This Repository Contains
|
## 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/specs/runtime/`: canonical PROMETEU machine specs
|
||||||
- `docs/vm-arch/`: canonical VM/runtime architecture and ISA references
|
- `docs/vm-arch/`: canonical VM/runtime architecture and ISA references
|
||||||
- `devtools/`: debugger protocol material
|
- `devtools/`: debugger protocol material
|
||||||
- `discussion/lessons/`: published engineering lessons and supporting assets
|
- `discussion/`: agendas, decisions, plans, and lessons for architectural work
|
||||||
- `test-cartridges/`: cartridge fixtures used for validation and manual runs
|
- `test-cartridges/`: cartridge fixtures used for validation and manual runs
|
||||||
|
|
||||||
## Canonical Documentation
|
## Canonical Documentation
|
||||||
@@ -29,11 +29,7 @@ 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
|
- [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
|
- [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
|
- [ISA reference](docs/vm-arch/ISA_CORE.md): bytecode-level instruction set authority
|
||||||
- [Engineering lessons](discussion/lessons/): published results and rationale
|
- [Discussion workflow](discussion/index.ndjson): architectural agenda, decision, and execution traceability
|
||||||
|
|
||||||
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
|
## Workspace Layout
|
||||||
|
|
||||||
@@ -64,21 +60,11 @@ Other ecosystem commands, such as build, pack, and verification tooling, are not
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Rust **1.98.1**, pinned in [rust-toolchain.toml](rust-toolchain.toml)
|
- Rust toolchain from [rust-toolchain.toml](rust-toolchain.toml)
|
||||||
- `rustup` for toolchain installation and management
|
- `rustup` for toolchain installation and management
|
||||||
- `cargo-llvm-cov` for coverage targets such as `make coverage` and `make ci-domains`
|
- `cargo-llvm-cov` for coverage targets such as `make coverage` and `make ci-domains`
|
||||||
- a local graphical environment for desktop runtime commands
|
- 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
|
## Quick Start
|
||||||
|
|
||||||
Build the workspace:
|
Build the workspace:
|
||||||
@@ -133,14 +119,7 @@ The authoritative target used by both GitHub and Jenkins is:
|
|||||||
make ci-domains
|
make ci-domains
|
||||||
```
|
```
|
||||||
|
|
||||||
`make test-unit` selects library and binary unit-test harnesses with
|
The five debugger socket tests bind localhost sockets and are marked ignored in Rust test metadata; both `make test` and `make ci-domains` select only `runner::tests` through `test-debugger-socket`. Physical audio and other manual probes remain opt-in. See [quality-gate prerequisites and commands](docs/vm-arch/COVERAGE_DOMAINS.md#commands-and-ci).
|
||||||
`--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
|
## Coverage Governance
|
||||||
|
|
||||||
@@ -166,6 +145,7 @@ This produces:
|
|||||||
- `target/llvm-cov/report.json`
|
- `target/llvm-cov/report.json`
|
||||||
- `target/llvm-cov/summary.json`
|
- `target/llvm-cov/summary.json`
|
||||||
- `target/llvm-cov/cobertura.xml`
|
- `target/llvm-cov/cobertura.xml`
|
||||||
|
- `target/quality-gate/metadata.json` and per-step logs
|
||||||
|
|
||||||
Domain-oriented evidence helpers:
|
Domain-oriented evidence helpers:
|
||||||
|
|
||||||
@@ -177,7 +157,7 @@ make coverage-domain-evidence DOMAIN=host/headless
|
|||||||
```
|
```
|
||||||
|
|
||||||
`make ci-domains` runs formatting, strict workspace/all-target/all-feature Clippy,
|
`make ci-domains` runs formatting, strict workspace/all-target/all-feature Clippy,
|
||||||
library/binary unit tests, fresh unit-only coverage and every domain
|
all-target tests, the five debugger socket tests, fresh coverage and every domain
|
||||||
ownership/evidence gate. Both CI providers archive available evidence on failure.
|
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
|
A failing run never lowers a configured floor; local and CI checks use the same
|
||||||
repository-owned commands. Reports identify their revision, source/configuration
|
repository-owned commands. Reports identify their revision, source/configuration
|
||||||
@@ -194,7 +174,7 @@ Improving the global percentage alone is not sufficient review evidence when the
|
|||||||
|
|
||||||
The project is still in active architectural and implementation convergence.
|
The project is still in active architectural and implementation convergence.
|
||||||
|
|
||||||
- the machine contract is being clarified through the specs and AI Engineering;
|
- the machine contract is being clarified through the specs and discussion workflow;
|
||||||
- the workspace already contains concrete runtime and host code;
|
- 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.
|
- 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);
|
emit_u16(CoreOpCode::Ret as u16, &mut out);
|
||||||
}
|
}
|
||||||
"YIELD" | "SLEEP" | "SPAWN" => {
|
"YIELD" | "SLEEP" | "SPAWN" => {
|
||||||
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()));
|
return Err(AsmError::InvalidOperand("retired coroutine instruction".into()))
|
||||||
}
|
}
|
||||||
"FRAME_SYNC" => {
|
"FRAME_SYNC" => {
|
||||||
emit_u16(CoreOpCode::FrameSync as u16, &mut out);
|
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(ty, &mut out);
|
||||||
emit_u32(field, &mut out);
|
emit_u32(field, &mut out);
|
||||||
}
|
}
|
||||||
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" | "ARRAY_COPY" => {
|
"STRUCT_NEW" | "ARRAY_NEW" | "ARRAY_LEN" | "ARRAY_GET" | "ARRAY_SET" => {
|
||||||
if ops.is_empty() {
|
if ops.is_empty() {
|
||||||
return Err(AsmError::MissingOperand(line.into()));
|
return Err(AsmError::MissingOperand(line.into()));
|
||||||
}
|
}
|
||||||
@@ -354,32 +354,23 @@ pub fn assemble(src: &str) -> Result<Vec<u8>, AsmError> {
|
|||||||
"ARRAY_NEW" => CoreOpCode::ArrayNew,
|
"ARRAY_NEW" => CoreOpCode::ArrayNew,
|
||||||
"ARRAY_LEN" => CoreOpCode::ArrayLen,
|
"ARRAY_LEN" => CoreOpCode::ArrayLen,
|
||||||
"ARRAY_GET" => CoreOpCode::ArrayGet,
|
"ARRAY_GET" => CoreOpCode::ArrayGet,
|
||||||
"ARRAY_COPY" => CoreOpCode::ArrayCopy,
|
|
||||||
_ => CoreOpCode::ArraySet,
|
_ => CoreOpCode::ArraySet,
|
||||||
};
|
};
|
||||||
emit_u16(op as u16, &mut out);
|
emit_u16(op as u16, &mut out);
|
||||||
emit_u32(parse_u32_any(ops)?, &mut out);
|
emit_u32(parse_u32_any(ops)?, &mut out);
|
||||||
}
|
}
|
||||||
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" | "REF_IDENTITY_HASH" => {
|
"DYN_PACK" | "DYN_UNPACK" | "DYN_CAN_UNPACK" => {
|
||||||
if ops.is_empty() {
|
if ops.is_empty() {
|
||||||
return Err(AsmError::MissingOperand(line.into()));
|
return Err(AsmError::MissingOperand(line.into()));
|
||||||
}
|
}
|
||||||
let opcode = match mn {
|
let opcode = match mn {
|
||||||
"DYN_PACK" => CoreOpCode::DynPack,
|
"DYN_PACK" => CoreOpCode::DynPack,
|
||||||
"DYN_UNPACK" => CoreOpCode::DynUnpack,
|
"DYN_UNPACK" => CoreOpCode::DynUnpack,
|
||||||
"REF_IDENTITY_HASH" => CoreOpCode::RefIdentityHash,
|
|
||||||
_ => CoreOpCode::DynCanUnpack,
|
_ => CoreOpCode::DynCanUnpack,
|
||||||
};
|
};
|
||||||
emit_u16(opcode as u16, &mut out);
|
emit_u16(opcode as u16, &mut out);
|
||||||
emit_u32(parse_u32_any(ops)?, &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" => {
|
"REF_IS_EXACT" | "REF_IMPLEMENTS" | "REF_TO_TRAIT" | "REF_CAST" => {
|
||||||
if ops.is_empty() {
|
if ops.is_empty() {
|
||||||
return Err(AsmError::MissingOperand(line.into()));
|
return Err(AsmError::MissingOperand(line.into()));
|
||||||
|
|||||||
@@ -65,14 +65,12 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
|
|||||||
| CoreOpCode::Hostcall
|
| CoreOpCode::Hostcall
|
||||||
| CoreOpCode::DynPack
|
| CoreOpCode::DynPack
|
||||||
| CoreOpCode::DynUnpack
|
| CoreOpCode::DynUnpack
|
||||||
| CoreOpCode::RefIdentityHash
|
|
||||||
| CoreOpCode::DynCanUnpack
|
| CoreOpCode::DynCanUnpack
|
||||||
| CoreOpCode::StructNew
|
| CoreOpCode::StructNew
|
||||||
| CoreOpCode::ArrayNew
|
| CoreOpCode::ArrayNew
|
||||||
| CoreOpCode::ArrayLen
|
| CoreOpCode::ArrayLen
|
||||||
| CoreOpCode::ArrayGet
|
| CoreOpCode::ArrayGet
|
||||||
| CoreOpCode::ArraySet
|
| CoreOpCode::ArraySet => {
|
||||||
| CoreOpCode::ArrayCopy => {
|
|
||||||
let v = u32::from_le_bytes(imm.try_into().unwrap());
|
let v = u32::from_le_bytes(imm.try_into().unwrap());
|
||||||
format!("{}", v)
|
format!("{}", v)
|
||||||
}
|
}
|
||||||
@@ -81,11 +79,6 @@ fn format_operand(op: CoreOpCode, imm: &[u8]) -> String {
|
|||||||
let field = u32::from_le_bytes(imm[4..].try_into().unwrap());
|
let field = u32::from_le_bytes(imm[4..].try_into().unwrap());
|
||||||
format!("type={}, field={}", ty, field)
|
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::RefIsExact
|
||||||
| CoreOpCode::RefImplements
|
| CoreOpCode::RefImplements
|
||||||
| CoreOpCode::RefToTrait
|
| CoreOpCode::RefToTrait
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//! Shared PBX v0 resource limits and structured validation failures.
|
//! Shared PBX v1 resource limits and structured validation failures.
|
||||||
//! Counters are deterministic; a failed reservation never changes usage.
|
//! Counters are deterministic; a failed reservation never changes usage.
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|||||||
@@ -192,10 +192,6 @@ pub enum OpCode {
|
|||||||
RefToTrait = 0x95,
|
RefToTrait = 0x95,
|
||||||
/// REF_CAST: source:TypeId, target:TypeId; requires validated Registry metadata.
|
/// REF_CAST: source:TypeId, target:TypeId; requires validated Registry metadata.
|
||||||
RefCast = 0x96,
|
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.
|
// Registry-backed collection operations. cycles() is their base component.
|
||||||
StructNew = 0xA0,
|
StructNew = 0xA0,
|
||||||
@@ -205,8 +201,6 @@ pub enum OpCode {
|
|||||||
ArrayLen = 0xA4,
|
ArrayLen = 0xA4,
|
||||||
ArrayGet = 0xA5,
|
ArrayGet = 0xA5,
|
||||||
ArraySet = 0xA6,
|
ArraySet = 0xA6,
|
||||||
/// Complete stored groups, overlap-safe; Array TypeId:u32.
|
|
||||||
ArrayCopy = 0xA9,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u16> for OpCode {
|
impl TryFrom<u16> for OpCode {
|
||||||
@@ -273,8 +267,6 @@ impl TryFrom<u16> for OpCode {
|
|||||||
0x94 => Ok(OpCode::RefImplements),
|
0x94 => Ok(OpCode::RefImplements),
|
||||||
0x95 => Ok(OpCode::RefToTrait),
|
0x95 => Ok(OpCode::RefToTrait),
|
||||||
0x96 => Ok(OpCode::RefCast),
|
0x96 => Ok(OpCode::RefCast),
|
||||||
0x97 => Ok(OpCode::RefIdentityHash),
|
|
||||||
0x98 => Ok(OpCode::RefIdentityEq),
|
|
||||||
0xA0 => Ok(OpCode::StructNew),
|
0xA0 => Ok(OpCode::StructNew),
|
||||||
0xA1 => Ok(OpCode::StructGet),
|
0xA1 => Ok(OpCode::StructGet),
|
||||||
0xA2 => Ok(OpCode::StructSet),
|
0xA2 => Ok(OpCode::StructSet),
|
||||||
@@ -282,7 +274,6 @@ impl TryFrom<u16> for OpCode {
|
|||||||
0xA4 => Ok(OpCode::ArrayLen),
|
0xA4 => Ok(OpCode::ArrayLen),
|
||||||
0xA5 => Ok(OpCode::ArrayGet),
|
0xA5 => Ok(OpCode::ArrayGet),
|
||||||
0xA6 => Ok(OpCode::ArraySet),
|
0xA6 => Ok(OpCode::ArraySet),
|
||||||
0xA9 => Ok(OpCode::ArrayCopy),
|
|
||||||
|
|
||||||
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
|
_ => Err(format!("Invalid OpCode: 0x{:04X}", value)),
|
||||||
}
|
}
|
||||||
@@ -353,8 +344,6 @@ impl OpCode {
|
|||||||
OpCode::RefImplements => 4,
|
OpCode::RefImplements => 4,
|
||||||
OpCode::RefToTrait => 5,
|
OpCode::RefToTrait => 5,
|
||||||
OpCode::RefCast => 4,
|
OpCode::RefCast => 4,
|
||||||
OpCode::RefIdentityHash => 8,
|
|
||||||
OpCode::RefIdentityEq => 4,
|
|
||||||
OpCode::StructNew => 8,
|
OpCode::StructNew => 8,
|
||||||
OpCode::StructGet => 3,
|
OpCode::StructGet => 3,
|
||||||
OpCode::StructSet => 4,
|
OpCode::StructSet => 4,
|
||||||
@@ -362,7 +351,6 @@ impl OpCode {
|
|||||||
OpCode::ArrayLen => 3,
|
OpCode::ArrayLen => 3,
|
||||||
OpCode::ArrayGet => 3,
|
OpCode::ArrayGet => 3,
|
||||||
OpCode::ArraySet => 4,
|
OpCode::ArraySet => 4,
|
||||||
OpCode::ArrayCopy => 4,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -596,26 +596,6 @@ impl OpCodeSpecExt for OpCode {
|
|||||||
may_trap: true,
|
may_trap: true,
|
||||||
is_safepoint: false,
|
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 {
|
OpCode::StructNew => OpcodeSpec {
|
||||||
name: "STRUCT_NEW",
|
name: "STRUCT_NEW",
|
||||||
imm_bytes: 4,
|
imm_bytes: 4,
|
||||||
@@ -686,16 +666,6 @@ impl OpCodeSpecExt for OpCode {
|
|||||||
may_trap: true,
|
may_trap: true,
|
||||||
is_safepoint: false,
|
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 {
|
OpCode::FrameSync => OpcodeSpec {
|
||||||
name: "FRAME_SYNC",
|
name: "FRAME_SYNC",
|
||||||
imm_bytes: 0,
|
imm_bytes: 0,
|
||||||
|
|||||||
@@ -52,19 +52,6 @@ pub enum TypeKind {
|
|||||||
Trait(Vec<MethodDecl>),
|
Trait(Vec<MethodDecl>),
|
||||||
Fn(Signature),
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct TypeDecl {
|
pub struct TypeDecl {
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//! Strict PBX v0 envelope and Registry sections. No legacy fallback.
|
//! Strict PBX v1 envelope and Registry sections. No legacy fallback.
|
||||||
//! Envelope parsing is structural only: non-Registry sections and instructions
|
//! Envelope parsing is structural only: non-Registry sections and instructions
|
||||||
//! must be validated by their consumers before an executable image is published.
|
//! must be validated by their consumers before an executable image is published.
|
||||||
use crate::load_limits::{
|
use crate::load_limits::{
|
||||||
@@ -32,7 +32,7 @@ impl<'a> PbxEnvelope<'a> {
|
|||||||
if &bytes[..4] != b"PBX\0" {
|
if &bytes[..4] != b"PBX\0" {
|
||||||
return Err(E::new(C::InvalidFormat, R::Magic));
|
return Err(E::new(C::InvalidFormat, R::Magic));
|
||||||
}
|
}
|
||||||
if bytes[4..6] != [0, 0] {
|
if bytes[4..6] != [1, 0] {
|
||||||
return Err(E::new(C::UnsupportedFormat, R::Version));
|
return Err(E::new(C::UnsupportedFormat, R::Version));
|
||||||
}
|
}
|
||||||
if bytes[6] != 0 {
|
if bytes[6] != 0 {
|
||||||
@@ -314,14 +314,14 @@ impl<'a, 'b> Reader<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Raw section input for a v0 envelope writer. Non-Registry section bytes must
|
/// Raw section input for a v1 envelope writer. Non-Registry section bytes must
|
||||||
/// come from their own validated writers; this API never invents defaults.
|
/// come from their own validated writers; this API never invents defaults.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct SectionData<'a> {
|
pub struct SectionData<'a> {
|
||||||
pub kind: u32,
|
pub kind: u32,
|
||||||
pub bytes: &'a [u8],
|
pub bytes: &'a [u8],
|
||||||
}
|
}
|
||||||
/// Assemble strict v0 sections. The output buffer is charged to temporary load storage.
|
/// Assemble strict v1 sections. The output buffer is charged to temporary load storage.
|
||||||
pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Result<Vec<u8>, E> {
|
||||||
b.transaction(|b| {
|
b.transaction(|b| {
|
||||||
let mut by_kind = [None; 9];
|
let mut by_kind = [None; 9];
|
||||||
@@ -349,7 +349,7 @@ pub fn encode_envelope(sections: &[SectionData<'_>], b: &mut LoadBudget) -> Resu
|
|||||||
b.work(total)?;
|
b.work(total)?;
|
||||||
let mut out = b.vector(total, Q::TemporaryBytes)?;
|
let mut out = b.vector(total, Q::TemporaryBytes)?;
|
||||||
out.extend_from_slice(b"PBX\0");
|
out.extend_from_slice(b"PBX\0");
|
||||||
out.extend_from_slice(&[0, 0, 0, 0]);
|
out.extend_from_slice(&[1, 0, 0, 0]);
|
||||||
out.extend_from_slice(&(count as u32).to_le_bytes());
|
out.extend_from_slice(&(count as u32).to_le_bytes());
|
||||||
out.extend_from_slice(&[0; 20]);
|
out.extend_from_slice(&[0; 20]);
|
||||||
let mut offset = directory_end;
|
let mut offset = directory_end;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use crate::load_limits::IdDomain;
|
|||||||
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
|
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
/// Owned, bounded PBX v0 tables for the executable loader. This is not an
|
/// Owned, bounded PBX v1 tables for the executable loader. This is not an
|
||||||
/// executable image: bindings and instruction/type verification are still due.
|
/// executable image: bindings and instruction/type verification are still due.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct OwnedPbx {
|
pub struct OwnedPbx {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
//! Producer-side serialization of a complete typed module. This writer emits
|
//! Producer-side serialization of a complete typed module. This writer emits
|
||||||
//! only v0; executable consumers still need bindings and code verification.
|
//! only v1; executable consumers still need bindings and code verification.
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::load_limits::IdDomain;
|
use crate::load_limits::IdDomain;
|
||||||
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
|
use crate::model::{ConstantPoolEntry, DebugInfo, Export, SyscallDecl};
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ impl DecodedInstr<'_> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
Op::GetCapture => {
|
Op::GetCapture => {
|
||||||
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex));
|
return Err(LoadFailure::new(LoadCode::InvalidReference, LoadReason::CaptureIndex))
|
||||||
}
|
}
|
||||||
Op::CallTrait => {
|
Op::CallTrait => {
|
||||||
let (ty, method) = self.imm_u32x2().map_err(|_| malformed())?;
|
let (ty, method) = self.imm_u32x2().map_err(|_| malformed())?;
|
||||||
@@ -133,29 +133,6 @@ impl DecodedInstr<'_> {
|
|||||||
};
|
};
|
||||||
(source_layout.storage.slots, pushes)
|
(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::StructNew
|
||||||
| Op::StructGet
|
| Op::StructGet
|
||||||
| Op::StructSet
|
| Op::StructSet
|
||||||
|
|||||||
@@ -34,8 +34,6 @@ 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_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_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_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 {
|
for &(text, bytes, cycles) in cases {
|
||||||
assert_eq!(assemble(text).unwrap(), bytes);
|
assert_eq!(assemble(text).unwrap(), bytes);
|
||||||
@@ -51,8 +49,8 @@ fn all_typed_instructions_match_independent_bytes_and_costs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
decode_next(0, &[0x99, 0]),
|
decode_next(0, &[0x97, 0]),
|
||||||
Err(DecodeError::UnknownOpcode { opcode: 0x99, .. })
|
Err(DecodeError::UnknownOpcode { opcode: 0x97, .. })
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
@@ -70,8 +68,6 @@ fn slot_effects_resolve_semantic_widths_and_reference_categories() {
|
|||||||
("REF_TO_TRAIT source=4, target=6", 1, 2),
|
("REF_TO_TRAIT source=4, target=6", 1, 2),
|
||||||
("REF_TO_TRAIT source=6, target=6", 2, 2),
|
("REF_TO_TRAIT source=6, target=6", 2, 2),
|
||||||
("REF_CAST source=6, target=4", 2, 1),
|
("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();
|
let bytes = assemble(text).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -98,9 +94,6 @@ fn invalid_type_immediates_are_contextual_load_errors() {
|
|||||||
"REF_IS_EXACT source=4, target=6",
|
"REF_IS_EXACT source=4, target=6",
|
||||||
"REF_TO_TRAIT source=4, target=0",
|
"REF_TO_TRAIT source=4, target=0",
|
||||||
"REF_IMPLEMENTS source=0, target=6",
|
"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 bytes = assemble(text).unwrap();
|
||||||
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err();
|
let e = decode_next(0, &bytes).unwrap().registry_stack_effect(®istry).unwrap_err();
|
||||||
@@ -121,10 +114,6 @@ fn assembler_rejects_missing_overflowing_or_ambiguous_type_ids() {
|
|||||||
"REF_CAST source=4",
|
"REF_CAST source=4",
|
||||||
"REF_CAST source=4, target=5, target=6",
|
"REF_CAST source=4, target=5, target=6",
|
||||||
"REF_CAST source=4, trait=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}");
|
assert!(assemble(text).is_err(), "{text}");
|
||||||
}
|
}
|
||||||
@@ -215,62 +204,3 @@ 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(®istry).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(®istry).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());
|
|
||||||
}
|
|
||||||
|
|||||||
+3
-3
@@ -172,7 +172,7 @@ fn hex(s: &str) -> Vec<u8> {
|
|||||||
// syscalls=168/4, types=172/10, implementations=182/4, captures=186/8. Total=194.
|
// syscalls=168/4, types=172/10, implementations=182/4, captures=186/8. Total=194.
|
||||||
fn minimal_bytes() -> Vec<u8> {
|
fn minimal_bytes() -> Vec<u8> {
|
||||||
hex("
|
hex("
|
||||||
50 42 58 00 00 00 00 00 08 00 00 00
|
50 42 58 00 01 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 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
|
00 00 00 00 80 00 00 00 04 00 00 00
|
||||||
01 00 00 00 84 00 00 00 1e 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]
|
#[test]
|
||||||
fn owned_v0_decoder_accepts_independent_bytes_without_publishing_an_image() {
|
fn owned_v1_decoder_accepts_independent_bytes_without_publishing_an_image() {
|
||||||
let mut b = budget();
|
let mut b = budget();
|
||||||
let owned = OwnedPbx::parse(&minimal_bytes(), &mut b).unwrap();
|
let owned = OwnedPbx::parse(&minimal_bytes(), &mut b).unwrap();
|
||||||
assert_eq!(owned.registry.declarations(), minimal_registry().declarations());
|
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() {
|
fn envelope_mutations_have_typed_reasons() {
|
||||||
for (offset, value, code, reason) in [
|
for (offset, value, code, reason) in [
|
||||||
(0, 0, LoadCode::InvalidFormat, LoadReason::Magic),
|
(0, 0, LoadCode::InvalidFormat, LoadReason::Magic),
|
||||||
(4, 1, LoadCode::UnsupportedFormat, LoadReason::Version),
|
(4, 0, LoadCode::UnsupportedFormat, LoadReason::Version),
|
||||||
(6, 1, LoadCode::InvalidFormat, LoadReason::Endianness),
|
(6, 1, LoadCode::InvalidFormat, LoadReason::Endianness),
|
||||||
(7, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
|
(7, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
|
||||||
(31, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
|
(31, 1, LoadCode::InvalidFormat, LoadReason::Reserved),
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
//! Controlled graphics measurement. This is not a cartridge stress-suite replacement.
|
|
||||||
use prometeu_drivers::{Gfx, GlyphBankPoolInstaller, MemoryBanks, hardware::Hardware};
|
|
||||||
use prometeu_hal::color::Color;
|
|
||||||
use prometeu_hal::glyph::Glyph;
|
|
||||||
use prometeu_hal::glyph_bank::{GlyphBank, TileSize};
|
|
||||||
use prometeu_hal::scene_bank::SceneBank;
|
|
||||||
use prometeu_hal::scene_layer::{ParallaxFactor, SceneLayer};
|
|
||||||
use prometeu_hal::scene_viewport_cache::SceneViewportCache;
|
|
||||||
use prometeu_hal::scene_viewport_resolver::SceneViewportResolver;
|
|
||||||
use prometeu_hal::sprite::Sprite;
|
|
||||||
use prometeu_hal::tile::Tile;
|
|
||||||
use prometeu_hal::tilemap::TileMap;
|
|
||||||
use prometeu_hal::{FrameId, OwnedRgba8888Frame, RenderOwnership, app_mode::AppMode};
|
|
||||||
use std::alloc::{GlobalAlloc, Layout, System};
|
|
||||||
use std::sync::{
|
|
||||||
Arc,
|
|
||||||
atomic::{AtomicUsize, Ordering},
|
|
||||||
};
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
struct CountingAllocator;
|
|
||||||
static LIVE: AtomicUsize = AtomicUsize::new(0);
|
|
||||||
static PEAK: AtomicUsize = AtomicUsize::new(0);
|
|
||||||
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
|
|
||||||
// Only the measurement executable pays the atomic allocation instrumentation cost.
|
|
||||||
unsafe impl GlobalAlloc for CountingAllocator {
|
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
|
||||||
let ptr = unsafe { System.alloc(layout) };
|
|
||||||
if !ptr.is_null() {
|
|
||||||
let live = LIVE.fetch_add(layout.size(), Ordering::Relaxed) + layout.size();
|
|
||||||
PEAK.fetch_max(live, Ordering::Relaxed);
|
|
||||||
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
ptr
|
|
||||||
}
|
|
||||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
|
||||||
LIVE.fetch_sub(layout.size(), Ordering::Relaxed);
|
|
||||||
unsafe { System.dealloc(ptr, layout) };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[global_allocator]
|
|
||||||
static ALLOCATOR: CountingAllocator = CountingAllocator;
|
|
||||||
|
|
||||||
fn summary(samples: &mut [u64]) -> serde_json::Value {
|
|
||||||
samples.sort_unstable();
|
|
||||||
let n = samples.len();
|
|
||||||
serde_json::json!({"mean_us": samples.iter().sum::<u64>() as f64 / n as f64 / 1000.0,
|
|
||||||
"p95_us": samples[(n * 95).div_ceil(100) - 1] as f64 / 1000.0,
|
|
||||||
"p99_us": samples[(n * 99).div_ceil(100) - 1] as f64 / 1000.0,
|
|
||||||
"max_us": samples[n-1] as f64 / 1000.0,
|
|
||||||
"over_60hz_budget": samples.iter().filter(|&&v| v > 1_000_000_000 / 60).count()})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let count: usize =
|
|
||||||
std::env::args().nth(1).map(|v| v.parse().expect("frame count")).unwrap_or(600);
|
|
||||||
assert!(count > 0);
|
|
||||||
let (w, h) = (Hardware::W, Hardware::H);
|
|
||||||
let banks = Arc::new(MemoryBanks::new());
|
|
||||||
let mixed = std::env::args().nth(2).as_deref() == Some("mixed");
|
|
||||||
let sizes = if mixed {
|
|
||||||
[TileSize::Size8, TileSize::Size16, TileSize::Size32, TileSize::Size8]
|
|
||||||
} else {
|
|
||||||
[TileSize::Size16; 4]
|
|
||||||
};
|
|
||||||
for (i, size) in sizes.iter().enumerate() {
|
|
||||||
let edge = *size as usize;
|
|
||||||
let mut bank = GlyphBank::new(*size, edge, edge);
|
|
||||||
bank.palettes[0][1] = Color::rgba((i * 47 + 30) as u8, 120, 210, 255);
|
|
||||||
// Transparent holes make all four layers participate in the visible output.
|
|
||||||
for (p, pixel) in bank.pixel_indices.iter_mut().enumerate() {
|
|
||||||
*pixel = u8::from(i == 0 || (p + i) % 3 != 0);
|
|
||||||
}
|
|
||||||
banks.install_glyph_bank(i, Arc::new(bank));
|
|
||||||
}
|
|
||||||
let scene = SceneBank {
|
|
||||||
layers: std::array::from_fn(|i| SceneLayer {
|
|
||||||
active: true,
|
|
||||||
glyph_asset_id: i as i32,
|
|
||||||
tile_size: sizes[i],
|
|
||||||
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
|
|
||||||
tilemap: TileMap {
|
|
||||||
width: 160,
|
|
||||||
height: 100,
|
|
||||||
tiles: vec![
|
|
||||||
Tile {
|
|
||||||
active: true,
|
|
||||||
glyph: Glyph { glyph_id: 0, palette_id: 0 },
|
|
||||||
flip_x: false,
|
|
||||||
flip_y: false,
|
|
||||||
};
|
|
||||||
160 * 100
|
|
||||||
],
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
let min_tile = if mixed { 8 } else { 16 };
|
|
||||||
let (cw, ch) = (w.div_ceil(min_tile) + 5, h.div_ceil(min_tile) + 4);
|
|
||||||
let mut cache = SceneViewportCache::new(&scene, cw, ch);
|
|
||||||
let mut resolver = SceneViewportResolver::new(
|
|
||||||
w as i32,
|
|
||||||
h as i32,
|
|
||||||
cw,
|
|
||||||
ch,
|
|
||||||
min_tile as i32 - 4,
|
|
||||||
min_tile as i32 + 4,
|
|
||||||
);
|
|
||||||
let mut gfx = Gfx::new(w, h, banks);
|
|
||||||
let sprites: Vec<_> = (0..512)
|
|
||||||
.map(|i| Sprite {
|
|
||||||
glyph: Glyph { glyph_id: 0, palette_id: 0 },
|
|
||||||
x: ((i * 37) % w) as i32,
|
|
||||||
y: ((i * 53) % h) as i32,
|
|
||||||
layer: (i % 4) as u8,
|
|
||||||
bank_id: (i % 4) as u8,
|
|
||||||
active: true,
|
|
||||||
flip_x: i % 2 == 0,
|
|
||||||
flip_y: i % 3 == 0,
|
|
||||||
priority: (i % 4) as u8,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
gfx.load_frame_sprites(&sprites);
|
|
||||||
let mut compose = Vec::with_capacity(count);
|
|
||||||
let mut publish = Vec::with_capacity(count);
|
|
||||||
let mut upload = Vec::with_capacity(count);
|
|
||||||
let mut total = Vec::with_capacity(count);
|
|
||||||
let mut rgba = vec![0u8; w * h * 4];
|
|
||||||
let ownership = RenderOwnership::new(1, AppMode::Game, 1);
|
|
||||||
let mut checksum = 0u32;
|
|
||||||
for frame_id in 0..(count + 120) {
|
|
||||||
// Resolve/materialize cache outside the raster timing; total includes it.
|
|
||||||
let start = Instant::now();
|
|
||||||
let update = resolver.update(&scene, (frame_id % 71) as i32, (frame_id % 43) as i32);
|
|
||||||
for request in &update.copy_requests {
|
|
||||||
cache.move_layer_window_to(
|
|
||||||
request.layer_index,
|
|
||||||
request.cache_origin_tile_x,
|
|
||||||
request.cache_origin_tile_y,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
cache.materialize_all_layers(&scene);
|
|
||||||
let t = Instant::now();
|
|
||||||
gfx.render_scene_from_cache(&cache, &update, &[0, 1, 2, 3]);
|
|
||||||
gfx.fill_rect(0, h as i32 - 8, w as i32, 8, Color::WHITE);
|
|
||||||
let raster = t.elapsed().as_nanos() as u64;
|
|
||||||
let t = Instant::now();
|
|
||||||
gfx.present();
|
|
||||||
let frame = OwnedRgba8888Frame::packed(
|
|
||||||
FrameId::new(frame_id as u64),
|
|
||||||
ownership,
|
|
||||||
w,
|
|
||||||
h,
|
|
||||||
gfx.front_buffer().to_vec(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let publication = t.elapsed().as_nanos() as u64;
|
|
||||||
let t = Instant::now();
|
|
||||||
for (pixel, bytes) in frame.pixels.iter().zip(rgba.as_chunks_mut::<4>().0.iter_mut()) {
|
|
||||||
bytes.copy_from_slice(&pixel.to_be_bytes());
|
|
||||||
}
|
|
||||||
std::hint::black_box(&rgba);
|
|
||||||
let staging = t.elapsed().as_nanos() as u64;
|
|
||||||
checksum ^= frame.pixels[(frame_id * 173) % (w * h)];
|
|
||||||
if frame_id >= 120 {
|
|
||||||
compose.push(raster);
|
|
||||||
publish.push(publication);
|
|
||||||
upload.push(staging);
|
|
||||||
total.push(start.elapsed().as_nanos() as u64);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let live = LIVE.load(Ordering::Relaxed);
|
|
||||||
let peak = PEAK.load(Ordering::Relaxed);
|
|
||||||
let allocations = ALLOCATIONS.load(Ordering::Relaxed);
|
|
||||||
println!(
|
|
||||||
"{}",
|
|
||||||
serde_json::json!({"schema":1,"width":w,"height":h,"frames":count,
|
|
||||||
"warmup_frames":120,"tiles":sizes.map(|s|s as u32),"sprites_accepted":512,"layers":4,
|
|
||||||
"framebuffer_bytes":w*h*4,"front_back_bytes":w*h*8,"host_staging_bytes":rgba.len(),
|
|
||||||
"compose":summary(&mut compose),"publish_owned":summary(&mut publish),
|
|
||||||
"rgba_staging":summary(&mut upload),"total":summary(&mut total),
|
|
||||||
"allocated_live_bytes":live,"allocated_peak_bytes":peak,"allocation_count":allocations,
|
|
||||||
"checksum":checksum,"vm_hz":null,"physical_display_hz":null,
|
|
||||||
"limitations":"unpaced controlled raster/cache/owned publication/staging; no VM, IPC or GPU present; allocator instrumented; RSS is measured by the process wrapper"})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -664,7 +664,7 @@ mod tests {
|
|||||||
assert!(frame_composer.bind_scene(0));
|
assert!(frame_composer.bind_scene(0));
|
||||||
|
|
||||||
let cache = frame_composer.cache().expect("cache should exist for bound scene");
|
let cache = frame_composer.cache().expect("cache should exist for bound scene");
|
||||||
assert_eq!((cache.width(), cache.height()), (85, 49));
|
assert_eq!((cache.width(), cache.height()), (65, 38));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -43,8 +43,6 @@ pub struct Gfx {
|
|||||||
front: Vec<u32>,
|
front: Vec<u32>,
|
||||||
/// Back buffer: the working buffer where canonical game frames are composed
|
/// Back buffer: the working buffer where canonical game frames are composed
|
||||||
back: Vec<u32>,
|
back: Vec<u32>,
|
||||||
/// Complete local framebuffer publications, including native Hub/Shell output.
|
|
||||||
publication_count: u64,
|
|
||||||
|
|
||||||
/// Shared access to graphical memory banks (tiles and palettes).
|
/// Shared access to graphical memory banks (tiles and palettes).
|
||||||
pub glyph_banks: Arc<dyn GlyphBankPoolAccess>,
|
pub glyph_banks: Arc<dyn GlyphBankPoolAccess>,
|
||||||
@@ -323,7 +321,6 @@ impl Gfx {
|
|||||||
h,
|
h,
|
||||||
front: vec![0; len],
|
front: vec![0; len],
|
||||||
back: vec![0; len],
|
back: vec![0; len],
|
||||||
publication_count: 0,
|
|
||||||
glyph_banks,
|
glyph_banks,
|
||||||
sprites: [EMPTY_SPRITE; 512],
|
sprites: [EMPTY_SPRITE; 512],
|
||||||
sprite_count: 0,
|
sprite_count: 0,
|
||||||
@@ -573,11 +570,6 @@ impl Gfx {
|
|||||||
/// Typically called by the Host when it's time to display the finished frame.
|
/// Typically called by the Host when it's time to display the finished frame.
|
||||||
pub fn present(&mut self) {
|
pub fn present(&mut self) {
|
||||||
std::mem::swap(&mut self.front, &mut self.back);
|
std::mem::swap(&mut self.front, &mut self.back);
|
||||||
self.publication_count = self.publication_count.wrapping_add(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn publication_count(&self) -> u64 {
|
|
||||||
self.publication_count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_frame_sprites(&mut self, sprites: &[Sprite]) {
|
pub fn load_frame_sprites(&mut self, sprites: &[Sprite]) {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ use std::sync::Arc;
|
|||||||
/// for easier testing and different host implementations (Desktop, Web, etc.).
|
/// for easier testing and different host implementations (Desktop, Web, etc.).
|
||||||
///
|
///
|
||||||
/// ### Console Specifications:
|
/// ### Console Specifications:
|
||||||
/// - **Resolution**: 640x360 (16:9 Aspect Ratio).
|
/// - **Resolution**: 480x270 (16:9 Aspect Ratio).
|
||||||
/// - **Color Format**: RGBA8888.
|
/// - **Color Format**: RGBA8888.
|
||||||
/// - **Audio**: Stereo, Command-based mixing.
|
/// - **Audio**: Stereo, Command-based mixing.
|
||||||
/// - **Input**: 12-button Digital Gamepad + Absolute Touch/Mouse.
|
/// - **Input**: 12-button Digital Gamepad + Absolute Touch/Mouse.
|
||||||
@@ -201,9 +201,9 @@ impl RuntimePlatform for Hardware {
|
|||||||
|
|
||||||
impl Hardware {
|
impl Hardware {
|
||||||
/// Internal hardware width in pixels.
|
/// Internal hardware width in pixels.
|
||||||
pub const W: usize = prometeu_hal::graphics_contract::WIDTH;
|
pub const W: usize = 480;
|
||||||
/// Internal hardware height in pixels.
|
/// Internal hardware height in pixels.
|
||||||
pub const H: usize = prometeu_hal::graphics_contract::HEIGHT;
|
pub const H: usize = 270;
|
||||||
|
|
||||||
/// Creates a fresh hardware instance with default settings.
|
/// Creates a fresh hardware instance with default settings.
|
||||||
pub fn new(wall_clock: Arc<dyn WallClock>) -> Self {
|
pub fn new(wall_clock: Arc<dyn WallClock>) -> Self {
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ mod gfx;
|
|||||||
pub mod hardware;
|
pub mod hardware;
|
||||||
mod local_render_backend;
|
mod local_render_backend;
|
||||||
mod memory_banks;
|
mod memory_banks;
|
||||||
#[cfg(test)]
|
|
||||||
mod native_graphics_tests;
|
|
||||||
mod pad;
|
mod pad;
|
||||||
mod test_platform;
|
mod test_platform;
|
||||||
mod touch;
|
mod touch;
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ fn io_error(e: std::io::Error) -> Error {
|
|||||||
use std::io::ErrorKind as K;
|
use std::io::ErrorKind as K;
|
||||||
match e.kind() {
|
match e.kind() {
|
||||||
K::NotFound => Error::Unavailable,
|
K::NotFound => Error::Unavailable,
|
||||||
K::PermissionDenied => Error::AccessDenied,
|
K::PermissionDenied => Error::ReadOnly,
|
||||||
K::AlreadyExists => Error::AlreadyExists,
|
|
||||||
K::StorageFull => Error::NoSpace,
|
K::StorageFull => Error::NoSpace,
|
||||||
K::InvalidInput | K::UnexpectedEof => Error::InvalidRange,
|
K::InvalidInput | K::UnexpectedEof => Error::InvalidRange,
|
||||||
K::Unsupported => Error::Unsupported,
|
K::Unsupported => Error::Unsupported,
|
||||||
@@ -22,15 +21,8 @@ fn io_error(e: std::io::Error) -> Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl FileMemcard {
|
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> {
|
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)?;
|
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();
|
let length = file.metadata().map_err(io_error)?.len();
|
||||||
Ok(Self { file, length, read_only })
|
Ok(Self { file, length, read_only })
|
||||||
}
|
}
|
||||||
@@ -55,9 +47,7 @@ impl FileMemcard {
|
|||||||
.create_new(true)
|
.create_new(true)
|
||||||
.open(&temporary)
|
.open(&temporary)
|
||||||
.map_err(io_error)?;
|
.map_err(io_error)?;
|
||||||
let mut published = false;
|
|
||||||
let result = (|| {
|
let result = (|| {
|
||||||
Self::lock(&file)?;
|
|
||||||
let mut card = Self { file, length, read_only: false };
|
let mut card = Self { file, length, read_only: false };
|
||||||
let zeros = [0u8; 65_536];
|
let zeros = [0u8; 65_536];
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
@@ -74,7 +64,6 @@ impl FileMemcard {
|
|||||||
card.sync()?;
|
card.sync()?;
|
||||||
// hard_link publishes without replacing a user file; parent sync makes the name durable.
|
// hard_link publishes without replacing a user file; parent sync makes the name durable.
|
||||||
std::fs::hard_link(&temporary, path).map_err(io_error)?;
|
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)?;
|
File::open(parent).and_then(|f| f.sync_all()).map_err(io_error)?;
|
||||||
Ok(card)
|
Ok(card)
|
||||||
})();
|
})();
|
||||||
@@ -82,10 +71,10 @@ impl FileMemcard {
|
|||||||
std::fs::remove_file(&temporary).and_then(|()| File::open(parent)?.sync_all());
|
std::fs::remove_file(&temporary).and_then(|()| File::open(parent)?.sync_all());
|
||||||
match result {
|
match result {
|
||||||
Ok(card) => {
|
Ok(card) => {
|
||||||
cleanup.map_err(|_| Error::Indeterminate)?;
|
cleanup.map_err(io_error)?;
|
||||||
Ok(card)
|
Ok(card)
|
||||||
}
|
}
|
||||||
Err(e) => Err(if published { Error::Indeterminate } else { e }),
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
|
|||||||
@@ -1,109 +0,0 @@
|
|||||||
use crate::{Gfx, GlyphBankPoolInstaller, MemoryBanks};
|
|
||||||
use prometeu_hal::color::Color;
|
|
||||||
use prometeu_hal::glyph::Glyph;
|
|
||||||
use prometeu_hal::glyph_bank::{GlyphBank, TileSize};
|
|
||||||
use prometeu_hal::graphics_contract::{FRAME_BYTES, HEIGHT, PIXELS, WIDTH};
|
|
||||||
use prometeu_hal::scene_bank::SceneBank;
|
|
||||||
use prometeu_hal::scene_layer::{ParallaxFactor, SceneLayer};
|
|
||||||
use prometeu_hal::scene_viewport_cache::SceneViewportCache;
|
|
||||||
use prometeu_hal::scene_viewport_resolver::SceneViewportResolver;
|
|
||||||
use prometeu_hal::tile::Tile;
|
|
||||||
use prometeu_hal::tilemap::TileMap;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn native_allocation_stride_corners_and_external_coordinates() {
|
|
||||||
let mut gfx = Gfx::new(WIDTH, HEIGHT, Arc::new(MemoryBanks::new()));
|
|
||||||
assert_eq!(gfx.publication_count(), 0);
|
|
||||||
gfx.clear(Color::BLACK);
|
|
||||||
for (x, y) in [(0, 0), (639, 0), (0, 359), (639, 359)] {
|
|
||||||
gfx.draw_pixel(x, y, Color::RED);
|
|
||||||
}
|
|
||||||
for (x, y) in [(-1, 0), (640, 359), (639, 360), (0, -1)] {
|
|
||||||
gfx.draw_pixel(x, y, Color::GREEN);
|
|
||||||
}
|
|
||||||
assert_eq!(gfx.publication_count(), 0, "drawing is not a complete publication");
|
|
||||||
gfx.present();
|
|
||||||
assert_eq!(gfx.publication_count(), 1);
|
|
||||||
let front = gfx.front_buffer();
|
|
||||||
assert_eq!(gfx.size(), (640, 360));
|
|
||||||
assert_eq!(front.len(), PIXELS);
|
|
||||||
assert_eq!(std::mem::size_of_val(front), FRAME_BYTES);
|
|
||||||
for index in [0, 639, 359 * WIDTH, PIXELS - 1] {
|
|
||||||
assert_eq!(front[index], Color::RED.raw());
|
|
||||||
}
|
|
||||||
assert_eq!(front.iter().filter(|&&p| p == Color::RED.raw()).count(), 4);
|
|
||||||
gfx.present();
|
|
||||||
assert_eq!(gfx.publication_count(), 2, "each complete swap is a publication");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn native_tile_edges_and_mixed_camera_layers_cover_every_corner() {
|
|
||||||
for sizes in [
|
|
||||||
[TileSize::Size8; 4],
|
|
||||||
[TileSize::Size16; 4],
|
|
||||||
[TileSize::Size32; 4],
|
|
||||||
[TileSize::Size8, TileSize::Size16, TileSize::Size32, TileSize::Size8],
|
|
||||||
] {
|
|
||||||
let banks = Arc::new(MemoryBanks::new());
|
|
||||||
let colors = [Color::RED, Color::GREEN, Color::BLUE, Color::WHITE];
|
|
||||||
for i in 0..4 {
|
|
||||||
let n = sizes[i] as usize;
|
|
||||||
let mut bank = GlyphBank::new(sizes[i], n, n);
|
|
||||||
bank.palettes[0][1] = colors[i];
|
|
||||||
bank.pixel_indices.fill(1);
|
|
||||||
banks.install_glyph_bank(i, Arc::new(bank));
|
|
||||||
}
|
|
||||||
let scene = SceneBank {
|
|
||||||
layers: std::array::from_fn(|i| SceneLayer {
|
|
||||||
active: true,
|
|
||||||
glyph_asset_id: i as i32,
|
|
||||||
tile_size: sizes[i],
|
|
||||||
parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 },
|
|
||||||
tilemap: TileMap {
|
|
||||||
width: 160,
|
|
||||||
height: 120,
|
|
||||||
tiles: vec![
|
|
||||||
Tile {
|
|
||||||
active: true,
|
|
||||||
glyph: Glyph { glyph_id: 0, palette_id: 0 },
|
|
||||||
flip_x: true,
|
|
||||||
flip_y: true,
|
|
||||||
};
|
|
||||||
160 * 120
|
|
||||||
],
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
let min = sizes.iter().map(|s| *s as usize).min().unwrap();
|
|
||||||
let (cw, ch) = (WIDTH.div_ceil(min) + 5, HEIGHT.div_ceil(min) + 4);
|
|
||||||
let mut cache = SceneViewportCache::new(&scene, cw, ch);
|
|
||||||
let mut resolver = SceneViewportResolver::new(
|
|
||||||
WIDTH as i32,
|
|
||||||
HEIGHT as i32,
|
|
||||||
cw,
|
|
||||||
ch,
|
|
||||||
min as i32 - 4,
|
|
||||||
min as i32 + 4,
|
|
||||||
);
|
|
||||||
let mut gfx = Gfx::new(WIDTH, HEIGHT, banks);
|
|
||||||
for (x, y) in [(0, 0), (1, 1), (7, 7), (15, 15), (31, 31), (400, 300), (2, 3)] {
|
|
||||||
let update = resolver.update(&scene, x, y);
|
|
||||||
for request in &update.copy_requests {
|
|
||||||
cache.move_layer_window_to(
|
|
||||||
request.layer_index,
|
|
||||||
request.cache_origin_tile_x,
|
|
||||||
request.cache_origin_tile_y,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
cache.materialize_all_layers(&scene);
|
|
||||||
gfx.render_scene_from_cache(&cache, &update, &[0, 1, 2, 3]);
|
|
||||||
gfx.present();
|
|
||||||
// Every pixel, including rows 352..359, must be supplied by the last layer.
|
|
||||||
assert!(
|
|
||||||
gfx.front_buffer().iter().all(|&p| p == Color::WHITE.raw()),
|
|
||||||
"sizes={sizes:?}, camera=({x},{y})"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -18,8 +18,6 @@ impl ControlledWallClock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Advance civil time explicitly, saturating at the largest timestamp.
|
/// 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) {
|
pub fn advance(&self, seconds: u64) {
|
||||||
// Harness control may retry on a concurrent update; reads remain a single load.
|
// Harness control may retry on a concurrent update; reads remain a single load.
|
||||||
let _ = self.seconds.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
|
let _ = self.seconds.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |current| {
|
||||||
|
|||||||
@@ -609,7 +609,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn front_buffer_pixel(platform: &TestPlatform, x: i32, y: i32) -> u32 {
|
fn front_buffer_pixel(platform: &TestPlatform, x: i32, y: i32) -> u32 {
|
||||||
const FRAMEBUFFER_WIDTH: usize = prometeu_hal::graphics_contract::WIDTH;
|
const FRAMEBUFFER_WIDTH: usize = 480;
|
||||||
let index = x as usize + y as usize * FRAMEBUFFER_WIDTH;
|
let index = x as usize + y as usize * FRAMEBUFFER_WIDTH;
|
||||||
platform.local_hardware().gfx.front_buffer()[index]
|
platform.local_hardware().gfx.front_buffer()[index]
|
||||||
}
|
}
|
||||||
@@ -1094,9 +1094,7 @@ mod tests {
|
|||||||
assert_eq!(dummy_ownership.app_id, 2);
|
assert_eq!(dummy_ownership.app_id, 2);
|
||||||
|
|
||||||
let front = platform.local_hardware().gfx.front_buffer();
|
let front = platform.local_hardware().gfx.front_buffer();
|
||||||
let center = prometeu_hal::graphics_contract::WIDTH / 2
|
let center = 240 + 135 * 480;
|
||||||
+ (prometeu_hal::graphics_contract::HEIGHT / 2)
|
|
||||||
* prometeu_hal::graphics_contract::WIDTH;
|
|
||||||
assert_eq!(front[center], Color::GREEN.raw());
|
assert_eq!(front[center], Color::GREEN.raw());
|
||||||
|
|
||||||
firmware.request_home_from_host();
|
firmware.request_home_from_host();
|
||||||
@@ -1514,12 +1512,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn shell_running_close_click_uses_lifecycle_and_returns_hub_home() {
|
fn shell_running_close_click_uses_lifecycle_and_returns_hub_home() {
|
||||||
let (mut firmware, mut platform, _signals, task_id) = load_shell_running_firmware();
|
let (mut firmware, mut platform, _signals, task_id) = load_shell_running_firmware();
|
||||||
let close_signals = InputSignals {
|
let close_signals =
|
||||||
f_signal: true,
|
InputSignals { f_signal: true, x_pos: 420, y_pos: 24, ..Default::default() };
|
||||||
x_pos: prometeu_hal::graphics_contract::WIDTH as i32 - 60,
|
|
||||||
y_pos: 24,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
firmware.tick(&close_signals, &mut platform);
|
firmware.tick(&close_signals, &mut platform);
|
||||||
|
|
||||||
|
|||||||
@@ -47,12 +47,7 @@ impl HubHomeStep {
|
|||||||
let id = ctx.os.windows().add_window(
|
let id = ctx.os.windows().add_window(
|
||||||
app.title().to_string(),
|
app.title().to_string(),
|
||||||
WindowOwner::Task(task_id),
|
WindowOwner::Task(task_id),
|
||||||
Rect {
|
Rect { x: 40, y: 20, w: 400, h: 220 },
|
||||||
x: 40,
|
|
||||||
y: 20,
|
|
||||||
w: prometeu_hal::graphics_contract::WIDTH as i32 - 80,
|
|
||||||
h: prometeu_hal::graphics_contract::HEIGHT as i32 - 50,
|
|
||||||
},
|
|
||||||
app.color(),
|
app.color(),
|
||||||
);
|
);
|
||||||
ctx.os.windows().set_focus(id);
|
ctx.os.windows().set_focus(id);
|
||||||
|
|||||||
@@ -48,25 +48,6 @@ impl LoadCartridgeStep {
|
|||||||
pub fn needs_memcard_choice(&self) -> bool {
|
pub fn needs_memcard_choice(&self) -> bool {
|
||||||
self.awaiting_memcard && !self.preparing_memcard
|
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 {
|
pub fn new_game_switch(cartridge: Cartridge) -> Self {
|
||||||
Self { failure_recovery: LoadCartridgeFailureRecovery::ReturnToHub, ..Self::new(cartridge) }
|
Self { failure_recovery: LoadCartridgeFailureRecovery::ReturnToHub, ..Self::new(cartridge) }
|
||||||
@@ -147,8 +128,13 @@ impl LoadCartridgeStep {
|
|||||||
Choice::MemoryExisting
|
Choice::MemoryExisting
|
||||||
};
|
};
|
||||||
let created_at = ctx.platform.wall_clock().utc_unix_seconds();
|
let created_at = ctx.platform.wall_clock().utc_unix_seconds();
|
||||||
match self.choose_memcard(ctx.os, choice, created_at) {
|
match ctx.os.memcard().choose_for_game(
|
||||||
Ok(()) => {}
|
self.cartridge.app_id,
|
||||||
|
self.cartridge.memcard_slots.unwrap() as usize,
|
||||||
|
choice,
|
||||||
|
created_at,
|
||||||
|
) {
|
||||||
|
Ok(()) => self.preparing_memcard = true,
|
||||||
Err(error) => self.memcard_message = Self::preparation_message(error),
|
Err(error) => self.memcard_message = Self::preparation_message(error),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
//! Fixed machine graphics contract. Host surfaces never change these dimensions.
|
|
||||||
pub const WIDTH: usize = 640;
|
|
||||||
pub const HEIGHT: usize = 360;
|
|
||||||
pub const REFRESH_HZ: u32 = 60;
|
|
||||||
/// Canonical RGBA8888, with tightly packed native rows.
|
|
||||||
pub const BYTES_PER_PIXEL: usize = 4;
|
|
||||||
pub const PIXELS: usize = WIDTH * HEIGHT;
|
|
||||||
pub const STRIDE_PIXELS: usize = WIDTH;
|
|
||||||
pub const STRIDE_BYTES: usize = STRIDE_PIXELS * BYTES_PER_PIXEL;
|
|
||||||
pub const FRAME_BYTES: usize = STRIDE_BYTES * HEIGHT;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
#[test]
|
|
||||||
fn native_graphics_contract_is_fixed() {
|
|
||||||
assert_eq!((WIDTH, HEIGHT, REFRESH_HZ), (640, 360, 60));
|
|
||||||
assert_eq!((PIXELS, STRIDE_BYTES, FRAME_BYTES), (230_400, 2_560, 921_600));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,6 @@ pub mod debugger_protocol;
|
|||||||
pub mod gfx_bridge;
|
pub mod gfx_bridge;
|
||||||
pub mod glyph;
|
pub mod glyph;
|
||||||
pub mod glyph_bank;
|
pub mod glyph_bank;
|
||||||
pub mod graphics_contract;
|
|
||||||
pub mod host_context;
|
pub mod host_context;
|
||||||
pub mod host_return;
|
pub mod host_return;
|
||||||
pub mod input_signals;
|
pub mod input_signals;
|
||||||
|
|||||||
@@ -57,21 +57,6 @@ impl LogService {
|
|||||||
self.events.iter().filter(|e| e.seq > seq).cloned().collect()
|
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> {
|
pub fn last_seq(&self) -> Option<u64> {
|
||||||
self.events.back().map(|e| e.seq)
|
self.events.back().map(|e| e.seq)
|
||||||
}
|
}
|
||||||
@@ -122,35 +107,4 @@ mod tests {
|
|||||||
assert_eq!(after[0].msg, "Log 3");
|
assert_eq!(after[0].msg, "Log 3");
|
||||||
assert_eq!(after[1].msg, "Log 4");
|
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,10 +6,6 @@ pub enum MemcardIoError {
|
|||||||
InvalidRange,
|
InvalidRange,
|
||||||
NoSpace,
|
NoSpace,
|
||||||
Unsupported,
|
Unsupported,
|
||||||
Busy,
|
|
||||||
AccessDenied,
|
|
||||||
AlreadyExists,
|
|
||||||
Indeterminate,
|
|
||||||
IoError,
|
IoError,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ pub trait RuntimePlatform {
|
|||||||
fn wall_clock(&self) -> &dyn WallClock;
|
fn wall_clock(&self) -> &dyn WallClock;
|
||||||
|
|
||||||
fn display_size(&self) -> (usize, usize) {
|
fn display_size(&self) -> (usize, usize) {
|
||||||
(crate::graphics_contract::WIDTH, crate::graphics_contract::HEIGHT)
|
(480, 270)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_submission_sink(&mut self) -> &mut dyn RenderSubmissionSink;
|
fn render_submission_sink(&mut self) -> &mut dyn RenderSubmissionSink;
|
||||||
|
|||||||
@@ -224,8 +224,6 @@ impl SceneViewportResolver {
|
|||||||
scene_height_tiles: usize,
|
scene_height_tiles: usize,
|
||||||
) -> TileAnchor {
|
) -> TileAnchor {
|
||||||
let mut next = current;
|
let mut next = current;
|
||||||
// Mixed layers must not step past the center and immediately step back.
|
|
||||||
let trigger = self.hysteresis_trigger_px.max(tile_size_px / 2);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let center_x_px = next.x * tile_size_px + tile_size_px / 2;
|
let center_x_px = next.x * tile_size_px + tile_size_px / 2;
|
||||||
@@ -233,11 +231,11 @@ impl SceneViewportResolver {
|
|||||||
if drift_x.abs() <= self.hysteresis_safe_px {
|
if drift_x.abs() <= self.hysteresis_safe_px {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if drift_x > trigger {
|
if drift_x > self.hysteresis_trigger_px {
|
||||||
next.x += 1;
|
next.x += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if drift_x < -trigger {
|
if drift_x < -self.hysteresis_trigger_px {
|
||||||
next.x -= 1;
|
next.x -= 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -250,11 +248,11 @@ impl SceneViewportResolver {
|
|||||||
if drift_y.abs() <= self.hysteresis_safe_px {
|
if drift_y.abs() <= self.hysteresis_safe_px {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if drift_y > trigger {
|
if drift_y > self.hysteresis_trigger_px {
|
||||||
next.y += 1;
|
next.y += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if drift_y < -trigger {
|
if drift_y < -self.hysteresis_trigger_px {
|
||||||
next.y -= 1;
|
next.y -= 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -435,34 +433,20 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn first_update_initializes_master_and_layer_anchors() {
|
fn first_update_initializes_master_and_layer_anchors() {
|
||||||
let scene = make_scene();
|
let scene = make_scene();
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let update = resolver.update(&scene, 0, 0);
|
let update = resolver.update(&scene, 0, 0);
|
||||||
|
|
||||||
assert_eq!(update.master_anchor, TileAnchor { x: 22, y: 13 });
|
assert_eq!(update.master_anchor, TileAnchor { x: 17, y: 10 });
|
||||||
assert_eq!(update.layer_anchors[0], TileAnchor { x: 22, y: 13 });
|
assert_eq!(update.layer_anchors[0], TileAnchor { x: 17, y: 10 });
|
||||||
assert_eq!(update.layer_anchors[1], TileAnchor { x: 22, y: 13 });
|
assert_eq!(update.layer_anchors[1], TileAnchor { x: 17, y: 10 });
|
||||||
assert_eq!(update.refresh_requests.len(), 4);
|
assert_eq!(update.refresh_requests.len(), 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn per_layer_copy_requests_follow_parallax_factor() {
|
fn per_layer_copy_requests_follow_parallax_factor() {
|
||||||
let scene = make_scene();
|
let scene = make_scene();
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let update = resolver.update(&scene, 32, 48);
|
let update = resolver.update(&scene, 32, 48);
|
||||||
|
|
||||||
@@ -477,38 +461,24 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn hysteresis_prevents_small_back_and_forth_refresh_churn() {
|
fn hysteresis_prevents_small_back_and_forth_refresh_churn() {
|
||||||
let scene = make_scene();
|
let scene = make_scene();
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let _ = resolver.update(&scene, 0, 0);
|
let _ = resolver.update(&scene, 0, 0);
|
||||||
let update = resolver.update(&scene, 8, 0);
|
let update = resolver.update(&scene, 8, 0);
|
||||||
|
|
||||||
assert!(update.refresh_requests.is_empty());
|
assert!(update.refresh_requests.is_empty());
|
||||||
assert_eq!(update.master_anchor, TileAnchor { x: 22, y: 13 });
|
assert_eq!(update.master_anchor, TileAnchor { x: 17, y: 10 });
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn repeated_high_speed_movement_advances_in_tile_steps() {
|
fn repeated_high_speed_movement_advances_in_tile_steps() {
|
||||||
let scene = make_scene();
|
let scene = make_scene();
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let _ = resolver.update(&scene, 0, 0);
|
let _ = resolver.update(&scene, 0, 0);
|
||||||
let update = resolver.update(&scene, 64, 0);
|
let update = resolver.update(&scene, 64, 0);
|
||||||
|
|
||||||
assert!(update.master_anchor.x > 22);
|
assert!(update.master_anchor.x > 17);
|
||||||
assert!(update.refresh_requests.iter().any(|request| matches!(
|
assert!(update.refresh_requests.iter().any(|request| matches!(
|
||||||
request,
|
request,
|
||||||
CacheRefreshRequest::RefreshColumn { layer_index: 0, .. }
|
CacheRefreshRequest::RefreshColumn { layer_index: 0, .. }
|
||||||
@@ -519,32 +489,18 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn anchors_clamp_near_scene_edges() {
|
fn anchors_clamp_near_scene_edges() {
|
||||||
let scene = make_scene();
|
let scene = make_scene();
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let update = resolver.update(&scene, 10_000, 10_000);
|
let update = resolver.update(&scene, 10_000, 10_000);
|
||||||
|
|
||||||
assert_eq!(update.master_anchor, TileAnchor { x: 41, y: 50 });
|
assert_eq!(update.master_anchor, TileAnchor { x: 46, y: 53 });
|
||||||
assert_eq!(update.layer_anchors[0], TileAnchor { x: 41, y: 50 });
|
assert_eq!(update.layer_anchors[0], TileAnchor { x: 46, y: 53 });
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn corner_trigger_converts_to_non_overlapping_region_requests() {
|
fn corner_trigger_converts_to_non_overlapping_region_requests() {
|
||||||
let scene = make_scene();
|
let scene = make_scene();
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let _ = resolver.update(&scene, 0, 0);
|
let _ = resolver.update(&scene, 0, 0);
|
||||||
let update = resolver.update(&scene, 64, 80);
|
let update = resolver.update(&scene, 64, 80);
|
||||||
@@ -559,22 +515,15 @@ mod tests {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
assert_eq!(regions.len(), 2);
|
assert_eq!(regions.len(), 2);
|
||||||
assert_eq!(regions[0].x + regions[0].width, 45);
|
assert_eq!(regions[0].x + regions[0].width, 35);
|
||||||
assert_eq!(regions[0].height, 27);
|
assert_eq!(regions[0].height, 21);
|
||||||
assert_eq!(regions[1].width, 44);
|
assert_eq!(regions[1].width, 34);
|
||||||
assert_eq!(regions[1].height, 2);
|
assert_eq!(regions[1].height, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn reset_scene_requests_full_invalidation_for_all_layers() {
|
fn reset_scene_requests_full_invalidation_for_all_layers() {
|
||||||
let mut resolver = SceneViewportResolver::new(
|
let mut resolver = SceneViewportResolver::new(480, 270, 35, 21, 12, 20);
|
||||||
crate::graphics_contract::WIDTH as i32,
|
|
||||||
crate::graphics_contract::HEIGHT as i32,
|
|
||||||
45,
|
|
||||||
27,
|
|
||||||
12,
|
|
||||||
20,
|
|
||||||
);
|
|
||||||
|
|
||||||
let requests = resolver.reset_scene();
|
let requests = resolver.reset_scene();
|
||||||
|
|
||||||
|
|||||||
@@ -385,14 +385,6 @@ impl<'a> VmFacade<'a> {
|
|||||||
self.os.vm_runtime.record_repeated_render_worker_frame(frame_id);
|
self.os.vm_runtime.record_repeated_render_worker_frame(frame_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn record_presented_render_worker_frame(
|
|
||||||
&self,
|
|
||||||
ownership: RenderOwnership,
|
|
||||||
frame_id: FrameId,
|
|
||||||
) {
|
|
||||||
self.os.vm_runtime.record_presented_render_worker_frame(ownership, frame_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn active_render_ownership(&self) -> RenderOwnership {
|
pub fn active_render_ownership(&self) -> RenderOwnership {
|
||||||
if let Some(task_id) = self.foreground_session_task_id()
|
if let Some(task_id) = self.foreground_session_task_id()
|
||||||
&& let Some(session) = self.os.vm_sessions.for_task(task_id)
|
&& let Some(session) = self.os.vm_sessions.for_task(task_id)
|
||||||
|
|||||||
@@ -64,14 +64,6 @@ impl SystemOS {
|
|||||||
pub fn memcard(&mut self) -> &mut MemcardService {
|
pub fn memcard(&mut self) -> &mut MemcardService {
|
||||||
&mut self.memcard
|
&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.
|
/// Host/player operations are admitted only while gameplay is suspended.
|
||||||
pub fn replace_memcard(
|
pub fn replace_memcard(
|
||||||
@@ -170,16 +162,6 @@ impl SystemOS {
|
|||||||
self.log_service.get_after(seq)
|
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 {
|
pub fn frame_index(&mut self) -> u64 {
|
||||||
self.vm().logical_frame_index()
|
self.vm().logical_frame_index()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,41 +10,19 @@ use prometeu_hal::{
|
|||||||
|
|
||||||
const SHELL_A_BUTTON: Rect = Rect { x: 112, y: 172, w: 112, h: 32 };
|
const SHELL_A_BUTTON: Rect = Rect { x: 112, y: 172, w: 112, h: 32 };
|
||||||
const SHELL_B_BUTTON: Rect = Rect { x: 256, y: 172, w: 112, h: 32 };
|
const SHELL_B_BUTTON: Rect = Rect { x: 256, y: 172, w: 112, h: 32 };
|
||||||
const CLOSE_BUTTON: Rect =
|
const CLOSE_BUTTON: Rect = Rect { x: 420, y: 24, w: 16, h: 16 };
|
||||||
Rect { x: prometeu_hal::graphics_contract::WIDTH as i32 - 60, y: 24, w: 16, h: 16 };
|
const VIEWPORT: Rect = Rect { x: 0, y: 0, w: 480, h: 270 };
|
||||||
const VIEWPORT: Rect = Rect {
|
const HOME_PANEL: Rect = Rect { x: 48, y: 58, w: 384, h: 154 };
|
||||||
x: 0,
|
const TITLE_PANEL: Rect = Rect { x: 72, y: 34, w: 336, h: 28 };
|
||||||
y: 0,
|
|
||||||
w: prometeu_hal::graphics_contract::WIDTH as i32,
|
|
||||||
h: prometeu_hal::graphics_contract::HEIGHT as i32,
|
|
||||||
};
|
|
||||||
const HOME_PANEL: Rect = Rect {
|
|
||||||
x: 48,
|
|
||||||
y: 58,
|
|
||||||
w: prometeu_hal::graphics_contract::WIDTH as i32 - 96,
|
|
||||||
h: prometeu_hal::graphics_contract::HEIGHT as i32 - 116,
|
|
||||||
};
|
|
||||||
const TITLE_PANEL: Rect =
|
|
||||||
Rect { x: 72, y: 34, w: prometeu_hal::graphics_contract::WIDTH as i32 - 144, h: 28 };
|
|
||||||
const GAME_ROW_X: i32 = 72;
|
const GAME_ROW_X: i32 = 72;
|
||||||
const GAME_ROW_Y: i32 = 90;
|
const GAME_ROW_Y: i32 = 90;
|
||||||
const GAME_ROW_W: i32 = prometeu_hal::graphics_contract::WIDTH as i32 - 144;
|
const GAME_ROW_W: i32 = 336;
|
||||||
const GAME_ROW_H: i32 = 22;
|
const GAME_ROW_H: i32 = 22;
|
||||||
const GAME_ROW_GAP: i32 = 6;
|
const GAME_ROW_GAP: i32 = 6;
|
||||||
const MAX_HOME_GAME_ROWS: usize = 3;
|
const MAX_HOME_GAME_ROWS: usize = 3;
|
||||||
pub const HUB_SUSPENDED_GAME_KILL_CONTROL: Rect = Rect {
|
pub const HUB_SUSPENDED_GAME_KILL_CONTROL: Rect = Rect { x: 288, y: 230, w: 168, h: 24 };
|
||||||
x: prometeu_hal::graphics_contract::WIDTH as i32 - 192,
|
|
||||||
y: prometeu_hal::graphics_contract::HEIGHT as i32 - 40,
|
|
||||||
w: 168,
|
|
||||||
h: 24,
|
|
||||||
};
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
const SHELL_FRAME: Rect = Rect {
|
const SHELL_FRAME: Rect = Rect { x: 40, y: 20, w: 400, h: 220 };
|
||||||
x: 40,
|
|
||||||
y: 20,
|
|
||||||
w: prometeu_hal::graphics_contract::WIDTH as i32 - 80,
|
|
||||||
h: prometeu_hal::graphics_contract::HEIGHT as i32 - 50,
|
|
||||||
};
|
|
||||||
|
|
||||||
const COLOR_BG: Color = Color::rgb(12, 14, 22);
|
const COLOR_BG: Color = Color::rgb(12, 14, 22);
|
||||||
const COLOR_PANEL: Color = Color::rgb(25, 31, 48);
|
const COLOR_PANEL: Color = Color::rgb(25, 31, 48);
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
//! shared IO worker owns storage during a publication. Legacy per-slot files are
|
//! shared IO worker owns storage during a publication. Legacy per-slot files are
|
||||||
//! intentionally neither read nor migrated by this service.
|
//! intentionally neither read nor migrated by this service.
|
||||||
pub mod admission;
|
pub mod admission;
|
||||||
pub mod diagnostics;
|
|
||||||
pub mod guest;
|
pub mod guest;
|
||||||
pub mod image;
|
pub mod image;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ pub struct Admission {
|
|||||||
pub state: PreparationState,
|
pub state: PreparationState,
|
||||||
pub snapshot_budget: usize,
|
pub snapshot_budget: usize,
|
||||||
faulted: bool,
|
faulted: bool,
|
||||||
replacement_observation: Option<super::diagnostics::SessionDiagnostics>,
|
|
||||||
}
|
}
|
||||||
impl Default for Admission {
|
impl Default for Admission {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
@@ -49,26 +48,10 @@ impl Default for Admission {
|
|||||||
state: PreparationState::Idle,
|
state: PreparationState::Idle,
|
||||||
snapshot_budget: DEFAULT_SNAPSHOT_BUDGET,
|
snapshot_budget: DEFAULT_SNAPSHOT_BUDGET,
|
||||||
faulted: false,
|
faulted: false,
|
||||||
replacement_observation: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl MemcardService {
|
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 {
|
pub fn host_faulted(&self) -> bool {
|
||||||
self.admission.faulted || self.session.host_faulted()
|
self.admission.faulted || self.session.host_faulted()
|
||||||
}
|
}
|
||||||
@@ -77,11 +60,7 @@ impl MemcardService {
|
|||||||
}
|
}
|
||||||
pub fn installed_health(&self) -> Option<(bool, bool, bool)> {
|
pub fn installed_health(&self) -> Option<(bool, bool, bool)> {
|
||||||
self.admission.installed.as_ref().map(|card| {
|
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 {
|
pub fn card_has_game(&self, app_id: u32) -> bool {
|
||||||
@@ -156,9 +135,6 @@ impl MemcardService {
|
|||||||
let owned =
|
let owned =
|
||||||
card.catalog.games.iter().find(|g| g.app_id == app_id).map_or(0, |g| g.slots.len());
|
card.catalog.games.iter().find(|g| g.app_id == app_id).map_or(0, |g| g.slots.len());
|
||||||
if choice == Choice::Persistent {
|
if choice == Choice::Persistent {
|
||||||
if card.uncertain {
|
|
||||||
return Err(Error::Indeterminate);
|
|
||||||
}
|
|
||||||
if card.storage_read_only() || card.write_blocked {
|
if card.storage_read_only() || card.write_blocked {
|
||||||
return Err(Error::ReadOnly);
|
return Err(Error::ReadOnly);
|
||||||
}
|
}
|
||||||
@@ -195,10 +171,6 @@ impl MemcardService {
|
|||||||
}
|
}
|
||||||
Choice::Persistent => {
|
Choice::Persistent => {
|
||||||
let mut card = card.take().unwrap();
|
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 {
|
if card.storage_read_only() || card.write_blocked {
|
||||||
session.retain_idle_card(card);
|
session.retain_idle_card(card);
|
||||||
return Err(Error::ReadOnly);
|
return Err(Error::ReadOnly);
|
||||||
@@ -284,24 +256,9 @@ impl MemcardService {
|
|||||||
&mut self,
|
&mut self,
|
||||||
storage: Box<dyn MemcardStorage>,
|
storage: Box<dyn MemcardStorage>,
|
||||||
load_existing: bool,
|
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<()> {
|
) -> Result<()> {
|
||||||
if self.admission.receiver.is_some()
|
if self.admission.receiver.is_some()
|
||||||
|| self.session.io_active()
|
|| self.session.has_in_flight()
|
||||||
|| self.session.has_deferred_write()
|
|
||||||
|| self.session.is_boot_active()
|
|| self.session.is_boot_active()
|
||||||
{
|
{
|
||||||
return Err(Error::Busy);
|
return Err(Error::Busy);
|
||||||
@@ -309,7 +266,6 @@ impl MemcardService {
|
|||||||
if self.admission.faulted {
|
if self.admission.faulted {
|
||||||
return Err(Error::ReadOnly);
|
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 handoff = Arc::new(Mutex::new(Some(std::mem::take(&mut self.session))));
|
||||||
let worker = handoff.clone();
|
let worker = handoff.clone();
|
||||||
let control = Arc::new(PublicationControl::default());
|
let control = Arc::new(PublicationControl::default());
|
||||||
@@ -319,14 +275,13 @@ impl MemcardService {
|
|||||||
.lane
|
.lane
|
||||||
.submit_memcard_commit_write(move |_| {
|
.submit_memcard_commit_write(move |_| {
|
||||||
let mut session = worker.lock().unwrap().take().unwrap();
|
let mut session = worker.lock().unwrap().take().unwrap();
|
||||||
let result = operation(&mut session, &worker_control);
|
let result = session.replace_storage(storage, load_existing, &worker_control);
|
||||||
let _ = tx.send(Prepared::Replacement(session, result));
|
let _ = tx.send(Prepared::Replacement(session, result));
|
||||||
crate::AsyncWorkJobOutcome::Completed
|
crate::AsyncWorkJobOutcome::Completed
|
||||||
})
|
})
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
self.session = handoff.lock().unwrap().take().unwrap();
|
self.session = handoff.lock().unwrap().take().unwrap();
|
||||||
self.admission.replacement_observation = None;
|
|
||||||
return Err(Error::InvalidState);
|
return Err(Error::InvalidState);
|
||||||
}
|
}
|
||||||
self.admission.control = Some(control);
|
self.admission.control = Some(control);
|
||||||
@@ -376,7 +331,6 @@ impl MemcardService {
|
|||||||
}
|
}
|
||||||
self.admission.receiver = None;
|
self.admission.receiver = None;
|
||||||
self.admission.control = None;
|
self.admission.control = None;
|
||||||
self.admission.replacement_observation = None;
|
|
||||||
}
|
}
|
||||||
Err(mpsc::TryRecvError::Disconnected) => {
|
Err(mpsc::TryRecvError::Disconnected) => {
|
||||||
self.admission.state = PreparationState::Failed(Error::InvalidState);
|
self.admission.state = PreparationState::Failed(Error::InvalidState);
|
||||||
@@ -407,37 +361,3 @@ 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
//! 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,14 +31,12 @@ pub fn status(e: Error) -> i64 {
|
|||||||
use prometeu_hal::memcard::MemcardIoError as I;
|
use prometeu_hal::memcard::MemcardIoError as I;
|
||||||
match e {
|
match e {
|
||||||
Error::NoSpace | Error::Io(I::NoSpace) => 3,
|
Error::NoSpace | Error::Io(I::NoSpace) => 3,
|
||||||
Error::AccessDenied
|
Error::AccessDenied => 4,
|
||||||
| Error::IdentityMismatch
|
|
||||||
| Error::Io(I::AccessDenied | I::AlreadyExists) => 4,
|
|
||||||
Error::Corrupt => 5,
|
Error::Corrupt => 5,
|
||||||
Error::Indeterminate | Error::Io(I::Indeterminate) => 6,
|
Error::Indeterminate => 6,
|
||||||
Error::Io(I::Unavailable) => 7,
|
Error::Io(I::Unavailable) => 7,
|
||||||
Error::InvalidState | Error::Canceled => 8,
|
Error::InvalidState | Error::Canceled => 8,
|
||||||
Error::Busy | Error::Io(I::Busy) => 9,
|
Error::Busy => 9,
|
||||||
Error::ReadOnly | Error::CounterExhausted | Error::Io(I::ReadOnly) => 10,
|
Error::ReadOnly | Error::CounterExhausted | Error::Io(I::ReadOnly) => 10,
|
||||||
Error::InvalidRange | Error::Io(I::InvalidRange) => 11,
|
Error::InvalidRange | Error::Io(I::InvalidRange) => 11,
|
||||||
Error::Unsupported | Error::Io(I::Unsupported) => 12,
|
Error::Unsupported | Error::Io(I::Unsupported) => 12,
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ pub enum Error {
|
|||||||
OutOfMemory,
|
OutOfMemory,
|
||||||
AccessDenied,
|
AccessDenied,
|
||||||
InvalidState,
|
InvalidState,
|
||||||
IdentityMismatch,
|
|
||||||
}
|
}
|
||||||
impl From<MemcardIoError> for Error {
|
impl From<MemcardIoError> for Error {
|
||||||
fn from(e: MemcardIoError) -> Self {
|
fn from(e: MemcardIoError) -> Self {
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ use std::time::{Duration, Instant};
|
|||||||
|
|
||||||
pub const GUEST_BYTES: usize = 4096;
|
pub const GUEST_BYTES: usize = 4096;
|
||||||
static NEXT_CONTEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
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> {
|
fn next_context() -> Result<u64> {
|
||||||
NEXT_CONTEXT
|
NEXT_CONTEXT
|
||||||
.fetch_update(
|
.fetch_update(
|
||||||
@@ -73,14 +71,12 @@ struct Request {
|
|||||||
job: AsyncWorkJobId,
|
job: AsyncWorkJobId,
|
||||||
released: bool,
|
released: bool,
|
||||||
saves_payload: bool,
|
saves_payload: bool,
|
||||||
worker_returned: bool,
|
|
||||||
}
|
}
|
||||||
/// Main-lane control; the Card moves into the single worker while its source is protected.
|
/// Main-lane control; the Card moves into the single worker while its source is protected.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
card_id: Option<[u8; 16]>,
|
card_id: Option<[u8; 16]>,
|
||||||
uncertain_target: Option<Map>,
|
uncertain_target: Option<Map>,
|
||||||
known_card_exclusions: Option<([u8; 16], [bool; BLOCK_COUNT])>,
|
|
||||||
health: (bool, bool, bool),
|
health: (bool, bool, bool),
|
||||||
context: u64,
|
context: u64,
|
||||||
epoch: u64,
|
epoch: u64,
|
||||||
@@ -104,82 +100,6 @@ pub struct Session {
|
|||||||
deferred: Option<(u64, usize, u64, Instant)>,
|
deferred: Option<(u64, usize, u64, Instant)>,
|
||||||
}
|
}
|
||||||
impl Session {
|
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 {
|
pub fn context(&self) -> u64 {
|
||||||
self.context
|
self.context
|
||||||
}
|
}
|
||||||
@@ -611,7 +531,6 @@ impl Session {
|
|||||||
job,
|
job,
|
||||||
released: !saves_payload,
|
released: !saves_payload,
|
||||||
saves_payload,
|
saves_payload,
|
||||||
worker_returned: false,
|
|
||||||
});
|
});
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -755,7 +674,6 @@ impl Session {
|
|||||||
job,
|
job,
|
||||||
released: false,
|
released: false,
|
||||||
saves_payload: true,
|
saves_payload: true,
|
||||||
worker_returned: false,
|
|
||||||
});
|
});
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
@@ -774,7 +692,6 @@ impl Session {
|
|||||||
let Some(r) = &mut self.request else { return };
|
let Some(r) = &mut self.request else { return };
|
||||||
match r.receiver.try_recv() {
|
match r.receiver.try_recv() {
|
||||||
Ok(completion) => {
|
Ok(completion) => {
|
||||||
r.worker_returned = true;
|
|
||||||
r.worker_error = completion.result.error;
|
r.worker_error = completion.result.error;
|
||||||
let current =
|
let current =
|
||||||
self.mode.is_some() && r.session == self.context && r.epoch == self.epoch;
|
self.mode.is_some() && r.session == self.context && r.epoch == self.epoch;
|
||||||
@@ -902,7 +819,7 @@ impl Session {
|
|||||||
return Err(Error::Busy);
|
return Err(Error::Busy);
|
||||||
}
|
}
|
||||||
r.released = true;
|
r.released = true;
|
||||||
if r.worker_returned {
|
if self.card.is_some() {
|
||||||
self.request = None;
|
self.request = None;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -916,7 +833,7 @@ impl Session {
|
|||||||
r.control.cancel();
|
r.control.cancel();
|
||||||
r.released = true;
|
r.released = true;
|
||||||
}
|
}
|
||||||
if !self.io_active() {
|
if self.card.is_some() {
|
||||||
self.request = None;
|
self.request = None;
|
||||||
}
|
}
|
||||||
self.removal_pending = false;
|
self.removal_pending = false;
|
||||||
@@ -952,23 +869,7 @@ impl Session {
|
|||||||
load_existing: bool,
|
load_existing: bool,
|
||||||
control: &PublicationControl,
|
control: &PublicationControl,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.replace_storage_with(move || Ok(storage), load_existing, false, control)
|
if self.has_in_flight() || self.boot_active || self.mode.is_none() {
|
||||||
}
|
|
||||||
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);
|
return Err(Error::Busy);
|
||||||
}
|
}
|
||||||
if self.icon.is_empty()
|
if self.icon.is_empty()
|
||||||
@@ -981,18 +882,11 @@ impl Session {
|
|||||||
{
|
{
|
||||||
self.uncertain_target = card.attempted_publication.clone();
|
self.uncertain_target = card.attempted_publication.clone();
|
||||||
}
|
}
|
||||||
if let Some(card) = &self.card {
|
let known = self.card.as_ref().map(|c| (c.current().card_id, c.known_exclusions()));
|
||||||
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.card.take(); // Release the old scratch before mounting another image.
|
||||||
self.scratch = None;
|
self.scratch = None;
|
||||||
self.reconcile_blocked = true;
|
self.reconcile_blocked = true;
|
||||||
let candidate = Card::mount(open()?)?;
|
self.card = Some(Card::mount(storage)?);
|
||||||
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 card = self.card.as_mut().unwrap();
|
||||||
let same = self.card_id == Some(card.current().card_id);
|
let same = self.card_id == Some(card.current().card_id);
|
||||||
if let Some((id, known)) = known
|
if let Some((id, known)) = known
|
||||||
@@ -1079,15 +973,14 @@ impl Session {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn detach(&mut self) -> Result<()> {
|
pub fn detach(&mut self) -> Result<()> {
|
||||||
if self.deferred.is_some() || self.io_active() {
|
if self.deferred.is_some()
|
||||||
|
|| self
|
||||||
|
.request
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|r| r.result.state == RequestState::Pending || self.card.is_none())
|
||||||
|
{
|
||||||
return Err(Error::Busy);
|
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.mode = Some(Mode::MemoryOnly);
|
||||||
self.removal_pending = false;
|
self.removal_pending = false;
|
||||||
self.reconcile_blocked = false;
|
self.reconcile_blocked = false;
|
||||||
|
|||||||
@@ -16,8 +16,6 @@ impl PublicationControl {
|
|||||||
pub fn publication_attempted(&self) -> bool {
|
pub fn publication_attempted(&self) -> bool {
|
||||||
self.0.load(Ordering::SeqCst) & 1 != 0
|
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<()> {
|
fn enter(&self) -> Result<()> {
|
||||||
self.0
|
self.0
|
||||||
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |state| {
|
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |state| {
|
||||||
|
|||||||
@@ -187,16 +187,6 @@ impl VirtualMachineRuntime {
|
|||||||
self.render_worker_ownership.set(self.render_manager.active_ownership());
|
self.render_worker_ownership.set(self.render_manager.active_ownership());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn record_presented_render_worker_frame(
|
|
||||||
&self,
|
|
||||||
ownership: prometeu_hal::RenderOwnership,
|
|
||||||
frame_id: FrameId,
|
|
||||||
) {
|
|
||||||
if let Some(controller) = self.render_worker_controller.as_ref() {
|
|
||||||
controller.record_presented_frame(ownership, frame_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn record_repeated_render_worker_frame(&self, frame_id: FrameId) {
|
pub fn record_repeated_render_worker_frame(&self, frame_id: FrameId) {
|
||||||
if let Some(controller) = self.render_worker_controller.as_ref() {
|
if let Some(controller) = self.render_worker_controller.as_ref() {
|
||||||
controller.record_repeated_frame(frame_id);
|
controller.record_repeated_frame(frame_id);
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ impl Default for RenderWorkerConfig {
|
|||||||
struct RenderWorkerState {
|
struct RenderWorkerState {
|
||||||
telemetry: RenderWorkerTelemetry,
|
telemetry: RenderWorkerTelemetry,
|
||||||
last_error: Option<RenderWorkerError>,
|
last_error: Option<RenderWorkerError>,
|
||||||
last_presented: Option<(RenderOwnership, FrameId)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
@@ -41,19 +40,6 @@ impl LatestRenderFrameStore {
|
|||||||
pub fn latest_frame(&self) -> Option<OwnedRgba8888Frame> {
|
pub fn latest_frame(&self) -> Option<OwnedRgba8888Frame> {
|
||||||
self.latest.lock().unwrap().clone()
|
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 {
|
impl RenderWorkerFrameSink for LatestRenderFrameStore {
|
||||||
@@ -158,7 +144,6 @@ impl RenderWorkerController {
|
|||||||
let mut telemetry = self.handoff.telemetry();
|
let mut telemetry = self.handoff.telemetry();
|
||||||
let state = self.state.lock().unwrap();
|
let state = self.state.lock().unwrap();
|
||||||
telemetry.published_frames = state.telemetry.published_frames;
|
telemetry.published_frames = state.telemetry.published_frames;
|
||||||
telemetry.presented_frames = state.telemetry.presented_frames;
|
|
||||||
telemetry.render_failures = state.telemetry.render_failures;
|
telemetry.render_failures = state.telemetry.render_failures;
|
||||||
telemetry.publish_failures = state.telemetry.publish_failures;
|
telemetry.publish_failures = state.telemetry.publish_failures;
|
||||||
telemetry.present_failures = state.telemetry.present_failures;
|
telemetry.present_failures = state.telemetry.present_failures;
|
||||||
@@ -181,17 +166,6 @@ impl RenderWorkerController {
|
|||||||
self.ownership.snapshot()
|
self.ownership.snapshot()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Record only after a successful host presentation, never on publication.
|
|
||||||
pub fn record_presented_frame(&self, ownership: RenderOwnership, frame_id: FrameId) {
|
|
||||||
let mut state = self.state.lock().unwrap();
|
|
||||||
if state.last_presented == Some((ownership, frame_id)) {
|
|
||||||
state.telemetry.record_repeated_frame(frame_id.get());
|
|
||||||
} else {
|
|
||||||
state.telemetry.record_presented(frame_id.get());
|
|
||||||
}
|
|
||||||
state.last_presented = Some((ownership, frame_id));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn record_repeated_frame(&self, frame_id: FrameId) {
|
pub fn record_repeated_frame(&self, frame_id: FrameId) {
|
||||||
let mut state = self.state.lock().unwrap();
|
let mut state = self.state.lock().unwrap();
|
||||||
state.telemetry.record_repeated_frame(frame_id.get());
|
state.telemetry.record_repeated_frame(frame_id.get());
|
||||||
@@ -216,13 +190,13 @@ fn run_worker_loop<B, S>(
|
|||||||
B: RenderWorkerBackend,
|
B: RenderWorkerBackend,
|
||||||
S: RenderWorkerFrameSink,
|
S: RenderWorkerFrameSink,
|
||||||
{
|
{
|
||||||
let report_path = std::env::var_os("PROMETEU_GRAPHICS_METRICS").map(std::path::PathBuf::from);
|
loop {
|
||||||
let mut measurements = Vec::new();
|
let submission = match handoff.wait_take() {
|
||||||
let mut measured_frames = 0usize;
|
RenderWorkerHandoffWait::Submission(submission) => submission,
|
||||||
while let RenderWorkerHandoffWait::Submission(submission) = handoff.wait_take() {
|
RenderWorkerHandoffWait::Shutdown => return,
|
||||||
|
};
|
||||||
let frame_id = submission.frame_id;
|
let frame_id = submission.frame_id;
|
||||||
|
|
||||||
let render_begin = report_path.as_ref().map(|_| std::time::Instant::now());
|
|
||||||
let render_result = catch_unwind(AssertUnwindSafe(|| backend.render(&submission)))
|
let render_result = catch_unwind(AssertUnwindSafe(|| backend.render(&submission)))
|
||||||
.map_err(|_| RenderWorkerError::WorkerPanic);
|
.map_err(|_| RenderWorkerError::WorkerPanic);
|
||||||
let frame = match render_result {
|
let frame = match render_result {
|
||||||
@@ -238,45 +212,13 @@ fn run_worker_loop<B, S>(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let render_us = render_begin.map(|start| start.elapsed().as_micros() as u64);
|
|
||||||
let publish_begin = report_path.as_ref().map(|_| std::time::Instant::now());
|
|
||||||
let publish_result = catch_unwind(AssertUnwindSafe(|| sink.publish(frame)))
|
let publish_result = catch_unwind(AssertUnwindSafe(|| sink.publish(frame)))
|
||||||
.map_err(|_| RenderWorkerError::WorkerPanic);
|
.map_err(|_| RenderWorkerError::WorkerPanic);
|
||||||
if let (Some(render_us), Some(begin)) = (render_us, publish_begin) {
|
|
||||||
let sample = (frame_id.get(), render_us, begin.elapsed().as_micros() as u64);
|
|
||||||
if measurements.len() < 16384 {
|
|
||||||
measurements.push(sample);
|
|
||||||
} else {
|
|
||||||
measurements[measured_frames % 16384] = sample;
|
|
||||||
}
|
|
||||||
measured_frames += 1;
|
|
||||||
}
|
|
||||||
match publish_result {
|
match publish_result {
|
||||||
Ok(Ok(())) => record_published(&state, frame_id),
|
Ok(Ok(())) => record_published(&state, frame_id),
|
||||||
Ok(Err(error)) | Err(error) => record_error(&state, error, frame_id),
|
Ok(Err(error)) | Err(error) => record_error(&state, error, frame_id),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(path) = report_path {
|
|
||||||
use std::io::Write;
|
|
||||||
let worker = state.lock().unwrap();
|
|
||||||
let handoff = handoff.telemetry();
|
|
||||||
let report = serde_json::json!({"schema":1,"source":"async_render_worker",
|
|
||||||
"produced_submissions":handoff.produced_submissions,"consumed_submissions":handoff.consumed_submissions,
|
|
||||||
"replaced_before_consume":handoff.replaced_before_consume,
|
|
||||||
"published_frames":worker.telemetry.published_frames,"first_presentations":worker.telemetry.presented_frames,
|
|
||||||
"repeated_presentations":worker.telemetry.repeated_frames,
|
|
||||||
"render_failures":worker.telemetry.render_failures,"publish_failures":worker.telemetry.publish_failures,
|
|
||||||
"measured_frames":measured_frames,"samples_frame_render_publish_us":measurements,
|
|
||||||
"physical_display_hz":null,
|
|
||||||
"limitations":"last 16384 successful-render samples; render includes owned-frame copy; sink publication is not scanout; opt-in clock and storage overhead"});
|
|
||||||
if let Ok(mut file) = std::fs::OpenOptions::new()
|
|
||||||
.create(true)
|
|
||||||
.append(true)
|
|
||||||
.open(path.with_extension("renderer.jsonl"))
|
|
||||||
{
|
|
||||||
let _ = writeln!(file, "{report}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn record_published(state: &Arc<Mutex<RenderWorkerState>>, frame_id: FrameId) {
|
fn record_published(state: &Arc<Mutex<RenderWorkerState>>, frame_id: FrameId) {
|
||||||
@@ -391,14 +333,6 @@ mod tests {
|
|||||||
SharedFakeBackend(backend),
|
SharedFakeBackend(backend),
|
||||||
);
|
);
|
||||||
|
|
||||||
let first_session = RenderOwnership::new(1, AppMode::Game, 7);
|
|
||||||
controller.record_presented_frame(first_session, FrameId::ZERO);
|
|
||||||
controller.record_presented_frame(first_session, FrameId::ZERO);
|
|
||||||
// Frame zero in a new epoch is a new image, not a repeat of the old session.
|
|
||||||
controller.record_presented_frame(RenderOwnership::new(2, AppMode::Game, 7), FrameId::ZERO);
|
|
||||||
assert_eq!(controller.telemetry().presented_frames, 2);
|
|
||||||
assert_eq!(controller.telemetry().repeated_frames, 1);
|
|
||||||
|
|
||||||
assert_eq!(controller.stop(), Ok(()));
|
assert_eq!(controller.stop(), Ok(()));
|
||||||
assert_eq!(controller.last_error(), None);
|
assert_eq!(controller.last_error(), None);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -235,13 +235,12 @@ pub type IntrinsicHostImplementation =
|
|||||||
pub enum IntrinsicImplementation {
|
pub enum IntrinsicImplementation {
|
||||||
Host(IntrinsicHostImplementation),
|
Host(IntrinsicHostImplementation),
|
||||||
VmOwned,
|
VmOwned,
|
||||||
BorrowedInline(crate::IntrinsicInlineImplementation),
|
|
||||||
}
|
}
|
||||||
impl IntrinsicImplementation {
|
impl IntrinsicImplementation {
|
||||||
pub fn host_callback(self) -> Option<IntrinsicHostImplementation> {
|
pub fn host_callback(self) -> Option<IntrinsicHostImplementation> {
|
||||||
match self {
|
match self {
|
||||||
Self::Host(f) => Some(f),
|
Self::Host(f) => Some(f),
|
||||||
Self::VmOwned | Self::BorrowedInline(_) => None,
|
Self::VmOwned => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,14 +249,12 @@ impl IntrinsicImplementation {
|
|||||||
pub enum IntrinsicCostModel {
|
pub enum IntrinsicCostModel {
|
||||||
Fixed,
|
Fixed,
|
||||||
RandomRejectionV1,
|
RandomRejectionV1,
|
||||||
ArgumentUtf8BytesV1,
|
|
||||||
}
|
}
|
||||||
impl IntrinsicCostModel {
|
impl IntrinsicCostModel {
|
||||||
pub const fn name(self) -> &'static str {
|
pub const fn name(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::Fixed => "fixed",
|
Self::Fixed => "fixed",
|
||||||
Self::RandomRejectionV1 => "random_rejection_v1",
|
Self::RandomRejectionV1 => "random_rejection_v1",
|
||||||
Self::ArgumentUtf8BytesV1 => "argument_utf8_bytes_v1",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -269,14 +266,11 @@ pub struct IntrinsicMeta {
|
|||||||
pub name: &'static str,
|
pub name: &'static str,
|
||||||
pub version: u16,
|
pub version: u16,
|
||||||
pub arg_layout: &'static [AbiType],
|
pub arg_layout: &'static [AbiType],
|
||||||
pub arg_nullable: &'static [bool],
|
|
||||||
pub ret_layout: &'static [AbiType],
|
pub ret_layout: &'static [AbiType],
|
||||||
pub parameter_names: &'static [&'static str],
|
pub parameter_names: &'static [&'static str],
|
||||||
/// Full fixed cost, or base cost for a variable model.
|
/// Full fixed cost, or base cost for a variable model.
|
||||||
pub cost_cycles: u64,
|
pub cost_cycles: u64,
|
||||||
pub cost_model: IntrinsicCostModel,
|
pub cost_model: IntrinsicCostModel,
|
||||||
pub cost_arg_index: Option<usize>,
|
|
||||||
pub cost_per_byte: u64,
|
|
||||||
pub attempt_cost_cycles: u64,
|
pub attempt_cost_cycles: u64,
|
||||||
pub max_attempts: u32,
|
pub max_attempts: u32,
|
||||||
pub mutates_guest_state: bool,
|
pub mutates_guest_state: bool,
|
||||||
@@ -306,16 +300,7 @@ impl IntrinsicMeta {
|
|||||||
&self,
|
&self,
|
||||||
values: &[Value],
|
values: &[Value],
|
||||||
) -> Result<(), IntrinsicExecutionError> {
|
) -> 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(
|
fn validate_values(
|
||||||
@@ -578,13 +563,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "dot",
|
name: "dot",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &VEC2_DOT_ARGS,
|
arg_layout: &VEC2_DOT_ARGS,
|
||||||
arg_nullable: &[false; VEC2_DOT_ARGS.len()],
|
|
||||||
ret_layout: &SINGLE_FLOAT_RET,
|
ret_layout: &SINGLE_FLOAT_RET,
|
||||||
parameter_names: &["ax", "ay", "bx", "by"],
|
parameter_names: &["ax", "ay", "bx", "by"],
|
||||||
cost_cycles: 10,
|
cost_cycles: 10,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -598,13 +580,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "length",
|
name: "length",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &VEC2_LENGTH_ARGS,
|
arg_layout: &VEC2_LENGTH_ARGS,
|
||||||
arg_nullable: &[false; VEC2_LENGTH_ARGS.len()],
|
|
||||||
ret_layout: &SINGLE_FLOAT_RET,
|
ret_layout: &SINGLE_FLOAT_RET,
|
||||||
parameter_names: &["x", "y"],
|
parameter_names: &["x", "y"],
|
||||||
cost_cycles: 40,
|
cost_cycles: 40,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -618,13 +597,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "from_raw",
|
name: "from_raw",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &INT_RET,
|
arg_layout: &INT_RET,
|
||||||
arg_nullable: &[false; INT_RET.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &["raw"],
|
parameter_names: &["raw"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -638,13 +614,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "rgb",
|
name: "rgb",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &COLOR_RGB_ARGS,
|
arg_layout: &COLOR_RGB_ARGS,
|
||||||
arg_nullable: &[false; COLOR_RGB_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &["r", "g", "b"],
|
parameter_names: &["r", "g", "b"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -658,13 +631,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "rgba",
|
name: "rgba",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &COLOR_RGBA_ARGS,
|
arg_layout: &COLOR_RGBA_ARGS,
|
||||||
arg_nullable: &[false; COLOR_RGBA_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &["r", "g", "b", "a"],
|
parameter_names: &["r", "g", "b", "a"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -678,13 +648,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "html_rgba",
|
name: "html_rgba",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &COLOR_HTML_RGBA_ARGS,
|
arg_layout: &COLOR_HTML_RGBA_ARGS,
|
||||||
arg_nullable: &[false; COLOR_HTML_RGBA_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &["value"],
|
parameter_names: &["value"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -698,13 +665,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "gray_scale",
|
name: "gray_scale",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &INT_RET,
|
arg_layout: &INT_RET,
|
||||||
arg_nullable: &[false; INT_RET.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &["value"],
|
parameter_names: &["value"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -718,13 +682,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "hex",
|
name: "hex",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &COLOR_ARGS,
|
arg_layout: &COLOR_ARGS,
|
||||||
arg_nullable: &[false; COLOR_ARGS.len()],
|
|
||||||
ret_layout: &INT_RET,
|
ret_layout: &INT_RET,
|
||||||
parameter_names: &["color"],
|
parameter_names: &["color"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -738,13 +699,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "alpha",
|
name: "alpha",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &COLOR_ARGS,
|
arg_layout: &COLOR_ARGS,
|
||||||
arg_nullable: &[false; COLOR_ARGS.len()],
|
|
||||||
ret_layout: &INT_RET,
|
ret_layout: &INT_RET,
|
||||||
parameter_names: &["color"],
|
parameter_names: &["color"],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -758,13 +716,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "black",
|
name: "black",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -778,13 +733,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "white",
|
name: "white",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -798,13 +750,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "red",
|
name: "red",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -818,13 +767,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "green",
|
name: "green",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -838,13 +784,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "blue",
|
name: "blue",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -858,13 +801,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "yellow",
|
name: "yellow",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -878,13 +818,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "orange",
|
name: "orange",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -898,13 +835,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "indigo",
|
name: "indigo",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -918,13 +852,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "gray",
|
name: "gray",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -938,13 +869,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "cyan",
|
name: "cyan",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -958,13 +886,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "magenta",
|
name: "magenta",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -978,13 +903,10 @@ const INTRINSICS: [IntrinsicMeta; 21] = [
|
|||||||
name: "transparent",
|
name: "transparent",
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &NO_ARGS,
|
arg_layout: &NO_ARGS,
|
||||||
arg_nullable: &[false; NO_ARGS.len()],
|
|
||||||
ret_layout: &COLOR_RET,
|
ret_layout: &COLOR_RET,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
@@ -1022,7 +944,6 @@ pub fn lookup_intrinsic(owner: &str, name: &str, version: u16) -> Option<&'stati
|
|||||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||||
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
||||||
.chain(crate::random_intrinsics::RANDOM_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)
|
.find(|meta| meta.owner == owner && meta.name == name && meta.version == version)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1032,7 +953,6 @@ pub fn lookup_intrinsic_by_id(id: u32) -> Option<&'static IntrinsicMeta> {
|
|||||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||||
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
||||||
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
|
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
|
||||||
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
|
|
||||||
.find(|meta| meta.id == id)
|
.find(|meta| meta.id == id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1334,17 +1254,9 @@ pub(crate) fn metered_lookup_intrinsic(
|
|||||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||||
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
||||||
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
|
.chain(crate::random_intrinsics::RANDOM_INTRINSICS.iter())
|
||||||
.chain(crate::hash_intrinsics::HASH_INTRINSICS.iter())
|
|
||||||
{
|
{
|
||||||
budget.work(1)?;
|
budget.work(1)?;
|
||||||
if meta.id == id {
|
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));
|
return Ok(Some(meta));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1358,7 +1270,6 @@ pub fn intrinsic_catalog() -> impl Iterator<Item = &'static IntrinsicMeta> {
|
|||||||
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
.chain(crate::input_intrinsics::INPUT_INTRINSICS.iter())
|
||||||
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
.chain(crate::math_intrinsics::MATH_INTRINSICS.iter())
|
||||||
.chain(crate::random_intrinsics::RANDOM_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> {
|
pub fn builtin_constant_catalog() -> impl Iterator<Item = &'static BuiltinConstMeta> {
|
||||||
BUILTIN_CONSTS.iter()
|
BUILTIN_CONSTS.iter()
|
||||||
|
|||||||
@@ -16,15 +16,14 @@ fn atom(t: AbiType) -> String {
|
|||||||
}
|
}
|
||||||
pub fn intrinsic_catalog_csv() -> String {
|
pub fn intrinsic_catalog_csv() -> String {
|
||||||
let mut out = String::from(
|
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,schema_version,arg_nullable,invocation_model,scratch_model,context_access,cost_arg_index,cost_per_byte\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\n",
|
||||||
);
|
);
|
||||||
let mut entries: Vec<_> = intrinsic_catalog().collect();
|
let mut entries: Vec<_> = intrinsic_catalog().collect();
|
||||||
entries.sort_by_key(|m| m.id);
|
entries.sort_by_key(|m| m.id);
|
||||||
for m in entries {
|
for m in entries {
|
||||||
m.validate_descriptor().expect("valid canonical intrinsic descriptor");
|
|
||||||
writeln!(
|
writeln!(
|
||||||
&mut out,
|
&mut out,
|
||||||
"0x{:04X},{},{}.{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},2,{},{},{},{},{},{}",
|
"0x{:04X},{},{}.{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
|
||||||
m.id,
|
m.id,
|
||||||
m.id,
|
m.id,
|
||||||
m.owner,
|
m.owner,
|
||||||
@@ -43,13 +42,7 @@ pub fn intrinsic_catalog_csv() -> String {
|
|||||||
m.mutates_guest_state,
|
m.mutates_guest_state,
|
||||||
m.cost_model.name(),
|
m.cost_model.name(),
|
||||||
m.attempt_cost_cycles,
|
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();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
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))
|
|
||||||
}
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
//! 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -428,34 +428,6 @@ impl Heap {
|
|||||||
self.allocation_count.load(Ordering::Relaxed)
|
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")]
|
#[cfg(feature = "resource-diagnostics")]
|
||||||
pub(crate) fn diagnostic_handles(&self) -> (usize, usize) {
|
pub(crate) fn diagnostic_handles(&self) -> (usize, usize) {
|
||||||
(self.free_list.len(), self.objects.len() - self.free_list.len() - self.len())
|
(self.free_list.len(), self.objects.len() - self.free_list.len() - self.len())
|
||||||
@@ -548,18 +520,6 @@ impl Heap {
|
|||||||
value_contract::validate(registry, self, storage.slot_type(index)?, values.as_slice())?;
|
value_contract::validate(registry, self, storage.slot_type(index)?, values.as_slice())?;
|
||||||
Ok(values)
|
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(
|
pub(crate) fn store_typed(
|
||||||
&mut self,
|
&mut self,
|
||||||
registry: &Registry,
|
registry: &Registry,
|
||||||
|
|||||||
@@ -20,13 +20,10 @@ const fn query(
|
|||||||
name,
|
name,
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: &[],
|
arg_layout: &[],
|
||||||
arg_nullable: &[],
|
|
||||||
ret_layout,
|
ret_layout,
|
||||||
parameter_names: &[],
|
parameter_names: &[],
|
||||||
cost_cycles: 1,
|
cost_cycles: 1,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
|
|||||||
@@ -1,143 +0,0 @@
|
|||||||
//! 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,11 +2,7 @@
|
|||||||
extern crate self as prometeu_vm;
|
extern crate self as prometeu_vm;
|
||||||
mod builtins;
|
mod builtins;
|
||||||
mod catalog;
|
mod catalog;
|
||||||
mod hash_intrinsics;
|
|
||||||
mod hashing;
|
|
||||||
mod intrinsic_contract;
|
|
||||||
pub use catalog::{builtin_constant_catalog_csv, intrinsic_catalog_csv};
|
pub use catalog::{builtin_constant_catalog_csv, intrinsic_catalog_csv};
|
||||||
pub use intrinsic_contract::{IntrinsicInlineImplementation, IntrinsicResults};
|
|
||||||
mod call_frame;
|
mod call_frame;
|
||||||
mod input_intrinsics;
|
mod input_intrinsics;
|
||||||
mod math_intrinsics;
|
mod math_intrinsics;
|
||||||
|
|||||||
@@ -20,17 +20,10 @@ const fn entry<const ID: u32>(
|
|||||||
name,
|
name,
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout,
|
arg_layout,
|
||||||
arg_nullable: match arg_layout.len() {
|
|
||||||
1 => &[false],
|
|
||||||
2 => &[false, false],
|
|
||||||
_ => &[false, false, false],
|
|
||||||
},
|
|
||||||
ret_layout,
|
ret_layout,
|
||||||
parameter_names,
|
parameter_names,
|
||||||
cost_cycles,
|
cost_cycles,
|
||||||
cost_model: crate::IntrinsicCostModel::Fixed,
|
cost_model: crate::IntrinsicCostModel::Fixed,
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: 0,
|
attempt_cost_cycles: 0,
|
||||||
max_attempts: 0,
|
max_attempts: 0,
|
||||||
mutates_guest_state: false,
|
mutates_guest_state: false,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//! PBX v0 publication: immutable metadata and shared payload, after bindings
|
//! PBX v1 publication: immutable metadata and shared payload, after bindings
|
||||||
//! and bounded whole-program verification. No public unchecked constructor.
|
//! and bounded whole-program verification. No public unchecked constructor.
|
||||||
use crate::string_identity_index::Identity;
|
use crate::string_identity_index::Identity;
|
||||||
use crate::{LoaderPatchError, VmInitError};
|
use crate::{LoaderPatchError, VmInitError};
|
||||||
|
|||||||
@@ -21,13 +21,6 @@ const fn entry(
|
|||||||
name,
|
name,
|
||||||
version: 1,
|
version: 1,
|
||||||
arg_layout: args,
|
arg_layout: args,
|
||||||
arg_nullable: if id == 0x4000 {
|
|
||||||
&[false]
|
|
||||||
} else if rejection {
|
|
||||||
&[true, false, false]
|
|
||||||
} else {
|
|
||||||
&[true]
|
|
||||||
},
|
|
||||||
ret_layout: ret,
|
ret_layout: ret,
|
||||||
parameter_names: names,
|
parameter_names: names,
|
||||||
cost_cycles: cost,
|
cost_cycles: cost,
|
||||||
@@ -36,8 +29,6 @@ const fn entry(
|
|||||||
} else {
|
} else {
|
||||||
IntrinsicCostModel::Fixed
|
IntrinsicCostModel::Fixed
|
||||||
},
|
},
|
||||||
cost_arg_index: None,
|
|
||||||
cost_per_byte: 0,
|
|
||||||
attempt_cost_cycles: if rejection { 8 } else { 0 },
|
attempt_cost_cycles: if rejection { 8 } else { 0 },
|
||||||
max_attempts: if rejection { crate::random::MAX_ATTEMPTS } else { 0 },
|
max_attempts: if rejection { crate::random::MAX_ATTEMPTS } else { 0 },
|
||||||
deterministic: true,
|
deterministic: true,
|
||||||
|
|||||||
@@ -485,10 +485,6 @@ fn transfer(
|
|||||||
group(&mut working[base..], registry, ty, (instruction.pc as u64) << 32, budget)?;
|
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 => {
|
Op::RefCast | Op::RefToTrait => {
|
||||||
group(
|
group(
|
||||||
&mut working[base..],
|
&mut working[base..],
|
||||||
@@ -507,7 +503,6 @@ fn transfer(
|
|||||||
| Op::And
|
| Op::And
|
||||||
| Op::Or
|
| Op::Or
|
||||||
| Op::Not
|
| Op::Not
|
||||||
| Op::RefIdentityEq
|
|
||||||
| Op::RefIsExact
|
| Op::RefIsExact
|
||||||
| Op::RefImplements
|
| Op::RefImplements
|
||||||
| Op::DynCanUnpack => {
|
| Op::DynCanUnpack => {
|
||||||
@@ -710,15 +705,6 @@ fn check(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Op::ArrayNew => require(working[end - 1], I64, 0, registry, budget)?,
|
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 => {
|
Op::StructGet | Op::StructSet | Op::ArrayLen | Op::ArrayGet | Op::ArraySet => {
|
||||||
budget.work(3)?;
|
budget.work(3)?;
|
||||||
let (id, field) = if matches!(instruction.opcode, Op::StructGet | Op::StructSet) {
|
let (id, field) = if matches!(instruction.opcode, Op::StructGet | Op::StructSet) {
|
||||||
@@ -774,21 +760,6 @@ fn check(
|
|||||||
return Err(mismatch(1));
|
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 => {
|
Op::RefCast | Op::RefToTrait | Op::RefIsExact | Op::RefImplements => {
|
||||||
budget.work(1)?;
|
budget.work(1)?;
|
||||||
let ty = TypeId(instruction.imm_u32x2().unwrap().0);
|
let ty = TypeId(instruction.imm_u32x2().unwrap().0);
|
||||||
@@ -801,12 +772,7 @@ fn check(
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let base = end - native.arg_slots();
|
let base = end - native.arg_slots();
|
||||||
for (part, abi) in native.arg_layout.iter().enumerate() {
|
for (part, abi) in native.arg_layout.iter().enumerate() {
|
||||||
let accepted = if native.arg_nullable[part] {
|
require(working[base + part], abi_mask(*abi), part, registry, budget)?;
|
||||||
abi_mask(*abi) | NULL
|
|
||||||
} else {
|
|
||||||
abi_mask(*abi) & !NULL
|
|
||||||
};
|
|
||||||
require(working[base + part], accepted, part, registry, budget)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Op::JmpIfTrue | Op::JmpIfFalse | Op::Not => {
|
Op::JmpIfTrue | Op::JmpIfFalse | Op::Not => {
|
||||||
|
|||||||
@@ -87,39 +87,3 @@ mod tests {
|
|||||||
assert_eq!(h.max_ns, 99);
|
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>>,
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -221,24 +221,6 @@ pub(crate) fn unpack_dynamic(
|
|||||||
|
|
||||||
// All reference operations validate complete source values before querying the
|
// All reference operations validate complete source values before querying the
|
||||||
// underlying concrete object. Invalid handles/pairs never become false or null.
|
// 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(
|
fn reference(
|
||||||
registry: &Registry,
|
registry: &Registry,
|
||||||
heap: &Heap,
|
heap: &Heap,
|
||||||
|
|||||||
@@ -1,20 +1,12 @@
|
|||||||
use crate::numeric;
|
use crate::numeric;
|
||||||
use crate::resource_budget::BudgetVec;
|
use crate::resource_budget::BudgetVec;
|
||||||
mod array_copy;
|
|
||||||
#[cfg(test)]
|
|
||||||
mod array_copy_tests;
|
|
||||||
mod callable;
|
mod callable;
|
||||||
#[cfg(test)]
|
|
||||||
#[path = "../tests/support/collections.rs"]
|
|
||||||
mod collection_test_support;
|
|
||||||
mod collections;
|
mod collections;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod collections_tests;
|
mod collections_tests;
|
||||||
mod control;
|
mod control;
|
||||||
mod dynamic;
|
mod dynamic;
|
||||||
mod gc;
|
mod gc;
|
||||||
mod identity;
|
|
||||||
mod intrinsic;
|
|
||||||
mod lifecycle;
|
mod lifecycle;
|
||||||
mod loader;
|
mod loader;
|
||||||
mod native_arrays;
|
mod native_arrays;
|
||||||
@@ -335,10 +327,8 @@ impl VirtualMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let charged_math = if opcode == OpCode::Intrinsic {
|
let charged_math = if opcode == OpCode::Intrinsic {
|
||||||
lookup_intrinsic_by_id(instr.imm_u32().unwrap()).filter(|m| {
|
lookup_intrinsic_by_id(instr.imm_u32().unwrap())
|
||||||
matches!(m.implementation, crate::IntrinsicImplementation::Host(_))
|
.filter(|m| m.owner == "vec2" || m.owner == "mathf" || m.owner.starts_with("math."))
|
||||||
&& (m.owner == "vec2" || m.owner == "mathf" || m.owner.starts_with("math."))
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
@@ -962,12 +952,6 @@ 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) {
|
if matches!(intrinsic.implementation, crate::IntrinsicImplementation::VmOwned) {
|
||||||
return self.execute_random(intrinsic, pc_at_intrinsic);
|
return self.execute_random(intrinsic, pc_at_intrinsic);
|
||||||
}
|
}
|
||||||
@@ -1099,18 +1083,6 @@ impl VirtualMachine {
|
|||||||
let (source, target) = instr.imm_u32x2().unwrap();
|
let (source, target) = instr.imm_u32x2().unwrap();
|
||||||
return self.execute_reference(opcode, source, target, start_pc as u32);
|
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::StructNew
|
||||||
| OpCode::StructGet
|
| OpCode::StructGet
|
||||||
| OpCode::StructSet
|
| OpCode::StructSet
|
||||||
@@ -2513,7 +2485,7 @@ mod tests {
|
|||||||
let mut vm = VirtualMachine::default();
|
let mut vm = VirtualMachine::default();
|
||||||
let mut header = vec![0u8; 32];
|
let mut header = vec![0u8; 32];
|
||||||
header[0..4].copy_from_slice(&prometeu_bytecode::PBX_MAGIC);
|
header[0..4].copy_from_slice(&prometeu_bytecode::PBX_MAGIC);
|
||||||
header[4..6].copy_from_slice(&1u16.to_le_bytes()); // unsupported version
|
header[4..6].copy_from_slice(&0u16.to_le_bytes()); // retired version
|
||||||
|
|
||||||
let res = vm.initialize(header);
|
let res = vm.initialize(header);
|
||||||
assert!(
|
assert!(
|
||||||
@@ -2522,11 +2494,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_loader_hardening_malformed_pbx_v0() {
|
fn test_loader_hardening_malformed_pbx_v1() {
|
||||||
let mut vm = VirtualMachine::default();
|
let mut vm = VirtualMachine::default();
|
||||||
let mut header = vec![0u8; 32];
|
let mut header = vec![0u8; 32];
|
||||||
header[0..4].copy_from_slice(&prometeu_bytecode::PBX_MAGIC);
|
header[0..4].copy_from_slice(&prometeu_bytecode::PBX_MAGIC);
|
||||||
header[4..6].copy_from_slice(&0u16.to_le_bytes());
|
header[4..6].copy_from_slice(&1u16.to_le_bytes());
|
||||||
header[8..12].copy_from_slice(&1u32.to_le_bytes()); // missing directory
|
header[8..12].copy_from_slice(&1u32.to_le_bytes()); // missing directory
|
||||||
|
|
||||||
let res = vm.initialize(header);
|
let res = vm.initialize(header);
|
||||||
@@ -3921,7 +3893,3 @@ mod callable_tests;
|
|||||||
|
|
||||||
#[cfg(feature = "resource-diagnostics")]
|
#[cfg(feature = "resource-diagnostics")]
|
||||||
pub mod string_provenance_qualification;
|
pub mod string_provenance_qualification;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[path = "virtual_machine/identity_tests.rs"]
|
|
||||||
mod identity_tests;
|
|
||||||
|
|||||||
@@ -1,110 +0,0 @@
|
|||||||
//! 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,487 +0,0 @@
|
|||||||
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(®istry, 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(®istry, a, 1).unwrap().as_slice(), present);
|
|
||||||
for i in [2, 3] {
|
|
||||||
assert_eq!(
|
|
||||||
vm.heap.load_typed(®istry, 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(®istry, 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(
|
|
||||||
®istry,
|
|
||||||
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(®istry, 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(®istry, 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(®istry, source, i, &[Value::Int32(i as i32 + 1)]).unwrap();
|
|
||||||
}
|
|
||||||
operands(&mut vm, ©_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(®istry, 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(®istry, 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, ©_inputs(source, target, 10));
|
|
||||||
refused(&mut vm, TRAP_INVALID_HANDLE, 24);
|
|
||||||
for i in 0..10 {
|
|
||||||
assert_eq!(vm.heap.load_typed(®istry, 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, ©_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(®istry, source, 0, &group).unwrap();
|
|
||||||
operands(&mut vm, ©_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(®istry, 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(®istry, array, 0, &[value]).unwrap();
|
|
||||||
}
|
|
||||||
operands(&mut vm, ©_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(®istry, source, 0).unwrap().as_slice(),
|
|
||||||
[Value::String(std::sync::Arc::from("candidate owner"))]
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
vm.heap.load_typed(®istry, 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);
|
|
||||||
}
|
|
||||||
@@ -175,7 +175,7 @@ impl VirtualMachine {
|
|||||||
.expect("result capacity reserved before publication");
|
.expect("result capacity reserved before publication");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub(super) fn collection_size_trap(
|
fn collection_size_trap(
|
||||||
&self,
|
&self,
|
||||||
opcode: OpCode,
|
opcode: OpCode,
|
||||||
pc: u32,
|
pc: u32,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use super::collection_test_support as support;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
#[path = "../../tests/support/collections.rs"]
|
||||||
|
mod support;
|
||||||
use crate::value_contract::ValueError;
|
use crate::value_contract::ValueError;
|
||||||
use prometeu_bytecode::assemble;
|
use prometeu_bytecode::assemble;
|
||||||
use support::Native;
|
use support::Native;
|
||||||
|
|||||||
@@ -1,59 +0,0 @@
|
|||||||
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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,236 +0,0 @@
|
|||||||
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)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
//! 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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -132,39 +132,3 @@ fn collection_load_limits_and_analysis_refusals_keep_context_and_refund_scratch(
|
|||||||
assert_eq!(budget.used(LoadResource::RegistryBytes), 0);
|
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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -134,96 +134,3 @@ fn provenance_queries_allocate_nothing_at_all_qualification_sizes() {
|
|||||||
assert_eq!(counts, (1, 1), "only snapshot Value storage allocates");
|
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -1,23 +0,0 @@
|
|||||||
# 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.
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
"""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()
|
|
||||||
@@ -1,152 +0,0 @@
|
|||||||
#[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)]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
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()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,7 @@ use prometeu_vm::{
|
|||||||
// Independent executable oracle: 32-byte header, eight directory entries,
|
// Independent executable oracle: 32-byte header, eight directory entries,
|
||||||
// payload at 128, one Bool import, one zero-slot HALT function; total 194.
|
// payload at 128, one Bool import, one zero-slot HALT function; total 194.
|
||||||
fn minimal() -> Vec<u8> {
|
fn minimal() -> Vec<u8> {
|
||||||
"50 42 58 00 00 00 00 00 08 00 00 00
|
"50 42 58 00 01 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 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
|
00 00 00 00 80 00 00 00 04 00 00 00
|
||||||
01 00 00 00 84 00 00 00 1e 00 00 00
|
01 00 00 00 84 00 00 00 1e 00 00 00
|
||||||
@@ -46,7 +46,7 @@ impl NativeInterface for Native {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn independent_v0_bytes_load_publish_and_execute_through_the_public_sealed_boundary() {
|
fn independent_v1_bytes_load_publish_and_execute_through_the_public_sealed_boundary() {
|
||||||
let bytes = minimal();
|
let bytes = minimal();
|
||||||
assert_eq!(bytes.len(), 194);
|
assert_eq!(bytes.len(), 194);
|
||||||
let mut load = LoadBudget::new(Default::default());
|
let mut load = LoadBudget::new(Default::default());
|
||||||
@@ -64,7 +64,7 @@ fn independent_v0_bytes_load_publish_and_execute_through_the_public_sealed_bound
|
|||||||
#[test]
|
#[test]
|
||||||
fn single_violation_oracles_reject_before_image_publication_with_typed_context() {
|
fn single_violation_oracles_reject_before_image_publication_with_typed_context() {
|
||||||
for (offset, replacement, code, reason, pc) in [
|
for (offset, replacement, code, reason, pc) in [
|
||||||
(4, 1, LoadCode::UnsupportedFormat, LoadReason::Version, None),
|
(4, 0, LoadCode::UnsupportedFormat, LoadReason::Version, None),
|
||||||
(178, 0, LoadCode::UnsupportedFormat, LoadReason::Builtin, None),
|
(178, 0, LoadCode::UnsupportedFormat, LoadReason::Builtin, None),
|
||||||
(162, 0xff, LoadCode::InvalidCode, LoadReason::UnknownOpcode, Some(0)),
|
(162, 0xff, LoadCode::InvalidCode, LoadReason::UnknownOpcode, Some(0)),
|
||||||
(162, 0x11, LoadCode::InvalidCode, LoadReason::StackUnderflow, Some(0)),
|
(162, 0x11, LoadCode::InvalidCode, LoadReason::StackUnderflow, Some(0)),
|
||||||
|
|||||||
@@ -1,114 +0,0 @@
|
|||||||
#![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: ®istry,
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
@@ -107,5 +107,4 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod memcard_fixture;
|
|
||||||
pub mod program;
|
pub mod program;
|
||||||
|
|||||||
@@ -1,146 +0,0 @@
|
|||||||
//! 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,13 +63,10 @@ impl TestProgram {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn serialize(&self) -> Vec<u8> {
|
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 mut budget = LoadBudget::new(Default::default());
|
||||||
let registry = Registry::resolve(
|
let registry = Registry::resolve(
|
||||||
RegistryDeclarations {
|
RegistryDeclarations {
|
||||||
types,
|
types: scalar_catalog(),
|
||||||
functions: self.functions.clone(),
|
functions: self.functions.clone(),
|
||||||
implementations: vec![],
|
implementations: vec![],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
//! Bounded run of the production desktop host, with normal teardown for metrics.
|
|
||||||
//! No machine timing or rendering loop is replaced by this measurement harness.
|
|
||||||
use prometeu_host_desktop_winit::{audio, fs_backend, log_sink, stats, utilities, wall_clock};
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[path = "../src/debugger.rs"]
|
|
||||||
mod debugger;
|
|
||||||
#[path = "../src/input.rs"]
|
|
||||||
mod input;
|
|
||||||
#[path = "../src/presentation_blit.rs"]
|
|
||||||
mod presentation_blit;
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[path = "../src/studio/mod.rs"]
|
|
||||||
mod studio;
|
|
||||||
#[path = "../src/viewport.rs"]
|
|
||||||
mod viewport;
|
|
||||||
// Compile the actual host implementation; its non-desktop entry points are unused
|
|
||||||
// by this executable. Keep measurement access out of the product's public API.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
mod runner {
|
|
||||||
include!("../src/runner.rs");
|
|
||||||
impl HostRunner {
|
|
||||||
pub(super) fn measured_state(&mut self) -> serde_json::Value {
|
|
||||||
serde_json::json!({
|
|
||||||
"host_vm_ticks": self.firmware.os.vm().tick_index(),
|
|
||||||
"completed_vm_frames": self.firmware.os.vm().completed_logical_frames(),
|
|
||||||
"logical_frame_index": self.firmware.os.vm().logical_frame_index(),
|
|
||||||
"synchronous_publications": self.firmware.os.vm().telemetry_snapshot().presented_frames,
|
|
||||||
"front_buffer_publications": self.hardware.gfx.publication_count(),
|
|
||||||
"physical_display_hz": null,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
use winit::{
|
|
||||||
application::ApplicationHandler,
|
|
||||||
event::WindowEvent,
|
|
||||||
event_loop::{ActiveEventLoop, EventLoop},
|
|
||||||
window::WindowId,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Measure {
|
|
||||||
runner: runner::HostRunner,
|
|
||||||
started: Option<Instant>,
|
|
||||||
duration: Duration,
|
|
||||||
}
|
|
||||||
impl ApplicationHandler for Measure {
|
|
||||||
fn resumed(&mut self, events: &ActiveEventLoop) {
|
|
||||||
self.runner.resumed(events);
|
|
||||||
self.started = Some(Instant::now());
|
|
||||||
}
|
|
||||||
fn window_event(&mut self, events: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
|
|
||||||
self.runner.window_event(events, id, event);
|
|
||||||
}
|
|
||||||
fn about_to_wait(&mut self, events: &ActiveEventLoop) {
|
|
||||||
if self.started.is_some_and(|start| start.elapsed() >= self.duration) {
|
|
||||||
events.exit();
|
|
||||||
} else {
|
|
||||||
self.runner.about_to_wait(events);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let path = std::env::args().nth(1).expect("cartridge directory");
|
|
||||||
let seconds: u64 = std::env::args().nth(2).map(|s| s.parse().expect("seconds")).unwrap_or(10);
|
|
||||||
assert!(seconds > 0);
|
|
||||||
let mut runner = runner::HostRunner::new(None, None);
|
|
||||||
runner.set_boot_target(prometeu_firmware::BootTarget::Cartridge {
|
|
||||||
path,
|
|
||||||
debug: false,
|
|
||||||
debug_port: 0,
|
|
||||||
});
|
|
||||||
let mut app = Measure { runner, started: None, duration: Duration::from_secs(seconds) };
|
|
||||||
let mut events = EventLoop::builder();
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
{
|
|
||||||
use winit::platform::macos::EventLoopBuilderExtMacOS;
|
|
||||||
events.with_activate_ignoring_other_apps(true);
|
|
||||||
}
|
|
||||||
events.build()?.run_app(&mut app)?;
|
|
||||||
println!(
|
|
||||||
"{}",
|
|
||||||
serde_json::json!({"schema":1,"source":"production_desktop_host",
|
|
||||||
"elapsed_seconds":app.started.map(|s|s.elapsed().as_secs_f64()),
|
|
||||||
"state":app.runner.measured_state(),
|
|
||||||
"limitation":"includes boot, no discarded warmup; host/API submission is not physical scanout"})
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
//! GPU readback qualification of the actual host blit, not a software geometry mirror.
|
|
||||||
#[path = "../src/presentation_blit.rs"]
|
|
||||||
mod presentation_blit;
|
|
||||||
#[path = "../src/viewport.rs"]
|
|
||||||
mod viewport;
|
|
||||||
use pixels::{PixelsBuilder, SurfaceTexture, wgpu};
|
|
||||||
use winit::{
|
|
||||||
application::ApplicationHandler,
|
|
||||||
event_loop::{ActiveEventLoop, EventLoop},
|
|
||||||
window::Window,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct Probe;
|
|
||||||
impl ApplicationHandler for Probe {
|
|
||||||
fn resumed(&mut self, events: &ActiveEventLoop) {
|
|
||||||
let window = Box::leak(Box::new(
|
|
||||||
events
|
|
||||||
.create_window(
|
|
||||||
Window::default_attributes()
|
|
||||||
.with_title("PROMETEU graphics qualification")
|
|
||||||
.with_inner_size(winit::dpi::PhysicalSize::new(640, 360)),
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
));
|
|
||||||
let size = window.inner_size();
|
|
||||||
let mut pixels =
|
|
||||||
PixelsBuilder::new(640, 360, SurfaceTexture::new(size.width, size.height, &*window))
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
for (i, p) in pixels.frame_mut().as_chunks_mut::<4>().0.iter_mut().enumerate() {
|
|
||||||
let (x, y) = (i % 640, i / 640);
|
|
||||||
// Single-pixel checkerboard detects smoothing; all channels equal avoid channel-order ambiguity.
|
|
||||||
let value = if (x + y) % 2 == 0 { 255 } else { 0 };
|
|
||||||
p.copy_from_slice(&[value, value, value, 255]);
|
|
||||||
}
|
|
||||||
// Exercise real surface acquisition/upload/shader validation too.
|
|
||||||
let blit = presentation_blit::PresentationBlit::new(&pixels);
|
|
||||||
pixels
|
|
||||||
.render_with(|e, t, _| {
|
|
||||||
blit.render(
|
|
||||||
e,
|
|
||||||
t,
|
|
||||||
viewport::Viewport::new((size.width, size.height), (640, 360)).unwrap(),
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let ctx = pixels.context();
|
|
||||||
// A newly created macOS surface may be occluded: render_with then returns
|
|
||||||
// without uploading. Offscreen verification must establish its own input.
|
|
||||||
ctx.queue.write_texture(
|
|
||||||
wgpu::TexelCopyTextureInfo {
|
|
||||||
texture: &ctx.texture,
|
|
||||||
mip_level: 0,
|
|
||||||
origin: wgpu::Origin3d::ZERO,
|
|
||||||
aspect: wgpu::TextureAspect::All,
|
|
||||||
},
|
|
||||||
pixels.frame(),
|
|
||||||
wgpu::TexelCopyBufferLayout {
|
|
||||||
offset: 0,
|
|
||||||
bytes_per_row: Some(640 * 4),
|
|
||||||
rows_per_image: Some(360),
|
|
||||||
},
|
|
||||||
ctx.texture_extent,
|
|
||||||
);
|
|
||||||
for (w, h) in [(1280u32, 720u32), (1000, 700), (320, 240), (641, 361)] {
|
|
||||||
let view = viewport::Viewport::new((w, h), (640, 360)).unwrap();
|
|
||||||
let texture = ctx.device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
label: Some("blit-readback"),
|
|
||||||
size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: pixels.render_texture_format(),
|
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
|
|
||||||
view_formats: &[],
|
|
||||||
});
|
|
||||||
let stride = (w * 4).div_ceil(256) * 256;
|
|
||||||
let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
|
|
||||||
label: Some("readback"),
|
|
||||||
size: u64::from(stride) * u64::from(h),
|
|
||||||
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
|
|
||||||
mapped_at_creation: false,
|
|
||||||
});
|
|
||||||
let mut encoder = ctx.device.create_command_encoder(&Default::default());
|
|
||||||
blit.render(&mut encoder, &texture.create_view(&Default::default()), view);
|
|
||||||
encoder.copy_texture_to_buffer(
|
|
||||||
wgpu::TexelCopyTextureInfo {
|
|
||||||
texture: &texture,
|
|
||||||
mip_level: 0,
|
|
||||||
origin: wgpu::Origin3d::ZERO,
|
|
||||||
aspect: wgpu::TextureAspect::All,
|
|
||||||
},
|
|
||||||
wgpu::TexelCopyBufferInfo {
|
|
||||||
buffer: &buffer,
|
|
||||||
layout: wgpu::TexelCopyBufferLayout {
|
|
||||||
offset: 0,
|
|
||||||
bytes_per_row: Some(stride),
|
|
||||||
rows_per_image: Some(h),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
|
|
||||||
);
|
|
||||||
ctx.queue.submit([encoder.finish()]);
|
|
||||||
let (tx, rx) = std::sync::mpsc::channel();
|
|
||||||
buffer.slice(..).map_async(wgpu::MapMode::Read, move |result| tx.send(result).unwrap());
|
|
||||||
ctx.device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
|
|
||||||
rx.recv().unwrap().unwrap();
|
|
||||||
let data = buffer.slice(..).get_mapped_range();
|
|
||||||
let mut checked = 0;
|
|
||||||
for y in 0..h {
|
|
||||||
for x in 0..w {
|
|
||||||
let offset = (y * stride + x * 4) as usize;
|
|
||||||
let rgb = &data[offset..offset + 3];
|
|
||||||
// Every output pixel is black or white: any intermediate value means smoothing.
|
|
||||||
assert!(
|
|
||||||
rgb == [0, 0, 0] || rgb == [255, 255, 255],
|
|
||||||
"filtered pixel at {w}x{h}:({x},{y})={rgb:?}"
|
|
||||||
);
|
|
||||||
if let Some((nx, ny)) = view.inverse(x as f64 + 0.5, y as f64 + 0.5) {
|
|
||||||
let expected = if (nx + ny) % 2 == 0 { 255 } else { 0 };
|
|
||||||
assert_eq!(
|
|
||||||
rgb, [expected; 3],
|
|
||||||
"GPU/input transform mismatch at {w}x{h}:({x},{y})"
|
|
||||||
);
|
|
||||||
checked += 1;
|
|
||||||
} else {
|
|
||||||
assert_eq!(rgb, [0, 0, 0], "bar");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert!(checked > 0);
|
|
||||||
println!("GPU_NEAREST_PASS={w}x{h} checked={checked}");
|
|
||||||
}
|
|
||||||
events.exit();
|
|
||||||
}
|
|
||||||
fn window_event(
|
|
||||||
&mut self,
|
|
||||||
_: &ActiveEventLoop,
|
|
||||||
_: winit::window::WindowId,
|
|
||||||
_: winit::event::WindowEvent,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn main() {
|
|
||||||
EventLoop::new().unwrap().run_app(&mut Probe).unwrap();
|
|
||||||
}
|
|
||||||
@@ -3,11 +3,48 @@ use winit::event::{ElementState, MouseButton, WindowEvent};
|
|||||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct FbViewport {
|
||||||
|
pub x: f32,
|
||||||
|
pub y: f32,
|
||||||
|
pub w: f32,
|
||||||
|
pub h: f32,
|
||||||
|
pub scale: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FbViewport {
|
||||||
|
fn framebuffer_viewport(window: &Window, display_size: (usize, usize)) -> Option<FbViewport> {
|
||||||
|
let size = window.inner_size();
|
||||||
|
|
||||||
|
let win_w = size.width as f32;
|
||||||
|
let win_h = size.height as f32;
|
||||||
|
|
||||||
|
if win_w <= 0.0 || win_h <= 0.0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let fb_w = display_size.0 as f32;
|
||||||
|
let fb_h = display_size.1 as f32;
|
||||||
|
|
||||||
|
let scale_x = (win_w / fb_w).floor();
|
||||||
|
let scale_y = (win_h / fb_h).floor();
|
||||||
|
|
||||||
|
let scale = scale_x.min(scale_y).max(1.0);
|
||||||
|
|
||||||
|
let viewport_w = fb_w * scale;
|
||||||
|
let viewport_h = fb_h * scale;
|
||||||
|
|
||||||
|
let viewport_x = ((win_w - viewport_w) * 0.5).floor();
|
||||||
|
let viewport_y = ((win_h - viewport_h) * 0.5).floor();
|
||||||
|
|
||||||
|
Some(FbViewport { x: viewport_x, y: viewport_y, w: viewport_w, h: viewport_h, scale })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct HostInputHandler {
|
pub struct HostInputHandler {
|
||||||
pub signals: InputSignals,
|
pub signals: InputSignals,
|
||||||
system_controls: HostSystemControls,
|
system_controls: HostSystemControls,
|
||||||
display_size: (usize, usize),
|
display_size: (usize, usize),
|
||||||
cursor: Option<(f64, f64)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -29,7 +66,7 @@ impl HostSystemControls {
|
|||||||
|
|
||||||
impl Default for HostInputHandler {
|
impl Default for HostInputHandler {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new((prometeu_hal::graphics_contract::WIDTH, prometeu_hal::graphics_contract::HEIGHT))
|
Self::new((480, 270))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +76,6 @@ impl HostInputHandler {
|
|||||||
signals: InputSignals::default(),
|
signals: InputSignals::default(),
|
||||||
system_controls: HostSystemControls::default(),
|
system_controls: HostSystemControls::default(),
|
||||||
display_size,
|
display_size,
|
||||||
cursor: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,34 +121,50 @@ impl HostInputHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
self.cursor = Some((position.x, position.y));
|
if let Some((x, y)) =
|
||||||
let size = window.inner_size();
|
window_to_fb(position.x as f32, position.y as f32, window, self.display_size)
|
||||||
self.update_pointer((size.width, size.height));
|
{
|
||||||
|
self.signals.x_pos = x;
|
||||||
|
self.signals.y_pos = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
WindowEvent::CursorLeft { .. } | WindowEvent::Focused(false) => {
|
|
||||||
self.signals.f_signal = false;
|
|
||||||
self.cursor = None;
|
|
||||||
}
|
|
||||||
WindowEvent::Resized(size) => self.update_pointer((size.width, size.height)),
|
|
||||||
WindowEvent::MouseInput { state, button, .. } if *button == MouseButton::Left => {
|
WindowEvent::MouseInput { state, button, .. } if *button == MouseButton::Left => {
|
||||||
let size = window.inner_size();
|
match state {
|
||||||
let inside = self.pointer_position((size.width, size.height)).is_some();
|
ElementState::Pressed => {
|
||||||
self.signals.f_signal = *state == ElementState::Pressed && inside;
|
self.signals.f_signal = true;
|
||||||
|
}
|
||||||
|
ElementState::Released => {
|
||||||
|
self.signals.f_signal = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fn pointer_position(&self, surface: (u32, u32)) -> Option<(i32, i32)> {
|
fn window_to_fb(
|
||||||
let (x, y) = self.cursor?;
|
wx: f32,
|
||||||
crate::viewport::Viewport::new(surface, self.display_size)?.inverse(x, y)
|
wy: f32,
|
||||||
}
|
window: &Window,
|
||||||
fn update_pointer(&mut self, surface: (u32, u32)) {
|
display_size: (usize, usize),
|
||||||
if let Some((x, y)) = self.pointer_position(surface) {
|
) -> Option<(i32, i32)> {
|
||||||
self.signals.x_pos = x;
|
let viewport = FbViewport::framebuffer_viewport(window, display_size)?;
|
||||||
self.signals.y_pos = y;
|
|
||||||
} else {
|
let local_x = wx - viewport.x;
|
||||||
self.signals.f_signal = false;
|
let local_y = wy - viewport.y;
|
||||||
|
|
||||||
|
if local_x < 0.0 || local_y < 0.0 || local_x >= viewport.w || local_y >= viewport.h {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let fb_x = (local_x / viewport.scale).floor() as i32;
|
||||||
|
let fb_y = (local_y / viewport.scale).floor() as i32;
|
||||||
|
|
||||||
|
Some((
|
||||||
|
fb_x.clamp(0, display_size.0 as i32 - 1),
|
||||||
|
fb_y.clamp(0, display_size.1 as i32 - 1),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,23 +173,6 @@ impl HostInputHandler {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn drag_cancels_outside_and_cannot_recapture_on_reentry() {
|
|
||||||
let mut input = HostInputHandler {
|
|
||||||
cursor: Some((200.0, 200.0)),
|
|
||||||
signals: InputSignals { f_signal: true, ..Default::default() },
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
input.update_pointer((1000, 700));
|
|
||||||
assert_eq!((input.signals.x_pos, input.signals.y_pos), (20, 30));
|
|
||||||
input.cursor = Some((10.0, 200.0)); // left letterbox
|
|
||||||
input.update_pointer((1000, 700));
|
|
||||||
assert!(!input.signals.f_signal);
|
|
||||||
input.cursor = Some((200.0, 200.0));
|
|
||||||
input.update_pointer((1000, 700));
|
|
||||||
assert!(!input.signals.f_signal);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn assert_no_pad_signal(signals: InputSignals) {
|
fn assert_no_pad_signal(signals: InputSignals) {
|
||||||
assert!(!signals.up_signal);
|
assert!(!signals.up_signal);
|
||||||
assert!(!signals.down_signal);
|
assert!(!signals.down_signal);
|
||||||
|
|||||||
@@ -4,12 +4,9 @@ pub mod debugger;
|
|||||||
pub mod fs_backend;
|
pub mod fs_backend;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod log_sink;
|
pub mod log_sink;
|
||||||
mod presentation_blit;
|
|
||||||
pub mod runner;
|
pub mod runner;
|
||||||
pub mod stats;
|
pub mod stats;
|
||||||
pub mod studio;
|
|
||||||
pub mod utilities;
|
pub mod utilities;
|
||||||
mod viewport;
|
|
||||||
pub mod wall_clock;
|
pub mod wall_clock;
|
||||||
|
|
||||||
use cap::load_cap_config;
|
use cap::load_cap_config;
|
||||||
@@ -21,9 +18,6 @@ use winit::event_loop::EventLoop;
|
|||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
/// Run an interactive Studio session without creating a desktop window.
|
|
||||||
#[arg(long, requires = "run", conflicts_with = "debug")]
|
|
||||||
studio: bool,
|
|
||||||
/// Run a cartridge
|
/// Run a cartridge
|
||||||
#[arg(long, value_name = "PATH", conflicts_with = "debug")]
|
#[arg(long, value_name = "PATH", conflicts_with = "debug")]
|
||||||
run: Option<String>,
|
run: Option<String>,
|
||||||
@@ -60,23 +54,6 @@ struct Cli {
|
|||||||
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
|
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let cli = Cli::parse();
|
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 fs_root = cli.fs_root;
|
||||||
let cap_config = cli.cap.as_ref().and_then(|path| load_cap_config(path));
|
let cap_config = cli.cap.as_ref().and_then(|path| load_cap_config(path));
|
||||||
|
|
||||||
|
|||||||
@@ -1,107 +0,0 @@
|
|||||||
//! Host-only blit: pixels' built-in PixelPerfect path crops below 1x and Fill smooths.
|
|
||||||
use crate::viewport::Viewport;
|
|
||||||
use pixels::{Pixels, wgpu};
|
|
||||||
|
|
||||||
pub(crate) struct PresentationBlit {
|
|
||||||
pipeline: wgpu::RenderPipeline,
|
|
||||||
texture: wgpu::BindGroup,
|
|
||||||
geometry: wgpu::Buffer,
|
|
||||||
queue: wgpu::Queue,
|
|
||||||
}
|
|
||||||
impl PresentationBlit {
|
|
||||||
pub fn new(pixels: &Pixels<'_>) -> Self {
|
|
||||||
let device = pixels.device();
|
|
||||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
|
||||||
label: Some("native-nearest-blit"),
|
|
||||||
source: wgpu::ShaderSource::Wgsl(
|
|
||||||
r#"
|
|
||||||
struct Vertex { @builtin(position) position: vec4f, @location(0) uv: vec2f }
|
|
||||||
@vertex fn vs(@builtin(vertex_index) i: u32) -> Vertex {
|
|
||||||
let p = array<vec2f,3>(vec2f(-1,-1),vec2f(3,-1),vec2f(-1,3));
|
|
||||||
var v: Vertex; v.position=vec4f(p[i],0,1); v.uv=vec2f((p[i].x+1)*0.5,(1-p[i].y)*0.5); return v;
|
|
||||||
}
|
|
||||||
@group(0) @binding(0) var image: texture_2d<f32>;
|
|
||||||
@group(0) @binding(1) var<uniform> geometry: vec4f;
|
|
||||||
@fragment fn fs(v: Vertex) -> @location(0) vec4f {
|
|
||||||
let size=vec2i(textureDimensions(image));
|
|
||||||
let pixel=clamp(vec2i(floor((v.position.xy-geometry.xy)/geometry.z)),vec2i(0),size-vec2i(1));
|
|
||||||
return textureLoad(image,pixel,0);
|
|
||||||
}"#
|
|
||||||
.into(),
|
|
||||||
),
|
|
||||||
});
|
|
||||||
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
||||||
label: Some("native-nearest-blit"),
|
|
||||||
layout: None,
|
|
||||||
vertex: wgpu::VertexState {
|
|
||||||
module: &shader,
|
|
||||||
entry_point: Some("vs"),
|
|
||||||
buffers: &[],
|
|
||||||
compilation_options: Default::default(),
|
|
||||||
},
|
|
||||||
fragment: Some(wgpu::FragmentState {
|
|
||||||
module: &shader,
|
|
||||||
entry_point: Some("fs"),
|
|
||||||
compilation_options: Default::default(),
|
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
|
||||||
format: pixels.render_texture_format(),
|
|
||||||
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
|
||||||
})],
|
|
||||||
}),
|
|
||||||
primitive: Default::default(),
|
|
||||||
depth_stencil: None,
|
|
||||||
multisample: Default::default(),
|
|
||||||
multiview_mask: None,
|
|
||||||
cache: None,
|
|
||||||
});
|
|
||||||
let view = pixels.context().texture.create_view(&Default::default());
|
|
||||||
let geometry = device.create_buffer(&wgpu::BufferDescriptor {
|
|
||||||
label: Some("native-viewport"),
|
|
||||||
size: 16,
|
|
||||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
|
||||||
mapped_at_creation: false,
|
|
||||||
});
|
|
||||||
let texture = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
label: Some("native-nearest-texture"),
|
|
||||||
layout: &pipeline.get_bind_group_layout(0),
|
|
||||||
entries: &[
|
|
||||||
wgpu::BindGroupEntry {
|
|
||||||
binding: 0,
|
|
||||||
resource: wgpu::BindingResource::TextureView(&view),
|
|
||||||
},
|
|
||||||
wgpu::BindGroupEntry { binding: 1, resource: geometry.as_entire_binding() },
|
|
||||||
],
|
|
||||||
});
|
|
||||||
Self { pipeline, texture, geometry, queue: pixels.context().queue.clone() }
|
|
||||||
}
|
|
||||||
pub fn render(
|
|
||||||
&self,
|
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
|
||||||
target: &wgpu::TextureView,
|
|
||||||
viewport: Viewport,
|
|
||||||
) {
|
|
||||||
let geometry = [viewport.x, viewport.y, viewport.scale, 0.0].map(f32::to_ne_bytes);
|
|
||||||
self.queue.write_buffer(&self.geometry, 0, geometry.as_flattened());
|
|
||||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
||||||
label: Some("native-nearest-present"),
|
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
|
||||||
view: target,
|
|
||||||
resolve_target: None,
|
|
||||||
ops: wgpu::Operations {
|
|
||||||
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
|
|
||||||
store: wgpu::StoreOp::Store,
|
|
||||||
},
|
|
||||||
depth_slice: None,
|
|
||||||
})],
|
|
||||||
depth_stencil_attachment: None,
|
|
||||||
timestamp_writes: None,
|
|
||||||
occlusion_query_set: None,
|
|
||||||
multiview_mask: None,
|
|
||||||
});
|
|
||||||
pass.set_pipeline(&self.pipeline);
|
|
||||||
pass.set_bind_group(0, &self.texture, &[]);
|
|
||||||
pass.set_viewport(viewport.x, viewport.y, viewport.width, viewport.height, 0.0, 1.0);
|
|
||||||
pass.draw(0..3, 0..1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -118,7 +118,6 @@ pub struct HostRunner {
|
|||||||
window: Option<&'static Window>,
|
window: Option<&'static Window>,
|
||||||
/// The pixel buffer interface for rendering to the GPU.
|
/// The pixel buffer interface for rendering to the GPU.
|
||||||
pixels: Option<Pixels<'static>>,
|
pixels: Option<Pixels<'static>>,
|
||||||
blit: Option<crate::presentation_blit::PresentationBlit>,
|
|
||||||
|
|
||||||
/// The instance of the virtual hardware peripherals.
|
/// The instance of the virtual hardware peripherals.
|
||||||
hardware: Hardware,
|
hardware: Hardware,
|
||||||
@@ -152,7 +151,6 @@ pub struct HostRunner {
|
|||||||
render_frame_store: LatestRenderFrameStore,
|
render_frame_store: LatestRenderFrameStore,
|
||||||
/// Tracks whether a new logical frame or host-only surface invalidation requires presentation.
|
/// Tracks whether a new logical frame or host-only surface invalidation requires presentation.
|
||||||
presentation: PresentationState,
|
presentation: PresentationState,
|
||||||
studio_capture: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HostRunner {
|
impl HostRunner {
|
||||||
@@ -196,7 +194,7 @@ impl HostRunner {
|
|||||||
|
|
||||||
/// Creates a new desktop runner instance.
|
/// Creates a new desktop runner instance.
|
||||||
pub(crate) fn new(fs_root: Option<String>, cap_config: Option<CertificationConfig>) -> Self {
|
pub(crate) fn new(fs_root: Option<String>, cap_config: Option<CertificationConfig>) -> Self {
|
||||||
let target_fps = prometeu_hal::graphics_contract::REFRESH_HZ;
|
let target_fps = 60;
|
||||||
|
|
||||||
let mut firmware = Firmware::new(cap_config);
|
let mut firmware = Firmware::new(cap_config);
|
||||||
if let Some(root) = &fs_root {
|
if let Some(root) = &fs_root {
|
||||||
@@ -226,13 +224,12 @@ impl HostRunner {
|
|||||||
Self {
|
Self {
|
||||||
window: None,
|
window: None,
|
||||||
pixels: None,
|
pixels: None,
|
||||||
blit: None,
|
|
||||||
hardware,
|
hardware,
|
||||||
firmware,
|
firmware,
|
||||||
input: HostInputHandler::new(display_size),
|
input: HostInputHandler::new(display_size),
|
||||||
fs_root,
|
fs_root,
|
||||||
log_sink: HostConsoleSink::new(),
|
log_sink: HostConsoleSink::new(),
|
||||||
frame_target_dt: Duration::from_nanos(1_000_000_000 / u64::from(target_fps)),
|
frame_target_dt: Duration::from_nanos(1_000_000_000 / target_fps),
|
||||||
last_frame_time: Instant::now(),
|
last_frame_time: Instant::now(),
|
||||||
accumulator: Duration::ZERO,
|
accumulator: Duration::ZERO,
|
||||||
|
|
||||||
@@ -241,161 +238,9 @@ impl HostRunner {
|
|||||||
audio: HostAudio::new(),
|
audio: HostAudio::new(),
|
||||||
render_frame_store,
|
render_frame_store,
|
||||||
presentation: PresentationState::default(),
|
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 {
|
fn window(&self) -> &'static Window {
|
||||||
self.window.expect("window not created yet")
|
self.window.expect("window not created yet")
|
||||||
}
|
}
|
||||||
@@ -432,32 +277,15 @@ impl HostRunner {
|
|||||||
fn machine_running(&mut self) -> bool {
|
fn machine_running(&mut self) -> bool {
|
||||||
!self.firmware.os.vm().paused() && !self.debugger.waiting_for_start
|
!self.firmware.os.vm().paused() && !self.debugger.waiting_for_start
|
||||||
}
|
}
|
||||||
|
|
||||||
fn observe_latest_frame(&mut self) {
|
|
||||||
let active_ownership = self.firmware.os.vm().active_render_ownership();
|
|
||||||
if let Some(worker_frame) = self.render_frame_store.latest_frame()
|
|
||||||
&& should_present_worker_frame(&self.firmware.state, &worker_frame, active_ownership)
|
|
||||||
{
|
|
||||||
self.presentation
|
|
||||||
.note_published_frame(worker_frame.ownership, worker_frame.frame_id.get());
|
|
||||||
} else {
|
|
||||||
// Observe the actual front-buffer swap, including native Hub/Shell
|
|
||||||
// publications which do not execute a VM frame.
|
|
||||||
let publication = self.hardware.gfx.publication_count();
|
|
||||||
if publication > 0 {
|
|
||||||
self.presentation.note_published_frame(active_ownership, publication);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ApplicationHandler for HostRunner {
|
impl ApplicationHandler for HostRunner {
|
||||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||||
let attrs = WindowAttributes::default()
|
let attrs = WindowAttributes::default()
|
||||||
.with_title(format!(
|
.with_title(format!(
|
||||||
"PROMETEU | GFX: {:.1} KB | Host ticks/s: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {} done {}",
|
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {} done {}",
|
||||||
0.0, 0.0, 0.0, 0, 0, 0, 0))
|
0.0, 0.0, 0.0, 0, 0, 0, 0))
|
||||||
.with_inner_size(LogicalSize::new((prometeu_hal::graphics_contract::WIDTH * 2) as f64, (prometeu_hal::graphics_contract::HEIGHT * 2) as f64))
|
.with_inner_size(LogicalSize::new(960.0, 540.0))
|
||||||
.with_min_inner_size(LogicalSize::new(320.0, 180.0));
|
.with_min_inner_size(LogicalSize::new(320.0, 180.0));
|
||||||
|
|
||||||
let window = event_loop.create_window(attrs).expect("failed to create window");
|
let window = event_loop.create_window(attrs).expect("failed to create window");
|
||||||
@@ -479,7 +307,6 @@ impl ApplicationHandler for HostRunner {
|
|||||||
|
|
||||||
pixels.frame_mut().fill(0);
|
pixels.frame_mut().fill(0);
|
||||||
|
|
||||||
self.blit = Some(crate::presentation_blit::PresentationBlit::new(&pixels));
|
|
||||||
self.pixels = Some(pixels);
|
self.pixels = Some(pixels);
|
||||||
|
|
||||||
if let Err(err) = self.audio.init(self.hardware.audio.transport()) {
|
if let Err(err) = self.audio.init(self.hardware.audio.transport()) {
|
||||||
@@ -510,16 +337,9 @@ impl ApplicationHandler for HostRunner {
|
|||||||
self.request_redraw_if_needed();
|
self.request_redraw_if_needed();
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowEvent::Occluded(false) => {
|
|
||||||
self.invalidate_host_surface();
|
|
||||||
self.request_redraw_if_needed();
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
let pixels = self.pixels.as_mut().expect("pixels not initialized");
|
let pixels = self.pixels.as_mut().expect("pixels not initialized");
|
||||||
|
|
||||||
let mut presented_worker = None;
|
|
||||||
let mut presented_frame = None;
|
|
||||||
{
|
{
|
||||||
let frame = pixels.frame_mut();
|
let frame = pixels.frame_mut();
|
||||||
|
|
||||||
@@ -531,47 +351,20 @@ impl ApplicationHandler for HostRunner {
|
|||||||
active_ownership,
|
active_ownership,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
presented_worker = Some((worker_frame.ownership, worker_frame.frame_id));
|
self.firmware
|
||||||
presented_frame =
|
.os
|
||||||
Some((worker_frame.ownership, worker_frame.frame_id.get()));
|
.vm()
|
||||||
|
.record_repeated_render_worker_frame(worker_frame.frame_id);
|
||||||
draw_owned_rgba8888_frame_to_rgba8(&worker_frame, frame);
|
draw_owned_rgba8888_frame_to_rgba8(&worker_frame, frame);
|
||||||
} else {
|
} else {
|
||||||
let src = self.hardware.gfx.front_buffer();
|
let src = self.hardware.gfx.front_buffer();
|
||||||
draw_rgba8888_to_rgba8(src, frame);
|
draw_rgba8888_to_rgba8(src, frame);
|
||||||
let publication = self.hardware.gfx.publication_count();
|
|
||||||
if publication > 0 {
|
|
||||||
presented_frame = Some((active_ownership, publication));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let size = self.window.expect("window").inner_size();
|
if pixels.render().is_err() {
|
||||||
let rendered = std::cell::Cell::new(false);
|
|
||||||
let viewport = crate::viewport::Viewport::new(
|
|
||||||
(size.width, size.height),
|
|
||||||
self.hardware.display_size(),
|
|
||||||
);
|
|
||||||
let present_start = Instant::now();
|
|
||||||
let result = pixels.render_with(|encoder, target, _| {
|
|
||||||
if let Some(viewport) = viewport {
|
|
||||||
self.blit.as_ref().expect("blit").render(encoder, target, viewport);
|
|
||||||
rendered.set(true);
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
self.stats.record_present_time(present_start.elapsed());
|
|
||||||
if result.is_err() {
|
|
||||||
event_loop.exit();
|
event_loop.exit();
|
||||||
} else if rendered.get() {
|
} else {
|
||||||
if let Some((ownership, frame_id)) = presented_frame {
|
|
||||||
self.stats.record_presented(ownership, frame_id);
|
|
||||||
}
|
|
||||||
if let Some((ownership, frame_id)) = presented_worker {
|
|
||||||
self.firmware
|
|
||||||
.os
|
|
||||||
.vm()
|
|
||||||
.record_presented_render_worker_frame(ownership, frame_id);
|
|
||||||
}
|
|
||||||
self.presentation.mark_presented();
|
self.presentation.mark_presented();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -642,12 +435,34 @@ impl ApplicationHandler for HostRunner {
|
|||||||
while self.accumulator >= self.frame_target_dt {
|
while self.accumulator >= self.frame_target_dt {
|
||||||
// Reclaim returned audio owners before another guest attempt, even
|
// Reclaim returned audio owners before another guest attempt, even
|
||||||
// when the previous attempt paused or stopped at backpressure.
|
// when the previous attempt paused or stopped at backpressure.
|
||||||
self.tick_machine();
|
self.audio.service();
|
||||||
|
// 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();
|
||||||
|
|
||||||
self.accumulator -= self.frame_target_dt;
|
self.accumulator -= self.frame_target_dt;
|
||||||
self.stats.record_frame();
|
self.stats.record_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let active_ownership = self.firmware.os.vm().active_render_ownership();
|
||||||
|
if let Some(worker_frame) = self.render_frame_store.latest_frame()
|
||||||
|
&& should_present_worker_frame(&self.firmware.state, &worker_frame, active_ownership)
|
||||||
|
{
|
||||||
|
self.presentation
|
||||||
|
.note_published_frame(worker_frame.ownership, worker_frame.frame_id.get());
|
||||||
|
} else {
|
||||||
|
self.presentation
|
||||||
|
.note_published_frame(active_ownership, self.firmware.os.frame_index());
|
||||||
|
}
|
||||||
|
|
||||||
if was_debugger_connected != self.debugger.stream.is_some()
|
if was_debugger_connected != self.debugger.stream.is_some()
|
||||||
|| was_waiting_for_start != self.debugger.waiting_for_start
|
|| was_waiting_for_start != self.debugger.waiting_for_start
|
||||||
|| was_paused != self.firmware.os.vm().paused()
|
|| was_paused != self.firmware.os.vm().paused()
|
||||||
@@ -656,7 +471,6 @@ impl ApplicationHandler for HostRunner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. Feedback and Synchronization.
|
// 4. Feedback and Synchronization.
|
||||||
self.observe_latest_frame();
|
|
||||||
self.audio.update_stats(&mut self.stats);
|
self.audio.update_stats(&mut self.stats);
|
||||||
|
|
||||||
// Update technical statistics displayed in the window title.
|
// Update technical statistics displayed in the window title.
|
||||||
@@ -686,73 +500,6 @@ impl ApplicationHandler for HostRunner {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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::BootTarget;
|
||||||
use prometeu_firmware::firmware::firmware_state::{
|
use prometeu_firmware::firmware::firmware_state::{
|
||||||
GameRunningStep, HubHomeStep, ShellRunningStep,
|
GameRunningStep, HubHomeStep, ShellRunningStep,
|
||||||
@@ -797,15 +544,6 @@ mod tests {
|
|||||||
for _ in 0..8 {
|
for _ in 0..8 {
|
||||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||||
}
|
}
|
||||||
// Successful session publications must drive redraw even when the
|
|
||||||
// physical surface has not otherwise been invalidated.
|
|
||||||
let completed = runner.firmware.os.vm().logical_frame_index();
|
|
||||||
assert!(completed > 0, "session published a frame in boot {cycle}");
|
|
||||||
runner.observe_latest_frame();
|
|
||||||
runner.presentation.mark_presented();
|
|
||||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
|
||||||
runner.observe_latest_frame();
|
|
||||||
assert!(runner.presentation.needs_redraw(), "new session frame must redraw");
|
|
||||||
runner.firmware.request_home_from_host();
|
runner.firmware.request_home_from_host();
|
||||||
for _ in 0..32 {
|
for _ in 0..32 {
|
||||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||||
@@ -814,16 +552,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert!(matches!(runner.firmware.state, FirmwareState::HubHome(_)), "home {cycle}");
|
assert!(matches!(runner.firmware.state, FirmwareState::HubHome(_)), "home {cycle}");
|
||||||
runner.observe_latest_frame();
|
|
||||||
runner.presentation.mark_presented();
|
|
||||||
let vm_frame = runner.firmware.os.vm().logical_frame_index();
|
|
||||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
|
||||||
assert_eq!(runner.firmware.os.vm().logical_frame_index(), vm_frame);
|
|
||||||
runner.observe_latest_frame();
|
|
||||||
assert!(
|
|
||||||
runner.presentation.needs_redraw(),
|
|
||||||
"native Hub publication must redraw without a VM frame"
|
|
||||||
);
|
|
||||||
assert!(runner.firmware.resume_resident_game_from_home());
|
assert!(runner.firmware.resume_resident_game_from_home());
|
||||||
for _ in 0..8 {
|
for _ in 0..8 {
|
||||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||||
|
|||||||
@@ -4,13 +4,6 @@ use winit::window::Window;
|
|||||||
|
|
||||||
pub struct HostStats {
|
pub struct HostStats {
|
||||||
pub last_stats_update: Instant,
|
pub last_stats_update: Instant,
|
||||||
metrics_path: Option<std::path::PathBuf>,
|
|
||||||
present_us: Vec<u64>,
|
|
||||||
present_count: u64,
|
|
||||||
ticks_total: u64,
|
|
||||||
first_presentations: u64,
|
|
||||||
repeated_presentations: u64,
|
|
||||||
last_presented: Option<(prometeu_hal::RenderOwnership, u64)>,
|
|
||||||
pub frames_since_last_update: u64,
|
pub frames_since_last_update: u64,
|
||||||
pub current_fps: f64,
|
pub current_fps: f64,
|
||||||
pub audio_load_accum_us: u64,
|
pub audio_load_accum_us: u64,
|
||||||
@@ -30,13 +23,6 @@ impl HostStats {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
last_stats_update: Instant::now(),
|
last_stats_update: Instant::now(),
|
||||||
metrics_path: std::env::var_os("PROMETEU_GRAPHICS_METRICS").map(Into::into),
|
|
||||||
present_us: Vec::new(),
|
|
||||||
present_count: 0,
|
|
||||||
ticks_total: 0,
|
|
||||||
first_presentations: 0,
|
|
||||||
repeated_presentations: 0,
|
|
||||||
last_presented: None,
|
|
||||||
frames_since_last_update: 0,
|
frames_since_last_update: 0,
|
||||||
current_fps: 0.0,
|
current_fps: 0.0,
|
||||||
audio_load_accum_us: 0,
|
audio_load_accum_us: 0,
|
||||||
@@ -47,31 +33,8 @@ impl HostStats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// API latency, including texture upload/submit; not a physical scanout timestamp.
|
|
||||||
pub fn record_present_time(&mut self, duration: Duration) {
|
|
||||||
self.present_count += 1;
|
|
||||||
if self.metrics_path.is_some() {
|
|
||||||
let value = duration.as_micros() as u64;
|
|
||||||
if self.present_us.len() < 16384 {
|
|
||||||
self.present_us.push(value);
|
|
||||||
} else {
|
|
||||||
self.present_us[(self.present_count as usize - 1) % 16384] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn record_frame(&mut self) {
|
pub fn record_frame(&mut self) {
|
||||||
self.frames_since_last_update += 1;
|
self.frames_since_last_update += 1;
|
||||||
self.ticks_total += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn record_presented(&mut self, ownership: prometeu_hal::RenderOwnership, frame_id: u64) {
|
|
||||||
let marker = (ownership, frame_id);
|
|
||||||
if self.last_presented == Some(marker) {
|
|
||||||
self.repeated_presentations += 1;
|
|
||||||
} else {
|
|
||||||
self.first_presentations += 1;
|
|
||||||
}
|
|
||||||
self.last_presented = Some(marker);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn record_audio_perf(&mut self, us: u64) {
|
pub fn record_audio_perf(&mut self, us: u64) {
|
||||||
@@ -103,8 +66,7 @@ impl HostStats {
|
|||||||
|
|
||||||
if let Some(window) = window {
|
if let Some(window) = window {
|
||||||
// Fixed comparison always against 60Hz, keep even when doing CPU stress tests
|
// Fixed comparison always against 60Hz, keep even when doing CPU stress tests
|
||||||
let frame_budget_us =
|
let frame_budget_us = 16666.0;
|
||||||
1_000_000.0 / prometeu_hal::graphics_contract::REFRESH_HZ as f64;
|
|
||||||
let cpu_load_core = (self.average_host_cpu_ms() * 1000.0 / frame_budget_us) * 100.0;
|
let cpu_load_core = (self.average_host_cpu_ms() * 1000.0 / frame_budget_us) * 100.0;
|
||||||
|
|
||||||
let cpu_load_audio = if self.audio_load_samples > 0 {
|
let cpu_load_audio = if self.audio_load_samples > 0 {
|
||||||
@@ -114,7 +76,7 @@ impl HostStats {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let title = format!(
|
let title = format!(
|
||||||
"PROMETEU | GFX: {:.1} KB | Host ticks/s: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {} done {}",
|
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {} done {}",
|
||||||
0,
|
0,
|
||||||
self.current_fps,
|
self.current_fps,
|
||||||
cpu_load_core,
|
cpu_load_core,
|
||||||
@@ -133,41 +95,3 @@ impl HostStats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for HostStats {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if let Some(path) = &self.metrics_path {
|
|
||||||
self.present_us.sort_unstable();
|
|
||||||
let n = self.present_us.len();
|
|
||||||
let percentile =
|
|
||||||
|p: usize| if n == 0 { 0 } else { self.present_us[(n * p).div_ceil(100) - 1] };
|
|
||||||
let report = serde_json::json!({"schema":1,"host_ticks":self.ticks_total,
|
|
||||||
"first_presentations":self.first_presentations,"repeated_presentations":self.repeated_presentations,
|
|
||||||
"present_api_calls":self.present_count,"samples":n,
|
|
||||||
"present_api_mean_us":if n==0 {0.0} else {self.present_us.iter().sum::<u64>() as f64/n as f64},
|
|
||||||
"present_api_p95_us":percentile(95),"present_api_p99_us":percentile(99),
|
|
||||||
"present_api_max_us":self.present_us.last().copied().unwrap_or(0),
|
|
||||||
"present_api_over_budget":self.present_us.iter().filter(|&&v|v > 1_000_000 / u64::from(prometeu_hal::graphics_contract::REFRESH_HZ)).count(),
|
|
||||||
"physical_display_hz":null,"limitation":"bounded last 16384 API samples; API upload/submit/present time is not scanout latency"});
|
|
||||||
if let Err(error) = std::fs::write(path, report.to_string()) {
|
|
||||||
eprintln!("graphics metrics: {error}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
use prometeu_hal::{RenderOwnership, app_mode::AppMode};
|
|
||||||
#[test]
|
|
||||||
fn host_counts_actual_submissions_by_identity_and_epoch() {
|
|
||||||
let mut stats = HostStats::new();
|
|
||||||
let first = RenderOwnership::new(1, AppMode::Game, 2);
|
|
||||||
stats.record_presented(first, 0);
|
|
||||||
stats.record_presented(first, 0);
|
|
||||||
stats.record_presented(RenderOwnership::new(2, AppMode::Game, 2), 0);
|
|
||||||
assert_eq!(stats.first_presentations, 2);
|
|
||||||
assert_eq!(stats.repeated_presentations, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,262 +0,0 @@
|
|||||||
//! 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 / u64::from(prometeu_hal::graphics_contract::REFRESH_HZ),
|
|
||||||
);
|
|
||||||
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,
|
|
||||||
prometeu_hal::graphics_contract::REFRESH_HZ,
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
//! 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,260 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,227 +0,0 @@
|
|||||||
//! 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(640, 360, 641).is_err());
|
|
||||||
assert_eq!(frame_bytes(640, 360, 640).unwrap(), 921600);
|
|
||||||
}
|
|
||||||
#[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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
//! Physical surface geometry shared by the nearest blit and pointer normalization.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub(crate) struct Viewport {
|
|
||||||
pub x: f32,
|
|
||||||
pub y: f32,
|
|
||||||
pub width: f32,
|
|
||||||
pub height: f32,
|
|
||||||
pub scale: f32,
|
|
||||||
native: (usize, usize),
|
|
||||||
}
|
|
||||||
impl Viewport {
|
|
||||||
pub fn new(surface: (u32, u32), native: (usize, usize)) -> Option<Self> {
|
|
||||||
if surface.0 == 0 || surface.1 == 0 || native.0 == 0 || native.1 == 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let fit = (surface.0 as f32 / native.0 as f32).min(surface.1 as f32 / native.1 as f32);
|
|
||||||
let scale = if fit >= 1.0 { fit.floor() } else { fit };
|
|
||||||
let width = native.0 as f32 * scale;
|
|
||||||
let height = native.1 as f32 * scale;
|
|
||||||
Some(Self {
|
|
||||||
x: ((surface.0 as f32 - width) / 2.0).floor(),
|
|
||||||
y: ((surface.1 as f32 - height) / 2.0).floor(),
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
scale,
|
|
||||||
native,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
pub fn inverse(self, x: f64, y: f64) -> Option<(i32, i32)> {
|
|
||||||
let (x, y) = (x - self.x as f64, y - self.y as f64);
|
|
||||||
if !x.is_finite()
|
|
||||||
|| !y.is_finite()
|
|
||||||
|| x < 0.0
|
|
||||||
|| y < 0.0
|
|
||||||
|| x >= self.width as f64
|
|
||||||
|| y >= self.height as f64
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
Some((
|
|
||||||
((x / self.scale as f64).floor() as i32).min(self.native.0 as i32 - 1),
|
|
||||||
((y / self.scale as f64).floor() as i32).min(self.native.1 as i32 - 1),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
#[test]
|
|
||||||
fn physical_scaling_and_inverse_share_bounds() {
|
|
||||||
for (surface, scale, offset) in [
|
|
||||||
((1280, 720), 2.0, (0.0, 0.0)),
|
|
||||||
((1920, 1080), 3.0, (0.0, 0.0)),
|
|
||||||
((1000, 700), 1.0, (180.0, 170.0)),
|
|
||||||
((320, 240), 0.5, (0.0, 30.0)),
|
|
||||||
((2560, 1440), 4.0, (0.0, 0.0)),
|
|
||||||
] {
|
|
||||||
let v = Viewport::new(surface, (640, 360)).unwrap();
|
|
||||||
assert_eq!((v.scale, v.x, v.y), (scale, offset.0, offset.1));
|
|
||||||
assert_eq!(v.inverse(v.x as f64, v.y as f64), Some((0, 0)));
|
|
||||||
assert_eq!(
|
|
||||||
v.inverse((v.x + v.width - 0.01) as f64, (v.y + v.height - 0.01) as f64),
|
|
||||||
Some((639, 359))
|
|
||||||
);
|
|
||||||
assert_eq!(v.inverse((v.x + v.width) as f64, v.y as f64), None);
|
|
||||||
assert_eq!(v.inverse(v.x as f64, (v.y + v.height) as f64), None);
|
|
||||||
assert_eq!(v.inverse(v.x as f64 - 0.01, v.y as f64), None);
|
|
||||||
}
|
|
||||||
assert!(Viewport::new((0, 0), (640, 360)).is_none());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,215 +0,0 @@
|
|||||||
//! 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()), 640);
|
|
||||||
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 + 640 * 360 * 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());
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
//! Unpaced logical-frame and capture timing through the real headless host.
|
|
||||||
use prometeu_host_headless::{AdvanceFrameOutcome, HeadlessHost};
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
fn stats(mut times: Vec<u64>) -> serde_json::Value {
|
|
||||||
times.sort_unstable();
|
|
||||||
let n = times.len();
|
|
||||||
serde_json::json!({"mean_us":times.iter().sum::<u64>() as f64/n as f64/1000.0,
|
|
||||||
"p95_us":times[(n*95).div_ceil(100)-1] as f64/1000.0,
|
|
||||||
"p99_us":times[(n*99).div_ceil(100)-1] as f64/1000.0,
|
|
||||||
"max_us":times[n-1] as f64/1000.0,
|
|
||||||
"over_60hz_budget":times.iter().filter(|&&t|t>1_000_000_000/60).count()})
|
|
||||||
}
|
|
||||||
fn main() {
|
|
||||||
let mut args = std::env::args().skip(1);
|
|
||||||
let cartridge = args.next().expect("cartridge directory");
|
|
||||||
let count: usize = args.next().map(|s| s.parse().expect("frame count")).unwrap_or(600);
|
|
||||||
assert!(count > 0);
|
|
||||||
let mut host = HeadlessHost::new();
|
|
||||||
host.boot(cartridge).expect("boot");
|
|
||||||
let mut advance = Vec::with_capacity(count);
|
|
||||||
let mut capture = Vec::with_capacity(count);
|
|
||||||
let mut previous = None;
|
|
||||||
let mut published = 0;
|
|
||||||
let mut checksum = 0;
|
|
||||||
for i in 0..count + 120 {
|
|
||||||
let start = Instant::now();
|
|
||||||
assert!(matches!(
|
|
||||||
host.advance_frame().expect("advance"),
|
|
||||||
AdvanceFrameOutcome::FrameCompleted { .. }
|
|
||||||
));
|
|
||||||
let elapsed = start.elapsed().as_nanos() as u64;
|
|
||||||
let start = Instant::now();
|
|
||||||
let frame = host.capture_frame().expect("published frame").frame;
|
|
||||||
let capture_ns = start.elapsed().as_nanos() as u64;
|
|
||||||
assert_eq!((frame.width, frame.height, frame.stride_pixels), (640, 360, 640));
|
|
||||||
assert_ne!(previous, Some((frame.ownership, frame.frame_id)));
|
|
||||||
previous = Some((frame.ownership, frame.frame_id));
|
|
||||||
checksum ^= frame.pixels[(i * 173) % frame.pixels.len()];
|
|
||||||
if i >= 120 {
|
|
||||||
advance.push(elapsed);
|
|
||||||
capture.push(capture_ns);
|
|
||||||
published += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let state = host.get_state();
|
|
||||||
println!(
|
|
||||||
"{}",
|
|
||||||
serde_json::json!({"schema":1,"width":640,"height":360,"frames":count,"warmup_frames":120,
|
|
||||||
"completed_logical_frames_total":state.frame_index,"host_ticks_total":state.tick_index,
|
|
||||||
"published_frames_measured":published,"repeated_publications_measured":0,
|
|
||||||
"advance_including_vm_render_publish":stats(advance),"owned_capture":stats(capture),"checksum":checksum,
|
|
||||||
"vm_hz":null,"physical_display_hz":null,
|
|
||||||
"limitations":"unpaced real VM/headless; capture copies owned pixels; no GPU, realtime cadence or physical display"})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
mod wire;
|
mod wire;
|
||||||
pub use wire::Limits;
|
pub use wire::Limits;
|
||||||
|
|
||||||
use crate::{AdvanceFrameOutcome, HeadlessHost, PendingWrites, TeardownPolicy};
|
use crate::{AdvanceFrameOutcome, HeadlessHost};
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
use std::io::{self, BufRead, Write};
|
use std::io::{self, BufRead, Write};
|
||||||
use std::sync::{Arc, Mutex, mpsc};
|
use std::sync::{Arc, Mutex, mpsc};
|
||||||
@@ -16,8 +16,7 @@ struct Control {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum Job {
|
enum Job {
|
||||||
Work(String, Command, bool),
|
Work(String, Command),
|
||||||
Shutdown(String, TeardownPolicy, bool, mpsc::Sender<bool>),
|
|
||||||
Stop(Option<String>),
|
Stop(Option<String>),
|
||||||
ReadError(io::Error),
|
ReadError(io::Error),
|
||||||
}
|
}
|
||||||
@@ -105,8 +104,6 @@ fn read_commands<R: BufRead, W: Write>(
|
|||||||
limits: &Limits,
|
limits: &Limits,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
let mut initialized = false;
|
let mut initialized = false;
|
||||||
let mut memcard = false;
|
|
||||||
let mut log_read = false;
|
|
||||||
loop {
|
loop {
|
||||||
let bytes = match read_line(&mut input, limits.max_command_bytes)? {
|
let bytes = match read_line(&mut input, limits.max_command_bytes)? {
|
||||||
Line::Eof => break,
|
Line::Eof => break,
|
||||||
@@ -145,36 +142,11 @@ fn read_commands<R: BufRead, W: Write>(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if matches!(command, Command::GetLogs(_)) && !log_read {
|
if let Command::Shutdown = command {
|
||||||
output.reply(
|
let mut c = control.lock().unwrap();
|
||||||
Some(&id),
|
c.canceled = true;
|
||||||
Err(WireError::new(
|
sender.send(Job::Stop(Some(id))).map_err(|_| io::Error::other("execution stopped"))?;
|
||||||
"unsupported_capability",
|
return Ok(());
|
||||||
"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 {
|
if let Command::Initialize(params) = command {
|
||||||
let result = if initialized {
|
let result = if initialized {
|
||||||
@@ -187,23 +159,9 @@ fn read_commands<R: BufRead, W: Write>(
|
|||||||
.any(|c| !CAPABILITIES.contains(&c.as_str()))
|
.any(|c| !CAPABILITIES.contains(&c.as_str()))
|
||||||
{
|
{
|
||||||
Err(WireError::new("unsupported_capability", "required capability unavailable"))
|
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 {
|
} else {
|
||||||
initialized = true;
|
initialized = true;
|
||||||
memcard = params.protocol_version.minor >= 1
|
Ok(json!({"protocolVersion":{"major":1,"minor":0},"capabilities":CAPABILITIES}))
|
||||||
&& 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)?;
|
output.reply(Some(&id), result)?;
|
||||||
continue;
|
continue;
|
||||||
@@ -239,9 +197,7 @@ fn read_commands<R: BufRead, W: Write>(
|
|||||||
c.active = Some(id.clone());
|
c.active = Some(id.clone());
|
||||||
c.advancing = matches!(command, Command::Advance(_));
|
c.advancing = matches!(command, Command::Advance(_));
|
||||||
c.canceled = false;
|
c.canceled = false;
|
||||||
sender
|
sender.send(Job::Work(id, command)).map_err(|_| io::Error::other("execution stopped"))?;
|
||||||
.send(Job::Work(id, command, memcard))
|
|
||||||
.map_err(|_| io::Error::other("execution stopped"))?;
|
|
||||||
}
|
}
|
||||||
let mut c = control.lock().unwrap();
|
let mut c = control.lock().unwrap();
|
||||||
c.canceled = true;
|
c.canceled = true;
|
||||||
@@ -254,39 +210,14 @@ fn execute<W: Write>(
|
|||||||
id: &str,
|
id: &str,
|
||||||
output: &Output<W>,
|
output: &Output<W>,
|
||||||
control: &Mutex<Control>,
|
control: &Mutex<Control>,
|
||||||
memcard: bool,
|
|
||||||
) -> io::Result<Result<Value, WireError>> {
|
) -> io::Result<Result<Value, WireError>> {
|
||||||
Ok(match command {
|
Ok(match command {
|
||||||
Command::Boot(p) => {
|
Command::Boot(p) => {
|
||||||
let result = if let Some(choice) = p.memcard_choice {
|
host.boot(p.path).map(|()| wire::state(host.get_state())).map_err(wire::host_error)
|
||||||
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(params) => host
|
Command::Reset => {
|
||||||
.reset_with_policy(params.policy())
|
host.reset();
|
||||||
.map(|snapshot| {
|
Ok(wire::state(host.get_state()))
|
||||||
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) => {
|
Command::SetInput(p) => {
|
||||||
host.set_input(p.into());
|
host.set_input(p.into());
|
||||||
@@ -298,7 +229,6 @@ fn execute<W: Write>(
|
|||||||
}
|
}
|
||||||
Command::GetState => Ok(wire::state(host.get_state())),
|
Command::GetState => Ok(wire::state(host.get_state())),
|
||||||
Command::GetFault => Ok(host.get_fault().as_ref().map(wire::fault).unwrap_or(Value::Null)),
|
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::Capture => host.capture_frame().map_err(wire::host_error).and_then(wire::capture),
|
||||||
Command::Advance(p) => {
|
Command::Advance(p) => {
|
||||||
let mut result = json!({"requested":p.frames,"completed":0,"stop":"completed"});
|
let mut result = json!({"requested":p.frames,"completed":0,"stop":"completed"});
|
||||||
@@ -324,10 +254,6 @@ fn execute<W: Write>(
|
|||||||
result["stop"] = json!("terminated");
|
result["stop"] = json!("terminated");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Ok(AdvanceFrameOutcome::ActionRequired) => {
|
|
||||||
result["stop"] = json!("action_required");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
result["stop"] = json!("host_error");
|
result["stop"] = json!("host_error");
|
||||||
result["error"] = serde_json::to_value(wire::host_error(error))?;
|
result["error"] = serde_json::to_value(wire::host_error(error))?;
|
||||||
@@ -357,7 +283,7 @@ pub fn serve<R: BufRead + Send + 'static, W: Write + Send + 'static>(
|
|||||||
let output =
|
let output =
|
||||||
Arc::new(Output { writer: Mutex::new(writer), session, limit: limits.max_response_bytes });
|
Arc::new(Output { writer: Mutex::new(writer), session, limit: limits.max_response_bytes });
|
||||||
output.send(
|
output.send(
|
||||||
json!({"type":"hello","sessionId":output.session,"protocolVersion":{"major":1,"minor":2},
|
json!({"type":"hello","sessionId":output.session,"protocolVersion":{"major":1,"minor":0},
|
||||||
"runtimeVersion":runtime_version,"capabilities":CAPABILITIES,"limits":limits}),
|
"runtimeVersion":runtime_version,"capabilities":CAPABILITIES,"limits":limits}),
|
||||||
)?;
|
)?;
|
||||||
let control = Arc::new(Mutex::new(Control::default()));
|
let control = Arc::new(Mutex::new(Control::default()));
|
||||||
@@ -372,58 +298,18 @@ pub fn serve<R: BufRead + Send + 'static, W: Write + Send + 'static>(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
let mut host = HeadlessHost::new();
|
let mut host = HeadlessHost::new();
|
||||||
loop {
|
while let Ok(job) = receiver.recv() {
|
||||||
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 {
|
match job {
|
||||||
Job::Work(id, command, memcard) => {
|
Job::Work(id, command) => {
|
||||||
let result = match execute(&mut host, command, &id, &output, &control, memcard) {
|
let result = execute(&mut host, command, &id, &output, &control)?;
|
||||||
Ok(result) => result,
|
|
||||||
Err(error) => {
|
|
||||||
cleanup_after_loss(&mut host);
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Release authority under the same lock as response publication:
|
// Release authority under the same lock as response publication:
|
||||||
// a client reacting to this reply cannot race a stale busy flag.
|
// a client reacting to this reply cannot race a stale busy flag.
|
||||||
let mut c = control.lock().unwrap();
|
let mut c = control.lock().unwrap();
|
||||||
if let Err(error) = output.reply(Some(&id), result) {
|
output.reply(Some(&id), result)?;
|
||||||
drop(c);
|
|
||||||
cleanup_after_loss(&mut host);
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
c.active = None;
|
c.active = None;
|
||||||
c.advancing = false;
|
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) => {
|
Job::Stop(id) => {
|
||||||
let _ = host.shutdown(TeardownPolicy {
|
|
||||||
discard_unsaved: true,
|
|
||||||
pending_writes: PendingWrites::Cancel,
|
|
||||||
});
|
|
||||||
drop(host);
|
drop(host);
|
||||||
if let Some(id) = id {
|
if let Some(id) = id {
|
||||||
output.reply(Some(&id), Ok(json!({"closed":true})))?;
|
output.reply(Some(&id), Ok(json!({"closed":true})))?;
|
||||||
@@ -431,24 +317,13 @@ pub fn serve<R: BufRead + Send + 'static, W: Write + Send + 'static>(
|
|||||||
reader.join().map_err(|_| io::Error::other("input reader panicked"))?;
|
reader.join().map_err(|_| io::Error::other("input reader panicked"))?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
Job::ReadError(error) => {
|
Job::ReadError(error) => return Err(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"))?;
|
reader.join().map_err(|_| io::Error::other("input reader panicked"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanup_after_loss(host: &mut HeadlessHost) {
|
|
||||||
let _ = host
|
|
||||||
.shutdown(TeardownPolicy { discard_unsaved: true, pending_writes: PendingWrites::Cancel });
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -494,7 +369,7 @@ mod tests {
|
|||||||
"../../../../../devtools/headless-protocol/requests.json"
|
"../../../../../devtools/headless-protocol/requests.json"
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(!requests.is_empty());
|
assert_eq!(requests.len(), 11);
|
||||||
for request in requests {
|
for request in requests {
|
||||||
wire::parse(serde_json::from_value(request).unwrap()).unwrap();
|
wire::parse(serde_json::from_value(request).unwrap()).unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,7 @@ use std::io;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod fault_tests;
|
mod fault_tests;
|
||||||
|
|
||||||
use crate::{
|
use crate::{CapturedFrame, HostError, HostExecutionStatus, HostState};
|
||||||
CapturedFrame, HostError, HostExecutionStatus, HostState, MediaAccess, MediaAction,
|
|
||||||
MemcardChoice, MemcardMount, PendingWrites, TeardownPolicy,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub const CAPABILITIES: &[&str] = &[
|
pub const CAPABILITIES: &[&str] = &[
|
||||||
"boot",
|
"boot",
|
||||||
@@ -22,15 +19,8 @@ pub const CAPABILITIES: &[&str] = &[
|
|||||||
"capture",
|
"capture",
|
||||||
"get_state",
|
"get_state",
|
||||||
"get_fault",
|
"get_fault",
|
||||||
"log_read",
|
|
||||||
"cancel",
|
"cancel",
|
||||||
"shutdown",
|
"shutdown",
|
||||||
"memcard",
|
|
||||||
"memcard_mount",
|
|
||||||
"memcard_unmount",
|
|
||||||
"get_memcard_state",
|
|
||||||
"memcard_reconcile",
|
|
||||||
"memcard_continue_in_memory",
|
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize)]
|
#[derive(Clone, Debug, Serialize)]
|
||||||
@@ -105,33 +95,6 @@ pub(super) struct Initialize {
|
|||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub(super) struct Boot {
|
pub(super) struct Boot {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
#[serde(default, rename = "memcardChoice", deserialize_with = "present")]
|
|
||||||
pub memcard_choice: Option<MemcardChoice>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn present<'de, D: serde::Deserializer<'de>, T: Deserialize<'de>>(
|
|
||||||
d: D,
|
|
||||||
) -> Result<Option<T>, D::Error> {
|
|
||||||
T::deserialize(d).map(Some)
|
|
||||||
}
|
|
||||||
#[derive(Debug, Default, Deserialize)]
|
|
||||||
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
|
|
||||||
pub(super) struct Teardown {
|
|
||||||
#[serde(deserialize_with = "present")]
|
|
||||||
pub discard_unsaved: Option<bool>,
|
|
||||||
#[serde(deserialize_with = "present")]
|
|
||||||
pub pending_writes: Option<PendingWrites>,
|
|
||||||
}
|
|
||||||
impl Teardown {
|
|
||||||
pub fn explicit(&self) -> bool {
|
|
||||||
self.discard_unsaved.is_some() || self.pending_writes.is_some()
|
|
||||||
}
|
|
||||||
pub fn policy(&self) -> TeardownPolicy {
|
|
||||||
TeardownPolicy {
|
|
||||||
discard_unsaved: self.discard_unsaved.unwrap_or(false),
|
|
||||||
pending_writes: self.pending_writes.unwrap_or_default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -140,19 +103,6 @@ pub(super) struct Advance {
|
|||||||
pub frames: u64,
|
pub frames: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
||||||
pub(super) struct GetLogs {
|
|
||||||
// Explicit deserializer makes the nullable field required, including on first read.
|
|
||||||
#[serde(deserialize_with = "nullable_cursor")]
|
|
||||||
pub after_sequence: Option<u64>,
|
|
||||||
pub limit: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn nullable_cursor<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<u64>, D::Error> {
|
|
||||||
Option::<u64>::deserialize(d)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||||
pub(super) struct Cancel {
|
pub(super) struct Cancel {
|
||||||
@@ -205,35 +155,15 @@ impl From<Input> for InputSignals {
|
|||||||
pub(super) enum Command {
|
pub(super) enum Command {
|
||||||
Initialize(Initialize),
|
Initialize(Initialize),
|
||||||
Boot(Boot),
|
Boot(Boot),
|
||||||
Reset(Teardown),
|
Reset,
|
||||||
MemcardMount(MemcardMount),
|
|
||||||
MemcardUnmount,
|
|
||||||
GetMemcardState,
|
|
||||||
MemcardReconcile,
|
|
||||||
MemcardContinueInMemory,
|
|
||||||
SetInput(Input),
|
SetInput(Input),
|
||||||
ClearInput,
|
ClearInput,
|
||||||
Advance(Advance),
|
Advance(Advance),
|
||||||
Capture,
|
Capture,
|
||||||
GetState,
|
GetState,
|
||||||
GetFault,
|
GetFault,
|
||||||
GetLogs(GetLogs),
|
|
||||||
Cancel(Cancel),
|
Cancel(Cancel),
|
||||||
Shutdown(Teardown),
|
Shutdown,
|
||||||
}
|
|
||||||
impl Command {
|
|
||||||
pub fn needs_memcard(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
Self::MemcardMount(_)
|
|
||||||
| Self::MemcardUnmount
|
|
||||||
| Self::GetMemcardState
|
|
||||||
| Self::MemcardReconcile
|
|
||||||
| Self::MemcardContinueInMemory => true,
|
|
||||||
Self::Boot(p) => p.memcard_choice.is_some(),
|
|
||||||
Self::Reset(p) | Self::Shutdown(p) => p.explicit(),
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn params<T: serde::de::DeserializeOwned>(value: Value) -> Result<T, WireError> {
|
fn params<T: serde::de::DeserializeOwned>(value: Value) -> Result<T, WireError> {
|
||||||
@@ -262,24 +192,6 @@ pub(super) fn parse(req: Request) -> Result<(String, Command), WireError> {
|
|||||||
Command::Boot(p)
|
Command::Boot(p)
|
||||||
}
|
}
|
||||||
"advance" => Command::Advance(params(req.params)?),
|
"advance" => Command::Advance(params(req.params)?),
|
||||||
"get_logs" => {
|
|
||||||
let p: GetLogs = params(req.params)?;
|
|
||||||
if !(1..=1024).contains(&p.limit) {
|
|
||||||
return Err(WireError::new("invalid_params", "limit must be 1..1024"));
|
|
||||||
}
|
|
||||||
Command::GetLogs(p)
|
|
||||||
}
|
|
||||||
"reset" => Command::Reset(params(req.params)?),
|
|
||||||
"shutdown" => Command::Shutdown(params(req.params)?),
|
|
||||||
"memcard_mount" => {
|
|
||||||
let p: MemcardMount = params(req.params)?;
|
|
||||||
if p.path.trim().is_empty()
|
|
||||||
|| matches!((p.action, p.access), (MediaAction::Create, MediaAccess::ReadOnly))
|
|
||||||
{
|
|
||||||
return Err(WireError::new("invalid_params", "invalid media path/action/access"));
|
|
||||||
}
|
|
||||||
Command::MemcardMount(p)
|
|
||||||
}
|
|
||||||
"set_input" => Command::SetInput(params(req.params)?),
|
"set_input" => Command::SetInput(params(req.params)?),
|
||||||
"cancel" => {
|
"cancel" => {
|
||||||
let p: Cancel = params(req.params)?;
|
let p: Cancel = params(req.params)?;
|
||||||
@@ -288,25 +200,15 @@ pub(super) fn parse(req: Request) -> Result<(String, Command), WireError> {
|
|||||||
}
|
}
|
||||||
Command::Cancel(p)
|
Command::Cancel(p)
|
||||||
}
|
}
|
||||||
"clear_input"
|
"reset" | "clear_input" | "capture" | "get_state" | "get_fault" | "shutdown" => {
|
||||||
| "capture"
|
|
||||||
| "get_state"
|
|
||||||
| "get_fault"
|
|
||||||
| "memcard_unmount"
|
|
||||||
| "get_memcard_state"
|
|
||||||
| "memcard_reconcile"
|
|
||||||
| "memcard_continue_in_memory" => {
|
|
||||||
empty(&req.params)?;
|
empty(&req.params)?;
|
||||||
match req.command.as_str() {
|
match req.command.as_str() {
|
||||||
"memcard_unmount" => Command::MemcardUnmount,
|
"reset" => Command::Reset,
|
||||||
"get_memcard_state" => Command::GetMemcardState,
|
|
||||||
"memcard_reconcile" => Command::MemcardReconcile,
|
|
||||||
"memcard_continue_in_memory" => Command::MemcardContinueInMemory,
|
|
||||||
"clear_input" => Command::ClearInput,
|
"clear_input" => Command::ClearInput,
|
||||||
"capture" => Command::Capture,
|
"capture" => Command::Capture,
|
||||||
"get_state" => Command::GetState,
|
"get_state" => Command::GetState,
|
||||||
"get_fault" => Command::GetFault,
|
"get_fault" => Command::GetFault,
|
||||||
_ => unreachable!(),
|
_ => Command::Shutdown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => return Err(WireError::new("unknown_command", "unknown command")),
|
_ => return Err(WireError::new("unknown_command", "unknown command")),
|
||||||
@@ -329,9 +231,6 @@ impl WireError {
|
|||||||
|
|
||||||
pub(super) fn host_error(error: HostError) -> WireError {
|
pub(super) fn host_error(error: HostError) -> WireError {
|
||||||
let (code, details) = match &error {
|
let (code, details) = match &error {
|
||||||
HostError::Memcard(e) => {
|
|
||||||
("memcard_error", json!({"operation":e.operation,"reason":e.reason,"state":e.state}))
|
|
||||||
}
|
|
||||||
HostError::AlreadyBooted => ("already_booted", json!({})),
|
HostError::AlreadyBooted => ("already_booted", json!({})),
|
||||||
HostError::BootFailed { reason, report } => {
|
HostError::BootFailed { reason, report } => {
|
||||||
("boot_failed", json!({"reason":reason,"fault":report.as_deref().map(fault)}))
|
("boot_failed", json!({"reason":reason,"fault":report.as_deref().map(fault)}))
|
||||||
@@ -379,48 +278,6 @@ pub(super) fn state(s: HostState) -> Value {
|
|||||||
"status":status_name(s.status)})
|
"status":status_name(s.status)})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn logs(
|
|
||||||
host: &crate::HeadlessHost,
|
|
||||||
params: GetLogs,
|
|
||||||
session: &str,
|
|
||||||
) -> Result<Value, WireError> {
|
|
||||||
let (events, oldest, newest) =
|
|
||||||
host.log_snapshot(params.after_sequence, usize::from(params.limit));
|
|
||||||
if params.after_sequence.is_some_and(|after| newest.is_none_or(|newest| after > newest)) {
|
|
||||||
return Err(WireError::new("invalid_cursor", "afterSequence exceeds the log stream"));
|
|
||||||
}
|
|
||||||
let gap = oldest.is_some_and(|oldest| match params.after_sequence {
|
|
||||||
None => oldest > 0,
|
|
||||||
Some(after) => after.checked_add(1).is_some_and(|next| next < oldest),
|
|
||||||
});
|
|
||||||
let next = events.last().map(|event| event.seq).or(params.after_sequence);
|
|
||||||
Ok(
|
|
||||||
json!({"sessionId":session,"logEpoch":0,"events":events.iter().map(log_event).collect::<Vec<_>>(),
|
|
||||||
"oldestSequence":oldest,"newestSequence":newest,"nextAfterSequence":next,
|
|
||||||
"gap":gap,"overflow":oldest.is_some_and(|oldest| oldest > 0)}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn log_event(event: &prometeu_hal::log::LogEvent) -> Value {
|
|
||||||
use prometeu_hal::log::{LogLevel, LogSource};
|
|
||||||
let level = match event.level {
|
|
||||||
LogLevel::Trace => "trace",
|
|
||||||
LogLevel::Debug => "debug",
|
|
||||||
LogLevel::Info => "info",
|
|
||||||
LogLevel::Warn => "warn",
|
|
||||||
LogLevel::Error => "error",
|
|
||||||
};
|
|
||||||
let (source, app_id) = match event.source {
|
|
||||||
LogSource::Pos => ("pos", None),
|
|
||||||
LogSource::Hub => ("hub", None),
|
|
||||||
LogSource::Vm => ("vm", None),
|
|
||||||
LogSource::Fs => ("fs", None),
|
|
||||||
LogSource::App { app_id } => ("app", Some(app_id)),
|
|
||||||
};
|
|
||||||
json!({"sequence":event.seq,"frame":event.frame,"timestampMs":event.ts_ms,
|
|
||||||
"level":level,"source":source,"appId":app_id,"tag":event.tag,"message":event.msg})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn fault(report: &CrashReport) -> Value {
|
pub(super) fn fault(report: &CrashReport) -> Value {
|
||||||
let mut result = json!({"kind":report.kind(),"pc":report.pc(),"summary":report.summary()});
|
let mut result = json!({"kind":report.kind(),"pc":report.pc(),"summary":report.summary()});
|
||||||
match report {
|
match report {
|
||||||
@@ -466,105 +323,3 @@ pub(super) fn capture(c: CapturedFrame) -> Result<Value, WireError> {
|
|||||||
"width":width,"height":height,"stridePixels":frame.stride_pixels,"pixelFormat":"RGBA8888",
|
"width":width,"height":height,"stridePixels":frame.stride_pixels,"pixelFormat":"RGBA8888",
|
||||||
"logicalFrameIndex":c.logical_frame_index,"frameId":frame.frame_id.get()}))
|
"logicalFrameIndex":c.logical_frame_index,"frameId":frame.frame_id.get()}))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod log_tests {
|
|
||||||
use super::*;
|
|
||||||
use prometeu_hal::log::{LogEvent, LogLevel, LogSource};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn oversized_native_event_is_an_error_without_partial_result() {
|
|
||||||
let event = LogEvent {
|
|
||||||
seq: 0,
|
|
||||||
frame: 0,
|
|
||||||
ts_ms: 0,
|
|
||||||
level: LogLevel::Error,
|
|
||||||
source: LogSource::Vm,
|
|
||||||
tag: 0,
|
|
||||||
msg: "x".repeat(8192),
|
|
||||||
};
|
|
||||||
let output = super::super::Output {
|
|
||||||
writer: std::sync::Mutex::new(Vec::new()),
|
|
||||||
session: "session".into(),
|
|
||||||
limit: 4096,
|
|
||||||
};
|
|
||||||
output.reply(Some("logs"), Ok(json!({"events":[log_event(&event)]}))).unwrap();
|
|
||||||
let bytes = output.writer.into_inner().unwrap();
|
|
||||||
assert!(bytes.len() <= 4097);
|
|
||||||
let response: Value = serde_json::from_slice(&bytes).unwrap();
|
|
||||||
assert_eq!(response["error"]["code"], "response_too_large");
|
|
||||||
assert_eq!(response["requestId"], "logs");
|
|
||||||
assert!(response.get("result").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn native_records_preserve_unsigned_maxima_and_typed_sources() {
|
|
||||||
for (source, name, app_id) in [
|
|
||||||
(LogSource::Pos, "pos", None),
|
|
||||||
(LogSource::Hub, "hub", None),
|
|
||||||
(LogSource::Vm, "vm", None),
|
|
||||||
(LogSource::Fs, "fs", None),
|
|
||||||
(LogSource::App { app_id: u32::MAX }, "app", Some(u32::MAX)),
|
|
||||||
] {
|
|
||||||
for (level, level_name) in [
|
|
||||||
(LogLevel::Trace, "trace"),
|
|
||||||
(LogLevel::Debug, "debug"),
|
|
||||||
(LogLevel::Info, "info"),
|
|
||||||
(LogLevel::Warn, "warn"),
|
|
||||||
(LogLevel::Error, "error"),
|
|
||||||
] {
|
|
||||||
let event = LogEvent {
|
|
||||||
seq: u64::MAX,
|
|
||||||
frame: u64::MAX,
|
|
||||||
ts_ms: u64::MAX,
|
|
||||||
level,
|
|
||||||
source,
|
|
||||||
tag: u16::MAX,
|
|
||||||
msg: "complete native message".into(),
|
|
||||||
};
|
|
||||||
let encoded = log_event(&event);
|
|
||||||
let decoded: Value =
|
|
||||||
serde_json::from_slice(&serde_json::to_vec(&encoded).unwrap()).unwrap();
|
|
||||||
for key in ["sequence", "frame", "timestampMs"] {
|
|
||||||
assert_eq!(decoded[key].as_u64(), Some(u64::MAX));
|
|
||||||
}
|
|
||||||
assert_eq!(decoded["level"], level_name);
|
|
||||||
assert_eq!(decoded["source"], name);
|
|
||||||
assert_eq!(decoded["appId"], json!(app_id));
|
|
||||||
assert_eq!(decoded["tag"], u16::MAX);
|
|
||||||
assert_eq!(decoded["message"], event.msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn required_nullable_cursor_and_limit_reject_malformed_values() {
|
|
||||||
for value in [
|
|
||||||
json!({"limit":1}),
|
|
||||||
json!({"afterSequence":null}),
|
|
||||||
json!({"afterSequence":-1,"limit":1}),
|
|
||||||
json!({"afterSequence":1.5,"limit":1}),
|
|
||||||
json!({"afterSequence":"0","limit":1}),
|
|
||||||
json!({"afterSequence":null,"limit":0}),
|
|
||||||
json!({"afterSequence":null,"limit":1025}),
|
|
||||||
json!({"afterSequence":null,"limit":1,"filter":"app"}),
|
|
||||||
] {
|
|
||||||
assert!(
|
|
||||||
parse(Request {
|
|
||||||
request_id: "logs".into(),
|
|
||||||
command: "get_logs".into(),
|
|
||||||
params: value
|
|
||||||
})
|
|
||||||
.is_err()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
assert!(
|
|
||||||
parse(Request {
|
|
||||||
request_id: "logs".into(),
|
|
||||||
command: "get_logs".into(),
|
|
||||||
params: json!({"afterSequence":u64::MAX,"limit":1024})
|
|
||||||
})
|
|
||||||
.is_ok()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
use prometeu_drivers::hardware::Hardware;
|
use prometeu_drivers::hardware::Hardware;
|
||||||
use prometeu_drivers::{ControlledWallClock, MemoryBanks};
|
use prometeu_drivers::{ControlledWallClock, MemoryBanks};
|
||||||
@@ -12,30 +11,9 @@ use prometeu_hal::{
|
|||||||
RuntimePlatform, TelemetryPlatform, WallClock,
|
RuntimePlatform, TelemetryPlatform, WallClock,
|
||||||
};
|
};
|
||||||
use prometeu_system::{CrashReport, LatestRenderFrameStore};
|
use prometeu_system::{CrashReport, LatestRenderFrameStore};
|
||||||
#[path = "memcard.rs"]
|
|
||||||
mod memcard;
|
|
||||||
pub use memcard::{
|
|
||||||
MediaAccess, MediaAction, MemcardChoice, MemcardFailure, MemcardMount, MemcardState,
|
|
||||||
PendingWrites, TeardownPolicy,
|
|
||||||
};
|
|
||||||
|
|
||||||
const BOOT_TICK_LIMIT: usize = 5000;
|
const BOOT_TICK_LIMIT: usize = 5000;
|
||||||
const HOST_FRAME_ADVANCE_TICK_LIMIT: usize = 100_000;
|
const HOST_FRAME_ADVANCE_TICK_LIMIT: usize = 100_000;
|
||||||
const BOOTSTRAP_IO_WAIT_QUANTUM: Duration = Duration::from_millis(5);
|
|
||||||
const BOOTSTRAP_IO_WAIT_LIMIT: Duration = Duration::from_secs(2);
|
|
||||||
|
|
||||||
/// One deadline for the entire advance, including repeated admission to the gate.
|
|
||||||
#[derive(Default)]
|
|
||||||
struct BootstrapIoWait {
|
|
||||||
deadline: Option<Instant>,
|
|
||||||
}
|
|
||||||
impl BootstrapIoWait {
|
|
||||||
fn next_wait(&mut self, now: Instant) -> Option<Duration> {
|
|
||||||
let deadline = *self.deadline.get_or_insert(now + BOOTSTRAP_IO_WAIT_LIMIT);
|
|
||||||
let remaining = deadline.saturating_duration_since(now);
|
|
||||||
(!remaining.is_zero()).then_some(remaining.min(BOOTSTRAP_IO_WAIT_QUANTUM))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// One published logical frame plus the automation counter.
|
/// One published logical frame plus the automation counter.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@@ -68,7 +46,6 @@ pub enum AdvanceFrameOutcome {
|
|||||||
FrameCompleted { logical_frame_index: u64, frame_id: FrameId },
|
FrameCompleted { logical_frame_index: u64, frame_id: FrameId },
|
||||||
Faulted(CrashReport),
|
Faulted(CrashReport),
|
||||||
Terminated,
|
Terminated,
|
||||||
ActionRequired,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@@ -76,7 +53,6 @@ pub enum AdvanceFramesStop {
|
|||||||
Completed,
|
Completed,
|
||||||
Faulted(CrashReport),
|
Faulted(CrashReport),
|
||||||
Terminated,
|
Terminated,
|
||||||
ActionRequired,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@@ -88,7 +64,6 @@ pub struct AdvanceFramesOutcome {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum HostError {
|
pub enum HostError {
|
||||||
Memcard(Box<MemcardFailure>),
|
|
||||||
AlreadyBooted,
|
AlreadyBooted,
|
||||||
BootFailed { reason: String, report: Option<Box<CrashReport>> },
|
BootFailed { reason: String, report: Option<Box<CrashReport>> },
|
||||||
InvalidState { operation: &'static str, status: HostExecutionStatus },
|
InvalidState { operation: &'static str, status: HostExecutionStatus },
|
||||||
@@ -100,7 +75,6 @@ pub enum HostError {
|
|||||||
impl fmt::Display for HostError {
|
impl fmt::Display for HostError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Memcard(error) => write!(f, "{}: {}", error.operation, error.reason),
|
|
||||||
Self::AlreadyBooted => write!(f, "boot requires reset before another attempt"),
|
Self::AlreadyBooted => write!(f, "boot requires reset before another attempt"),
|
||||||
Self::BootFailed { reason, .. } => write!(f, "boot failed: {reason}"),
|
Self::BootFailed { reason, .. } => write!(f, "boot failed: {reason}"),
|
||||||
Self::InvalidState { operation, status } => {
|
Self::InvalidState { operation, status } => {
|
||||||
@@ -130,7 +104,6 @@ pub struct HeadlessHost {
|
|||||||
boot_attempted: bool,
|
boot_attempted: bool,
|
||||||
last_completed_frame: Option<CompletedFrame>,
|
last_completed_frame: Option<CompletedFrame>,
|
||||||
controlled_wall_clock: Option<Arc<ControlledWallClock>>,
|
controlled_wall_clock: Option<Arc<ControlledWallClock>>,
|
||||||
media: memcard::MediaSelection,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@@ -180,7 +153,6 @@ impl HeadlessHost {
|
|||||||
boot_attempted: false,
|
boot_attempted: false,
|
||||||
last_completed_frame: None,
|
last_completed_frame: None,
|
||||||
controlled_wall_clock,
|
controlled_wall_clock,
|
||||||
media: Default::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,15 +166,6 @@ impl HeadlessHost {
|
|||||||
self.controlled_wall_clock.as_deref()
|
self.controlled_wall_clock.as_deref()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Observe native log history without advancing, draining or changing lifecycle.
|
|
||||||
pub fn log_snapshot(
|
|
||||||
&self,
|
|
||||||
after: Option<u64>,
|
|
||||||
limit: usize,
|
|
||||||
) -> (Vec<prometeu_hal::log::LogEvent>, Option<u64>, Option<u64>) {
|
|
||||||
self.firmware.os.log_snapshot(after, limit)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Load `path` and tick until the firmware is in `GameRunning`.
|
/// Load `path` and tick until the firmware is in `GameRunning`.
|
||||||
///
|
///
|
||||||
/// The transition into `GameRunning` is the last tick. The first game
|
/// The transition into `GameRunning` is the last tick. The first game
|
||||||
@@ -226,20 +189,15 @@ impl HeadlessHost {
|
|||||||
return Err(HostError::AlreadyBooted);
|
return Err(HostError::AlreadyBooted);
|
||||||
}
|
}
|
||||||
self.boot_attempted = true;
|
self.boot_attempted = true;
|
||||||
self.media.choice = choice;
|
|
||||||
self.media.boot_phase = "loading";
|
|
||||||
let path = path.into();
|
let path = path.into();
|
||||||
self.firmware.boot_target = BootTarget::Cartridge { path, debug: false, debug_port: 7777 };
|
self.firmware.boot_target = BootTarget::Cartridge { path, debug: false, debug_port: 7777 };
|
||||||
|
|
||||||
let boot_deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
let boot_deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||||
let mut choice_submitted = false;
|
|
||||||
for _ in 0..BOOT_TICK_LIMIT {
|
for _ in 0..BOOT_TICK_LIMIT {
|
||||||
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
||||||
self.media.boot_phase = "running";
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if matches!(self.firmware.state, FirmwareState::AppCrashes(_)) {
|
if matches!(self.firmware.state, FirmwareState::AppCrashes(_)) {
|
||||||
self.media.boot_phase = "failed";
|
|
||||||
let report = self.get_fault();
|
let report = self.get_fault();
|
||||||
let reason = report
|
let reason = report
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@@ -248,55 +206,33 @@ impl HeadlessHost {
|
|||||||
return Err(HostError::BootFailed { reason, report: report.map(Box::new) });
|
return Err(HostError::BootFailed { reason, report: report.map(Box::new) });
|
||||||
}
|
}
|
||||||
if matches!(self.firmware.state, FirmwareState::HubHome(_)) {
|
if matches!(self.firmware.state, FirmwareState::HubHome(_)) {
|
||||||
self.media.boot_phase = "failed";
|
|
||||||
return Err(HostError::BootFailed {
|
return Err(HostError::BootFailed {
|
||||||
reason: "cartridge did not load".to_string(),
|
reason: "cartridge did not load".to_string(),
|
||||||
report: None,
|
report: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let progress = self.firmware.os.memcard().lane.progress_stamp();
|
let progress = self.firmware.os.memcard().lane.progress_stamp();
|
||||||
if let FirmwareState::LoadCartridge(step) = &self.firmware.state
|
|
||||||
&& step.cartridge.memcard_slots.is_none()
|
|
||||||
&& choice.is_some()
|
|
||||||
{
|
|
||||||
self.media.boot_phase = "failed";
|
|
||||||
return Err(self.memcard_error("boot", "invalid_state"));
|
|
||||||
}
|
|
||||||
if choice_submitted
|
|
||||||
&& let prometeu_system::memcard::admission::PreparationState::Failed(error) =
|
|
||||||
self.firmware.os.memcard().admission.state
|
|
||||||
{
|
|
||||||
self.media.boot_phase = "failed";
|
|
||||||
return Err(self.memcard_error("boot", memcard::error_name(error)));
|
|
||||||
}
|
|
||||||
let waiting = matches!(&self.firmware.state,FirmwareState::LoadCartridge(step) if step.needs_memcard_choice());
|
let waiting = matches!(&self.firmware.state,FirmwareState::LoadCartridge(step) if step.needs_memcard_choice());
|
||||||
if waiting {
|
if waiting {
|
||||||
let Some(choice) = choice else {
|
let Some(choice) = choice else {
|
||||||
self.media.boot_phase = "choice_required";
|
return Err(HostError::BootFailed {
|
||||||
return Err(self.memcard_error("boot", "choice_required"));
|
reason: "Memory-card choice required; use boot_with_memcard_choice".into(),
|
||||||
|
report: None,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
if self.firmware.os.memcard().preparation_active() {
|
self.signals = InputSignals::default();
|
||||||
self.poll_memcard();
|
self.tick_once();
|
||||||
self.firmware
|
self.signals = match choice {
|
||||||
.os
|
prometeu_system::memcard::admission::Choice::Persistent => {
|
||||||
.memcard()
|
InputSignals { a_signal: true, ..Default::default() }
|
||||||
.lane
|
}
|
||||||
.wait_for_progress(progress, std::time::Duration::from_millis(5));
|
prometeu_system::memcard::admission::Choice::MemoryFresh => {
|
||||||
continue;
|
InputSignals { x_signal: true, ..Default::default() }
|
||||||
}
|
}
|
||||||
let created_at = self.platform.wall_clock().utc_unix_seconds();
|
prometeu_system::memcard::admission::Choice::MemoryExisting => {
|
||||||
let result = match &mut self.firmware.state {
|
InputSignals { y_signal: true, ..Default::default() }
|
||||||
FirmwareState::LoadCartridge(step) => {
|
|
||||||
step.choose_memcard(&mut self.firmware.os, choice, created_at)
|
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
};
|
||||||
if let Err(error) = result {
|
|
||||||
self.media.boot_phase = "failed";
|
|
||||||
return Err(self.memcard_error("boot", memcard::error_name(error)));
|
|
||||||
}
|
|
||||||
choice_submitted = true;
|
|
||||||
self.media.boot_phase = "preparing";
|
|
||||||
}
|
}
|
||||||
self.tick_once();
|
self.tick_once();
|
||||||
self.signals = InputSignals::default();
|
self.signals = InputSignals::default();
|
||||||
@@ -315,7 +251,6 @@ impl HeadlessHost {
|
|||||||
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
if matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
self.media.boot_phase = "failed";
|
|
||||||
Err(HostError::BootFailed {
|
Err(HostError::BootFailed {
|
||||||
reason: "boot did not reach the game".to_string(),
|
reason: "boot did not reach the game".to_string(),
|
||||||
report: self.get_fault().map(Box::new),
|
report: self.get_fault().map(Box::new),
|
||||||
@@ -323,10 +258,7 @@ impl HeadlessHost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) -> Result<(), HostError> {
|
pub fn reset(&mut self) {
|
||||||
self.reset_with_policy(TeardownPolicy::default()).map(|_| ())
|
|
||||||
}
|
|
||||||
fn reset_inner(&mut self) {
|
|
||||||
self.signals = InputSignals::default();
|
self.signals = InputSignals::default();
|
||||||
self.firmware.change_state(
|
self.firmware.change_state(
|
||||||
FirmwareState::Reset(ResetStep),
|
FirmwareState::Reset(ResetStep),
|
||||||
@@ -364,11 +296,7 @@ impl HeadlessHost {
|
|||||||
return Ok(AdvanceFrameOutcome::Faulted(report));
|
return Ok(AdvanceFrameOutcome::Faulted(report));
|
||||||
}
|
}
|
||||||
if !matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
if !matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
||||||
return Ok(if self.host_action_required() {
|
return Ok(AdvanceFrameOutcome::Terminated);
|
||||||
AdvanceFrameOutcome::ActionRequired
|
|
||||||
} else {
|
|
||||||
AdvanceFrameOutcome::Terminated
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let start = self.firmware.os.vm().logical_frame_index();
|
let start = self.firmware.os.vm().logical_frame_index();
|
||||||
@@ -376,13 +304,7 @@ impl HeadlessHost {
|
|||||||
self.firmware.os.vm().set_paused(false);
|
self.firmware.os.vm().set_paused(false);
|
||||||
self.firmware.os.vm().request_debug_step();
|
self.firmware.os.vm().request_debug_step();
|
||||||
|
|
||||||
let mut io_wait = BootstrapIoWait::default();
|
|
||||||
for _ in 0..HOST_FRAME_ADVANCE_TICK_LIMIT {
|
for _ in 0..HOST_FRAME_ADVANCE_TICK_LIMIT {
|
||||||
// A controlled lifecycle slice can consume the runtime step marker
|
|
||||||
// without closing a logical frame. This operation still owns the
|
|
||||||
// request until its first frame or an explicit stop boundary.
|
|
||||||
self.firmware.os.vm().set_paused(false);
|
|
||||||
self.firmware.os.vm().request_debug_step();
|
|
||||||
self.tick_once();
|
self.tick_once();
|
||||||
if let Some(report) = self.get_fault() {
|
if let Some(report) = self.get_fault() {
|
||||||
self.finish_frame_advance();
|
self.finish_frame_advance();
|
||||||
@@ -390,11 +312,7 @@ impl HeadlessHost {
|
|||||||
}
|
}
|
||||||
if !matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
if !matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
||||||
self.finish_frame_advance();
|
self.finish_frame_advance();
|
||||||
return Ok(if self.host_action_required() {
|
return Ok(AdvanceFrameOutcome::Terminated);
|
||||||
AdvanceFrameOutcome::ActionRequired
|
|
||||||
} else {
|
|
||||||
AdvanceFrameOutcome::Terminated
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let logical_frame_index = self.firmware.os.vm().logical_frame_index();
|
let logical_frame_index = self.firmware.os.vm().logical_frame_index();
|
||||||
@@ -415,24 +333,6 @@ impl HeadlessHost {
|
|||||||
frame_id: frame.frame_id,
|
frame_id: frame.frame_id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
|
||||||
// Refresh before every observation/poll: another lane job may
|
|
||||||
// have progressed without completing this publication.
|
|
||||||
let progress = self.firmware.os.memcard().lane.progress_stamp();
|
|
||||||
if !self.bootstrap_io_pending() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Native polling and waiting do not execute machine ticks.
|
|
||||||
self.poll_memcard();
|
|
||||||
if self.host_action_required() {
|
|
||||||
self.finish_frame_advance();
|
|
||||||
return Ok(AdvanceFrameOutcome::ActionRequired);
|
|
||||||
}
|
|
||||||
if self.bootstrap_io_pending() {
|
|
||||||
self.wait_for_bootstrap_io(&mut io_wait, progress)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.finish_frame_advance();
|
self.finish_frame_advance();
|
||||||
@@ -441,32 +341,6 @@ impl HeadlessHost {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bootstrap_io_pending(&mut self) -> bool {
|
|
||||||
let FirmwareState::GameRunning(step) = &self.firmware.state else { return false };
|
|
||||||
let task = step.task_id;
|
|
||||||
let hook_complete = self
|
|
||||||
.firmware
|
|
||||||
.os
|
|
||||||
.sessions()
|
|
||||||
.vm_session_for_task(task)
|
|
||||||
.is_some_and(|session| session.vm.memcard_boot_result() == Some(0));
|
|
||||||
let service = self.firmware.os.memcard();
|
|
||||||
hook_complete && service.session.boot_pending() && service.session.io_active()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wait_for_bootstrap_io(
|
|
||||||
&mut self,
|
|
||||||
wait: &mut BootstrapIoWait,
|
|
||||||
progress: u64,
|
|
||||||
) -> Result<(), HostError> {
|
|
||||||
let Some(timeout) = wait.next_wait(Instant::now()) else {
|
|
||||||
self.finish_frame_advance();
|
|
||||||
return Err(self.memcard_error("advance", "restart_required"));
|
|
||||||
};
|
|
||||||
self.firmware.os.memcard().lane.wait_for_progress(progress, timeout);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn advance_frames(&mut self, count: u64) -> Result<AdvanceFramesOutcome, HostError> {
|
pub fn advance_frames(&mut self, count: u64) -> Result<AdvanceFramesOutcome, HostError> {
|
||||||
let mut completed = 0;
|
let mut completed = 0;
|
||||||
for _ in 0..count {
|
for _ in 0..count {
|
||||||
@@ -486,13 +360,6 @@ impl HeadlessHost {
|
|||||||
stop: AdvanceFramesStop::Terminated,
|
stop: AdvanceFramesStop::Terminated,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
AdvanceFrameOutcome::ActionRequired => {
|
|
||||||
return Ok(AdvanceFramesOutcome {
|
|
||||||
requested: count,
|
|
||||||
completed,
|
|
||||||
stop: AdvanceFramesStop::ActionRequired,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(AdvanceFramesOutcome { requested: count, completed, stop: AdvanceFramesStop::Completed })
|
Ok(AdvanceFramesOutcome { requested: count, completed, stop: AdvanceFramesStop::Completed })
|
||||||
@@ -549,8 +416,6 @@ impl HeadlessHost {
|
|||||||
fn execution_status(&mut self) -> HostExecutionStatus {
|
fn execution_status(&mut self) -> HostExecutionStatus {
|
||||||
if self.fault_is_visible() {
|
if self.fault_is_visible() {
|
||||||
HostExecutionStatus::Faulted
|
HostExecutionStatus::Faulted
|
||||||
} else if self.host_action_required() {
|
|
||||||
HostExecutionStatus::Paused
|
|
||||||
} else if !matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
} else if !matches!(self.firmware.state, FirmwareState::GameRunning(_)) {
|
||||||
HostExecutionStatus::Terminated
|
HostExecutionStatus::Terminated
|
||||||
} else if self.firmware.os.vm().paused() {
|
} else if self.firmware.os.vm().paused() {
|
||||||
@@ -567,9 +432,7 @@ impl HeadlessHost {
|
|||||||
|
|
||||||
fn require_active(&mut self, operation: &'static str) -> Result<(), HostError> {
|
fn require_active(&mut self, operation: &'static str) -> Result<(), HostError> {
|
||||||
let status = self.execution_status();
|
let status = self.execution_status();
|
||||||
if matches!(status, HostExecutionStatus::Running | HostExecutionStatus::Paused)
|
if matches!(status, HostExecutionStatus::Running | HostExecutionStatus::Paused) {
|
||||||
&& !self.host_action_required()
|
|
||||||
{
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(HostError::InvalidState { operation, status })
|
Err(HostError::InvalidState { operation, status })
|
||||||
@@ -600,11 +463,6 @@ impl RenderSubmissionSink for HeadlessPlatform {
|
|||||||
let frame_id = submission.frame_id;
|
let frame_id = submission.frame_id;
|
||||||
let ownership = submission.ownership;
|
let ownership = submission.ownership;
|
||||||
self.hardware.submit_render_submission(submission)?;
|
self.hardware.submit_render_submission(submission)?;
|
||||||
// Host dialogs use their own producer and may reuse FrameId::ZERO.
|
|
||||||
// Capture/advance observe only canonical game publications.
|
|
||||||
if ownership.app_mode != prometeu_hal::app_mode::AppMode::Game {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
let (width, height) = self.hardware.gfx.size();
|
let (width, height) = self.hardware.gfx.size();
|
||||||
let pixels = self.hardware.gfx.front_buffer().to_vec();
|
let pixels = self.hardware.gfx.front_buffer().to_vec();
|
||||||
let frame = OwnedRgba8888Frame::packed(frame_id, ownership, width, height, pixels)
|
let frame = OwnedRgba8888Frame::packed(frame_id, ownership, width, height, pixels)
|
||||||
|
|||||||
@@ -11,6 +11,5 @@ mod host;
|
|||||||
|
|
||||||
pub use host::{
|
pub use host::{
|
||||||
AdvanceFrameOutcome, AdvanceFramesOutcome, AdvanceFramesStop, CapturedFrame, HeadlessHost,
|
AdvanceFrameOutcome, AdvanceFramesOutcome, AdvanceFramesStop, CapturedFrame, HeadlessHost,
|
||||||
HostError, HostExecutionStatus, HostState, MediaAccess, MediaAction, MemcardChoice,
|
HostError, HostExecutionStatus, HostState,
|
||||||
MemcardFailure, MemcardMount, MemcardState, PendingWrites, TeardownPolicy,
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,480 +0,0 @@
|
|||||||
//! Public native host controls; all image policy stays in the shared system service.
|
|
||||||
use super::{HeadlessHost, HostError};
|
|
||||||
use prometeu_drivers::memcard::FileMemcard;
|
|
||||||
use prometeu_firmware::FirmwareState;
|
|
||||||
use prometeu_hal::memcard::{MemcardIoError, MemcardStorage};
|
|
||||||
use prometeu_system::memcard::{
|
|
||||||
admission::{Choice, PreparationState},
|
|
||||||
image::{Error, IMAGE_BYTES},
|
|
||||||
session::{Mode, RequestReason, RequestState},
|
|
||||||
transaction::Card,
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
path::PathBuf,
|
|
||||||
time::{Duration, Instant},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "snake_case")]
|
|
||||||
pub enum MediaAction {
|
|
||||||
Open,
|
|
||||||
Create,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "snake_case")]
|
|
||||||
pub enum MediaAccess {
|
|
||||||
ReadWrite,
|
|
||||||
ReadOnly,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
|
||||||
#[serde(deny_unknown_fields)]
|
|
||||||
pub struct MemcardMount {
|
|
||||||
pub path: String,
|
|
||||||
pub action: MediaAction,
|
|
||||||
pub access: MediaAccess,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "snake_case")]
|
|
||||||
pub enum PendingWrites {
|
|
||||||
#[default]
|
|
||||||
Wait,
|
|
||||||
Cancel,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, Copy, Default, Deserialize)]
|
|
||||||
#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
|
|
||||||
pub struct TeardownPolicy {
|
|
||||||
pub discard_unsaved: bool,
|
|
||||||
pub pending_writes: PendingWrites,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, Copy, Deserialize)]
|
|
||||||
#[serde(rename_all = "snake_case")]
|
|
||||||
pub enum MemcardChoice {
|
|
||||||
Persistent,
|
|
||||||
MemoryFresh,
|
|
||||||
MemoryExisting,
|
|
||||||
}
|
|
||||||
impl From<MemcardChoice> for Choice {
|
|
||||||
fn from(c: MemcardChoice) -> Self {
|
|
||||||
match c {
|
|
||||||
MemcardChoice::Persistent => Self::Persistent,
|
|
||||||
MemcardChoice::MemoryFresh => Self::MemoryFresh,
|
|
||||||
MemcardChoice::MemoryExisting => Self::MemoryExisting,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub(super) fn choice_name(c: Choice) -> &'static str {
|
|
||||||
match c {
|
|
||||||
Choice::Persistent => "persistent",
|
|
||||||
Choice::MemoryFresh => "memory_fresh",
|
|
||||||
Choice::MemoryExisting => "memory_existing",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub(super) fn error_name(e: Error) -> &'static str {
|
|
||||||
match e {
|
|
||||||
Error::Io(e) => match e {
|
|
||||||
MemcardIoError::Unavailable => "unavailable",
|
|
||||||
MemcardIoError::ReadOnly => "read_only",
|
|
||||||
MemcardIoError::InvalidRange => "invalid_range",
|
|
||||||
MemcardIoError::NoSpace => "no_space",
|
|
||||||
MemcardIoError::Unsupported => "unsupported",
|
|
||||||
MemcardIoError::Busy => "busy",
|
|
||||||
MemcardIoError::AccessDenied => "access_denied",
|
|
||||||
MemcardIoError::AlreadyExists => "already_exists",
|
|
||||||
MemcardIoError::Indeterminate => "indeterminate",
|
|
||||||
MemcardIoError::IoError => "io_error",
|
|
||||||
},
|
|
||||||
Error::Corrupt => "corrupt",
|
|
||||||
Error::Unsupported => "unsupported",
|
|
||||||
Error::NoSpace => "no_space",
|
|
||||||
Error::InvalidRange => "invalid_range",
|
|
||||||
Error::CounterExhausted => "counter_exhausted",
|
|
||||||
Error::ReadOnly => "read_only",
|
|
||||||
Error::Indeterminate => "indeterminate",
|
|
||||||
Error::Busy => "busy",
|
|
||||||
Error::Canceled => "canceled",
|
|
||||||
Error::OutOfMemory => "out_of_memory",
|
|
||||||
Error::AccessDenied => "access_denied",
|
|
||||||
Error::InvalidState => "invalid_state",
|
|
||||||
Error::IdentityMismatch => "identity_mismatch",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct MemcardState {
|
|
||||||
pub media: MediaState,
|
|
||||||
pub session: SessionState,
|
|
||||||
pub dirty: bool,
|
|
||||||
pub dirty_slots: Vec<usize>,
|
|
||||||
pub request: Option<RequestObservation>,
|
|
||||||
pub io: IoState,
|
|
||||||
pub recovery: RecoveryState,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct MediaState {
|
|
||||||
pub path: Option<PathBuf>,
|
|
||||||
pub card_id: Option<String>,
|
|
||||||
pub access: Option<MediaAccess>,
|
|
||||||
pub mount_epoch: String,
|
|
||||||
pub preparation: &'static str,
|
|
||||||
pub error: Option<&'static str>,
|
|
||||||
pub recovered: bool,
|
|
||||||
pub degraded: bool,
|
|
||||||
pub write_blocked: bool,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct SessionState {
|
|
||||||
pub app_id: Option<u32>,
|
|
||||||
pub choice: Option<&'static str>,
|
|
||||||
pub mode: Option<&'static str>,
|
|
||||||
pub boot_attempted: bool,
|
|
||||||
pub boot_phase: &'static str,
|
|
||||||
pub resident: bool,
|
|
||||||
pub action_required: bool,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct RequestObservation {
|
|
||||||
pub id: String,
|
|
||||||
pub state: &'static str,
|
|
||||||
pub reason: &'static str,
|
|
||||||
pub storage_error: Option<&'static str>,
|
|
||||||
pub operation: &'static str,
|
|
||||||
pub slot: Option<usize>,
|
|
||||||
pub retained: bool,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
||||||
pub struct IoState {
|
|
||||||
pub active: bool,
|
|
||||||
pub deferred: bool,
|
|
||||||
pub phase: &'static str,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct RecoveryState {
|
|
||||||
pub removal_pending: bool,
|
|
||||||
pub reconciliation_required: bool,
|
|
||||||
pub restart_required: bool,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub struct MemcardFailure {
|
|
||||||
pub operation: &'static str,
|
|
||||||
pub reason: &'static str,
|
|
||||||
pub state: MemcardState,
|
|
||||||
}
|
|
||||||
#[derive(Default)]
|
|
||||||
pub(super) struct MediaSelection {
|
|
||||||
path: Option<PathBuf>,
|
|
||||||
access: Option<MediaAccess>,
|
|
||||||
card_id: Option<[u8; 16]>,
|
|
||||||
error: Option<&'static str>,
|
|
||||||
pub choice: Option<Choice>,
|
|
||||||
pub boot_phase: &'static str,
|
|
||||||
shutdown_failed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HeadlessHost {
|
|
||||||
/// Poll only native work. This does not tick firmware or execute guest code.
|
|
||||||
pub fn poll_memcard(&mut self) {
|
|
||||||
self.firmware.os.poll_background_io(&mut self.platform);
|
|
||||||
}
|
|
||||||
pub(super) fn memcard_error(
|
|
||||||
&mut self,
|
|
||||||
operation: &'static str,
|
|
||||||
reason: &'static str,
|
|
||||||
) -> HostError {
|
|
||||||
HostError::Memcard(Box::new(MemcardFailure {
|
|
||||||
operation,
|
|
||||||
reason,
|
|
||||||
state: self.get_memcard_state(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
pub fn get_memcard_state(&mut self) -> MemcardState {
|
|
||||||
let resident = self.firmware.os.lifecycle().resident_game_task().is_some();
|
|
||||||
let service = self.firmware.os.memcard();
|
|
||||||
let d = service.session_diagnostics();
|
|
||||||
let preparation = service.preparation_active();
|
|
||||||
let host_faulted = service.host_faulted() || self.media.shutdown_failed;
|
|
||||||
let (state, error) = match service.admission.state {
|
|
||||||
PreparationState::Idle => ("idle", None),
|
|
||||||
PreparationState::Pending => ("pending", None),
|
|
||||||
PreparationState::Ready => ("ready", None),
|
|
||||||
PreparationState::Failed(e) => ("failed", Some(error_name(e))),
|
|
||||||
};
|
|
||||||
let id = d.card_id.or(service.installed_card_id()).or(self.media.card_id);
|
|
||||||
let health = if d.mode.is_some() {
|
|
||||||
d.health
|
|
||||||
} else {
|
|
||||||
service.installed_health().unwrap_or((false, false, false))
|
|
||||||
};
|
|
||||||
let phase = if preparation {
|
|
||||||
"preparing"
|
|
||||||
} else if d.request.as_ref().is_some_and(|r| r.deferred) {
|
|
||||||
"deferred"
|
|
||||||
} else if d.io_active {
|
|
||||||
if service.lane.telemetry().active_job_id == service.session.pending_job() {
|
|
||||||
"running"
|
|
||||||
} else {
|
|
||||||
"queued"
|
|
||||||
}
|
|
||||||
} else if d.request.is_some() {
|
|
||||||
"returned"
|
|
||||||
} else {
|
|
||||||
"idle"
|
|
||||||
};
|
|
||||||
MemcardState {
|
|
||||||
media: MediaState {
|
|
||||||
path: self.media.path.clone(),
|
|
||||||
access: self.media.access,
|
|
||||||
card_id: id.map(|id| id.iter().map(|v| format!("{v:02x}")).collect()),
|
|
||||||
mount_epoch: d.epoch.to_string(),
|
|
||||||
preparation: state,
|
|
||||||
error: error.or(self.media.error),
|
|
||||||
recovered: health.0,
|
|
||||||
degraded: health.1,
|
|
||||||
write_blocked: health.2,
|
|
||||||
},
|
|
||||||
session: SessionState {
|
|
||||||
app_id: d.app_id,
|
|
||||||
choice: self.media.choice.map(choice_name),
|
|
||||||
mode: d.mode.map(|m| match m {
|
|
||||||
Mode::Persistent => "persistent",
|
|
||||||
Mode::MemoryOnly => "memory_only",
|
|
||||||
}),
|
|
||||||
boot_attempted: self.boot_attempted,
|
|
||||||
boot_phase: if self.media.boot_phase.is_empty() {
|
|
||||||
"idle"
|
|
||||||
} else {
|
|
||||||
self.media.boot_phase
|
|
||||||
},
|
|
||||||
resident,
|
|
||||||
action_required: d.mode.is_some()
|
|
||||||
&& resident
|
|
||||||
&& (d.removal_pending
|
|
||||||
|| host_faulted
|
|
||||||
|| !matches!(self.firmware.state, FirmwareState::GameRunning(_))),
|
|
||||||
},
|
|
||||||
dirty: !d.dirty_slots.is_empty(),
|
|
||||||
dirty_slots: d.dirty_slots,
|
|
||||||
request: d.request.map(|r| RequestObservation {
|
|
||||||
id: r.result.id.to_string(),
|
|
||||||
state: match r.result.state {
|
|
||||||
RequestState::Pending => "pending",
|
|
||||||
RequestState::Success => "success",
|
|
||||||
RequestState::Failure => "failure",
|
|
||||||
RequestState::Indeterminate => "indeterminate",
|
|
||||||
RequestState::MemoryOnly => "memory_only",
|
|
||||||
},
|
|
||||||
reason: match r.result.reason {
|
|
||||||
RequestReason::None => "none",
|
|
||||||
RequestReason::Timeout => "timeout",
|
|
||||||
RequestReason::RestartRequired => "restart_required",
|
|
||||||
RequestReason::Storage(e) => error_name(e),
|
|
||||||
},
|
|
||||||
storage_error: r.storage_error.map(error_name),
|
|
||||||
operation: r.operation,
|
|
||||||
slot: r.slot,
|
|
||||||
retained: r.retained,
|
|
||||||
}),
|
|
||||||
io: IoState {
|
|
||||||
active: preparation || d.io_active,
|
|
||||||
deferred: phase == "deferred",
|
|
||||||
phase,
|
|
||||||
},
|
|
||||||
recovery: RecoveryState {
|
|
||||||
removal_pending: d.removal_pending,
|
|
||||||
reconciliation_required: d.reconciliation_required,
|
|
||||||
restart_required: host_faulted,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn mount_memcard(&mut self, options: MemcardMount) -> Result<MemcardState, HostError> {
|
|
||||||
self.poll_memcard();
|
|
||||||
if self.firmware.os.lifecycle().resident_game_task().is_some() {
|
|
||||||
return Err(self.memcard_error("memcard_mount", "invalid_state"));
|
|
||||||
}
|
|
||||||
if options.path.trim().is_empty()
|
|
||||||
|| matches!(
|
|
||||||
(options.action, options.access),
|
|
||||||
(MediaAction::Create, MediaAccess::ReadOnly)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
return Err(self.memcard_error("memcard_mount", "invalid_state"));
|
|
||||||
}
|
|
||||||
let path = std::path::absolute(&options.path)
|
|
||||||
.map_err(|_| self.memcard_error("memcard_mount", "unavailable"))?;
|
|
||||||
// Do not change selection when admission is refused as busy.
|
|
||||||
let open_path = path.clone();
|
|
||||||
let result = self.firmware.os.memcard().mount_card_with(move || {
|
|
||||||
let io = match options.action {
|
|
||||||
MediaAction::Open => {
|
|
||||||
FileMemcard::open(&open_path, options.access == MediaAccess::ReadOnly)?
|
|
||||||
}
|
|
||||||
MediaAction::Create => FileMemcard::create(&open_path, IMAGE_BYTES, |io, id| {
|
|
||||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
|
||||||
})?,
|
|
||||||
};
|
|
||||||
Ok(Box::new(io) as Box<dyn MemcardStorage>)
|
|
||||||
});
|
|
||||||
result.map_err(|e| self.memcard_error("memcard_mount", error_name(e)))?;
|
|
||||||
self.media.path = Some(path);
|
|
||||||
self.media.access = Some(options.access);
|
|
||||||
self.media.error = None;
|
|
||||||
self.media.card_id = None;
|
|
||||||
if let Err(error) = self.wait_preparation("memcard_mount") {
|
|
||||||
if let HostError::Memcard(e) = &error {
|
|
||||||
self.media.error = Some(e.reason);
|
|
||||||
}
|
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
self.media.card_id = self.firmware.os.memcard().installed_card_id();
|
|
||||||
Ok(self.get_memcard_state())
|
|
||||||
}
|
|
||||||
pub fn unmount_memcard(&mut self) -> Result<MemcardState, HostError> {
|
|
||||||
self.poll_memcard();
|
|
||||||
if self.firmware.os.lifecycle().resident_game_task().is_some() {
|
|
||||||
return Err(self.memcard_error("memcard_unmount", "invalid_state"));
|
|
||||||
}
|
|
||||||
self.firmware
|
|
||||||
.os
|
|
||||||
.memcard()
|
|
||||||
.uninstall_card()
|
|
||||||
.map_err(|e| self.memcard_error("memcard_unmount", error_name(e)))?;
|
|
||||||
self.media = MediaSelection { boot_phase: self.media.boot_phase, ..Default::default() };
|
|
||||||
Ok(self.get_memcard_state())
|
|
||||||
}
|
|
||||||
fn wait_preparation(&mut self, operation: &'static str) -> Result<(), HostError> {
|
|
||||||
let deadline = Instant::now() + Duration::from_secs(2);
|
|
||||||
loop {
|
|
||||||
self.poll_memcard();
|
|
||||||
match self.firmware.os.memcard().admission.state {
|
|
||||||
PreparationState::Ready => return Ok(()),
|
|
||||||
PreparationState::Failed(e) => {
|
|
||||||
let reason = if self.firmware.os.memcard().host_faulted() {
|
|
||||||
"restart_required"
|
|
||||||
} else {
|
|
||||||
error_name(e)
|
|
||||||
};
|
|
||||||
return Err(self.memcard_error(operation, reason));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
if Instant::now() >= deadline {
|
|
||||||
return Err(self.memcard_error(operation, "restart_required"));
|
|
||||||
}
|
|
||||||
let lane = self.firmware.os.memcard().lane.clone();
|
|
||||||
lane.wait_for_progress(lane.progress_stamp(), Duration::from_millis(5));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn prepare_teardown(
|
|
||||||
&mut self,
|
|
||||||
policy: TeardownPolicy,
|
|
||||||
operation: &'static str,
|
|
||||||
) -> Result<MemcardState, HostError> {
|
|
||||||
self.poll_memcard();
|
|
||||||
if matches!(policy.pending_writes, PendingWrites::Cancel) {
|
|
||||||
self.firmware.os.memcard().cancel_pending();
|
|
||||||
}
|
|
||||||
let deadline = Instant::now() + Duration::from_secs(2);
|
|
||||||
loop {
|
|
||||||
self.poll_memcard();
|
|
||||||
let state = self.get_memcard_state();
|
|
||||||
if !state.io.active && !state.io.deferred {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if state.recovery.restart_required || Instant::now() >= deadline {
|
|
||||||
return Err(self.memcard_error(operation, "restart_required"));
|
|
||||||
}
|
|
||||||
let lane = self.firmware.os.memcard().lane.clone();
|
|
||||||
lane.wait_for_progress(lane.progress_stamp(), Duration::from_millis(5));
|
|
||||||
}
|
|
||||||
if self.get_memcard_state().dirty && !policy.discard_unsaved {
|
|
||||||
return Err(self.memcard_error(operation, "unsaved_changes"));
|
|
||||||
}
|
|
||||||
Ok(self.get_memcard_state())
|
|
||||||
}
|
|
||||||
pub fn reset_with_policy(&mut self, policy: TeardownPolicy) -> Result<MemcardState, HostError> {
|
|
||||||
self.prepare_teardown(policy, "reset")?;
|
|
||||||
self.reset_inner();
|
|
||||||
self.media.choice = None;
|
|
||||||
self.media.boot_phase = "idle";
|
|
||||||
self.poll_memcard();
|
|
||||||
Ok(self.get_memcard_state())
|
|
||||||
}
|
|
||||||
pub fn shutdown(&mut self, policy: TeardownPolicy) -> Result<MemcardState, HostError> {
|
|
||||||
let state = self.prepare_teardown(policy, "shutdown")?;
|
|
||||||
if self.firmware.os.stop_background_io().is_err() {
|
|
||||||
self.media.shutdown_failed = true;
|
|
||||||
return Err(self.memcard_error("shutdown", "restart_required"));
|
|
||||||
}
|
|
||||||
self.firmware.os.memcard().session.end_session();
|
|
||||||
self.firmware.os.memcard().poll(Instant::now());
|
|
||||||
self.firmware
|
|
||||||
.os
|
|
||||||
.memcard()
|
|
||||||
.uninstall_card()
|
|
||||||
.map_err(|e| self.memcard_error("shutdown", error_name(e)))?;
|
|
||||||
Ok(state)
|
|
||||||
}
|
|
||||||
pub fn reconcile_memcard(&mut self) -> Result<MemcardState, HostError> {
|
|
||||||
self.poll_memcard();
|
|
||||||
if !self.suspended_for_memcard() {
|
|
||||||
return Err(self.memcard_error("memcard_reconcile", "invalid_state"));
|
|
||||||
}
|
|
||||||
let path = self
|
|
||||||
.media
|
|
||||||
.path
|
|
||||||
.clone()
|
|
||||||
.ok_or_else(|| self.memcard_error("memcard_reconcile", "invalid_state"))?;
|
|
||||||
let read_only = self.media.access == Some(MediaAccess::ReadOnly);
|
|
||||||
self.firmware
|
|
||||||
.os
|
|
||||||
.memcard()
|
|
||||||
.reconcile_card_with(move || {
|
|
||||||
Ok(Box::new(FileMemcard::open(&path, read_only)?) as Box<dyn MemcardStorage>)
|
|
||||||
})
|
|
||||||
.map_err(|e| self.memcard_error("memcard_reconcile", error_name(e)))?;
|
|
||||||
self.wait_preparation("memcard_reconcile")?;
|
|
||||||
self.media.error = None;
|
|
||||||
if !self.firmware.resume_resident_game_from_home() {
|
|
||||||
return Err(self.memcard_error("memcard_reconcile", "invalid_state"));
|
|
||||||
}
|
|
||||||
Ok(self.get_memcard_state())
|
|
||||||
}
|
|
||||||
pub fn continue_memcard_in_memory(&mut self) -> Result<MemcardState, HostError> {
|
|
||||||
self.poll_memcard();
|
|
||||||
if !self.suspended_for_memcard() {
|
|
||||||
return Err(self.memcard_error("memcard_continue_in_memory", "invalid_state"));
|
|
||||||
}
|
|
||||||
self.firmware
|
|
||||||
.os
|
|
||||||
.detach_memcard()
|
|
||||||
.map_err(|e| self.memcard_error("memcard_continue_in_memory", error_name(e)))?;
|
|
||||||
if !self.firmware.resume_resident_game_from_home() {
|
|
||||||
return Err(self.memcard_error("memcard_continue_in_memory", "invalid_state"));
|
|
||||||
}
|
|
||||||
Ok(self.get_memcard_state())
|
|
||||||
}
|
|
||||||
fn suspended_for_memcard(&mut self) -> bool {
|
|
||||||
let Some(task) = self.firmware.os.lifecycle().resident_game_task() else { return false };
|
|
||||||
self.firmware.os.lifecycle().task_state(task)
|
|
||||||
== Some(prometeu_system::task::TaskState::Suspended)
|
|
||||||
&& (self.firmware.os.memcard().session.removal_pending()
|
|
||||||
|| self.firmware.os.memcard().session.diagnostics().reconciliation_required
|
|
||||||
|| self.firmware.os.memcard().session.host_faulted())
|
|
||||||
}
|
|
||||||
pub(super) fn host_action_required(&mut self) -> bool {
|
|
||||||
self.firmware.os.memcard().session.mode().is_some()
|
|
||||||
&& self.firmware.os.lifecycle().resident_game_task().is_some()
|
|
||||||
&& (self.firmware.os.memcard().session.removal_pending()
|
|
||||||
|| self.firmware.os.memcard().host_faulted()
|
|
||||||
|| !matches!(self.firmware.state, FirmwareState::GameRunning(_)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[path = "memcard_tests.rs"]
|
|
||||||
mod tests;
|
|
||||||
@@ -1,487 +0,0 @@
|
|||||||
use super::*;
|
|
||||||
use crate::{AdvanceFrameOutcome, HostExecutionStatus};
|
|
||||||
use prometeu_test_support::memcard_fixture::{MemcardFixture, memcard_program};
|
|
||||||
use std::{fs, sync::mpsc};
|
|
||||||
|
|
||||||
/// Holds the first native write after admission; the controller decides its outcome.
|
|
||||||
struct BootstrapStore {
|
|
||||||
file: FileMemcard,
|
|
||||||
entered: mpsc::Sender<()>,
|
|
||||||
continue_io: mpsc::Receiver<Result<(), MemcardIoError>>,
|
|
||||||
held: bool,
|
|
||||||
}
|
|
||||||
impl MemcardStorage for BootstrapStore {
|
|
||||||
fn len(&self) -> Result<u64, MemcardIoError> {
|
|
||||||
self.file.len()
|
|
||||||
}
|
|
||||||
fn read_only(&self) -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
fn read_at(&mut self, offset: u64, bytes: &mut [u8]) -> Result<(), MemcardIoError> {
|
|
||||||
self.file.read_at(offset, bytes)
|
|
||||||
}
|
|
||||||
fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), MemcardIoError> {
|
|
||||||
if !self.held {
|
|
||||||
self.held = true;
|
|
||||||
let _ = self.entered.send(());
|
|
||||||
self.continue_io
|
|
||||||
.recv_timeout(Duration::from_secs(5))
|
|
||||||
.unwrap_or(Err(MemcardIoError::IoError))?;
|
|
||||||
}
|
|
||||||
self.file.write_at(offset, bytes)
|
|
||||||
}
|
|
||||||
fn sync(&mut self) -> Result<(), MemcardIoError> {
|
|
||||||
self.file.sync()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn reopen_with_controlled_io(
|
|
||||||
kind: MemcardFixture,
|
|
||||||
) -> (Fixture, HeadlessHost, mpsc::Receiver<()>, mpsc::Sender<Result<(), MemcardIoError>>) {
|
|
||||||
use prometeu_system::memcard::{
|
|
||||||
image::{Game, Icon, crc32c},
|
|
||||||
transaction::{DurableResult, PublicationControl},
|
|
||||||
};
|
|
||||||
let f = Fixture::new(kind);
|
|
||||||
let file = FileMemcard::create(&f.image, IMAGE_BYTES, |io, id| {
|
|
||||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let mut card = Card::mount(Box::new(file)).unwrap();
|
|
||||||
let icon = [0; 560];
|
|
||||||
card.register(
|
|
||||||
Game {
|
|
||||||
app_id: 42,
|
|
||||||
title: "Existing record".into(),
|
|
||||||
data_format_version: 1,
|
|
||||||
created_at: 0,
|
|
||||||
slots: vec![0],
|
|
||||||
icon: Icon { offset: 0, frames: 1, duration: 1, crc32c: crc32c(&icon) },
|
|
||||||
},
|
|
||||||
&icon,
|
|
||||||
&PublicationControl::default(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
card.commit(0, &[0, 127, 128, 255], 0, &PublicationControl::default()).durable,
|
|
||||||
DurableResult::Success
|
|
||||||
);
|
|
||||||
drop(card);
|
|
||||||
let (entered_tx, entered) = mpsc::channel();
|
|
||||||
let (continue_io, release_rx) = mpsc::channel();
|
|
||||||
let mut host = HeadlessHost::new();
|
|
||||||
host.media.path = Some(f.image.clone());
|
|
||||||
host.media.access = Some(MediaAccess::ReadWrite);
|
|
||||||
host.install_memcard(Box::new(BootstrapStore {
|
|
||||||
file: FileMemcard::open(&f.image, false).unwrap(),
|
|
||||||
entered: entered_tx,
|
|
||||||
continue_io: release_rx,
|
|
||||||
held: false,
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
host.boot_with_memcard_choice(f.cart.to_string_lossy(), Choice::Persistent).unwrap();
|
|
||||||
assert_eq!(host.get_state().tick_index, 0);
|
|
||||||
(f, host, entered, continue_io)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn first_reopen_frame_waits_for_controlled_presentation_without_replaying() {
|
|
||||||
let (_f, mut host, entered, continue_io) =
|
|
||||||
reopen_with_controlled_io(MemcardFixture::ReadWithPresentation);
|
|
||||||
let (finished_tx, finished) = mpsc::channel();
|
|
||||||
let controller = std::thread::spawn(move || {
|
|
||||||
entered.recv_timeout(Duration::from_secs(3)).unwrap();
|
|
||||||
// Observe the owner while publication is provably held. This is a
|
|
||||||
// channel deadline, not a sleep used to infer that IO has started.
|
|
||||||
let premature = finished.recv_timeout(Duration::from_millis(300)).is_ok();
|
|
||||||
let _ = continue_io.send(Ok(()));
|
|
||||||
premature
|
|
||||||
});
|
|
||||||
let result = host.advance_frame();
|
|
||||||
let _ = finished_tx.send(());
|
|
||||||
let premature = controller.join().unwrap();
|
|
||||||
assert!(!premature, "advance returned while native publication was held: {result:?}");
|
|
||||||
assert!(matches!(
|
|
||||||
result,
|
|
||||||
Ok(AdvanceFrameOutcome::FrameCompleted { logical_frame_index: 1, .. })
|
|
||||||
));
|
|
||||||
let state = host.get_state();
|
|
||||||
assert_eq!(state.frame_index, 1);
|
|
||||||
assert_eq!(state.tick_index, 2);
|
|
||||||
assert_eq!(state.status, HostExecutionStatus::Paused);
|
|
||||||
assert_eq!(state.stack_top.last(), Some(&prometeu_bytecode::Value::Int64(201)));
|
|
||||||
assert!(!host.firmware.os.vm().debug_step_requested());
|
|
||||||
assert!(host.get_fault().is_none());
|
|
||||||
assert!(!host.get_memcard_state().io.active);
|
|
||||||
assert_eq!(host.capture_frame().unwrap().logical_frame_index, 1);
|
|
||||||
host.shutdown(TeardownPolicy::default()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn blocked_bootstrap_reaches_native_deadline_and_keeps_worker_lock() {
|
|
||||||
let (f, mut host, entered, continue_io) =
|
|
||||||
reopen_with_controlled_io(MemcardFixture::ReadWithPresentation);
|
|
||||||
let started = Instant::now();
|
|
||||||
let result = host.advance_frame();
|
|
||||||
entered.recv_timeout(Duration::from_secs(1)).unwrap();
|
|
||||||
let state = host.get_memcard_state();
|
|
||||||
let busy = matches!(FileMemcard::open(&f.image, false), Err(MemcardIoError::Busy));
|
|
||||||
// Always let the worker return before asserting, including on the old host.
|
|
||||||
continue_io.send(Ok(())).unwrap();
|
|
||||||
settle(&mut host);
|
|
||||||
assert_eq!(result.unwrap(), AdvanceFrameOutcome::ActionRequired);
|
|
||||||
assert!(started.elapsed() < Duration::from_secs(3));
|
|
||||||
assert!(state.recovery.restart_required);
|
|
||||||
assert!(state.io.active && busy);
|
|
||||||
assert_eq!(host.get_state().frame_index, 0);
|
|
||||||
assert_eq!(host.get_state().tick_index, 1);
|
|
||||||
assert_eq!(host.get_state().status, HostExecutionStatus::Paused);
|
|
||||||
assert!(!host.firmware.os.vm().debug_step_requested());
|
|
||||||
assert!(host.get_fault().is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ordinary_gameplay_commit_does_not_wait_for_native_io() {
|
|
||||||
let (_f, mut host, entered, continue_io) = reopen_with_controlled_io(MemcardFixture::Read);
|
|
||||||
host.advance_frame().unwrap();
|
|
||||||
let service = host.firmware.os.memcard();
|
|
||||||
let context = service.session.context();
|
|
||||||
service.session.write(context, 0, 0, &[0, 127, 128, 255]).unwrap();
|
|
||||||
service.session.commit(context, 0, 0, Instant::now(), &service.lane).unwrap();
|
|
||||||
entered.recv_timeout(Duration::from_secs(2)).unwrap();
|
|
||||||
let result = host.advance_frame();
|
|
||||||
let pending = host.get_memcard_state();
|
|
||||||
continue_io.send(Ok(())).unwrap();
|
|
||||||
settle(&mut host);
|
|
||||||
assert!(matches!(
|
|
||||||
result,
|
|
||||||
Ok(AdvanceFrameOutcome::FrameCompleted { logical_frame_index: 2, .. })
|
|
||||||
));
|
|
||||||
assert!(pending.io.active);
|
|
||||||
assert_eq!(pending.request.unwrap().operation, "commit");
|
|
||||||
assert!(!host.firmware.os.vm().debug_step_requested());
|
|
||||||
assert!(host.get_fault().is_none());
|
|
||||||
host.shutdown(TeardownPolicy::default()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bootstrap_wait_deadline_is_shared_across_wakes_and_reentry() {
|
|
||||||
use super::super::{BOOTSTRAP_IO_WAIT_QUANTUM, BootstrapIoWait};
|
|
||||||
let start = Instant::now();
|
|
||||||
let mut wait = BootstrapIoWait::default();
|
|
||||||
assert_eq!(wait.next_wait(start), Some(BOOTSTRAP_IO_WAIT_QUANTUM));
|
|
||||||
assert_eq!(
|
|
||||||
wait.next_wait(start + Duration::from_millis(1000)),
|
|
||||||
Some(BOOTSTRAP_IO_WAIT_QUANTUM)
|
|
||||||
);
|
|
||||||
assert_eq!(wait.next_wait(start + Duration::from_millis(1999)), Some(Duration::from_millis(1)));
|
|
||||||
assert_eq!(wait.next_wait(start + Duration::from_secs(2)), None);
|
|
||||||
assert_eq!(wait.next_wait(start + Duration::from_secs(3)), None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bootstrap_outer_timeout_preserves_native_snapshot_and_machine_state() {
|
|
||||||
use super::super::BootstrapIoWait;
|
|
||||||
let (_f, mut host, _entered, _release) = reopen_with_controlled_io(MemcardFixture::Read);
|
|
||||||
let before = host.get_state();
|
|
||||||
host.firmware.os.vm().set_paused(false);
|
|
||||||
host.firmware.os.vm().request_debug_step();
|
|
||||||
let mut wait = BootstrapIoWait { deadline: Some(Instant::now()) };
|
|
||||||
let error = host.wait_for_bootstrap_io(&mut wait, 0).unwrap_err();
|
|
||||||
let snapshot = host.get_memcard_state();
|
|
||||||
let HostError::Memcard(error) = error else { panic!("expected a native memcard error") };
|
|
||||||
assert_eq!(error.operation, "advance");
|
|
||||||
assert_eq!(error.reason, "restart_required");
|
|
||||||
assert_eq!(error.state, snapshot);
|
|
||||||
let after = host.get_state();
|
|
||||||
assert_eq!((after.pc, after.frame_index, after.tick_index), (before.pc, 0, 0));
|
|
||||||
assert_eq!(after.stack_top.to_vec(), before.stack_top.to_vec());
|
|
||||||
assert_eq!(after.status, HostExecutionStatus::Paused);
|
|
||||||
assert!(!host.firmware.os.vm().debug_step_requested());
|
|
||||||
assert!(host.get_fault().is_none());
|
|
||||||
host.shutdown(TeardownPolicy::default()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn failed_bootstrap_io_returns_native_media_action_without_guest_fault() {
|
|
||||||
let (_f, mut host, entered, continue_io) =
|
|
||||||
reopen_with_controlled_io(MemcardFixture::ReadWithPresentation);
|
|
||||||
let controller = std::thread::spawn(move || {
|
|
||||||
entered.recv_timeout(Duration::from_secs(2)).unwrap();
|
|
||||||
continue_io.send(Err(MemcardIoError::Unavailable)).unwrap();
|
|
||||||
});
|
|
||||||
let result = host.advance_frame();
|
|
||||||
controller.join().unwrap();
|
|
||||||
assert_eq!(result.unwrap(), AdvanceFrameOutcome::ActionRequired);
|
|
||||||
let state = host.get_memcard_state();
|
|
||||||
assert!(state.recovery.removal_pending);
|
|
||||||
assert!(!state.io.active);
|
|
||||||
assert_eq!(host.get_state().frame_index, 0);
|
|
||||||
assert_eq!(host.get_state().status, HostExecutionStatus::Paused);
|
|
||||||
assert!(!host.firmware.os.vm().debug_step_requested());
|
|
||||||
assert!(host.get_fault().is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn queued_bootstrap_publication_can_progress_without_moving_guest_during_poll() {
|
|
||||||
use prometeu_system::AsyncWorkJobOutcome;
|
|
||||||
let (_f, mut host, entered, continue_io) =
|
|
||||||
reopen_with_controlled_io(MemcardFixture::ReadWithPresentation);
|
|
||||||
let (busy_tx, busy) = mpsc::channel();
|
|
||||||
let (unblock, unblock_rx) = mpsc::channel();
|
|
||||||
host.firmware
|
|
||||||
.os
|
|
||||||
.memcard()
|
|
||||||
.lane
|
|
||||||
.submit_memcard_commit_write(move |_| {
|
|
||||||
busy_tx.send(()).unwrap();
|
|
||||||
let _ = unblock_rx.recv_timeout(Duration::from_secs(3));
|
|
||||||
AsyncWorkJobOutcome::Completed
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
busy.recv_timeout(Duration::from_secs(2)).unwrap();
|
|
||||||
for _ in 0..10 {
|
|
||||||
host.firmware.os.vm().set_paused(false);
|
|
||||||
host.firmware.os.vm().request_debug_step();
|
|
||||||
host.tick_once();
|
|
||||||
if host.bootstrap_io_pending() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let pending = host.bootstrap_io_pending();
|
|
||||||
let queued = host.get_memcard_state();
|
|
||||||
let before = host.get_state();
|
|
||||||
host.poll_memcard();
|
|
||||||
let after = host.get_state();
|
|
||||||
let (finished_tx, finished) = mpsc::channel();
|
|
||||||
let controller = std::thread::spawn(move || {
|
|
||||||
let premature = finished.recv_timeout(Duration::from_millis(300)).is_ok();
|
|
||||||
let _ = unblock.send(());
|
|
||||||
entered.recv_timeout(Duration::from_secs(2)).unwrap();
|
|
||||||
let _ = continue_io.send(Ok(()));
|
|
||||||
premature
|
|
||||||
});
|
|
||||||
let result = host.advance_frame();
|
|
||||||
let _ = finished_tx.send(());
|
|
||||||
let premature = controller.join().unwrap();
|
|
||||||
assert!(pending);
|
|
||||||
assert!(!premature, "advance returned while its publication was queued: {result:?}");
|
|
||||||
assert_eq!(queued.io.phase, "queued");
|
|
||||||
assert_eq!(
|
|
||||||
(before.pc, before.frame_index, before.tick_index),
|
|
||||||
(after.pc, after.frame_index, after.tick_index)
|
|
||||||
);
|
|
||||||
assert_eq!(before.stack_top.to_vec(), after.stack_top.to_vec());
|
|
||||||
assert!(matches!(
|
|
||||||
result,
|
|
||||||
Ok(AdvanceFrameOutcome::FrameCompleted { logical_frame_index: 1, .. })
|
|
||||||
));
|
|
||||||
host.shutdown(TeardownPolicy::default()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Fixture {
|
|
||||||
root: PathBuf,
|
|
||||||
image: PathBuf,
|
|
||||||
cart: PathBuf,
|
|
||||||
}
|
|
||||||
impl Fixture {
|
|
||||||
fn new(kind: MemcardFixture) -> Self {
|
|
||||||
static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
|
||||||
let root = std::env::temp_dir().join(format!(
|
|
||||||
"native-memcard-{}-{}-{}",
|
|
||||||
std::process::id(),
|
|
||||||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(),
|
|
||||||
COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
|
||||||
));
|
|
||||||
let cart = root.join("cart");
|
|
||||||
fs::create_dir_all(&cart).unwrap();
|
|
||||||
fs::write(cart.join("program.pbx"), memcard_program(kind)).unwrap();
|
|
||||||
fs::write(cart.join("manifest.json"),r#"{"magic":"PMTU","cartridge_version":1,"app_id":42,"title":"Native memcard","app_version":"1","app_mode":"Game","capabilities":["memcard"],"memcard_slots":1}"#).unwrap();
|
|
||||||
let image = root.join("save.pmc");
|
|
||||||
Self { root, image, cart }
|
|
||||||
}
|
|
||||||
fn mount(&self, host: &mut HeadlessHost) {
|
|
||||||
host.mount_memcard(MemcardMount {
|
|
||||||
path: self.image.to_string_lossy().into_owned(),
|
|
||||||
action: MediaAction::Create,
|
|
||||||
access: MediaAccess::ReadWrite,
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
host.boot_with_memcard_choice(self.cart.to_string_lossy(), Choice::Persistent).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Drop for Fixture {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
let _ = fs::remove_dir_all(&self.root);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn settle(host: &mut HeadlessHost) {
|
|
||||||
let deadline = Instant::now() + Duration::from_secs(3);
|
|
||||||
loop {
|
|
||||||
host.poll_memcard();
|
|
||||||
if !host.get_memcard_state().io.active {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
assert!(Instant::now() < deadline, "IO did not cease");
|
|
||||||
let lane = host.firmware.os.memcard().lane.clone();
|
|
||||||
lane.wait_for_progress(lane.progress_stamp(), Duration::from_millis(5));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn pause_for_media(host: &mut HeadlessHost) {
|
|
||||||
let task = host.firmware.os.lifecycle().resident_game_task().unwrap();
|
|
||||||
host.firmware.os.lifecycle().suspend_task(task).unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
host.firmware.os.lifecycle().task_state(task),
|
|
||||||
Some(prometeu_system::task::TaskState::Suspended)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn same_media_recovery_rejects_replacement_and_preserves_ram_continuation() {
|
|
||||||
let f = Fixture::new(MemcardFixture::Dirty);
|
|
||||||
let mut host = HeadlessHost::new();
|
|
||||||
f.mount(&mut host);
|
|
||||||
assert!(matches!(host.advance_frame().unwrap(), AdvanceFrameOutcome::FrameCompleted { .. }));
|
|
||||||
settle(&mut host);
|
|
||||||
let context = host.firmware.os.memcard().session.context();
|
|
||||||
host.firmware.os.memcard().session.notify_removal();
|
|
||||||
pause_for_media(&mut host);
|
|
||||||
assert_eq!(host.get_state().status, HostExecutionStatus::Paused);
|
|
||||||
assert_eq!(host.advance_frame().unwrap(), AdvanceFrameOutcome::ActionRequired);
|
|
||||||
let old = f.root.join("old.pmc");
|
|
||||||
fs::rename(&f.image, &old).unwrap();
|
|
||||||
let replacement = FileMemcard::create(&f.image, IMAGE_BYTES, |io, id| {
|
|
||||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
drop(replacement);
|
|
||||||
let error = host.reconcile_memcard().unwrap_err();
|
|
||||||
assert!(matches!(error,HostError::Memcard(e) if e.reason=="identity_mismatch"));
|
|
||||||
let mut bytes = [0; 4];
|
|
||||||
host.firmware.os.memcard().session.read(context, 0, 0, &mut bytes).unwrap();
|
|
||||||
assert_eq!(bytes, [0, 127, 128, 255]);
|
|
||||||
fs::rename(&f.image, f.root.join("other.pmc")).unwrap();
|
|
||||||
fs::rename(&old, &f.image).unwrap();
|
|
||||||
let restored = host.reconcile_memcard().unwrap();
|
|
||||||
assert!(restored.dirty);
|
|
||||||
assert!(!restored.recovery.reconciliation_required);
|
|
||||||
assert_eq!(host.firmware.os.memcard().session.context(), context);
|
|
||||||
assert!(matches!(host.advance_frame().unwrap(), AdvanceFrameOutcome::FrameCompleted { .. }));
|
|
||||||
assert_eq!(host.get_state().stack_top.last(), Some(&prometeu_bytecode::Value::Int64(301)));
|
|
||||||
host.shutdown(TeardownPolicy { discard_unsaved: true, ..Default::default() }).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn explicit_continue_in_ram_preserves_dirty_snapshot_and_uses_no_new_boot() {
|
|
||||||
let f = Fixture::new(MemcardFixture::Dirty);
|
|
||||||
let mut host = HeadlessHost::new();
|
|
||||||
f.mount(&mut host);
|
|
||||||
host.advance_frame().unwrap();
|
|
||||||
settle(&mut host);
|
|
||||||
let context = host.firmware.os.memcard().session.context();
|
|
||||||
host.firmware.os.memcard().session.notify_removal();
|
|
||||||
pause_for_media(&mut host);
|
|
||||||
let ram = host.continue_memcard_in_memory().unwrap();
|
|
||||||
assert_eq!(ram.session.mode, Some("memory_only"));
|
|
||||||
assert!(ram.dirty);
|
|
||||||
assert_eq!(host.firmware.os.memcard().session.context(), context);
|
|
||||||
host.advance_frame().unwrap();
|
|
||||||
host.shutdown(TeardownPolicy { discard_unsaved: true, ..Default::default() }).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct GateStore {
|
|
||||||
file: FileMemcard,
|
|
||||||
inside_publication: bool,
|
|
||||||
payload_seen: bool,
|
|
||||||
fired: bool,
|
|
||||||
entered: mpsc::Sender<()>,
|
|
||||||
continue_io: mpsc::Receiver<()>,
|
|
||||||
}
|
|
||||||
impl MemcardStorage for GateStore {
|
|
||||||
fn len(&self) -> Result<u64, MemcardIoError> {
|
|
||||||
self.file.len()
|
|
||||||
}
|
|
||||||
fn read_only(&self) -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
fn read_at(&mut self, o: u64, b: &mut [u8]) -> Result<(), MemcardIoError> {
|
|
||||||
self.file.read_at(o, b)
|
|
||||||
}
|
|
||||||
fn sync(&mut self) -> Result<(), MemcardIoError> {
|
|
||||||
self.file.sync()
|
|
||||||
}
|
|
||||||
fn write_at(&mut self, o: u64, b: &[u8]) -> Result<(), MemcardIoError> {
|
|
||||||
let payload = o >= prometeu_system::memcard::image::DATA_OFFSET;
|
|
||||||
self.payload_seen |= payload;
|
|
||||||
let publication = self.payload_seen && (o == 8192 || o == 12288);
|
|
||||||
if !self.fired && if self.inside_publication { publication } else { payload } {
|
|
||||||
self.fired = true;
|
|
||||||
if self.inside_publication {
|
|
||||||
self.file.write_at(o, b)?;
|
|
||||||
}
|
|
||||||
self.entered.send(()).unwrap();
|
|
||||||
self.continue_io.recv_timeout(Duration::from_secs(10)).unwrap();
|
|
||||||
return Err(MemcardIoError::IoError);
|
|
||||||
}
|
|
||||||
self.file.write_at(o, b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn unceased_io_retains_lock_and_terminal_result_survives_late_return_and_reconcile() {
|
|
||||||
for inside_publication in [false, true] {
|
|
||||||
let f = Fixture::new(MemcardFixture::Write);
|
|
||||||
let file = FileMemcard::create(&f.image, IMAGE_BYTES, |io, id| {
|
|
||||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let (entered_tx, entered) = mpsc::channel();
|
|
||||||
let (continue_io, release_rx) = mpsc::channel();
|
|
||||||
let mut host = HeadlessHost::new();
|
|
||||||
host.media.path = Some(f.image.clone());
|
|
||||||
host.media.access = Some(MediaAccess::ReadWrite);
|
|
||||||
host.install_memcard(Box::new(GateStore {
|
|
||||||
file,
|
|
||||||
inside_publication,
|
|
||||||
payload_seen: false,
|
|
||||||
fired: false,
|
|
||||||
entered: entered_tx,
|
|
||||||
continue_io: release_rx,
|
|
||||||
}))
|
|
||||||
.unwrap();
|
|
||||||
host.boot_with_memcard_choice(f.cart.to_string_lossy(), Choice::Persistent).unwrap();
|
|
||||||
host.advance_frame().unwrap();
|
|
||||||
entered.recv_timeout(Duration::from_secs(3)).unwrap();
|
|
||||||
host.firmware.os.memcard().poll(Instant::now() + Duration::from_secs(2));
|
|
||||||
let before = host.get_memcard_state();
|
|
||||||
assert!(before.io.active);
|
|
||||||
assert!(before.recovery.restart_required);
|
|
||||||
assert_eq!(
|
|
||||||
before.request.as_ref().unwrap().state,
|
|
||||||
if inside_publication { "indeterminate" } else { "failure" }
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
matches!(host.shutdown(TeardownPolicy{discard_unsaved:true,..Default::default()}),Err(HostError::Memcard(e)) if e.reason=="restart_required")
|
|
||||||
);
|
|
||||||
assert!(matches!(FileMemcard::open(&f.image, false), Err(MemcardIoError::Busy)));
|
|
||||||
continue_io.send(()).unwrap();
|
|
||||||
settle(&mut host);
|
|
||||||
let returned = host.get_memcard_state().request;
|
|
||||||
let original = before.request.as_ref().unwrap();
|
|
||||||
let late = returned.as_ref().unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
(&late.id, late.state, late.reason),
|
|
||||||
(&original.id, original.state, original.reason),
|
|
||||||
"late completion cannot rewrite the original result"
|
|
||||||
);
|
|
||||||
assert_eq!(late.storage_error, Some("io_error"));
|
|
||||||
pause_for_media(&mut host);
|
|
||||||
let restored = host.reconcile_memcard().unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
restored.request, returned,
|
|
||||||
"recovery must not consume or rewrite the guest result"
|
|
||||||
);
|
|
||||||
assert!(!restored.recovery.restart_required);
|
|
||||||
host.shutdown(TeardownPolicy { discard_unsaved: true, ..Default::default() }).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user