From 96a2f74e14025853a532db6923e4bf28f28c52a5 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Mon, 27 Nov 2023 08:11:52 -0800 Subject: [PATCH] [Yunq] Release server cap on destructor. --- sys/denali/lib/denali/denali.yunq.client.cpp | 14 ++++++++ sys/denali/lib/denali/denali.yunq.client.h | 4 +++ sys/denali/lib/denali/denali.yunq.server.cpp | 32 ++++++++++++++++--- sys/denali/lib/denali/denali.yunq.server.h | 5 +++ .../victoriafalls.yunq.client.cpp | 14 ++++++++ .../victoriafalls/victoriafalls.yunq.client.h | 4 +++ .../victoriafalls.yunq.server.cpp | 32 ++++++++++++++++--- .../victoriafalls/victoriafalls.yunq.server.h | 5 +++ .../lib/voyageurs/voyageurs.yunq.server.cpp | 6 ++++ .../lib/voyageurs/voyageurs.yunq.server.h | 1 + .../yellowstone/yellowstone.yunq.server.cpp | 6 ++++ .../lib/yellowstone/yellowstone.yunq.server.h | 1 + yunq/server.cpp.jinja | 6 ++++ yunq/server.h.jinja | 1 + 14 files changed, 123 insertions(+), 8 deletions(-) diff --git a/sys/denali/lib/denali/denali.yunq.client.cpp b/sys/denali/lib/denali/denali.yunq.client.cpp index 3f5cdab..a8c29f0 100644 --- a/sys/denali/lib/denali/denali.yunq.client.cpp +++ b/sys/denali/lib/denali/denali.yunq.client.cpp @@ -8,7 +8,9 @@ + glcr::ErrorCode DenaliClient::Read(const ReadRequest& request, ReadResponse& response) { + uint64_t buffer_size = kBufferSize; uint64_t cap_size = kCapBufferSize; @@ -17,7 +19,10 @@ glcr::ErrorCode DenaliClient::Read(const ReadRequest& request, ReadResponse& res buffer_.WriteAt(8, 0); cap_buffer_.Reset(); + uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_); + + buffer_.WriteAt(4, 16 + length); z_cap_t reply_port_cap; @@ -33,14 +38,18 @@ glcr::ErrorCode DenaliClient::Read(const ReadRequest& request, ReadResponse& res // Check Response Code. RET_ERR(buffer_.At(8)); + response.ParseFromBytes(buffer_, 16, cap_buffer_); + return glcr::OK; } + glcr::ErrorCode DenaliClient::ReadMany(const ReadManyRequest& request, ReadResponse& response) { + uint64_t buffer_size = kBufferSize; uint64_t cap_size = kCapBufferSize; @@ -49,7 +58,10 @@ glcr::ErrorCode DenaliClient::ReadMany(const ReadManyRequest& request, ReadRespo buffer_.WriteAt(8, 1); cap_buffer_.Reset(); + uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_); + + buffer_.WriteAt(4, 16 + length); z_cap_t reply_port_cap; @@ -65,8 +77,10 @@ glcr::ErrorCode DenaliClient::ReadMany(const ReadManyRequest& request, ReadRespo // Check Response Code. RET_ERR(buffer_.At(8)); + response.ParseFromBytes(buffer_, 16, cap_buffer_); + return glcr::OK; } diff --git a/sys/denali/lib/denali/denali.yunq.client.h b/sys/denali/lib/denali/denali.yunq.client.h index 1ef05d5..c0e2d42 100644 --- a/sys/denali/lib/denali/denali.yunq.client.h +++ b/sys/denali/lib/denali/denali.yunq.client.h @@ -17,9 +17,13 @@ class DenaliClient { z_cap_t Capability() { return endpoint_; } + [[nodiscard]] glcr::ErrorCode Read(const ReadRequest& request, ReadResponse& response); + + [[nodiscard]] glcr::ErrorCode ReadMany(const ReadManyRequest& request, ReadResponse& response); + private: z_cap_t endpoint_; diff --git a/sys/denali/lib/denali/denali.yunq.server.cpp b/sys/denali/lib/denali/denali.yunq.server.cpp index 9398599..1fda418 100644 --- a/sys/denali/lib/denali/denali.yunq.server.cpp +++ b/sys/denali/lib/denali/denali.yunq.server.cpp @@ -29,6 +29,12 @@ void DenaliServerBaseThreadBootstrap(void* server_base) { ((DenaliServerBase*)server_base)->ServerThread(); } +DenaliServerBase::~DenaliServerBase() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + glcr::ErrorOr DenaliServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); @@ -87,25 +93,43 @@ glcr::ErrorCode DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request, switch(method_select) { case 0: { + + ReadRequest yunq_request; - ReadResponse yunq_response; - yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); + + + ReadResponse yunq_response; + + + RET_ERR(HandleRead(yunq_request, yunq_response)); + + resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps); + break; } case 1: { + + ReadManyRequest yunq_request; - ReadResponse yunq_response; - yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); + + + ReadResponse yunq_response; + + + RET_ERR(HandleReadMany(yunq_request, yunq_response)); + + resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps); + break; } default: { diff --git a/sys/denali/lib/denali/denali.yunq.server.h b/sys/denali/lib/denali/denali.yunq.server.h index d337d0a..b10e187 100644 --- a/sys/denali/lib/denali/denali.yunq.server.h +++ b/sys/denali/lib/denali/denali.yunq.server.h @@ -15,17 +15,22 @@ class DenaliServerBase { DenaliServerBase(z_cap_t Denali_cap) : endpoint_(Denali_cap) {} DenaliServerBase(const DenaliServerBase&) = delete; DenaliServerBase(DenaliServerBase&&) = delete; + virtual ~DenaliServerBase(); glcr::ErrorOr CreateClient(); [[nodiscard]] Thread RunServer(); + [[nodiscard]] virtual glcr::ErrorCode HandleRead(const ReadRequest&, ReadResponse&) = 0; + + [[nodiscard]] virtual glcr::ErrorCode HandleReadMany(const ReadManyRequest&, ReadResponse&) = 0; + private: z_cap_t endpoint_; diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp index 2375b15..142625a 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp @@ -8,7 +8,9 @@ + glcr::ErrorCode VFSClient::OpenFile(const OpenFileRequest& request, OpenFileResponse& response) { + uint64_t buffer_size = kBufferSize; uint64_t cap_size = kCapBufferSize; @@ -17,7 +19,10 @@ glcr::ErrorCode VFSClient::OpenFile(const OpenFileRequest& request, OpenFileResp buffer_.WriteAt(8, 0); cap_buffer_.Reset(); + uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_); + + buffer_.WriteAt(4, 16 + length); z_cap_t reply_port_cap; @@ -33,14 +38,18 @@ glcr::ErrorCode VFSClient::OpenFile(const OpenFileRequest& request, OpenFileResp // Check Response Code. RET_ERR(buffer_.At(8)); + response.ParseFromBytes(buffer_, 16, cap_buffer_); + return glcr::OK; } + glcr::ErrorCode VFSClient::GetDirectory(const GetDirectoryRequest& request, Directory& response) { + uint64_t buffer_size = kBufferSize; uint64_t cap_size = kCapBufferSize; @@ -49,7 +58,10 @@ glcr::ErrorCode VFSClient::GetDirectory(const GetDirectoryRequest& request, Dire buffer_.WriteAt(8, 1); cap_buffer_.Reset(); + uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_); + + buffer_.WriteAt(4, 16 + length); z_cap_t reply_port_cap; @@ -65,8 +77,10 @@ glcr::ErrorCode VFSClient::GetDirectory(const GetDirectoryRequest& request, Dire // Check Response Code. RET_ERR(buffer_.At(8)); + response.ParseFromBytes(buffer_, 16, cap_buffer_); + return glcr::OK; } diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h index 94910f3..d0ffcdc 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h @@ -17,9 +17,13 @@ class VFSClient { z_cap_t Capability() { return endpoint_; } + [[nodiscard]] glcr::ErrorCode OpenFile(const OpenFileRequest& request, OpenFileResponse& response); + + [[nodiscard]] glcr::ErrorCode GetDirectory(const GetDirectoryRequest& request, Directory& response); + private: z_cap_t endpoint_; diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp index 75dba40..995f8cb 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp @@ -29,6 +29,12 @@ void VFSServerBaseThreadBootstrap(void* server_base) { ((VFSServerBase*)server_base)->ServerThread(); } +VFSServerBase::~VFSServerBase() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + glcr::ErrorOr VFSServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); @@ -87,25 +93,43 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request, switch(method_select) { case 0: { + + OpenFileRequest yunq_request; - OpenFileResponse yunq_response; - yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); + + + OpenFileResponse yunq_response; + + + RET_ERR(HandleOpenFile(yunq_request, yunq_response)); + + resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps); + break; } case 1: { + + GetDirectoryRequest yunq_request; - Directory yunq_response; - yunq_request.ParseFromBytes(request, kHeaderSize, req_caps); + + + Directory yunq_response; + + + RET_ERR(HandleGetDirectory(yunq_request, yunq_response)); + + resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps); + break; } default: { diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h index 7490959..d7cabe0 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h @@ -15,17 +15,22 @@ class VFSServerBase { VFSServerBase(z_cap_t VFS_cap) : endpoint_(VFS_cap) {} VFSServerBase(const VFSServerBase&) = delete; VFSServerBase(VFSServerBase&&) = delete; + virtual ~VFSServerBase(); glcr::ErrorOr CreateClient(); [[nodiscard]] Thread RunServer(); + [[nodiscard]] virtual glcr::ErrorCode HandleOpenFile(const OpenFileRequest&, OpenFileResponse&) = 0; + + [[nodiscard]] virtual glcr::ErrorCode HandleGetDirectory(const GetDirectoryRequest&, Directory&) = 0; + private: z_cap_t endpoint_; diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp index e0095c9..a062b02 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp @@ -29,6 +29,12 @@ void VoyageursServerBaseThreadBootstrap(void* server_base) { ((VoyageursServerBase*)server_base)->ServerThread(); } +VoyageursServerBase::~VoyageursServerBase() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + glcr::ErrorOr VoyageursServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h index 9921534..7c85b40 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h @@ -15,6 +15,7 @@ class VoyageursServerBase { VoyageursServerBase(z_cap_t Voyageurs_cap) : endpoint_(Voyageurs_cap) {} VoyageursServerBase(const VoyageursServerBase&) = delete; VoyageursServerBase(VoyageursServerBase&&) = delete; + virtual ~VoyageursServerBase(); glcr::ErrorOr CreateClient(); diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp index c5f4a0e..88afc31 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp @@ -29,6 +29,12 @@ void YellowstoneServerBaseThreadBootstrap(void* server_base) { ((YellowstoneServerBase*)server_base)->ServerThread(); } +YellowstoneServerBase::~YellowstoneServerBase() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + glcr::ErrorOr YellowstoneServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h index c5b8f3b..316a8d3 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h @@ -15,6 +15,7 @@ class YellowstoneServerBase { YellowstoneServerBase(z_cap_t Yellowstone_cap) : endpoint_(Yellowstone_cap) {} YellowstoneServerBase(const YellowstoneServerBase&) = delete; YellowstoneServerBase(YellowstoneServerBase&&) = delete; + virtual ~YellowstoneServerBase(); glcr::ErrorOr CreateClient(); diff --git a/yunq/server.cpp.jinja b/yunq/server.cpp.jinja index 66763e9..595b278 100644 --- a/yunq/server.cpp.jinja +++ b/yunq/server.cpp.jinja @@ -29,6 +29,12 @@ void {{interface.name}}ServerBaseThreadBootstrap(void* server_base) { (({{interface.name}}ServerBase*)server_base)->ServerThread(); } +{{interface.name}}ServerBase::~{{interface.name}}ServerBase() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + glcr::ErrorOr<{{interface.name}}Client> {{interface.name}}ServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/yunq/server.h.jinja b/yunq/server.h.jinja index 74d0de7..5b487ac 100644 --- a/yunq/server.h.jinja +++ b/yunq/server.h.jinja @@ -15,6 +15,7 @@ class {{interface.name}}ServerBase { {{interface.name}}ServerBase(z_cap_t {{interface.name}}_cap) : endpoint_({{interface.name}}_cap) {} {{interface.name}}ServerBase(const {{interface.name}}ServerBase&) = delete; {{interface.name}}ServerBase({{interface.name}}ServerBase&&) = delete; + virtual ~{{interface.name}}ServerBase(); glcr::ErrorOr<{{interface.name}}Client> CreateClient();