libphobos: Fix segfault at run-time when using custom Fibers (PR 98025)
authorIain Buclaw <ibuclaw@gdcproject.org>
Fri, 27 Nov 2020 12:15:44 +0000 (13:15 +0100)
committerIain Buclaw <ibuclaw@gdcproject.org>
Fri, 27 Nov 2020 20:27:14 +0000 (21:27 +0100)
When libphobos is configured with --enable-cet, this adds extra fields
to the Fiber class to support the ucontext_t fallback implementation.
These fields get omitted when compiling user code unless they also used
`-fversion=CET' to build their project, which resulted in data being
overwritten from within swapcontext().

On reviewing the ucontext_t definitions, it was found that the shadow
stack fields were missing, and the struct size didn't match up on X32.
This has been fixed in upstream druntime and merged down here.

Reviewed-on: https://github.com/dlang/druntime/pull/3293

libphobos/ChangeLog:

PR d/98025
* Makefile.in: Regenerate.
* configure: Regenerate.
* configure.ac (DCFG_ENABLE_CET): Substitute.
* libdruntime/MERGE: Merge upstream druntime 0fe7974c.
* libdruntime/Makefile.in: Regenerate.
* libdruntime/core/thread.d: Import gcc.config.
(class Fiber): Add ucontext_t fields when GNU_Enable_CET is true.
* libdruntime/gcc/config.d.in (GNU_Enable_CET): Define.
* src/Makefile.in: Regenerate.
* testsuite/Makefile.in: Regenerate.

libphobos/Makefile.in
libphobos/configure
libphobos/configure.ac
libphobos/libdruntime/MERGE
libphobos/libdruntime/Makefile.in
libphobos/libdruntime/core/sys/posix/ucontext.d
libphobos/libdruntime/core/thread.d
libphobos/libdruntime/gcc/config.d.in
libphobos/src/Makefile.in
libphobos/testsuite/Makefile.in

index f692b2f719e068d1dcc718833dbe8292042cf2b7..a13959298197767bd43c011a3108ebf8c852c5ed 100644 (file)
@@ -217,6 +217,7 @@ CPPFLAGS = @CPPFLAGS@
 CYGPATH_W = @CYGPATH_W@
 DCFG_ARM_EABI_UNWINDER = @DCFG_ARM_EABI_UNWINDER@
 DCFG_DLPI_TLS_MODID = @DCFG_DLPI_TLS_MODID@
+DCFG_ENABLE_CET = @DCFG_ENABLE_CET@
 DCFG_HAVE_64BIT_ATOMICS = @DCFG_HAVE_64BIT_ATOMICS@
 DCFG_HAVE_ATOMIC_BUILTINS = @DCFG_HAVE_ATOMIC_BUILTINS@
 DCFG_HAVE_LIBATOMIC = @DCFG_HAVE_LIBATOMIC@
index 6d8461febf9631d3489b4c789ffaf57f8a39eacd..77a3125cbd6d7ad712b28734030542163aa2c0c9 100755 (executable)
@@ -722,6 +722,7 @@ LIBTOOL
 CFLAGS_FOR_BUILD
 CC_FOR_BUILD
 AR
+DCFG_ENABLE_CET
 CET_DFLAGS
 CET_FLAGS
 RANLIB
@@ -5652,11 +5653,20 @@ fi
 
 
 # To ensure that runtime code for CET is compiled in, add in D version flags.
-if test "$enable_cet" = yes; then
+if test x$enable_cet = xyes; then :
+
   CET_DFLAGS="$CET_FLAGS -fversion=CET"
+  DCFG_ENABLE_CET=true
+
+else
+
+  CET_DFLAGS=
+  DCFG_ENABLE_CET=false
 
 fi
 
+
+
 # This should be inherited in the recursive make, but ensure it is defined.
 test "$AR" || AR=ar
 
@@ -11744,7 +11754,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11747 "configure"
+#line 11757 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11850,7 +11860,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11853 "configure"
+#line 11863 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
index 60aee3ffe8b57a7a6f0883059288ce53bb1b6c25..2d51e465a153535ee8f310697dd40b1a122dd46f 100644 (file)
@@ -69,10 +69,15 @@ AC_PROG_MAKE_SET
 GCC_CET_FLAGS(CET_FLAGS)
 AC_SUBST(CET_FLAGS)
 # To ensure that runtime code for CET is compiled in, add in D version flags.
-if test "$enable_cet" = yes; then
+AS_IF([test x$enable_cet = xyes], [
   CET_DFLAGS="$CET_FLAGS -fversion=CET"
-  AC_SUBST(CET_DFLAGS)
-fi
+  DCFG_ENABLE_CET=true
+], [
+  CET_DFLAGS=
+  DCFG_ENABLE_CET=false
+])
+AC_SUBST(CET_DFLAGS)
+AC_SUBST(DCFG_ENABLE_CET)
 
 # This should be inherited in the recursive make, but ensure it is defined.
 test "$AR" || AR=ar
index 6b65a44e6d2d13278e344d0882ed49d7ff0bc71a..7162844b9b6a3d3dd52b20c915c7e128ff3b4800 100644 (file)
@@ -1,4 +1,4 @@
-d37ef985a97eb446371ab4b2315a52b87233fbf3
+0fe7974cf53b75db59461de2a3d6e53ce933d297
 
 The first line of this file holds the git revision number of the last
 merge done from the dlang/druntime repository.
index 0b895142a136b4c42725a0431e2e389e3c5bb7d3..99ee8b92afae18660831dc176d697ac79f0a6c4d 100644 (file)
@@ -577,6 +577,7 @@ CPPFLAGS = @CPPFLAGS@
 CYGPATH_W = @CYGPATH_W@
 DCFG_ARM_EABI_UNWINDER = @DCFG_ARM_EABI_UNWINDER@
 DCFG_DLPI_TLS_MODID = @DCFG_DLPI_TLS_MODID@
+DCFG_ENABLE_CET = @DCFG_ENABLE_CET@
 DCFG_HAVE_64BIT_ATOMICS = @DCFG_HAVE_64BIT_ATOMICS@
 DCFG_HAVE_ATOMIC_BUILTINS = @DCFG_HAVE_ATOMIC_BUILTINS@
 DCFG_HAVE_LIBATOMIC = @DCFG_HAVE_LIBATOMIC@
index 49a7c3e389a0dc3325cfa46e7d583d907c2cc415..2e518aefa8456154b4f1ff28c985a2ad03f7258b 100644 (file)
@@ -114,7 +114,7 @@ version (CRuntime_Glibc)
 
             enum NGREG = 23;
 
-            alias c_long            greg_t;
+            alias long              greg_t;
             alias greg_t[NGREG]     gregset_t;
             alias _libc_fpstate*    fpregset_t;
         }
@@ -123,7 +123,7 @@ version (CRuntime_Glibc)
         {
             gregset_t   gregs;
             fpregset_t  fpregs;
-            c_ulong[8]  __reserved1;
+            ulong[8]    __reserved1;
         }
 
         struct ucontext_t
@@ -134,6 +134,7 @@ version (CRuntime_Glibc)
             mcontext_t      uc_mcontext;
             sigset_t        uc_sigmask;
             _libc_fpstate   __fpregs_mem;
+            ulong[4]        __ssp;
         }
     }
     else version (X86)
@@ -205,6 +206,7 @@ version (CRuntime_Glibc)
             mcontext_t      uc_mcontext;
             sigset_t        uc_sigmask;
             _libc_fpstate   __fpregs_mem;
+            c_ulong[4]      __ssp;
         }
     }
     else version (HPPA)
index eaf088d53c119432b177b173004be4dfa8ebc5f2..7506a8b3ee38c81a664a4b01dacafecd37494f08 100644 (file)
@@ -52,6 +52,7 @@ version (Solaris)
 version (GNU)
 {
     import gcc.builtins;
+    import gcc.config;
     version (GNU_StackGrowsDown)
         version = StackGrowsDown;
 }
@@ -5123,6 +5124,15 @@ private:
         ucontext_t              m_utxt  = void;
         ucontext_t*             m_ucur  = null;
     }
+    else static if (GNU_Enable_CET)
+    {
+        // When libphobos was built with --enable-cet, these fields need to
+        // always be present in the Fiber class layout.
+        import core.sys.posix.ucontext;
+        static ucontext_t       sm_utxt = void;
+        ucontext_t              m_utxt  = void;
+        ucontext_t*             m_ucur  = null;
+    }
 
 
 private:
index 6301aaff069b1c4dc92796fb2e423f37a839bcd9..9ac7d055271d2ab57e4590f3a14d94b7981e697e 100644 (file)
@@ -49,3 +49,6 @@ enum GNU_Have_LibAtomic = @DCFG_HAVE_LIBATOMIC@;
 
 // Do we have qsort_r function
 enum Have_Qsort_R = @DCFG_HAVE_QSORT_R@;
+
+// Whether libphobos been configured with --enable-cet.
+enum GNU_Enable_CET = @DCFG_ENABLE_CET@;
index 4a0612a613bd225fad064d6a8219b25bcd2afb88..2e721783d068170be5aa87f793c941267671d0dc 100644 (file)
@@ -333,6 +333,7 @@ CPPFLAGS = @CPPFLAGS@
 CYGPATH_W = @CYGPATH_W@
 DCFG_ARM_EABI_UNWINDER = @DCFG_ARM_EABI_UNWINDER@
 DCFG_DLPI_TLS_MODID = @DCFG_DLPI_TLS_MODID@
+DCFG_ENABLE_CET = @DCFG_ENABLE_CET@
 DCFG_HAVE_64BIT_ATOMICS = @DCFG_HAVE_64BIT_ATOMICS@
 DCFG_HAVE_ATOMIC_BUILTINS = @DCFG_HAVE_ATOMIC_BUILTINS@
 DCFG_HAVE_LIBATOMIC = @DCFG_HAVE_LIBATOMIC@
index 2f6911d4d4754dfdc11e532e5ae12e29c9ded686..c38a4688258f3ff110d21ca08129bfe6a4550ee2 100644 (file)
@@ -161,6 +161,7 @@ CPPFLAGS = @CPPFLAGS@
 CYGPATH_W = @CYGPATH_W@
 DCFG_ARM_EABI_UNWINDER = @DCFG_ARM_EABI_UNWINDER@
 DCFG_DLPI_TLS_MODID = @DCFG_DLPI_TLS_MODID@
+DCFG_ENABLE_CET = @DCFG_ENABLE_CET@
 DCFG_HAVE_64BIT_ATOMICS = @DCFG_HAVE_64BIT_ATOMICS@
 DCFG_HAVE_ATOMIC_BUILTINS = @DCFG_HAVE_ATOMIC_BUILTINS@
 DCFG_HAVE_LIBATOMIC = @DCFG_HAVE_LIBATOMIC@