forked from model-checking/kani
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_var.rs
More file actions
50 lines (45 loc) · 2.15 KB
/
Copy pathstatic_var.rs
File metadata and controls
50 lines (45 loc) · 2.15 KB
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
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! This file contains functions related to codegenning MIR static variables into gotoc
use crate::codegen_cprover_gotoc::GotocCtx;
use crate::kani_middle::is_interior_mut;
use rustc_public::CrateDef;
use rustc_public::mir::mono::{Instance, StaticDef};
use tracing::debug;
impl GotocCtx<'_, '_> {
/// Ensures a static variable is initialized.
///
/// Note that each static variable have their own location in memory. Per Rust documentation:
/// "statics declare global variables. These represent a memory address."
/// Source: <https://rust-lang.github.io/rfcs/0246-const-vs-static.html>
pub fn codegen_static(&mut self, def: StaticDef) {
debug!("codegen_static");
let alloc = def.eval_initializer().unwrap();
let symbol_name = Instance::from(def).mangled_name();
self.codegen_alloc_in_memory(
alloc,
symbol_name,
self.codegen_span_stable(def.span()),
is_interior_mut(self.tcx, def.ty()),
false,
);
}
/// Mutates the Goto-C symbol table to add a forward-declaration of the static variable.
pub fn declare_static(&mut self, def: StaticDef) {
let instance = Instance::from(def);
// Unique mangled monomorphized name.
let symbol_name = instance.mangled_name();
// Pretty name which may include function name.
let pretty_name = instance.name();
debug!(?def, ?symbol_name, ?pretty_name, "declare_static");
let typ = self.codegen_ty_stable(instance.ty());
let location = self.codegen_span_stable(def.span());
// Contracts instrumentation relies on `--nondet-static-exclude` to properly
// havoc static variables. Kani uses the location and pretty name to identify
// the correct variables. If the wrong name is used, CBMC may fail silently.
// More details at https://github.com/diffblue/cbmc/issues/8225.
self.ensure_global_var(symbol_name, false, typ, location)
.set_is_hidden(false) // Static items are always user defined.
.set_pretty_name(pretty_name);
}
}