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.
See the following extreme test-case:
This is what godbolt says rust stable does with it with -O:
That is, it allocates 1024 bytes (expected), writes
42directly at the allocated location, and then proceeds to copy 1016 (uninitialized) bytes from the stack.