x86: Generate INT3 for __builtin_eh_return
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 6 Jan 2022 02:04:21 +0000 (18:04 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 6 Jan 2022 19:35:11 +0000 (11:35 -0800)
Generate INT3 after indirect jmp in exception return for -fcf-protection
with -mharden-sls=indirect-jmp.

gcc/

PR target/103925
* config/i386/i386.c (ix86_output_indirect_function_return):
Generate INT3 after indirect jmp for -mharden-sls=indirect-jmp.

gcc/testsuite/

PR target/103925
* gcc.target/i386/harden-sls-6.c: New test.

gcc/config/i386/i386.c
gcc/testsuite/gcc.target/i386/harden-sls-6.c [new file with mode: 0644]

index 7365775..c4ed82d 100644 (file)
@@ -16382,11 +16382,14 @@ ix86_output_indirect_function_return (rtx ret_op)
        }
       else
        output_indirect_thunk (regno);
-
-      return "";
     }
   else
-    return "%!jmp\t%A0";
+    {
+      output_asm_insn ("%!jmp\t%A0", &ret_op);
+      if (ix86_harden_sls & harden_sls_indirect_jmp)
+       fputs ("\tint3\n", asm_out_file);
+    }
+  return "";
 }
 
 /* Output the assembly for a call instruction.  */
diff --git a/gcc/testsuite/gcc.target/i386/harden-sls-6.c b/gcc/testsuite/gcc.target/i386/harden-sls-6.c
new file mode 100644 (file)
index 0000000..9068eb6
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile { target { ! ia32 } } } */
+/* { dg-options "-O2 -fcf-protection -mharden-sls=indirect-jmp" } */
+
+struct _Unwind_Context _Unwind_Resume_or_Rethrow_this_context;
+
+void offset (int);
+
+struct _Unwind_Context {
+  void *reg[7];
+} _Unwind_Resume_or_Rethrow() {
+  struct _Unwind_Context cur_contextcur_context =
+      _Unwind_Resume_or_Rethrow_this_context;
+  offset(0);
+  __builtin_eh_return ((long) offset, 0);
+}
+
+/* { dg-final { scan-assembler "jmp\[ \t\]+\\*%rcx" } } */
+/* { dg-final { scan-assembler-times "int3" 1 } } */