This is an archive of the discontinued LLVM Phabricator instance.

[LowerTypeTests] Add ENDBR to .cfi.jumptable for x86 Indirect Branch Tracking
ClosedPublic

Authored by MaskRay on Dec 24 2022, 1:24 PM.

Details

Summary

Similar to D81251 for AArch64 BTI. This fixes ./a.out test for

void foo(void) {}
void bar(void) {}
static void (*fptr)(void);
int main(int argc, char **argv) {
  if (argv[1]) fptr = foo;
  else fptr = bar;
  fptr();
}

clang -flto=thin -fvisibility=hidden -fsanitize=cfi-icall -fcf-protection=branch -fuse-ld=lld a.cc

Diff Detail

Event Timeline

MaskRay created this revision.Dec 24 2022, 1:24 PM
MaskRay requested review of this revision.Dec 24 2022, 1:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 24 2022, 1:24 PM
MaskRay edited the summary of this revision. (Show Details)Dec 24 2022, 1:29 PM
q66 added a subscriber: q66.Dec 24 2022, 2:28 PM

tested here and works great

I looked up x86 ENDBR and the adding of that instruction seems fine to me, but I have a couple questions on the related changes in the patch.

llvm/lib/Transforms/IPO/LowerTypeTests.cpp
1231

Why is this needed only in the endbr case?

1419

Add comment about why this attribute is needed. I see that the attribute is only checked for the x86 backends. But can be queried on the function for any arch - is there any possibility this feature will be implemented for non-x86 in the future? If so, I suggest adding the attribute unconditionally here.

Also, is this needed in the non-endbr case for x86? I.e. it is being added here for all x86 which presumably also changes the pre-existing non-endbr x86 handling.

MaskRay marked 2 inline comments as done.Dec 27 2022, 10:53 AM
MaskRay added inline comments.
llvm/lib/Transforms/IPO/LowerTypeTests.cpp
1231

The endbr case has a larger jump table entry size (16 bytes). We can add as many INT3 as needed, but .balign 16, 0xcc seems easier.

1419

This is similar to F->addFnAttr("branch-target-enforcement", "false"); for AArch64 BTI (D81251). Without this attribute, the function will have 2 consecutive endbr64 at the function start (one from the BTI instrumentation, the other from the first instruction of the inline asm instruction).

tejohnson accepted this revision.Jan 4 2023, 8:16 AM

lgtm with comment or change suggested below.

llvm/lib/Transforms/IPO/LowerTypeTests.cpp
1419

Please add a comment about why it is needed and that it does not have any effect in the case where CF branch protection is not enabled (alternatively, just guard this with whether the cf-protection-branch module flag is set).

This revision is now accepted and ready to land.Jan 4 2023, 8:16 AM
MaskRay updated this revision to Diff 486365.Jan 4 2023, 12:23 PM
MaskRay marked 3 inline comments as done.

add comments for ENDBR and BTI

This revision was landed with ongoing or failed builds.Jan 4 2023, 12:28 PM
This revision was automatically updated to reflect the committed changes.