This is an archive of the discontinued LLVM Phabricator instance.

MachineFunction: -fsanitize={function,kcfi}: ensure 4-byte alignment
ClosedPublic

Authored by MaskRay on Jun 29 2023, 11:11 AM.

Details

Summary

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 doesn't set setMinFunctionAlignment to 4 or greater.

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

If __attribute__((aligned(N))) or -falign-functions=N is specified, the
larger alignment will be used.

Diff Detail

Event Timeline

MaskRay created this revision.Jun 29 2023, 11:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2023, 11:11 AM
MaskRay requested review of this revision.Jun 29 2023, 11:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 29 2023, 11:11 AM
samitolvanen accepted this revision.Jun 29 2023, 1:32 PM

Thanks for fixing this! This approach looks good to me.

llvm/test/CodeGen/RISCV/kcfi-patchable-function-prefix.ll
6–7

Nit: These checks are the same now, so we can replace both lines with one CHECK: line. Also the other instance below.

This revision is now accepted and ready to land.Jun 29 2023, 1:32 PM
MaskRay updated this revision to Diff 535994.Jun 29 2023, 2:11 PM

improve llvm/test/CodeGen/RISCV/kcfi-patchable-function-prefix.ll

samitolvanen accepted this revision.Jun 29 2023, 3:20 PM
simon_tatham accepted this revision.Jun 30 2023, 1:24 AM

Thanks – this looks much more like what I was imagining!

(I didn't just fix it myself this way because I was nervous about it affecting non-Arm architectures whose requirements I didn't know anything about. But @efriedma's comment on D154043 seems like a good argument that it's the right thing everywhere.)

This revision was landed with ongoing or failed builds.Jun 30 2023, 9:13 AM
This revision was automatically updated to reflect the committed changes.