WebAssembly / WebAssembly/wabt

wasm2c in freestanding environment

Open
#2,460 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
8.1k
Forks
827
Avg merge
4d 6h
Merged PRs (30d)
18

Description

Opening this issue as a narrowed-down version of https://github.com/WebAssembly/wabt/issues/1949 to track freestanding-related questions.

I didn't end up using wasm2c in the original use case mentioned there for reasons completely unrelated to wasm, but I very recently considered using it for another task, again in the same environment (kernel out of tree driver, basically identical to embedded development context).

The only remaining issue seems to be:

  1. the inclusion of helper functions for some opcode, like wasm_trunc().
  2. the inclusion of some stdlib headers that may not be available in a freestanding environment [1]

Point 1. is relatively easy to deal with by providing custom trapping implementations of the libc functions they rely on, but it would be nice for embedded use case to simply fail to compile floating point opcodes, rather than emitting a call to something.

Headers of point 2. can be removed by editing the C file after generation or providing them with an empty content, but it would be nicer to allow direct customization of the content. As it stands, it's stored in ./src/template/wasm2c.includes.c which then becomes part of the wasm2c binary itself.

In terms of how they are used, it looks like:

  • alloca.h is only needed for exception handling, which are not enabled by default
  • math.h is only needed for floating point opcode-supporting functions (like wasm_trunc())
  • what is relied on in the other headers should be available everywhere without troubles.

So overall it's looking pretty good.

[1] A C freestanding environment is allowed to only distribute the following headers, which does not contain e.g. math.h: <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>
https://port70.net/~nsz/c/c11/n1570.html#4p6
The kernel does not even have float.h AFAICT.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/template/wasm2c.includes.c and the wasm2c generation path; inspect how wasm_trunc() and the listed headers enter generated C. Done means freestanding builds can customize or omit unavailable headers and avoid unsupported floating-point helpers, with the generated output checked for the requested configurations.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cpp, wasm
Domain
compilers, embedded-iot, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.