Skip to content

Commit 52fbf66

Browse files
committed
Remove fd_dup and change fd_replace to fd_renumber.
This avoids committing core WASI to the "file descriptor" vs "file description" distinction. And `dup` is mainly useful for (a) things that really ought to be done in other ways, and (b) manipulating file descriptors in the proximity of a fork, and wasi doesn't have fork. This also removes the emulated mmap's support for PROT_WRITE, since it depened on dup, however this wasn't very useful.
1 parent a174fb6 commit 52fbf66

14 files changed

Lines changed: 71 additions & 95 deletions

File tree

expected/wasm32-wasi/defined-symbols.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ __uflow
243243
__unlist_locked_file
244244
__uselocale
245245
__utc
246+
__wasilibc_fd_renumber
246247
__wasilibc_register_preopened_fd
247248
__wcscoll_l
248249
__wcsftime_l
@@ -410,8 +411,6 @@ dprintf
410411
drand48
411412
drem
412413
dremf
413-
dup
414-
dup2
415414
duplocale
416415
eaccess
417416
ecvt

expected/wasm32-wasi/predefined-macros.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,6 @@
14551455
#define MON_9 0x20022
14561456
#define MORECTL 1
14571457
#define MOREDATA 2
1458-
#define MQ_PRIO_MAX 32768
14591458
#define MSG_ANY 0x02
14601459
#define MSG_BAND 0x04
14611460
#define MSG_CTRUNC __WASI_SOCK_RECV_FDS_TRUNCATED
@@ -2711,19 +2710,11 @@
27112710
#define _POSIX_FSYNC _POSIX_VERSION
27122711
#define _POSIX_HOST_NAME_MAX 255
27132712
#define _POSIX_IPV6 _POSIX_VERSION
2714-
#define _POSIX_JOB_CONTROL 1
27152713
#define _POSIX_LINK_MAX 8
27162714
#define _POSIX_LOGIN_NAME_MAX 9
2717-
#define _POSIX_MAPPED_FILES _POSIX_VERSION
27182715
#define _POSIX_MAX_CANON 255
27192716
#define _POSIX_MAX_INPUT 255
2720-
#define _POSIX_MEMLOCK _POSIX_VERSION
2721-
#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION
2722-
#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION
2723-
#define _POSIX_MESSAGE_PASSING _POSIX_VERSION
27242717
#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION
2725-
#define _POSIX_MQ_OPEN_MAX 8
2726-
#define _POSIX_MQ_PRIO_MAX 32
27272718
#define _POSIX_NAME_MAX 14
27282719
#define _POSIX_NGROUPS_MAX 8
27292720
#define _POSIX_NO_TRUNC 1
@@ -2736,14 +2727,11 @@
27362727
#define _POSIX_REGEXP 1
27372728
#define _POSIX_RE_DUP_MAX 255
27382729
#define _POSIX_RTSIG_MAX 8
2739-
#define _POSIX_SAVED_IDS 1
27402730
#define _POSIX_SEMAPHORES _POSIX_VERSION
27412731
#define _POSIX_SEM_NSEMS_MAX 256
27422732
#define _POSIX_SEM_VALUE_MAX 32767
27432733
#define _POSIX_SHARED_MEMORY_OBJECTS _POSIX_VERSION
2744-
#define _POSIX_SHELL 1
27452734
#define _POSIX_SIGQUEUE_MAX 32
2746-
#define _POSIX_SPAWN _POSIX_VERSION
27472735
#define _POSIX_SPIN_LOCKS _POSIX_VERSION
27482736
#define _POSIX_SSIZE_MAX 32767
27492737
#define _POSIX_SS_REPL_MAX 4

expected/wasm32-wasi/undefined-symbols.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ __wasi_clock_res_get
2727
__wasi_clock_time_get
2828
__wasi_fd_close
2929
__wasi_fd_datasync
30-
__wasi_fd_dup
3130
__wasi_fd_pread
3231
__wasi_fd_pwrite
3332
__wasi_fd_read
34-
__wasi_fd_replace
33+
__wasi_fd_renumber
3534
__wasi_fd_seek
3635
__wasi_fd_stat_get
3736
__wasi_fd_stat_put

libc-bottom-half/cloudlibc/src/libc/unistd/dup.c

Lines changed: 0 additions & 17 deletions
This file was deleted.

libc-bottom-half/cloudlibc/src/libc/unistd/dup2.c

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
int __wasilibc_register_preopened_fd(int, const char *);
1+
int __wasilibc_register_preopened_fd(int fd, const char *path);
2+
int __wasilibc_fd_renumber(int fd, int newfd);

libc-bottom-half/headers/public/wasi.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,6 @@ __wasi_errno_t __wasi_fd_datasync(
564564
__wasi_fd_t fd
565565
) __WASI_SYSCALL_NAME(fd_datasync) __attribute__((__warn_unused_result__));
566566

567-
__wasi_errno_t __wasi_fd_dup(
568-
__wasi_fd_t from,
569-
__wasi_fd_t *fd
570-
) __WASI_SYSCALL_NAME(fd_dup) __attribute__((__warn_unused_result__));
571-
572567
__wasi_errno_t __wasi_fd_pread(
573568
__wasi_fd_t fd,
574569
const __wasi_iovec_t *iovs,
@@ -592,10 +587,10 @@ __wasi_errno_t __wasi_fd_read(
592587
size_t *nread
593588
) __WASI_SYSCALL_NAME(fd_read) __attribute__((__warn_unused_result__));
594589

595-
__wasi_errno_t __wasi_fd_replace(
590+
__wasi_errno_t __wasi_fd_renumber(
596591
__wasi_fd_t from,
597592
__wasi_fd_t to
598-
) __WASI_SYSCALL_NAME(fd_replace) __attribute__((__warn_unused_result__));
593+
) __WASI_SYSCALL_NAME(fd_renumber) __attribute__((__warn_unused_result__));
599594

600595
__wasi_errno_t __wasi_fd_seek(
601596
__wasi_fd_t fd,

libc-bottom-half/mman/mman.c

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <sys/types.h>
1616

1717
struct map {
18-
int dup_fd;
1918
int prot;
2019
int flags;
2120
off_t offset;
@@ -50,7 +49,10 @@ void *mmap(void *addr, size_t length, int prot, int flags,
5049

5150
// Check for unsupported protection requests.
5251
if (prot == PROT_NONE ||
53-
(prot & PROT_EXEC) != 0)
52+
#ifdef PROT_EXEC
53+
(prot & PROT_EXEC) != 0 ||
54+
#endif
55+
0)
5456
{
5557
errno = EINVAL;
5658
return MAP_FAILED;
@@ -63,12 +65,7 @@ void *mmap(void *addr, size_t length, int prot, int flags,
6365
return MAP_FAILED;
6466
}
6567

66-
int dup_fd = dup(fd);
67-
if (dup_fd < 0)
68-
return MAP_FAILED;
69-
7068
// Initialize the header.
71-
map->dup_fd = dup_fd;
7269
map->prot = prot;
7370
map->flags = flags;
7471
map->offset = offset;
@@ -79,7 +76,7 @@ void *mmap(void *addr, size_t length, int prot, int flags,
7976
if ((flags & MAP_ANON) == 0) {
8077
char *body = map->body;
8178
while (length > 0) {
82-
ssize_t nread = pread(dup_fd, body, length, offset);
79+
ssize_t nread = pread(fd, body, length, offset);
8380
if (nread < 0) {
8481
if (errno == EINTR)
8582
continue;
@@ -100,7 +97,6 @@ void *mmap(void *addr, size_t length, int prot, int flags,
10097

10198
int munmap(void *addr, size_t length) {
10299
struct map *map = (struct map *)addr - 1;
103-
int dup_fd = map->dup_fd;
104100
off_t offset = map->offset;
105101
int flags = map->flags;
106102
int prot = map->prot;
@@ -111,30 +107,6 @@ int munmap(void *addr, size_t length) {
111107
return -1;
112108
}
113109

114-
// If needed, sync the contents of memory back to the file.
115-
if ((flags & MAP_ANON) == 0 &&
116-
(flags & MAP_SHARED) != 0 &&
117-
(prot & PROT_WRITE) != 0)
118-
{
119-
char *body = map->body;
120-
while (length > 0) {
121-
ssize_t nwritten = pwrite(dup_fd, body, length, offset);
122-
if (nwritten < 0) {
123-
if (errno == EINTR)
124-
continue;
125-
return -1;
126-
}
127-
length -= (size_t)nwritten;
128-
offset += (size_t)nwritten;
129-
body += (size_t)nwritten;
130-
}
131-
}
132-
133-
// Close the dup'd file descriptor.
134-
if (close(dup_fd) != 0) {
135-
return -1;
136-
}
137-
138110
// Release the memory.
139111
free(map);
140112

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <wasi.h>
2+
#include <errno.h>
3+
#include <unistd.h>
4+
5+
int __wasilibc_fd_renumber(int fd, int newfd) {
6+
__wasi_errno_t error = __wasi_fd_renumber(fd, newfd);
7+
if (error != 0) {
8+
errno = error;
9+
return -1;
10+
}
11+
return 0;
12+
}

libc-bottom-half/sources/pause.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
#include <unistd.h>
1+
#include <stddef.h>
2+
#include <errno.h>
23
#include <wasi.h>
34

45
int pause(void) {
56
size_t n;
6-
(void)__wasi_poll_oneoff(0, 0, 0, &n);
7+
__wasi_errno_t error = __wasi_poll_oneoff(0, 0, 0, &n);
8+
if (error != 0) {
9+
errno = error;
10+
return -1;
11+
}
12+
__builtin_trap();
713
}

0 commit comments

Comments
 (0)