Skip to content

Add support for request-time WebAssembly compilation - #6938

Open
danlapid wants to merge 1 commit into
mainfrom
dlapid/requestTimeWasmCompilation
Open

Add support for request-time WebAssembly compilation#6938
danlapid wants to merge 1 commit into
mainfrom
dlapid/requestTimeWasmCompilation

Conversation

@danlapid

Copy link
Copy Markdown
Collaborator

Add the request_time_webassembly_compilation compatibility flag and keep the feature disabled by default.

Wrap WebAssembly.compile(), byte-based instantiate(), and Module() to snapshot exact input bytes before compilation.
Await retention for promise APIs and queue best-effort retention for the synchronous constructor.

Expose embedder upload hooks and per-request admission accounting while keeping eval() and Function() disabled. Preserve startup compilation and existing WebAssembly API behavior.

@danlapid
danlapid requested review from a team as code owners August 10, 2026 04:05

@ask-bonk ask-bonk Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm Bonk, and I've done a quick review of your PR.

This PR adds a request_time_webassembly_compilation experimental compat flag that enables request-time WebAssembly.compile(), byte-based instantiate(), and new Module() by wrapping them with a shim that snapshots input bytes before compilation and records them via embedder hooks.

Overall this is well-structured: the compat flag is correctly $experimental with no enable date, the JS shim captures trusted intrinsics to prevent tampering, the C++ callback properly bridges V8/KJ exceptions, and the shim layering with wasm-instantiate-shim.js composes correctly. Two findings:

  1. [MEDIUM] Hardcoded limit in error message may drift from the production implementation.
  2. [LOW] originalCompile is captured but never used in the JS shim.

This review was generated by an AI assistant and may contain inaccuracies.

Comment thread src/workerd/io/worker.c++ Outdated
Comment thread src/workerd/io/wasm-codegen-shim.js
@ask-bonk

ask-bonk Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

I'm Bonk, and I've done a quick review of your PR.

This PR adds a request_time_webassembly_compilation experimental compat flag that enables request-time WebAssembly.compile(), byte-based instantiate(), and new Module() by wrapping them with a JS shim that snapshots input bytes before compilation and records them via embedder hooks (uploadWasmForCodeGeneration / queueWasmUploadForCodeGeneration).

What looks good:

  • Compat flag is correctly $experimental with no enable date -- no risk of ungated behavioral changes
  • JS shim captures trusted intrinsics (Reflect.apply, Promise.prototype.then, etc.) before user code can tamper with them
  • The shim layering with wasm-instantiate-shim.js composes correctly: codegen shim captures the already-wrapped Instance from the instantiate shim, so shutdown registration still works
  • C++ callback properly bridges V8/KJ exceptions via kj::runCatchingExceptions + ThrowException
  • Memory ownership is clean: source.copy() creates independent copies for the V8 snapshot ArrayBuffer and the upload bytes
  • All IoChannelFactory subclasses (2 direct, 3 indirect) implement the new pure virtual methods
  • Both positive and negative tests are included

Two findings posted as inline review comments:

  1. [MEDIUM] Error message hardcodes "100 unique WebAssembly modules" but the actual limit is enforced by the production recordLoadedWasmModule() override -- suggested a generic message instead.
  2. [LOW] originalCompile is captured but never used in the JS shim.

github run

@github-actions

github-actions Bot commented Aug 10, 2026

Copy link
Copy Markdown

The generated output of @cloudflare/workers-types has been changed by this PR. If this is intentional, run just generate-types to update the snapshot. Alternatively, you can download the full generated types:

Full Type Diff
diff -r types/generated-snapshot/index.d.ts bazel-bin/types/definitions/index.d.ts
266c266,271
<   abstract class Module {
---
>   interface CompileOptions {
>     builtins?: string[];
>     importedStringConstants?: string;
>   }
>   class Module {
>     constructor(bytes: BufferSource, options?: CompileOptions);
283a289,301
>   interface WebAssemblyInstantiatedSource {
>     instance: Instance;
>     module: Module;
>   }
>   function compile(
>     bytes: BufferSource,
>     options?: CompileOptions,
>   ): Promise<Module>;
>   function instantiate(
>     bytes: BufferSource,
>     imports?: Imports,
>     options?: CompileOptions,
>   ): Promise<WebAssemblyInstantiatedSource>;
diff -r types/generated-snapshot/index.ts bazel-bin/types/definitions/index.ts
266c266,271
<   abstract class Module {
---
>   interface CompileOptions {
>     builtins?: string[];
>     importedStringConstants?: string;
>   }
>   class Module {
>     constructor(bytes: BufferSource, options?: CompileOptions);
283a289,301
>   interface WebAssemblyInstantiatedSource {
>     instance: Instance;
>     module: Module;
>   }
>   function compile(
>     bytes: BufferSource,
>     options?: CompileOptions,
>   ): Promise<Module>;
>   function instantiate(
>     bytes: BufferSource,
>     imports?: Imports,
>     options?: CompileOptions,
>   ): Promise<WebAssemblyInstantiatedSource>;

@danlapid
danlapid force-pushed the dlapid/requestTimeWasmCompilation branch 4 times, most recently from c7dc9e0 to 513bc91 Compare August 10, 2026 04:53
Add the request_time_webassembly_compilation compatibility flag and keep
the feature disabled by default.

Wrap WebAssembly.compile(), byte-based instantiate(), and Module() to
snapshot exact input bytes before compilation.
Await retention for promise APIs and queue best-effort retention for the
synchronous constructor.

Expose embedder upload hooks and per-request admission accounting while
keeping eval() and Function() disabled. Preserve startup compilation and
existing WebAssembly API behavior.
@danlapid
danlapid force-pushed the dlapid/requestTimeWasmCompilation branch from 513bc91 to b8462ed Compare August 10, 2026 04:53
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