/
fd_table.c
446 lines (361 loc) Β· 12.2 KB
/
fd_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifndef _WIN32
# include <sys/types.h>
#endif /* _WIN32 */
#include "uv.h"
#include "fd_table.h"
#include "path_resolver.h"
#include "wasi_types.h"
#include "wasi_rights.h"
#include "uv_mapping.h"
#include "uvwasi_alloc.h"
static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uvwasi_fd_t fd,
const uvwasi_fd_t expected,
const char* name) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_filetype_t type;
uvwasi_rights_t base;
uvwasi_rights_t inheriting;
uvwasi_errno_t err;
err = uvwasi__get_filetype_by_fd(fd, &type);
if (err != UVWASI_ESUCCESS)
return err;
err = uvwasi__get_rights(fd, UV_FS_O_RDWR, type, &base, &inheriting);
if (err != UVWASI_ESUCCESS)
return err;
err = uvwasi_fd_table_insert(uvwasi,
table,
fd,
name,
name,
type,
base,
inheriting,
0,
&wrap);
if (err != UVWASI_ESUCCESS)
return err;
if (wrap->id != expected)
err = UVWASI_EBADF;
uv_mutex_unlock(&wrap->mutex);
return err;
}
uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
const char* mapped_path,
const char* real_path,
uvwasi_filetype_t type,
uvwasi_rights_t rights_base,
uvwasi_rights_t rights_inheriting,
int preopen,
struct uvwasi_fd_wrap_t** wrap) {
struct uvwasi_fd_wrap_t* entry;
struct uvwasi_fd_wrap_t** new_fds;
uvwasi_errno_t err;
uint32_t new_size;
uint32_t index;
uint32_t i;
int r;
size_t mp_len;
char* mp_copy;
size_t rp_len;
char* rp_copy;
char* np_copy;
mp_len = strlen(mapped_path);
rp_len = strlen(real_path);
/* Reserve room for the mapped path, real path, and normalized mapped path. */
entry = (struct uvwasi_fd_wrap_t*)
uvwasi__malloc(uvwasi, sizeof(*entry) + mp_len + mp_len + rp_len + 3);
if (entry == NULL)
return UVWASI_ENOMEM;
mp_copy = (char*)(entry + 1);
rp_copy = mp_copy + mp_len + 1;
np_copy = rp_copy + rp_len + 1;
memcpy(mp_copy, mapped_path, mp_len);
mp_copy[mp_len] = '\0';
memcpy(rp_copy, real_path, rp_len);
rp_copy[rp_len] = '\0';
/* Calculate the normalized version of the mapped path, as it will be used for
any path calculations on this fd. Use the length of the mapped path as an
upper bound for the normalized path length. */
err = uvwasi__normalize_path(mp_copy, mp_len, np_copy, mp_len);
if (err) {
uvwasi__free(uvwasi, entry);
goto exit;
}
uv_rwlock_wrlock(&table->rwlock);
/* Check that there is room for a new item. If there isn't, grow the table. */
if (table->used >= table->size) {
new_size = table->size * 2;
new_fds = uvwasi__realloc(uvwasi, table->fds, new_size * sizeof(*new_fds));
if (new_fds == NULL) {
uvwasi__free(uvwasi, entry);
err = UVWASI_ENOMEM;
goto exit;
}
for (i = table->size; i < new_size; ++i)
new_fds[i] = NULL;
index = table->size;
table->fds = new_fds;
table->size = new_size;
} else {
/* The table is big enough, so find an empty slot for the new data. */
int valid_slot = 0;
for (i = 0; i < table->size; ++i) {
if (table->fds[i] == NULL) {
valid_slot = 1;
index = i;
break;
}
}
/* This should never happen. */
if (valid_slot == 0) {
uvwasi__free(uvwasi, entry);
err = UVWASI_ENOSPC;
goto exit;
}
}
table->fds[index] = entry;
r = uv_mutex_init(&entry->mutex);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
goto exit;
}
entry->id = index;
entry->fd = fd;
entry->path = mp_copy;
entry->real_path = rp_copy;
entry->normalized_path = np_copy;
entry->type = type;
entry->rights_base = rights_base;
entry->rights_inheriting = rights_inheriting;
entry->preopen = preopen;
if (wrap != NULL) {
uv_mutex_lock(&entry->mutex);
*wrap = entry;
}
table->used++;
err = UVWASI_ESUCCESS;
exit:
uv_rwlock_wrunlock(&table->rwlock);
return err;
}
uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
uvwasi_options_t* options) {
struct uvwasi_fd_table_t* table;
uvwasi_errno_t err;
int r;
/* Require an initial size of at least three to store the stdio FDs. */
if (uvwasi == NULL || options == NULL || options->fd_table_size < 3)
return UVWASI_EINVAL;
table = &uvwasi->fds;
table->fds = NULL;
table->used = 0;
table->size = options->fd_table_size;
table->fds = uvwasi__calloc(uvwasi,
options->fd_table_size,
sizeof(struct uvwasi_fd_wrap_t*));
if (table->fds == NULL)
return UVWASI_ENOMEM;
r = uv_rwlock_init(&table->rwlock);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
/* Free table->fds and set it to NULL here. This is done explicitly instead
of jumping to error_exit because uvwasi_fd_table_free() relies on fds
being NULL to know whether or not to destroy the rwlock.
*/
uvwasi__free(uvwasi, table->fds);
table->fds = NULL;
return err;
}
/* Create the stdio FDs. */
err = uvwasi__insert_stdio(uvwasi, table, options->in, 0, "<stdin>");
if (err != UVWASI_ESUCCESS)
goto error_exit;
err = uvwasi__insert_stdio(uvwasi, table, options->out, 1, "<stdout>");
if (err != UVWASI_ESUCCESS)
goto error_exit;
err = uvwasi__insert_stdio(uvwasi, table, options->err, 2, "<stderr>");
if (err != UVWASI_ESUCCESS)
goto error_exit;
return UVWASI_ESUCCESS;
error_exit:
uvwasi_fd_table_free(uvwasi, table);
return err;
}
void uvwasi_fd_table_free(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table) {
struct uvwasi_fd_wrap_t* entry;
uint32_t i;
if (table == NULL)
return;
for (i = 0; i < table->size; i++) {
entry = table->fds[i];
if (entry == NULL) continue;
uv_mutex_destroy(&entry->mutex);
uvwasi__free(uvwasi, entry);
}
if (table->fds != NULL) {
uvwasi__free(uvwasi, table->fds);
table->fds = NULL;
table->size = 0;
table->used = 0;
uv_rwlock_destroy(&table->rwlock);
}
}
uvwasi_errno_t uvwasi_fd_table_insert_preopen(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const char* path,
const char* real_path) {
uvwasi_filetype_t type;
uvwasi_rights_t base;
uvwasi_rights_t inheriting;
uvwasi_errno_t err;
if (table == NULL || path == NULL || real_path == NULL)
return UVWASI_EINVAL;
err = uvwasi__get_filetype_by_fd(fd, &type);
if (err != UVWASI_ESUCCESS)
return err;
if (type != UVWASI_FILETYPE_DIRECTORY)
return UVWASI_ENOTDIR;
err = uvwasi__get_rights(fd, 0, type, &base, &inheriting);
if (err != UVWASI_ESUCCESS)
return err;
return uvwasi_fd_table_insert(uvwasi,
table,
fd,
path,
real_path,
UVWASI_FILETYPE_DIRECTORY,
UVWASI__RIGHTS_DIRECTORY_BASE,
UVWASI__RIGHTS_DIRECTORY_INHERITING,
1,
NULL);
}
uvwasi_errno_t uvwasi_fd_table_get(struct uvwasi_fd_table_t* table,
const uvwasi_fd_t id,
struct uvwasi_fd_wrap_t** wrap,
uvwasi_rights_t rights_base,
uvwasi_rights_t rights_inheriting) {
uvwasi_errno_t err;
if (table == NULL)
return UVWASI_EINVAL;
uv_rwlock_wrlock(&table->rwlock);
err = uvwasi_fd_table_get_nolock(table,
id,
wrap,
rights_base,
rights_inheriting);
uv_rwlock_wrunlock(&table->rwlock);
return err;
}
/* uvwasi_fd_table_get_nolock() retrieves a file descriptor and locks its mutex,
but does not lock the file descriptor table like uvwasi_fd_table_get() does.
*/
uvwasi_errno_t uvwasi_fd_table_get_nolock(struct uvwasi_fd_table_t* table,
const uvwasi_fd_t id,
struct uvwasi_fd_wrap_t** wrap,
uvwasi_rights_t rights_base,
uvwasi_rights_t rights_inheriting) {
struct uvwasi_fd_wrap_t* entry;
if (table == NULL || wrap == NULL)
return UVWASI_EINVAL;
if (id >= table->size)
return UVWASI_EBADF;
entry = table->fds[id];
if (entry == NULL || entry->id != id)
return UVWASI_EBADF;
/* Validate that the fd has the necessary rights. */
if ((~entry->rights_base & rights_base) != 0 ||
(~entry->rights_inheriting & rights_inheriting) != 0) {
return UVWASI_ENOTCAPABLE;
}
uv_mutex_lock(&entry->mutex);
*wrap = entry;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_table_remove_nolock(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uvwasi_fd_t id) {
struct uvwasi_fd_wrap_t* entry;
if (table == NULL)
return UVWASI_EINVAL;
if (id >= table->size)
return UVWASI_EBADF;
entry = table->fds[id];
if (entry == NULL || entry->id != id)
return UVWASI_EBADF;
uv_mutex_destroy(&entry->mutex);
uvwasi__free(uvwasi, entry);
table->fds[id] = NULL;
table->used--;
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_table_renumber(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
const uvwasi_fd_t dst,
const uvwasi_fd_t src) {
struct uvwasi_fd_wrap_t* dst_entry;
struct uvwasi_fd_wrap_t* src_entry;
uv_fs_t req;
uvwasi_errno_t err;
int r;
if (uvwasi == NULL || table == NULL)
return UVWASI_EINVAL;
if (dst == src)
return UVWASI_ESUCCESS;
uv_rwlock_wrlock(&table->rwlock);
if (dst >= table->size || src >= table->size) {
err = UVWASI_EBADF;
goto exit;
}
dst_entry = table->fds[dst];
src_entry = table->fds[src];
if (dst_entry == NULL || dst_entry->id != dst ||
src_entry == NULL || src_entry->id != src) {
err = UVWASI_EBADF;
goto exit;
}
uv_mutex_lock(&dst_entry->mutex);
uv_mutex_lock(&src_entry->mutex);
/* Close the existing destination descriptor. */
r = uv_fs_close(NULL, &req, dst_entry->fd, NULL);
uv_fs_req_cleanup(&req);
if (r != 0) {
uv_mutex_unlock(&src_entry->mutex);
uv_mutex_unlock(&dst_entry->mutex);
err = uvwasi__translate_uv_error(r);
goto exit;
}
/* Move the source entry to the destination slot in the table. */
table->fds[dst] = table->fds[src];
table->fds[dst]->id = dst;
uv_mutex_unlock(&table->fds[dst]->mutex);
table->fds[src] = NULL;
table->used--;
/* Clean up what's left of the old destination entry. */
uv_mutex_unlock(&dst_entry->mutex);
uv_mutex_destroy(&dst_entry->mutex);
uvwasi__free(uvwasi, dst_entry);
err = UVWASI_ESUCCESS;
exit:
uv_rwlock_wrunlock(&table->rwlock);
return err;
}
uvwasi_errno_t uvwasi_fd_table_lock(struct uvwasi_fd_table_t* table) {
if (table == NULL)
return UVWASI_EINVAL;
uv_rwlock_wrlock(&table->rwlock);
return UVWASI_ESUCCESS;
}
uvwasi_errno_t uvwasi_fd_table_unlock(struct uvwasi_fd_table_t* table) {
if (table == NULL)
return UVWASI_EINVAL;
uv_rwlock_wrunlock(&table->rwlock);
return UVWASI_ESUCCESS;
}