Skip to content

Commit 6053c45

Browse files
committed
[CHERIoT] Fixes to cheriot_cap_import attribute
In particular, fix the mangling scheme of permissions ("" -> "-" when permission not present) and, when checking for volatile qualifiers, reach to pointee if type is pointer
1 parent 1c10399 commit 6053c45

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,10 @@ static void handleCHERIOTMMIODevice(Sema &S, Decl *D, const ParsedAttr &Attr,
21172117
}
21182118

21192119
QualType QT = VD->getType();
2120-
if (!QT.isVolatileQualified()) {
2120+
auto *PQT = dyn_cast<PointerType>(QT);
2121+
auto IsVolatile = (PQT && PQT->getPointeeType().isVolatileQualified()) ||
2122+
QT.isVolatileQualified();
2123+
if (!IsVolatile) {
21212124
S.Diag(Attr.getLoc(), diag::warn_cheriot_global_cap_import_non_volatile)
21222125
<< VD->getName() << Attr.getAttrName();
21232126
}

clang/test/CodeGen/cheri/riscv/cheriot-cap-import-attr.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ void func() {
176176

177177
// CHECK: attributes #0 = { "cheriot_global_cap_import"="mem,uart,RWcm" }
178178
// CHECK: attributes #1 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,RWcm" }
179-
// CHECK: attributes #2 = { "cheriot_global_cap_import"="mem,uart,R" }
180-
// CHECK: attributes #3 = { "cheriot_global_cap_import"="mem,uart,Rc" }
181-
// CHECK: attributes #4 = { "cheriot_global_cap_import"="mem,uart,Rcm" }
182-
// CHECK: attributes #5 = { "cheriot_global_cap_import"="mem,uart,W" }
183-
// CHECK: attributes #6 = { "cheriot_global_cap_import"="mem,uart,Wc" }
184-
// CHECK: attributes #7 = { "cheriot_global_cap_import"="mem,uart,RWc" }
185-
// CHECK: attributes #8 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,R" }
186-
// CHECK: attributes #9 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,Rc" }
187-
// CHECK: attributes #10 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,Rcm" }
188-
// CHECK: attributes #11 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,W" }
189-
// CHECK: attributes #12 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,Wc" }
190-
// CHECK: attributes #13 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,RWc" }
179+
// CHECK: attributes #2 = { "cheriot_global_cap_import"="mem,uart,R---" }
180+
// CHECK: attributes #3 = { "cheriot_global_cap_import"="mem,uart,R-c-" }
181+
// CHECK: attributes #4 = { "cheriot_global_cap_import"="mem,uart,R-cm" }
182+
// CHECK: attributes #5 = { "cheriot_global_cap_import"="mem,uart,-W--" }
183+
// CHECK: attributes #6 = { "cheriot_global_cap_import"="mem,uart,-Wc-" }
184+
// CHECK: attributes #7 = { "cheriot_global_cap_import"="mem,uart,RWc-" }
185+
// CHECK: attributes #8 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,R---" }
186+
// CHECK: attributes #9 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,R-c-" }
187+
// CHECK: attributes #10 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,R-cm" }
188+
// CHECK: attributes #11 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,-W--" }
189+
// CHECK: attributes #12 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,-Wc-" }
190+
// CHECK: attributes #13 = { "cheriot_global_cap_import"="cheriot_shared_object,SO,RWc-" }

clang/test/Sema/cheri/cheriot-cap-import-attr-ill-formed.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ __attribute__((cheriot_mmio("uart", "m"))) extern volatile struct Uart uart2; //
77
__attribute__((cheriot_mmio("uart", "R"))) volatile struct Uart uart3; // no warnings or errors, extern is implied
88
__attribute__((cheriot_mmio("uart"))) volatile struct Uart uart4 = {}; // expected-error{{global variable definition 'uart4' with attribute 'cheriot_mmio' cannot have an initializer}}
99
__attribute__((cheriot_mmio("uart"))) volatile struct {int k;} uart5 = {10}; // expected-error{{global variable definition 'uart5' with attribute 'cheriot_mmio' cannot have an initializer}}
10+
__attribute__((cheriot_mmio("uart", "R"))) extern volatile struct Uart *uart6; /* no warnings or errors*/
1011
__attribute__((cheriot_shared_object("exampleK", "RR"))) extern int exampleK; // expected-warning{{the permissions in 'cheriot_shared_object' contain a duplicate permission symbol: 'R' (value: 'RR')}}
1112
__attribute__((cheriot_shared_object("exampleK", "m"))) extern int exampleK; // expected-error{{the permissions in 'cheriot_shared_object' contain ill-formed dependencies: does not contain either read (R) or write (W) (value: 'm')}}
1213
__attribute__((cheriot_shared_object("exampleK", "Wm"))) extern int exampleK; // expected-error{{the permissions in 'cheriot_shared_object' contain ill-formed dependencies: contains mut (m) but does not have both read (R) and cap (c) (value: 'Wm')}}
1314
__attribute__((cheriot_shared_object("exampleK", "R"))) int exampleK; // no warnings or errors, extern is implied
1415
__attribute__((cheriot_shared_object("exampleK", "R"))) int exampleK2 = 10; // expected-error{{global variable definition 'exampleK2' with attribute 'cheriot_shared_object' cannot have an initializer}}
16+
__attribute__((cheriot_shared_object("exampleK", "R"))) int *exampleK3; /* no warnings or errors */

llvm/include/llvm/IR/Attributes.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/BitmaskEnum.h"
2121
#include "llvm/ADT/StringRef.h"
22+
#include "llvm/ADT/Twine.h"
2223
#include "llvm/Config/llvm-config.h"
2324
#include "llvm/Support/Alignment.h"
2425
#include "llvm/Support/CodeGen.h"
@@ -1498,10 +1499,11 @@ class CHERIoTGlobalCapabilityImportAttr {
14981499
return false;
14991500
}
15001501

1501-
Permissions = (HasRead ? std::string(ReadPermissionSymbol) : "") +
1502-
(HasWrite ? std::string(WritePermissionSymbol) : "") +
1503-
(HasCap ? std::string(CapPermissionSymbol) : "") +
1504-
(HasMut ? std::string(MutPermissionSymbol) : "");
1502+
Permissions = ((HasRead ? Twine(ReadPermissionSymbol) : "-") +
1503+
(HasWrite ? Twine(WritePermissionSymbol) : "-") +
1504+
(HasCap ? Twine(CapPermissionSymbol) : "-") +
1505+
(HasMut ? Twine(MutPermissionSymbol) : "-"))
1506+
.str();
15051507
return true;
15061508
}
15071509

0 commit comments

Comments
 (0)