Re: [PATCH 1/2] libphobos: fix CET for non-glibc targets

2021-12-28 Thread Alex Xu (Hello71) via Gcc-patches
Excerpts from ibuc...@gdcproject.org's message of December 20, 2021 4:00 pm:
>> On 20/12/2021 16:41 Alex Xu (Hello71)  wrote:
>> 
>>  
>> Excerpts from ibuc...@gdcproject.org's message of December 20, 2021 8:56 am:
>> >> On 20/12/2021 01:08 Alex Xu (Hello71) via Gcc-patches 
>> >>  wrote:
>> >> 
>> >>  
>> >> On musl, linking against libphobos fails because it requires ucontext
>> >> but is not explicitly linked against it. This is caused by configure
>> >> assuming that it is implemented in assembly, but it is actually not
>> >> implemented. This silently works on other libcs because context API does
>> >> not require an external library.
>> > 
>> > Thanks.
>> > 
>> > Looks reasonable to me, also for backporting to gcc-11, which also has the 
>> > same CET support.
>> > 
>> > Iain.
>> > 
>> 
>> Yes, we noticed this first on gcc 11. I tested that these patches fix 
>> the issue on gcc 11, and since nothing seems to have changed, I think 
>> the same problem exists and will be fixed by these patches on master.
>> 
> 
> Do you need me to commit this?
> 
> Iain.
> 

I wrote this patch for Alpine Linux and have submitted it for review 
and, since I do not have commit access to gcc, in the hope that someone 
can commit it so that non-Alpine users may benefit and also Alpine will 
not have to maintain the patch. Since I am not familiar with gcc 
development procedures, I don't know if that means I need something from 
you specifically, but if somebody could commit it, that would be 
appreciated.

Also, since I forgot to do so initially, please apply Signed-off-by: 
Alex Xu (Hello71) .

Cheers,
Alex.


Re: [PATCH 1/2] libphobos: fix CET for non-glibc targets

2021-12-20 Thread Alex Xu (Hello71) via Gcc-patches
Excerpts from ibuc...@gdcproject.org's message of December 20, 2021 8:56 am:
>> On 20/12/2021 01:08 Alex Xu (Hello71) via Gcc-patches 
>>  wrote:
>> 
>>  
>> On musl, linking against libphobos fails because it requires ucontext
>> but is not explicitly linked against it. This is caused by configure
>> assuming that it is implemented in assembly, but it is actually not
>> implemented. This silently works on other libcs because context API does
>> not require an external library.
> 
> Thanks.
> 
> Looks reasonable to me, also for backporting to gcc-11, which also has the 
> same CET support.
> 
> Iain.
> 

Yes, we noticed this first on gcc 11. I tested that these patches fix 
the issue on gcc 11, and since nothing seems to have changed, I think 
the same problem exists and will be fixed by these patches on master.

Cheers,
Alex.


[PATCH 2/2] libphobos: don't compile empty switchcontext.S

2021-12-19 Thread Alex Xu (Hello71) via Gcc-patches
If it does not contain any files, then there is no point compiling it.
Skipping this saves some milliseconds and ~650 bytes in libgphobos.a.
---
 libphobos/configure.ac|  1 +
 libphobos/libdruntime/Makefile.am |  2 ++
 .../libdruntime/config/x86/switchcontext.S| 25 ++-
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/libphobos/configure.ac b/libphobos/configure.ac
index c961e68105a..7169f016ffe 100644
--- a/libphobos/configure.ac
+++ b/libphobos/configure.ac
@@ -78,6 +78,7 @@ AS_IF([test x$enable_cet = xyes], [
 ])
 AC_SUBST(CET_DFLAGS)
 AC_SUBST(DCFG_ENABLE_CET)
+AM_CONDITIONAL([ENABLE_CET], [test x$enable_cet = xyes])
 
 # This should be inherited in the recursive make, but ensure it is defined.
 test "$AR" || AR=ar
diff --git a/libphobos/libdruntime/Makefile.am 
b/libphobos/libdruntime/Makefile.am
index 224d06e78ca..77d7934e705 100644
--- a/libphobos/libdruntime/Makefile.am
+++ b/libphobos/libdruntime/Makefile.am
@@ -93,9 +93,11 @@ if DRUNTIME_CPU_X86
 if DRUNTIME_OS_MINGW
 DRUNTIME_SOURCES_CONFIGURED += config/mingw/switchcontext.S
 else
+if !ENABLE_CET
 DRUNTIME_SOURCES_CONFIGURED += config/x86/switchcontext.S
 endif
 endif
+endif
 if DRUNTIME_CPU_SYSTEMZ
 DRUNTIME_SOURCES_CONFIGURED += config/systemz/get_tls_offset.S
 endif
diff --git a/libphobos/libdruntime/config/x86/switchcontext.S 
b/libphobos/libdruntime/config/x86/switchcontext.S
index 9f4befdb49c..adc46a3b89d 100644
--- a/libphobos/libdruntime/config/x86/switchcontext.S
+++ b/libphobos/libdruntime/config/x86/switchcontext.S
@@ -24,16 +24,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If 
not, see
 
 #include "../common/threadasm.S"
 
-/* NB: Generate the CET marker for -fcf-protection.  */
-#ifdef __CET__
-# include 
-#endif
+#if defined(__ELF__)
 
-#if !defined(__CET__)
-
-# if defined(__ELF__)
-
-#  if defined(__i386__)
+# if defined(__i386__)
 
 .text
 .globl CSYM(fiber_switchContext)
@@ -104,13 +97,13 @@ CSYM(fiber_switchContext):
 .cfi_endproc
.size CSYM(fiber_switchContext),.-CSYM(fiber_switchContext)
 
-#  endif /* defined(__ELF__) && defined(__x86_64__) && !defined(__ILP32__) */
+# endif /* defined(__ELF__) && defined(__x86_64__) && !defined(__ILP32__) */
 
-# endif /* defined(__ELF__) */
+#endif /* defined(__ELF__) */
 
-# if defined(__MACH__)
+#if defined(__MACH__)
 
-#  if defined(__i386__)
+# if defined(__i386__)
 
 .text
 .globl CSYM(fiber_switchContext)
@@ -247,8 +240,6 @@ LASFDE1:
 .p2align 3,0
 LEFDE1:
 
-#  endif /* defined(__MACH__) && defined(__x86_64__) && !defined(__ILP32__) */
+# endif /* defined(__MACH__) && defined(__x86_64__) && !defined(__ILP32__) */
 
-# endif /* defined (__MACH__) */
-
-#endif /* !defined(__CET__) */
+#endif /* defined (__MACH__) */
-- 
2.34.1



[PATCH 1/2] libphobos: fix CET for non-glibc targets

2021-12-19 Thread Alex Xu (Hello71) via Gcc-patches
On musl, linking against libphobos fails because it requires ucontext
but is not explicitly linked against it. This is caused by configure
assuming that it is implemented in assembly, but it is actually not
implemented. This silently works on other libcs because context API does
not require an external library.
---
 libphobos/m4/druntime/libraries.m4 | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libphobos/m4/druntime/libraries.m4 
b/libphobos/m4/druntime/libraries.m4
index 45a56f6f76a..e11582e433a 100644
--- a/libphobos/m4/druntime/libraries.m4
+++ b/libphobos/m4/druntime/libraries.m4
@@ -223,10 +223,14 @@ AC_DEFUN([DRUNTIME_LIBRARIES_UCONTEXT],
   case "$target_cpu" in
 aarch64* | \
 arm* | \
-i[[34567]]86|x86_64 | \
 powerpc)
   druntime_fiber_asm_external=yes
   ;;
+i[[34567]]86|x86_64)
+  if test "$enable_cet" = no; then
+druntime_fiber_asm_external=yes
+  fi
+  ;;
   esac
   if test "$druntime_fiber_asm_external" = no; then
 AC_SEARCH_LIBS([swapcontext], [c ucontext], [],
-- 
2.34.1