This is an archive of the discontinued LLVM Phabricator instance.

Don't emit prologues/epilogues for naked functions (PR18791, PR20028)
ClosedPublic

Authored by hans on Sep 3 2014, 7:17 PM.

Details

Summary

For naked functions with parameters, Clang would emit prologues that end up clobbering the stack because LLVM doesn't set up a stack frame. For example, this function on X86:

attribute((naked)) int f(int x) {

asm("movl $42, %eax");
asm("retl");

}

Results in:

_Z1fi:

movl    12(%esp), %eax
movl    %eax, (%esp)  <--- Oops.
movl    $42, %eax
retl

(This was already reported as PR18791, and PR20028 for the epilogue.)

My patch does three things, which can be committed separately, but I figured it's easier to review them together:

  • Don't emit prologues/epilogues for naked functions
  • Don't allow non-asm statements in naked functions
  • Don't allow asm statements to refer to parameters in naked functions.

Please take a look.

Diff Detail

Repository
rL LLVM

Event Timeline

hans updated this revision to Diff 13237.Sep 3 2014, 7:17 PM
hans retitled this revision from to Don't emit prologues/epilogues for naked functions (PR18791, PR20028).
hans updated this object.
hans edited the test plan for this revision. (Show Details)
hans added a reviewer: rnk.
hans added subscribers: Unknown Object (MLST), hansw.

Is supporting the attribute (( naked )) spelling on x86 intentional? It can certainly be a nice extension, but keep in mind that GCC only permits this on ARM, AVR, MCORE, MSP430, NDS32, RL78, RX and SPU ports. However, we need the __declspec(naked) spelling for MSVC compatibility. It might be nice to document that we support the attribute across the board rather than keep GCC compatibility.

thakis added a subscriber: thakis.Sep 3 2014, 7:42 PM
thakis added inline comments.
lib/Sema/SemaStmtAsm.cpp
414 ↗(On Diff #13237)

Do you need to Func->setInvalidDecl(); here too?

rnk accepted this revision.Sep 4 2014, 1:34 PM
rnk edited edge metadata.

lgtm

lib/Sema/SemaStmtAsm.cpp
414 ↗(On Diff #13237)

Let's do that.

test/CodeGen/attr-naked.c
21 ↗(On Diff #13237)

This CHECK will fail in a non-asserts build, because the name 'entry' will be elided. I guess I'd just rely on the CHECK-NOT for alloca and store followed by CHECK unreachable.

This revision is now accepted and ready to land.Sep 4 2014, 1:34 PM
hans added inline comments.Sep 4 2014, 1:38 PM
lib/Sema/SemaStmtAsm.cpp
414 ↗(On Diff #13237)

I commented on the email thread that maybe "return ExprError()" is the right thing to do here. That seems to be how the rest of this function does it.

hans closed this revision.Sep 4 2014, 3:26 PM
hans updated this revision to Diff 13289.

Closed by commit rL217198 (authored by @hans).