From 9e2180a7258ba0762a16aacbfd6fcfb928a9987e Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Thu, 6 Jul 2023 16:41:30 -0700 Subject: [PATCH] Extend the __capability qualifier tests for typeof() and decltype() This exposed a new crash: https://github.com/CTSRD-CHERI/llvm-project/issues/710 --- .../cheri-capability-qualifier-declspec.c | 27 +++++++++++++++++-- .../cheri-capability-qualifier-declspec.cpp | 9 +++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/clang/test/Sema/cheri/cheri-capability-qualifier-declspec.c b/clang/test/Sema/cheri/cheri-capability-qualifier-declspec.c index 13c7debd54a2..2c86c6c4bd60 100644 --- a/clang/test/Sema/cheri/cheri-capability-qualifier-declspec.c +++ b/clang/test/Sema/cheri/cheri-capability-qualifier-declspec.c @@ -1,7 +1,30 @@ -// RUN: %cheri_cc1 -o - %s -fsyntax-only -verify +// RUN: %cheri_cc1 %s -fsyntax-only -verify // expected-no-diagnostics -void * __capability test_typeof(void *p) { +void *__capability test_typeof(void *p) { __typeof__(p) __capability c = (__cheri_tocap __typeof__(p) __capability)p; + _Static_assert(__builtin_types_compatible_p(typeof(c), void *__capability), ""); + __capability __typeof__(p) c2 = (__cheri_tocap __capability __typeof__(p))p; + _Static_assert(__builtin_types_compatible_p(typeof(c2), void *__capability), ""); + __capability __typeof__(p) *__capability c3 = (__cheri_tocap __capability __typeof__(p) *__capability)p; + _Static_assert(__builtin_types_compatible_p(typeof(c3), void *__capability *__capability), ""); return c; } + +static int *global_intptr = 0; +static __capability typeof(global_intptr) global_intcap = 0; +_Static_assert(__builtin_types_compatible_p(typeof(global_intptr), int *), ""); +_Static_assert(__builtin_types_compatible_p(typeof(global_intcap), int *__capability), ""); + +/// Crash reproducer for https://github.com/CTSRD-CHERI/llvm-project/issues/710 +// RUN: not --crash %cheri_cc1 %s -fsyntax-only -verify -DCRASH +// REQUIRES: asserts +#ifdef CRASH +struct s { + int i; +}; +static struct s *global_sptr = 0; +static __capability typeof(global_sptr) global_scap = 0; +_Static_assert(__builtin_types_compatible_p(typeof(global_sptr), struct s *), ""); +_Static_assert(__builtin_types_compatible_p(typeof(global_scap), struct s *__capability), ""); +#endif diff --git a/clang/test/SemaCXX/cheri/cheri-capability-qualifier-declspec.cpp b/clang/test/SemaCXX/cheri/cheri-capability-qualifier-declspec.cpp index 7c2747f5cf63..a283d4b00dc6 100644 --- a/clang/test/SemaCXX/cheri/cheri-capability-qualifier-declspec.cpp +++ b/clang/test/SemaCXX/cheri/cheri-capability-qualifier-declspec.cpp @@ -3,5 +3,14 @@ void * __capability test_decltype(void *p) { decltype(p) __capability c = (__cheri_tocap decltype(p) __capability)p; + static_assert(__is_same(typeof(c) *, void *__capability *), ""); + static_assert(__is_same(decltype(c) &__capability, void *__capability &__capability), ""); + void *__capability &cref = c; + static_assert(__is_same(decltype(cref) __capability, void *__capability &__capability), ""); + static_assert(__is_same(__capability decltype(cref), void *__capability &__capability), ""); + + using intref = int &; + static_assert(__is_same(intref __capability, int &__capability), ""); + static_assert(__is_same(__capability intref, int &__capability), ""); return c; }