Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openssl backend fails to import since 41.0.0 #9023

Closed
felixonmars opened this issue Jun 4, 2023 · 21 comments
Closed

openssl backend fails to import since 41.0.0 #9023

felixonmars opened this issue Jun 4, 2023 · 21 comments
Labels
Stale waiting-on-reporter Issue is waiting on a reply from the reporter. It will be automatically cloesd if there is no reply.

Comments

@felixonmars
Copy link

  • Versions of Python, cryptography, cffi, pip, and setuptools
    you're using
    Python 3.11.3
    cryptography 41.0.0/41.0.1
    pip 23.1.2
    setuptools 67.8.0
    setuptools-rust 1.6.0
    openssl 3.1.1

  • How you installed cryptography
    Building the Arch Linux package (I am the packager)

  • Clear steps for reproducing your bug
    Since 41.0.0, I am getting the following error when trying to import the openssl backend:

>>> from cryptography.hazmat.backends.openssl import backend
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/site-packages/cryptography/hazmat/backends/openssl/__init__.py", line 7, in <module>
    from cryptography.hazmat.backends.openssl.backend import backend
  File "/usr/lib/python3.11/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 13, in <module>
    from cryptography import utils, x509
  File "/usr/lib/python3.11/site-packages/cryptography/x509/__init__.py", line 7, in <module>
    from cryptography.x509 import certificate_transparency
  File "/usr/lib/python3.11/site-packages/cryptography/x509/certificate_transparency.py", line 11, in <module>
    from cryptography.hazmat.bindings._rust import x509 as rust_x509
ImportError: /usr/lib/python3.11/site-packages/cryptography/hazmat/bindings/_rust.abi3.so: undefined symbol: PyInit__openssl

Which isn't the case for 40.0.2 or older versions with everything else unchanged. The package was built with a plain "python setup.py build" and the build went well.

@alex
Copy link
Member

alex commented Jun 4, 2023

Does cryptography 40.0 work?

@felixonmars
Copy link
Author

Yes. 40.0.0, 40.0.1 and 40.0.2 all work properly.

@reaperhulk
Copy link
Member

I just pulled archlinux:latest on an x86_64 machine and built and ran all the tests via pip install ".[test]" (+ installing vectors) without any issues. Is it possible there's something specific about the packaging environment here?

Running python setup.py build also shows the following when I nm the resulting _rust.abi3.so:

0000000000164570 t PyInit__openssl

@alex
Copy link
Member

alex commented Jun 4, 2023

Hmm, that's a bit perplexing, the only things I can imagine causing this were all changes that happened in 40.0, between 40.0 and 41.0 there weren't any substantial changes to our build process that I can think of (or that a brief perusal of the diff turned up).

@felixonmars
Copy link
Author

I just pulled archlinux:latest on an x86_64 machine and built and ran all the tests via pip install ".[test]" (+ installing vectors) without any issues. Is it possible there's something specific about the packaging environment here?

Indeed that is the case. I have just figured out that LTO is what makes the issue appears. The problem only reproduces with -flto=auto set and 41.0.0+.

@alex
Copy link
Member

alex commented Jun 4, 2023

That's a head-scratcher. I guess lto is deleting the function for some reason, even though there's a direct call to it from Rust?

@felixonmars
Copy link
Author

Comparing the nm output indicates that all openssl and cffi symbols were removed by LTO, too.
nm_diff.txt

@alex
Copy link
Member

alex commented Jun 5, 2023

The only change we made between 40.0 and 41.0 that might effect this: in 40.0, the crate that links against the compiled cffi module is the root crate, but in 41.0 it's another crate (which the root crate depends on).

I don't know why that matters, but I'm sure it's the cause. I also don't understand how to fix it.

@felixonmars
Copy link
Author

As pointed out by @liushuyu (Thanks!), using GCC's LTO on the C/C++ side with LLVM's LTO on the Rust side is known to have compatibility issues.

I have updated Arch's PKGBUILD to use clang and lld respectively, and the problem did go away even with LTO still turned on.

It might be a good idea if setuptools-rust or cryptography would default to use clang/lld when available, though. I am currently supplying CC=clang RUSTFLAGS="-Clinker-plugin-lto -Clinker=clang -Clink-arg=-fuse-ld=lld" as envvars.

@alex
Copy link
Member

alex commented Jun 6, 2023

Hmm, on the one hand, this is miserable to debug. On the other hand using something other than the default system compiler seems like very surprising behavior.

@mgorny
Copy link

mgorny commented Jun 6, 2023

One of Gentoo users his this problem too: https://bugs.gentoo.org/903908

@alex
Copy link
Member

alex commented Jun 6, 2023

I wonder if the appropriate fix is in the cc create, to have it reject -flto, since as I understand it, that will never work with rustc?

@liushuyu
Copy link

liushuyu commented Jun 6, 2023

I wonder if the appropriate fix is in the cc create, to have it reject -flto, since as I understand it, that will never work with rustc?

It will work with rustc if the C/C++ compiler is Clang (sharing the same code generator, LLVM). And will have issues when LTO is enabled, but the compilers use different code-generation facilities.

@alex
Copy link
Member

alex commented Jun 6, 2023

Yes, you're right I should have been more precise. Though, AFAIK, it can also fail with clang, if clang and rustc are using different versions of LLVM.

@matoro
Copy link

matoro commented Jun 13, 2023

I can confirm that this is due to GCC's LTO, and is also platform-independent as I observed it on PowerPC.

@alex
Copy link
Member

alex commented Jun 13, 2023

So, I don't think there's an action for us to take here, since this is a general problem. I'd like us to find an appropriate home for this issue before I close it though, do folks feel cc is the right place?

@alex alex added the waiting-on-reporter Issue is waiting on a reply from the reporter. It will be automatically cloesd if there is no reply. label Jun 17, 2023
@github-actions
Copy link

This issue has been waiting for a reporter response for 3 days. It will be auto-closed if no activity occurs in the next 5 days.

@github-actions github-actions bot added the Stale label Jun 21, 2023
alanswanson added a commit to alanswanson/gentooLTO that referenced this issue Jun 25, 2023
Causes "_rust.abi3.so: undefined symbol: PyInit__openssl" due to
LTO conflict when using GCC for C/C++ and Clang/LLVM for Rust.

https://bugs.gentoo.org/903908
pyca/cryptography#9023
@github-actions
Copy link

This issue has not received a reporter response and has been auto-closed. If the issue is still relevant please leave a comment and we can reopen it.

@kloczek
Copy link

kloczek commented Aug 14, 2023

41.0.3 still affected by this issue.
#9415

@meerfrau
Copy link

41.0.4 still affected

mold can't be used, too:
'native-Clink-arg=-fuse-ld=mold' is not a recognized processor for this target (ignoring processor)

@alex
Copy link
Member

alex commented Oct 19, 2023

Mold is unrelated to this issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Stale waiting-on-reporter Issue is waiting on a reply from the reporter. It will be automatically cloesd if there is no reply.
Development

No branches or pull requests

8 participants