Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random_get WASI function #195

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Comment on lines +75 to +77
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the spec,
random_get function writes a random data into a buffer input.
Does this buffer mean memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes, but I'll ask my co-worker about it to be sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they do mean the memory.

https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md

Regardless of the kind, all modules accessing WASI APIs also export a linear memory with the name memory. Data pointers in WASI API calls are relative to this memory's index space.

Maybe in the future if multiple memory ever becomes a standard then these funcitons may also gain an index to which memory to use.

}

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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't _start be called automatically? I remember there was a discussion about it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is discussed in #187 , but not yet completed