Skip to content

Commit 574034b

Browse files
authored
Implement some more of the wasi API (#12704)
In order to test our WASI API implementation I've added `wasi-test-suite` as submodule. This submodule is only needed for testing.
1 parent 18e0fad commit 574034b

14 files changed

+445
-24
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "test/third_party/googletest"]
55
path = test/third_party/googletest
66
url = https://github.com/google/googletest
7+
[submodule "test/third_party/wasi-test-suite"]
8+
path = test/third_party/wasi-test-suite
9+
url = https://github.com/khronosproject/wasi-test-suite

emcc.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ def package_files(options, target):
11371137
if options.preload_files:
11381138
# Preloading files uses --pre-js code that runs before the module is loaded.
11391139
file_code = shared.check_call(cmd, stdout=PIPE).stdout
1140-
js_manipulation.add_files_pre_js(options.pre_js, file_code)
1140+
js_manipulation.add_files_pre_js(settings.PRE_JS_FILES, file_code)
11411141
else:
11421142
# Otherwise, we are embedding files, which does not require --pre-js code,
11431143
# and instead relies on a static constrcutor to populate the filesystem.
@@ -1311,9 +1311,6 @@ def run(args):
13111311
if len(options.preload_files) or len(options.embed_files):
13121312
linker_arguments += package_files(options, target)
13131313

1314-
settings.PRE_JS_FILES = [os.path.abspath(f) for f in options.pre_js]
1315-
settings.POST_JS_FILES = [os.path.abspath(f) for f in options.post_js]
1316-
13171314
if options.oformat == OFormat.OBJECT:
13181315
logger.debug(f'link_to_object: {linker_arguments} -> {target}')
13191316
building.link_to_object(linker_arguments, target)
@@ -1891,7 +1888,7 @@ def phase_linker_setup(options, state, newargs):
18911888
# Requesting both Wasm and Wasm2JS support
18921889
settings.WASM2JS = 1
18931890

1894-
if (options.oformat == OFormat.WASM or settings.PURE_WASI) and not settings.SIDE_MODULE:
1891+
if options.oformat == OFormat.WASM and not settings.SIDE_MODULE:
18951892
# if the output is just a wasm file, it will normally be a standalone one,
18961893
# as there is no JS. an exception are side modules, as we can't tell at
18971894
# compile time whether JS will be involved or not - the main module may
@@ -1902,6 +1899,10 @@ def phase_linker_setup(options, state, newargs):
19021899
if settings.LZ4:
19031900
settings.EXPORTED_RUNTIME_METHODS += ['LZ4']
19041901

1902+
if settings.PURE_WASI:
1903+
settings.STANDALONE_WASM = 1
1904+
settings.WASM_BIGINT = 1
1905+
19051906
if settings.WASM2C:
19061907
# wasm2c only makes sense with standalone wasm - there will be no JS,
19071908
# just wasm and then C
@@ -2906,6 +2907,9 @@ def get_full_import_name(name):
29062907
# in standalone mode, crt1 will call the constructors from inside the wasm
29072908
settings.REQUIRED_EXPORTS.append('__wasm_call_ctors')
29082909

2910+
settings.PRE_JS_FILES = [os.path.abspath(f) for f in options.pre_js]
2911+
settings.POST_JS_FILES = [os.path.abspath(f) for f in options.post_js]
2912+
29092913
return target, wasm_target
29102914

29112915

emscripten.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ def optimize_syscalls(declares):
7777
# without filesystem support, it doesn't matter what syscalls need
7878
settings.SYSCALLS_REQUIRE_FILESYSTEM = 0
7979
else:
80+
# TODO(sbc): Find a better way to identify wasi syscalls
8081
syscall_prefixes = ('__syscall_', 'fd_')
81-
syscalls = {d for d in declares if d.startswith(syscall_prefixes)}
82+
syscalls = {d for d in declares if d.startswith(syscall_prefixes) or d in ['path_open']}
8283
# check if the only filesystem syscalls are in: close, ioctl, llseek, write
8384
# (without open, etc.. nothing substantial can be done, so we can disable
8485
# extra filesystem support in that case)

src/generated_struct_info32.json

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,49 @@
394394
"__WASI_CLOCKID_PROCESS_CPUTIME_ID": 2,
395395
"__WASI_CLOCKID_REALTIME": 0,
396396
"__WASI_CLOCKID_THREAD_CPUTIME_ID": 3,
397+
"__WASI_FDFLAGS_APPEND": 1,
398+
"__WASI_FDFLAGS_DSYNC": 2,
399+
"__WASI_FDFLAGS_NONBLOCK": 4,
400+
"__WASI_FDFLAGS_RSYNC": 8,
401+
"__WASI_FDFLAGS_SYNC": 16,
397402
"__WASI_FILETYPE_CHARACTER_DEVICE": 2,
398403
"__WASI_FILETYPE_DIRECTORY": 3,
399404
"__WASI_FILETYPE_REGULAR_FILE": 4,
400-
"__WASI_FILETYPE_SYMBOLIC_LINK": 7
405+
"__WASI_FILETYPE_SYMBOLIC_LINK": 7,
406+
"__WASI_OFLAGS_CREAT": 1,
407+
"__WASI_OFLAGS_DIRECTORY": 2,
408+
"__WASI_OFLAGS_EXCL": 4,
409+
"__WASI_OFLAGS_TRUNC": 8,
410+
"__WASI_PREOPENTYPE_DIR": 0,
411+
"__WASI_RIGHTS_FD_ADVISE": 128,
412+
"__WASI_RIGHTS_FD_ALLOCATE": 256,
413+
"__WASI_RIGHTS_FD_DATASYNC": 1,
414+
"__WASI_RIGHTS_FD_FDSTAT_SET_FLAGS": 8,
415+
"__WASI_RIGHTS_FD_FILESTAT_GET": 2097152,
416+
"__WASI_RIGHTS_FD_FILESTAT_SET_SIZE": 4194304,
417+
"__WASI_RIGHTS_FD_FILESTAT_SET_TIMES": 8388608,
418+
"__WASI_RIGHTS_FD_READ": 2,
419+
"__WASI_RIGHTS_FD_READDIR": 16384,
420+
"__WASI_RIGHTS_FD_SEEK": 4,
421+
"__WASI_RIGHTS_FD_SYNC": 16,
422+
"__WASI_RIGHTS_FD_TELL": 32,
423+
"__WASI_RIGHTS_FD_WRITE": 64,
424+
"__WASI_RIGHTS_PATH_CREATE_DIRECTORY": 512,
425+
"__WASI_RIGHTS_PATH_CREATE_FILE": 1024,
426+
"__WASI_RIGHTS_PATH_FILESTAT_GET": 262144,
427+
"__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE": 524288,
428+
"__WASI_RIGHTS_PATH_FILESTAT_SET_TIMES": 1048576,
429+
"__WASI_RIGHTS_PATH_LINK_SOURCE": 2048,
430+
"__WASI_RIGHTS_PATH_LINK_TARGET": 4096,
431+
"__WASI_RIGHTS_PATH_OPEN": 8192,
432+
"__WASI_RIGHTS_PATH_READLINK": 32768,
433+
"__WASI_RIGHTS_PATH_REMOVE_DIRECTORY": 33554432,
434+
"__WASI_RIGHTS_PATH_RENAME_SOURCE": 65536,
435+
"__WASI_RIGHTS_PATH_RENAME_TARGET": 131072,
436+
"__WASI_RIGHTS_PATH_SYMLINK": 16777216,
437+
"__WASI_RIGHTS_PATH_UNLINK_FILE": 67108864,
438+
"__WASI_RIGHTS_POLL_FD_READWRITE": 134217728,
439+
"__WASI_RIGHTS_SOCK_SHUTDOWN": 268435456
401440
},
402441
"structs": {
403442
"EmscriptenBatteryEvent": {
@@ -1236,6 +1275,26 @@
12361275
"fs_rights_base": 8,
12371276
"fs_rights_inheriting": 16
12381277
},
1278+
"__wasi_filestat_t": {
1279+
"__size__": 64,
1280+
"atim": 40,
1281+
"ctim": 56,
1282+
"dev": 0,
1283+
"filetype": 16,
1284+
"ino": 8,
1285+
"mtim": 48,
1286+
"nlink": 24,
1287+
"size": 32
1288+
},
1289+
"__wasi_prestat_dir_t": {
1290+
"__size__": 4,
1291+
"pr_name_len": 0
1292+
},
1293+
"__wasi_prestat_t": {
1294+
"__size__": 8,
1295+
"pr_type": 0,
1296+
"u": 4
1297+
},
12391298
"addrinfo": {
12401299
"__size__": 32,
12411300
"ai_addr": 20,

src/generated_struct_info64.json

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,49 @@
394394
"__WASI_CLOCKID_PROCESS_CPUTIME_ID": 2,
395395
"__WASI_CLOCKID_REALTIME": 0,
396396
"__WASI_CLOCKID_THREAD_CPUTIME_ID": 3,
397+
"__WASI_FDFLAGS_APPEND": 1,
398+
"__WASI_FDFLAGS_DSYNC": 2,
399+
"__WASI_FDFLAGS_NONBLOCK": 4,
400+
"__WASI_FDFLAGS_RSYNC": 8,
401+
"__WASI_FDFLAGS_SYNC": 16,
397402
"__WASI_FILETYPE_CHARACTER_DEVICE": 2,
398403
"__WASI_FILETYPE_DIRECTORY": 3,
399404
"__WASI_FILETYPE_REGULAR_FILE": 4,
400-
"__WASI_FILETYPE_SYMBOLIC_LINK": 7
405+
"__WASI_FILETYPE_SYMBOLIC_LINK": 7,
406+
"__WASI_OFLAGS_CREAT": 1,
407+
"__WASI_OFLAGS_DIRECTORY": 2,
408+
"__WASI_OFLAGS_EXCL": 4,
409+
"__WASI_OFLAGS_TRUNC": 8,
410+
"__WASI_PREOPENTYPE_DIR": 0,
411+
"__WASI_RIGHTS_FD_ADVISE": 128,
412+
"__WASI_RIGHTS_FD_ALLOCATE": 256,
413+
"__WASI_RIGHTS_FD_DATASYNC": 1,
414+
"__WASI_RIGHTS_FD_FDSTAT_SET_FLAGS": 8,
415+
"__WASI_RIGHTS_FD_FILESTAT_GET": 2097152,
416+
"__WASI_RIGHTS_FD_FILESTAT_SET_SIZE": 4194304,
417+
"__WASI_RIGHTS_FD_FILESTAT_SET_TIMES": 8388608,
418+
"__WASI_RIGHTS_FD_READ": 2,
419+
"__WASI_RIGHTS_FD_READDIR": 16384,
420+
"__WASI_RIGHTS_FD_SEEK": 4,
421+
"__WASI_RIGHTS_FD_SYNC": 16,
422+
"__WASI_RIGHTS_FD_TELL": 32,
423+
"__WASI_RIGHTS_FD_WRITE": 64,
424+
"__WASI_RIGHTS_PATH_CREATE_DIRECTORY": 512,
425+
"__WASI_RIGHTS_PATH_CREATE_FILE": 1024,
426+
"__WASI_RIGHTS_PATH_FILESTAT_GET": 262144,
427+
"__WASI_RIGHTS_PATH_FILESTAT_SET_SIZE": 524288,
428+
"__WASI_RIGHTS_PATH_FILESTAT_SET_TIMES": 1048576,
429+
"__WASI_RIGHTS_PATH_LINK_SOURCE": 2048,
430+
"__WASI_RIGHTS_PATH_LINK_TARGET": 4096,
431+
"__WASI_RIGHTS_PATH_OPEN": 8192,
432+
"__WASI_RIGHTS_PATH_READLINK": 32768,
433+
"__WASI_RIGHTS_PATH_REMOVE_DIRECTORY": 33554432,
434+
"__WASI_RIGHTS_PATH_RENAME_SOURCE": 65536,
435+
"__WASI_RIGHTS_PATH_RENAME_TARGET": 131072,
436+
"__WASI_RIGHTS_PATH_SYMLINK": 16777216,
437+
"__WASI_RIGHTS_PATH_UNLINK_FILE": 67108864,
438+
"__WASI_RIGHTS_POLL_FD_READWRITE": 134217728,
439+
"__WASI_RIGHTS_SOCK_SHUTDOWN": 268435456
401440
},
402441
"structs": {
403442
"EmscriptenBatteryEvent": {
@@ -1236,6 +1275,26 @@
12361275
"fs_rights_base": 8,
12371276
"fs_rights_inheriting": 16
12381277
},
1278+
"__wasi_filestat_t": {
1279+
"__size__": 64,
1280+
"atim": 40,
1281+
"ctim": 56,
1282+
"dev": 0,
1283+
"filetype": 16,
1284+
"ino": 8,
1285+
"mtim": 48,
1286+
"nlink": 24,
1287+
"size": 32
1288+
},
1289+
"__wasi_prestat_dir_t": {
1290+
"__size__": 8,
1291+
"pr_name_len": 0
1292+
},
1293+
"__wasi_prestat_t": {
1294+
"__size__": 16,
1295+
"pr_type": 0,
1296+
"u": 8
1297+
},
12391298
"addrinfo": {
12401299
"__size__": 48,
12411300
"ai_addr": 24,

src/library_sigs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,11 @@ sigs = {
10011001
lineRGBA__sig: 'ipiiiiiiii',
10021002
pixelRGBA__sig: 'ipiiiiii',
10031003
proc_exit__sig: 'vi',
1004+
random_get__sig: 'ipp',
10041005
rectangleColor__sig: 'ipiiiii',
10051006
rectangleRGBA__sig: 'ipiiiiiiii',
10061007
rotozoomSurface__sig: 'ppddi',
1008+
sched_yield__sig: 'i',
10071009
setprotoent__sig: 'vi',
10081010
strftime__sig: 'ppppp',
10091011
strftime_l__sig: 'pppppp',

src/library_syscall.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,18 @@ function wrapSyscallFunction(x, library, isWasi) {
994994
var canThrow = library[x + '__nothrow'] !== true;
995995
#endif
996996

997-
var pre = '', post = '';
997+
if (!library[x + '__deps']) library[x + '__deps'] = [];
998+
999+
#if PURE_WASI
1000+
// In PURE_WASI mode we can't assume the wasm binary was built by emscripten
1001+
// and politely notify us on memory growth. Instead we have to check for
1002+
// possible memory growth on each syscall.
1003+
var pre = '\nif (!HEAPU8.byteLength) _emscripten_notify_memory_growth(0);\n'
1004+
library[x + '__deps'].push('emscripten_notify_memory_growth');
1005+
#else
1006+
var pre = '';
1007+
#endif
1008+
var post = '';
9981009
if (isVariadic) {
9991010
pre += 'SYSCALLS.varargs = varargs;\n';
10001011
}
@@ -1047,7 +1058,6 @@ function wrapSyscallFunction(x, library, isWasi) {
10471058
}
10481059

10491060
library[x] = eval('(' + t + ')');
1050-
if (!library[x + '__deps']) library[x + '__deps'] = [];
10511061
library[x + '__deps'].push('$SYSCALLS');
10521062
#if PTHREADS
10531063
// Most syscalls need to happen on the main JS thread (e.g. because the

0 commit comments

Comments
 (0)