Skip to content

Commit 2886020

Browse files
authored
Merge pull request #279 from bytecodealliance/main
IO: support populate fds into WASM application (#655)
2 parents 4ce2f2c + c6783ef commit 2886020

File tree

8 files changed

+90
-20
lines changed

8 files changed

+90
-20
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ aot_instantiate(AOTModule *module, bool is_sub_inst,
10361036
module->wasi_args.env_count,
10371037
module->wasi_args.argv,
10381038
module->wasi_args.argc,
1039+
module->wasi_args.stdio[0],
1040+
module->wasi_args.stdio[1],
1041+
module->wasi_args.stdio[2],
10391042
error_buf, error_buf_size))
10401043
goto fail;
10411044
}

core/iwasm/common/wasm_runtime_common.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,12 +1793,14 @@ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
17931793
}
17941794

17951795
#if WASM_ENABLE_LIBC_WASI != 0
1796+
17961797
void
1797-
wasm_runtime_set_wasi_args(WASMModuleCommon *module,
1798+
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module,
17981799
const char *dir_list[], uint32 dir_count,
17991800
const char *map_dir_list[], uint32 map_dir_count,
18001801
const char *env_list[], uint32 env_count,
1801-
char *argv[], int argc)
1802+
char *argv[], int argc,
1803+
int stdinfd, int stdoutfd, int stderrfd)
18021804
{
18031805
WASIArguments *wasi_args = NULL;
18041806

@@ -1820,16 +1822,35 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module,
18201822
wasi_args->env_count = env_count;
18211823
wasi_args->argv = argv;
18221824
wasi_args->argc = (uint32)argc;
1825+
wasi_args->stdio[0] = stdinfd;
1826+
wasi_args->stdio[1] = stdoutfd;
1827+
wasi_args->stdio[2] = stderrfd;
18231828
}
18241829
}
18251830

1831+
void
1832+
wasm_runtime_set_wasi_args(WASMModuleCommon *module,
1833+
const char *dir_list[], uint32 dir_count,
1834+
const char *map_dir_list[], uint32 map_dir_count,
1835+
const char *env_list[], uint32 env_count,
1836+
char *argv[], int argc)
1837+
{
1838+
wasm_runtime_set_wasi_args_ex(module,
1839+
dir_list, dir_count,
1840+
map_dir_list, map_dir_count,
1841+
env_list, env_count,
1842+
argv, argc,
1843+
-1, -1, -1);
1844+
}
1845+
18261846
#if WASM_ENABLE_UVWASI == 0
18271847
bool
18281848
wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
18291849
const char *dir_list[], uint32 dir_count,
18301850
const char *map_dir_list[], uint32 map_dir_count,
18311851
const char *env[], uint32 env_count,
18321852
char *argv[], uint32 argc,
1853+
int stdinfd, int stdoutfd, int stderrfd,
18331854
char *error_buf, uint32 error_buf_size)
18341855
{
18351856
WASIContext *wasi_ctx;
@@ -1951,9 +1972,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
19511972
argv_environ_inited = true;
19521973

19531974
/* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
1954-
if (!fd_table_insert_existing(curfds, 0, 0)
1955-
|| !fd_table_insert_existing(curfds, 1, 1)
1956-
|| !fd_table_insert_existing(curfds, 2, 2)) {
1975+
if (!fd_table_insert_existing(curfds, 0, (stdinfd != -1) ? stdinfd : 0)
1976+
|| !fd_table_insert_existing(curfds, 1, (stdoutfd != -1) ? stdoutfd : 1)
1977+
|| !fd_table_insert_existing(curfds, 2, (stderrfd != -1) ? stderrfd : 2)) {
19571978
set_error_buf(error_buf, error_buf_size,
19581979
"Init wasi environment failed: init fd table failed");
19591980
goto fail;
@@ -2065,6 +2086,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
20652086
const char *map_dir_list[], uint32 map_dir_count,
20662087
const char *env[], uint32 env_count,
20672088
char *argv[], uint32 argc,
2089+
int stdinfd, int stdoutfd, int stderrfd,
20682090
char *error_buf, uint32 error_buf_size)
20692091
{
20702092
uvwasi_t *uvwasi = NULL;
@@ -2084,6 +2106,9 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
20842106
init_options.allocator = &uvwasi_allocator;
20852107
init_options.argc = argc;
20862108
init_options.argv = (const char **)argv;
2109+
init_options.in = (stdinfd != -1) ? (uvwasi_fd_t)stdinfd : init_options.in;
2110+
init_options.out = (stdoutfd != -1) ? (uvwasi_fd_t)stdoutfd : init_options.out;
2111+
init_options.err = (stderrfd != -1) ? (uvwasi_fd_t)stderrfd : init_options.err;
20872112

20882113
if (dir_count > 0) {
20892114
init_options.preopenc = dir_count;

core/iwasm/common/wasm_runtime_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,14 @@ wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
630630
#endif
631631

632632
#if WASM_ENABLE_LIBC_WASI != 0
633+
WASM_RUNTIME_API_EXTERN void
634+
wasm_runtime_set_wasi_args_ex(WASMModuleCommon *module,
635+
const char *dir_list[], uint32 dir_count,
636+
const char *map_dir_list[], uint32 map_dir_count,
637+
const char *env_list[], uint32 env_count,
638+
char *argv[], int argc,
639+
int stdinfd, int stdoutfd, int stderrfd);
640+
633641
/* See wasm_export.h for description */
634642
WASM_RUNTIME_API_EXTERN void
635643
wasm_runtime_set_wasi_args(WASMModuleCommon *module,
@@ -652,6 +660,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
652660
const char *map_dir_list[], uint32 map_dir_count,
653661
const char *env[], uint32 env_count,
654662
char *argv[], uint32 argc,
663+
int stdinfd, int stdoutfd, int stderrfd,
655664
char *error_buf, uint32 error_buf_size);
656665

657666
void

core/iwasm/include/wasm_export.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ wasm_runtime_load_from_sections(wasm_section_list_t section_list, bool is_aot,
313313
WASM_RUNTIME_API_EXTERN void
314314
wasm_runtime_unload(wasm_module_t module);
315315

316+
WASM_RUNTIME_API_EXTERN void
317+
wasm_runtime_set_wasi_args_ex(wasm_module_t module,
318+
const char *dir_list[], uint32_t dir_count,
319+
const char *map_dir_list[], uint32_t map_dir_count,
320+
const char *env[], uint32_t env_count,
321+
char *argv[], int argc,
322+
int stdinfd, int stdoutfd, int stderrfd);
323+
316324
WASM_RUNTIME_API_EXTERN void
317325
wasm_runtime_set_wasi_args(wasm_module_t module,
318326
const char *dir_list[], uint32_t dir_count,

core/iwasm/interpreter/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ typedef struct WASIArguments {
305305
uint32 env_count;
306306
char **argv;
307307
uint32 argc;
308+
int stdio[3];
308309
} WASIArguments;
309310
#endif
310311

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,9 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
14831483
module->wasi_args.env_count,
14841484
module->wasi_args.argv,
14851485
module->wasi_args.argc,
1486+
module->wasi_args.stdio[0],
1487+
module->wasi_args.stdio[1],
1488+
module->wasi_args.stdio[2],
14861489
error_buf, error_buf_size)) {
14871490
goto fail;
14881491
}

product-mini/platforms/linux-sgx/enclave-sample/App/App.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,20 +550,24 @@ static bool
550550
set_wasi_args(void *wasm_module,
551551
const char **dir_list, uint32_t dir_list_size,
552552
const char **env_list, uint32_t env_list_size,
553+
int stdinfd, int stdoutfd, int stderrfd,
553554
char **argv, uint32_t argc)
554555
{
555-
uint64_t ecall_args[7];
556+
uint64_t ecall_args[10];
556557

557558
ecall_args[0] = (uint64_t)(uintptr_t)wasm_module;
558559
ecall_args[1] = (uint64_t)(uintptr_t)dir_list;
559560
ecall_args[2] = dir_list_size;
560561
ecall_args[3] = (uint64_t)(uintptr_t)env_list;
561562
ecall_args[4] = env_list_size;
562-
ecall_args[5] = (uint64_t)(uintptr_t)argv;
563-
ecall_args[6] = argc;
563+
ecall_args[5] = stdinfd;
564+
ecall_args[6] = stdoutfd;
565+
ecall_args[7] = stderrfd;
566+
ecall_args[8] = (uint64_t)(uintptr_t)argv;
567+
ecall_args[9] = argc;
564568
if (SGX_SUCCESS != ecall_handle_command(g_eid, CMD_SET_WASI_ARGS,
565569
(uint8_t *)ecall_args,
566-
sizeof(uint64_t) * 7)) {
570+
sizeof(uint64_t) * 10)) {
567571
printf("Call ecall_handle_command() failed.\n");
568572
}
569573

@@ -702,7 +706,7 @@ main(int argc, char *argv[])
702706

703707
/* Set wasi arguments */
704708
if (!set_wasi_args(wasm_module, dir_list, dir_list_size,
705-
env_list, env_list_size, argv, argc)) {
709+
env_list, env_list_size, 0, 1, 2, argv, argc)) {
706710
printf("%s\n", "set wasi arguments failed.\n");
707711
goto fail3;
708712
}
@@ -773,6 +777,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
773777
uint32_t max_thread_num = 4;
774778
char *wasm_files[16];
775779
void *wasm_module_inst[16];
780+
int stdinfd = -1;
781+
int stdoutfd = -1;
782+
int stderrfd = -1;
776783

777784
int argc = 2;
778785
char *argv[argc] = { (char*)"./iwasm", (char *)args->argv[0] };
@@ -796,6 +803,12 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
796803
wasm_files[i] = (char *)args->argv[i];
797804
}
798805

806+
if (args->stdio != NULL) {
807+
stdinfd = args->stdio->stdin_fd;
808+
stdoutfd = args->stdio->stdout_fd;
809+
stderrfd = args->stdio->stderr_fd;
810+
}
811+
799812
/* Init runtime */
800813
if (!init_runtime(alloc_with_pool, max_thread_num)) {
801814
printf("Failed to init runtime\n");
@@ -834,7 +847,9 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
834847

835848
/* Set wasi arguments */
836849
if (!set_wasi_args(wasm_module, dir_list, dir_list_size,
837-
env_list, env_list_size, argv, argc)) {
850+
env_list, env_list_size,
851+
stdinfd, stdoutfd, stderrfd,
852+
argv, argc)) {
838853
printf("%s\n", "set wasi arguments failed.\n");
839854
unload_module(wasm_module);
840855
free(wasm_file_buf);

product-mini/platforms/linux-sgx/enclave-sample/Enclave/Enclave.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,16 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
311311
uint32 dir_list_size = *(uint32 *)args++;
312312
char **env_list = *(char ***)args++;
313313
uint32 env_list_size = *(uint32 *)args++;
314+
int stdinfd = *(int *)args++;
315+
int stdoutfd = *(int *)args++;
316+
int stderrfd = *(int *)args++;
314317
char **wasi_argv = *(char ***)args++;
315318
char *p, *p1;
316319
uint32 wasi_argc = *(uint32 *)args++;
317320
uint64 total_size = 0;
318321
int32 i, str_len;
319322

320-
bh_assert(argc == 7);
323+
bh_assert(argc == 10);
321324

322325
total_size += sizeof(char *) * (uint64)dir_list_size
323326
+ sizeof(char *) * (uint64)env_list_size
@@ -382,14 +385,17 @@ handle_cmd_set_wasi_args(uint64 *args, int32 argc)
382385
p += sizeof(char *) * wasi_argc;
383386
}
384387

385-
wasm_runtime_set_wasi_args(enclave_module->module,
386-
(const char **)enclave_module->wasi_dir_list,
387-
dir_list_size,
388-
NULL, 0,
389-
(const char **)enclave_module->wasi_env_list,
390-
env_list_size,
391-
enclave_module->wasi_argv,
392-
enclave_module->wasi_argc);
388+
wasm_runtime_set_wasi_args_ex(enclave_module->module,
389+
(const char **)enclave_module->wasi_dir_list,
390+
dir_list_size,
391+
NULL, 0,
392+
(const char **)enclave_module->wasi_env_list,
393+
env_list_size,
394+
enclave_module->wasi_argv,
395+
enclave_module->wasi_argc,
396+
(stdinfd != -1) ? stdinfd : 0,
397+
(stdoutfd != -1) ? stdoutfd : 1,
398+
(stderrfd != -1) ? stderrfd : 2);
393399

394400
*args_org = true;
395401
}

0 commit comments

Comments
 (0)