Migrate to error constants in glacier

This commit is contained in:
Drew Galbraith 2023-06-21 18:28:54 -07:00
parent 3ab9b4d818
commit 0b86a94f14
30 changed files with 171 additions and 114 deletions

View file

@ -0,0 +1,39 @@
#pragma once
#include <stdint.h>
namespace glcr {
typedef uint64_t ErrorCode;
#define RET_ERR(expr) \
{ \
z_err_t _tmp_err = expr; \
if (_tmp_err != glcr::OK) { \
return _tmp_err; \
} \
}
static const uint64_t OK = 0x0;
// First set of error codes generally indicate user errors.
static const uint64_t INVALID_ARGUMENT = 0x1;
static const uint64_t NOT_FOUND = 0x2;
static const uint64_t PERMISSION_DENIED = 0x3;
static const uint64_t NULL_PTR = 0x4;
static const uint64_t EMPTY = 0x5;
static const uint64_t ALREADY_EXISTS = 0x6;
static const uint64_t BUFFER_SIZE = 0x7;
static const uint64_t FAILED_PRECONDITION = 0x8;
// Second set of error codes generally indicate service errors.
static const uint64_t INTERNAL = 0x100;
static const uint64_t UNIMPLEMENTED = 0x101;
static const uint64_t EXHAUSTED = 0x102;
// Kernel specific error codes (relating to capabilities).
static const uint64_t CAP_NOT_FOUND = 0x1000;
static const uint64_t CAP_WRONG_TYPE = 0x1001;
static const uint64_t CAP_PERMISSION_DENIED = 0x1002;
} // namespace glcr

View file

@ -0,0 +1,41 @@
#pragma once
#include <glacier/status/error.h>
namespace glcr {
template <typename T>
class ErrorOr {
public:
ErrorOr() = delete;
ErrorOr(const ErrorOr&) = delete;
ErrorOr(ErrorOr&&) = delete;
ErrorOr(ErrorCode code) : error_(code), ok_(false) {}
ErrorOr(T obj) : obj_(obj), ok_(true) {}
ErrorOr(T&& obj) : obj_(obj), ok_(true) {}
bool ok() { return ok_; }
operator bool() { return ok_; }
T& value() { return obj_; }
ErrorCode error() { return error_; }
private:
union {
ErrorCode error_;
T obj_;
};
bool ok_;
};
#define ASSIGN_OR_RETURN(lhs, rhs) \
\
auto e##__LINE__ = rhs; \
if (!e##__LINE__.ok()) { \
return e##__LINE__.error(); \
} \
lhs = rhs.value();
} // namespace glcr

View file

@ -13,6 +13,7 @@ target_include_directories(mammoth_lib
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(mammoth_lib
glacier
c
zion_lib)

View file

@ -1,5 +1,6 @@
#pragma once
#include <glacier/status/error.h>
#include <stdint.h>
#include <zcall.h>
@ -37,13 +38,10 @@ template <typename T>
z_err_t Channel::ReadStructAndCap(T* obj, uint64_t* cap) {
uint64_t num_bytes = sizeof(T);
uint64_t num_caps = 1;
uint64_t ret = ZChannelRecv(chan_cap_, &num_bytes, obj, &num_caps, cap);
RET_ERR(ZChannelRecv(chan_cap_, &num_bytes, obj, &num_caps, cap));
if (ret != Z_OK) {
return ret;
}
if (num_caps != 1 || num_bytes != sizeof(T)) {
return Z_ERR_INVALID;
return glcr::FAILED_PRECONDITION;
}
return Z_OK;
return glcr::OK;
}

View file

@ -10,11 +10,3 @@ void dbgln(const char* fmt, ...);
void check(uint64_t);
void crash(const char*, z_err_t);
#define RET_ERR(expr) \
{ \
z_err_t _tmp_err = expr; \
if (_tmp_err != Z_OK) { \
return _tmp_err; \
} \
}

View file

@ -19,7 +19,7 @@ uint64_t strlen(const char* ptr) {
void Channel::adopt_cap(uint64_t id) {
if (chan_cap_ != 0) {
crash("Adopting over channel.", Z_ERR_EXISTS);
crash("Adopting over channel.", glcr::ALREADY_EXISTS);
}
chan_cap_ = id;
}
@ -33,14 +33,14 @@ z_cap_t Channel::cap() { return chan_cap_; }
z_err_t Channel::WriteStr(const char* msg) {
if (!chan_cap_) {
return Z_ERR_NULL;
return glcr::NULL_PTR;
}
return ZChannelSend(chan_cap_, strlen(msg), msg, 0, nullptr);
}
z_err_t Channel::ReadStr(char* buffer, uint64_t* size) {
if (!chan_cap_) {
return Z_ERR_NULL;
return glcr::NULL_PTR;
}
uint64_t num_caps = 0;
return ZChannelRecv(chan_cap_, size, reinterpret_cast<uint8_t*>(buffer),
@ -49,12 +49,9 @@ z_err_t Channel::ReadStr(char* buffer, uint64_t* size) {
z_err_t CreateChannels(Channel& c1, Channel& c2) {
z_cap_t chan1, chan2;
z_err_t err = ZChannelCreate(&chan1, &chan2);
if (err != Z_OK) {
return err;
}
RET_ERR(ZChannelCreate(&chan1, &chan2));
c1.adopt_cap(chan1);
c2.adopt_cap(chan2);
return Z_OK;
return glcr::OK;
}

View file

@ -1,5 +1,6 @@
#include "include/mammoth/debug.h"
#include <glacier/status/error.h>
#include <stdarg.h>
#include <stdio.h>
#include <zcall.h>
@ -25,18 +26,18 @@ void dbgln(const char* fmt, ...) {
void check(uint64_t code) {
switch (code) {
case Z_OK:
case glcr::OK:
return;
case Z_ERR_UNIMPLEMENTED:
case glcr::UNIMPLEMENTED:
dbgln("crash: UNIMPLEMENTED");
break;
case Z_ERR_CAP_NOT_FOUND:
case glcr::CAP_NOT_FOUND:
dbgln("crash: missing capability");
break;
case Z_ERR_CAP_TYPE:
case glcr::CAP_WRONG_TYPE:
dbgln("crash: capability of the wrong type");
break;
case Z_ERR_CAP_DENIED:
case glcr::CAP_PERMISSION_DENIED:
dbgln("crash: capability permissions error");
break;
default:

View file

@ -1,5 +1,6 @@
#include "mammoth/init.h"
#include <glacier/status/error.h>
#include <ztypes.h>
#include "mammoth/debug.h"
@ -16,7 +17,7 @@ z_err_t ParseInitPort(uint64_t init_port_cap) {
Port port(init_port_cap);
z_err_t ret;
uint64_t init_sig, init_cap;
while ((ret = port.PollForIntCap(&init_sig, &init_cap)) != Z_ERR_EMPTY) {
while ((ret = port.PollForIntCap(&init_sig, &init_cap)) != glcr::EMPTY) {
RET_ERR(ret);
switch (init_sig) {
case Z_INIT_SELF_PROC:
@ -39,5 +40,5 @@ z_err_t ParseInitPort(uint64_t init_port_cap) {
}
}
return Z_OK;
return glcr::OK;
}

View file

@ -1,5 +1,6 @@
#include "mammoth/port.h"
#include <glacier/status/error.h>
#include <zcall.h>
#include "mammoth/debug.h"
@ -13,10 +14,10 @@ z_err_t Port::PollForIntCap(uint64_t *msg, uint64_t *cap) {
cap));
if (bytes != sizeof(uint64_t)) {
return Z_ERR_INVALID;
return glcr::FAILED_PRECONDITION;
}
if (caps != 1) {
return Z_ERR_INVALID;
return glcr::FAILED_PRECONDITION;
}
return Z_OK;
return glcr::OK;
}

View file

@ -1,5 +1,6 @@
#include "mammoth/process.h"
#include <glacier/status/error.h>
#include <zcall.h>
#include "mammoth/channel.h"
@ -133,5 +134,5 @@ uint64_t SpawnProcessFromElfRegion(uint64_t program, Channel& local) {
#endif
check(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
return Z_OK;
return glcr::OK;
}