-
Notifications
You must be signed in to change notification settings - Fork 45
/
cheri.hh
1371 lines (1265 loc) · 33.8 KB
/
cheri.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Microsoft and CHERIoT Contributors.
// SPDX-License-Identifier: MIT
#pragma once
/**
* C++ helpers for operating on capabilities.
*/
#include <cheri.h>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <magic_enum/magic_enum.hpp>
namespace CHERI
{
/**
* The complete set of architectural permissions.
*/
enum class Permission : uint32_t
{
/**
* Capability refers to global memory (this capability may be stored
* anywhere).
*/
Global = 0,
/**
* Global capabilities can be loaded through this capability. Without
* this permission, any capability loaded via this capability will
* have `Global` and `LoadGlobal` removed.
*/
LoadGlobal = 1,
/**
* Capability may be used to store. Any store via a capability without
* this permission will trap.
*/
Store = 2,
/**
* Capabilities with store permission may be loaded through this
* capability. Without this, any loaded capability will have
* `LoadMutable` and `Store` removed.
*/
LoadMutable = 3,
/**
* This capability may be used to store capabilities that do not have
* `Global` permission.
*/
StoreLocal = 4,
/**
* This capability can be used to load.
*/
Load = 5,
/**
* Any load and store permissions on this capability convey the right to
* load or store capabilities in addition to data.
*/
LoadStoreCapability = 6,
/**
* If installed as the program counter capability, running code may
* access privileged system registers.
*/
AccessSystemRegisters = 7,
/**
* This capability may be used as a jump target and used to execute
* instructions.
*/
Execute = 8,
/**
* This capability may be used to unseal other capabilities. The
* 'address' range is in the sealing type namespace and not in the
* memory namespace.
*/
Unseal = 9,
/**
* This capability may be used to seal other capabilities. The
* 'address' range is in the sealing type namespace and not in the
* memory namespace.
*/
Seal = 10,
/**
* Software defined permission bit, no architectural meaning.
*/
User0 = 11
};
/**
* Class encapsulating a set of permissions.
*/
class PermissionSet
{
/**
* Helper that returns the bit associated with a given permission.
*/
static constexpr uint32_t permission_bit(Permission p)
{
return 1 << static_cast<uint32_t>(p);
}
/**
* Helper for building permissions, adds a permission to the raw
* bitfield.
*/
__always_inline constexpr void add_permission(Permission p)
{
rawPermissions |= permission_bit(p);
}
/**
* Private constructor for creating a permission set from a raw bitmask.
* This should never be used accidentally and so is hidden behind a
* factory method with an explicit name. Callers should use
* `PermissionSet::from_raw`.
*/
constexpr PermissionSet(uint32_t rawPermissions)
: rawPermissions(rawPermissions)
{
}
public:
/**
* Computes (at compile time) a bitmask containing the set of valid
* permission bits.
*
* FIXME would ideally make this private and expose a public static
* constexpr field but this seems to trigger a compiler bug when trying
* to initialise said field using this function.
*/
static constexpr uint32_t valid_permissions_mask()
{
uint32_t mask = 0;
for (auto permission : magic_enum::enum_values<Permission>())
{
mask |= 1 << static_cast<uint32_t>(permission);
}
return mask;
}
private:
/**
* Permissions iterator. Stores the permissions and iterates over them
* one bit at a time.
*/
class Iterator
{
/// `PermissionSet` may construct this.
friend class PermissionSet;
/// The raw permissions bitmap.
uint32_t permissions;
/// Constructor, take a raw permissions bitmap.
constexpr Iterator(uint32_t rawPermissions)
: permissions(rawPermissions)
{
}
public:
/**
* Dereference, returns the lowest-numbered permission.
*/
constexpr Permission operator*()
{
return Permission(__builtin_ffs(permissions) - 1);
}
/**
* Preincrement, drops the lowest-numbered permission.
*/
constexpr Iterator &operator++()
{
permissions &= ~(1 << (__builtin_ffs(permissions) - 1));
return *this;
}
/**
* Returns true if the other iterator has a different set of
* permissions.
*/
constexpr bool operator!=(const Iterator Other)
{
return permissions != Other.permissions;
}
};
public:
/**
* The raw bitmap of permissions. This is public so that this class
* meets the requirements of a structural type and can therefore be
* used as a template parameter. This field should never be directly
* modified.
*/
uint32_t rawPermissions = 0;
/**
* Constructs a permission set from a raw permission mask.
*/
static constexpr PermissionSet from_raw(uint32_t raw)
{
raw &= valid_permissions_mask();
return {raw};
}
/**
* Constructs a permission set from a single permission.
*/
constexpr PermissionSet(Permission p)
{
add_permission(p);
}
/**
* Construct a permission set from a list of permissions.
*/
__always_inline constexpr PermissionSet(
std::initializer_list<Permission> permissions)
{
for (auto p : permissions)
{
add_permission(p);
}
}
/**
* Copy constructor.
*/
constexpr PermissionSet(const PermissionSet &other)
= default;
/**
* Returns a permission set representing all permissions.
*/
constexpr static PermissionSet omnipotent()
{
return PermissionSet{valid_permissions_mask()};
}
/**
* And-permissions operation, creates a new permission set containing
* only permissions present in both this set and the argument.
*/
constexpr PermissionSet operator&(PermissionSet p)
{
return PermissionSet{rawPermissions & p.rawPermissions};
}
/**
* And-permissions operation, removes all permissions that are not
* present in both permission sets.
*/
constexpr PermissionSet &operator&=(PermissionSet p)
{
rawPermissions &= p.rawPermissions;
return *this;
}
/**
* Constructs a new permission set without the specified permission.
*/
[[nodiscard]] constexpr PermissionSet without(Permission p) const
{
return {rawPermissions & ~permission_bit(p)};
}
/**
* Constructs a new permission set without the specified permissions.
*/
template<std::same_as<Permission>... Permissions>
[[nodiscard]] constexpr PermissionSet without(Permission p,
Permissions... ps) const
{
return this->without(p).without(ps...);
}
/**
* Returns true if, and only if, this permission set can be derived
* from the argument set.
*/
[[nodiscard]] constexpr bool can_derive_from(PermissionSet other) const
{
return (rawPermissions & other.rawPermissions) == rawPermissions;
}
/**
* Returns true if this permission set contains the specified
* permission.
*/
[[nodiscard]] constexpr bool contains(Permission permission) const
{
return (permission_bit(permission) & rawPermissions) ==
permission_bit(permission);
}
/**
* Returns true if this permission set contains the specified
* permissions.
*/
template<std::same_as<Permission>... Permissions>
[[nodiscard]] constexpr bool contains(Permission p,
Permissions... ps) const
{
return this->contains(p) && this->contains(ps...);
}
/**
* Returns the raw permission mask as an integer containing a bitfield
* of permissions.
*/
[[nodiscard]] constexpr uint32_t as_raw() const
{
return rawPermissions;
}
/**
* Returns an iterator over the permissions starting at the
* lowest-numbered permission.
*/
[[nodiscard]] constexpr Iterator begin() const
{
return {rawPermissions};
}
/**
* Returns an end iterator.
*/
[[nodiscard]] constexpr Iterator end() const
{
// Each increment of an iterator will drop one permission and so an
// iterator will compare equal to {0} once all permissions have
// been dropped.
return {0};
}
/**
* Three-way comparison. Treats a superset as greater-than, identical
* permissions as equivalent, and sets that don't have a superset
* releationship as unordered.
*/
constexpr auto operator<=>(const PermissionSet Other) const
{
if (rawPermissions == Other.rawPermissions)
{
return std::partial_ordering::equivalent;
}
if (can_derive_from(Other))
{
return std::partial_ordering::less;
}
if (Other.can_derive_from(*this))
{
return std::partial_ordering::greater;
}
return std::partial_ordering::unordered;
}
/**
* Equality operator, wraps the three-way compare operator.
*/
constexpr bool operator==(PermissionSet other) const
{
// Clang-tidy spuriously suggests that this 0 should be nullptr.
return (*this <=> other) == 0; // NOLINT(modernize-use-nullptr)
}
};
/**
* Helper class for accessing capability properties on pointers.
*/
template<typename T>
class Capability
{
protected:
/// The capability that this class wraps.
T *ptr;
private:
/**
* Constructs a PermissionSet with the permissions of the given pointer.
*/
static PermissionSet permission_set_from_pointer(const void *p)
{
auto perms = __builtin_cheri_perms_get(p);
auto mask = PermissionSet::valid_permissions_mask();
/* FIXME teach the compiler that the builtin always returns a value
* that is a subset of the mask, otherwise it unnecessarily
* constructs and applies the mask in from_raw */
__builtin_assume((perms & ~mask) == 0);
return PermissionSet::from_raw(perms);
}
/**
* Base class for the proxies that accessors in this class return.
*/
class PropertyProxyBase
{
/**
* The capability that this proxy refers to.
*/
Capability ∩
protected:
/**
* Replaces the underlying capability
*/
template<typename U>
void set(U *newPtr)
{
cap.ptr = static_cast<T *>(newPtr);
}
/**
* Returns the capability's pointer.
*/
[[nodiscard]] T *ptr() const
{
return cap.ptr;
}
public:
/// Constructor, takes the capability whose property this class is
/// proxying.
PropertyProxyBase(Capability &c) : cap(c) {}
};
/**
* Proxy for accessing a capability's address.
*/
struct AddressProxy : public PropertyProxyBase
{
/// Inherit the constructor from the base class.
using PropertyProxyBase::PropertyProxyBase;
/// Inherit the pointer accesors
/// @{
using PropertyProxyBase::ptr;
using PropertyProxyBase::set;
/// @}
/**
* Implicit casts can convert this to an address.
*/
operator ptraddr_t() const
{
return __builtin_cheri_address_get(ptr());
}
/**
* Set the address in the underlying capability.
*/
AddressProxy &operator=(ptraddr_t addr)
{
set(__builtin_cheri_address_set(ptr(), addr));
return *this;
}
/**
* Set the address in the underlying capability given another
* address proxy.
*/
AddressProxy &operator=(AddressProxy addr)
{
set(__builtin_cheri_address_set(ptr(), addr));
return *this;
}
/**
* Add a displacement to the capability's address.
*/
AddressProxy &operator+=(ptrdiff_t displacement)
{
set(__builtin_cheri_offset_increment(ptr(), displacement));
return *this;
}
/**
* Subtract a displacement from the capability's address.
*/
AddressProxy &operator-=(ptrdiff_t displacement)
{
set(__builtin_cheri_offset_increment(ptr(), -displacement));
return *this;
}
};
/**
* Proxy for accessing an object's bounds.
*/
struct BoundsProxy : public PropertyProxyBase
{
/// Inherit the constructor from the base class.
using PropertyProxyBase::PropertyProxyBase;
/// Inherit the pointer accesors
/// @{
using PropertyProxyBase::ptr;
using PropertyProxyBase::set;
/// @}
/**
* Return the object's bounds (displacement from the address to the
* end).
*/
operator ptrdiff_t() const
{
return __builtin_cheri_length_get(ptr()) -
(__builtin_cheri_address_get(ptr()) -
__builtin_cheri_base_get(ptr()));
}
/**
* Set the capability's bounds, giving an invalid capability if this
* cannot be represented exactly.
*/
BoundsProxy &operator=(size_t bounds)
{
set(__builtin_cheri_bounds_set_exact(ptr(), bounds));
return *this;
}
/**
* Set the bounds, adding some padding (up to the bounds of the
* original capability) if necessary for alignment.
*/
BoundsProxy &set_inexact(size_t bounds)
{
set(__builtin_cheri_bounds_set(ptr(), bounds));
return *this;
}
};
/**
* Proxy for accessing a capability's permissions
*/
struct PermissionsProxy : public PropertyProxyBase
{
/// Inherit the constructor from the base class.
using PropertyProxyBase::PropertyProxyBase;
/// Inherit the pointer accesors
/// @{
using PropertyProxyBase::ptr;
using PropertyProxyBase::set;
/// @}
/**
* Implicitly convert to a permission set.
*/
operator PermissionSet() const
{
return permission_set_from_pointer(ptr());
}
/**
* And-permissions operation, removes all permissions that are not
* present in both permission sets from the capability.
*/
PermissionsProxy &operator&=(PermissionSet permissions)
{
set(__builtin_cheri_perms_and(ptr(), permissions.as_raw()));
return *this;
}
/**
* Returns a permission set containing only the permissions held by
* the capability and the argument.
*/
constexpr PermissionSet operator&(PermissionSet p)
{
return static_cast<PermissionSet>(*this) & p;
}
/**
* Constructs a new permission set without the specified
* permissions.
*/
template<std::same_as<Permission>... Permissions>
constexpr PermissionSet without(Permissions... ps) const
{
return static_cast<PermissionSet>(*this).without(ps...);
}
/**
* Returns true if, and only if, this permission set can be derived
* from the argument set.
*/
[[nodiscard]] constexpr bool
can_derive_from(PermissionSet other) const
{
return static_cast<PermissionSet>(*this).can_derive_from(other);
}
/**
* Returns true if this permission set contains the specified
* permissions.
*/
template<std::same_as<Permission>... Permissions>
constexpr bool contains(Permissions... permissions) const
{
return static_cast<PermissionSet>(*this).contains(
permissions...);
}
/**
* Returns the raw permission mask as an integer containing a
* bitfield of permissions.
*/
[[nodiscard]] constexpr uint32_t as_raw() const
{
return static_cast<PermissionSet>(*this).as_raw();
}
/**
* Returns an iterator over the permissions starting at the
* lowest-numbered permission.
*/
auto begin()
{
return static_cast<PermissionSet>(*this).begin();
}
/**
* Returns an end iterator.
*/
auto end()
{
return static_cast<PermissionSet>(*this).end();
}
/**
* Comparison operator.
*/
constexpr std::partial_ordering
operator<=>(const PermissionSet Other) const
{
return static_cast<PermissionSet>(*this) <=> Other;
}
/**
* Equality operator, wraps the three-way compare operator.
*/
constexpr bool operator==(const PermissionSet Other) const
{
return (*this <=> Other) == 0;
}
};
/// The property proxy base is allowed to directly access the pointer
/// that this class wraps.
friend class PropertyProxyBase;
public:
/// Constructor from a null pointer.
constexpr Capability(std::nullptr_t) : ptr(nullptr) {}
/// Default constructor, initialises with a null pointer.
constexpr Capability() : ptr(nullptr) {}
/// Constructor, takes an existing pointer to wrap
constexpr Capability(T *p) : ptr(p) {}
/// Copy constructor, aliases the object that is pointed to by `ptr`.
constexpr Capability(const Capability &other) : ptr(other.ptr) {}
/// Move constructor.
constexpr Capability(Capability &&other) : ptr(other.ptr)
{
other.ptr = nullptr;
}
/**
* Replace the pointer that this capability wraps with another.
*/
Capability &operator=(const Capability &other)
{
ptr = other.ptr;
return *this;
}
/**
* Transfer the pointer that this capability wraps from .
*/
Capability &operator=(Capability &&other)
{
ptr = other.ptr;
other.ptr = nullptr;
return *this;
}
/**
* Access the address of the capability.
*/
AddressProxy address() [[clang::lifetimebound]]
{
return {*this};
}
/**
* Return the address as an integer from a `const` capability.
*/
[[nodiscard]] ptraddr_t address() const
{
return __builtin_cheri_address_get(ptr);
}
/**
* Access (read, set) the capability's bounds.
*/
BoundsProxy bounds() [[clang::lifetimebound]]
{
return {*this};
}
/**
* Return the bounds as an integer.
*/
[[nodiscard]] ptrdiff_t bounds() const
{
return __builtin_cheri_length_get(ptr) -
(__builtin_cheri_address_get(ptr()) -
__builtin_cheri_base_get(ptr()));
}
/**
* Access the permissions of this capability.
*/
PermissionsProxy permissions() [[clang::lifetimebound]]
{
return {*this};
}
/**
* Get a copy of the permissions from a `const` capability.
*/
[[nodiscard]] PermissionSet permissions() const
{
return permission_set_from_pointer(ptr);
}
/**
* Pointer subtraction.
*/
Capability operator-(ptrdiff_t diff)
{
return {ptr - diff};
}
/**
* Pointer subtraction.
*/
Capability &operator-=(ptrdiff_t diff)
{
ptr -= diff;
return *this;
}
/**
* Pointer addition.
*/
Capability operator+(ptrdiff_t diff)
{
return {ptr + diff};
}
/**
* Pointer addition.
*/
Capability &operator+=(ptrdiff_t diff)
{
ptr += diff;
return *this;
}
/**
* Returns the tag bit indicating whether this is a valid capability.
*/
[[nodiscard]] bool is_valid() const
{
// The clang static analyser doesn't yet know that null is untagged
// and so warns of possible null dereferences after this method
// returns true. Explicitly assume that a tagged thing is non-null
// to fix this.
if (__builtin_cheri_tag_get(ptr))
{
__builtin_assume(ptr != nullptr);
return true;
}
return false;
}
/**
* Return whether this is a sealed capability.
*/
[[nodiscard]] bool is_sealed() const
{
return __builtin_cheri_type_get(ptr) != 0;
}
/**
* Returns the type of this capability, 0 if this is not a sealed
* capability.
*/
[[nodiscard]] uint32_t type() const
{
return __builtin_cheri_type_get(ptr);
}
/**
* Returns the base address of this capability.
*/
[[nodiscard]] ptraddr_t base() const
{
return __builtin_cheri_base_get(ptr);
}
/**
* Returns the length of this capability.
*/
[[nodiscard]] size_t length() const
{
return __builtin_cheri_length_get(ptr);
}
/**
* Returns the address of the top of this capability.
*/
[[nodiscard]] ptraddr_t top() const
{
return base() + length();
}
/**
* Capability comparison. Defines ordered comparison for capabilities
* with the same bounds and permissions. All other capabilities are
* either equivalent (identical bit pattern, including the tag bit) or
* unordered.
*/
constexpr std::partial_ordering operator<=>(T *other) const
{
return (*this <=> Capability<T>{other}) == 0;
}
/**
* Comparison against null pointer.
*
* Returns equivalent if this is a canonical null pointer, returns
* unordered for any other (tagged or untagged) value. Callers may
* often want `is_valid` instead of this.
*/
constexpr std::partial_ordering operator<=>(std::nullptr_t) const
{
if (__builtin_cheri_equal_exact(ptr, nullptr))
{
return std::partial_ordering::equivalent;
}
return std::partial_ordering::unordered;
}
constexpr bool operator==(const Capability Other) const
{
return __builtin_cheri_equal_exact(ptr, Other.ptr);
}
/**
* Capability comparison. Defines ordered comparison for capabilities
* with the same bounds and permissions. All other capabilities are
* either equivalent (identical bit pattern, including the tag bit) or
* unordered.
*
* Callers may want to compare addresses, rather than capabilities, if
* they want a defined comparison that is stable between two objects.
*/
constexpr std::partial_ordering
operator<=>(const Capability Other) const
{
if (__builtin_cheri_equal_exact(ptr, Other.ptr))
{
return std::partial_ordering::equivalent;
}
// If neither capability is sealed, check if everything except the
// address is the same and define ordered comparison on pointers to
// the same object.
if (!(is_sealed() || Other.is_sealed()) &&
__builtin_cheri_equal_exact(__builtin_address_set(
ptr, __builtin_address_get(Other), Other)))
{
return static_cast<ptraddr_t>(ptr) <=>
static_cast<ptraddr_t>(Other);
}
// Comparison of pointers to different objects is ub, you probably
// want address comparison:
return std::partial_ordering::unordered;
}
/**
* Equality operator, wraps the three-way compare operator.
*/
constexpr bool operator==(std::nullptr_t) const
{
return (*this <=> nullptr) == 0;
}
/**
* Implicit cast to the raw pointer type.
*/
template<typename U = T>
requires(!std::same_as<U, void>) operator U *()
{
return ptr;
}
/**
* Implicit cast to a raw pointer type.
*/
operator void *()
{
return ptr;
}
/**
* Access fields of the target as if this were a raw pointer.
*/
T *operator->()
{
return ptr;
}
/**
* Explicitly get the raw pointer.
*/
T *get()
{
return ptr;
}
/**
* Dereference operator.
*/
template<typename U = T>
requires(!std::same_as<U, void>) U &operator*()
{
return *ptr;
}
/**
* Cast this capability to some other type.
*/
template<typename U>
Capability<U> cast()
{
return {static_cast<U *>(ptr)};
}
/**
* Returns true if the tags of `this` and `other` match and if `this`
* conveys no rights that are not present in `other`. Returns false in
* all other cases.
*/
template<typename U>
bool is_subset_of(Capability<U> other)
{
return __builtin_cheri_subset_test(other.ptr, ptr);
}
/**
* Seal this capability with the given key.
*/
Capability<T> &seal(void *key)
{
ptr = static_cast<T *>(__builtin_cheri_seal(ptr, key));
return *this;
}
/**
* Unseal this capability with the given key.
*/
Capability<T> &unseal(void *key)
{
#ifdef FLUTE
// Flute still throws exceptions on invalid use. As a temporary
// work-around, add a quick check that this thing has the sealing
// type and don't unseal if it hasn't. This isn't a complete test,
// it's just sufficient to get the tests passing on Flute.
if (type() != __builtin_cheri_address_get(key))
{
ptr = nullptr;
}
else
#endif
{
ptr = static_cast<T *>(__builtin_cheri_unseal(ptr, key));
}
return *this;
}
/**
* Subscript operator.
*/
template<typename U = T>
requires(!std::same_as<U, void>) U &operator[](size_t index)
{
return ptr[index];
}
/**
* Returns true if the capability is `align`-byte aligned, false
* otherwise.
*/
bool is_aligned(size_t align)
{
return __builtin_is_aligned(ptr, align);