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.
  • Loading branch information
arichardson committed Jun 21, 2023
1 parent 6812917 commit f274b26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8332,7 +8332,9 @@ static void HandleCHERICapabilityAttr(QualType &CurType, TypeProcessingState &st
default:
llvm_unreachable("Unknown type attribute location");
}

// Report an error if the target does not support CHERI.
if (!S.Context.getTargetInfo().SupportsCapabilities())
S.Diag(attr.getLoc(), diag::err_attribute_unsupported) << Name << "CHERI";
CurType = S.BuildPointerInterpretationAttr(CurType, PIK_Capability,
attr.getLoc());
}
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CodeGen/cheri/riscv/non-cheri-march.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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
// RUN: %clang_cc1 -triple riscv64 -o - -emit-llvm %s -verify=no-cheri
// RUN: %clang_cc1 -triple riscv64 -target-feature +xcheri -o - -emit-llvm %s | FileCheck %s

// CHECK-LABEL: @test(
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i8 addrspace(200)* null
//
void *__capability test(void) { return 0; } // no-cheri-error{{__capability attribute is not supported on targets missing CHERI; specify an appropriate -march= or -mcpu=}}

0 comments on commit f274b26

Please sign in to comment.