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

Export wasi_random_get(). #68

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cc_library(
copts = COPTS,
deps = [
":include",
"@boringssl//:crypto",
"@com_google_protobuf//:protobuf_lite",
"@proxy_wasm_cpp_sdk//:api_lib",
],
Expand Down
7 changes: 7 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ git_repository(
shallow_since = "1565024848 -0700",
)

http_archive(
name = "boringssl",
sha256 = "bb55b0ed2f0cb548b5dce6a6b8307ce37f7f748eb9f1be6bfe2d266ff2b4d52b",
strip_prefix = "boringssl-2192bbc878822cf6ab5977d4257a1339453d9d39",
urls = ["https://github.com/google/boringssl/archive/2192bbc878822cf6ab5977d4257a1339453d9d39.tar.gz"],
)

http_archive(
name = "com_google_googletest",
sha256 = "9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb",
Expand Down
1 change: 1 addition & 0 deletions include/proxy-wasm/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Word wasi_unstable_args_get(void *raw_context, Word argc_ptr, Word argv_buf_size
Word wasi_unstable_args_sizes_get(void *raw_context, Word argc_ptr, Word argv_buf_size_ptr);
void wasi_unstable_proc_exit(void *, Word);
Word wasi_unstable_clock_time_get(void *, Word, uint64_t, Word);
Word wasi_unstable_random_get(void *, Word, Word);
Word pthread_equal(void *, Word left, Word right);

// Support for embedders, not exported to Wasm.
Expand Down
15 changes: 15 additions & 0 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//
#include "include/proxy-wasm/wasm.h"

#include <openssl/rand.h>

#define WASM_CONTEXT(_c) \
(ContextOrEffectiveContext(static_cast<ContextBase *>((void)_c, current_context_)))

Expand Down Expand Up @@ -814,6 +816,19 @@ Word wasi_unstable_clock_time_get(void *raw_context, Word clock_id, uint64_t pre
return 0; // __WASI_ESUCCESS
}

// __wasi_errno_t __wasi_random_get(uint8_t *buf, size_t buf_len);
Word wasi_unstable_random_get(void *raw_context, Word result_buf_ptr, Word buf_len) {
auto context = WASM_CONTEXT(raw_context);
std::vector<uint8_t> random(buf_len);
if (!RAND_bytes(random.data(), random.size())) {
return 21; // __WASI_EFAULT
PiotrSikora marked this conversation as resolved.
Show resolved Hide resolved
}
if (!context->wasmVm()->setMemory(result_buf_ptr, random.size(), random.data())) {
return 21; // __WASI_EFAULT
}
return 0; // __WASI_ESUCCESS
}

// void __wasi_proc_exit(__wasi_exitcode_t rval);
void wasi_unstable_proc_exit(void *raw_context, Word) {
auto context = WASM_CONTEXT(raw_context);
Expand Down
1 change: 1 addition & 0 deletions src/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ void WasmBase::registerCallbacks() {
_REGISTER_WASI(args_get);
_REGISTER_WASI(args_sizes_get);
_REGISTER_WASI(clock_time_get);
_REGISTER_WASI(random_get);
_REGISTER_WASI(proc_exit);
#undef _REGISTER_WASI

Expand Down