[libc-commits] [libc] [libc] Add ucontext_t and mcontext_t for riscv (PR #240879)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Nov 10 07:52:18 PST 2026


https://github.com/labath created https://github.com/llvm/llvm-project/pull/240879

The layout matches the kernel's struct ucontext (and the struct sigcontext inside it). Field and macro names match other libc implementations.

One tricky part of the layout is that uc_mcontext needs to be 16-byte aligned (because of the Q extension state, even though Linux doesn't support Q). This leaves a hole between the padding after uc_sigmask and uc_mcontext.

I'm also enabling the ucontext.h and sys/ucontext.h headers on riscv. Since getcontext and friends aren't implemented there yet, the headers only provide the types for now.

The test checks the layout by loading known values into the registers, executing an ebreak, and checking that the SIGTRAP handler finds them in the right places of the ucontext_t. The exceptions are sp, gp and tp, which need to stay valid for the kernel and the signal handler, so for those I just check that they hold their original values. I also check the signal mask, alternate stack, and the floating point registers if the D extension is available.

Assisted-by: Claude

>From 4419ac7a60e72a8809ba21017f603e08ddc33927 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 10 Nov 2026 14:35:35 +0000
Subject: [PATCH] [libc] Add ucontext_t and mcontext_t for riscv

The layout matches the kernel's struct ucontext (and the struct
sigcontext inside it). Field and macro names match other libc
implementations.

One tricky part of the layout is that uc_mcontext needs to be
16-byte aligned (because of the Q extension state, even though Linux
doesn't support Q). This leaves a hole between the padding after
uc_sigmask and uc_mcontext.

I'm also enabling the ucontext.h and sys/ucontext.h headers on riscv.
Since getcontext and friends aren't implemented there yet, the headers
only provide the types for now.

The test checks the layout by loading known values into the registers,
executing an ebreak, and checking that the SIGTRAP handler finds them
in the right places of the ucontext_t. The exceptions are sp, gp and
tp, which need to stay valid for the kernel and the signal handler, so
for those I just check that they hold their original values. I also
check the signal mask, and the floating point registers if the D
extension is available.

Assisted-by: Claude
---
 libc/config/linux/riscv/headers.txt           |   2 +
 libc/include/CMakeLists.txt                   |   2 +-
 libc/include/llvm-libc-types/mcontext_t.h     |   2 +
 .../llvm-libc-types/riscv/CMakeLists.txt      |  15 ++
 .../llvm-libc-types/riscv/mcontext_t.h        |  72 ++++++
 .../llvm-libc-types/riscv/ucontext_t.h        |  34 +++
 libc/include/llvm-libc-types/ucontext_t.h     |   2 +
 libc/test/src/ucontext/CMakeLists.txt         |   4 +
 libc/test/src/ucontext/riscv/CMakeLists.txt   |  22 ++
 .../ucontext/riscv/ucontext_layout_test.cpp   | 237 ++++++++++++++++++
 10 files changed, 391 insertions(+), 1 deletion(-)
 create mode 100644 libc/include/llvm-libc-types/riscv/CMakeLists.txt
 create mode 100644 libc/include/llvm-libc-types/riscv/mcontext_t.h
 create mode 100644 libc/include/llvm-libc-types/riscv/ucontext_t.h
 create mode 100644 libc/test/src/ucontext/riscv/CMakeLists.txt
 create mode 100644 libc/test/src/ucontext/riscv/ucontext_layout_test.cpp

diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index b3e8569a06887c..040f891e387ab1 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -79,6 +79,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_syslog
     libc.include.sys_time
     libc.include.sys_types
+    libc.include.sys_ucontext
     libc.include.sys_uio
     libc.include.sys_un
     libc.include.sys_unistd
@@ -92,6 +93,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.threads
     libc.include.time
     libc.include.uchar
+    libc.include.ucontext
     libc.include.unistd
     libc.include.wchar
     libc.include.wctype
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 523bb11541f7ac..7010d6998b5fe8 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -645,7 +645,7 @@ add_header_macro(
     .llvm_libc_common_h
 )
 
-if(LIBC_TARGET_ARCHITECTURE STREQUAL "x86_64")
+if(LIBC_TARGET_ARCHITECTURE_IS_X86_64 OR LIBC_TARGET_ARCHITECTURE_IS_ANY_RISCV)
   add_header_macro(
     ucontext
     ../libc/include/ucontext.yaml
diff --git a/libc/include/llvm-libc-types/mcontext_t.h b/libc/include/llvm-libc-types/mcontext_t.h
index f2226555c2ac85..367f06b713bf9b 100644
--- a/libc/include/llvm-libc-types/mcontext_t.h
+++ b/libc/include/llvm-libc-types/mcontext_t.h
@@ -11,6 +11,8 @@
 
 #if defined(__x86_64__)
 #include "x86_64/mcontext_t.h"
+#elif defined(__riscv)
+#include "riscv/mcontext_t.h"
 #else
 #error "mcontext_t not available for your target architecture."
 #endif
diff --git a/libc/include/llvm-libc-types/riscv/CMakeLists.txt b/libc/include/llvm-libc-types/riscv/CMakeLists.txt
new file mode 100644
index 00000000000000..a0b329365b1f12
--- /dev/null
+++ b/libc/include/llvm-libc-types/riscv/CMakeLists.txt
@@ -0,0 +1,15 @@
+add_header(
+  mcontext_t
+  HDR
+    mcontext_t.h
+)
+
+add_header(
+  ucontext_t
+  HDR
+    ucontext_t.h
+  DEPENDS
+    .mcontext_t
+    libc.include.llvm-libc-types.sigset_t
+    libc.include.llvm-libc-types.stack_t
+)
diff --git a/libc/include/llvm-libc-types/riscv/mcontext_t.h b/libc/include/llvm-libc-types/riscv/mcontext_t.h
new file mode 100644
index 00000000000000..a28f5ce6b3aa55
--- /dev/null
+++ b/libc/include/llvm-libc-types/riscv/mcontext_t.h
@@ -0,0 +1,72 @@
+//===-- Definition of type mcontext_t -------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Note: Definitions in this file are based on the Linux kernel ABI.
+
+#ifndef LLVM_LIBC_TYPES_RISCV_MCONTEXT_T_H
+#define LLVM_LIBC_TYPES_RISCV_MCONTEXT_T_H
+
+#include "../../__llvm-libc-common.h"
+
+// The general purpose registers. The layout of gregset_t must match the layout
+// of 'struct user_regs_struct' in the Linux kernel (see
+// arch/riscv/include/uapi/asm/ptrace.h): element 0 holds the program counter
+// and element N (N > 0) holds the value of register xN.
+#define NGREG 32
+
+#define REG_PC 0
+#define REG_RA 1
+#define REG_SP 2
+#define REG_TP 4
+#define REG_S0 8
+#define REG_S1 9
+#define REG_A0 10
+#define REG_S2 18
+#define REG_NARGS 8
+
+typedef unsigned long greg_t;
+typedef greg_t gregset_t[NGREG];
+
+// The floating point registers. The layout of these types must match the layout
+// of 'union __riscv_fp_state' in the Linux kernel (see
+// arch/riscv/include/uapi/asm/ptrace.h). The kernel saves the floating point
+// state in the format of the D extension.
+struct __riscv_mc_f_ext_state {
+  unsigned int __f[32];
+  unsigned int __fcsr;
+};
+
+struct __riscv_mc_d_ext_state {
+  unsigned long long __f[32];
+  unsigned int __fcsr;
+};
+
+struct __riscv_mc_q_ext_state {
+  _Alignas(16) unsigned long long __f[64];
+  unsigned int __fcsr;
+  // Reserved for extending the structure. The kernel uses this space to
+  // describe additional (e.g. vector) state, which it stores after the end of
+  // the mcontext_t structure.
+  unsigned int __reserved[3];
+};
+
+typedef union __riscv_mc_fp_state {
+  struct __riscv_mc_f_ext_state __f;
+  struct __riscv_mc_d_ext_state __d;
+  struct __riscv_mc_q_ext_state __q;
+} fpregset_t;
+
+// mcontext_t represents the machine state. Its layout must match the layout of
+// 'struct sigcontext' in the Linux kernel (see
+// arch/riscv/include/uapi/asm/sigcontext.h).
+typedef struct mcontext_t {
+  gregset_t __gregs;
+  fpregset_t __fpregs;
+} mcontext_t;
+
+#endif // LLVM_LIBC_TYPES_RISCV_MCONTEXT_T_H
diff --git a/libc/include/llvm-libc-types/riscv/ucontext_t.h b/libc/include/llvm-libc-types/riscv/ucontext_t.h
new file mode 100644
index 00000000000000..1928846516af33
--- /dev/null
+++ b/libc/include/llvm-libc-types/riscv/ucontext_t.h
@@ -0,0 +1,34 @@
+//===-- Definition of type ucontext_t -------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Note: Definitions in this file are based on the Linux kernel ABI.
+
+#ifndef LLVM_LIBC_TYPES_RISCV_UCONTEXT_T_H
+#define LLVM_LIBC_TYPES_RISCV_UCONTEXT_T_H
+
+#include "../sigset_t.h"
+#include "../stack_t.h"
+#include "mcontext_t.h"
+
+// The layout of ucontext_t must match the layout of 'struct ucontext' in the
+// Linux kernel (see arch/riscv/include/uapi/asm/ucontext.h).
+typedef struct ucontext_t {
+  unsigned long uc_flags;
+  struct ucontext_t *uc_link;
+  stack_t uc_stack;
+  sigset_t uc_sigmask;
+  // The kernel reserves space for extending the signal mask to 1024 bits.
+  unsigned char __reserved[1024 / 8 - sizeof(sigset_t)];
+  // uc_mcontext is the last member, as the kernel may store additional (e.g.
+  // vector) state after it. Its 16-byte alignment (inherited from
+  // __riscv_mc_q_ext_state) leaves an implicit gap of 8 (RV64) or 12 (RV32)
+  // bytes after __reserved.
+  mcontext_t uc_mcontext;
+} ucontext_t;
+
+#endif // LLVM_LIBC_TYPES_RISCV_UCONTEXT_T_H
diff --git a/libc/include/llvm-libc-types/ucontext_t.h b/libc/include/llvm-libc-types/ucontext_t.h
index 1c5319bf807fa7..b5926758134897 100644
--- a/libc/include/llvm-libc-types/ucontext_t.h
+++ b/libc/include/llvm-libc-types/ucontext_t.h
@@ -11,6 +11,8 @@
 
 #if defined(__x86_64__)
 #include "x86_64/ucontext_t.h"
+#elif defined(__riscv)
+#include "riscv/ucontext_t.h"
 #else
 #error "ucontext_t not available for your target architecture."
 #endif
diff --git a/libc/test/src/ucontext/CMakeLists.txt b/libc/test/src/ucontext/CMakeLists.txt
index d05ed32774dc8f..a2b60cc91ae528 100644
--- a/libc/test/src/ucontext/CMakeLists.txt
+++ b/libc/test/src/ucontext/CMakeLists.txt
@@ -1,5 +1,9 @@
 add_libc_testsuite(libc-ucontext-tests)
 
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_ARCHITECTURE})
+  add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
+endif()
+
 if(TARGET libc.src.ucontext.getcontext)
   add_libc_test(
     ucontext_test
diff --git a/libc/test/src/ucontext/riscv/CMakeLists.txt b/libc/test/src/ucontext/riscv/CMakeLists.txt
new file mode 100644
index 00000000000000..9613234e1e994f
--- /dev/null
+++ b/libc/test/src/ucontext/riscv/CMakeLists.txt
@@ -0,0 +1,22 @@
+add_libc_test(
+  ucontext_layout_test
+  SUITE
+    libc-ucontext-tests
+  SRCS
+    ucontext_layout_test.cpp
+  DEPENDS
+    libc.hdr.signal_macros
+    libc.hdr.types.siginfo_t
+    libc.hdr.types.sigset_t
+    libc.hdr.types.stack_t
+    libc.hdr.types.struct_sigaction
+    libc.include.llvm-libc-types.ucontext_t
+    libc.src.__support.CPP.scope
+    libc.src.__support.macros.properties.cpu_features
+    libc.src.signal.sigaction
+    libc.src.signal.sigaddset
+    libc.src.signal.sigaltstack
+    libc.src.signal.sigemptyset
+    libc.src.signal.sigismember
+    libc.src.signal.sigprocmask
+)
diff --git a/libc/test/src/ucontext/riscv/ucontext_layout_test.cpp b/libc/test/src/ucontext/riscv/ucontext_layout_test.cpp
new file mode 100644
index 00000000000000..8830157045f104
--- /dev/null
+++ b/libc/test/src/ucontext/riscv/ucontext_layout_test.cpp
@@ -0,0 +1,237 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Verifies that the layout of ucontext_t matches the signal frame set up by
+/// the kernel. The test loads known values into as many registers as possible,
+/// executes an ebreak instruction to raise a SIGTRAP, and checks that the
+/// values show up in the right fields of the ucontext_t passed to the signal
+/// handler.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/signal_macros.h"
+#include "hdr/types/siginfo_t.h"
+#include "hdr/types/sigset_t.h"
+#include "hdr/types/stack_t.h"
+#include "hdr/types/struct_sigaction.h"
+#include "include/llvm-libc-types/ucontext_t.h"
+#include "src/__support/CPP/scope.h"
+#include "src/__support/macros/properties/cpu_features.h"
+#include "src/signal/sigaction.h"
+#include "src/signal/sigaddset.h"
+#include "src/signal/sigaltstack.h"
+#include "src/signal/sigemptyset.h"
+#include "src/signal/sigismember.h"
+#include "src/signal/sigprocmask.h"
+#include "test/UnitTest/Test.h"
+
+#if __riscv_xlen == 64
+#define XLEN_LOAD "ld"
+#define XLEN_STORE "sd"
+#define XLEN_BYTES "8"
+#elif __riscv_xlen == 32
+#define XLEN_LOAD "lw"
+#define XLEN_STORE "sw"
+#define XLEN_BYTES "4"
+#else
+#error "Unsupported RISC-V architecture"
+#endif
+
+// Testing the floating point registers requires the D extension, as we use
+// fld/fsd to load and save their 64-bit values. At present, the linux kernel
+// does not support the Q extension, or the F extension alone.
+#ifdef LIBC_TARGET_CPU_HAS_RISCV_FPU_DOUBLE
+#define TEST_FP_REGS 1
+#else
+#define TEST_FP_REGS 0
+#endif
+
+// Load/store a register from/to element N of the array pointed to by t6.
+#define LOAD_X(n) XLEN_LOAD " x" #n ", " #n "*" XLEN_BYTES "(t6)\n\t"
+#define STORE_X(reg, n) XLEN_STORE " " #reg ", " #n "*" XLEN_BYTES "(t6)\n\t"
+// Load fN from element N of the array pointed to by a1.
+#define LOAD_F(n) "fld f" #n ", " #n "*8(a1)\n\t"
+
+// Save/restore a register to/from an 8-byte stack slot.
+#define SAVE(reg, slot) XLEN_STORE " " #reg ", " #slot "*8(sp)\n\t"
+#define RESTORE(reg, slot) XLEN_LOAD " " #reg ", " #slot "*8(sp)\n\t"
+#define SAVE_F(reg, slot) "fsd " #reg ", " #slot "*8(sp)\n\t"
+#define RESTORE_F(reg, slot) "fld " #reg ", " #slot "*8(sp)\n\t"
+// The stack frame holds 26 slots: ra, s0-s11, fs0-fs11 and fcsr. The size must
+// be a multiple of 16 to keep the stack aligned.
+#define FRAME_SIZE "26*8"
+
+// clang-format mangles this badly
+// clang-format off
+// The callee-saved registers, and the stack slots we save them in.
+#define CALLEE_SAVED_X(M)                                                      \
+  M(ra, 0) M(s0, 1) M(s1, 2) M(s2, 3) M(s3, 4) M(s4, 5) M(s5, 6) M(s6, 7)      \
+  M(s7, 8) M(s8, 9) M(s9, 10) M(s10, 11) M(s11, 12)
+#define CALLEE_SAVED_F(M)                                                      \
+  M(fs0, 13) M(fs1, 14) M(fs2, 15) M(fs3, 16) M(fs4, 17) M(fs5, 18)            \
+  M(fs6, 19) M(fs7, 20) M(fs8, 21) M(fs9, 22) M(fs10, 23) M(fs11, 24)
+
+// The register numbers.
+#define ALL_REGS(M)                                                            \
+  M(0) M(1) M(2) M(3) M(4) M(5) M(6) M(7) M(8) M(9) M(10) M(11) M(12) M(13)    \
+  M(14) M(15) M(16) M(17) M(18) M(19) M(20) M(21) M(22) M(23) M(24) M(25)      \
+  M(26) M(27) M(28) M(29) M(30) M(31)
+// clang-format on
+
+static int handler_calls;
+static int received_signal;
+static ucontext_t received_context;
+static char alt_stack_buffer[SIGSTKSZ];
+
+static void handler(int signal, siginfo_t *, void *context) {
+  ucontext_t *uc = static_cast<ucontext_t *>(context);
+  ++handler_calls;
+  received_signal = signal;
+  received_context = *uc;
+  // Resume execution after the (four byte) ebreak instruction.
+  uc->uc_mcontext.__gregs[REG_PC] += 4;
+}
+
+// Loads known values into the registers and executes an ebreak instruction:
+// - xN is loaded from x[N]. The exceptions are sp, gp and tp, as the kernel and
+//   the signal handler depend on them having sane values. Their current values
+//   are stored into x[2], x[3] and x[4] instead. x[0] receives the address of
+//   the ebreak instruction.
+// - fN is loaded from f[N], and fcsr is set to `fcsr`.
+// The callee-saved registers (and fcsr) are restored before returning.
+[[gnu::naked]] static void
+trap_with_known_registers(unsigned long * /*x*/,
+                          const unsigned long long * /*f*/,
+                          unsigned int /*fcsr*/) {
+  // clang-format mangles this badly
+  // clang-format off
+  asm(
+      // Save the callee-saved registers and fcsr.
+      "addi sp, sp, -" FRAME_SIZE "\n\t"
+      CALLEE_SAVED_X(SAVE)
+#if TEST_FP_REGS
+      CALLEE_SAVED_F(SAVE_F)
+      "frcsr t0\n\t"
+      SAVE(t0, 25)
+#endif
+
+      // Record the values of the registers we don't control. Use t6 (x31) to
+      // hold the address of the array, as it is the last register we load.
+      "mv t6, a0\n\t"
+      "lla t0, 1f\n\t"
+      STORE_X(t0, 0) STORE_X(sp, 2) STORE_X(gp, 3) STORE_X(tp, 4)
+
+      // Load the known values. sp, gp and tp get the values we have just
+      // stored, so they don't change, and the load into x0 has no effect.
+#if TEST_FP_REGS
+      ALL_REGS(LOAD_F)
+      "fscsr a2\n\t"
+#endif
+      ALL_REGS(LOAD_X)
+
+      // Raise the signal. Make sure the instruction does not get compressed,
+      // so that the signal handler knows how to skip over it.
+      ".option push\n\t"
+      ".option norvc\n\t"
+      "1: ebreak\n\t"
+      ".option pop\n\t"
+
+      // Restore the saved registers and return.
+      CALLEE_SAVED_X(RESTORE)
+#if TEST_FP_REGS
+      CALLEE_SAVED_F(RESTORE_F)
+      RESTORE(t0, 25)
+      "fscsr t0\n\t"
+#endif
+      "addi sp, sp, " FRAME_SIZE "\n\t"
+      "ret");
+  // clang-format on
+}
+
+TEST(LlvmLibcUcontextLayoutTest, SignalContext) {
+  // The values which the registers should hold when the signal is raised.
+  // Element N of expected_x corresponds to register xN, except for element 0,
+  // which corresponds to the pc. Every register gets a distinct and
+  // recognizable value: each byte of xN is equal to N, and each byte of fN to
+  // N + 0x80.
+  unsigned long expected_x[32];
+  unsigned long long expected_f[32];
+  for (int i = 0; i < 32; ++i) {
+    expected_x[i] = static_cast<unsigned long>(0x0101010101010101ULL * i);
+    expected_f[i] = 0x0101010101010101ULL * (i + 0x80);
+  }
+  // Rounding mode RUP, and the NV, OF and NX exception flags.
+  constexpr unsigned int EXPECTED_FCSR = 0x75;
+
+  struct sigaction action = {};
+  action.sa_sigaction = handler;
+  // With SA_RESETHAND, the process crashes (instead of looping forever) if the
+  // handler fails to skip over the ebreak instruction.
+  action.sa_flags = SA_SIGINFO | SA_RESETHAND;
+  ASSERT_EQ(LIBC_NAMESPACE::sigaction(SIGTRAP, &action, nullptr), 0);
+
+  // Install an alternate signal stack to give uc_stack a recognizable value.
+  // The handler does not run on it, as SA_ONSTACK is not set.
+  stack_t alt_stack = {};
+  alt_stack.ss_sp = alt_stack_buffer;
+  alt_stack.ss_size = sizeof(alt_stack_buffer);
+  stack_t old_stack;
+  ASSERT_EQ(LIBC_NAMESPACE::sigaltstack(&alt_stack, &old_stack), 0);
+  LIBC_NAMESPACE::cpp::scope_exit restore_stack(
+      [&] { LIBC_NAMESPACE::sigaltstack(&old_stack, nullptr); });
+
+  // Block some signals to give the signal mask a recognizable value.
+  sigset_t blocked;
+  ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&blocked), 0);
+  ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&blocked, SIGUSR1), 0);
+  ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&blocked, SIGUSR2), 0);
+  ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&blocked, SIGRTMAX), 0);
+  sigset_t old_mask, expected_mask;
+  ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, &blocked, &old_mask), 0);
+  LIBC_NAMESPACE::cpp::scope_exit restore_mask(
+      [&] { LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &old_mask, nullptr); });
+  ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, nullptr, &expected_mask), 0);
+
+  trap_with_known_registers(expected_x, expected_f, EXPECTED_FCSR);
+
+  ASSERT_EQ(handler_calls, 1);
+  ASSERT_EQ(received_signal, SIGTRAP);
+
+  EXPECT_EQ(received_context.uc_flags, 0UL);
+  EXPECT_EQ(received_context.uc_link, nullptr);
+  EXPECT_EQ(received_context.uc_stack.ss_sp, alt_stack.ss_sp);
+  EXPECT_EQ(received_context.uc_stack.ss_flags, alt_stack.ss_flags);
+  EXPECT_EQ(received_context.uc_stack.ss_size, alt_stack.ss_size);
+  for (int sig = 1; sig < NSIG; ++sig) {
+    EXPECT_EQ(LIBC_NAMESPACE::sigismember(&received_context.uc_sigmask, sig),
+              LIBC_NAMESPACE::sigismember(&expected_mask, sig))
+        << "signal " << sig << "\n";
+  }
+
+  const mcontext_t &mcontext = received_context.uc_mcontext;
+  for (int i = 0; i < 32; ++i) {
+    EXPECT_EQ(mcontext.__gregs[i], expected_x[i]) << "__gregs[" << i << "]\n";
+  }
+  EXPECT_EQ(mcontext.__gregs[REG_PC], expected_x[0]);
+  EXPECT_EQ(mcontext.__gregs[REG_RA], expected_x[1]);
+  EXPECT_EQ(mcontext.__gregs[REG_SP], expected_x[2]);
+  EXPECT_EQ(mcontext.__gregs[REG_TP], expected_x[4]);
+  EXPECT_EQ(mcontext.__gregs[REG_S0], expected_x[8]);
+  EXPECT_EQ(mcontext.__gregs[REG_S1], expected_x[9]);
+  EXPECT_EQ(mcontext.__gregs[REG_A0], expected_x[10]);
+  EXPECT_EQ(mcontext.__gregs[REG_S2], expected_x[18]);
+
+#if TEST_FP_REGS
+  for (int i = 0; i < 32; ++i) {
+    EXPECT_EQ(mcontext.__fpregs.__d.__f[i], expected_f[i])
+        << "__fpregs.__d.__f[" << i << "]\n";
+  }
+  EXPECT_EQ(mcontext.__fpregs.__d.__fcsr, EXPECTED_FCSR);
+#endif
+}



More information about the libc-commits mailing list