This is an archive of the discontinued LLVM Phabricator instance.

Fix crash when using __attribute__((section (".bss.name"))) incorrectly
AbandonedPublic

Authored by kuhar on Jul 15 2015, 8:12 AM.

Details

Reviewers
None
Summary

Previously, using attribute((section("bss.name")) incorrectly caused clang to crash displaying error from backed. This patch makes clang's frontend produce clear diagnostic/error.

Two main cases that are handled is using the attribute with functions and with variables initialized to non-zero values.
__attribute__((section(".bss.name"))) void func() { }
__attribute__((section(".bss.name"))) int a = 3;

Diff Detail

Repository
rL LLVM

Event Timeline

kuhar updated this revision to Diff 29781.Jul 15 2015, 8:12 AM
kuhar retitled this revision from to Fix crash when using __attribute__((section (".bss.name"))) incorrectly.
kuhar updated this object.
kuhar set the repository for this revision to rL LLVM.
kuhar added a subscriber: cfe-commits.
rsmith added a subscriber: rsmith.Jul 15 2015, 1:56 PM

High-level comments:

  1. This is wrong for C++. A non-zero initializer there is not a problem unless we are required to perform constant initialization for the variable. In any other case, we should emit the variable to the specified section and emit a dynamic initializer for it. (We'll need to check that globalopt doesn't optimize away that initialization and give the variable a non-zero initializer.)
  2. It seems unnecessarily kind to allow any initializer at all on these variables. If we want to reject these cases, why not just reject any case where the variable has a specified initializer? (I think it's less reasonable to do this in C++, but whatever we do, making the constant evaluator smarter should not result in us rejecting more code.)
  3. Sema is not in a position to decide for itself which constants / types will correspond to all-zero-bits (and for instance the MS ABI and Itanium ABI will disagree on whether some member pointer types are all-zero-bits). It should ask the CXXABI object to determine that on its behalf.
  4. We should not be hard-coding knowledge of a target-specific ".bss" section into Sema. We should determine if the section has known properties by asking the target.
include/clang/Basic/DiagnosticSemaKinds.td
2180–2183

No need for \ before ' here.

kuhar updated this revision to Diff 29896.Jul 16 2015, 6:38 AM
kuhar added a subscriber: aaron.ballman.

Thanks for review.
Aaron - fixed things you pointed out, except not dropping attribute - I'm not entirely convinced, because attributes are being dropped on error in the entire file...

Richard:
ad. 1. This should be fixed now - I only report error when variables are initialized with some 'constant expression', so that dynamic initializers are allowed.
ad. 2.

I think it's less reasonable to do this in C++>

Could you explain why?
ad. 3. I didn't know what to do with member pointers, so I left them unchecked. Is there a better way to determine non-zeroness of variable? Or maybe I should add it somewhere, so it can be target-specific (CXXABI.h?)?
ad. 4. Are there some existing mechanisms to do so, or could explain how it should be done? I've only found a function for validating section names (isValidSectionSpecifier in TargetInfo.h).
I would really appreciate your further help on this.

joerg added a subscriber: joerg.Jul 18 2015, 4:08 AM

Why should this be in the frontend? Arguable, writing assembler with this is not wrong and if anything, the diagnostic should really be in the MC layer. I don't think precision of diagnostic is really that high a priority here, which would be the normal concern with errors from the MC layer. I don't think this is different from declaring multiple symbols of the same name etc.

kuhar added a comment.Jul 20 2015, 2:56 AM

Why should this be in the frontend? Arguable, writing assembler with this is not wrong and if anything, the diagnostic should really be in the MC layer. I don't think precision of diagnostic is really that high a priority here, which would be the normal concern with errors from the MC layer. I don't think this is different from declaring multiple symbols of the same name etc.

I'm a Clang newbie and I'm not really sure which approach is the best.
Richard, what do you think about this? I would really appreciate your opinion and some help.

kuhar added a subscriber: kuhar.Jul 28 2015, 2:47 AM

ping! Richard, I'm not sure what to do here. Do you have any input?

kuhar abandoned this revision.Sep 4 2015, 11:34 AM