Skip to content

Commit 36fb419

Browse files
dgryskiclaude
andcommitted
wasip2: build against wasi-libc instead of the Go mini-libc
Wasip2 previously got its libc from a ~1300-line hand-written Go implementation (src/syscall/libc_wasip2.go) calling wasi:cli/filesystem/io component bindings directly. wasi-libc upstream already ships a C implementation of the same wasip2 surface (wasip2.c, wasip2_stdio.c, descriptor_table.c, socket support), generated from the same WIT interfaces, so link wasip2 against that instead. - builder/wasilibc.go: turn libWasiLibc into a newWasiLibc(preview, name) factory. libWasiLibcWasip2 builds with __wasip2__ defined and the descriptor-table-based socket/stdio sources instead of wasip1's cloudlibc syscalls. Distinct library name since the build cache is keyed by name+triple and both targets share the same wasm32 triple. - builder/build.go / targets/wasip2.json: route the "wasi-libc" case to the wasip2 variant when the wasip2 build tag is set. - src/syscall/syscall_libc.go and syscall_libc_wasi.go were already shared between wasip1/wasip2 (linker-resolved //export symbols), so they needed no changes. env_libc.go/errno_wasilibc.go are now shared too; the wasip2-only mini-libc files are deleted. - Environ() needed to move to wasip1/wasip2-specific files (environ_libc.go / environ_wasip2.go): wasi-libc's eager, constructor-based environ population is fine for wasip1 (a plain core wasm module) but traps under wasip2's component model when it fires reentrantly during cabi_realloc ("cannot leave component instance"). wasip2 uses wasi-libc's documented lazy __wasilibc_get_environ() accessor instead. - runtime_wasip2.go: cabi_realloc now goes through the malloc-tracked libc_realloc instead of the GC-internal realloc, since wasi-libc's C code (e.g. wasip2_string_free) frees buffers allocated during component-ABI lifting with a plain free(). - lib/wasi-libc-wasip2-stub.c: satisfies wasip2.c's forced-link reference to wasi-libc's own crt1 component-type metadata, which TinyGo doesn't build (it provides its own entry points and its own component-embedding step). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 657e1dc commit 36fb419

16 files changed

Lines changed: 396 additions & 1697 deletions

builder/build.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
167167
defer unlock()
168168
libcDependencies = append(libcDependencies, libcJob)
169169
case "wasi-libc":
170-
libcJob, unlock, err := libWasiLibc.load(config, tmpdir)
170+
lib := libWasiLibc
171+
if slices.Contains(config.BuildTags(), "wasip2") {
172+
lib = libWasiLibcWasip2
173+
}
174+
libcJob, unlock, err := lib.load(config, tmpdir)
171175
if err != nil {
172176
return BuildResult{}, err
173177
}

0 commit comments

Comments
 (0)