[Yunq] Add a basic yunq test into libyunq.

This commit is contained in:
Drew Galbraith 2024-01-16 18:58:03 -08:00
parent 9c860dd6a4
commit fecaa387b0
21 changed files with 189 additions and 936 deletions

View File

@ -5,8 +5,6 @@ set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
project(AcadiaOS VERSION 0.0.1 LANGUAGES CXX ASM-ATT) project(AcadiaOS VERSION 0.0.1 LANGUAGES CXX ASM-ATT)
include(CTest)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_EXPORT_COMPILE_COMMANDS True) set(CMAKE_EXPORT_COMPILE_COMMANDS True)
@ -14,8 +12,16 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS True)
set(BASE_COMPILE_FLAGS "-ffreestanding -fno-rtti -fno-exceptions -mincoming-stack-boundary=3") set(BASE_COMPILE_FLAGS "-ffreestanding -fno-rtti -fno-exceptions -mincoming-stack-boundary=3")
set(BASE_LINK_FLAGS "-nostdlib") set(BASE_LINK_FLAGS "-nostdlib")
if (enable_testing)
include(CTest)
find_package(Catch2 3 REQUIRED)
find_program(MEMORYCHECK_COMMAND valgrind)
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")
add_custom_target(build_test)
endif()
add_subdirectory(zion) add_subdirectory(zion)
add_subdirectory(lib)
add_subdirectory(yunq) add_subdirectory(yunq)
add_subdirectory(lib)
add_subdirectory(usr) add_subdirectory(usr)
add_subdirectory(sys) add_subdirectory(sys)

View File

@ -1,10 +1,2 @@
find_package(Catch2 3 REQUIRED)
find_program(MEMORYCHECK_COMMAND valgrind)
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")
add_subdirectory(container) add_subdirectory(container)
add_custom_target(build_test)
add_dependencies(build_test
glc_vec_test)

View File

@ -2,3 +2,7 @@ add_executable(glc_vec_test vector.cpp)
target_link_libraries(glc_vec_test glacier Catch2::Catch2WithMain) target_link_libraries(glc_vec_test glacier Catch2::Catch2WithMain)
target_include_directories(glc_vec_test PRIVATE "../..") target_include_directories(glc_vec_test PRIVATE "../..")
add_test(NAME glc_vec_test COMMAND $<TARGET_FILE:glc_vec_test>) add_test(NAME glc_vec_test COMMAND $<TARGET_FILE:glc_vec_test>)
add_dependencies(build_test
glc_vec_test)

View File

@ -16,3 +16,7 @@ target_include_directories(yunq
set_target_properties(yunq PROPERTIES set_target_properties(yunq PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}") COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}")
if (enable_testing)
add_subdirectory(test)
endif()

View File

@ -0,0 +1,42 @@
add_executable(yunq_test yunq_test.cpp)
add_dependencies(yunq_test
example_yunq)
target_link_libraries(yunq_test
Catch2::Catch2WithMain
example_yunq)
target_include_directories(yunq_test PRIVATE "." "../../../zion/include")
add_test(NAME yunq_test COMMAND $<TARGET_FILE:yunq_test>)
add_dependencies(build_test
yunq_test)
# Build the yunq manualy rather than using the generator
# because we don't want to link against mammoth and overrite new.
set(target example_yunq)
add_library(example_yunq
${CMAKE_CURRENT_SOURCE_DIR}/example/example.yunq.cpp
)
target_include_directories(example_yunq
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/example"
)
target_link_libraries(${target}
glacier
yunq
zion_stub
)
set(PYTHON "${CMAKE_SOURCE_DIR}/yunq/venv/bin/python")
set(YUNQ "${CMAKE_SOURCE_DIR}/yunq/yunq.py")
add_custom_command(
OUTPUT example/example.yunq.cpp
COMMAND ${PYTHON} ${YUNQ} ${CMAKE_CURRENT_SOURCE_DIR}/example/example.yunq
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/example/example.yunq
)

View File

@ -0,0 +1,5 @@
package ex;
message Basic {
u64 field;
}

View File

@ -0,0 +1,58 @@
// Generated file -- DO NOT MODIFY.
#include "example.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
namespace ex {
namespace {
const uint64_t header_size = 24; // 4x uint32, 1x uint64
struct ExtPointer {
uint32_t offset;
uint32_t length;
};
} // namespace
glcr::Status Basic::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status Basic::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status Basic::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse field.
ASSIGN_OR_RETURN(field_, message.ReadField<uint64_t>(0));
return glcr::Status::Ok();
}
uint64_t Basic::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 1);
return SerializeInternal(serializer);
}
uint64_t Basic::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 1, caps);
return SerializeInternal(serializer);
}
uint64_t Basic::SerializeInternal(yunq::Serializer& serializer) const {
// Write field.
serializer.WriteField<uint64_t>(0, field_);
serializer.WriteHeader();
return serializer.size();
}
} // namepace ex

View File

@ -0,0 +1,43 @@
// Generated file - DO NOT MODIFY
#pragma once
#include <glacier/buffer/byte_buffer.h>
#include <glacier/buffer/cap_buffer.h>
#include <glacier/status/status.h>
#include <glacier/container/vector.h>
#include <glacier/string/string.h>
#include <yunq/message_view.h>
#include <yunq/serialize.h>
#include <ztypes.h>
namespace ex {
class Basic {
public:
Basic() {}
// Delete copy and move until implemented.
Basic(const Basic&) = delete;
Basic(Basic&&) = default;
Basic& operator=(Basic&&) = default;
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message);
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const uint64_t& field() const { return field_; }
uint64_t& mutable_field() { return field_; }
void set_field(const uint64_t& value) { field_ = value; }
private:
uint64_t field_;
// Parses everything except for caps.
glcr::Status ParseFromBytesInternal(const yunq::MessageView& message);
uint64_t SerializeInternal(yunq::Serializer& serializer) const;
};
} // namepace ex

View File

@ -0,0 +1,23 @@
#include <catch2/catch_test_macros.hpp>
#include "example/example.yunq.h"
TEST_CASE("Basic Setter/Getter", "[yunq]") {
ex::Basic b;
b.set_field(1);
REQUIRE(b.field() == 1);
}
TEST_CASE("Basic serialization", "[yunq]") {
ex::Basic a;
a.set_field(1);
glcr::ByteBuffer buf(1024);
a.SerializeToBytes(buf, 0);
ex::Basic b;
yunq::MessageView v(buf, 0);
REQUIRE(b.ParseFromBytes(v).ok() == true);
REQUIRE(b.field() == 1);
}

View File

@ -1,100 +0,0 @@
// Generated file - DO NOT MODIFY
#include "denali.yunq.client.h"
#include <glacier/buffer/byte_buffer.h>
#include <glacier/buffer/cap_buffer.h>
#include <mammoth/util/debug.h>
#include <zcall.h>
DenaliClient::~DenaliClient() {
if (endpoint_ != 0) {
check(ZCapRelease(endpoint_));
}
}
glcr::Status DenaliClient::Read(const ReadRequest& request, ReadResponse& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
const uint32_t kSentinel = 0xBEEFDEAD;
buffer_.WriteAt<uint32_t>(0, kSentinel);
buffer_.WriteAt<uint64_t>(8, 0);
cap_buffer_.Reset();
uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_);
buffer_.WriteAt<uint32_t>(4, 16 + length);
z_cap_t reply_port_cap;
RET_ERR(ZEndpointSend(endpoint_, 16 + length, buffer_.RawPtr(), cap_buffer_.UsedSlots(), cap_buffer_.RawPtr(), &reply_port_cap));
// FIXME: Add a way to zero out the first buffer.
RET_ERR(ZReplyPortRecv(reply_port_cap, &buffer_size, buffer_.RawPtr(), &cap_size, cap_buffer_.RawPtr()));
if (buffer_.At<uint32_t>(0) != kSentinel) {
return glcr::InvalidResponse("Got an invalid response from server.");
}
// Check Response Code.
RET_ERR(buffer_.At<uint64_t>(8));
yunq::MessageView resp_view(buffer_, 16);
RETURN_ERROR(response.ParseFromBytes(resp_view, cap_buffer_));
return glcr::OK;
}
glcr::Status DenaliClient::ReadMany(const ReadManyRequest& request, ReadResponse& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
const uint32_t kSentinel = 0xBEEFDEAD;
buffer_.WriteAt<uint32_t>(0, kSentinel);
buffer_.WriteAt<uint64_t>(8, 1);
cap_buffer_.Reset();
uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_);
buffer_.WriteAt<uint32_t>(4, 16 + length);
z_cap_t reply_port_cap;
RET_ERR(ZEndpointSend(endpoint_, 16 + length, buffer_.RawPtr(), cap_buffer_.UsedSlots(), cap_buffer_.RawPtr(), &reply_port_cap));
// FIXME: Add a way to zero out the first buffer.
RET_ERR(ZReplyPortRecv(reply_port_cap, &buffer_size, buffer_.RawPtr(), &cap_size, cap_buffer_.RawPtr()));
if (buffer_.At<uint32_t>(0) != kSentinel) {
return glcr::InvalidResponse("Got an invalid response from server.");
}
// Check Response Code.
RET_ERR(buffer_.At<uint64_t>(8));
yunq::MessageView resp_view(buffer_, 16);
RETURN_ERROR(response.ParseFromBytes(resp_view, cap_buffer_));
return glcr::OK;
}

View File

@ -1,186 +0,0 @@
// Generated file -- DO NOT MODIFY.
#include "denali.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
namespace {
const uint64_t header_size = 24; // 4x uint32, 1x uint64
struct ExtPointer {
uint32_t offset;
uint32_t length;
};
} // namespace
glcr::Status DiskBlock::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status DiskBlock::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status DiskBlock::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse lba.
ASSIGN_OR_RETURN(lba_, message.ReadField<uint64_t>(0));
// Parse size.
ASSIGN_OR_RETURN(size_, message.ReadField<uint64_t>(1));
return glcr::Status::Ok();
}
uint64_t DiskBlock::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 2);
return SerializeInternal(serializer);
}
uint64_t DiskBlock::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 2, caps);
return SerializeInternal(serializer);
}
uint64_t DiskBlock::SerializeInternal(yunq::Serializer& serializer) const {
// Write lba.
serializer.WriteField<uint64_t>(0, lba_);
// Write size.
serializer.WriteField<uint64_t>(1, size_);
serializer.WriteHeader();
return serializer.size();
}
glcr::Status ReadRequest::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status ReadRequest::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status ReadRequest::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse device_id.
ASSIGN_OR_RETURN(device_id_, message.ReadField<uint64_t>(0));
// Parse block.
message.ReadMessage<DiskBlock>(1, block_);
return glcr::Status::Ok();
}
uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 2);
return SerializeInternal(serializer);
}
uint64_t ReadRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 2, caps);
return SerializeInternal(serializer);
}
uint64_t ReadRequest::SerializeInternal(yunq::Serializer& serializer) const {
// Write device_id.
serializer.WriteField<uint64_t>(0, device_id_);
// Write block.
serializer.WriteMessage<DiskBlock>(1, block_);
serializer.WriteHeader();
return serializer.size();
}
glcr::Status ReadManyRequest::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status ReadManyRequest::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status ReadManyRequest::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse device_id.
ASSIGN_OR_RETURN(device_id_, message.ReadField<uint64_t>(0));
// Parse blocks.
message.ReadRepeatedMessage<DiskBlock>(1, blocks_);
return glcr::Status::Ok();
}
uint64_t ReadManyRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 2);
return SerializeInternal(serializer);
}
uint64_t ReadManyRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 2, caps);
return SerializeInternal(serializer);
}
uint64_t ReadManyRequest::SerializeInternal(yunq::Serializer& serializer) const {
// Write device_id.
serializer.WriteField<uint64_t>(0, device_id_);
// Write blocks.
serializer.WriteRepeatedMessage<DiskBlock>(1, blocks_);
serializer.WriteHeader();
return serializer.size();
}
glcr::Status ReadResponse::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
// Parse memory.
ASSIGN_OR_RETURN(memory_, message.ReadCapability(2));
return glcr::Status::Ok();
}
glcr::Status ReadResponse::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
// Parse memory.
ASSIGN_OR_RETURN(memory_, message.ReadCapability(2, caps));
return glcr::Status::Ok();
}
glcr::Status ReadResponse::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse device_id.
ASSIGN_OR_RETURN(device_id_, message.ReadField<uint64_t>(0));
// Parse size.
ASSIGN_OR_RETURN(size_, message.ReadField<uint64_t>(1));
// Parse memory.
return glcr::Status::Ok();
}
uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 3);
return SerializeInternal(serializer);
}
uint64_t ReadResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 3, caps);
return SerializeInternal(serializer);
}
uint64_t ReadResponse::SerializeInternal(yunq::Serializer& serializer) const {
// Write device_id.
serializer.WriteField<uint64_t>(0, device_id_);
// Write size.
serializer.WriteField<uint64_t>(1, size_);
// Write memory.
serializer.WriteCapability(2, memory_);
serializer.WriteHeader();
return serializer.size();
}

View File

@ -1,152 +0,0 @@
// Generated file -- DO NOT MODIFY.
#include "denali.yunq.server.h"
#include <mammoth/util/debug.h>
#include <zcall.h>
namespace {
const uint32_t kSentinel = 0xBEEFDEAD;
const uint32_t kHeaderSize = 0x10;
void WriteError(glcr::ByteBuffer& buffer, glcr::ErrorCode err) {
buffer.WriteAt<uint32_t>(0, kSentinel);
buffer.WriteAt<uint32_t>(4, kHeaderSize);
buffer.WriteAt<uint64_t>(8, err);
}
void WriteHeader(glcr::ByteBuffer& buffer, uint64_t message_length) {
buffer.WriteAt<uint32_t>(0, kSentinel);
buffer.WriteAt<uint32_t>(4, kHeaderSize + message_length);
buffer.WriteAt<uint64_t>(8, glcr::OK);
}
} // namespace
void DenaliServerBaseThreadBootstrap(void* server_base) {
((DenaliServerBase*)server_base)->ServerThread();
}
DenaliServerBase::~DenaliServerBase() {
if (endpoint_ != 0) {
check(ZCapRelease(endpoint_));
}
}
glcr::ErrorOr<z_cap_t> DenaliServerBase::CreateClientCap() {
uint64_t client_cap;
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
return client_cap;
}
glcr::ErrorOr<DenaliClient> DenaliServerBase::CreateClient() {
uint64_t client_cap;
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
return DenaliClient(client_cap);
}
Thread DenaliServerBase::RunServer() {
return Thread(DenaliServerBaseThreadBootstrap, this);
}
void DenaliServerBase::ServerThread() {
glcr::ByteBuffer recv_buffer(0x1000);
glcr::CapBuffer recv_cap(0x10);
glcr::ByteBuffer resp_buffer(0x1000);
glcr::CapBuffer resp_cap(0x10);
z_cap_t reply_port_cap;
while (true) {
uint64_t recv_cap_size = 0x10;
uint64_t recv_buf_size = 0x1000;
recv_cap.Reset();
glcr::ErrorCode recv_err = static_cast<glcr::ErrorCode>(ZEndpointRecv(endpoint_, &recv_buf_size, recv_buffer.RawPtr(), &recv_cap_size, recv_cap.RawPtr(), &reply_port_cap));
if (recv_err != glcr::OK) {
dbgln("Error in receive: {x}", recv_err);
continue;
}
uint64_t resp_length = 0;
glcr::ErrorCode reply_err = glcr::OK;
resp_cap.Reset();
glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap);
if (!err) {
WriteError(resp_buffer, err.code());
dbgln("Responding Error {}", err.message());
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr));
} else {
WriteHeader(resp_buffer, resp_length);
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize + resp_length, resp_buffer.RawPtr(), resp_cap.UsedSlots(), resp_cap.RawPtr()));
}
if (reply_err != glcr::OK) {
dbgln("Error in reply: {x}", reply_err);
}
}
}
glcr::Status DenaliServerBase::HandleRequest(const glcr::ByteBuffer& request,
const glcr::CapBuffer& req_caps,
glcr::ByteBuffer& response, uint64_t& resp_length,
glcr::CapBuffer& resp_caps) {
if (request.At<uint32_t>(0) != kSentinel) {
return glcr::InvalidArgument("Request Not Valid");
}
uint64_t method_select = request.At<uint64_t>(8);
switch(method_select) {
case 0: {
ReadRequest yunq_request;
yunq::MessageView request_view(request, kHeaderSize);
RETURN_ERROR(yunq_request.ParseFromBytes(request_view, req_caps));
ReadResponse yunq_response;
RETURN_ERROR(HandleRead(yunq_request, yunq_response));
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
case 1: {
ReadManyRequest yunq_request;
yunq::MessageView request_view(request, kHeaderSize);
RETURN_ERROR(yunq_request.ParseFromBytes(request_view, req_caps));
ReadResponse yunq_response;
RETURN_ERROR(HandleReadMany(yunq_request, yunq_response));
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
default: {
return glcr::Unimplemented("Method unimplemented by server.");
}
}
return glcr::Status::Ok();
}

View File

@ -19,6 +19,7 @@ macro(yunq_gen dir include_dir name)
mammoth mammoth
glacier glacier
yunq yunq
zion_stub
) )
set_target_properties(${target} PROPERTIES set_target_properties(${target} PROPERTIES
COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}" COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}"

View File

@ -1,16 +0,0 @@
package srv.file;
interface VFS {
method open (OpenFileRequest) -> (File);
}
message OpenFileRequest {
string path;
repeated u64 options;
}
message File {
string path;
u64 attrs;
capability mem_cap;
}

View File

@ -1,64 +0,0 @@
// Generated file - DO NOT MODIFY
#include "example.yunq.client.h"
#include <glacier/buffer/byte_buffer.h>
#include <glacier/buffer/cap_buffer.h>
#include <mammoth/util/debug.h>
#include <zcall.h>
namespace srv::file {
VFSClient::~VFSClient() {
if (endpoint_ != 0) {
check(ZCapRelease(endpoint_));
}
}
glcr::Status VFSClient::open(const OpenFileRequest& request, File& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
const uint32_t kSentinel = 0xBEEFDEAD;
buffer_.WriteAt<uint32_t>(0, kSentinel);
buffer_.WriteAt<uint64_t>(8, 0);
cap_buffer_.Reset();
uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_);
buffer_.WriteAt<uint32_t>(4, 16 + length);
z_cap_t reply_port_cap;
RET_ERR(ZEndpointSend(endpoint_, 16 + length, buffer_.RawPtr(), cap_buffer_.UsedSlots(), cap_buffer_.RawPtr(), &reply_port_cap));
// FIXME: Add a way to zero out the first buffer.
RET_ERR(ZReplyPortRecv(reply_port_cap, &buffer_size, buffer_.RawPtr(), &cap_size, cap_buffer_.RawPtr()));
if (buffer_.At<uint32_t>(0) != kSentinel) {
return glcr::InvalidResponse("Got an invalid response from server.");
}
// Check Response Code.
RET_ERR(buffer_.At<uint64_t>(8));
yunq::MessageView resp_view(buffer_, 16);
RETURN_ERROR(response.ParseFromBytes(resp_view, cap_buffer_));
return glcr::OK;
}
} // namepace srv::file

View File

@ -1,37 +0,0 @@
// Generated file - DO NOT MODIFY
#pragma once
#include <glacier/buffer/byte_buffer.h>
#include <glacier/buffer/cap_buffer.h>
#include <glacier/status/error.h>
#include <ztypes.h>
#include "example.yunq.h"
namespace srv::file {
class VFSClient {
public:
VFSClient(z_cap_t VFS_cap) : endpoint_(VFS_cap) {}
VFSClient(const VFSClient&) = delete;
VFSClient(VFSClient&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;};
~VFSClient();
z_cap_t Capability() { return endpoint_; }
[[nodiscard]] glcr::Status open(const OpenFileRequest& request, File& response);
private:
z_cap_t endpoint_;
uint64_t kBufferSize = 0x1000;
glcr::ByteBuffer buffer_{kBufferSize};
uint64_t kCapBufferSize = 0x10;
glcr::CapBuffer cap_buffer_{kCapBufferSize};
};
} // namepace srv::file

View File

@ -1,110 +0,0 @@
// Generated file -- DO NOT MODIFY.
#include "example.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
namespace srv::file {
namespace {
const uint64_t header_size = 24; // 4x uint32, 1x uint64
struct ExtPointer {
uint32_t offset;
uint32_t length;
};
} // namespace
glcr::Status OpenFileRequest::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status OpenFileRequest::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
return glcr::Status::Ok();
}
glcr::Status OpenFileRequest::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse path.
ASSIGN_OR_RETURN(path_, message.ReadField<glcr::String>(0));
// Parse options.
ASSIGN_OR_RETURN(options_, message.ReadRepeated<uint64_t>(1));
return glcr::Status::Ok();
}
uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 2);
return SerializeInternal(serializer);
}
uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 2, caps);
return SerializeInternal(serializer);
}
uint64_t OpenFileRequest::SerializeInternal(yunq::Serializer& serializer) const {
// Write path.
serializer.WriteField<glcr::String>(0, path_);
// Write options.
serializer.WriteRepeated<uint64_t>(1, options_);
serializer.WriteHeader();
return serializer.size();
}
glcr::Status File::ParseFromBytes(const yunq::MessageView& message) {
RETURN_ERROR(ParseFromBytesInternal(message));
// Parse mem_cap.
ASSIGN_OR_RETURN(mem_cap_, message.ReadCapability(2));
return glcr::Status::Ok();
}
glcr::Status File::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) {
RETURN_ERROR(ParseFromBytesInternal(message));
// Parse mem_cap.
ASSIGN_OR_RETURN(mem_cap_, message.ReadCapability(2, caps));
return glcr::Status::Ok();
}
glcr::Status File::ParseFromBytesInternal(const yunq::MessageView& message) {
RETURN_ERROR(message.CheckHeader());
// Parse path.
ASSIGN_OR_RETURN(path_, message.ReadField<glcr::String>(0));
// Parse attrs.
ASSIGN_OR_RETURN(attrs_, message.ReadField<uint64_t>(1));
// Parse mem_cap.
return glcr::Status::Ok();
}
uint64_t File::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
yunq::Serializer serializer(bytes, offset, 3);
return SerializeInternal(serializer);
}
uint64_t File::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
yunq::Serializer serializer(bytes, offset, 3, caps);
return SerializeInternal(serializer);
}
uint64_t File::SerializeInternal(yunq::Serializer& serializer) const {
// Write path.
serializer.WriteField<glcr::String>(0, path_);
// Write attrs.
serializer.WriteField<uint64_t>(1, attrs_);
// Write mem_cap.
serializer.WriteCapability(2, mem_cap_);
serializer.WriteHeader();
return serializer.size();
}
} // namepace srv::file

View File

@ -1,71 +0,0 @@
// Generated file - DO NOT MODIFY
#pragma once
#include <glacier/buffer/byte_buffer.h>
#include <glacier/buffer/cap_buffer.h>
#include <glacier/status/status.h>
#include <glacier/container/vector.h>
#include <glacier/string/string.h>
#include <yunq/message_view.h>
#include <yunq/serialize.h>
#include <ztypes.h>
namespace srv::file {
class OpenFileRequest {
public:
OpenFileRequest() {}
// Delete copy and move until implemented.
OpenFileRequest(const OpenFileRequest&) = delete;
OpenFileRequest(OpenFileRequest&&) = delete;
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message);
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const glcr::String& path() const { return path_; }
void set_path(const glcr::String& value) { path_ = value; }
const glcr::Vector<uint64_t>& options() const { return options_; }
void add_options(const uint64_t& value) { options_.PushBack(value); }
private:
glcr::String path_;
glcr::Vector<uint64_t> options_;
// Parses everything except for caps.
glcr::Status ParseFromBytesInternal(const yunq::MessageView& message);
uint64_t SerializeInternal(yunq::Serializer& serializer) const;
};
class File {
public:
File() {}
// Delete copy and move until implemented.
File(const File&) = delete;
File(File&&) = delete;
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message);
[[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const glcr::String& path() const { return path_; }
void set_path(const glcr::String& value) { path_ = value; }
const uint64_t& attrs() const { return attrs_; }
void set_attrs(const uint64_t& value) { attrs_ = value; }
const z_cap_t& mem_cap() const { return mem_cap_; }
void set_mem_cap(const z_cap_t& value) { mem_cap_ = value; }
private:
glcr::String path_;
uint64_t attrs_;
z_cap_t mem_cap_;
// Parses everything except for caps.
glcr::Status ParseFromBytesInternal(const yunq::MessageView& message);
uint64_t SerializeInternal(yunq::Serializer& serializer) const;
};
} // namepace srv::file

View File

@ -1,135 +0,0 @@
// Generated file -- DO NOT MODIFY.
#include "example.yunq.server.h"
#include <mammoth/util/debug.h>
#include <zcall.h>
namespace srv::file {
namespace {
const uint32_t kSentinel = 0xBEEFDEAD;
const uint32_t kHeaderSize = 0x10;
void WriteError(glcr::ByteBuffer& buffer, glcr::ErrorCode err) {
buffer.WriteAt<uint32_t>(0, kSentinel);
buffer.WriteAt<uint32_t>(4, kHeaderSize);
buffer.WriteAt<uint64_t>(8, err);
}
void WriteHeader(glcr::ByteBuffer& buffer, uint64_t message_length) {
buffer.WriteAt<uint32_t>(0, kSentinel);
buffer.WriteAt<uint32_t>(4, kHeaderSize + message_length);
buffer.WriteAt<uint64_t>(8, glcr::OK);
}
} // namespace
void VFSServerBaseThreadBootstrap(void* server_base) {
((VFSServerBase*)server_base)->ServerThread();
}
VFSServerBase::~VFSServerBase() {
if (endpoint_ != 0) {
check(ZCapRelease(endpoint_));
}
}
glcr::ErrorOr<z_cap_t> VFSServerBase::CreateClientCap() {
uint64_t client_cap;
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
return client_cap;
}
glcr::ErrorOr<VFSClient> VFSServerBase::CreateClient() {
uint64_t client_cap;
RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap));
return VFSClient(client_cap);
}
Thread VFSServerBase::RunServer() {
return Thread(VFSServerBaseThreadBootstrap, this);
}
void VFSServerBase::ServerThread() {
glcr::ByteBuffer recv_buffer(0x1000);
glcr::CapBuffer recv_cap(0x10);
glcr::ByteBuffer resp_buffer(0x1000);
glcr::CapBuffer resp_cap(0x10);
z_cap_t reply_port_cap;
while (true) {
uint64_t recv_cap_size = 0x10;
uint64_t recv_buf_size = 0x1000;
recv_cap.Reset();
glcr::ErrorCode recv_err = static_cast<glcr::ErrorCode>(ZEndpointRecv(endpoint_, &recv_buf_size, recv_buffer.RawPtr(), &recv_cap_size, recv_cap.RawPtr(), &reply_port_cap));
if (recv_err != glcr::OK) {
dbgln("Error in receive: {x}", recv_err);
continue;
}
uint64_t resp_length = 0;
glcr::ErrorCode reply_err = glcr::OK;
resp_cap.Reset();
glcr::Status err = HandleRequest(recv_buffer, recv_cap, resp_buffer, resp_length, resp_cap);
if (!err) {
WriteError(resp_buffer, err.code());
dbgln("Responding Error {}", err.message());
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize, resp_buffer.RawPtr(), 0, nullptr));
} else {
WriteHeader(resp_buffer, resp_length);
reply_err = static_cast<glcr::ErrorCode>(ZReplyPortSend(reply_port_cap, kHeaderSize + resp_length, resp_buffer.RawPtr(), resp_cap.UsedSlots(), resp_cap.RawPtr()));
}
if (reply_err != glcr::OK) {
dbgln("Error in reply: {x}", reply_err);
}
}
}
glcr::Status VFSServerBase::HandleRequest(const glcr::ByteBuffer& request,
const glcr::CapBuffer& req_caps,
glcr::ByteBuffer& response, uint64_t& resp_length,
glcr::CapBuffer& resp_caps) {
if (request.At<uint32_t>(0) != kSentinel) {
return glcr::InvalidArgument("Request Not Valid");
}
uint64_t method_select = request.At<uint64_t>(8);
switch(method_select) {
case 0: {
OpenFileRequest yunq_request;
yunq::MessageView request_view(request, kHeaderSize);
RETURN_ERROR(yunq_request.ParseFromBytes(request_view, req_caps));
File yunq_response;
RETURN_ERROR(Handleopen(yunq_request, yunq_response));
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
default: {
return glcr::Unimplemented("Method unimplemented by server.");
}
}
return glcr::Status::Ok();
}
} // namepace srv::file

View File

@ -1,50 +0,0 @@
// Generated File -- DO NOT MODIFY.
#pragma once
#include <glacier/status/error_or.h>
#include <glacier/status/status.h>
#include <mammoth/proc/thread.h>
#include <ztypes.h>
#include "example.yunq.h"
#include "example.yunq.client.h"
namespace srv::file {
class VFSServerBase {
public:
VFSServerBase(z_cap_t VFS_cap) : endpoint_(VFS_cap) {}
VFSServerBase(const VFSServerBase&) = delete;
VFSServerBase(VFSServerBase&&) = delete;
virtual ~VFSServerBase();
glcr::ErrorOr<z_cap_t> CreateClientCap();
glcr::ErrorOr<VFSClient> CreateClient();
[[nodiscard]] Thread RunServer();
[[nodiscard]] virtual glcr::Status Handleopen(const OpenFileRequest&, File&) = 0;
private:
z_cap_t endpoint_;
friend void VFSServerBaseThreadBootstrap(void*);
void ServerThread();
[[nodiscard]] glcr::Status HandleRequest(const glcr::ByteBuffer& request, const glcr::CapBuffer& req_caps,
glcr::ByteBuffer& response, uint64_t& resp_length,
glcr::CapBuffer& resp_caps);
};
} // namepace srv::file

View File

@ -99,7 +99,3 @@ add_library(zion_stub STATIC
target_include_directories(zion_stub target_include_directories(zion_stub
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
set_target_properties(zion_stub
PROPERTIES
COMPILE_FLAGS "${_Z_COMPILE_FLAGS}")