logicaffeine_compile/semantics/acceptance.rs
1//! C2 Layer C — the receiver's typed, bounded **acceptance contract** for shipped
2//! computation.
3//!
4//! `Send computed f` (Layer B) lets a peer ship a *pure function* — its `GenExpr` body,
5//! not its data — which the receiver evaluates in a bounded sandbox. That is safe in that
6//! the sandbox can only do total integer arithmetic over one argument; it is NOT safe in
7//! that the receiver would run *whatever shape* arrived on *whatever argument*.
8//!
9//! An [`AcceptanceContract`] closes that gap the way a web form's validator does: the
10//! receiver writes down **exactly** the interface it will run — a single integer argument
11//! within a declared inclusive range — and every invocation is validated against it
12//! *before* evaluation. A function of the wrong shape is refused at the signature check; an
13//! argument outside the range is **refused, never silently clamped**. The attack surface is
14//! precisely what the receiver wrote down, and nothing more.
15//!
16//! The check is O(1) — two integer comparisons over a value the sandbox already bounds — so
17//! the safety costs essentially nothing on the hot path.
18
19use crate::concurrency::marshal::gen_eval;
20use crate::interpreter::RuntimeValue;
21
22/// A receiver-declared acceptance contract: a single integer parameter accepted only within
23/// `[lo, hi]` (inclusive), returning an integer. The bound is what the receiver promises to
24/// honor; anything outside is refused at the seam.
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub struct AcceptanceContract {
27 pub lo: i64,
28 pub hi: i64,
29}
30
31impl AcceptanceContract {
32 pub fn new(lo: i64, hi: i64) -> Self {
33 // A reversed range accepts nothing — normalize so the bound reads as the user wrote
34 // it but still rejects every argument (lo > hi ⇒ no `arg` satisfies lo ≤ arg ≤ hi).
35 AcceptanceContract { lo, hi }
36 }
37
38 /// Validate `function` and `arg` against this contract, then evaluate. The two failure
39 /// modes are distinct and both surface as `Err`:
40 ///
41 /// * **signature** — `function` must be a *shipped* pure computation (`generated`) of
42 /// exactly one argument. An ordinary closure (arena body) or a wrong arity is refused;
43 /// the contract only ever runs the bounded sandbox, never interpreter-resident code.
44 /// * **range** — `arg` must satisfy `lo ≤ arg ≤ hi`. Out-of-range is refused, NOT
45 /// clamped: the receiver asked for a bounded domain, so an out-of-domain input is an
46 /// error at the edge, not a quietly-different computation.
47 pub fn apply(&self, function: &RuntimeValue, arg: i64) -> Result<i64, String> {
48 let closure = match function {
49 RuntimeValue::Function(c) => c,
50 other => {
51 return Err(format!(
52 "acceptance contract: expected a shipped computation, got {}",
53 other.type_name()
54 ))
55 }
56 };
57 let gen = closure.generated.as_ref().ok_or_else(|| {
58 "acceptance contract: an ordinary closure is refused — only a `Send computed` \
59 shipped pure computation may be run under a contract"
60 .to_string()
61 })?;
62 if closure.param_names.len() != 1 {
63 return Err(format!(
64 "acceptance contract: a shipped computation must take exactly one argument, \
65 this one takes {}",
66 closure.param_names.len()
67 ));
68 }
69 if arg < self.lo || arg > self.hi {
70 return Err(format!(
71 "acceptance contract: argument {arg} is outside the accepted range \
72 {}..={} — refused (the contract is not satisfied; the value is not clamped)",
73 self.lo, self.hi
74 ));
75 }
76 Ok(gen_eval(gen, arg))
77 }
78}
79
80#[cfg(test)]
81#[path = "../../../logicaffeine_tests/unit/logicaffeine_compile/semantics/acceptance/tests.rs"]
82mod tests;