Blame SOURCES/gcc8-harden-2.patch

6d6b46
From 0df8313a0a5d8533f2487e21d7b42e9adee28f18 Mon Sep 17 00:00:00 2001
6d6b46
From: "H.J. Lu" <hjl.tools@gmail.com>
6d6b46
Date: Wed, 27 Oct 2021 06:27:15 -0700
6d6b46
Subject: [PATCH 2/4] x86: Add -mindirect-branch-cs-prefix
6d6b46
6d6b46
Add -mindirect-branch-cs-prefix to add CS prefix to call and jmp to
6d6b46
indirect thunk with branch target in r8-r15 registers so that the call
6d6b46
and jmp instruction length is 6 bytes to allow them to be replaced with
6d6b46
"lfence; call *%r8-r15" or "lfence; jmp *%r8-r15" at run-time.
6d6b46
6d6b46
gcc/
6d6b46
6d6b46
	PR target/102952
6d6b46
	* config/i386/i386.c (ix86_output_jmp_thunk_or_indirect): Emit
6d6b46
	CS prefix for -mindirect-branch-cs-prefix.
6d6b46
	(ix86_output_indirect_branch_via_reg): Likewise.
6d6b46
	* config/i386/i386.opt: Add -mindirect-branch-cs-prefix.
6d6b46
	* doc/invoke.texi: Document -mindirect-branch-cs-prefix.
6d6b46
6d6b46
gcc/testsuite/
6d6b46
6d6b46
	PR target/102952
6d6b46
	* gcc.target/i386/indirect-thunk-cs-prefix-1.c: New test.
6d6b46
	* gcc.target/i386/indirect-thunk-cs-prefix-2.c: Likewise.
6d6b46
6d6b46
(cherry picked from commit 2196a681d7810ad8b227bf983f38ba716620545e)
6d6b46
---
6d6b46
 gcc/config/i386/i386.c                            | 14 ++++++++++++--
6d6b46
 gcc/config/i386/i386.opt                          |  4 ++++
6d6b46
 gcc/doc/invoke.texi                               | 10 +++++++++-
6d6b46
 .../gcc.target/i386/indirect-thunk-cs-prefix-1.c  | 14 ++++++++++++++
6d6b46
 .../gcc.target/i386/indirect-thunk-cs-prefix-2.c  | 15 +++++++++++++++
6d6b46
 5 files changed, 54 insertions(+), 3 deletions(-)
6d6b46
 create mode 100644 gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c
6d6b46
 create mode 100644 gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c
6d6b46
6d6b46
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
6d6b46
index eb9303f8742..8442dd0daea 100644
6d6b46
--- a/gcc/config/i386/i386.c
6d6b46
+++ b/gcc/config/i386/i386.c
6d6b46
@@ -28728,7 +28728,12 @@ ix86_output_jmp_thunk_or_indirect (const char *thunk_name,
6d6b46
       if (need_prefix == indirect_thunk_prefix_bnd)
6d6b46
 	fprintf (asm_out_file, "\tbnd jmp\t");
6d6b46
       else
6d6b46
-	fprintf (asm_out_file, "\tjmp\t");
6d6b46
+	{
6d6b46
+	  if (REX_INT_REGNO_P (regno)
6d6b46
+	      && ix86_indirect_branch_cs_prefix)
6d6b46
+	    fprintf (asm_out_file, "\tcs\n");
6d6b46
+	  fprintf (asm_out_file, "\tjmp\t");
6d6b46
+	}
6d6b46
       assemble_name (asm_out_file, thunk_name);
6d6b46
       putc ('\n', asm_out_file);
6d6b46
       if ((ix86_harden_sls & harden_sls_indirect_branch))
6d6b46
@@ -28787,7 +28792,12 @@ ix86_output_indirect_branch_via_reg (rtx call_op, bool sibcall_p)
6d6b46
 	  if (need_prefix == indirect_thunk_prefix_bnd)
6d6b46
 	    fprintf (asm_out_file, "\tbnd call\t");
6d6b46
 	  else
6d6b46
-	    fprintf (asm_out_file, "\tcall\t");
6d6b46
+	    {
6d6b46
+	      if (REX_INT_REGNO_P (regno)
6d6b46
+		  && ix86_indirect_branch_cs_prefix)
6d6b46
+		fprintf (asm_out_file, "\tcs\n");
6d6b46
+	      fprintf (asm_out_file, "\tcall\t");
6d6b46
+	    }
6d6b46
 	  assemble_name (asm_out_file, thunk_name);
6d6b46
 	  putc ('\n', asm_out_file);
6d6b46
 	  return;
6d6b46
diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
6d6b46
index 3ae48609e25..9f67ef558dc 100644
6d6b46
--- a/gcc/config/i386/i386.opt
6d6b46
+++ b/gcc/config/i386/i386.opt
6d6b46
@@ -1044,6 +1044,10 @@ Enum(indirect_branch) String(thunk-inline) Value(indirect_branch_thunk_inline)
6d6b46
 EnumValue
6d6b46
 Enum(indirect_branch) String(thunk-extern) Value(indirect_branch_thunk_extern)
6d6b46
 
6d6b46
+mindirect-branch-cs-prefix
6d6b46
+Target Var(ix86_indirect_branch_cs_prefix) Init(0)
6d6b46
+Add CS prefix to call and jmp to indirect thunk with branch target in r8-r15 registers.
6d6b46
+
6d6b46
 mindirect-branch-register
6d6b46
 Target Report Var(ix86_indirect_branch_register) Init(0)
6d6b46
 Force indirect call and jump via register.
6d6b46
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
6d6b46
index 1e20efd6969..605cd4b93f1 100644
6d6b46
--- a/gcc/doc/invoke.texi
6d6b46
+++ b/gcc/doc/invoke.texi
6d6b46
@@ -1284,7 +1284,8 @@ See RS/6000 and PowerPC Options.
6d6b46
 -mstack-protector-guard-symbol=@var{symbol} -mmitigate-rop @gol
6d6b46
 -mgeneral-regs-only -mcall-ms2sysv-xlogues @gol
6d6b46
 -mindirect-branch=@var{choice} -mfunction-return=@var{choice} @gol
6d6b46
--mindirect-branch-register -mharden-sls=@var{choice}}
6d6b46
+-mindirect-branch-register -mharden-sls=@var{choice} @gol
6d6b46
+-mindirect-branch-cs-prefix}
6d6b46
 
6d6b46
 @emph{x86 Windows Options}
6d6b46
 @gccoptlist{-mconsole  -mcygwin  -mno-cygwin  -mdll @gol
6d6b46
@@ -28044,6 +28045,13 @@ hardening.  @samp{return} enables SLS hardening for function return.
6d6b46
 @samp{indirect-branch} enables SLS hardening for indirect branch.
6d6b46
 @samp{all} enables all SLS hardening.
6d6b46
 
6d6b46
+@item -mindirect-branch-cs-prefix
6d6b46
+@opindex mindirect-branch-cs-prefix
6d6b46
+Add CS prefix to call and jmp to indirect thunk with branch target in
6d6b46
+r8-r15 registers so that the call and jmp instruction length is 6 bytes
6d6b46
+to allow them to be replaced with @samp{lfence; call *%r8-r15} or
6d6b46
+@samp{lfence; jmp *%r8-r15} at run-time.
6d6b46
+
6d6b46
 @end table
6d6b46
 
6d6b46
 These @samp{-m} switches are supported in addition to the above
6d6b46
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c
6d6b46
new file mode 100644
6d6b46
index 00000000000..db2f3416823
6d6b46
--- /dev/null
6d6b46
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-1.c
6d6b46
@@ -0,0 +1,14 @@
6d6b46
+/* { dg-do compile { target { ! ia32 } } } */
6d6b46
+/* { dg-options "-O2 -ffixed-rax -ffixed-rbx -ffixed-rcx -ffixed-rdx -ffixed-rdi -ffixed-rsi -mindirect-branch-cs-prefix -mindirect-branch=thunk-extern" } */
6d6b46
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
6d6b46
+
6d6b46
+extern void (*fptr) (void);
6d6b46
+
6d6b46
+void
6d6b46
+foo (void)
6d6b46
+{
6d6b46
+  fptr ();
6d6b46
+}
6d6b46
+
6d6b46
+/* { dg-final { scan-assembler-times "jmp\[ \t\]+_?__x86_indirect_thunk_r\[0-9\]+" 1 } } */
6d6b46
+/* { dg-final { scan-assembler-times "\tcs" 1 } } */
6d6b46
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c
6d6b46
new file mode 100644
6d6b46
index 00000000000..adfc39a49d4
6d6b46
--- /dev/null
6d6b46
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-cs-prefix-2.c
6d6b46
@@ -0,0 +1,15 @@
6d6b46
+/* { dg-do compile { target { ! ia32 } } } */
6d6b46
+/* { dg-options "-O2 -ffixed-rax -ffixed-rbx -ffixed-rcx -ffixed-rdx -ffixed-rdi -ffixed-rsi -mindirect-branch-cs-prefix -mindirect-branch=thunk-extern" } */
6d6b46
+/* { dg-additional-options "-fno-pic" { target { ! *-*-darwin* } } } */
6d6b46
+
6d6b46
+extern void (*bar) (void);
6d6b46
+
6d6b46
+int
6d6b46
+foo (void)
6d6b46
+{
6d6b46
+  bar ();
6d6b46
+  return 0;
6d6b46
+}
6d6b46
+
6d6b46
+/* { dg-final { scan-assembler-times "call\[ \t\]+_?__x86_indirect_thunk_r\[0-9\]+" 1 } } */
6d6b46
+/* { dg-final { scan-assembler-times "\tcs" 1 } } */
6d6b46
-- 
6d6b46
2.36.1
6d6b46