summaryrefslogtreecommitdiff
path: root/deps/uvwasi/src/fd_table.c
blob: aad40e7407b259a48a278d5283bdcb2254789c9f (plain)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#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 "wasi_types.h"
#include "uv_mapping.h"
#include "uvwasi_alloc.h"


#define UVWASI__RIGHTS_ALL (UVWASI_RIGHT_FD_DATASYNC |                        \
                            UVWASI_RIGHT_FD_READ |                            \
                            UVWASI_RIGHT_FD_SEEK |                            \
                            UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS |                \
                            UVWASI_RIGHT_FD_SYNC |                            \
                            UVWASI_RIGHT_FD_TELL |                            \
                            UVWASI_RIGHT_FD_WRITE |                           \
                            UVWASI_RIGHT_FD_ADVISE |                          \
                            UVWASI_RIGHT_FD_ALLOCATE |                        \
                            UVWASI_RIGHT_PATH_CREATE_DIRECTORY |              \
                            UVWASI_RIGHT_PATH_CREATE_FILE |                   \
                            UVWASI_RIGHT_PATH_LINK_SOURCE |                   \
                            UVWASI_RIGHT_PATH_LINK_TARGET |                   \
                            UVWASI_RIGHT_PATH_OPEN |                          \
                            UVWASI_RIGHT_FD_READDIR |                         \
                            UVWASI_RIGHT_PATH_READLINK |                      \
                            UVWASI_RIGHT_PATH_RENAME_SOURCE |                 \
                            UVWASI_RIGHT_PATH_RENAME_TARGET |                 \
                            UVWASI_RIGHT_PATH_FILESTAT_GET |                  \
                            UVWASI_RIGHT_PATH_FILESTAT_SET_SIZE |             \
                            UVWASI_RIGHT_PATH_FILESTAT_SET_TIMES |            \
                            UVWASI_RIGHT_FD_FILESTAT_GET |                    \
                            UVWASI_RIGHT_FD_FILESTAT_SET_TIMES |              \
                            UVWASI_RIGHT_FD_FILESTAT_SET_SIZE |               \
                            UVWASI_RIGHT_PATH_SYMLINK |                       \
                            UVWASI_RIGHT_PATH_UNLINK_FILE |                   \
                            UVWASI_RIGHT_PATH_REMOVE_DIRECTORY |              \
                            UVWASI_RIGHT_POLL_FD_READWRITE |                  \
                            UVWASI_RIGHT_SOCK_SHUTDOWN)

#define UVWASI__RIGHTS_BLOCK_DEVICE_BASE UVWASI__RIGHTS_ALL
#define UVWASI__RIGHTS_BLOCK_DEVICE_INHERITING UVWASI__RIGHTS_ALL

#define UVWASI__RIGHTS_CHARACTER_DEVICE_BASE UVWASI__RIGHTS_ALL
#define UVWASI__RIGHTS_CHARACTER_DEVICE_INHERITING UVWASI__RIGHTS_ALL

#define UVWASI__RIGHTS_REGULAR_FILE_BASE (UVWASI_RIGHT_FD_DATASYNC |          \
                                          UVWASI_RIGHT_FD_READ |              \
                                          UVWASI_RIGHT_FD_SEEK |              \
                                          UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS |  \
                                          UVWASI_RIGHT_FD_SYNC |              \
                                          UVWASI_RIGHT_FD_TELL |              \
                                          UVWASI_RIGHT_FD_WRITE |             \
                                          UVWASI_RIGHT_FD_ADVISE |            \
                                          UVWASI_RIGHT_FD_ALLOCATE |          \
                                          UVWASI_RIGHT_FD_FILESTAT_GET |      \
                                          UVWASI_RIGHT_FD_FILESTAT_SET_SIZE | \
                                          UVWASI_RIGHT_FD_FILESTAT_SET_TIMES |\
                                          UVWASI_RIGHT_POLL_FD_READWRITE)
#define UVWASI__RIGHTS_REGULAR_FILE_INHERITING 0

#define UVWASI__RIGHTS_DIRECTORY_BASE (UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS |     \
                                       UVWASI_RIGHT_FD_SYNC |                 \
                                       UVWASI_RIGHT_FD_ADVISE |               \
                                       UVWASI_RIGHT_PATH_CREATE_DIRECTORY |   \
                                       UVWASI_RIGHT_PATH_CREATE_FILE |        \
                                       UVWASI_RIGHT_PATH_LINK_SOURCE |        \
                                       UVWASI_RIGHT_PATH_LINK_TARGET |        \
                                       UVWASI_RIGHT_PATH_OPEN |               \
                                       UVWASI_RIGHT_FD_READDIR |              \
                                       UVWASI_RIGHT_PATH_READLINK |           \
                                       UVWASI_RIGHT_PATH_RENAME_SOURCE |      \
                                       UVWASI_RIGHT_PATH_RENAME_TARGET |      \
                                       UVWASI_RIGHT_PATH_FILESTAT_GET |       \
                                       UVWASI_RIGHT_PATH_FILESTAT_SET_SIZE |  \
                                       UVWASI_RIGHT_PATH_FILESTAT_SET_TIMES | \
                                       UVWASI_RIGHT_FD_FILESTAT_GET |         \
                                       UVWASI_RIGHT_FD_FILESTAT_SET_TIMES |   \
                                       UVWASI_RIGHT_PATH_SYMLINK |            \
                                       UVWASI_RIGHT_PATH_UNLINK_FILE |        \
                                       UVWASI_RIGHT_PATH_REMOVE_DIRECTORY |   \
                                       UVWASI_RIGHT_POLL_FD_READWRITE)
#define UVWASI__RIGHTS_DIRECTORY_INHERITING (UVWASI__RIGHTS_DIRECTORY_BASE |  \
                                             UVWASI__RIGHTS_REGULAR_FILE_BASE)

#define UVWASI__RIGHTS_SOCKET_BASE (UVWASI_RIGHT_FD_READ |                    \
                                    UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS |        \
                                    UVWASI_RIGHT_FD_WRITE |                   \
                                    UVWASI_RIGHT_FD_FILESTAT_GET |            \
                                    UVWASI_RIGHT_POLL_FD_READWRITE |          \
                                    UVWASI_RIGHT_SOCK_SHUTDOWN)
#define UVWASI__RIGHTS_SOCKET_INHERITING UVWASI__RIGHTS_ALL;

#define UVWASI__RIGHTS_TTY_BASE (UVWASI_RIGHT_FD_READ |                       \
                                 UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS |           \
                                 UVWASI_RIGHT_FD_WRITE |                      \
                                 UVWASI_RIGHT_FD_FILESTAT_GET |               \
                                 UVWASI_RIGHT_POLL_FD_READWRITE)
#define UVWASI__RIGHTS_TTY_INHERITING 0

static uvwasi_errno_t uvwasi__get_type_and_rights(uv_file fd,
                                           int flags,
                                           uvwasi_filetype_t* type,
                                           uvwasi_rights_t* rights_base,
                                           uvwasi_rights_t* rights_inheriting) {
  uv_fs_t req;
  uvwasi_filetype_t filetype;
  int read_or_write_only;
  int r;

  r = uv_fs_fstat(NULL, &req, fd, NULL);
  filetype = uvwasi__stat_to_filetype(&req.statbuf);
  uv_fs_req_cleanup(&req);
  if (r != 0)
    return uvwasi__translate_uv_error(r);

  *type = filetype;
  switch (filetype) {
    case UVWASI_FILETYPE_REGULAR_FILE:
      *rights_base = UVWASI__RIGHTS_REGULAR_FILE_BASE;
      *rights_inheriting = UVWASI__RIGHTS_REGULAR_FILE_INHERITING;
      break;

    case UVWASI_FILETYPE_DIRECTORY:
      *rights_base = UVWASI__RIGHTS_DIRECTORY_BASE;
      *rights_inheriting = UVWASI__RIGHTS_DIRECTORY_INHERITING;
      break;

    /* uvwasi__stat_to_filetype() cannot differentiate socket types. It only
       returns UVWASI_FILETYPE_SOCKET_STREAM. */
    case UVWASI_FILETYPE_SOCKET_STREAM:
      if (uv_guess_handle(fd) == UV_UDP)
        *type = UVWASI_FILETYPE_SOCKET_DGRAM;

      *rights_base = UVWASI__RIGHTS_SOCKET_BASE;
      *rights_inheriting = UVWASI__RIGHTS_SOCKET_INHERITING;
      break;

    case UVWASI_FILETYPE_CHARACTER_DEVICE:
      if (uv_guess_handle(fd) == UV_TTY) {
        *rights_base = UVWASI__RIGHTS_TTY_BASE;
        *rights_inheriting = UVWASI__RIGHTS_TTY_INHERITING;
      } else {
        *rights_base = UVWASI__RIGHTS_CHARACTER_DEVICE_BASE;
        *rights_inheriting = UVWASI__RIGHTS_CHARACTER_DEVICE_INHERITING;
      }
      break;

    case UVWASI_FILETYPE_BLOCK_DEVICE:
      *rights_base = UVWASI__RIGHTS_BLOCK_DEVICE_BASE;
      *rights_inheriting = UVWASI__RIGHTS_BLOCK_DEVICE_INHERITING;
      break;

    default:
      *rights_base = 0;
      *rights_inheriting = 0;
  }

  if (*type == UVWASI_FILETYPE_UNKNOWN)
    return UVWASI_EINVAL;

  /* Disable read/write bits depending on access mode. */
  read_or_write_only = flags & (UV_FS_O_RDONLY | UV_FS_O_WRONLY | UV_FS_O_RDWR);

  if (read_or_write_only == UV_FS_O_RDONLY)
    *rights_base &= ~UVWASI_RIGHT_FD_WRITE;
  else if (read_or_write_only == UV_FS_O_WRONLY)
    *rights_base &= ~UVWASI_RIGHT_FD_READ;

  return UVWASI_ESUCCESS;
}


static 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;
  int index;
  uint32_t i;
  int r;
  size_t mp_len;
  char* mp_copy;
  size_t rp_len;
  char* rp_copy;

  mp_len = strlen(mapped_path);
  rp_len = strlen(real_path);
  entry = (struct uvwasi_fd_wrap_t*)
    uvwasi__malloc(uvwasi, sizeof(*entry) + mp_len + rp_len + 2);
  if (entry == NULL) return UVWASI_ENOMEM;

  mp_copy = (char*)(entry + 1);
  rp_copy = mp_copy + mp_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';

  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. */
    index = -1;
    for (i = 0; i < table->size; ++i) {
      if (table->fds[i] == NULL) {
        index = i;
        break;
      }
    }

    /* index should never be -1. */
    if (index == -1) {
      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->type = type;
  entry->rights_base = rights_base;
  entry->rights_inheriting = rights_inheriting;
  entry->preopen = preopen;
  table->used++;

  if (wrap != NULL)
    *wrap = entry;

  err = UVWASI_ESUCCESS;
exit:
  uv_rwlock_wrunlock(&table->rwlock);
  return err;
}


uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
                                    struct uvwasi_fd_table_t* table,
                                    uint32_t init_size) {
  struct uvwasi_fd_wrap_t* wrap;
  uvwasi_filetype_t type;
  uvwasi_rights_t base;
  uvwasi_rights_t inheriting;
  uvwasi_errno_t err;
  uvwasi_fd_t i;
  int r;

  /* Require an initial size of at least three to store the stdio FDs. */
  if (table == NULL || init_size < 3)
    return UVWASI_EINVAL;

  table->fds = NULL;
  r = uv_rwlock_init(&table->rwlock);
  if (r != 0)
    return uvwasi__translate_uv_error(r);

  table->used = 0;
  table->size = init_size;
  table->fds = uvwasi__calloc(uvwasi,
                              init_size,
                              sizeof(struct uvwasi_fd_wrap_t*));

  if (table->fds == NULL) {
    err = UVWASI_ENOMEM;
    goto error_exit;
  }

  /* Create the stdio FDs. */
  for (i = 0; i < 3; ++i) {
    err = uvwasi__get_type_and_rights(i,
                                      UV_FS_O_RDWR,
                                      &type,
                                      &base,
                                      &inheriting);
    if (err != UVWASI_ESUCCESS)
      goto error_exit;

    err = uvwasi__fd_table_insert(uvwasi,
                                  table,
                                  i,
                                  "",
                                  "",
                                  type,
                                  base,
                                  inheriting,
                                  0,
                                  &wrap);
    if (err != UVWASI_ESUCCESS)
      goto error_exit;

    if (wrap->id != i || wrap->id != (uvwasi_fd_t) wrap->fd) {
      err = UVWASI_EBADF;
      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) {
    /* It's fine to call uvwasi__free() multiple times on table->fds. However,
       it is not fine to call uv_rwlock_destroy() multiple times. Guard against
       that by ensuring that table->fds is not NULL. Technically, it's possible
       that uvwasi_fd_table_init() initialized the rwlock successfully, but
       failed to initialize fds. However, the only way that's possible is if
       the application already ran out of memory.
    */
    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_type_and_rights(fd, 0, &type, &base, &inheriting);
  if (err != UVWASI_ESUCCESS)
    return err;

  if (type != UVWASI_FILETYPE_DIRECTORY)
    return UVWASI_ENOTDIR;

  err = uvwasi__fd_table_insert(uvwasi,
                                table,
                                fd,
                                path,
                                real_path,
                                UVWASI_FILETYPE_DIRECTORY,
                                UVWASI__RIGHTS_DIRECTORY_BASE,
                                UVWASI__RIGHTS_DIRECTORY_INHERITING,
                                1,
                                NULL);
  if (err != UVWASI_ESUCCESS)
    return err;

  return UVWASI_ESUCCESS;
}


uvwasi_errno_t uvwasi_fd_table_insert_fd(uvwasi_t* uvwasi,
                                         struct uvwasi_fd_table_t* table,
                                         const uv_file fd,
                                         const int flags,
                                         const char* path,
                                         uvwasi_rights_t rights_base,
                                         uvwasi_rights_t rights_inheriting,
                                         struct uvwasi_fd_wrap_t* wrap) {
  struct uvwasi_fd_wrap_t* fd_wrap;
  uvwasi_filetype_t type;
  uvwasi_rights_t max_base;
  uvwasi_rights_t max_inheriting;
  uvwasi_errno_t r;

  if (table == NULL || path == NULL || wrap == NULL)
    return UVWASI_EINVAL;

  r = uvwasi__get_type_and_rights(fd, flags, &type, &max_base, &max_inheriting);
  if (r != UVWASI_ESUCCESS)
    return r;

  r = uvwasi__fd_table_insert(uvwasi,
                              table,
                              fd,
                              path,
                              path,
                              type,
                              rights_base & max_base,
                              rights_inheriting & max_inheriting,
                              0,
                              &fd_wrap);
  if (r != UVWASI_ESUCCESS)
    return r;

  *wrap = *fd_wrap;
  return UVWASI_ESUCCESS;
}


uvwasi_errno_t uvwasi_fd_table_get(const 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;
  uvwasi_errno_t err;

  if (table == NULL || wrap == NULL)
    return UVWASI_EINVAL;

  uv_rwlock_rdlock((uv_rwlock_t *)&table->rwlock);

  if (id >= table->size) {
    err = UVWASI_EBADF;
    goto exit;
  }

  entry = table->fds[id];

  if (entry == NULL || entry->id != id) {
    err = UVWASI_EBADF;
    goto exit;
  }

  /* Validate that the fd has the necessary rights. */
  if ((~entry->rights_base & rights_base) != 0 ||
      (~entry->rights_inheriting & rights_inheriting) != 0) {
    err = UVWASI_ENOTCAPABLE;
    goto exit;
  }

  uv_mutex_lock(&entry->mutex);
  *wrap = entry;
  err = UVWASI_ESUCCESS;
exit:
  uv_rwlock_rdunlock((uv_rwlock_t *)&table->rwlock);
  return err;
}


uvwasi_errno_t uvwasi_fd_table_remove(uvwasi_t* uvwasi,
                                      struct uvwasi_fd_table_t* table,
                                      const uvwasi_fd_t id) {
  struct uvwasi_fd_wrap_t* entry;
  uvwasi_errno_t err;

  if (table == NULL)
    return UVWASI_EINVAL;

  uv_rwlock_wrlock(&table->rwlock);

  if (id >= table->size) {
    err = UVWASI_EBADF;
    goto exit;
  }

  entry = table->fds[id];

  if (entry == NULL || entry->id != id) {
    err = UVWASI_EBADF;
    goto exit;
  }

  uv_mutex_destroy(&entry->mutex);
  uvwasi__free(uvwasi, entry);
  table->fds[id] = NULL;
  table->used--;
  err = UVWASI_ESUCCESS;
exit:
  uv_rwlock_wrunlock(&table->rwlock);
  return err;
}