head 1.5; access; symbols netbsd-11-0-RC6:1.4 netbsd-11-0-RC5:1.4 netbsd-11-0-RC4:1.4 netbsd-11-0-RC3:1.4 netbsd-11-0-RC2:1.4 netbsd-11-0-RC1:1.4 gcc-14-3-0:1.1.1.3 perseant-exfatfs-base-20250801:1.4 netbsd-11:1.4.0.4 netbsd-11-base:1.4 gcc-12-5-0:1.1.1.2 perseant-exfatfs-base-20240630:1.4 gcc-12-4-0:1.1.1.2 perseant-exfatfs:1.4.0.2 perseant-exfatfs-base:1.4 gcc-12-3-0:1.1.1.2 gcc-10-5-0:1.1.1.1 gcc-10-4-0:1.1.1.1 cjep_sun2x:1.2.0.4 cjep_sun2x-base:1.2 cjep_staticlib_x-base1:1.2 cjep_staticlib_x:1.2.0.2 cjep_staticlib_x-base:1.2 gcc-10-3-0:1.1.1.1 FSF:1.1.1; locks; strict; comment @// @; 1.5 date 2025.09.14.00.08.59; author mrg; state Exp; branches; next 1.4; commitid x9D5QEnvbeMI4CaG; 1.4 date 2023.08.03.01.36.54; author mrg; state Exp; branches; next 1.3; commitid 94ccj90Ss370qhzE; 1.3 date 2023.07.31.01.44.58; author mrg; state Exp; branches; next 1.2; commitid q79F5Opf0FLsyTyE; 1.2 date 2021.04.11.23.54.28; author mrg; state dead; branches; next 1.1; commitid wJn7ggfUTEMOWVOC; 1.1 date 2021.04.10.22.09.22; author mrg; state Exp; branches 1.1.1.1; next ; commitid eC4g0MRpqTvEkNOC; 1.1.1.1 date 2021.04.10.22.09.22; author mrg; state Exp; branches; next 1.1.1.2; commitid eC4g0MRpqTvEkNOC; 1.1.1.2 date 2023.07.30.05.20.41; author mrg; state Exp; branches; next 1.1.1.3; commitid tk6nV4mbc9nVEMyE; 1.1.1.3 date 2025.09.13.23.45.04; author mrg; state Exp; branches; next ; commitid KwhwN4krNWa6XBaG; desc @@ 1.5 log @merge GCC 14.3.0. @ text @//===-- tsan_platform_linux.cpp -------------------------------------------===// // // 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. // // Linux- and BSD-specific code. //===----------------------------------------------------------------------===// #include "sanitizer_common/sanitizer_platform.h" #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_libc.h" #include "sanitizer_common/sanitizer_linux.h" #include "sanitizer_common/sanitizer_platform_limits_netbsd.h" #include "sanitizer_common/sanitizer_platform_limits_posix.h" #include "sanitizer_common/sanitizer_posix.h" #include "sanitizer_common/sanitizer_procmaps.h" #include "sanitizer_common/sanitizer_stackdepot.h" #include "sanitizer_common/sanitizer_stoptheworld.h" #include "tsan_flags.h" #include "tsan_platform.h" #include "tsan_rtl.h" #include #include #include #include #include #include #include #include #if SANITIZER_LINUX #include #include #endif #include #include #include #include #include #include #include #include #include #if SANITIZER_LINUX #define __need_res_state #include #endif #ifdef sa_handler # undef sa_handler #endif #ifdef sa_sigaction # undef sa_sigaction #endif #if SANITIZER_FREEBSD extern "C" void *__libc_stack_end; void *__libc_stack_end = 0; #endif #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) && \ !SANITIZER_GO # define INIT_LONGJMP_XOR_KEY 1 #else # define INIT_LONGJMP_XOR_KEY 0 #endif #if INIT_LONGJMP_XOR_KEY #include "interception/interception.h" // Must be declared outside of other namespaces. DECLARE_REAL(int, _setjmp, void *env) #endif namespace __tsan { #if INIT_LONGJMP_XOR_KEY static void InitializeLongjmpXorKey(); static uptr longjmp_xor_key; #endif // Runtime detected VMA size. uptr vmaSize; enum { MemTotal, MemShadow, MemMeta, MemFile, MemMmap, MemHeap, MemOther, MemCount, }; void FillProfileCallback(uptr p, uptr rss, bool file, uptr *mem) { mem[MemTotal] += rss; if (p >= ShadowBeg() && p < ShadowEnd()) mem[MemShadow] += rss; else if (p >= MetaShadowBeg() && p < MetaShadowEnd()) mem[MemMeta] += rss; else if ((p >= LoAppMemBeg() && p < LoAppMemEnd()) || (p >= MidAppMemBeg() && p < MidAppMemEnd()) || (p >= HiAppMemBeg() && p < HiAppMemEnd())) mem[file ? MemFile : MemMmap] += rss; else if (p >= HeapMemBeg() && p < HeapMemEnd()) mem[MemHeap] += rss; else mem[MemOther] += rss; } void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) { uptr mem[MemCount]; internal_memset(mem, 0, sizeof(mem)); GetMemoryProfile(FillProfileCallback, mem); auto meta = ctx->metamap.GetMemoryStats(); StackDepotStats stacks = StackDepotGetStats(); uptr nthread, nlive; ctx->thread_registry.GetNumberOfThreads(&nthread, &nlive); uptr trace_mem; { Lock l(&ctx->slot_mtx); trace_mem = ctx->trace_part_total_allocated * sizeof(TracePart); } uptr internal_stats[AllocatorStatCount]; internal_allocator()->GetStats(internal_stats); // All these are allocated from the common mmap region. mem[MemMmap] -= meta.mem_block + meta.sync_obj + trace_mem + stacks.allocated + internal_stats[AllocatorStatMapped]; if (s64(mem[MemMmap]) < 0) mem[MemMmap] = 0; internal_snprintf( buf, buf_size, "==%zu== %llus [%zu]: RSS %zd MB: shadow:%zd meta:%zd file:%zd" " mmap:%zd heap:%zd other:%zd intalloc:%zd memblocks:%zd syncobj:%zu" " trace:%zu stacks=%zd threads=%zu/%zu\n", internal_getpid(), uptime_ns / (1000 * 1000 * 1000), ctx->global_epoch, mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemHeap] >> 20, mem[MemOther] >> 20, internal_stats[AllocatorStatMapped] >> 20, meta.mem_block >> 20, meta.sync_obj >> 20, trace_mem >> 20, stacks.allocated >> 20, nlive, nthread); } #if !SANITIZER_GO // Mark shadow for .rodata sections with the special Shadow::kRodata marker. // Accesses to .rodata can't race, so this saves time, memory and trace space. static NOINLINE void MapRodata(char* buffer, uptr size) { // First create temp file. const char *tmpdir = GetEnv("TMPDIR"); if (tmpdir == 0) tmpdir = GetEnv("TEST_TMPDIR"); #ifdef P_tmpdir if (tmpdir == 0) tmpdir = P_tmpdir; #endif if (tmpdir == 0) return; internal_snprintf(buffer, size, "%s/tsan.rodata.%d", tmpdir, (int)internal_getpid()); uptr openrv = internal_open(buffer, O_RDWR | O_CREAT | O_EXCL, 0600); if (internal_iserror(openrv)) return; internal_unlink(buffer); // Unlink it now, so that we can reuse the buffer. fd_t fd = openrv; // Fill the file with Shadow::kRodata. const uptr kMarkerSize = 512 * 1024 / sizeof(RawShadow); InternalMmapVector marker(kMarkerSize); // volatile to prevent insertion of memset for (volatile RawShadow *p = marker.data(); p < marker.data() + kMarkerSize; p++) *p = Shadow::kRodata; internal_write(fd, marker.data(), marker.size() * sizeof(RawShadow)); // Map the file into memory. uptr page = internal_mmap(0, GetPageSizeCached(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0); if (internal_iserror(page)) { internal_close(fd); return; } // Map the file into shadow of .rodata sections. MemoryMappingLayout proc_maps(/*cache_enabled*/true); // Reusing the buffer 'buffer'. MemoryMappedSegment segment(buffer, size); while (proc_maps.Next(&segment)) { if (segment.filename[0] != 0 && segment.filename[0] != '[' && segment.IsReadable() && segment.IsExecutable() && !segment.IsWritable() && IsAppMem(segment.start)) { // Assume it's .rodata char *shadow_start = (char *)MemToShadow(segment.start); char *shadow_end = (char *)MemToShadow(segment.end); for (char *p = shadow_start; p < shadow_end; p += marker.size() * sizeof(RawShadow)) { internal_mmap( p, Min(marker.size() * sizeof(RawShadow), shadow_end - p), PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); } } } internal_close(fd); } void InitializeShadowMemoryPlatform() { char buffer[256]; // Keep in a different frame. MapRodata(buffer, sizeof(buffer)); } #endif // #if !SANITIZER_GO void InitializePlatformEarly() { vmaSize = (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1); #if defined(__aarch64__) # if !SANITIZER_GO if (vmaSize != 39 && vmaSize != 42 && vmaSize != 48) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 39, 42 and 48\n", vmaSize); Die(); } #else if (vmaSize != 48) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 48\n", vmaSize); Die(); } #endif #elif SANITIZER_LOONGARCH64 # if !SANITIZER_GO if (vmaSize != 47) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 47\n", vmaSize); Die(); } # endif #elif defined(__powerpc64__) # if !SANITIZER_GO if (vmaSize != 44 && vmaSize != 46 && vmaSize != 47) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 44, 46, and 47\n", vmaSize); Die(); } # else if (vmaSize != 46 && vmaSize != 47) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 46, and 47\n", vmaSize); Die(); } # endif #elif defined(__mips64) # if !SANITIZER_GO if (vmaSize != 40) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 40\n", vmaSize); Die(); } # else if (vmaSize != 47) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 47\n", vmaSize); Die(); } # endif # elif SANITIZER_RISCV64 // the bottom half of vma is allocated for userspace vmaSize = vmaSize + 1; # if !SANITIZER_GO if (vmaSize != 39 && vmaSize != 48) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 39 and 48\n", vmaSize); Die(); } # endif # endif } void InitializePlatform() { DisableCoreDumperIfNecessary(); // Go maps shadow memory lazily and works fine with limited address space. // Unlimited stack is not a problem as well, because the executable // is not compiled with -pie. #if !SANITIZER_GO { bool reexec = false; // TSan doesn't play well with unlimited stack size (as stack // overlaps with shadow memory). If we detect unlimited stack size, // we re-exec the program with limited stack size as a best effort. if (StackSizeIsUnlimited()) { const uptr kMaxStackSize = 32 * 1024 * 1024; VReport(1, "Program is run with unlimited stack size, which wouldn't " "work with ThreadSanitizer.\n" "Re-execing with stack size limited to %zd bytes.\n", kMaxStackSize); SetStackSizeLimitInBytes(kMaxStackSize); reexec = true; } if (!AddressSpaceIsUnlimited()) { Report("WARNING: Program is run with limited virtual address space," " which wouldn't work with ThreadSanitizer.\n"); Report("Re-execing with unlimited virtual address space.\n"); SetAddressSpaceUnlimited(); reexec = true; } #if SANITIZER_ANDROID && (defined(__aarch64__) || defined(__x86_64__)) // After patch "arm64: mm: support ARCH_MMAP_RND_BITS." is introduced in // linux kernel, the random gap between stack and mapped area is increased // from 128M to 36G on 39-bit aarch64. As it is almost impossible to cover // this big range, we should disable randomized virtual space on aarch64. // ASLR personality check. int old_personality = personality(0xffffffff); if (old_personality != -1 && (old_personality & ADDR_NO_RANDOMIZE) == 0) { VReport(1, "WARNING: Program is run with randomized virtual address " "space, which wouldn't work with ThreadSanitizer.\n" "Re-execing with fixed virtual address space.\n"); CHECK_NE(personality(old_personality | ADDR_NO_RANDOMIZE), -1); reexec = true; } #endif #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) // Initialize the xor key used in {sig}{set,long}jump. InitializeLongjmpXorKey(); #endif if (reexec) ReExec(); } CheckAndProtect(); InitTlsSize(); #endif // !SANITIZER_GO } #if !SANITIZER_GO // Extract file descriptors passed to glibc internal __res_iclose function. // This is required to properly "close" the fds, because we do not see internal // closes within glibc. The code is a pure hack. int ExtractResolvFDs(void *state, int *fds, int nfd) { #if SANITIZER_LINUX && !SANITIZER_ANDROID int cnt = 0; struct __res_state *statp = (struct __res_state*)state; for (int i = 0; i < MAXNS && cnt < nfd; i++) { if (statp->_u._ext.nsaddrs[i] && statp->_u._ext.nssocks[i] != -1) fds[cnt++] = statp->_u._ext.nssocks[i]; } return cnt; #else return 0; #endif } // Extract file descriptors passed via UNIX domain sockets. // This is required to properly handle "open" of these fds. // see 'man recvmsg' and 'man 3 cmsg'. int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) { int res = 0; msghdr *msg = (msghdr*)msgp; struct cmsghdr *cmsg = CMSG_FIRSTHDR(msg); for (; cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) continue; int n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(fds[0]); for (int i = 0; i < n; i++) { fds[res++] = ((int*)CMSG_DATA(cmsg))[i]; if (res == nfd) return res; } } return res; } // Reverse operation of libc stack pointer mangling static uptr UnmangleLongJmpSp(uptr mangled_sp) { #if defined(__x86_64__) # if SANITIZER_LINUX // Reverse of: // xor %fs:0x30, %rsi // rol $0x11, %rsi uptr sp; asm("ror $0x11, %0 \n" "xor %%fs:0x30, %0 \n" : "=r" (sp) : "0" (mangled_sp)); return sp; # else return mangled_sp; # endif #elif defined(__aarch64__) # if SANITIZER_LINUX return mangled_sp ^ longjmp_xor_key; # else return mangled_sp; # endif #elif defined(__loongarch_lp64) return mangled_sp ^ longjmp_xor_key; #elif defined(__powerpc64__) // Reverse of: // ld r4, -28696(r13) // xor r4, r3, r4 uptr xor_key; asm("ld %0, -28696(%%r13)" : "=r" (xor_key)); return mangled_sp ^ xor_key; #elif defined(__mips__) return mangled_sp; # elif SANITIZER_RISCV64 return mangled_sp; # elif defined(__s390x__) // tcbhead_t.stack_guard uptr xor_key = ((uptr *)__builtin_thread_pointer())[5]; return mangled_sp ^ xor_key; # else # error "Unknown platform" # endif } #if SANITIZER_NETBSD # ifdef __x86_64__ # define LONG_JMP_SP_ENV_SLOT 6 # elifdef __aarch64__ # define LONG_JMP_SP_ENV_SLOT 1 # else # error unsupported # endif #elif defined(__powerpc__) # define LONG_JMP_SP_ENV_SLOT 0 #elif SANITIZER_FREEBSD # ifdef __aarch64__ # define LONG_JMP_SP_ENV_SLOT 1 # else # define LONG_JMP_SP_ENV_SLOT 2 # endif #elif SANITIZER_LINUX # ifdef __aarch64__ # define LONG_JMP_SP_ENV_SLOT 13 # elif defined(__loongarch__) # define LONG_JMP_SP_ENV_SLOT 1 # elif defined(__mips64) # define LONG_JMP_SP_ENV_SLOT 1 # elif SANITIZER_RISCV64 # define LONG_JMP_SP_ENV_SLOT 13 # elif defined(__s390x__) # define LONG_JMP_SP_ENV_SLOT 9 # else # define LONG_JMP_SP_ENV_SLOT 6 # endif #endif uptr ExtractLongJmpSp(uptr *env) { uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT]; return UnmangleLongJmpSp(mangled_sp); } #if INIT_LONGJMP_XOR_KEY // GLIBC mangles the function pointers in jmp_buf (used in {set,long}*jmp // functions) by XORing them with a random key. For AArch64 it is a global // variable rather than a TCB one (as for x86_64/powerpc). We obtain the key by // issuing a setjmp and XORing the SP pointer values to derive the key. static void InitializeLongjmpXorKey() { // 1. Call REAL(setjmp), which stores the mangled SP in env. jmp_buf env; REAL(_setjmp)(env); // 2. Retrieve vanilla/mangled SP. uptr sp; #ifdef __loongarch__ asm("move %0, $sp" : "=r" (sp)); #else asm("mov %0, sp" : "=r" (sp)); #endif uptr mangled_sp = ((uptr *)&env)[LONG_JMP_SP_ENV_SLOT]; // 3. xor SPs to obtain key. longjmp_xor_key = mangled_sp ^ sp; } #endif extern "C" void __tsan_tls_initialization() {} void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size) { // Check that the thr object is in tls; const uptr thr_beg = (uptr)thr; const uptr thr_end = (uptr)thr + sizeof(*thr); CHECK_GE(thr_beg, tls_addr); CHECK_LE(thr_beg, tls_addr + tls_size); CHECK_GE(thr_end, tls_addr); CHECK_LE(thr_end, tls_addr + tls_size); // Since the thr object is huge, skip it. const uptr pc = StackTrace::GetNextInstructionPc( reinterpret_cast(__tsan_tls_initialization)); MemoryRangeImitateWrite(thr, pc, tls_addr, thr_beg - tls_addr); MemoryRangeImitateWrite(thr, pc, thr_end, tls_addr + tls_size - thr_end); } // Note: this function runs with async signals enabled, // so it must not touch any tsan state. int call_pthread_cancel_with_cleanup(int (*fn)(void *arg), void (*cleanup)(void *arg), void *arg) { // pthread_cleanup_push/pop are hardcore macros mess. // We can't intercept nor call them w/o including pthread.h. int res; pthread_cleanup_push(cleanup, arg); res = fn(arg); pthread_cleanup_pop(0); return res; } #endif // !SANITIZER_GO #if !SANITIZER_GO void ReplaceSystemMalloc() { } #endif #if !SANITIZER_GO #if SANITIZER_ANDROID // On Android, one thread can call intercepted functions after // DestroyThreadState(), so add a fake thread state for "dead" threads. static ThreadState *dead_thread_state = nullptr; ThreadState *cur_thread() { ThreadState* thr = reinterpret_cast(*get_android_tls_ptr()); if (thr == nullptr) { __sanitizer_sigset_t emptyset; internal_sigfillset(&emptyset); __sanitizer_sigset_t oldset; CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset)); thr = reinterpret_cast(*get_android_tls_ptr()); if (thr == nullptr) { thr = reinterpret_cast(MmapOrDie(sizeof(ThreadState), "ThreadState")); *get_android_tls_ptr() = reinterpret_cast(thr); if (dead_thread_state == nullptr) { dead_thread_state = reinterpret_cast( MmapOrDie(sizeof(ThreadState), "ThreadState")); dead_thread_state->fast_state.SetIgnoreBit(); dead_thread_state->ignore_interceptors = 1; dead_thread_state->is_dead = true; *const_cast(&dead_thread_state->tid) = -1; CHECK_EQ(0, internal_mprotect(dead_thread_state, sizeof(ThreadState), PROT_READ)); } } CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr)); } return thr; } void set_cur_thread(ThreadState *thr) { *get_android_tls_ptr() = reinterpret_cast(thr); } void cur_thread_finalize() { __sanitizer_sigset_t emptyset; internal_sigfillset(&emptyset); __sanitizer_sigset_t oldset; CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &emptyset, &oldset)); ThreadState* thr = reinterpret_cast(*get_android_tls_ptr()); if (thr != dead_thread_state) { *get_android_tls_ptr() = reinterpret_cast(dead_thread_state); UnmapOrDie(thr, sizeof(ThreadState)); } CHECK_EQ(0, internal_sigprocmask(SIG_SETMASK, &oldset, nullptr)); } #endif // SANITIZER_ANDROID #endif // if !SANITIZER_GO } // namespace __tsan #endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD @ 1.4 log @many fixes for native GCC 12 builds - default to dwarf4 since out ctf tools fail with dwarf5. i tried to do this via subtarget options as other ports do, but this would require adjusting several of the targets non-netbsd code, and ideally we'll fix our tools sooner than later, so just hack it here for now. - catch up config/rs6000/netbsd64.h (mostly delete things) - remove some more upstream diffs in config/vax, though it does not work currently - libc.so link fails due to weird relocations. - include some headers to ensure types are available - add missing sanitizer netbsd files to the 'dist' tree - remove no longer present TIOCRCVFRAME and TIOCXMTFRAME interceptors - expanding the sanitizer OS/cpu defs to cover more netbsd platforms - copy across a few minor changes from gcc.old's sanitizer's (eg, disabling linux-specific code, adding netbsd specific code.) - add (untested, but building) tsan support for arm64 - re-enable the sanitzers, at least simple -fsanitize=address builds a working application on amd64 now - fix the source lists (mostly .cc -> .cpp rename here) - add some missing new defines, -f*, and -W* - bump sanitizer shlib version - apply -fno-jump-tables in some more places on m68k - update the list in README.gcc12 to note many things now work or a few things to do not work (although there are many uncommited changes at this point across the rest of 'src' and 'xsrc'.) main points are that vax, hppa, ppc, and sh3 don't build yet, but most other things are either testing and working or at least building. @ text @d69 2 a70 1 #if SANITIZER_LINUX && defined(__aarch64__) && !SANITIZER_GO a97 1 MemTrace, a114 2 else if (p >= TraceMemBeg() && p < TraceMemEnd()) mem[MemTrace] += rss; d127 5 d135 2 a136 2 mem[MemMmap] -= meta.mem_block + meta.sync_obj + stacks.allocated + internal_stats[AllocatorStatMapped]; d141 6 a146 6 "%llus: RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd" " trace:%zd heap:%zd other:%zd intalloc:%zd memblocks:%zd syncobj:%zu" " stacks=%zd[%zd] nthr=%zd/%zd\n", uptime_ns / (1000 * 1000 * 1000), mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20, mem[MemHeap] >> 20, d148 2 a149 16 meta.mem_block >> 20, meta.sync_obj >> 20, stacks.allocated >> 20, stacks.n_uniq_ids, nlive, nthread); } # if SANITIZER_LINUX void FlushShadowMemoryCallback( const SuspendedThreadsList &suspended_threads_list, void *argument) { ReleaseMemoryPagesToOS(ShadowBeg(), ShadowEnd()); } #endif void FlushShadowMemory() { #if SANITIZER_LINUX StopTheWorld(FlushShadowMemoryCallback, 0); #endif d153 1 a153 1 // Mark shadow for .rodata sections with the special kShadowRodata marker. d155 1 a155 1 static void MapRodata() { d166 1 a166 2 char name[256]; internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d", d168 1 a168 1 uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); d171 1 a171 1 internal_unlink(name); // Unlink it now, so that we can reuse the buffer. d173 1 a173 1 // Fill the file with kShadowRodata. d179 1 a179 1 *p = kShadowRodata; d190 2 a191 2 // Reusing the buffer 'name'. MemoryMappedSegment segment(name, ARRAY_SIZE(name)); d211 2 a212 1 MapRodata(); d234 8 d270 11 a280 1 #endif d312 1 a312 1 #if SANITIZER_LINUX && defined(__aarch64__) d317 1 d326 3 d401 2 d412 3 a414 1 #elif defined(__s390x__) d418 3 a420 3 #else #error "Unknown platform" #endif d434 5 a438 1 # define LONG_JMP_SP_ENV_SLOT 2 d442 2 d446 7 a452 5 # elif defined(__s390x__) # define LONG_JMP_SP_ENV_SLOT 9 # else # define LONG_JMP_SP_ENV_SLOT 6 # endif d472 3 d476 1 @ 1.3 log @make this actually be GCC 12.3.0's libsanitizer. the libsanitizer we used with GCC 9 and GCC 10 was significantly ahead of the GCC 9 and GCC 10 provided versions. @ text @d411 2 @ 1.2 log @revert sanitizer back to the version we were using with GCC 9, since that one was already newer than the GCC 10 version. @ text @d11 1 a11 1 // Linux- and FreeBSD-specific code. a13 1 d24 1 d26 1 a26 1 #include "sanitizer_common/sanitizer_stackdepot.h" a28 1 #include "tsan_flags.h" a87 1 #ifdef TSAN_RUNTIME_VMA a89 1 #endif d92 9 a100 9 MemTotal = 0, MemShadow = 1, MemMeta = 2, MemFile = 3, MemMmap = 4, MemTrace = 5, MemHeap = 6, MemOther = 7, MemCount = 8, d103 1 a103 2 void FillProfileCallback(uptr p, uptr rss, bool file, uptr *mem, uptr stats_size) { d109 4 a112 1 #if !SANITIZER_GO a114 8 else if (p >= LoAppMemBeg() && p < LoAppMemEnd()) mem[file ? MemFile : MemMmap] += rss; else if (p >= HiAppMemBeg() && p < HiAppMemEnd()) mem[file ? MemFile : MemMmap] += rss; #else else if (p >= AppMemBeg() && p < AppMemEnd()) mem[file ? MemFile : MemMmap] += rss; #endif d121 1 a121 1 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) { d123 24 a146 11 internal_memset(mem, 0, sizeof(mem[0]) * MemCount); __sanitizer::GetMemoryProfile(FillProfileCallback, mem, 7); StackDepotStats *stacks = StackDepotGetStats(); internal_snprintf(buf, buf_size, "RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd" " trace:%zd heap:%zd other:%zd stacks=%zd[%zd] nthr=%zd/%zd\n", mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20, mem[MemHeap] >> 20, mem[MemOther] >> 20, stacks->allocated >> 20, stacks->n_uniq_ids, nlive, nthread); d149 1 a149 1 #if SANITIZER_LINUX d186 2 a187 2 const uptr kMarkerSize = 512 * 1024 / sizeof(u64); InternalMmapVector marker(kMarkerSize); d189 2 a190 1 for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++) d192 1 a192 1 internal_write(fd, marker.data(), marker.size() * sizeof(u64)); d212 4 a215 3 p += marker.size() * sizeof(u64)) { internal_mmap(p, Min(marker.size() * sizeof(u64), shadow_end - p), PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); a228 1 #ifdef TSAN_RUNTIME_VMA d259 14 a272 1 #endif d349 1 a349 1 // This is requried to properly handle "open" of these fds. d399 4 d408 7 a414 1 #ifdef __powerpc__ a417 2 #elif SANITIZER_NETBSD # define LONG_JMP_SP_ENV_SLOT 6 d423 2 d455 2 d466 4 a469 3 MemoryRangeImitateWrite(thr, /*pc=*/2, tls_addr, thr_beg - tls_addr); MemoryRangeImitateWrite(thr, /*pc=*/2, thr_end, tls_addr + tls_size - thr_end); d474 2 a475 3 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) { d480 1 a480 1 res = fn(c, m, abstime); d514 1 a514 1 *const_cast(&dead_thread_state->tid) = -1; @ 1.1 log @Initial revision @ text @@ 1.1.1.1 log @initial import of GCC 10.3.0. main changes include: caveats: - ABI issue between c++14 and c++17 fixed - profile mode is removed from libstdc++ - -fno-common is now the default new features: - new flags -fallocation-dce, -fprofile-partial-training, -fprofile-reproducible, -fprofile-prefix-path, and -fanalyzer - many new compile and link time optimisations - enhanced drive optimisations - openacc 2.6 support - openmp 5.0 features - new warnings: -Wstring-compare and -Wzero-length-bounds - extended warnings: -Warray-bounds, -Wformat-overflow, -Wrestrict, -Wreturn-local-addr, -Wstringop-overflow, -Warith-conversion, -Wmismatched-tags, and -Wredundant-tags - some likely C2X features implemented - more C++20 implemented - many new arm & intel CPUs known hundreds of reported bugs are fixed. full list of changes can be found at: https://gcc.gnu.org/gcc-10/changes.html @ text @@ 1.1.1.2 log @initial import of GCC 12.3.0. major changes in GCC 11 included: - The default mode for C++ is now -std=gnu++17 instead of -std=gnu++14. - When building GCC itself, the host compiler must now support C++11, rather than C++98. - Some short options of the gcov tool have been renamed: -i to -j and -j to -H. - ThreadSanitizer improvements. - Introduce Hardware-assisted AddressSanitizer support. - For targets that produce DWARF debugging information GCC now defaults to DWARF version 5. This can produce up to 25% more compact debug information compared to earlier versions. - Many optimisations. - The existing malloc attribute has been extended so that it can be used to identify allocator/deallocator API pairs. A pair of new -Wmismatched-dealloc and -Wmismatched-new-delete warnings are added. - Other new warnings: -Wsizeof-array-div, enabled by -Wall, warns about divisions of two sizeof operators when the first one is applied to an array and the divisor does not equal the size of the array element. -Wstringop-overread, enabled by default, warns about calls to string functions reading past the end of the arrays passed to them as arguments. -Wtsan, enabled by default, warns about unsupported features in ThreadSanitizer (currently std::atomic_thread_fence). - Enchanced warnings: -Wfree-nonheap-object detects many more instances of calls to deallocation functions with pointers not returned from a dynamic memory allocation function. -Wmaybe-uninitialized diagnoses passing pointers or references to uninitialized memory to functions taking const-qualified arguments. -Wuninitialized detects reads from uninitialized dynamically allocated memory. -Warray-parameter warns about functions with inconsistent array forms. -Wvla-parameter warns about functions with inconsistent VLA forms. - Several new features from the upcoming C2X revision of the ISO C standard are supported with -std=c2x and -std=gnu2x. - Several C++20 features have been implemented. - The C++ front end has experimental support for some of the upcoming C++23 draft. - Several new C++ warnings. - Enhanced Arm, AArch64, x86, and RISC-V CPU support. - The implementation of how program state is tracked within -fanalyzer has been completely rewritten with many enhancements. see https://gcc.gnu.org/gcc-11/changes.html for a full list. major changes in GCC 12 include: - An ABI incompatibility between C and C++ when passing or returning by value certain aggregates containing zero width bit-fields has been discovered on various targets. x86-64, ARM and AArch64 will always ignore them (so there is a C ABI incompatibility between GCC 11 and earlier with GCC 12 or later), PowerPC64 ELFv2 always take them into account (so there is a C++ ABI incompatibility, GCC 4.4 and earlier compatible with GCC 12 or later, incompatible with GCC 4.5 through GCC 11). RISC-V has changed the handling of these already starting with GCC 10. As the ABI requires, MIPS takes them into account handling function return values so there is a C++ ABI incompatibility with GCC 4.5 through 11. - STABS: Support for emitting the STABS debugging format is deprecated and will be removed in the next release. All ports now default to emit DWARF (version 2 or later) debugging info or are obsoleted. - Vectorization is enabled at -O2 which is now equivalent to the original -O2 -ftree-vectorize -fvect-cost-model=very-cheap. - GCC now supports the ShadowCallStack sanitizer. - Support for __builtin_shufflevector compatible with the clang language extension was added. - Support for attribute unavailable was added. - Support for __builtin_dynamic_object_size compatible with the clang language extension was added. - New warnings: -Wbidi-chars warns about potentially misleading UTF-8 bidirectional control characters. -Warray-compare warns about comparisons between two operands of array type. - Some new features from the upcoming C2X revision of the ISO C standard are supported with -std=c2x and -std=gnu2x. - Several C++23 features have been implemented. - Many C++ enhancements across warnings and -f options. see https://gcc.gnu.org/gcc-12/changes.html for a full list. @ text @d11 1 a11 1 // Linux- and BSD-specific code. d14 1 d25 1 a26 2 #include "sanitizer_common/sanitizer_stoptheworld.h" #include "tsan_flags.h" d29 1 d89 1 d92 1 d95 9 a103 9 MemTotal, MemShadow, MemMeta, MemFile, MemMmap, MemTrace, MemHeap, MemOther, MemCount, d106 2 a107 1 void FillProfileCallback(uptr p, uptr rss, bool file, uptr *mem) { d113 1 a113 4 else if ((p >= LoAppMemBeg() && p < LoAppMemEnd()) || (p >= MidAppMemBeg() && p < MidAppMemEnd()) || (p >= HiAppMemBeg() && p < HiAppMemEnd())) mem[file ? MemFile : MemMmap] += rss; d116 8 d130 1 a130 1 void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns) { d132 11 a142 24 internal_memset(mem, 0, sizeof(mem)); GetMemoryProfile(FillProfileCallback, mem); auto meta = ctx->metamap.GetMemoryStats(); StackDepotStats stacks = StackDepotGetStats(); uptr nthread, nlive; ctx->thread_registry.GetNumberOfThreads(&nthread, &nlive); uptr internal_stats[AllocatorStatCount]; internal_allocator()->GetStats(internal_stats); // All these are allocated from the common mmap region. mem[MemMmap] -= meta.mem_block + meta.sync_obj + stacks.allocated + internal_stats[AllocatorStatMapped]; if (s64(mem[MemMmap]) < 0) mem[MemMmap] = 0; internal_snprintf( buf, buf_size, "%llus: RSS %zd MB: shadow:%zd meta:%zd file:%zd mmap:%zd" " trace:%zd heap:%zd other:%zd intalloc:%zd memblocks:%zd syncobj:%zu" " stacks=%zd[%zd] nthr=%zd/%zd\n", uptime_ns / (1000 * 1000 * 1000), mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemTrace] >> 20, mem[MemHeap] >> 20, mem[MemOther] >> 20, internal_stats[AllocatorStatMapped] >> 20, meta.mem_block >> 20, meta.sync_obj >> 20, stacks.allocated >> 20, stacks.n_uniq_ids, nlive, nthread); d145 1 a145 1 # if SANITIZER_LINUX d182 2 a183 2 const uptr kMarkerSize = 512 * 1024 / sizeof(RawShadow); InternalMmapVector marker(kMarkerSize); d185 1 a185 2 for (volatile RawShadow *p = marker.data(); p < marker.data() + kMarkerSize; p++) d187 1 a187 1 internal_write(fd, marker.data(), marker.size() * sizeof(RawShadow)); d207 3 a209 4 p += marker.size() * sizeof(RawShadow)) { internal_mmap( p, Min(marker.size() * sizeof(RawShadow), shadow_end - p), PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0); d223 1 d254 1 a254 14 #elif defined(__mips64) # if !SANITIZER_GO if (vmaSize != 40) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 40\n", vmaSize); Die(); } # else if (vmaSize != 47) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 47\n", vmaSize); Die(); } # endif d331 1 a331 1 // This is required to properly handle "open" of these fds. a380 4 #elif defined(__s390x__) // tcbhead_t.stack_guard uptr xor_key = ((uptr *)__builtin_thread_pointer())[5]; return mangled_sp ^ xor_key; d386 1 a386 7 #if SANITIZER_NETBSD # ifdef __x86_64__ # define LONG_JMP_SP_ENV_SLOT 6 # else # error unsupported # endif #elif defined(__powerpc__) d390 2 a396 2 # elif defined(__s390x__) # define LONG_JMP_SP_ENV_SLOT 9 a426 2 extern "C" void __tsan_tls_initialization() {} d436 3 a438 4 const uptr pc = StackTrace::GetNextInstructionPc( reinterpret_cast(__tsan_tls_initialization)); MemoryRangeImitateWrite(thr, pc, tls_addr, thr_beg - tls_addr); MemoryRangeImitateWrite(thr, pc, thr_end, tls_addr + tls_size - thr_end); d443 3 a445 2 int call_pthread_cancel_with_cleanup(int (*fn)(void *arg), void (*cleanup)(void *arg), void *arg) { d450 1 a450 1 res = fn(arg); d484 1 a484 1 *const_cast(&dead_thread_state->tid) = -1; @ 1.1.1.3 log @initial import of GCC 14.3.0. major changes in GCC 13: - improved sanitizer - zstd debug info compression - LTO improvements - SARIF based diagnostic support - new warnings: -Wxor-used-as-pow, -Wenum-int-mismatch, -Wself-move, -Wdangling-reference - many new -Wanalyzer* specific warnings - enhanced warnings: -Wpessimizing-move, -Wredundant-move - new attributes to mark file descriptors, c++23 "assume" - several C23 features added - several C++23 features added - many new features for Arm, x86, RISC-V major changes in GCC 14: - more strict C99 or newer support - ia64* marked deprecated (but seemingly still in GCC 15.) - several new hardening features - support for "hardbool", which can have user supplied values of true/false - explicit support for stack scrubbing upon function exit - better auto-vectorisation support - added clang-compatible __has_feature and __has_extension - more C23, including -std=c23 - several C++26 features added - better diagnostics in C++ templates - new warnings: -Wnrvo, Welaborated-enum-base - many new features for Arm, x86, RISC-V - possible ABI breaking change for SPARC64 and small structures with arrays of floats. @ text @d69 1 a69 2 #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) && \ !SANITIZER_GO d97 1 d115 2 a128 5 uptr trace_mem; { Lock l(&ctx->slot_mtx); trace_mem = ctx->trace_part_total_allocated * sizeof(TracePart); } d132 2 a133 2 mem[MemMmap] -= meta.mem_block + meta.sync_obj + trace_mem + stacks.allocated + internal_stats[AllocatorStatMapped]; d138 6 a143 6 "==%zu== %llus [%zu]: RSS %zd MB: shadow:%zd meta:%zd file:%zd" " mmap:%zd heap:%zd other:%zd intalloc:%zd memblocks:%zd syncobj:%zu" " trace:%zu stacks=%zd threads=%zu/%zu\n", internal_getpid(), uptime_ns / (1000 * 1000 * 1000), ctx->global_epoch, mem[MemTotal] >> 20, mem[MemShadow] >> 20, mem[MemMeta] >> 20, mem[MemFile] >> 20, mem[MemMmap] >> 20, mem[MemHeap] >> 20, d145 16 a160 2 meta.mem_block >> 20, meta.sync_obj >> 20, trace_mem >> 20, stacks.allocated >> 20, nlive, nthread); d164 1 a164 1 // Mark shadow for .rodata sections with the special Shadow::kRodata marker. d166 1 a166 1 static NOINLINE void MapRodata(char* buffer, uptr size) { d177 2 a178 1 internal_snprintf(buffer, size, "%s/tsan.rodata.%d", d180 1 a180 1 uptr openrv = internal_open(buffer, O_RDWR | O_CREAT | O_EXCL, 0600); d183 1 a183 1 internal_unlink(buffer); // Unlink it now, so that we can reuse the buffer. d185 1 a185 1 // Fill the file with Shadow::kRodata. d191 1 a191 1 *p = Shadow::kRodata; d202 2 a203 2 // Reusing the buffer 'buffer'. MemoryMappedSegment segment(buffer, size); d223 1 a223 2 char buffer[256]; // Keep in a different frame. MapRodata(buffer, sizeof(buffer)); a244 8 #elif SANITIZER_LOONGARCH64 # if !SANITIZER_GO if (vmaSize != 47) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 47\n", vmaSize); Die(); } # endif d273 1 a273 11 # elif SANITIZER_RISCV64 // the bottom half of vma is allocated for userspace vmaSize = vmaSize + 1; # if !SANITIZER_GO if (vmaSize != 39 && vmaSize != 48) { Printf("FATAL: ThreadSanitizer: unsupported VMA range\n"); Printf("FATAL: Found %zd - Supported 39 and 48\n", vmaSize); Die(); } # endif # endif d305 1 a305 1 #if SANITIZER_ANDROID && (defined(__aarch64__) || defined(__x86_64__)) a309 1 // ASLR personality check. a317 3 #endif #if SANITIZER_LINUX && (defined(__aarch64__) || defined(__loongarch_lp64)) a389 2 #elif defined(__loongarch_lp64) return mangled_sp ^ longjmp_xor_key; d399 1 a399 3 # elif SANITIZER_RISCV64 return mangled_sp; # elif defined(__s390x__) d403 3 a405 3 # else # error "Unknown platform" # endif d417 1 a417 5 # ifdef __aarch64__ # define LONG_JMP_SP_ENV_SLOT 1 # else # define LONG_JMP_SP_ENV_SLOT 2 # endif a420 2 # elif defined(__loongarch__) # define LONG_JMP_SP_ENV_SLOT 1 d423 5 a427 7 # elif SANITIZER_RISCV64 # define LONG_JMP_SP_ENV_SLOT 13 # elif defined(__s390x__) # define LONG_JMP_SP_ENV_SLOT 9 # else # define LONG_JMP_SP_ENV_SLOT 6 # endif a446 3 #ifdef __loongarch__ asm("move %0, $sp" : "=r" (sp)); #else a447 1 #endif @