Skip to content

Commit 216404d

Browse files
authored
initialize WASI stdio handles to invalid for better error handling (#4092)
* initialize WASI stdio handles to invalid for better error handling * implement os_invalid_raw_handle function for consistent invalid handle representation
1 parent 0e8b57d commit 216404d

File tree

11 files changed

+71
-2
lines changed

11 files changed

+71
-2
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,6 +4127,12 @@ create_module(char *name, char *error_buf, uint32 error_buf_size)
41274127
}
41284128
#endif
41294129

4130+
#if WASM_ENABLE_LIBC_WASI != 0
4131+
module->wasi_args.stdio[0] = os_invalid_raw_handle();
4132+
module->wasi_args.stdio[1] = os_invalid_raw_handle();
4133+
module->wasi_args.stdio[2] = os_invalid_raw_handle();
4134+
#endif
4135+
41304136
return module;
41314137
#if WASM_ENABLE_GC != 0
41324138
fail2:

core/iwasm/interpreter/wasm_loader.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6376,6 +6376,12 @@ create_module(char *name, char *error_buf, uint32 error_buf_size)
63766376
}
63776377
#endif
63786378

6379+
#if WASM_ENABLE_LIBC_WASI != 0
6380+
module->wasi_args.stdio[0] = os_invalid_raw_handle();
6381+
module->wasi_args.stdio[1] = os_invalid_raw_handle();
6382+
module->wasi_args.stdio[2] = os_invalid_raw_handle();
6383+
#endif
6384+
63796385
(void)ret;
63806386
return module;
63816387

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,6 +3137,12 @@ create_module(char *name, char *error_buf, uint32 error_buf_size)
31373137
}
31383138
#endif
31393139

3140+
#if WASM_ENABLE_LIBC_WASI != 0
3141+
module->wasi_args.stdio[0] = os_invalid_raw_handle();
3142+
module->wasi_args.stdio[1] = os_invalid_raw_handle();
3143+
module->wasi_args.stdio[2] = os_invalid_raw_handle();
3144+
#endif
3145+
31403146
(void)ret;
31413147
return module;
31423148
}

core/shared/platform/alios/alios_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ os_dcache_flush()
7979
void
8080
os_icache_flush(void *start, size_t len)
8181
{}
82+
83+
os_raw_file_handle
84+
os_invalid_raw_handle(void)
85+
{
86+
return -1;
87+
}

core/shared/platform/common/posix/posix_file.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,4 +1032,10 @@ char *
10321032
os_realpath(const char *path, char *resolved_path)
10331033
{
10341034
return realpath(path, resolved_path);
1035-
}
1035+
}
1036+
1037+
os_raw_file_handle
1038+
os_invalid_raw_handle(void)
1039+
{
1040+
return -1;
1041+
}

core/shared/platform/esp-idf/espidf_file.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,4 +1032,10 @@ char *
10321032
os_realpath(const char *path, char *resolved_path)
10331033
{
10341034
return realpath(path, resolved_path);
1035-
}
1035+
}
1036+
1037+
os_raw_file_handle
1038+
os_invalid_raw_handle(void)
1039+
{
1040+
return -1;
1041+
}

core/shared/platform/include/platform_api_extension.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,15 @@ os_is_dir_stream_valid(os_dir_stream *dir_stream);
16071607
os_file_handle
16081608
os_get_invalid_handle(void);
16091609

1610+
/**
1611+
* Returns an invalid raw file handle that is guaranteed to cause failure when
1612+
* called with any filesystem operation.
1613+
*
1614+
* @return the invalid raw file handle
1615+
*/
1616+
os_raw_file_handle
1617+
os_invalid_raw_handle(void);
1618+
16101619
/**
16111620
* Checks whether the given file handle is valid. An invalid handle is
16121621
* guaranteed to cause failure when called with any filesystem operation.

core/shared/platform/riot/riot_platform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,9 @@ os_dcache_flush(void)
9595
void
9696
os_icache_flush(void *start, size_t len)
9797
{}
98+
99+
os_raw_file_handle
100+
os_invalid_raw_handle(void)
101+
{
102+
return -1;
103+
}

core/shared/platform/rt-thread/rtt_file.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,9 @@ posix_fallocate(int __fd, off_t __offset, off_t __length)
192192
errno = ENOSYS;
193193
return -1;
194194
}
195+
196+
os_raw_file_handle
197+
os_invalid_raw_handle(void)
198+
{
199+
return -1;
200+
}

core/shared/platform/windows/win_file.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,3 +1810,9 @@ os_realpath(const char *path, char *resolved_path)
18101810

18111811
return resolved_path;
18121812
}
1813+
1814+
os_raw_file_handle
1815+
os_invalid_raw_handle(void)
1816+
{
1817+
return INVALID_HANDLE_VALUE;
1818+
}

0 commit comments

Comments
 (0)