You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Typeid information not synchronized across shared library boundaries #912
I've been running into strange inconsistencies where type information is not synchronized across shared library boundaries. This happens when using Clang & libc++ on Linux/x86_64 machines.
Before escalating to Clang/LLVM, I wanted to gauge if anyone here has previously run into this. To reproduce:
issue.py
import mod1, mod2
mod2.test(mod1.Test())
# Expected output
# ================
# $ python issue.py
# $
# Actual output:
# ================
# $ python issue.py
# Traceback (most recent call last):
# File "issue.py", line 3, in <module>
# mod2.test(mod1.Test())
# TypeError: test(): incompatible function arguments. The following argument types are supported:
# 1. (arg0: Test) -> None
#
# Invoked with: <mod1.Test object at 0x7f12861f5df8>
CMakeLists.txt:
cmake_minimum_required(VERSION2.8.12)
project(example)
# Issue is triggered in a release buildset(CMAKE_BUILD_TYPE Release CACHESTRING""FORCE)
# Issue is triggered when using libc++set(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS} -stdlib=libc++")
add_subdirectory(pybind11)
add_library(sharedSHAREDshared.cpp)
pybind11_add_module(mod1mod1.cpp)
pybind11_add_module(mod2mod2.cpp)
target_link_libraries(mod1shared)
target_link_libraries(mod2shared)
shared.h
#pragma once
#defineEXPORT__attribute__ ((visibility("default")))
class EXPORT Test {
public:Test();
};
shared.cpp
#include"shared.h"Test::Test() { }
mod1.cpp
#include<pybind11/pybind11.h>
#include"shared.h"namespacepy= pybind11;
PYBIND11_MODULE(mod1, m) {
py::class_<Test>(m, "Test")
.def(py::init<>());
}
mod2.cpp
#include<pybind11/pybind11.h>
#include"shared.h"namespacepy= pybind11;
voidtest(Test b) { }
PYBIND11_MODULE(mod2, m) {
py::module::import("mod1");
m.def("test", &test);
}
Yes, indeed -- the typeids differ in these two modules despite the __attribute__ ((visibility("default"))) annotation. Interestingly, the problem disappears when the -stdlib=libc++ flag is removed from the compilation flags.
I think our stance has been that if you dlopen with RTLD_LOCAL (the default),
then the "Test" type in mod1.so and mod2.so are different types. They live in
different symbol namespaces, all the symbols in mod1.so are not visible from
mod2.so, etc.
libstdc++ has taken a different stance by moving to string equality for
std::type_info::operator==.
Unfortunately, the C++ standard gives us very few tools to reason about how C++
should behave in the presence of dynamic libraries. The best mental model we
have is either a DSO is part of the "program" (a collection of translation
units) or they represent separate "programs" with some implementation-defined
semantics for interoperating.
we might have to switch to using the type.name() field to identify types, at least under libc++.
I'm curious, though, why this doesn't seem to affect macOS.
@wjakob - give #915 a try in your project: it switches to name-based type lookups outside libstdc++ (which already does name-based equality/hashing). It does fixes the code you posted in the initial report under clang/libc++ for me (tested with clang/libc++ 4.0).
I just did, and it works with that patch -- thank you! Regarding the OSX inconsistency: it's likely that the dlopen semantics are a bit different on Mach-O binaries.
Activity
aldanor commentedon Jun 21, 2017
Hm, is
typeid(Test)not the same in mod1/mod2, can you check?wjakob commentedon Jun 21, 2017
Yes, indeed -- the typeids differ in these two modules despite the
__attribute__ ((visibility("default")))annotation. Interestingly, the problem disappears when the-stdlib=libc++flag is removed from the compilation flags.jagerman commentedon Jun 21, 2017
We do have a few places that do a pointer equality comparison, which seems slightly wrong, but fixing it (PR #914) doesn't fix the issue.
I think, however, given the response in https://bugs.llvm.org/show_bug.cgi?id=33542:
we might have to switch to using the
type.name()field to identify types, at least under libc++.I'm curious, though, why this doesn't seem to affect macOS.
jagerman commentedon Jun 21, 2017
As a workaround, you can call this before loading the pybind modules:
Use std::type_info::name() for type lookups outside stdlibc++
jagerman commentedon Jun 21, 2017
@wjakob - give #915 a try in your project: it switches to name-based type lookups outside libstdc++ (which already does name-based equality/hashing). It does fixes the code you posted in the initial report under clang/libc++ for me (tested with clang/libc++ 4.0).
wjakob commentedon Jun 22, 2017
I just did, and it works with that patch -- thank you! Regarding the OSX inconsistency: it's likely that the dlopen semantics are a bit different on Mach-O binaries.
Use std::type_info::name() for type lookups outside stdlibc++