diff --git a/compiler-rt/lib/tsan/go/buildgo.sh b/compiler-rt/lib/tsan/go/buildgo.sh
index 9e21b77d5a0..2238caf53b3 100755
--- a/compiler-rt/lib/tsan/go/buildgo.sh
+++ b/compiler-rt/lib/tsan/go/buildgo.sh
@@ -1,200 +1,221 @@
 #!/bin/sh
 
 set -e
 
 SRCS="
 	tsan_go.cpp
 	../rtl/tsan_clock.cpp
 	../rtl/tsan_external.cpp
 	../rtl/tsan_flags.cpp
 	../rtl/tsan_interface_atomic.cpp
 	../rtl/tsan_md5.cpp
 	../rtl/tsan_mutex.cpp
 	../rtl/tsan_report.cpp
 	../rtl/tsan_rtl.cpp
 	../rtl/tsan_rtl_mutex.cpp
 	../rtl/tsan_rtl_report.cpp
 	../rtl/tsan_rtl_thread.cpp
 	../rtl/tsan_rtl_proc.cpp
 	../rtl/tsan_stack_trace.cpp
 	../rtl/tsan_stat.cpp
 	../rtl/tsan_suppressions.cpp
 	../rtl/tsan_sync.cpp
 	../../sanitizer_common/sanitizer_allocator.cpp
 	../../sanitizer_common/sanitizer_common.cpp
 	../../sanitizer_common/sanitizer_common_libcdep.cpp
 	../../sanitizer_common/sanitizer_deadlock_detector2.cpp
 	../../sanitizer_common/sanitizer_file.cpp
 	../../sanitizer_common/sanitizer_flag_parser.cpp
 	../../sanitizer_common/sanitizer_flags.cpp
 	../../sanitizer_common/sanitizer_libc.cpp
 	../../sanitizer_common/sanitizer_persistent_allocator.cpp
 	../../sanitizer_common/sanitizer_printf.cpp
 	../../sanitizer_common/sanitizer_suppressions.cpp
 	../../sanitizer_common/sanitizer_thread_registry.cpp
 	../../sanitizer_common/sanitizer_stackdepot.cpp
 	../../sanitizer_common/sanitizer_stacktrace.cpp
 	../../sanitizer_common/sanitizer_symbolizer.cpp
 	../../sanitizer_common/sanitizer_symbolizer_report.cpp
 	../../sanitizer_common/sanitizer_termination.cpp
 "
 
 if [ "`uname -a | grep Linux`" != "" ]; then
 	OSCFLAGS="-fPIC -Wno-maybe-uninitialized"
 	OSLDFLAGS="-lpthread -fPIC -fpie"
 	SRCS="
 		$SRCS
 		../rtl/tsan_platform_linux.cpp
 		../../sanitizer_common/sanitizer_posix.cpp
 		../../sanitizer_common/sanitizer_posix_libcdep.cpp
 		../../sanitizer_common/sanitizer_procmaps_common.cpp
 		../../sanitizer_common/sanitizer_procmaps_linux.cpp
 		../../sanitizer_common/sanitizer_linux.cpp
 		../../sanitizer_common/sanitizer_linux_libcdep.cpp
 		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
 		../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
 		"
 	if [ "`uname -a | grep ppc64le`" != "" ]; then
 		SUFFIX="linux_ppc64le"
 		ARCHCFLAGS="-m64"
 	elif [ "`uname -a | grep x86_64`" != "" ]; then
 		SUFFIX="linux_amd64"
 		ARCHCFLAGS="-m64"
 		OSCFLAGS="$OSCFLAGS -ffreestanding -Wno-unused-const-variable -Werror -Wno-unknown-warning-option"
 	elif [ "`uname -a | grep aarch64`" != "" ]; then
 		SUFFIX="linux_arm64"
 		ARCHCFLAGS=""
 	fi
 elif [ "`uname -a | grep FreeBSD`" != "" ]; then
 	# The resulting object still depends on libc.
 	# We removed this dependency for Go runtime for other OSes,
 	# and we should remove it for FreeBSD as well, but there is no pressing need.
 	DEPENDS_ON_LIBC=1
 	SUFFIX="freebsd_amd64"
 	OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
 	ARCHCFLAGS="-m64"
 	OSLDFLAGS="-lpthread -fPIC -fpie"
 	SRCS="
 		$SRCS
 		../rtl/tsan_platform_linux.cpp
 		../../sanitizer_common/sanitizer_posix.cpp
 		../../sanitizer_common/sanitizer_posix_libcdep.cpp
 		../../sanitizer_common/sanitizer_procmaps_bsd.cpp
 		../../sanitizer_common/sanitizer_procmaps_common.cpp
 		../../sanitizer_common/sanitizer_linux.cpp
 		../../sanitizer_common/sanitizer_linux_libcdep.cpp
 		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
 		../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
 	"
 elif [ "`uname -a | grep NetBSD`" != "" ]; then
 	# The resulting object still depends on libc.
 	# We removed this dependency for Go runtime for other OSes,
 	# and we should remove it for NetBSD as well, but there is no pressing need.
 	DEPENDS_ON_LIBC=1
 	SUFFIX="netbsd_amd64"
 	OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
 	ARCHCFLAGS="-m64"
 	OSLDFLAGS="-lpthread -fPIC -fpie"
 	SRCS="
 		$SRCS
 		../rtl/tsan_platform_linux.cpp
 		../../sanitizer_common/sanitizer_posix.cpp
 		../../sanitizer_common/sanitizer_posix_libcdep.cpp
 		../../sanitizer_common/sanitizer_procmaps_bsd.cpp
 		../../sanitizer_common/sanitizer_procmaps_common.cpp
 		../../sanitizer_common/sanitizer_linux.cpp
 		../../sanitizer_common/sanitizer_linux_libcdep.cpp
 		../../sanitizer_common/sanitizer_netbsd.cpp
 		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
 		../../sanitizer_common/sanitizer_stoptheworld_netbsd_libcdep.cpp
 	"
+elif [ "`uname -a | grep OpenBSD`" != "" ]; then
+	# The resulting object still depends on libc.
+	# We removed this dependency for Go runtime for other OSes,
+	# and we should remove it for OpenBSD as well, but there is no pressing need.
+	DEPENDS_ON_LIBC=1
+	SUFFIX="openbsd_amd64"
+	OSCFLAGS="-fno-strict-aliasing -fPIC -Werror"
+	ARCHCFLAGS="-m64"
+	OSLDFLAGS="-pthread -fPIC -fpie"
+	SRCS="
+		$SRCS
+		../rtl/tsan_platform_linux.cpp
+		../../sanitizer_common/sanitizer_posix.cpp
+		../../sanitizer_common/sanitizer_posix_libcdep.cpp
+		../../sanitizer_common/sanitizer_procmaps_bsd.cpp
+		../../sanitizer_common/sanitizer_procmaps_common.cpp
+		../../sanitizer_common/sanitizer_linux.cpp
+		../../sanitizer_common/sanitizer_linux_libcdep.cpp
+		../../sanitizer_common/sanitizer_openbsd.cpp
+		../../sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cpp
+	"
 elif [ "`uname -a | grep Darwin`" != "" ]; then
 	SUFFIX="darwin_amd64"
 	OSCFLAGS="-fPIC -Wno-unused-const-variable -Wno-unknown-warning-option -mmacosx-version-min=10.7"
 	ARCHCFLAGS="-m64"
 	OSLDFLAGS="-lpthread -fPIC -fpie -mmacosx-version-min=10.7"
 	SRCS="
 		$SRCS
 		../rtl/tsan_platform_mac.cpp
 		../../sanitizer_common/sanitizer_mac.cpp
 		../../sanitizer_common/sanitizer_posix.cpp
 		../../sanitizer_common/sanitizer_posix_libcdep.cpp
 		../../sanitizer_common/sanitizer_procmaps_mac.cpp
 	"
 elif [ "`uname -a | grep MINGW`" != "" ]; then
 	SUFFIX="windows_amd64"
 	OSCFLAGS="-Wno-error=attributes -Wno-attributes -Wno-unused-const-variable -Wno-unknown-warning-option"
 	ARCHCFLAGS="-m64"
 	OSLDFLAGS=""
 	SRCS="
 		$SRCS
 		../rtl/tsan_platform_windows.cpp
 		../../sanitizer_common/sanitizer_win.cpp
 	"
 else
 	echo Unknown platform
 	exit 1
 fi
 
 CC=${CC:-gcc}
 IN_TMPDIR=${IN_TMPDIR:-0}
 SILENT=${SILENT:-0}
 
 if [ $IN_TMPDIR != "0" ]; then
   DIR=$(mktemp -qd /tmp/gotsan.XXXXXXXXXX)
   cleanup() {
     rm -rf $DIR
   }
   trap cleanup EXIT
 else
   DIR=.
 fi
 
 SRCS="$SRCS $ADD_SRCS"
 
 rm -f $DIR/gotsan.cpp
 for F in $SRCS; do
 	cat $F >> $DIR/gotsan.cpp
 done
 
 FLAGS=" -I../rtl -I../.. -I../../sanitizer_common -I../../../include -std=c++14 -Wall -fno-exceptions -fno-rtti -DSANITIZER_GO=1 -DSANITIZER_DEADLOCK_DETECTOR_VERSION=2 $OSCFLAGS $ARCHCFLAGS"
 DEBUG_FLAGS="$FLAGS -DSANITIZER_DEBUG=1 -g"
 FLAGS="$FLAGS -DSANITIZER_DEBUG=0 -O3 -fomit-frame-pointer"
 if [ "$SUFFIX" = "linux_ppc64le" ]; then
 	FLAGS="$FLAGS -mcpu=power8 -fno-function-sections"
 elif [ "$SUFFIX" = "linux_amd64" ]; then
 	FLAGS="$FLAGS -msse3"
 fi
 
 if [ "$DEBUG" = "" ]; then
 	# Do a build test with debug flags.
 	$CC $DIR/gotsan.cpp -c -o $DIR/race_debug_$SUFFIX.syso $DEBUG_FLAGS $CFLAGS
 else
 	FLAGS="$DEBUG_FLAGS"
 fi
 
 if [ "$SILENT" != "1" ]; then
   echo $CC gotsan.cpp -c -o $DIR/race_$SUFFIX.syso $FLAGS $CFLAGS
 fi
 $CC $DIR/gotsan.cpp -c -o $DIR/race_$SUFFIX.syso $FLAGS $CFLAGS
 
 $CC $OSCFLAGS $ARCHCFLAGS test.c $DIR/race_$SUFFIX.syso -g -o $DIR/test $OSLDFLAGS $LDFLAGS
 
 # Verify that no libc specific code is present.
 if [ "$DEPENDS_ON_LIBC" != "1" ]; then
 	if nm $DIR/race_$SUFFIX.syso | grep -q __libc_; then
 		printf -- '%s seems to link to libc\n' "race_$SUFFIX.syso"
 		exit 1
 	fi
 fi
 
 if [ "`uname -a | grep NetBSD`" != "" ]; then
   # Turn off ASLR in the test binary.
   /usr/sbin/paxctl +a $DIR/test
 fi
 export GORACE="exitcode=0 atexit_sleep_ms=0"
 if [ "$SILENT" != "1" ]; then
   $DIR/test
 else
   $DIR/test 2>/dev/null
 fi
diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
index 33fa586ca1b..be459b47a0d 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
@@ -1,515 +1,516 @@
 //===-- 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 FreeBSD-specific code.
+// Linux- and BSD-specific code.
 //===----------------------------------------------------------------------===//
 
 
 #include "sanitizer_common/sanitizer_platform.h"
-#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
+#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
 
 #include "sanitizer_common/sanitizer_common.h"
 #include "sanitizer_common/sanitizer_libc.h"
 #include "sanitizer_common/sanitizer_linux.h"
+#include "sanitizer_common/sanitizer_platform_limits_openbsd.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_stoptheworld.h"
 #include "sanitizer_common/sanitizer_stackdepot.h"
 #include "tsan_platform.h"
 #include "tsan_rtl.h"
 #include "tsan_flags.h"
 
 #include <fcntl.h>
 #include <pthread.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
 #include <sys/mman.h>
 #if SANITIZER_LINUX
 #include <sys/personality.h>
 #include <setjmp.h>
 #endif
 #include <sys/syscall.h>
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sched.h>
 #include <dlfcn.h>
 #if SANITIZER_LINUX
 #define __need_res_state
 #include <resolv.h>
 #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__) && !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
 
 #ifdef TSAN_RUNTIME_VMA
 // Runtime detected VMA size.
 uptr vmaSize;
 #endif
 
 enum {
   MemTotal  = 0,
   MemShadow = 1,
   MemMeta   = 2,
   MemFile   = 3,
   MemMmap   = 4,
   MemTrace  = 5,
   MemHeap   = 6,
   MemOther  = 7,
   MemCount  = 8,
 };
 
 void FillProfileCallback(uptr p, uptr rss, bool file,
                          uptr *mem, uptr stats_size) {
   mem[MemTotal] += rss;
   if (p >= ShadowBeg() && p < ShadowEnd())
     mem[MemShadow] += rss;
   else if (p >= MetaShadowBeg() && p < MetaShadowEnd())
     mem[MemMeta] += rss;
 #if !SANITIZER_GO
   else if (p >= HeapMemBeg() && p < HeapMemEnd())
     mem[MemHeap] += rss;
   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
   else if (p >= TraceMemBeg() && p < TraceMemEnd())
     mem[MemTrace] += rss;
   else
     mem[MemOther] += rss;
 }
 
 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive) {
   uptr mem[MemCount];
   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);
 }
 
 #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
 }
 
 #if !SANITIZER_GO
 // Mark shadow for .rodata sections with the special kShadowRodata marker.
 // Accesses to .rodata can't race, so this saves time, memory and trace space.
 static void MapRodata() {
   // 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;
   char name[256];
   internal_snprintf(name, sizeof(name), "%s/tsan.rodata.%d",
                     tmpdir, (int)internal_getpid());
   uptr openrv = internal_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
   if (internal_iserror(openrv))
     return;
   internal_unlink(name);  // Unlink it now, so that we can reuse the buffer.
   fd_t fd = openrv;
   // Fill the file with kShadowRodata.
   const uptr kMarkerSize = 512 * 1024 / sizeof(u64);
   InternalMmapVector<u64> marker(kMarkerSize);
   // volatile to prevent insertion of memset
   for (volatile u64 *p = marker.data(); p < marker.data() + kMarkerSize; p++)
     *p = kShadowRodata;
   internal_write(fd, marker.data(), marker.size() * sizeof(u64));
   // 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 'name'.
   MemoryMappedSegment segment(name, ARRAY_SIZE(name));
   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(u64)) {
         internal_mmap(p, Min<uptr>(marker.size() * sizeof(u64), shadow_end - p),
                       PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0);
       }
     }
   }
   internal_close(fd);
 }
 
 void InitializeShadowMemoryPlatform() {
   MapRodata();
 }
 
 #endif  // #if !SANITIZER_GO
 
 void InitializePlatformEarly() {
 #ifdef TSAN_RUNTIME_VMA
   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 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
 #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_LINUX && defined(__aarch64__)
     // 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.
     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;
     }
     // 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 requried 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(__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;
 #else
   #error "Unknown platform"
 #endif
 }
 
 #ifdef __powerpc__
 # define LONG_JMP_SP_ENV_SLOT 0
 #elif SANITIZER_FREEBSD
 # define LONG_JMP_SP_ENV_SLOT 2
 #elif SANITIZER_NETBSD
 # define LONG_JMP_SP_ENV_SLOT 6
 #elif SANITIZER_LINUX
 # ifdef __aarch64__
 #  define LONG_JMP_SP_ENV_SLOT 13
 # elif defined(__mips64)
 #  define LONG_JMP_SP_ENV_SLOT 1
 # 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;
   asm("mov  %0, sp" : "=r" (sp));
   uptr mangled_sp = ((uptr *)&env)[LONG_JMP_SP_ENV_SLOT];
 
   // 3. xor SPs to obtain key.
   longjmp_xor_key = mangled_sp ^ sp;
 }
 #endif
 
 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.
   MemoryRangeImitateWrite(thr, /*pc=*/2, tls_addr, thr_beg - tls_addr);
   MemoryRangeImitateWrite(thr, /*pc=*/2, 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 *c, void *m,
     void *abstime), void *c, void *m, void *abstime,
     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(c, m, abstime);
   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<ThreadState*>(*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<ThreadState*>(*get_android_tls_ptr());
     if (thr == nullptr) {
       thr = reinterpret_cast<ThreadState*>(MmapOrDie(sizeof(ThreadState),
                                                      "ThreadState"));
       *get_android_tls_ptr() = reinterpret_cast<uptr>(thr);
       if (dead_thread_state == nullptr) {
         dead_thread_state = reinterpret_cast<ThreadState*>(
             MmapOrDie(sizeof(ThreadState), "ThreadState"));
         dead_thread_state->fast_state.SetIgnoreBit();
         dead_thread_state->ignore_interceptors = 1;
         dead_thread_state->is_dead = true;
         *const_cast<int*>(&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<uptr>(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<ThreadState*>(*get_android_tls_ptr());
   if (thr != dead_thread_state) {
     *get_android_tls_ptr() = reinterpret_cast<uptr>(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
+#endif  // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
