Fix sigsetjmp/siglongjmp to save/restore RT signals on arm/x86 too.

Our arm and x86 ABIs shipped with a 32-bit `sigset_t`, so we need to
use sigprocmask64 to save/restore the RT signals too. (This is important
because several are in use by the system, and the behavior of our 32-bit
`sigset_t` is to clear the RT signals.)

Bug: http://b/72493232
Test: ran tests
Change-Id: Idff91f8b2849276e5a3073d07eccd84e02a34410
diff --git a/libc/arch-arm/bionic/setjmp.S b/libc/arch-arm/bionic/setjmp.S
index c10bd3e..30e7e23 100644
--- a/libc/arch-arm/bionic/setjmp.S
+++ b/libc/arch-arm/bionic/setjmp.S
@@ -56,12 +56,14 @@
 //
 // word   name            description
 // 0      sigflag/cookie  setjmp cookie in top 31 bits, signal mask flag in low bit
-// 1      sigmask         signal mask (not used with _setjmp / _longjmp)
-// 2      float_base      base of float registers (d8 to d15)
-// 18     float_state     floating-point status and control register
-// 19     core_base       base of core registers (r4-r11, r13-r14)
-// 29     checksum        checksum of all of the core registers, to give better error messages.
-// 30     reserved        reserved entries (room to grow)
+// 1      sigmask         64-bit signal mask (not used with _setjmp / _longjmp)
+// 2      "               "
+// 3      reserved        (unused to allow float_base to be maximally aligned)
+// 4      float_base      base of float registers (d8 to d15)
+// 20     float_state     floating-point status and control register
+// 21     core_base       base of core registers (r4-r11, r13-r14)
+// 31     checksum        checksum of all of the core registers, to give better error messages.
+// 32     reserved        reserved entries (room to grow)
 // 64
 //
 // NOTE: float_base must be at an even word index, since the
@@ -69,8 +71,8 @@
 //       that expect 8-byte alignment.
 
 #define _JB_SIGFLAG     0
-#define _JB_SIGMASK     (_JB_SIGFLAG+1)
-#define _JB_FLOAT_BASE  (_JB_SIGMASK+1)
+#define _JB_SIGMASK     (_JB_SIGFLAG + 1)
+#define _JB_FLOAT_BASE  (_JB_SIGMASK + 3)
 #define _JB_FLOAT_STATE (_JB_FLOAT_BASE + (15-8+1)*2)
 #define _JB_CORE_BASE   (_JB_FLOAT_STATE+1)
 #define _JB_CHECKSUM    (_JB_CORE_BASE+10)
@@ -151,7 +153,7 @@
   add r2, r0, #(_JB_SIGMASK * 4)
   mov r0, #2 // SIG_SETMASK
   mov r1, #0
-  bl sigprocmask
+  bl sigprocmask64
 
   // Unalign the stack.
   add sp, #4
@@ -221,7 +223,7 @@
   mov r2, #0
   add r1, r0, #(_JB_SIGMASK * 4)
   mov r0, #2 // SIG_SETMASK
-  bl sigprocmask
+  bl sigprocmask64
 
 1:
   ldmfd sp!, {r0, r1, lr}
diff --git a/libc/arch-x86/bionic/setjmp.S b/libc/arch-x86/bionic/setjmp.S
index f9f7cd7..1e1ce58 100644
--- a/libc/arch-x86/bionic/setjmp.S
+++ b/libc/arch-x86/bionic/setjmp.S
@@ -42,10 +42,10 @@
 // 3      ebp
 // 4      esi
 // 5      edi
-// 6      sigmask         signal mask (not used with _setjmp / _longjmp)
-// 7      sigflag/cookie  setjmp cookie in top 31 bits, signal mask flag in low bit
-// 8      checksum        checksum of the core registers, to give better error messages.
-// 9      reserved
+// 6      sigmask         64-bit signal mask (not used with _setjmp / _longjmp)
+// 7      "               "
+// 8      sigflag/cookie  setjmp cookie in top 31 bits, signal mask flag in low bit
+// 9      checksum        checksum of the core registers, to give better error messages.
 
 #define _JB_EDX 0
 #define _JB_EBX 1
@@ -54,8 +54,8 @@
 #define _JB_ESI 4
 #define _JB_EDI 5
 #define _JB_SIGMASK 6
-#define _JB_SIGFLAG 7
-#define _JB_CHECKSUM 8
+#define _JB_SIGFLAG 8
+#define _JB_CHECKSUM 9
 
 .macro m_mangle_registers reg
   xorl \reg,%edx
@@ -117,7 +117,7 @@
   pushl %eax
   pushl $0 // NULL
   pushl $2 // SIG_SETMASK
-  call PIC_PLT(sigprocmask)
+  call PIC_PLT(sigprocmask64)
   addl $12,%esp
   PIC_EPILOGUE
   popl %ecx
@@ -165,7 +165,7 @@
   pushl $0 // NULL
   pushl %eax
   pushl $2 // SIG_SETMASK
-  call PIC_PLT(sigprocmask)
+  call PIC_PLT(sigprocmask64)
   addl $12,%esp
   PIC_EPILOGUE
 
diff --git a/tests/setjmp_test.cpp b/tests/setjmp_test.cpp
index bb01601..4f5e60c 100644
--- a/tests/setjmp_test.cpp
+++ b/tests/setjmp_test.cpp
@@ -19,6 +19,8 @@
 #include <setjmp.h>
 #include <stdlib.h>
 
+#include "ScopedSignalHandler.h"
+
 TEST(setjmp, setjmp_smoke) {
   int value;
   jmp_buf jb;
@@ -63,61 +65,59 @@
   }
 }
 
-// Two distinct signal sets, pipu
+// Two distinct signal sets.
 struct SigSets {
   SigSets() : one(MakeSigSet(0)), two(MakeSigSet(1)) {
   }
 
-  static sigset_t MakeSigSet(int offset) {
-    sigset_t ss;
-    sigemptyset(&ss);
-    sigaddset(&ss, SIGUSR1 + offset);
-#if defined(__LP64__)
-    // For arm and x86, sigset_t was too small for the RT signals.
-    // For mips, sigset_t was large enough but jmp_buf wasn't.
-    sigaddset(&ss, SIGRTMIN + offset);
-#endif
+  static sigset64_t MakeSigSet(int offset) {
+    sigset64_t ss;
+    sigemptyset64(&ss);
+    sigaddset64(&ss, SIGUSR1 + offset);
+    sigaddset64(&ss, SIGRTMIN + offset);
     return ss;
   }
 
-  sigset_t one;
-  sigset_t two;
-  sigset_t original;
+  sigset64_t one;
+  sigset64_t two;
 };
 
-void AssertSigmaskEquals(const sigset_t& expected) {
-  sigset_t actual;
-  sigprocmask(0 /* ignored */, NULL, &actual);
-  size_t end = sizeof(sigset_t) * 8;
+void AssertSigmaskEquals(const sigset64_t& expected) {
+  sigset64_t actual;
+  sigprocmask64(SIG_SETMASK, NULL, &actual);
+  size_t end = sizeof(expected) * 8;
   for (size_t i = 1; i <= end; ++i) {
-    EXPECT_EQ(sigismember(&expected, i), sigismember(&actual, i)) << i;
+    EXPECT_EQ(sigismember64(&expected, i), sigismember64(&actual, i)) << i;
   }
 }
 
 TEST(setjmp, _setjmp_signal_mask) {
+  SignalMaskRestorer smr;
+
   // _setjmp/_longjmp do not save/restore the signal mask.
   SigSets ss;
-  sigprocmask(SIG_SETMASK, &ss.one, &ss.original);
+  sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
   jmp_buf jb;
   if (_setjmp(jb) == 0) {
-    sigprocmask(SIG_SETMASK, &ss.two, NULL);
+    sigprocmask64(SIG_SETMASK, &ss.two, NULL);
     _longjmp(jb, 1);
     FAIL(); // Unreachable.
   } else {
     AssertSigmaskEquals(ss.two);
   }
-  sigprocmask(SIG_SETMASK, &ss.original, NULL);
 }
 
 TEST(setjmp, setjmp_signal_mask) {
+  SignalMaskRestorer smr;
+
   // setjmp/longjmp do save/restore the signal mask on bionic, but not on glibc.
   // This is a BSD versus System V historical accident. POSIX leaves the
   // behavior unspecified, so any code that cares needs to use sigsetjmp.
   SigSets ss;
-  sigprocmask(SIG_SETMASK, &ss.one, &ss.original);
+  sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
   jmp_buf jb;
   if (setjmp(jb) == 0) {
-    sigprocmask(SIG_SETMASK, &ss.two, NULL);
+    sigprocmask64(SIG_SETMASK, &ss.two, NULL);
     longjmp(jb, 1);
     FAIL(); // Unreachable.
   } else {
@@ -129,37 +129,38 @@
     AssertSigmaskEquals(ss.two);
 #endif
   }
-  sigprocmask(SIG_SETMASK, &ss.original, NULL);
 }
 
 TEST(setjmp, sigsetjmp_0_signal_mask) {
+  SignalMaskRestorer smr;
+
   // sigsetjmp(0)/siglongjmp do not save/restore the signal mask.
   SigSets ss;
-  sigprocmask(SIG_SETMASK, &ss.one, &ss.original);
+  sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
   sigjmp_buf sjb;
   if (sigsetjmp(sjb, 0) == 0) {
-    sigprocmask(SIG_SETMASK, &ss.two, NULL);
+    sigprocmask64(SIG_SETMASK, &ss.two, NULL);
     siglongjmp(sjb, 1);
     FAIL(); // Unreachable.
   } else {
     AssertSigmaskEquals(ss.two);
   }
-  sigprocmask(SIG_SETMASK, &ss.original, NULL);
 }
 
 TEST(setjmp, sigsetjmp_1_signal_mask) {
+  SignalMaskRestorer smr;
+
   // sigsetjmp(1)/siglongjmp does save/restore the signal mask.
   SigSets ss;
-  sigprocmask(SIG_SETMASK, &ss.one, &ss.original);
+  sigprocmask64(SIG_SETMASK, &ss.one, nullptr);
   sigjmp_buf sjb;
   if (sigsetjmp(sjb, 1) == 0) {
-    sigprocmask(SIG_SETMASK, &ss.two, NULL);
+    sigprocmask64(SIG_SETMASK, &ss.two, NULL);
     siglongjmp(sjb, 1);
     FAIL(); // Unreachable.
   } else {
     AssertSigmaskEquals(ss.one);
   }
-  sigprocmask(SIG_SETMASK, &ss.original, NULL);
 }
 
 #if defined(__aarch64__)
@@ -218,7 +219,7 @@
 #elif defined(__aarch64__)
 #define __JB_SIGFLAG 0
 #elif defined(__i386__)
-#define __JB_SIGFLAG 7
+#define __JB_SIGFLAG 8
 #elif defined(__x86_64)
 #define __JB_SIGFLAG 8
 #elif defined(__mips__) && defined(__LP64__)