Skip to content

Useless copies of alignment padding #56297

Closed
@glandium

Description

@glandium

See the following extreme test-case:

#[repr(align(1024))]
pub struct Foo(usize);

pub fn foo() -> Box<Foo> {
    Box::new(Foo(42))
}

This is what godbolt says rust stable does with it with -O:

        push    rbx
        sub     rsp, 1024
        mov     edi, 1024
        mov     esi, 1024
        call    __rust_alloc@PLT
        test    rax, rax
        je      .LBB0_1
        mov     rbx, rax
        mov     qword ptr [rax], 42
        mov     rdi, rax
        add     rdi, 8
        lea     rsi, [rsp + 8]
        mov     edx, 1016
        call    memcpy@PLT
        mov     rax, rbx
        add     rsp, 1024
        pop     rbx
        ret
.LBB0_1:
        mov     edi, 1024
        mov     esi, 1024
        call    alloc::alloc::handle_alloc_error@PLT

That is, it allocates 1024 bytes (expected), writes 42 directly at the allocated location, and then proceeds to copy 1016 (uninitialized) bytes from the stack.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions