File tree Expand file tree Collapse file tree 3 files changed +80
-1
lines changed
libc-top-half/musl/src/env Expand file tree Collapse file tree 3 files changed +80
-1
lines changed Original file line number Diff line number Diff line change 1
1
#include <errno.h>
2
2
#include <unistd.h>
3
+ #ifdef __wasilibc_use_wasip2
4
+ #include <wasi/wasip2.h>
5
+ #else
3
6
#include <wasi/api.h>
7
+ #endif
4
8
5
9
int __getentropy (void * buffer , size_t len ) {
6
10
if (len > 256 ) {
7
11
errno = EIO ;
8
12
return -1 ;
9
13
}
10
14
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 );
12
27
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 );
13
36
if (r != 0 ) {
14
37
errno = r ;
15
38
return -1 ;
16
39
}
40
+ #endif
17
41
18
42
return 0 ;
19
43
}
Original file line number Diff line number Diff line change @@ -38,12 +38,36 @@ hidden void __stack_chk_fail_local(void);
38
38
weak_alias (__stack_chk_fail , __stack_chk_fail_local );
39
39
40
40
#ifndef __wasilibc_unmodified_upstream
41
+ #ifdef __wasilibc_use_wasip2
42
+ # include <wasi/wasip2.h>
43
+ #else
41
44
# include <wasi/api.h>
45
+ #endif
42
46
43
47
__attribute__((constructor (60 )))
44
48
static void __wasilibc_init_ssp (void ) {
45
49
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
46
69
int r = __wasi_random_get ((uint8_t * )& entropy , sizeof (uintptr_t ));
70
+ #endif
47
71
__init_ssp (r ? NULL : & entropy );
48
72
}
49
73
#endif
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments