Skip to content

Commit

Permalink
Fix issues with TypeIndex on MacOS, i.e. hash on the type name wher…
Browse files Browse the repository at this point in the history
…e available since this otherwise causes problems when loading different shared libraries with `RTLD_LOCAL`.

PiperOrigin-RevId: 317395983
Change-Id: I14b3add5fa19725b2150b68813364d16b8320130
  • Loading branch information
tensorflower-gardener committed Jun 19, 2020
1 parent e380940 commit 1823f87
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions tensorflow/core/framework/type_index.h
Expand Up @@ -24,6 +24,10 @@ limitations under the License.

#include "tensorflow/core/platform/types.h"

#if defined(MACOS) || defined(TARGET_OS_MAC)
#include "tensorflow/core/platform/hash.h"
#endif // defined(MACOS) || defined(TARGET_OS_MAC)

namespace tensorflow {

// On some platforms, we would like to avoid using RTTI in order to have smaller
Expand Down Expand Up @@ -53,10 +57,33 @@ class TypeIndex {

// Returns a TypeIndex object that corresponds to a typename.
template <typename T>
static TypeIndex Make(const char* name) {
static TypeIndex Make() {
static bool hash_bit[1];

#if defined(__GXX_RTTI) || defined(_CPPRTTI)

#if defined(MACOS) || defined(TARGET_OS_MAC)
// Use a hash based on the type name to avoid issues due to RTLD_LOCAL on
// MacOS (b/156979412).
return TypeIndex(Hash64(typeid(T).name()), typeid(T).name());
#else
// Use the real type name if we have RTTI.
return TypeIndex(static_cast<uint64>(reinterpret_cast<intptr_t>(hash_bit)),
typeid(T).name());
#endif // defined(MACOS) || defined(TARGET_OS_MAC)

#else
#if defined(MACOS) || defined(TARGET_OS_MAC)
// Warn MacOS users that not using RTTI can cause problems (b/156979412).
#warning \
"Compiling with RTTI disabled on MacOS can cause problems when comparing " \
"types across shared libraries."
#endif // defined(MACOS) || defined(TARGET_OS_MAC)

// No type names available.
return TypeIndex(static_cast<uint64>(reinterpret_cast<intptr_t>(hash_bit)),
name);
"[RTTI disabled]");
#endif // __GXX_RTTI
}

private:
Expand All @@ -70,12 +97,7 @@ class TypeIndex {

template <typename T>
inline TypeIndex MakeTypeIndex() {
#if defined(__GXX_RTTI) || defined(_CPPRTTI)
// Use the real type name if we have RTTI.
return TypeIndex::Make<T>(typeid(T).name());
#else
return TypeIndex::Make<T>("[RTTI disabled]");
#endif // __GXX_RTTI
return TypeIndex::Make<T>();
}

} // namespace tensorflow
Expand Down

0 comments on commit 1823f87

Please sign in to comment.