Skip to content

Commit

Permalink
Add random_get WASI function
Browse files Browse the repository at this point in the history
  • Loading branch information
GorogPeter committed Nov 17, 2023
1 parent ed6d35d commit 5c9658d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/runtime/SpecTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SpecTestFunctionTypes {
NONE = 0,
I32R,
I32_RI32,
I32I32_RI32,
I32I32I32I32_RI32,
RI32,
I64R,
Expand Down Expand Up @@ -67,6 +68,15 @@ class SpecTestFunctionTypes {
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I32I32_RI32
param = new ValueTypeVector();
result = new ValueTypeVector();
param->push_back(Value::Type::I32);
param->push_back(Value::Type::I32);
result->push_back(Value::Type::I32);
m_vector[index++] = new FunctionType(param, result);
}
{
// I32I32I32I32_RI32
param = new ValueTypeVector();
Expand Down
13 changes: 13 additions & 0 deletions src/wasi/Wasi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ void WASI::proc_exit(ExecutionState& state, Value* argv, Value* result, Instance
ASSERT_NOT_REACHED();
}

void WASI::random_get(ExecutionState& state, Value* argv, Value* result, Instance* instance)
{
ASSERT(argv[0].type() == Value::I32);
ASSERT(argv[1].type() == Value::I32);
if (!WASI::checkMemOffset(instance->memory(0), argv[0].asI32(), argv[1].asI32())) {
result[0] = Value(WASI::wasi_errno::inval);
}

void* buf = (void*)(instance->memory(0)->buffer() + argv[0].asI32());
uvwasi_size_t length = argv[1].asI32();
result[0] = Value(uvwasi_random_get(WASI::m_uvwasi, buf, length));
}

void WASI::fillWasiFuncTable()
{
#define WASI_FUNC_TABLE(NAME, FUNCTYPE) \
Expand Down
6 changes: 4 additions & 2 deletions src/wasi/Wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ class WASI {
WasiFunction::WasiFunctionCallback ptr;
};

#define FOR_EACH_WASI_FUNC(F) \
F(proc_exit, I32R) \
#define FOR_EACH_WASI_FUNC(F) \
F(proc_exit, I32R) \
F(random_get, I32I32_RI32) \
F(fd_write, I32I32I32I32_RI32)

enum WasiFuncName : size_t {
Expand All @@ -153,6 +154,7 @@ class WASI {

static void proc_exit(ExecutionState& state, Value* argv, Value* result, Instance* instance);
static void fd_write(ExecutionState& state, Value* argv, Value* result, Instance* instance);
static void random_get(ExecutionState& state, Value* argv, Value* result, Instance* instance);

static WasiFunc m_wasiFunctions[FuncEnd];
static uvwasi_t* m_uvwasi;
Expand Down
10 changes: 10 additions & 0 deletions test/wasi/random_get.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(module
(import "wasi_snapshot_preview1" "random_get" (func $random (param i32 i32) (result i32)))
(memory 1)
(export "_start" (func $_start))
(func $_start (result i32)
(call $random (i32.const 10) (i32.const 5))
)
)

(assert_return (invoke "_start") (i32.const 0))

0 comments on commit 5c9658d

Please sign in to comment.