1use std::collections::{BTreeMap, BTreeSet};
43use std::sync::Arc;
44
45use wasmtime::{
46 Caller, Engine, Instance, Linker, Memory, Module, Store, StoreLimits, StoreLimitsBuilder,
47 TypedFunc,
48};
49
50use crate::corroborate::FunctionRun;
51use crate::error::{QueryError, QueryResult};
52use crate::runtime::{Execution, FunctionRuntime, RunLimits};
53
54pub type RecordSnapshot = BTreeMap<(String, String), Vec<u8>>;
58
59#[derive(Clone, Debug)]
63pub struct PinnedRecords {
64 records: Arc<RecordSnapshot>,
65 hash: [u8; 32],
66}
67
68impl PinnedRecords {
69 pub fn new(records: RecordSnapshot) -> Self {
71 let hash = hash_records(&records);
72 Self {
73 records: Arc::new(records),
74 hash,
75 }
76 }
77
78 pub fn records(&self) -> &RecordSnapshot {
80 &self.records
81 }
82
83 pub fn hash(&self) -> [u8; 32] {
85 self.hash
86 }
87}
88
89#[derive(Clone, Copy, Debug, PartialEq, Eq)]
92pub struct CtxRights {
93 pub read: bool,
94 pub write: bool,
95}
96
97#[derive(Debug)]
102pub struct FunctionCtx {
103 snapshot: Arc<RecordSnapshot>,
104 snapshot_hash: [u8; 32],
105 overlay: BTreeMap<(String, String), Option<Vec<u8>>>,
109 rights: CtxRights,
110 denied: BTreeSet<String>,
111 lookup: (String, String),
114}
115
116impl FunctionCtx {
117 pub fn new(snapshot: RecordSnapshot, rights: CtxRights, denied: BTreeSet<String>) -> Self {
118 Self::with_pinned(&PinnedRecords::new(snapshot), rights, denied)
119 }
120
121 pub fn with_pinned(pinned: &PinnedRecords, rights: CtxRights, denied: BTreeSet<String>) -> Self {
124 Self {
125 snapshot: Arc::clone(&pinned.records),
126 snapshot_hash: pinned.hash,
127 overlay: BTreeMap::new(),
128 rights,
129 denied,
130 lookup: (String::new(), String::new()),
131 }
132 }
133
134 pub fn snapshot_hash(&self) -> [u8; 32] {
137 self.snapshot_hash
138 }
139
140 pub fn writes(&self) -> &BTreeMap<(String, String), Option<Vec<u8>>> {
142 &self.overlay
143 }
144
145 pub fn writes_digest(&self) -> [u8; 32] {
148 let mut h = blake3::Hasher::new();
149 for ((c, id), v) in &self.overlay {
150 frame(&mut h, c.as_bytes());
151 frame(&mut h, id.as_bytes());
152 match v {
153 Some(bytes) => {
154 h.update(&[1]);
155 frame(&mut h, bytes);
156 }
157 None => {
158 h.update(&[0]);
159 }
160 }
161 }
162 *h.finalize().as_bytes()
163 }
164
165 pub fn write_bytes(&self) -> u64 {
168 self.overlay
169 .iter()
170 .map(|((c, id), v)| {
171 (c.len() + id.len() + v.as_ref().map(Vec::len).unwrap_or(0)) as u64
172 })
173 .sum()
174 }
175
176 fn dispatch(&mut self, op: &str, payload: &[u8], out: &mut Vec<u8>) -> Result<(), String> {
180 out.clear();
181 match op {
182 "get" => {
183 if !self.rights.read {
184 return Err("rights: get requires database:read".into());
185 }
186 let (c, id) = split2(payload).ok_or("get: payload must be collection\\0id")?;
187 self.check_open(c)?;
188 let (lc, lid) = &mut self.lookup;
189 lc.clear();
190 lc.push_str(c);
191 lid.clear();
192 lid.push_str(id);
193 let value = match self.overlay.get(&self.lookup) {
194 Some(v) => v.as_deref(), None => self.snapshot.get(&self.lookup).map(Vec::as_slice),
196 };
197 match value {
198 Some(v) => {
199 out.push(1);
200 out.extend_from_slice(v);
201 }
202 None => out.push(0),
203 }
204 Ok(())
205 }
206 "query" => {
207 if !self.rights.read {
208 return Err("rights: query requires database:read".into());
209 }
210 let c = std::str::from_utf8(payload).map_err(|_| "query: collection must be UTF-8")?;
211 self.check_open(c)?;
212 let (lc, lid) = &mut self.lookup;
216 lc.clear();
217 lc.push_str(c);
218 lid.clear();
219 let from = &self.lookup;
220 let mut live: BTreeMap<&str, &[u8]> = self
221 .snapshot
222 .range::<(String, String), _>(from..)
223 .take_while(|((col, _), _)| col == c)
224 .map(|((_, id), v)| (id.as_str(), v.as_slice()))
225 .collect();
226 for ((col, id), v) in self.overlay.range::<(String, String), _>(from..) {
227 if col != c {
228 break;
229 }
230 match v {
231 Some(bytes) => {
232 live.insert(id.as_str(), bytes.as_slice());
233 }
234 None => {
235 live.remove(id.as_str());
236 }
237 }
238 }
239 for (id, v) in live {
240 frame_into(out, id.as_bytes());
241 frame_into(out, v);
242 }
243 Ok(())
244 }
245 "put" => {
246 if !self.rights.write {
247 return Err("rights: put requires database:write".into());
248 }
249 let (c, id, value) =
250 split3(payload).ok_or("put: payload must be collection\\0id\\0value")?;
251 self.check_open(c)?;
252 self.overlay.insert((c.to_string(), id.to_string()), Some(value.to_vec()));
253 Ok(())
254 }
255 "delete" => {
256 if !self.rights.write {
257 return Err("rights: delete requires database:write".into());
258 }
259 let (c, id) = split2(payload).ok_or("delete: payload must be collection\\0id")?;
260 self.check_open(c)?;
261 self.overlay.insert((c.to_string(), id.to_string()), None);
262 Ok(())
263 }
264 other => Err(format!("unknown host op `{other}`")),
265 }
266 }
267
268 fn check_open(&self, collection: &str) -> Result<(), String> {
269 if self.denied.contains(collection) {
270 return Err(format!(
271 "collection `{collection}` is zero-knowledge: the host cannot compute on it"
272 ));
273 }
274 Ok(())
275 }
276}
277
278#[derive(Debug)]
281pub struct FunctionOutcome {
282 pub execution: Execution,
283 pub ctx: FunctionCtx,
284}
285
286pub(crate) struct CtxHostState {
287 limits: StoreLimits,
288 ctx: FunctionCtx,
289 memory: Option<Memory>,
294 alloc: Option<TypedFunc<i32, i32>>,
295 resp: Vec<u8>,
297}
298
299impl FunctionRuntime {
300 pub fn validate_function(&self, module_wasm: &[u8]) -> QueryResult<()> {
304 let module = Module::new(self.engine(), module_wasm)
305 .map_err(|e| QueryError::Compile(e.to_string()))?;
306 for import in module.imports() {
307 if !(import.module() == "spacedb" && import.name() == "host_call") {
308 return Err(QueryError::Abi(format!(
309 "forbidden import `{}::{}` — functions may import only spacedb::host_call",
310 import.module(),
311 import.name()
312 )));
313 }
314 }
315 for export in ["memory", "alloc", "run"] {
316 if module.get_export(export).is_none() {
317 return Err(QueryError::Abi(format!("missing required export `{export}`")));
318 }
319 }
320 Ok(())
321 }
322
323 pub fn run_with_ctx(
329 &self,
330 module_wasm: &[u8],
331 input: &[u8],
332 limits: &RunLimits,
333 ctx: FunctionCtx,
334 ) -> QueryResult<FunctionOutcome> {
335 let workload_hash = hash(module_wasm);
336 let input_digest = {
337 let mut h = blake3::Hasher::new();
338 h.update(input);
339 h.update(&ctx.snapshot_hash());
340 *h.finalize().as_bytes()
341 };
342
343 let module = self.module(&workload_hash, module_wasm)?;
344
345 let max_bytes = limits.max_mem_mb as usize * 1024 * 1024;
346 let mut store = Store::new(
347 self.engine(),
348 CtxHostState {
349 limits: StoreLimitsBuilder::new().memory_size(max_bytes).build(),
350 ctx,
351 memory: None,
352 alloc: None,
353 resp: Vec::new(),
354 },
355 );
356 store.limiter(|h| &mut h.limits);
357 store.set_fuel(limits.max_fuel).map_err(|e| QueryError::Fuel(e.to_string()))?;
358
359 let instance: Instance = self
362 .ctx_linker()
363 .instantiate(&mut store, &module)
364 .map_err(|e| QueryError::Instantiate(e.to_string()))?;
365
366 let memory = instance
367 .get_memory(&mut store, "memory")
368 .ok_or(QueryError::MissingExport("memory"))?;
369 let alloc: TypedFunc<i32, i32> = instance
370 .get_typed_func(&mut store, "alloc")
371 .map_err(|_| QueryError::MissingExport("alloc"))?;
372 let run: TypedFunc<(i32, i32), i64> = instance
373 .get_typed_func(&mut store, "run")
374 .map_err(|_| QueryError::MissingExport("run"))?;
375 store.data_mut().memory = Some(memory);
376 store.data_mut().alloc = Some(alloc.clone());
377
378 let in_len =
379 i32::try_from(input.len()).map_err(|_| QueryError::Abi("input too large".into()))?;
380 let in_ptr = alloc.call(&mut store, in_len).map_err(|e| QueryError::Trap(e.to_string()))?;
381 memory
382 .write(&mut store, in_ptr as usize, input)
383 .map_err(|e| QueryError::Abi(format!("input write: {e}")))?;
384
385 let packed =
386 run.call(&mut store, (in_ptr, in_len)).map_err(|e| QueryError::Trap(e.to_string()))?;
387 let packed = packed as u64;
388 let (out_ptr, out_len) = ((packed >> 32) as usize, (packed & 0xFFFF_FFFF) as usize);
389 let output = read_output(memory.data(&store), out_ptr, out_len)?;
390 let output_digest = hash(&output);
391
392 let remaining = store.get_fuel().map_err(|e| QueryError::Fuel(e.to_string()))?;
393 let fuel_used = limits.max_fuel.saturating_sub(remaining);
394 let mem_peak_mb = (memory.data_size(&store) / (1024 * 1024)) as u32;
395
396 Ok(FunctionOutcome {
397 execution: Execution {
398 output,
399 run: FunctionRun {
400 workload_hash,
401 input_digest,
402 output_digest,
403 fuel_used,
404 mem_peak_mb,
405 },
406 },
407 ctx: store.into_data().ctx,
408 })
409 }
410}
411
412pub(crate) fn ctx_linker(engine: &Engine) -> QueryResult<Linker<CtxHostState>> {
414 let mut linker: Linker<CtxHostState> = Linker::new(engine);
415 linker
416 .func_wrap("spacedb", "host_call", host_call)
417 .map_err(|e| QueryError::Instantiate(e.to_string()))?;
418 Ok(linker)
419}
420
421fn host_call(
423 mut caller: Caller<'_, CtxHostState>,
424 op_ptr: i32,
425 op_len: i32,
426 pay_ptr: i32,
427 pay_len: i32,
428) -> wasmtime::Result<i64> {
429 let memory = match caller.data().memory {
430 Some(memory) => memory,
431 None => caller
432 .get_export("memory")
433 .and_then(|e| e.into_memory())
434 .ok_or_else(|| wasmtime::Error::msg("host_call: no guest memory"))?,
435 };
436
437 let resp = {
443 let (data, state) = memory.data_and_store_mut(&mut caller);
444 let data: &[u8] = data;
445 let slice = |ptr: i32, len: i32| {
446 let (ptr, len) = (ptr as usize, len as usize);
447 data.get(ptr..ptr + len)
448 .ok_or_else(|| wasmtime::Error::msg("host_call: slice out of bounds"))
449 };
450 let op_bytes = slice(op_ptr, op_len)?;
451 let payload = slice(pay_ptr, pay_len)?;
452 let op = std::str::from_utf8(op_bytes)
453 .map_err(|_| wasmtime::Error::msg("host_call: op must be UTF-8"))?;
454 let mut resp = std::mem::take(&mut state.resp);
455 state
456 .ctx
457 .dispatch(op, payload, &mut resp)
458 .map_err(wasmtime::Error::msg)?;
459 resp
460 };
461
462 let alloc: TypedFunc<i32, i32> = match caller.data().alloc.clone() {
465 Some(alloc) => alloc,
466 None => caller
467 .get_export("alloc")
468 .and_then(|e| e.into_func())
469 .ok_or_else(|| wasmtime::Error::msg("host_call: no guest alloc"))?
470 .typed(&caller)
471 .map_err(|e| wasmtime::Error::msg(format!("host_call: alloc: {e}")))?,
472 };
473 let len = i32::try_from(resp.len())
474 .map_err(|_| wasmtime::Error::msg("host_call: response too large"))?;
475 let ptr = alloc.call(&mut caller, len)?;
476 memory.write(&mut caller, ptr as usize, &resp)?;
477 let packed = (((ptr as u32) as i64) << 32) | (resp.len() as u32) as i64;
478 caller.data_mut().resp = resp; Ok(packed)
480}
481
482pub(crate) fn read_output(data: &[u8], out_ptr: usize, out_len: usize) -> QueryResult<Vec<u8>> {
487 out_ptr
488 .checked_add(out_len)
489 .and_then(|end| data.get(out_ptr..end))
490 .map(<[u8]>::to_vec)
491 .ok_or_else(|| {
492 QueryError::Abi(format!(
494 "output read at {out_ptr}+{out_len}: out of bounds memory access"
495 ))
496 })
497}
498
499fn split2(payload: &[u8]) -> Option<(&str, &str)> {
501 let nul = payload.iter().position(|b| *b == 0)?;
502 let c = std::str::from_utf8(&payload[..nul]).ok()?;
503 let id = std::str::from_utf8(&payload[nul + 1..]).ok()?;
504 (!id.as_bytes().contains(&0)).then_some((c, id))
505}
506
507fn split3(payload: &[u8]) -> Option<(&str, &str, &[u8])> {
509 let first = payload.iter().position(|b| *b == 0)?;
510 let rest = &payload[first + 1..];
511 let second = rest.iter().position(|b| *b == 0)?;
512 let c = std::str::from_utf8(&payload[..first]).ok()?;
513 let id = std::str::from_utf8(&rest[..second]).ok()?;
514 Some((c, id, &rest[second + 1..]))
515}
516
517fn frame(h: &mut blake3::Hasher, bytes: &[u8]) {
518 h.update(&(bytes.len() as u32).to_le_bytes());
519 h.update(bytes);
520}
521
522fn frame_into(out: &mut Vec<u8>, bytes: &[u8]) {
523 out.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
524 out.extend_from_slice(bytes);
525}
526
527fn hash_records(records: &RecordSnapshot) -> [u8; 32] {
528 let mut h = blake3::Hasher::new();
529 for ((c, id), v) in records {
530 frame(&mut h, c.as_bytes());
531 frame(&mut h, id.as_bytes());
532 frame(&mut h, v);
533 }
534 *h.finalize().as_bytes()
535}
536
537fn hash(bytes: &[u8]) -> [u8; 32] {
538 *blake3::hash(bytes).as_bytes()
539}