Skip to content

[6x Migration] cryptography - #7992

Merged
DerThorsten merged 3 commits into
emscripten-forge:emscripten-6xfrom
DerThorsten:migrate-6x/cryptography
Oct 29, 2026
Merged

DerThorsten merged 3 commits into
emscripten-forge:emscripten-6xfrom
DerThorsten:migrate-6x/cryptography

Conversation

@DerThorsten

Copy link
Copy Markdown
Contributor

Moves cryptography from recipes/recipes_emscripten to recipes/recipes_wasm.

Changes for emscripten-6x:

  • Host python → python-dev, the same as the other migrated Python recipes.
  • Host openssl → libopenssl-static, the static OpenSSL output on 6x.
  • cffi is dropped from host. It is still in build (crossenv) and in run, and the cffi 2.0.0 variant pin has no 6x build.
  • The build script now does two things before pip install:
    • It exports MATURIN_PYTHON_SYSCONFIGDATA_DIR for the target, derived from $target_platform, the same as the other maturin recipes.
    • It adds -I${PREFIX}/include/python${PY_VER} to CFLAGS. The cryptography-cffi crate compiles the generated _openssl.c with the cc crate, which otherwise fails with 'Python.h' file not found.
  • Adds the generic-testing test. The existing pytester test (imports fernet, hazmat, x509, …) is unchanged.

Tested locally with ./build_wasm.sh recipes/recipes_wasm/cryptography wasm32. All tests pass: pytester and generic-testing. Lint passes.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DerThorsten

Copy link
Copy Markdown
Contributor Author

I looked into the wasm64 failure (wasm-ld: error: function signature mismatch: poll) and reproduced it locally. The mismatched objects come from the host libopenssl-static for wasm64, not from cryptography: openssl-sys bundles $PREFIX/lib/libcrypto.a/libssl.a into its rlib.

  • libssl-lib-quic_reactor.o calls poll as (i64, i32, i32), while Rust std (wasm64) declares it as (i64, i64, i32). That is a hard error.
  • BN_div_word, BN_set_word, etc. in libcrypto take a 32-bit BN_ULONG, while the Rust openssl crate expects 64-bit on wasm64. These only produce warnings, but they're the same root cause.

recipes/recipes_wasm/openssl/build.sh runs ./Configure gcc, which is a generic 32-bit target, even for wasm64, where it only adds -m64 -sMEMORY64=1. So the wasm64 OpenSSL has a 32-bit internal ABI. I think the openssl recipe should use a 64-bit target for wasm64 (e.g. linux-generic64) and then be rebuilt. That's a change to an already-migrated recipe, so I haven't touched it in this PR. wasm32 builds and passes here. Should I open a separate PR for the openssl recipe?

@DerThorsten

Copy link
Copy Markdown
Contributor Author

Correction to my previous comment, after digging further. There are two separate wasm64 issues, and the hard error is not OpenSSL's fault:

1. poll signature mismatch (the link error) comes from rust-nightly's std.

  • Emscripten deliberately keeps nfds_t 32-bit on wasm64. sysroot/include/poll.h: "XXX Emscripten: nfds_t is kept 32-bit even on wasm64. typedef unsigned int nfds_t;" ([wasm64] Keep nfds_t 32-bit. NFC emscripten-core/emscripten#27115, since 6.0.1). So OpenSSL's poll import (i64, i32, i32) is correct.
  • Rust std declares (i64, i64, i32) because the libc crate inside rust-nightly 1.101.0 (madsmtm/rust@f26d0d0, library/Cargo.lock → libc 0.2.189) still has nfds_t = c_ulong for emscripten.
  • Upstream libc fixed this in rust-lang/libc@60b52fe ("emscripten: fix ABI-breakages"), released in libc 0.2.191 (0.2.189 and 0.2.190 still have c_ulong).
  • → The fix is to rebuild recipes_native/rust-nightly with std's libc bumped to ≥ 0.2.191. After that, cryptography (and any other wasm64 Rust package whose final link also pulls in a C poll user) should link. Nothing in this recipe can work around it cleanly.

2. OpenSSL BN_ULONG is 32-bit on wasm64 (link warnings, but would trap at runtime).
recipes_wasm/openssl/build.sh configures with the generic gcc target, so BN_ULONG is 32-bit even with -m64. The Rust openssl crate expects 64-bit on wasm64. That gives the function signature mismatch: BN_div_word / BN_set_word / … warnings, and those calls would trap at runtime. A 64-bit target for wasm64 (e.g. linux-generic64) should fix it.

wasm32 is fine; both points are toolchain/dependency fixes outside this recipe. Should I open separate PRs for (1) bumping libc in rust-nightly and (2) the openssl wasm64 target, or would you rather handle the toolchain side yourself?

@DerThorsten

Copy link
Copy Markdown
Contributor Author

@claude

→ The fix is to rebuild recipes_native/rust-nightly with std's libc bumped to ≥ 0.2.191. After that, cryptography (and any other wasm64 Rust package whose final link also pulls in a C poll user) should link. Nothing in this recipe can work around it cleanly.

try that, do it in this PR that we have a proof that its working

…ssl; more cryptography tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DerThorsten

Copy link
Copy Markdown
Contributor Author

Done, both wasm64 fixes are now in this PR, and cryptography builds and passes on wasm64 locally.

1. rust-nightly (recipes_native, build 0 → 1): patches/0001-std-libc-0.2.191.patch bumps libc in std's library/Cargo.lock from 0.2.189 to 0.2.191, the release with rust-lang/libc@60b52fe (32-bit nfds_t for emscripten ≥ 6.0.1). std only requires libc = "0.2.189", so nothing else changes. The new cfg comes from libc's build.rs: if emcc isn't found (as in the toolchain build) it defaults to the current ABI.

  • new regression test in test_toolchain.sh (tests/poll_abi.rs + tests/poll_user.c): links Rust code calling libc::poll together with a C object calling poll(), for wasm64 and wasm32. With the published toolchain this fails exactly like cryptography did (wasm-ld: error: function signature mismatch: poll, (i64, i64, i32) vs (i64, i32, i32)); with the patched one it links.

2. OpenSSL BN_ULONG on wasm64: Configure target linux-generic64 instead of the generic gcc on wasm64 (SIXTY_FOUR_BIT_LONG → 64-bit BN_ULONG, what the Rust openssl crate expects); wasm32 stays on gcc.

  • recipes_wasm/openssl3 (3.5.8, build 1 → 2): the one actually used here (libopenssl-static: 3.5 in variant.yaml). Also no-afalgeng: 3.5's linux targets enable the Linux-only AF_ALG engine (linux/version.h); with gcc it was already off
  • recipes_wasm/openssl (4.1.0, build 0 → 1): same target change for when the pin moves to 4.x (4.x has no engines)

cryptography: the pytester test now also does real work: Fernet round trip, SHA-256 + AES-GCM, RSA keygen/sign/verify (BIGNUM, would trap with the 32-bit BN_ULONG), EC key + self-signed X.509 round trip.

Local results (macOS, CI-like local channel):

  • rust-nightly: builds, std compiles libc v0.2.191, test_toolchain.sh incl. the poll test passes
  • openssl3 / openssl wasm64: Configuring OpenSSL … for target linux-generic64, C tests under node pass; wasm32 unchanged and passing
  • cryptography wasm64 against those: no signature mismatch warnings at all (neither poll nor BN_*_word), all 5 tests pass on all three pytester backends; wasm32 also passes

CI builds the changed recipes_native and recipes_wasm recipes of this PR in the same job, so cryptography should pick up the patched toolchain there too (the rust-nightly build is long).

🤖 Generated with Claude Code

…er fetchable; it already has libc 0.2.191)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DerThorsten

Copy link
Copy Markdown
Contributor Author

Update: CI failed to fetch the pinned rust rev (could not find … f26d0d0…). The wasm64-emscripten branch of madsmtm/rust (rust-lang/rust#163808) was rebased onto a newer master, so f26d0d0 is no longer reachable from any branch (GitHub: diverged, 1837 ahead / 2 behind) and git can't fetch it; my earlier local build only worked from rattler-build's git cache. This affects any rust-nightly rebuild, not just this PR.

Now pinned to the branch head 4b605e01 (the same "Disable a few more tests on wasm64" commit after the rebase, still 1.101.0). Its std already has libc 0.2.191, so the lockfile patch is gone; the new poll regression test in test_toolchain.sh stays.

Rebuilt locally from a fresh fetch: rust-nightly builds and passes test_toolchain.sh (incl. the wasm64 poll link test), and cryptography passes all 5 tests on wasm64 and wasm32 (all three pytester backends) against it, without any signature-mismatch warnings.

🤖 Generated with Claude Code

@DerThorsten
DerThorsten merged commit 860145d into emscripten-forge:emscripten-6x Oct 29, 2026
5 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.

1 participant