[PATCH] D154043: [CodeGen] -fsanitize={function, kcfi}: ensure align 4 if +strict-align

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 28 23:18:29 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: efriedma, rjmccall, simon_tatham, samitolvanen.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fix https://github.com/llvm/llvm-project/issues/63579

  % cat a.c
  void foo() {}
  % clang --target=arm-none-eabi -mthumb -mno-unaligned-access -fsanitize=kcfi a.c -S -o - | grep p2align
          .p2align        1
  % clang --target=armv6m-none-eabi -fsanitize=function a.c -S -o - | grep p2align
          .p2align        1

With -mno-unaligned-access (possibly implicit), we should ensure that
-fsanitize={function,kcfi} instrumented functions are aligned by at least 4, so
that loading the type hash before the function label will not cause a misaligned
access, even if the backend doesn't set `setMinFunctionAlignment` to 4 or greater.

With this patch, the generated assembly for the examples above will contain `.p2align 2`.

If `-falign-functions=` is specified, take the maxiumum.

If `__attribute__((aligned(2)))` is specified, arbitrarily let the function
attribute win.

Since `SanOpts` is per-function, move the alignment setting code from
CodeGenModule::SetLLVMFunctionAttributesForDefinition to CodeGenFunction.
This move requires some attention.

Note: CodeGenModule::SetLLVMFunctionAttributesForDefinition is called by many
thunk codegen code with a dummy GlobalDecl/FunctionDecl.
However, in one call site, MicrosoftCXXABI::EmitVirtualMemPtrThunk has a
`SetLLVMFunctionAttributesForDefinition` use case that requires the
"Some C++ ABIs require 2-byte alignment for member functions" code. So
keep this part in CodeGenModule.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154043

Files:
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/kcfi.c
  clang/test/CodeGen/ubsan-function.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154043.535634.patch
Type: text/x-patch
Size: 6225 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230629/b4dca8db/attachment-0001.bin>


More information about the cfe-commits mailing list