From f274b26c38c2610f27c931c6ba9afb0569b212cb Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Wed, 21 Jun 2023 14:52:07 -0700 Subject: [PATCH] Emit an error instead of crashing on invalid use of __capability 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. --- clang/lib/Sema/SemaType.cpp | 4 +++- clang/test/CodeGen/cheri/riscv/non-cheri-march.c | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGen/cheri/riscv/non-cheri-march.c diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 1fb0078371c9..f9412452382a 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -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()); } diff --git a/clang/test/CodeGen/cheri/riscv/non-cheri-march.c b/clang/test/CodeGen/cheri/riscv/non-cheri-march.c new file mode 100644 index 000000000000..0e3edabd4907 --- /dev/null +++ b/clang/test/CodeGen/cheri/riscv/non-cheri-march.c @@ -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=}}