Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use witx tagged unions #165

Merged
merged 10 commits into from
Feb 24, 2020
48 changes: 18 additions & 30 deletions libc-bottom-half/cloudlibc/src/libc/poll/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,17 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
__wasi_subscription_t *subscription = &subscriptions[nevents++];
*subscription = (__wasi_subscription_t){
.userdata = (uintptr_t)pollfd,
.type = __WASI_EVENTTYPE_FD_READ,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.fd_readwrite.fd = pollfd->fd,
.fd_readwrite.flags = __WASI_SUBSCRIPTION_FD_READWRITE_POLL,
#else
.u.fd_readwrite.file_descriptor = pollfd->fd,
#endif
.u.tag = __WASI_EVENTTYPE_FD_READ,
.u.u.fd_read.file_descriptor = pollfd->fd,
};
created_events = true;
}
if ((pollfd->events & POLLWRNORM) != 0) {
__wasi_subscription_t *subscription = &subscriptions[nevents++];
*subscription = (__wasi_subscription_t){
.userdata = (uintptr_t)pollfd,
.type = __WASI_EVENTTYPE_FD_WRITE,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.fd_readwrite.fd = pollfd->fd,
.fd_readwrite.flags = __WASI_SUBSCRIPTION_FD_READWRITE_POLL,
#else
.u.fd_readwrite.file_descriptor = pollfd->fd,
#endif
.u.tag = __WASI_EVENTTYPE_FD_WRITE,
.u.u.fd_write.file_descriptor = pollfd->fd,
};
created_events = true;
}
Expand All @@ -59,14 +49,9 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
if (timeout >= 0) {
__wasi_subscription_t *subscription = &subscriptions[nevents++];
*subscription = (__wasi_subscription_t){
.type = __WASI_EVENTTYPE_CLOCK,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.clock.clock_id = __WASI_CLOCK_REALTIME,
.clock.timeout = (__wasi_timestamp_t)timeout * 1000000,
#else
.u.clock.id = __WASI_CLOCKID_REALTIME,
.u.clock.timeout = (__wasi_timestamp_t)timeout * 1000000,
#endif
.u.tag = __WASI_EVENTTYPE_CLOCK,
.u.u.clock.id = __WASI_CLOCKID_REALTIME,
.u.u.clock.timeout = (__wasi_timestamp_t)timeout * 1000000,
};
}

Expand Down Expand Up @@ -114,14 +99,17 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
pollfd->revents |= POLLERR;
} else {
// Data can be read or written.
pollfd->revents |=
event->type == __WASI_EVENTTYPE_FD_READ ? POLLRDNORM : POLLWRNORM;
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
if (event->fd_readwrite.flags & __WASI_EVENT_FD_READWRITE_HANGUP)
#else
if (event->u.fd_readwrite.flags & __WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP)
#endif
pollfd->revents |= POLLHUP;
if (event->type == __WASI_EVENTTYPE_FD_READ) {
pollfd->revents |= POLLRDNORM;
if (event->fd_readwrite.flags & __WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP) {
pollfd->revents |= POLLHUP;
}
} else if (event->type == __WASI_EVENTTYPE_FD_WRITE) {
pollfd->revents |= POLLWRNORM;
if (event->fd_readwrite.flags & __WASI_EVENTRWFLAGS_FD_READWRITE_HANGUP) {
pollfd->revents |= POLLHUP;
}
}
}
}
}
Expand Down
21 changes: 4 additions & 17 deletions libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@ int ioctl(int fildes, int request, ...) {
// Poll the file descriptor to determine how many bytes can be read.
__wasi_subscription_t subscriptions[2] = {
{
.type = __WASI_EVENTTYPE_FD_READ,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.fd_readwrite.fd = fildes,
.fd_readwrite.flags = __WASI_SUBSCRIPTION_FD_READWRITE_POLL,
#else
.u.fd_readwrite.file_descriptor = fildes,
#endif
.u.tag = __WASI_EVENTTYPE_FD_READ,
.u.u.fd_read.file_descriptor = fildes,
},
{
.type = __WASI_EVENTTYPE_CLOCK,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.clock.clock_id = __WASI_CLOCK_MONOTONIC,
#else
.u.clock.id = __WASI_CLOCKID_MONOTONIC,
#endif
.u.tag = __WASI_EVENTTYPE_CLOCK,
.u.u.clock.id = __WASI_CLOCKID_MONOTONIC,
},
};
__wasi_event_t events[__arraycount(subscriptions)];
Expand Down Expand Up @@ -58,11 +49,7 @@ int ioctl(int fildes, int request, ...) {
return -1;
}
if (event->type == __WASI_EVENTTYPE_FD_READ) {
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
*result = event->fd_readwrite.nbytes;
#else
*result = event->u.fd_readwrite.nbytes;
#endif
return 0;
}
}
Expand Down
32 changes: 7 additions & 25 deletions libc-bottom-half/cloudlibc/src/libc/sys/select/pselect.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
__wasi_subscription_t *subscription = &subscriptions[nevents++];
*subscription = (__wasi_subscription_t){
.userdata = fd,
.type = __WASI_EVENTTYPE_FD_READ,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.fd_readwrite.fd = fd,
.fd_readwrite.flags = __WASI_SUBSCRIPTION_FD_READWRITE_POLL,
#else
.u.fd_readwrite.file_descriptor = fd,
#endif
.u.tag = __WASI_EVENTTYPE_FD_READ,
.u.u.fd_read.file_descriptor = fd,
};
}
}
Expand All @@ -67,13 +62,8 @@ int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
__wasi_subscription_t *subscription = &subscriptions[nevents++];
*subscription = (__wasi_subscription_t){
.userdata = fd,
.type = __WASI_EVENTTYPE_FD_WRITE,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.fd_readwrite.fd = fd,
.fd_readwrite.flags = __WASI_SUBSCRIPTION_FD_READWRITE_POLL,
#else
.u.fd_readwrite.file_descriptor = fd,
#endif
.u.tag = __WASI_EVENTTYPE_FD_WRITE,
.u.u.fd_write.file_descriptor = fd,
};
}
}
Expand All @@ -82,18 +72,10 @@ int pselect(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
if (timeout != NULL) {
__wasi_subscription_t *subscription = &subscriptions[nevents++];
*subscription = (__wasi_subscription_t){
.type = __WASI_EVENTTYPE_CLOCK,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.clock.clock_id = __WASI_CLOCK_REALTIME,
#else
.u.clock.id = __WASI_CLOCKID_REALTIME,
#endif
.u.tag = __WASI_EVENTTYPE_CLOCK,
.u.u.clock.id = __WASI_CLOCKID_REALTIME,
};
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
if (!timespec_to_timestamp_clamp(timeout, &subscription->clock.timeout)) {
#else
if (!timespec_to_timestamp_clamp(timeout, &subscription->u.clock.timeout)) {
#endif
if (!timespec_to_timestamp_clamp(timeout, &subscription->u.u.clock.timeout)) {
errno = EINVAL;
return -1;
}
Expand Down
17 changes: 4 additions & 13 deletions libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,11 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,

// Prepare polling subscription.
__wasi_subscription_t sub = {
.type = __WASI_EVENTTYPE_CLOCK,
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
.clock.clock_id = clock_id->id,
.clock.flags = flags,
#else
.u.clock.id = clock_id->id,
.u.clock.flags = flags,
#endif
.u.tag = __WASI_EVENTTYPE_CLOCK,
.u.u.clock.id = clock_id->id,
.u.u.clock.flags = flags,
};
#ifdef __wasilibc_unmodified_upstream // non-anonymous unions
if (!timespec_to_timestamp_clamp(rqtp, &sub.clock.timeout))
#else
if (!timespec_to_timestamp_clamp(rqtp, &sub.u.clock.timeout))
#endif
if (!timespec_to_timestamp_clamp(rqtp, &sub.u.u.clock.timeout))
return EINVAL;

// Block until polling event is triggered.
Expand Down
83 changes: 23 additions & 60 deletions libc-bottom-half/headers/public/wasi/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1166,20 +1166,6 @@ _Static_assert(_Alignof(__wasi_event_fd_readwrite_t) == 8, "witx calculated alig
_Static_assert(offsetof(__wasi_event_fd_readwrite_t, nbytes) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_event_fd_readwrite_t, flags) == 8, "witx calculated offset");

/**
* The contents of an $event.
*/
typedef union __wasi_event_u_t {
/**
* When type is `eventtype::fd_read` or `eventtype::fd_write`:
*/
__wasi_event_fd_readwrite_t fd_readwrite;

} __wasi_event_u_t;

_Static_assert(sizeof(__wasi_event_u_t) == 16, "witx calculated size");
_Static_assert(_Alignof(__wasi_event_u_t) == 8, "witx calculated align");

/**
* An event that occurred.
*/
Expand All @@ -1195,14 +1181,15 @@ typedef struct __wasi_event_t {
__wasi_errno_t error;

/**
* The type of the event that occurred.
* The type of event that occured
*/
__wasi_eventtype_t type;

/**
* The contents of the event.
* The contents of the event, if it is an `eventtype::fd_read` or
* `eventtype::fd_write`. `eventtype::clock` events ignore this field.
*/
__wasi_event_u_t u;
__wasi_event_fd_readwrite_t fd_readwrite;

} __wasi_event_t;

Expand All @@ -1211,7 +1198,7 @@ _Static_assert(_Alignof(__wasi_event_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_event_t, userdata) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_event_t, error) == 8, "witx calculated offset");
_Static_assert(offsetof(__wasi_event_t, type) == 10, "witx calculated offset");
_Static_assert(offsetof(__wasi_event_t, u) == 16, "witx calculated offset");
_Static_assert(offsetof(__wasi_event_t, fd_readwrite) == 16, "witx calculated offset");

/**
* Flags determining how to interpret the timestamp provided in
Expand Down Expand Up @@ -1284,21 +1271,21 @@ _Static_assert(offsetof(__wasi_subscription_fd_readwrite_t, file_descriptor) ==
/**
* The contents of a $subscription.
*/
typedef union __wasi_subscription_u_t {
/**
* When type is `eventtype::clock`:
*/
typedef union __wasi_subscription_u_u_t {
__wasi_subscription_clock_t clock;

/**
* When type is `eventtype::fd_read` or `eventtype::fd_write`:
*/
__wasi_subscription_fd_readwrite_t fd_readwrite;

__wasi_subscription_fd_readwrite_t fd_read;
__wasi_subscription_fd_readwrite_t fd_write;
} __wasi_subscription_u_u_t;
typedef struct __wasi_subscription_u_t {
__wasi_eventtype_t tag;
__wasi_subscription_u_u_t u;
} __wasi_subscription_u_t;

_Static_assert(sizeof(__wasi_subscription_u_t) == 32, "witx calculated size");
_Static_assert(sizeof(__wasi_subscription_u_t) == 40, "witx calculated size");
_Static_assert(_Alignof(__wasi_subscription_u_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_subscription_u_t, u) == 8, "witx calculated union offset");
_Static_assert(sizeof(__wasi_subscription_u_u_t) == 32, "witx calculated union size");
_Static_assert(_Alignof(__wasi_subscription_u_u_t) == 8, "witx calculated union align");

/**
* Subscription to an event.
Expand All @@ -1311,12 +1298,7 @@ typedef struct __wasi_subscription_t {
__wasi_userdata_t userdata;

/**
* The type of the event to which to subscribe.
*/
__wasi_eventtype_t type;

/**
* The contents of the subscription.
* The type of the event to which to subscribe, and its contents
*/
__wasi_subscription_u_t u;

Expand All @@ -1325,8 +1307,7 @@ typedef struct __wasi_subscription_t {
_Static_assert(sizeof(__wasi_subscription_t) == 48, "witx calculated size");
_Static_assert(_Alignof(__wasi_subscription_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_subscription_t, userdata) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_subscription_t, type) == 8, "witx calculated offset");
_Static_assert(offsetof(__wasi_subscription_t, u) == 16, "witx calculated offset");
_Static_assert(offsetof(__wasi_subscription_t, u) == 8, "witx calculated offset");

/**
* Exit code generated by a process when exiting.
Expand Down Expand Up @@ -1617,39 +1598,21 @@ _Static_assert(_Alignof(__wasi_prestat_dir_t) == 4, "witx calculated align");
_Static_assert(offsetof(__wasi_prestat_dir_t, pr_name_len) == 0, "witx calculated offset");

/**
* The contents of an $prestat.
* Information about a pre-opened capability.
*/
typedef union __wasi_prestat_u_t {
/**
* When type is `preopentype::dir`:
*/
__wasi_prestat_dir_t dir;

} __wasi_prestat_u_t;

_Static_assert(sizeof(__wasi_prestat_u_t) == 4, "witx calculated size");
_Static_assert(_Alignof(__wasi_prestat_u_t) == 4, "witx calculated align");

/**
* Information about a pre-opened capability.
*/
typedef struct __wasi_prestat_t {
/**
* The type of the pre-opened capability.
*/
__wasi_preopentype_t pr_type;

/**
* The contents of the information.
*/
__wasi_preopentype_t tag;
__wasi_prestat_u_t u;

} __wasi_prestat_t;

_Static_assert(sizeof(__wasi_prestat_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_prestat_t) == 4, "witx calculated align");
_Static_assert(offsetof(__wasi_prestat_t, pr_type) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_prestat_t, u) == 4, "witx calculated offset");
_Static_assert(offsetof(__wasi_prestat_t, u) == 4, "witx calculated union offset");
_Static_assert(sizeof(__wasi_prestat_u_t) == 4, "witx calculated union size");
_Static_assert(_Alignof(__wasi_prestat_u_t) == 4, "witx calculated union align");

/**
* @defgroup wasi_snapshot_preview1
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/libpreopen/libpreopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ __wasilibc_populate_libpreopen(void)
break;
if (ret != __WASI_ERRNO_SUCCESS)
goto oserr;
switch (prestat.pr_type) {
switch (prestat.tag) {
case __WASI_PREOPENTYPE_DIR: {
char *path = malloc(prestat.u.dir.pr_name_len + 1);
if (path == NULL)
Expand Down
Loading