Skip to content

Commit c79b802

Browse files
committed
Force hidden visibility on functions needing distinct static locals
This commit adds a PYBIND11_UNSHARED_STATIC_LOCALS macro that forces a function to have hidden visibility under gcc and gcc-compatible compilers. gcc, in particular, needs this to to avoid sharing static local variables across modules (which happens even under a RTLD_LOCAL dlopen()!). clang doesn't appear to have this issue, but the forced visibility on internal pybind functions certainly won't hurt it and icc. This updates the workaround from pybind#862 to use this rather than the version-specific template.
1 parent cca20a7 commit c79b802

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

include/pybind11/cast.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ struct type_info {
6060
bool default_holder : 1;
6161
};
6262

63-
// Store the static internals pointer in a version-specific function so that we're guaranteed it
64-
// will be distinct for modules compiled for different pybind11 versions. Without this, some
65-
// compilers (i.e. gcc) can use the same static pointer storage location across different .so's,
66-
// even though the `get_internals()` function itself is local to each shared object.
67-
template <int = PYBIND11_VERSION_MAJOR, int = PYBIND11_VERSION_MINOR>
68-
internals *&get_internals_ptr() { static internals *internals_ptr = nullptr; return internals_ptr; }
63+
PYBIND11_UNSHARED_STATIC_LOCALS PYBIND11_NOINLINE inline internals *&get_internals_ptr() {
64+
static internals *internals_ptr = nullptr;
65+
return internals_ptr;
66+
}
6967

7068
PYBIND11_NOINLINE inline internals &get_internals() {
7169
internals *&internals_ptr = get_internals_ptr();

include/pybind11/common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@
6868
# endif
6969
#endif
7070

71+
// Attribute macro for a function containing one or more static local variables that mustn't share
72+
// the variable across shared objects (for example, because the value might be incompatible for
73+
// modules compiled under different pybind versions). This is required under g++ (depending on the
74+
// specific compiler and linker options), and won't hurt under gcc-compatible compilers:
75+
#if defined(__GNUG__)
76+
# define PYBIND11_UNSHARED_STATIC_LOCALS __attribute__ ((visibility("hidden")))
77+
#else
78+
# define PYBIND11_UNSHARED_STATIC_LOCALS
79+
#endif
80+
7181
#if defined(_MSC_VER)
7282
# define PYBIND11_NOINLINE __declspec(noinline)
7383
#else

0 commit comments

Comments
 (0)