-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RTS: remove repr(packed) attributes from RTS structs (#2764)
This is to remove some of the potential undefined behaviors (UB). It will also remove some of the syntactic noise in #2761. Rust has alignment restrictions for types and fields beyond the hardware limitations. This means even on Wasm (which has no alignment restrictions) we have some restrictions to deal with. Recently the compiler started to check for some of the obvious sources of UB (rust-lang/rust#82523). One of these is when taking a pointer or reference to a `packed` struct. Since `packed` means no padding between fields, the fields may be unaligned, in which case getting a reference to them would be UB. To fix this, this PR removes `packed` attributes and refactors the `Bits64` type to make sure that the layout is as before. It turns out for types other than `Bits64` we don't need `packed`: all fields are word sizes so the compiler does not add any padding. For `Bits64`, we had a `u64` field which is aligned on 64-bit boundary. To avoid this we now split 64-bit payload into two 32-bit fields. A new module `static_checks` added to make sure struct sizes are as expected. Assertions in this module are checked in compile time. No extra work needed to align objects. All objects need 4 bytes alignment (checked with `core::mem::align_of`). The compiler already [aligns static objects][2], and in runtime we only allocate whole words. So both static and dynamic objects are always aligned. Debug mode assertions added in GC to check object alignment. [1]: https://doc.rust-lang.org/stable/reference/ [2]: https://github.com/dfinity/motoko/blob/59ddaa4520793b6e7038b60e3c30ff267d8415f6/src/codegen/compile.ml#L453
- Loading branch information
Showing
11 changed files
with
90 additions
and
20 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,54 @@ | ||
//! Static assertions to make sure object layouts are as expected | ||
|
||
use crate::types::*; | ||
|
||
use core::mem::{align_of, size_of}; | ||
|
||
use static_assertions::const_assert_eq; | ||
|
||
// TODO: I don't understand why I get a "unused constant" warning for this. Removing it causes | ||
// compilation failures as expected. | ||
#[allow(unused)] | ||
const WORD_SIZE: usize = crate::constants::WORD_SIZE as usize; | ||
|
||
// Check platform word size | ||
const_assert_eq!(size_of::<usize>(), size_of::<u32>()); | ||
const_assert_eq!(size_of::<usize>(), WORD_SIZE); | ||
|
||
// Check that sizes of structs are as expected by the compiler | ||
// (Expectations are all over the place, e.g. `header_size` definitions in `compile.ml`, calls to `static_closure`, etc.) | ||
const_assert_eq!(size_of::<Obj>(), 1 * WORD_SIZE); | ||
const_assert_eq!(size_of::<ObjInd>(), 2 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Closure>(), 3 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Blob>(), 2 * WORD_SIZE); | ||
const_assert_eq!(size_of::<BigInt>(), 5 * WORD_SIZE); | ||
const_assert_eq!(size_of::<MutBox>(), 2 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Some>(), 2 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Variant>(), 3 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Concat>(), 4 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Null>(), 1 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Bits32>(), 2 * WORD_SIZE); | ||
const_assert_eq!(size_of::<Bits64>(), 3 * WORD_SIZE); | ||
|
||
// These aren't used generated by the compiler | ||
const_assert_eq!(size_of::<OneWordFiller>(), 1 * WORD_SIZE); | ||
const_assert_eq!(size_of::<FreeSpace>(), 2 * WORD_SIZE); | ||
const_assert_eq!(size_of::<FwdPtr>(), 2 * WORD_SIZE); | ||
|
||
// Check that objects need to be aligned on word boundaries. Having a different alignment | ||
// restriction an object type would require changing allocation routines for it. | ||
const_assert_eq!(align_of::<Obj>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<ObjInd>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Closure>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Blob>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<BigInt>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<MutBox>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Some>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Variant>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Concat>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Null>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Bits32>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<Bits64>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<OneWordFiller>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<FreeSpace>(), WORD_SIZE); | ||
const_assert_eq!(align_of::<FwdPtr>(), WORD_SIZE); |
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