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

Padding can create objects larger than isize::MAX bytes #117265

Closed
LegionMammal978 opened this issue Oct 27, 2023 · 1 comment · Fixed by #117277
Closed

Padding can create objects larger than isize::MAX bytes #117265

LegionMammal978 opened this issue Oct 27, 2023 · 1 comment · Fixed by #117277
Labels
A-layout Area: Memory layout of types C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@LegionMammal978
Copy link
Contributor

LegionMammal978 commented Oct 27, 2023

rustc generally detects when a static type is larger than isize::MAX bytes, giving an error that values of the type `...` are too big for the current architecture. However, if a type has fields at most isize::MAX bytes long, and additional padding that brings it to isize::MAX + 1 bytes, then this error will not be generated. To illustrate, all four of these print 0x80000000 (i.e., isize::MAX + 1):

// cargo build --release --target i686-unknown-linux-gnu
// prlimit --stack=unlimited target/i686-unknown-linux-gnu/release/example

use std::{hint, mem::{self, MaybeUninit}, alloc::Layout};

#[repr(C, align(2))]
struct Example(MaybeUninit<[u8; 0x7fffffff]>);

fn main() {
    println!("{:#x}", mem::size_of::<Example>());
    println!("{:#x}", Layout::new::<Example>().size());

    let e = Example(MaybeUninit::uninit());
    hint::black_box(&e);
    println!("{:#x}", mem::size_of_val(&e));
    println!("{:#x}", Layout::for_value(&e).size());
}

(The black_box() is to ensure that an object of size 0x80000000 is actually created on the stack.)

This is clearly unsound, since it breaks the size invariant of Layout, and since third-party crates may depend on types being no larger than isize::MAX for soundness. However, it's nontrivial to observe unexpected behavior from this using only safe APIs in the standard library, since placing the overlarge type within any other type, even a repr(transparent) wrapper, will result in a compile error as expected, and the standard APIs tend to refer to &[T; 1] when creating a slice from a reference.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 27, 2023
@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness C-bug Category: This is a bug. A-layout Area: Memory layout of types and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 27, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 27, 2023
@RalfJung
Copy link
Member

Wow, good catch! Looks like we're doing the check before rounding up to the next multiple of alignment? That's bad.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 28, 2023
…li-obk

fix failure to detect a too-big-type after adding padding

Fixes rust-lang#117265
@bors bors closed this as completed in 09fd68d Oct 28, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 28, 2023
Rollup merge of rust-lang#117277 - RalfJung:too-big-with-padding, r=oli-obk

fix failure to detect a too-big-type after adding padding

Fixes rust-lang#117265
@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layout Area: Memory layout of types C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants