-
Notifications
You must be signed in to change notification settings - Fork 216
Port random numbers to use wasip2 methods #612
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
Merged
pchickey
merged 2 commits into
WebAssembly:main
from
catamorphism:wasip2-porting-random
Aug 13, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#ifdef __wasilibc_use_wasip2 | ||
#include <wasi/wasip2.h> | ||
#include <sysexits.h> | ||
|
||
int __wasilibc_random(void *buffer, size_t len) { | ||
|
||
// Set up a WASI byte list to receive the results | ||
wasip2_list_u8_t wasi_list; | ||
|
||
// Get random bytes | ||
random_get_random_bytes(len, &wasi_list); | ||
|
||
// The spec for get-random-bytes specifies that wasi_list.len | ||
// will be equal to len. | ||
if (wasi_list.len != len) | ||
_Exit(EX_OSERR); | ||
else { | ||
// Copy the result | ||
memcpy(buffer, wasi_list.ptr, len); | ||
} | ||
|
||
// Free the WASI byte list | ||
wasip2_list_u8_free(&wasi_list); | ||
|
||
return 0; | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#ifdef __wasilibc_use_wasip2 | ||
#include <wasi/libc.h> | ||
#else | ||
#include <wasi/api.h> | ||
#endif | ||
|
||
int __getentropy(void *buffer, size_t len) { | ||
if (len > 256) { | ||
errno = EIO; | ||
return -1; | ||
} | ||
|
||
#ifdef __wasilibc_use_wasip2 | ||
int r = __wasilibc_random(buffer, len); | ||
#else | ||
int r = __wasi_random_get(buffer, len); | ||
|
||
#endif | ||
if (r != 0) { | ||
errno = r; | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
weak_alias(__getentropy, getentropy); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! add-flags.py(CFLAGS): -I. | ||
//! add-flags.py(ARGS): foo bar | ||
#include <limits.h> | ||
#include <stdio.h> | ||
#include "test.h" | ||
|
||
#define TEST(c, ...) \ | ||
( (c) || (t_error(#c " failed: " __VA_ARGS__),0) ) | ||
|
||
int main() | ||
{ | ||
size_t len = 256; | ||
uint8_t buffer[len]; | ||
|
||
TEST(getentropy(&buffer, len) == 0, "getentropy() should return 0\n"); | ||
int all_zeroes = 1; | ||
|
||
for (size_t i = 0; i < len; i++) { | ||
if (buffer[i] != 0) { | ||
all_zeroes = 0; | ||
break; | ||
} | ||
} | ||
|
||
TEST(all_zeroes == 0, "getentropy() returned 256 zeroes\n"); | ||
|
||
// More than 256 bytes is an error | ||
TEST(getentropy(&buffer, 257)==-1, "requesting > 256 bytes should be an error\n"); | ||
|
||
return t_status; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little hesitant to handle this error by returning, vs exiting with EX_OSERR or something, since the docs of get-random-bytes guarantee that the wasi_list len will be equal to len. WDYT about exiting instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. Changed in cdcd442