Skip to content

Commit 255c4fc

Browse files
committed
fix: sort callees by file path and fn body to get stable hash
close #18 It turns out that call insertion order from MIRs are not reliable. Adding another contract will mess up the callee insertion order.
1 parent bae859c commit 255c4fc

File tree

6 files changed

+3135
-3119
lines changed

6 files changed

+3135
-3119
lines changed

src/functions/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use stable_mir::{
99
mir::mono::{Instance, MonoItem},
1010
ty::{FnDef, RigidTy, Ty, TyKind},
1111
};
12+
use std::cmp::Ordering;
1213

1314
mod kani;
1415
mod serialization;
@@ -81,6 +82,7 @@ impl Function {
8182

8283
let mut callees = IndexSet::new();
8384
callgraph.recursive_callees(item, &mut callees);
85+
callees.sort_by(|a, b| cmp_callees(a, b, tcx, src_map));
8486

8587
let func = source_code_with(body.span, tcx, src_map);
8688
info!(" - {:?} ({span:?}): {func}", inst_def.name());
@@ -119,3 +121,20 @@ fn source_code_with(
119121
let span = internal(tcx, stable_mir_span);
120122
source_code(span, src_map)
121123
}
124+
125+
fn source_code_of_body(inst: &Instance, tcx: TyCtxt, src_map: &SourceMap) -> Option<String> {
126+
inst.body().map(|body| source_code_with(body.span, tcx, src_map))
127+
}
128+
129+
fn cmp_callees(a: &Instance, b: &Instance, tcx: TyCtxt, src_map: &SourceMap) -> Ordering {
130+
let filename_a = a.def.span().get_filename();
131+
let filename_b = b.def.span().get_filename();
132+
match filename_a.cmp(&filename_b) {
133+
Ordering::Equal => (),
134+
ord => return ord,
135+
}
136+
137+
let body_a = source_code_of_body(a, tcx, src_map);
138+
let body_b = source_code_of_body(b, tcx, src_map);
139+
body_a.cmp(&body_b)
140+
}

src/functions/serialization.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ impl Callee {
7676
let inst_def = &inst.def;
7777
let def_id = format_def_id(&inst_def.def_id());
7878
let file = inst_def.span().get_filename();
79-
let func = inst
80-
.body()
81-
.map(|body| super::source_code_with(body.span, tcx, src_map))
82-
.unwrap_or_default();
79+
let func = super::source_code_of_body(inst, tcx, src_map).unwrap_or_default();
8380
Callee { def_id, file, func }
8481
}
8582
}

0 commit comments

Comments
 (0)