Skip to content

Commit

Permalink
Export wasi_random_get(). (#68)
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora authored Oct 16, 2020
1 parent 4636486 commit 6a75b62
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
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
13 changes: 13 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,17 @@ 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);
RAND_bytes(random.data(), random.size());
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

0 comments on commit 6a75b62

Please sign in to comment.