//===-- tsan_platform.h -----------------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file is a part of ThreadSanitizer (TSan), a race detector. // // Platform-specific code. //===----------------------------------------------------------------------===// #ifndef TSAN_PLATFORM_H #define TSAN_PLATFORM_H #if !defined(__LP64__) && !defined(_WIN64) # error "Only 64-bit is supported" #endif #include "sanitizer_common/sanitizer_common.h" #include "tsan_defs.h" namespace __tsan { enum { … }; /* C/C++ on linux/x86_64 and freebsd/x86_64 0000 0000 1000 - 0200 0000 0000: main binary and/or MAP_32BIT mappings (2TB) 0200 0000 0000 - 1000 0000 0000: - 1000 0000 0000 - 3000 0000 0000: shadow (32TB) 3000 0000 0000 - 3800 0000 0000: metainfo (memory blocks and sync objects; 8TB) 3800 0000 0000 - 5500 0000 0000: - 5500 0000 0000 - 5a00 0000 0000: pie binaries without ASLR or on 4.1+ kernels 5a00 0000 0000 - 7200 0000 0000: - 7200 0000 0000 - 7300 0000 0000: heap (1TB) 7300 0000 0000 - 7a00 0000 0000: - 7a00 0000 0000 - 8000 0000 0000: modules and main thread stack (6TB) C/C++ on netbsd/amd64 can reuse the same mapping: * The address space starts from 0x1000 (option with 0x0) and ends with 0x7f7ffffff000. * LoAppMem-kHeapMemEnd can be reused as it is. * No VDSO support. * No MidAppMem region. * No additional HeapMem region. * HiAppMem contains the stack, loader, shared libraries and heap. * Stack on NetBSD/amd64 has prereserved 128MB. * Heap grows downwards (top-down). * ASLR must be disabled per-process or globally. */ struct Mapping48AddressSpace { … }; /* C/C++ on linux/mips64 (40-bit VMA) 0000 0000 00 - 0100 0000 00: - (4 GB) 0100 0000 00 - 0200 0000 00: main binary (4 GB) 0200 0000 00 - 1200 0000 00: - (64 GB) 1200 0000 00 - 2200 0000 00: shadow (64 GB) 2200 0000 00 - 4000 0000 00: - (120 GB) 4000 0000 00 - 5000 0000 00: metainfo (memory blocks and sync objects) (64 GB) 5000 0000 00 - aa00 0000 00: - (360 GB) aa00 0000 00 - ab00 0000 00: main binary (PIE) (4 GB) ab00 0000 00 - fe00 0000 00: - (332 GB) fe00 0000 00 - ff00 0000 00: heap (4 GB) ff00 0000 00 - ff80 0000 00: - (2 GB) ff80 0000 00 - ffff ffff ff: modules and main thread stack (<2 GB) */ struct MappingMips64_40 { … }; /* C/C++ on Darwin/iOS/ARM64 (36-bit VMA, 64 GB VM) 0000 0000 00 - 0100 0000 00: - (4 GB) 0100 0000 00 - 0200 0000 00: main binary, modules, thread stacks (4 GB) 0200 0000 00 - 0300 0000 00: heap (4 GB) 0300 0000 00 - 0400 0000 00: - (4 GB) 0400 0000 00 - 0800 0000 00: shadow memory (16 GB) 0800 0000 00 - 0d00 0000 00: - (20 GB) 0d00 0000 00 - 0e00 0000 00: metainfo (4 GB) 0e00 0000 00 - 1000 0000 00: - */ struct MappingAppleAarch64 { … }; /* C/C++ on linux/aarch64 (39-bit VMA) 0000 0010 00 - 0500 0000 00: main binary (20 GB) 0100 0000 00 - 2000 0000 00: - 2000 0000 00 - 4000 0000 00: shadow memory (128 GB) 4000 0000 00 - 4800 0000 00: metainfo (32 GB) 4800 0000 00 - 5500 0000 00: - 5500 0000 00 - 5a00 0000 00: main binary (PIE) (20 GB) 5600 0000 00 - 7c00 0000 00: - 7a00 0000 00 - 7d00 0000 00: heap (12 GB) 7d00 0000 00 - 7fff ffff ff: modules and main thread stack (12 GB) */ struct MappingAarch64_39 { … }; /* C/C++ on linux/aarch64 (42-bit VMA) 00000 0010 00 - 02000 0000 00: main binary (128 GB) 02000 0000 00 - 08000 0000 00: - 10000 0000 00 - 20000 0000 00: shadow memory (1024 GB) 20000 0000 00 - 24000 0000 00: metainfo (256 GB) 24000 0000 00 - 2aa00 0000 00: - 2aa00 0000 00 - 2c000 0000 00: main binary (PIE) (88 GB) 2c000 0000 00 - 3c000 0000 00: - 3c000 0000 00 - 3f000 0000 00: heap (192 GB) 3f000 0000 00 - 3ffff ffff ff: modules and main thread stack (64 GB) */ struct MappingAarch64_42 { … }; /* C/C++ on linux/aarch64 (48-bit VMA) 0000 0000 1000 - 0a00 0000 0000: main binary (10240 GB) 0a00 0000 1000 - 1554 0000 0000: - 1554 0000 1000 - 5400 0000 0000: shadow memory (64176 GB) 5400 0000 1000 - 8000 0000 0000: - 8000 0000 1000 - 0a00 0000 0000: metainfo (32768 GB) a000 0000 1000 - aaaa 0000 0000: - aaaa 0000 1000 - ac00 0000 0000: main binary (PIE) (1368 GB) ac00 0000 1000 - fc00 0000 0000: - fc00 0000 1000 - ffff ffff ffff: modules and main thread stack (4096 GB) N.B. the shadow memory region has a strange start address, because it contains the shadows for the mid, high and low app regions (in this unusual order). */ struct MappingAarch64_48 { … }; /* C/C++ on linux/loongarch64 (47-bit VMA) 0000 0000 4000 - 0080 0000 0000: main binary 0080 0000 0000 - 0100 0000 0000: - 0100 0000 0000 - 1000 0000 0000: shadow memory 1000 0000 0000 - 3000 0000 0000: - 3000 0000 0000 - 3400 0000 0000: metainfo 3400 0000 0000 - 5555 0000 0000: - 5555 0000 0000 - 5556 0000 0000: main binary (PIE) 5556 0000 0000 - 7ffe 0000 0000: - 7ffe 0000 0000 - 7fff 0000 0000: heap 7fff 0000 0000 - 7fff 8000 0000: - 7fff 8000 0000 - 8000 0000 0000: modules and main thread stack */ struct MappingLoongArch64_47 { … }; /* C/C++ on linux/powerpc64 (44-bit VMA) 0000 0000 0100 - 0001 0000 0000: main binary 0001 0000 0000 - 0001 0000 0000: - 0001 0000 0000 - 0b00 0000 0000: shadow 0b00 0000 0000 - 0b00 0000 0000: - 0b00 0000 0000 - 0d00 0000 0000: metainfo (memory blocks and sync objects) 0d00 0000 0000 - 0f00 0000 0000: - 0f00 0000 0000 - 0f50 0000 0000: heap 0f50 0000 0000 - 0f60 0000 0000: - 0f60 0000 0000 - 1000 0000 0000: modules and main thread stack */ struct MappingPPC64_44 { … }; /* C/C++ on linux/powerpc64 (46-bit VMA) 0000 0000 1000 - 0100 0000 0000: main binary 0100 0000 0000 - 0200 0000 0000: - 0100 0000 0000 - 0800 0000 0000: shadow 0800 0000 0000 - 1000 0000 0000: - 1000 0000 0000 - 1200 0000 0000: metainfo (memory blocks and sync objects) 1200 0000 0000 - 3d00 0000 0000: - 3d00 0000 0000 - 3e00 0000 0000: heap 3e00 0000 0000 - 3e80 0000 0000: - 3e80 0000 0000 - 4000 0000 0000: modules and main thread stack */ struct MappingPPC64_46 { … }; /* 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 - 0800 0000 0000: shadow 0800 0000 0000 - 1000 0000 0000: - 1000 0000 0000 - 1200 0000 0000: metainfo (memory blocks and sync objects) 1200 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 MappingPPC64_47 { … }; /* C/C++ on linux/riscv64 (39-bit VMA) 0000 0010 00 - 0200 0000 00: main binary ( 8 GB) 0200 0000 00 - 1000 0000 00: - 1000 0000 00 - 4000 0000 00: shadow memory (64 GB) 4000 0000 00 - 4800 0000 00: metainfo (16 GB) 4800 0000 00 - 5500 0000 00: - 5500 0000 00 - 5a00 0000 00: main binary (PIE) (~8 GB) 5600 0000 00 - 7c00 0000 00: - 7d00 0000 00 - 7fff ffff ff: libraries and main thread stack ( 8 GB) mmap by default allocates from top downwards VDSO sits below loader and above dynamic libraries, within HiApp region. Heap starts after program region whose position depends on pie or non-pie. Disable tracking them since their locations are not fixed. */ struct MappingRiscv64_39 { … }; /* C/C++ on linux/riscv64 (48-bit VMA) 0000 0000 1000 - 0400 0000 0000: main binary ( 4 TB) 0500 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 4000 0000 0000: shadow memory (32 TB) 4000 0000 0000 - 4800 0000 0000: metainfo ( 8 TB) 4800 0000 0000 - 5555 5555 5000: - 5555 5555 5000 - 5a00 0000 0000: main binary (PIE) (~5 TB) 5a00 0000 0000 - 7a00 0000 0000: - 7a00 0000 0000 - 7fff ffff ffff: libraries and main thread stack ( 6 TB) */ struct MappingRiscv64_48 { … }; /* C/C++ on linux/s390x While the kernel provides a 64-bit address space, we have to restrict ourselves to 48 bits due to how e.g. SyncVar::GetId() works. 0000 0000 1000 - 0e00 0000 0000: binary, modules, stacks - 14 TiB 0e00 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 4000 0000 0000: shadow - 32TiB (2 * app) 4000 0000 0000 - 9000 0000 0000: - 9000 0000 0000 - 9800 0000 0000: metainfo - 8TiB (0.5 * app) 9800 0000 0000 - be00 0000 0000: - be00 0000 0000 - c000 0000 0000: heap - 2TiB (max supported by the allocator) */ struct MappingS390x { … }; /* Go on linux, darwin and freebsd on x86_64 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00c0 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 21c0 0000 0000: shadow 21c0 0000 0000 - 3000 0000 0000: - 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects) 4000 0000 0000 - 8000 0000 0000: - */ struct MappingGo48 { … }; /* Go on windows 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00f8 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 0100 0000 0000: - 0100 0000 0000 - 0300 0000 0000: shadow 0300 0000 0000 - 0700 0000 0000: - 0700 0000 0000 - 0770 0000 0000: metainfo (memory blocks and sync objects) 07d0 0000 0000 - 8000 0000 0000: - PIE binaries currently not supported, but it should be theoretically possible. */ struct MappingGoWindows { … }; /* Go on linux/powerpc64 (46-bit VMA) 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00c0 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 21c0 0000 0000: shadow 21c0 0000 0000 - 2400 0000 0000: - 2400 0000 0000 - 2470 0000 0000: metainfo (memory blocks and sync objects) 2470 0000 0000 - 4000 0000 0000: - */ struct MappingGoPPC64_46 { … }; /* Go on linux/powerpc64 (47-bit VMA) 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00c0 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 2800 0000 0000: shadow 2800 0000 0000 - 3000 0000 0000: - 3000 0000 0000 - 3200 0000 0000: metainfo (memory blocks and sync objects) 3200 0000 0000 - 8000 0000 0000: - */ struct MappingGoPPC64_47 { … }; /* Go on linux/aarch64 (48-bit VMA) and darwin/aarch64 (47-bit VMA) 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00c0 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 2800 0000 0000: shadow 2800 0000 0000 - 3000 0000 0000: - 3000 0000 0000 - 3200 0000 0000: metainfo (memory blocks and sync objects) 3200 0000 0000 - 8000 0000 0000: - */ struct MappingGoAarch64 { … }; /* Go on linux/loongarch64 (47-bit VMA) 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00c0 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 2800 0000 0000: shadow 2800 0000 0000 - 3000 0000 0000: - 3000 0000 0000 - 3200 0000 0000: metainfo (memory blocks and sync objects) 3200 0000 0000 - 8000 0000 0000: - */ struct MappingGoLoongArch64_47 { … }; /* Go on linux/mips64 (47-bit VMA) 0000 0000 1000 - 0000 1000 0000: executable 0000 1000 0000 - 00c0 0000 0000: - 00c0 0000 0000 - 00e0 0000 0000: heap 00e0 0000 0000 - 2000 0000 0000: - 2000 0000 0000 - 2800 0000 0000: shadow 2800 0000 0000 - 3000 0000 0000: - 3000 0000 0000 - 3200 0000 0000: metainfo (memory blocks and sync objects) 3200 0000 0000 - 8000 0000 0000: - */ struct MappingGoMips64_47 { … }; /* Go on linux/s390x 0000 0000 1000 - 1000 0000 0000: executable and heap - 16 TiB 1000 0000 0000 - 4000 0000 0000: - 4000 0000 0000 - 6000 0000 0000: shadow - 64TiB (4 * app) 6000 0000 0000 - 9000 0000 0000: - 9000 0000 0000 - 9800 0000 0000: metainfo - 8TiB (0.5 * app) */ struct MappingGoS390x { … }; extern uptr vmaSize; template <typename Func, typename Arg> ALWAYS_INLINE auto SelectMapping(Arg arg) { … } template <typename Func> void ForEachMapping() { … } enum MappingType { … }; struct MappingField { … }; ALWAYS_INLINE uptr LoAppMemBeg(void) { … } ALWAYS_INLINE uptr LoAppMemEnd(void) { … } ALWAYS_INLINE uptr MidAppMemBeg(void) { … } ALWAYS_INLINE uptr MidAppMemEnd(void) { … } ALWAYS_INLINE uptr HeapMemBeg(void) { … } ALWAYS_INLINE uptr HeapMemEnd(void) { … } ALWAYS_INLINE uptr HiAppMemBeg(void) { … } ALWAYS_INLINE uptr HiAppMemEnd(void) { … } ALWAYS_INLINE uptr VdsoBeg(void) { … } ALWAYS_INLINE uptr ShadowBeg(void) { … } ALWAYS_INLINE uptr ShadowEnd(void) { … } ALWAYS_INLINE uptr MetaShadowBeg(void) { … } ALWAYS_INLINE uptr MetaShadowEnd(void) { … } struct IsAppMemImpl { … }; ALWAYS_INLINE bool IsAppMem(uptr mem) { … } struct IsShadowMemImpl { … }; ALWAYS_INLINE bool IsShadowMem(RawShadow *p) { … } struct IsMetaMemImpl { … }; ALWAYS_INLINE bool IsMetaMem(const u32 *p) { … } struct MemToShadowImpl { … }; ALWAYS_INLINE RawShadow *MemToShadow(uptr x) { … } struct MemToMetaImpl { … }; ALWAYS_INLINE u32 *MemToMeta(uptr x) { … } struct ShadowToMemImpl { … }; ALWAYS_INLINE uptr ShadowToMem(RawShadow *s) { … } // Compresses addr to kCompressedAddrBits stored in least significant bits. ALWAYS_INLINE uptr CompressAddr(uptr addr) { … } struct RestoreAddrImpl { … }; // Restores compressed addr from kCompressedAddrBits to full representation. // This is called only during reporting and is not performance-critical. inline uptr RestoreAddr(uptr addr) { … } void InitializePlatform(); void InitializePlatformEarly(); bool CheckAndProtect(bool protect, bool ignore_heap, bool print_warnings); void InitializeShadowMemoryPlatform(); void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns); int ExtractResolvFDs(void *state, int *fds, int nfd); int ExtractRecvmsgFDs(void *msg, int *fds, int nfd); uptr ExtractLongJmpSp(uptr *env); void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size); int call_pthread_cancel_with_cleanup(int (*fn)(void *arg), void (*cleanup)(void *arg), void *arg); void DestroyThreadState(); void PlatformCleanUpThreadState(ThreadState *thr); } // namespace __tsan #endif // TSAN_PLATFORM_H