This is an archive of the discontinued LLVM Phabricator instance.

Fix a crash with variably-modified parameter types in a naked function
ClosedPublic

Authored by aaron.ballman on Mar 23 2022, 12:35 PM.

Details

Summary

Naked functions have no prolog, so it's not valid to emit prolog code to evaluate the variably-modified type. This fixes Issue 50541.

Diff Detail

Event Timeline

aaron.ballman created this revision.Mar 23 2022, 12:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2022, 12:35 PM
aaron.ballman requested review of this revision.Mar 23 2022, 12:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2022, 12:35 PM

Other than this 1 thing, LGTM. I DO find myself wondering how much of the CXXMethodDecl bit in the branch above is valid for 'naked' as well. If it ISN'T we might consider moving this loop AND that to EmitFunctionPrologue.

clang/lib/CodeGen/CodeGenFunction.cpp
1200

Since you're touching it... this looks like a 'range-for' loop :D

Updating based on review feedback

Other than this 1 thing, LGTM. I DO find myself wondering how much of the CXXMethodDecl bit in the branch above is valid for 'naked' as well. If it ISN'T we might consider moving this loop AND that to EmitFunctionPrologue.

There's a different kind of bug there. MSVC disallows using the naked attribute on a member function. Clang doesn't enforce this: https://godbolt.org/z/6jKGbzYTq GCC has no such restriction, but GCC doesn't have many diagnostics in this area anyway. We may want to reconsider whether allowing the naked attribute on a non static member function is a good idea. We probably don't want to allow it in MS compatibility mode at the very least. But I think this can be a change for another day. WDYT?

clang/lib/CodeGen/CodeGenFunction.cpp
1200

Sure, I can do that.

Other than this 1 thing, LGTM. I DO find myself wondering how much of the CXXMethodDecl bit in the branch above is valid for 'naked' as well. If it ISN'T we might consider moving this loop AND that to EmitFunctionPrologue.

There's a different kind of bug there. MSVC disallows using the naked attribute on a member function. Clang doesn't enforce this: https://godbolt.org/z/6jKGbzYTq GCC has no such restriction, but GCC doesn't have many diagnostics in this area anyway. We may want to reconsider whether allowing the naked attribute on a non static member function is a good idea. We probably don't want to allow it in MS compatibility mode at the very least. But I think this can be a change for another day. WDYT?

Ah! I didn't know the bit about MSVC! I think that makes it even more interesting. From what I can tell however, GCC actually DOES 'do the right thing' with member functions, though the motivation for them is somewhat suspicious. I definitely agree we don't want to allow it in MS Compat mode (and agree that is a different patch). As for in GCC mode, a part of me wants to just DO it and see what the fallout is. The only value I can see for these as non-static member functions is in a template (or perhaps as a virtual function? *shudders*), and I don't see much overlap in the usage there.

clang/test/CodeGen/attr-naked.c
31

so in the test, both 'entry' and '%0'/'%1' above are unstable names. You might want to replace both with wildcards. THOUGH, I see other parts of this test have the param names, so IDK? Either way, 'entry' isn't necessarily a stable label.

Updated the tests

erichkeane accepted this revision.Mar 24 2022, 7:34 AM
This revision is now accepted and ready to land.Mar 24 2022, 7:34 AM

Other than this 1 thing, LGTM. I DO find myself wondering how much of the CXXMethodDecl bit in the branch above is valid for 'naked' as well. If it ISN'T we might consider moving this loop AND that to EmitFunctionPrologue.

There's a different kind of bug there. MSVC disallows using the naked attribute on a member function. Clang doesn't enforce this: https://godbolt.org/z/6jKGbzYTq GCC has no such restriction, but GCC doesn't have many diagnostics in this area anyway. We may want to reconsider whether allowing the naked attribute on a non static member function is a good idea. We probably don't want to allow it in MS compatibility mode at the very least. But I think this can be a change for another day. WDYT?

Ah! I didn't know the bit about MSVC! I think that makes it even more interesting. From what I can tell however, GCC actually DOES 'do the right thing' with member functions, though the motivation for them is somewhat suspicious. I definitely agree we don't want to allow it in MS Compat mode (and agree that is a different patch). As for in GCC mode, a part of me wants to just DO it and see what the fallout is. The only value I can see for these as non-static member functions is in a template (or perhaps as a virtual function? *shudders*), and I don't see much overlap in the usage there.

I don't have very strong opinions on whether we restrict in non-Microsoft mode, but from what I can tell, the Itanium ABI does do some interesting things for some member functions (having to do with virtual table tables, also having to do with targets that mandate returning this, but otherwise don't seem to do any interesting things in the prolog. So it may be sensible to restrict some situations while still allowing it on member functions. I think for the moment, I'll just restrict on Microsoft mode under the assumption that Itanium is "okay enough" for the moment. But I'll do that in a follow-up patch.

clang/test/CodeGen/attr-naked.c
31

Ah, good to know -- I'll correct that.

aaron.ballman closed this revision.Mar 24 2022, 7:40 AM

Thanks for the reviews! I've commit in 488c772920566354075f7933eedbe4358c128bd2.