Skip to content

Commit 46952c8

Browse files
authored
Fix static variable malformed symbol (rust-lang#1380)
A symbol's `base_name` has to be a suffix of its `name` otherwise it is considered a malformed symbol. For static variables, Kani was using the `pretty_name` to generate the `base_name` which was generating invalid iRep as a result. This was caught while trying to run symtab2gb after the std compilation. Fixes rust-lang#1361
1 parent 89c1269 commit 46952c8

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

cprover_bindings/src/goto_program/symbol.rs

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ impl Symbol {
7171
let name = name.into();
7272
let base_name = base_name.intern();
7373
let pretty_name = pretty_name.intern();
74+
// See https://github.com/model-checking/kani/issues/1361#issuecomment-1181499683
75+
assert!(
76+
name.to_string().ends_with(&base_name.map_or(String::new(), |s| s.to_string())),
77+
"Symbol's base_name must be the suffix of its name.\nName: {:?}\nBase name: {:?}",
78+
name,
79+
base_name
80+
);
7481
Symbol {
7582
name,
7683
location,

kani-compiler/src/codegen_cprover_gotoc/codegen/static_var.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ use rustc_middle::mir::mono::MonoItem;
1010
use rustc_middle::ty::{subst::InternalSubsts, Instance};
1111
use tracing::debug;
1212

13-
/// Separator used to generate function static variable names (<function_name>::<variable_name>).
14-
const SEPARATOR: &str = "::";
15-
1613
impl<'tcx> GotocCtx<'tcx> {
1714
pub fn codegen_static(&mut self, def_id: DefId, item: MonoItem<'tcx>) {
1815
debug!("codegen_static");
@@ -26,15 +23,12 @@ impl<'tcx> GotocCtx<'tcx> {
2623
let symbol_name = item.symbol_name(self.tcx).to_string();
2724
// Pretty name which may include function name.
2825
let pretty_name = Instance::new(def_id, InternalSubsts::empty()).to_string();
29-
// Name of the variable in the local context.
30-
let base_name =
31-
pretty_name.rsplit_once(SEPARATOR).map(|names| names.1).unwrap_or(pretty_name.as_str());
32-
debug!(?symbol_name, ?pretty_name, ?base_name, "declare_static {}", item);
26+
debug!(?symbol_name, ?pretty_name, "declare_static {}", item);
3327

3428
let typ = self.codegen_ty(self.tcx.type_of(def_id));
3529
let span = self.tcx.def_span(def_id);
3630
let location = self.codegen_span(&span);
37-
let symbol = Symbol::static_variable(symbol_name, base_name, typ, location)
31+
let symbol = Symbol::static_variable(symbol_name.clone(), symbol_name, typ, location)
3832
.with_is_hidden(false) // Static items are always user defined.
3933
.with_pretty_name(pretty_name);
4034
self.symbol_table.insert(symbol);

scripts/std-lib-regression.sh

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ kani = {path=\"${KANI_DIR}/library/kani\"}
6161
cp ${KANI_DIR}/rust-toolchain.toml .
6262

6363
echo "Starting cargo build with Kani"
64+
export RUST_BACKTRACE=1
6465
export RUSTC_LOG=error
6566
export KANIFLAGS="--goto-c --ignore-global-asm"
6667
export RUSTFLAGS="--kani-flags"

0 commit comments

Comments
 (0)