Skip to content

wasm2c: uvwasi adapter layer for wasm2c output#2782

Draft
shravanrn wants to merge 1 commit into
WebAssembly:mainfrom
UT-Security:wasi-adapter
Draft

wasm2c: uvwasi adapter layer for wasm2c output#2782
shravanrn wants to merge 1 commit into
WebAssembly:mainfrom
UT-Security:wasi-adapter

Conversation

@shravanrn

Copy link
Copy Markdown
Collaborator

Implementation of a uvwasi adapter for wasm2c output

Last discussed in a comment with @sbc100 here a while back
#2395 (comment)

wasm2c can now use uvwasi for the wasi implementation by including wasm-rt-uvwasi-adapter.c (and of course linking libuv ad libuvwasi)

I have added an example of using this adapter in the examples folder.

Full disclosure: While the initial adapter was written by me on an old fork of wasm2c, in a pre-LLM era, I used an LLM to adapt that patch for this PR and the current wasm2c codebase.

LLM Mitigations: I audited every line of the generated code. I fixed several double-fetch bugs, edge cases on assuming null termination and off-by-ones. I also fixed more minor stylistic things to keep the generated code similar to the wasm2c codebase. To the extent possible, I believe my audit has made the code about as secure as it would be if I hand wrote it from scratch.

@shravanrn shravanrn requested review from keithw and sbc100 June 29, 2026 19:52
@shravanrn shravanrn force-pushed the wasi-adapter branch 2 times, most recently from e954a44 to 17eb353 Compare June 30, 2026 19:59
@shravanrn

Copy link
Copy Markdown
Collaborator Author

@sbc100 Just checking if there is interest in landing this, before i spend time rebasing or cleanups

@sbc100

sbc100 commented Jul 13, 2026

Copy link
Copy Markdown
Member

I'm not against landing this, I'm just a little stretched on review time right now.

I can try to get to it soon. We also have @zherczeg who is been quite active recently on the project and could maybe review ?

@shravanrn

shravanrn commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

@sbc100 thanks and no worries! I'll push a rebase soon to keep the diff/merge clean

@zherczeg

Copy link
Copy Markdown
Collaborator

I can review it, but I don't know the context, so I need to understand the purpose of this code first. It adds several files to gitignore, two files to examples, and an adapter. Is it adds some built ins? How wasm and libuw is connected? Is it something wasm-only, or do you need wasm-host communication as well?

return UVWASI_ENOMEM;
}

ret = uvwasi_args_get(wasi->uvwasi, uv_argv, buf_base);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these calls coming from? Which include provides them?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from uvwasi.h that is included in wasm-rt-uvwasi-adapter.h

@shravanrn

shravanrn commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator Author

I can review it, but I don't know the context, so I need to understand the purpose of this code first. It adds several files to gitignore, two files to examples, and an adapter. Is it adds some built ins? How wasm and libuw is connected? Is it something wasm-only, or do you need wasm-host communication as well?

Broadly the idea here is that Wasm programs can be compiled as a "pure compute" Wasm program (target of --target=wasm32-unknown in Clang) or a Wasm program that uses a system interface to do things like access the file system, and perform network requests (target of --target=wasm32-wasi in Clang). The latter is done through a standardized interface called WASI.

For a program compiled to the WASI backend, the Wasm compiler includes a wasi-libc that provides the standard C library functions like fopen and implements this internally treating the WASI APIs like its system call layer. The WASI APIs themselves are expected to be supported by the Wasm runtime. There are some standard WASI API implementations like uvwasi and Wasm compilers can add WASI support simply by writing adapters to uvwasi. I think uvwasi is already used by the wasm interpreter in the wabt project to implement WASI APIs. So the sub-modules in wabt already pull in uvwasi, and the CMakeLists.txt in the wabt repo can be configured to build uvwasi by passing in the flag -DWITH_WASI=ON

This PR implements the uvwasi adapter for wasm2c.

@zherczeg

Copy link
Copy Markdown
Collaborator

Thank you for the explanation.

@zherczeg zherczeg left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks valid. Does the patch contains all wasi 0.1 functions or it is a partial implementation of them?

hello-wasi
hello-wasi.wasm
hello-wasi-wasm.*
test.out

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually tmp files goes to the bin/ build/ or out/ directories when wabt is compiled. Can these files goes there as well (and no need to add them to gitignore)?

uvwasi_t* uvwasi;
};

void wasm2c_uvwasi_link(struct w2c_wasi__snapshot__preview1* wasi,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an extension to wasi interface? If not, can it be a static func?

struct w2c_wasi__snapshot__preview1* wasi,
u32 fd,
u64 filesize) {
return uvwasi_fd_filestat_set_size(wasi->uvwasi, fd, filesize);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this adapter works? Does the wasm file contains external references such as w2c_wasi__snapshot__preview1_fd_filestat_set_size? Then these references are redirected to other functions such as uvwasi_fd_filestat_set_size? Then, when the output .c file is compiled, do you need a wasi library?

TRAP(OOB); \
} while (0);

static inline void* mem_get_checked_pointer(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does wasi use a linear memory in a .c program? Is it better than raw pointers? Do you have multiple w2c_wasi__snapshot__preview1 structures or is it a global value in the .c implementation of wasi?

@shravanrn

Copy link
Copy Markdown
Collaborator Author

@zherczeg Thanks for the review. I'll address these shortly

I also spoke to @keithw offline and he pointed out that this implementation currently traps on bad parameter rather than returning system call error codes. I will need to address this as well. Moving this to a draft, and will convert back to an active PR once I address this

@shravanrn shravanrn marked this pull request as draft July 15, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants