diff options
author | Fangrui Song <i@maskray.me> | 2023-06-30 09:13:19 -0700 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2023-06-30 09:13:19 -0700 |
commit | afd20587f9952654a9a9ec427052220b5e3a6982 (patch) | |
tree | 79a4607b91f845e9576aac0c7b42444b931ee2bc /llvm/lib/CodeGen/MachineFunction.cpp | |
parent | e5cc56a0d1026ca25410b3abea64496c13bfbbe3 (diff) | |
download | llvm-afd20587f9952654a9a9ec427052220b5e3a6982.zip llvm-afd20587f9952654a9a9ec427052220b5e3a6982.tar.gz llvm-afd20587f9952654a9a9ec427052220b5e3a6982.tar.bz2 |
MachineFunction: -fsanitize={function,kcfi}: ensure 4-byte alignment
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
```
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. This is especially important for -mno-unaligned-access
configurations that don't set `setMinFunctionAlignment` to 4 or greater.
With this patch, the generated assembly for the examples above will contain `.p2align 2`
before the type hash.
If `__attribute__((aligned(N)))` or `-falign-functions=N` is specified, the
larger alignment will be used.
Reviewed By: simon_tatham, samitolvanen
Differential Revision: https://reviews.llvm.org/D154125
Diffstat (limited to 'llvm/lib/CodeGen/MachineFunction.cpp')
-rw-r--r-- | llvm/lib/CodeGen/MachineFunction.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp index a934a5c..88939e9 100644 --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -212,6 +212,14 @@ void MachineFunction::init() { Alignment = std::max(Alignment, STI->getTargetLowering()->getPrefFunctionAlignment()); + // -fsanitize=function and -fsanitize=kcfi instrument indirect function calls + // to load a type hash before the function label. Ensure functions are aligned + // by a least 4 to avoid unaligned access, which is especially important for + // -mno-unaligned-access. + if (F.hasMetadata(LLVMContext::MD_func_sanitize) || + F.getMetadata(LLVMContext::MD_kcfi_type)) + Alignment = std::max(Alignment, Align(4)); + if (AlignAllFunctions) Alignment = Align(1ULL << AlignAllFunctions); |