GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-131298: eliminate HACL* static libraries for cryptographic modules by picnixz · Pull Request #132438 · python/cpython

Conversation

@picnixz

@picnixz picnixz commented Apr 12, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

In #130157, I actually statically linked HACL* modules but this has issues with freeze.py. Instead, I'm reverting back to dynamic linking when possible. .However, I don't know how we can make a dynamic linking for BLAKE2 and for HMAC (considering the underlying objects need to be compiled with possible SIMD instructions, I don't think it's possible to provide a shared library, or am I wrong here? honestly, I'm not really good at linking issues :D)

This supersedes #119320 but this does NOT simplify the build process (configure & co are still messy IMO but I've explained the whys in #132438 (comment))

cc @msprotz

  • Issue: Undefined references to HACL symbols when statically linking Python 3.12 #131298

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@picnixz
simplify HACL* build for MD5, SHA1, SHA2 and SHA3 modules
ac1180f
@picnixz
picnixz requested a review from erlend-aasland as a code owner April 12, 2025 12:24
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 12, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Apr 12, 2025
Undefined references to HACL symbols when statically linking Python 3.12 #131298
Closed
@picnixz picnixz added skip news 🔨 test-with-buildbots Test PR w/ buildbots; report in status section labels Apr 12, 2025
@bedevere-bot

This comment was marked as resolved.

Sign in to view
@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 12, 2025
@picnixz

picnixz commented Apr 12, 2025

Copy link
Copy Markdown
Member Author

I'm leaving my dev session for now so I'll be back tomorrow if needs arise.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz picnixz mentioned this pull request Apr 12, 2025
gh-116043: Simplify HACL* build #119320
Closed
@picnixz

picnixz commented Apr 12, 2025

Copy link
Copy Markdown
Member Author

Mmh, I'll need some configure hacks when only BLAKE2 is present and other hash functions are disabled (otherwise the build bot will never build and it's not very useful).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@msprotz

msprotz commented Apr 12, 2025

Copy link
Copy Markdown
Contributor

I don't think there's any issue with shared objects containing simd instructions, as long as the files are compiled with compiler flags consistent with their suffix (e.g. -mavx2 only for Hacl_*_Simd256) prior to linking into a shared object (which we already do properly).

Thanks for looking into this!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz requested a review from corona10 as a code owner April 13, 2025 12:26
@picnixz picnixz changed the title gh-131298: simplify HACL* build for MD5, SHA1, SHA2 and SHA3 modules gh-131298: eliminate HACL* static libraries for cryptographic modules Apr 13, 2025
@picnixz

picnixz commented Apr 13, 2025

Copy link
Copy Markdown
Member Author

Ok, it's not really a simplification of the build, but I managed to eliminate static libraries.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz force-pushed the fix/hacl/dynamic-linker-131298 branch from 5113a98 to 8d86ff6 Compare April 13, 2025 12:29
@picnixz

picnixz commented Apr 13, 2025

Copy link
Copy Markdown
Member Author

!buildbot FIPS

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot

bedevere-bot commented Apr 13, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @picnixz for commit 8d86ff6 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F132438%2Fmerge

The command will test the builders whose names match following regular expression: FIPS

The builders matched are:

  • AMD64 CentOS9 FIPS No Builtin Hashes PR
  • AMD64 CentOS9 FIPS Only Blake2 Builtin Hash PR
  • AMD64 RHEL8 FIPS No Builtin Hashes PR
  • AMD64 RHEL8 FIPS Only Blake2 Builtin Hash PR
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz force-pushed the fix/hacl/dynamic-linker-131298 branch from 8d86ff6 to b7d509c Compare April 13, 2025 12:45
@picnixz
picnixz marked this pull request as draft April 13, 2025 12:45
@bedevere-app bedevere-app Bot removed the awaiting core review label Apr 13, 2025
@picnixz
remove statically linked libraries for HACL* implementation
3f0b41e
@picnixz
picnixz force-pushed the fix/hacl/dynamic-linker-131298 branch from b7d509c to 3f0b41e Compare April 13, 2025 12:46
@picnixz

picnixz commented Apr 13, 2025

Copy link
Copy Markdown
Member Author

Ok, so for WASI I don't know what to do :D

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz added 3 commits April 13, 2025 16:03
@picnixz
is it better now?
08f207b
@picnixz
is it better now?
3a3fc29
@picnixz
fixup
a5d39fa
@picnixz

picnixz commented Apr 13, 2025

Copy link
Copy Markdown
Member Author

Ok, so nothing was fixed. I give up. I don't know how to do it for WASI. Maybe static linking is the only way to do it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz marked this pull request as ready for review April 13, 2025 16:22
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 13, 2025
@picnixz
avoid smelly symbols
6a47f8a
@picnixz
picnixz requested a review from gpshead as a code owner April 19, 2025 10:51
picnixz added 4 commits April 19, 2025 12:56
@picnixz
fixup checksums
ca584d9
@picnixz
regen sbom
f260cbb
@picnixz
fixup shell warnings and comments
7f9868c
@picnixz
it *should* work
c87bb27
@picnixz
picnixz requested a review from sethmlarson as a code owner April 19, 2025 13:40
@picnixz

picnixz commented Apr 19, 2025

Copy link
Copy Markdown
Member Author

Ok, so here is how I eventually fix:

  • HACL* can be built statically or dynamically. In other words, it's not possible to just use Setup.stdlib.in. Why? if I were to specify a .o file, it would actually create rules automatically and I won't be able to customize the HACL* build itself.
  • So, what I do is the following: I just say "ok build this extension module but don't bother about the dependencies, I'll tell you which ones are dependencies".

For regular builds, there are two kinds of dependencies:

  • the Makefile rule prequisites.
  • the actual files to pass to the linker when building the final *.so or *.a file.

For instance, to build the HACL*-based MD5 module I first need to build the HACL* MD5 part and then build md5module.o itself, and finally link _md5.so.

We have:

Modules/_hacl/Hacl_Hash_MD5.o: $(srcdir)/Modules/_hacl/Hacl_Hash_MD5.c $(LIBHACL_MD5_HEADERS)
	$(CC) -c $(LIBHACL_CFLAGS) -o $@ $(srcdir)/Modules/_hacl/Hacl_Hash_MD5.c
$(LIBHACL_MD5_LIB_STATIC): $(LIBHACL_MD5_OBJS)
	-rm -f $@
	$(AR) $(ARFLAGS) $@ $(LIBHACL_MD5_OBJS)

This step builds the HACL* part. The $(LIBHACL_MD5_LIB_STATIC) rule may not necessarily be used later but it's the rule needed to build statically the HACL* MD5 part. It's as if I was building a separate project.

Then, I have explicitly defined:

MODULE__MD5_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_MD5_HEADERS) $(LIBHACL_MD5_LIB_SHARED)
MODULE__MD5_LDEPS=$(LIBHACL_MD5_LIB_SHARED)

The first variable is for everything related to rule dependencies. In this example, to build the md5 (cpython) module, I need the HACL* library and some other stuff. When calling the linker, I only need to pass the .o files but I also need them to be built separately, which is why they are marked as LDEPS. The LDEPS variable is only used when I want to indicate additional linker rule prerequisites. Example:

Modules/md5module.o: $(srcdir)/Modules/md5module.c $(MODULE__MD5_DEPS) $(MODULE_DEPS_SHARED) $(PYTHON_HEADERS)
	$(CC) $(MODULE__MD5_CFLAGS) $(PY_STDMODULE_CFLAGS) $(CCSHARED) -c $(srcdir)/Modules/md5module.c -o Modules/md5module.o
Modules/_md5$(EXT_SUFFIX):  Modules/md5module.o $(MODULE__MD5_LDEPS)
	$(BLDSHARED)  Modules/md5module.o $(MODULE__MD5_LDFLAGS) $(LIBPYTHON) -o Modules/_md5$(EXT_SUFFIX)

Note that the rules for Modules/_md5$(EXT_SUFFIX) are automatically created by makesetup and I cannot just change them. I need to tell makesetup that I have an extra prerequisite for that rule but I cannot tell it via Setup.stdlib.in. So I introduced the LDEPS variable instead. Note that it can be different from the LDFLAGS variable (in this case, it's not).


Now what was the issue with WASI? well... before, I didn't add the .a file as a rule prequisite (namely, I forgot to also include it in the DEPS variable). Because of that, when executing the libpython rule, there was a missing prequisite, which is why the static HACL* library was never built, leading to a missing file error. It's because LIBPYTHON was built before the extension modules were built.

gpshead reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 19, 2025

Copy link
Copy Markdown
Member Author

It works!!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz picnixz added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 19, 2025
@bedevere-bot

bedevere-bot commented Apr 19, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @picnixz for commit c87bb27 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F132438%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 19, 2025
@picnixz picnixz self-assigned this Apr 19, 2025
@picnixz

picnixz commented Apr 20, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

All failures are unrelated or already known. So I think we're good with this change. I'll now actually check if freeze.py works (I actually didn't test this...).

The following works:

$ ./configure -q --prefix="$(pwd)/build" --libdir="$(pwd)/build/lib" --exec-prefix="$(pwd)/build" && make -j12 && make install
$ echo 'print("Hello world")' >> hello.py
$ ./python ./Tools/freeze/freeze.py -o frozenhello hello.py
$ make -C frozenhello
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gpshead
gpshead approved these changes Apr 20, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 20, 2025
@gpshead gpshead added the build The build process and cross-build label Apr 20, 2025
@gpshead
gpshead enabled auto-merge (squash) April 20, 2025 17:40
@gpshead
gpshead merged commit 5f2ba15 into python:main Apr 20, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 20, 2025
@picnixz
picnixz deleted the fix/hacl/dynamic-linker-131298 branch April 24, 2025 21:01
asottile added a commit to asottile/cpython that referenced this pull request Apr 26, 2025
@asottile
update Modules/Setup after HACL static linking changes
cca76e6
looks like this was missed in python#132438
@asottile asottile mentioned this pull request Apr 26, 2025
gh-131298: update Modules/Setup after HACL static linking changes #133012
Merged
@asottile

asottile commented Apr 26, 2025

Copy link
Copy Markdown
Contributor

looks like Modules/Setup was missed as part of this patch -- I opened #133012 to update that as well!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz picnixz mentioned this pull request Apr 27, 2025
gh-133042: disable HACL* HMAC on Emscripten #133064
Merged
@zanieb zanieb mentioned this pull request May 18, 2025
Update to 3.14.0b1 astral-sh/python-build-standalone#610
Merged
zanieb added a commit to astral-sh/python-build-standalone that referenced this pull request Jun 4, 2025
@zanieb @Wingysam @geofft
Update to 3.14.0b1 (#610)
d2030cb
- python/cpython#133027
- python/cpython#133366
- python/cpython#133284
- python/cpython#133398
- python/cpython#131298
- python/cpython#132438
- python/cpython#133012

---------

Co-authored-by: Wingy <[email protected]>
Co-authored-by: Geoffrey Thomas <[email protected]>
@picnixz picnixz mentioned this pull request Mar 12, 2026
freeze cannot find Modules/_hacl/libHacl_Hash_*.o #145810
Open
@booxter booxter mentioned this pull request Mar 21, 2026
Static musl build regression (3.14+): duplicate _Py_LibHacl_* symbols from duplicated Modules/_hacl/*.o #146264
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead gpshead approved these changes
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner
@sethmlarson sethmlarson Awaiting requested review from sethmlarson sethmlarson is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@picnixz picnixz

Labels

build The build process and cross-build skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@picnixz @bedevere-bot @msprotz @hoodmane @brettcannon @asottile @gpshead
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.