Skip to content

Commit 74ec0e4

Browse files
authored
Allow calls to descriptor_table_update() to initialize descriptor table (#626)
This was breaking reads/writes to stdin/stdout/stderr. Also check return values from init_{stdin, stdout, stderr}, although this may not help much since the return values from printf()/etc. aren't usually checked.
1 parent 008d705 commit 74ec0e4

File tree

8 files changed

+58
-22
lines changed

8 files changed

+58
-22
lines changed

libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ DIR *fdopendir(int fd) {
5656
stream_info.directory_file_handle = file_handle;
5757
new_entry.directory_stream_info = stream_info;
5858
int new_fd = -1;
59-
descriptor_table_update(dirp->fd, new_entry);
60-
59+
if (!descriptor_table_update(dirp->fd, new_entry)) {
60+
errno = EBADF;
61+
return NULL;
62+
}
6163
dirp->cookie = __WASI_DIRCOOKIE_START;
6264
dirp->buffer_processed = 0;
6365
dirp->buffer_size = DIRENT_DEFAULT_BUFFER_SIZE;

libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ int __wasilibc_nocwd_openat_nomode(int fd, const char *path, int oflag) {
110110
new_entry.file.readable = ((oflag & O_RDONLY) != 0);
111111
new_entry.file.writable = ((oflag & O_WRONLY) != 0);
112112
new_entry.file.file_handle = filesystem_borrow_descriptor(new_handle);
113-
descriptor_table_insert(new_entry, &new_fd);
114-
113+
if (!descriptor_table_insert(new_entry, &new_fd)) {
114+
errno = ENOMEM;
115+
return -1;
116+
}
115117
// Return the new file descriptor from the table
116118
return new_fd;
117119
#else

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ off_t __lseek(int fildes, off_t offset, int whence) {
3737
off_t offset_to_use = 0;
3838
// Look up a stream for fildes
3939
descriptor_table_entry_t *entry;
40-
descriptor_table_get_ref(fildes, &entry);
40+
if (!descriptor_table_get_ref(fildes, &entry)) {
41+
errno = EBADF;
42+
return -1;
43+
}
4144
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_STREAM) {
4245
// Find the offset relative to the beginning of the file
4346
// The offset is always *added*: either to 0, the current

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
1818
bool ok = false;
1919

2020
// Check for stdin
21-
if (fildes == 0)
22-
init_stdin();
21+
if (fildes == 0) {
22+
if (!init_stdin()) {
23+
errno = EINVAL;
24+
return -1;
25+
}
26+
}
2327

2428
// Translate the file descriptor to an internal handle
2529
descriptor_table_entry_t* entry = 0;
26-
descriptor_table_get_ref(fildes, &entry);
30+
if (!descriptor_table_get_ref(fildes, &entry)) {
31+
errno = EBADF;
32+
return -1;
33+
}
2734
streams_borrow_input_stream_t input_stream;
2835
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_HANDLE) {
2936
// File's input stream hasn't been opened yet
@@ -69,7 +76,10 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
6976
new_entry.stream.file_info.readable = entry->file.readable;
7077
new_entry.stream.file_info.writable = entry->file.writable;
7178
new_entry.stream.file_info.file_handle = entry->file.file_handle;
72-
descriptor_table_update(fildes, new_entry);
79+
if (!descriptor_table_update(fildes, new_entry)) {
80+
errno = ENOMEM;
81+
return -1;
82+
}
7383
} else if (entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_STREAM) {
7484
if (!entry->stream.file_info.readable) {
7585
errno = EBADF;
@@ -108,7 +118,10 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
108118
wasip2_list_u8_free(&contents);
109119

110120
// Update the offset
111-
descriptor_table_get_ref(fildes, &entry);
121+
if (!descriptor_table_get_ref(fildes, &entry)) {
122+
errno = EBADF;
123+
return -1;
124+
}
112125
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_STREAM) {
113126
entry->stream.offset += contents.len;
114127
} else {

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,24 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
2525
descriptor_table_entry_t* entry = 0;
2626

2727
// Check for stdout/stderr
28-
if (fildes == 1)
29-
init_stdout();
30-
else if (fildes == 2)
31-
init_stderr();
28+
if (fildes == 1) {
29+
if (!init_stdout()) {
30+
errno = EINVAL;
31+
return -1;
32+
}
33+
}
34+
else if (fildes == 2) {
35+
if (!init_stderr()) {
36+
errno = EINVAL;
37+
return -1;
38+
}
39+
}
3240

3341
// Translate the file descriptor to an internal handle
34-
descriptor_table_get_ref(fildes, &entry);
42+
if (!descriptor_table_get_ref(fildes, &entry)) {
43+
errno = EBADF;
44+
return -1;
45+
}
3546
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_HANDLE) {
3647
create_new_stream = true;
3748

@@ -123,7 +134,10 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
123134
new_entry.stream.file_info.writable = entry->file.writable;
124135
new_entry.stream.file_info.file_handle = entry->file.file_handle;
125136
new_entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
126-
descriptor_table_update(fildes, new_entry);
137+
if (!descriptor_table_update(fildes, new_entry)) {
138+
errno = ENOMEM;
139+
return -1;
140+
}
127141
}
128142

129143
ok = streams_method_output_stream_blocking_flush(output_stream,

libc-bottom-half/headers/private/wasi/file_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ static void remove_and_drop_directory_stream(int fd) {
5555

5656
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_DIRECTORY_STREAM) {
5757
filesystem_directory_entry_stream_drop_own(entry->directory_stream_info.directory_stream);
58-
descriptor_table_remove(fd, entry);
58+
if (!descriptor_table_remove(fd, entry))
59+
return;
5960
}
6061
}
6162

libc-bottom-half/sources/__wasilibc_tell.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ off_t __wasilibc_tell(int fildes) {
1111
#ifdef __wasilibc_use_wasip2
1212
// Look up a stream for fildes
1313
descriptor_table_entry_t *entry;
14-
descriptor_table_get_ref(fildes, &entry);
14+
if (!descriptor_table_get_ref(fildes, &entry)) {
15+
errno = EBADF;
16+
return -1;
17+
}
1518

1619
// Return the current offset in the stream
1720
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_FILE_STREAM) {

libc-bottom-half/sources/descriptor_table.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,8 @@ bool descriptor_table_get_ref(int fd, descriptor_table_entry_t **entry)
216216
return get(fd, entry, &global_table);
217217
}
218218

219-
bool descriptor_table_update(int fd, descriptor_table_entry_t entry) {
220-
if (!global_table.entries)
221-
return false;
222-
219+
bool descriptor_table_update(int fd, descriptor_table_entry_t entry)
220+
{
223221
return insert(entry, fd, &global_table, true);
224222
}
225223

0 commit comments

Comments
 (0)