[libc-commits] [libc] [libc] Implement getcontext and setcontext for x86_64 (PR #192343)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Thu Apr 16 01:16:03 PDT 2026
================
@@ -0,0 +1,86 @@
+//===-- Implementation of setcontext for x86_64 ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/ucontext/setcontext.h"
+#include "include/llvm-libc-types/ucontext_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+#include "hdr/types/size_t.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+__attribute__((naked)) LLVM_LIBC_FUNCTION(int, setcontext,
+ (const ucontext_t *ucp)) {
+ asm(R"(
+ # ucp is in rdi
+
+ # Restore the signal mask using rt_sigprocmask syscall.
+ # rt_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL, sizeof(sigset_t))
+ pushq %%rdi # Save ucp
+ leaq %c[sigmask](%%rdi), %%rsi # set = &ucp->uc_sigmask
+ xorq %%rdx, %%rdx # oldset = NULL
+ movq $%c[sigset_size], %%r10 # sigsetsize = sizeof(sigset_t)
+ movq $2, %%rdi # how = SIG_SETMASK
+ movq $%c[syscall_num], %%rax
+ syscall
----------------
labath wrote:
By restoring the signal mask early, we're permitting a signal to happen in the "old" context, even though it might have it blocked. (The most visible effect of this is that the handler will run on the old stack.) It would kind of make more sense to do this later so that the signal executes in the context which actually has it enabled.
OTOH, I understand why you did it this way (makes restoring the registers easy) and I don't think anything promises this behavior, so I don't think you need to change this -- but I thought I should mention that anyway :)
https://github.com/llvm/llvm-project/pull/192343
More information about the libc-commits
mailing list