Skip to content

x86_64: implement the context return trampoline in assembly - #85

Open
zhaojh329 wants to merge 1 commit into
kaniini:masterfrom
zhaojh329:fix-x86
Open

zhaojh329 wants to merge 1 commit into
kaniini:masterfrom
zhaojh329:fix-x86

Conversation

@zhaojh329

Copy link
Copy Markdown

The common C trampoline reads uc_link through an implicit RBX input. GCC 9.5 at -O0 allocates the initialized uc_link local to RBX and clears it before FETCH_LINKPTR dereferences it. Returning from a context entry function therefore causes SIGSEGV, including in freestanding builds. Omitting the frame pointer does not prevent this register allocation.

Reproduce on x86_64 Linux with GCC 9.5, starting from libucontext 1.5.2:

make ARCH=x86_64 clean
make ARCH=x86_64 FREESTANDING=yes CC=gcc CFLAGS='-O0 -g -Wall' check

Before this change, test_libucontext segfaults when a context entry returns. After this change, the same command succeeds.

Replace the x86_64 C trampoline with startcontext.S, following the existing assembly backends. Read uc_link before calling C code and align the stack for calls to setcontext and exit. Preserve hidden visibility and Darwin symbol naming. Remove the unused FETCH_LINKPTR macro and update Meson's source selection and the architecture table.

Extend the existing context-return tests to cover a null uc_link, which must terminate the process successfully instead of resuming main.

Validation on x86_64 Linux:

  • Make check with GCC 9.5 and Clang 14, -O0 and -O2, each with and without FREESTANDING=yes (eight configurations).
  • Meson debug tests with both compilers and both freestanding modes.
  • Downstream libeco Debug, Release, and Debug UBSan suites: 10/10 each.
  • Darwin assembly cross-compiles with Clang; not runtime-tested.

The common C trampoline reads uc_link through an implicit RBX input.
GCC 9.5 at -O0 allocates the initialized uc_link local to RBX and clears
it before FETCH_LINKPTR dereferences it. Returning from a context entry
function therefore causes SIGSEGV, including in freestanding builds.
Omitting the frame pointer does not prevent this register allocation.

Reproduce on x86_64 Linux with GCC 9.5, starting from libucontext 1.5.2:

    make ARCH=x86_64 clean
    make ARCH=x86_64 FREESTANDING=yes CC=gcc CFLAGS='-O0 -g -Wall' check

Before this change, test_libucontext segfaults when a context entry
returns. After this change, the same command succeeds.

Replace the x86_64 C trampoline with startcontext.S, following the
existing assembly backends. Read uc_link before calling C code and
align the stack for calls to setcontext and exit. Preserve hidden
visibility and Darwin symbol naming. Remove the unused FETCH_LINKPTR
macro and update Meson's source selection and the architecture table.

Extend the existing context-return tests to cover a null uc_link,
which must terminate the process successfully instead of resuming main.

Validation on x86_64 Linux:
- Make check with GCC 9.5 and Clang 14, -O0 and -O2, each with and
  without FREESTANDING=yes (eight configurations).
- Meson debug tests with both compilers and both freestanding modes.
- Downstream libeco Debug, Release, and Debug UBSan suites: 10/10 each.
- Darwin assembly cross-compiles with Clang; not runtime-tested.

Signed-off-by: Jianhui Zhao <[email protected]>
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

Successfully merging this pull request may close these issues.

1 participant