Hi all,
My name is Stan. I wanted to announce that I have started working on the WASI support for the IC under the Developer Grant Program.
Background
WebAssembly System Interface (WASI) is emerging as the standard target for compiling WebAssembly (Wasm) programs. WASI provides a set of functions to access system resources such as the file system, network, and environment variables regardless of the underlying operating system. This allows Wasm programs to be run in a variety of environments, including the web, cloud, and embedded devices.
Many popular programming languages, including JavaScript (SpiderMonkey engine), Python (CPython), C++, Go (TinyGo), Haskell (GHC), Swift, and Ruby, support WASI as a compilation target. A popular database Sqlite can also be compiled to WASI.
Project Description
The goal of the project is to use the IC’s System API to implement the core WASI functions, effectively polyfilling WASI for the IC. This would allow developers to take advantage of the WASI ecosystem and run WASI-compatible programs on the Internet Computer.
The set of WASI functions consists of the following groups:
- Accessing the program arguments and environment variables:
- args_get(), args_sizes_get(), environ_get(), environ_sizes_get().
- Getting the current time:
- clock_res_get(), clock_time_get().
- Working with files and paths:
- fd_advise(), fd_allocate(), fd_close(), fd_datasync(), fd_fdstat_get(), fd_fdstat_set_flags(), fd_fdstat_set_rights(), fd_filestat_get(), fd_filestat_set_size(), fd_filestat_set_times(), fd_pread(), fd_prestat_get(), fd_prestat_dir_name(), fd_pwrite(), fd_read(), fd_readdir(), fd_renumber(), fd_seek(), fd_sync(), fd_tell(), fd_write(), path_create_directory(), path_filestat_get(), path_filestat_set_times(), path_link(), path_open(), path_readlink(), path_remove_directory(), path_rename(), path_symlink(), path_unlink_file()
- Working with sockets:
- sock_accept(), sock_recv(), sock_send(), sock_shutdown().
- Polling on time and file events:
- poll_oneoff()
- Getting randomness:
- random_get()
- Other:
- proc_exit(), proc_raise(), sched_yield().
Polyfilling groups 1, 2, 5, 6, 7 should be straightforward with a caveat that currently randomness is not available at canister installation time, which will make random_get() pseudorandom in the initial version. True randomness would require protocol changes to provide randomness during canister installation time. The plan for group 3 is to implement the simplest possible file system on top of IC’s stable memory and stable-structures. This is where most of the work is expected to happen. Group 4 will not be supported because sockets are not required to run most of the WASI programs mentioned in the Background section.
Additionally, I am planning to write a command line tool to parse Wasm binaries and substitute the WASI imports with their implementations from the polyfill library.
The workflow for an IC developer utilizing WASI will look as follows:
1. Write a WASI program in any language that supports wasm32-wasi.
2. Incorporate a build step to my command line tool to replace WASI dependencies.
3. Run the Wasm binary on the IC.
I’ll post more updates in this thread once I have the initial prototype implementation.