feat(runtime): implement shared transactional memcard with async saves
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
This commit is contained in:
@@ -0,0 +1,363 @@
|
||||
//! External SO/Hub choices prepare the snapshot before any game boot execution.
|
||||
use super::{
|
||||
MemcardService,
|
||||
image::*,
|
||||
session::Session,
|
||||
transaction::{Card, PublicationControl},
|
||||
};
|
||||
use prometeu_hal::memcard::MemcardStorage;
|
||||
use std::sync::{Arc, Mutex, mpsc};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
pub const DEFAULT_SNAPSHOT_BUDGET: usize = 5 * 1024 * 1024;
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Choice {
|
||||
Persistent,
|
||||
MemoryFresh,
|
||||
MemoryExisting,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PreparationState {
|
||||
Idle,
|
||||
Pending,
|
||||
Ready,
|
||||
Failed(Error),
|
||||
}
|
||||
enum Prepared {
|
||||
Card(Result<Card>),
|
||||
Session(Session, Result<()>),
|
||||
Replacement(Session, Result<()>),
|
||||
Maintenance(Card, Result<()>),
|
||||
}
|
||||
pub struct Admission {
|
||||
installed: Option<Card>,
|
||||
receiver: Option<mpsc::Receiver<Prepared>>,
|
||||
admitted: Option<Instant>,
|
||||
control: Option<Arc<PublicationControl>>,
|
||||
pub state: PreparationState,
|
||||
pub snapshot_budget: usize,
|
||||
faulted: bool,
|
||||
}
|
||||
impl Default for Admission {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
installed: None,
|
||||
receiver: None,
|
||||
admitted: None,
|
||||
control: None,
|
||||
state: PreparationState::Idle,
|
||||
snapshot_budget: DEFAULT_SNAPSHOT_BUDGET,
|
||||
faulted: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl MemcardService {
|
||||
pub fn host_faulted(&self) -> bool {
|
||||
self.admission.faulted || self.session.host_faulted()
|
||||
}
|
||||
pub fn card_installed(&self) -> bool {
|
||||
self.admission.installed.is_some()
|
||||
}
|
||||
pub fn installed_health(&self) -> Option<(bool, bool, bool)> {
|
||||
self.admission.installed.as_ref().map(|card| {
|
||||
(card.recovered, card.degraded, card.write_blocked || card.storage_read_only())
|
||||
})
|
||||
}
|
||||
pub fn card_has_game(&self, app_id: u32) -> bool {
|
||||
self.admission
|
||||
.installed
|
||||
.as_ref()
|
||||
.is_some_and(|c| c.catalog.games.iter().any(|g| g.app_id == app_id))
|
||||
}
|
||||
pub fn uninstall_card(&mut self) -> Result<()> {
|
||||
if self.session.mode().is_some()
|
||||
|| self.session.has_in_flight()
|
||||
|| self.admission.receiver.is_some()
|
||||
{
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
self.admission.installed = None;
|
||||
self.admission.state = PreparationState::Idle;
|
||||
Ok(())
|
||||
}
|
||||
pub fn mount_card(&mut self, storage: Box<dyn MemcardStorage>) -> Result<()> {
|
||||
self.mount_card_with(move || Ok(storage))
|
||||
}
|
||||
pub fn mount_card_with(
|
||||
&mut self,
|
||||
open: impl FnOnce() -> Result<Box<dyn MemcardStorage>> + Send + 'static,
|
||||
) -> Result<()> {
|
||||
if self.admission.installed.is_some()
|
||||
|| self.admission.receiver.is_some()
|
||||
|| self.session.mode().is_some()
|
||||
|| self.session.has_in_flight()
|
||||
{
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
if self.admission.faulted {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
let (tx, rx) = mpsc::channel();
|
||||
self.lane
|
||||
.submit_memcard_commit_write(move |_| {
|
||||
let _ = tx.send(Prepared::Card(open().and_then(Card::mount)));
|
||||
crate::AsyncWorkJobOutcome::Completed
|
||||
})
|
||||
.map_err(|_| Error::InvalidState)?;
|
||||
self.admission.receiver = Some(rx);
|
||||
self.admission.admitted = Some(Instant::now());
|
||||
self.admission.state = PreparationState::Pending;
|
||||
Ok(())
|
||||
}
|
||||
pub fn choose_for_game(
|
||||
&mut self,
|
||||
app_id: u32,
|
||||
count: usize,
|
||||
choice: Choice,
|
||||
created_at: u64,
|
||||
) -> Result<()> {
|
||||
if self.admission.receiver.is_some()
|
||||
|| self.session.mode().is_some()
|
||||
|| self.session.has_in_flight()
|
||||
{
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
if self.admission.faulted {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
if choice != Choice::MemoryFresh && self.admission.installed.is_none() {
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
if Session::required_bytes(count)? > self.admission.snapshot_budget {
|
||||
return Err(Error::OutOfMemory);
|
||||
}
|
||||
if let Some(card) = &self.admission.installed {
|
||||
let owned =
|
||||
card.catalog.games.iter().find(|g| g.app_id == app_id).map_or(0, |g| g.slots.len());
|
||||
if choice == Choice::Persistent {
|
||||
if card.storage_read_only() || card.write_blocked {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
if count.saturating_sub(owned)
|
||||
> card.current().slots.iter().filter(|s| !s.assigned).count()
|
||||
{
|
||||
return Err(Error::NoSpace);
|
||||
}
|
||||
}
|
||||
if choice == Choice::MemoryExisting && owned == 0 {
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
}
|
||||
let handoff = Arc::new(Mutex::new(self.admission.installed.take()));
|
||||
let worker_handoff = handoff.clone();
|
||||
let control = Arc::new(PublicationControl::default());
|
||||
let worker_control = control.clone();
|
||||
let budget = self.admission.snapshot_budget;
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let submitted = self.lane.submit_memcard_commit_write(move |_| {
|
||||
let mut card = worker_handoff.lock().unwrap().take();
|
||||
let mut session = Session::default();
|
||||
let result = (|| {
|
||||
if worker_control.canceled() {
|
||||
session.retain_idle_card_opt(card.take());
|
||||
return Err(Error::Canceled);
|
||||
}
|
||||
match choice {
|
||||
Choice::MemoryFresh => {
|
||||
session.start_memory_with_card(app_id, count, budget, card.take())?;
|
||||
}
|
||||
Choice::MemoryExisting => {
|
||||
session.load_card_memory(card.take().unwrap(), app_id, count, budget)?;
|
||||
}
|
||||
Choice::Persistent => {
|
||||
let mut card = card.take().unwrap();
|
||||
if card.storage_read_only() || card.write_blocked {
|
||||
session.retain_idle_card(card);
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
if card.catalog.games.iter().any(|g| g.app_id == app_id) {
|
||||
if let Err(e) = card.grow(app_id, count, &worker_control) {
|
||||
session.retain_idle_card(card);
|
||||
return Err(e);
|
||||
}
|
||||
session.load_card(card, app_id, count, budget)?;
|
||||
} else {
|
||||
session.prepare_new_card(card, app_id, count, created_at, budget)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
session.begin_boot();
|
||||
Ok(())
|
||||
})();
|
||||
let _ = tx.send(Prepared::Session(session, result));
|
||||
crate::AsyncWorkJobOutcome::Completed
|
||||
});
|
||||
if submitted.is_err() {
|
||||
self.admission.installed = handoff.lock().unwrap().take();
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
self.admission.control = Some(control);
|
||||
self.admission.receiver = Some(rx);
|
||||
self.admission.admitted = Some(Instant::now());
|
||||
self.admission.state = PreparationState::Pending;
|
||||
Ok(())
|
||||
}
|
||||
/// Whole-game removal is only available outside a resident game session.
|
||||
pub fn remove_game(&mut self, app_id: u32) -> Result<()> {
|
||||
self.maintain_card(move |card, control| card.remove_game(app_id, control))
|
||||
}
|
||||
pub fn revalidate_card(&mut self) -> Result<()> {
|
||||
self.maintain_card(|card, control| card.revalidate(control))
|
||||
}
|
||||
pub fn reintegrate_block(&mut self, block: u8) -> Result<()> {
|
||||
self.maintain_card(move |card, control| card.reintegrate(block, control))
|
||||
}
|
||||
fn maintain_card(
|
||||
&mut self,
|
||||
operation: impl FnOnce(&mut Card, &PublicationControl) -> Result<()> + Send + 'static,
|
||||
) -> Result<()> {
|
||||
if self.admission.receiver.is_some()
|
||||
|| self.session.mode().is_some()
|
||||
|| self.session.has_in_flight()
|
||||
{
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
if self.admission.faulted {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
let card = self.admission.installed.take().ok_or(Error::InvalidState)?;
|
||||
let handoff = Arc::new(Mutex::new(Some(card)));
|
||||
let worker = handoff.clone();
|
||||
let control = Arc::new(PublicationControl::default());
|
||||
let worker_control = control.clone();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
if self
|
||||
.lane
|
||||
.submit_memcard_commit_write(move |_| {
|
||||
let mut card = worker.lock().unwrap().take().unwrap();
|
||||
let result = operation(&mut card, &worker_control);
|
||||
let _ = tx.send(Prepared::Maintenance(card, result));
|
||||
crate::AsyncWorkJobOutcome::Completed
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
self.admission.installed = handoff.lock().unwrap().take();
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
self.admission.control = Some(control);
|
||||
self.admission.receiver = Some(rx);
|
||||
self.admission.admitted = Some(Instant::now());
|
||||
self.admission.state = PreparationState::Pending;
|
||||
Ok(())
|
||||
}
|
||||
/// Caller must suspend gameplay and obtain the player's keep-RAM/load-existing choice.
|
||||
/// Once admitted, the resident continuation must wait for `PreparationState::Ready`.
|
||||
pub fn replace_card(
|
||||
&mut self,
|
||||
storage: Box<dyn MemcardStorage>,
|
||||
load_existing: bool,
|
||||
) -> Result<()> {
|
||||
if self.admission.receiver.is_some()
|
||||
|| self.session.has_in_flight()
|
||||
|| self.session.is_boot_active()
|
||||
{
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
if self.admission.faulted {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
let handoff = Arc::new(Mutex::new(Some(std::mem::take(&mut self.session))));
|
||||
let worker = handoff.clone();
|
||||
let control = Arc::new(PublicationControl::default());
|
||||
let worker_control = control.clone();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
if self
|
||||
.lane
|
||||
.submit_memcard_commit_write(move |_| {
|
||||
let mut session = worker.lock().unwrap().take().unwrap();
|
||||
let result = session.replace_storage(storage, load_existing, &worker_control);
|
||||
let _ = tx.send(Prepared::Replacement(session, result));
|
||||
crate::AsyncWorkJobOutcome::Completed
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
self.session = handoff.lock().unwrap().take().unwrap();
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
self.admission.control = Some(control);
|
||||
self.admission.receiver = Some(rx);
|
||||
self.admission.admitted = Some(Instant::now());
|
||||
self.admission.state = PreparationState::Pending;
|
||||
Ok(())
|
||||
}
|
||||
pub fn poll(&mut self, now: Instant) {
|
||||
self.session.poll(now);
|
||||
if let Some(rx) = &self.admission.receiver {
|
||||
match rx.try_recv() {
|
||||
Ok(prepared) => {
|
||||
if !self.admission.faulted {
|
||||
self.admission.state = match prepared {
|
||||
Prepared::Card(Ok(card)) => {
|
||||
self.admission.installed = Some(card);
|
||||
PreparationState::Ready
|
||||
}
|
||||
Prepared::Maintenance(card, result) => {
|
||||
self.admission.installed = Some(card);
|
||||
match result {
|
||||
Ok(()) => PreparationState::Ready,
|
||||
Err(e) => PreparationState::Failed(e),
|
||||
}
|
||||
}
|
||||
Prepared::Replacement(session, result) => {
|
||||
self.session = session;
|
||||
match result {
|
||||
Ok(()) => PreparationState::Ready,
|
||||
Err(e) => PreparationState::Failed(e),
|
||||
}
|
||||
}
|
||||
Prepared::Session(session, Ok(())) => {
|
||||
self.session = session;
|
||||
PreparationState::Ready
|
||||
}
|
||||
Prepared::Card(Err(e)) => PreparationState::Failed(e),
|
||||
Prepared::Session(mut session, Err(e)) => {
|
||||
session.end_session();
|
||||
self.admission.installed = session.take_idle_card().ok().flatten();
|
||||
PreparationState::Failed(e)
|
||||
}
|
||||
};
|
||||
} else if let Prepared::Replacement(session, _) = prepared {
|
||||
self.session = session;
|
||||
}
|
||||
self.admission.receiver = None;
|
||||
self.admission.control = None;
|
||||
}
|
||||
Err(mpsc::TryRecvError::Disconnected) => {
|
||||
self.admission.state = PreparationState::Failed(Error::InvalidState);
|
||||
self.admission.receiver = None;
|
||||
}
|
||||
Err(mpsc::TryRecvError::Empty) => {
|
||||
if self.admission.admitted.is_some_and(|at| {
|
||||
now.saturating_duration_since(at) >= Duration::from_millis(500)
|
||||
}) && let Some(control) = &self.admission.control
|
||||
{
|
||||
control.cancel();
|
||||
}
|
||||
if self.admission.admitted.is_some_and(|at| {
|
||||
now.saturating_duration_since(at) >= Duration::from_secs(1)
|
||||
}) {
|
||||
self.admission.faulted = true;
|
||||
self.admission.state = PreparationState::Failed(Error::Indeterminate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.session.mode().is_none()
|
||||
&& !self.session.has_in_flight()
|
||||
&& self.admission.installed.is_none()
|
||||
&& let Ok(card) = self.session.take_idle_card()
|
||||
{
|
||||
self.admission.installed = card;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
//! Guest mem ABI. Only bounded copies cross the VM/native ownership boundary.
|
||||
use super::{
|
||||
MemcardService,
|
||||
image::Error,
|
||||
session::{Mode, RequestReason, RequestState},
|
||||
};
|
||||
use prometeu_bytecode::{HeapRef, TRAP_OOB, TRAP_TYPE, Value};
|
||||
use prometeu_hal::{HostReturn, expect_int, syscalls::Syscall, vm_fault::VmFault};
|
||||
use std::time::Instant;
|
||||
fn index(args: &[Value], n: usize) -> Result<usize, VmFault> {
|
||||
usize::try_from(expect_int(args, n)?)
|
||||
.map_err(|_| VmFault::Trap(TRAP_OOB, "Negative memcard range".into()))
|
||||
}
|
||||
fn opaque(args: &[Value], n: usize) -> Result<u64, VmFault> {
|
||||
u64::try_from(expect_int(args, n)?)
|
||||
.map_err(|_| VmFault::Trap(TRAP_OOB, "Negative memcard identity".into()))
|
||||
}
|
||||
fn handle(args: &[Value], n: usize) -> Result<HeapRef, VmFault> {
|
||||
match args.get(n) {
|
||||
Some(Value::HeapRef(h)) => Ok(*h),
|
||||
_ => Err(VmFault::Trap(TRAP_TYPE, "Expected Array<Int32>".into())),
|
||||
}
|
||||
}
|
||||
fn text(args: &[Value], n: usize) -> Result<&str, VmFault> {
|
||||
match args.get(n) {
|
||||
Some(Value::String(s)) => Ok(s),
|
||||
_ => Err(VmFault::Trap(TRAP_TYPE, "Expected String".into())),
|
||||
}
|
||||
}
|
||||
pub fn status(e: Error) -> i64 {
|
||||
use prometeu_hal::memcard::MemcardIoError as I;
|
||||
match e {
|
||||
Error::NoSpace | Error::Io(I::NoSpace) => 3,
|
||||
Error::AccessDenied => 4,
|
||||
Error::Corrupt => 5,
|
||||
Error::Indeterminate => 6,
|
||||
Error::Io(I::Unavailable) => 7,
|
||||
Error::InvalidState | Error::Canceled => 8,
|
||||
Error::Busy => 9,
|
||||
Error::ReadOnly | Error::CounterExhausted | Error::Io(I::ReadOnly) => 10,
|
||||
Error::InvalidRange | Error::Io(I::InvalidRange) => 11,
|
||||
Error::Unsupported | Error::Io(I::Unsupported) => 12,
|
||||
Error::Io(I::IoError) => 13,
|
||||
Error::OutOfMemory => 16,
|
||||
}
|
||||
}
|
||||
fn error(ret: &mut HostReturn, syscall: Syscall, e: Error) {
|
||||
ret.push_int(status(e));
|
||||
for _ in 1..syscall.results_count() {
|
||||
ret.push_null();
|
||||
}
|
||||
}
|
||||
impl MemcardService {
|
||||
pub fn guest(
|
||||
&mut self,
|
||||
syscall: Syscall,
|
||||
args: &[Value],
|
||||
ret: &mut HostReturn,
|
||||
app_id: u32,
|
||||
timestamp: u64,
|
||||
game_profile: bool,
|
||||
) -> Result<(), VmFault> {
|
||||
let authorized = game_profile && self.session.app_id() == Some(app_id);
|
||||
if syscall == Syscall::MemBootstrap {
|
||||
if !authorized || !self.session.is_boot_active() {
|
||||
return Err(VmFault::Trap(
|
||||
TRAP_TYPE,
|
||||
"Memcard snapshot is not admitted for bootstrap".into(),
|
||||
));
|
||||
}
|
||||
ret.push_int(self.session.context() as i64);
|
||||
return Ok(());
|
||||
}
|
||||
let context = opaque(args, 0)?;
|
||||
if !authorized || context != self.session.context() {
|
||||
error(ret, syscall, Error::AccessDenied);
|
||||
return Ok(());
|
||||
}
|
||||
match syscall {
|
||||
Syscall::MemSlotCount => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(self.session.count() as i64);
|
||||
}
|
||||
Syscall::MemSlotStat => match self.session.descriptor(context, index(args, 1)?) {
|
||||
Ok((d, used, dirty)) => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(i64::from(used > 0));
|
||||
ret.push_int(used as i64);
|
||||
ret.push_int(d.generation as i64);
|
||||
ret.push_int(d.update_at as i64);
|
||||
ret.push_int(d.crc as i64);
|
||||
ret.push_bool(dirty);
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
},
|
||||
Syscall::MemSlotRead | Syscall::MemSlotWrite => {
|
||||
let local = index(args, 1)?;
|
||||
let offset = index(args, 2)?;
|
||||
let array = handle(args, 3)?;
|
||||
let array_offset = index(args, 4)?;
|
||||
let count = index(args, 5)?;
|
||||
if count > 4096 {
|
||||
return Err(VmFault::Trap(
|
||||
TRAP_OOB,
|
||||
"Memcard transfer exceeds 4096 bytes".into(),
|
||||
));
|
||||
}
|
||||
if let Err(e) = self.session.descriptor(context, local) {
|
||||
error(ret, syscall, e);
|
||||
return Ok(());
|
||||
}
|
||||
if offset.checked_add(count).is_none_or(|end| end > 65536) {
|
||||
error(ret, syscall, Error::InvalidRange);
|
||||
return Ok(());
|
||||
}
|
||||
ret.byte_arrays()?.validate(array, array_offset, count)?;
|
||||
let mut bytes = [0; 4096];
|
||||
if syscall == Syscall::MemSlotRead {
|
||||
match self.session.read(context, local, offset, &mut bytes[..count]) {
|
||||
Ok(n) => {
|
||||
ret.byte_arrays()?.write(array, array_offset, &bytes[..n])?;
|
||||
ret.push_int(0);
|
||||
ret.push_int(n as i64)
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
}
|
||||
} else {
|
||||
ret.byte_arrays()?.read(array, array_offset, &mut bytes[..count])?;
|
||||
match self.session.write(context, local, offset, &bytes[..count]) {
|
||||
Ok(()) => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(count as i64)
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
}
|
||||
}
|
||||
}
|
||||
Syscall::MemSlotCommit => {
|
||||
match self.session.commit(
|
||||
context,
|
||||
index(args, 1)?,
|
||||
timestamp,
|
||||
Instant::now(),
|
||||
&self.lane,
|
||||
) {
|
||||
Ok(id) => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(id as i64)
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
}
|
||||
}
|
||||
Syscall::MemSlotClear => match self.session.clear(context, index(args, 1)?) {
|
||||
Ok(()) => ret.push_int(0),
|
||||
Err(e) => error(ret, syscall, e),
|
||||
},
|
||||
Syscall::MemQuery => match self.session.query(context, opaque(args, 1)?) {
|
||||
Ok(r) => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(match r.state {
|
||||
RequestState::Pending => 0,
|
||||
RequestState::Success => 1,
|
||||
RequestState::Failure => 2,
|
||||
RequestState::Indeterminate => 3,
|
||||
RequestState::MemoryOnly => 4,
|
||||
});
|
||||
ret.push_int(match r.reason {
|
||||
RequestReason::None => 0,
|
||||
RequestReason::Storage(e) => status(e),
|
||||
RequestReason::Timeout | RequestReason::RestartRequired => 15,
|
||||
});
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
},
|
||||
Syscall::MemRelease => match self.session.release_request(context, opaque(args, 1)?) {
|
||||
Ok(()) => ret.push_int(0),
|
||||
Err(e) => error(ret, syscall, e),
|
||||
},
|
||||
Syscall::MemInfo => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(i64::from(self.session.mode() == Some(Mode::MemoryOnly)));
|
||||
ret.push_bool(self.session.is_new());
|
||||
let (recovered, degraded, blocked) = self.session.health();
|
||||
ret.push_bool(recovered);
|
||||
ret.push_bool(degraded);
|
||||
ret.push_bool(blocked);
|
||||
}
|
||||
Syscall::MemMetadata => match self.session.presentation(context) {
|
||||
Ok(Some(g)) => {
|
||||
ret.push_int(0);
|
||||
ret.push_string(&g.title)?;
|
||||
ret.push_int(g.data_format_version as i64);
|
||||
ret.push_int(g.created_at as i64)
|
||||
}
|
||||
Ok(None) => {
|
||||
ret.push_int(0);
|
||||
ret.push_string("")?;
|
||||
ret.push_int(0);
|
||||
ret.push_int(timestamp as i64)
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
},
|
||||
Syscall::MemPresentation => {
|
||||
let title = text(args, 1)?;
|
||||
let version = u32::try_from(index(args, 2)?)
|
||||
.map_err(|_| VmFault::Trap(TRAP_OOB, "Invalid dataFormatVersion".into()))?;
|
||||
let frames = u8::try_from(index(args, 3)?)
|
||||
.map_err(|_| VmFault::Trap(TRAP_OOB, "Invalid icon frames".into()))?;
|
||||
let duration = u16::try_from(index(args, 4)?)
|
||||
.map_err(|_| VmFault::Trap(TRAP_OOB, "Invalid icon duration".into()))?;
|
||||
let array = handle(args, 5)?;
|
||||
let offset = index(args, 6)?;
|
||||
let count = index(args, 7)?;
|
||||
if count > 2096 || title.len() > 128 {
|
||||
error(ret, syscall, Error::InvalidRange);
|
||||
return Ok(());
|
||||
}
|
||||
let mut bytes = [0; 2096];
|
||||
ret.byte_arrays()?.read(array, offset, &mut bytes[..count])?;
|
||||
match self.session.set_presentation(
|
||||
context,
|
||||
title,
|
||||
version,
|
||||
frames,
|
||||
duration,
|
||||
&bytes[..count],
|
||||
timestamp,
|
||||
) {
|
||||
Ok(()) => ret.push_int(0),
|
||||
Err(e) => error(ret, syscall, e),
|
||||
}
|
||||
}
|
||||
Syscall::MemDiagnostic => {
|
||||
let message = text(args, 1)?;
|
||||
if message.len() > 1024 {
|
||||
error(ret, syscall, Error::InvalidRange)
|
||||
} else {
|
||||
self.diagnostic.clear();
|
||||
self.diagnostic.push_str(message);
|
||||
ret.push_int(0)
|
||||
}
|
||||
}
|
||||
Syscall::MemPublishPresentation => {
|
||||
match self.session.publish_presentation(context, &self.lane, Instant::now()) {
|
||||
Ok(id) => {
|
||||
ret.push_int(0);
|
||||
ret.push_int(id as i64)
|
||||
}
|
||||
Err(e) => error(ret, syscall, e),
|
||||
}
|
||||
}
|
||||
_ => return Err(VmFault::Trap(TRAP_TYPE, "Invalid memcard operation".into())),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
//! Fixed shared-card codec. All offsets and CRC coverage are defined in the save spec.
|
||||
use prometeu_hal::memcard::{MemcardIoError, MemcardStorage};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const SLOT_COUNT: usize = 64;
|
||||
pub const BLOCK_COUNT: usize = 68;
|
||||
pub const SLOT_BYTES: usize = 65_536;
|
||||
pub const PAGE_BYTES: usize = 4096;
|
||||
pub const CATALOG_BYTES: usize = 131_072;
|
||||
pub const ICON_BYTES: usize = 262_144;
|
||||
pub const CATALOG_OFFSETS: [u64; 2] = [16_384, 409_600];
|
||||
pub const ICON_OFFSETS: [u64; 2] = [147_456, 540_672];
|
||||
pub const MAP_OFFSETS: [u64; 2] = [8192, 12_288];
|
||||
pub const DATA_OFFSET: u64 = 802_816;
|
||||
pub const IMAGE_BYTES: u64 = DATA_OFFSET + (BLOCK_COUNT * SLOT_BYTES) as u64;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
Io(MemcardIoError),
|
||||
Corrupt,
|
||||
Unsupported,
|
||||
NoSpace,
|
||||
InvalidRange,
|
||||
CounterExhausted,
|
||||
ReadOnly,
|
||||
Indeterminate,
|
||||
Busy,
|
||||
Canceled,
|
||||
OutOfMemory,
|
||||
AccessDenied,
|
||||
InvalidState,
|
||||
}
|
||||
impl From<MemcardIoError> for Error {
|
||||
fn from(e: MemcardIoError) -> Self {
|
||||
Self::Io(e)
|
||||
}
|
||||
}
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub fn crc32c(bytes: &[u8]) -> u32 {
|
||||
crc_extend(!0, bytes) ^ !0
|
||||
}
|
||||
pub fn crc_extend(mut crc: u32, bytes: &[u8]) -> u32 {
|
||||
for &byte in bytes {
|
||||
crc ^= byte as u32;
|
||||
for _ in 0..8 {
|
||||
crc = (crc >> 1) ^ (0x82f6_3b78 & 0u32.wrapping_sub(crc & 1));
|
||||
}
|
||||
}
|
||||
crc
|
||||
}
|
||||
fn u32_at(b: &[u8], p: usize) -> u32 {
|
||||
u32::from_le_bytes(b[p..p + 4].try_into().unwrap())
|
||||
}
|
||||
fn u64_at(b: &[u8], p: usize) -> u64 {
|
||||
u64::from_le_bytes(b[p..p + 8].try_into().unwrap())
|
||||
}
|
||||
fn put32(b: &mut [u8], p: usize, n: u32) {
|
||||
b[p..p + 4].copy_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
fn put64(b: &mut [u8], p: usize, n: u64) {
|
||||
b[p..p + 8].copy_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
fn zero(b: &[u8]) -> Result<()> {
|
||||
if b.iter().any(|&v| v != 0) { Err(Error::Corrupt) } else { Ok(()) }
|
||||
}
|
||||
fn page_crc(b: &[u8; PAGE_BYTES]) -> Result<()> {
|
||||
if crc32c(&b[..4092]) != u32_at(b, 4092) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn seal(b: &mut [u8; PAGE_BYTES]) {
|
||||
let crc = crc32c(&b[..4092]);
|
||||
put32(b, 4092, crc);
|
||||
}
|
||||
|
||||
pub fn identity_page(id: [u8; 16]) -> [u8; PAGE_BYTES] {
|
||||
let mut b = [0; PAGE_BYTES];
|
||||
b[..8].copy_from_slice(b"PMCARD1\0");
|
||||
put32(&mut b, 8, 1);
|
||||
put32(&mut b, 12, IMAGE_BYTES as u32);
|
||||
b[16..32].copy_from_slice(&id);
|
||||
seal(&mut b);
|
||||
b
|
||||
}
|
||||
pub fn decode_identity(b: &[u8; PAGE_BYTES]) -> Result<[u8; 16]> {
|
||||
if &b[..8] != b"PMCARD1\0" || u32_at(b, 8) != 1 {
|
||||
return Err(Error::Unsupported);
|
||||
}
|
||||
page_crc(b)?;
|
||||
if u32_at(b, 12) != IMAGE_BYTES as u32 {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
zero(&b[32..4092])?;
|
||||
Ok(b[16..32].try_into().unwrap())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Icon {
|
||||
pub offset: u32,
|
||||
pub frames: u8,
|
||||
pub duration: u16,
|
||||
pub crc32c: u32,
|
||||
}
|
||||
impl Icon {
|
||||
pub fn length(&self) -> Result<usize> {
|
||||
if !(1..=4).contains(&self.frames)
|
||||
|| self.duration == 0
|
||||
|| (self.frames == 1 && self.duration != 1)
|
||||
{
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
Ok(48 + 512 * self.frames as usize)
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Game {
|
||||
pub app_id: u32,
|
||||
pub title: String,
|
||||
#[serde(rename = "dataFormatVersion")]
|
||||
pub data_format_version: u32,
|
||||
pub created_at: u64,
|
||||
pub slots: Vec<u8>,
|
||||
pub icon: Icon,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Catalog {
|
||||
#[serde(rename = "metadataVersion")]
|
||||
pub metadata_version: u32,
|
||||
pub games: Vec<Game>,
|
||||
}
|
||||
impl Default for Catalog {
|
||||
fn default() -> Self {
|
||||
Self { metadata_version: 1, games: Vec::new() }
|
||||
}
|
||||
}
|
||||
impl Catalog {
|
||||
pub fn validate(&self, icons_used: usize) -> Result<[bool; SLOT_COUNT]> {
|
||||
if self.metadata_version != 1 || self.games.len() > 64 || icons_used > ICON_BYTES {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
let mut assigned = [false; SLOT_COUNT];
|
||||
let mut spans = Vec::with_capacity(64);
|
||||
for (index, g) in self.games.iter().enumerate() {
|
||||
if g.title.trim().is_empty()
|
||||
|| g.title.len() > 128
|
||||
|| g.slots.is_empty()
|
||||
|| g.slots.len() > 64
|
||||
|| self.games[..index].iter().any(|prior| prior.app_id == g.app_id)
|
||||
{
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
for &slot in &g.slots {
|
||||
let used = assigned.get_mut(slot as usize).ok_or(Error::Corrupt)?;
|
||||
if *used {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
*used = true;
|
||||
}
|
||||
let start = g.icon.offset as usize;
|
||||
let end = start.checked_add(g.icon.length()?).ok_or(Error::Corrupt)?;
|
||||
if end > icons_used || spans.iter().any(|&(a, b)| start < b && a < end) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
spans.push((start, end));
|
||||
}
|
||||
Ok(assigned)
|
||||
}
|
||||
pub fn encode(&self, icons_used: usize) -> Result<Vec<u8>> {
|
||||
self.validate(icons_used)?;
|
||||
let b = serde_json::to_vec(self).map_err(|_| Error::Corrupt)?;
|
||||
if b.len() > CATALOG_BYTES {
|
||||
return Err(Error::NoSpace);
|
||||
}
|
||||
Ok(b)
|
||||
}
|
||||
pub fn decode(b: &[u8], icons_used: usize) -> Result<Self> {
|
||||
if b.len() > CATALOG_BYTES {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
// Bound nesting before serde; strings (including escaped quotes) do not count.
|
||||
let (mut depth, mut quoted, mut escaped) = (0u8, false, false);
|
||||
for &v in b {
|
||||
if quoted {
|
||||
if escaped {
|
||||
escaped = false;
|
||||
} else if v == b'\\' {
|
||||
escaped = true;
|
||||
} else if v == b'"' {
|
||||
quoted = false;
|
||||
}
|
||||
} else {
|
||||
match v {
|
||||
b'"' => quoted = true,
|
||||
b'[' | b'{' => {
|
||||
depth = depth.checked_add(1).ok_or(Error::Corrupt)?;
|
||||
if depth > 8 {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
}
|
||||
b']' | b'}' => depth = depth.checked_sub(1).ok_or(Error::Corrupt)?,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
let catalog: Self = serde_json::from_slice(b).map_err(|_| Error::Corrupt)?;
|
||||
catalog.validate(icons_used)?;
|
||||
Ok(catalog)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub struct Slot {
|
||||
pub assigned: bool,
|
||||
pub block: Option<u8>,
|
||||
pub used: u32,
|
||||
pub generation: u64,
|
||||
pub update_at: u64,
|
||||
pub crc: u32,
|
||||
}
|
||||
impl Slot {
|
||||
fn validate(&self) -> Result<()> {
|
||||
if !self.assigned && *self != Self::default() {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
if self.generation == 0 && (self.update_at != 0 || self.block.is_some()) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
match self.block {
|
||||
Some(id)
|
||||
if id as usize >= BLOCK_COUNT
|
||||
|| self.used == 0
|
||||
|| self.used as usize > SLOT_BYTES =>
|
||||
{
|
||||
Err(Error::Corrupt)
|
||||
}
|
||||
None if self.used != 0 || self.crc != 0 => Err(Error::Corrupt),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Map {
|
||||
pub card_id: [u8; 16],
|
||||
pub sequence: u64,
|
||||
pub public_copy: usize,
|
||||
pub catalog_len: u32,
|
||||
pub catalog_crc: u32,
|
||||
pub icons_len: u32,
|
||||
pub icons_crc: u32,
|
||||
pub order: [u8; BLOCK_COUNT],
|
||||
pub quarantine: [bool; BLOCK_COUNT],
|
||||
pub slots: [Slot; SLOT_COUNT],
|
||||
}
|
||||
impl Map {
|
||||
pub fn new(card_id: [u8; 16]) -> Self {
|
||||
Self {
|
||||
card_id,
|
||||
sequence: 1,
|
||||
public_copy: 0,
|
||||
catalog_len: 0,
|
||||
catalog_crc: 0,
|
||||
icons_len: 0,
|
||||
icons_crc: 0,
|
||||
order: std::array::from_fn(|i| i as u8),
|
||||
quarantine: [false; BLOCK_COUNT],
|
||||
slots: [Slot::default(); SLOT_COUNT],
|
||||
}
|
||||
}
|
||||
pub fn encode(&self) -> Result<[u8; PAGE_BYTES]> {
|
||||
if self.sequence == 0
|
||||
|| self.public_copy > 1
|
||||
|| self.catalog_len as usize > CATALOG_BYTES
|
||||
|| self.icons_len as usize > ICON_BYTES
|
||||
{
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
let mut seen = [false; BLOCK_COUNT];
|
||||
for &id in &self.order {
|
||||
let v = seen.get_mut(id as usize).ok_or(Error::Corrupt)?;
|
||||
if *v {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
*v = true;
|
||||
}
|
||||
let mut owned = [false; BLOCK_COUNT];
|
||||
let mut b = [0; PAGE_BYTES];
|
||||
b[..8].copy_from_slice(b"PMMAP1\0\0");
|
||||
put64(&mut b, 8, self.sequence);
|
||||
put32(&mut b, 16, self.public_copy as u32);
|
||||
put32(&mut b, 20, self.catalog_len);
|
||||
put32(&mut b, 24, self.catalog_crc);
|
||||
put32(&mut b, 28, self.icons_len);
|
||||
put32(&mut b, 32, self.icons_crc);
|
||||
b[36..52].copy_from_slice(&self.card_id);
|
||||
b[64..132].copy_from_slice(&self.order);
|
||||
for i in 0..BLOCK_COUNT {
|
||||
b[132 + i] = self.quarantine[i] as u8;
|
||||
}
|
||||
for (i, s) in self.slots.iter().enumerate() {
|
||||
s.validate()?;
|
||||
if let Some(id) = s.block {
|
||||
if owned[id as usize] || self.quarantine[id as usize] {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
owned[id as usize] = true;
|
||||
}
|
||||
let p = 256 + i * 40;
|
||||
b[p] = if !s.assigned {
|
||||
0
|
||||
} else if s.block.is_none() {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
b[p + 1] = s.block.unwrap_or(255);
|
||||
put32(&mut b, p + 4, s.used);
|
||||
put64(&mut b, p + 8, s.generation);
|
||||
put64(&mut b, p + 16, s.update_at);
|
||||
put32(&mut b, p + 24, s.crc);
|
||||
}
|
||||
seal(&mut b);
|
||||
Ok(b)
|
||||
}
|
||||
pub fn decode(b: &[u8; PAGE_BYTES], card_id: [u8; 16]) -> Result<Self> {
|
||||
page_crc(b)?;
|
||||
if &b[..8] != b"PMMAP1\0\0" || b[36..52] != card_id {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
zero(&b[52..64])?;
|
||||
zero(&b[200..256])?;
|
||||
zero(&b[2816..4092])?;
|
||||
let mut m = Self::new(card_id);
|
||||
m.sequence = u64_at(b, 8);
|
||||
m.public_copy = u32_at(b, 16) as usize;
|
||||
m.catalog_len = u32_at(b, 20);
|
||||
m.catalog_crc = u32_at(b, 24);
|
||||
m.icons_len = u32_at(b, 28);
|
||||
m.icons_crc = u32_at(b, 32);
|
||||
m.order.copy_from_slice(&b[64..132]);
|
||||
for i in 0..BLOCK_COUNT {
|
||||
if b[132 + i] > 1 {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
m.quarantine[i] = b[132 + i] != 0;
|
||||
}
|
||||
for i in 0..SLOT_COUNT {
|
||||
let p = 256 + i * 40;
|
||||
zero(&b[p + 2..p + 4])?;
|
||||
zero(&b[p + 28..p + 40])?;
|
||||
if b[p] > 2 || (b[p] != 2 && b[p + 1] != 255) || (b[p] == 2 && b[p + 1] == 255) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
m.slots[i] = Slot {
|
||||
assigned: b[p] != 0,
|
||||
block: if b[p] == 2 { Some(b[p + 1]) } else { None },
|
||||
used: u32_at(b, p + 4),
|
||||
generation: u64_at(b, p + 8),
|
||||
update_at: u64_at(b, p + 16),
|
||||
crc: u32_at(b, p + 24),
|
||||
};
|
||||
}
|
||||
if m.encode()? != *b {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
Ok(m)
|
||||
}
|
||||
pub fn game_update_at(&self, game: &Game) -> Option<u64> {
|
||||
game.slots
|
||||
.iter()
|
||||
.filter_map(|&i| self.slots.get(i as usize))
|
||||
.filter(|s| s.generation > 0)
|
||||
.map(|s| s.update_at)
|
||||
.max()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_region(io: &mut dyn MemcardStorage, offset: u64, out: &mut [u8]) -> Result<()> {
|
||||
for (i, b) in out.chunks_mut(SLOT_BYTES).enumerate() {
|
||||
io.read_at(offset + (i * SLOT_BYTES) as u64, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn write_region(io: &mut dyn MemcardStorage, offset: u64, bytes: &[u8]) -> Result<()> {
|
||||
for (i, b) in bytes.chunks(SLOT_BYTES).enumerate() {
|
||||
io.write_at(offset + (i * SLOT_BYTES) as u64, b)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn block_offset(id: u8) -> Result<u64> {
|
||||
if id as usize >= BLOCK_COUNT {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
Ok(DATA_OFFSET + id as u64 * SLOT_BYTES as u64)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn crc_vectors_and_identity_golden() {
|
||||
assert_eq!(crc32c(b""), 0);
|
||||
assert_eq!(crc32c(b"123456789"), 0xe3069283);
|
||||
let p = identity_page([7; 16]);
|
||||
assert_eq!(&p[..16], b"PMCARD1\0\x01\0\0\0\0\x40\x50\0");
|
||||
assert_eq!(decode_identity(&p), Ok([7; 16]));
|
||||
}
|
||||
#[test]
|
||||
fn descriptor_roundtrip_rejects_reserved_and_alias() {
|
||||
let mut m = Map::new([1; 16]);
|
||||
m.slots[2] =
|
||||
Slot { assigned: true, block: Some(3), used: 7, generation: 1, update_at: 0, crc: 12 };
|
||||
assert_eq!(Map::decode(&m.encode().unwrap(), [1; 16]), Ok(m.clone()));
|
||||
m.slots[4] = m.slots[2];
|
||||
assert_eq!(m.encode(), Err(Error::Corrupt));
|
||||
let mut b = Map::new([1; 16]).encode().unwrap();
|
||||
b[3000] = 1;
|
||||
seal(&mut b);
|
||||
assert_eq!(Map::decode(&b, [1; 16]), Err(Error::Corrupt));
|
||||
}
|
||||
#[test]
|
||||
fn closed_catalog_rejects_duplicates_and_deep_input() {
|
||||
for s in [
|
||||
r#"{"metadataVersion":1,"metadataVersion":1,"games":[]}"#,
|
||||
r#"{"metadataVersion":1,"games":[],"extra":0}"#,
|
||||
r#"[[[[[[[[[]]]]]]]]]"#,
|
||||
] {
|
||||
assert_eq!(Catalog::decode(s.as_bytes(), 0), Err(Error::Corrupt));
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn maximum_catalog_and_icons_fit_and_preserve_uint64() {
|
||||
let mut c = Catalog::default();
|
||||
for i in 0..64 {
|
||||
c.games.push(Game {
|
||||
app_id: u32::MAX - i,
|
||||
title: "\u{1}".repeat(127) + "a",
|
||||
data_format_version: u32::MAX,
|
||||
created_at: u64::MAX,
|
||||
slots: vec![i as u8],
|
||||
icon: Icon { offset: i * 2096, frames: 4, duration: u16::MAX, crc32c: u32::MAX },
|
||||
});
|
||||
}
|
||||
let b = c.encode(64 * 2096).unwrap();
|
||||
assert!(b.len() < CATALOG_BYTES);
|
||||
assert_eq!(Catalog::decode(&b, 64 * 2096), Ok(c));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,610 @@
|
||||
//! Worker-owned transaction engine. Main-lane callers exchange owned requests/results.
|
||||
use super::image::*;
|
||||
use prometeu_hal::memcard::MemcardStorage;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
|
||||
/// Publication entry and pre-publication cancellation share a single linearization point.
|
||||
#[derive(Default)]
|
||||
pub struct PublicationControl(AtomicU8);
|
||||
impl PublicationControl {
|
||||
pub fn cancel(&self) {
|
||||
self.0.fetch_or(2, Ordering::SeqCst);
|
||||
}
|
||||
pub fn canceled(&self) -> bool {
|
||||
self.0.load(Ordering::SeqCst) & 2 != 0
|
||||
}
|
||||
pub fn publication_attempted(&self) -> bool {
|
||||
self.0.load(Ordering::SeqCst) & 1 != 0
|
||||
}
|
||||
fn enter(&self) -> Result<()> {
|
||||
self.0
|
||||
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |state| {
|
||||
if state & 2 == 0 { Some(state | 1) } else { None }
|
||||
})
|
||||
.map(|_| ())
|
||||
.map_err(|_| Error::Canceled)
|
||||
}
|
||||
fn check(&self) -> Result<()> {
|
||||
if self.canceled() { Err(Error::Canceled) } else { Ok(()) }
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum DurableResult {
|
||||
Success,
|
||||
Failure,
|
||||
Indeterminate,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct CommitResult {
|
||||
pub durable: DurableResult,
|
||||
pub error: Option<Error>,
|
||||
pub sequence: u64,
|
||||
}
|
||||
|
||||
pub struct Card {
|
||||
io: Box<dyn MemcardStorage>,
|
||||
pub maps: [Option<Map>; 2],
|
||||
pub active: usize,
|
||||
pub catalog: Catalog,
|
||||
scratch: Box<[u8; SLOT_BYTES]>,
|
||||
pub recovered: bool,
|
||||
pub degraded: bool,
|
||||
pub uncertain: bool,
|
||||
pub(super) attempted_publication: Option<Map>,
|
||||
pub write_blocked: bool,
|
||||
known_quarantine: [bool; BLOCK_COUNT],
|
||||
}
|
||||
impl Card {
|
||||
pub fn format(io: &mut dyn MemcardStorage, id: [u8; 16]) -> Result<()> {
|
||||
if io.len()? != IMAGE_BYTES {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
let identity = identity_page(id);
|
||||
io.write_at(0, &identity)?;
|
||||
io.write_at(PAGE_BYTES as u64, &identity)?;
|
||||
let catalog = Catalog::default().encode(0)?;
|
||||
write_region(io, CATALOG_OFFSETS[0], &catalog)?;
|
||||
let mut map = Map::new(id);
|
||||
map.catalog_len = catalog.len() as u32;
|
||||
map.catalog_crc = crc32c(&catalog);
|
||||
let encoded = map.encode()?;
|
||||
io.write_at(MAP_OFFSETS[0], &encoded)?;
|
||||
io.write_at(MAP_OFFSETS[1], &encoded)?;
|
||||
io.sync()?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn mount(mut io: Box<dyn MemcardStorage>) -> Result<Self> {
|
||||
if io.len()? != IMAGE_BYTES {
|
||||
return Err(Error::Unsupported);
|
||||
}
|
||||
let mut page = [0; PAGE_BYTES];
|
||||
let mut identities = [None; 2];
|
||||
let mut recognized = false;
|
||||
for (i, item) in identities.iter_mut().enumerate() {
|
||||
io.read_at((i * PAGE_BYTES) as u64, &mut page)?;
|
||||
recognized |= &page[..8] == b"PMCARD1\0"
|
||||
&& u32::from_le_bytes(page[8..12].try_into().unwrap()) == 1;
|
||||
*item = decode_identity(&page).ok();
|
||||
}
|
||||
let id = identities[0].or(identities[1]).ok_or(if recognized {
|
||||
Error::Corrupt
|
||||
} else {
|
||||
Error::Unsupported
|
||||
})?;
|
||||
if identities.iter().flatten().any(|other| *other != id) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
let mut maps: [Option<Map>; 2] = [None, None];
|
||||
let mut unknown_metadata = false;
|
||||
for i in 0..2 {
|
||||
io.read_at(MAP_OFFSETS[i], &mut page)?;
|
||||
maps[i] = Map::decode(&page, id).ok();
|
||||
// A zero page is a verified retirement. An undecodable nonzero page
|
||||
// may have lost quarantine information; do not silently reuse its pool.
|
||||
unknown_metadata |= maps[i].is_none() && page.iter().any(|&byte| byte != 0);
|
||||
}
|
||||
if let (Some(a), Some(b)) = (&maps[0], &maps[1])
|
||||
&& a.sequence == b.sequence
|
||||
&& a != b
|
||||
{
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
let highest = maps.iter().flatten().map(|m| m.sequence).max().unwrap_or(0);
|
||||
let mut known = [false; BLOCK_COUNT];
|
||||
for m in maps.iter().flatten() {
|
||||
for (i, &bad) in m.quarantine.iter().enumerate() {
|
||||
known[i] |= bad;
|
||||
}
|
||||
}
|
||||
let mut scratch = Box::new([0; SLOT_BYTES]);
|
||||
let mut catalogs: [Option<Catalog>; 2] = [None, None];
|
||||
let mut lost_map = false;
|
||||
for i in 0..2 {
|
||||
if let Some(m) = &maps[i] {
|
||||
match Self::validate_map(io.as_mut(), m, scratch.as_mut()) {
|
||||
Ok(c) => catalogs[i] = Some(c),
|
||||
Err(Error::Corrupt) => {
|
||||
maps[i] = None;
|
||||
lost_map = true
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
let active = match (&maps[0], &maps[1]) {
|
||||
(Some(a), Some(b)) => usize::from(b.sequence > a.sequence),
|
||||
(Some(_), None) => 0,
|
||||
(None, Some(_)) => 1,
|
||||
_ => return Err(Error::Corrupt),
|
||||
};
|
||||
let m = maps[active].as_ref().unwrap();
|
||||
let recovered = m.sequence < highest || lost_map || unknown_metadata;
|
||||
let write_blocked =
|
||||
unknown_metadata || known.iter().zip(m.quarantine.iter()).any(|(&a, &b)| a && !b);
|
||||
let degraded = identities.iter().any(Option::is_none)
|
||||
|| maps.iter().any(Option::is_none)
|
||||
|| known.iter().any(|v| *v);
|
||||
Ok(Self {
|
||||
io,
|
||||
maps,
|
||||
active,
|
||||
catalog: catalogs[active].take().unwrap(),
|
||||
scratch,
|
||||
recovered,
|
||||
degraded,
|
||||
uncertain: false,
|
||||
attempted_publication: None,
|
||||
write_blocked,
|
||||
known_quarantine: known,
|
||||
})
|
||||
}
|
||||
fn validate_map(
|
||||
io: &mut dyn MemcardStorage,
|
||||
m: &Map,
|
||||
scratch: &mut [u8; SLOT_BYTES],
|
||||
) -> Result<Catalog> {
|
||||
let mut raw = vec![0; m.catalog_len as usize];
|
||||
read_region(io, CATALOG_OFFSETS[m.public_copy], &mut raw)?;
|
||||
if crc32c(&raw) != m.catalog_crc {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
let catalog = Catalog::decode(&raw, m.icons_len as usize)?;
|
||||
let assigned = catalog.validate(m.icons_len as usize)?;
|
||||
for (s, &expected) in m.slots.iter().zip(&assigned) {
|
||||
if s.assigned != expected {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
if let Some(id) = s.block {
|
||||
io.read_at(block_offset(id)?, &mut scratch[..s.used as usize])?;
|
||||
if crc32c(&scratch[..s.used as usize]) != s.crc {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut crc = !0;
|
||||
let mut at = 0usize;
|
||||
while at < m.icons_len as usize {
|
||||
let n = (m.icons_len as usize - at).min(SLOT_BYTES);
|
||||
io.read_at(ICON_OFFSETS[m.public_copy] + at as u64, &mut scratch[..n])?;
|
||||
crc = crc_extend(crc, &scratch[..n]);
|
||||
at += n;
|
||||
}
|
||||
if crc ^ !0 != m.icons_crc {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
for game in &catalog.games {
|
||||
let n = game.icon.length()?;
|
||||
io.read_at(ICON_OFFSETS[m.public_copy] + game.icon.offset as u64, &mut scratch[..n])?;
|
||||
if crc32c(&scratch[..n]) != game.icon.crc32c {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
}
|
||||
Ok(catalog)
|
||||
}
|
||||
pub fn current(&self) -> &Map {
|
||||
self.maps[self.active].as_ref().unwrap()
|
||||
}
|
||||
pub(super) fn known_exclusions(&self) -> [bool; BLOCK_COUNT] {
|
||||
self.known_quarantine
|
||||
}
|
||||
pub(super) fn remember_exclusions(&mut self, known: [bool; BLOCK_COUNT]) {
|
||||
for (i, bad) in known.into_iter().enumerate() {
|
||||
self.known_quarantine[i] |= bad;
|
||||
self.write_blocked |= bad && !self.current().quarantine[i];
|
||||
}
|
||||
self.degraded |= self.known_quarantine.iter().any(|&bad| bad);
|
||||
}
|
||||
pub fn storage_read_only(&self) -> bool {
|
||||
self.io.read_only()
|
||||
}
|
||||
pub fn into_storage(self) -> Box<dyn MemcardStorage> {
|
||||
self.io
|
||||
}
|
||||
pub fn read_slot(&mut self, slot: usize, target: &mut [u8]) -> Result<usize> {
|
||||
let s = *self.current().slots.get(slot).ok_or(Error::InvalidRange)?;
|
||||
if !s.assigned || target.len() < s.used as usize {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
if let Some(id) = s.block {
|
||||
self.io.read_at(block_offset(id)?, &mut target[..s.used as usize])?;
|
||||
if crc32c(&target[..s.used as usize]) != s.crc {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
}
|
||||
Ok(s.used as usize)
|
||||
}
|
||||
fn writable(&self) -> Result<()> {
|
||||
if self.uncertain {
|
||||
return Err(Error::Indeterminate);
|
||||
}
|
||||
if self.write_blocked || self.io.read_only() {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
if self.current().sequence == u64::MAX {
|
||||
return Err(Error::CounterExhausted);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn retire_inactive(&mut self, control: &PublicationControl) -> Result<()> {
|
||||
control.check()?;
|
||||
let inactive = 1 - self.active;
|
||||
// Also invalidate torn/unknown pages before a previously referenced region is reused.
|
||||
self.io.write_at(MAP_OFFSETS[inactive], &[0; PAGE_BYTES])?;
|
||||
self.io.sync()?;
|
||||
self.io.read_at(MAP_OFFSETS[inactive], &mut self.scratch[..PAGE_BYTES])?;
|
||||
if self.scratch[..PAGE_BYTES].iter().any(|&v| v != 0) {
|
||||
self.write_blocked = true;
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
self.maps[inactive] = None;
|
||||
control.check()
|
||||
}
|
||||
fn publish(&mut self, next: Map, control: &PublicationControl) -> Result<()> {
|
||||
let b = next.encode()?;
|
||||
control.enter()?;
|
||||
self.uncertain = true;
|
||||
self.attempted_publication = Some(next.clone());
|
||||
let inactive = 1 - self.active;
|
||||
self.io.write_at(MAP_OFFSETS[inactive], &b)?;
|
||||
self.io.sync()?;
|
||||
self.io.read_at(MAP_OFFSETS[inactive], &mut self.scratch[..PAGE_BYTES])?;
|
||||
if self.scratch[..PAGE_BYTES] != b {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
self.maps[inactive] = Some(next);
|
||||
self.active = inactive;
|
||||
self.uncertain = false;
|
||||
Ok(())
|
||||
}
|
||||
pub fn commit(
|
||||
&mut self,
|
||||
slot: usize,
|
||||
payload: &[u8],
|
||||
timestamp: u64,
|
||||
control: &PublicationControl,
|
||||
) -> CommitResult {
|
||||
let outcome = self.commit_inner(slot, payload, timestamp, control);
|
||||
CommitResult {
|
||||
durable: if outcome.is_ok() {
|
||||
DurableResult::Success
|
||||
} else if self.uncertain {
|
||||
DurableResult::Indeterminate
|
||||
} else {
|
||||
DurableResult::Failure
|
||||
},
|
||||
error: outcome.err(),
|
||||
sequence: self.current().sequence,
|
||||
}
|
||||
}
|
||||
fn commit_inner(
|
||||
&mut self,
|
||||
slot: usize,
|
||||
payload: &[u8],
|
||||
timestamp: u64,
|
||||
control: &PublicationControl,
|
||||
) -> Result<()> {
|
||||
self.writable()?;
|
||||
self.attempted_publication = None;
|
||||
control.check()?;
|
||||
let old =
|
||||
*self.current().slots.get(slot).filter(|s| s.assigned).ok_or(Error::InvalidRange)?;
|
||||
if payload.len() > SLOT_BYTES {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
let generation = old.generation.checked_add(1).ok_or(Error::CounterExhausted)?;
|
||||
self.retire_inactive(control)?;
|
||||
let mut next = self.current().clone();
|
||||
next.sequence += 1;
|
||||
let block = if payload.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let mut protected = self.known_quarantine;
|
||||
for m in self.maps.iter().flatten() {
|
||||
for s in &m.slots {
|
||||
if let Some(b) = s.block {
|
||||
protected[b as usize] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
let id =
|
||||
*next.order.iter().find(|&&id| !protected[id as usize]).ok_or(Error::NoSpace)?;
|
||||
control.check()?;
|
||||
self.io.write_at(block_offset(id)?, payload)?;
|
||||
self.io.sync()?;
|
||||
control.check()?;
|
||||
self.io.read_at(block_offset(id)?, &mut self.scratch[..payload.len()])?;
|
||||
if self.scratch[..payload.len()] != *payload {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
Some(id)
|
||||
};
|
||||
if let Some(id) = block {
|
||||
move_last(&mut next.order, id)
|
||||
}
|
||||
if let Some(id) = old.block {
|
||||
move_last(&mut next.order, id)
|
||||
}
|
||||
next.slots[slot] = Slot {
|
||||
assigned: true,
|
||||
block,
|
||||
used: payload.len() as u32,
|
||||
generation,
|
||||
update_at: timestamp,
|
||||
crc: crc32c(payload),
|
||||
};
|
||||
self.publish(next, control)
|
||||
}
|
||||
/// Runtime-authorized catalog mutation; userland cannot provide global ownership.
|
||||
/// Existing icon data streams between copies through the single scratch buffer.
|
||||
pub fn publish_catalog(
|
||||
&mut self,
|
||||
mut catalog: Catalog,
|
||||
slots: [Slot; SLOT_COUNT],
|
||||
new_icon: Option<(u32, &[u8])>,
|
||||
control: &PublicationControl,
|
||||
) -> Result<()> {
|
||||
self.writable()?;
|
||||
self.attempted_publication = None;
|
||||
control.check()?;
|
||||
let mut next = self.current().clone();
|
||||
next.sequence += 1;
|
||||
next.public_copy = 1 - next.public_copy;
|
||||
next.slots = slots;
|
||||
let mut offset = 0usize;
|
||||
for game in &mut catalog.games {
|
||||
let len = game.icon.length()?;
|
||||
game.icon.offset = offset as u32;
|
||||
offset = offset.checked_add(len).ok_or(Error::NoSpace)?;
|
||||
}
|
||||
catalog.validate(offset)?;
|
||||
let raw = catalog.encode(offset)?;
|
||||
let assigned = catalog.validate(offset)?;
|
||||
if slots.iter().zip(assigned).any(|(s, a)| s.assigned != a) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
next.catalog_len = raw.len() as u32;
|
||||
next.catalog_crc = crc32c(&raw);
|
||||
next.icons_len = offset as u32;
|
||||
// Reject invalid descriptors before any administrative IO.
|
||||
next.encode()?;
|
||||
self.retire_inactive(control)?;
|
||||
let mut crc = !0;
|
||||
for game in &catalog.games {
|
||||
control.check()?;
|
||||
let len = game.icon.length()?;
|
||||
if let Some((id, bytes)) = new_icon.filter(|(id, _)| *id == game.app_id) {
|
||||
let _ = id;
|
||||
if bytes.len() != len || crc32c(bytes) != game.icon.crc32c {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
self.scratch[..len].copy_from_slice(bytes);
|
||||
} else {
|
||||
let old = self
|
||||
.catalog
|
||||
.games
|
||||
.iter()
|
||||
.find(|g| g.app_id == game.app_id)
|
||||
.ok_or(Error::InvalidRange)?;
|
||||
if old.icon.length()? != len || old.icon.crc32c != game.icon.crc32c {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
self.io.read_at(
|
||||
ICON_OFFSETS[self.current().public_copy] + old.icon.offset as u64,
|
||||
&mut self.scratch[..len],
|
||||
)?;
|
||||
if crc32c(&self.scratch[..len]) != old.icon.crc32c {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
}
|
||||
crc = crc_extend(crc, &self.scratch[..len]);
|
||||
self.io.write_at(
|
||||
ICON_OFFSETS[next.public_copy] + game.icon.offset as u64,
|
||||
&self.scratch[..len],
|
||||
)?;
|
||||
}
|
||||
next.icons_crc = crc ^ !0;
|
||||
write_region(self.io.as_mut(), CATALOG_OFFSETS[next.public_copy], &raw)?;
|
||||
self.io.sync()?;
|
||||
Self::validate_map(self.io.as_mut(), &next, self.scratch.as_mut())?;
|
||||
self.publish(next, control)?;
|
||||
self.catalog = catalog;
|
||||
Ok(())
|
||||
}
|
||||
pub fn register(
|
||||
&mut self,
|
||||
mut game: Game,
|
||||
icon: &[u8],
|
||||
control: &PublicationControl,
|
||||
) -> Result<()> {
|
||||
if self.catalog.games.iter().any(|g| g.app_id == game.app_id) {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
let n = game.slots.len();
|
||||
if !(1..=64).contains(&n) {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
let mut slots = self.current().slots;
|
||||
let free: Vec<u8> = slots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, s)| !s.assigned)
|
||||
.map(|(i, _)| i as u8)
|
||||
.take(n)
|
||||
.collect();
|
||||
if free.len() != n {
|
||||
return Err(Error::NoSpace);
|
||||
}
|
||||
game.slots = free;
|
||||
for &id in &game.slots {
|
||||
slots[id as usize] = Slot { assigned: true, ..Slot::default() };
|
||||
}
|
||||
let app_id = game.app_id;
|
||||
let mut c = self.catalog.clone();
|
||||
c.games.push(game);
|
||||
self.publish_catalog(c, slots, Some((app_id, icon)), control)
|
||||
}
|
||||
pub fn remove_game(&mut self, app_id: u32, control: &PublicationControl) -> Result<()> {
|
||||
let game =
|
||||
self.catalog.games.iter().find(|g| g.app_id == app_id).ok_or(Error::InvalidRange)?;
|
||||
let mut slots = self.current().slots;
|
||||
for &id in &game.slots {
|
||||
slots[id as usize] = Slot::default();
|
||||
}
|
||||
let mut c = self.catalog.clone();
|
||||
c.games.retain(|g| g.app_id != app_id);
|
||||
self.publish_catalog(c, slots, None, control)
|
||||
}
|
||||
pub fn grow(&mut self, app_id: u32, count: usize, control: &PublicationControl) -> Result<()> {
|
||||
if !(1..=64).contains(&count) {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
let mut c = self.catalog.clone();
|
||||
let g = c.games.iter_mut().find(|g| g.app_id == app_id).ok_or(Error::InvalidRange)?;
|
||||
if count <= g.slots.len() {
|
||||
return Ok(());
|
||||
}
|
||||
let need = count - g.slots.len();
|
||||
let mut slots = self.current().slots;
|
||||
let free: Vec<u8> = slots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, s)| !s.assigned)
|
||||
.map(|(i, _)| i as u8)
|
||||
.take(need)
|
||||
.collect();
|
||||
if free.len() != need {
|
||||
return Err(Error::NoSpace);
|
||||
}
|
||||
for &id in &free {
|
||||
slots[id as usize] = Slot { assigned: true, ..Slot::default() };
|
||||
}
|
||||
g.slots.extend(free);
|
||||
self.publish_catalog(c, slots, None, control)
|
||||
}
|
||||
/// Explicit host maintenance after coherent mount. Unknown exclusion history
|
||||
/// can only be cleared after every reusable, non-quarantined block is tested.
|
||||
pub fn revalidate(&mut self, control: &PublicationControl) -> Result<()> {
|
||||
if self.uncertain {
|
||||
return Err(Error::Indeterminate);
|
||||
}
|
||||
if self.io.read_only() {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
if self.current().sequence == u64::MAX {
|
||||
return Err(Error::CounterExhausted);
|
||||
}
|
||||
self.write_blocked = true;
|
||||
self.retire_inactive(control)?;
|
||||
for block in 0..BLOCK_COUNT {
|
||||
if !self.known_quarantine[block]
|
||||
&& !self.current().slots.iter().any(|s| s.block == Some(block as u8))
|
||||
{
|
||||
self.test_free_block(block as u8, control)?;
|
||||
}
|
||||
}
|
||||
let mut next = self.current().clone();
|
||||
next.sequence += 1;
|
||||
next.quarantine = self.known_quarantine;
|
||||
self.publish(next, control)?;
|
||||
self.write_blocked = false;
|
||||
self.degraded = self.known_quarantine.iter().any(|&bad| bad);
|
||||
Ok(())
|
||||
}
|
||||
fn test_free_block(&mut self, block: u8, control: &PublicationControl) -> Result<()> {
|
||||
for pattern in [0x55, 0xaa] {
|
||||
control.check()?;
|
||||
self.scratch.fill(pattern);
|
||||
self.io.write_at(block_offset(block)?, self.scratch.as_ref())?;
|
||||
self.io.sync()?;
|
||||
control.check()?;
|
||||
self.io.read_at(block_offset(block)?, self.scratch.as_mut())?;
|
||||
if self.scratch.iter().any(|&byte| byte != pattern) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Reintegrating a known excluded block is a separate, explicit operation.
|
||||
pub fn reintegrate(&mut self, block: u8, control: &PublicationControl) -> Result<()> {
|
||||
self.writable()?;
|
||||
if block as usize >= BLOCK_COUNT
|
||||
|| self.current().slots.iter().any(|s| s.block == Some(block))
|
||||
{
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
self.retire_inactive(control)?;
|
||||
self.test_free_block(block, control)?;
|
||||
let mut next = self.current().clone();
|
||||
next.sequence += 1;
|
||||
next.quarantine[block as usize] = false;
|
||||
self.publish(next, control)?;
|
||||
self.known_quarantine[block as usize] = false;
|
||||
self.degraded = self.known_quarantine.iter().any(|&bad| bad);
|
||||
Ok(())
|
||||
}
|
||||
pub fn read_icon(&mut self, app_id: u32) -> Result<Vec<u8>> {
|
||||
let icon = &self
|
||||
.catalog
|
||||
.games
|
||||
.iter()
|
||||
.find(|g| g.app_id == app_id)
|
||||
.ok_or(Error::InvalidRange)?
|
||||
.icon;
|
||||
let mut bytes = vec![0; icon.length()?];
|
||||
self.io
|
||||
.read_at(ICON_OFFSETS[self.current().public_copy] + icon.offset as u64, &mut bytes)?;
|
||||
if crc32c(&bytes) != icon.crc32c {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
pub fn quarantine(&mut self, block: u8, control: &PublicationControl) -> Result<()> {
|
||||
if block as usize >= BLOCK_COUNT {
|
||||
return Err(Error::InvalidRange);
|
||||
}
|
||||
self.known_quarantine[block as usize] = true;
|
||||
self.degraded = true;
|
||||
let result = (|| {
|
||||
self.writable()?;
|
||||
if self.current().slots.iter().any(|s| s.block == Some(block)) {
|
||||
return Err(Error::Corrupt);
|
||||
}
|
||||
self.retire_inactive(control)?;
|
||||
let mut next = self.current().clone();
|
||||
next.sequence += 1;
|
||||
next.quarantine = self.known_quarantine;
|
||||
self.publish(next, control)
|
||||
})();
|
||||
if result.is_err() {
|
||||
self.write_blocked = true;
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
fn move_last(order: &mut [u8; BLOCK_COUNT], id: u8) {
|
||||
let pos = order.iter().position(|&b| b == id).unwrap();
|
||||
order[pos..].rotate_left(1);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,662 @@
|
||||
use super::*;
|
||||
use prometeu_hal::memcard::MemcardIoError;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Disk {
|
||||
volatile: Vec<u8>,
|
||||
durable: Vec<u8>,
|
||||
calls: usize,
|
||||
fail: Option<usize>,
|
||||
sync_then_fail: bool,
|
||||
}
|
||||
struct Store(Arc<Mutex<Disk>>);
|
||||
impl MemcardStorage for Store {
|
||||
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
|
||||
Ok(IMAGE_BYTES)
|
||||
}
|
||||
fn read_only(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn read_at(&mut self, offset: u64, out: &mut [u8]) -> std::result::Result<(), MemcardIoError> {
|
||||
let mut d = self.0.lock().unwrap();
|
||||
d.calls += 1;
|
||||
if d.fail == Some(d.calls) {
|
||||
return Err(MemcardIoError::IoError);
|
||||
}
|
||||
out.copy_from_slice(&d.volatile[offset as usize..offset as usize + out.len()]);
|
||||
Ok(())
|
||||
}
|
||||
fn write_at(&mut self, offset: u64, bytes: &[u8]) -> std::result::Result<(), MemcardIoError> {
|
||||
let mut d = self.0.lock().unwrap();
|
||||
d.calls += 1;
|
||||
let start = offset as usize;
|
||||
if d.fail == Some(d.calls) {
|
||||
let half = bytes.len() / 2;
|
||||
d.volatile[start..start + half].copy_from_slice(&bytes[..half]);
|
||||
d.durable[start..start + half].copy_from_slice(&bytes[..half]);
|
||||
return Err(MemcardIoError::IoError);
|
||||
}
|
||||
d.volatile[start..start + bytes.len()].copy_from_slice(bytes);
|
||||
Ok(())
|
||||
}
|
||||
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
|
||||
let mut d = self.0.lock().unwrap();
|
||||
d.calls += 1;
|
||||
let fail = d.fail == Some(d.calls);
|
||||
if !fail || d.sync_then_fail {
|
||||
d.durable = d.volatile.clone();
|
||||
}
|
||||
if fail { Err(MemcardIoError::IoError) } else { Ok(()) }
|
||||
}
|
||||
}
|
||||
fn disk() -> Arc<Mutex<Disk>> {
|
||||
Arc::new(Mutex::new(Disk {
|
||||
volatile: vec![0; IMAGE_BYTES as usize],
|
||||
durable: vec![0; IMAGE_BYTES as usize],
|
||||
calls: 0,
|
||||
fail: None,
|
||||
sync_then_fail: false,
|
||||
}))
|
||||
}
|
||||
fn formatted() -> (Card, Arc<Mutex<Disk>>) {
|
||||
let d = disk();
|
||||
Card::format(&mut Store(d.clone()), [7; 16]).unwrap();
|
||||
(Card::mount(Box::new(Store(d.clone()))).unwrap(), d)
|
||||
}
|
||||
fn add(card: &mut Card, app: u32, n: usize) {
|
||||
let icon = [0; 560];
|
||||
card.register(
|
||||
Game {
|
||||
app_id: app,
|
||||
title: "game".into(),
|
||||
data_format_version: 0,
|
||||
created_at: 0,
|
||||
slots: vec![0; n],
|
||||
icon: Icon { offset: 0, frames: 1, duration: 1, crc32c: crc32c(&icon) },
|
||||
},
|
||||
&icon,
|
||||
&PublicationControl::default(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
fn reboot(d: &Arc<Mutex<Disk>>) -> Card {
|
||||
{
|
||||
let mut disk = d.lock().unwrap();
|
||||
disk.volatile = disk.durable.clone();
|
||||
disk.fail = None;
|
||||
}
|
||||
Card::mount(Box::new(Store(d.clone()))).unwrap()
|
||||
}
|
||||
#[test]
|
||||
fn full_pool_rotates_and_empty_keeps_ownership() {
|
||||
let (mut c, d) = formatted();
|
||||
add(&mut c, 1, 64);
|
||||
for i in 0..64 {
|
||||
assert_eq!(
|
||||
c.commit(i, &[i as u8], 0, &PublicationControl::default()).durable,
|
||||
DurableResult::Success
|
||||
);
|
||||
}
|
||||
let mut visited = std::collections::BTreeSet::new();
|
||||
for i in 0..90 {
|
||||
assert_eq!(
|
||||
c.commit(0, &[i], 10, &PublicationControl::default()).durable,
|
||||
DurableResult::Success
|
||||
);
|
||||
visited.insert(c.current().slots[0].block.unwrap());
|
||||
}
|
||||
assert!(visited.len() >= 5);
|
||||
assert_eq!(c.commit(0, &[], 0, &PublicationControl::default()).durable, DurableResult::Success);
|
||||
let r = reboot(&d);
|
||||
assert!(r.current().slots[0].assigned);
|
||||
assert_eq!(r.current().slots[0].block, None);
|
||||
assert_eq!(r.current().slots[0].generation, 92);
|
||||
assert_eq!(r.current().slots[0].update_at, 0);
|
||||
}
|
||||
#[test]
|
||||
fn cut_every_commit_step_including_sync_that_persists_then_errors() {
|
||||
let (mut c, d) = formatted();
|
||||
add(&mut c, 1, 1);
|
||||
c.commit(0, b"old", 7, &PublicationControl::default());
|
||||
let initial = d.lock().unwrap().clone();
|
||||
for persist in [false, true] {
|
||||
for cut in 1..=12 {
|
||||
let mut initial = initial.clone();
|
||||
initial.calls = 0;
|
||||
initial.fail = None;
|
||||
initial.sync_then_fail = persist;
|
||||
let d = Arc::new(Mutex::new(initial));
|
||||
let mut card = Card::mount(Box::new(Store(d.clone()))).unwrap();
|
||||
{
|
||||
let mut disk = d.lock().unwrap();
|
||||
disk.calls = 0;
|
||||
disk.fail = Some(cut);
|
||||
}
|
||||
let result = card.commit(0, b"new version", 1, &PublicationControl::default());
|
||||
let mut recovered = reboot(&d);
|
||||
let mut bytes = [0; 64];
|
||||
let n = recovered.read_slot(0, &mut bytes).unwrap();
|
||||
match result.durable {
|
||||
DurableResult::Success => assert_eq!(&bytes[..n], b"new version"),
|
||||
DurableResult::Failure => assert_eq!(&bytes[..n], b"old"),
|
||||
DurableResult::Indeterminate => {
|
||||
assert!(&bytes[..n] == b"old" || &bytes[..n] == b"new version")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn catalog_cuts_never_publish_partial_registration() {
|
||||
let (c, d) = formatted();
|
||||
drop(c);
|
||||
let baseline = d.lock().unwrap().clone();
|
||||
for cut in 1..=14 {
|
||||
let d = Arc::new(Mutex::new(baseline.clone()));
|
||||
let mut card = Card::mount(Box::new(Store(d.clone()))).unwrap();
|
||||
{
|
||||
let mut state = d.lock().unwrap();
|
||||
state.calls = 0;
|
||||
state.fail = Some(cut);
|
||||
state.sync_then_fail = true;
|
||||
}
|
||||
let icon = [0; 560];
|
||||
let _ = card.register(
|
||||
Game {
|
||||
app_id: 3,
|
||||
title: "new".into(),
|
||||
data_format_version: 0,
|
||||
created_at: 0,
|
||||
slots: vec![0; 2],
|
||||
icon: Icon { offset: 0, frames: 1, duration: 1, crc32c: crc32c(&icon) },
|
||||
},
|
||||
&icon,
|
||||
&PublicationControl::default(),
|
||||
);
|
||||
let recovered = reboot(&d);
|
||||
let n = recovered.catalog.games.len();
|
||||
assert!(n <= 1);
|
||||
assert_eq!(recovered.current().slots.iter().filter(|s| s.assigned).count(), n * 2);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn corrupted_new_payload_recovers_old_map_with_diagnostic() {
|
||||
let (mut c, d) = formatted();
|
||||
add(&mut c, 1, 1);
|
||||
c.commit(0, b"old", 7, &PublicationControl::default());
|
||||
c.commit(0, b"new", 8, &PublicationControl::default());
|
||||
let at = block_offset(c.current().slots[0].block.unwrap()).unwrap() as usize;
|
||||
{
|
||||
let mut disk = d.lock().unwrap();
|
||||
disk.durable[at] ^= 1;
|
||||
}
|
||||
let mut r = reboot(&d);
|
||||
assert!(r.recovered);
|
||||
let mut out = [0; 8];
|
||||
let n = r.read_slot(0, &mut out).unwrap();
|
||||
assert_eq!(&out[..n], b"old");
|
||||
}
|
||||
#[test]
|
||||
fn grow_shrink_removal_and_clean_reassignment() {
|
||||
let (mut c, d) = formatted();
|
||||
add(&mut c, 1, 2);
|
||||
c.commit(1, b"private", 5, &PublicationControl::default());
|
||||
c.grow(1, 4, &PublicationControl::default()).unwrap();
|
||||
c.grow(1, 1, &PublicationControl::default()).unwrap();
|
||||
assert_eq!(c.catalog.games[0].slots.len(), 4);
|
||||
c.remove_game(1, &PublicationControl::default()).unwrap();
|
||||
add(&mut c, 2, 4);
|
||||
let r = reboot(&d);
|
||||
assert!(
|
||||
r.current().slots[..4].iter().all(|s| s.assigned && s.generation == 0 && s.block.is_none())
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn cancellation_prevents_publication_and_generations_do_not_wrap() {
|
||||
let (mut c, d) = formatted();
|
||||
add(&mut c, 1, 1);
|
||||
let token = PublicationControl::default();
|
||||
token.cancel();
|
||||
assert_eq!(c.commit(0, b"no", 1, &token).durable, DurableResult::Failure);
|
||||
assert_eq!(reboot(&d).current().slots[0].generation, 0);
|
||||
c.maps[c.active].as_mut().unwrap().slots[0].generation = u64::MAX;
|
||||
assert_eq!(
|
||||
c.commit(0, b"no", 1, &PublicationControl::default()).error,
|
||||
Some(Error::CounterExhausted)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn quarantine_survives_remount_and_is_never_selected() {
|
||||
let (mut c, d) = formatted();
|
||||
add(&mut c, 1, 1);
|
||||
c.quarantine(0, &PublicationControl::default()).unwrap();
|
||||
let mut c = reboot(&d);
|
||||
assert!(c.degraded);
|
||||
c.commit(0, b"yes", 1, &PublicationControl::default());
|
||||
assert_ne!(c.current().slots[0].block, Some(0));
|
||||
assert!(reboot(&d).current().quarantine[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn async_commit_protects_only_source_and_retains_result_without_occupying_lane() {
|
||||
use crate::services::async_work::{
|
||||
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
|
||||
};
|
||||
use crate::services::memcard::session::{RequestState, Session};
|
||||
use std::time::{Duration, Instant};
|
||||
let (mut card, _) = formatted();
|
||||
add(&mut card, 4, 2);
|
||||
let mut session = Session::default();
|
||||
let context = session.load_card(card, 4, 2, Session::required_bytes(2).unwrap()).unwrap();
|
||||
session.write(context, 0, 0, b"save").unwrap();
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
let now = Instant::now();
|
||||
let id = session.commit(context, 0, 77, now, &lane).unwrap();
|
||||
assert_eq!(session.write(context, 0, 0, b"bad"), Err(Error::Busy));
|
||||
session.write(context, 1, 0, b"other").unwrap();
|
||||
let mut bytes = [0; 4];
|
||||
assert_eq!(session.read(context, 0, 0, &mut bytes), Ok(4));
|
||||
assert_eq!(&bytes, b"save");
|
||||
let mut controller =
|
||||
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while session.query(context, id).unwrap().state == RequestState::Pending {
|
||||
assert!(Instant::now() < deadline);
|
||||
session.poll(Instant::now());
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert_eq!(session.query(context, id).unwrap().state, RequestState::Success);
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
lane.submit_asset_load(move |_| {
|
||||
tx.send(()).unwrap();
|
||||
crate::services::async_work::AsyncWorkJobOutcome::Completed
|
||||
})
|
||||
.unwrap();
|
||||
rx.recv_timeout(Duration::from_secs(1)).unwrap();
|
||||
assert_eq!(session.commit(context, 1, 78, Instant::now(), &lane), Err(Error::Busy));
|
||||
assert_eq!(session.descriptor(context, 0).unwrap().0.update_at, 77);
|
||||
session.release_request(context, id).unwrap();
|
||||
controller.stop().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocked_host_timeout_release_keeps_source_and_prevents_replacement_work() {
|
||||
use crate::services::async_work::{
|
||||
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
|
||||
};
|
||||
use crate::services::memcard::session::{RequestReason, RequestState, Session};
|
||||
use std::time::{Duration, Instant};
|
||||
struct Blocked {
|
||||
store: Box<dyn MemcardStorage>,
|
||||
publication: bool,
|
||||
entered: std::sync::mpsc::Sender<()>,
|
||||
resume: std::sync::mpsc::Receiver<()>,
|
||||
}
|
||||
impl MemcardStorage for Blocked {
|
||||
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
|
||||
self.store.len()
|
||||
}
|
||||
fn read_only(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn read_at(&mut self, o: u64, b: &mut [u8]) -> std::result::Result<(), MemcardIoError> {
|
||||
self.store.read_at(o, b)
|
||||
}
|
||||
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
|
||||
self.store.sync()
|
||||
}
|
||||
fn write_at(&mut self, o: u64, b: &[u8]) -> std::result::Result<(), MemcardIoError> {
|
||||
if if self.publication { b.starts_with(b"PMMAP1\0\0") } else { o >= DATA_OFFSET } {
|
||||
self.entered.send(()).unwrap();
|
||||
self.resume.recv().unwrap();
|
||||
}
|
||||
self.store.write_at(o, b)
|
||||
}
|
||||
}
|
||||
for publication in [false, true] {
|
||||
let (mut card, _) = formatted();
|
||||
add(&mut card, 4, 1);
|
||||
let (et, er) = std::sync::mpsc::channel();
|
||||
let (rt, rr) = std::sync::mpsc::channel();
|
||||
let card = Card::mount(Box::new(Blocked {
|
||||
store: card.into_storage(),
|
||||
entered: et,
|
||||
resume: rr,
|
||||
publication,
|
||||
}))
|
||||
.unwrap();
|
||||
let mut s = Session::default();
|
||||
let c = s.load_card(card, 4, 1, Session::required_bytes(1).unwrap()).unwrap();
|
||||
s.write(c, 0, 0, b"pending").unwrap();
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
let mut controller =
|
||||
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
|
||||
let admitted = Instant::now();
|
||||
let id = s.commit(c, 0, 7, admitted, &lane).unwrap();
|
||||
er.recv_timeout(Duration::from_secs(1)).unwrap();
|
||||
s.poll(admitted + Duration::from_millis(500));
|
||||
s.poll(admitted + Duration::from_secs(1));
|
||||
let result = s.query(c, id).unwrap();
|
||||
assert_eq!(
|
||||
result.state,
|
||||
if publication { RequestState::Indeterminate } else { RequestState::Failure }
|
||||
);
|
||||
assert_eq!(result.reason, RequestReason::RestartRequired);
|
||||
assert!(s.host_faulted());
|
||||
s.release_request(c, id).unwrap();
|
||||
assert_eq!(s.write(c, 0, 0, b"unsafe"), Err(Error::Busy));
|
||||
assert_eq!(s.commit(c, 0, 7, Instant::now(), &lane), Err(Error::Busy));
|
||||
rt.send(()).unwrap();
|
||||
controller.stop().unwrap();
|
||||
s.poll(Instant::now());
|
||||
assert!(s.unsaved());
|
||||
assert!(s.host_faulted());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn file_backend_allocates_full_image_and_reopens_verified_save_without_replacing_files() {
|
||||
use prometeu_drivers::memcard::FileMemcard;
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
"prometeu-memcard-{}-{}",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
|
||||
));
|
||||
std::fs::create_dir(&root).unwrap();
|
||||
let path = root.join("card.pmc");
|
||||
let file = FileMemcard::create(&path, IMAGE_BYTES, |io, id| {
|
||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
||||
})
|
||||
.unwrap();
|
||||
let metadata = std::fs::metadata(&path).unwrap();
|
||||
assert_eq!(metadata.len(), IMAGE_BYTES);
|
||||
assert!(metadata.blocks() * 512 >= IMAGE_BYTES);
|
||||
let mut card = Card::mount(Box::new(file)).unwrap();
|
||||
add(&mut card, 1, 1);
|
||||
assert_eq!(
|
||||
card.commit(0, &vec![17; SLOT_BYTES], 5, &PublicationControl::default()).durable,
|
||||
DurableResult::Success
|
||||
);
|
||||
drop(card);
|
||||
assert!(FileMemcard::create(&path, IMAGE_BYTES, |_, _| Ok(())).is_err());
|
||||
let mut card = Card::mount(Box::new(FileMemcard::open(&path, true).unwrap())).unwrap();
|
||||
let mut out = vec![0; SLOT_BYTES];
|
||||
assert_eq!(card.read_slot(0, &mut out), Ok(SLOT_BYTES));
|
||||
assert!(out.iter().all(|&b| b == 17));
|
||||
assert_eq!(
|
||||
card.commit(0, b"no", 6, &PublicationControl::default()).error,
|
||||
Some(Error::ReadOnly)
|
||||
);
|
||||
drop(card);
|
||||
std::fs::remove_file(path).unwrap();
|
||||
std::fs::remove_dir(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boot_commit_waits_for_ready_and_publishes_registration_before_payload() {
|
||||
use crate::services::async_work::{
|
||||
AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController,
|
||||
};
|
||||
use crate::services::memcard::session::{RequestState, Session};
|
||||
use std::time::{Duration, Instant};
|
||||
let (card, d) = formatted();
|
||||
let mut s = Session::default();
|
||||
let budget = Session::required_bytes(2).unwrap();
|
||||
let c = s.prepare_new_card(card, 9, 2, 123, budget).unwrap();
|
||||
let icon = [0; 560];
|
||||
s.set_presentation(c, "new game", 2, 1, 1, &icon, 124).unwrap();
|
||||
s.write(c, 0, 0, b"boot save").unwrap();
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
let now = Instant::now();
|
||||
let id = s.commit(c, 0, 124, now, &lane).unwrap();
|
||||
assert_eq!(lane.pending_depth(), 0);
|
||||
assert_eq!(s.query(c, id).unwrap().state, RequestState::Pending);
|
||||
assert!(reboot(&d).catalog.games.is_empty());
|
||||
s.finish_boot(true, &lane, now).unwrap();
|
||||
assert_eq!(lane.pending_depth(), 1);
|
||||
let mut controller = AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane);
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while s.query(c, id).unwrap().state == RequestState::Pending {
|
||||
assert!(Instant::now() < deadline);
|
||||
s.poll(Instant::now());
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert_eq!(s.query(c, id).unwrap().state, RequestState::Success);
|
||||
assert!(!s.boot_pending());
|
||||
assert_eq!(s.boot_error(), None);
|
||||
controller.stop().unwrap();
|
||||
let mut card = reboot(&d);
|
||||
assert_eq!(card.catalog.games[0].created_at, 123);
|
||||
let mut b = [0; 16];
|
||||
let n = card.read_slot(0, &mut b).unwrap();
|
||||
assert_eq!(&b[..n], b"boot save");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_revalidation_preserves_exclusions_until_separate_reintegration() {
|
||||
let (mut card, disk) = formatted();
|
||||
add(&mut card, 8, 1);
|
||||
card.quarantine(67, &PublicationControl::default()).unwrap();
|
||||
card.write_blocked = true;
|
||||
card.revalidate(&PublicationControl::default()).unwrap();
|
||||
assert!(!card.write_blocked);
|
||||
assert!(card.current().quarantine[67]);
|
||||
let mut mounted = reboot(&disk);
|
||||
assert!(mounted.current().quarantine[67]);
|
||||
mounted.reintegrate(67, &PublicationControl::default()).unwrap();
|
||||
assert!(!reboot(&disk).current().quarantine[67]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reinsertion_retains_ram_and_different_card_requires_explicit_load() {
|
||||
use crate::memcard::session::Session;
|
||||
let (mut card, source_disk) = formatted();
|
||||
add(&mut card, 9, 1);
|
||||
card.commit(0, b"on disk", 20, &PublicationControl::default());
|
||||
let mut session = Session::default();
|
||||
let context = session.load_card(card, 9, 1, 1024 * 1024).unwrap();
|
||||
session.clear(context, 0).unwrap();
|
||||
session.write(context, 0, 0, b"local").unwrap();
|
||||
session
|
||||
.replace_storage(Box::new(Store(source_disk)), true, &PublicationControl::default())
|
||||
.unwrap();
|
||||
let mut bytes = [0; 7];
|
||||
assert_eq!(session.read(context, 0, 0, &mut bytes).unwrap(), 5);
|
||||
assert_eq!(&bytes[..5], b"local");
|
||||
assert!(session.unsaved());
|
||||
let other = disk();
|
||||
Card::format(&mut Store(other.clone()), [8; 16]).unwrap();
|
||||
let mut target = Card::mount(Box::new(Store(other.clone()))).unwrap();
|
||||
add(&mut target, 9, 1);
|
||||
target.commit(0, b"remote", 30, &PublicationControl::default());
|
||||
drop(target);
|
||||
session.replace_storage(Box::new(Store(other)), true, &PublicationControl::default()).unwrap();
|
||||
assert_eq!(session.read(context, 0, 0, &mut bytes).unwrap(), 6);
|
||||
assert_eq!(&bytes[..6], b"remote");
|
||||
assert!(!session.unsaved());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "manual file-host latency qualification; prints measurements for this host"]
|
||||
fn qualify_file_commits_with_production_asset_decoder() {
|
||||
use crate::memcard::session::{RequestState, Session};
|
||||
use crate::{AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController};
|
||||
use prometeu_drivers::{AssetManager, MemoryBanks, memcard::FileMemcard};
|
||||
use prometeu_hal::{
|
||||
AssetBridge,
|
||||
asset::{AssetCodec, AssetEntry, BankType},
|
||||
cartridge::AssetsPayloadSource,
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
let path = std::env::temp_dir()
|
||||
.join(format!("prometeu-memcard-qualification-{}.pmc", std::process::id()));
|
||||
let file = FileMemcard::create(&path, IMAGE_BYTES, |io, id| {
|
||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
||||
})
|
||||
.unwrap();
|
||||
struct Timed {
|
||||
file: FileMemcard,
|
||||
first: Arc<Mutex<Option<Instant>>>,
|
||||
}
|
||||
impl MemcardStorage for Timed {
|
||||
fn len(&self) -> std::result::Result<u64, MemcardIoError> {
|
||||
self.file.len()
|
||||
}
|
||||
fn read_only(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn read_at(&mut self, o: u64, b: &mut [u8]) -> std::result::Result<(), MemcardIoError> {
|
||||
self.file.read_at(o, b)
|
||||
}
|
||||
fn write_at(&mut self, o: u64, b: &[u8]) -> std::result::Result<(), MemcardIoError> {
|
||||
self.first.lock().unwrap().get_or_insert_with(Instant::now);
|
||||
self.file.write_at(o, b)
|
||||
}
|
||||
fn sync(&mut self) -> std::result::Result<(), MemcardIoError> {
|
||||
self.file.sync()
|
||||
}
|
||||
}
|
||||
let first = Arc::new(Mutex::new(None));
|
||||
let mut card = Card::mount(Box::new(Timed { file, first: first.clone() })).unwrap();
|
||||
add(&mut card, 1, 1);
|
||||
let mut session = Session::default();
|
||||
let context = session.load_card(card, 1, 1, 1024 * 1024).unwrap();
|
||||
for offset in (0..SLOT_BYTES).step_by(4096) {
|
||||
session.write(context, 0, offset, &[0xa5; 4096]).unwrap();
|
||||
}
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
let banks = Arc::new(MemoryBanks::new());
|
||||
let mut assets = vec![0x12; 512 * 512 / 2];
|
||||
assets.extend([255; 64]);
|
||||
let entries=(1..=20).map(|id|AssetEntry{asset_id:id,asset_name:format!("glyph-{id}"),bank_type:BankType::GLYPH,
|
||||
codec:AssetCodec::None,offset:0,size:assets.len() as u64,decoded_size:512*512+64,
|
||||
metadata:serde_json::json!({"tile_size":8,"width":512,"height":512,"palette_count":1,"palette_authored":1})}).collect();
|
||||
let mut manager = AssetManager::new(
|
||||
entries,
|
||||
AssetsPayloadSource::from_bytes(assets),
|
||||
banks.clone(),
|
||||
banks.clone(),
|
||||
banks,
|
||||
);
|
||||
assert!(manager.bind_background_work(lane.clone()));
|
||||
let mut controller =
|
||||
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
|
||||
let mut times = vec![];
|
||||
let mut starts = vec![];
|
||||
for sample in 1..=20 {
|
||||
let asset = manager.load(sample, 0).unwrap();
|
||||
let asset_deadline = Instant::now() + Duration::from_secs(1);
|
||||
while matches!(
|
||||
manager.status(asset),
|
||||
prometeu_hal::asset::LoadStatus::QUEUED | prometeu_hal::asset::LoadStatus::PENDING
|
||||
) {
|
||||
assert!(Instant::now() < asset_deadline);
|
||||
std::thread::yield_now();
|
||||
}
|
||||
*first.lock().unwrap() = None;
|
||||
let start = Instant::now();
|
||||
let request = session.commit(context, 0, sample as u64, start, &lane).unwrap();
|
||||
loop {
|
||||
session.poll(Instant::now());
|
||||
if session.query(context, request).unwrap().state != RequestState::Pending {
|
||||
break;
|
||||
}
|
||||
assert!(start.elapsed() < Duration::from_secs(2));
|
||||
std::thread::yield_now();
|
||||
}
|
||||
assert_eq!(session.query(context, request).unwrap().state, RequestState::Success);
|
||||
times.push(start.elapsed().as_micros());
|
||||
starts.push(first.lock().unwrap().unwrap().duration_since(start).as_micros());
|
||||
session.release_request(context, request).unwrap();
|
||||
}
|
||||
times.sort();
|
||||
starts.sort();
|
||||
eprintln!(
|
||||
"file-host admission-to-first-IO: p50={}us p95={}us max={}us",
|
||||
starts[10], starts[18], starts[19]
|
||||
);
|
||||
eprintln!(
|
||||
"file-host 64KiB commit + production asset load: samples={} p50={}us p95={}us max={}us",
|
||||
times.len(),
|
||||
times[10],
|
||||
times[18],
|
||||
times[19]
|
||||
);
|
||||
controller.stop().unwrap();
|
||||
drop(session);
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_card_admission_preserves_card_and_memory_only_can_extend_existing_data() {
|
||||
use crate::memcard::{
|
||||
MemcardService,
|
||||
admission::{Choice, PreparationState},
|
||||
session::Mode,
|
||||
};
|
||||
use crate::{AsyncWorkLaneConfig, AsyncWorkLaneController};
|
||||
use std::time::{Duration, Instant};
|
||||
let (mut card, _) = formatted();
|
||||
add(&mut card, 1, 1);
|
||||
add(&mut card, 2, 63);
|
||||
card.commit(0, b"prior", 10, &PublicationControl::default());
|
||||
let mut service = MemcardService::new();
|
||||
let mut controller =
|
||||
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), service.lane.clone());
|
||||
service.mount_card(card.into_storage()).unwrap();
|
||||
let wait = |service: &mut MemcardService| {
|
||||
let deadline = Instant::now() + Duration::from_secs(2);
|
||||
while service.admission.state == PreparationState::Pending {
|
||||
service.poll(Instant::now());
|
||||
assert!(Instant::now() < deadline);
|
||||
std::thread::yield_now();
|
||||
}
|
||||
};
|
||||
wait(&mut service);
|
||||
assert_eq!(service.admission.state, PreparationState::Ready);
|
||||
assert_eq!(service.choose_for_game(1, 2, Choice::Persistent, 12), Err(Error::NoSpace));
|
||||
assert!(service.card_installed());
|
||||
service.choose_for_game(1, 2, Choice::MemoryExisting, 12).unwrap();
|
||||
wait(&mut service);
|
||||
assert_eq!(service.admission.state, PreparationState::Ready);
|
||||
let s = &mut service.session;
|
||||
let c = s.context();
|
||||
assert_eq!(s.mode(), Some(Mode::MemoryOnly));
|
||||
let mut bytes = [9; 5];
|
||||
assert_eq!(s.read(c, 0, 0, &mut bytes).unwrap(), 5);
|
||||
assert_eq!(&bytes, b"prior");
|
||||
assert_eq!(s.read(c, 1, 0, &mut bytes).unwrap(), 0);
|
||||
s.end_session();
|
||||
service.poll(Instant::now());
|
||||
assert!(service.card_installed());
|
||||
controller.stop().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ending_a_session_releases_a_completed_unconsumed_request() {
|
||||
use crate::memcard::session::{RequestState, Session};
|
||||
use crate::{AsyncWorkLane, AsyncWorkLaneConfig, AsyncWorkLaneController};
|
||||
use std::time::{Duration, Instant};
|
||||
let (mut card, _) = formatted();
|
||||
add(&mut card, 1, 1);
|
||||
let mut s = Session::default();
|
||||
let c = s.load_card(card, 1, 1, 1024 * 1024).unwrap();
|
||||
s.write(c, 0, 0, b"saved").unwrap();
|
||||
let lane = Arc::new(AsyncWorkLane::default());
|
||||
let mut controller =
|
||||
AsyncWorkLaneController::start(AsyncWorkLaneConfig::default(), lane.clone());
|
||||
let start = Instant::now();
|
||||
let id = s.commit(c, 0, 1, start, &lane).unwrap();
|
||||
while s.query(c, id).unwrap().state == RequestState::Pending {
|
||||
s.poll(Instant::now());
|
||||
assert!(start.elapsed() < Duration::from_secs(2));
|
||||
std::thread::yield_now();
|
||||
}
|
||||
s.end_session();
|
||||
assert!(!s.has_in_flight());
|
||||
assert!(s.take_idle_card().unwrap().is_some());
|
||||
controller.stop().unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user