MachineFunction: -fsanitize={function,kcfi}: ensure 4-byte alignment
authorFangrui Song <i@maskray.me>
Fri, 30 Jun 2023 16:13:19 +0000 (09:13 -0700)
committerFangrui Song <i@maskray.me>
Fri, 30 Jun 2023 16:13:19 +0000 (09:13 -0700)
commitafd20587f9952654a9a9ec427052220b5e3a6982
tree79a4607b91f845e9576aac0c7b42444b931ee2bc
parente5cc56a0d1026ca25410b3abea64496c13bfbbe3
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
llvm/lib/CodeGen/MachineFunction.cpp
llvm/test/CodeGen/ARM/func-sanitizer.ll [new file with mode: 0644]
llvm/test/CodeGen/ARM/kcfi.ll [new file with mode: 0644]
llvm/test/CodeGen/RISCV/kcfi-patchable-function-prefix.ll