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
#![forbid(unsafe_code)]
use crate::cli::Options;
use abigen::Abigen;
use anyhow::anyhow;
use boogie_backend::{
add_prelude, boogie_wrapper::BoogieWrapper, bytecode_translator::BoogieTranslator,
};
use bytecode::{
function_target_pipeline::{FunctionTargetPipeline, FunctionTargetsHolder},
pipeline_factory,
read_write_set_analysis::{self, ReadWriteSetProcessor},
};
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream, WriteColor};
use docgen::Docgen;
use errmapgen::ErrmapGen;
#[allow(unused_imports)]
use log::{debug, info, warn};
use move_model::{
code_writer::CodeWriter, model::GlobalEnv, parse_addresses_from_options,
run_model_builder_with_options,
};
use std::{
fs,
path::{Path, PathBuf},
time::Instant,
};
pub mod cli;
pub fn run_move_prover_errors_to_stderr(options: Options) -> anyhow::Result<()> {
let mut error_writer = StandardStream::stderr(ColorChoice::Auto);
run_move_prover(&mut error_writer, options)
}
pub fn run_move_prover<W: WriteColor>(
error_writer: &mut W,
options: Options,
) -> anyhow::Result<()> {
let now = Instant::now();
let env = run_model_builder_with_options(
&options.move_sources,
&options.move_deps,
options.model_builder.clone(),
parse_addresses_from_options(options.move_named_address_values.clone())?,
)?;
run_move_prover_with_model(&env, error_writer, options, Some(now))
}
pub fn run_move_prover_with_model<W: WriteColor>(
env: &GlobalEnv,
error_writer: &mut W,
options: Options,
timer: Option<Instant>,
) -> anyhow::Result<()> {
let now = timer.unwrap_or_else(Instant::now);
let build_duration = now.elapsed();
check_errors(
env,
&options,
error_writer,
"exiting with model building errors",
)?;
env.report_diag(error_writer, options.prover.report_severity);
env.set_extension(options.prover.clone());
if options.run_docgen {
return run_docgen(env, &options, error_writer, now);
}
if options.run_abigen {
return run_abigen(env, &options, now);
}
if options.run_errmapgen {
return {
run_errmapgen(env, &options, now);
Ok(())
};
}
if options.run_read_write_set {
return {
run_read_write_set(env, &options, now);
Ok(())
};
}
options.backend.check_tool_versions()?;
let now = Instant::now();
let targets = create_and_process_bytecode(&options, env);
let trafo_duration = now.elapsed();
check_errors(
env,
&options,
error_writer,
"exiting with bytecode transformation errors",
)?;
let now = Instant::now();
let code_writer = generate_boogie(env, &options, &targets)?;
let gen_duration = now.elapsed();
check_errors(
env,
&options,
error_writer,
"exiting with boogie generation errors",
)?;
let now = Instant::now();
verify_boogie(env, &options, &targets, code_writer)?;
let verify_duration = now.elapsed();
info!(
"{:.3}s build, {:.3}s trafo, {:.3}s gen, {:.3}s verify",
build_duration.as_secs_f64(),
trafo_duration.as_secs_f64(),
gen_duration.as_secs_f64(),
verify_duration.as_secs_f64()
);
check_errors(
env,
&options,
error_writer,
"exiting with boogie verification errors",
)
}
pub fn check_errors<W: WriteColor>(
env: &GlobalEnv,
options: &Options,
error_writer: &mut W,
msg: &'static str,
) -> anyhow::Result<()> {
env.report_diag(error_writer, options.prover.report_severity);
if env.has_errors() {
Err(anyhow!(msg))
} else {
Ok(())
}
}
pub fn generate_boogie(
env: &GlobalEnv,
options: &Options,
targets: &FunctionTargetsHolder,
) -> anyhow::Result<CodeWriter> {
let writer = CodeWriter::new(env.internal_loc());
add_prelude(env, &options.backend, &writer)?;
let mut translator = BoogieTranslator::new(env, &options.backend, targets, &writer);
translator.translate();
Ok(writer)
}
pub fn verify_boogie(
env: &GlobalEnv,
options: &Options,
targets: &FunctionTargetsHolder,
writer: CodeWriter,
) -> anyhow::Result<()> {
let output_existed = std::path::Path::new(&options.output_path).exists();
debug!("writing boogie to `{}`", &options.output_path);
writer.process_result(|result| fs::write(&options.output_path, result))?;
let boogie = BoogieWrapper {
env,
targets,
writer: &writer,
options: &options.backend,
};
boogie.call_boogie_and_verify_output(&options.output_path)?;
if !output_existed && !options.backend.keep_artifacts {
std::fs::remove_file(&options.output_path).unwrap_or_default();
}
Ok(())
}
pub fn create_and_process_bytecode(options: &Options, env: &GlobalEnv) -> FunctionTargetsHolder {
let mut targets = FunctionTargetsHolder::default();
let output_dir = Path::new(&options.output_path)
.parent()
.expect("expect the parent directory of the output path to exist");
let output_prefix = options.move_sources.get(0).map_or("bytecode", |s| {
Path::new(s).file_name().unwrap().to_str().unwrap()
});
for module_env in env.get_modules() {
if options.prover.dump_bytecode {
let dump_file = output_dir.join(format!("{}.mv.disas", output_prefix));
fs::write(&dump_file, &module_env.disassemble()).expect("dumping disassembled module");
}
for func_env in module_env.get_functions() {
targets.add_target(&func_env)
}
}
let pipeline = if options.experimental_pipeline {
pipeline_factory::experimental_pipeline()
} else {
pipeline_factory::default_pipeline_with_options(&options.prover)
};
if options.prover.dump_bytecode {
let dump_file_base = output_dir
.join(output_prefix)
.into_os_string()
.into_string()
.unwrap();
pipeline.run_with_dump(env, &mut targets, &dump_file_base, options.prover.dump_cfg)
} else {
pipeline.run(env, &mut targets);
}
targets
}
fn run_docgen<W: WriteColor>(
env: &GlobalEnv,
options: &Options,
error_writer: &mut W,
now: Instant,
) -> anyhow::Result<()> {
let generator = Docgen::new(env, &options.docgen);
let checking_elapsed = now.elapsed();
info!("generating documentation");
for (file, content) in generator.gen() {
let path = PathBuf::from(&file);
fs::create_dir_all(path.parent().unwrap())?;
fs::write(path.as_path(), content)?;
}
let generating_elapsed = now.elapsed();
info!(
"{:.3}s checking, {:.3}s generating",
checking_elapsed.as_secs_f64(),
(generating_elapsed - checking_elapsed).as_secs_f64()
);
if env.has_errors() {
env.report_diag(error_writer, options.prover.report_severity);
Err(anyhow!("exiting with documentation generation errors"))
} else {
Ok(())
}
}
fn run_abigen(env: &GlobalEnv, options: &Options, now: Instant) -> anyhow::Result<()> {
let mut generator = Abigen::new(env, &options.abigen);
let checking_elapsed = now.elapsed();
info!("generating ABI files");
generator.gen();
for (file, content) in generator.into_result() {
let path = PathBuf::from(&file);
fs::create_dir_all(path.parent().unwrap())?;
fs::write(path.as_path(), content)?;
}
let generating_elapsed = now.elapsed();
info!(
"{:.3}s checking, {:.3}s generating",
checking_elapsed.as_secs_f64(),
(generating_elapsed - checking_elapsed).as_secs_f64()
);
Ok(())
}
fn run_errmapgen(env: &GlobalEnv, options: &Options, now: Instant) {
let mut generator = ErrmapGen::new(env, &options.errmapgen);
let checking_elapsed = now.elapsed();
info!("generating error map");
generator.gen();
generator.save_result();
let generating_elapsed = now.elapsed();
info!(
"{:.3}s checking, {:.3}s generating",
checking_elapsed.as_secs_f64(),
(generating_elapsed - checking_elapsed).as_secs_f64()
);
}
fn run_read_write_set(env: &GlobalEnv, options: &Options, now: Instant) {
let mut targets = FunctionTargetsHolder::default();
for module_env in env.get_modules() {
for func_env in module_env.get_functions() {
targets.add_target(&func_env)
}
}
let mut pipeline = FunctionTargetPipeline::default();
pipeline.add_processor(ReadWriteSetProcessor::new());
let start = now.elapsed();
info!("generating read/write set");
pipeline.run(env, &mut targets);
read_write_set_analysis::get_read_write_set(env, &targets);
println!("generated for {:?}", options.move_sources);
let end = now.elapsed();
info!("{:.3}s analyzing", (end - start).as_secs_f64());
}