-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
cf2a35e
commit 436ce88
Showing
5 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |