feat(domain): R4.2e OD-3 value cap (400,000), rescue-copy guard, 1 GiB linear ceiling, budget measurements
json_js::MAX_JSON_VALUES from the R4.2e measurements (CAP envelope 39,652 values, 612-leaf export 23,803; ~100 B of tree per value, a 16 MiB dense document 784 MiB with the cap off, 16 MiB with it); draft.restore refuses an over-budget copy as invalid; --max-memory 1 GiB proven to trap on the proofs build; cargo pdf_budget prints the value table, the Node budget suite prints the per-class cost (fresh instance, W4 asserted).
This commit is contained in:
1 parent
f8579bc19a
commit
6ee04c96c5
13 files changed
+599
-14
No files matched your search
@@ -47,5 +47,9 @@ required-features = ["pdf-read"]
|
||||
name = "pdf_import"
|
||||
required-features = ["pdf-read"]
|
||||
|
||||
[[test]]
|
||||
name = "pdf_budget"
|
||||
required-features = ["pdf-read"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -10,7 +10,6 @@ pub mod domain;
|
||||
pub mod facade;
|
||||
pub mod json;
|
||||
pub mod ops;
|
||||
#[cfg(feature = "pdf-read")]
|
||||
pub mod pdf;
|
||||
|
||||
/// Version of the core, reported by the wasm facade and the worker.
|
||||
|
||||
@@ -18,6 +18,7 @@ use crate::domain::report::{LateralSide, Observation, Report, SheetRef, Vertebra
|
||||
use crate::domain::session::{self, ImportFailure, RestoreFailure};
|
||||
use crate::domain::sheet::{LoadedSheet, SheetIndexData, check_sheet_document};
|
||||
use crate::facade::{Failure, parse_input, to_value};
|
||||
use crate::pdf::json_js::{MAX_JSON_VALUES, prescan};
|
||||
|
||||
type Outcome = Result<Value, Failure>;
|
||||
|
||||
@@ -314,6 +315,11 @@ fn versioned(kind: &'static str, found: Option<f64>) -> Failure {
|
||||
/// draft, and leaves it where it is.
|
||||
fn draft_restore(sheet: &LoadedSheet, input: &str) -> Outcome {
|
||||
let input: DraftRestoreInput = parse_input(input)?;
|
||||
// OD-3: a token-dense text is refused before the parser allocates its tree; the caller
|
||||
// sets the text aside verbatim, as for any unreadable draft (`invalid`).
|
||||
if prescan(input.draft.as_bytes()).values > MAX_JSON_VALUES {
|
||||
return Err(Failure::new("invalid", "json value budget exceeded"));
|
||||
}
|
||||
session::restore_draft(sheet, &input.draft, input.archive.as_deref())
|
||||
.map_err(|failure| match failure {
|
||||
RestoreFailure::UnsupportedVersion(found) => versioned("unsupported_version", found),
|
||||
|
||||
@@ -26,6 +26,15 @@ use crate::domain::js::{find_lone_surrogate, parse_json};
|
||||
/// The report and its envelope nest under 16 levels; deeper is not ours (`MAX_JSON_DEPTH`).
|
||||
pub const MAX_JSON_DEPTH: usize = 64;
|
||||
|
||||
/// Values ([`prescan`]) a document may hold before the probe and the rescue copy refuse it
|
||||
/// (OD-3). Measured at R4.2e in the shipped module: the parser's tree costs about 100 bytes
|
||||
/// of linear memory per value, a 16 MiB token-dense document holds 8.4 million (784 MiB,
|
||||
/// what BLK-012 observed); the largest legitimate archive, an envelope at the collection
|
||||
/// caps (500 observations, 1,000 views, logos) holds 39,652, the 612-leaf export 23,803.
|
||||
/// Ten times the largest legitimate archive, rounded: the worst case left is about 40 MiB
|
||||
/// of tree (W4 ≤ 256 MiB). Set here, confirmed by the owner before R4.3 wires the import.
|
||||
pub const MAX_JSON_VALUES: u64 = 400_000;
|
||||
|
||||
const BOM: &[u8] = b"\xEF\xBB\xBF";
|
||||
|
||||
/// The signed form of the cap: the depth may go negative, as in the TypeScript.
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
//! pinned by the frozen oracles O7 (`tests/oracles/r4/read.json`), O8 (`tolerance.json`) and
|
||||
//! O9 (`inflate.json`). The writer (`write`) and the import entry (`import`) follow.
|
||||
|
||||
#[cfg(feature = "pdf-read")]
|
||||
pub mod import;
|
||||
pub mod json_js;
|
||||
#[cfg(feature = "pdf-read")]
|
||||
pub mod read;
|
||||
@@ -12,7 +12,7 @@
|
||||
//!
|
||||
//! OD-3 adds one cap the TypeScript did not have: a candidate whose value count (the
|
||||
//! parser's allocation proxy) exceeds the limit is skipped like one nested too deep, before
|
||||
//! any parser allocates its tree. Its threshold is measured at R4.2e.
|
||||
//! any parser allocates its tree (`json_js::MAX_JSON_VALUES`, measured at R4.2e).
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
@@ -24,8 +24,8 @@ use super::{
|
||||
has_pdf_header,
|
||||
};
|
||||
use crate::pdf::json_js::{
|
||||
JsonPrescan, decode_lossy, is_versioned_object, prescan, rewrite_overflow_numbers,
|
||||
starts_json_object,
|
||||
JsonPrescan, MAX_JSON_VALUES, decode_lossy, is_versioned_object, prescan,
|
||||
rewrite_overflow_numbers, starts_json_object,
|
||||
};
|
||||
|
||||
/// The caps the probe applies on top of the scanner's own.
|
||||
@@ -35,12 +35,6 @@ pub struct ProbeLimits {
|
||||
pub max_json_values: u64,
|
||||
}
|
||||
|
||||
/// Values a candidate may hold before the probe skips it (OD-3). A legitimate archive
|
||||
/// holds a few thousand values; the threshold is set from the R4.2e measurements (10 × the
|
||||
/// largest legitimate archive) and confirmed by the owner before the import is wired.
|
||||
/// Until then the cap is off, so the parity suites measure the TypeScript behaviour.
|
||||
pub const MAX_JSON_VALUES: u64 = u64::MAX;
|
||||
|
||||
impl ProbeLimits {
|
||||
/// No value cap: the TypeScript behaviour, for the parity suites.
|
||||
pub const UNBOUNDED: Self = Self {
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
//! R4.2e, OD-3: the value count (`json_js::prescan`, the parser's allocation proxy) of the
|
||||
//! legitimate archives against the hostile references, printed for RUST.md §10.3 and pinned
|
||||
//! loosely so a drift of an order of magnitude is seen.
|
||||
//!
|
||||
//! Legitimate: the R3 export of the demo with logos (its attachment located by the reader),
|
||||
//! the v2 full and 612-leaves exports, and envelopes built like the export does for the
|
||||
//! synthetic L (60 observations) and CAP (500, the collection cap) reports: the demo's
|
||||
//! branding with logos and labels, a snapshot of the draft sheet for that report, the
|
||||
//! `JSON.stringify(_, null, 2)` form. Hostile: the token-dense `values-1m.pdf` of O8.
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
|
||||
use common::r4::{r4_input, repo_root};
|
||||
use faunosteo_core::domain::archive::{ReportArchiveEnvelope, build_sheet_snapshot};
|
||||
use faunosteo_core::domain::js::parse_json;
|
||||
use faunosteo_core::domain::report::Report;
|
||||
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
|
||||
use faunosteo_core::json::to_js_pretty;
|
||||
use faunosteo_core::pdf::import::import_pdf;
|
||||
use faunosteo_core::pdf::json_js::{JsonPrescan, prescan};
|
||||
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
const SEED: &str = "supabase/seed/anatomical-sheets/equine/draft";
|
||||
const FIXTURES: &str = "apps/app/tests/fixtures";
|
||||
|
||||
fn draft_sheet() -> LoadedSheet {
|
||||
let seed = repo_root().join(SEED);
|
||||
let read = |name: &str| std::fs::read_to_string(seed.join(name)).unwrap();
|
||||
let manifest: Value = serde_json::from_str(&read("assets.json")).unwrap();
|
||||
let mut assets = serde_json::Map::new();
|
||||
let mut hashes = serde_json::Map::new();
|
||||
for asset in manifest.as_array().unwrap() {
|
||||
let id = asset["svg_asset_id"].as_str().unwrap().to_owned();
|
||||
let file = format!("assets/{}", asset["file"].as_str().unwrap());
|
||||
assets.insert(id.clone(), Value::String(read(&file)));
|
||||
hashes.insert(id, asset["sha256"].clone());
|
||||
}
|
||||
let payload = json!({
|
||||
"status": "draft",
|
||||
"ref": parse_json(&read("sheet-ref.json")).unwrap(),
|
||||
"document": parse_json(&read("document.json")).unwrap(),
|
||||
"migrationMap": parse_json(&read("report-migration-map.json")).unwrap(),
|
||||
"assets": assets,
|
||||
"assetHashes": hashes,
|
||||
});
|
||||
let data: BundledSheetData = serde_json::from_value(payload).unwrap();
|
||||
LoadedSheet::from_bundle(data).unwrap()
|
||||
}
|
||||
|
||||
fn fixture(path: &str) -> Vec<u8> {
|
||||
std::fs::read(repo_root().join(FIXTURES).join(path)).unwrap()
|
||||
}
|
||||
|
||||
/// The attachment text of a PDF, as the reader hands it to the session.
|
||||
fn attachment_text(pdf: &[u8]) -> String {
|
||||
read_attached_document(pdf, ProbeLimits::UNBOUNDED)
|
||||
.unwrap()
|
||||
.text
|
||||
.into_owned()
|
||||
}
|
||||
|
||||
/// An envelope like the export writes for `report`: the demo's branding (logos) and labels,
|
||||
/// a snapshot of the draft for that report.
|
||||
fn envelope_for(sheet: &LoadedSheet, demo: &ReportArchiveEnvelope, report: Report) -> String {
|
||||
let envelope = ReportArchiveEnvelope {
|
||||
sheet_snapshot: build_sheet_snapshot(sheet, &report),
|
||||
report,
|
||||
..demo.clone()
|
||||
};
|
||||
to_js_pretty(&envelope).unwrap()
|
||||
}
|
||||
|
||||
fn report(path: &str) -> Report {
|
||||
let text = String::from_utf8(fixture(path)).unwrap();
|
||||
serde_json::from_value(parse_json(&text).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
fn row(name: &str, text: &str) -> (String, usize, JsonPrescan) {
|
||||
(name.to_owned(), text.len(), prescan(text.as_bytes()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legitimate_archives_stay_far_below_the_hostile_references() {
|
||||
let sheet = draft_sheet();
|
||||
let demo_pdf = fixture("pdf/r3/demo.fr.logos.pdf");
|
||||
let demo = import_pdf(&sheet, &demo_pdf, ProbeLimits::UNBOUNDED)
|
||||
.unwrap()
|
||||
.read
|
||||
.archive
|
||||
.unwrap();
|
||||
let rows = [
|
||||
row("demo fr logos (R3 export)", &attachment_text(&demo_pdf)),
|
||||
row(
|
||||
"v2full fr logos (R3 export)",
|
||||
&attachment_text(&fixture("pdf/r3/v2full.fr.logos.pdf")),
|
||||
),
|
||||
row(
|
||||
"612 leaves (O7 read.612-leaves-exported)",
|
||||
&attachment_text(&r4_input("read", "read.612-leaves-exported.pdf")),
|
||||
),
|
||||
row(
|
||||
"L envelope (60 observations, 120 views, logos)",
|
||||
&envelope_for(&sheet, &demo, report("r4/reports/L.json")),
|
||||
),
|
||||
row(
|
||||
"CAP envelope (500 observations, 1,000 views, logos)",
|
||||
&envelope_for(&sheet, &demo, report("r4/reports/CAP.json")),
|
||||
),
|
||||
row(
|
||||
"hostile: tolerance/values-1m.pdf",
|
||||
&attachment_text(&fixture("pdf/r4/tolerance/values-1m.pdf")),
|
||||
),
|
||||
];
|
||||
println!("| Document | Bytes | Values | Within depth |");
|
||||
println!("|---|---|---|---|");
|
||||
for (name, bytes, scan) in &rows {
|
||||
println!(
|
||||
"| {name} | {bytes} | {} | {} |",
|
||||
scan.values, scan.within_depth
|
||||
);
|
||||
}
|
||||
let cap = rows[4].2.values;
|
||||
assert!(cap < 200_000, "CAP envelope values: {cap}");
|
||||
assert!(rows[5].2.values > 5 * cap, "values-1m is not far above CAP");
|
||||
}
|
||||
@@ -268,6 +268,16 @@ fn o8_import_matches_the_r3_path_outside_the_declared_differences() {
|
||||
assert_eq!(declared, 3);
|
||||
}
|
||||
|
||||
/// O8 cases the application's caps refuse (OD-3, `MAX_JSON_VALUES`): a token-dense
|
||||
/// document is skipped like one nested too deep, so the kind follows the stamp, as the
|
||||
/// depth cap's does today (`depth-65` is `attachment_stripped` in O8).
|
||||
fn declared_od3(id: &str) -> Option<Value> {
|
||||
match id {
|
||||
"values-1m" => Some(json!({ "ok": false, "kind": "attachment_stripped" })),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_facade_envelope_carries_the_closed_kinds() {
|
||||
let sheet = draft_sheet();
|
||||
@@ -276,7 +286,9 @@ fn the_facade_envelope_carries_the_closed_kinds() {
|
||||
let id = case["id"].as_str().unwrap();
|
||||
let pdf = std::fs::read(repo_root().join(case["pdf"].as_str().unwrap())).unwrap();
|
||||
let envelope: Value = serde_json::from_str(&facade::import_pdf(&sheet, &pdf)).unwrap();
|
||||
let expected = declared_o8(id).unwrap_or_else(|| case["outcome"].clone());
|
||||
let expected = declared_od3(id)
|
||||
.or_else(|| declared_o8(id))
|
||||
.unwrap_or_else(|| case["outcome"].clone());
|
||||
assert_eq!(envelope["ok"], expected["ok"], "{id}");
|
||||
if expected["ok"] == false {
|
||||
assert_eq!(envelope["kind"], expected["kind"], "{id}");
|
||||
|
||||
@@ -50,6 +50,35 @@ impl SheetHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// R4.2e proof: the import with the OD-3 value cap off, so that a token-dense document runs
|
||||
/// into the linear memory ceiling and traps (never in the module the application ships).
|
||||
#[cfg(feature = "proofs")]
|
||||
#[wasm_bindgen]
|
||||
pub fn import_pdf_unbounded_for_test(bundle: &str, bytes: &[u8]) -> Result<String, JsError> {
|
||||
use faunosteo_core::pdf::import::import_pdf;
|
||||
use faunosteo_core::pdf::read::probe::ProbeLimits;
|
||||
let sheet = faunosteo_core::facade::load_sheet(bundle)
|
||||
.map_err(|failure| JsError::new(&format!("{}: {}", failure.kind, failure.detail)))?;
|
||||
Ok(match import_pdf(&sheet, bytes, ProbeLimits::UNBOUNDED) {
|
||||
Ok(_) => "ok".to_owned(),
|
||||
Err(failure) => format!("{failure:?}"),
|
||||
})
|
||||
}
|
||||
|
||||
/// R4.2e proof: the linear memory ceiling (`--max-memory`, `.cargo/config.toml`). Allocates
|
||||
/// and touches `mib` MiB; past the ceiling the allocation fails and Rust aborts, a trap.
|
||||
#[cfg(feature = "proofs")]
|
||||
#[wasm_bindgen]
|
||||
pub fn allocate_for_test(mib: u32) -> u32 {
|
||||
let bytes = vec![1u8; (mib as usize) * 1024 * 1024];
|
||||
std::hint::black_box(&bytes);
|
||||
bytes
|
||||
.iter()
|
||||
.step_by(1024 * 1024)
|
||||
.map(|b| u32::from(*b))
|
||||
.sum()
|
||||
}
|
||||
|
||||
/// R0 proof: on wasm32 a panic is a trap (panic = abort), reaching JavaScript as a
|
||||
/// `WebAssembly.RuntimeError`; the caller re-instantiates the module.
|
||||
#[cfg(feature = "proofs")]
|
||||
|
||||
Reference in new issue
Block a user