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

Miscompilation of tail call on x86_64: RSP is erroneously increased #109279

Open
cypheon opened this issue Sep 19, 2024 · 1 comment
Open

Miscompilation of tail call on x86_64: RSP is erroneously increased #109279

cypheon opened this issue Sep 19, 2024 · 1 comment

Comments

@cypheon
Copy link

cypheon commented Sep 19, 2024

In some cases, when calling a tailcc function in tail position, rsp is increased in a branch where it has never been decreased. This leads to corrupted stack pointer (and probably a segfault) in the following function.

Repro:

declare tailcc void @f2()
declare tailcc void @f1(ptr, i64, ptr, i8, ptr, ptr, ptr, ptr, ptr, ptr)

define tailcc void @repro(i64 %0) {
  %cond = icmp ugt i64 %0, 0
  br i1 %cond, label %a, label %b
a:
  musttail call tailcc void @f1(ptr null, i64 16, ptr null, i8 1, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null)
  ret void
b:
  musttail call tailcc void @f2()
  ret void
}

Resulting assembly:

repro:                                  # @repro
# %bb.0:
        test    rdi, rdi
        je      .LBB0_2
# %bb.1:                                # %a
        sub     rsp, 32
        xorps   xmm0, xmm0
        movaps  xmmword ptr [rsp + 8], xmm0
        mov     rax, qword ptr [rsp + 32]
        movaps  xmmword ptr [rsp + 24], xmm0
        mov     qword ptr [rsp], rax
        mov     esi, 16
        xor     edi, edi
        xor     edx, edx
        mov     ecx, 1
        xor     r8d, r8d
        xor     r9d, r9d
        jmp     f1@PLT                          # TAILCALL
.LBB0_2:                                # %b
        add     rsp, 32  # <- rsp should not be increased here, as it is only decreased in branch "%a"
        jmp     f2@PLT                          # TAILCALL
                                        # -- End function

https://godbolt.org/z/ze7r8j67o

As far as I can see, the issue does not occur on aarch64. There the stack pointer is decremented right at the beginning (before the branch), so the increment before the tail call is correct.

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 19, 2024

@llvm/issue-subscribers-backend-x86

Author: Johann Rudloff (cypheon)

In some cases, when calling a tailcc function in tail position, `rsp` is increased in a branch where it has never been decreased. This leads to corrupted stack pointer (and probably a segfault) in the following function.

Repro:

declare tailcc void @<!-- -->f2()
declare tailcc void @<!-- -->f1(ptr, i64, ptr, i8, ptr, ptr, ptr, ptr, ptr, ptr)

define tailcc void @<!-- -->repro(i64 %0) {
  %cond = icmp ugt i64 %0, 0
  br i1 %cond, label %a, label %b
a:
  musttail call tailcc void @<!-- -->f1(ptr null, i64 16, ptr null, i8 1, ptr null, ptr null, ptr null, ptr null, ptr null, ptr null)
  ret void
b:
  musttail call tailcc void @<!-- -->f2()
  ret void
}

Resulting assembly:

repro:                                  # @<!-- -->repro
# %bb.0:
        test    rdi, rdi
        je      .LBB0_2
# %bb.1:                                # %a
        sub     rsp, 32
        xorps   xmm0, xmm0
        movaps  xmmword ptr [rsp + 8], xmm0
        mov     rax, qword ptr [rsp + 32]
        movaps  xmmword ptr [rsp + 24], xmm0
        mov     qword ptr [rsp], rax
        mov     esi, 16
        xor     edi, edi
        xor     edx, edx
        mov     ecx, 1
        xor     r8d, r8d
        xor     r9d, r9d
        jmp     f1@<!-- -->PLT                          # TAILCALL
.LBB0_2:                                # %b
        add     rsp, 32  # &lt;- rsp should not be increased here, as it is only decreased in branch "%a"
        jmp     f2@<!-- -->PLT                          # TAILCALL
                                        # -- End function

https://godbolt.org/z/ze7r8j67o

As far as I can see, the issue does not occur on aarch64. There the stack pointer is decremented right at the beginning (before the branch), so the increment before the tail call is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants