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

Default link options causing seg-faults #121

Open
kif opened this issue Aug 31, 2017 · 6 comments
Open

Default link options causing seg-faults #121

kif opened this issue Aug 31, 2017 · 6 comments

Comments

@kif
Copy link

kif commented Aug 31, 2017

I found a bug in pyFAI when compiled on manylinux1. All library built on "manylinux1" was segfaulting on all linux distribution but "manylinux1".
The reference to the bug is: silx-kit/pyFAI#649

The core of the bug is that on manylinux1, the linker (gcc) does not try to resolve internally the names:

  • One solution is to use manual name mangling, as implemented.
  • another solution would be to add an extra link argument: -Wl,-Bsymbolic-functions, but I am not sure gcc4.1 from manylinux1 supports it
  • The only portable options found seams to mangle the attribute of the function (attribute((visibility("hidden")))) but this is far from being intuitive.

Any opinion on this ?

@njsmith
Copy link
Member

njsmith commented Aug 31, 2017

There's some discussion here: https://groups.google.com/forum/#!topic/cython-users/lgNG9ws-9-4

Unfortunately it's true, C symbol resolution is very counterintuitive...

I'm hesitant to suggest -Bsymbolic everywhere because it's a hack that breaks the normal rules for how Linux symbol resolution work (in particular it breaks LD_PRELOAD), and it only stops the code being built from getting symbols from elsewhere; it doesn't stop the code from breaking other libraries by interposing its symbols onto them. Hidden visibility is a cleaner solution. OTOH, sometimes the best solution is a hack.

Arguably the right solution is to force -fvisibility=hidden when building Python extensions, but unfortunately we can't just do that globally in the manylinux compilers, because they're also used to build shared libraries that are used by Python extensions and it would break them. I think setuptools could do this though b/c it knows whether it's building a Python extension or not? Of course this wouldn't 100% solve the problem because you could potentially have a shared library that your extension uses and that conflicts with one of the symbols that Python exports. But that's unlikely because common shared libraries have already worked out naming schemes to avoid popular libraries like libz.

Or possibly Python should stop linking directly to libz and libexpat, why do they even do that.

@kif
Copy link
Author

kif commented Sep 1, 2017 via email

@njsmith
Copy link
Member

njsmith commented Sep 1, 2017

-D'PyMODINIT_FUNC=__attribute__((visibility("default"))) void ' ''')

Are you sure this is necessary? I think PyMODINIT_FUNC should already set symbol visibility correctly by default. (It uses __declspec(dllexport), but gcc recognizes that as an alias for __attribute__((visibility("default"))).)

Libz has little to do in this story. It could have been any third party library.

Not true! Only libraries that the Python interpreter is explicitly linked to (i.e., the ones that show up if you do ldd python) get forced onto extension modules. Otherwise, extension modules and the libraries they use are loaded into separate namespaces so they can't interfere with each other. So on my system it's literally only libz and libexpat whose symbols can cause collisions.

(Well, life gets more complicated if you're embedding Python into another program -- then all that program's symbols can also cause problems. But let's worry about that another time...)

@kif
Copy link
Author

kif commented Sep 1, 2017 via email

@njsmith
Copy link
Member

njsmith commented Sep 2, 2017

My question was: if you leave out the -DPyMODINIT_FUNC=... flag, and just use -fvisibility=hidden alone, then does that work?

@kif
Copy link
Author

kif commented Sep 2, 2017 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

No branches or pull requests

2 participants