Skip to content

The configure script incorrectly detects crypt() when it doesn't exist #123917

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

Closed
madscientist opened this issue Sep 10, 2024 · 6 comments
Closed
Labels
3.12 only security fixes build The build process and cross-build type-bug An unexpected behavior, bug, or error

Comments

@madscientist
Copy link

madscientist commented Sep 10, 2024

Bug report

Bug description:

I'm trying to build Python in a very limited sysroot, with limited functionality. In particular, there is no crypt library in this restricted system.

Unfortunately, Python configure detects that there IS a crypt() and then the build fails:

$ /data/src/python3/Python-3.12.6/configure 'CFLAGS=-march=haswell -mtune=intel -O2 -fPIC -static-libgcc' 'LDFLAGS=-L/data/src/build/x86_64-linux/sysroot/rl84/lib64 -L/data/src/build/x86_64-linux/sysroot/rl84/usr/lib64' --prefix=/data/src/python3/Linux-Release-make/dist --enable-static --without-readline --with-openssl=/data/src/openssl/Linux-Release-make-maximal/dist
  ...
checking for crypt.h... no
 ...
checking for libxcrypt >= 3.1.1... no
checking for library containing crypt_r... no
checking for crypt or crypt_r... yes
  ...
checking for stdlib extension module _crypt... yes
  ...
[ERROR] _crypt failed to import: /data/src/python3/Linux-Release-make/bld.python3/build/lib.linux-x86_64-3.12/_crypt.cpython-312-x86_64-linux-gnu.so: undefined symbol: crypt
  ...
/bin/install -c -m 755 Modules/_crypt.cpython-312-x86_64-linux-gnu.so /data/src/python3/Linux-Release-make/dist/lib/python3.12/lib-dynload/_crypt.cpython-312-x86_64-linux-gnu.so
/bin/install: cannot stat 'Modules/_crypt.cpython-312-x86_64-linux-gnu.so': No such file or directory
make: *** [Makefile:2079: sharedinstall] Error 1

I believe the problem is that this test is insufficient (from configure.ac):

    AC_LINK_IFELSE([AC_LANG_PROGRAM([
        #ifdef HAVE_CRYPT_H
          #include <crypt.h>
        #endif
        #include <unistd.h>
      ], [
        #ifdef HAVE_CRYPT_R
          void *x = crypt_r;
        #else
          void *x = crypt;
        #endif
      ])
    ], [ac_cv_crypt_crypt=yes], [ac_cv_crypt_crypt=no])

If you compile and link the above with CFLAGS=-O2 then the assignment will be optimized away. I think you can force it by changing the test like this:

    AC_LINK_IFELSE([AC_LANG_PROGRAM([
        #ifdef HAVE_CRYPT_H
          #include <crypt.h>
        #endif
        #include <unistd.h>
        volatile void *x;
      ], [
        #ifdef HAVE_CRYPT_R
          x = crypt_r;
        #else
          x = crypt;
        #endif
      ])
    ], [ac_cv_crypt_crypt=yes], [ac_cv_crypt_crypt=no])

If I do this then the link fails as expected even with -O2.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

@madscientist madscientist added the type-bug An unexpected behavior, bug, or error label Sep 10, 2024
@zware
Copy link
Member

zware commented Sep 10, 2024

Sounds like a reasonable thing to fix, even if it's not necessarily technically a supported platform. Are you interested in submitting a PR?

@zware
Copy link
Member

zware commented Sep 10, 2024

Note: 3.12 is the last version to actually include the crypt module; it is removed in 3.13. This will make the doing the PR correctly a bit more challenging than it would otherwise be.

@vstinner vstinner added the 3.12 only security fixes label Sep 11, 2024
@madscientist
Copy link
Author

Hm. I have worked around my issue by adding a patch with the above change to my local Python build environment. I don't anticipate taking a new build of Python 3.12.x; the next time I import a Python it will be >=3.13. If this change is not needed there, and I'm the only one who has ever run into this, I'm not sure it's worth trying to create a fix just for that branch.

@vstinner
Copy link
Member

I wrote PR gh-123952 to fix the issue in the 3.12 branch.

@madscientist: Would you mind to test the fix in your setup?

vstinner added a commit to vstinner/cpython that referenced this issue Sep 11, 2024
Use the volatile keyword when checking crypt_r() or crypt() function
in the configure script. Without volatile, the assignment can be
removed by an optimization.

Co-Authored-By: Paul Smith <[email protected]>
vstinner added a commit to vstinner/cpython that referenced this issue Sep 11, 2024
Use the volatile keyword when checking crypt_r() or crypt() function
in the configure script. Without volatile, the assignment can be
removed by an optimization.

Co-Authored-By: Paul Smith <[email protected]>
@picnixz picnixz added the build The build process and cross-build label Sep 11, 2024
vstinner added a commit to vstinner/cpython that referenced this issue Sep 11, 2024
Check if the function is not NULL to use the variable. Otherwise, a
compiler can remove the variable making the check useless.

Co-Authored-By: Paul Smith <[email protected]>
vstinner added a commit to vstinner/cpython that referenced this issue Sep 12, 2024
Use a global volatile variable and check if the function is not NULL
to use the variable. Otherwise, a compiler optimization can remove
the variable making the check useless.

Co-Authored-By: Paul Smith <[email protected]>
vstinner added a commit that referenced this issue Sep 12, 2024
Use a global volatile variable and check if the function is not NULL
to use the variable. Otherwise, a compiler optimization can remove
the variable making the check useless.

Co-authored-by: Paul Smith <[email protected]>
@vstinner
Copy link
Member

vstinner commented Sep 12, 2024

Fixed by 53af5b2.

Thanks for your bug report and your fix @madscientist.

@picnixz
Copy link
Member

picnixz commented Sep 13, 2024

Closing as completed since this only affected Python 3.12.

@picnixz picnixz closed this as completed Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes build The build process and cross-build type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants
@madscientist @vstinner @zware @picnixz and others