This is an archive of the discontinued LLVM Phabricator instance.

EntryExitInstrumenter: skip naked functions
ClosedPublic

Authored by MaskRay on May 2 2023, 9:08 PM.

Details

Summary

The asm in a naked function may reasonably expect the argument registers and the
return address register (if present) to be live.

When using -pg and -finstrument-functions, functions are instrumented by adding
a function call to _mcount/__cyg_profile_func_enter/__cyg_profile_func_enter_bare/etc,
which will clobber these registers. If the return address register is clobbered,
the function will be unable to return to the caller, possibly causing an
infinite loop.

__attribute__((naked)) void g() {
#if defined(__arm__)
  __asm__("bx lr");
#else
  __asm__("ret");
#endif
}

int main() { g(); }

It seems that the only one reasonable way to handle the combination is to
disable instrumenting for naked functions.

GCC PR: https://gcc.gnu.org/PR109707
Close https://github.com/llvm/llvm-project/issues/62504

Diff Detail

Event Timeline

MaskRay created this revision.May 2 2023, 9:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 9:08 PM
Herald added subscribers: hoy, hiraditya. · View Herald Transcript
MaskRay requested review of this revision.May 2 2023, 9:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 9:08 PM
MaskRay updated this revision to Diff 518967.May 2 2023, 9:44 PM

add a comment

MaskRay edited the summary of this revision. (Show Details)May 2 2023, 9:53 PM
MaskRay edited the summary of this revision. (Show Details)
hans added a comment.May 3 2023, 2:16 AM

Seems reasonable to me.

Would -fprofile-instr-generate and -fprofile-generate have the same issue?

Do we have any docs about the naked attribute that should be updated?

Seems reasonable to me.

Would -fprofile-instr-generate and -fprofile-generate have the same issue?

No. The two instrumentations don't insert calls, and are therefore unaffected.

-fsanitize-coverage= works with naked by skipping an entry block ending with an unreachable.

Do we have any docs about the naked attribute that should be updated?

I think a doc need isn't strong, and even if does, the doc need likely belongs to the individual instrumentation features.
An instrumentation may skip many functions and we don't document all of them...

For this patch, I think there isn't anything to update...

hans accepted this revision.May 4 2023, 2:19 AM

lgtm

This revision is now accepted and ready to land.May 4 2023, 2:19 AM
This revision was automatically updated to reflect the committed changes.