Fix TSan on ppc64le (PR libsanitizer/86759)

Message ID 20180801170518.GR3264@redhat.com
State New
Headers show
Series
  • Fix TSan on ppc64le (PR libsanitizer/86759)
Related show

Commit Message

Marek Polacek Aug. 1, 2018, 5:05 p.m.
Cherry-pick compiler-rt revision 318044 and 319180.

    [PowerPC][tsan] Update tsan to handle changed memory layouts in newer kernels
    
    In more recent Linux kernels with 47 bit VMAs the layout of virtual memory
    for powerpc64 changed causing the thread sanitizer to not work properly. This
    patch adds support for 47 bit VMA kernels for powerpc64.
    
    Tested on several 4.x and 3.x kernel releases.

Regtested/bootstrapped on ppc64le-linux with kernel 4.14; applying to
trunk/8.3.

2018-08-01  Marek Polacek  <polacek@redhat.com>

	PR libsanitizer/86759
	* tsan/tsan_platform.h: Cherry-pick compiler-rt revision 318044.
	* tsan/tsan_platform_linux.cc: Cherry-pick compiler-rt revision
	319180.

Patch

--- gcc/libsanitizer/tsan/tsan_platform.h
+++ gcc/libsanitizer/tsan/tsan_platform.h
@@ -301,6 +301,38 @@  struct Mapping46 {
   static const uptr kVdsoBeg       = 0x7800000000000000ull;
 };
 
+/*
+C/C++ on linux/powerpc64 (47-bit VMA)
+0000 0000 1000 - 0100 0000 0000: main binary
+0100 0000 0000 - 0200 0000 0000: -
+0100 0000 0000 - 1000 0000 0000: shadow
+1000 0000 0000 - 1000 0000 0000: -
+1000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
+2000 0000 0000 - 2000 0000 0000: -
+2000 0000 0000 - 2200 0000 0000: traces
+2200 0000 0000 - 7d00 0000 0000: -
+7d00 0000 0000 - 7e00 0000 0000: heap
+7e00 0000 0000 - 7e80 0000 0000: -
+7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
+*/
+struct Mapping47 {
+  static const uptr kMetaShadowBeg = 0x100000000000ull;
+  static const uptr kMetaShadowEnd = 0x200000000000ull;
+  static const uptr kTraceMemBeg   = 0x200000000000ull;
+  static const uptr kTraceMemEnd   = 0x220000000000ull;
+  static const uptr kShadowBeg     = 0x010000000000ull;
+  static const uptr kShadowEnd     = 0x100000000000ull;
+  static const uptr kHeapMemBeg    = 0x7d0000000000ull;
+  static const uptr kHeapMemEnd    = 0x7e0000000000ull;
+  static const uptr kLoAppMemBeg   = 0x000000001000ull;
+  static const uptr kLoAppMemEnd   = 0x010000000000ull;
+  static const uptr kHiAppMemBeg   = 0x7e8000000000ull;
+  static const uptr kHiAppMemEnd   = 0x800000000000ull; // 47 bits
+  static const uptr kAppMemMsk     = 0x7c0000000000ull;
+  static const uptr kAppMemXor     = 0x020000000000ull;
+  static const uptr kVdsoBeg       = 0x7800000000000000ull;
+};
+
 // Indicates the runtime will define the memory regions at runtime.
 #define TSAN_RUNTIME_VMA 1
 #endif
@@ -427,11 +459,13 @@  uptr MappingArchImpl(void) {
   DCHECK(0);
   return 0;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return MappingImpl<Mapping44, Type>();
-  else
-    return MappingImpl<Mapping46, Type>();
+  switch (vmaSize) {
+    case 44: return MappingImpl<Mapping44, Type>();
+    case 46: return MappingImpl<Mapping46, Type>();
+    case 47: return MappingImpl<Mapping47, Type>();
+  }
   DCHECK(0);
+  return 0;
 #else
   return MappingImpl<Mapping, Type>();
 #endif
@@ -580,11 +614,13 @@  bool IsAppMem(uptr mem) {
   DCHECK(0);
   return false;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return IsAppMemImpl<Mapping44>(mem);
-  else
-    return IsAppMemImpl<Mapping46>(mem);
+  switch (vmaSize) {
+    case 44: return IsAppMemImpl<Mapping44>(mem);
+    case 46: return IsAppMemImpl<Mapping46>(mem);
+    case 47: return IsAppMemImpl<Mapping47>(mem);
+  }
   DCHECK(0);
+  return false;
 #else
   return IsAppMemImpl<Mapping>(mem);
 #endif
@@ -607,11 +643,13 @@  bool IsShadowMem(uptr mem) {
   DCHECK(0);
   return false;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return IsShadowMemImpl<Mapping44>(mem);
-  else
-    return IsShadowMemImpl<Mapping46>(mem);
+  switch (vmaSize) {
+    case 44: return IsShadowMemImpl<Mapping44>(mem);
+    case 46: return IsShadowMemImpl<Mapping46>(mem);
+    case 47: return IsShadowMemImpl<Mapping47>(mem);
+  }
   DCHECK(0);
+  return false;
 #else
   return IsShadowMemImpl<Mapping>(mem);
 #endif
@@ -634,11 +672,13 @@  bool IsMetaMem(uptr mem) {
   DCHECK(0);
   return false;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return IsMetaMemImpl<Mapping44>(mem);
-  else
-    return IsMetaMemImpl<Mapping46>(mem);
+  switch (vmaSize) {
+    case 44: return IsMetaMemImpl<Mapping44>(mem);
+    case 46: return IsMetaMemImpl<Mapping46>(mem);
+    case 47: return IsMetaMemImpl<Mapping47>(mem);
+  }
   DCHECK(0);
+  return false;
 #else
   return IsMetaMemImpl<Mapping>(mem);
 #endif
@@ -671,11 +711,13 @@  uptr MemToShadow(uptr x) {
   DCHECK(0);
   return 0;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return MemToShadowImpl<Mapping44>(x);
-  else
-    return MemToShadowImpl<Mapping46>(x);
+  switch (vmaSize) {
+    case 44: return MemToShadowImpl<Mapping44>(x);
+    case 46: return MemToShadowImpl<Mapping46>(x);
+    case 47: return MemToShadowImpl<Mapping47>(x);
+  }
   DCHECK(0);
+  return 0;
 #else
   return MemToShadowImpl<Mapping>(x);
 #endif
@@ -710,11 +752,13 @@  u32 *MemToMeta(uptr x) {
   DCHECK(0);
   return 0;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return MemToMetaImpl<Mapping44>(x);
-  else
-    return MemToMetaImpl<Mapping46>(x);
+  switch (vmaSize) {
+    case 44: return MemToMetaImpl<Mapping44>(x);
+    case 46: return MemToMetaImpl<Mapping46>(x);
+    case 47: return MemToMetaImpl<Mapping47>(x);
+  }
   DCHECK(0);
+  return 0;
 #else
   return MemToMetaImpl<Mapping>(x);
 #endif
@@ -762,11 +806,13 @@  uptr ShadowToMem(uptr s) {
   DCHECK(0);
   return 0;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return ShadowToMemImpl<Mapping44>(s);
-  else
-    return ShadowToMemImpl<Mapping46>(s);
+  switch (vmaSize) {
+    case 44: return ShadowToMemImpl<Mapping44>(s);
+    case 46: return ShadowToMemImpl<Mapping46>(s);
+    case 47: return ShadowToMemImpl<Mapping47>(s);
+  }
   DCHECK(0);
+  return 0;
 #else
   return ShadowToMemImpl<Mapping>(s);
 #endif
@@ -797,11 +843,13 @@  uptr GetThreadTrace(int tid) {
   DCHECK(0);
   return 0;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return GetThreadTraceImpl<Mapping44>(tid);
-  else
-    return GetThreadTraceImpl<Mapping46>(tid);
+  switch (vmaSize) {
+    case 44: return GetThreadTraceImpl<Mapping44>(tid);
+    case 46: return GetThreadTraceImpl<Mapping46>(tid);
+    case 47: return GetThreadTraceImpl<Mapping47>(tid);
+  }
   DCHECK(0);
+  return 0;
 #else
   return GetThreadTraceImpl<Mapping>(tid);
 #endif
@@ -827,11 +875,13 @@  uptr GetThreadTraceHeader(int tid) {
   DCHECK(0);
   return 0;
 #elif defined(__powerpc64__)
-  if (vmaSize == 44)
-    return GetThreadTraceHeaderImpl<Mapping44>(tid);
-  else
-    return GetThreadTraceHeaderImpl<Mapping46>(tid);
+  switch (vmaSize) {
+    case 44: return GetThreadTraceHeaderImpl<Mapping44>(tid);
+    case 46: return GetThreadTraceHeaderImpl<Mapping46>(tid);
+    case 47: return GetThreadTraceHeaderImpl<Mapping47>(tid);
+  }
   DCHECK(0);
+  return 0;
 #else
   return GetThreadTraceHeaderImpl<Mapping>(tid);
 #endif
--- gcc/libsanitizer/tsan/tsan_platform_linux.cc
+++ gcc/libsanitizer/tsan/tsan_platform_linux.cc
@@ -214,9 +214,9 @@  void InitializePlatformEarly() {
     Die();
   }
 #elif defined(__powerpc64__)
-  if (vmaSize != 44 && vmaSize != 46) {
+  if (vmaSize != 44 && vmaSize != 46 && vmaSize != 47) {
     Printf("FATAL: ThreadSanitizer: unsupported VMA range\n");
-    Printf("FATAL: Found %d - Supported 44 and 46\n", vmaSize);
+    Printf("FATAL: Found %d - Supported 44, 46, and 47\n", vmaSize);
     Die();
   }
 #endif