Merge pull request #13 from MaxGraey/improve-wasi

Better handle getrandom. Add wasi_clock_res_get
extensions
Volodymyr Shymanskyy 5 years ago committed by GitHub
commit 66a818a815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,6 +27,10 @@
#include <errno.h>
#include <stdio.h>
//TODO
#define PREOPEN_CNT 3
#define NANOS_PER_SEC 1000000000
typedef uint32_t __wasi_size_t;
struct wasi_iovec
@ -40,9 +44,6 @@ typedef struct Preopen {
unsigned path_len;
} Preopen;
//TODO
#define PREOPEN_CNT 3
Preopen preopen[PREOPEN_CNT] = {
{ .path = "<stdin>", .path_len = 7, },
{ .path = "<stdout>", .path_len = 8, },
@ -260,20 +261,40 @@ uint32_t m3_wasi_unstable_fd_datasync(uint32_t fd)
uint32_t m3_wasi_unstable_random_get(void* buf, __wasi_size_t buflen)
{
getrandom(buf, buflen, GRND_RANDOM);
while (1) {
ssize_t retlen = getrandom(buf, buflen, 0);
if (retlen < 0) {
if (errno == EINTR) { continue; }
return errno_to_wasi(errno);
}
if (retlen == buflen) { return __WASI_ESUCCESS; }
buf = (void *)((uint8_t *)buf + retlen);
buflen -= retlen;
}
}
uint32_t m3_wasi_unstable_clock_res_get(IM3Runtime runtime,
__wasi_clockid_t clock_id,
__wasi_timestamp_t* resolution)
{
struct timespec tp;
if (clock_getres(clock_id, &tp) != 0)
*resolution = 1000000;
else
*resolution = (tp.tv_sec * NANOS_PER_SEC) + tp.tv_nsec;
return __WASI_ESUCCESS;
}
uint32_t m3_wasi_unstable_clock_time_get(IM3Runtime runtime,
__wasi_clockid_t clock_id,
__wasi_timestamp_t precision,
uint32_t m3_wasi_unstable_clock_time_get(IM3Runtime runtime,
__wasi_clockid_t clock_id,
__wasi_timestamp_t precision,
__wasi_timestamp_t* time)
{
struct timespec tp;
clock_gettime(clock_id, &tp);
if (clock_gettime(clock_id, &tp) != 0) { return errno_to_wasi(errno); }
//printf("=== time: %lu.%09u\n", tp.tv_sec, tp.tv_nsec);
*time = (uint64_t)tp.tv_sec * 1000000000 + tp.tv_nsec;
*time = (uint64_t)tp.tv_sec * NANOS_PER_SEC + tp.tv_nsec;
return __WASI_ESUCCESS;
}
@ -317,6 +338,7 @@ _ (SuppressLookupFailure (m3_LinkFunction (module, "fd_close", "i(i)",
_ (SuppressLookupFailure (m3_LinkFunction (module, "random_get", "v(*i)", &m3_wasi_unstable_random_get)));
_ (SuppressLookupFailure (m3_LinkFunction (module, "clock_res_get", "v(Mi*)", &m3_wasi_unstable_clock_res_get)));
_ (SuppressLookupFailure (m3_LinkFunction (module, "clock_time_get", "v(MiI*)", &m3_wasi_unstable_clock_time_get)));
_ (SuppressLookupFailure (m3_LinkFunction (module, "proc_exit", "v(i)", &m3_wasi_unstable_proc_exit)));

Loading…
Cancel
Save