Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit an error instead of crashing on invalid use of __capability #704

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def err_expected_selector_for_method : Error<
"expected selector for Objective-C method">;
def err_expected_property_name : Error<"expected property name">;

def warn_cheri_capability_attribute_location : Warning<
def warn_cheri_capability_qualifier_location : Warning<
"use of __capability before the pointer type is deprecated">,
InGroup<DeprecatedDeclarations>;

Expand Down
7 changes: 5 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1298,9 +1298,12 @@ def note_cheri_func_noproto_explanation : Note<
"values from the argument registers.">;
def note_cheri_func_decl_add_types : Note<
"candidate function declaration needs parameter types">;
def err_cheri_capability_attribute_ambiguous : Error<
def err_cheri_capability_qualifier_not_supported : Error<
"use of __capability is not supported without CHERI; "
"specify an appropriate -march= or -mcpu=">;
def err_cheri_capability_qualifier_ambiguous : Error<
"use of __capability is ambiguous">;
def err_cheri_capability_attribute_pointers_only : Error<
def err_cheri_capability_qualifier_pointers_only : Error<
"__capability only applies to pointers; type here is %0">;

def err_objc_var_decl_inclass :
Expand Down
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_cheri_capability_qualifier_not_supported);
else
Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
ParsedAttr::AS_Keyword);
}

static bool VersionNumberSeparator(const char Separator) {
Expand Down
22 changes: 12 additions & 10 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6372,8 +6372,7 @@ fillDependentPointerTypeLoc(DependentPointerTypeLoc DPTL,
}
}

llvm_unreachable(
"no cheri_capability attribute found at the expected location!");
llvm_unreachable("no __capability qualifier found at the expected location!");
}

static void fillMatrixTypeLoc(MatrixTypeLoc MTL,
Expand Down Expand Up @@ -8213,18 +8212,19 @@ QualType Sema::BuildPointerInterpretationAttr(QualType T,
} else if (T->isDependentType()) {
T = Context.getDependentPointerType(T, PIK, QualifierLoc);
} else {
Diag(QualifierLoc, diag::err_cheri_capability_attribute_pointers_only)
<< T;
Diag(QualifierLoc, diag::err_cheri_capability_qualifier_pointers_only) << T;
}

return T;
}

/// HandleCHERICapabilityAttr - Process the cheri_capability attribute. It is
/// HandleCHERICapabilityQualifier - Process the __capability qualifier. It is
/// only applicable to pointer and reference types and specifies that this
/// pointer/reference should be treated as a capability.
static void HandleCHERICapabilityAttr(QualType &CurType, TypeProcessingState &state,
TypeAttrLocation TAL, ParsedAttr& attr) {
static void HandleCHERICapabilityQualifier(QualType &CurType,
TypeProcessingState &state,
TypeAttrLocation TAL,
ParsedAttr &attr) {
Declarator &declarator = state.getDeclarator();
Sema& S = state.getSema();
std::string Name = attr.getAttrName()->getName().str();
Expand Down Expand Up @@ -8256,14 +8256,15 @@ static void HandleCHERICapabilityAttr(QualType &CurType, TypeProcessingState &st
if (nextChunk.Kind == DeclaratorChunk::Pointer) {
auto Attr = nextChunk.getAttrs();
if (!Attr.hasAttribute(ParsedAttr::AT_CHERICapability)) {
S.Diag(nextChunk.Loc, diag::err_cheri_capability_attribute_ambiguous);
S.Diag(nextChunk.Loc,
diag::err_cheri_capability_qualifier_ambiguous);
return;
}
}
}

// Output a deprecated usage warning with a FixItHint
S.Diag(chunk.Loc, diag::warn_cheri_capability_attribute_location)
S.Diag(chunk.Loc, diag::warn_cheri_capability_qualifier_location)
<< FixItHint::CreateRemoval(attr.getRange())
<< FixItHint::CreateInsertion(chunk.Loc.getLocWithOffset(1),
" " + Name + " ");
Expand Down Expand Up @@ -8333,6 +8334,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 Expand Up @@ -8645,7 +8647,7 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,

case ParsedAttr::AT_CHERICapability:
attr.setUsedAsTypeAttr();
HandleCHERICapabilityAttr(type, state, TAL, attr);
HandleCHERICapabilityQualifier(type, state, TAL, attr);
break;
case ParsedAttr::AT_CHERINoSubobjectBounds:
attr.setUsedAsTypeAttr();
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Parser/capability-qualifier-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{{use of __capability is not supported without CHERI; specify an appropriate -march= or -mcpu=}}
28 changes: 0 additions & 28 deletions clang/test/Sema/cheri/cheri-capability-attr-invalid-type.c

This file was deleted.

14 changes: 14 additions & 0 deletions clang/test/Sema/cheri/cheri-capability-qualifier-typo-correction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %cheri_cc1 -target-abi n64 -fsyntax-only %s -verify
/// Test case for https://github.com/CTSRD-CHERI/clang/issues/157
/// Applying __capability qualifier to a typedef as part of typo correction used to crash.

#define a(b) \
__extension__({ \
typedef __typeof__(b) c; \
__capability c d; \
})
int e() {
int *__capability pipv = a(pip); // expected-error{{use of undeclared identifier 'pip'; did you mean 'pipv'?}}
// expected-error@-1{{initializing 'int * __capability' with an expression of incompatible type 'void'}}
// expected-note@-2{{'pipv' declared here}}
}