Skip to content

Commit a0d1b1b

Browse files
added support for entry point specification
1 parent 7fb5bc5 commit a0d1b1b

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

src/adapter.rs

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use crate::esbmc::ESBMCParseResult;
99
use crate::irep::Irept;
1010
use log::trace;
1111

12-
pub fn cbmc2esbmc(input: &str, output: &str) {
12+
pub fn cbmc2esbmc(entrypoint: &str, input: &str, output: &str) {
1313
trace!("cbmc2esbmc mode, {} {}", input, output);
1414

15-
let result = crate::cbmc::process_cbmc_file(input);
15+
let result = crate::cbmc::process_cbmc_file(input, entrypoint);
1616
std::fs::remove_file(output).ok();
1717

1818
let converted = ESBMCParseResult::from(result);
@@ -22,7 +22,7 @@ pub fn cbmc2esbmc(input: &str, output: &str) {
2222
}
2323

2424
trait IrepAdapter {
25-
fn to_esbmc_irep(self) -> Irept;
25+
fn to_esbmc_irep(self, entrypoint: &str) -> Irept;
2626
}
2727

2828
pub fn irep_contains(irep: &Irept, id: &str) -> bool {
@@ -57,6 +57,7 @@ impl From<CBMCParseResult> for ESBMCParseResult {
5757
symbols_irep: Vec::with_capacity(data.symbols_irep.len()),
5858
functions_irep: Vec::with_capacity(data.functions_irep.len()),
5959
};
60+
let entrypoint = &data.entrypoint;
6061

6162
// First, we need to walk through the symbols and map all the
6263
// ref-types into concrete types
@@ -69,7 +70,7 @@ impl From<CBMCParseResult> for ESBMCParseResult {
6970
sym.stype.fix_type(&type_cache);
7071
type_cache.insert(tagname, sym.stype.clone());
7172
}
72-
adapted.symbols_irep.push(sym.to_esbmc_irep());
73+
adapted.symbols_irep.push(sym.to_esbmc_irep(entrypoint));
7374
}
7475

7576
// A symbol might have been defined later, we need to check everything again
@@ -102,8 +103,8 @@ impl From<CBMCParseResult> for ESBMCParseResult {
102103
}
103104
}
104105

105-
let function_name = esbmcfixes::fix_name(&foo.name);
106-
let mut function_irep = foo.to_esbmc_irep();
106+
let function_name = esbmcfixes::fix_name(&foo.name, entrypoint);
107+
let mut function_irep = foo.to_esbmc_irep(entrypoint);
107108
function_irep.fix_type(&type_cache);
108109
adapted.functions_irep.push((function_name, function_irep));
109110
}
@@ -115,32 +116,11 @@ impl From<CBMCParseResult> for ESBMCParseResult {
115116
mod esbmcfixes {
116117
use super::HashSet;
117118
use super::Irept;
118-
pub fn fix_name(name: &str) -> String {
119-
match name {
120-
"__CPROVER__start" => String::from("__ESBMC_main"),
121-
"_RNvNtNtCsesPP5EAma4_4core3num6verify24checked_unchecked_add_i8" => {
122-
"__ESBMC_main".to_string()
123-
}
124-
"_RNvNtNtCsesPP5EAma4_4core3num6verify24checked_unchecked_sub_i8" => {
125-
"__ESBMC_main".to_string()
126-
}
127-
"_RNvNtNtCsesPP5EAma4_4core3num6verify24checked_unchecked_mul_i8" => {
128-
"__ESBMC_main".to_string()
129-
}
130-
"_RNvNtNtCsesPP5EAma4_4core3num6verify24checked_unchecked_shr_i8" => {
131-
"__ESBMC_main".to_string()
132-
}
133-
"_RNvNtNtCsesPP5EAma4_4core3num6verify25checked_unchecked_add_i16" => {
134-
"__ESBMC_main".to_string()
135-
}
136-
"_RNvNtNtCsesPP5EAma4_4core3num6verify25checked_unchecked_add_i32" => {
137-
"__ESBMC_main".to_string()
138-
}
139-
"_RNvNtNtCsesPP5EAma4_4core3num6verify25checked_unchecked_add_i64" => {
140-
"__ESBMC_main".to_string()
141-
}
142-
_ => String::from(name),
119+
pub fn fix_name(name: &str, entry: &str) -> String {
120+
if name == entry {
121+
return "__ESBMC_main".to_string();
143122
}
123+
return String::from(name);
144124
}
145125

146126
pub fn fix_expression(irep: &mut Irept) {
@@ -233,7 +213,7 @@ mod esbmcfixes {
233213
}
234214

235215
impl IrepAdapter for CBMCInstruction {
236-
fn to_esbmc_irep(self) -> Irept {
216+
fn to_esbmc_irep(self, entrypoint: &str) -> Irept {
237217
let mut result = Irept::default();
238218
assert_ne!(self.instr_type, 19);
239219

@@ -286,19 +266,19 @@ impl IrepAdapter for CBMCInstruction {
286266
}
287267

288268
impl IrepAdapter for CBMCFunction {
289-
fn to_esbmc_irep(self) -> Irept {
269+
fn to_esbmc_irep(self, entrypoint: &str) -> Irept {
290270
let mut result = Irept::from("goto-program");
291271
for instr in self.instructions {
292272
if instr.code.id == "nil" || instr.code.named_subt["statement"].id != "output" {
293-
result.subt.push(instr.to_esbmc_irep());
273+
result.subt.push(instr.to_esbmc_irep(entrypoint));
294274
}
295275
}
296276
result
297277
}
298278
}
299279

300280
impl IrepAdapter for CBMCSymbol {
301-
fn to_esbmc_irep(self) -> Irept {
281+
fn to_esbmc_irep(self, entrypoint: &str) -> Irept {
302282
let mut result = Irept::default();
303283
result.named_subt.insert("type".to_string(), self.stype);
304284
result.named_subt.insert("symvalue".to_string(), self.value);
@@ -315,8 +295,8 @@ impl IrepAdapter for CBMCSymbol {
315295
.named_subt
316296
.insert("mode".to_string(), Irept::from(&self.mode));
317297

318-
let name = esbmcfixes::fix_name(self.name.as_str());
319-
let basename = esbmcfixes::fix_name(self.base_name.as_str());
298+
let name = esbmcfixes::fix_name(self.name.as_str(), entrypoint);
299+
let basename = esbmcfixes::fix_name(self.base_name.as_str(), entrypoint);
320300

321301
assert_ne!(basename, "num::verify::checked_unchecked_add_i8");
322302

@@ -786,7 +766,7 @@ mod tests {
786766

787767
generate_cbmc_gbf(test_path.to_str().unwrap(), cbmc_gbf.as_str());
788768

789-
cbmc2esbmc(cbmc_gbf.as_str(), esbmc_gbf.as_str());
769+
cbmc2esbmc("__CPROVER__start", cbmc_gbf.as_str(), esbmc_gbf.as_str());
790770
run_esbmc_gbf(&esbmc_gbf, args, expected);
791771
std::fs::remove_file(&cbmc_gbf).ok();
792772
std::fs::remove_file(&esbmc_gbf).ok();
@@ -801,7 +781,11 @@ mod tests {
801781
std::path::Path::new(&cargo_dir).join(format!("resources/test/{}", input_goto));
802782

803783
let esbmc_gbf = format!("{}.goto", input_goto); // TODO: generate UUID!
804-
cbmc2esbmc(test_path.to_str().unwrap(), esbmc_gbf.as_str());
784+
cbmc2esbmc(
785+
"__CPROVER__start",
786+
test_path.to_str().unwrap(),
787+
esbmc_gbf.as_str(),
788+
);
805789
run_esbmc_gbf(&esbmc_gbf, args, expected);
806790
std::fs::remove_file(&esbmc_gbf).ok();
807791
}

src/cbmc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ pub struct CBMCParseResult {
100100
pub reader: ByteReader,
101101
pub symbols_irep: Vec<CBMCSymbol>,
102102
pub functions_irep: Vec<CBMCFunction>,
103+
pub entrypoint: String,
103104
}
104105

105-
pub fn process_cbmc_file(path: &str) -> CBMCParseResult {
106+
pub fn process_cbmc_file(path: &str, entrypoint: &str) -> CBMCParseResult {
106107
let mut result = CBMCParseResult {
107108
reader: ByteReader::read_file(path),
108109
functions_irep: Vec::new(),
109110
symbols_irep: Vec::new(),
111+
entrypoint: String::from(entrypoint),
110112
};
111113

112114
result.reader.check_cbmc_header().unwrap();

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum Commands {
5151

5252
#[derive(Args)]
5353
struct CmdArgs {
54+
entrypoint: String,
5455
input: std::path::PathBuf,
5556
output: std::path::PathBuf,
5657
}
@@ -62,7 +63,11 @@ fn main() {
6263

6364
match cli.command {
6465
Commands::CBMC2ESBMC(args) => {
65-
cbmc2esbmc(&args.input.to_str().unwrap(), args.output.to_str().unwrap());
66+
cbmc2esbmc(
67+
&args.entrypoint,
68+
&args.input.to_str().unwrap(),
69+
args.output.to_str().unwrap(),
70+
);
6671
}
6772
_ => panic!("Command not implemented yet"),
6873
};

0 commit comments

Comments
 (0)