Skip to content

Commit

Permalink
Emit an error instead of crashing on invalid use of __capability
Browse files Browse the repository at this point in the history
Previously, attempting to generate code that contained __capability while
using a target without support for CHERI would assert with
`Assertion `llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"' failed.`
Fix this by emitting a more user-friendly error message instead of crashing
when we encounter __capability but CHERI is not supported.
Ideally we would not expose the keyword for non-CHERI but that is a
rather more invasive change.
  • Loading branch information
arichardson committed Jul 5, 2023
1 parent 6812917 commit 7092f79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,14 @@ void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
void Parser::ParseCapabilityQualifier(ParsedAttributes &Attrs) {
IdentifierInfo *AttrName = Tok.getIdentifierInfo();
SourceLocation AttrNameLoc = Tok.getLocation();
Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
ParsedAttr::AS_Keyword);
// Report an error if the target does not support CHERI.
// TODO: we should not treat __capability as a keyword for non-CHERI.
// See https://github.com/CTSRD-CHERI/llvm-project/issues/706.
if (!getTargetInfo().SupportsCapabilities())
Diag(AttrNameLoc, diag::err_attribute_unsupported) << AttrName << "CHERI";
else
Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
ParsedAttr::AS_Keyword);
}

static bool VersionNumberSeparator(const char Separator) {
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8333,6 +8333,7 @@ static void HandleCHERICapabilityAttr(QualType &CurType, TypeProcessingState &st
llvm_unreachable("Unknown type attribute location");
}

assert(S.Context.getTargetInfo().SupportsCapabilities());
CurType = S.BuildPointerInterpretationAttr(CurType, PIK_Capability,
attr.getLoc());
}
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Parser/capability-attribute-non-cheri.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
/// Using __capability without the appropriate -march string previously
/// crashed clang during codegen instead of emitting an error earlier.
// RUN: %clang_cc1 -triple riscv64 -fsyntax-only %s -verify=no-cheri
// RUN: %clang_cc1 -triple riscv64 -target-feature +xcheri -fsyntax-only %s -verify=cheri
// cheri-no-diagnostics

void *__capability foo; // no-cheri-error{{'__capability' attribute is not supported on targets missing CHERI; specify an appropriate -march= or -mcpu=}}

0 comments on commit 7092f79

Please sign in to comment.