Skip to content

Stack pointer misaligned on x86 #61

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

Closed
nmeum opened this issue Feb 10, 2024 · 3 comments
Closed

Stack pointer misaligned on x86 #61

nmeum opened this issue Feb 10, 2024 · 3 comments

Comments

@nmeum
Copy link
Contributor

nmeum commented Feb 10, 2024

The x86 calling convention requires the stack pointer to be aligned on a 16-byte boundary. Otherwise, code using SSE instructions on x86 in conjunction with libucontext will just crash. This currently causes the gccgo runtime to crash on x86 Alpine Linux, see golang/go#63935.

Independent of gccgo, we can reproduce this issue by inspecting SP values in the libucontext test suite:

$ env LD_LIBRARY_PATH="$(pwd)" gdb ./test_libucontext_posix
(gdb) break f2
Breakpoint 1 at 0x1410
(gdb) run
Breakpoint 1, 0x56556410 in f2 ()
(gdb) disassemble
Dump of assembler code for function f2:
…
   0x56556418 <+12>:    add    $0x2b8c,%ebx
   0x5655641e <+18>:    sub    $0xc,%esp
   0x56556421 <+21>:    lea    -0x1ef4(%ebx),%eax
   0x56556427 <+27>:    push   %eax
   0x56556428 <+28>:    call   0x56556030 <puts@plt>
   0x5655642d <+33>:    add    $0x10,%esp
   0x56556430 <+36>:    sub    $0xc,%esp
…
(gdb) break *0x56556428
(gdb) cont
Breakpoint 2, 0x56556428 in f2 ()
(gdb) disassemble
=> 0x56556428 <+28>:    call   0x56556030 <puts@plt>
(gdb) p $esp
$1 = (void *) 0xffffdbac

From this debugging session, we can deduce that the stack pointer ($esp) is not aligned before the first call to the puts(3) function in f2 (0xffffdbac % 16 == 12). I believe this to be non-conformant with the x86 calling convention which requires it to be 16-byte aligned when calling a function.

@kaniini
Copy link
Owner

kaniini commented Feb 11, 2024

Likely it is the code which aligns the stack pointer in makecontext.c which needs to be fixed here.

@nmeum
Copy link
Contributor Author

nmeum commented Feb 12, 2024

Most definitely so, the initial SP value set in makecontext.c is not 16-byte aligned. However, one also needs to adjust the setcontext.S, swapcontext.S and FETCH_LINKPTR implementations to operate on the new SP value and my x86 assembly skills are a bit too rusty for that.

@kaniini kaniini closed this as completed in c1ecb0e Aug 8, 2024
@nmeum
Copy link
Contributor Author

nmeum commented Aug 14, 2024

Are we positive that the alignment issue has been fixed? gcc-go binaries still crash on x86. And the gdb instructions provided above still print an $esp value which is not 16-byte aligned.

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

No branches or pull requests

2 participants