-
Notifications
You must be signed in to change notification settings - Fork 650
/
Copy pathlibc_wasi_wrapper.c
2323 lines (1848 loc) · 71.6 KB
/
libc_wasi_wrapper.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "libc_wasi_wrapper.h"
#include "bh_platform.h"
#include "wasm_export.h"
#include "wasm_runtime_common.h"
#if WASM_ENABLE_THREAD_MGR != 0
#include "../../../thread-mgr/thread_manager.h"
#endif
void
wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
/* clang-format off */
#define get_module_inst(exec_env) \
wasm_runtime_get_module_inst(exec_env)
#define get_wasi_ctx(module_inst) \
wasm_runtime_get_wasi_ctx(module_inst)
#define validate_app_addr(offset, size) \
wasm_runtime_validate_app_addr(module_inst, offset, size)
#define validate_native_addr(addr, size) \
wasm_runtime_validate_native_addr(module_inst, addr, size)
#define addr_app_to_native(offset) \
wasm_runtime_addr_app_to_native(module_inst, offset)
#define addr_native_to_app(ptr) \
wasm_runtime_addr_native_to_app(module_inst, ptr)
#define module_malloc(size, p_native_addr) \
wasm_runtime_module_malloc(module_inst, size, p_native_addr)
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
/* clang-format on */
typedef struct wasi_prestat_app {
wasi_preopentype_t pr_type;
uint32 pr_name_len;
} wasi_prestat_app_t;
typedef struct iovec_app {
uint32 buf_offset;
uint32 buf_len;
} iovec_app_t;
typedef struct WASIContext *wasi_ctx_t;
wasi_ctx_t
wasm_runtime_get_wasi_ctx(wasm_module_inst_t module_inst);
static inline size_t
min(size_t a, size_t b)
{
return a > b ? b : a;
}
static inline struct fd_table *
wasi_ctx_get_curfds(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
{
if (!wasi_ctx)
return NULL;
return wasi_ctx->curfds;
}
static inline struct argv_environ_values *
wasi_ctx_get_argv_environ(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
{
if (!wasi_ctx)
return NULL;
return wasi_ctx->argv_environ;
}
static inline struct fd_prestats *
wasi_ctx_get_prestats(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
{
if (!wasi_ctx)
return NULL;
return wasi_ctx->prestats;
}
static inline struct addr_pool *
wasi_ctx_get_addr_pool(wasm_module_inst_t module_inst, wasi_ctx_t wasi_ctx)
{
if (!wasi_ctx)
return NULL;
return wasi_ctx->addr_pool;
}
static inline char **
wasi_ctx_get_ns_lookup_list(wasi_ctx_t wasi_ctx)
{
if (!wasi_ctx)
return NULL;
return wasi_ctx->ns_lookup_list;
}
static wasi_errno_t
wasi_args_get(wasm_exec_env_t exec_env, uint32 *argv_offsets, char *argv_buf)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct argv_environ_values *argv_environ =
wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
size_t argc, argv_buf_size, i;
char **argv;
uint64 total_size;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
err = wasmtime_ssp_args_sizes_get(argv_environ, &argc, &argv_buf_size);
if (err)
return err;
total_size = sizeof(int32) * ((uint64)argc + 1);
if (total_size >= UINT32_MAX
|| !validate_native_addr(argv_offsets, (uint32)total_size)
|| argv_buf_size >= UINT32_MAX
|| !validate_native_addr(argv_buf, (uint32)argv_buf_size))
return (wasi_errno_t)-1;
total_size = sizeof(char *) * ((uint64)argc + 1);
if (total_size >= UINT32_MAX
|| !(argv = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_args_get(argv_environ, argv, argv_buf);
if (err) {
wasm_runtime_free(argv);
return err;
}
for (i = 0; i < argc; i++)
argv_offsets[i] = addr_native_to_app(argv[i]);
wasm_runtime_free(argv);
return 0;
}
static wasi_errno_t
wasi_args_sizes_get(wasm_exec_env_t exec_env, uint32 *argc_app,
uint32 *argv_buf_size_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct argv_environ_values *argv_environ;
size_t argc, argv_buf_size;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(argc_app, sizeof(uint32))
|| !validate_native_addr(argv_buf_size_app, sizeof(uint32)))
return (wasi_errno_t)-1;
argv_environ = wasi_ctx->argv_environ;
err = wasmtime_ssp_args_sizes_get(argv_environ, &argc, &argv_buf_size);
if (err)
return err;
*argc_app = (uint32)argc;
*argv_buf_size_app = (uint32)argv_buf_size;
return 0;
}
static wasi_errno_t
wasi_clock_res_get(wasm_exec_env_t exec_env,
wasi_clockid_t clock_id, /* uint32 clock_id */
wasi_timestamp_t *resolution /* uint64 *resolution */)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (!validate_native_addr(resolution, sizeof(wasi_timestamp_t)))
return (wasi_errno_t)-1;
return wasmtime_ssp_clock_res_get(clock_id, resolution);
}
static wasi_errno_t
wasi_clock_time_get(wasm_exec_env_t exec_env,
wasi_clockid_t clock_id, /* uint32 clock_id */
wasi_timestamp_t precision, /* uint64 precision */
wasi_timestamp_t *time /* uint64 *time */)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
if (!validate_native_addr(time, sizeof(wasi_timestamp_t)))
return (wasi_errno_t)-1;
return wasmtime_ssp_clock_time_get(clock_id, precision, time);
}
static wasi_errno_t
wasi_environ_get(wasm_exec_env_t exec_env, uint32 *environ_offsets,
char *environ_buf)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct argv_environ_values *argv_environ =
wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
size_t environ_count, environ_buf_size, i;
uint64 total_size;
char **environs;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
err = wasmtime_ssp_environ_sizes_get(argv_environ, &environ_count,
&environ_buf_size);
if (err)
return err;
total_size = sizeof(int32) * ((uint64)environ_count + 1);
if (total_size >= UINT32_MAX
|| !validate_native_addr(environ_offsets, (uint32)total_size)
|| environ_buf_size >= UINT32_MAX
|| !validate_native_addr(environ_buf, (uint32)environ_buf_size))
return (wasi_errno_t)-1;
total_size = sizeof(char *) * (((uint64)environ_count + 1));
if (total_size >= UINT32_MAX
|| !(environs = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_environ_get(argv_environ, environs, environ_buf);
if (err) {
wasm_runtime_free(environs);
return err;
}
for (i = 0; i < environ_count; i++)
environ_offsets[i] = addr_native_to_app(environs[i]);
wasm_runtime_free(environs);
return 0;
}
static wasi_errno_t
wasi_environ_sizes_get(wasm_exec_env_t exec_env, uint32 *environ_count_app,
uint32 *environ_buf_size_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct argv_environ_values *argv_environ =
wasi_ctx_get_argv_environ(module_inst, wasi_ctx);
size_t environ_count, environ_buf_size;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(environ_count_app, sizeof(uint32))
|| !validate_native_addr(environ_buf_size_app, sizeof(uint32)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_environ_sizes_get(argv_environ, &environ_count,
&environ_buf_size);
if (err)
return err;
*environ_count_app = (uint32)environ_count;
*environ_buf_size_app = (uint32)environ_buf_size;
return 0;
}
static wasi_errno_t
wasi_fd_prestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_prestat_app_t *prestat_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
wasi_prestat_t prestat;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(prestat_app, sizeof(wasi_prestat_app_t)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_fd_prestat_get(prestats, fd, &prestat);
if (err)
return err;
prestat_app->pr_type = prestat.pr_type;
prestat_app->pr_name_len = (uint32)prestat.u.dir.pr_name_len;
return 0;
}
static wasi_errno_t
wasi_fd_prestat_dir_name(wasm_exec_env_t exec_env, wasi_fd_t fd, char *path,
uint32 path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_prestat_dir_name(prestats, fd, path, path_len);
}
static wasi_errno_t
wasi_fd_close(wasm_exec_env_t exec_env, wasi_fd_t fd)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_close(curfds, prestats, fd);
}
static wasi_errno_t
wasi_fd_datasync(wasm_exec_env_t exec_env, wasi_fd_t fd)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_datasync(curfds, fd);
}
static wasi_errno_t
wasi_fd_pread(wasm_exec_env_t exec_env, wasi_fd_t fd, iovec_app_t *iovec_app,
uint32 iovs_len, wasi_filesize_t offset, uint32 *nread_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
wasi_iovec_t *iovec, *iovec_begin;
uint64 total_size;
size_t nread;
uint32 i;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
if (!validate_native_addr(nread_app, (uint32)sizeof(uint32))
|| total_size >= UINT32_MAX
|| !validate_native_addr(iovec_app, (uint32)total_size))
return (wasi_errno_t)-1;
total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX
|| !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
iovec = iovec_begin;
for (i = 0; i < iovs_len; i++, iovec_app++, iovec++) {
if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
err = (wasi_errno_t)-1;
goto fail;
}
iovec->buf = (void *)addr_app_to_native(iovec_app->buf_offset);
iovec->buf_len = iovec_app->buf_len;
}
err = wasmtime_ssp_fd_pread(curfds, fd, iovec_begin, iovs_len, offset,
&nread);
if (err)
goto fail;
*nread_app = (uint32)nread;
/* success */
err = 0;
fail:
wasm_runtime_free(iovec_begin);
return err;
}
static wasi_errno_t
wasi_fd_pwrite(wasm_exec_env_t exec_env, wasi_fd_t fd,
const iovec_app_t *iovec_app, uint32 iovs_len,
wasi_filesize_t offset, uint32 *nwritten_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
wasi_ciovec_t *ciovec, *ciovec_begin;
uint64 total_size;
size_t nwritten;
uint32 i;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
if (!validate_native_addr(nwritten_app, (uint32)sizeof(uint32))
|| total_size >= UINT32_MAX
|| !validate_native_addr((void *)iovec_app, (uint32)total_size))
return (wasi_errno_t)-1;
total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX
|| !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
ciovec = ciovec_begin;
for (i = 0; i < iovs_len; i++, iovec_app++, ciovec++) {
if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
err = (wasi_errno_t)-1;
goto fail;
}
ciovec->buf = (char *)addr_app_to_native(iovec_app->buf_offset);
ciovec->buf_len = iovec_app->buf_len;
}
err = wasmtime_ssp_fd_pwrite(curfds, fd, ciovec_begin, iovs_len, offset,
&nwritten);
if (err)
goto fail;
*nwritten_app = (uint32)nwritten;
/* success */
err = 0;
fail:
wasm_runtime_free(ciovec_begin);
return err;
}
static wasi_errno_t
wasi_fd_read(wasm_exec_env_t exec_env, wasi_fd_t fd,
const iovec_app_t *iovec_app, uint32 iovs_len, uint32 *nread_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
wasi_iovec_t *iovec, *iovec_begin;
uint64 total_size;
size_t nread;
uint32 i;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
if (!validate_native_addr(nread_app, (uint32)sizeof(uint32))
|| total_size >= UINT32_MAX
|| !validate_native_addr((void *)iovec_app, (uint32)total_size))
return (wasi_errno_t)-1;
total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX
|| !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
iovec = iovec_begin;
for (i = 0; i < iovs_len; i++, iovec_app++, iovec++) {
if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
err = (wasi_errno_t)-1;
goto fail;
}
iovec->buf = (void *)addr_app_to_native(iovec_app->buf_offset);
iovec->buf_len = iovec_app->buf_len;
}
err = wasmtime_ssp_fd_read(curfds, fd, iovec_begin, iovs_len, &nread);
if (err)
goto fail;
*nread_app = (uint32)nread;
/* success */
err = 0;
fail:
wasm_runtime_free(iovec_begin);
return err;
}
static wasi_errno_t
wasi_fd_renumber(wasm_exec_env_t exec_env, wasi_fd_t from, wasi_fd_t to)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_renumber(curfds, prestats, from, to);
}
static wasi_errno_t
wasi_fd_seek(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filedelta_t offset,
wasi_whence_t whence, wasi_filesize_t *newoffset)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(newoffset, sizeof(wasi_filesize_t)))
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_seek(curfds, fd, offset, whence, newoffset);
}
static wasi_errno_t
wasi_fd_tell(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t *newoffset)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(newoffset, sizeof(wasi_filesize_t)))
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_tell(curfds, fd, newoffset);
}
static wasi_errno_t
wasi_fd_fdstat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_fdstat_t *fdstat_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
wasi_fdstat_t fdstat;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(fdstat_app, sizeof(wasi_fdstat_t)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_fd_fdstat_get(curfds, fd, &fdstat);
if (err)
return err;
memcpy(fdstat_app, &fdstat, sizeof(wasi_fdstat_t));
return 0;
}
static wasi_errno_t
wasi_fd_fdstat_set_flags(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_fdflags_t flags)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_fdstat_set_flags(curfds, fd, flags);
}
static wasi_errno_t
wasi_fd_fdstat_set_rights(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_rights_t fs_rights_base,
wasi_rights_t fs_rights_inheriting)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_fdstat_set_rights(curfds, fd, fs_rights_base,
fs_rights_inheriting);
}
static wasi_errno_t
wasi_fd_sync(wasm_exec_env_t exec_env, wasi_fd_t fd)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_sync(curfds, fd);
}
static wasi_errno_t
wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
const iovec_app_t *iovec_app, uint32 iovs_len,
uint32 *nwritten_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
wasi_ciovec_t *ciovec, *ciovec_begin;
uint64 total_size;
size_t nwritten;
uint32 i;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
total_size = sizeof(iovec_app_t) * (uint64)iovs_len;
if (!validate_native_addr(nwritten_app, (uint32)sizeof(uint32))
|| total_size >= UINT32_MAX
|| !validate_native_addr((void *)iovec_app, (uint32)total_size))
return (wasi_errno_t)-1;
total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
if (total_size >= UINT32_MAX
|| !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
ciovec = ciovec_begin;
for (i = 0; i < iovs_len; i++, iovec_app++, ciovec++) {
if (!validate_app_addr(iovec_app->buf_offset, iovec_app->buf_len)) {
err = (wasi_errno_t)-1;
goto fail;
}
ciovec->buf = (char *)addr_app_to_native(iovec_app->buf_offset);
ciovec->buf_len = iovec_app->buf_len;
}
err = wasmtime_ssp_fd_write(curfds, fd, ciovec_begin, iovs_len, &nwritten);
if (err)
goto fail;
*nwritten_app = (uint32)nwritten;
/* success */
err = 0;
fail:
wasm_runtime_free(ciovec_begin);
return err;
}
static wasi_errno_t
wasi_fd_advise(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset,
wasi_filesize_t len, wasi_advice_t advice)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_advise(curfds, fd, offset, len, advice);
}
static wasi_errno_t
wasi_fd_allocate(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_filesize_t offset,
wasi_filesize_t len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_allocate(curfds, fd, offset, len);
}
static wasi_errno_t
wasi_path_create_directory(wasm_exec_env_t exec_env, wasi_fd_t fd,
const char *path, uint32 path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_create_directory(curfds, fd, path, path_len);
}
static wasi_errno_t
wasi_path_link(wasm_exec_env_t exec_env, wasi_fd_t old_fd,
wasi_lookupflags_t old_flags, const char *old_path,
uint32 old_path_len, wasi_fd_t new_fd, const char *new_path,
uint32 new_path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_link(curfds, prestats, old_fd, old_flags, old_path,
old_path_len, new_fd, new_path, new_path_len);
}
static wasi_errno_t
wasi_path_open(wasm_exec_env_t exec_env, wasi_fd_t dirfd,
wasi_lookupflags_t dirflags, const char *path, uint32 path_len,
wasi_oflags_t oflags, wasi_rights_t fs_rights_base,
wasi_rights_t fs_rights_inheriting, wasi_fdflags_t fs_flags,
wasi_fd_t *fd_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
wasi_fd_t fd = (wasi_fd_t)-1; /* set fd_app -1 if path open failed */
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(fd_app, sizeof(wasi_fd_t)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_path_open(curfds, dirfd, dirflags, path, path_len,
oflags, fs_rights_base, fs_rights_inheriting,
fs_flags, &fd);
*fd_app = fd;
return err;
}
static wasi_errno_t
wasi_fd_readdir(wasm_exec_env_t exec_env, wasi_fd_t fd, void *buf,
uint32 buf_len, wasi_dircookie_t cookie, uint32 *bufused_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
size_t bufused;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(bufused_app, sizeof(uint32)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_fd_readdir(curfds, fd, buf, buf_len, cookie, &bufused);
if (err)
return err;
*bufused_app = (uint32)bufused;
return 0;
}
static wasi_errno_t
wasi_path_readlink(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path,
uint32 path_len, char *buf, uint32 buf_len,
uint32 *bufused_app)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
size_t bufused;
wasi_errno_t err;
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(bufused_app, sizeof(uint32)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_path_readlink(curfds, fd, path, path_len, buf, buf_len,
&bufused);
if (err)
return err;
*bufused_app = (uint32)bufused;
return 0;
}
static wasi_errno_t
wasi_path_rename(wasm_exec_env_t exec_env, wasi_fd_t old_fd,
const char *old_path, uint32 old_path_len, wasi_fd_t new_fd,
const char *new_path, uint32 new_path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_rename(curfds, old_fd, old_path, old_path_len,
new_fd, new_path, new_path_len);
}
static wasi_errno_t
wasi_fd_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_filestat_t *filestat)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(filestat, sizeof(wasi_filestat_t)))
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_filestat_get(curfds, fd, filestat);
}
static wasi_errno_t
wasi_fd_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_timestamp_t st_atim, wasi_timestamp_t st_mtim,
wasi_fstflags_t fstflags)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_filestat_set_times(curfds, fd, st_atim, st_mtim,
fstflags);
}
static wasi_errno_t
wasi_fd_filestat_set_size(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_filesize_t st_size)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_fd_filestat_set_size(curfds, fd, st_size);
}
static wasi_errno_t
wasi_path_filestat_get(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_lookupflags_t flags, const char *path,
uint32 path_len, wasi_filestat_t *filestat)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
if (!validate_native_addr(filestat, sizeof(wasi_filestat_t)))
return (wasi_errno_t)-1;
return wasmtime_ssp_path_filestat_get(curfds, fd, flags, path, path_len,
filestat);
}
static wasi_errno_t
wasi_path_filestat_set_times(wasm_exec_env_t exec_env, wasi_fd_t fd,
wasi_lookupflags_t flags, const char *path,
uint32 path_len, wasi_timestamp_t st_atim,
wasi_timestamp_t st_mtim, wasi_fstflags_t fstflags)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_filestat_set_times(
curfds, fd, flags, path, path_len, st_atim, st_mtim, fstflags);
}
static wasi_errno_t
wasi_path_symlink(wasm_exec_env_t exec_env, const char *old_path,
uint32 old_path_len, wasi_fd_t fd, const char *new_path,
uint32 new_path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
struct fd_prestats *prestats = wasi_ctx_get_prestats(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_symlink(curfds, prestats, old_path, old_path_len,
fd, new_path, new_path_len);
}
static wasi_errno_t
wasi_path_unlink_file(wasm_exec_env_t exec_env, wasi_fd_t fd, const char *path,
uint32 path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_unlink_file(curfds, fd, path, path_len);
}
static wasi_errno_t
wasi_path_remove_directory(wasm_exec_env_t exec_env, wasi_fd_t fd,
const char *path, uint32 path_len)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasi_ctx_t wasi_ctx = get_wasi_ctx(module_inst);
struct fd_table *curfds = wasi_ctx_get_curfds(module_inst, wasi_ctx);
if (!wasi_ctx)
return (wasi_errno_t)-1;
return wasmtime_ssp_path_remove_directory(curfds, fd, path, path_len);
}
#if WASM_ENABLE_THREAD_MGR != 0
static __wasi_timestamp_t
get_timeout_for_poll_oneoff(const wasi_subscription_t *in,
uint32 nsubscriptions)
{
__wasi_timestamp_t timeout = (__wasi_timestamp_t)-1;
uint32 i = 0;
for (i = 0; i < nsubscriptions; ++i) {
const __wasi_subscription_t *s = &in[i];
if (s->u.type == __WASI_EVENTTYPE_CLOCK
&& (s->u.u.clock.flags & __WASI_SUBSCRIPTION_CLOCK_ABSTIME) == 0) {
timeout = min(timeout, s->u.u.clock.timeout);
}
}
return timeout;
}
static void
update_clock_subscription_data(wasi_subscription_t *in, uint32 nsubscriptions,
const wasi_timestamp_t new_timeout)
{
uint32 i = 0;
for (i = 0; i < nsubscriptions; ++i) {
__wasi_subscription_t *s = &in[i];
if (s->u.type == __WASI_EVENTTYPE_CLOCK) {
s->u.u.clock.timeout = new_timeout;
}
}
}
static wasi_errno_t
execute_interruptible_poll_oneoff(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct fd_table *curfds,
#endif
const __wasi_subscription_t *in, __wasi_event_t *out, size_t nsubscriptions,
size_t *nevents, wasm_exec_env_t exec_env)
{
if (nsubscriptions == 0) {
*nevents = 0;
return __WASI_ESUCCESS;
}
wasi_errno_t err;
__wasi_timestamp_t elapsed = 0;
bool all_outs_are_type_clock;
uint32 i;