This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] -fpatchable-function-entry=N,0: place patch label after BTI
ClosedPublic

Authored by MaskRay on Jan 29 2020, 9:03 PM.

Details

Summary

For -fpatchable-function-entry=N,0 -mbranch-protection=bti, after
9a24488cb67a90f889529987275c5e411ce01dda, we place the NOP sled after
the initial BTI.

.Lfunc_begin0:
bti c
nop
nop

.section __patchable_function_entries,"awo",@progbits,f,unique,0
.p2align 3
.xword .Lfunc_begin0

This patch adds a label after the initial BTI and changes the __patchable_function_entries entry to reference the label:

.Lfunc_begin0:
bti c
.Lpatch0:
nop
nop

.section __patchable_function_entries,"awo",@progbits,f,unique,0
.p2align 3
.xword .Lpatch0

This placement is compatible with the resolution in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92424 .

A local linkage function whose address is not taken does not need a BTI.
Placing the patch label after BTI has the advantage that code does not
need to differentiate whether the function has an initial BTI.

Diff Detail

Event Timeline

MaskRay created this revision.Jan 29 2020, 9:03 PM
MaskRay edited the summary of this revision. (Show Details)Jan 29 2020, 9:20 PM

Unit tests: pass. 62315 tests passed, 0 failed and 838 were skipped.

clang-tidy: pass.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Pre-merge checks is in beta. Report issue. Please join beta or enable it for your project.

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- CC=/tmp/ReleaseA/bin/clang LD=/tmp/ReleaseA/bin/ld.lld KCFLAGS=-mbranch-protection=bti O=/tmp/out/arm64 allmodconfig all -j 30

Picked one object file randomly. Verified that .rela__patchable_function_entries reference the instructions after BTI.

nickdesaulniers accepted this revision.Jan 30 2020, 9:28 AM

Indeed, it looks like just from the emitted assembly GCC and LLVM will differ where they put the .section directives relative to the .text, but once assembled there's no difference from an object file perspective. I find putting the __patchable_function_entries outside of the function a nice touch. Thanks for the patch @MaskRay !

llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll
15

Would it be helpful have a test for patchable-function-entry > 1 and branch-target-enforcement?

This revision is now accepted and ready to land.Jan 30 2020, 9:28 AM
MaskRay marked an inline comment as done.Jan 30 2020, 9:39 AM
MaskRay added inline comments.
llvm/test/CodeGen/AArch64/patchable-function-entry-bti.ll
15

I feel that may be redundant. Different numbers of NOPs are tested in patchable-function-entry.ll . For interaction with BTI, this one is sufficient.

This revision was automatically updated to reflect the committed changes.