Skip to content

Commit 39e65a4

Browse files
committed
Port clocks and filesystems functions to use native wasip2 operations
At this point, there should be no further uses of the preview1 component adapter for clocks and filesystems methods when __wasilibc_use_wasip2 is defined. Also added a DEBUG option to the Makefile that enables -O0/g when DEBUG=true is passed in.
1 parent 93bd1f6 commit 39e65a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+4020
-45
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ NM ?= $(patsubst %clang,%llvm-nm,$(filter-out ccache sccache,$(CC)))
88
ifeq ($(origin AR), default)
99
AR = $(patsubst %clang,%llvm-ar,$(filter-out ccache sccache,$(CC)))
1010
endif
11+
ifeq ($(DEBUG), true)
12+
EXTRA_CFLAGS ?= -O0 -g
13+
else
1114
EXTRA_CFLAGS ?= -O2 -DNDEBUG
15+
endif
1216
# The directory where we build the sysroot.
1317
SYSROOT ?= $(CURDIR)/sysroot
1418
# A directory to install to for "make install".

expected/wasm32-wasip2/defined-symbols.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ ctime_r
566566
descriptor_table_get_ref
567567
descriptor_table_insert
568568
descriptor_table_remove
569+
descriptor_table_update
569570
difftime
570571
dirfd
571572
dirname
@@ -574,6 +575,8 @@ dprintf
574575
drand48
575576
drem
576577
dremf
578+
drop_directory_stream
579+
drop_file_handle
577580
drop_tcp_socket
578581
drop_udp_socket
579582
drop_udp_socket_streams

libc-bottom-half/clocks/clock.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
#define _WASI_EMULATED_PROCESS_CLOCKS
22
#include <time.h>
3+
#ifdef __wasilibc_use_wasip2
4+
#include <wasi/wasip2.h>
5+
#else
36
#include <wasi/api.h>
7+
#endif
48
#include <common/time.h>
59

610
_Static_assert(
711
CLOCKS_PER_SEC == NSEC_PER_SEC,
812
"This implementation assumes that `clock` is in nanoseconds"
913
);
1014

15+
#ifdef __wasilibc_use_wasip2
16+
// Snapshot of the monotonic clock at the start of the program.
17+
static wall_clock_datetime_t start;
18+
19+
// Use a priority of 10 to run fairly early in the implementation-reserved
20+
// constructor priority range.
21+
__attribute__((constructor(10)))
22+
static void init(void) {
23+
wall_clock_now(&start);
24+
}
25+
26+
// Define the libc symbol as `__clock` so that we can reliably call it
27+
// from elsewhere in libc.
28+
clock_t __clock(void) {
29+
// Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have
30+
// an inherent concept of a process. Note that this means we'll incorrectly
31+
// include time from other processes, so this function is only declared by
32+
// the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined.
33+
wall_clock_datetime_t now;
34+
wall_clock_now(&now);
35+
now.seconds -= start.seconds;
36+
now.nanoseconds -= start.nanoseconds;
37+
return (now.seconds + (now.nanoseconds * NSEC_PER_SEC));
38+
}
39+
#else
40+
1141
// Snapshot of the monotonic clock at the start of the program.
1242
static __wasi_timestamp_t start;
1343

@@ -29,6 +59,7 @@ clock_t __clock(void) {
2959
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
3060
return now - start;
3161
}
62+
#endif
3263

3364
// Define a user-visible alias as a weak symbol.
3465
__attribute__((__weak__, __alias__("__clock")))

libc-bottom-half/clocks/getrusage.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
#include <sys/resource.h>
33
#include <errno.h>
44
#include <time.h>
5+
#ifdef __wasilibc_use_wasip2
6+
#include <wasi/wasip2.h>
7+
#else
58
#include <wasi/api.h>
9+
#endif
610
#include <common/time.h>
711

812
// `clock` is a weak symbol so that application code can override it.
@@ -12,10 +16,17 @@ clock_t __clock(void);
1216
int getrusage(int who, struct rusage *r_usage) {
1317
switch (who) {
1418
case RUSAGE_SELF: {
19+
#ifdef __wasilibc_use_wasip2
20+
clock_t usertime = __clock();
21+
*r_usage = (struct rusage) {
22+
.ru_utime = instant_to_timeval(usertime)
23+
};
24+
#else
1525
__wasi_timestamp_t usertime = __clock();
1626
*r_usage = (struct rusage) {
1727
.ru_utime = timestamp_to_timeval(usertime)
1828
};
29+
#endif
1930
return 0;
2031
}
2132
case RUSAGE_CHILDREN:
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#ifdef __wasilibc_use_wasip2
2+
#include <wasi/wasip2.h>
3+
#include <errno.h>
4+
5+
static void translate_error(filesystem_error_code_t error) {
6+
switch (error) {
7+
case FILESYSTEM_ERROR_CODE_ACCESS:
8+
errno = __WASI_ERRNO_ACCES;
9+
break;
10+
case FILESYSTEM_ERROR_CODE_WOULD_BLOCK:
11+
errno = __WASI_ERRNO_AGAIN;
12+
break;
13+
case FILESYSTEM_ERROR_CODE_ALREADY:
14+
errno = __WASI_ERRNO_ALREADY;
15+
break;
16+
case FILESYSTEM_ERROR_CODE_BAD_DESCRIPTOR:
17+
errno = __WASI_ERRNO_BADF;
18+
break;
19+
case FILESYSTEM_ERROR_CODE_BUSY:
20+
errno = __WASI_ERRNO_BUSY;
21+
break;
22+
case FILESYSTEM_ERROR_CODE_DEADLOCK:
23+
errno = __WASI_ERRNO_DEADLK;
24+
break;
25+
case FILESYSTEM_ERROR_CODE_QUOTA:
26+
errno = __WASI_ERRNO_DQUOT;
27+
break;
28+
case FILESYSTEM_ERROR_CODE_EXIST:
29+
errno = __WASI_ERRNO_EXIST;
30+
break;
31+
case FILESYSTEM_ERROR_CODE_FILE_TOO_LARGE:
32+
errno = __WASI_ERRNO_FBIG;
33+
break;
34+
case FILESYSTEM_ERROR_CODE_ILLEGAL_BYTE_SEQUENCE:
35+
errno = __WASI_ERRNO_ILSEQ;
36+
break;
37+
case FILESYSTEM_ERROR_CODE_IN_PROGRESS:
38+
errno = __WASI_ERRNO_INPROGRESS;
39+
break;
40+
case FILESYSTEM_ERROR_CODE_INTERRUPTED:
41+
errno = __WASI_ERRNO_INTR;
42+
break;
43+
case FILESYSTEM_ERROR_CODE_INVALID:
44+
errno = __WASI_ERRNO_INVAL;
45+
break;
46+
case FILESYSTEM_ERROR_CODE_IO:
47+
errno = __WASI_ERRNO_IO;
48+
break;
49+
case FILESYSTEM_ERROR_CODE_IS_DIRECTORY:
50+
errno = __WASI_ERRNO_ISDIR;
51+
break;
52+
case FILESYSTEM_ERROR_CODE_LOOP:
53+
errno = __WASI_ERRNO_LOOP;
54+
break;
55+
case FILESYSTEM_ERROR_CODE_TOO_MANY_LINKS:
56+
errno = __WASI_ERRNO_MLINK;
57+
break;
58+
case FILESYSTEM_ERROR_CODE_MESSAGE_SIZE:
59+
errno = __WASI_ERRNO_MSGSIZE;
60+
break;
61+
case FILESYSTEM_ERROR_CODE_NAME_TOO_LONG:
62+
errno = __WASI_ERRNO_NAMETOOLONG;
63+
break;
64+
case FILESYSTEM_ERROR_CODE_NO_DEVICE:
65+
errno = __WASI_ERRNO_NODEV;
66+
break;
67+
case FILESYSTEM_ERROR_CODE_NO_ENTRY:
68+
errno = __WASI_ERRNO_NOENT;
69+
break;
70+
case FILESYSTEM_ERROR_CODE_NO_LOCK:
71+
errno = __WASI_ERRNO_NOLCK;
72+
break;
73+
case FILESYSTEM_ERROR_CODE_INSUFFICIENT_MEMORY:
74+
errno = __WASI_ERRNO_NOMEM;
75+
break;
76+
case FILESYSTEM_ERROR_CODE_INSUFFICIENT_SPACE:
77+
errno = __WASI_ERRNO_NOSPC;
78+
break;
79+
case FILESYSTEM_ERROR_CODE_NOT_DIRECTORY:
80+
errno = __WASI_ERRNO_NOTDIR;
81+
break;
82+
case FILESYSTEM_ERROR_CODE_NOT_EMPTY:
83+
errno = __WASI_ERRNO_NOTEMPTY;
84+
break;
85+
case FILESYSTEM_ERROR_CODE_NOT_RECOVERABLE:
86+
errno = __WASI_ERRNO_NOTRECOVERABLE;
87+
break;
88+
case FILESYSTEM_ERROR_CODE_UNSUPPORTED:
89+
errno = __WASI_ERRNO_NOTSUP;
90+
break;
91+
case FILESYSTEM_ERROR_CODE_NO_TTY:
92+
errno = __WASI_ERRNO_NOTTY;
93+
break;
94+
case FILESYSTEM_ERROR_CODE_NO_SUCH_DEVICE:
95+
errno = __WASI_ERRNO_NXIO;
96+
break;
97+
case FILESYSTEM_ERROR_CODE_OVERFLOW:
98+
errno = __WASI_ERRNO_OVERFLOW;
99+
break;
100+
case FILESYSTEM_ERROR_CODE_NOT_PERMITTED:
101+
errno = __WASI_ERRNO_PERM;
102+
break;
103+
case FILESYSTEM_ERROR_CODE_PIPE:
104+
errno = __WASI_ERRNO_PIPE;
105+
break;
106+
case FILESYSTEM_ERROR_CODE_READ_ONLY:
107+
errno = __WASI_ERRNO_ROFS;
108+
break;
109+
case FILESYSTEM_ERROR_CODE_INVALID_SEEK:
110+
errno = __WASI_ERRNO_SPIPE;
111+
break;
112+
case FILESYSTEM_ERROR_CODE_TEXT_FILE_BUSY:
113+
errno = __WASI_ERRNO_TXTBSY;
114+
break;
115+
case FILESYSTEM_ERROR_CODE_CROSS_DEVICE:
116+
errno = __WASI_ERRNO_XDEV;
117+
break;
118+
default:
119+
abort(); // Unreachable
120+
}
121+
}
122+
#endif

libc-bottom-half/cloudlibc/src/common/time.h

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99

1010
#include <sys/time.h>
1111

12+
#ifdef __wasilibc_use_wasip2
13+
#include <wasi/wasip2.h>
14+
#else
1215
#include <wasi/api.h>
16+
#endif
1317
#include <stdbool.h>
1418
#include <time.h>
1519

1620
#define NSEC_PER_SEC 1000000000
1721

1822
static inline bool timespec_to_timestamp_exact(
19-
const struct timespec *timespec, __wasi_timestamp_t *timestamp) {
23+
#ifdef __wasilibc_use_wasip2
24+
const struct timespec *timespec, wall_clock_datetime_t *timestamp) {
25+
#else
26+
const struct timespec *timespec, __wasi_timestamp_t *timestamp) {
27+
#endif
2028
// Invalid nanoseconds field.
2129
if (timespec->tv_nsec < 0 || timespec->tv_nsec >= NSEC_PER_SEC)
2230
return false;
@@ -25,39 +33,86 @@ static inline bool timespec_to_timestamp_exact(
2533
if (timespec->tv_sec < 0)
2634
return false;
2735

36+
#ifdef __wasilibc_use_wasip2
37+
timestamp->seconds = timespec->tv_sec;
38+
timestamp->nanoseconds = timespec->tv_nsec;
39+
return true;
40+
#else
2841
// Make sure our timestamp does not overflow.
2942
return !__builtin_mul_overflow(timespec->tv_sec, NSEC_PER_SEC, timestamp) &&
3043
!__builtin_add_overflow(*timestamp, timespec->tv_nsec, timestamp);
44+
#endif
3145
}
3246

3347
static inline bool timespec_to_timestamp_clamp(
34-
const struct timespec *timespec, __wasi_timestamp_t *timestamp) {
48+
#ifdef __wasilibc_use_wasip2
49+
const struct timespec *timespec, wall_clock_datetime_t *timestamp) {
50+
#else
51+
const struct timespec *timespec, __wasi_timestamp_t *timestamp) {
52+
#endif
3553
// Invalid nanoseconds field.
3654
if (timespec->tv_nsec < 0 || timespec->tv_nsec >= NSEC_PER_SEC)
3755
return false;
3856

3957
if (timespec->tv_sec < 0) {
4058
// Timestamps before the Epoch are not supported.
41-
*timestamp = 0;
59+
#if __wasilibc_use_wasip2
60+
timestamp->seconds = 0;
61+
timestamp->nanoseconds = 0;
62+
} else {
63+
timestamp->seconds = timespec->tv_sec;
64+
timestamp->nanoseconds = timespec->tv_nsec;
65+
#else
66+
*timestamp = 0;
4267
} else if (__builtin_mul_overflow(timespec->tv_sec, NSEC_PER_SEC, timestamp) ||
4368
__builtin_add_overflow(*timestamp, timespec->tv_nsec, timestamp)) {
4469
// Make sure our timestamp does not overflow.
4570
*timestamp = NUMERIC_MAX(__wasi_timestamp_t);
71+
#endif
4672
}
4773
return true;
4874
}
4975

76+
#ifdef __wasilibc_use_wasip2
77+
static inline struct timespec timestamp_to_timespec(
78+
wall_clock_datetime_t *timestamp) {
79+
return (struct timespec){.tv_sec = timestamp->seconds,
80+
.tv_nsec = timestamp->nanoseconds};
81+
}
82+
#else
5083
static inline struct timespec timestamp_to_timespec(
51-
__wasi_timestamp_t timestamp) {
84+
__wasi_timestamp_t timestamp) {
5285
// Decompose timestamp into seconds and nanoseconds.
5386
return (struct timespec){.tv_sec = timestamp / NSEC_PER_SEC,
5487
.tv_nsec = timestamp % NSEC_PER_SEC};
5588
}
89+
#endif
90+
91+
#ifdef __wasilibc_use_wasip2
92+
static inline struct timespec instant_to_timespec(
93+
monotonic_clock_instant_t ns) {
94+
// Decompose instant into seconds and nanoseconds
95+
return (struct timespec){.tv_sec = ns / NSEC_PER_SEC,
96+
.tv_nsec = ns % NSEC_PER_SEC};
97+
}
98+
99+
static inline struct timeval instant_to_timeval(
100+
monotonic_clock_instant_t ns) {
101+
// Decompose instant into seconds and microoseconds
102+
return (struct timeval){.tv_sec = ns / 1000,
103+
.tv_usec = ns % 1000};
104+
}
56105

57106
static inline struct timeval timestamp_to_timeval(
58-
__wasi_timestamp_t timestamp) {
107+
wall_clock_datetime_t *timestamp) {
108+
return (struct timeval){.tv_sec = timestamp->seconds,
109+
.tv_usec = timestamp->nanoseconds / 1000};
110+
}
111+
#else
112+
static inline struct timeval timestamp_to_timeval(
113+
__wasi_timestamp_t timestamp) {
59114
struct timespec ts = timestamp_to_timespec(timestamp);
60115
return (struct timeval){.tv_sec = ts.tv_sec, ts.tv_nsec / 1000};
61116
}
62-
117+
#endif
63118
#endif

0 commit comments

Comments
 (0)