2012-05-10 21:48:04 +08:00
|
|
|
//===-- tsan_platform.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
|
|
|
//
|
|
|
|
// Platform-specific code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-11-28 15:44:26 +08:00
|
|
|
/*
|
|
|
|
C++ linux memory layout:
|
|
|
|
0000 0000 0000 - 03c0 0000 0000: protected
|
|
|
|
03c0 0000 0000 - 1000 0000 0000: shadow
|
2012-11-28 18:35:31 +08:00
|
|
|
1000 0000 0000 - 6000 0000 0000: protected
|
|
|
|
6000 0000 0000 - 6200 0000 0000: traces
|
|
|
|
6200 0000 0000 - 7d00 0000 0000: -
|
2012-11-28 15:44:26 +08:00
|
|
|
7d00 0000 0000 - 7e00 0000 0000: heap
|
|
|
|
7e00 0000 0000 - 7fff ffff ffff: modules and main thread stack
|
|
|
|
|
|
|
|
C++ COMPAT linux memory layout:
|
|
|
|
0000 0000 0000 - 0400 0000 0000: protected
|
|
|
|
0400 0000 0000 - 1000 0000 0000: shadow
|
|
|
|
1000 0000 0000 - 2900 0000 0000: protected
|
|
|
|
2900 0000 0000 - 2c00 0000 0000: modules
|
2012-11-28 18:35:31 +08:00
|
|
|
2c00 0000 0000 - 6000 0000 0000: -
|
|
|
|
6000 0000 0000 - 6200 0000 0000: traces
|
|
|
|
6200 0000 0000 - 7d00 0000 0000: -
|
2012-11-28 15:44:26 +08:00
|
|
|
7d00 0000 0000 - 7e00 0000 0000: heap
|
|
|
|
7e00 0000 0000 - 7f00 0000 0000: -
|
|
|
|
7f00 0000 0000 - 7fff ffff ffff: main thread stack
|
|
|
|
|
|
|
|
Go linux and darwin memory layout:
|
|
|
|
0000 0000 0000 - 0000 1000 0000: executable
|
|
|
|
0000 1000 0000 - 00f8 0000 0000: -
|
2013-04-23 23:24:26 +08:00
|
|
|
00c0 0000 0000 - 00e0 0000 0000: heap
|
|
|
|
00e0 0000 0000 - 1000 0000 0000: -
|
|
|
|
1000 0000 0000 - 1380 0000 0000: shadow
|
2012-11-28 18:35:31 +08:00
|
|
|
1460 0000 0000 - 6000 0000 0000: -
|
|
|
|
6000 0000 0000 - 6200 0000 0000: traces
|
|
|
|
6200 0000 0000 - 7fff ffff ffff: -
|
2012-11-28 15:44:26 +08:00
|
|
|
|
|
|
|
Go windows memory layout:
|
|
|
|
0000 0000 0000 - 0000 1000 0000: executable
|
|
|
|
0000 1000 0000 - 00f8 0000 0000: -
|
2013-04-23 23:24:26 +08:00
|
|
|
00c0 0000 0000 - 00e0 0000 0000: heap
|
|
|
|
00e0 0000 0000 - 0100 0000 0000: -
|
2012-11-28 15:44:26 +08:00
|
|
|
0100 0000 0000 - 0560 0000 0000: shadow
|
2012-11-28 18:35:31 +08:00
|
|
|
0560 0000 0000 - 0760 0000 0000: traces
|
|
|
|
0760 0000 0000 - 07ff ffff ffff: -
|
2012-11-28 15:44:26 +08:00
|
|
|
*/
|
|
|
|
|
2012-07-16 21:02:40 +08:00
|
|
|
#ifndef TSAN_PLATFORM_H
|
|
|
|
#define TSAN_PLATFORM_H
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-11-28 18:35:31 +08:00
|
|
|
#include "tsan_defs.h"
|
|
|
|
#include "tsan_trace.h"
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-11-06 21:26:57 +08:00
|
|
|
#if defined(__LP64__) || defined(_WIN64)
|
2012-05-10 21:48:04 +08:00
|
|
|
namespace __tsan {
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
#if defined(TSAN_GO)
|
|
|
|
static const uptr kLinuxAppMemBeg = 0x000000000000ULL;
|
2013-04-23 23:59:56 +08:00
|
|
|
static const uptr kLinuxAppMemEnd = 0x04dfffffffffULL;
|
2013-03-19 22:33:38 +08:00
|
|
|
# if SANITIZER_WINDOWS
|
2012-11-06 21:26:57 +08:00
|
|
|
static const uptr kLinuxShadowMsk = 0x010000000000ULL;
|
|
|
|
# else
|
2013-04-23 23:59:56 +08:00
|
|
|
static const uptr kLinuxShadowMsk = 0x200000000000ULL;
|
2012-11-06 21:26:57 +08:00
|
|
|
# endif
|
2012-05-10 21:48:04 +08:00
|
|
|
// TSAN_COMPAT_SHADOW is intended for COMPAT virtual memory layout,
|
|
|
|
// when memory addresses are of the 0x2axxxxxxxxxx form.
|
|
|
|
// The option is enabled with 'setarch x86_64 -L'.
|
2012-07-06 00:18:28 +08:00
|
|
|
#elif defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
|
2012-08-16 21:27:25 +08:00
|
|
|
static const uptr kLinuxAppMemBeg = 0x290000000000ULL;
|
2012-05-10 21:48:04 +08:00
|
|
|
static const uptr kLinuxAppMemEnd = 0x7fffffffffffULL;
|
2014-04-23 15:01:02 +08:00
|
|
|
static const uptr kAppMemGapBeg = 0x2c0000000000ULL;
|
|
|
|
static const uptr kAppMemGapEnd = 0x7d0000000000ULL;
|
2012-05-10 21:48:04 +08:00
|
|
|
#else
|
2012-08-15 23:35:15 +08:00
|
|
|
static const uptr kLinuxAppMemBeg = 0x7cf000000000ULL;
|
2012-05-10 21:48:04 +08:00
|
|
|
static const uptr kLinuxAppMemEnd = 0x7fffffffffffULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const uptr kLinuxAppMemMsk = 0x7c0000000000ULL;
|
|
|
|
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2012-11-28 18:35:31 +08:00
|
|
|
const uptr kTraceMemBegin = 0x056000000000ULL;
|
|
|
|
#else
|
|
|
|
const uptr kTraceMemBegin = 0x600000000000ULL;
|
|
|
|
#endif
|
|
|
|
const uptr kTraceMemSize = 0x020000000000ULL;
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
// This has to be a macro to allow constant initialization of constants below.
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifndef TSAN_GO
|
2012-05-10 21:48:04 +08:00
|
|
|
#define MemToShadow(addr) \
|
|
|
|
(((addr) & ~(kLinuxAppMemMsk | (kShadowCell - 1))) * kShadowCnt)
|
2012-07-06 00:18:28 +08:00
|
|
|
#else
|
2012-07-07 04:23:59 +08:00
|
|
|
#define MemToShadow(addr) \
|
|
|
|
((((addr) & ~(kShadowCell - 1)) * kShadowCnt) | kLinuxShadowMsk)
|
2012-07-06 00:18:28 +08:00
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
|
|
|
|
static const uptr kLinuxShadowBeg = MemToShadow(kLinuxAppMemBeg);
|
|
|
|
static const uptr kLinuxShadowEnd =
|
2012-11-24 13:03:11 +08:00
|
|
|
MemToShadow(kLinuxAppMemEnd) | 0xff;
|
2012-05-10 21:48:04 +08:00
|
|
|
|
|
|
|
static inline bool IsAppMem(uptr mem) {
|
2014-04-23 15:01:02 +08:00
|
|
|
#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
|
|
|
|
return (mem >= kLinuxAppMemBeg && mem < kAppMemGapBeg) ||
|
|
|
|
(mem >= kAppMemGapEnd && mem <= kLinuxAppMemEnd);
|
|
|
|
#else
|
2012-05-10 21:48:04 +08:00
|
|
|
return mem >= kLinuxAppMemBeg && mem <= kLinuxAppMemEnd;
|
2014-04-23 15:01:02 +08:00
|
|
|
#endif
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsShadowMem(uptr mem) {
|
|
|
|
return mem >= kLinuxShadowBeg && mem <= kLinuxShadowEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uptr ShadowToMem(uptr shadow) {
|
|
|
|
CHECK(IsShadowMem(shadow));
|
2012-07-06 00:18:28 +08:00
|
|
|
#ifdef TSAN_GO
|
|
|
|
return (shadow & ~kLinuxShadowMsk) / kShadowCnt;
|
2012-05-10 21:48:04 +08:00
|
|
|
#else
|
|
|
|
return (shadow / kShadowCnt) | kLinuxAppMemMsk;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-06-15 05:40:35 +08:00
|
|
|
// For COMPAT mapping returns an alternative address
|
|
|
|
// that mapped to the same shadow address.
|
2012-08-16 21:27:25 +08:00
|
|
|
// COMPAT mapping is not quite one-to-one.
|
2012-06-15 05:40:35 +08:00
|
|
|
static inline uptr AlternativeAddress(uptr addr) {
|
|
|
|
#if defined(TSAN_COMPAT_SHADOW) && TSAN_COMPAT_SHADOW
|
2012-08-16 22:20:45 +08:00
|
|
|
return (addr & ~kLinuxAppMemMsk) | 0x280000000000ULL;
|
2012-06-15 05:40:35 +08:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-05-23 02:07:45 +08:00
|
|
|
void FlushShadowMemory();
|
2013-03-18 21:55:33 +08:00
|
|
|
void WriteMemoryProfile(char *buf, uptr buf_size);
|
2013-10-04 01:14:35 +08:00
|
|
|
uptr GetRSS();
|
2012-05-22 19:33:03 +08:00
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
const char *InitializePlatform();
|
|
|
|
void FinalizePlatform();
|
2013-10-16 23:35:12 +08:00
|
|
|
|
|
|
|
// The additional page is to catch shadow stack overflow as paging fault.
|
2014-02-05 19:37:20 +08:00
|
|
|
// Windows wants 64K alignment for mmaps.
|
|
|
|
const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
|
|
|
|
+ (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
|
2013-10-16 23:35:12 +08:00
|
|
|
|
2013-03-29 02:52:40 +08:00
|
|
|
uptr ALWAYS_INLINE GetThreadTrace(int tid) {
|
2013-10-16 23:35:12 +08:00
|
|
|
uptr p = kTraceMemBegin + (uptr)tid * kTotalTraceSize;
|
2013-03-20 18:31:53 +08:00
|
|
|
DCHECK_LT(p, kTraceMemBegin + kTraceMemSize);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-03-29 02:52:40 +08:00
|
|
|
uptr ALWAYS_INLINE GetThreadTraceHeader(int tid) {
|
2013-10-16 23:35:12 +08:00
|
|
|
uptr p = kTraceMemBegin + (uptr)tid * kTotalTraceSize
|
|
|
|
+ kTraceSize * sizeof(Event);
|
2012-11-28 18:35:31 +08:00
|
|
|
DCHECK_LT(p, kTraceMemBegin + kTraceMemSize);
|
|
|
|
return p;
|
|
|
|
}
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2014-04-24 21:09:17 +08:00
|
|
|
void *internal_start_thread(void(*func)(void*), void *arg);
|
|
|
|
void internal_join_thread(void *th);
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-09-08 02:08:02 +08:00
|
|
|
// Says whether the addr relates to a global var.
|
|
|
|
// Guesses with high probability, may yield both false positives and negatives.
|
|
|
|
bool IsGlobalVar(uptr addr);
|
2013-02-07 23:27:45 +08:00
|
|
|
int ExtractResolvFDs(void *state, int *fds, int nfd);
|
2013-11-28 17:09:42 +08:00
|
|
|
int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2014-04-17 07:06:46 +08:00
|
|
|
int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
|
|
|
|
void *abstime), void *c, void *m, void *abstime,
|
|
|
|
void(*cleanup)(void *arg), void *arg);
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
} // namespace __tsan
|
|
|
|
|
2012-11-06 21:26:57 +08:00
|
|
|
#else // defined(__LP64__) || defined(_WIN64)
|
2012-05-10 21:48:04 +08:00
|
|
|
# error "Only 64-bit is supported"
|
|
|
|
#endif
|
|
|
|
|
2012-07-16 21:02:40 +08:00
|
|
|
#endif // TSAN_PLATFORM_H
|