[zion] Dynamically check Capability type.

Instead of passing an enum with the capability when creating it, relying
on polymorphism and a template struct tag to determine the object type
at runtime.

This is cleaner and avoids errors where we pass the wrong capability
type with the cap and do a bad cast at runtime.
This commit is contained in:
Drew Galbraith 2023-06-16 14:53:57 -07:00
parent b4902a79ef
commit a47bac9966
13 changed files with 113 additions and 106 deletions

View file

@ -10,23 +10,12 @@ class Thread;
class Capability : public RefCounted<Capability> {
public:
enum Type {
UNDEFINED,
PROCESS,
THREAD,
ADDRESS_SPACE,
MEMORY_OBJECT,
CHANNEL,
PORT,
};
Capability(const RefPtr<KernelObject>& obj, Type type, uint64_t id,
uint64_t permissions)
: obj_(obj), type_(type), id_(id), permissions_(permissions) {}
Capability(const RefPtr<KernelObject>& obj, uint64_t id, uint64_t permissions)
: obj_(obj), id_(id), permissions_(permissions) {}
template <typename T>
Capability(const RefPtr<T>& obj, Type type, uint64_t id, uint64_t permissions)
: Capability(StaticCastRefPtr<KernelObject>(obj), type, id, permissions) {
}
Capability(const RefPtr<T>& obj, uint64_t id, uint64_t permissions)
: Capability(StaticCastRefPtr<KernelObject>(obj), id, permissions) {}
template <typename T>
RefPtr<T> obj();
@ -34,8 +23,6 @@ class Capability : public RefCounted<Capability> {
uint64_t id() { return id_; }
void set_id(uint64_t id) { id_ = id; }
bool CheckType(Type type) { return type_ == type; }
uint64_t permissions() { return permissions_; }
bool HasPermissions(uint64_t requested) {
return (permissions_ & requested) == requested;
@ -43,7 +30,14 @@ class Capability : public RefCounted<Capability> {
private:
RefPtr<KernelObject> obj_;
Type type_;
uint64_t id_;
uint64_t permissions_;
};
template <typename T>
RefPtr<T> Capability::obj() {
if (obj_->TypeTag() != KernelObjectTag<T>::type) {
return nullptr;
}
return StaticCastRefPtr<T>(obj_);
}