Skip to content

feat: update README and WASM memory management for TinyGo compatibility - #11

Merged
pikann merged 3 commits into
masterfrom
feature/update-readme-and-wasm-memory-management
Aug 22, 2026
Merged

pikann merged 3 commits into
masterfrom
feature/update-readme-and-wasm-memory-management

Conversation

@pikann

@pikann pikann commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes plugin builds under TinyGo, which is the recommended compiler per the README but was previously broken in two ways when actually used against the host runtime.

  • wasm_exports.go: Export the SDK's allocator functions as paca_malloc/paca_free instead of malloc/free. TinyGo's bundled wasi-libc already exports its own malloc/free, so exporting under the same names produced a WASM module with duplicate exports — invalid per spec and rejected at load time.
  • wasm_backends.go: Shrink mallocBuffer from 10MB to 1MB to match services/api's DefaultResourceLimits.MaxRequestBodyBytes (paca/services/api/internal/platform/plugin/runtime.go). The host already rejects any payload larger than that before writing to this buffer, so 10MB was unnecessary — and since every loaded plugin gets its own independent copy of this buffer, the smaller size meaningfully reduces the host's per-plugin memory footprint.
  • README.md: Update the build instructions to use tinygo build -target=wasip1 -buildmode=c-shared. -buildmode=c-shared is required, not optional — without it, TinyGo doesn't wire up the WASI reactor entry point (_initialize) this SDK's runtime depends on, and the plugin panics at load with "//go:wasmexport function called before runtime initialization" (it still compiles fine without the flag, so this only surfaces once the host actually loads the plugin). Also documents the standard-Go fallback for TinyGo stdlib gaps, and a required workaround (wrap in a closure) for plugins that declare their own //go:wasmimport host function and pass it to CallHostFunction, since TinyGo doesn't support taking the address of a go:wasmimport function.

Test plan

  • Build a plugin with tinygo build -target=wasip1 -buildmode=c-shared and confirm it loads and handles requests against the host runtime
  • Confirm the module has no duplicate malloc/free exports
  • Confirm CallHostFunction with a closure-wrapped //go:wasmimport function builds under both TinyGo and standard Go

@greptile-apps

greptile-apps Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates the documented WASI build process for TinyGo and changes WASM memory management to avoid TinyGo allocator-export collisions.

  • Renames the guest allocator exports from malloc/free to paca_malloc/paca_free.
  • Reduces the fixed allocator arena from 10 MiB to 1 MiB.
  • Adds TinyGo reactor-mode guidance and a standard Go fallback to the README.

Confidence Score: 3/5

The PR should not merge until the allocator can hold simultaneously live request and response data and the documented standard Go minimum is corrected.

A valid near-limit request can exhaust the newly reduced shared arena while its response is packed, and users on the newly advertised Go 1.21–1.23 range cannot build the documented standard-Go fallback.

Files Needing Attention: wasm_backends.go, README.md

Important Files Changed

Filename Overview
wasm_backends.go Reduces the shared bump-allocation arena to 1 MiB, which cannot hold a near-limit request and its response simultaneously.
wasm_exports.go Renames allocator exports for TinyGo and retains the response allocation flow that shares the request arena until reset.
README.md Adds useful TinyGo reactor guidance but incorrectly advertises the standard Go c-shared WASI path as supported from Go 1.21.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  H[Host] -->|paca_malloc request size| A[1 MiB bump arena]
  H -->|write request| A
  H -->|HandleRequest| P[Plugin handler]
  P -->|serialize response| R[packWASMResult]
  R -->|wasmMalloc response size| A
  A -->|combined size exceeds 1 MiB| F[Allocation returns 0]
  H -->|copy response, then ResetAllocator| X[Reset arena]
Loading

Reviews (1): Last reviewed commit: "feat: update README and WASM memory mana..." | Re-trigger Greptile

Comment thread wasm_backends.go Resolved
Comment thread README.md Resolved
pikann and others added 2 commits August 22, 2026 23:05
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@pikann
pikann merged commit 9c77542 into master Aug 22, 2026
4 checks passed
@pikann
pikann deleted the feature/update-readme-and-wasm-memory-management branch August 22, 2026 16:14
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.

1 participant