Skip to content

Add WasmVm::serialize method to get serialized AOT compiled module#543

Merged
leonm1 merged 13 commits into
proxy-wasm:mainfrom
leonm1:wasmtime/deserialize
May 29, 2026
Merged

Add WasmVm::serialize method to get serialized AOT compiled module#543
leonm1 merged 13 commits into
proxy-wasm:mainfrom
leonm1:wasmtime/deserialize

Conversation

@leonm1
Copy link
Copy Markdown
Contributor

@leonm1 leonm1 commented May 22, 2026

This PR contains both serialization and deserialization since we had no testing surface for deserialization, as pointed out in https://github.com/proxy-wasm/proxy-wasm-cpp-host/pull/522/changes#r2921882220.

This implements serialization and deserialization for wasmtime and V8.

leonm1 and others added 2 commits May 22, 2026 14:58
Co-Authored-By: Rachel Green <rachgreen@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
Copy link
Copy Markdown
Member

@PiotrSikora PiotrSikora left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

Comment thread test/runtime_test.cc Outdated
Comment thread test/runtime_test.cc Outdated
Comment thread src/wasmtime/wasmtime.cc Outdated
leonm1 added 5 commits May 27, 2026 17:19
Signed-off-by: Matt Leon <mattleon@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
@leonm1 leonm1 requested a review from PiotrSikora May 27, 2026 21:26
Comment thread src/wasmtime/wasmtime.cc Outdated
Comment thread test/runtime_test.cc Outdated
Comment thread test/runtime_test.cc Outdated
leonm1 added 3 commits May 28, 2026 17:54
Signed-off-by: Matt Leon <mattleon@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
Signed-off-by: Matt Leon <mattleon@google.com>
Comment thread test/runtime_test.cc Outdated
Comment thread test/runtime_test.cc Outdated
Copy link
Copy Markdown
Member

@PiotrSikora PiotrSikora left a comment

Choose a reason for hiding this comment

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

LGTM, sans the remaining auto nit.

Signed-off-by: Matt Leon <mattleon@google.com>
@leonm1
Copy link
Copy Markdown
Contributor Author

leonm1 commented May 29, 2026

Thanks for your review!

@leonm1 leonm1 enabled auto-merge (squash) May 29, 2026 02:09
Copy link
Copy Markdown
Member

@PiotrSikora PiotrSikora left a comment

Choose a reason for hiding this comment

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

LGTM, sans style.

Comment thread src/wasmtime/wasmtime.cc Outdated

Result<Module> module =
Module::compile(*engine(), std::span((uint8_t *)bytecode.data(), bytecode.size()));
Result<Module> module(::wasmtime::Error("Unable to load Wasm module: zero-length."));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: I don't think you need to declare this ahead of time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a comment. This is a fallback status when both precompiled and bytecode are empty.

If precompiled is not empty, then the error returned by deserialize will be output.

If bytecode is not empty, then the fail on line 197 would not be executed, and module would contain the error from compile().

Copy link
Copy Markdown
Member

@PiotrSikora PiotrSikora May 29, 2026

Choose a reason for hiding this comment

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

Right. I know how it works, but the style in this function is quite inconsistent, and this doesn't help with the readability.

What I meant is that you could write it like this:

if (!precompiled.empty()) {
  auto module = Module::deserialize(*engine(),
                               std::span((uint8_t *)precompiled.data(), precompiled.size()));
  if (!module) {
    fail(FailState::UnableToInitializeCode,
         "Failed to load Wasm module: " + module.err().message());
    return false;
  }

  module_.emplace(module.ok());
  return true;

} else {
  auto module = Module::compile(*engine(), std::span((uint8_t *)bytecode.data(), bytecode.size()));
  if (!module) {
    fail(FailState::UnableToInitializeCode,
         "Failed to load Wasm module: " + module.err().message());
    return false;
  }

  module_.emplace(module.ok());
  return true;
}

Or if you wanted to pre-allocate module and keep the fallback, then something like this:

Result<Module> module(::wasmtime::Error("Unable to load Wasm module: zero-length."));

if (!precompiled.empty()) {
  module = Module::deserialize(*engine(),
                               std::span((uint8_t *)precompiled.data(), precompiled.size()));
  if (module) {
    module_.emplace(module.ok());
    return true;
  }
}

if (!bytecode.empty()) {
  module = Module::compile(*engine(), std::span((uint8_t *)bytecode.data(), bytecode.size()));
  if (module) {
    module_.emplace(module.ok());
    return true;
  }
}

fail(FailState::UnableToInitializeCode, "Failed to load Wasm module: " + module.err().message());
return false;

Also, note that this diverges from V8 and WAMR integrations, and silently(!) ignores an invalid precompiled module, falling back to bytecode compilation.

Comment thread src/wasmtime/wasmtime.cc Outdated
@leonm1 leonm1 disabled auto-merge May 29, 2026 02:56
Signed-off-by: Matt Leon <mattleon@google.com>
@leonm1 leonm1 enabled auto-merge (squash) May 29, 2026 14:58
@leonm1 leonm1 merged commit cfe1bc9 into proxy-wasm:main May 29, 2026
75 of 78 checks passed
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.

2 participants