[PATCH] D140035: [X86] Prevent -mibt-seal to work together with -flto=thin

Joao Moreira via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 14 10:32:59 PST 2022


joaomoreira created this revision.
joaomoreira added reviewers: samitolvanen, nickdesaulniers, xiangzhangllvm, pengfei, aaron.ballman, pcc, gftg85.
joaomoreira added a project: LLVM.
Herald added a subscriber: inglorion.
Herald added a project: All.
joaomoreira requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

As pointed out by @samitolvanen and then confirmed by @nickdesaulniers  here - https://github.com/ClangBuiltLinux/linux/issues/1737 - -flto=thin does not reliably know about address-taken properties across different translation units. This breaks the assumptions behind the -mibt-seal optimization, that removes ENDBR instructions from prologues of functions which are observed as non-address-taken.

Thus, because of the above, prevent -mibt-seal from being used together with -flto=thin by only enabling it if lto is Full.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140035

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGen/X86/x86-cf-protection.c


Index: clang/test/CodeGen/X86/x86-cf-protection.c
===================================================================
--- clang/test/CodeGen/X86/x86-cf-protection.c
+++ clang/test/CodeGen/X86/x86-cf-protection.c
@@ -2,6 +2,7 @@
 // RUN: %clang_cc1 -E -triple i386 -dM -o - -fcf-protection=branch %s | FileCheck %s --check-prefix=BRANCH
 // RUN: %clang_cc1 -E -triple i386 -dM -o - -fcf-protection=full %s   | FileCheck %s --check-prefix=FULL
 // RUN: %clang_cc1 -emit-llvm -triple i386 -o - -fcf-protection=branch -mibt-seal -flto %s | FileCheck %s --check-prefixes=CFPROT,IBTSEAL
+// RUN: %clang_cc1 -emit-llvm -triple i386 -o - -fcf-protection=branch -mibt-seal -flto=thin %s | FileCheck %s --check-prefixes=CFPROT,NOIBTSEAL
 // RUN: %clang_cc1 -emit-llvm -triple i386 -o - -fcf-protection=branch -flto %s | FileCheck %s --check-prefixes=CFPROT,NOIBTSEAL
 // RUN: %clang_cc1 -emit-llvm -triple i386 -o - -fcf-protection=branch -mibt-seal %s | FileCheck %s --check-prefixes=CFPROT,NOIBTSEAL
 // RUN: not %clang_cc1 -emit-llvm-only -triple i386 -target-cpu pentium-mmx -fcf-protection=branch %s 2>&1 | FileCheck %s --check-prefix=NOCFPROT
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -1853,8 +1853,11 @@
       Opts.FunctionReturnThunks = static_cast<unsigned>(Val);
   }
 
-  if (Opts.PrepareForLTO && Args.hasArg(OPT_mibt_seal))
+  if (Opts.PrepareForLTO &&
+      !Opts.PrepareForThinLTO &&
+      Args.hasArg(OPT_mibt_seal)) {
     Opts.IBTSeal = 1;
+  }
 
   for (auto *A :
        Args.filtered(OPT_mlink_bitcode_file, OPT_mlink_builtin_bitcode)) {
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -6439,7 +6439,7 @@
         Args.MakeArgString(Twine("-fcf-protection=") + A->getValue()));
   }
 
-  if (IsUsingLTO)
+  if (LTOMode == LTOK_Full)
     Args.AddLastArg(CmdArgs, options::OPT_mibt_seal);
 
   if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140035.482923.patch
Type: text/x-patch
Size: 2193 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221214/df2b55ee/attachment-0001.bin>


More information about the cfe-commits mailing list