sail-solve 0.3.1

Fast, cached Verus verification for Rust: standalone files, projects, watch and agent workflows
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Candidate selection only. Current-source validation owns every verdict.

use super::{command, external_dive, periodic, validated};
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::{BTreeMap, BTreeSet};
use std::fs::{self, File};
use std::io::Read as _;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};

const MAX_DONORS: usize = 8;
const MAX_ATTEMPTS: usize = 16;
const MAX_WORD: u64 = 8 << 20;

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Index {
    version: u32,
    donors: Vec<Entry>,
    #[serde(default)]
    layout: Layout,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Entry {
    module: PathBuf,
    config: PathBuf,
    word: PathBuf,
}

#[derive(Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct Layout {
    integer_carrier: Option<String>,
    #[serde(default)]
    renames: BTreeMap<String, String>,
    #[serde(default)]
    fields: Vec<Field>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Field {
    path: Vec<String>,
    kind: Kind,
}

#[derive(Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Kind {
    Integer,
    IntegerOrModel,
    Model,
}

struct Donor {
    module: PathBuf,
    cfg: PathBuf,
    provenance: Value,
    word: Value,
    config: Value,
}

pub(super) struct Search {
    pub(super) report: Option<Value>,
    pub(super) detail: Value,
}

struct Scratch(PathBuf);
impl Scratch {
    fn new() -> std::io::Result<Self> {
        static NEXT: AtomicU64 = AtomicU64::new(0);
        let dir = std::env::temp_dir().join(format!(
            "sail-witness-{}-{}",
            std::process::id(),
            NEXT.fetch_add(1, Ordering::Relaxed)
        ));
        let mut builder = fs::DirBuilder::new();
        #[cfg(unix)]
        {
            use std::os::unix::fs::DirBuilderExt as _;
            builder.mode(0o700);
        }
        builder.create(&dir)?;
        Ok(Self(dir))
    }
}
impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}

fn read(path: &Path, limit: u64) -> Option<Vec<u8>> {
    let metadata = fs::metadata(path).ok()?;
    if !metadata.is_file() || metadata.len() > limit {
        return None;
    }
    let file = File::open(path).ok()?;
    if !file.metadata().ok()?.is_file() {
        return None;
    }
    let mut bytes = Vec::new();
    file.take(limit + 1).read_to_end(&mut bytes).ok()?;
    (bytes.len() as u64 <= limit).then_some(bytes)
}

fn configured(cfg: &Value, property: &str) -> bool {
    cfg["properties"]
        .as_array()
        .is_some_and(|properties| properties.iter().any(|p| p["text"] == property))
}

fn claimed(word: &Value, property: &str) -> bool {
    word["properties"]
        .as_array()
        .is_some_and(|properties| properties.iter().any(|p| p == property))
}

fn load(entry: &Entry, base: &Path, module: &Path, property: &str) -> Option<Donor> {
    if module.file_name()? != entry.module.file_name()? {
        return None;
    }
    let module = fs::canonicalize(base.join(&entry.module)).ok()?;
    let cfg_path = fs::canonicalize(base.join(&entry.config)).ok()?;
    let source = read(&module, 1 << 20)?;
    let cfg = read(&cfg_path, 128 << 10)?;
    let config = sail_dive::witness_configuration(std::str::from_utf8(&cfg).ok()?)?;
    if !configured(&config, property) {
        return None;
    }
    let path = base.join(&entry.word);
    let bytes = read(&path, MAX_WORD)?;
    let word: Value = serde_json::from_slice(&bytes).ok()?;
    if !claimed(&word, property)
        || !matches!(word["length"].as_u64(), Some(1..=4096))
        || !periodic(&word)
    {
        return None;
    }
    let hashed = sail_api::temporal::sha256;
    let provenance = json!({"word":path,"word_sha256":hashed(&bytes),"module_sha256":hashed(&source),"cfg_sha256":hashed(&cfg)});
    Some(Donor {
        module,
        cfg: cfg_path,
        provenance,
        word,
        config,
    })
}

fn bound(cfg: &Value, name: &str) -> Option<u64> {
    let mut matching = cfg["constants"]
        .as_array()?
        .iter()
        .filter(|c| c["name"]["text"] == name && c["module"].is_null());
    let entry = matching.next()?;
    if matching.next().is_some()
        || entry["kind"] != "assignment"
        || !entry["args"].as_array()?.is_empty()
    {
        return None;
    }
    entry["value"]["int"]
        .as_u64()
        .filter(|n| (1..=128).contains(n))
}

fn valid_layout(layout: &Layout) -> bool {
    layout.fields.len() <= 24
        && layout.renames.len() <= 128
        && layout.renames.values().collect::<BTreeSet<_>>().len() == layout.renames.len()
        && layout.fields.iter().enumerate().all(|(i, f)| {
            !f.path.is_empty()
                && f.path.len() <= 12
                && f.path[0] != "*"
                && f.path.iter().all(|segment| {
                    segment
                        .parse::<usize>()
                        .ok()
                        .is_none_or(|index| index.to_string() == *segment)
                })
                && layout.fields[..i].iter().all(|g| {
                    f.path
                        .iter()
                        .zip(&g.path)
                        .any(|(a, b)| a != b && a != "*" && b != "*")
                })
                && match f.kind {
                    Kind::Model => !layout.renames.is_empty(),
                    _ => layout.integer_carrier.is_some(),
                }
        })
}

fn embeddings(old: u64, new: u64) -> Vec<Vec<u64>> {
    if new <= old {
        return vec![];
    }
    // Keep the first coordinate fixed. Try putting the new interval after
    // successive existing coordinates, then try contiguous translations.
    let insert = new - old;
    let mut candidates = (1..old.min(9))
        .map(|gap| {
            (1..=old)
                .map(|i| if i <= gap { i } else { i + insert })
                .collect()
        })
        .collect::<Vec<_>>();
    for shift in 1..=insert.min(4) {
        candidates.push((1 + shift..=old + shift).collect());
    }
    candidates
}

fn rewrite(
    value: &mut Value,
    path: &[String],
    kind: Kind,
    integers: &[u64],
    renames: &BTreeMap<String, String>,
    images: &mut BTreeMap<String, String>,
) -> Option<()> {
    if let Some((part, tail)) = path.split_first() {
        if part == "*" {
            for element in value.as_array_mut()? {
                rewrite(element, tail, kind, integers, renames, images)?;
            }
        } else {
            let child = if value.is_array() {
                value.get_mut(part.parse::<usize>().ok()?)?
            } else {
                value.get_mut(part)?
            };
            rewrite(child, tail, kind, integers, renames, images)?;
        }
        return Some(());
    }
    match kind {
        Kind::IntegerOrModel if value.get("model").and_then(Value::as_str).is_some() => (),
        Kind::Integer | Kind::IntegerOrModel => {
            let index = usize::try_from(value.as_u64()?.checked_sub(1)?).ok()?;
            *value = json!(integers.get(index)?);
        }
        Kind::Model => {
            let name = value.get_mut("model")?;
            let old = name.as_str()?;
            let new = renames.get(old).map_or(old, String::as_str);
            match images.get(new) {
                Some(previous) if previous != old => return None,
                Some(_) => (),
                None => {
                    images.insert(new.to_owned(), old.to_owned());
                }
            }
            if new != old {
                *name = json!(new);
            }
        }
    }
    Some(())
}

fn mapped(donor: &Donor, layout: &Layout, integers: &[u64]) -> Option<Value> {
    let mut word = donor.word.clone();
    let mut images = BTreeMap::new();
    for state in word.get_mut("states")?.as_array_mut()? {
        for field in &layout.fields {
            rewrite(
                state.get_mut("values")?,
                &field.path,
                field.kind,
                integers,
                &layout.renames,
                &mut images,
            )?;
        }
        state["action"] = Value::Null;
    }
    Some(word)
}

pub(super) struct Session<'a> {
    dive: &'a Path,
    property: &'a str,
    started: Instant,
    deadline: Duration,
    cancel: &'a AtomicBool,
    attempts: Vec<Value>,
    donor_replays: Vec<Value>,
    mapping_ms: f64,
}

impl Session<'_> {
    fn available(&self) -> bool {
        self.attempts.len() < MAX_ATTEMPTS
            && self.started.elapsed() < self.deadline
            && !self.cancel.load(Ordering::Relaxed)
    }

    fn invoke(&self, cmd: Command) -> Option<Value> {
        let deadline = self
            .deadline
            .min(self.started.elapsed() + Duration::from_millis(900));
        let (code, bytes) =
            external_dive::run(cmd, self.started, deadline, self.cancel, "witness donor").ok()?;
        (code == 0)
            .then(|| serde_json::from_slice(&bytes).ok())
            .flatten()
    }

    fn precheck(&mut self, trace: &Path, id: usize, donor: &Donor) -> bool {
        if !self.available() {
            return false;
        }
        let mut cmd = command(self.dive, "validate-trace", &donor.module, &donor.cfg);
        cmd.arg("--trace").arg(trace);
        let started = Instant::now();
        let accepted =
            self.invoke(cmd).is_some_and(|report| {
                report["command"] == "validate-trace"
                    && report["schema_version"] == 4
                    && report["result"] == "valid"
                    && report["length"] == donor.word["length"]
            }) && matches_artifact(&donor.module, 1 << 20, &donor.provenance["module_sha256"])
                && matches_artifact(&donor.cfg, 128 << 10, &donor.provenance["cfg_sha256"]);
        self.donor_replays
            .push(json!({"donor":id,"wall_ms":ms(started),"accepted":accepted}));
        accepted
    }

    fn check(
        &mut self,
        mut cmd: Command,
        trace: &Path,
        donor: usize,
        mapping: &Value,
    ) -> Option<Value> {
        if !self.available() {
            return None;
        }
        cmd.arg("--trace").arg(trace).arg("--repair");
        let started = Instant::now();
        let result = self.invoke(cmd).filter(|report| {
            validated(report) && claimed(&report["repair"]["trace"], self.property)
        });
        self.attempts.push(json!({"donor":donor,"mapping":mapping,"wall_ms":ms(started),"accepted":result.is_some()}));
        result
    }

    fn search(
        &mut self,
        donors: &[Donor],
        cfg: &Value,
        layout: &Layout,
        current: &dyn Fn() -> Command,
    ) -> Option<Value> {
        if donors.is_empty() {
            return None;
        }
        let scratch = Scratch::new().ok()?;
        let path = scratch.0.join("candidate.json");
        let mut verified = Vec::new();
        for (id, donor) in donors.iter().enumerate() {
            if !self.available() {
                return None;
            }
            if write_candidate(&path, &donor.word) && self.precheck(&path, id, donor) {
                verified.push((id, donor));
                if let Some(report) = self.check(current(), &path, id, &Value::Null) {
                    return Some(report);
                }
            }
        }
        if !self.available() || !valid_layout(layout) || layout.fields.is_empty() {
            return None;
        }
        let maps = verified
            .iter()
            .map(|(_, d)| candidate_maps(&d.config, cfg, layout))
            .collect::<Vec<_>>();
        for round in 0..maps.iter().map(Vec::len).max().unwrap_or_default() {
            for (&(id, donor), choices) in verified.iter().zip(&maps) {
                if !self.available() {
                    return None;
                }
                let Some(integers) = choices.get(round) else {
                    continue;
                };
                let mapping_started = Instant::now();
                let mapped = mapped(donor, layout, integers)
                    .is_some_and(|word| write_candidate(&path, &word));
                self.mapping_ms += ms(mapping_started);
                if !mapped {
                    continue;
                }
                let mapping = json!({"integer_carrier":layout.integer_carrier,"integer_image":integers,"model_renames":layout.renames});
                if let Some(report) = self.check(current(), &path, id, &mapping) {
                    return Some(report);
                }
            }
        }
        None
    }
}

fn matches_artifact(path: &Path, max: u64, digest: &Value) -> bool {
    read(path, max).is_some_and(|data| digest == &sail_api::temporal::sha256(&data))
}

fn write_candidate(path: &Path, word: &Value) -> bool {
    serde_json::to_vec(word)
        .is_ok_and(|bytes| bytes.len() as u64 <= MAX_WORD && fs::write(path, bytes).is_ok())
}

fn candidate_maps(old: &Value, new: &Value, layout: &Layout) -> Vec<Vec<u64>> {
    if let Some(carrier) = &layout.integer_carrier {
        let Some((old, new)) = bound(old, carrier).zip(bound(new, carrier)) else {
            return vec![];
        };
        let mut candidates = embeddings(old, new);
        if !layout.renames.is_empty() && old <= new {
            candidates.insert(0, (1..=old).collect());
        }
        candidates
    } else {
        vec![Vec::new()]
    }
}

fn ms(started: Instant) -> f64 {
    started.elapsed().as_secs_f64() * 1000.
}

pub(super) fn select(
    index_path: &Path,
    module: &Path,
    config: &Path,
    mut session: Session<'_>,
) -> Search {
    let lookup = Instant::now();
    let index = read(index_path, 64 << 10)
        .and_then(|bytes| serde_json::from_slice::<Index>(&bytes).ok())
        .filter(|i| i.version == 1 && i.donors.len() <= MAX_DONORS);
    let cfg = read(config, 128 << 10)
        .and_then(|bytes| sail_dive::witness_configuration(std::str::from_utf8(&bytes).ok()?));
    let mut reason = match (index.as_ref(), cfg.as_ref()) {
        (None, _) => "missing-or-invalid-donor-index",
        (_, None) => "invalid-current-configuration",
        _ => "property-not-configured",
    };
    let mut donors = Vec::new();
    let mut report = None;
    let mut lookup_ms = ms(lookup);
    if let (Some(index), Some(cfg)) = (
        index,
        cfg.as_ref().filter(|cfg| configured(cfg, session.property)),
    ) {
        let base = index_path.parent().unwrap_or_else(|| Path::new("."));
        donors = index
            .donors
            .iter()
            .take_while(|_| session.available())
            .filter_map(|entry| load(entry, base, module, session.property))
            .collect();
        lookup_ms = ms(lookup);
        reason = if donors.is_empty() {
            "no-matching-donor"
        } else if !valid_layout(&index.layout) {
            "invalid-or-ambiguous-layout"
        } else {
            "donor-attempts-exhausted"
        };
        let dive = session.dive;
        report = session.search(&donors, cfg, &index.layout, &|| {
            command(dive, "validate-trace", module, config)
        });
        if report.is_none()
            && !session.donor_replays.is_empty()
            && session.donor_replays.iter().all(|r| r["accepted"] == false)
        {
            reason = "no-donor-replayed-on-its-source";
        }
    }
    let chosen = session
        .attempts
        .last()
        .filter(|attempt| attempt["accepted"] == true)
        .map(|attempt| json!({"donor":attempt["donor"],"mapping":attempt["mapping"]}));
    Search {
        detail: json!({"lookup_ms":lookup_ms,"donor_phase_ms":ms(lookup),"mapping_ms":session.mapping_ms,"chosen":chosen,"donors":donors.iter().map(|d| &d.provenance).collect::<Vec<_>>(),"attempts":session.attempts,"donor_replays":session.donor_replays,"fallback_reason":report.is_none().then_some(if session.available() { reason } else { "donor-budget-exhausted" })}),
        report,
    }
}

pub(super) fn session<'a>(
    dive: &'a Path,
    property: &'a str,
    started: Instant,
    budget: Duration,
    cancel: &'a AtomicBool,
) -> Session<'a> {
    Session {
        dive,
        property,
        started,
        deadline: (budget / 2).min(Duration::from_secs(5)),
        cancel,
        attempts: vec![],
        donor_replays: vec![],
        mapping_ms: 0.,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn embeddings_touch_only_declared_typed_coordinates() -> Result<(), String> {
        let layout: Layout = serde_json::from_value(json!({
            "integer_carrier":"N", "renames":{"p_x":"p_y"}, "fields":[
                {"path":["positions","fcn","*","1","record","coordinate"],"kind":"integer_or_model"},
                {"path":["positions","fcn","*","0"],"kind":"model"}
            ]
        })).map_err(|error| error.to_string())?;
        assert!(valid_layout(&layout));
        let donor = Donor {
            module: PathBuf::new(),
            cfg: PathBuf::new(),
            provenance: Value::Null,
            config: Value::Null,
            word: json!({"states":[{"values":{
                "positions":{"fcn":[[{"model":"p_x"},{"record":{"coordinate":2,"value":2}}]]},
                "noise":2,
                "copy":{"model":"p_x"}
            }}]}),
        };
        let maps = embeddings(3, 4);
        assert_eq!(maps[0], vec![1, 3, 4]);
        let candidate = mapped(&donor, &layout, &maps[0]).ok_or("no mapped candidate")?;
        let row = &candidate["states"][0]["values"];
        assert_eq!(row["positions"]["fcn"][0][0]["model"], "p_y");
        assert_eq!(row["positions"]["fcn"][0][1]["record"]["coordinate"], 3);
        assert_eq!(row["positions"]["fcn"][0][1]["record"]["value"], 2);
        assert_eq!(row["noise"], 2);
        assert_eq!(row["copy"]["model"], "p_x");
        assert!(mapped(&donor, &layout, &[1]).is_none());
        let mut collision = donor;
        collision.word["states"][0]["values"]["positions"]["fcn"]
            .as_array_mut()
            .ok_or("positions")?
            .push(json!([{"model":"p_y"},{"record":{"coordinate":2,"value":2}}]));
        assert!(mapped(&collision, &layout, &maps[0]).is_none());
        Ok(())
    }

    #[test]
    fn refuses_overlapping_coordinates_and_nonunique_cfg_bindings() -> Result<(), String> {
        let layout: Layout = serde_json::from_value(json!({"integer_carrier":"N","fields":[
            {"path":["rows","fcn","*","1"],"kind":"integer"},
            {"path":["rows","fcn","2","1"],"kind":"integer"}
        ]}))
        .map_err(|error| error.to_string())?;
        assert!(!valid_layout(&layout));
        let config =
            sail_dive::witness_configuration("CONSTANT N = 3\nPROPERTY Live\n").ok_or("CFG")?;
        assert_eq!(bound(&config, "N"), Some(3));
        assert!(configured(&config, "Live"));
        assert!(!configured(&config, "Other"));
        let config = sail_dive::witness_configuration("CONSTANTS N = 3 N = 4\nPROPERTY Live\n");
        assert!(config.is_none_or(|value| bound(&value, "N").is_none()));
        Ok(())
    }
}