Skip to content

Commit

Permalink
Audit for transmute (#1104)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adpaco-aws authored Apr 26, 2022
1 parent cf2a35e commit 436ce88
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
7 changes: 6 additions & 1 deletion kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,10 @@ impl<'tcx> GotocCtx<'tcx> {
/// bitpattern = *((unsigned int *)&temp_0);
/// assert(bitpattern == 0x3F800000);
/// }
///
/// Note(std): An earlier attempt to add alignment checks for both the argument and result types
/// had catastrophic results in the regression. Hence, we don't perform any additional checks
/// and only encode the transmute operation here.
fn codegen_intrinsic_transmute(
&mut self,
mut fargs: Vec<Expr>,
Expand All @@ -953,7 +957,8 @@ impl<'tcx> GotocCtx<'tcx> {
) -> Stmt {
assert!(fargs.len() == 1, "transmute had unexpected arguments {:?}", fargs);
let arg = fargs.remove(0);
let expr = arg.transmute_to(self.codegen_ty(ret_ty), &self.symbol_table);
let cbmc_ret_ty = self.codegen_ty(ret_ty);
let expr = arg.transmute_to(cbmc_ret_ty, &self.symbol_table);
self.codegen_expr_to_place(p, expr)
}

Expand Down
18 changes: 18 additions & 0 deletions tests/kani/Intrinsics/Transmute/arr_to_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Checks that `transmute` works as expected when turning an array into
// a struct.

struct Pair {
fst: u16,
snd: u16,
}

#[kani::proof]
fn main() {
let arr = [0; 4];
let pair = unsafe { std::mem::transmute::<[u8; 4], Pair>(arr) };
assert_eq!(pair.fst, 0);
assert_eq!(pair.snd, 0);
}
15 changes: 15 additions & 0 deletions tests/kani/Intrinsics/Transmute/bytes_to_u32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Checks that `transmute` works as expected when turning raw bytes into
// a `u32`.

// This test is a modified version of the example found in
// https://doc.rust-lang.org/std/intrinsics/fn.transmute.html

#[kani::proof]
fn main() {
let raw_bytes = [0x78, 0x56, 0x34, 0x12];
let num = unsafe { std::mem::transmute::<[u8; 4], u32>(raw_bytes) };
assert_eq!(num, 0x12345678);
}
19 changes: 19 additions & 0 deletions tests/kani/Intrinsics/Transmute/ptr_to_fn_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Checks that `transmute` works as expected when turning a pointer into
// a function pointer.

// This test is a modified version of the example found in
// https://doc.rust-lang.org/std/intrinsics/fn.transmute.html

fn foo() -> i32 {
0
}

#[kani::proof]
fn main() {
let pointer = foo as *const ();
let function = unsafe { std::mem::transmute::<*const (), fn() -> i32>(pointer) };
assert_eq!(function(), 0);
}
14 changes: 14 additions & 0 deletions tests/kani/Intrinsics/Transmute/str_to_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Checks that `transmute` works as expected when turning a `str` into
// `&[u8]`.

// This test is a modified version of the example found in
// https://doc.rust-lang.org/std/intrinsics/fn.transmute.html

#[kani::proof]
fn main() {
let slice = unsafe { std::mem::transmute::<&str, &[u8]>("Rust") };
assert_eq!(slice, &[82, 117, 115, 116]);
}

0 comments on commit 436ce88

Please sign in to comment.