-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
147 lines (127 loc) · 4.03 KB
/
lib.rs
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
extern crate serde;
extern crate serde_json;
use std::env;
use std::io::{LineWriter, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::result::Result;
use stainless_data::ast as st;
pub mod messages;
use messages::{Report, Response};
pub struct Config {
timeout: usize,
print_ids: bool,
print_types: bool,
debug_trees: bool,
debug_phases: Vec<String>,
strict_arithmetic: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
timeout: 30,
print_ids: false,
print_types: false,
debug_trees: false,
debug_phases: vec![],
strict_arithmetic: false,
}
}
}
#[allow(dead_code)]
pub struct Backend {
config: Config,
child: Child,
}
impl Backend {
pub fn create(config: Config) -> Result<Self, String> {
let home_path = find_stainless_home()?;
let exec_path = home_path.join("stainless");
if !exec_path.is_file() {
return Err(format!(
"Could not find stainless executable at {}",
exec_path.to_string_lossy()
));
}
let mut cmd = Command::new(exec_path);
cmd
.arg("--interactive")
.arg("--batched")
.arg("--vc-cache=false")
.arg("--type-checker=false")
.arg("--check-measures=false")
.arg("--infer-measures=false")
.arg(format!("--timeout={}", config.timeout))
.arg(format!("--print-ids={}", config.print_ids))
.arg(format!("--print-types={}", config.print_types))
.arg(format!("--strict-arithmetic={}", config.strict_arithmetic));
if config.debug_trees {
cmd
.arg("--debug=trees")
.arg(format!("--debug-phases={}", config.debug_phases.join(",")));
}
if let Ok(extra_flags) = env::var("STAINLESS_FLAGS") {
cmd.args(extra_flags.split(' '));
}
let child = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(|err| format!("Could not spawn stainless: {}", err))?;
Ok(Self { config, child })
}
pub fn query<P: AsRef<Path>>(&mut self, query_path: P) -> Result<Response, String> {
let stdin = self
.child
.stdin
.as_mut()
.expect("Stdin for stainless process is missing");
let mut stdin = LineWriter::new(stdin);
writeln!(stdin, "{}", query_path.as_ref().to_str().unwrap())
.map_err(|_| "Could not write query to stainless stdin")?;
let stdout = self
.child
.stdout
.as_mut()
.expect("Stdout for stainless process is missing");
// NOTE: A hacky way to debug unparsable responses from stainless:
// use std::io::Read;
// let mut buffer = String::new();
// stdout.read_to_string(&mut buffer).unwrap();
// eprintln!("Got the following from: {}", buffer);
// panic!();
let deserializer = serde_json::Deserializer::from_reader(stdout);
let mut iterator = deserializer.into_iter::<Response>();
iterator
.next()
.ok_or("Stainless stopped before responding")?
.map_err(|err| format!("Failed to parse response: {}", err))
}
pub fn query_for_program(&mut self, symbols: &st::Symbols) -> Result<Response, String> {
use stainless_data::ser::*;
use tempfile::NamedTempFile;
let mut file = NamedTempFile::new().expect("Unable to create temporary example file");
let mut s = BufferSerializer::new();
symbols
.serialize(&mut s)
.expect("Failed to serialize stainless program");
file
.write_all(s.as_slice())
.expect("Unable to write example file");
self.query(file.path())
}
}
fn find_stainless_home() -> Result<PathBuf, String> {
match std::env::var("STAINLESS_HOME") {
Ok(home_path) => Ok(PathBuf::from(home_path)),
Err(_) => {
Err("Could not find stainless directory. Please make sure STAINLESS_HOME is set.".into())
}
}
}
/// Convenience method to verify a single program
pub fn verify_program(config: Config, symbols: &st::Symbols) -> Result<Report, String> {
let mut backend = Backend::create(config)?;
let response = backend.query_for_program(symbols)?;
response.into_verification_report()
}