Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit for transmute #1104

Merged
merged 8 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ impl<'tcx> GotocCtx<'tcx> {
"sqrtf32" => unstable_codegen!(codegen_simple_intrinsic!(Sqrtf)),
"sqrtf64" => unstable_codegen!(codegen_simple_intrinsic!(Sqrt)),
"sub_with_overflow" => codegen_op_with_overflow!(sub_overflow),
"transmute" => self.codegen_intrinsic_transmute(fargs, ret_ty, p),
"transmute" => self.codegen_intrinsic_transmute(instance, fargs, ret_ty, p, span),
"truncf32" => codegen_unimplemented_intrinsic!(
"https://github.com/model-checking/kani/issues/1025"
),
Expand Down Expand Up @@ -916,14 +916,41 @@ impl<'tcx> GotocCtx<'tcx> {
/// }
fn codegen_intrinsic_transmute(
&mut self,
instance: Instance<'tcx>,
mut fargs: Vec<Expr>,
ret_ty: Ty<'tcx>,
p: &Place<'tcx>,
span: Option<Span>,
) -> Stmt {
assert!(fargs.len() == 1, "transmute had unexpected arguments {:?}", fargs);
let loc = self.codegen_span_option(span);

// Check that the argument type is properly aligned
let arg = fargs.remove(0);
let expr = arg.transmute_to(self.codegen_ty(ret_ty), &self.symbol_table);
self.codegen_expr_to_place(p, expr)
let arg_typ = self.monomorphize(instance.substs.type_at(0));
let arg_align = self.is_aligned(arg_typ, arg.clone());
let arg_align_check = self.codegen_assert(
arg_align,
PropertyClass::DefaultAssertion,
"transmute: argument type is properly aligned",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The failure message usually states the failure. Maybe change it to something like: transmute check: argument type isn't properly aligned or transmute check: argument type must be properly aligned

loc.clone(),
);

// Check that the result type is properly aligned
let ret_expr = self.codegen_place(&p).unwrap().goto_expr;
let ret_align = self.is_aligned(ret_ty, ret_expr);
let ret_align_check = self.codegen_assert(
ret_align,
PropertyClass::DefaultAssertion,
"transmute: result type is properly aligned",
loc.clone(),
);

// Encode the actual transmute expression
let cbmc_ret_ty = self.codegen_ty(ret_ty);
let transmute_expr = arg.transmute_to(cbmc_ret_ty, &self.symbol_table);
let expr = self.codegen_expr_to_place(p, transmute_expr);
Stmt::block(vec![arg_align_check, ret_align_check, expr], loc)
}

// `raw_eq` determines whether the raw bytes of two values are equal.
Expand Down
2 changes: 2 additions & 0 deletions tests/expected/transmute-arg-align/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FAILURE\
transmute: argument type is properly aligned
17 changes: 17 additions & 0 deletions tests/expected/transmute-arg-align/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[repr(packed)]
struct Packed {
_padding: u8,
unaligned: u32,
}

#[kani::proof]
fn main() {
let packed: Packed = unsafe { std::mem::zeroed() };
unsafe {
let x = std::mem::transmute::<u32, u32>(packed.unaligned);
assert_eq!(x, 0);
}
}