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

musttail + no_caller_saved_registers + x86 PIC = bad jmp #53327

Open
easyaspi314 opened this issue Jan 20, 2022 · 1 comment
Open

musttail + no_caller_saved_registers + x86 PIC = bad jmp #53327

easyaspi314 opened this issue Jan 20, 2022 · 1 comment

Comments

@easyaspi314
Copy link

easyaspi314 commented Jan 20, 2022

Don't ask me how I stumbled on this one.

The bug

LLVM will load the GOT address of bar into eax, then pop eax, then jump to the address in eax register.

Since eax is popped from the stack, it jumps to a garbage address.

What is expected to happen

Either LLVM must

  1. clobber eax
  2. push the address on to the stack then ret (does that mess with CET?)
  3. or not allow musttail to be used with this attribute.

example code

void foo(void);
__attribute__((no_caller_saved_registers))
void bar(void)
{
    __attribute__((musttail))
    return foo();
}

clang --target=i386-linux-gnu -fPIC -march=i386 -O2 -S file.c -masm=intel

        .text
        .intel_syntax noprefix
        .file   "test.c"
        .globl  bar                             # -- Begin function bar
        .p2align        4, 0x90
        .type   bar,@function
bar:                                    # @bar
# %bb.0:
        push    edx
        push    ecx
        push    eax
        call    .L0$pb
.L0$pb:
        pop     eax
.Ltmp0:
        add     eax, offset _GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb)
        # load the function pointer into eax
        mov     eax, dword ptr [eax + foo@GOT]
        # then immediately pop eax from the stack?????
        pop     eax
        pop     ecx
        pop     edx
        # then jump to eax which is not a function pointer??????
        jmp     eax                             # TAILCALL
.Lfunc_end0:
        .size   bar, .Lfunc_end0-bar
                                        # -- End function
        .ident  "clang version 13.0.0"
        .section        ".note.GNU-stack","",@progbits

Reproduced on Clang 13.0.0.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 20, 2022

@llvm/issue-subscribers-backend-x86

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