Skip to content

Commit 17e6672

Browse files
committed
Port random numbers to use wasip2 methods
1 parent 08799da commit 17e6672

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

libc-bottom-half/sources/getentropy.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
#include <errno.h>
22
#include <unistd.h>
3+
#ifdef __wasilibc_use_wasip2
4+
#include <wasi/wasip2.h>
5+
#else
36
#include <wasi/api.h>
7+
#endif
48

59
int __getentropy(void *buffer, size_t len) {
610
if (len > 256) {
711
errno = EIO;
812
return -1;
913
}
1014

11-
int r = __wasi_random_get(buffer, len);
15+
#ifdef __wasilibc_use_wasip2
16+
// Set up a WASI byte list to receive the results
17+
wasip2_list_u8_t wasi_list;
18+
19+
// Get random bytes
20+
random_get_random_bytes(len, &wasi_list);
21+
22+
// Copy the result into the provided buffer
23+
memcpy(buffer, wasi_list.ptr, wasi_list.len);
24+
25+
// Free the WASI byte list
26+
wasip2_list_u8_free(&wasi_list);
1227

28+
// It's an error if random_get_random_bytes returned
29+
// fewer (or more) bytes than requested
30+
if (wasi_list.len != len) {
31+
errno = EINVAL;
32+
return -1;
33+
}
34+
#else
35+
int r = __wasi_random_get(buffer, len);
1336
if (r != 0) {
1437
errno = r;
1538
return -1;
1639
}
40+
#endif
1741

1842
return 0;
1943
}

libc-top-half/musl/src/env/__stack_chk_fail.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,36 @@ hidden void __stack_chk_fail_local(void);
3838
weak_alias(__stack_chk_fail, __stack_chk_fail_local);
3939

4040
#ifndef __wasilibc_unmodified_upstream
41+
#ifdef __wasilibc_use_wasip2
42+
# include <wasi/wasip2.h>
43+
#else
4144
# include <wasi/api.h>
45+
#endif
4246

4347
__attribute__((constructor(60)))
4448
static void __wasilibc_init_ssp(void) {
4549
uintptr_t entropy;
50+
#ifdef __wasilibc_use_wasip2
51+
int r = 0;
52+
int len = sizeof(uintptr_t);
53+
54+
// Set up a WASI byte list to receive the results
55+
wasip2_list_u8_t wasi_list;
56+
57+
// Get random bytes
58+
random_get_random_bytes(len, &wasi_list);
59+
if (wasi_list.len != len)
60+
r = -1;
61+
else {
62+
// Copy the result
63+
memcpy(&entropy, wasi_list.ptr, len);
64+
}
65+
66+
// Free the WASI byte list
67+
wasip2_list_u8_free(&wasi_list);
68+
#else
4669
int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t));
70+
#endif
4771
__init_ssp(r ? NULL : &entropy);
4872
}
4973
#endif

test/src/misc/getentropy.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! add-flags.py(CFLAGS): -I.
2+
//! add-flags.py(ARGS): foo bar
3+
#include <limits.h>
4+
#include <stdio.h>
5+
#include "test.h"
6+
7+
#define TEST(c, ...) \
8+
( (c) || (t_error(#c " failed: " __VA_ARGS__),0) )
9+
10+
int main()
11+
{
12+
size_t len = 256;
13+
uint8_t buffer[len];
14+
15+
TEST(getentropy(&buffer, len) == 0, "getentropy() should return 0");
16+
int all_zeroes = 1;
17+
18+
for (size_t i = 0; i < len; i++) {
19+
if (buffer[i] != 0) {
20+
all_zeroes = 0;
21+
break;
22+
}
23+
}
24+
25+
TEST(all_zeroes == 0, "getentropy() returned 256 zeroes");
26+
27+
// More than 256 bytes is an error
28+
TEST(getentropy(&buffer, 257)==-1);
29+
30+
return t_status;
31+
}

0 commit comments

Comments
 (0)