Skip to content

Commit

Permalink
Implement WASI error-handling
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Laszlo Kulcsar <[email protected]>
  • Loading branch information
kulcsaradam committed Sep 5, 2023
1 parent 89c3d0b commit b41d5ed
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 36 deletions.
8 changes: 8 additions & 0 deletions src/runtime/SpecTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ class SpecTestFunctionTypes {
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// RI32
param = new ValueTypeVector();
result = new ValueTypeVector();
result->push_back(Value::Type::I32);
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I64
param = new ValueTypeVector();
Expand Down
18 changes: 2 additions & 16 deletions src/wasi/Wasi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "wasi/Wasi.h"

// https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md

namespace Walrus {

WASI::WasiFunc* WASI::find(std::string funcName)
Expand All @@ -28,22 +30,6 @@ WASI::WasiFunc* WASI::find(std::string funcName)
return nullptr;
}

void WASI::test(ExecutionState& state, Value* argv, Value* result, void* data)
{
printf("No argument test succesful.\n");
}

void WASI::printI32(ExecutionState& state, Value* argv, Value* result, void* data)
{
printf("Recieved number: %d.\n", argv[0].asI32());
}

void WASI::writeI32(ExecutionState& state, Value* argv, Value* result, void* data)
{
printf("Writing 42 to stack.\n");
result[0] = Value((int32_t)42);
}

void WASI::proc_exit(ExecutionState& state, Value* argv, Value* result, void* data)
{
ASSERT(argv[0].type() == Value::I32);
Expand Down
96 changes: 90 additions & 6 deletions src/wasi/Wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,96 @@ namespace Walrus {

class WASI {
public:
// type definitions according to preview1
// https://github.com/WebAssembly/WASI/blob/main/legacy/preview1/docs.md

#define ERRORS(ERR) \
ERR(success, "no error") \
ERR(toobig, "argument list too long") \
ERR(acces, "permission denied") \
ERR(addrinuse, "addres in use") \
ERR(addrnotavauil, "addres not abailable") \
ERR(afnosupport, "address family not supported") \
ERR(again, "resource unavailable, or operation would block") \
ERR(already, "connection already in progress") \
ERR(badf, "bad file descriptor") \
ERR(badmsg, "bad message") \
ERR(busy, "device or resource busy") \
ERR(canceled, "operation canceled") \
ERR(child, "no child processes") \
ERR(connaborted, "connection aborted") \
ERR(connrefused, "connection refused") \
ERR(connresset, "connection reset") \
ERR(deadlk, "resource deadlock would occur") \
ERR(destaddrreq, "destination address required") \
ERR(dom, "mathematics argument out of domain of function") \
ERR(dquot, "reserved") \
ERR(exist, "file exists") \
ERR(fault, "bad address") \
ERR(fbig, "file too large") \
ERR(hostunreach, "host unreachable") \
ERR(idrm, "identifier removed") \
ERR(ilseq, "illegal byte sequence") \
ERR(inprogress, "operation in progress") \
ERR(intr, "interrupted function") \
ERR(inval, "invalid function argument") \
ERR(io, "io error") \
ERR(isconn, "socket is connected") \
ERR(isdir, "is a directory") \
ERR(loop, "too many levels of symbolic links") \
ERR(mfile, "file descriptor value too large") \
ERR(mlink, "too many links") \
ERR(msgsize, "message too large") \
ERR(multihop, "reserved") \
ERR(nametoolong, "filename too long") \
ERR(netdown, "network is down") \
ERR(netreset, "connection aborted by network") \
ERR(netunreach, "network unreachable") \
ERR(nfile, "too many files open in system") \
ERR(nobufs, "no buffer space available") \
ERR(nodev, "no such device") \
ERR(noent, "no such file or directory") \
ERR(noexec, "executable file format error") \
ERR(nolck, "no locks available") \
ERR(nolink, "reserved") \
ERR(nomem, "not enough space") \
ERR(nomsg, "no message of the desired type") \
ERR(noprotoopt, "protocol not available") \
ERR(nospc, "no space left on device") \
ERR(nosys, "function not supported") \
ERR(notconn, "the socket is not connected") \
ERR(notdir, "not a directory or a symbolic link to a directory") \
ERR(notempty, "directory not empty") \
ERR(notrecoverable, "state not recoverable") \
ERR(notsock, "not a socket") \
ERR(notsup, "not supported or operation not supported in socket") \
ERR(notty, "inappropriate I/O control operation") \
ERR(nxio, "no such device or address") \
ERR(overflow, "value too large to be stored in data type") \
ERR(ownerdead, "previous owner died") \
ERR(perm, "operation not permitted") \
ERR(pipe, "broken pipe") \
ERR(proto, "protocol error") \
ERR(protonosupport, "protocol not supported") \
ERR(prototype, "protocol wrong type for socket") \
ERR(range, "result too large") \
ERR(rofst, "read-only file system") \
ERR(spipe, "invalid seek") \
ERR(srch, "no such process") \
ERR(stale, "reserved") \
ERR(timedout, "connection timed out") \
ERR(txtbsy, "text file busy") \
ERR(xdec, "cross-device link") \
ERR(notcapable, "capabilities insufficient")

#define TO_ENUM(ERR, MSG) ERR,
typedef enum wasi_errno : uint16_t {
ERRORS(TO_ENUM)
} wasi_errno_t;
#undef TO_ENUM

// end of type definitions

WASI();

~WASI()
Expand All @@ -37,9 +127,6 @@ class WASI {
};

#define FOR_EACH_WASI_FUNC(F) \
F(test, NONE) \
F(printI32, I32R) \
F(writeI32, RI32) \
F(proc_exit, I32R)

enum WasiFuncName : size_t {
Expand All @@ -52,9 +139,6 @@ class WASI {
void fillWasiFuncTable();
WasiFunc* find(std::string funcName);

static void test(ExecutionState& state, Value* argv, Value* result, void* data);
static void printI32(ExecutionState& state, Value* argv, Value* result, void* data);
static void writeI32(ExecutionState& state, Value* argv, Value* result, void* data);
static void proc_exit(ExecutionState& state, Value* argv, Value* result, void* data);

WasiFunc m_wasiFunctions[FuncEnd];
Expand Down
14 changes: 0 additions & 14 deletions test/wasi/example.wast

This file was deleted.

0 comments on commit b41d5ed

Please sign in to comment.