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

Rust binding in Cryptography 3.4+ dynamically links with non-existent library on macOS #106

Closed
dpage-edb opened this issue Feb 10, 2021 · 18 comments · Fixed by #119
Closed

Comments

@dpage-edb
Copy link

Issue cloned from the Cryptography issue tracker per request, as setuptools-rust handles this for them: pyca/cryptography#5807

The Cryptography wheel for macOS includes _rust.abi3.so. This references a library that isn't shipped and has a path clearly from the build machine:

(foo) dpage@hal:~/git/pgadmin4/foo/lib/python3.8/site-packages/cryptography/hazmat/bindings$ otool -L _rust.abi3.so 
_rust.abi3.so:
	/Users/runner/work/cryptography/cryptography/cryptography-3.4.4/src/rust/target/release/deps/libcryptography_rust.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
	/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)

Whilst this may not break the package, it:

A) Is not good practice
B) Breaks build systems that automatically fixup library paths when creating relocatable appbundles.

B is the issue I'd like to see fixed, as the current situation requires me to modify code that's worked for many years (15+) to ignore errors when processing this particular file.

An additional issue, again likely benign for the general use case, is that _rust.abi3.so does not have the executable bit set as is generally the norm for shared libraries on *nix platforms. This also required me to change build code, as searches for binaries that need to be codesigned filtered possible targets based on the executable bit, before doing further tests to see if the file was actually an executable or library (which gets very expensive without that check, in a codebase with thousands of Python/JS/HTML files etc).

Probably not relevant, but here's my system info:

(foo) dpage@hal:~/git/pgadmin4/foo/lib/python3.8/site-packages/cryptography/hazmat/bindings$ pip freeze
cffi==1.14.4
cryptography==3.4.4
pycparser==2.20
(foo) dpage@hal:~/git/pgadmin4/foo/lib/python3.8/site-packages/cryptography/hazmat/bindings$ python --version
Python 3.8.2
(foo) dpage@hal:~/git/pgadmin4/foo/lib/python3.8/site-packages/cryptography/hazmat/bindings$ pip --version
pip 21.0.1 from /Users/dpage/git/pgadmin4/foo/lib/python3.8/site-packages/pip (python 3.8)
(foo) dpage@hal:~/git/pgadmin4/foo/lib/python3.8/site-packages/cryptography/hazmat/bindings$ sw_vers -productVersion
11.1

Thanks!

@tiran
Copy link
Contributor

tiran commented Feb 10, 2021

The executable bit is missing on Linux, too.

$ ldd ./build/lib.linux-x86_64-3.9/cryptography/hazmat/bindings/_rust.abi3.so
ldd: warning: you do not have execution permission for `./build/lib.linux-x86_64-3.9/cryptography/hazmat/bindings/_rust.abi3.so'
        linux-vdso.so.1 (0x00007ffce3347000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f5702f8a000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5702f68000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f5702d9d000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5703026000)
$ ls -la ./build/lib.linux-x86_64-3.9/cryptography/hazmat/bindings/*.so
-rwxrwxr-x. 1 heimes heimes 2669672 Feb 10 13:32 ./build/lib.linux-x86_64-3.9/cryptography/hazmat/bindings/_openssl.abi3.so
-rwxrwxr-x. 1 heimes heimes   34400 Feb 10 13:32 ./build/lib.linux-x86_64-3.9/cryptography/hazmat/bindings/_padding.abi3.so
-rw-rw-r--. 1 heimes heimes 3708904 Feb 10 13:32 ./build/lib.linux-x86_64-3.9/cryptography/hazmat/bindings/_rust.abi3.so

@davidhewitt
Copy link
Member

Thanks for reporting. libcryptography_rust.dylib is the name of the library as output by the Rust compiler before setuptools-rust renames it to the final _rust.so name.

There's clearly one of two things going wrong. Either

  • setuptools-rust should be renaming this library path, or
  • this dependency should not exist.

I'm afraid I don't have access to a macOS computer right now to test on, so will need some help with identifying the fix.

If you clone the cryptography source tree, invoke cargo build in the src/rust subdirectory, and inspect target/release/libcryptography_rust.dylib with otool, does that include this same erroneous link? If so, this might be an upstream Rust issue.

@dpage-edb
Copy link
Author

dpage-edb commented Feb 11, 2021

I can't get it to build unfortunately. It looks like the linker is failing to find what I assume are functions in the Python library, however even explicitly adding that to LDFLAGS isn't helping (in fact it looks like LDFLAGS is being ignored):

   Compiling cryptography-rust v0.1.0 (/Users/dpage/git/cryptography/src/rust)
error: linking with `/usr/bin/clang` failed: exit code: 1
  |
  = note: "/usr/bin/clang" "-m64" "-arch" "x86_64" "-L" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.15gjghlu49iac21.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.1jkgirnp9b8rrlvu.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.1tk8h0snkgefotww.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.2aqkg278dbcjwmv6.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.2utiffjkt71shkdn.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.36tytxljafqzsxvl.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.3q3o6svhhkajtmx3.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.3z005hih81m7u173.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.41f5nnxh5akbe6su.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.4g8xce78q4nsgvw1.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.4npvpg2s7ofjp0zy.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.52fyt1allp219wbc.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.53nku9jpzl7eq0ib.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.54gxbp9era3lo0fz.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.55pzwx2vvhn527gw.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.593gs740su5s2o7h.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.5d3q7cg7uuuo8mx.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.5flweajuvviuskww.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.8p5grgilkxpgbjr.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.dqvfq4dax0kjc6d.rcgu.o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.u68ppxlip0v5bb1.rcgu.o" "-o" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libcryptography_rust.dylib" "-Wl,-exported_symbols_list,/var/folders/48/dq6ggtt92l95nzjj_4xqrd400000gn/T/rustc3A6XfP/list" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/cryptography_rust.205mtonn7kjxl86n.rcgu.o" "-Wl,-dead_strip" "-dynamiclib" "-Wl,-dylib" "-nodefaultlibs" "-L" "/Users/dpage/git/cryptography/src/rust/target/debug/deps" "-L" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libpyo3-e06e690e5cecc294.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libparking_lot-1ca969a9be3617d0.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libparking_lot_core-ab5e826405a87dfb.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libsmallvec-50db1cb0396c8bf7.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/liblock_api-18e840c0f9e7a2f8.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libscopeguard-9e058c2a551879f8.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libinstant-f369cae457cc2e1b.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libcfg_if-2c21884bff284602.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/liblibc-95a48ad04b2649c9.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libunindent-faf2e9e2e0d84eed.rlib" "/Users/dpage/git/cryptography/src/rust/target/debug/deps/libinventory-0126821be40f290e.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libstd-00c1b2a30df18801.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-f512d396ad36f67e.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libobject-fbd5b42f6031d7ca.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-bed1ac853b0deef6.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libgimli-01136bd058fa514d.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-200142ba51d1bfd5.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-097526c57563a37d.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-858253b43fca2811.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libunwind-78e01da90642f7b0.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-1ccf017aaea7a930.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/liblibc-20d74e683e6fc577.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc-4d8fa99874399801.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-0387df0e85a6becc.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libcore-86974b7b6295ebe8.rlib" "/opt/local/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-3847af7e923e2fe0.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: Undefined symbols for architecture x86_64:
            "_PyExc_BaseException", referenced from:
                _$LT$pyo3..exceptions..PyBaseException$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$::type_object_raw::hc392a0b462685cbe in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.0.rcgu.o)
            "_PyUnicode_AsEncodedString", referenced from:
                pyo3::types::string::PyString::to_string_lossy::h5e131baedd19615b in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.14.rcgu.o)
            "_PyUnicode_FromStringAndSize", referenced from:
                pyo3::types::string::PyString::new::hdd1b5d2a5fd89740 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.14.rcgu.o)
            "_PyObject_Repr", referenced from:
                pyo3::types::any::PyAny::repr::h009e16ca240e83e1 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.15.rcgu.o)
            "_PyUnicode_AsUTF8AndSize", referenced from:
                pyo3::types::string::PyString::to_str::hc9fdbe9616455bd2 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.14.rcgu.o)
            "_PyObject_SetAttr", referenced from:
                pyo3::types::any::PyAny::setattr::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h326355d88ab1caa1 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.15.rcgu.o)
                pyo3::types::any::PyAny::setattr::_$u7b$$u7b$closure$u7d$$u7d$::_$u7b$$u7b$closure$u7d$$u7d$::h54825ade16374a9d in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.15.rcgu.o)
            "_PyErr_NewException", referenced from:
                pyo3::err::PyErr::new_type::h55eb3f4ff71d0b27 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyObject_GetAttr", referenced from:
                pyo3::types::any::PyAny::getattr::_$u7b$$u7b$closure$u7d$$u7d$::hd39fe11b70377580 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.15.rcgu.o)
            "_PyGILState_Ensure", referenced from:
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::finalize::hc67fcbad215bdfcb in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
                pyo3::gil::GILGuard::acquire::hc3ecf9e3d10079ec in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_PyBytes_AsString", referenced from:
                pyo3::types::bytes::PyBytes::as_bytes::h83a377f562e02b60 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_Py_InitializeEx", referenced from:
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::hf50c060ae8fa627a in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_Py_Finalize", referenced from:
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::finalize::hc67fcbad215bdfcb in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_Py_IsInitialized", referenced from:
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::hf50c060ae8fa627a in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::finalize::hc67fcbad215bdfcb in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_PyGILState_Release", referenced from:
                _$LT$pyo3..gil..GILGuard$u20$as$u20$core..ops..drop..Drop$GT$::drop::h4cad7b79d037c08b in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_PyEval_SaveThread", referenced from:
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::hf50c060ae8fa627a in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_PyErr_Print", referenced from:
                pyo3::err::panic_after_error::h667c3f84912f4298 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyErr_GivenExceptionMatches", referenced from:
                pyo3::err::PyErr::is_instance::h9f9182db9db57990 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyList_Append", referenced from:
                pyo3::types::list::PyList::append::_$u7b$$u7b$closure$u7d$$u7d$::h04d9cd1923177461 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.6.rcgu.o)
            "_PyExc_SystemError", referenced from:
                _$LT$pyo3..exceptions..PySystemError$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$::type_object_raw::hf728ab47bcc04189 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.0.rcgu.o)
            "_PyList_New", referenced from:
                pyo3::types::list::PyList::empty::ha21a4ae55595c619 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.6.rcgu.o)
            "_PyExc_AttributeError", referenced from:
                _$LT$pyo3..exceptions..PyAttributeError$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$::type_object_raw::hfd8de49ee1ff743c in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.0.rcgu.o)
            "__Py_Dealloc", referenced from:
                pyo3::ffi::object::Py_DECREF::h0420ac3aa4917e16 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.4.rcgu.o)
            "_PyBytes_Size", referenced from:
                pyo3::types::bytes::PyBytes::as_bytes::h83a377f562e02b60 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_PyTuple_New", referenced from:
                pyo3::types::tuple::_$LT$impl$u20$pyo3..conversion..IntoPy$LT$pyo3..instance..Py$LT$pyo3..types..any..PyAny$GT$$GT$$u20$for$u20$$LP$A$C$$RP$$GT$::into_py::ha17853b045bd9d37 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.4.rcgu.o)
                pyo3::types::tuple::_$LT$impl$u20$pyo3..conversion..IntoPy$LT$pyo3..instance..Py$LT$pyo3..types..any..PyAny$GT$$GT$$u20$for$u20$$LP$A$C$$RP$$GT$::into_py::hd2d23f9a99a76146 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.12.rcgu.o)
            "_PyErr_NormalizeException", referenced from:
                pyo3::err::PyErr::normalized::h75e8127e42a18982 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyErr_PrintEx", referenced from:
                pyo3::err::PyErr::print::hb8f2f3ffe2d1fe30 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyErr_Restore", referenced from:
                pyo3::err::PyErr::restore::hb8e574d97e72ed24 in cryptography_rust.1jkgirnp9b8rrlvu.rcgu.o
                pyo3::err::PyErr::restore::hf5a4be04c1d3acb9 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyErr_Fetch", referenced from:
                pyo3::err::PyErr::fetch::h91002167fd75697f in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.7.rcgu.o)
            "_PyExc_TypeError", referenced from:
                _$LT$pyo3..exceptions..PyTypeError$u20$as$u20$pyo3..type_object..PyTypeInfo$GT$::type_object_raw::hec57ea5ed4e4d870 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.0.rcgu.o)
            "_PyTuple_SetItem", referenced from:
                pyo3::types::tuple::_$LT$impl$u20$pyo3..conversion..IntoPy$LT$pyo3..instance..Py$LT$pyo3..types..any..PyAny$GT$$GT$$u20$for$u20$$LP$A$C$$RP$$GT$::into_py::ha17853b045bd9d37 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.4.rcgu.o)
                pyo3::types::tuple::_$LT$impl$u20$pyo3..conversion..IntoPy$LT$pyo3..instance..Py$LT$pyo3..types..any..PyAny$GT$$GT$$u20$for$u20$$LP$A$C$$RP$$GT$::into_py::hd2d23f9a99a76146 in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.12.rcgu.o)
            "_PyEval_ThreadsInitialized", referenced from:
                pyo3::gil::prepare_freethreaded_python::_$u7b$$u7b$closure$u7d$$u7d$::hf50c060ae8fa627a in libpyo3-e06e690e5cecc294.rlib(pyo3-e06e690e5cecc294.pyo3.7kl564et-cgu.10.rcgu.o)
            "_PyModule_Create2", referenced from:
                pyo3::ffi::modsupport::PyModule_Create::hd8c2777f1ea7aa68 in cryptography_rust.36tytxljafqzsxvl.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `cryptography-rust`.

To learn more, run the command again with --verbose.
(cryptography) dpage@hal:~/git/cryptography/src/rust$ echo $CFLAGS
-mmacosx-version-min=10.12 -I/usr/local/openssl/include
(cryptography) dpage@hal:~/git/cryptography/src/rust$ echo $CXXFLAGS
-mmacosx-version-min=10.12
(cryptography) dpage@hal:~/git/cryptography/src/rust$ echo $LDFLAGS
-mmacosx-version-min=10.12 -L/usr/local/openssl/lib -L /Library/Frameworks/Python.framework/Versions/Current -lPython

Any ideas? I'm completely unfamiliar with rust/cargo.

FYI, that was using the Python 3.9.1 distro from python.org (x86_64, not the Universal2 build), on an Intel mac running Big Sur.

@davidhewitt
Copy link
Member

Ah, sorry, my bad. I'm not a regular macOS user, so forgot some linker configuration is needed.

Python extensions require the Python symbols to be unresolved at link time, so we have to pass special flags to rustc. Full notes are at https://github.com/PyO3/pyo3#using-rust-from-python

TLDR; use cargo rustc --release -- -C link-arg=-undefined -C link-arg=dynamic_lookup instead of cargo build --release.

@dpage-edb
Copy link
Author

Thanks, that worked.

The reference is there in the resulting build, albeit adjusted as you might expect for my build environment:

(cryptography) dpage@hal:~/git/cryptography/src/rust$ otool -L target/release/libcryptography_rust.dylib
target/release/libcryptography_rust.dylib:
	/Users/dpage/git/cryptography/src/rust/target/release/deps/libcryptography_rust.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
	/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)

The referenced library has also been built and is present at the path shown.

Thanks.

@davidhewitt
Copy link
Member

Great! Would you mind also checking the referenced library with otool? I am curious if it contains a link to itself.

I thought that the final library is just a copy of the referenced one, so it would also be interesting to know if there are any differences at all.

For example, on my Linux (ubuntu) machine both libraries exist and are clearly the same file; a quick inspection shows they have the same size, mtime, and hash.

@dpage-edb
Copy link
Author

Of course (and yes, they do have the same hash):

(cryptography) dpage@hal:~/git/cryptography/src/rust$ otool -L target/release/deps/libcryptography_rust.dylib
target/release/deps/libcryptography_rust.dylib:
	/Users/dpage/git/cryptography/src/rust/target/release/deps/libcryptography_rust.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
	/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
(cryptography) dpage@hal:~/git/cryptography/src/rust$ md5 target/release/libcryptography_rust.dylib 
MD5 (target/release/libcryptography_rust.dylib) = 9ba883023ce2cf144a2fbfdb7415e2a1
(cryptography) dpage@hal:~/git/cryptography/src/rust$ md5 target/release/deps/libcryptography_rust.dylib 
MD5 (target/release/deps/libcryptography_rust.dylib) = 9ba883023ce2cf144a2fbfdb7415e2a1

So it really seems like that reference just shouldn't be there.

@davidhewitt
Copy link
Member

Thanks for checking. Yes, it looks like it's probably an upstream Rust bug.

I'll try to borrow my wife's gloriously underpowered macbook air at the weekend to see if I can narrow down a minimal repro for upstream.

@messense
Copy link
Member

It seems that the non-existent library path comes from LC_ID_DYLIB.

❯ otool -D _rust.abi3.so
_rust.abi3.so:
/Users/messense/Projects/cryptography/src/rust/target/release/deps/libcryptography_rust.dylib

Interestingly, _openssl.abi3.so and _padding.abi3.so does not have LC_ID_DYLIB

❯ otool -D _openssl.abi3.so
_openssl.abi3.so:
❯ otool -D _padding.abi3.so
_padding.abi3.so:

_rust.abi3.so Detail

❯ otool -l _rust.abi3.so
_rust.abi3.so:
Load command 0
      cmd LC_SEGMENT_64
  cmdsize 632
  segname __TEXT
   vmaddr 0x0000000000000000
   vmsize 0x0000000000040000
  fileoff 0
 filesize 262144
  maxprot 0x00000005
 initprot 0x00000005
   nsects 7
    flags 0x0
Section
  sectname __text
   segname __TEXT
      addr 0x0000000000001268
      size 0x0000000000033fec
    offset 4712
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x80000400
 reserved1 0
 reserved2 0
Section
  sectname __stubs
   segname __TEXT
      addr 0x0000000000035254
      size 0x0000000000000414
    offset 217684
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x80000408
 reserved1 0 (index into indirect symbol table)
 reserved2 12 (size of stubs)
Section
  sectname __stub_helper
   segname __TEXT
      addr 0x0000000000035668
      size 0x000000000000042c
    offset 218728
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x80000400
 reserved1 0
 reserved2 0
Section
  sectname __gcc_except_tab
   segname __TEXT
      addr 0x0000000000035a94
      size 0x0000000000000d7c
    offset 219796
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __const
   segname __TEXT
      addr 0x0000000000036810
      size 0x0000000000003940
    offset 223248
     align 2^4 (16)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __unwind_info
   segname __TEXT
      addr 0x000000000003a150
      size 0x0000000000001fe8
    offset 237904
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __eh_frame
   segname __TEXT
      addr 0x000000000003c138
      size 0x0000000000003ec8
    offset 246072
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Load command 1
      cmd LC_SEGMENT_64
  cmdsize 232
  segname __DATA_CONST
   vmaddr 0x0000000000040000
   vmsize 0x0000000000004000
  fileoff 262144
 filesize 16384
  maxprot 0x00000003
 initprot 0x00000003
   nsects 2
    flags 0x10
Section
  sectname __got
   segname __DATA_CONST
      addr 0x0000000000040000
      size 0x0000000000000030
    offset 262144
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000006
 reserved1 87 (index into indirect symbol table)
 reserved2 0
Section
  sectname __const
   segname __DATA_CONST
      addr 0x0000000000040030
      size 0x00000000000020e8
    offset 262192
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Load command 2
      cmd LC_SEGMENT_64
  cmdsize 632
  segname __DATA
   vmaddr 0x0000000000044000
   vmsize 0x0000000000004000
  fileoff 278528
 filesize 16384
  maxprot 0x00000003
 initprot 0x00000003
   nsects 7
    flags 0x0
Section
  sectname __la_symbol_ptr
   segname __DATA
      addr 0x0000000000044000
      size 0x00000000000002b8
    offset 278528
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000007
 reserved1 93 (index into indirect symbol table)
 reserved2 0
Section
  sectname __data
   segname __DATA
      addr 0x00000000000442b8
      size 0x0000000000000258
    offset 279224
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __thread_vars
   segname __DATA
      addr 0x0000000000044510
      size 0x00000000000000c0
    offset 279824
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000013
 reserved1 0
 reserved2 0
Section
  sectname __thread_data
   segname __DATA
      addr 0x00000000000445d0
      size 0x0000000000000140
    offset 280016
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000011
 reserved1 0
 reserved2 0
Section
  sectname __thread_bss
   segname __DATA
      addr 0x0000000000044710
      size 0x00000000000000a1
    offset 0
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000012
 reserved1 0
 reserved2 0
Section
  sectname __common
   segname __DATA
      addr 0x00000000000447b8
      size 0x0000000000000030
    offset 0
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000001
 reserved1 0
 reserved2 0
Section
  sectname __bss
   segname __DATA
      addr 0x00000000000447e8
      size 0x0000000000000090
    offset 0
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000001
 reserved1 0
 reserved2 0
Load command 3
      cmd LC_SEGMENT_64
  cmdsize 72
  segname __LINKEDIT
   vmaddr 0x0000000000048000
   vmsize 0x0000000000024000
  fileoff 294912
 filesize 133591
  maxprot 0x00000001
 initprot 0x00000001
   nsects 0
    flags 0x0
Load command 4
          cmd LC_ID_DYLIB
      cmdsize 120
         name /Users/messense/Projects/cryptography/src/rust/target/release/deps/libcryptography_rust.dylib (offset 24)
   time stamp 1 Thu Jan  1 08:00:01 1970
      current version 0.0.0
compatibility version 0.0.0
Load command 5
            cmd LC_DYLD_INFO_ONLY
        cmdsize 48
     rebase_off 294912
    rebase_size 352
       bind_off 295264
      bind_size 144
  weak_bind_off 0
 weak_bind_size 0
  lazy_bind_off 295408
 lazy_bind_size 2064
     export_off 297472
    export_size 56
Load command 6
     cmd LC_SYMTAB
 cmdsize 24
  symoff 298304
   nsyms 2480
  stroff 338704
 strsize 86328
Load command 7
            cmd LC_DYSYMTAB
        cmdsize 80
      ilocalsym 0
      nlocalsym 2385
     iextdefsym 2385
     nextdefsym 2
      iundefsym 2387
      nundefsym 93
         tocoff 0
           ntoc 0
      modtaboff 0
        nmodtab 0
   extrefsymoff 0
    nextrefsyms 0
 indirectsymoff 337984
  nindirectsyms 180
      extreloff 0
        nextrel 0
      locreloff 0
        nlocrel 0
Load command 8
     cmd LC_UUID
 cmdsize 24
    uuid 5308928A-547B-3A3C-BE3D-22D8652852AE
Load command 9
      cmd LC_BUILD_VERSION
  cmdsize 32
 platform 1
    minos 11.0
      sdk 11.1
   ntools 1
     tool 3
  version 609.8
Load command 10
      cmd LC_SOURCE_VERSION
  cmdsize 16
  version 0.0
Load command 11
          cmd LC_LOAD_DYLIB
      cmdsize 56
         name /usr/lib/libSystem.B.dylib (offset 24)
   time stamp 2 Thu Jan  1 08:00:02 1970
      current version 1292.60.1
compatibility version 1.0.0
Load command 12
          cmd LC_LOAD_DYLIB
      cmdsize 56
         name /usr/lib/libresolv.9.dylib (offset 24)
   time stamp 2 Thu Jan  1 08:00:02 1970
      current version 1.0.0
compatibility version 1.0.0
Load command 13
      cmd LC_FUNCTION_STARTS
  cmdsize 16
  dataoff 297528
 datasize 776
Load command 14
      cmd LC_DATA_IN_CODE
  cmdsize 16
  dataoff 298304
 datasize 0
Load command 15
      cmd LC_CODE_SIGNATURE
  cmdsize 16
  dataoff 425040
 datasize 3463

@messense
Copy link
Member

Similar issue bytecodealliance/wasmtime#984

@messense
Copy link
Member

Note that you can use RUSTFLAGS to change LC_ID_DYLIB, for example

$ RUSTFLAGS='-C link-args=-Wl,-install_name,@rpath/libcryptography_rust.dylib' python setup.py build_ext -v
...
$ otool -D build/lib.macosx-10.14.6-arm64-3.8/cryptography/hazmat/bindings/_rust.abi3.so
build/lib.macosx-10.14.6-arm64-3.8/cryptography/hazmat/bindings/_rust.abi3.so:
@rpath/libcryptography_rust.dylib

@davidhewitt
Copy link
Member

Thanks @messense for the investigation. Do you think we should be setting LC_ID_DYLIB automatically to the final .so name in setuptools-rust?

e.g. we presumably want this to be @rpath/_rust.abi3.so ?

@messense
Copy link
Member

Do you think we should be setting LC_ID_DYLIB automatically to the final .so name in setuptools-rust?

I am not sure.

@dpage-edb Can you try change it using install_name_tool and test whether it resolves your issue?

install_name_tool -id '@rpath/_rust.abi3.so' /path/to/_rust.abi3.so

@dpage-edb
Copy link
Author

With a minor change to my build scripts to recognise the @rpath prefix as something to ignore along with @executable_path (which I'm fine with, as it's a generic change), yes, it seems to work fine.

@messense
Copy link
Member

Thanks @messense for the investigation. Do you think we should be setting LC_ID_DYLIB automatically to the final .so name in setuptools-rust?

e.g. we presumably want this to be @rpath/_rust.abi3.so ?

Implemented in #119

@alex
Copy link
Contributor

alex commented Mar 4, 2021

@davidhewitt would it be possible to get a release with this

@davidhewitt
Copy link
Member

Thanks for the ping @alex. Apologies for the delay, I've been following up on a couple of other threads. I'm planning to release a setuptools-rust 0.12 this weekend once I merge a PR to improve the documentation a bit (probably add a readthedocs site).

@alex
Copy link
Contributor

alex commented Mar 4, 2021 via email

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 a pull request may close this issue.

5 participants