]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add Intel CET support for EH in libgcc.
authorIgor Tsimbalist <igor.v.tsimbalist@intel.com>
Fri, 17 Nov 2017 15:21:23 +0000 (16:21 +0100)
committerIgor Tsimbalist <itsimbal@gcc.gnu.org>
Fri, 17 Nov 2017 15:21:23 +0000 (16:21 +0100)
Control-flow Enforcement Technology (CET), published by Intel,
introduces the Shadow Stack feature, which ensures a return from a
function is done to exactly the same location from where the function
was called. When EH is present the control-flow transfer may skip some
stack frames and the shadow stack has to be adjusted not to signal a
violation of a control-flow transfer. It's done by counting a number
of skiping frames and adjasting shadow stack pointer by this number.

Having new semantic of the 'ret' instruction if CET is supported in HW
the 'ret' instruction cannot be generated in ix86_expand_epilogue when
we are returning after EH is processed. Added a code in
ix86_expand_epilogue to adjust Shadow Stack pointer and to generate an
indirect jump instead of 'ret'. As sp register is used during this
adjustment thus the argument in pro_epilogue_adjust_stack is changed
to update cfa_reg based on whether control-flow instrumentation is set.
Without updating the cfa_reg field there is an assert later in dwarf2
pass related to mismatch the stack register and cfa_reg value.

gcc/
* config/i386/i386.c (ix86_expand_epilogue): Change simple
return to indirect jump for EH return if control-flow protection
is enabled. Change explicit 'false' argument in
pro_epilogue_adjust_stack with a value of flag_cf_protection.
* config/i386/i386.md (simple_return_indirect_internal): Remove
SImode restriction to support 64-bit.

libgcc/
* config/i386/linux-unwind.h: Include
config/i386/shadow-stack-unwind.h.
* config/i386/shadow-stack-unwind.h: New file.
* unwind-dw2.c: (uw_install_context): Add a frame parameter and
pass it to _Unwind_Frames_Extra.
* unwind-generic.h (_Unwind_Frames_Extra): New.
* unwind.inc (_Unwind_RaiseException_Phase2): Add frames_p
parameter. Add local variable frames to count number of frames.
(_Unwind_ForcedUnwind_Phase2): Likewise.
(_Unwind_RaiseException): Add local variable frames to count
number of frames, pass it to _Unwind_RaiseException_Phase2 and
uw_install_context.
(_Unwind_ForcedUnwind): Likewise.
(_Unwind_Resume): Likewise.
(_Unwind_Resume_or_Rethrow): Likewise.

From-SVN: r254876

gcc/ChangeLog
gcc/config/i386/i386.c
gcc/config/i386/i386.md
libgcc/ChangeLog
libgcc/config/i386/linux-unwind.h
libgcc/config/i386/shadow-stack-unwind.h [new file with mode: 0644]
libgcc/unwind-dw2.c
libgcc/unwind-generic.h
libgcc/unwind.inc

index c8e169044ef5eb9a078a7e633cb7b6a586b74a9e..f706890ad37e3f2b528e56b09a39a56c3a668b30 100644 (file)
@@ -1,3 +1,13 @@
+2017-11-17  Igor Tsimbalist  <igor.v.tsimbalist@intel.com>
+
+       * config/i386/i386.c (ix86_expand_epilogue): Change simple
+       return to indirect jump for EH return if control-flow
+       protection is enabled. Change explicit 'false' argument in
+       pro_epilogue_adjust_stack with a value of
+       flag_cf_protection.
+       * config/i386/i386.md (simple_return_indirect_internal):
+       Remove SImode restriction to support 64-bit.
+
 2017-11-17  Segher Boessenkool  <segher@kernel.crashing.org>
 
        * combine.c (added_notes_insn): New.
index c9580baaaac2aef5c138880be1650301eaf0be76..a53666966d6ca356269fdf62b870c5d3fca1281b 100644 (file)
@@ -13960,7 +13960,9 @@ ix86_expand_epilogue (int style)
                 offset relative to SA, and after this insn we have no
                 other reasonable register to use for the CFA.  We don't
                 bother resetting the CFA to the SP for the duration of
-                the return insn.  */
+                the return insn, unless the control flow instrumentation
+                is done.  In this case the SP is used later and we have
+                to reset CFA to SP.  */
              add_reg_note (insn, REG_CFA_DEF_CFA,
                            plus_constant (Pmode, sa, UNITS_PER_WORD));
              ix86_add_queued_cfa_restore_notes (insn);
@@ -13972,7 +13974,8 @@ ix86_expand_epilogue (int style)
              m->fs.fp_valid = false;
 
              pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
-                                        const0_rtx, style, false);
+                                        const0_rtx, style,
+                                        flag_cf_protection);
            }
          else
            {
@@ -14156,7 +14159,32 @@ ix86_expand_epilogue (int style)
        emit_jump_insn (gen_simple_return_pop_internal (popc));
     }
   else if (!m->call_ms2sysv || !restore_stub_is_tail)
-    emit_jump_insn (gen_simple_return_internal ());
+    {
+      /* In case of return from EH a simple return cannot be used
+        as a return address will be compared with a shadow stack
+        return address.  Use indirect jump instead.  */
+      if (style == 2 && flag_cf_protection)
+       {
+         /* Register used in indirect jump must be in word_mode.  But
+            Pmode may not be the same as word_mode for x32.  */
+         rtx ecx = gen_rtx_REG (word_mode, CX_REG);
+         rtx_insn *insn;
+
+         insn = emit_insn (gen_pop (ecx));
+         m->fs.cfa_offset -= UNITS_PER_WORD;
+         m->fs.sp_offset -= UNITS_PER_WORD;
+
+         rtx x = plus_constant (Pmode, stack_pointer_rtx, UNITS_PER_WORD);
+         x = gen_rtx_SET (stack_pointer_rtx, x);
+         add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
+         add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (ecx, pc_rtx));
+         RTX_FRAME_RELATED_P (insn) = 1;
+
+         emit_jump_insn (gen_simple_return_indirect_internal (ecx));
+       }
+      else
+       emit_jump_insn (gen_simple_return_internal ());
+    }
 
   /* Restore the state back to the state from the prologue,
      so that it's correct for the next epilogue.  */
index 90e622cbaf565faba5e8452fc640964705e7ab54..1e918233ddf744c48860e93e40ad512874b40161 100644 (file)
 
 (define_insn "simple_return_indirect_internal"
   [(simple_return)
-   (use (match_operand:SI 0 "register_operand" "r"))]
+   (use (match_operand 0 "register_operand" "r"))]
   "reload_completed"
   "%!jmp\t%A0"
   [(set_attr "type" "ibr")
index da4d1adad91cd5b8737202711de6dd200cc248d6..234f0b274f6f6c23616d545c17a364dc41ff2930 100644 (file)
@@ -1,3 +1,21 @@
+2017-11-17  Igor Tsimbalist  <igor.v.tsimbalist@intel.com>
+
+       * config/i386/linux-unwind.h: Include
+       config/i386/shadow-stack-unwind.h.
+       * config/i386/shadow-stack-unwind.h: New file.
+       * unwind-dw2.c: (uw_install_context): Add a frame parameter and
+       pass it to _Unwind_Frames_Extra.
+       * unwind-generic.h (_Unwind_Frames_Extra): New.
+       * unwind.inc (_Unwind_RaiseException_Phase2): Add frames_p
+       parameter. Add local variable frames to count number of frames.
+       (_Unwind_ForcedUnwind_Phase2): Likewise.
+       (_Unwind_RaiseException): Add local variable frames to count
+       number of frames, pass it to _Unwind_RaiseException_Phase2 and
+       uw_install_context.
+       (_Unwind_ForcedUnwind): Likewise.
+       (_Unwind_Resume): Likewise.
+       (_Unwind_Resume_or_Rethrow): Likewise.
+
 2017-11-17  Igor Tsimbalist  <igor.v.tsimbalist@intel.com>
 
        * Makefile.in (configure_deps): Add $(srcdir)/../config/cet.m4.
index 2009ad72260df0b33fed7c42f99927e67781bb4b..c51b4d9087d08df58d51b28f55b6f6dcf886d2e0 100644 (file)
@@ -22,6 +22,11 @@ a copy of the GCC Runtime Library Exception along with this program;
 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 <http://www.gnu.org/licenses/>.  */
 
+/* Unwind shadow stack for -fcf-protection -mshstk.  */
+#if defined __SHSTK__ && defined __CET__
+# include "config/i386/shadow-stack-unwind.h"
+#endif
+
 /* Do code reading to identify a signal frame, and set the frame
    state data appropriately.  See unwind-dw2.c for the structs.
    Don't use this at all if inhibit_libc is used.  */
diff --git a/libgcc/config/i386/shadow-stack-unwind.h b/libgcc/config/i386/shadow-stack-unwind.h
new file mode 100644 (file)
index 0000000..2789e89
--- /dev/null
@@ -0,0 +1,51 @@
+/* _Unwind_Frames_Extra with shadow stack for x86-64 and x86.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+#ifdef __x86_64__
+# define incssp(x) __builtin_ia32_incsspq ((x))
+# define rdssp(x) __builtin_ia32_rdsspq (x)
+#else
+# define incssp(x) __builtin_ia32_incsspd ((x))
+# define rdssp(x) __builtin_ia32_rdsspd (x)
+#endif
+
+/* Unwind the shadow stack for EH.  */
+#undef _Unwind_Frames_Extra
+#define _Unwind_Frames_Extra(x)                        \
+  do                                           \
+    {                                          \
+      unsigned long ssp = 0;                   \
+      ssp = rdssp (ssp);                       \
+      if (ssp != 0)                            \
+       {                                       \
+         unsigned long tmp = (x);              \
+         while (tmp > 255)                     \
+           {                                   \
+             incssp (tmp);                     \
+             tmp -= 255;                       \
+           }                                   \
+         incssp (tmp);                         \
+       }                                       \
+    }                                          \
+    while (0)
index 3f26eaf73de3116b538dc8ba0a99f12de939c5e5..e80ac7418f25a719b65c49c45cd21c1377e5aadc 100644 (file)
@@ -1644,14 +1644,18 @@ uw_frob_return_addr (struct _Unwind_Context *current
 
 /* Install TARGET into CURRENT so that we can return to it.  This is a
    macro because __builtin_eh_return must be invoked in the context of
-   our caller.  */
+   our caller.  FRAMES is a number of frames to be unwind.
+   _Unwind_Frames_Extra is a macro to do additional work during unwinding
+   if needed, for example shadow stack pointer adjustment for Intel CET
+   technology.  */
 
-#define uw_install_context(CURRENT, TARGET)                            \
+#define uw_install_context(CURRENT, TARGET, FRAMES)                    \
   do                                                                   \
     {                                                                  \
       long offset = uw_install_context_1 ((CURRENT), (TARGET));                \
       void *handler = uw_frob_return_addr ((CURRENT), (TARGET));       \
       _Unwind_DebugHook ((TARGET)->cfa, handler);                      \
+      _Unwind_Frames_Extra (FRAMES);                                   \
       __builtin_eh_return (offset, handler);                           \
     }                                                                  \
   while (0)
index 77dd5a90202d2ae4928e442ea6abf09541103d83..570ea50a5a6096bd40d8c1b3111fa29bf225e7f3 100644 (file)
@@ -288,4 +288,7 @@ EXCEPTION_DISPOSITION _GCC_specific_handler (PEXCEPTION_RECORD, void *,
 #pragma GCC visibility pop
 #endif
 
+/* Additional actions to unwind number of stack frames.  */
+#define _Unwind_Frames_Extra(frames)
+
 #endif /* unwind.h */
index 658bd94f8f3a9d3afd48fa615f7aea766f85394b..a98154b1b7207ebe9b2a90e5288a72eba42bfabe 100644 (file)
 
 static _Unwind_Reason_Code
 _Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc,
-                             struct _Unwind_Context *context)
+                             struct _Unwind_Context *context,
+                             unsigned long *frames_p)
 {
   _Unwind_Reason_Code code;
+  unsigned long frames = 1;
 
   while (1)
     {
@@ -71,8 +73,10 @@ _Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc,
       gcc_assert (!match_handler);
 
       uw_update_context (context, &fs);
+      frames++;
     }
 
+  *frames_p = frames;
   return code;
 }
 
@@ -83,6 +87,7 @@ _Unwind_RaiseException(struct _Unwind_Exception *exc)
 {
   struct _Unwind_Context this_context, cur_context;
   _Unwind_Reason_Code code;
+  unsigned long frames;
 
   /* Set up this_context to describe the current stack frame.  */
   uw_init_context (&this_context);
@@ -128,11 +133,11 @@ _Unwind_RaiseException(struct _Unwind_Exception *exc)
   exc->private_2 = uw_identify_context (&cur_context);
 
   cur_context = this_context;
-  code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
+  code = _Unwind_RaiseException_Phase2 (exc, &cur_context, &frames);
   if (code != _URC_INSTALL_CONTEXT)
     return code;
 
-  uw_install_context (&this_context, &cur_context);
+  uw_install_context (&this_context, &cur_context, frames);
 }
 
 
@@ -140,11 +145,13 @@ _Unwind_RaiseException(struct _Unwind_Exception *exc)
 
 static _Unwind_Reason_Code
 _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc,
-                            struct _Unwind_Context *context)
+                            struct _Unwind_Context *context,
+                            unsigned long *frames_p)
 {
   _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) (_Unwind_Ptr) exc->private_1;
   void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2;
   _Unwind_Reason_Code code, stop_code;
+  unsigned long frames = 1;
 
   while (1)
     {
@@ -183,8 +190,10 @@ _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc,
       /* Update cur_context to describe the same frame as fs, and discard
         the previous context if necessary.  */
       uw_advance_context (context, &fs);
+      frames++;
     }
 
+  *frames_p = frames;
   return code;
 }
 
@@ -197,6 +206,7 @@ _Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
 {
   struct _Unwind_Context this_context, cur_context;
   _Unwind_Reason_Code code;
+  unsigned long frames;
 
   uw_init_context (&this_context);
   cur_context = this_context;
@@ -204,11 +214,11 @@ _Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
   exc->private_1 = (_Unwind_Ptr) stop;
   exc->private_2 = (_Unwind_Ptr) stop_argument;
 
-  code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
+  code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
   if (code != _URC_INSTALL_CONTEXT)
     return code;
 
-  uw_install_context (&this_context, &cur_context);
+  uw_install_context (&this_context, &cur_context, frames);
 }
 
 
@@ -220,6 +230,7 @@ _Unwind_Resume (struct _Unwind_Exception *exc)
 {
   struct _Unwind_Context this_context, cur_context;
   _Unwind_Reason_Code code;
+  unsigned long frames;
 
   uw_init_context (&this_context);
   cur_context = this_context;
@@ -227,13 +238,13 @@ _Unwind_Resume (struct _Unwind_Exception *exc)
   /* Choose between continuing to process _Unwind_RaiseException
      or _Unwind_ForcedUnwind.  */
   if (exc->private_1 == 0)
-    code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
+    code = _Unwind_RaiseException_Phase2 (exc, &cur_context, &frames);
   else
-    code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
+    code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
 
   gcc_assert (code == _URC_INSTALL_CONTEXT);
 
-  uw_install_context (&this_context, &cur_context);
+  uw_install_context (&this_context, &cur_context, frames);
 }
 
 
@@ -245,6 +256,7 @@ _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
 {
   struct _Unwind_Context this_context, cur_context;
   _Unwind_Reason_Code code;
+  unsigned long frames;
 
   /* Choose between continuing to process _Unwind_RaiseException
      or _Unwind_ForcedUnwind.  */
@@ -254,11 +266,11 @@ _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
   uw_init_context (&this_context);
   cur_context = this_context;
 
-  code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
+  code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context, &frames);
 
   gcc_assert (code == _URC_INSTALL_CONTEXT);
 
-  uw_install_context (&this_context, &cur_context);
+  uw_install_context (&this_context, &cur_context, frames);
 }