llvm-project/clang/lib/CodeGen
Peter Collingbourne 0e497d1554 cfi-icall: Allow the jump table to be optionally made non-canonical.
The default behavior of Clang's indirect function call checker will replace
the address of each CFI-checked function in the output file's symbol table
with the address of a jump table entry which will pass CFI checks. We refer
to this as making the jump table `canonical`. This property allows code that
was not compiled with ``-fsanitize=cfi-icall`` to take a CFI-valid address
of a function, but it comes with a couple of caveats that are especially
relevant for users of cross-DSO CFI:

- There is a performance and code size overhead associated with each
  exported function, because each such function must have an associated
  jump table entry, which must be emitted even in the common case where the
  function is never address-taken anywhere in the program, and must be used
  even for direct calls between DSOs, in addition to the PLT overhead.

- There is no good way to take a CFI-valid address of a function written in
  assembly or a language not supported by Clang. The reason is that the code
  generator would need to insert a jump table in order to form a CFI-valid
  address for assembly functions, but there is no way in general for the
  code generator to determine the language of the function. This may be
  possible with LTO in the intra-DSO case, but in the cross-DSO case the only
  information available is the function declaration. One possible solution
  is to add a C wrapper for each assembly function, but these wrappers can
  present a significant maintenance burden for heavy users of assembly in
  addition to adding runtime overhead.

For these reasons, we provide the option of making the jump table non-canonical
with the flag ``-fno-sanitize-cfi-canonical-jump-tables``. When the jump
table is made non-canonical, symbol table entries point directly to the
function body. Any instances of a function's address being taken in C will
be replaced with a jump table address.

This scheme does have its own caveats, however. It does end up breaking
function address equality more aggressively than the default behavior,
especially in cross-DSO mode which normally preserves function address
equality entirely.

Furthermore, it is occasionally necessary for code not compiled with
``-fsanitize=cfi-icall`` to take a function address that is valid
for CFI. For example, this is necessary when a function's address
is taken by assembly code and then called by CFI-checking C code. The
``__attribute__((cfi_jump_table_canonical))`` attribute may be used to make
the jump table entry of a specific function canonical so that the external
code will end up taking a address for the function that will pass CFI checks.

Fixes PR41972.

Differential Revision: https://reviews.llvm.org/D65629

llvm-svn: 368495
2019-08-09 22:31:59 +00:00
..
ABIInfo.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Address.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
BackendUtil.cpp [clang] Add no-warn support for Wa 2019-08-08 19:19:20 +00:00
CGAtomic.cpp add periods 2019-04-03 22:19:07 +00:00
CGBlocks.cpp [CodeGen][ObjC] Annotate retain-agnostic ObjC globals with attribute 2019-06-14 22:06:28 +00:00
CGBlocks.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGBuilder.h Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGBuiltin.cpp Builtins: Start adding half versions of math builtins 2019-08-06 03:28:37 +00:00
CGCUDANV.cpp [clang][CodeGen] Remove std::move on temporary 2019-06-17 14:23:06 +00:00
CGCUDARuntime.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGCUDARuntime.h [HIP] Add the interface deriving the stub name of device kernels. 2019-06-17 12:51:36 +00:00
CGCXX.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGCXXABI.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGCXXABI.h [OpenCL] Improve destructor support in C++ for OpenCL 2019-07-22 09:39:13 +00:00
CGCall.cpp [OpenCL] Improve destructor support in C++ for OpenCL 2019-07-22 09:39:13 +00:00
CGCall.h [opaque pointer types] Make EmitCall pass Function Types to 2019-02-07 01:15:41 +00:00
CGClass.cpp [OpenCL] Improve destructor support in C++ for OpenCL 2019-07-22 09:39:13 +00:00
CGCleanup.cpp [NFC] avoid AlignedCharArray in clang 2019-07-29 23:12:48 +00:00
CGCleanup.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGCoroutine.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGDebugInfo.cpp Add SVE opaque built-in types 2019-08-09 08:52:54 +00:00
CGDebugInfo.h Revert "[CGDebugInfo] Simplify EmitFunctionDecl parameters, NFC" 2019-07-11 19:28:07 +00:00
CGDecl.cpp [OpenMP 5.0] Codegen support for user-defined mappers. 2019-08-05 18:43:21 +00:00
CGDeclCXX.cpp ARM MTE stack sanitizer. 2019-07-15 20:02:23 +00:00
CGException.cpp Updated the signature for some stack related intrinsics (CLANG) 2019-07-22 12:50:30 +00:00
CGExpr.cpp [BPF] annotate DIType metadata for builtin preseve_array_access_index() 2019-08-02 21:28:28 +00:00
CGExprAgg.cpp Revert "Revert "CodeGen: ensure placeholder instruction for cleanup is created"" 2019-07-25 20:59:48 +00:00
CGExprCXX.cpp Recommit Devirtualize destructor of final class. 2019-08-08 18:00:49 +00:00
CGExprComplex.cpp [C++2a] Add __builtin_bit_cast, used to implement std::bit_cast 2019-07-02 18:28:13 +00:00
CGExprConstant.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGExprScalar.cpp Treat the range of representable values of floating-point types as [-inf, +inf] not as [-max, +max]. 2019-07-06 21:05:52 +00:00
CGGPUBuiltin.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGLoopInfo.cpp [Clang] New loop pragma vectorize_predicate 2019-07-25 07:33:13 +00:00
CGLoopInfo.h [Clang] New loop pragma vectorize_predicate 2019-07-25 07:33:13 +00:00
CGNonTrivialStruct.cpp Add { } to silence compiler warning [NFC] 2019-04-11 06:57:12 +00:00
CGObjC.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGObjCGNU.cpp [gnustep-objc] Make the GNUstep v2 ABI work for Windows DLLs. 2019-03-31 11:22:33 +00:00
CGObjCMac.cpp Silence gcc warning "control reaches end of non-void function" [NFCI] 2019-07-05 06:12:24 +00:00
CGObjCRuntime.cpp [opaque pointer types] Pass function types for runtime function calls. 2019-02-05 16:42:33 +00:00
CGObjCRuntime.h [opaque pointer types] Pass function types for runtime function calls. 2019-02-05 16:42:33 +00:00
CGOpenCLRuntime.cpp [OpenCL] Simplify LLVM IR generated for OpenCL blocks 2019-02-21 11:02:10 +00:00
CGOpenCLRuntime.h [OpenCL] Simplify LLVM IR generated for OpenCL blocks 2019-02-21 11:02:10 +00:00
CGOpenMPRuntime.cpp [OpenMP] Add support for close map modifier in Clang 2019-08-09 21:42:13 +00:00
CGOpenMPRuntime.h [OpenMP 5.0] Codegen support for user-defined mappers. 2019-08-05 18:43:21 +00:00
CGOpenMPRuntimeNVPTX.cpp [OPENMP][NVPTX]Mark barrier functions calls as convergent. 2019-07-18 13:49:24 +00:00
CGOpenMPRuntimeNVPTX.h [OpenMP] Add support for registering requires directives with the runtime 2019-05-21 19:42:01 +00:00
CGRecordLayout.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGRecordLayoutBuilder.cpp P0840R2: support for [[no_unique_address]] attribute 2019-06-20 20:44:45 +00:00
CGStmt.cpp Delay diagnosing asm constraints that require immediates until after inlining 2019-08-06 22:41:22 +00:00
CGStmtOpenMP.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CGVTT.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGVTables.cpp [CodeGen] Set construction vtable visibility after creating initializer 2019-02-11 20:13:42 +00:00
CGVTables.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CGValue.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CMakeLists.txt Make CodeGen depend on ASTMatchers 2019-06-26 14:13:43 +00:00
CodeGenABITypes.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CodeGenAction.cpp Move LangStandard*, InputKind::Language to Basic 2019-08-05 13:59:26 +00:00
CodeGenFunction.cpp cfi-icall: Allow the jump table to be optionally made non-canonical. 2019-08-09 22:31:59 +00:00
CodeGenFunction.h [OpenCL] Improve destructor support in C++ for OpenCL 2019-07-22 09:39:13 +00:00
CodeGenModule.cpp cfi-icall: Allow the jump table to be optionally made non-canonical. 2019-08-09 22:31:59 +00:00
CodeGenModule.h C++ DR712 and others: handle non-odr-use resulting from an lvalue-to-rvalue conversion applied to a member access or similar not-quite-trivial lvalue expression. 2019-06-14 17:46:37 +00:00
CodeGenPGO.cpp [MS] Don't emit coverage for deleting dtors 2019-02-26 20:42:52 +00:00
CodeGenPGO.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CodeGenTBAA.cpp Fix TBAA representation for zero-sized fields and unnamed bit-fields. 2019-06-22 21:30:43 +00:00
CodeGenTBAA.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CodeGenTypeCache.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CodeGenTypes.cpp Add SVE opaque built-in types 2019-08-09 08:52:54 +00:00
CodeGenTypes.h IRGen: Remove StructorType; thread GlobalDecl through more code. NFCI. 2019-03-22 23:05:10 +00:00
ConstantEmitter.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ConstantInitBuilder.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
CoverageMappingGen.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
CoverageMappingGen.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
EHScopeStack.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ItaniumCXXABI.cpp Add SVE opaque built-in types 2019-08-09 08:52:54 +00:00
MacroPPCallbacks.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
MacroPPCallbacks.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
MicrosoftCXXABI.cpp [OpenCL] Improve destructor support in C++ for OpenCL 2019-07-22 09:39:13 +00:00
ModuleBuilder.cpp [OpenMP 5.0] Codegen support for user-defined mappers. 2019-08-05 18:43:21 +00:00
ObjectFilePCHContainerOperations.cpp [Bitcode] Move Bitstream to a separate library 2019-07-03 22:40:07 +00:00
PatternInit.cpp CodeGet: Init 32bit pointers with 0xFFFFFFFF 2019-07-12 17:21:55 +00:00
PatternInit.h Variable auto-init: also auto-init alloca 2019-04-12 00:11:27 +00:00
README.txt
SanitizerMetadata.cpp ARM MTE stack sanitizer. 2019-07-15 20:02:23 +00:00
SanitizerMetadata.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
SwiftCallingConv.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
TargetInfo.cpp [NFC] Remove unused variable 2019-07-19 21:59:42 +00:00
TargetInfo.h [OpenCL][PR41727] Prevent ICE on global dtors 2019-07-15 11:58:10 +00:00
VarBypassDetector.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
VarBypassDetector.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00

README.txt

IRgen optimization opportunities.

//===---------------------------------------------------------------------===//

The common pattern of
--
short x; // or char, etc
(x == 10)
--
generates an zext/sext of x which can easily be avoided.

//===---------------------------------------------------------------------===//

Bitfields accesses can be shifted to simplify masking and sign
extension. For example, if the bitfield width is 8 and it is
appropriately aligned then is is a lot shorter to just load the char
directly.

//===---------------------------------------------------------------------===//

It may be worth avoiding creation of alloca's for formal arguments
for the common situation where the argument is never written to or has
its address taken. The idea would be to begin generating code by using
the argument directly and if its address is taken or it is stored to
then generate the alloca and patch up the existing code.

In theory, the same optimization could be a win for block local
variables as long as the declaration dominates all statements in the
block.

NOTE: The main case we care about this for is for -O0 -g compile time
performance, and in that scenario we will need to emit the alloca
anyway currently to emit proper debug info. So this is blocked by
being able to emit debug information which refers to an LLVM
temporary, not an alloca.

//===---------------------------------------------------------------------===//

We should try and avoid generating basic blocks which only contain
jumps. At -O0, this penalizes us all the way from IRgen (malloc &
instruction overhead), all the way down through code generation and
assembly time.

On 176.gcc:expr.ll, it looks like over 12% of basic blocks are just
direct branches!

//===---------------------------------------------------------------------===//