Skip to content

Commit e862e49

Browse files
adpaco-awstedinski
authored andcommitted
Audit for transmute (rust-lang#1104)
* Audit for `transmute` * Add tests and minor fix * remove `mut` from `packed` * Restore original transmute codegen * Fixes transmute restoration * Remove alignment test * Add tests for transmute
1 parent 988f11f commit e862e49

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,10 @@ impl<'tcx> GotocCtx<'tcx> {
945945
/// bitpattern = *((unsigned int *)&temp_0);
946946
/// assert(bitpattern == 0x3F800000);
947947
/// }
948+
///
949+
/// Note(std): An earlier attempt to add alignment checks for both the argument and result types
950+
/// had catastrophic results in the regression. Hence, we don't perform any additional checks
951+
/// and only encode the transmute operation here.
948952
fn codegen_intrinsic_transmute(
949953
&mut self,
950954
mut fargs: Vec<Expr>,
@@ -953,7 +957,8 @@ impl<'tcx> GotocCtx<'tcx> {
953957
) -> Stmt {
954958
assert!(fargs.len() == 1, "transmute had unexpected arguments {:?}", fargs);
955959
let arg = fargs.remove(0);
956-
let expr = arg.transmute_to(self.codegen_ty(ret_ty), &self.symbol_table);
960+
let cbmc_ret_ty = self.codegen_ty(ret_ty);
961+
let expr = arg.transmute_to(cbmc_ret_ty, &self.symbol_table);
957962
self.codegen_expr_to_place(p, expr)
958963
}
959964

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Checks that `transmute` works as expected when turning an array into
5+
// a struct.
6+
7+
struct Pair {
8+
fst: u16,
9+
snd: u16,
10+
}
11+
12+
#[kani::proof]
13+
fn main() {
14+
let arr = [0; 4];
15+
let pair = unsafe { std::mem::transmute::<[u8; 4], Pair>(arr) };
16+
assert_eq!(pair.fst, 0);
17+
assert_eq!(pair.snd, 0);
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Checks that `transmute` works as expected when turning raw bytes into
5+
// a `u32`.
6+
7+
// This test is a modified version of the example found in
8+
// https://doc.rust-lang.org/std/intrinsics/fn.transmute.html
9+
10+
#[kani::proof]
11+
fn main() {
12+
let raw_bytes = [0x78, 0x56, 0x34, 0x12];
13+
let num = unsafe { std::mem::transmute::<[u8; 4], u32>(raw_bytes) };
14+
assert_eq!(num, 0x12345678);
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Checks that `transmute` works as expected when turning a pointer into
5+
// a function pointer.
6+
7+
// This test is a modified version of the example found in
8+
// https://doc.rust-lang.org/std/intrinsics/fn.transmute.html
9+
10+
fn foo() -> i32 {
11+
0
12+
}
13+
14+
#[kani::proof]
15+
fn main() {
16+
let pointer = foo as *const ();
17+
let function = unsafe { std::mem::transmute::<*const (), fn() -> i32>(pointer) };
18+
assert_eq!(function(), 0);
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Checks that `transmute` works as expected when turning a `str` into
5+
// `&[u8]`.
6+
7+
// This test is a modified version of the example found in
8+
// https://doc.rust-lang.org/std/intrinsics/fn.transmute.html
9+
10+
#[kani::proof]
11+
fn main() {
12+
let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
13+
assert_eq!(slice, &[82, 117, 115, 116]);
14+
}

0 commit comments

Comments
 (0)