Skip to content

Repair the runtime baseline for Golem - #1

Draft
noise64 wants to merge 14 commits into
mainfrom
runtime-baseline
Draft

noise64 wants to merge 14 commits into
mainfrom
runtime-baseline

Conversation

@noise64

@noise64 noise64 commented Nov 16, 2026 •

Copy link
Copy Markdown

Touches GOL-1850 (PY-02): runtime repairs that the Golem Python SDK needs. It does not resolve the issue.

Runtime repairs for the Golem Python SDK, on top of upstream 5382383. This is the first of two stacked fork PRs; the second adds the persisted prepare/assemble API.

  • CPython 3.14.10. The build downloads the python.org 3.14.10 tarball instead of a patched 3.14.0 checkout, and checks the SHA-256 of the CPython, zlib and SQLite tarballs. cpython-patches/ carries the WASI patches that 3.14.10 does not include: dynamic linking (GH-142234), sys/ioctl.h in socketmodule.c, and a deallocation depth bound (below). zlib moves to 1.3.2 and SQLite to 3.54.1.
  • dlopen table. wit-component 0.258.3 comes from the golemcloud/wasm-tools fork at 801650b (branch golem-2.0), through [patch.crates-io]. The fork counts only the symbols that it writes, so the table size matches its entries when a library exports exception tags. The other 0.258.3 crates (wasmparser, wasm-encoder, wit-parser, wasm-metadata, wit-dylib) still come from crates.io. Builds now fetch the fork from GitHub.
  • CPython cache identity. build.rs records the SHA-256 of the CPython sources: the release tarball, the name and content of each patch (line endings normalized), and the zlib and SQLite tarballs. It writes the identity to componentize-py-source-identity in cpython/ when it extracts and patches the tarball, and in cpython/builddir/wasi when it links libpython3.14.so. The file name is not hidden, because the CI artifact upload leaves out hidden files. A library or tree without that identity, or with another one, fails the build, and the error tells you to delete cpython/. A partly patched tree from an interrupted build is removed before extraction, and only *.patch files in cpython-patches/ are applied. Before, an existing cpython/ skipped the download, the checksum and the patches, and an existing libpython3.14.so skipped the build. A warm checkout then packaged an older runtime. The CI artifact cpython/builddir/wasi carries the identity, so the Test jobs reuse the Populate build.
  • Checked lowering. The runtime checks each value against its WIT type before it lowers it. A bad import argument raises TypeError, OverflowError or ValueError that names the argument, and the host does not see a call. A bad export result traps with a message that names the export. Integer and float checks extract the value as the lowering does, so numpy.int64 and other objects with __index__ or __float__ pass. Before, out-of-range integers wrapped and a moved handle reached the host as handle 0. The three trap_on_empty_* tests become reject_moved_*: a second use of a moved handle now raises ValueError in the guest.
  • Resource finalization. The destructor of an exported resource releases the Python object, and dropping an owned handle drops the handle. Finalizers now run.
  • Support libraries. The linker reads each extension's dylink.0 needed list and links the named support libraries from the Python path, or libsetjmp.so from the WASI SDK. Before, an extension that used setjmp/longjmp failed to link.
  • Pre-initialization inputs. The hash seed and random bytes come from a digest of the linked component, the app name and the files of each Python path entry, so apps built from the same libraries get different values. The clock is fixed, bytecode is not written and build directories get a fixed mtime. Two builds of the same input still differ in a small number of bytes; this is not solved yet.
  • C stack size. CPython measures only the linear-memory C stack, but the engine's call stack grows several times faster for the same C recursion. On an engine with a 1 MiB max_wasm_stack, deep repr, json, pickle and Python __repr__ recursion trapped before CPython raised RecursionError. --stack-size (and stack_size in the library API) sets the C stack. The default stays at the linker's 1 MiB, because the correct value depends on the engine. With 96 KiB on Golem's engine, every measured kind raises RecursionError, at a lower depth.
  • Deallocation depth. On WASI, _Py_Dealloc and the trashcan count nested deallocations and defer past depth 50, because the stack-margin check does not see the engine's stack. Before, freeing a deeply nested list trapped.

Known gaps: --stack-size accepts any value, including 0 or a value that is not a multiple of 16. The CPython cache identity does not cover the WASI SDK version or the configure and link recipe. The same owned handle passed twice in one call passes the check, and the host receives handle 0 for the second use.

Not in this PR: cancellation and the asyncio functions that raise NotImplementedError, Wasm exceptions on the Golem engine (needs exceptions in the shared engine), and a PyO3 extension that aborts on panic.

Verification

On this head, 7389c59 (Linux x86_64, local):

  • cargo test --release -- --skip http_p3_example: passed (118 lib, 21 bindings, 9 componentize tests).
  • bash .github/workflows/lint.sh: passed.
  • Before the removal, the source files and libdl.so of vendor/wit-component were byte-identical to the wit-component crate of wasm-tools 801650b. Cargo.lock changes only by the source line of wit-component, and no crate name and version has two sources.
  • The CPython cache cases ran at an earlier head of Persist prepared bindings and assemble apps with them #2, 59406fd, which has the same build.rs identity code as this branch. This branch adds the removal of a partly patched tree and the *.patch filter; no case exercised these two. The wit-component change does not touch build.rs.
    • A warm upstream-era cpython/ with libpython3.14.so and no identity fails before packaging, and the library is not changed.
    • A libpython3.14.so with no identity file next to it fails the same way.
    • A fresh checkout downloads, patches and builds, and writes the same identity into cpython/ and cpython/builddir/wasi.
    • A second build with the same identity reuses the library (same SHA-256 and mtime, no download).
    • A changed patch fails with the old and the expected identity. When the patch is restored, the library is reused.
    • A patch with CRLF line endings has the same identity, and the library is reused.
    • A changed patch with no library fails on the source identity of cpython/.
    • Only cpython/builddir/wasi restored without hidden files, as the CI artifact does: the build reuses it, with no download.
  • In the cache cases, LOCAL_TARBALL_DIR, a local build.rs change that is not in this PR, read the SQLite tarball from a local copy, because sqlite.org is not reachable from the build machine; the checksum is still checked.
  • Not run: http_p3_example, because its port is taken on the build machine.
  • CI: run 45834795648 on this head passed Populate cache and Test on Linux, macOS and Windows.

Verify the CPython, zlib and SQLite source tarballs by SHA-256 and carry the
WASI patches that 3.14.10 does not include in cpython-patches/. Update zlib to
1.3.2 and SQLite to 3.54.1.
wit-component 0.258.3 counts a library's exports, including Wasm tags it does
not write into the dlopen symbol table. A library exporting a tag, such as
__c_longjmp from setjmp/longjmp support, then makes the loader read past its
symbols. Carry wit-component with the one-line fix until it is released.
Lowering a Python value that does not match its WIT type used to panic part
way through, for example with an out-of-range int. Check the whole value first:
an import call raises TypeError, OverflowError or ValueError naming the
argument before the host sees a call, and an export whose result does not match
traps with a message naming the value.
The handle table holds a strong reference to an exported resource's Python
object, but the destructor did nothing, so objects whose handles the host
dropped were never finalized. Release the reference in the destructor, and drop
the handle when an owned handle returns to the guest instead of leaking it.
An extension may depend on a shared library that is not itself an extension,
such as libsetjmp.so or a libjpeg.so shipped next to it. Link each library that
a linked library names in its dylink.0 needed list, from the Python path or the
WASI SDK's libsetjmp.so, under the name its dependents use. Libraries that
nothing needs are not linked.
Pre-initialization saves the application's state into the component, so host
entropy, time and file writes during it made each build different. Derive the
hash seed and random bytes from the linked component, give the guest a clock
that starts at the Unix epoch, stop it from writing bytecode into the source
directories, and give the directories created for the build fixed times.
CPython defers a deallocation when its measured C stack margin is small, but
on WebAssembly that margin covers only the linear-memory stack. Freeing a
deeply nested list exhausted the engine's call stack first. Count nested
deallocations instead and defer at depth 50.
CPython raises RecursionError when the linear-memory C stack is nearly full,
but the engine's own call stack is separate and grows several times faster
for the same C recursion. The --stack-size option, and stack_size in the
library API, let the caller size the C stack so that RecursionError comes
first for its engine's call stack.
setup-python has no 3.14.0 build for the current ubuntu-latest image, so the
Test jobs failed before building. 3.14.10 is published for every runner in
the matrix and matches the CPython release that the build uses.
build.rs reused an existing cpython/ without the release tarball, its
checksum or the patches, and an existing libpython3.14.so without a
build. A checkout that had built other sources packaged that older
runtime. It now records the SHA-256 of the CPython, zlib and SQLite
tarballs and of each patch in cpython/ and cpython/builddir/wasi, and
fails on a tree or library with another identity or none.
The checks before lowering accepted only int and float instances, but
the lowering takes any object with __index__ or __float__, such as
numpy.int64. They now extract the value as the lowering does and keep
the TypeError and OverflowError messages.

Pre-initialization entropy came from the linked component alone, so
every app without native extensions got the same hash seed and random
bytes. It now also covers the app name and the files of each Python
path entry.

build.rs removes a partly patched CPython tree before extracting, and
applies only *.patch files.
The dlopen symbol-count fix is merged into the golem-2.0 branch of
golemcloud/wasm-tools. The patch now pins that commit instead of the
vendored copy, which is removed. Its source is the same as the vendored
copy, and its sibling crates still come from crates.io.
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