Skip to content

Commit

Permalink
fix: Implement getentropy using wasi preview 1 (#178)
Browse files Browse the repository at this point in the history
* fix: Implement getentropy while Emscripten gets patched

Issue: emscripten-core/emscripten#22782

Signed-off-by: Martijn Stevenson <[email protected]>
  • Loading branch information
martijneken authored Oct 28, 2024
1 parent 28e227c commit a982ad0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions proxy_wasm_intrinsics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,25 @@ extern "C" PROXY_WASM_KEEPALIVE void
proxy_on_foreign_function(uint32_t context_id, uint32_t foreign_function_id, uint32_t data_size) {
getContextBase(context_id)->onForeignFunction(foreign_function_id, data_size);
}

// Patch an Emscripten gap: https://github.com/emscripten-core/emscripten/issues/22782
// Implement getentropy for RNG support (e.g. for absl::random).

typedef uint16_t __wasi_errno_t;
typedef size_t __wasi_size_t;

__wasi_errno_t __wasi_random_get(uint8_t *buf, __wasi_size_t buf_len)
__attribute__((__import_module__("wasi_snapshot_preview1"), __import_name__("random_get")));

extern "C" int getentropy(void *buffer, size_t len) {
if (len > 256) {
errno = EIO;
return -1;
}
int r = __wasi_random_get((uint8_t *)buffer, len);
if (r != 0) {
errno = r;
return -1;
}
return 0;
}

0 comments on commit a982ad0

Please sign in to comment.