Skip to content

Commit 3de3498

Browse files
celinvaltedinski
authored andcommitted
Remove temporary variables from trace (rust-lang#817)
These changes set all temporary variables as hidden. The cbmc trace will no longer include them.
1 parent b99e02f commit 3de3498

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

src/kani-compiler/cbmc/src/goto_program/symbol.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Symbol {
9090
is_state_var: false,
9191
is_type: false,
9292
// ansi-C properties
93-
is_auxiliary: false,
93+
is_auxiliary: true,
9494
is_extern: false,
9595
is_file_local: false,
9696
is_lvalue: false,
@@ -297,6 +297,11 @@ impl Symbol {
297297
self.pretty_name = Some(pretty_name.into());
298298
self
299299
}
300+
301+
pub fn with_is_hidden(mut self, hidden: bool) -> Symbol {
302+
self.is_auxiliary = hidden;
303+
self
304+
}
300305
}
301306

302307
/// Predicates

src/kani-compiler/rustc_codegen_kani/src/codegen/function.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ impl<'tcx> GotocCtx<'tcx> {
5353
let t = self.codegen_ty(t);
5454
let loc = self.codegen_span(&ldata.source_info.span);
5555
let sym =
56-
Symbol::variable(name, base_name, t, self.codegen_span(&ldata.source_info.span));
56+
Symbol::variable(name, base_name, t, self.codegen_span(&ldata.source_info.span))
57+
.with_is_hidden(!ldata.is_user_variable());
5758
let sym_e = sym.to_expr();
5859
self.symbol_table.insert(sym);
5960

@@ -194,7 +195,8 @@ impl<'tcx> GotocCtx<'tcx> {
194195
// This follows the naming convention defined in `typ.rs`.
195196
let lc = Local::from_usize(arg_i + starting_idx);
196197
let (name, base_name) = self.codegen_spread_arg_name(&lc);
197-
let sym = Symbol::variable(name, base_name, self.codegen_ty(arg_t), loc.clone());
198+
let sym = Symbol::variable(name, base_name, self.codegen_ty(arg_t), loc.clone())
199+
.with_is_hidden(false);
198200
// The spread arguments are additional function paramaters that are patched in
199201
// They are to the function signature added in the `fn_typ` function.
200202
// But they were never added to the symbol table, which we currently do here.

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ use crate::GotocCtx;
77
use cbmc::goto_program::Symbol;
88
use rustc_hir::def_id::DefId;
99
use rustc_middle::mir::mono::MonoItem;
10+
use rustc_middle::ty::{subst::InternalSubsts, Instance};
1011
use tracing::debug;
1112

13+
/// Separator used to generate function static variable names (<function_name>::<variable_name>).
14+
const SEPARATOR: &str = "::";
15+
1216
impl<'tcx> GotocCtx<'tcx> {
1317
pub fn codegen_static(&mut self, def_id: DefId, item: MonoItem<'tcx>) {
1418
debug!("codegen_static");
@@ -18,12 +22,21 @@ impl<'tcx> GotocCtx<'tcx> {
1822
}
1923

2024
pub fn declare_static(&mut self, def_id: DefId, item: MonoItem<'tcx>) {
21-
debug!("declare_static {:?}", def_id);
25+
// Unique mangled monomorphized name.
2226
let symbol_name = item.symbol_name(self.tcx).to_string();
27+
// Pretty name which may include function name.
28+
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);
33+
2334
let typ = self.codegen_ty(self.tcx.type_of(def_id));
2435
let span = self.tcx.def_span(def_id);
2536
let location = self.codegen_span(&span);
26-
let symbol = Symbol::static_variable(symbol_name.to_string(), symbol_name, typ, location);
37+
let symbol = Symbol::static_variable(symbol_name.to_string(), base_name, typ, location)
38+
.with_is_hidden(false) // Static items are always user defined.
39+
.with_pretty_name(pretty_name);
2740
self.symbol_table.insert(symbol);
2841
}
2942
}

src/kani-compiler/rustc_codegen_kani/src/context/goto_ctx.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ impl<'tcx> GotocCtx<'tcx> {
182182
) -> Expr {
183183
let name = name.into();
184184
if !self.symbol_table.contains(name) {
185+
tracing::debug!(?name, "Ensure global variable");
185186
let sym = Symbol::static_variable(name, name, t.clone(), loc)
186-
.with_is_file_local(is_file_local);
187+
.with_is_file_local(is_file_local)
188+
.with_is_hidden(false);
187189
let var = sym.to_expr();
188190
self.symbol_table.insert(sym);
189191
if let Some(body) = init_fn(self, var) {
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
//! This test checks static variables declared inside methods are still unique.
5+
6+
// There should be no name collision.
7+
static counter: i8 = 0;
8+
9+
fn new_id() -> i8 {
10+
static mut counter: i8 = 0;
11+
unsafe {
12+
counter += 1;
13+
counter
14+
}
15+
}
16+
17+
fn main() {
18+
let id_1 = new_id();
19+
let id_2 = new_id();
20+
assert!(id_1 == 1);
21+
assert!(id_2 == 2);
22+
assert!(counter == 0);
23+
}

0 commit comments

Comments
 (0)