Skip to content

Naked functions generate unsound code #52836

@roblabla

Description

@roblabla

https://github.com/roblabla/unwind-rs/blob/d27dfb13a2dd663d89ba67257bc050b8e76febde/unwind/src/glue/aarch64.rs#L14

This function generates the following ASM:

str    x0, [sp, #8] ; wtf
mov    x1, sp
sub    sp, sp, #0xa0
stp    x19, x20, [sp]
stp    x21, x22, [sp, #16]
stp    x23, x24, [sp, #32]
stp    x25, x26, [sp, #48]
stp    x27, x28, [sp, #64]
stp    x29, x30, [sp, #80]
stp    d8, d9, [sp, #96]
stp    d10, d11, [sp, #112]
stp    d12, d13, [sp, #128]
stp    d14, d15, [sp, #144]
mov    x2, sp
bl     0xaaaaaac35dd8 <unwind_recorder>
ldr    x30, [sp, #88]
add    sp, sp, #0xa0
ret

The first instruction was not part of the asm! macro. It seems to be a stack slot for the first argument. However, since the prelude got removed, sp will not get updated to a new size, and this will write over whatever was in the parent's stack.

I tried to reproduce it on godbolt, but was unable to do so. It seems to depend on optimization levels and size of the project/code ? I have a similar problem on i386 in another project, so this is not arch-specific.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-nakedArea: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions