Skip to content

Rust C Structure alignment mismatch? #19189

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

Closed
donpdonp opened this issue Nov 21, 2014 · 2 comments
Closed

Rust C Structure alignment mismatch? #19189

donpdonp opened this issue Nov 21, 2014 · 2 comments
Labels
A-FFI Area: Foreign function interface (FFI)

Comments

@donpdonp
Copy link

I'm trying to craft a binding from rust into mruby (sebastianedwards/rust-mruby is obsolete). In debugging a segfault in a foreign function, I get conflicting answers as to the alignment of a rust struct.

#[repr(C)]
struct MRubyValue {
  value: [u8, ..8],
  tt: MRubyType,
}

Printing the size and align gives

    println!("MRubyValue size {} align {}", std::mem::size_of::<MRubyValue>(),
                                            std::mem::align_of::<MRubyValue>());
=>
MRubyValue size 12 align 8

I found some code on the internet which supposedly prints the same information, but the alignment is different.

    let tyd =
        unsafe {
            (*std::intrinsics::get_tydesc::<T>())
        };
    println!("name: {} size: {} align: {}", tyd.name, tyd.size, tyd.align);
=>
name: MRubyValue size: 12 align: 4

Is this a rust problem or a misunderstanding of the output? Thanks.

@kmcallister kmcallister added the A-FFI Area: Foreign function interface (FFI) label Nov 23, 2014
@ben0x539
Copy link
Contributor

A non-doc comment for align_of suggests that

pub fn align_of<T>() -> uint {
    // We use the preferred alignment as the default alignment for a type. This
    // appears to be what clang migrated towards as well:
    //
    // http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20110725/044411.html
    unsafe { intrinsics::pref_align_of::<T>() }
}

I guess tydescs return the actual alignment. It's sort of weird to have a size that isn't a multiple of the alignment, isn't it? The second element in a [MRubyValue] wouldn't be aligned if align_of was right!

@steveklabnik
Copy link
Member

tydesc was removed in #23376, so this is moot now.

lnicola pushed a commit to lnicola/rust that referenced this issue Feb 24, 2025
minor: Improve unset `OUT_DIR` error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI)
Projects
None yet
Development

No branches or pull requests

4 participants