Skip to content

Commit 6dc5fcc

Browse files
danielsntedinski
authored andcommitted
Make a seperate crate for CBMC (rust-lang#512)
1 parent 060b111 commit 6dc5fcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+197
-117
lines changed

Cargo.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,37 @@ dependencies = [
455455
name = "cargotest2"
456456
version = "0.1.0"
457457

458+
[[package]]
459+
name = "cbmc"
460+
version = "0.0.0"
461+
dependencies = [
462+
"bitflags",
463+
"cstr",
464+
"libc",
465+
"measureme",
466+
"num",
467+
"rustc-demangle",
468+
"rustc_arena",
469+
"rustc_ast",
470+
"rustc_attr",
471+
"rustc_codegen_ssa",
472+
"rustc_data_structures",
473+
"rustc_errors",
474+
"rustc_fs_util",
475+
"rustc_hir",
476+
"rustc_index",
477+
"rustc_llvm",
478+
"rustc_metadata",
479+
"rustc_middle",
480+
"rustc_serialize",
481+
"rustc_session",
482+
"rustc_span",
483+
"rustc_target",
484+
"smallvec",
485+
"snap",
486+
"tracing",
487+
]
488+
458489
[[package]]
459490
name = "cc"
460491
version = "1.0.69"
@@ -3766,6 +3797,7 @@ name = "rustc_codegen_rmc"
37663797
version = "0.0.0"
37673798
dependencies = [
37683799
"bitflags",
3800+
"cbmc",
37693801
"cstr",
37703802
"libc",
37713803
"measureme",

compiler/cbmc/Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
[package]
5+
name = "cbmc"
6+
version = "0.0.0"
7+
edition = "2018"
8+
9+
[lib]
10+
test = true
11+
doctest = false
12+
13+
[dependencies]
14+
bitflags = "1.0"
15+
cstr = "0.2"
16+
libc = "0.2"
17+
measureme = "9.1.0"
18+
num = "0.4.0"
19+
snap = "1"
20+
tracing = "0.1"
21+
rustc_middle = { path = "../rustc_middle" }
22+
rustc-demangle = "0.1.21"
23+
rustc_arena = { path = "../rustc_arena" }
24+
rustc_attr = { path = "../rustc_attr" }
25+
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
26+
rustc_data_structures = { path = "../rustc_data_structures" }
27+
rustc_errors = { path = "../rustc_errors" }
28+
rustc_fs_util = { path = "../rustc_fs_util" }
29+
rustc_hir = { path = "../rustc_hir" }
30+
rustc_index = { path = "../rustc_index" }
31+
rustc_llvm = { path = "../rustc_llvm" }
32+
rustc_metadata = { path = "../rustc_metadata" }
33+
rustc_session = { path = "../rustc_session" }
34+
rustc_serialize = { path = "../rustc_serialize" }
35+
rustc_target = { path = "../rustc_target" }
36+
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
37+
rustc_ast = { path = "../rustc_ast" }
38+
rustc_span = { path = "../rustc_span" }
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::ops::{BitAnd, Shl, Shr};
55

66
use super::super::Transformer;
7-
use crate::cbmc::goto_program::{
7+
use crate::goto_program::{
88
BinaryOperand, CIntType, Expr, Location, Parameter, Stmt, Symbol, SymbolTable, SymbolValues,
99
Type,
1010
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use super::super::Transformer;
5-
use crate::cbmc::goto_program::{
5+
use crate::goto_program::{
66
DatatypeComponent, Expr, Location, Parameter, Stmt, Symbol, SymbolTable, Type,
77
};
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use super::super::Transformer;
5-
use crate::cbmc::goto_program::{Expr, Location, Stmt, Symbol, SymbolTable, Type};
5+
use crate::goto_program::{Expr, Location, Stmt, Symbol, SymbolTable, Type};
66
use rustc_data_structures::fx::FxHashMap;
77

88
/// Struct for handling the nondet transformations for --gen-c-runnable.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

44
use super::Transformer;
5-
use crate::cbmc::goto_program::SymbolTable;
5+
use crate::goto_program::SymbolTable;
66

77
/// Struct for performing the identity transformation on a symbol table.
88
/// Mainly used as a demo/for testing.

compiler/rustc_codegen_rmc/src/cbmc/goto_program/symtab_transformer/passes.rs renamed to compiler/cbmc/src/goto_program/symtab_transformer/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use super::gen_c_transformer::{ExprTransformer, NameTransformer, NondetTransformer};
55
use super::identity_transformer::IdentityTransformer;
6-
use crate::cbmc::goto_program::SymbolTable;
6+
use crate::goto_program::SymbolTable;
77

88
/// Performs each pass provided on the given symbol table.
99
pub fn do_passes(mut symtab: SymbolTable, pass_names: &[String]) -> SymbolTable {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
use crate::cbmc::goto_program::{
4+
use crate::goto_program::{
55
BinaryOperand, CIntType, DatatypeComponent, Expr, ExprValue, Location, Parameter, SelfOperand,
66
Stmt, StmtBody, SwitchCase, Symbol, SymbolTable, SymbolValues, Type, UnaryOperand,
77
};
File renamed without changes.

compiler/rustc_codegen_rmc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ doctest = false
1212

1313
[dependencies]
1414
bitflags = "1.0"
15+
cbmc = { path = "../cbmc" }
1516
cstr = "0.2"
1617
libc = "0.2"
1718
measureme = "9.1.0"

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/assumptions.rs renamed to compiler/rustc_codegen_rmc/src/codegen/assumptions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33
//! this module defines functions which impose data invariant on generated data types.
44
5-
use crate::cbmc::goto_program::{Expr, Location, Stmt, Symbol, Type};
6-
use crate::mir_to_goto::GotocCtx;
5+
use crate::GotocCtx;
6+
use cbmc::goto_program::{Expr, Location, Stmt, Symbol, Type};
77
use rustc_middle::mir::interpret::{ConstValue, Scalar};
88
use rustc_middle::ty;
99
use rustc_middle::ty::layout::LayoutOf;

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/block.rs renamed to compiler/rustc_codegen_rmc/src/codegen/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//! This file contains functions related to codegenning MIR blocks into gotoc
55
6-
use crate::mir_to_goto::GotocCtx;
6+
use crate::GotocCtx;
77
use rustc_middle::mir::{BasicBlock, BasicBlockData};
88

99
impl<'tcx> GotocCtx<'tcx> {

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/function.rs renamed to compiler/rustc_codegen_rmc/src/codegen/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//! This file contains functions related to codegenning MIR functions into gotoc
55
6-
use crate::cbmc::goto_program::{Expr, Stmt, Symbol};
7-
use crate::mir_to_goto::GotocCtx;
6+
use crate::GotocCtx;
7+
use cbmc::goto_program::{Expr, Stmt, Symbol};
88
use rustc_middle::mir::{HasLocalDecls, Local};
99
use rustc_middle::ty::{self, Instance, TyS};
1010
use tracing::{debug, warn};

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/intrinsic.rs renamed to compiler/rustc_codegen_rmc/src/codegen/intrinsic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! this module handles intrinsics
44
use tracing::{debug, warn};
55

6-
use crate::cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Type};
7-
use crate::mir_to_goto::GotocCtx;
6+
use crate::GotocCtx;
7+
use cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Type};
88
use rustc_middle::mir::Place;
99
use rustc_middle::ty::layout::LayoutOf;
1010
use rustc_middle::ty::Instance;

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/mod.rs renamed to compiler/rustc_codegen_rmc/src/codegen/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ mod span;
1515
mod statement;
1616
mod static_var;
1717
mod typ;
18+
19+
pub use typ::TypeExt;

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/operand.rs renamed to compiler/rustc_codegen_rmc/src/codegen/operand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3-
use crate::cbmc::goto_program::{Expr, Location, Stmt, Symbol, Type};
4-
use crate::mir_to_goto::utils::slice_fat_ptr;
5-
use crate::mir_to_goto::GotocCtx;
3+
use crate::utils::slice_fat_ptr;
4+
use crate::GotocCtx;
5+
use cbmc::goto_program::{Expr, Location, Stmt, Symbol, Type};
66
use rustc_ast::ast::Mutability;
77
use rustc_middle::mir::interpret::{
88
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, Scalar,

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/place.rs renamed to compiler/rustc_codegen_rmc/src/codegen/place.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
//! a place is an expression of specifying a location in memory, like a left value. check the cases
66
//! in [codegen_place] below.
77
8-
use crate::cbmc::goto_program::{Expr, Type};
9-
use crate::mir_to_goto::utils::slice_fat_ptr;
10-
use crate::mir_to_goto::GotocCtx;
8+
use super::typ::TypeExt;
9+
use crate::utils::slice_fat_ptr;
10+
use crate::GotocCtx;
11+
use cbmc::goto_program::{Expr, Type};
1112
use rustc_hir::Mutability;
1213
use rustc_middle::ty::layout::LayoutOf;
1314
use rustc_middle::{

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/rvalue.rs renamed to compiler/rustc_codegen_rmc/src/codegen/rvalue.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3-
use super::typ::{is_pointer, pointee_type};
4-
use crate::btree_string_map;
5-
use crate::cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Symbol, Type};
6-
use crate::cbmc::utils::{aggr_name, BUG_REPORT_URL};
7-
use crate::cbmc::MachineModel;
8-
use crate::mir_to_goto::utils::{dynamic_fat_ptr, slice_fat_ptr};
9-
use crate::mir_to_goto::GotocCtx;
3+
use super::typ::{is_pointer, pointee_type, TypeExt};
4+
use crate::utils::{dynamic_fat_ptr, slice_fat_ptr};
5+
use crate::GotocCtx;
6+
use cbmc::btree_string_map;
7+
use cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Symbol, Type};
8+
use cbmc::utils::{aggr_name, BUG_REPORT_URL};
9+
use cbmc::MachineModel;
1010
use num::bigint::BigInt;
1111
use rustc_middle::mir::{AggregateKind, BinOp, CastKind, NullOp, Operand, Place, Rvalue, UnOp};
1212
use rustc_middle::ty::adjustment::PointerCast;

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/span.rs renamed to compiler/rustc_codegen_rmc/src/codegen/span.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//! MIR Span related functions
55
6-
use crate::cbmc::goto_program::Location;
7-
use crate::mir_to_goto::GotocCtx;
6+
use crate::GotocCtx;
7+
use cbmc::goto_program::Location;
88
use rustc_middle::mir::{Local, VarDebugInfo, VarDebugInfoContents};
99
use rustc_span::Span;
1010

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/statement.rs renamed to compiler/rustc_codegen_rmc/src/codegen/statement.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
use super::typ::TypeExt;
34
use super::typ::FN_RETURN_VOID_VAR_NAME;
4-
use crate::cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Type};
5-
use crate::mir_to_goto::GotocCtx;
5+
use crate::GotocCtx;
6+
use cbmc::goto_program::{BuiltinFn, Expr, Location, Stmt, Type};
67
use rustc_hir::def_id::DefId;
78
use rustc_middle::mir;
89
use rustc_middle::mir::{

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/static_var.rs renamed to compiler/rustc_codegen_rmc/src/codegen/static_var.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//! This file contains functions related to codegenning MIR static variables into gotoc
55
6-
use crate::cbmc::goto_program::Symbol;
7-
use crate::mir_to_goto::GotocCtx;
6+
use crate::GotocCtx;
7+
use cbmc::goto_program::Symbol;
88
use rustc_hir::def_id::DefId;
99
use rustc_middle::mir::mono::MonoItem;
1010
use tracing::debug;

compiler/rustc_codegen_rmc/src/mir_to_goto/codegen/typ.rs renamed to compiler/rustc_codegen_rmc/src/codegen/typ.rs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
3-
use crate::btree_map;
4-
use crate::cbmc::goto_program::{DatatypeComponent, Expr, Parameter, Symbol, SymbolTable, Type};
5-
use crate::cbmc::utils::aggr_name;
6-
use crate::mir_to_goto::GotocCtx;
3+
use crate::GotocCtx;
4+
use cbmc::btree_map;
5+
use cbmc::goto_program::{DatatypeComponent, Expr, Parameter, Symbol, SymbolTable, Type};
6+
use cbmc::utils::aggr_name;
77
use rustc_ast::ast::Mutability;
88
use rustc_index::vec::IndexVec;
99
use rustc_middle::mir::{HasLocalDecls, Local, Operand, Place, Rvalue};
@@ -36,38 +36,82 @@ use ty::layout::HasParamEnv;
3636
const UNIT_TYPE_EMPTY_STRUCT_NAME: &str = "Unit";
3737
pub const FN_RETURN_VOID_VAR_NAME: &str = "VoidUnit";
3838

39-
impl Type {
40-
pub fn unit() -> Self {
39+
pub trait TypeExt {
40+
fn is_rust_fat_ptr(&self, st: &SymbolTable) -> bool;
41+
fn is_rust_slice_fat_ptr(&self, st: &SymbolTable) -> bool;
42+
fn is_rust_trait_fat_ptr(&self, st: &SymbolTable) -> bool;
43+
fn is_unit(&self) -> bool;
44+
fn is_unit_pointer(&self) -> bool;
45+
fn unit() -> Self;
46+
}
47+
48+
impl TypeExt for Type {
49+
fn is_rust_slice_fat_ptr(&self, st: &SymbolTable) -> bool {
50+
match self {
51+
Type::Struct { components, .. } => {
52+
components.len() == 2
53+
&& components.iter().any(|x| x.name() == "data" && x.typ().is_pointer())
54+
&& components.iter().any(|x| x.name() == "len" && x.typ().is_integer())
55+
}
56+
Type::StructTag(tag) => st.lookup(tag).unwrap().typ.is_rust_slice_fat_ptr(st),
57+
_ => false,
58+
}
59+
}
60+
61+
fn is_rust_trait_fat_ptr(&self, st: &SymbolTable) -> bool {
62+
match self {
63+
Type::Struct { components, .. } => {
64+
components.len() == 2
65+
&& components.iter().any(|x| x.name() == "data" && x.typ().is_pointer())
66+
&& components.iter().any(|x| x.name() == "vtable" && x.typ().is_pointer())
67+
}
68+
Type::StructTag(tag) => st.lookup(tag).unwrap().typ.is_rust_trait_fat_ptr(st),
69+
_ => false,
70+
}
71+
}
72+
73+
fn is_rust_fat_ptr(&self, st: &SymbolTable) -> bool {
74+
self.is_rust_slice_fat_ptr(st) || self.is_rust_trait_fat_ptr(st)
75+
}
76+
77+
fn unit() -> Self {
4178
// We depend on GotocCtx::codegen_ty_unit() to put the type in the symbol table.
4279
// We don't have access to the symbol table here to do it ourselves.
4380
Type::struct_tag(UNIT_TYPE_EMPTY_STRUCT_NAME)
4481
}
4582

46-
pub fn is_unit(&self) -> bool {
83+
fn is_unit(&self) -> bool {
4784
match self {
4885
Type::StructTag(name) => *name == aggr_name(UNIT_TYPE_EMPTY_STRUCT_NAME),
4986
_ => false,
5087
}
5188
}
5289

53-
pub fn is_unit_pointer(&self) -> bool {
90+
fn is_unit_pointer(&self) -> bool {
5491
match self {
5592
Type::Pointer { typ } => typ.is_unit(),
5693
_ => false,
5794
}
5895
}
5996
}
97+
trait ExprExt {
98+
fn unit(symbol_table: &SymbolTable) -> Self;
99+
100+
fn is_unit(&self) -> bool;
101+
102+
fn is_unit_pointer(&self) -> bool;
103+
}
60104

61-
impl Expr {
62-
pub fn unit(symbol_table: &SymbolTable) -> Self {
105+
impl ExprExt for Expr {
106+
fn unit(symbol_table: &SymbolTable) -> Self {
63107
Expr::struct_expr(Type::unit(), btree_map![], symbol_table)
64108
}
65109

66-
pub fn is_unit(&self) -> bool {
110+
fn is_unit(&self) -> bool {
67111
self.typ().is_unit()
68112
}
69113

70-
pub fn is_unit_pointer(&self) -> bool {
114+
fn is_unit_pointer(&self) -> bool {
71115
self.typ().is_unit_pointer()
72116
}
73117
}

compiler/rustc_codegen_rmc/src/mir_to_goto/compiler_interface.rs renamed to compiler/rustc_codegen_rmc/src/compiler_interface.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
//! This file contains the code necessary to interface with the compiler backend
55
6-
use crate::cbmc::goto_program::symtab_transformer;
7-
use crate::cbmc::goto_program::SymbolTable;
8-
use crate::mir_to_goto::overrides::skip_monomorphize;
9-
use crate::mir_to_goto::GotocCtx;
6+
use crate::overrides::skip_monomorphize;
7+
use crate::GotocCtx;
108
use bitflags::_core::any::Any;
9+
use cbmc::goto_program::symtab_transformer;
10+
use cbmc::goto_program::SymbolTable;
1111
use rustc_codegen_ssa::traits::CodegenBackend;
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_errors::ErrorReported;

0 commit comments

Comments
 (0)