Skip to content

Commit b33c5fc

Browse files
Port clocks and filesystems functions to use native wasip2 operations (#606)
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. --------- Co-authored-by: Pat Hickey <[email protected]>
1 parent bbb0c01 commit b33c5fc

Some content is hidden

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

88 files changed

+4412
-286
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
@@ -569,6 +569,7 @@ ctime_r
569569
descriptor_table_get_ref
570570
descriptor_table_insert
571571
descriptor_table_remove
572+
descriptor_table_update
572573
difftime
573574
dirfd
574575
dirname
@@ -577,6 +578,8 @@ dprintf
577578
drand48
578579
drem
579580
dremf
581+
drop_directory_stream
582+
drop_file_handle
580583
drop_tcp_socket
581584
drop_udp_socket
582585
drop_udp_socket_streams

expected/wasm32-wasip2/undefined-symbols.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ __subtf3
6767
__trunctfdf2
6868
__trunctfsf2
6969
__unordtf2
70-
__wasi_preview1_adapter_close_badfd
71-
__wasi_preview1_adapter_open_badfd
7270
__wasm_call_ctors
7371
__wasm_import_environment_get_arguments
7472
__wasm_import_environment_get_environment

libc-bottom-half/clocks/clock.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
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 monotonic_clock_instant_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+
start = monotonic_clock_now();
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+
monotonic_clock_instant_t now = monotonic_clock_now();
34+
return now - start;
35+
}
36+
#else
37+
1138
// Snapshot of the monotonic clock at the start of the program.
1239
static __wasi_timestamp_t start;
1340

@@ -29,6 +56,7 @@ clock_t __clock(void) {
2956
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now);
3057
return now - start;
3158
}
59+
#endif
3260

3361
// Define a user-visible alias as a weak symbol.
3462
__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:

libc-bottom-half/clocks/times.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#define _WASI_EMULATED_PROCESS_CLOCKS
22
#include <time.h>
33
#include <sys/times.h>
4+
#ifdef __wasilibc_use_wasip2
5+
#include <wasi/wasip2.h>
6+
#else
47
#include <wasi/api.h>
8+
#endif
59
#include <common/time.h>
610

711
_Static_assert(
@@ -14,6 +18,17 @@ _Static_assert(
1418
clock_t __clock(void);
1519

1620
clock_t times(struct tms *buffer) {
21+
#ifdef __wasilibc_use_wasip2
22+
clock_t user = __clock();
23+
*buffer = (struct tms){
24+
.tms_utime = user,
25+
// WASI doesn't provide a way to spawn a new process, so always 0.
26+
.tms_cutime = 0
27+
};
28+
29+
monotonic_clock_instant_t realtime = monotonic_clock_now();
30+
#else
31+
1732
__wasi_timestamp_t user = __clock();
1833
*buffer = (struct tms){
1934
.tms_utime = user,
@@ -23,5 +38,6 @@ clock_t times(struct tms *buffer) {
2338

2439
__wasi_timestamp_t realtime = 0;
2540
(void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
41+
#endif
2642
return realtime;
2743
}
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

0 commit comments

Comments
 (0)