[libc-commits] [libc] [libc] Update siginfo_t to match kernel definition (PR #66560)

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Thu Sep 21 06:53:58 PDT 2023


https://github.com/mikhailramalho updated https://github.com/llvm/llvm-project/pull/66560

>From 579f02676271dddff72e5985e4a8566e84eb82f9 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 15 Sep 2023 21:02:12 -0300
Subject: [PATCH 1/3] [libc] Update siginfo_t to match kernel definition

This patch updates the siginfo_t struct definition to match the
definition from the kernel in here:

https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/siginfo.h

In particular, there are two main changes:

1. swap position of si_code and si_errno: si_code show come after
   si_errno in all systems except MIPS. Since we don't MIPS, the order
   is fixed for now, but can be easily \#ifdef'd if MIPS support is
   implemented in the future.

2. We add a union of structs that are filled depending on the signal
   raised.

This change was required for the fork and spawn integration tests in rv32,
since they fork/clone the running process, call wait/waitid/waitpid, and
read the status, which was wrong in rv32 because wait/waitid/waitpid are
implemented in rv32 using SYS_waitid.

SYS_waitid takes a pointer to a siginfo_t and fills the proper fields in
the struct. The previous siginfo_t definition was being incorrectly
filled due to not taking into account the signal raised.
---
 libc/include/llvm-libc-types/CMakeLists.txt |  2 +-
 libc/include/llvm-libc-types/siginfo_t.h    | 93 +++++++++++++++++++--
 2 files changed, 85 insertions(+), 10 deletions(-)

diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 006986fd98fc850..3c0cc7bbc71dacb 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -63,7 +63,7 @@ add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
 add_header(struct_dirent HDR struct_dirent.h DEPENDS .ino_t .off_t)
 add_header(struct_sched_param HDR struct_sched_param.h)
 add_header(union_sigval HDR union_sigval.h)
-add_header(siginfo_t HDR siginfo_t.h DEPENDS .union_sigval .pid_t .uid_t)
+add_header(siginfo_t HDR siginfo_t.h DEPENDS .union_sigval .pid_t .uid_t .clock_t)
 add_header(sig_atomic_t HDR sig_atomic_t.h)
 add_header(sigset_t HDR sigset_t.h DEPENDS libc.include.llvm-libc-macros.signal_macros)
 add_header(struct_sigaction HDR struct_sigaction.h DEPENDS .sigset_t .siginfo_t)
diff --git a/libc/include/llvm-libc-types/siginfo_t.h b/libc/include/llvm-libc-types/siginfo_t.h
index 59d39e046b66c7e..b4bbde00bfb266f 100644
--- a/libc/include/llvm-libc-types/siginfo_t.h
+++ b/libc/include/llvm-libc-types/siginfo_t.h
@@ -9,20 +9,95 @@
 #ifndef __LLVM_LIBC_TYPES_SIGINFO_T_H__
 #define __LLVM_LIBC_TYPES_SIGINFO_T_H__
 
+#include <llvm-libc-types/clock_t.h>
 #include <llvm-libc-types/pid_t.h>
 #include <llvm-libc-types/uid_t.h>
 #include <llvm-libc-types/union_sigval.h>
 
 typedef struct {
-  int si_signo;
-  int si_code;
-  int si_errno;
-  pid_t si_pid;
-  uid_t si_uid;
-  void *si_addr;
-  int si_status;
-  long si_band;
-  union sigval si_value;
+  int si_signo; /* Signal number.  */
+  int si_errno; /* If non-zero, an errno value associated with
+                   this signal, as defined in <errno.h>.  */
+  int si_code;  /* Signal code.  */
+  union {
+    /* kill() */
+    struct {
+      pid_t si_pid; /* sender's pid */
+      uid_t si_uid; /* sender's uid */
+    } _kill;
+
+    /* POSIX.1b timers */
+    struct {
+      int si_tid;             /* timer id */
+      int _overrun;           /* overrun count */
+      union sigval si_sigval; /* same as below */
+    } _timer;
+
+    /* POSIX.1b signals */
+    struct {
+      pid_t si_pid; /* sender's pid */
+      uid_t si_uid; /* sender's uid */
+      union sigval si_sigval;
+    } _rt;
+
+    /* SIGCHLD */
+    struct {
+      pid_t si_pid;  /* which child */
+      uid_t si_uid;  /* sender's uid */
+      int si_status; /* exit code */
+      clock_t si_utime;
+      clock_t si_stime;
+    } _sigchld;
+
+    /* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGTRAP, SIGEMT */
+    struct {
+      void *si_addr;         /* faulting insn/memory ref. */
+      short int si_addr_lsb; /* Valid LSB of the reported address.  */
+      union {
+        /* used when si_code=SEGV_BNDERR */
+        struct {
+          void *_lower;
+          void *_upper;
+        } _addr_bnd;
+        /* used when si_code=SEGV_PKUERR */
+        __UINT32_TYPE__ _pkey;
+      } _bounds;
+    } _sigfault;
+
+    /* SIGPOLL */
+    struct {
+      long int si_band; /* POLL_IN, POLL_OUT, POLL_MSG */
+      int si_fd;
+    } _sigpoll;
+
+    /* SIGSYS */
+    struct {
+      void *_call_addr;   /* calling user insn */
+      int _syscall;       /* triggering system call number */
+      unsigned int _arch; /* AUDIT_ARCH_* of syscall */
+    } _sigsys;
+  } _sifields;
 } siginfo_t;
 
+#define si_pid _sifields._kill.si_pid
+#define si_uid _sifields._kill.si_uid
+#define si_timerid _sifields._timer.si_tid
+#define si_overrun _sifields._timer.si_overrun
+#define si_status _sifields._sigchld.si_status
+#define si_utime _sifields._sigchld.si_utime
+#define si_stime _sifields._sigchld.si_stime
+#define si_value _sifields._rt.si_sigval
+#define si_int _sifields._rt.si_sigval.sival_int
+#define si_ptr _sifields._rt.si_sigval.sival_ptr
+#define si_addr _sifields._sigfault.si_addr
+#define si_addr_lsb _sifields._sigfault.si_addr_lsb
+#define si_lower _sifields._sigfault._bounds._addr_bnd._lower
+#define si_upper _sifields._sigfault._bounds._addr_bnd._upper
+#define si_pkey _sifields._sigfault._bounds._pkey
+#define si_band _sifields._sigpoll.si_band
+#define si_fd _sifields._sigpoll.si_fd
+#define si_call_addr _sifields._sigsys._call_addr
+#define si_syscall _sifields._sigsys._syscall
+#define si_arch _sifields._sigsys._arch
+
 #endif // __LLVM_LIBC_TYPES_SIGINFO_T_H__

>From 62d317ebe3fffc62ae692cb0c968662357e5cd1e Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Fri, 15 Sep 2023 21:55:11 -0300
Subject: [PATCH 2/3] Added padding

---
 libc/include/llvm-libc-types/siginfo_t.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libc/include/llvm-libc-types/siginfo_t.h b/libc/include/llvm-libc-types/siginfo_t.h
index b4bbde00bfb266f..b311e459c04d391 100644
--- a/libc/include/llvm-libc-types/siginfo_t.h
+++ b/libc/include/llvm-libc-types/siginfo_t.h
@@ -14,12 +14,16 @@
 #include <llvm-libc-types/uid_t.h>
 #include <llvm-libc-types/union_sigval.h>
 
+#define SI_MAX_SIZE 128
+
 typedef struct {
   int si_signo; /* Signal number.  */
   int si_errno; /* If non-zero, an errno value associated with
                    this signal, as defined in <errno.h>.  */
   int si_code;  /* Signal code.  */
   union {
+    int _si_pad[SI_MAX_SIZE / sizeof(int)];
+
     /* kill() */
     struct {
       pid_t si_pid; /* sender's pid */

>From 4161ad6fcf325cda632503ff87172abd32d7f4b9 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 21 Sep 2023 10:53:19 -0300
Subject: [PATCH 3/3] Undef macro in header after usage

---
 libc/include/llvm-libc-types/siginfo_t.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/include/llvm-libc-types/siginfo_t.h b/libc/include/llvm-libc-types/siginfo_t.h
index b311e459c04d391..ef8af78a88be690 100644
--- a/libc/include/llvm-libc-types/siginfo_t.h
+++ b/libc/include/llvm-libc-types/siginfo_t.h
@@ -83,6 +83,8 @@ typedef struct {
   } _sifields;
 } siginfo_t;
 
+#undef SI_MAX_SIZE
+
 #define si_pid _sifields._kill.si_pid
 #define si_uid _sifields._kill.si_uid
 #define si_timerid _sifields._timer.si_tid



More information about the libc-commits mailing list