sail-runtime 0.2.0

Exact bounded SMT execution for embedded and service clients
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Bounded exact SMT execution, independently usable without a daemon.
#![forbid(unsafe_code)]

mod admission;
mod process;
mod store;
pub mod temporal;
#[cfg(test)]
mod tests;

use sail_api::runtime::{
    BackendCapability, Capabilities, Error, Input, Limits, MAX_INPUT_BYTES, MAX_OUTPUT_BYTES,
    MAX_QUERIES, MAX_WALL_TIME_MS, Outcome, Output, Query, QueryResult, Request, Response,
    SolverIdentity, VERSION,
};
use sha2::{Digest as _, Sha256};
use std::{
    path::PathBuf,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

/// Configuration of installed tools and bounded execution; performs no installation.
#[derive(Clone, Debug)]
pub struct Config {
    /// Native temporal backend linked into this build; `None` (the default,
    /// and every build of this workspace) reports temporal jobs unavailable.
    pub temporal_backend: Option<Arc<dyn temporal::TemporalBackend>>,
    /// Trusted installed native temporal worker, absent when not installed.
    pub temporal_worker: Option<temporal::Worker>,
    /// Why no worker was configured, reported as the temporal unavailable reason.
    pub temporal_unavailable: Option<Error>,
    /// Existing bundled Z3 executable, independently verified against the embedded pin.
    pub z3: Option<PathBuf>,
    /// Optional disk cache root; existing Verus cache namespaces are untouched.
    pub cache_directory: Option<PathBuf>,
    /// Maximum concurrent independent transcripts, from one through sixteen.
    pub workers: usize,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            temporal_backend: None,
            temporal_worker: None,
            temporal_unavailable: None,
            z3: None,
            cache_directory: None,
            workers: 2,
        }
    }
}

/// Clonable explicit cancellation shared by the caller and actual child processes.
#[derive(Clone, Debug, Default)]
pub struct Cancellation(Arc<AtomicBool>);

impl Cancellation {
    /// Cancel current and undispatched work. Cancellation is permanent for this token.
    pub fn cancel(&self) {
        self.0.store(true, Ordering::SeqCst);
    }
    /// Whether cancellation was requested.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.0.load(Ordering::SeqCst)
    }
}

/// An exact-input runtime; no Verus serving path is modified by its construction.
pub struct Runtime {
    config: Config,
    pin: Option<(String, String)>,
}

pub(crate) fn invalid(kind: &str, message: impl Into<String>) -> Error {
    Error {
        code: "SAIL6101".into(),
        kind: kind.into(),
        message: message.into(),
    }
}

pub(crate) fn execution(kind: &str, message: impl Into<String>) -> Error {
    Error {
        code: "SAIL6102".into(),
        kind: kind.into(),
        message: message.into(),
    }
}

pub(crate) fn hash(bytes: &[u8]) -> String {
    format!("{:x}", Sha256::digest(bytes))
}

fn pin() -> Result<Option<(String, String)>, Error> {
    let pins: toml::Value = toml::from_str(include_str!("../resources/corpus/toolchain.toml"))
        .map_err(|e| invalid("invalid_config", e.to_string()))?;
    let host = match (std::env::consts::OS, std::env::consts::ARCH) {
        ("macos", "aarch64") => "aarch64-apple-darwin",
        ("linux", "x86_64") => "x86_64-unknown-linux-gnu",
        _ => return Ok(None),
    };
    let version = pins
        .get("z3")
        .and_then(|z3| z3.get("version"))
        .and_then(toml::Value::as_str);
    let digest = pins
        .get("assets")
        .and_then(|a| a.get(host))
        .and_then(|a| a.get("z3_sha256"))
        .and_then(toml::Value::as_str);
    match (version, digest) {
        (Some(version), Some(digest)) if digest.len() == 64 => {
            Ok(Some((version.into(), digest.into())))
        }
        _ => Err(invalid(
            "invalid_config",
            "embedded solver pin is incomplete",
        )),
    }
}

fn admitted(query: &Query) -> Result<admission::Plan, Error> {
    if query.obligation_id.trim().is_empty()
        || query.obligation_id.len() > 1024
        || query.requirement_ids.len() > 1024
        || query
            .requirement_ids
            .iter()
            .any(|id| id.trim().is_empty() || id.len() > 1024)
        || query
            .requirement_ids
            .windows(2)
            .any(|pair| pair[0] >= pair[1])
    {
        return Err(invalid(
            "invalid_request",
            "obligation identity must be nonempty; requirement IDs must be nonempty, unique and strictly sorted",
        ));
    }
    let limits = &query.limits;
    if query.backend.is_empty()
        || limits.resource_limit == 0
        || limits.wall_time_ms == 0
        || limits.wall_time_ms > MAX_WALL_TIME_MS
        || limits.input_bytes == 0
        || limits.input_bytes > MAX_INPUT_BYTES
        || limits.output_bytes == 0
        || limits.output_bytes > MAX_OUTPUT_BYTES
        || query.script.len() > limits.input_bytes
    {
        return Err(invalid(
            "invalid_limits",
            "backend, script size or limits are outside the admitted bounds",
        ));
    }
    admission::plan(query)
}

impl Runtime {
    /// Construct the runtime with an existing installed solver and optional cache.
    ///
    /// # Errors
    /// Returns `SAIL6101` for invalid configuration or malformed embedded pins.
    pub fn new(mut config: Config) -> Result<Self, Error> {
        if !(1..=16).contains(&config.workers) {
            return Err(invalid(
                "invalid_config",
                "worker bound must be between one and sixteen",
            ));
        }
        // A bare relative executable name must never be hashed in the current
        // directory and then resolved to a different PATH entry at spawn time.
        config.z3 = config
            .z3
            .map(std::path::absolute)
            .transpose()
            .map_err(|e| invalid("invalid_config", e.to_string()))?;
        Ok(Self {
            config,
            pin: pin()?,
        })
    }

    fn identity(&self, cancellation: &Cancellation) -> Result<SolverIdentity, Error> {
        let path =
            self.config.z3.as_ref().ok_or_else(|| {
                execution("unavailable", "bundled Z3 is not installed or configured")
            })?;
        let (version, expected) = self
            .pin
            .as_ref()
            .ok_or_else(|| execution("unavailable", "no bundled Z3 pin exists for this host"))?;
        let digest = store::file_hash(path)
            .map_err(|e| execution("unavailable", format!("cannot read bundled Z3: {e}")))?;
        if &digest != expected {
            return Err(execution(
                "unavailable",
                "solver executable does not match the bundled pin",
            ));
        }
        let limits = Limits {
            wall_time_ms: 1000,
            output_bytes: 4096,
            ..Limits::default()
        };
        let captured = process::run(path, &["--version".into()], b"", &limits, cancellation);
        let text = std::str::from_utf8(&captured.stdout).unwrap_or_default();
        if captured.outcome != Outcome::Completed
            || !captured.stderr.is_empty()
            || text
                .split_whitespace()
                .take(3)
                .ne(["Z3", "version", version.as_str()])
        {
            return Err(execution(
                "unavailable",
                "bundled Z3 version could not be verified",
            ));
        }
        Ok(SolverIdentity {
            backend: "z3".into(),
            version: version.clone(),
            sha256: digest,
            platform: format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH),
        })
    }

    /// Inspect actual installation readiness; this does not establish any theorem.
    #[must_use]
    pub fn capabilities(&self) -> Capabilities {
        let identity = self.identity(&Cancellation::default());
        Capabilities {
            temporal: Some(temporal::capability(
                self.config.temporal_backend.as_deref(),
                self.config.temporal_worker.as_ref(),
                self.config.temporal_unavailable.as_ref(),
            )),
            version: VERSION,
            backends: vec![BackendCapability {
                backend: "z3".into(),
                implemented: true,
                installed: identity.is_ok(),
                solver: identity.as_ref().ok().cloned(),
                error: identity.err(),
            }],
            incremental_transcripts: true,
            batches: true,
            max_queries: MAX_QUERIES,
        }
    }

    /// Execute a fully admitted bounded batch, preserving caller order and metadata.
    ///
    /// # Errors
    /// Returns `SAIL6101` before dispatch if any query or protocol version is invalid.
    /// Worker infrastructure failures use `SAIL6102` and never become solver answers.
    pub fn execute(
        &self,
        request: &Request,
        cancellation: &Cancellation,
    ) -> Result<Response, Error> {
        if request.version != VERSION {
            return Err(invalid("invalid_request", "unsupported runtime version"));
        }
        let queries = match &request.input {
            Input::Temporal { job } => {
                return Ok(Response {
                    version: VERSION,
                    output: Output::Temporal {
                        result: Box::new(temporal::execute(
                            job,
                            self.config.temporal_backend.as_deref(),
                            self.config.temporal_worker.as_ref(),
                            self.config.temporal_unavailable.as_ref(),
                            cancellation,
                        )?),
                    },
                });
            }
            Input::Smt { queries } => queries,
        };
        if request.version != VERSION
            || queries.is_empty()
            || queries.len() > MAX_QUERIES
            || queries
                .iter()
                .try_fold(0_usize, |sum, query| sum.checked_add(query.script.len()))
                .is_none_or(|size| size > MAX_INPUT_BYTES)
            || queries
                .iter()
                .try_fold(0_usize, |sum, query| {
                    query
                        .limits
                        .output_bytes
                        .checked_mul(3)
                        .and_then(|bound| sum.checked_add(bound))
                })
                .is_none_or(|bound| bound > sail_api::runtime::MAX_RETAINED_OUTPUT_BYTES)
        {
            return Err(invalid(
                "invalid_request",
                "unsupported version, empty or oversized batch",
            ));
        }
        let plans = queries
            .iter()
            .map(admitted)
            .collect::<Result<Vec<_>, _>>()?;
        // Join each bounded wave before starting another; no task outlives its request.
        let mut results = Vec::with_capacity(queries.len());
        for (query_wave, plan_wave) in queries
            .chunks(self.config.workers)
            .zip(plans.chunks(self.config.workers))
        {
            let mut wave = std::thread::scope(|scope| {
                let mut workers = Vec::new();
                let mut spawn_error = None;
                for (query, plan) in query_wave.iter().zip(plan_wave) {
                    match std::thread::Builder::new()
                        .name("sail-runtime-query".into())
                        .spawn_scoped(scope, || self.query(query, plan, cancellation))
                    {
                        Ok(worker) => workers.push(worker),
                        Err(e) => {
                            spawn_error = Some(execution("infrastructure", e.to_string()));
                            break;
                        }
                    }
                }
                let mut wave = Vec::new();
                for worker in workers {
                    match worker.join() {
                        Ok(result) => wave.push(result),
                        Err(_) => {
                            spawn_error =
                                Some(execution("infrastructure", "query worker panicked"));
                        }
                    }
                }
                spawn_error.map_or(Ok(wave), Err)
            })?;
            results.append(&mut wave);
        }
        Ok(Response {
            version: VERSION,
            output: Output::Smt { results },
        })
    }

    fn query(
        &self,
        query: &Query,
        plan: &admission::Plan,
        cancellation: &Cancellation,
    ) -> QueryResult {
        let mut result = empty_result(query);
        if cancellation.is_cancelled() {
            result.outcome = Outcome::Cancelled;
            result.error = Some(execution("cancelled", "cancelled before dispatch"));
            return result;
        }
        if query.backend != "z3" {
            result.error = Some(execution(
                "unavailable",
                "requested backend is not implemented",
            ));
            return result;
        }
        let identity = match self.identity(cancellation) {
            Ok(identity) => identity,
            Err(e) => {
                if cancellation.is_cancelled() {
                    result.outcome = Outcome::Cancelled;
                }
                result.error = Some(e);
                return result;
            }
        };
        result.solver = Some(identity.clone());
        if let Some(cached) = store::read(&self.config, query, &identity, plan) {
            result.stdout = cached;
            if let Ok(checks) = admission::responses(plan, &result.stdout) {
                result.checks = checks;
                result.outcome = Outcome::Completed;
                result.cache_hit = true;
                return result;
            }
        }
        let Some(path) = &self.config.z3 else {
            return result;
        };
        let args = vec![
            "-smt2".into(),
            "-in".into(),
            "smt.random_seed=0".into(),
            "sat.random_seed=0".into(),
            format!("rlimit={}", query.limits.resource_limit),
        ];
        let captured = process::run(
            path,
            &args,
            query.script.as_bytes(),
            &query.limits,
            cancellation,
        );
        result.outcome = captured.outcome;
        result.stdout = captured.stdout;
        result.stderr = captured.stderr;
        if result.outcome == Outcome::Completed {
            let checked = if !result.stderr.is_empty() {
                Err(execution(
                    "infrastructure",
                    "backend produced stderr diagnostics",
                ))
            } else if store::file_hash(path).ok().as_ref() != Some(&identity.sha256) {
                Err(execution(
                    "infrastructure",
                    "backend executable changed during execution",
                ))
            } else {
                admission::responses(plan, &result.stdout)
            };
            match checked {
                Ok(checks) => {
                    result.checks = checks;
                    store::write(
                        &self.config,
                        query,
                        &identity,
                        &result.stdout,
                        &result.checks,
                    );
                }
                Err(e) => {
                    result.outcome = Outcome::Infrastructure;
                    result.error = Some(execution("invalid_response", e.to_string()));
                }
            }
        } else {
            result.error = Some(execution(
                match result.outcome {
                    Outcome::Cancelled => "cancelled",
                    Outcome::Timeout => "timeout",
                    Outcome::OutputLimit => "output_limit",
                    _ => "infrastructure",
                },
                captured
                    .error
                    .unwrap_or_else(|| "backend execution did not complete".into()),
            ));
        }
        result
    }
}

fn empty_result(query: &Query) -> QueryResult {
    QueryResult {
        obligation_id: query.obligation_id.clone(),
        requirement_ids: query.requirement_ids.clone(),
        backend: query.backend.clone(),
        outcome: Outcome::Unavailable,
        checks: Vec::new(),
        stdout: Vec::new(),
        stderr: Vec::new(),
        solver: None,
        script_sha256: hash(query.script.as_bytes()),
        limits: query.limits.clone(),
        cache_hit: false,
        error: None,
    }
}