Skip to content

Commit

Permalink
fix: Implement getentropy while Emscripten gets patched
Browse files Browse the repository at this point in the history
Issue: emscripten-core/emscripten#22782

Signed-off-by: Martijn Stevenson <[email protected]>
  • Loading branch information
martijneken committed Oct 26, 2024
1 parent 9dc95ae commit c40818b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions proxy_wasm_intrinsics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,30 @@ 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).

int32_t __imported_wasi_snapshot_preview1_random_get(int32_t arg0, int32_t arg1)
__attribute__((__import_module__("wasi_snapshot_preview1"), __import_name__("random_get")));

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) {
int32_t ret = __imported_wasi_snapshot_preview1_random_get((int32_t)buf, (int32_t)buf_len);
return (uint16_t)ret;
}

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 c40818b

Please sign in to comment.