)]}'
{
  "commit": "352c02a423a387a39eb88a6c246736b90bfa50c8",
  "tree": "aa59210f7e8899c15bff1425618b377541817f97",
  "parents": [
    "9af5ded0528ec2a5917a13eedfa1eb22886d3cc6"
  ],
  "author": {
    "name": "Protobuf Team Bot",
    "email": "protobuf-github-bot@google.com",
    "time": "Fri Nov 06 14:01:43 2026 -0800"
  },
  "committer": {
    "name": "Copybara-Service",
    "email": "copybara-worker@google.com",
    "time": "Sat Nov 07 12:05:42 2026 -0800"
  },
  "message": "[rust] Support building and running the Rust protobuf upb kernel on wasm32-unknown-emscripten\n\n### Motivation\n\nWhile Google3 defaults `--//third_party/protobuf/rust:rust_proto_library_kernel`\nto `cpp`, the open-source `protobuf` crate ships only the `upb` kernel\n(`third_party/protobuf/rust/release_crates/google_protobuf/build.rs` compiles\n`libupb`), and Wasm (`wasm32-unknown-emscripten`) targets that want to evaluate\nor build Rust protobufs with the `upb` kernel currently fail at three build\nlayers plus link time.\n\n### The 4 Blockers on `wasm32-unknown-emscripten` and Their Fixes\n\n1. **Blaze `select()` conflict between `:use_upb_kernel` and `force_lite_runtime`**\n   - **Exact error (`wasm_upb_try1.log`)**:\n     ```\n     ERROR: third_party/protobuf/rust/BUILD:80:13: Illegal ambiguous match on\n     configurable attribute \"deps\" in //third_party/protobuf/rust:protobuf:\n     //third_party/protobuf/rust:use_upb_kernel\n     //net/proto2/public:force_lite_runtime\n     Multiple matches are not allowed unless one is unambiguously more specialized.\n     ```\n   - **Why `--features\u003d-proto_force_lite_runtime` cannot work around it (`wasm_upb_try2.log`)**:\n     ```\n     ERROR: The C++ toolchain \u0027//third_party/cpptoolchains/portable_llvm/stable:cc_toolchain_wasm\u0027\n     unconditionally implies feature \u0027proto_force_lite_runtime\u0027, which is disabled by command line\n     ```\n   - **Fix (`third_party/protobuf/rust/BUILD`)**:\n     - `deps`: add a `:use_cpp_kernel` `config_setting`\n       (`rust_proto_library_kernel \u003d \"cpp\"`) and a\n       `selects.config_setting_group(name \u003d \"force_lite_runtime_cpp_kernel\",\n       match_all \u003d [\":use_cpp_kernel\", \"//net/proto2/public:force_lite_runtime\"])`,\n       replacing `//net/proto2/public:force_lite_runtime` in the `deps` `select()`.\n     - `rustc_flags` of `:protobuf` and `:protobuf_lite`: split into two\n       concatenated, orthogonal `select()`s (kernel: `--cfg\u003dupb_kernel` /\n       `--cfg\u003dcpp_kernel`; `//net/proto2/public:force_lite_runtime`:\n       `--cfg\u003dlite_runtime`). cpp and cpp + force-lite get exactly the same flags\n       as before; upb + force-lite now also gets `--cfg\u003dlite_runtime`, gating off the\n       `not(lite_runtime)` items in `protobuf.rs` (so it reduces to\n       `pub use protobuf_lite::*`) to match the lite gencode that `aspects.bzl`\n       emits under `proto_force_lite_runtime` for both kernels.\n\n2. **`linkme::distributed_slice` unsupported on `target_os \u003d \"emscripten\"`**\n   - **Exact error (`wasm_upb_try3.log`)**:\n     ```\n     error: distributed_slice is not implemented for this platform\n       --\u003e third_party/protobuf/rust/upb_kernel/extension.rs:40:1\n        |\n     40 | #[linkme::distributed_slice]\n        | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n     error[E0425]: cannot find value `LINKME_START_EXTENSIONS` in this scope\n     error[E0425]: cannot find value `LINKME_STOP_EXTENSIONS` in this scope\n     error[E0425]: cannot find value `LINKME_DUPCHECK_START_EXTENSIONS` in this scope\n     error[E0425]: cannot find value `LINKME_DUPCHECK_STOP_EXTENSIONS` in this scope\n     ```\n   - **Root cause**: `linkme` 0.3.35 has no `target_os \u003d \"emscripten\"` backend\n     (the per-OS `link_section` `cfg_attr` list in `third_party/rust/linkme_impl/v0_3/src/declaration.rs`), and\n     cannot be extended directly: on Wasm targets (`is_like_wasm`), `rustc` emits\n     `#[link_section]` statics as non-addressable Wasm custom sections, with a\n     hardcoded exception only for `.init_array*` sections\n     (`rustc_codegen_llvm/src/consts.rs:545-560`).\n   - **Fix (`third_party/protobuf/rust/upb_kernel/extension.rs`)**: Gate\n     `#[linkme::distributed_slice]` on `#[cfg(not(target_os \u003d \"emscripten\"))]`. On\n     `#[cfg(target_os \u003d \"emscripten\")]`:\n     - Provide an empty `pub static EXTENSIONS: linkme::DistributedSlice\u003c[LazyLock\u003cMiniTableExtensionInitPtr\u003e]\u003e`\n       in the value namespace so generated `#[linkme::distributed_slice($pbr$::EXTENSIONS)]`\n       attributes continue to type-check without modifying `protoc`\u0027s Rust code\n       generator (`compiler/rust/extension.cc`).\n     - Export `#[macro_export] macro_rules! __protobuf_upb_emscripten_extensions`\n       re-exported as `pub use __protobuf_upb_emscripten_extensions as EXTENSIONS;`\n       in the macro namespace. `#[linkme::distributed_slice($pbr$::EXTENSIONS)]`\n       expands to `$pbr$::EXTENSIONS! { #![linkme_macro \u003d ...] pub static ... }`\n       (`third_party/rust/linkme_impl/v0_3/src/element.rs`, `expand`), which\n       our macro intercepts to emit the extension\u0027s `LazyLock\u003cMiniTableExtensionInitPtr\u003e`\n       static alongside a `#[used] #[unsafe(link_section \u003d \".init_array.101\")]`\n       constructor that pushes `\u0026\u0027static LazyLock\u003cMiniTableExtensionInitPtr\u003e` into\n       `EMSCRIPTEN_EXTENSIONS` during `__wasm_call_ctors` (`_initialize`).\n     - In `generated_extension_registry()`, iterate `EMSCRIPTEN_EXTENSIONS` and\n       register each extension in the `upb_ExtensionRegistry`.\n\n3. **Unstable `core::arch::global_asm!` on `wasm32`**\n   - **Exact error (`wasm_upb_try3.log`)**:\n     ```\n     error[E0658]: inline assembly is not stable yet on this architecture\n       --\u003e third_party/protobuf/rust/upb_kernel/extension.rs:44:1\n        |\n     44 | / core::arch::global_asm!(\n     ...  |\n     55 | | );\n        | |_^\n     ```\n   - **Root cause**: `wasm32-unknown-emscripten` sets `target_family \u003d \"unix\"`,\n     so it matches the ELF sentinel `global_asm!` gate.\n   - **Fix (`third_party/protobuf/rust/upb_kernel/extension.rs`)**: Add\n     `not(target_os \u003d \"emscripten\")` to both `global_asm!` gates (head already\n     excludes `target_vendor \u003d \"apple\"`).\n\n4. **Emscripten link failure when `longjmp \u003d \"off\"`**\n   - **Exact error (`wasm_upb_try4.log`)**:\n     ```\n     emcc: error: \u0027.../wasm-ld ...\u0027:\n     wasm-ld: error: ...: undefined symbol: __wasm_longjmp\n     error: longjmp support was disabled (SUPPORT_LONGJMP\u003d0), but it is required by the code (either set SUPPORT_LONGJMP\u003d1, or remove uses of it in the project)\n     ```\n   - **Root cause**: `upb`\u0027s wire decoder (`upb_Decode`), wire encoder\n     (`upb_Encode`), and MiniTable builder (`upb_MiniTable_Build`) use\n     `UPB_SETJMP` / `UPB_LONGJMP` (`setjmp` / `longjmp` in\n     `third_party/upb/upb/port/def.inc`) for error unwinding.\n   - **Requirement (documented in `third_party/protobuf/rust/test/upb/BUILD` and\n     `wasm_extension_test.rs`)**: `wasm_cc_binary` targets linking `upb` must set\n     `longjmp \u003d \"wasm\"` (Wasm Exception Handling instructions, required when\n     `standalone \u003d True`), use the default `longjmp \u003d \"emscripten\"`, or compile\n     `upb` with `--copt\u003d-DWASM_WAMR\u003d1` (which replaces `UPB_SETJMP`/`UPB_LONGJMP`\n     with `0`/`abort()`).\n\n### Caveats\n\n- **Unreferenced extension-only crates on `wasm32`**: On `wasm32`, `rustc`\n  sets `has_symbols_obj \u003d false` (`Target::object_architecture` returns `None`,\n  so `add_linked_symbol_object` in `rustc_codegen_ssa/src/back/link.rs` emits no\n  `symbols.o` table of `#[used]` statics into `.rlib` archives). Consequently,\n  when `wasm-ld` links an `.rlib` archive without `--whole-archive`, an object\n  file containing an `.init_array` extension constructor is extracted only if at\n  least one symbol from that codegen unit is referenced by the binary. Extensions\n  in crates that are never referenced by any code path will not have their\n  `.init_array` constructors linked on Wasm (whereas native `linkme` retains them\n  via `symbols.o`).\n- **Constructor priority `.init_array.101` + late-registration check**:\n  Bare `.init_array` entries run at default priority `65535`. If another static\n  constructor at default priority parses a message before all extension\n  constructors have run, `generated_extension_registry()` would freeze the\n  `LazyLock` registry early. The macro therefore places constructors in\n  `.init_array.101`, and `__emscripten_register_extension` asserts via\n  `debug_assert!` that `EMSCRIPTEN_REGISTRY_BUILT` is still `false`.\n- **Copybara compatibility**: `load(\"//third_party/bazel_skylib/lib:selects.bzl\", \"selects\")`\n  in `third_party/protobuf/rust/BUILD` and the Google3-only Wasm targets/loads in\n  `third_party/protobuf/rust/test/upb/BUILD` (`wasm_cc_binary`, `build_test`,\n  `rust_binary`) are annotated with `# copybara:strip` /\n  `# copybara:strip_begin` / `# copybara:strip_end_and_replace_begin` so OSS\n  Bazel exports remain clean.\n- **Second linkme slice (`EXTENSION_MINI_TABLE_TO_DEF_INITS`, reflection)**: both\n  linkme slices (`EXTENSIONS` and `EXTENSION_MINI_TABLE_TO_DEF_INITS`) are gated\n  off on Emscripten; `EXTENSION_MINI_TABLE_TO_DEF_INITS` elements are registered\n  from `.init_array.101` constructors like `EXTENSIONS`, and `def_pool.rs`\n  `load_all_extensions` reads those registrations on Emscripten.\n- **linkme internals**: the Emscripten path relies on `#[doc(hidden)]\n  linkme::DistributedSlice::private_new` and linkme_impl\u0027s `#![linkme_macro]` /\n  `#![linkme_sort_key]` expansion grammar (verified against 0.3.35), so the OSS\n  crate pins `linkme \u003d \"\u003d0.3.35\"` in\n  `rust/release_crates/google_protobuf/Cargo-template.toml` (linkme itself pins\n  `linkme-impl \u003d \"\u003d0.3.35\"` for the same reason). The exact pin applies to\n  every OSS user, so a dependency graph that needs linkme \u003e\u003d 0.3.36 will not\n  resolve until the Emscripten path is re-verified and the pin bumped; this is\n  a deliberate trade-off.\n- **Test coverage**: on TAP, `wasm_extension_test` runs natively (the\n  `target_os \u003d \"emscripten\"` code is compiled out) and `wasm_extension_build_test`\n  only compiles and links `:wasm_extension_wasm`. Executing the Emscripten\n  registration path (`node .../wasm_extension_bin.js`) and building\n  `//third_party/protobuf/rust:protobuf` with upb on wasm32 are manual steps\n  (see Tests Run).\n- **Exported constructor globals on Emscripten**: On `wasm32-unknown-emscripten`,\n  `#[used]` on `static __PROTOBUF_EXTENSION_CTOR` causes LLVM\u0027s Wasm backend to\n  set `WASM_SYMBOL_EXPORTED`, so `wasm-ld`/`emcc` emits a Wasm global and export\n  table entry per registered extension. Eliminate-at-compile-time retention\n  without exporting would require `#[used(compiler)]` (unstable) or a `protoc`\n  Rust generator change to emit one registration call per crate instead of per\n  extension.\n\nPiperOrigin-RevId: 1015838072\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "01afe0b4276acd87d140309a0fc685f5475a1dc3",
      "old_mode": 33188,
      "old_path": "rust/BUILD",
      "new_id": "6f8c92320d7b99b90fbd2f3786795b2a32f529c6",
      "new_mode": 33188,
      "new_path": "rust/BUILD"
    },
    {
      "type": "modify",
      "old_id": "6f8bbbdc74948dfa16124848d0a719fb32d66ae6",
      "old_mode": 33188,
      "old_path": "rust/release_crates/google_protobuf/Cargo-template.toml",
      "new_id": "05aabc650901eded6ead7a5d0019e75ad0c47006",
      "new_mode": 33188,
      "new_path": "rust/release_crates/google_protobuf/Cargo-template.toml"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "b92123009a2919b3336fdaca9fdb1c35118d6bb9",
      "new_mode": 33188,
      "new_path": "rust/test/upb/wasm_extension_test.rs"
    },
    {
      "type": "modify",
      "old_id": "833c834921035a0d062dbe85f1aff07e7282e0d5",
      "old_mode": 33188,
      "old_path": "rust/upb_kernel/def_pool.rs",
      "new_id": "ce1eab9b9dcd903f12da0e468ac9a6501fcc2673",
      "new_mode": 33188,
      "new_path": "rust/upb_kernel/def_pool.rs"
    },
    {
      "type": "modify",
      "old_id": "b766556e9666990cc60c397268b12f3b3a81b290",
      "old_mode": 33188,
      "old_path": "rust/upb_kernel/extension.rs",
      "new_id": "2868ef2af997621e5c99c91643d38a3c45331b9a",
      "new_mode": 33188,
      "new_path": "rust/upb_kernel/extension.rs"
    }
  ]
}
