[llvm-branch-commits] [compiler-rt] [compiler-rt] Add Device AddressSanitizer runtime (PR #242445)

Joseph Huber via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 14 11:34:02 PST 2026


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/242445

>From 0053d55322ca44183a6017f3c82e0ff5238d91df Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Thu, 12 Nov 2026 17:38:12 -0600
Subject: [PATCH] [compiler-rt] Add Device AddressSanitizer runtime

Host and device runtime for dasan, built on the sanitizer_common offload
layer. The common layer gains the HSA pieces dasan needs: pool and agent
queries, virtual memory reservation and sharing, code object enumeration,
cache publication, and device symbolization. The runtime keeps the
allocator, global aliasing, and report formatting. Each device begins on
its own metadata granule, so an entry load stays on the device that owns
the object.

The runtime follows ubsan's offload shape: HSA interceptors that report HSA
as absent when it is not loaded, a dasan-preinit archive, static and shared
runtimes, a receive-only RPC handler on SANITIZER_OFFLOAD_DASAN, and lit
tests on the common GPU substitutions. The report handlers elect the first
active lane, which is the first failing one, and take their return address
as the location of the access. Host checks call __dasan_report_load and
__dasan_report_store and take the caller's frame.

Fine-grain pools are placed only when the AMD extension honors pool flags
on the virtual-memory path. A point-to-point request on an older runtime
is left with the real allocator. Reports name the allocating and freeing
threads and omit the HSA interceptor and the runtime that called it.

RFC: https://discourse.llvm.org/t/rfc-add-a-device-address-sanitizer-for-gpus-dasan/92710
---
 .../cmake/Modules/AllSupportedArchDefs.cmake  |   1 +
 compiler-rt/cmake/caches/AMDGPU.cmake         |   2 +-
 compiler-rt/cmake/config-ix.cmake             |  15 +-
 compiler-rt/lib/dasan/.clang-format           |   3 +
 compiler-rt/lib/dasan/CMakeLists.txt          | 152 ++++
 compiler-rt/lib/dasan/dasan.cpp               | 187 +++++
 compiler-rt/lib/dasan/dasan.h                 |  40 +
 compiler-rt/lib/dasan/dasan.syms.extra        |   1 +
 compiler-rt/lib/dasan/dasan_allocator.cpp     | 792 ++++++++++++++++++
 compiler-rt/lib/dasan/dasan_allocator.h       | 273 ++++++
 compiler-rt/lib/dasan/dasan_device.cpp        | 115 +++
 compiler-rt/lib/dasan/dasan_flags.h           |  36 +
 compiler-rt/lib/dasan/dasan_flags.inc         |  38 +
 compiler-rt/lib/dasan/dasan_host.cpp          |  53 ++
 .../lib/dasan/dasan_hsa_interceptors.cpp      | 448 ++++++++++
 compiler-rt/lib/dasan/dasan_image.cpp         | 283 +++++++
 compiler-rt/lib/dasan/dasan_image.h           |  49 ++
 compiler-rt/lib/dasan/dasan_mapping.h         | 236 ++++++
 compiler-rt/lib/dasan/dasan_offload_packet.h  |  44 +
 compiler-rt/lib/dasan/dasan_platform.cpp      | 332 ++++++++
 compiler-rt/lib/dasan/dasan_platform.h        |  73 ++
 compiler-rt/lib/dasan/dasan_preinit.cpp       |  22 +
 compiler-rt/lib/dasan/dasan_report.cpp        | 446 ++++++++++
 compiler-rt/lib/dasan/dasan_report.h          |  34 +
 .../sanitizer_common/sanitizer_offload.cpp    | 531 ++++++++++--
 .../lib/sanitizer_common/sanitizer_offload.h  | 102 ++-
 .../sanitizer_common/sanitizer_offload_hsa.h  | 205 ++++-
 .../sanitizer_offload_opcodes.h               |   1 +
 .../sanitizer_offload_rpc.cpp                 |  10 +-
 compiler-rt/test/Inputs/hip.h                 |  28 +
 compiler-rt/test/dasan/AMDGPU/access-size.hip |  96 +++
 compiler-rt/test/dasan/AMDGPU/clean.hip       |  32 +
 .../test/dasan/AMDGPU/device-malloc.hip       |  33 +
 compiler-rt/test/dasan/AMDGPU/flags.hip       |  44 +
 .../test/dasan/AMDGPU/global-bounds.hip       | 104 +++
 .../test/dasan/AMDGPU/global-linkage.hip      |  50 ++
 .../test/dasan/AMDGPU/global-overflow.hip     |  24 +
 compiler-rt/test/dasan/AMDGPU/heap-bounds.hip |  92 ++
 .../test/dasan/AMDGPU/heap-overflow.hip       |  28 +
 .../test/dasan/AMDGPU/host-overflow.hip       |  24 +
 .../test/dasan/AMDGPU/lit.local.cfg.py        |  10 +
 .../test/dasan/AMDGPU/module-unload.hip       |  44 +
 .../test/dasan/AMDGPU/multi-device.hip        | 127 +++
 .../test/dasan/AMDGPU/openmp-overflow.cpp     |  49 ++
 .../test/dasan/AMDGPU/openmp-section-map.c    |  32 +
 compiler-rt/test/dasan/AMDGPU/quarantine.hip  |  31 +
 compiler-rt/test/dasan/AMDGPU/recover.hip     | 107 +++
 .../test/dasan/AMDGPU/report-location.hip     |  56 ++
 .../test/dasan/AMDGPU/shared-bounds.hip       | 112 +++
 .../test/dasan/AMDGPU/shared-overlay.hip      |  34 +
 .../test/dasan/AMDGPU/size-classes.hip        |  49 ++
 compiler-rt/test/dasan/AMDGPU/stress.hip      | 127 +++
 compiler-rt/test/dasan/AMDGPU/symbol-abi.hip  |  57 ++
 compiler-rt/test/dasan/AMDGPU/two-modules.hip |  46 +
 compiler-rt/test/dasan/CMakeLists.txt         |  56 ++
 .../test/dasan/Inputs/second-module.hip       |  14 +
 compiler-rt/test/dasan/hsa-load-order.c       |  33 +
 compiler-rt/test/dasan/hsa-missing.c          |  23 +
 compiler-rt/test/dasan/hsa-two-runtimes.c     |  53 ++
 compiler-rt/test/dasan/initialize.c           |   6 +
 compiler-rt/test/dasan/lit.cfg.py             |  49 ++
 compiler-rt/test/dasan/lit.site.cfg.py.in     |  14 +
 62 files changed, 6125 insertions(+), 83 deletions(-)
 create mode 100644 compiler-rt/lib/dasan/.clang-format
 create mode 100644 compiler-rt/lib/dasan/CMakeLists.txt
 create mode 100644 compiler-rt/lib/dasan/dasan.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan.h
 create mode 100644 compiler-rt/lib/dasan/dasan.syms.extra
 create mode 100644 compiler-rt/lib/dasan/dasan_allocator.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_allocator.h
 create mode 100644 compiler-rt/lib/dasan/dasan_device.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_flags.h
 create mode 100644 compiler-rt/lib/dasan/dasan_flags.inc
 create mode 100644 compiler-rt/lib/dasan/dasan_host.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_hsa_interceptors.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_image.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_image.h
 create mode 100644 compiler-rt/lib/dasan/dasan_mapping.h
 create mode 100644 compiler-rt/lib/dasan/dasan_offload_packet.h
 create mode 100644 compiler-rt/lib/dasan/dasan_platform.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_platform.h
 create mode 100644 compiler-rt/lib/dasan/dasan_preinit.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_report.cpp
 create mode 100644 compiler-rt/lib/dasan/dasan_report.h
 create mode 100644 compiler-rt/test/dasan/AMDGPU/access-size.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/clean.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/device-malloc.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/flags.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/global-bounds.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/global-linkage.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/global-overflow.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/heap-bounds.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/heap-overflow.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/host-overflow.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/lit.local.cfg.py
 create mode 100644 compiler-rt/test/dasan/AMDGPU/module-unload.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/multi-device.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/openmp-overflow.cpp
 create mode 100644 compiler-rt/test/dasan/AMDGPU/openmp-section-map.c
 create mode 100644 compiler-rt/test/dasan/AMDGPU/quarantine.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/recover.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/report-location.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/shared-bounds.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/shared-overlay.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/size-classes.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/stress.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/symbol-abi.hip
 create mode 100644 compiler-rt/test/dasan/AMDGPU/two-modules.hip
 create mode 100644 compiler-rt/test/dasan/CMakeLists.txt
 create mode 100644 compiler-rt/test/dasan/Inputs/second-module.hip
 create mode 100644 compiler-rt/test/dasan/hsa-load-order.c
 create mode 100644 compiler-rt/test/dasan/hsa-missing.c
 create mode 100644 compiler-rt/test/dasan/hsa-two-runtimes.c
 create mode 100644 compiler-rt/test/dasan/initialize.c
 create mode 100644 compiler-rt/test/dasan/lit.cfg.py
 create mode 100644 compiler-rt/test/dasan/lit.site.cfg.py.in

diff --git a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
index 6584f0180b7c36..349ea0489d3229 100644
--- a/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
+++ b/compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake
@@ -52,6 +52,7 @@ set(ALL_ASAN_ABI_SUPPORTED_ARCH ${X86_64} ${ARM64} ${ARM64_32})
 set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${LOONGARCH64}
     ${S390X})
 set(ALL_RTSAN_SUPPORTED_ARCH ${X86_64} ${ARM64} ${HEXAGON})
+set(ALL_DASAN_SUPPORTED_ARCH ${X86_64} ${ARM64} ${AMDGPU})
 
 if(ANDROID)
   set(OS_NAME "Android")
diff --git a/compiler-rt/cmake/caches/AMDGPU.cmake b/compiler-rt/cmake/caches/AMDGPU.cmake
index 34a5559fe52990..efb8ad1a3937b4 100644
--- a/compiler-rt/cmake/caches/AMDGPU.cmake
+++ b/compiler-rt/cmake/caches/AMDGPU.cmake
@@ -7,7 +7,7 @@ set(COMPILER_RT_BUILD_BUILTINS ON CACHE BOOL "")
 set(COMPILER_RT_BAREMETAL_BUILD ON CACHE BOOL "")
 set(COMPILER_RT_BUILD_CRT OFF CACHE BOOL "")
 set(COMPILER_RT_BUILD_SANITIZERS ON CACHE BOOL "")
-set(COMPILER_RT_SANITIZERS_TO_BUILD "ubsan;ubsan_minimal;csan" CACHE STRING "")
+set(COMPILER_RT_SANITIZERS_TO_BUILD "ubsan;ubsan_minimal;csan;dasan" CACHE STRING "")
 set(COMPILER_RT_BUILD_XRAY OFF CACHE BOOL "")
 set(COMPILER_RT_BUILD_LIBFUZZER OFF CACHE BOOL "")
 set(COMPILER_RT_BUILD_PROFILE ON CACHE BOOL "")
diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index 2ac72d6f8776c5..552f1bdde41a99 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -676,6 +676,9 @@ if(APPLE)
   list_intersect(RTSAN_SUPPORTED_ARCH
     ALL_RTSAN_SUPPORTED_ARCH
     SANITIZER_COMMON_SUPPORTED_ARCH)
+  list_intersect(DASAN_SUPPORTED_ARCH
+    ALL_DASAN_SUPPORTED_ARCH
+    SANITIZER_COMMON_SUPPORTED_ARCH)
   list_intersect(DFSAN_SUPPORTED_ARCH
     ALL_DFSAN_SUPPORTED_ARCH
     SANITIZER_COMMON_SUPPORTED_ARCH)
@@ -749,6 +752,7 @@ else()
     ${ALL_UBSAN_SUPPORTED_ARCH})
   filter_available_targets(ASAN_SUPPORTED_ARCH ${ALL_ASAN_SUPPORTED_ARCH})
   filter_available_targets(RTSAN_SUPPORTED_ARCH ${ALL_RTSAN_SUPPORTED_ARCH})
+  filter_available_targets(DASAN_SUPPORTED_ARCH ${ALL_DASAN_SUPPORTED_ARCH})
   filter_available_targets(FUZZER_SUPPORTED_ARCH ${ALL_FUZZER_SUPPORTED_ARCH})
   filter_available_targets(DFSAN_SUPPORTED_ARCH ${ALL_DFSAN_SUPPORTED_ARCH})
   filter_available_targets(LSAN_SUPPORTED_ARCH ${ALL_LSAN_SUPPORTED_ARCH})
@@ -800,7 +804,7 @@ if(COMPILER_RT_SUPPORTED_ARCH)
 endif()
 message(STATUS "Compiler-RT supported architectures: ${COMPILER_RT_SUPPORTED_ARCH}")
 
-set(ALL_SANITIZERS asan;rtsan;dfsan;msan;hwasan;tsan;csan;tysan;safestack;cfi;scudo_standalone;ubsan_minimal;gwp_asan;nsan;asan_abi)
+set(ALL_SANITIZERS asan;rtsan;dasan;dfsan;msan;hwasan;tsan;csan;tysan;safestack;cfi;scudo_standalone;ubsan_minimal;gwp_asan;nsan;asan_abi)
 set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
     "sanitizers to build if supported on the target (all;${ALL_SANITIZERS})")
 list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")
@@ -845,6 +849,15 @@ else()
   set(COMPILER_RT_HAS_RTSAN FALSE)
 endif()
 
+if (DASAN_SUPPORTED_ARCH AND
+    "dasan" IN_LIST COMPILER_RT_SANITIZERS_TO_BUILD AND
+    (COMPILER_RT_GPU_BUILD OR
+     (COMPILER_RT_HAS_SANITIZER_COMMON AND OS_NAME MATCHES "Linux")))
+  set(COMPILER_RT_HAS_DASAN TRUE)
+else()
+  set(COMPILER_RT_HAS_DASAN FALSE)
+endif()
+
 if (OS_NAME MATCHES "Linux|FreeBSD|Windows|NetBSD|SunOS")
   set(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME TRUE)
 else()
diff --git a/compiler-rt/lib/dasan/.clang-format b/compiler-rt/lib/dasan/.clang-format
new file mode 100644
index 00000000000000..1f2a97030379da
--- /dev/null
+++ b/compiler-rt/lib/dasan/.clang-format
@@ -0,0 +1,3 @@
+BasedOnStyle: Google
+AllowShortIfStatementsOnASingleLine: false
+IndentPPDirectives: AfterHash
diff --git a/compiler-rt/lib/dasan/CMakeLists.txt b/compiler-rt/lib/dasan/CMakeLists.txt
new file mode 100644
index 00000000000000..4958930b8aaf94
--- /dev/null
+++ b/compiler-rt/lib/dasan/CMakeLists.txt
@@ -0,0 +1,152 @@
+include_directories(..)
+
+include(FindLibcCommonUtils)
+
+# The GPU half only reports failed accesses over RPC.
+if(COMPILER_RT_GPU_BUILD)
+  if(NOT TARGET llvm-libc-common-utilities)
+    add_compiler_rt_component(dasan)
+    return()
+  endif()
+
+  set(DASAN_DEVICE_HEADERS
+    dasan_offload_packet.h
+    )
+
+  set(DASAN_DEVICE_CFLAGS
+    ${SANITIZER_COMMON_CFLAGS}
+    -DSANITIZER_COMMON_NO_REDEFINE_BUILTINS)
+  append_rtti_flag(OFF DASAN_DEVICE_CFLAGS)
+
+  add_compiler_rt_component(dasan)
+
+  add_compiler_rt_runtime(clang_rt.dasan
+    STATIC
+    ARCHS ${DASAN_SUPPORTED_ARCH}
+    SOURCES dasan_device.cpp
+    ADDITIONAL_HEADERS ${DASAN_DEVICE_HEADERS}
+    CFLAGS ${DASAN_DEVICE_CFLAGS}
+    LINK_LIBS llvm-libc-common-utilities
+    PARENT_TARGET dasan)
+
+  return()
+endif()
+
+if(NOT OS_NAME MATCHES "Linux" OR NOT TARGET llvm-libc-common-utilities)
+  return()
+endif()
+
+set(DASAN_SOURCES
+  dasan.cpp
+  dasan_allocator.cpp
+  dasan_host.cpp
+  dasan_hsa_interceptors.cpp
+  dasan_image.cpp
+  dasan_platform.cpp
+  dasan_report.cpp
+  )
+
+set(DASAN_HEADERS
+  dasan.h
+  dasan_allocator.h
+  dasan_flags.h
+  dasan_flags.inc
+  dasan_image.h
+  dasan_mapping.h
+  dasan_offload_packet.h
+  dasan_platform.h
+  dasan_report.h
+  )
+
+set(DASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
+append_rtti_flag(OFF DASAN_CFLAGS)
+
+set(DASAN_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
+
+set(DASAN_DYNAMIC_LIBS
+  ${COMPILER_RT_UNWINDER_LINK_LIBS}
+  ${SANITIZER_CXX_ABI_LIBRARIES}
+  ${SANITIZER_COMMON_LINK_LIBS})
+
+append_list_if(COMPILER_RT_HAS_LIBDL dl DASAN_DYNAMIC_LIBS)
+append_list_if(COMPILER_RT_HAS_LIBRT rt DASAN_DYNAMIC_LIBS)
+append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread DASAN_DYNAMIC_LIBS)
+
+add_compiler_rt_object_libraries(RTDasan
+  ARCHS ${DASAN_SUPPORTED_ARCH}
+  SOURCES ${DASAN_SOURCES}
+  ADDITIONAL_HEADERS ${DASAN_HEADERS}
+  CFLAGS ${DASAN_CFLAGS})
+foreach(arch ${DASAN_SUPPORTED_ARCH})
+  target_link_libraries(RTDasan.${arch} PRIVATE llvm-libc-common-utilities)
+endforeach()
+
+add_compiler_rt_component(dasan)
+
+add_compiler_rt_runtime(clang_rt.dasan
+  STATIC
+  ARCHS ${DASAN_SUPPORTED_ARCH}
+  OBJECT_LIBS RTDasan
+              RTSanitizerOffload
+              RTInterception
+              RTSanitizerCommon
+              RTSanitizerCommonLibc
+              RTSanitizerCommonCoverage
+              RTSanitizerCommonSymbolizer
+  CFLAGS ${DASAN_CFLAGS}
+  PARENT_TARGET dasan)
+
+add_compiler_rt_runtime(clang_rt.dasan-preinit
+  STATIC
+  ARCHS ${DASAN_SUPPORTED_ARCH}
+  SOURCES dasan_preinit.cpp
+  ADDITIONAL_HEADERS ${DASAN_HEADERS}
+  CFLAGS ${DASAN_CFLAGS}
+  PARENT_TARGET dasan)
+
+if(COMPILER_RT_HAS_VERSION_SCRIPT)
+  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
+  add_compiler_rt_object_libraries(RTDasan_dynamic_version_script_dummy
+    ARCHS ${DASAN_SUPPORTED_ARCH}
+    SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
+    CFLAGS ${DASAN_CFLAGS})
+
+  foreach(arch ${DASAN_SUPPORTED_ARCH})
+    add_sanitizer_rt_version_list(clang_rt.dasan-dynamic-${arch}
+                                  LIBS clang_rt.dasan-${arch}
+                                  EXTRA dasan.syms.extra)
+    set(VERSION_SCRIPT_FLAG
+        -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.dasan-dynamic-${arch}.vers)
+    if(COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
+      list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
+    endif()
+    set_property(SOURCE
+      ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
+      APPEND PROPERTY
+      OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.dasan-dynamic-${arch}.vers)
+
+    add_compiler_rt_runtime(clang_rt.dasan
+      SHARED
+      ARCHS ${arch}
+      OBJECT_LIBS RTDasan
+                  RTSanitizerOffload
+                  RTInterception
+                  RTSanitizerCommon
+                  RTSanitizerCommonLibc
+                  RTSanitizerCommonCoverage
+                  RTSanitizerCommonSymbolizer
+                  RTDasan_dynamic_version_script_dummy
+      CFLAGS ${DASAN_CFLAGS}
+      LINK_FLAGS ${DASAN_LINK_FLAGS} ${VERSION_SCRIPT_FLAG}
+      LINK_LIBS ${DASAN_DYNAMIC_LIBS}
+      PARENT_TARGET dasan)
+  endforeach()
+endif()
+
+# The list of symbols the driver hands the linker as --dynamic-list.
+if(SANITIZER_USE_SYMBOLS)
+  add_sanitizer_rt_symbols(clang_rt.dasan
+    ARCHS ${DASAN_SUPPORTED_ARCH}
+    PARENT_TARGET dasan
+    EXTRA dasan.syms.extra)
+endif()
diff --git a/compiler-rt/lib/dasan/dasan.cpp b/compiler-rt/lib/dasan/dasan.cpp
new file mode 100644
index 00000000000000..3352f375575513
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan.cpp
@@ -0,0 +1,187 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Initialization and runtime flags for the device address sanitizer.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan.h"
+
+#include <dlfcn.h>
+
+#include "dasan_flags.h"
+#include "dasan_report.h"
+#include "sanitizer_common/sanitizer_atomic.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_flag_parser.h"
+#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_mutex.h"
+#include "sanitizer_common/sanitizer_offload.h"
+#include "sanitizer_common/sanitizer_procmaps.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+using namespace __sanitizer;
+
+namespace __dasan {
+
+Mutex DasanMutex;
+Flags DasanFlags;
+
+void Flags::SetDefaults() {
+#define DASAN_FLAG(Type, Name, Default, Description) Name = Default;
+#include "dasan_flags.inc"
+#undef DASAN_FLAG
+}
+
+static void RegisterDasanFlags(FlagParser* Parser, Flags* F) {
+#define DASAN_FLAG(Type, Name, Default, Description) \
+  RegisterFlag(Parser, #Name, Description, &F->Name);
+#include "dasan_flags.inc"
+#undef DASAN_FLAG
+}
+
+void InitializeFlags() {
+  SetCommonFlagsDefaults();
+  {
+    CommonFlags Cf;
+    Cf.CopyFrom(*common_flags());
+    Cf.external_symbolizer_path = GetEnv("DASAN_SYMBOLIZER_PATH");
+    Cf.malloc_context_size = 30;
+    OverrideCommonFlags(Cf);
+  }
+
+  Flags* F = flags();
+  F->SetDefaults();
+
+  FlagParser Parser;
+  RegisterDasanFlags(&Parser, F);
+  RegisterCommonFlags(&Parser);
+
+  Parser.ParseString(__dasan_default_options());
+  Parser.ParseStringFromEnv("DASAN_OPTIONS");
+
+  InitializeCommonFlags();
+
+  if (Verbosity())
+    ReportUnrecognizedFlags();
+
+  if (common_flags()->help)
+    Parser.PrintFlagDescriptions();
+}
+
+static StaticSpinMutex InitMutex;
+static atomic_uint8_t Initialized;
+
+enum {
+  DASAN_RT_VERSION_UNDEFINED = 0,
+  DASAN_RT_VERSION_DYNAMIC,
+  DASAN_RT_VERSION_STATIC,
+};
+
+// One symbol for every copy of the interceptors.
+extern "C" {
+SANITIZER_INTERFACE_ATTRIBUTE int __dasan_rt_version;
+}
+
+// This copy lives in libclang_rt.dasan.so. The static copy lives in the
+// executable, so its address does not.
+static bool ThisCopyIsDynamic() {
+  Dl_info Info = {};
+  if (!dladdr(reinterpret_cast<void*>(&ThisCopyIsDynamic), &Info) ||
+      !Info.dli_fname)
+    return false;
+  return internal_strstr(Info.dli_fname, "libclang_rt.dasan");
+}
+
+static void ReportIncompatibleRuntime() {
+  Report(
+      "ERROR: %s: incompatible runtimes. Link with -shared-libsan so the "
+      "executable uses the same runtime as the sanitized library.\n",
+      SanitizerToolName);
+  Die();
+}
+
+// DASan rejects a static runtime in the executable once the shared runtime is
+// loaded. The interceptor would bind to the other copy and call itself.
+static void CheckIncompatibleRuntime() {
+  if (ThisCopyIsDynamic()) {
+    if (__dasan_rt_version == DASAN_RT_VERSION_UNDEFINED)
+      __dasan_rt_version = DASAN_RT_VERSION_DYNAMIC;
+    else if (__dasan_rt_version != DASAN_RT_VERSION_DYNAMIC)
+      ReportIncompatibleRuntime();
+    return;
+  }
+
+  if (__dasan_rt_version == DASAN_RT_VERSION_UNDEFINED) {
+    MemoryMappingLayout Layout(/*cache_enabled=*/true);
+    char Name[kMaxPathLength];
+    MemoryMappedSegment Segment(Name, sizeof(Name));
+    while (Layout.Next(&Segment)) {
+      if (internal_strstr(Name, "libclang_rt.dasan"))
+        ReportIncompatibleRuntime();
+    }
+    __dasan_rt_version = DASAN_RT_VERSION_STATIC;
+    return;
+  }
+  if (__dasan_rt_version != DASAN_RT_VERSION_STATIC)
+    ReportIncompatibleRuntime();
+}
+
+void Initialize() {
+  if (LIKELY(atomic_load(&Initialized, memory_order_acquire)))
+    return;
+
+  SpinMutexLock L(&InitMutex);
+  if (atomic_load(&Initialized, memory_order_relaxed))
+    return;
+
+  SanitizerToolName = "DeviceAddressSanitizer";
+  InitializeFlags();
+  CheckIncompatibleRuntime();
+
+  Offload::Get().RegisterHandler(HandleDeviceReport);
+  Atexit([] { Offload::Get().UntrackImages(); });
+  AddDieCallback([] { Offload::Get().UntrackImages(); });
+
+  VReport(1, "%s: initialized\n", SanitizerToolName);
+  atomic_store(&Initialized, 1, memory_order_release);
+}
+
+}  // namespace __dasan
+
+namespace {
+
+// pthread_getattr_np reads /proc. The bounds last for the life of the thread.
+struct StackBounds {
+  uptr Top;
+  uptr Bottom;
+  bool Ready;
+};
+THREADLOCAL StackBounds CachedBounds;
+}  // namespace
+
+void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
+                                                 void* context,
+                                                 bool request_fast,
+                                                 u32 max_depth) {
+  if (!CachedBounds.Ready) {
+    GetThreadStackTopAndBottom(false, &CachedBounds.Top, &CachedBounds.Bottom);
+    CachedBounds.Ready = true;
+  }
+  bool Fast = StackTrace::WillUseFastUnwind(request_fast);
+  Unwind(max_depth, pc, bp, context, CachedBounds.Top, CachedBounds.Bottom,
+         Fast);
+}
+
+SANITIZER_INTERFACE_WEAK_DEF(const char*, __dasan_default_options, void) {
+  return "";
+}
+
+extern "C" void __dasan_init() { __dasan::Initialize(); }
diff --git a/compiler-rt/lib/dasan/dasan.h b/compiler-rt/lib/dasan/dasan.h
new file mode 100644
index 00000000000000..372e7279ae1644
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Private interface of the device address sanitizer's host runtime.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_H
+#define DASAN_H
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_mutex.h"
+
+namespace __dasan {
+
+using namespace __sanitizer;
+
+void Initialize();
+
+// Platform, allocator, and images. Taken inside the RPC server's lock to print
+// a report, so never hold it across Offload::TrackExecutable.
+extern Mutex DasanMutex;
+
+}  // namespace __dasan
+
+extern "C" {
+
+SANITIZER_INTERFACE_ATTRIBUTE void __dasan_init();
+
+SANITIZER_INTERFACE_ATTRIBUTE const char* __dasan_default_options();
+
+}  // extern "C"
+
+#endif  // DASAN_H
diff --git a/compiler-rt/lib/dasan/dasan.syms.extra b/compiler-rt/lib/dasan/dasan.syms.extra
new file mode 100644
index 00000000000000..ae70e3e0d90430
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan.syms.extra
@@ -0,0 +1 @@
+__dasan_*
diff --git a/compiler-rt/lib/dasan/dasan_allocator.cpp b/compiler-rt/lib/dasan/dasan_allocator.cpp
new file mode 100644
index 00000000000000..881ef2e9d6a1ba
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_allocator.cpp
@@ -0,0 +1,792 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Device address sanitizer allocator.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan_allocator.h"
+
+#include "dasan_flags.h"
+#include "dasan_report.h"
+#include "sanitizer_common/sanitizer_common.h"
+
+using namespace __sanitizer;
+
+namespace __dasan {
+
+static const uptr kUserMapSize = 1 << 21;
+
+static_assert(kMaxRedzone <= kOffsetMax, "a redzone can outgrow its entry");
+
+uptr Allocator::UserMapChunks(uptr ClassId) const {
+  uptr Bytes = Granule > kUserMapSize ? Granule : kUserMapSize;
+  return Bytes / ClassIdToSize(ClassId);
+}
+
+uptr Allocator::AlignOf(uptr ClassId, uptr Alignment) const {
+  const uptr Chunk = ClassIdToSize(ClassId);
+  if (!Alignment || Alignment < Chunk)
+    Alignment = Chunk;
+  if (!IsPowerOfTwo(Alignment))
+    Alignment = RoundUpToPowerOfTwo(Alignment);
+  if (Alignment < Chunk)
+    Alignment = Chunk;
+  return Alignment;
+}
+
+void Allocator::SkipToFreshGranule(uptr ClassId, hsa_amd_memory_pool_t Pool) {
+  Region& R = Regions[ClassId];
+  const uptr Stride = MetaGranule / kMetadataSize;
+
+  if (R.NextChunk != 0 && R.MetadataPoolSet && R.MetadataPool != Pool) {
+    uptr Boundary = RoundUpTo(R.NextChunk, Stride);
+    // Boundary's granule is already homed on the other device.
+    if (R.MetadataBytes >= MetadataBytesFor(Boundary + 1))
+      Boundary += Stride;
+    R.NextChunk = Boundary;
+  }
+  R.MetadataPool = Pool;
+  R.MetadataPoolSet = true;
+  // The skipped index is a hole: nothing hands it out.
+  if (R.NextChunk <= R.Chunks.size())
+    return;
+
+  const uptr Mark = R.Chunks.size();
+  R.Chunks.resize(R.NextChunk);
+  for (uptr I = Mark; I < R.NextChunk; ++I) R.Chunks[I].Gap = true;
+}
+
+void Allocator::MarkGap(uptr ClassId, uptr Begin, uptr End) {
+  if (Begin >= End)
+    return;
+
+  Region& R = Regions[ClassId];
+  if (R.Chunks.size() < End)
+    R.Chunks.resize(End);
+  for (uptr I = Begin; I < End; ++I) R.Chunks[I].Gap = true;
+
+  if (R.NextChunk < End)
+    R.NextChunk = End;
+}
+
+uptr Allocator::FreeListFor(uptr ClassId, hsa_amd_memory_pool_t Pool) {
+  Region& R = Regions[ClassId];
+  for (uptr I = 0; I < R.FreeLists.size(); ++I)
+    if (R.FreeLists[I].Pool == Pool)
+      return I;
+  uptr I = R.FreeLists.size();
+  R.FreeLists.resize(I + 1);
+  R.FreeLists[I].Pool = Pool;
+  R.FreeLists[I].Head = 0;
+  R.FreeLists[I].QuarantinedBytes = 0;
+  return I;
+}
+
+void Allocator::PushFree(uptr ClassId, uptr List, Run R) {
+  if (!R.Count)
+    return;
+  auto& Free = Regions[ClassId].FreeLists[List].Free;
+  for (uptr I = 0; I < Free.size();) {
+    Run& F = Free[I];
+    const bool Abut =
+        F.Begin + F.Count == R.Begin || R.Begin + R.Count == F.Begin;
+    if (!Abut || F.UserMapped != R.UserMapped) {
+      ++I;
+      continue;
+    }
+    if (F.Begin < R.Begin)
+      R.Begin = F.Begin;
+    R.Count += F.Count;
+    F = Free.back();
+    Free.pop_back();
+    I = 0;
+  }
+  Free.push_back(R);
+}
+
+bool Allocator::TakeFree(uptr ClassId, uptr List, uptr Need, uptr Stride,
+                         bool MapUser, uptr* Begin) {
+  auto& Free = Regions[ClassId].FreeLists[List].Free;
+  for (uptr N = Free.size(); N-- > 0;) {
+    const Run R = Free[N];
+    if (!MapUser && R.UserMapped)
+      continue;
+    const uptr Aligned = RoundUpTo(R.Begin, Stride);
+    if (Aligned < R.Begin || Aligned - R.Begin > R.Count)
+      continue;
+    if (Need > R.Count - (Aligned - R.Begin))
+      continue;
+    Free[N] = Free.back();
+    Free.pop_back();
+    if (Aligned > R.Begin)
+      PushFree(ClassId, List, {R.Begin, Aligned - R.Begin, R.UserMapped});
+    const uptr End = R.Begin + R.Count;
+    if (Aligned + Need < End)
+      PushFree(ClassId, List,
+               {Aligned + Need, End - (Aligned + Need), R.UserMapped});
+    *Begin = Aligned;
+    return true;
+  }
+  return false;
+}
+
+bool Allocator::CommitMap(uptr Addr, uptr Bytes, u64 Handle,
+                          hsa_amd_memory_pool_t Home) {
+  if (!VaMap(Addr, Bytes, Handle, Home)) {
+    VaDestroy(Handle);
+    return false;
+  }
+  Maps.push_back({Addr, Bytes});
+  VmemHandles.push_back(Handle);
+  return true;
+}
+
+void Allocator::RollbackMap() {
+  const Mapping M = Maps.back();
+  Maps.pop_back();
+  VaUnmap(M.Addr, M.Size);
+  VaDestroy(VmemHandles.back());
+  VmemHandles.pop_back();
+}
+
+bool Allocator::CreateMetadata(hsa_amd_memory_pool_t Prefer, uptr Bytes,
+                               u64* Handle, hsa_amd_memory_pool_t* Home) {
+  if (HasPool(Prefer) && VaCreate(Prefer, Bytes, 0, Handle)) {
+    *Home = Prefer;
+    return true;
+  }
+  InternalMmapVector<hsa_amd_memory_pool_t> Pools;
+  PlaceablePools(&Pools);
+  for (hsa_amd_memory_pool_t Other : Pools) {
+    if (!HasPool(Other) || Other == Prefer)
+      continue;
+    if (VaCreate(Other, Bytes, 0, Handle)) {
+      *Home = Other;
+      return true;
+    }
+  }
+
+  if (VaCreate({}, Bytes, 0, Handle)) {
+    *Home = {};
+    return true;
+  }
+  return false;
+}
+
+bool Allocator::EnsureMetadata(hsa_amd_memory_pool_t Pool, uptr ClassId,
+                               uptr Chunks) {
+  Region& R = Regions[ClassId];
+  // Cover these chunks only. SkipToFreshGranule keeps another device's
+  // indices out of a granule this one already maps.
+  uptr Want = MetadataBytesFor(Chunks);
+  const uptr Max = MetadataBytesFor(ReachableChunks(ClassId));
+  if (Want > Max)
+    Want = Max;
+  if (R.MetadataBytes >= Want) {
+    return !HasPool(Pool) || VaAllow(MetadataBegin(ClassId, R.MetadataBytes),
+                                     R.MetadataBytes, Pool);
+  }
+
+  const uptr Beg = MetadataBegin(ClassId, Want);
+  const uptr Bytes = Want - R.MetadataBytes;
+  u64 H = 0;
+  hsa_amd_memory_pool_t Home = Pool;
+  if (!CreateMetadata(Pool, Bytes, &H, &Home))
+    return false;
+  if (!CommitMap(Beg, Bytes, H, Home))
+    return false;
+  if (HasPool(Pool) && Pool != Home && !VaAllow(Beg, Bytes, Pool)) {
+    RollbackMap();
+    return false;
+  }
+
+  if (!MetadataOk && !VaProbe(Beg)) {
+    Report("ERROR: %s: could not initialize metadata\n", SanitizerToolName);
+    Die();
+  }
+
+  VaPublish(Beg, nullptr, Bytes);
+  MetadataOk = true;
+  R.MetadataBytes = Want;
+  VReport(2, "%s: class %zu holds %zu KiB of entries%s\n", SanitizerToolName,
+          ClassId, Want >> 10,
+          Home != Pool ? " (spilled to another pool)" : "");
+  return true;
+}
+
+bool Allocator::MapUserChunk(hsa_amd_memory_pool_t Pool, uptr ClassId,
+                             uptr ChunkIdx, uptr Need, u32 AllocFlags) {
+  ChunkState& S = Regions[ClassId].Chunks[ChunkIdx];
+  const uptr Chunk = ClassIdToSize(ClassId);
+  uptr Want = RoundUpTo(Need, Granule);
+  if (Want > Chunk)
+    Want = Chunk;
+  if (S.Mapped >= Want)
+    return true;
+
+  const uptr Beg = ChunkBegin(ClassId, ChunkIdx) + S.Mapped;
+  const uptr Bytes = Want - S.Mapped;
+  u64 H = 0;
+  if (!VaCreate(Pool, Bytes, AllocFlags, &H))
+    return false;
+  if (!CommitMap(Beg, Bytes, H, Pool))
+    return false;
+  S.Mapped = Want;
+  return true;
+}
+
+bool Allocator::MapPrefix(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr Begin,
+                          uptr Count, uptr Bytes, u32 AllocFlags) {
+  const uptr Chunk = ClassIdToSize(ClassId);
+
+  // A small class was mapped as a batch. A short prefix here would be a
+  // granule the device will not take.
+  if (MapOnPopulate(ClassId)) {
+    for (uptr I = 0; I < Count && Bytes; ++I) {
+      const uptr Need = Bytes > Chunk ? Chunk : Bytes;
+      if (Regions[ClassId].Chunks[Begin + I].Mapped < Need)
+        return false;
+      Bytes -= Need;
+    }
+    return true;
+  }
+  for (uptr I = 0; I < Count && Bytes; ++I) {
+    const uptr Need = Bytes > Chunk ? Chunk : Bytes;
+    if (!MapUserChunk(Pool, ClassId, Begin + I, Need, AllocFlags))
+      return false;
+    Bytes -= Need;
+  }
+  return true;
+}
+
+bool Allocator::Grow(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr List,
+                     uptr Need, uptr Stride, bool MapUser, u32 AllocFlags,
+                     uptr* Begin) {
+  Region& R = Regions[ClassId];
+  SkipToFreshGranule(ClassId, Pool);
+
+  const uptr Max = MaxUsableChunks(ClassId);
+  const uptr Chunk = ClassIdToSize(ClassId);
+  const bool Batch = MapUser && MapOnPopulate(ClassId);
+  if (Batch) {
+    const uptr Per = Granule / Chunk;
+    const uptr MapAt = RoundUpTo(R.NextChunk, Per);
+    // The next granule would land in the metadata.
+    if (MapAt > Max)
+      return false;
+    if (MapAt > R.NextChunk)
+      MarkGap(ClassId, R.NextChunk, MapAt);
+  }
+
+  if (R.NextChunk > Max)
+    return false;
+  const uptr First = RoundUpTo(R.NextChunk, Stride);
+  if (First < R.NextChunk || First >= Max || Need > Max - First)
+    return false;
+
+  uptr BatchCount = Need;
+  uptr MapFrom = First;
+  if (Batch) {
+    const uptr Per = Granule / Chunk;
+    const uptr Span = First + Need - R.NextChunk;
+    BatchCount = UserMapChunks(ClassId);
+    if (BatchCount < Span)
+      BatchCount = RoundUpTo(Span, Per);
+    if (BatchCount > Max - R.NextChunk)
+      BatchCount = (Max - R.NextChunk) / Per * Per;
+    if (!BatchCount || BatchCount < Span)
+      return false;
+    MapFrom = R.NextChunk;
+  }
+
+  const uptr End = MapFrom + BatchCount;
+  if (!EnsureMetadata(Pool, ClassId, End))
+    return false;
+
+  // The caller maps this run. One reservation names it.
+  if (!MapUser) {
+    if (First > R.NextChunk)
+      PushFree(ClassId, List, {R.NextChunk, First - R.NextChunk, false});
+    R.NextChunk = End;
+    *Begin = First;
+    return true;
+  }
+
+  if (Batch) {
+    u64 H = 0;
+    if (!VaCreate(Pool, BatchCount * Chunk, AllocFlags, &H))
+      return false;
+    if (!CommitMap(ChunkBegin(ClassId, MapFrom), BatchCount * Chunk, H, Pool))
+      return false;
+  }
+
+  if (R.Chunks.size() < End)
+    R.Chunks.resize(End);
+  for (uptr I = R.NextChunk; I < End; ++I) {
+    ChunkState& S = R.Chunks[I];
+    S.List = static_cast<u32>(List);
+    // The batch map starts at NextChunk. A large class maps only the
+    // prefix the caller asked for, after Grow returns.
+    if (Batch && I >= MapFrom)
+      S.Mapped = Chunk;
+  }
+
+  // Alignment padding. Mapped when it sits inside the batch, otherwise a
+  // bare reservation a later run can claim.
+  if (First > R.NextChunk)
+    PushFree(ClassId, List, {R.NextChunk, First - R.NextChunk, Batch});
+  if (First + Need < End)
+    PushFree(ClassId, List, {First + Need, End - (First + Need), Batch});
+  R.NextChunk = End;
+  *Begin = First;
+  return true;
+}
+
+// Same function as globalRedzoneSize in the DeviceAddressSanitizer pass.
+static uptr GlobalRedzoneBytes(uptr Size) {
+  const uptr Chunk = ClassIdToSize(0);
+  const uptr Max = 1ull << 18;
+  if (Size <= Chunk / 2)
+    return Chunk - Size;
+  uptr RZ = (Size / Chunk / 4) * Chunk;
+  if (RZ < Chunk)
+    RZ = Chunk;
+  if (RZ > Max)
+    RZ = Max;
+  if (Size % Chunk)
+    RZ += Chunk - (Size % Chunk);
+  return RZ;
+}
+
+static uptr CoveredBytes(uptr Size) {
+  return RoundUpTo(Size + GlobalRedzoneBytes(Size), ClassIdToSize(0));
+}
+
+bool Allocator::Publish(uptr Addr, uptr Size, bool Pad) {
+  return WriteMetadata(Addr, Size, Pad, false);
+}
+
+bool Allocator::WriteMetadata(uptr Addr, uptr Size, bool Pad, bool Poison) {
+  if (!Size || !MetadataOk || Size > kSizeMask || !PointerIsMine(Addr))
+    return false;
+  const uptr ClassId = GetSizeClass(Addr);
+  const uptr Chunk = ClassIdToSize(ClassId);
+  uptr Base = 0;
+  uptr Off = 0;
+  uptr Count = 0;
+
+  // A heap chunk stores its redzone offset. Any other address is the base of
+  // a range the caller published.
+  const uptr Idx = GetChunkIdx(Addr, ClassId);
+  Region& R = Regions[ClassId];
+  if (Idx < R.Chunks.size() && !R.Chunks[Idx].Gap && R.Chunks[Idx].Heap &&
+      R.Chunks[Idx].RunLen) {
+    const ChunkState& S = R.Chunks[Idx];
+    Base = ChunkBegin(ClassId, S.RunStart);
+    Off = S.UserOffset;
+    if (Addr != Base + Off)
+      return false;
+    Count = S.RunLen;
+  } else {
+    if (Addr % Chunk)
+      return false;
+    Base = Addr;
+    const uptr Bytes = Pad ? CoveredBytes(Size) : RoundUpTo(Size, Chunk);
+    Count = Bytes / Chunk;
+  }
+  if (!Count || Base % Chunk || !PointerIsMine(Base) ||
+      GetSizeClass(Base) != ClassId)
+    return false;
+  const sptr Last =
+      static_cast<sptr>(Off) - static_cast<sptr>((Count - 1) * Chunk);
+  if (!OffsetFits(static_cast<sptr>(Off)) || !OffsetFits(Last))
+    return false;
+
+  auto Entry = [&](uptr I) {
+    const sptr Delta = static_cast<sptr>(Off) - static_cast<sptr>(I * Chunk);
+    u64 E = MakeEntry(Poison ? 0 : Size, Delta);
+    return Poison ? PoisonEntry(E) : E;
+  };
+  const uptr First = GetChunkIdx(Base, ClassId);
+  if (Count == 1) {
+    u64 E = Entry(0);
+    VaPublish(GetMetadata(RegionBeg(ClassId), First), &E, sizeof(E));
+    return true;
+  }
+  // The tail grows downward, so the last chunk is the lowest address.
+  InternalMmapVector<u64> Ordered(Count);
+  for (uptr I = 0; I < Count; ++I) Ordered[Count - 1 - I] = Entry(I);
+  VaPublish(GetMetadata(RegionBeg(ClassId), First + Count - 1), Ordered.data(),
+            Count * sizeof(u64));
+  return true;
+}
+
+void Allocator::MarkChunk(uptr ClassId, uptr Begin, uptr List,
+                          const Placement& Place) {
+  ChunkState& S = Regions[ClassId].Chunks[Begin];
+  S.List = static_cast<u32>(List);
+  S.Live = true;
+  S.Gap = false;
+  S.Heap = true;
+  S.RunStart = Begin;
+  S.RunLen = 1;
+  S.UserOffset = Place.Offset;
+  S.UserSize = Place.UserSize;
+  S.AllocStack = Place.Stack;
+  S.FreeStack = 0;
+  S.AllocTid = Place.Tid;
+  S.FreeTid = 0;
+}
+
+void Allocator::ClearChunk(uptr ClassId, uptr Begin) {
+  ChunkState& S = Regions[ClassId].Chunks[Begin];
+  S.Live = false;
+  S.Heap = false;
+  S.UserSize = 0;
+  S.UserOffset = 0;
+  S.RunStart = 0;
+  S.RunLen = 0;
+  S.AllocStack = S.FreeStack = 0;
+  S.AllocTid = S.FreeTid = 0;
+}
+
+bool Allocator::Recycle(uptr ClassId, uptr List) {
+  FreeList& F = Regions[ClassId].FreeLists[List];
+  if (F.Head >= F.Quarantine.size())
+    return false;
+  const Run R = F.Quarantine[F.Head++];
+  const uptr Bytes = R.Count * ClassIdToSize(ClassId);
+  F.QuarantinedBytes -= Bytes;
+  QuarantinedBytes -= Bytes;
+  if (F.Head == F.Quarantine.size()) {
+    F.Quarantine.clear();
+    F.Head = 0;
+  }
+  // Leave the chunk's record in place until a new run overwrites it, so a
+  // use-after-free still names the allocation that owned these bytes.
+  PushFree(ClassId, List, R);
+  return true;
+}
+
+bool Allocator::RecycleOne(uptr PreferClass, uptr PreferList) {
+  if (Recycle(PreferClass, PreferList))
+    return true;
+  for (uptr C = 0; C < kNumSizeClasses; ++C) {
+    Region& R = Regions[C];
+    for (uptr L = 0; L < R.FreeLists.size(); ++L) {
+      if (C == PreferClass && L == PreferList)
+        continue;
+      if (Recycle(C, L))
+        return true;
+    }
+  }
+  return false;
+}
+
+void Allocator::Quarantine(uptr ClassId, uptr List, Run R, u32 Stack, u32 Tid) {
+  Region& Reg = Regions[ClassId];
+  const uptr Chunk = ClassIdToSize(ClassId);
+  ChunkState& S = Reg.Chunks[R.Begin];
+  const uptr UserOff = S.UserOffset;
+  const uptr UserSize = S.UserSize;
+  S.Live = false;
+  S.FreeStack = Stack;
+  S.FreeTid = Tid;
+  WriteMetadata(ChunkBegin(ClassId, R.Begin) + UserOff, UserSize, false, true);
+
+  FreeList& F = Reg.FreeLists[List];
+  const uptr Bytes = R.Count * Chunk;
+  F.Quarantine.push_back(R);
+  F.QuarantinedBytes += Bytes;
+  QuarantinedBytes += Bytes;
+  const uptr Budget = (uptr)flags()->quarantine_size_mb << 20;
+  while (QuarantinedBytes > Budget && RecycleOne(ClassId, List)) {
+  }
+}
+
+bool Allocator::Init() {
+  if (Initialized)
+    return Reserved;
+  Initialized = true;
+
+  Granule = VaGranule();
+  MetaGranule = VaMetaGranule();
+  // Both are non-zero powers of two. The device granule covers a host page,
+  // so every smaller class divides it. The metadata granule tiles an entry
+  // and the metadata region.
+  const uptr Page = GetPageSizeCached();
+  if (!Granule || !MetaGranule || !IsPowerOfTwo(Granule) ||
+      !IsPowerOfTwo(MetaGranule) || Granule < Page ||
+      MetaGranule % kMetadataSize || kMetadataBytes % MetaGranule) {
+    VReport(1, "%s: no usable metadata granule; HSA calls pass through\n",
+            SanitizerToolName);
+    return false;
+  }
+
+  if (!VaReserve(kSpaceBeg, kSpaceUsed)) {
+    VReport(1, "%s: could not reserve [0x%zx, 0x%zx); HSA calls pass through\n",
+            SanitizerToolName, (uptr)kSpaceBeg, (uptr)(kSpaceBeg + kSpaceUsed));
+    return false;
+  }
+  Reserved = true;
+  VReport(1, "%s: reserved [0x%zx, 0x%zx)\n", SanitizerToolName,
+          (uptr)kSpaceBeg, (uptr)(kSpaceBeg + kSpaceUsed));
+  return true;
+}
+
+void Allocator::Shutdown() {
+  if (Reserved) {
+    for (uptr I = 0; I < Maps.size(); ++I) VaUnmap(Maps[I].Addr, Maps[I].Size);
+    Maps.clear();
+    for (uptr I = 0; I < VmemHandles.size(); ++I) VaDestroy(VmemHandles[I]);
+    VmemHandles.clear();
+
+    for (uptr C = 0; C < kNumSizeClasses; ++C) {
+      Region& R = Regions[C];
+      for (uptr I = 0; I < R.FreeLists.size(); ++I) {
+        R.FreeLists[I].Free.clear();
+        R.FreeLists[I].Quarantine.clear();
+      }
+      R.FreeLists.clear();
+      R.Chunks.clear();
+      R.Reservations.clear();
+      R.NextChunk = R.MetadataBytes = 0;
+      R.MetadataPoolSet = false;
+    }
+
+    VaRelease(kSpaceBeg, kSpaceUsed);
+    Reserved = false;
+    MetadataOk = false;
+    Granule = MetaGranule = 0;
+    QuarantinedBytes = 0;
+    Count = {};
+  }
+  Initialized = false;
+}
+
+uptr Allocator::Allocate(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr Size,
+                         u32 AllocFlags, u32 Stack, u32 Tid, bool MapUser) {
+  if (!Size || ClassId >= kNumSizeClasses)
+    return 0;
+  if (Size > ~uptr{0} - kMinRedzone)
+    return 0;
+
+  // Search for a chunk we can use in the allocator.
+  const uptr Chunk = ClassIdToSize(ClassId);
+  uptr Off = 0;
+  uptr Bytes = Size;
+  uptr RunAlign = Granule;
+  if (MapUser) {
+    if (Size > kSizeMask)
+      return 0;
+    const uptr Align = HeapAlign(Size);
+    // This class holds the object in one chunk, redzone included.
+    if (Chunk < Align)
+      return 0;
+    Off = LeadingRedzone(Size, Chunk, Align);
+    if (Off + Size + kMinRedzone > Chunk)
+      return 0;
+    Bytes = Off + Size;
+    RunAlign = Chunk;
+  }
+
+  const uptr AlignTo = AlignOf(ClassId, RunAlign);
+  const uptr Stride = AlignTo / Chunk;
+  const uptr MaxChunks = MaxUsableChunks(ClassId);
+  if (!MaxChunks || Bytes > MaxChunks * Chunk)
+    return 0;
+
+  const uptr Need = RoundUpTo(Bytes, Chunk) / Chunk;
+  if (!Need)
+    return 0;
+
+  // Find a free chunk in the current free list.
+  const uptr List = FreeListFor(ClassId, Pool);
+  uptr Begin = 0;
+  if (!TakeFree(ClassId, List, Need, Stride, MapUser, &Begin) &&
+      !Grow(Pool, ClassId, List, Need, Stride, MapUser, AllocFlags, &Begin)) {
+    bool Got = false;
+    while (Recycle(ClassId, List)) {
+      if (TakeFree(ClassId, List, Need, Stride, MapUser, &Begin)) {
+        Got = true;
+        break;
+      }
+    }
+    if (!Got) {
+      VReport(2, "%s: no room in class %zu for %zu bytes\n", SanitizerToolName,
+              ClassId, Size);
+      return 0;
+    }
+  }
+
+  if (MapUser && !MapPrefix(Pool, ClassId, Begin, Need, Bytes, AllocFlags)) {
+    VReport(2, "%s: no backing for class %zu at chunk %zu\n", SanitizerToolName,
+            ClassId, Begin);
+    const bool Mapped = Regions[ClassId].Chunks[Begin].Mapped != 0;
+    PushFree(ClassId, List, {Begin, Need, Mapped});
+    return 0;
+  }
+
+  if (!MapUser) {
+    Regions[ClassId].Reservations.push_back({Begin, Need, List, Size});
+  } else {
+    const Placement Place{Off, Size, Stack, Tid};
+    MarkChunk(ClassId, Begin, List, Place);
+    if (!Publish(ChunkBegin(ClassId, Begin) + Off, Size)) {
+      ClearChunk(ClassId, Begin);
+      const bool Mapped = Regions[ClassId].Chunks[Begin].Mapped != 0;
+      PushFree(ClassId, List, {Begin, Need, Mapped});
+      return 0;
+    }
+  }
+
+  ++Count.Placed;
+  if (!MapUser) {
+    ++Count.CodeObjects;
+    Count.CodeObjectChunks += Need;
+  }
+
+  const uptr Base = ChunkBegin(ClassId, Begin);
+  const uptr At = Base + Off;
+  VReport(3, "%s: placed %zu bytes at 0x%zx (class %zu, %zu chunks)\n",
+          SanitizerToolName, Size, At, ClassId, Need);
+  return At;
+}
+
+bool Allocator::Deallocate(uptr Addr, u32 Stack, u32 Tid) {
+  if (!IsPlaced(Addr))
+    return false;
+
+  const uptr ClassId = GetSizeClass(Addr);
+  const uptr Idx = GetChunkIdx(Addr, ClassId);
+  Region& R = Regions[ClassId];
+  if (Idx >= R.NextChunk)
+    return false;
+
+  ChunkState* S = Idx < R.Chunks.size() ? &R.Chunks[Idx] : nullptr;
+  const uptr Base = S && S->RunLen ? ChunkBegin(ClassId, S->RunStart) : 0;
+  if (!S || S->Gap || !S->Heap || !S->Live || !S->RunLen ||
+      Addr != Base + S->UserOffset) {
+    ReportInvalidFree(Addr, Stack, Tid);
+    return true;
+  }
+
+  Quarantine(ClassId, S->List, {S->RunStart, S->RunLen, S->Mapped != 0}, Stack,
+             Tid);
+  ++Count.Freed;
+  return true;
+}
+
+void Allocator::Release(uptr Addr) {
+  if (!Reserved || !PointerIsMine(Addr))
+    return;
+
+  const uptr ClassId = GetSizeClass(Addr);
+  const uptr Idx = GetChunkIdx(Addr, ClassId);
+  Region& R = Regions[ClassId];
+  if (Idx >= R.NextChunk)
+    return;
+
+  for (uptr I = 0; I < R.Reservations.size(); ++I) {
+    Reservation& M = R.Reservations[I];
+    if (M.Begin != Idx)
+      continue;
+
+    const uptr Len = M.Count;
+    const uptr List = M.List;
+    if (I + 1 != R.Reservations.size())
+      R.Reservations[I] = R.Reservations.back();
+    R.Reservations.pop_back();
+    PushFree(ClassId, List, {Idx, Len, false});
+    if (Count.CodeObjectChunks >= Len)
+      Count.CodeObjectChunks -= Len;
+    if (Count.CodeObjects)
+      --Count.CodeObjects;
+    return;
+  }
+}
+
+bool Allocator::getAllocInfo(uptr Addr, AllocInfo* Out) const {
+  if (!IsPlaced(Addr))
+    return false;
+
+  const uptr ClassId = GetSizeClass(Addr);
+  const uptr Idx = GetChunkIdx(Addr, ClassId);
+  const Region& R = Regions[ClassId];
+  if (Idx >= R.NextChunk)
+    return false;
+
+  if (Idx < R.Chunks.size()) {
+    const ChunkState& S = R.Chunks[Idx];
+    if (!S.Gap && S.RunLen) {
+      Out->Beg = ChunkBegin(ClassId, S.RunStart) + S.UserOffset;
+      Out->Size = S.UserSize;
+      Out->Pool = R.FreeLists[S.List].Pool;
+      Out->Freed = !S.Live;
+      Out->AllocStack = S.AllocStack;
+      Out->FreeStack = S.FreeStack;
+      Out->AllocTid = S.AllocTid;
+      Out->FreeTid = S.FreeTid;
+      return S.UserSize != 0;
+    }
+  }
+
+  for (const Reservation& M : R.Reservations) {
+    if (M.Begin != Idx || !M.UserSize)
+      continue;
+    Out->Beg = ChunkBegin(ClassId, Idx);
+    Out->Size = M.UserSize;
+    Out->Pool = R.FreeLists[M.List].Pool;
+    Out->Freed = false;
+    Out->AllocStack = Out->FreeStack = 0;
+    Out->AllocTid = Out->FreeTid = 0;
+    return true;
+  }
+  return false;
+}
+
+bool Allocator::AllowAccess(uptr Addr, const hsa_agent_t* Agents, u32 N) {
+  if (!IsPlaced(Addr))
+    return false;
+
+  const uptr ClassId = GetSizeClass(Addr);
+  const uptr Idx = GetChunkIdx(Addr, ClassId);
+  const Region& R = Regions[ClassId];
+  uptr UserBeg = Addr;
+  uptr UserSize = 1;
+  uptr Meta = GetMetadata(Addr, Idx);
+  uptr MetaBytes = kMetadataSize;
+  if (Idx < R.NextChunk && Idx < R.Chunks.size() && !R.Chunks[Idx].Gap &&
+      R.Chunks[Idx].Heap && R.Chunks[Idx].RunLen && R.Chunks[Idx].UserSize) {
+    const ChunkState& S = R.Chunks[Idx];
+    UserBeg = ChunkBegin(ClassId, S.RunStart);
+    UserSize = S.RunLen * ClassIdToSize(ClassId);
+    Meta = GetMetadata(RegionBeg(ClassId), S.RunStart + S.RunLen - 1);
+    MetaBytes = S.RunLen * kMetadataSize;
+  } else if (Idx < R.NextChunk) {
+    for (const Reservation& M : R.Reservations) {
+      if (Idx < M.Begin || Idx - M.Begin >= M.Count)
+        continue;
+      UserBeg = ChunkBegin(ClassId, M.Begin);
+      UserSize = M.Count * ClassIdToSize(ClassId);
+      Meta = GetMetadata(RegionBeg(ClassId), M.Begin + M.Count - 1);
+      MetaBytes = M.Count * kMetadataSize;
+      break;
+    }
+  }
+  if (!VaAllowAgents(UserBeg, UserSize, Agents, N))
+    return false;
+  return VaAllowAgents(Meta, MetaBytes, Agents, N);
+}
+
+static Allocator Instance;
+Allocator& GetAllocator() { return Instance; }
+
+}  // namespace __dasan
diff --git a/compiler-rt/lib/dasan/dasan_allocator.h b/compiler-rt/lib/dasan/dasan_allocator.h
new file mode 100644
index 00000000000000..483170285829d9
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_allocator.h
@@ -0,0 +1,273 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// One region per size class. Class n holds chunks of 2^(6+n) bytes.
+/// Chunks grow up from the base; one 8-byte entry per chunk grows down
+/// from the top. Distinct from the existing size-class allocator.
+///
+///   ┌───────────────────────────────────────────────┬──────────────────┐
+///   │ c0    c1    c2    ...                         │     ... e2 e1 e0 │
+///   └───────────────────────────────────────────────┴──────────────────┘
+///                                          chunks →   ← entries
+///
+/// An allocation is padded with redzones to prevent overruns into adjacent
+/// objects.
+///
+///         left                                               right
+///   ┌───────────────┐   ┌──────────────────────────┐   ┌───────────────┐
+///   │    redzone    │...│          memory          │...│    redzone    │
+///   └───────────────┘   └──────────────────────────┘   └───────────────┘
+///
+/// The allocation is tracked by a metadata entry calculated from the allocation
+/// address itself. The signed offset is used to skip the leading redzone and
+/// can also be made negative to span multiple chunks.
+///
+///   ┌──────────────────────────┬─┬─────────────────────────────────────┐
+///   │ signed offset         26 │p│ size                             37 │
+///   └──────────────────────────┴─┴─────────────────────────────────────┘
+///    63                     38 37 36                                  0
+///
+/// Freed ranges are quarantined and poisoned to enable detection of
+/// use-after-free errors.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_ALLOCATOR_H
+#define DASAN_ALLOCATOR_H
+
+#include "dasan_mapping.h"
+#include "dasan_platform.h"
+#include "sanitizer_common/sanitizer_common.h"
+
+namespace __dasan {
+
+inline constexpr uptr kMaxRedzone = 2048;
+
+// The amount of leading redzone to use for a given allocation size.
+inline uptr LeadingRedzone(uptr Size, uptr ChunkSize, uptr Alignment) {
+  if (!Alignment)
+    Alignment = kAllocAlignment;
+  if (Size + kMinRedzone >= ChunkSize)
+    return 0;
+  uptr Off = RoundDownTo(ChunkSize - Size - kMinRedzone, Alignment);
+  return Off > kMaxRedzone ? RoundDownTo(kMaxRedzone, Alignment) : Off;
+}
+
+// Allocations from the HSA runtime have a minimum granule. Ensure we are
+// aligned on that boundary.
+inline uptr HeapAlign(uptr Size) {
+  const uptr Granule = VaGranule();
+  return Size >= Granule ? Granule : kAllocAlignment;
+}
+
+// Smallest class whose one chunk holds Size at Alignment.
+inline uptr ChooseClass(uptr Size, uptr Alignment) {
+  if (!Size || Size > kSizeMask)
+    return kNumSizeClasses;
+  if (!Alignment)
+    Alignment = kAllocAlignment;
+  for (uptr ClassId = 0; ClassId < kNumSizeClasses; ++ClassId) {
+    const uptr Chunk = ClassIdToSize(ClassId);
+    // The chunk base is aligned to the chunk. A smaller chunk cannot hold
+    // an Alignment-aligned pointer.
+    if (Chunk < Alignment)
+      continue;
+    const uptr Off = LeadingRedzone(Size, Chunk, Alignment);
+    if (Off + Size + kMinRedzone <= Chunk)
+      return ClassId;
+  }
+  return kNumSizeClasses;
+}
+
+// Tracking state for a single allocation.
+struct AllocInfo {
+  uptr Beg;
+  uptr Size;
+  hsa_amd_memory_pool_t Pool;
+  bool Freed;
+  u32 AllocStack;
+  u32 FreeStack;
+  u32 AllocTid;
+  u32 FreeTid;
+};
+
+// Segregated fit size class allocator.
+class Allocator {
+ public:
+  bool Init();
+  void Shutdown();
+
+  bool IsPlaced(uptr Addr) const { return Reserved && PointerIsMine(Addr); }
+
+  // `Size` bytes in class `ClassId`. MapUser is false for a run the caller
+  // maps. The pointer to free, or 0 when this class cannot hold it.
+  uptr Allocate(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr Size,
+                u32 AllocFlags = 0, u32 Stack = 0, u32 Tid = 0,
+                bool MapUser = true);
+
+  // Free a pointer Allocate returned for a mapped object.
+  bool Deallocate(uptr Addr, u32 Stack = 0, u32 Tid = 0);
+
+  // Free the base Allocate returned for an unmapped run.
+  void Release(uptr Addr);
+
+  // Write the metadata associated with an existing allocation.
+  bool Publish(uptr Addr, uptr Size, bool Pad = false);
+
+  // Allow agents to access the allocation and metadata given the address.
+  bool AllowAccess(uptr Addr, const hsa_agent_t* Agents, u32 N);
+
+  // Return the information associated with this address for reporting.
+  bool getAllocInfo(uptr Addr, AllocInfo* Out) const;
+
+  struct Counters {
+    uptr Placed;
+    uptr LargerClass;
+    uptr Freed;
+    uptr TooLarge;
+    uptr Exhausted;
+    uptr CodeObjects;
+    uptr CodeObjectChunks;
+  } Count;
+
+ private:
+  struct ChunkState {
+    u64 UserSize;
+    u64 Mapped;
+    uptr UserOffset;
+    uptr RunStart;
+    uptr RunLen;
+    u32 List;
+    u32 AllocStack;
+    u32 FreeStack;
+    u32 AllocTid;
+    u32 FreeTid;
+    bool Live;
+    // Skipped so a device's metadata, or a user mapping, starts on a granule
+    // the previous one does not own.
+    bool Gap;
+    // Mapped, redzone, quarantined. An unmapped run has none of these.
+    bool Heap;
+  };
+
+  // Heap object. Offset is from the base of its chunk.
+  struct Placement {
+    uptr Offset;
+    uptr UserSize;
+    u32 Stack;
+    u32 Tid;
+  };
+
+  // A contiguous run of free chunks. SizeClassAllocator64 keeps one flat
+  // array of independent chunks in the VA. Chunks here are backed by one
+  // pool and an allocation is a run, so the list is per device, in
+  // ordinary memory, and each element is a run.
+  struct Run {
+    uptr Begin;
+    uptr Count;
+    bool UserMapped;
+  };
+
+  struct FreeList {
+    hsa_amd_memory_pool_t Pool;
+    InternalMmapVectorNoCtor<Run> Free;
+    InternalMmapVectorNoCtor<Run> Quarantine;
+    uptr Head;
+    uptr QuarantinedBytes;
+  };
+
+  // One record for a run the caller maps. Begin is its first chunk; Count
+  // is how many chunks the mapping occupies.
+  struct Reservation {
+    uptr Begin;
+    uptr Count;
+    uptr List;
+    uptr UserSize;
+  };
+
+  struct Region {
+    InternalMmapVectorNoCtor<ChunkState> Chunks;
+    InternalMmapVectorNoCtor<Reservation> Reservations;
+    InternalMmapVectorNoCtor<FreeList> FreeLists;
+    uptr NextChunk;
+    uptr MetadataBytes;
+    hsa_amd_memory_pool_t MetadataPool;
+    bool MetadataPoolSet;
+  };
+
+  struct Mapping {
+    uptr Addr;
+    uptr Size;
+  };
+
+  Region Regions[kNumSizeClasses];
+  InternalMmapVectorNoCtor<u64> VmemHandles;
+  InternalMmapVectorNoCtor<Mapping> Maps;
+  uptr Granule;
+  uptr MetaGranule;
+  uptr QuarantinedBytes;
+
+  bool Initialized;
+  bool Reserved;
+  bool MetadataOk;
+
+  uptr ChunkBegin(uptr ClassId, uptr ChunkIdx) const {
+    return RegionBeg(ClassId) + ChunkIdx * ClassIdToSize(ClassId);
+  }
+  uptr MetadataBytesFor(uptr Chunks) const {
+    return RoundUpTo(Chunks * kMetadataSize, MetaGranule);
+  }
+  uptr MetadataBegin(uptr ClassId, uptr Bytes) const {
+    return RegionBeg(ClassId) + kRegionSize - Bytes;
+  }
+  // kUserMapSize in SizeClassAllocator64. The floor is the device granule:
+  // 2 MB on current parts, so a small class is one handle per run.
+  bool MapOnPopulate(uptr ClassId) const {
+    return ClassIdToSize(ClassId) <= Granule;
+  }
+  uptr UserMapChunks(uptr ClassId) const;
+
+  uptr AlignOf(uptr ClassId, uptr Alignment) const;
+
+  void SkipToFreshGranule(uptr ClassId, hsa_amd_memory_pool_t Pool);
+  void MarkGap(uptr ClassId, uptr Begin, uptr End);
+
+  uptr FreeListFor(uptr ClassId, hsa_amd_memory_pool_t Pool);
+  void PushFree(uptr ClassId, uptr List, Run R);
+  bool TakeFree(uptr ClassId, uptr List, uptr Need, uptr Stride, bool MapUser,
+                uptr* Begin);
+
+  bool CommitMap(uptr Addr, uptr Bytes, u64 Handle, hsa_amd_memory_pool_t Home);
+  void RollbackMap();
+
+  bool CreateMetadata(hsa_amd_memory_pool_t Prefer, uptr Bytes, u64* Handle,
+                      hsa_amd_memory_pool_t* Home);
+  bool EnsureMetadata(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr Chunks);
+  bool MapUserChunk(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr ChunkIdx,
+                    uptr Need, u32 AllocFlags);
+  bool MapPrefix(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr Begin,
+                 uptr Count, uptr Bytes, u32 AllocFlags);
+
+  bool Grow(hsa_amd_memory_pool_t Pool, uptr ClassId, uptr List, uptr Need,
+            uptr Stride, bool MapUser, u32 AllocFlags, uptr* Begin);
+
+  bool WriteMetadata(uptr Addr, uptr Size, bool Pad, bool Poison);
+  void MarkChunk(uptr ClassId, uptr Begin, uptr List, const Placement& Place);
+  void ClearChunk(uptr ClassId, uptr Begin);
+
+  bool Recycle(uptr ClassId, uptr List);
+  bool RecycleOne(uptr PreferClass, uptr PreferList);
+  void Quarantine(uptr ClassId, uptr List, Run R, u32 Stack, u32 Tid);
+};
+
+Allocator& GetAllocator();
+
+}  // namespace __dasan
+
+#endif  // DASAN_ALLOCATOR_H
diff --git a/compiler-rt/lib/dasan/dasan_device.cpp b/compiler-rt/lib/dasan/dasan_device.cpp
new file mode 100644
index 00000000000000..babfe748c91a56
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_device.cpp
@@ -0,0 +1,115 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Device report path. Checks are emitted by the DeviceAddressSanitizer pass.
+///
+//===----------------------------------------------------------------------===//
+
+#include <gpuintrin.h>
+#include <stdint.h>
+
+#include "dasan_offload_packet.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "shared/rpc.h"
+
+[[gnu::visibility("protected"),
+  gnu::weak]] rpc::Client __dasan_rpc_client asm("__llvm_rpc_client");
+
+// Shallow deduplication table to avoid flooding the RPC channel on recover.
+static bool ShouldReport(uintptr_t Pc, uint8_t Kind, bool IsWrite) {
+  static uint64_t Seen[64];
+  const uint64_t Key =
+      (Pc & ~uint64_t{3}) | (uint64_t{Kind} << 1) | (IsWrite ? 1 : 0);
+  const uint64_t Idx = (Key * 0x9E3779B97F4A7C15ULL) >> 58;
+  uint64_t Last = __scoped_atomic_exchange_n(&Seen[Idx], Key, __ATOMIC_RELAXED,
+                                             __MEMORY_SCOPE_DEVICE);
+  return Last != Key;
+}
+
+template <bool Abort>
+[[clang::always_inline]] static void Send(uintptr_t Addr, uint64_t AccessSize,
+                                          bool IsWrite, uintptr_t Pc,
+                                          uint8_t Kind, uint64_t Base,
+                                          uint64_t Size) {
+  if (!__gpu_is_first_in_lane(__gpu_lane_mask()))
+    return;
+  if constexpr (!Abort) {
+    if (!ShouldReport(Pc, Kind, IsWrite))
+      return;
+  }
+
+  __dasan_offload_report R{};
+  R.Addr = Addr;
+  R.Base = Base;
+  R.AllocSize = Size;
+  R.Pc = Pc;
+  R.Recover = !Abort;
+  R.Kind = Kind;
+  R.AccessSize = AccessSize;
+  R.Block[0] = __gpu_block_id(__GPU_X_DIM);
+  R.Block[1] = __gpu_block_id(__GPU_Y_DIM);
+  R.Block[2] = __gpu_block_id(__GPU_Z_DIM);
+  R.Thread[0] = __gpu_thread_id(__GPU_X_DIM);
+  R.Thread[1] = __gpu_thread_id(__GPU_Y_DIM);
+  R.Thread[2] = __gpu_thread_id(__GPU_Z_DIM);
+  R.Lane = __gpu_lane_id();
+  R.IsWrite = IsWrite;
+
+  static_assert(sizeof(__dasan_offload_report) <= sizeof(rpc::Buffer),
+                "a report must fit in a single packet");
+
+  rpc::Client::Port Port = __dasan_rpc_client.open<SANITIZER_OFFLOAD_DASAN>();
+  Port.send([&](rpc::Buffer* Buffer, uint32_t) {
+    __builtin_memcpy(Buffer->data, &R, sizeof(R));
+  });
+
+  // Fatal reports are synchronous and wait on the host server.
+  if constexpr (Abort)
+    Port.recv([](rpc::Buffer*, uint32_t) {});
+}
+
+// The return address is the faulting access, so these must stay real calls.
+#define DASAN_HANDLER(Name, Kind, IsWrite)                                   \
+  extern "C" SANITIZER_INTERFACE_ATTRIBUTE                                   \
+      [[gnu::cold, gnu::noinline, gnu::noreturn]] void                       \
+      __dasan_report_##Name(void* Addr, uint64_t AccessSize, uint64_t Base,  \
+                            uint64_t Size) {                                 \
+    Send<true>(reinterpret_cast<uintptr_t>(Addr), AccessSize, IsWrite,       \
+               GET_CALLER_PC(), Kind, Base, Size);                           \
+    __gpu_exit();                                                            \
+  }                                                                          \
+  extern "C" SANITIZER_INTERFACE_ATTRIBUTE [[gnu::cold, gnu::noinline]] void \
+  __dasan_report_##Name##_noabort(void* Addr, uint64_t AccessSize,           \
+                                  uint64_t Base, uint64_t Size) {            \
+    Send<false>(reinterpret_cast<uintptr_t>(Addr), AccessSize, IsWrite,      \
+                GET_CALLER_PC(), Kind, Base, Size);                          \
+  }
+
+DASAN_HANDLER(load, DASAN_KIND_SPACE, false)
+DASAN_HANDLER(store, DASAN_KIND_SPACE, true)
+DASAN_HANDLER(shared_load, DASAN_KIND_SHARED, false)
+DASAN_HANDLER(shared_store, DASAN_KIND_SHARED, true)
+
+// TODO: place device heap traffic in the tracked window. Error for now.
+[[gnu::noinline, gnu::noreturn]] static void UnsupportedAlloc(uintptr_t Addr,
+                                                              uintptr_t Pc,
+                                                              bool IsFree) {
+  Send<true>(Addr, 0, IsFree, Pc, DASAN_KIND_ALLOC, 0, 0);
+  __gpu_exit();
+}
+
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE [[gnu::noinline]] void* malloc(
+    __SIZE_TYPE__) {
+  UnsupportedAlloc(0, GET_CALLER_PC(), false);
+}
+
+extern "C" SANITIZER_INTERFACE_ATTRIBUTE [[gnu::noinline]] void free(
+    void* Ptr) {
+  UnsupportedAlloc(reinterpret_cast<uintptr_t>(Ptr), GET_CALLER_PC(), true);
+}
diff --git a/compiler-rt/lib/dasan/dasan_flags.h b/compiler-rt/lib/dasan/dasan_flags.h
new file mode 100644
index 00000000000000..39a799b175b29d
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_flags.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Device address sanitizer runtime flags.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_FLAGS_H
+#define DASAN_FLAGS_H
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+namespace __dasan {
+
+struct Flags {
+#define DASAN_FLAG(Type, Name, Default, Description) Type Name;
+#include "dasan_flags.inc"
+#undef DASAN_FLAG
+
+  void SetDefaults();
+};
+
+extern Flags DasanFlags;
+inline Flags* flags() { return &DasanFlags; }
+
+void InitializeFlags();
+
+}  // namespace __dasan
+
+#endif  // DASAN_FLAGS_H
diff --git a/compiler-rt/lib/dasan/dasan_flags.inc b/compiler-rt/lib/dasan/dasan_flags.inc
new file mode 100644
index 00000000000000..9e550d57364f6f
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_flags.inc
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Device address sanitizer runtime flags.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_FLAG
+#  error "Define DASAN_FLAG prior to including this file!"
+#endif
+
+DASAN_FLAG(
+    bool, alias_globals, true,
+    "Place each loaded code object in the tracked window so its globals are "
+    "checked. The loader has to be able to load at an address. Off leaves "
+    "globals at the loader's addresses, where the check cannot see them.")
+
+DASAN_FLAG(int, quarantine_size_mb, 256,
+           "How much freed device memory to hold back, across all devices, "
+           "before its chunks may be handed out again. A use after free is "
+           "only reported while the chunk it names is still in here.")
+
+DASAN_FLAG(
+    int, halt_on_error, -1,
+    "Stop the program at the first report. The default of -1 follows "
+    "the build: an access compiled with -fsanitize-recover=daddress "
+    "keeps going, and 0 or 1 overrides that. An access compiled without "
+    "recover cannot resume, so 0 leaves that wave's work lost and warns.")
+
+DASAN_FLAG(bool, print_stats, false,
+           "Print how many allocations were placed, and why the rest were not, "
+           "when the device runtime shuts down.")
diff --git a/compiler-rt/lib/dasan/dasan_host.cpp b/compiler-rt/lib/dasan/dasan_host.cpp
new file mode 100644
index 00000000000000..c65aa61cefaea1
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_host.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Host report path API interface.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan.h"
+#include "dasan_report.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+using namespace __sanitizer;
+
+template <bool Abort>
+static bool Report(uintptr_t Addr, uint64_t AccessSize, bool IsWrite,
+                   uintptr_t Pc, uintptr_t Bp, uint64_t Base, uint64_t Size) {
+  return __dasan::HandleHostReport(Addr, AccessSize, Base, Size, IsWrite,
+                                   !Abort, Pc, Bp);
+}
+
+// The return address is the faulting access, so these must stay real calls.
+#define DASAN_HANDLER(Name, IsWrite)                                          \
+  extern "C" SANITIZER_INTERFACE_ATTRIBUTE                                    \
+      [[gnu::cold, gnu::noinline, gnu::noreturn]] void                        \
+      __dasan_report_##Name(void* Addr, uint64_t AccessSize, uint64_t Base,   \
+                            uint64_t Size) {                                  \
+    __dasan::Initialize();                                                    \
+    GET_CALLER_PC_BP;                                                         \
+    Report<true>(reinterpret_cast<uintptr_t>(Addr), AccessSize, IsWrite, pc,  \
+                 bp, Base, Size);                                             \
+    Die();                                                                    \
+  }                                                                           \
+  extern "C" SANITIZER_INTERFACE_ATTRIBUTE [[gnu::cold, gnu::noinline]] void  \
+  __dasan_report_##Name##_noabort(void* Addr, uint64_t AccessSize,            \
+                                  uint64_t Base, uint64_t Size) {             \
+    __dasan::Initialize();                                                    \
+    GET_CALLER_PC_BP;                                                         \
+    if (Report<false>(reinterpret_cast<uintptr_t>(Addr), AccessSize, IsWrite, \
+                      pc, bp, Base, Size))                                    \
+      Die();                                                                  \
+  }
+
+DASAN_HANDLER(load, false)
+DASAN_HANDLER(store, true)
+
+#undef DASAN_HANDLER
diff --git a/compiler-rt/lib/dasan/dasan_hsa_interceptors.cpp b/compiler-rt/lib/dasan/dasan_hsa_interceptors.cpp
new file mode 100644
index 00000000000000..ff80df6fafe2d4
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_hsa_interceptors.cpp
@@ -0,0 +1,448 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// HSA interceptors for the device address sanitizer runtime.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan.h"
+#include "dasan_allocator.h"
+#include "dasan_flags.h"
+#include "dasan_image.h"
+#include "dasan_platform.h"
+#include "dasan_report.h"
+#include "interception/interception.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_mutex.h"
+#include "sanitizer_common/sanitizer_offload.h"
+#include "sanitizer_common/sanitizer_platform.h"
+#include "sanitizer_common/sanitizer_stackdepot.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+
+#if !SANITIZER_LINUX
+#  error "the device address sanitizer is supported on Linux only"
+#endif
+
+using namespace __sanitizer;
+using namespace __dasan;
+
+// The runtime exports these to every program, act as if HSA is absent when it
+// is not loaded.
+#define DASAN_HSA_ENTER(name)                                               \
+  Initialize();                                                             \
+  if (UNLIKELY(!REAL(name))) {                                              \
+    INTERCEPT_FUNCTION(name);                                               \
+    if (UNLIKELY(!REAL(name))) {                                            \
+      VReport(1, "%s: cannot find %s in this process\n", SanitizerToolName, \
+              #name);                                                       \
+      return HSA_STATUS_ERROR;                                              \
+    }                                                                       \
+  }
+
+// Passthrough to the real version if initialization was unsuccessful.
+#define DASAN_HSA_FORWARD(name, ...)                         \
+  DASAN_HSA_ENTER(name);                                     \
+  if (UNLIKELY(!Offload::Get().Ready() || !PlatformReady())) \
+    return REAL(name)(__VA_ARGS__);
+
+// Bind the VA space and internal state after the offloading layer.
+static void Bind() {
+  Lock L(&DasanMutex);
+  if (!Offload::Get().Ready() || PlatformReady())
+    return;
+  if (!PlatformInit() || !GetAllocator().Init()) {
+    PlatformShutdown();
+    return;
+  }
+  PlatformCommit();
+}
+
+// Release the VA reservation and tear down the internal state.
+static void Unbind() {
+  Lock L(&DasanMutex);
+  if (!PlatformReady() || Offload::Get().Ready())
+    return;
+  UntrackAll();
+  PrintStats();
+  GetAllocator().Shutdown();
+  PlatformShutdown();
+}
+
+static void WatchGpuFaults();
+
+INTERCEPTOR(hsa_status_t, hsa_init, void) {
+  DASAN_HSA_ENTER(hsa_init);
+
+  hsa_status_t Status = REAL(hsa_init)();
+  if (Status != HSA_STATUS_SUCCESS)
+    return Status;
+
+  Offload::Get().Init();
+  Bind();
+  if (!PlatformReady()) {
+    static bool Told = false;
+    if (!Told) {
+      Told = true;
+      Report("%s: initialization failed, runtime disabled\n",
+             SanitizerToolName);
+    }
+  } else if (!flags()->alias_globals || !Offload::Get().CanLoadAtAddress()) {
+    static bool Told = false;
+    if (!Told) {
+      Told = true;
+      Report("%s: device globals will not be checked\n", SanitizerToolName);
+    }
+  }
+  if (PlatformReady())
+    WatchGpuFaults();
+  return Status;
+}
+
+INTERCEPTOR(hsa_status_t, hsa_shut_down, void) {
+  DASAN_HSA_ENTER(hsa_shut_down);
+
+  // Drains outstanding reports while the allocator can still describe them.
+  Offload::Get().Shutdown();
+  Unbind();
+  return REAL(hsa_shut_down)();
+}
+
+// Loads the user's executable into the tracked VA range. Global values will
+// pass the membership test and be checked like heap allocations.
+INTERCEPTOR(hsa_status_t, hsa_executable_load_agent_code_object,
+            hsa_executable_t Executable, hsa_agent_t Agent,
+            hsa_code_object_reader_t Reader, const char* Options,
+            hsa_loaded_code_object_t* Loaded) {
+  DASAN_HSA_FORWARD(hsa_executable_load_agent_code_object, Executable, Agent,
+                    Reader, Options, Loaded);
+
+  // The runtime does not support the necessary 1.04 loader extension. Loads the
+  // executable normally outside the checked VA range.
+  if (!flags()->alias_globals || !Offload::Get().CanLoadAtAddress())
+    return REAL(hsa_executable_load_agent_code_object)(Executable, Agent,
+                                                       Reader, Options, Loaded);
+
+  uptr LoadSize = 0;
+  if (!Offload::Get().CodeObjectLoadSize(Reader, &LoadSize))
+    return REAL(hsa_executable_load_agent_code_object)(Executable, Agent,
+                                                       Reader, Options, Loaded);
+
+  // Map the image into the window. The loader copies it there on freeze.
+  uptr Addr = 0, Span = 0;
+  {
+    Lock L(&DasanMutex);
+    if (!ReserveExecutable(Agent, LoadSize, Addr, Span))
+      Addr = 0;
+  }
+  if (!Addr)
+    return REAL(hsa_executable_load_agent_code_object)(Executable, Agent,
+                                                       Reader, Options, Loaded);
+
+  // Load the executable into the tracked memory region.
+  hsa_loaded_code_object_t Local = {};
+  if (!Offload::Get().LoadCodeObjectAt(Executable, Agent, Reader, Options,
+                                       reinterpret_cast<void*>(Addr), Span,
+                                       Loaded ? Loaded : &Local)) {
+    {
+      Lock L(&DasanMutex);
+      ReleaseExecutable(Addr);
+    }
+    return REAL(hsa_executable_load_agent_code_object)(Executable, Agent,
+                                                       Reader, Options, Loaded);
+  }
+
+  Lock L(&DasanMutex);
+  RecordExecutable(Addr);
+  VReport(1, "%s: loaded %zu bytes at 0x%zx\n", SanitizerToolName, LoadSize,
+          Addr);
+  return HSA_STATUS_SUCCESS;
+}
+
+INTERCEPTOR(hsa_status_t, hsa_executable_freeze, hsa_executable_t Executable,
+            const char* Options) {
+  DASAN_HSA_FORWARD(hsa_executable_freeze, Executable, Options);
+
+  // Traverse the loaded executable's globals and record their size in the
+  // associated metadata region.
+  hsa_status_t Status = REAL(hsa_executable_freeze)(Executable, Options);
+  if (Status != HSA_STATUS_SUCCESS)
+    return Status;
+  {
+    Lock L(&DasanMutex);
+    TrackExecutable(Executable);
+  }
+
+  // Track the executable for the RPC interface.
+  Offload::Get().TrackExecutable(Executable);
+  return Status;
+}
+
+INTERCEPTOR(hsa_status_t, hsa_executable_destroy, hsa_executable_t Executable) {
+  DASAN_HSA_FORWARD(hsa_executable_destroy, Executable);
+
+  // Forget the image and its associated globals.
+  Offload::Get().UntrackExecutable(Executable);
+  {
+    Lock L(&DasanMutex);
+    UntrackExecutable(Executable);
+  }
+  return REAL(hsa_executable_destroy)(Executable);
+}
+
+// Gets the thread ID for printing diagnostics without intercepting pthreads.
+static StaticSpinMutex TidMu;
+static ThreadID Tids[256];
+static u32 NTids;
+
+static u32 CompactTid() {
+  const ThreadID Os = GetTid();
+  SpinMutexLock L(&TidMu);
+  for (u32 I = 0; I < NTids; ++I)
+    if (Tids[I] == Os)
+      return I;
+  if (NTids == ARRAY_SIZE(Tids))
+    return 0;
+  Tids[NTids] = Os;
+  return NTids++;
+}
+
+// Record the stack trace at the function call for later diagnostics.
+static void CaptureSite(uptr Pc, uptr Bp, u32* Stack, u32* Tid) {
+  BufferedStackTrace Trace;
+  Trace.Unwind(Pc, Bp, nullptr, common_flags()->fast_unwind_on_malloc,
+               common_flags()->malloc_context_size);
+  *Stack = Trace.size ? StackDepotPut(Trace) : 0;
+  *Tid = CompactTid();
+}
+
+// Allocate memory into the tracked virtual address window. Attempt to use the
+// size class that best fits the allocation size, taking the next if exhausted.
+static bool PlaceAlloc(hsa_amd_memory_pool_t MemoryPool, size_t Size,
+                       uint32_t Flags, void** Ptr, u32 Stack, u32 Tid) {
+  if (!Size)
+    return false;
+  Lock L(&DasanMutex);
+  if (!IsValidPool(MemoryPool))
+    return false;
+
+  Allocator& A = GetAllocator();
+  const uptr First = ChooseClass(Size, HeapAlign(Size));
+  if (First >= kNumSizeClasses) {
+    ++A.Count.TooLarge;
+    return false;
+  }
+
+  for (uptr ClassId = First; ClassId < kNumSizeClasses; ++ClassId) {
+    const uptr Placed =
+        A.Allocate(MemoryPool, ClassId, Size, Flags, Stack, Tid);
+    if (!Placed)
+      continue;
+    if (ClassId != First)
+      ++A.Count.LargerClass;
+    *Ptr = reinterpret_cast<void*>(Placed);
+    return true;
+  }
+  ++A.Count.Exhausted;
+  return false;
+}
+
+static bool PlaceFree(void* Ptr, u32 Stack, u32 Tid) {
+  Lock L(&DasanMutex);
+  return GetAllocator().Deallocate(reinterpret_cast<uptr>(Ptr), Stack, Tid);
+}
+
+// Allow agent access to both the allocated memory and its associated metadata.
+static bool PlaceAllow(uptr Addr, const hsa_agent_t* Agents, u32 N,
+                       hsa_status_t* Status) {
+  Lock L(&DasanMutex);
+  if (!GetAllocator().IsPlaced(Addr))
+    return false;
+  *Status = GetAllocator().AllowAccess(Addr, Agents, N) ? HSA_STATUS_SUCCESS
+                                                        : HSA_STATUS_ERROR;
+  return true;
+}
+
+INTERCEPTOR(hsa_status_t, hsa_amd_memory_pool_allocate,
+            hsa_amd_memory_pool_t Pool, size_t Size, uint32_t Flags,
+            void** Ptr) {
+  DASAN_HSA_FORWARD(hsa_amd_memory_pool_allocate, Pool, Size, Flags, Ptr);
+
+  // PCI-e asks for point-to-point fine grain. A runtime that does not honor
+  // that flag on VRAM would place a coarse copy of a fine-grain request.
+  if ((Flags & HSA_AMD_MEMORY_POOL_PCIE_FLAG) &&
+      !Offload::Get().FineGrainVmem()) {
+    return REAL(hsa_amd_memory_pool_allocate)(Pool, Size, Flags, Ptr);
+  }
+
+  // Record the point of allocation for later diagnostics and attempt to place
+  // the allocation in the tracked virtual address space.
+  GET_CURRENT_PC_BP;
+  u32 Stack, Tid;
+  CaptureSite(pc, bp, &Stack, &Tid);
+  if (PlaceAlloc(Pool, Size, Flags, Ptr, Stack, Tid))
+    return HSA_STATUS_SUCCESS;
+
+  // If we failed to the place the allocation fall back to the HSA placement.
+  return REAL(hsa_amd_memory_pool_allocate)(Pool, Size, Flags, Ptr);
+}
+
+INTERCEPTOR(hsa_status_t, hsa_amd_memory_pool_free, void* Ptr) {
+  DASAN_HSA_FORWARD(hsa_amd_memory_pool_free, Ptr);
+
+  // Free the associated allocation and record.
+  GET_CURRENT_PC_BP;
+  u32 Stack, Tid;
+  CaptureSite(pc, bp, &Stack, &Tid);
+  if (PlaceFree(Ptr, Stack, Tid))
+    return HSA_STATUS_SUCCESS;
+  return REAL(hsa_amd_memory_pool_free)(Ptr);
+}
+
+INTERCEPTOR(hsa_status_t, hsa_memory_allocate, hsa_region_t Region, size_t Size,
+            void** Ptr) {
+  DASAN_HSA_FORWARD(hsa_memory_allocate, Region, Size, Ptr);
+
+  // ROCr uses one value for the region and the pool, borrow the handle.
+  GET_CURRENT_PC_BP;
+  u32 Stack, Tid;
+  CaptureSite(pc, bp, &Stack, &Tid);
+  hsa_amd_memory_pool_t Pool = {};
+  Pool.handle = Region.handle;
+  if (PlaceAlloc(Pool, Size, 0, Ptr, Stack, Tid))
+    return HSA_STATUS_SUCCESS;
+  return REAL(hsa_memory_allocate)(Region, Size, Ptr);
+}
+
+INTERCEPTOR(hsa_status_t, hsa_memory_free, void* Ptr) {
+  DASAN_HSA_FORWARD(hsa_memory_free, Ptr);
+
+  GET_CURRENT_PC_BP;
+  u32 Stack, Tid;
+  CaptureSite(pc, bp, &Stack, &Tid);
+  if (PlaceFree(Ptr, Stack, Tid))
+    return HSA_STATUS_SUCCESS;
+  return REAL(hsa_memory_free)(Ptr);
+}
+
+INTERCEPTOR(hsa_status_t, hsa_amd_agents_allow_access, uint32_t NumAgents,
+            const hsa_agent_t* Agents, const uint32_t* Flags, const void* Ptr) {
+  DASAN_HSA_FORWARD(hsa_amd_agents_allow_access, NumAgents, Agents, Flags, Ptr);
+
+  hsa_status_t Status;
+  if (PlaceAllow(reinterpret_cast<uptr>(Ptr), Agents, NumAgents, &Status))
+    return Status;
+  return REAL(hsa_amd_agents_allow_access)(NumAgents, Agents, Flags, Ptr);
+}
+
+INTERCEPTOR(hsa_status_t, hsa_amd_vmem_set_access, void* Va, size_t Size,
+            const hsa_amd_memory_access_desc_t* Desc, size_t DescCnt) {
+  DASAN_HSA_FORWARD(hsa_amd_vmem_set_access, Va, Size, Desc, DescCnt);
+
+  hsa_status_t Status;
+  if (!Desc || !DescCnt) {
+    if (PlaceAllow(reinterpret_cast<uptr>(Va), nullptr, 0, &Status))
+      return HSA_STATUS_ERROR;
+    return REAL(hsa_amd_vmem_set_access)(Va, Size, Desc, DescCnt);
+  }
+
+  InternalMmapVector<hsa_agent_t> Agents;
+  for (size_t I = 0; I < DescCnt; ++I) Agents.push_back(Desc[I].agent_handle);
+  if (PlaceAllow(reinterpret_cast<uptr>(Va), Agents.data(),
+                 static_cast<u32>(Agents.size()), &Status))
+    return Status;
+  return REAL(hsa_amd_vmem_set_access)(Va, Size, Desc, DescCnt);
+}
+
+INTERCEPTOR(hsa_status_t, hsa_amd_pointer_info, const void* Ptr,
+            hsa_amd_pointer_info_t* Info, void* (*Alloc)(size_t),
+            uint32_t* NumAgents, hsa_agent_t** Accessible) {
+  DASAN_HSA_FORWARD(hsa_amd_pointer_info, Ptr, Info, Alloc, NumAgents,
+                    Accessible);
+
+  // The runtime keeps track of virtual memory allocations. These must be
+  // modified to appear as if they came from a standard allocation.
+  hsa_status_t Status =
+      REAL(hsa_amd_pointer_info)(Ptr, Info, Alloc, NumAgents, Accessible);
+  if (Status != HSA_STATUS_SUCCESS)
+    return Status;
+
+  AllocInfo A;
+  u32 Flags = 0;
+  hsa_agent_t Owner = {};
+  bool HaveOwner = false;
+  {
+    Lock L(&DasanMutex);
+    if (!GetAllocator().getAllocInfo(reinterpret_cast<uptr>(Ptr), &A) ||
+        A.Freed)
+      return Status;
+    Flags = PoolFlags(A.Pool);
+    HaveOwner = AgentForPool(A.Pool, &Owner);
+  }
+
+  const uint32_t Size = Info->size;
+  if (Size < offsetof(hsa_amd_pointer_info_t, sizeInBytes) + sizeof(size_t))
+    return Status;
+  Info->type = HSA_EXT_POINTER_TYPE_HSA;
+  Info->agentBaseAddress = reinterpret_cast<void*>(A.Beg);
+  Info->hostBaseAddress = Info->agentBaseAddress;
+  Info->sizeInBytes = A.Size;
+  if (HaveOwner && Size >= offsetof(hsa_amd_pointer_info_t, agentOwner) +
+                               sizeof(Info->agentOwner))
+    Info->agentOwner = Owner;
+  if (Size >= offsetof(hsa_amd_pointer_info_t, global_flags) +
+                  sizeof(Info->global_flags))
+    Info->global_flags = Flags;
+  return Status;
+}
+
+// A non-success return leaves the runtime's own fault report in place.
+static hsa_status_t OnGpuEvent(const hsa_amd_event_t* Event, void*) {
+  if (Event && Event->event_type == HSA_AMD_GPU_MEMORY_FAULT_EVENT &&
+      PointerIsMetadata(Event->memory_fault.virtual_address))
+    Report(
+        "%s: GPU fault at 0x%zx is inside metadata. The program likely formed "
+        "an illegal out-of-bounds base pointer.\n",
+        SanitizerToolName,
+        static_cast<uptr>(Event->memory_fault.virtual_address));
+  return HSA_STATUS_ERROR;
+}
+
+// The wrapper exists so REAL() has a type. Registration itself is unchanged.
+INTERCEPTOR(hsa_status_t, hsa_amd_register_system_event_handler,
+            hsa_amd_system_event_callback_t Callback, void* Data) {
+  DASAN_HSA_FORWARD(hsa_amd_register_system_event_handler, Callback, Data);
+  return REAL(hsa_amd_register_system_event_handler)(Callback, Data);
+}
+
+// The runtime blindly trusts 'inbounds' pointers. Creating a base pointer
+// outside of the allocation range is undefined behavior, but manifests as a
+// memory fault in the metadata arena. Watch the faults to hint to the user.
+static void WatchGpuFaults() {
+  static bool Done = false;
+  if (Done)
+    return;
+  Done = true;
+  if (!REAL(hsa_amd_register_system_event_handler))
+    INTERCEPT_FUNCTION(hsa_amd_register_system_event_handler);
+  if (!REAL(hsa_amd_register_system_event_handler) ||
+      REAL(hsa_amd_register_system_event_handler)(OnGpuEvent, nullptr) !=
+          HSA_STATUS_SUCCESS)
+    VReport(1, "%s: GPU fault watch unavailable\n", SanitizerToolName);
+}
+
+[[gnu::constructor(0)]] static void DasanDynInit() {
+  __dasan_init();
+  WarnIfHsaNotInterposed();
+}
+
+[[gnu::destructor]] static void DasanAtExit() {
+  Lock L(&DasanMutex);
+  PrintStats();
+}
diff --git a/compiler-rt/lib/dasan/dasan_image.cpp b/compiler-rt/lib/dasan/dasan_image.cpp
new file mode 100644
index 00000000000000..d533f687f88886
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_image.cpp
@@ -0,0 +1,283 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Tracking associated with executable images. Support for global values comes
+/// from loading the executable within the virtual address region and marking
+/// chunks that overlap with globals in the executable.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan_image.h"
+
+#include <elf.h>
+
+#include "dasan_allocator.h"
+#include "dasan_flags.h"
+#include "dasan_mapping.h"
+#include "dasan_platform.h"
+#include "sanitizer_common/sanitizer_allocator_internal.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_offload.h"
+
+using namespace __sanitizer;
+
+namespace __dasan {
+
+namespace {
+const char kGuardName[] = "__dasan_guard";
+
+// An executable image that was loaded into virtual memory.
+struct TrackedImage {
+  uptr Addr;
+  uptr LoadSize;
+  uptr MapSize;
+  u64 Handle;
+  bool Recorded;
+  bool Described;
+};
+
+InternalMmapVectorNoCtor<GlobalInfo> Globals;
+InternalMmapVectorNoCtor<TrackedImage> Images;
+
+bool InFile(uptr Size, const Elf64_Shdr& S) {
+  return S.sh_type != SHT_NOBITS && S.sh_offset <= Size &&
+         S.sh_size <= Size - S.sh_offset;
+}
+
+// Only objects with valid symbols are instrumented by the runtime.
+bool DefinedObject(const Elf64_Ehdr& Hdr, const Elf64_Shdr* Sections,
+                   const Elf64_Sym& Sym, uptr LoadSize, uptr Chunk) {
+  if (ELF64_ST_TYPE(Sym.st_info) != STT_OBJECT || !Sym.st_size)
+    return false;
+  if (Sym.st_shndx == SHN_UNDEF || Sym.st_shndx >= Hdr.e_shnum)
+    return false;
+  if (!(Sections[Sym.st_shndx].sh_flags & SHF_ALLOC))
+    return false;
+  if (Sym.st_value % Chunk)
+    return false;
+  if (Sym.st_value > LoadSize || Sym.st_size > LoadSize - Sym.st_value)
+    return false;
+  return true;
+}
+
+// The pass does not pad __* / .* symbols or kernel descriptors, ignore them.
+bool HasRedzone(const char* Name) {
+  if (!Name)
+    return true;
+  if (Name[0] == '.' || (Name[0] == '_' && Name[1] == '_'))
+    return false;
+  uptr N = internal_strlen(Name);
+  return N < 3 || internal_strncmp(Name + N - 3, ".kd", 3) != 0;
+}
+
+// Iterate the executable's symbol table to get each symbol's size and offset.
+// The HSA symbol interface only exposes the dynamic symbol table.
+void DescribeGlobals(uptr Base, uptr LoadSize, const void* Storage,
+                     uptr StorageSize, uptr& Described) {
+  if (!Storage || StorageSize < sizeof(Elf64_Ehdr) || !LoadSize)
+    return;
+  auto* Hdr = reinterpret_cast<const Elf64_Ehdr*>(Storage);
+  if (internal_memcmp(Hdr->e_ident, ELFMAG, SELFMAG) != 0 ||
+      Hdr->e_ident[EI_CLASS] != ELFCLASS64 ||
+      Hdr->e_shentsize != sizeof(Elf64_Shdr) || !Hdr->e_shnum)
+    return;
+  const uptr ShdrBytes = Hdr->e_shnum * sizeof(Elf64_Shdr);
+  if (Hdr->e_shoff > StorageSize || ShdrBytes > StorageSize - Hdr->e_shoff)
+    return;
+  auto* Sections = reinterpret_cast<const Elf64_Shdr*>(
+      static_cast<const char*>(Storage) + Hdr->e_shoff);
+
+  // Find the symbol table.
+  const Elf64_Shdr* Symtab = nullptr;
+  for (uptr S = 0; S < Hdr->e_shnum; ++S) {
+    const Elf64_Shdr& Sec = Sections[S];
+    if (!InFile(StorageSize, Sec) || Sec.sh_entsize != sizeof(Elf64_Sym))
+      continue;
+    if (Sec.sh_type == SHT_SYMTAB)
+      Symtab = &Sec;
+    else if (Sec.sh_type == SHT_DYNSYM && !Symtab)
+      Symtab = &Sec;
+  }
+  if (!Symtab || !Symtab->sh_size || Symtab->sh_link >= Hdr->e_shnum)
+    return;
+
+  // Get the symbol table and string table offsets in the ELF.
+  const Elf64_Shdr& Strtab = Sections[Symtab->sh_link];
+  const char* Names = InFile(StorageSize, Strtab)
+                          ? static_cast<const char*>(Storage) + Strtab.sh_offset
+                          : nullptr;
+  auto* Syms = reinterpret_cast<const Elf64_Sym*>(
+      static_cast<const char*>(Storage) + Symtab->sh_offset);
+
+  auto NameOf = [&](const Elf64_Sym& Sym) -> const char* {
+    return Names && Sym.st_name < Strtab.sh_size ? Names + Sym.st_name
+                                                 : nullptr;
+  };
+  auto IsGuard = [](const char* Name) {
+    return Name && !internal_strncmp(Name, kGuardName, sizeof(kGuardName) - 1);
+  };
+
+  Allocator& A = GetAllocator();
+  const uptr Chunk = ClassIdToSize(0);
+  const uptr Count = Symtab->sh_size / sizeof(Elf64_Sym);
+  for (uptr N = 0; N < Count; ++N) {
+    const Elf64_Sym& Sym = Syms[N];
+    if (!DefinedObject(*Hdr, Sections, Sym, LoadSize, Chunk))
+      continue;
+    const char* Name = NameOf(Sym);
+    if (IsGuard(Name))
+      continue;
+    const uptr Obj = Base + (uptr)Sym.st_value;
+    const uptr Size = (uptr)Sym.st_size;
+    if (!A.Publish(Obj, Size, HasRedzone(Name)))
+      continue;
+    Globals.push_back({Obj, Size, Name ? internal_strdup(Name) : nullptr});
+    ++Described;
+  }
+}
+
+void ForgetGlobals() {
+  for (uptr N = 0; N < Globals.size(); ++N)
+    if (Globals[N].Name)
+      InternalFree(const_cast<char*>(Globals[N].Name));
+  Globals.clear();
+}
+
+void ForgetGlobals(uptr Beg, uptr Size) {
+  uptr Dst = 0;
+  for (uptr N = 0; N < Globals.size(); ++N) {
+    if (Globals[N].Beg >= Beg && Globals[N].Beg - Beg < Size) {
+      if (Globals[N].Name)
+        InternalFree(const_cast<char*>(Globals[N].Name));
+      continue;
+    }
+    if (Dst != N)
+      Globals[Dst] = Globals[N];
+    ++Dst;
+  }
+  Globals.resize(Dst);
+}
+
+void DropImage(uptr I) {
+  TrackedImage& Img = Images[I];
+  VaUnmap(Img.Addr, Img.MapSize);
+  VaDestroy(Img.Handle);
+  GetAllocator().Release(Img.Addr);
+  ForgetGlobals(Img.Addr, Img.LoadSize);
+  if (I + 1 != Images.size())
+    Images[I] = Images.back();
+  Images.pop_back();
+}
+
+void DescribePlaced(TrackedImage& Img, const Offload::CodeObject& Obj) {
+  if (Img.Described)
+    return;
+  uptr Described = 0;
+  DescribeGlobals(Img.Addr, Obj.LoadSize, Obj.Storage, Obj.StorageSize,
+                  Described);
+  Img.Described = true;
+  VReport(1, "%s: placed 0x%zx (+%zu); %zu globals\n", SanitizerToolName,
+          Img.Addr, Img.LoadSize, Described);
+}
+
+// The image is already in the window. Publish each global from its symbol.
+void OnCodeObject(const Offload::CodeObject& Obj) {
+  for (TrackedImage& Img : Images) {
+    if (!Img.Recorded || Img.Addr != Obj.LoadBase)
+      continue;
+    DescribePlaced(Img, Obj);
+    return;
+  }
+}
+
+}  // namespace
+
+bool ReserveExecutable(hsa_agent_t Agent, uptr LoadSize, uptr& Addr,
+                       uptr& Span) {
+  hsa_amd_memory_pool_t Pool = PoolForAgent(Agent);
+  if (!HasPool(Pool) || !LoadSize)
+    return false;
+  const uptr MapSize = RoundUpTo(LoadSize, VaGranule());
+  Allocator& A = GetAllocator();
+
+  // The instrumentation pass pads globals to the smallest size class.
+  uptr Base = A.Allocate(Pool, /*ClassId=*/0, MapSize, 0, 0, 0,
+                         /*MapUser=*/false);
+  if (!Base) {
+    ++A.Count.Exhausted;
+    return false;
+  }
+  u64 H = 0;
+  if (!VaCreate(Pool, MapSize, HSA_AMD_VMEM_HANDLE_CREATE_EXECUTABLE, &H) ||
+      !VaMap(Base, MapSize, H, Pool, 0, /*Executable=*/true)) {
+    if (H)
+      VaDestroy(H);
+    A.Release(Base);
+    return false;
+  }
+  Images.push_back({Base, LoadSize, MapSize, H, false, false});
+  Addr = Base;
+  Span = MapSize;
+  return true;
+}
+
+void ReleaseExecutable(uptr Addr) {
+  for (uptr I = 0; I < Images.size(); ++I) {
+    if (Images[I].Addr != Addr || Images[I].Recorded)
+      continue;
+    DropImage(I);
+    return;
+  }
+}
+
+void RecordExecutable(uptr Addr) {
+  for (TrackedImage& Img : Images) {
+    if (Img.Addr != Addr)
+      continue;
+    Img.Recorded = true;
+    return;
+  }
+}
+
+void TrackExecutable(hsa_executable_t Exec) {
+  if (!flags()->alias_globals)
+    return;
+  Offload::Get().ForEachCodeObject(Exec, OnCodeObject);
+}
+
+void UntrackExecutable(hsa_executable_t Exec) {
+  Offload::Get().ForEachCodeObject(Exec, [](const Offload::CodeObject& Obj) {
+    for (uptr I = 0; I < Images.size(); ++I) {
+      if (!Images[I].Recorded || Images[I].Addr != Obj.LoadBase)
+        continue;
+      VReport(1, "%s: released 0x%zx (+%zu)\n", SanitizerToolName,
+              Images[I].Addr, Images[I].LoadSize);
+      DropImage(I);
+      return;
+    }
+  });
+}
+
+void UntrackAll() {
+  while (Images.size()) DropImage(Images.size() - 1);
+  ForgetGlobals();
+}
+
+bool LookupGlobal(uptr Beg, GlobalInfo& Out) {
+  for (const GlobalInfo& G : Globals) {
+    if (G.Beg == Beg) {
+      Out = G;
+      return true;
+    }
+  }
+  return false;
+}
+
+}  // namespace __dasan
diff --git a/compiler-rt/lib/dasan/dasan_image.h b/compiler-rt/lib/dasan/dasan_image.h
new file mode 100644
index 00000000000000..0dc073d25de03d
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_image.h
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Tracking associated with executable images. Support for global values comes
+/// from loading the executable within the virtual address region and marking
+/// chunks that overlap with globals in the executable.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_IMAGE_H
+#define DASAN_IMAGE_H
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_offload_hsa.h"
+
+namespace __dasan {
+
+using namespace __sanitizer;
+
+struct GlobalInfo {
+  uptr Beg;
+  uptr Size;
+  const char* Name;
+};
+
+// Executable mapping large enough for LoadSize. Addr and Span receive the
+// range the loader places into. Empty on failure. The handle stays here.
+bool ReserveExecutable(hsa_agent_t Agent, uptr LoadSize, uptr& Addr,
+                       uptr& Span);
+// Drop a reservation the loader did not keep.
+void ReleaseExecutable(uptr Addr);
+// The load landed. Freeze may publish its globals.
+void RecordExecutable(uptr Addr);
+
+void TrackExecutable(hsa_executable_t Exec);
+void UntrackExecutable(hsa_executable_t Exec);
+void UntrackAll();
+
+bool LookupGlobal(uptr Beg, GlobalInfo& Out);
+
+}  // namespace __dasan
+
+#endif  // DASAN_IMAGE_H
diff --git a/compiler-rt/lib/dasan/dasan_mapping.h b/compiler-rt/lib/dasan/dasan_mapping.h
new file mode 100644
index 00000000000000..fdb2fa752b2d07
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_mapping.h
@@ -0,0 +1,236 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Instrumented heap layout. ABI between the host runtime and the pass.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_MAPPING_H
+#define DASAN_MAPPING_H
+
+#include <stdint.h>
+
+namespace __dasan {
+
+inline constexpr uint64_t kAddressSpaceSize = 1ULL << 47;
+
+inline constexpr uint64_t kSpaceSizeLog = 45;
+inline constexpr uint64_t kSpaceSize = 1ULL << kSpaceSizeLog;
+inline constexpr uint64_t kSpaceBeg = kSpaceSize;
+
+inline constexpr uint64_t kNumClassesLog = 5;
+inline constexpr uint64_t kNumClasses = 1ULL << kNumClassesLog;
+inline constexpr uint64_t kRegionSizeLog = kSpaceSizeLog - kNumClassesLog;
+inline constexpr uint64_t kRegionSize = 1ULL << kRegionSizeLog;
+inline constexpr uint64_t kRegionMask = kRegionSize - 1;
+
+// The device computes the chunk index as offset >> (kMinSizeLog + class).
+inline constexpr uint64_t kMinSizeLog = 6;
+
+inline constexpr uint64_t ClassIdToSizeLog(uint64_t ClassId) {
+  return kMinSizeLog + ClassId;
+}
+
+inline constexpr uint64_t ClassIdToSize(uint64_t ClassId) {
+  return 1ULL << ClassIdToSizeLog(ClassId);
+}
+
+inline constexpr uint64_t kMetadataSize = 8;
+
+inline constexpr uint64_t ReachableChunks(uint64_t ClassId) {
+  return 1ULL << (kRegionSizeLog - ClassIdToSizeLog(ClassId));
+}
+
+inline constexpr uint64_t kMetadataBytes = ReachableChunks(0) * kMetadataSize;
+
+inline constexpr uint64_t CountSizeClasses() {
+  uint64_t N = 0;
+  while (N < kNumClasses && ClassIdToSize(N) <= kRegionSize - kMetadataBytes)
+    ++N;
+  return N;
+}
+inline constexpr uint64_t kNumSizeClasses = CountSizeClasses();
+inline constexpr uint64_t kSpaceUsed = kNumSizeClasses << kRegionSizeLog;
+
+static_assert(kSpaceUsed == kSpaceBeg,
+              "the layout must keep every size class: see above");
+
+inline constexpr uint64_t kMinRedzone = 16;
+
+inline constexpr uint64_t kAllocAlignment = 256;
+
+inline constexpr uint64_t RegionBeg(uint64_t ClassId) {
+  return kSpaceBeg + (ClassId << kRegionSizeLog);
+}
+
+inline constexpr uint64_t MaxUsableChunks(uint64_t ClassId) {
+  return (kRegionSize - kMetadataBytes) >> ClassIdToSizeLog(ClassId);
+}
+
+inline constexpr uint64_t kMaxSize =
+    ClassIdToSize(kNumSizeClasses - 1) - 2 * kMinRedzone;
+
+// Smallest class whose chunk holds Size plus additional redzoning.
+inline constexpr uint64_t ClassID(uint64_t Size) {
+  if (Size > kMaxSize)
+    return kNumSizeClasses;
+  uint64_t ClassId = 0;
+  while (ClassIdToSize(ClassId) < Size + 2 * kMinRedzone) ++ClassId;
+  return ClassId;
+}
+
+// Entry layout.
+//   [36:0]   size of the live allocation, cleared on free
+//   [37]     poisoned: this chunk held an allocation and it is gone
+//   [63:38]  signed offset of the allocation from this chunk's base, in units
+//            of the smallest chunk size.
+// Every object starts on a class 0 boundary, so scaling the offset costs no
+// precision and lets an object span 2 GiB of chunks.
+inline constexpr uint64_t kSizeBits = 37;
+inline constexpr uint64_t kSizeMask = (1ULL << kSizeBits) - 1;
+inline constexpr uint64_t kPoisoned = 1ULL << kSizeBits;
+inline constexpr uint64_t kOffsetShift = kSizeBits + 1;
+inline constexpr uint64_t kOffsetBits = 64 - kOffsetShift;
+inline constexpr uint64_t kOffsetFieldMask = ~((1ULL << kOffsetShift) - 1);
+inline constexpr uint64_t kOffsetScaleLog = kMinSizeLog;
+inline constexpr int64_t kOffsetScale = 1LL << kOffsetScaleLog;
+inline constexpr int64_t kOffsetMax = ((1LL << (kOffsetBits - 1)) - 1)
+                                      << kOffsetScaleLog;
+inline constexpr int64_t kOffsetMin =
+    -((1LL << (kOffsetBits - 1)) << kOffsetScaleLog);
+
+inline constexpr bool OffsetFits(int64_t Offset) {
+  return Offset >= kOffsetMin && Offset <= kOffsetMax &&
+         Offset % kOffsetScale == 0;
+}
+
+inline constexpr uint64_t MakeEntry(uint64_t Size, int64_t Offset) {
+  return (Size & kSizeMask) |
+         ((static_cast<uint64_t>(Offset) << (kOffsetShift - kOffsetScaleLog)) &
+          kOffsetFieldMask);
+}
+
+inline constexpr uint64_t EntrySize(uint64_t Entry) {
+  return Entry & kSizeMask;
+}
+
+inline constexpr int64_t EntryOffset(uint64_t Entry) {
+  return static_cast<int64_t>(Entry & kOffsetFieldMask) >>
+         (kOffsetShift - kOffsetScaleLog);
+}
+
+inline constexpr uint64_t PoisonEntry(uint64_t Entry) {
+  return (Entry & ~kSizeMask) | kPoisoned;
+}
+
+inline constexpr bool IsPoisoned(uint64_t Entry) { return Entry & kPoisoned; }
+
+inline bool PointerIsMine(uint64_t Addr) {
+  return Addr - kSpaceBeg < kSpaceUsed;
+}
+
+inline bool PointerIsMetadata(uint64_t Addr) {
+  return PointerIsMine(Addr) &&
+         (Addr & kRegionMask) >= kRegionSize - kMetadataBytes;
+}
+
+inline uint64_t GetSizeClass(uint64_t Addr) {
+  return (Addr - kSpaceBeg) >> kRegionSizeLog;
+}
+
+inline uint64_t GetChunkIdx(uint64_t Addr, uint64_t ClassId) {
+  return (Addr & kRegionMask) >> ClassIdToSizeLog(ClassId);
+}
+
+inline constexpr uint64_t GetMetadata(uint64_t Addr, uint64_t ChunkIdx) {
+  return (Addr | kRegionMask) - ((ChunkIdx << 3) | 7);
+}
+
+inline constexpr bool IsInBounds(uint64_t ChunkOffset, uint64_t Entry) {
+  return ChunkOffset - static_cast<uint64_t>(EntryOffset(Entry)) <
+         EntrySize(Entry);
+}
+
+constexpr bool ValidateSizeClassMap() {
+  for (uint64_t C = 0; C < kNumSizeClasses; ++C) {
+    if (ClassID(ClassIdToSize(C) - 2 * kMinRedzone) != C)
+      return false;
+    if (C && ClassID(ClassIdToSize(C - 1) - 2 * kMinRedzone + 1) != C)
+      return false;
+    if (ClassIdToSize(C) > kRegionSize)
+      return false;
+    if (MaxUsableChunks(C) == 0)
+      return false;
+    if (MaxUsableChunks(C) * ClassIdToSize(C) > kRegionSize - kMetadataBytes)
+      return false;
+    if (ReachableChunks(C) * kMetadataSize > kMetadataBytes)
+      return false;
+    if (GetMetadata(RegionBeg(C), 0) !=
+        RegionBeg(C) + kRegionSize - kMetadataSize)
+      return false;
+    if (GetMetadata(RegionBeg(C), ReachableChunks(C) - 1) <
+        RegionBeg(C) + kRegionSize - kMetadataBytes)
+      return false;
+  }
+  return true;
+}
+
+constexpr bool ValidateEntry() {
+  uint64_t E = MakeEntry(4000, 128);
+  if (EntrySize(E) != 4000 || EntryOffset(E) != 128 || IsPoisoned(E))
+    return false;
+  if (!IsInBounds(128, E) || !IsInBounds(4127, E))
+    return false;
+  if (IsInBounds(127, E) || IsInBounds(4128, E) || IsInBounds(0, E))
+    return false;
+  if (!IsPoisoned(PoisonEntry(E)) || IsInBounds(128, PoisonEntry(E)) ||
+      EntryOffset(PoisonEntry(E)) != 128)
+    return false;
+
+  const int64_t Back = -static_cast<int64_t>(kAllocAlignment);
+  uint64_t S = MakeEntry(1024, Back);
+  if (EntryOffset(S) != Back || IsPoisoned(S))
+    return false;
+  if (!IsInBounds(0, S) || !IsInBounds(255, S))
+    return false;
+  if (IsInBounds(768, S) || !IsInBounds(767, S))
+    return false;
+
+  uint64_t Far = MakeEntry(kSizeMask, kOffsetMin);
+  if (EntrySize(Far) != kSizeMask || EntryOffset(Far) != kOffsetMin ||
+      IsPoisoned(Far))
+    return false;
+
+  return OffsetFits(kOffsetMax) && OffsetFits(kOffsetMin) &&
+         !OffsetFits(kOffsetMax + kOffsetScale) &&
+         !OffsetFits(kOffsetMin - kOffsetScale) &&
+         !OffsetFits(kOffsetScale / 2) &&
+         EntryOffset(MakeEntry(1, kOffsetMax)) == kOffsetMax &&
+         EntryOffset(MakeEntry(1, kOffsetMin)) == kOffsetMin;
+}
+
+static_assert(kSpaceBeg + kSpaceSize <= kAddressSpaceSize,
+              "space escapes the 47 bit range");
+static_assert(ValidateSizeClassMap(), "size class map does not round trip");
+static_assert(ValidateEntry(), "entry encoding does not round trip");
+static_assert(kMaxSize <= kSizeMask, "an allocation can outgrow its entry");
+static_assert(kAllocAlignment <= kOffsetMax,
+              "an offset cannot reach its entry");
+static_assert(kMinRedzone <= kAllocAlignment,
+              "a redzone is not a whole stride");
+static_assert(kAllocAlignment % kOffsetScale == 0,
+              "a heap offset is not a whole number of units");
+static_assert(ClassIdToSize(0) == kOffsetScale,
+              "a global's chunk offset is not a whole number of units");
+static_assert(-kOffsetMin >= (1LL << 31), "a large global cannot be described");
+static_assert(kSizeBits + kOffsetBits + 1 == 64, "an entry is not one word");
+
+}  // namespace __dasan
+
+#endif  // DASAN_MAPPING_H
diff --git a/compiler-rt/lib/dasan/dasan_offload_packet.h b/compiler-rt/lib/dasan/dasan_offload_packet.h
new file mode 100644
index 00000000000000..e99ecf167af8c8
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_offload_packet.h
@@ -0,0 +1,44 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Offload/host RPC packet for a failed device access.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_OFFLOAD_PACKET_H
+#define DASAN_OFFLOAD_PACKET_H
+
+#include <stdint.h>
+
+#include "sanitizer_common/sanitizer_offload_opcodes.h"
+
+enum __dasan_report_kind : uint8_t {
+  DASAN_KIND_SPACE = 0,
+  DASAN_KIND_SHARED = 1,
+  DASAN_KIND_ALLOC = 2,
+};
+
+struct __dasan_offload_report {
+  uint64_t Addr;       // Address accessed.
+  uint64_t Base;       // Base of the object the access was checked against.
+  uint64_t AllocSize;  // That object's size, zero once it is freed.
+  uint64_t Pc;         // Where the access was, as the wave saw it.
+  uint32_t AccessSize;
+  uint32_t Block[3];
+  uint16_t Thread[3];
+  uint8_t Lane;
+  uint8_t IsWrite;
+  uint8_t Kind;
+  uint8_t Recover;
+};
+
+static_assert(sizeof(__dasan_offload_report) <= 64,
+              "Offload DASan report must fit one RPC packet");
+
+#endif  // DASAN_OFFLOAD_PACKET_H
diff --git a/compiler-rt/lib/dasan/dasan_platform.cpp b/compiler-rt/lib/dasan/dasan_platform.cpp
new file mode 100644
index 00000000000000..995409eef78f43
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_platform.cpp
@@ -0,0 +1,332 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// How each reserved range is mapped, and who may touch it. Which pool backs a
+/// range is answered by the offload list.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan_platform.h"
+
+#include "sanitizer_common/sanitizer_atomic.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_offload.h"
+
+using namespace __sanitizer;
+
+namespace __dasan {
+namespace {
+
+constexpr uptr kNpos = ~(uptr)0;
+
+template <typename Vec, typename Pred>
+uptr FindIndex(const Vec& V, Pred P) {
+  for (uptr I = 0; I < V.size(); ++I)
+    if (P(V[I]))
+      return I;
+  return kNpos;
+}
+
+struct MappedRange {
+  uptr Addr;
+  uptr Size;
+  hsa_amd_memory_pool_t Pool;
+  bool CpuRw;
+
+  bool Covers(uptr P) const { return P >= Addr && P < Addr + Size; }
+  bool Overlaps(uptr Beg, uptr End) const {
+    return Addr < End && Beg < Addr + Size;
+  }
+};
+
+struct State {
+  InternalMmapVectorNoCtor<MappedRange> Mapped;
+  uptr Granule;
+  uptr MetaGranule;
+  atomic_uint8_t Bound;
+};
+State S;
+
+Offload& O() { return Offload::Get(); }
+
+// The memory types we can correct place and track in the reserved VA region.
+u32 PlaceableMask() {
+  u32 Match = HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_COARSE_GRAINED;
+  if (O().FineGrainVmem())
+    Match |= HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED |
+             HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_EXTENDED_SCOPE_FINE_GRAINED;
+  return Match;
+}
+
+bool Lookup(hsa_amd_memory_pool_t Pool, Offload::MemoryPool* Out) {
+  return HasPool(Pool) && O().FindPool(Pool, Out);
+}
+
+bool HostCanAccess(hsa_amd_memory_pool_t Pool) {
+  Offload::MemoryPool Found;
+  if (!HasPool(Pool))
+    return O().HostPool(&Found);
+  return Lookup(Pool, &Found) && Found.HostAccess;
+}
+
+bool CpuCanAccess(hsa_agent_t Cpu, hsa_amd_memory_pool_t Pool) {
+  return !HasPool(Pool) || O().CanAccess(Cpu, Pool);
+}
+
+bool CpuMapped(uptr Addr) {
+  const uptr I =
+      FindIndex(S.Mapped, [&](const MappedRange& R) { return R.Covers(Addr); });
+  return I != kNpos && S.Mapped[I].CpuRw;
+}
+
+void DropMapped(uptr I) {
+  if (I + 1 != S.Mapped.size())
+    S.Mapped[I] = S.Mapped.back();
+  S.Mapped.pop_back();
+}
+
+// The host stays writable when it can reach the pages. The owning GPU gets
+// read-write, or read-write-execute for a code object.
+bool Grant(uptr Addr, uptr Size, hsa_amd_memory_pool_t Pool, bool WithCpu,
+           bool Executable) {
+  InternalMmapVector<hsa_amd_memory_access_desc_t> Descs;
+  hsa_agent_t Owner = {};
+  const bool HaveOwner = HasPool(Pool) && AgentForPool(Pool, &Owner);
+  if (WithCpu) {
+    for (hsa_agent_t Cpu : O().Cpus()) {
+      if (!CpuCanAccess(Cpu, Pool))
+        continue;
+      Descs.push_back({HSA_ACCESS_PERMISSION_RW, Cpu});
+    }
+  }
+  if (HaveOwner)
+    Descs.push_back(
+        {Executable ? HSA_ACCESS_PERMISSION_RWX : HSA_ACCESS_PERMISSION_RW,
+         Owner});
+  if (Descs.empty())
+    return false;
+  return O().SetAccess(Addr, Size, Descs.data(), Descs.size());
+}
+
+}  // namespace
+
+bool PlatformInit() {
+  if (atomic_load(&S.Bound, memory_order_acquire))
+    return true;
+  Offload& Off = O();
+  if (!Off.Ready())
+    return false;
+
+  S.Granule = S.MetaGranule = 0;
+  const u32 Match = PlaceableMask();
+  uptr N = 0;
+  uptr Granule = 0;
+  uptr Meta = 0;
+  bool CpuDirect = true;
+  Off.ForEachPool([&](const Offload::MemoryPool& P) {
+    if (!Off.IsGpu(P.Agent) || !(P.Flags & Match))
+      return;
+    ++N;
+    Granule = Max(Granule, P.Granule);
+    Meta = Max(Meta, P.AllocGranule ? P.AllocGranule : P.Granule);
+    CpuDirect &= P.HostAccess;
+  });
+
+  if (!N || !Granule || !Meta) {
+    VReport(1, "%s: no device with placeable memory; HSA calls pass through\n",
+            SanitizerToolName);
+    return false;
+  }
+  S.Granule = Granule;
+  S.MetaGranule = Meta;
+  VReport(1, "%s: %zu placeable pools, granule %zu, metadata %zu%s%s\n",
+          SanitizerToolName, N, S.Granule, S.MetaGranule,
+          CpuDirect ? "" : ", host copies metadata",
+          Off.FineGrainVmem() ? ", fine-grain vmem" : "");
+  return true;
+}
+
+void PlatformShutdown() {
+  atomic_store(&S.Bound, 0, memory_order_release);
+  S.Mapped.clear();
+  S.Granule = S.MetaGranule = 0;
+}
+
+void PlatformCommit() { atomic_store(&S.Bound, 1, memory_order_release); }
+
+bool PlatformReady() {
+  return atomic_load(&S.Bound, memory_order_acquire) != 0;
+}
+
+void PlaceablePools(InternalMmapVector<hsa_amd_memory_pool_t>* Out) {
+  if (!Out)
+    return;
+  const u32 Match = PlaceableMask();
+  InternalMmapVector<hsa_agent_t> Seen;
+  O().ForEachPool([&](const Offload::MemoryPool& P) {
+    if (!O().IsGpu(P.Agent))
+      return;
+    for (hsa_agent_t Agent : Seen)
+      if (Agent.handle == P.Agent.handle)
+        return;
+    Seen.push_back(P.Agent);
+    Offload::MemoryPool Chosen;
+    if (O().PoolForAgent(P.Agent, Match, &Chosen))
+      Out->push_back(Chosen.Pool);
+  });
+}
+
+bool IsValidPool(hsa_amd_memory_pool_t Pool) {
+  Offload::MemoryPool Found;
+  return Lookup(Pool, &Found) && O().IsGpu(Found.Agent) &&
+         (Found.Flags & PlaceableMask());
+}
+
+hsa_amd_memory_pool_t PoolForAgent(hsa_agent_t Agent) {
+  Offload::MemoryPool Found;
+  if (!O().PoolForAgent(Agent, PlaceableMask(), &Found))
+    return {};
+  return Found.Pool;
+}
+
+bool AgentForPool(hsa_amd_memory_pool_t Pool, hsa_agent_t* Out) {
+  Offload::MemoryPool Found;
+  if (!Lookup(Pool, &Found))
+    return false;
+  *Out = Found.Agent;
+  return true;
+}
+
+u32 PoolFlags(hsa_amd_memory_pool_t Pool) {
+  Offload::MemoryPool Found;
+  return Lookup(Pool, &Found) ? Found.Flags : 0;
+}
+
+uptr VaGranule() { return S.Granule; }
+
+uptr VaMetaGranule() { return S.MetaGranule; }
+
+bool VaReserve(uptr Addr, uptr Size) { return O().ReserveAddress(Addr, Size); }
+
+void VaRelease(uptr Addr, uptr Size) { O().FreeAddress(Addr, Size); }
+
+bool VaCreate(hsa_amd_memory_pool_t Pool, uptr Size, u32 Flags, u64* Handle) {
+  Offload::MemoryPool Found;
+  const bool Ok = HasPool(Pool) ? Lookup(Pool, &Found) : O().HostPool(&Found);
+  return Ok && HasPool(Found.Pool) &&
+         O().CreateHandle(Found.Pool, Size, Flags, Handle);
+}
+
+void VaDestroy(u64 Handle) { O().ReleaseHandle(Handle); }
+
+bool VaMap(uptr Addr, uptr Size, u64 Handle, hsa_amd_memory_pool_t Pool,
+           uptr Offset, bool Executable) {
+  if (!O().Map(Addr, Size, Offset, Handle))
+    return false;
+  const bool WithCpu = HostCanAccess(Pool);
+  if (!Grant(Addr, Size, Pool, WithCpu, Executable)) {
+    O().Unmap(Addr, Size);
+    return false;
+  }
+  S.Mapped.resize(S.Mapped.size() + 1);
+  MappedRange& R = S.Mapped.back();
+  R.Addr = Addr;
+  R.Size = Size;
+  R.Pool = Pool;
+  R.CpuRw = WithCpu;
+  return true;
+}
+
+void VaUnmap(uptr Addr, uptr Size) {
+  O().Unmap(Addr, Size);
+  const uptr I = FindIndex(S.Mapped, [&](const MappedRange& M) {
+    return M.Addr == Addr && M.Size == Size;
+  });
+  if (I != kNpos)
+    DropMapped(I);
+}
+
+bool VaAllowAgents(uptr Addr, uptr Size, const hsa_agent_t* Agents, u32 N) {
+  if (!N)
+    return true;
+  const uptr End = Addr + Size;
+  InternalMmapVector<hsa_amd_memory_access_desc_t> Descs;
+  for (u32 I = 0; I < N; ++I)
+    Descs.push_back({HSA_ACCESS_PERMISSION_RW, Agents[I]});
+  bool Hit = false;
+  for (MappedRange& M : S.Mapped) {
+    if (!M.Overlaps(Addr, End))
+      continue;
+    Hit = true;
+    if (!O().SetAccess(M.Addr, M.Size, Descs.data(), Descs.size()))
+      return false;
+  }
+  return Hit;
+}
+
+bool VaAllow(uptr Addr, uptr Size, hsa_amd_memory_pool_t Pool) {
+  hsa_agent_t Agent = {};
+  if (!AgentForPool(Pool, &Agent))
+    return false;
+  return VaAllowAgents(Addr, Size, &Agent, 1);
+}
+
+bool VaProbe(uptr Addr) {
+  const u64 Magic = 0xda5a4e0000da5a4eULL;
+  u64 Got = 0;
+  if (CpuMapped(Addr)) {
+    *reinterpret_cast<volatile u64*>(Addr) = Magic;
+    Got = *reinterpret_cast<volatile u64*>(Addr);
+    return Got == Magic;
+  }
+  void* Dst = reinterpret_cast<void*>(Addr);
+  return O().Copy(Dst, &Magic, sizeof(Magic)) &&
+         O().Copy(&Got, Dst, sizeof(Got)) && Got == Magic;
+}
+
+void VaPublish(uptr Addr, const void* Src, uptr N) {
+  if (!N)
+    return;
+  uptr Done = 0;
+  while (Done < N) {
+    uptr Pos = Addr + Done;
+    const uptr I = FindIndex(
+        S.Mapped, [&](const MappedRange& R) { return R.Covers(Pos); });
+    if (I == kNpos) {
+      Report("ERROR: %s: device write has no mapping at 0x%zx\n",
+             SanitizerToolName, Pos);
+      Die();
+    }
+    const MappedRange& M = S.Mapped[I];
+    uptr Step = Min(N - Done, M.Addr + M.Size - Pos);
+    const void* Part = Src ? static_cast<const u8*>(Src) + Done : nullptr;
+    // Pages belong to the pool's agent. A GPU owner is stored through its
+    // command processor; the host is stored directly. Access is not ownership.
+    hsa_agent_t Owner = {};
+    if (HasPool(M.Pool) && AgentForPool(M.Pool, &Owner) && O().IsGpu(Owner) &&
+        !O().PublishDeviceBytes(Pos, Part, Step, Owner)) {
+      Report("ERROR: %s: device write failed at 0x%zx\n", SanitizerToolName,
+             Pos);
+      Die();
+    }
+    // The host check loads this mapping. The confirmed L2 store does not
+    // update the copy that load reads.
+    if (M.CpuRw) {
+      void* Dst = reinterpret_cast<void*>(Pos);
+      if (Part)
+        internal_memcpy(Dst, Part, Step);
+      else
+        internal_memset(Dst, 0, Step);
+    }
+    Done += Step;
+  }
+}
+
+}  // namespace __dasan
diff --git a/compiler-rt/lib/dasan/dasan_platform.h b/compiler-rt/lib/dasan/dasan_platform.h
new file mode 100644
index 00000000000000..2c3be87cadf364
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_platform.h
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// How each reserved range is mapped, and who may touch it. Which pool backs a
+/// range is answered by the offload list.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_PLATFORM_H
+#define DASAN_PLATFORM_H
+
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_offload_hsa.h"
+
+namespace __dasan {
+
+using namespace __sanitizer;
+
+inline bool operator==(hsa_amd_memory_pool_t A, hsa_amd_memory_pool_t B) {
+  return A.handle == B.handle;
+}
+inline bool operator!=(hsa_amd_memory_pool_t A, hsa_amd_memory_pool_t B) {
+  return !(A == B);
+}
+// A zero handle is the host pool, resolved when a handle is created.
+inline bool HasPool(hsa_amd_memory_pool_t Pool) { return Pool.handle != 0; }
+
+bool PlatformInit();
+void PlatformShutdown();
+// The VA window is reserved. Callers hold DasanMutex.
+void PlatformCommit();
+bool PlatformReady();
+
+// One placeable pool per GPU, coarse preferred, in agent order.
+void PlaceablePools(InternalMmapVector<hsa_amd_memory_pool_t>* Out);
+// True when this pool is one we place into.
+bool IsValidPool(hsa_amd_memory_pool_t Pool);
+hsa_amd_memory_pool_t PoolForAgent(hsa_agent_t Agent);
+bool AgentForPool(hsa_amd_memory_pool_t Pool, hsa_agent_t* Out);
+u32 PoolFlags(hsa_amd_memory_pool_t Pool);
+
+uptr VaGranule();
+// Allocation granule for metadata handles. VaGranule() is the recommended
+// block.
+uptr VaMetaGranule();
+
+bool VaReserve(uptr Addr, uptr Size);
+void VaRelease(uptr Addr, uptr Size);
+
+bool VaCreate(hsa_amd_memory_pool_t Pool, uptr Size, u32 Flags, u64* Handle);
+void VaDestroy(u64 Handle);
+
+bool VaMap(uptr Addr, uptr Size, u64 Handle, hsa_amd_memory_pool_t Pool,
+           uptr Offset = 0, bool Executable = false);
+void VaUnmap(uptr Addr, uptr Size);
+bool VaAllow(uptr Addr, uptr Size, hsa_amd_memory_pool_t Pool);
+bool VaAllowAgents(uptr Addr, uptr Size, const hsa_agent_t* Agents, u32 N);
+
+bool VaProbe(uptr Addr);
+// Store [Addr, Addr + N). A GPU-owned range goes through that agent's
+// command processor and is confirmed in L2. A host-owned range is stored
+// directly. Null Src stores zeros. A failed store is fatal.
+void VaPublish(uptr Addr, const void* Src, uptr N);
+
+}  // namespace __dasan
+
+#endif  // DASAN_PLATFORM_H
diff --git a/compiler-rt/lib/dasan/dasan_preinit.cpp b/compiler-rt/lib/dasan/dasan_preinit.cpp
new file mode 100644
index 00000000000000..a01070266357bf
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_preinit.cpp
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Call __dasan_init at the very early stage of process startup.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+#if SANITIZER_CAN_USE_PREINIT_ARRAY
+// This section is linked into the main executable when offloading uses DASan
+// to perform initialization at a very early stage.
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __dasan_init;
+#endif
diff --git a/compiler-rt/lib/dasan/dasan_report.cpp b/compiler-rt/lib/dasan/dasan_report.cpp
new file mode 100644
index 00000000000000..553b4091f8329e
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_report.cpp
@@ -0,0 +1,446 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Format a device report for the host.
+///
+//===----------------------------------------------------------------------===//
+
+#include "dasan_report.h"
+
+#include "dasan.h"
+#include "dasan_allocator.h"
+#include "dasan_flags.h"
+#include "dasan_image.h"
+#include "sanitizer_common/sanitizer_common.h"
+#include "sanitizer_common/sanitizer_flags.h"
+#include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_offload.h"
+#include "sanitizer_common/sanitizer_report_decorator.h"
+#include "sanitizer_common/sanitizer_stackdepot.h"
+#include "sanitizer_common/sanitizer_stacktrace.h"
+#include "sanitizer_common/sanitizer_stacktrace_printer.h"
+#include "sanitizer_common/sanitizer_symbolizer.h"
+#include "shared/rpc.h"
+
+using namespace __sanitizer;
+
+namespace __dasan {
+
+namespace {
+
+class Decorator : public SanitizerCommonDecorator {
+ public:
+  const char* Access() { return Blue(); }
+  const char* Location() { return Green(); }
+  const char* Allocation() { return Magenta(); }
+};
+}  // namespace
+
+static void PrintHeader(const char* Bug, uptr Addr, uptr Pc) {
+  Decorator D;
+  Printf("=================================================================\n");
+  Printf("%s", D.Error());
+  if (Pc)
+    Report("ERROR: %s: %s on address %p at pc %p\n", SanitizerToolName, Bug,
+           (void*)Addr, (void*)Pc);
+  else
+    Report("ERROR: %s: %s on address %p\n", SanitizerToolName, Bug,
+           (void*)Addr);
+  Printf("%s", D.Default());
+}
+
+static s32 GroupOff(uptr V) { return static_cast<s32>(static_cast<u32>(V)); }
+
+static bool Before(uptr Addr, uptr Beg, bool Shared) {
+  if (Shared)
+    return GroupOff(Addr) < GroupOff(Beg);
+  return Addr < Beg;
+}
+
+static void PrintOffset(uptr Addr, uptr Beg, uptr Size, bool Shared = false) {
+  if (Shared) {
+    const s32 At = GroupOff(Addr);
+    const s32 Base = GroupOff(Beg);
+    const s32 End = Base + static_cast<s32>(Size);
+    if (At < Base)
+      Printf("%p is located %zu bytes before", (void*)Addr, (uptr)(Base - At));
+    else if (At >= End)
+      Printf("%p is located %zu bytes after", (void*)Addr, (uptr)(At - End));
+    else
+      Printf("%p is located %zu bytes inside of", (void*)Addr,
+             (uptr)(At - Base));
+    return;
+  }
+  if (Addr < Beg)
+    Printf("%p is located %zu bytes before", (void*)Addr, Beg - Addr);
+  else if (Addr >= Beg + Size)
+    Printf("%p is located %zu bytes after", (void*)Addr, Addr - (Beg + Size));
+  else
+    Printf("%p is located %zu bytes inside of", (void*)Addr, Addr - Beg);
+}
+
+static void PrintLocation(uptr Addr, uptr Beg, uptr Size, bool Shared = false) {
+  Decorator D;
+  Printf("%s", D.Location());
+  PrintOffset(Addr, Beg, Size, Shared);
+  Printf(" %zu-byte region [%p,%p)\n", Size, (void*)Beg, (void*)(Beg + Size));
+  Printf("%s", D.Default());
+}
+
+static void PrintDeviceStack(SymbolizedStack* Frames, uptr Pc) {
+  if (!Pc)
+    return;
+  if (!Frames) {
+    Printf("    #0 0x%zx\n\n", Pc);
+    return;
+  }
+
+  InternalScopedString Out;
+  uptr N = 0;
+  for (SymbolizedStack* F = Frames; F; F = F->next) {
+    uptr Was = Out.length();
+    StackTracePrinter::GetOrInit()->RenderFrame(
+        &Out, common_flags()->stack_trace_format, N++, F->info.address,
+        &F->info, common_flags()->symbolize_vs_style,
+        common_flags()->strip_path_prefix);
+    if (Out.length() != Was)
+      Out.Append("\n");
+  }
+  Out.Append("\n");
+  Printf("%s", Out.data());
+}
+
+// The trace starts in the HSA interceptor. The next module is whoever called
+// it (HIP, the OpenMP plugin). That whole module is the nest under the
+// program's call. A direct call from the same module keeps every frame but
+// the interceptor.
+static uptr CallerNestEnd(const char** Mods, uptr N) {
+  if (!N)
+    return 0;
+  uptr Begin = 1;
+  if (Begin < N && Mods[0] && Mods[Begin] && Mods[Begin] != Mods[0]) {
+    const char* Wrap = Mods[Begin];
+    while (Begin < N && Mods[Begin] == Wrap) ++Begin;
+  }
+  return Begin;
+}
+
+static void PrintUserStack(const StackTrace& Stack) {
+  Symbolizer* Sym = Symbolizer::GetOrInit();
+  const char* Mods[kStackTraceMax];
+  uptr Pcs[kStackTraceMax];
+  uptr N = 0;
+  for (; N < Stack.size && Stack.trace[N] && N < kStackTraceMax; ++N) {
+    Pcs[N] = StackTrace::GetPreviousInstructionPc(Stack.trace[N]);
+    Mods[N] = Sym->GetModuleNameForPc(Pcs[N]);
+  }
+  const uptr Begin = CallerNestEnd(Mods, N);
+
+  InternalScopedString Out;
+  uptr Shown = 0;
+  StackTracePrinter* Printer = StackTracePrinter::GetOrInit();
+  for (uptr I = Begin; I < N; ++I) {
+    SymbolizedStack* Frames = Sym->SymbolizePC(Pcs[I]);
+    for (SymbolizedStack* F = Frames; F; F = F->next) {
+      uptr Was = Out.length();
+      Printer->RenderFrame(&Out, common_flags()->stack_trace_format, Shown,
+                           Pcs[I], &F->info, common_flags()->symbolize_vs_style,
+                           common_flags()->strip_path_prefix);
+      if (Out.length() != Was) {
+        Out.Append("\n");
+        ++Shown;
+      }
+    }
+    if (Frames)
+      Frames->ClearAll();
+  }
+  if (!Shown) {
+    Stack.Print();
+    return;
+  }
+  Out.Append("\n");
+  Printf("%s", Out.data());
+}
+
+// ASan's SuppressErrorReport. The pool stores program counters only. The
+// twenty-sixth distinct one aborts without a report.
+static bool SuppressEqualPcs(uptr Pc) {
+  if (!common_flags()->suppress_equal_pcs)
+    return false;
+  constexpr unsigned kPoolSize = 25;
+  static uptr Pool[kPoolSize];
+  static unsigned Used;
+  for (unsigned I = 0; I < Used; ++I)
+    if (Pool[I] == Pc)
+      return true;
+  if (Used == kPoolSize)
+    Die();
+  Pool[Used++] = Pc;
+  return false;
+}
+
+static void PrintSite(const char* Verb, u32 Tid, u32 Id) {
+  if (!Id)
+    return;
+  Decorator D;
+  Printf("%s%s thread T%u here:%s\n", D.Allocation(), Verb, Tid, D.Default());
+  PrintUserStack(StackDepotGet(Id));
+}
+
+static void PrintGlobalLocation(uptr Addr, const GlobalInfo& G) {
+  Decorator D;
+  Printf("%s", D.Location());
+  PrintOffset(Addr, G.Beg, G.Size);
+  Printf(" global variable '%s' (%p) of size %zu\n",
+         G.Name ? G.Name : "<unknown>", (void*)G.Beg, G.Size);
+  Printf("%s", D.Default());
+}
+
+// A fatal handler has already ended the wave. halt_on_error cannot resume it.
+static bool ShouldHalt(bool Recover) {
+  const int Want = flags()->halt_on_error;
+  const bool Halt = Want < 0 ? !Recover : Want != 0;
+  if (!Halt && !Recover) {
+    static bool Told = false;
+    if (!Told) {
+      Told = true;
+      Report(
+          "WARNING: %s: halt_on_error=0 cannot resume an access compiled "
+          "without -fsanitize-recover=daddress; that wave's work is lost\n",
+          SanitizerToolName);
+    }
+  }
+  return Halt;
+}
+
+static bool PrintReport(const __dasan_offload_report& R) {
+  if (R.Kind == DASAN_KIND_ALLOC) {
+    const char* What = R.IsWrite ? "free" : "malloc";
+    Decorator D;
+    Printf(
+        "=================================================================\n");
+    Printf("%s", D.Error());
+    Report("ERROR: %s: device %s is not supported\n", SanitizerToolName, What);
+    Printf("%s", D.Default());
+    SymbolizedStack* Frames = Offload::Get().Symbolize(R.Pc);
+    PrintDeviceStack(Frames, R.Pc);
+    if (Frames) {
+      InternalScopedString S;
+      S.AppendF("unsupported-device-%s", What);
+      ReportErrorSummary(S.data(), Frames->info);
+      Frames->ClearAll();
+    }
+    return true;
+  }
+
+  const bool IsShared = R.Kind == DASAN_KIND_SHARED;
+
+  GlobalInfo G;
+  const bool IsGlobal = !IsShared && LookupGlobal(R.Base, G);
+  AllocInfo A;
+  const bool Known =
+      !IsShared && !IsGlobal && GetAllocator().getAllocInfo(R.Base, &A);
+  // The host may free the region before the report is served, so this is the
+  // check's view: a freed entry keeps its offset but reads as zero-sized.
+  const bool Freed = Known && R.AllocSize == 0;
+  uptr Beg = IsGlobal ? G.Beg : Known ? A.Beg : R.Base;
+  uptr Size = IsGlobal ? G.Size : Known ? A.Size : R.AllocSize;
+
+  const char* Bug;
+  if (Freed)
+    Bug = "heap-use-after-free";
+  else if (IsShared)
+    Bug = Before(R.Addr, Beg, true) ? "shared-buffer-underflow"
+                                    : "shared-buffer-overflow";
+  else if (IsGlobal)
+    Bug = R.Addr < Beg ? "global-buffer-underflow" : "global-buffer-overflow";
+  else if (!Known && R.AllocSize == 0)
+    Bug = "unknown-device-address";
+  else if (R.Addr < Beg)
+    Bug = "heap-buffer-underflow";
+  else
+    Bug = "heap-buffer-overflow";
+
+  const bool Halt = ShouldHalt(R.Recover);
+  if (!Halt && SuppressEqualPcs(R.Pc))
+    return false;
+
+  PrintHeader(Bug, R.Addr, R.Pc);
+
+  Decorator D;
+  Printf("%s%s of size %u at %p thread (%u,%u,%u) of block (%u,%u,%u)%s\n",
+         D.Access(), R.IsWrite ? "WRITE" : "READ", R.AccessSize, (void*)R.Addr,
+         R.Thread[0], R.Thread[1], R.Thread[2], R.Block[0], R.Block[1],
+         R.Block[2], D.Default());
+
+  SymbolizedStack* Frames = Offload::Get().Symbolize(R.Pc);
+  PrintDeviceStack(Frames, R.Pc);
+
+  if (IsGlobal)
+    PrintGlobalLocation(R.Addr, G);
+  else if (Size)
+    PrintLocation(R.Addr, Beg, Size, IsShared);
+  if (IsShared)
+    Printf(
+        "the region is group memory, which every workgroup has its own copy "
+        "of at these addresses\n");
+  if (!IsShared && !IsGlobal && Known) {
+    if (Freed)
+      PrintSite("freed by", A.FreeTid, A.FreeStack);
+    PrintSite(Freed ? "previously allocated by" : "allocated by", A.AllocTid,
+              A.AllocStack);
+  }
+
+  if (Frames) {
+    ReportErrorSummary(Bug, Frames->info);
+    Frames->ClearAll();
+  } else {
+    InternalScopedString S;
+    S.AppendF("%s (%p)", Bug, (void*)(uptr)R.Addr);
+    ReportErrorSummary(S.data());
+  }
+
+  static bool Told = false;
+  if (Halt && !R.Recover && !Told) {
+    Told = true;
+    Report(
+        "HINT: build with -fsanitize-recover=daddress to report and keep "
+        "going\n");
+  }
+  return Halt;
+}
+
+bool HandleHostReport(uptr Addr, uptr AccessSize, uptr Base, uptr AllocSize,
+                      bool IsWrite, bool Recover, uptr Pc, uptr Bp) {
+  BufferedStackTrace Stack;
+  Stack.Unwind(Pc, Bp, nullptr, common_flags()->fast_unwind_on_fatal);
+  const uptr FaultPc =
+      Stack.size ? StackTrace::GetPreviousInstructionPc(Stack.trace[0]) : 0;
+
+  Lock L(&DasanMutex);
+
+  GlobalInfo G;
+  const bool IsGlobal = LookupGlobal(Base, G);
+  AllocInfo A;
+  const bool Known = !IsGlobal && GetAllocator().getAllocInfo(Base, &A);
+  // A freed entry keeps its offset but reads as zero-sized.
+  const bool Freed = Known && AllocSize == 0;
+  uptr Beg = IsGlobal ? G.Beg : Known ? A.Beg : Base;
+  uptr Size = IsGlobal ? G.Size : Known ? A.Size : AllocSize;
+
+  const char* Bug;
+  if (Freed)
+    Bug = "heap-use-after-free";
+  else if (IsGlobal)
+    Bug = Addr < Beg ? "global-buffer-underflow" : "global-buffer-overflow";
+  else if (!Known && AllocSize == 0)
+    Bug = "unknown-device-address";
+  else if (Addr < Beg)
+    Bug = "heap-buffer-underflow";
+  else
+    Bug = "heap-buffer-overflow";
+
+  const bool Halt = ShouldHalt(Recover);
+  if (!Halt && SuppressEqualPcs(FaultPc))
+    return false;
+
+  PrintHeader(Bug, Addr, FaultPc);
+
+  Decorator D;
+  Printf("%s%s of size %zu at %p%s\n", D.Access(), IsWrite ? "WRITE" : "READ",
+         AccessSize, (void*)Addr, D.Default());
+  Stack.Print();
+
+  if (IsGlobal)
+    PrintGlobalLocation(Addr, G);
+  else if (Size)
+    PrintLocation(Addr, Beg, Size);
+  if (!IsGlobal && Known) {
+    if (Freed)
+      PrintSite("freed by", A.FreeTid, A.FreeStack);
+    PrintSite(Freed ? "previously allocated by" : "allocated by", A.AllocTid,
+              A.AllocStack);
+  }
+
+  SymbolizedStack* Frames = nullptr;
+  if (Stack.size)
+    Frames = Symbolizer::GetOrInit()->SymbolizePC(FaultPc);
+  if (Frames) {
+    ReportErrorSummary(Bug, Frames->info);
+    Frames->ClearAll();
+  } else {
+    InternalScopedString S;
+    S.AppendF("%s (%p)", Bug, (void*)Addr);
+    ReportErrorSummary(S.data());
+  }
+
+  static bool Told = false;
+  if (Halt && !Recover && !Told) {
+    Told = true;
+    Report(
+        "HINT: build with -fsanitize-recover=daddress to report and keep "
+        "going\n");
+  }
+  return Halt;
+}
+
+u32 HandleDeviceReport(void* PortPtr, u32) {
+  auto& Port = *reinterpret_cast<rpc::Server::Port*>(PortPtr);
+  if (Port.get_opcode() != SANITIZER_OFFLOAD_DASAN)
+    return rpc::RPC_UNHANDLED_OPCODE;
+
+  bool Halt = false;
+  Port.recv([&](rpc::Buffer* Buffer, uint32_t) {
+    __dasan_offload_report R;
+    internal_memcpy(&R, Buffer->data, sizeof(R));
+    Lock L(&DasanMutex);
+    Halt = PrintReport(R);
+  });
+  if (Halt)
+    Die();
+  return rpc::RPC_SUCCESS;
+}
+
+void ReportInvalidFree(uptr Addr, u32 Stack, u32 Tid) {
+  AllocInfo A;
+  bool Known = GetAllocator().getAllocInfo(Addr, &A);
+  const char* Bug = Known && A.Freed ? "double-free" : "bad-free";
+  PrintHeader(Bug, Addr, /*Pc=*/0);
+  if (Stack)
+    PrintUserStack(StackDepotGet(Stack));
+  if (Known)
+    PrintLocation(Addr, A.Beg, A.Size);
+  if (Known && A.Freed)
+    PrintSite("freed by", A.FreeTid, A.FreeStack);
+  if (Known)
+    PrintSite(A.Freed ? "previously allocated by" : "allocated by", A.AllocTid,
+              A.AllocStack);
+  InternalScopedString S;
+  S.AppendF("%s (%p)", Bug, (void*)Addr);
+  ReportErrorSummary(S.data());
+}
+
+void PrintStats() {
+  if (!flags()->print_stats)
+    return;
+  static bool Printed = false;
+  if (Printed)
+    return;
+  Printed = true;
+  Allocator& A = GetAllocator();
+  Report(
+      "stats: %zu placed (%zu above their own class), %zu freed; passed "
+      "through: %zu too large, %zu no room\n",
+      A.Count.Placed, A.Count.LargerClass, A.Count.Freed, A.Count.TooLarge,
+      A.Count.Exhausted);
+  Report("stats: %zu code objects placed over %zu chunks\n",
+         A.Count.CodeObjects, A.Count.CodeObjectChunks);
+}
+
+}  // namespace __dasan
diff --git a/compiler-rt/lib/dasan/dasan_report.h b/compiler-rt/lib/dasan/dasan_report.h
new file mode 100644
index 00000000000000..ca5793b35f3582
--- /dev/null
+++ b/compiler-rt/lib/dasan/dasan_report.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Formats a report to be presented to the user.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef DASAN_REPORT_H
+#define DASAN_REPORT_H
+
+#include "dasan_offload_packet.h"
+#include "sanitizer_common/sanitizer_internal_defs.h"
+
+namespace __dasan {
+
+using namespace __sanitizer;
+
+u32 HandleDeviceReport(void* Port, u32 Lanes);
+bool HandleHostReport(uptr Addr, uptr AccessSize, uptr Base, uptr AllocSize,
+                      bool IsWrite, bool Recover, uptr Pc, uptr Bp);
+
+void ReportInvalidFree(uptr Addr, u32 Stack, u32 Tid);
+
+void PrintStats();
+
+}  // namespace __dasan
+
+#endif  // DASAN_REPORT_H
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_offload.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_offload.cpp
index 2604900607cbbd..916b367a42d9a1 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_offload.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_offload.cpp
@@ -18,6 +18,16 @@
 namespace __sanitizer {
 namespace {
 
+// Where or not hsa_amd_vmem_handle_create supports fine-grained memory.
+constexpr u16 kFineGrainVmemMinor = 33;
+
+bool IsDevice(decltype(&hsa_agent_get_info) Query, hsa_agent_t Agent,
+              hsa_device_type_t Type) {
+  hsa_device_type_t Got;
+  return Query(Agent, HSA_AGENT_INFO_DEVICE, &Got) == HSA_STATUS_SUCCESS &&
+         Got == Type;
+}
+
 // Generic helpers to iterate HSA agents and pools.
 template <typename ElemTy, typename IterFuncTy, typename CallbackTy>
 hsa_status_t Iterate(IterFuncTy Func, CallbackTy Cb) {
@@ -52,6 +62,55 @@ void CheckHsa(hsa_status_t S) {
   Die();
 }
 
+// PM4 type-3 header. Count is the body size minus one. Bit 1 is the compute
+// shader type.
+u32 Pm4Header(u32 Opcode, u32 Dwords) {
+  return (1u << 1) | (Opcode << 8) | ((Dwords - 2) << 16) | (3u << 30);
+}
+
+// One WRITE_DATA of a contiguous run. dst_sel = TC_L2, wr_confirm, address
+// increments per dword, cache policy LRU. The control word is the same from
+// gfx9 onward; a qword is two data dwords, a longer run is more of them.
+//
+// The type-3 count is 14 bits, so one WRITE_DATA stops at 65520 bytes. The
+// indirect-buffer size is 20 bits, so one jump carries 63 of those packets
+// and one completion signal. Every packet confirms before the next starts.
+constexpr u32 kWritePacketDwords = 16384;
+constexpr u32 kPacketsPerIb = 63;
+constexpr u32 kIbDwords = kWritePacketDwords * kPacketsPerIb;
+constexpr uptr kPm4IbBytes = static_cast<uptr>(kIbDwords) * sizeof(u32);
+constexpr uptr kWriteChunkBytes = (kWritePacketDwords - 4) * sizeof(u32);
+static_assert(kWritePacketDwords - 2 <= 0x3FFFu, "WRITE_DATA count is 14 bits");
+static_assert(kIbDwords <= 0xFFFFFu, "indirect buffer size is 20 bits");
+static_assert(kWriteChunkBytes % sizeof(u64) == 0, "entries are 8 bytes");
+
+u32 EmitWriteData(u32* Out, uptr Addr, const void* Src, uptr Bytes) {
+  u32 DataDwords = Bytes / sizeof(u32);
+  u32 PacketDwords = DataDwords + 4;
+  Out[0] = Pm4Header(0x37, PacketDwords);
+  Out[1] = (2u << 8) | (1u << 20);
+  Out[2] = static_cast<u32>(Addr);
+  Out[3] = static_cast<u32>(Addr >> 32);
+  if (Src)
+    internal_memcpy(Out + 4, Src, Bytes);
+  else
+    internal_memset(Out + 4, 0, Bytes);
+  return PacketDwords;
+}
+
+// Vendor AQL packet that jumps to a PM4 indirect buffer. dw_cnt_remain counts
+// the dwords after that field; the firmware expects 0xA.
+struct AmdAqlPm4Ib {
+  u16 Header;
+  u16 VenHdr;
+  u32 IbJump[4];
+  u32 DwCntRemain;
+  u32 Reserved[8];
+  hsa_signal_t Completion;
+};
+static_assert(sizeof(AmdAqlPm4Ib) == 64, "vendor PM4 packet");
+static_assert(offsetof(AmdAqlPm4Ib, Completion) == 56, "vendor PM4 packet");
+
 }  // namespace
 
 Offload Offload::Ctx;
@@ -95,33 +154,44 @@ bool Offload::Resolve() {
 // disabled.
 bool Offload::Discover() {
   DeviceList.clear();
-  HostDevice = {};
+  CpuList.clear();
+  PoolList.clear();
 
   auto Info = [&](hsa_agent_t Agent, u32 Attr, void* Out) {
     return Api.hsa_agent_get_info(Agent, static_cast<hsa_agent_info_t>(Attr),
                                   Out) == HSA_STATUS_SUCCESS;
   };
-  auto Pool = [&](hsa_agent_t Agent) {
-    hsa_amd_memory_pool_t Fine{};
+  auto PoolInfo = [&](hsa_amd_memory_pool_t Mem, u32 Attr, void* Out) {
+    return Api.hsa_amd_memory_pool_get_info(
+               Mem, static_cast<hsa_amd_memory_pool_info_t>(Attr), Out) ==
+           HSA_STATUS_SUCCESS;
+  };
+  auto RecordPools = [&](hsa_agent_t Agent) {
     Iterate<hsa_amd_memory_pool_t>(
         Api.hsa_amd_agent_iterate_memory_pools, Agent,
         [&](hsa_amd_memory_pool_t Mem) {
           hsa_amd_segment_t Seg;
           u32 Flags = 0;
-          if (Api.hsa_amd_memory_pool_get_info(Mem,
-                                               HSA_AMD_MEMORY_POOL_INFO_SEGMENT,
-                                               &Seg) != HSA_STATUS_SUCCESS ||
-              Seg != HSA_AMD_SEGMENT_GLOBAL)
-            return HSA_STATUS_SUCCESS;
-          if (Api.hsa_amd_memory_pool_get_info(
-                  Mem, HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS, &Flags) !=
-              HSA_STATUS_SUCCESS)
+          bool Allowed = false;
+          if (!PoolInfo(Mem, HSA_AMD_MEMORY_POOL_INFO_SEGMENT, &Seg) ||
+              Seg != HSA_AMD_SEGMENT_GLOBAL ||
+              !PoolInfo(Mem, HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS, &Flags) ||
+              !PoolInfo(Mem, HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_ALLOWED,
+                        &Allowed) ||
+              !Allowed)
             return HSA_STATUS_SUCCESS;
-          if (Flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED)
-            Fine = Mem;
+          MemoryPool P = {Agent, Mem, Flags, 0, 0, false};
+          uptr Granule = 0;
+          if (PoolInfo(Mem, HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_GRANULE,
+                       &Granule))
+            P.AllocGranule = P.Granule = Granule;
+          if (PoolInfo(Mem, HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_REC_GRANULE,
+                       &Granule) &&
+              Granule > P.Granule)
+            P.Granule = Granule;
+          PoolList.push_back(P);
           return HSA_STATUS_SUCCESS;
         });
-    return Fine;
   };
 
   CheckHsa(Iterate<hsa_agent_t>(Api.hsa_iterate_agents, [&](hsa_agent_t Agent) {
@@ -129,12 +199,12 @@ bool Offload::Discover() {
     if (hsa_status_t S =
             Api.hsa_agent_get_info(Agent, HSA_AGENT_INFO_DEVICE, &Type))
       return S;
-    Device D = {};
-    D.Agent = Agent;
-    D.Pool = Pool(Agent);
-    if (Type == HSA_DEVICE_TYPE_CPU && !HostDevice.Agent.handle) {
-      HostDevice = D;
+    RecordPools(Agent);
+    if (Type == HSA_DEVICE_TYPE_CPU) {
+      CpuList.push_back(Agent);
     } else if (Type == HSA_DEVICE_TYPE_GPU) {
+      Device D = {};
+      D.Agent = Agent;
       u32 CUs = 0, WavesPerCU = 0;
       if (!Info(Agent, HSA_AGENT_INFO_WAVEFRONT_SIZE, &D.Lanes) || !D.Lanes ||
           !Info(Agent, HSA_AMD_AGENT_INFO_COMPUTE_UNIT_COUNT, &CUs) ||
@@ -148,15 +218,30 @@ bool Offload::Discover() {
     }
     return HSA_STATUS_SUCCESS;
   }));
-  return HostDevice.Agent.handle && !DeviceList.empty() &&
-         HostDevice.Pool.handle;
+
+  for (MemoryPool& P : PoolList)
+    for (hsa_agent_t Cpu : CpuList)
+      P.HostAccess = P.HostAccess || CanAccess(Cpu, P.Pool);
+  bool HostFine = false;
+  for (const MemoryPool& P : PoolList)
+    HostFine |= IsCpu(P.Agent) && P.HostAccess &&
+                (P.Flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED);
+  return !CpuList.empty() && !DeviceList.empty() && HostFine;
 }
 
 // Initialize the HSA loader extension used to manage host addresses.
 bool Offload::BindLoader() {
   Loader = {};
-  CheckHsa(Api.hsa_system_get_major_extension_table(HSA_EXTENSION_AMD_LOADER, 1,
-                                                    sizeof(Loader), &Loader));
+  // The loader's size depends on the supported version, a larger size will be
+  // left null by the runtime.
+  if (Api.hsa_system_get_major_extension_table(HSA_EXTENSION_AMD_LOADER, 1,
+                                               sizeof(Loader),
+                                               &Loader) != HSA_STATUS_SUCCESS) {
+    Loader = {};
+    CheckHsa(Api.hsa_system_get_major_extension_table(
+        HSA_EXTENSION_AMD_LOADER, 1, offsetof(LoaderApi, CreateFromFile),
+        &Loader));
+  }
   return Loader.QueryHostAddress && Loader.IterateLoadedCodeObjects &&
          Loader.GetCodeObjectInfo;
 }
@@ -168,6 +253,59 @@ bool Offload::ExecutableInfo(hsa_loaded_code_object_t Obj,
   return Loader.GetCodeObjectInfo(Obj, Attr, Out) == HSA_STATUS_SUCCESS;
 }
 
+bool Offload::DescribeCodeObject(hsa_loaded_code_object_t Obj,
+                                 hsa_agent_t Agent, CodeObject* Out) {
+  *Out = {};
+  Out->Agent = Agent;
+  u64 LoadBase = 0, LoadSize = 0;
+  if (!ExecutableInfo(Obj, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_BASE,
+                      &LoadBase) ||
+      !ExecutableInfo(Obj, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_SIZE,
+                      &LoadSize) ||
+      !LoadBase || !LoadSize)
+    return false;
+  Out->LoadBase = (uptr)LoadBase;
+  Out->LoadSize = (uptr)LoadSize;
+
+  u64 StorageType = 0, StorageBase = 0, StorageSize = 0;
+  if (ExecutableInfo(
+          Obj,
+          HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_TYPE,
+          &StorageType) &&
+      StorageType == HSA_VEN_AMD_LOADER_CODE_OBJECT_STORAGE_TYPE_MEMORY &&
+      ExecutableInfo(
+          Obj,
+          HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_MEMORY_BASE,
+          &StorageBase) &&
+      ExecutableInfo(
+          Obj,
+          HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_MEMORY_SIZE,
+          &StorageSize) &&
+      StorageBase && StorageSize) {
+    Out->Storage = reinterpret_cast<const void*>(StorageBase);
+    Out->StorageSize = (uptr)StorageSize;
+  }
+  return true;
+}
+
+void Offload::VisitCodeObjects(hsa_executable_t Exec,
+                               void (*Fn)(const CodeObject&, void*),
+                               void* Arg) {
+  InternalMmapVector<CodeObject> Objects;
+  {
+    Lock L(&OffloadMtx);
+    if (!Ready())
+      return;
+    ForEachAgentObject(Exec,
+                       [&](hsa_loaded_code_object_t Obj, hsa_agent_t Agent) {
+                         CodeObject Desc;
+                         if (DescribeCodeObject(Obj, Agent, &Desc))
+                           Objects.push_back(Desc);
+                       });
+  }
+  for (const CodeObject& Obj : Objects) Fn(Obj, Arg);
+}
+
 bool Offload::Init() {
   Lock Life(&LifetimeMtx);
   Lock L(&OffloadMtx);
@@ -181,8 +319,16 @@ bool Offload::Init() {
     --Refs;
     return false;
   }
-  VReport(1, "%s: device reporting on %zu GPU(s)\n", SanitizerToolName,
-          DeviceList.size());
+  CheckHsa(Api.hsa_system_get_info(HSA_AMD_SYSTEM_INFO_EXT_VERSION_MINOR,
+                                   &AmdExtMinor));
+  VReport(1, "%s: device reporting on %zu GPU(s), AMD extension 1.%u%s\n",
+          SanitizerToolName, DeviceList.size(), AmdExtMinor,
+          CanLoadAtAddress() ? ", code objects can be placed" : "");
+  if (!CreateCommands()) {
+    Teardown();
+    --Refs;
+    return false;
+  }
   atomic_store(&Active, 1, memory_order_release);
   return true;
 }
@@ -197,8 +343,18 @@ bool Offload::Release() {
 void Offload::Teardown() {
   UntrackImages();
   Loader = {};
+  for (Device& D : DeviceList) {
+    if (D.Queue)
+      Api.hsa_queue_destroy(D.Queue);
+    if (D.Signal.handle)
+      DestroySignal(D.Signal);
+  }
+  if (Ib)
+    Free(Ib);
+  Ib = nullptr;
   DeviceList.clear();
-  HostDevice = {};
+  CpuList.clear();
+  PoolList.clear();
 }
 
 void Offload::Shutdown() {
@@ -227,37 +383,13 @@ void Offload::TrackExecutable(hsa_executable_t Exec) {
     Lock L(&OffloadMtx);
     if (!Ready())
       return;
-    ForEachAgentObject(Exec, [&](hsa_loaded_code_object_t Obj, hsa_agent_t) {
-      u64 LoadBase = 0, LoadSize = 0;
-      if (!ExecutableInfo(Obj,
-                          HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_BASE,
-                          &LoadBase) ||
-          !ExecutableInfo(Obj,
-                          HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_SIZE,
-                          &LoadSize) ||
-          !LoadBase || !LoadSize)
-        return;
-
-      u64 StorageType = 0, StorageBase = 0, StorageSize = 0;
-      const void* Storage = nullptr;
-      if (ExecutableInfo(
-              Obj,
-              HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_TYPE,
-              &StorageType) &&
-          StorageType == HSA_VEN_AMD_LOADER_CODE_OBJECT_STORAGE_TYPE_MEMORY &&
-          ExecutableInfo(
-              Obj,
-              HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_MEMORY_BASE,
-              &StorageBase) &&
-          ExecutableInfo(
-              Obj,
-              HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_CODE_OBJECT_STORAGE_MEMORY_SIZE,
-              &StorageSize) &&
-          StorageBase && StorageSize)
-        Storage = reinterpret_cast<const void*>(StorageBase);
-
-      TrackImage((uptr)LoadBase, (uptr)LoadSize, Storage, (uptr)StorageSize);
-    });
+    ForEachAgentObject(Exec,
+                       [&](hsa_loaded_code_object_t Obj, hsa_agent_t Agent) {
+                         CodeObject Desc;
+                         if (DescribeCodeObject(Obj, Agent, &Desc))
+                           TrackImage(Desc.LoadBase, Desc.LoadSize,
+                                      Desc.Storage, Desc.StorageSize);
+                       });
   }
   OffloadRpc::Start(*this, Exec);
 }
@@ -275,18 +407,19 @@ void Offload::UntrackExecutable(hsa_executable_t Exec) {
   });
 }
 
-// Allocate coherent 'fine-grained' memory for host and device communication.
-bool Offload::Alloc(const Device& D, uptr Bytes, void** Out) {
+// Allocate from a pool. Grant every agent access if it is reachable.
+bool Offload::Alloc(const MemoryPool& Pool, uptr Bytes, void** Out, u64 Flags) {
   void* P = nullptr;
-  if (!D.Pool.handle ||
-      Api.hsa_amd_memory_pool_allocate(D.Pool, Bytes, 0, &P) !=
+  if (!Pool.Pool.handle ||
+      Api.hsa_amd_memory_pool_allocate(Pool.Pool, Bytes, Flags, &P) !=
           HSA_STATUS_SUCCESS ||
       !P)
     return false;
   InternalMmapVector<hsa_agent_t> All;
-  All.push_back(HostDevice.Agent);
-  for (uptr I = 0; I < DeviceList.size(); ++I)
-    All.push_back(DeviceList[I].Agent);
+  for (hsa_agent_t Cpu : CpuList)
+    if (CanAccess(Cpu, Pool.Pool))
+      All.push_back(Cpu);
+  for (const Device& D : DeviceList) All.push_back(D.Agent);
   if (Api.hsa_amd_agents_allow_access(All.size(), All.data(), nullptr, P) !=
       HSA_STATUS_SUCCESS) {
     Api.hsa_amd_memory_pool_free(P);
@@ -296,6 +429,63 @@ bool Offload::Alloc(const Device& D, uptr Bytes, void** Out) {
   return true;
 }
 
+bool Offload::PoolForAgent(hsa_agent_t Agent, u32 Match, MemoryPool* Out) {
+  if (!Out || !Agent.handle || !Match)
+    return false;
+  Lock L(&OffloadMtx);
+  if (!Ready())
+    return false;
+  const MemoryPool* Fallback = nullptr;
+  for (const MemoryPool& P : PoolList) {
+    if (P.Agent.handle != Agent.handle || !(P.Flags & Match))
+      continue;
+    if (P.Flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_COARSE_GRAINED) {
+      *Out = P;
+      return true;
+    }
+    if (!Fallback)
+      Fallback = &P;
+  }
+  if (!Fallback)
+    return false;
+  *Out = *Fallback;
+  return true;
+}
+
+bool Offload::FindPool(hsa_amd_memory_pool_t Pool, MemoryPool* Out) {
+  if (!Out || !Pool.handle)
+    return false;
+  Lock L(&OffloadMtx);
+  if (!Ready())
+    return false;
+  for (const MemoryPool& P : PoolList) {
+    if (P.Pool.handle != Pool.handle)
+      continue;
+    *Out = P;
+    return true;
+  }
+  return false;
+}
+
+bool Offload::HostPool(MemoryPool* Out) {
+  if (!Out)
+    return false;
+  Lock L(&OffloadMtx);
+  if (!Ready())
+    return false;
+  for (const MemoryPool& P : PoolList) {
+    bool Gpu = false;
+    for (const Device& D : DeviceList)
+      if (D.Agent.handle == P.Agent.handle)
+        Gpu = true;
+    if (Gpu || !(P.Flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED))
+      continue;
+    *Out = P;
+    return true;
+  }
+  return false;
+}
+
 bool Offload::GetMemoryPool(hsa_agent_t Agent, hsa_amd_memory_pool_t* Pool) {
   Lock L(&OffloadMtx);
   if (!Ready() || !Pool)
@@ -341,7 +531,7 @@ bool Offload::Allocate(hsa_amd_memory_pool_t Pool, uptr Bytes, void** Out) {
 
 void Offload::Free(void* P) { Api.hsa_amd_memory_pool_free(P); }
 
-bool Offload::Copy(void* Dst, const void* Src, uptr N) {
+bool Offload::Copy(void* Dst, const void* Src, uptr N) const {
   return Api.hsa_memory_copy(Dst, Src, N) == HSA_STATUS_SUCCESS;
 }
 
@@ -388,6 +578,217 @@ void Offload::StoreSignal(hsa_signal_t Sig) {
   Api.hsa_signal_store_screlease(Sig, 1);
 }
 
+bool Offload::IsGpu(hsa_agent_t Agent) const {
+  return IsDevice(Api.hsa_agent_get_info, Agent, HSA_DEVICE_TYPE_GPU);
+}
+
+bool Offload::IsCpu(hsa_agent_t Agent) const {
+  return IsDevice(Api.hsa_agent_get_info, Agent, HSA_DEVICE_TYPE_CPU);
+}
+
+bool Offload::CanAccess(hsa_agent_t Agent, hsa_amd_memory_pool_t Pool) const {
+  hsa_amd_memory_pool_access_t Access =
+      HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED;
+  return Api.hsa_amd_agent_memory_pool_get_info(
+             Agent, Pool, HSA_AMD_AGENT_MEMORY_POOL_INFO_ACCESS, &Access) ==
+             HSA_STATUS_SUCCESS &&
+         Access != HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED;
+}
+
+// Requires HSA AMD version minor 0.33.
+bool Offload::FineGrainVmem() const {
+  return AmdExtMinor >= kFineGrainVmemMinor;
+}
+
+// Supports loader extension API 1.04.
+bool Offload::CanLoadAtAddress() const {
+  return Loader.GetLoadSize && Loader.LoadAtAddress;
+}
+
+// Requires loader extension API 1.04.
+bool Offload::CodeObjectLoadSize(hsa_code_object_reader_t Reader,
+                                 uptr* Size) const {
+  DCHECK(Loader.GetLoadSize);
+  size_t Bytes = 0;
+  if (!Loader.GetLoadSize ||
+      Loader.GetLoadSize(Reader, &Bytes) != HSA_STATUS_SUCCESS || !Bytes)
+    return false;
+  *Size = (uptr)Bytes;
+  return true;
+}
+
+// Requires loader extension API 1.04.
+bool Offload::LoadCodeObjectAt(hsa_executable_t Exec, hsa_agent_t Agent,
+                               hsa_code_object_reader_t Reader,
+                               const char* Options, void* Address, uptr Size,
+                               hsa_loaded_code_object_t* Loaded) const {
+  DCHECK(Loader.LoadAtAddress);
+  return Loader.LoadAtAddress &&
+         Loader.LoadAtAddress(Exec, Agent, Reader, Options, Address, Size,
+                              Loaded) == HSA_STATUS_SUCCESS;
+}
+
+// Reserve exactly [Addr, Addr + Size), or nothing.
+bool Offload::ReserveAddress(uptr Addr, uptr Size) {
+  // The HSA call here is allowed to return an HSA_STATUS_SUCCESS if it failed
+  // to reserve the specified range. The sanitizer requires an exact size, so we
+  // need to detect this and return it as a failure..
+  auto Exact = [&](u64 Flags, uptr Align) {
+    void* Va = nullptr;
+    if (!Align || Api.hsa_amd_vmem_address_reserve_align(
+                      &Va, Size, Addr, Align, Flags) != HSA_STATUS_SUCCESS)
+      return false;
+    if (reinterpret_cast<uptr>(Va) == Addr)
+      return true;
+    Api.hsa_amd_vmem_address_free(Va, Size);
+    return false;
+  };
+  return Exact(HSA_AMD_VMEM_ADDRESS_NO_REGISTER, GetPageSizeCached()) ||
+         Exact(0, Addr & -Addr);
+}
+
+void Offload::FreeAddress(uptr Addr, uptr Size) {
+  Api.hsa_amd_vmem_address_free(reinterpret_cast<void*>(Addr), Size);
+}
+
+bool Offload::CreateHandle(hsa_amd_memory_pool_t Pool, uptr Size, u64 Flags,
+                           u64* Handle) {
+  hsa_amd_vmem_alloc_handle_t H;
+  if (Api.hsa_amd_vmem_handle_create(Pool, Size, MEMORY_TYPE_NONE, Flags, &H) !=
+      HSA_STATUS_SUCCESS)
+    return false;
+  *Handle = H.handle;
+  return true;
+}
+
+void Offload::ReleaseHandle(u64 Handle) {
+  Api.hsa_amd_vmem_handle_release(hsa_amd_vmem_alloc_handle_t{Handle});
+}
+
+bool Offload::Map(uptr Addr, uptr Size, uptr Offset, u64 Handle) {
+  return Api.hsa_amd_vmem_map(reinterpret_cast<void*>(Addr), Size, Offset,
+                              hsa_amd_vmem_alloc_handle_t{Handle},
+                              0) == HSA_STATUS_SUCCESS;
+}
+
+void Offload::Unmap(uptr Addr, uptr Size) {
+  Api.hsa_amd_vmem_unmap(reinterpret_cast<void*>(Addr), Size);
+}
+
+bool Offload::SetAccess(uptr Addr, uptr Size,
+                        const hsa_amd_memory_access_desc_t* Desc, uptr Count) {
+  return Api.hsa_amd_vmem_set_access(reinterpret_cast<void*>(Addr), Size, Desc,
+                                     Count) == HSA_STATUS_SUCCESS;
+}
+
+// One queue per GPU, at the agent's minimum depth, and one executable
+// fine-grained buffer. Both stay until shutdown. A store fills the buffer
+// and rings the queue.
+bool Offload::CreateCommands() {
+  const MemoryPool* Fine = nullptr;
+  for (const MemoryPool& P : PoolList) {
+    if (IsCpu(P.Agent) && P.HostAccess &&
+        (P.Flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED)) {
+      Fine = &P;
+      break;
+    }
+  }
+  if (!Fine ||
+      !Alloc(*Fine, kPm4IbBytes, &Ib, HSA_AMD_MEMORY_POOL_EXECUTABLE_FLAG)) {
+    Report("%s: device write buffer unavailable\n", SanitizerToolName);
+    return false;
+  }
+  for (Device& D : DeviceList) {
+    u32 Packets = 0;
+    if (Api.hsa_agent_get_info(D.Agent, HSA_AGENT_INFO_QUEUE_MIN_SIZE,
+                               &Packets) != HSA_STATUS_SUCCESS ||
+        !Packets ||
+        Api.hsa_queue_create(D.Agent, Packets, HSA_QUEUE_TYPE_SINGLE, nullptr,
+                             nullptr, 0, 0, &D.Queue) != HSA_STATUS_SUCCESS ||
+        !CreateSignal(&D.Signal)) {
+      if (D.Queue) {
+        Api.hsa_queue_destroy(D.Queue);
+        D.Queue = nullptr;
+      }
+      D.Signal = {};
+      Report("%s: device write queue unavailable\n", SanitizerToolName);
+      return false;
+    }
+  }
+  return true;
+}
+
+void Offload::SubmitIb(Device& D, u32 Dwords) {
+  AmdAqlPm4Ib Pkt = {};
+  Pkt.VenHdr = 1;
+  uptr Base = reinterpret_cast<uptr>(Ib);
+  // The AQL firmware rejects the compute bit on this jump.
+  Pkt.IbJump[0] = Pm4Header(0x3F, 4) & ~(1u << 1);
+  Pkt.IbJump[1] = static_cast<u32>(Base);
+  Pkt.IbJump[2] = static_cast<u32>(Base >> 32);
+  Pkt.IbJump[3] = (Dwords & 0xFFFFFu) | (1u << 23);
+  Pkt.DwCntRemain = 0xA;
+  Pkt.Completion = D.Signal;
+
+  Api.hsa_signal_store_screlease(D.Signal, 1);
+  u64 Index = Api.hsa_queue_add_write_index_screlease(D.Queue, 1);
+  auto* Slot = static_cast<u8*>(D.Queue->base_address) +
+               (Index % D.Queue->size) * sizeof(Pkt);
+  internal_memcpy(Slot + sizeof(u32), reinterpret_cast<u8*>(&Pkt) + sizeof(u32),
+                  sizeof(Pkt) - sizeof(u32));
+  __atomic_store_n(reinterpret_cast<u32*>(Slot),
+                   u32(Pkt.VenHdr) << 16 | Pkt.Header, __ATOMIC_RELEASE);
+  Api.hsa_signal_store_screlease(D.Queue->doorbell_signal, Index);
+}
+
+void Offload::Await(Device& D) {
+  // The runtime may wake before the value meets the condition.
+  while (Api.hsa_signal_wait_scacquire(D.Signal, HSA_SIGNAL_CONDITION_EQ, 0,
+                                       UINT64_MAX,
+                                       HSA_WAIT_STATE_BLOCKED) != 0);
+}
+
+// Publish a metadata write to the location. This is done through a direct write
+// from the command processor to ensure cache coherence without using a fence.
+bool Offload::PublishDeviceBytes(uptr Addr, const void* Src, uptr Bytes,
+                                 hsa_agent_t Agent) {
+  Lock L(&OffloadMtx);
+  if (!Bytes)
+    return true;
+  if (!Ready() || !Ib || (Addr % sizeof(u64)) || (Bytes % sizeof(u64))) {
+    Report("ERROR: %s: device write rejected\n", SanitizerToolName);
+    Die();
+  }
+
+  Device* Target = nullptr;
+  for (Device& D : DeviceList)
+    if (D.Agent.handle == Agent.handle)
+      Target = &D;
+  if (!Target || !Target->Queue) {
+    Report("ERROR: %s: device write queue unavailable\n", SanitizerToolName);
+    Die();
+  }
+
+  for (uptr Off = 0; Off < Bytes;) {
+    u32* Out = static_cast<u32*>(Ib);
+    u32 At = 0;
+    while (Off < Bytes) {
+      uptr Step = Min(Bytes - Off, kWriteChunkBytes);
+      u32 Packet = static_cast<u32>(Step / sizeof(u32)) + 4;
+      if (At + Packet > kIbDwords)
+        break;
+      const void* Part = Src ? static_cast<const u8*>(Src) + Off : nullptr;
+      At += EmitWriteData(Out + At, Addr + Off, Part, Step);
+      Off += Step;
+    }
+    CHECK_LE(At, kIbDwords);
+    CHECK_LE(At, 0xFFFFFu);
+    SubmitIb(*Target, At);
+    Await(*Target);
+  }
+  return true;
+}
+
 // dlsym(RTLD_DEFAULT) follows load order. hsa_init resolving inside libhsa
 // means this runtime was loaded too late to interpose.
 void WarnIfHsaNotInterposed() {
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_offload.h b/compiler-rt/lib/sanitizer_common/sanitizer_offload.h
index 46b8f906ae3322..5c2384cc6b0a38 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_offload.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_offload.h
@@ -45,6 +45,86 @@ class Offload {
   SymbolizedStack* Symbolize(uptr PC);
   bool SymbolizeData(uptr Addr, DataInfo* Info);
 
+  // Informating relating to an HSA memory pool.
+  struct MemoryPool {
+    hsa_agent_t Agent;
+    hsa_amd_memory_pool_t Pool;
+    u32 Flags;
+    uptr Granule;
+    // Minimum the pool accepts. Granule may be the larger recommended block.
+    uptr AllocGranule;
+    bool HostAccess;
+  };
+
+  struct CodeObject {
+    hsa_agent_t Agent;
+    uptr LoadBase;
+    uptr LoadSize;
+    const void* Storage;
+    uptr StorageSize;
+  };
+
+  bool IsGpu(hsa_agent_t Agent) const;
+  const InternalMmapVectorNoCtor<hsa_agent_t>& Cpus() const { return CpuList; }
+  const InternalMmapVectorNoCtor<MemoryPool>& Pools() const { return PoolList; }
+
+  // Pool on Agent whose global flags intersect Match. Coarse-grain wins when
+  // several match.
+  bool PoolForAgent(hsa_agent_t Agent, u32 Match, MemoryPool* Out);
+  // Discovered record for this pool.
+  bool FindPool(hsa_amd_memory_pool_t Pool, MemoryPool* Out);
+  // First fine-grain pool on a non-GPU agent.
+  bool HostPool(MemoryPool* Out);
+
+  // Copies the discovered pools, then runs F without OffloadMtx held.
+  template <typename Fn>
+  void ForEachPool(Fn F) {
+    InternalMmapVector<MemoryPool> Copy;
+    {
+      Lock L(&OffloadMtx);
+      if (!Ready())
+        return;
+      for (const MemoryPool& P : PoolList) Copy.push_back(P);
+    }
+    for (const MemoryPool& P : Copy) F(P);
+  }
+
+  bool CanAccess(hsa_agent_t Agent, hsa_amd_memory_pool_t Pool) const;
+  bool Copy(void* Dst, const void* Src, uptr N) const;
+
+  bool FineGrainVmem() const;
+  bool ReserveAddress(uptr Addr, uptr Size);
+  void FreeAddress(uptr Addr, uptr Size);
+  bool CreateHandle(hsa_amd_memory_pool_t Pool, uptr Size, u64 Flags,
+                    u64* Handle);
+  void ReleaseHandle(u64 Handle);
+  bool Map(uptr Addr, uptr Size, uptr Offset, u64 Handle);
+  void Unmap(uptr Addr, uptr Size);
+  bool SetAccess(uptr Addr, uptr Size, const hsa_amd_memory_access_desc_t* Desc,
+                 uptr Count);
+
+  // Loader 1.04 can place a code object at a reserved address.
+  bool CanLoadAtAddress() const;
+  bool CodeObjectLoadSize(hsa_code_object_reader_t Reader, uptr* Size) const;
+  bool LoadCodeObjectAt(hsa_executable_t Exec, hsa_agent_t Agent,
+                        hsa_code_object_reader_t Reader, const char* Options,
+                        void* Address, uptr Size,
+                        hsa_loaded_code_object_t* Loaded) const;
+
+  // The callback runs without the offload lock held.
+  template <typename Fn>
+  void ForEachCodeObject(hsa_executable_t Exec, Fn F) {
+    VisitCodeObjects(
+        Exec,
+        [](const CodeObject& Obj, void* Arg) { (*static_cast<Fn*>(Arg))(Obj); },
+        &F);
+  }
+
+  // Confirmed L2 store of [Addr, Addr + Bytes) through the GPU that owns
+  // the pages. Src null stores zeros.
+  bool PublishDeviceBytes(uptr Addr, const void* Src, uptr Bytes,
+                          hsa_agent_t Agent);
+
  private:
   friend struct OffloadRpc;
 
@@ -54,9 +134,10 @@ class Offload {
 
   struct Device {
     hsa_agent_t Agent;
-    hsa_amd_memory_pool_t Pool;
     u32 Lanes;
     u32 MaxWaves;
+    hsa_queue_t* Queue;
+    hsa_signal_t Signal;
   };
 
   struct Api {
@@ -68,15 +149,15 @@ class Offload {
   bool Resolve();
   bool Discover();
   bool BindLoader();
+  bool CreateCommands();
   bool Release();
   void Teardown();
 
-  const Device& Host() const { return HostDevice; }
   const InternalMmapVectorNoCtor<Device>& Devices() const { return DeviceList; }
 
-  bool Alloc(const Device& D, uptr Bytes, void** Out);
+  bool IsCpu(hsa_agent_t Agent) const;
+  bool Alloc(const MemoryPool& Pool, uptr Bytes, void** Out, u64 Flags = 0);
   void Free(void* P);
-  bool Copy(void* Dst, const void* Src, uptr N);
   bool Lookup(hsa_executable_t Exec, const char* Name, hsa_agent_t Agent,
               u64* Addr);
   bool CreateSignal(hsa_signal_t* Out);
@@ -84,17 +165,28 @@ class Offload {
   void WaitSignal(hsa_signal_t Sig);
   void StoreSignal(hsa_signal_t Sig);
 
+  void SubmitIb(Device& D, u32 Dwords);
+  void Await(Device& D);
+
   void TrackImage(uptr LoadBase, uptr LoadSize, const void* Storage,
                   uptr StorageSize);
   void UntrackImage(uptr LoadBase);
   bool ExecutableInfo(hsa_loaded_code_object_t Obj,
                       hsa_ven_amd_loader_loaded_code_object_info_t Attr,
                       u64* Out);
+  bool DescribeCodeObject(hsa_loaded_code_object_t Obj, hsa_agent_t Agent,
+                          CodeObject* Out);
   template <typename Cb>
   void ForEachAgentObject(hsa_executable_t Exec, Cb F);
+  void VisitCodeObjects(hsa_executable_t Exec,
+                        void (*Fn)(const CodeObject&, void*), void* Arg);
+
+  u16 AmdExtMinor{};
+  void* Ib{};
 
-  Device HostDevice{};
   InternalMmapVectorNoCtor<Device> DeviceList{};
+  InternalMmapVectorNoCtor<hsa_agent_t> CpuList{};
+  InternalMmapVectorNoCtor<MemoryPool> PoolList{};
 
   // Serializes initialization and shutdown of the core HSA interface.
   Mutex LifetimeMtx{};
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_offload_hsa.h b/compiler-rt/lib/sanitizer_common/sanitizer_offload_hsa.h
index bae82ef22e07bd..7e39ee2acf865e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_offload_hsa.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_offload_hsa.h
@@ -20,6 +20,7 @@
 
 #define SANITIZER_HSA_FUNCTIONS(X)        \
   X(hsa_iterate_agents)                   \
+  X(hsa_system_get_info)                  \
   X(hsa_agent_get_info)                   \
   X(hsa_executable_get_symbol_by_name)    \
   X(hsa_executable_symbol_get_info)       \
@@ -28,12 +29,23 @@
   X(hsa_amd_memory_pool_free)             \
   X(hsa_amd_agents_allow_access)          \
   X(hsa_amd_memory_pool_get_info)         \
+  X(hsa_amd_agent_memory_pool_get_info)   \
   X(hsa_amd_agent_iterate_memory_pools)   \
   X(hsa_system_get_major_extension_table) \
+  X(hsa_queue_create)                     \
+  X(hsa_queue_destroy)                    \
+  X(hsa_queue_add_write_index_screlease)  \
   X(hsa_amd_signal_create)                \
   X(hsa_signal_destroy)                   \
   X(hsa_signal_store_screlease)           \
-  X(hsa_signal_wait_scacquire)
+  X(hsa_signal_wait_scacquire)            \
+  X(hsa_amd_vmem_address_reserve_align)   \
+  X(hsa_amd_vmem_address_free)            \
+  X(hsa_amd_vmem_handle_create)           \
+  X(hsa_amd_vmem_handle_release)          \
+  X(hsa_amd_vmem_map)                     \
+  X(hsa_amd_vmem_unmap)                   \
+  X(hsa_amd_vmem_set_access)
 
 extern "C" {
 
@@ -66,8 +78,15 @@ typedef void* hsa_file_t;
 #else
 typedef int hsa_file_t;
 #endif
+typedef struct hsa_amd_vmem_alloc_handle_s {
+  uint64_t handle;
+} hsa_amd_vmem_alloc_handle_t;
+typedef struct hsa_region_s {
+  uint64_t handle;
+} hsa_region_t;
 
 typedef int64_t hsa_signal_value_t;
+typedef uint32_t hsa_queue_type32_t;
 
 typedef enum {
   HSA_STATUS_SUCCESS = 0,
@@ -83,6 +102,7 @@ typedef enum {
 typedef enum {
   HSA_AGENT_INFO_NAME = 0,
   HSA_AGENT_INFO_WAVEFRONT_SIZE = 6,
+  HSA_AGENT_INFO_QUEUE_MIN_SIZE = 13,
   HSA_AGENT_INFO_DEVICE = 17,
 } hsa_agent_info_t;
 
@@ -92,19 +112,80 @@ typedef enum {
   HSA_AMD_MEMORY_POOL_INFO_SEGMENT = 0,
   HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS = 1,
   HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_ALLOWED = 5,
+  HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_GRANULE = 6,
+  HSA_AMD_MEMORY_POOL_INFO_RUNTIME_ALLOC_REC_GRANULE = 18,
 } hsa_amd_memory_pool_info_t;
 
 typedef enum {
+  HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT = 1,
   HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED = 2,
   HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_COARSE_GRAINED = 4,
+  HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_EXTENDED_SCOPE_FINE_GRAINED = 8,
 } hsa_amd_memory_pool_global_flag_t;
 
+// Flags hsa_amd_vmem_handle_create honors. Bit 0 is point-to-point fine grain.
+typedef enum {
+  HSA_AMD_MEMORY_POOL_PCIE_FLAG = 1,
+  HSA_AMD_MEMORY_POOL_CONTIGUOUS_FLAG = 1 << 1,
+  HSA_AMD_MEMORY_POOL_EXECUTABLE_FLAG = 1 << 2,
+  HSA_AMD_MEMORY_POOL_UNCACHED_FLAG = 1 << 3,
+} hsa_amd_memory_pool_flag_t;
+
+// Reserve the range without registering it. A later map is what the driver
+// sees.
+typedef enum {
+  HSA_AMD_VMEM_ADDRESS_NO_REGISTER = 1,
+} hsa_amd_vmem_address_reserve_flag_t;
+
+// hsa_amd_vmem_handle_create takes the pool flags. Executable is bit 2.
+typedef enum {
+  HSA_AMD_VMEM_HANDLE_CREATE_EXECUTABLE = 1 << 2,
+} hsa_amd_vmem_handle_create_flag_t;
+
+typedef enum {
+  HSA_AMD_SYSTEM_INFO_EXT_VERSION_MINOR = 0x208,
+} hsa_system_info_t;
+
+typedef enum {
+  HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED = 0,
+} hsa_amd_memory_pool_access_t;
+
+typedef enum {
+  HSA_AMD_AGENT_MEMORY_POOL_INFO_ACCESS = 0,
+} hsa_amd_agent_memory_pool_info_t;
+
+typedef enum {
+  MEMORY_TYPE_NONE = 0,
+  MEMORY_TYPE_PINNED = 1
+} hsa_amd_memory_type_t;
+
+// Read, write, and execute are independent bits.
+typedef enum {
+  HSA_ACCESS_PERMISSION_NONE = 0,
+  HSA_ACCESS_PERMISSION_RO = 1,
+  HSA_ACCESS_PERMISSION_WO = 2,
+  HSA_ACCESS_PERMISSION_RW = 3,
+  HSA_ACCESS_PERMISSION_EX = 4,
+  // HSA leaves the combination to the caller.
+  HSA_ACCESS_PERMISSION_RWX = 7,
+} hsa_access_permission_t;
+
+typedef struct hsa_amd_memory_access_desc_s {
+  hsa_access_permission_t permissions;
+  hsa_agent_t agent_handle;
+} hsa_amd_memory_access_desc_t;
+
+static_assert(sizeof(hsa_amd_memory_access_desc_t) == 16, "layout drift");
+static_assert(offsetof(hsa_amd_memory_access_desc_t, agent_handle) == 8,
+              "layout drift");
+
 typedef enum {
   HSA_AMD_AGENT_INFO_COMPUTE_UNIT_COUNT = 0xA002,
   HSA_AMD_AGENT_INFO_MAX_WAVES_PER_CU = 0xA00A,
 } hsa_amd_agent_info_t;
 
 typedef enum {
+  HSA_SIGNAL_CONDITION_EQ = 0,
   HSA_SIGNAL_CONDITION_NE = 1,
 } hsa_signal_condition_t;
 
@@ -121,8 +202,65 @@ typedef enum {
   HSA_EXECUTABLE_SYMBOL_INFO_NAME_LENGTH = 1,
   HSA_EXECUTABLE_SYMBOL_INFO_NAME = 2,
   HSA_EXECUTABLE_SYMBOL_INFO_VARIABLE_ADDRESS = 21,
+  HSA_EXECUTABLE_SYMBOL_INFO_KERNEL_OBJECT = 22,
 } hsa_executable_symbol_info_t;
 
+typedef enum { HSA_EXT_POINTER_TYPE_HSA = 1 } hsa_amd_pointer_type_t;
+
+typedef enum { HSA_QUEUE_TYPE_SINGLE = 1 } hsa_queue_type_t;
+
+typedef struct hsa_amd_pointer_info_s {
+  uint32_t size;
+  hsa_amd_pointer_type_t type;
+  void* agentBaseAddress;
+  void* hostBaseAddress;
+  size_t sizeInBytes;
+  void* userData;
+  hsa_agent_t agentOwner;
+  uint32_t global_flags;
+  bool registered;
+} hsa_amd_pointer_info_t;
+
+typedef struct hsa_queue_s {
+  hsa_queue_type32_t type;
+  uint32_t features;
+  void* base_address;
+  hsa_signal_t doorbell_signal;
+  uint32_t size;
+  uint32_t reserved1;
+  uint64_t id;
+} hsa_queue_t;
+
+static_assert(sizeof(hsa_amd_pointer_info_t) == 56, "layout drift");
+static_assert(offsetof(hsa_amd_pointer_info_t, sizeInBytes) == 24,
+              "layout drift");
+static_assert(offsetof(hsa_amd_pointer_info_t, global_flags) == 48,
+              "layout drift");
+static_assert(sizeof(hsa_queue_t) == 40, "layout drift");
+static_assert(offsetof(hsa_queue_t, doorbell_signal) == 16, "layout drift");
+
+typedef enum {
+  HSA_AMD_GPU_MEMORY_FAULT_EVENT = 0,
+} hsa_amd_event_type_t;
+
+typedef struct hsa_amd_gpu_memory_fault_info_s {
+  hsa_agent_t agent;
+  uint64_t virtual_address;
+  uint32_t fault_reason_mask;
+} hsa_amd_gpu_memory_fault_info_t;
+
+typedef struct hsa_amd_event_s {
+  hsa_amd_event_type_t event_type;
+  hsa_amd_gpu_memory_fault_info_t memory_fault;
+} hsa_amd_event_t;
+
+static_assert(sizeof(hsa_amd_event_t) == 32, "layout drift");
+static_assert(offsetof(hsa_amd_event_t, memory_fault.virtual_address) == 16,
+              "layout drift");
+
+typedef hsa_status_t (*hsa_amd_system_event_callback_t)(
+    const hsa_amd_event_t* event, void* data);
+
 typedef enum { HSA_EXTENSION_AMD_LOADER = 0x201 } hsa_extension_t;
 
 typedef enum {
@@ -160,7 +298,10 @@ struct hsa_ven_amd_loader_segment_descriptor_t {
 static_assert(sizeof(hsa_ven_amd_loader_segment_descriptor_t) == 64,
               "layout drift");
 
-// Version 1.04 of HSA_EXTENSION_AMD_LOADER. Slot order is the extension ABI.
+// Version 1.04 of HSA_EXTENSION_AMD_LOADER. Slots are appended, so an older
+// runtime fills the prefix and leaves the tail null. CreateFromFile and
+// IterateExecutables are the 1.02 and 1.03 slots; they keep the offsets honest
+// and are not called.
 struct LoaderApi {
   hsa_status_t (*QueryHostAddress)(const void*, const void**);
   hsa_status_t (*QuerySegmentDescriptors)(
@@ -173,21 +314,24 @@ struct LoaderApi {
   hsa_status_t (*GetCodeObjectInfo)(
       hsa_loaded_code_object_t, hsa_ven_amd_loader_loaded_code_object_info_t,
       void*);
-  hsa_status_t (*CreateCodeObjectReaderFromFile)(hsa_file_t, size_t, size_t,
-                                                 hsa_code_object_reader_t*);
+  hsa_status_t (*CreateFromFile)(hsa_file_t, size_t, size_t,
+                                 hsa_code_object_reader_t*);
   hsa_status_t (*IterateExecutables)(hsa_status_t (*)(hsa_executable_t, void*),
                                      void*);
-  hsa_status_t (*CodeObjectReaderGetLoadSize)(hsa_code_object_reader_t,
-                                              size_t*);
-  hsa_status_t (*LoadAgentCodeObjectAtAddress)(hsa_executable_t, hsa_agent_t,
-                                               hsa_code_object_reader_t,
-                                               const char*, void*, size_t,
-                                               hsa_loaded_code_object_t*);
+  hsa_status_t (*GetLoadSize)(hsa_code_object_reader_t, size_t*);
+  hsa_status_t (*LoadAtAddress)(hsa_executable_t, hsa_agent_t,
+                                hsa_code_object_reader_t, const char*, void*,
+                                size_t, hsa_loaded_code_object_t*);
 };
 static_assert(sizeof(LoaderApi) == 9 * sizeof(void*), "layout drift");
+static_assert(offsetof(LoaderApi, CreateFromFile) == 5 * sizeof(void*),
+              "layout drift");
+static_assert(offsetof(LoaderApi, GetLoadSize) == 7 * sizeof(void*),
+              "layout drift");
 
 hsa_status_t hsa_init(void);
 hsa_status_t hsa_shut_down(void);
+hsa_status_t hsa_system_get_info(hsa_system_info_t attribute, void* value);
 hsa_status_t hsa_iterate_agents(hsa_status_t (*callback)(hsa_agent_t, void*),
                                 void* data);
 hsa_status_t hsa_agent_get_info(hsa_agent_t agent, hsa_agent_info_t attribute,
@@ -214,6 +358,9 @@ hsa_status_t hsa_amd_memory_pool_free(void* ptr);
 hsa_status_t hsa_amd_memory_pool_get_info(hsa_amd_memory_pool_t memory_pool,
                                           hsa_amd_memory_pool_info_t attribute,
                                           void* value);
+hsa_status_t hsa_amd_agent_memory_pool_get_info(
+    hsa_agent_t agent, hsa_amd_memory_pool_t memory_pool,
+    hsa_amd_agent_memory_pool_info_t attribute, void* value);
 hsa_status_t hsa_amd_agent_iterate_memory_pools(
     hsa_agent_t agent, hsa_status_t (*callback)(hsa_amd_memory_pool_t, void*),
     void* data);
@@ -225,7 +372,45 @@ hsa_status_t hsa_system_get_major_extension_table(uint16_t extension,
                                                   uint16_t version_major,
                                                   size_t table_length,
                                                   void* table);
+hsa_status_t hsa_amd_vmem_address_reserve_align(void** va, size_t size,
+                                                uint64_t address,
+                                                uint64_t alignment,
+                                                uint64_t flags);
+hsa_status_t hsa_amd_vmem_address_free(void* va, size_t size);
+hsa_status_t hsa_amd_vmem_handle_create(hsa_amd_memory_pool_t pool, size_t size,
+                                        hsa_amd_memory_type_t type,
+                                        uint64_t flags,
+                                        hsa_amd_vmem_alloc_handle_t* handle);
+hsa_status_t hsa_amd_vmem_handle_release(hsa_amd_vmem_alloc_handle_t handle);
+hsa_status_t hsa_amd_vmem_map(void* va, size_t size, size_t in_offset,
+                              hsa_amd_vmem_alloc_handle_t handle,
+                              uint64_t flags);
+hsa_status_t hsa_amd_vmem_unmap(void* va, size_t size);
+hsa_status_t hsa_amd_vmem_set_access(void* va, size_t size,
+                                     const hsa_amd_memory_access_desc_t* desc,
+                                     size_t desc_cnt);
+hsa_status_t hsa_amd_pointer_info(const void* ptr, hsa_amd_pointer_info_t* info,
+                                  void* (*alloc)(size_t),
+                                  uint32_t* num_agents_accessible,
+                                  hsa_agent_t** accessible);
+hsa_status_t hsa_amd_register_system_event_handler(
+    hsa_amd_system_event_callback_t callback, void* data);
+hsa_status_t hsa_executable_load_agent_code_object(
+    hsa_executable_t executable, hsa_agent_t agent,
+    hsa_code_object_reader_t code_object_reader, const char* options,
+    hsa_loaded_code_object_t* loaded_code_object);
+hsa_status_t hsa_memory_allocate(hsa_region_t region, size_t size, void** ptr);
+hsa_status_t hsa_memory_free(void* ptr);
 hsa_status_t hsa_memory_copy(void* dst, const void* src, size_t size);
+hsa_status_t hsa_queue_create(hsa_agent_t agent, uint32_t size,
+                              hsa_queue_type32_t type,
+                              void (*callback)(hsa_status_t status,
+                                               hsa_queue_t* source, void* data),
+                              void* data, uint32_t private_segment_size,
+                              uint32_t group_segment_size, hsa_queue_t** queue);
+hsa_status_t hsa_queue_destroy(hsa_queue_t* queue);
+uint64_t hsa_queue_add_write_index_screlease(const hsa_queue_t* queue,
+                                             uint64_t value);
 hsa_status_t hsa_amd_signal_create(hsa_signal_value_t initial_value,
                                    uint32_t num_consumers,
                                    const hsa_agent_t* consumers,
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_offload_opcodes.h b/compiler-rt/lib/sanitizer_common/sanitizer_offload_opcodes.h
index 6285e4d527face..5e9d9311cb37a5 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_offload_opcodes.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_offload_opcodes.h
@@ -21,6 +21,7 @@
 enum {
   SANITIZER_OFFLOAD_UBSAN = SANITIZER_OFFLOAD_OPCODE(0),
   SANITIZER_OFFLOAD_CSAN = SANITIZER_OFFLOAD_OPCODE(1),
+  SANITIZER_OFFLOAD_DASAN = SANITIZER_OFFLOAD_OPCODE(2),
 };
 
 #undef SANITIZER_OFFLOAD_RPC_BASE
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_offload_rpc.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_offload_rpc.cpp
index e2c40197d3df7c..8bcb9933fb27de 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_offload_rpc.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_offload_rpc.cpp
@@ -201,8 +201,16 @@ void OffloadRpc::Start(Offload& O, hsa_executable_t Exec) {
       if (Slot.Ports > rpc::MAX_PORT_COUNT)
         Slot.Ports = rpc::MAX_PORT_COUNT;
       const uptr Bytes = rpc::Server::allocation_size(Slot.Lanes, Slot.Ports);
+      const Offload::MemoryPool* Fine = nullptr;
+      for (const Offload::MemoryPool& P : O.Pools()) {
+        if (O.IsCpu(P.Agent) && P.HostAccess &&
+            (P.Flags & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_FINE_GRAINED)) {
+          Fine = &P;
+          break;
+        }
+      }
       void* Buffer = nullptr;
-      if (!O.Alloc(O.Host(), Bytes, &Buffer) || !Buffer)
+      if (!Fine || !O.Alloc(*Fine, Bytes, &Buffer) || !Buffer)
         FailRpc();
       internal_memset(Buffer, 0, Bytes);
       PlantSignal(R, Buffer);
diff --git a/compiler-rt/test/Inputs/hip.h b/compiler-rt/test/Inputs/hip.h
index 1837b6581fa0cc..03083579c94bf9 100644
--- a/compiler-rt/test/Inputs/hip.h
+++ b/compiler-rt/test/Inputs/hip.h
@@ -24,7 +24,20 @@ typedef struct ihipStream_t *hipStream_t;
 
 int hipMalloc(void **Ptr, unsigned long Size);
 int hipFree(void *Ptr);
+int hipMemcpy(void *Dst, const void *Src, unsigned long Size, int Kind);
+int hipMemset(void *Ptr, int Value, unsigned long Size);
 int hipDeviceSynchronize(void);
+int hipGetLastError(void);
+int hipGetDeviceCount(int *Count);
+int hipSetDevice(int Device);
+int hipGetDevice(int *Device);
+
+int hipGetSymbolSize(unsigned long *Size, const void *Symbol);
+int hipGetSymbolAddress(void **Ptr, const void *Symbol);
+int hipMemcpyToSymbol(const void *Symbol, const void *Src, unsigned long Size,
+                      unsigned long Offset = 0, int Kind = 1);
+int hipMemcpyFromSymbol(void *Dst, const void *Symbol, unsigned long Size,
+                        unsigned long Offset = 0, int Kind = 2);
 
 int __hipPushCallConfiguration(dim3 GridDim, dim3 BlockDim,
                                unsigned long SharedMem = 0,
@@ -35,6 +48,9 @@ int hipLaunchKernel(const void *Func, dim3 GridDim, dim3 BlockDim, void **Args,
                     unsigned long SharedMem, hipStream_t Stream);
 
 int printf(const char *, ...);
+
+__device__ void *malloc(unsigned long);
+__device__ void free(void *);
 }
 
 __device__ inline int __hip_warp_size() {
@@ -74,6 +90,18 @@ __device__ inline int __shfl_xor(int Var, int LaneMask, int Width = warpSize) {
   return __builtin_amdgcn_wave_shuffle(Var, Index);
 }
 
+enum hipMemcpyKind { hipMemcpyHostToDevice = 1, hipMemcpyDeviceToHost = 2 };
+
+__device__ inline unsigned threadIdxX() {
+  return __builtin_amdgcn_workitem_id_x();
+}
+__device__ inline unsigned blockIdxX() {
+  return __builtin_amdgcn_workgroup_id_x();
+}
+__device__ inline void syncThreads() { __builtin_amdgcn_s_barrier(); }
+
+typedef float float4 __attribute__((ext_vector_type(4)));
+
 #define CHECK_HIP(Expr)                                                        \
   do {                                                                         \
     if ((Expr) != 0) {                                                         \
diff --git a/compiler-rt/test/dasan/AMDGPU/access-size.hip b/compiler-rt/test/dasan/AMDGPU/access-size.hip
new file mode 100644
index 00000000000000..abf2f60eddaf6f
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/access-size.hip
@@ -0,0 +1,96 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+
+__global__ void write_byte(char *P) { P[256] = 1; }
+__global__ void write_short(short *P) { P[128] = 1; }
+__global__ void write_long(long *P) { P[32] = 1; }
+__global__ void write_vector(float4 *P) { P[16] = float4(1.0f); }
+
+__global__ void bump(int *P, int I) {
+  __atomic_fetch_add(P + I, 1, __ATOMIC_RELAXED);
+}
+
+__global__ void claim(int *P, int I) {
+  int Want = 0;
+  __atomic_compare_exchange_n(P + I, &Want, 1, false, __ATOMIC_RELAXED,
+                              __ATOMIC_RELAXED);
+}
+
+__global__ void copy_into(char *Dst, const char *Src, unsigned long N) {
+  __builtin_memcpy(Dst + 128, Src, N);
+}
+
+__global__ void copy_out_of(char *Dst, const char *Src, unsigned long N) {
+  __builtin_memcpy(Dst, Src + 128, N);
+}
+
+__global__ void fill(char *Dst, unsigned long N) {
+  __builtin_memset(Dst + 128, 0x41, N);
+}
+
+int main() {
+  char *P = nullptr, *Q = nullptr;
+  CHECK_HIP(hipMalloc((void **)&P, 256));
+  CHECK_HIP(hipMalloc((void **)&Q, 256));
+
+  write_byte<<<1, 1>>>(P);
+  write_short<<<1, 1>>>((short *)P);
+  write_long<<<1, 1>>>((long *)P);
+  write_vector<<<1, 1>>>((float4 *)P);
+  bump<<<1, 1>>>((int *)P, 64);
+  claim<<<1, 1>>>((int *)P, 64);
+  copy_into<<<1, 1>>>(P, Q, 200);
+  copy_out_of<<<1, 1>>>(P, Q, 200);
+  fill<<<1, 1>>>(P, 200);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: WRITE of size 1 at
+// CHECK: in write_byte{{.*}}access-size.hip
+// CHECK: is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 2 at
+// CHECK: in write_short{{.*}}access-size.hip
+// CHECK: is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 8 at
+// CHECK: in write_long{{.*}}access-size.hip
+// CHECK: is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 16 at
+// CHECK: in write_vector{{.*}}access-size.hip
+// CHECK: is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 4 at
+// CHECK: in bump{{.*}}access-size.hip
+// CHECK: is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 4 at
+// CHECK: in claim{{.*}}access-size.hip
+// CHECK: is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 200 at
+// CHECK: in copy_into{{.*}}access-size.hip
+// CHECK: is located 128 bytes inside of 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: READ of size 200 at
+// CHECK: in copy_out_of{{.*}}access-size.hip
+// CHECK: is located 128 bytes inside of 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: WRITE of size 200 at
+// CHECK: in fill{{.*}}access-size.hip
+// CHECK: is located 128 bytes inside of 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/clean.hip b/compiler-rt/test/dasan/AMDGPU/clean.hip
new file mode 100644
index 00000000000000..956c318c1b7733
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/clean.hip
@@ -0,0 +1,32 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %run %t
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %run %t
+
+// Every kind of covered memory, touched only in bounds.
+__device__ int Global[16];
+
+__global__ void touch_all(int *Heap, int N) {
+  __shared__ int Shared[16];
+  extern __shared__ int Dyn[];
+  unsigned T = threadIdxX();
+
+  if (T < 16) {
+    Shared[T] = T;
+    Global[T] = T;
+  }
+  Dyn[T] = T;
+  syncThreads();
+
+  for (int I = T; I < N; I += 16)
+    Heap[I] = Shared[I % 16] + Global[I % 16] + Dyn[T];
+}
+
+int main() {
+  const int N = 256;
+  int *Heap = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Heap, N * sizeof(int)));
+  touch_all<<<1, 16, 16 * sizeof(int)>>>(Heap, N);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
diff --git a/compiler-rt/test/dasan/AMDGPU/device-malloc.hip b/compiler-rt/test/dasan/AMDGPU/device-malloc.hip
new file mode 100644
index 00000000000000..9340edda04bf65
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/device-malloc.hip
@@ -0,0 +1,33 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=MALLOC
+// RUN: not %run %t free 2>&1 | FileCheck %s --check-prefix=FREE
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=MALLOC
+// RUN: not %run %t free 2>&1 | FileCheck %s --check-prefix=FREE
+// RUN: %clang_hip_dasan -shared-libsan %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=MALLOC
+// RUN: not %run %t free 2>&1 | FileCheck %s --check-prefix=FREE
+
+__global__ void device_malloc() {
+  void *P = malloc(16);
+  (void)P;
+}
+
+__global__ void device_free() { free(nullptr); }
+
+int main(int argc, char **argv) {
+  if (argc > 1 && argv[1][0] == 'f')
+    device_free<<<1, 1>>>();
+  else
+    device_malloc<<<1, 1>>>();
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// MALLOC: ERROR: DeviceAddressSanitizer: device malloc is not supported
+// MALLOC: #0 {{0x[0-9a-f]+}} in device_malloc{{.*}}device-malloc.hip
+// MALLOC: SUMMARY: DeviceAddressSanitizer: unsupported-device-malloc
+
+// FREE: ERROR: DeviceAddressSanitizer: device free is not supported
+// FREE: #0 {{0x[0-9a-f]+}} in device_free{{.*}}device-malloc.hip
+// FREE: SUMMARY: DeviceAddressSanitizer: unsupported-device-free
diff --git a/compiler-rt/test/dasan/AMDGPU/flags.hip b/compiler-rt/test/dasan/AMDGPU/flags.hip
new file mode 100644
index 00000000000000..49d38746157a66
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/flags.hip
@@ -0,0 +1,44 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=alias_globals=0 %run %t global
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t heap 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CARRYON
+// RUN: %env_dasan_opts=print_stats=1 %run %t 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=STATS
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=alias_globals=0 %run %t global
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t heap 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CARRYON
+// RUN: %env_dasan_opts=print_stats=1 %run %t 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=STATS
+
+#include <stdio.h>
+
+__device__ int Table[16];
+
+__global__ void write_heap(int *P) { P[64] = 1; }
+
+__global__ void write_global(int I) { Table[I] = 1; }
+
+int main(int argc, char **argv) {
+  const char Mode = argc > 1 ? argv[1][0] : 0;
+  int *P = nullptr;
+  CHECK_HIP(hipMalloc((void **)&P, 64 * sizeof(int)));
+
+  if (Mode == 'h')
+    write_heap<<<1, 1>>>(P);
+  else if (Mode == 'g')
+    write_global<<<1, 1>>>(16);
+  CHECK_HIP(hipDeviceSynchronize());
+  fprintf(stderr, "synchronized\n");
+  return 0;
+}
+
+// The wave is already gone. The host carries on, and says so once.
+// CARRYON: WARNING: DeviceAddressSanitizer: halt_on_error=0 cannot resume
+// CARRYON: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// CARRYON: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+// CARRYON-NOT: HINT
+// CARRYON: synchronized
+
+// STATS: stats: {{[0-9]+}} placed ({{[0-9]+}} above their own class), {{[0-9]+}} freed
+// STATS: stats: {{[0-9]+}} code objects placed over {{[0-9]+}} chunks
diff --git a/compiler-rt/test/dasan/AMDGPU/global-bounds.hip b/compiler-rt/test/dasan/AMDGPU/global-bounds.hip
new file mode 100644
index 00000000000000..338eddab85c7cf
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/global-bounds.hip
@@ -0,0 +1,104 @@
+// REQUIRES: dasan-globals
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+
+__device__ int Before[256];
+__device__ int Table[256];
+__device__ int Pointee[16];
+__device__ int *Ptr;
+__constant__ int Konst[16] = {1};
+
+__device__ __attribute__((aligned(1024))) int Aligned[4];
+
+__device__ int Wide[1024];
+__device__ int Small[32];
+__device__ int Big[4096];
+
+constexpr long N = 4L << 20;
+__device__ int Huge[N];
+
+__global__ void read_before(int I) { Before[0] = Table[I]; }
+
+__global__ void publish() { Ptr = Pointee; }
+__global__ void write_indirect(int I) { Ptr[I] = 1; }
+
+__global__ void read_const(int *Out, int I) { *Out = Konst[I]; }
+
+__global__ void write_aligned(int I) { Aligned[I] = 1; }
+__global__ void write_wide(int I) { Wide[I] = 1; }
+__global__ void write_small(int I) { Small[I] = 1; }
+__global__ void write_big(int I) { Big[I] = 1; }
+
+__global__ void write_huge(int *Q, long Lo, long Hi) {
+  Q[Lo] = 1;
+  Q[Hi] = 2;
+}
+
+int main() {
+  int *Out = nullptr, *H = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+  CHECK_HIP(hipGetSymbolAddress((void **)&H, (const void *)&Huge));
+
+  read_before<<<1, 1>>>(-1);
+  publish<<<1, 1>>>();
+  write_indirect<<<1, 1>>>(16);
+  read_const<<<1, 1>>>(Out, 16);
+  write_aligned<<<1, 1>>>(3);
+  write_aligned<<<1, 1>>>(4);
+  write_wide<<<1, 1>>>(1023);
+  write_wide<<<1, 1>>>(1024);
+  write_small<<<1, 1>>>(31);
+  write_small<<<1, 1>>>(40);
+  write_big<<<1, 1>>>(4095);
+  write_big<<<1, 1>>>(4608);
+  write_huge<<<1, 1>>>(H + N - 16, -(N - 16), 15);
+  write_huge<<<1, 1>>>(H + N - 16, -(N - 16), 16);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-underflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: READ of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in read_before{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 4 bytes before global variable 'Table' {{.*}} of size 1024
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-underflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_indirect{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Pointee' {{.*}} of size 64
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: READ of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in read_const{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Konst' {{.*}} of size 64
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_aligned{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Aligned' {{.*}} of size 16
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_wide{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Wide' {{.*}} of size 4096
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_small{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 32 bytes after global variable 'Small' {{.*}} of size 128
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_big{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 2048 bytes after global variable 'Big' {{.*}} of size 16384
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_huge{{.*}}global-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Huge' {{.*}} of size 16777216
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/global-linkage.hip b/compiler-rt/test/dasan/AMDGPU/global-linkage.hip
new file mode 100644
index 00000000000000..065f4f75774f94
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/global-linkage.hip
@@ -0,0 +1,50 @@
+// REQUIRES: dasan-globals
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+// RUN: %clang_hip_dasan -O3 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+
+// COMDAT and internal globals are placed like any other.
+inline __device__ int Inline[16];
+inline __device__ char Tail[100];
+static __device__ int Local[16];
+
+__device__ __attribute__((noinline)) void fill(int *Begin, int *End) {
+  for (int *P = End; P != Begin;)
+    *--P = 1;
+}
+
+__global__ void fill_inline() { fill(Inline, Inline + 16); }
+
+__global__ void write_tail(int I) { Tail[I] = 1; }
+
+__global__ void write_local(int *Out, int I) {
+  Local[I] = 1;
+  *Out = Local[2];
+}
+
+int main() {
+  int *Out = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+
+  fill_inline<<<1, 1>>>();
+  write_tail<<<1, 1>>>(99);
+  write_tail<<<1, 1>>>(100);
+  write_local<<<1, 1>>>(Out, 16);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_tail{{.*}}global-linkage.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Tail' {{.*}} of size 100
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_local{{.*}}global-linkage.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable '{{.*}}Local' {{.*}} of size 64
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/global-overflow.hip b/compiler-rt/test/dasan/AMDGPU/global-overflow.hip
new file mode 100644
index 00000000000000..778b044cfd15bd
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/global-overflow.hip
@@ -0,0 +1,24 @@
+// REQUIRES: dasan-globals
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hip -Xarch_device -fsanitize=daddress -shared-libsan %s -o %t \
+// RUN:   %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+__device__ int Table[16];
+
+__global__ void write_past(int I) { Table[I] = 1; }
+
+int main() {
+  write_past<<<1, 1>>>(16);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_past{{.*}}global-overflow.hip
+// CHECK: [[ADDR]] is located 0 bytes after global variable 'Table' {{.*}} of size 64
+// CHECK: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/heap-bounds.hip b/compiler-rt/test/dasan/AMDGPU/heap-bounds.hip
new file mode 100644
index 00000000000000..5a48f074d14f04
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/heap-bounds.hip
@@ -0,0 +1,92 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+
+__global__ void read_past(int *P, int *Out, long I) { *Out = P[I]; }
+
+template <int> __global__ void write_at(int *P, long I) { P[I] = 1; }
+
+__global__ void write_across(double *P, int I) { P[I] = 1.0; }
+
+// The reported frame is the innermost function, not the kernel.
+__attribute__((noinline)) __device__ void store(int *P, long I) { P[I] = 1; }
+__global__ void call_store(int *P, long I) { store(P, I); }
+
+// Bounds come from the allocation, not the interior pointer it was handed.
+__global__ void walk(int *Q, int Hi) {
+#pragma clang loop unroll(disable)
+  for (int I = -32; I <= Hi; ++I)
+    Q[I] = I;
+}
+
+__global__ void write_byte(char *P) { *P = 1; }
+
+int main() {
+  int *A = nullptr, *B = nullptr, *C = nullptr, *Out = nullptr;
+  double *D = nullptr;
+  CHECK_HIP(hipMalloc((void **)&A, 60 * sizeof(int)));
+  CHECK_HIP(hipMalloc((void **)&B, 60 * sizeof(int)));
+  CHECK_HIP(hipMalloc((void **)&C, 64 * sizeof(int)));
+  CHECK_HIP(hipMalloc((void **)&D, 252));
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+  int *Lo = A < B ? A : B;
+  int *Hi = A < B ? B : A;
+
+  read_past<<<1, 1>>>(C, Out, 64);
+  write_at<0><<<1, 1>>>(C, -1);
+  write_across<<<1, 1>>>(D, 31);
+  write_at<1><<<1, 1>>>(A, -(1L << 20));
+  write_at<2><<<1, 1>>>(Lo, Hi - Lo);
+  call_store<<<1, 1>>>(C, 64);
+  walk<<<1, 1>>>(C + 32, 31);
+  walk<<<1, 1>>>(C + 32, 32);
+  write_byte<<<1, 1>>>((char *)C + (1 << 20));
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: READ of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in read_past{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-underflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}write_at{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located 4 bytes before 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-underflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 8 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_across{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located 248 bytes inside of 252-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-underflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}write_at{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located 4194304 bytes before 240-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-underflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}write_at{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located {{[0-9]+}} bytes after 240-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in store{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: #0 {{0x[0-9a-f]+}} in walk{{.*}}heap-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after 256-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: unknown-device-address on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_byte{{.*}}heap-bounds.hip
+// CHECK: SUMMARY: DeviceAddressSanitizer: unknown-device-address
diff --git a/compiler-rt/test/dasan/AMDGPU/heap-overflow.hip b/compiler-rt/test/dasan/AMDGPU/heap-overflow.hip
new file mode 100644
index 00000000000000..1d933c6143ac4b
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/heap-overflow.hip
@@ -0,0 +1,28 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -shared-libsan %s -o %t %hip_libs
+// RUN: llvm-readelf -d %t | FileCheck %s --check-prefix=NEEDED
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+__global__ void write_past(int *P, int I) { P[I] = 1; }
+
+int main() {
+  int *P = nullptr;
+  CHECK_HIP(hipMalloc((void **)&P, 64 * sizeof(int)));
+  write_past<<<1, 1>>>(P, 64);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]] thread (0,0,0) of block (0,0,0)
+// CHECK: #0 {{0x[0-9a-f]+}} in write_past{{.*}}heap-overflow.hip
+// CHECK: [[ADDR]] is located 0 bytes after 256-byte region
+// CHECK: allocated by thread T{{[0-9]+}} here:
+// CHECK: heap-overflow.hip
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+// CHECK: HINT: build with -fsanitize-recover=daddress
+
+// NEEDED: libclang_rt.dasan.so
diff --git a/compiler-rt/test/dasan/AMDGPU/host-overflow.hip b/compiler-rt/test/dasan/AMDGPU/host-overflow.hip
new file mode 100644
index 00000000000000..76368628bca59d
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/host-overflow.hip
@@ -0,0 +1,24 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+
+int main() {
+  int *P = nullptr;
+  CHECK_HIP(hipMalloc((void **)&P, 64 * sizeof(int)));
+  P[0] = 1;
+  if (P[0] != 1)
+    return 1;
+  P[64] = 1;
+  fprintf(stderr, "missed\n");
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in main {{.*}}host-overflow.hip
+// CHECK: [[ADDR]] is located 0 bytes after 256-byte region
+// CHECK: allocated by thread T{{[0-9]+}} here:
+// CHECK: host-overflow.hip
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+// CHECK: HINT: build with -fsanitize-recover=daddress
diff --git a/compiler-rt/test/dasan/AMDGPU/lit.local.cfg.py b/compiler-rt/test/dasan/AMDGPU/lit.local.cfg.py
new file mode 100644
index 00000000000000..868fe2933ba4e5
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/lit.local.cfg.py
@@ -0,0 +1,10 @@
+# Discover only tests for GPU frontends that are usable on this system.
+config.suffixes = []
+if "dasan-hip" in config.available_features:
+    config.suffixes.append(".hip")
+if "dasan-openmp-offload" in config.available_features:
+    config.suffixes += [".c", ".cpp"]
+if not config.suffixes:
+    config.unsupported = True
+else:
+    config.parallelism_group = "gpu"
diff --git a/compiler-rt/test/dasan/AMDGPU/module-unload.hip b/compiler-rt/test/dasan/AMDGPU/module-unload.hip
new file mode 100644
index 00000000000000..20dbd86386a88d
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/module-unload.hip
@@ -0,0 +1,44 @@
+// REQUIRES: dasan-globals
+// RUN: %clang_hip_dasan --offload-device-only %s -o %t.co
+// RUN: %clang_dasan -x c++ -DHOST %s -o %t %hip_libs
+// RUN: %run %t %t.co 2>&1 | FileCheck %s
+
+// Loading the same image again after unloading it must reuse the span it held.
+
+#ifdef HOST
+
+extern "C" {
+typedef struct ihipModule_t *hipModule_t;
+typedef struct ihipModuleSymbol_t *hipFunction_t;
+typedef struct ihipStream_t *hipStream_t;
+int hipModuleLoad(hipModule_t *Module, const char *File);
+int hipModuleUnload(hipModule_t Module);
+int hipModuleGetFunction(hipFunction_t *Function, hipModule_t Module,
+                         const char *Name);
+int hipModuleLaunchKernel(hipFunction_t Function, unsigned GridX,
+                          unsigned GridY, unsigned GridZ, unsigned BlockX,
+                          unsigned BlockY, unsigned BlockZ, unsigned SharedMem,
+                          hipStream_t Stream, void **Params, void **Extra);
+int hipDeviceSynchronize(void);
+}
+
+int main(int argc, char **argv) {
+  for (int N = 0; N < 2; ++N) {
+    hipModule_t Module = nullptr;
+    hipFunction_t Fn = nullptr;
+    if (hipModuleLoad(&Module, argv[1]) ||
+        hipModuleGetFunction(&Fn, Module, "touch") ||
+        hipModuleLaunchKernel(Fn, 1, 1, 1, 1, 1, 1, 0, 0, nullptr, nullptr) ||
+        hipDeviceSynchronize() || hipModuleUnload(Module))
+      return 2;
+  }
+  return 0;
+}
+
+#else
+extern "C" __global__ void touch() {}
+#endif
+
+// CHECK: placed {{0x[0-9a-f]+}} (+{{[0-9]+}}) at [[ADDR:0x[0-9a-f]+]]
+// CHECK: released {{0x[0-9a-f]+}} (+{{[0-9]+}}) at [[ADDR]]
+// CHECK: placed {{0x[0-9a-f]+}} (+{{[0-9]+}}) at [[ADDR]]
diff --git a/compiler-rt/test/dasan/AMDGPU/multi-device.hip b/compiler-rt/test/dasan/AMDGPU/multi-device.hip
new file mode 100644
index 00000000000000..df5f0cf7e3a4cc
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/multi-device.hip
@@ -0,0 +1,127 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs -pthread
+// RUN: %run %t
+// RUN: not %run %t heap 0 2>&1 | FileCheck %s --check-prefix=HEAP
+// RUN: not %run %t heap 1 2>&1 | FileCheck %s --check-prefix=HEAP
+// RUN: %if dasan-globals %{ not %run %t global 0 2>&1 | FileCheck %s --check-prefix=GLOBAL %}
+// RUN: %if dasan-globals %{ not %run %t global 1 2>&1 | FileCheck %s --check-prefix=GLOBAL %}
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs -pthread
+// RUN: %run %t
+// RUN: not %run %t heap 0 2>&1 | FileCheck %s --check-prefix=HEAP
+// RUN: not %run %t heap 1 2>&1 | FileCheck %s --check-prefix=HEAP
+// RUN: %if dasan-globals %{ not %run %t global 0 2>&1 | FileCheck %s --check-prefix=GLOBAL %}
+// RUN: %if dasan-globals %{ not %run %t global 1 2>&1 | FileCheck %s --check-prefix=GLOBAL %}
+
+// REQUIRES: multi-device
+
+#ifndef __HIP_DEVICE_COMPILE__
+#  include <pthread.h>
+#endif
+
+__device__ int Table[16];
+
+__global__ void fill(int *P, int N, int Tag) {
+  unsigned T = threadIdxX();
+  if ((int)T < N)
+    P[T] = Tag + (int)T;
+}
+
+__global__ void poke_heap(int *P, int I) { P[I] = 1; }
+
+__global__ void poke_global(int I) { Table[I] = 1; }
+
+#ifndef __HIP_DEVICE_COMPILE__
+
+static const int N = 64;
+
+struct Job {
+  int Device;
+  int Tag;
+  int *Live;
+  int Rc;
+};
+
+static int *Place(int Device) {
+  int *P = nullptr;
+  if (hipSetDevice(Device) || hipMalloc((void **)&P, N * sizeof(int)))
+    return nullptr;
+  return P;
+}
+
+static int Run(int Device, int *P, int Tag) {
+  if (hipSetDevice(Device))
+    return 2;
+  fill<<<1, N>>>(P, N, Tag);
+  return hipDeviceSynchronize();
+}
+
+static int Release(int Device, int *P) {
+  return hipSetDevice(Device) || hipFree(P);
+}
+
+static void *Work(void *Arg) {
+  Job *J = (Job *)Arg;
+  J->Live = Place(J->Device);
+  J->Rc = J->Live ? Run(J->Device, J->Live, J->Tag) : 2;
+  if (J->Live && Release(J->Device, J->Live))
+    J->Rc = 2;
+  return nullptr;
+}
+
+int main(int argc, char **argv) {
+  int Devices = 0;
+  CHECK_HIP(hipGetDeviceCount(&Devices));
+  if (Devices < 2)
+    return 2;
+
+  if (argc > 2) {
+    const int Dev = argv[2][0] - '0';
+    if (argv[1][0] == 'h') {
+      int *P = Place(Dev);
+      if (!P)
+        return 2;
+      poke_heap<<<1, 1>>>(P, N);
+    } else {
+      CHECK_HIP(hipSetDevice(Dev));
+      poke_global<<<1, 1>>>(16);
+    }
+    CHECK_HIP(hipDeviceSynchronize());
+    return 0;
+  }
+
+  // Each device on its own.
+  for (int D = 0; D < 2; ++D) {
+    Job J = {D, D + 1, nullptr, 0};
+    Work(&J);
+    if (J.Rc)
+      return 2;
+  }
+
+  // Both heaps live in the same size class at once.
+  int *A = Place(0);
+  int *B = Place(1);
+  if (!A || !B || Run(0, A, 10) || Run(1, B, 20) || Release(0, A) ||
+      Release(1, B))
+    return 2;
+
+  // One host thread per device, through the interceptors at once.
+  Job J0 = {0, 30, nullptr, 0};
+  Job J1 = {1, 40, nullptr, 0};
+  pthread_t T0, T1;
+  if (pthread_create(&T0, nullptr, Work, &J0) ||
+      pthread_create(&T1, nullptr, Work, &J1))
+    return 2;
+  if (pthread_join(T0, nullptr) || pthread_join(T1, nullptr))
+    return 2;
+  return J0.Rc || J1.Rc ? 2 : 0;
+}
+
+#endif
+
+// HEAP: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// HEAP: WRITE of size 4
+// HEAP: in poke_heap{{.*}}multi-device.hip
+// HEAP: is located 0 bytes after 256-byte region
+
+// GLOBAL: ERROR: DeviceAddressSanitizer: global-buffer-overflow
+// GLOBAL: in poke_global{{.*}}multi-device.hip
+// GLOBAL: is located 0 bytes after global variable 'Table' {{.*}} of size 64
diff --git a/compiler-rt/test/dasan/AMDGPU/openmp-overflow.cpp b/compiler-rt/test/dasan/AMDGPU/openmp-overflow.cpp
new file mode 100644
index 00000000000000..b95b7464d08b28
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/openmp-overflow.cpp
@@ -0,0 +1,49 @@
+// RUN: %clang_omp_offload_dasan %s -o %t
+// RUN: %run %t clean
+// RUN: not %run %t heap 2>&1 | FileCheck %s --check-prefix=HEAP
+// RUN: %if dasan-globals %{ %env_dasan_opts=halt_on_error=0 %run %t heap global 2>&1 | FileCheck %s --check-prefixes=HEAP,GLOBAL --implicit-check-not=SUMMARY %} %else %{ %env_dasan_opts=halt_on_error=0 %run %t heap 2>&1 | FileCheck %s --check-prefix=HEAP %}
+// RUN: %clang_omp_offload_dasan -O1 %s -o %t
+// RUN: %run %t clean
+// RUN: not %run %t heap 2>&1 | FileCheck %s --check-prefix=HEAP
+// RUN: %if dasan-globals %{ %env_dasan_opts=halt_on_error=0 %run %t heap global 2>&1 | FileCheck %s --check-prefixes=HEAP,GLOBAL --implicit-check-not=SUMMARY %} %else %{ %env_dasan_opts=halt_on_error=0 %run %t heap 2>&1 | FileCheck %s --check-prefix=HEAP %}
+// RUN: %clang_omp_offload_dasan -shared-libsan %s -o %t
+// RUN: not %run %t heap 2>&1 | FileCheck %s --check-prefix=HEAP
+
+#include <omp.h>
+
+#pragma omp begin declare target
+int Table[16];
+#pragma omp end declare target
+
+int main(int argc, char **argv) {
+  int Dev = omp_get_default_device();
+  int *P = (int *)omp_target_alloc(64 * sizeof(int), Dev);
+  if (!P)
+    return 2;
+
+  for (int A = 1; A < argc; ++A) {
+    int Past = argv[A][0] != 'c';
+    if (argv[A][0] != 'g') {
+#pragma omp target is_device_ptr(P) firstprivate(Past) device(Dev)
+      P[63 + Past] = 1;
+    }
+    if (argv[A][0] != 'h') {
+#pragma omp target firstprivate(Past) device(Dev)
+      Table[15 + Past] = 1;
+    }
+  }
+  omp_target_free(P, Dev);
+  return 0;
+}
+
+// HEAP: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// HEAP: WRITE of size 4 at [[ADDR]]
+// HEAP: #0 {{0x[0-9a-f]+}} in {{.*}}openmp-overflow.cpp
+// HEAP: [[ADDR]] is located 0 bytes after 256-byte region
+// HEAP: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// GLOBAL: ERROR: DeviceAddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// GLOBAL: WRITE of size 4 at [[ADDR]]
+// GLOBAL: #0 {{0x[0-9a-f]+}} in {{.*}}openmp-overflow.cpp
+// GLOBAL: [[ADDR]] is located 0 bytes after global variable 'Table' {{.*}} of size 64
+// GLOBAL: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/openmp-section-map.c b/compiler-rt/test/dasan/AMDGPU/openmp-section-map.c
new file mode 100644
index 00000000000000..f677aeb67c916a
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/openmp-section-map.c
@@ -0,0 +1,32 @@
+// RUN: %clang_omp_offload_dasan -O2 %s -o %t
+// RUN: %run %t
+// RUN: not %run %t overflow 2>&1 | FileCheck %s
+
+#include <omp.h>
+
+#define M 1000
+static int Data[8][M];
+
+int main(int argc, char **argv) {
+  int Overflow = argc > 1;
+
+#pragma omp parallel num_threads(8)
+  {
+    int P = omp_get_thread_num();
+    int N = Overflow ? M + 4096 : M;
+#pragma omp target map(tofrom : Data[P : 1][0 : M]) firstprivate(P, N)
+    {
+      for (int I = 0; I < N; ++I)
+        Data[P][I] = P + 1;
+    }
+  }
+
+  for (int P = 0; P < 8; ++P)
+    for (int I = 0; I < M; ++I)
+      if (Data[P][I] != P + 1)
+        return 1;
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// CHECK: WRITE of size 4
diff --git a/compiler-rt/test/dasan/AMDGPU/quarantine.hip b/compiler-rt/test/dasan/AMDGPU/quarantine.hip
new file mode 100644
index 00000000000000..eac59aa7ebec4f
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/quarantine.hip
@@ -0,0 +1,31 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: not %run %t held 2>&1 | FileCheck %s
+// RUN: %env_dasan_opts=quarantine_size_mb=0 %run %t reused
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: not %run %t held 2>&1 | FileCheck %s
+// RUN: %env_dasan_opts=quarantine_size_mb=0 %run %t reused
+
+__global__ void write_one(int *P) { P[0] = 1; }
+
+int main(int argc, char **argv) {
+  const bool Reuse = argv[1][0] == 'r';
+  void *A = nullptr, *B = nullptr;
+  CHECK_HIP(hipMalloc(&A, 256));
+  CHECK_HIP(hipFree(A));
+  CHECK_HIP(hipMalloc(&B, 256));
+  if ((A == B) != Reuse)
+    return 1;
+
+  write_one<<<1, 1>>>((int *)A);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_one{{.*}}quarantine.hip
+// CHECK: freed by thread T{{[0-9]+}} here:
+// CHECK: quarantine.hip
+// CHECK: previously allocated by thread T{{[0-9]+}} here:
+// CHECK: quarantine.hip
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-use-after-free
diff --git a/compiler-rt/test/dasan/AMDGPU/recover.hip b/compiler-rt/test/dasan/AMDGPU/recover.hip
new file mode 100644
index 00000000000000..199462cf23877f
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/recover.hip
@@ -0,0 +1,107 @@
+// RUN: %clang_hip_dasan -fsanitize-recover=daddress %s -o %t %hip_libs
+// RUN: %run %t repeat 2>&1 | FileCheck %s --check-prefix=REPEAT \
+// RUN:   --implicit-check-not=SUMMARY
+// RUN: %run %t wave 2>&1 | FileCheck %s --check-prefix=WAVE \
+// RUN:   --implicit-check-not=SUMMARY
+// RUN: %run %t lane 2>&1 | FileCheck %s --check-prefix=LANE
+// RUN: %if dasan-globals %{ %run %t global 2>&1 | FileCheck %s --check-prefix=GLOBAL %}
+// RUN: %run %t shared 2>&1 | FileCheck %s --check-prefix=SHARED
+// RUN: %clang_hip_dasan -fsanitize-recover=daddress -O1 %s -o %t %hip_libs
+// RUN: %run %t repeat 2>&1 | FileCheck %s --check-prefix=REPEAT \
+// RUN:   --implicit-check-not=SUMMARY
+// RUN: %run %t wave 2>&1 | FileCheck %s --check-prefix=WAVE \
+// RUN:   --implicit-check-not=SUMMARY
+// RUN: %run %t lane 2>&1 | FileCheck %s --check-prefix=LANE
+// RUN: %if dasan-globals %{ %run %t global 2>&1 | FileCheck %s --check-prefix=GLOBAL %}
+// RUN: %run %t shared 2>&1 | FileCheck %s --check-prefix=SHARED
+
+// A repeat of one program counter within a dispatch is reported once. The
+// stores still land, so the memory past a fresh allocation must be mapped.
+__global__ void walk(int *P, int N) {
+#pragma clang loop unroll(disable)
+  for (int I = 0; I < N; ++I)
+    P[64 + I] = I;
+}
+
+__global__ void every_lane(int *P) { P[64 + threadIdxX()] = 1; }
+
+__global__ void one_lane(int *P) {
+  if (blockIdxX() == 2 && threadIdxX() == 33)
+    P[64] = 1;
+}
+
+__device__ int Table[16];
+
+__global__ void write_global(int *Out, int I) {
+  Table[I] = 1;
+  *Out = Table[0] + 1;
+}
+
+// Without LDS redzones the recovered write lands in the neighbouring object.
+__global__ void write_shared(int *Out, int I) {
+  __shared__ int Buf[16];
+  __shared__ int Other[16];
+  unsigned T = threadIdxX();
+  Buf[T] = T;
+  Other[T] = T;
+  syncThreads();
+  if (T == 0)
+    Buf[I] = 0x41;
+  syncThreads();
+  *Out = Buf[0] + Other[0] + 1;
+}
+
+int main(int argc, char **argv) {
+  int *P = nullptr, *Out = nullptr;
+  CHECK_HIP(hipMalloc((void **)&P, 64 * sizeof(int)));
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+
+  int Want = 0;
+  switch (argv[1][0]) {
+  case 'r':
+    walk<<<1, 1>>>(P, 200);
+    break;
+  case 'w':
+    every_lane<<<1, 32>>>(P);
+    break;
+  case 'l':
+    one_lane<<<4, 64>>>(P);
+    break;
+  case 'g':
+    write_global<<<1, 16>>>(Out, 16);
+    Want = 1;
+    break;
+  case 's':
+    write_shared<<<1, 16>>>(Out, 16);
+    Want = 66;
+    break;
+  }
+  CHECK_HIP(hipDeviceSynchronize());
+
+  int Got = 0;
+  CHECK_HIP(hipMemcpy(&Got, Out, sizeof(Got), hipMemcpyDeviceToHost));
+  return Want && Got != Want;
+}
+
+// REPEAT: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// REPEAT: in walk{{.*}}recover.hip
+// REPEAT: is located 0 bytes after 256-byte region
+// REPEAT: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// WAVE: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// WAVE: WRITE of size 4 at {{0x[0-9a-f]+}} thread ({{[0-9]+}},0,0) of block (0,0,0)
+// WAVE: in every_lane{{.*}}recover.hip
+// WAVE: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+
+// LANE: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// LANE: WRITE of size 4 at {{0x[0-9a-f]+}} thread (33,0,0) of block (2,0,0)
+// LANE: in one_lane{{.*}}recover.hip
+// LANE: is located 0 bytes after 256-byte region
+
+// GLOBAL: ERROR: DeviceAddressSanitizer: global-buffer-overflow
+// GLOBAL: in write_global{{.*}}recover.hip
+// GLOBAL: is located 0 bytes after global variable 'Table' {{.*}} of size 64
+
+// SHARED: ERROR: DeviceAddressSanitizer: shared-buffer-overflow
+// SHARED: in write_shared{{.*}}recover.hip
+// SHARED: is located 0 bytes after 64-byte region
diff --git a/compiler-rt/test/dasan/AMDGPU/report-location.hip b/compiler-rt/test/dasan/AMDGPU/report-location.hip
new file mode 100644
index 00000000000000..4bad9ea8977886
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/report-location.hip
@@ -0,0 +1,56 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -O2 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -fsanitize-recover=daddress -O2 %s -o %t %hip_libs
+// RUN: %run %t 2>&1 | FileCheck %s
+
+__device__ inline __attribute__((always_inline)) void put(int *P, long I) {
+  P[I] = 0; // CHECK-DAG: #0 {{.*}} in put{{.*}}report-location.hip:[[@LINE]]:
+}
+
+#define AT(K) (W == K ? N : 0)
+
+__global__ void sites(int *A, int *B, int *C, long W, long N, long Trip) {
+  put(A, AT(0));    // CHECK-DAG: #1 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  A[AT(1)] = 1;     // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  B[AT(2)] = 2;     // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  int X = A[AT(3)]; // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  int Y = B[AT(4)]; // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  A[AT(5) + 1] = X; // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  if (threadIdxX() == 0) {
+    A[AT(6) + 2] = 6; // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+    B[AT(7) + 2] = 7; // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  }
+#pragma clang loop unroll(disable)
+  for (long I = 0; I < Trip; ++I)
+    C[I * AT(8)] = I;   // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+  C[AT(9) + 1] = X + Y; // CHECK-DAG: #0 {{.*}} in sites{{.*}}.hip:[[@LINE]]:
+}
+
+// Identical checks on sibling paths are kept apart only by nomerge. The sleeps
+// stop the stores from being sunk into one before instrumentation.
+__global__ void twins(int *P, long I, long W) {
+  if (W == 0) {
+    P[I] = 0; // CHECK-DAG: #0 {{.*}} in twins{{.*}}.hip:[[@LINE]]:
+    __builtin_amdgcn_s_sleep(1);
+  } else {
+    P[I] = 1; // CHECK-DAG: #0 {{.*}} in twins{{.*}}.hip:[[@LINE]]:
+    __builtin_amdgcn_s_sleep(2);
+  }
+}
+
+int main() {
+  const long N = 60;
+  int *A = nullptr, *B = nullptr, *C = nullptr;
+  CHECK_HIP(hipMalloc((void **)&A, N * sizeof(int)));
+  CHECK_HIP(hipMalloc((void **)&B, N * sizeof(int)));
+  CHECK_HIP(hipMalloc((void **)&C, N * sizeof(int)));
+
+  for (long W = 0; W < 10; ++W)
+    sites<<<1, 1>>>(A, B, C, W, N, 2);
+  for (long W = 0; W < 2; ++W)
+    twins<<<1, 1>>>(C, N, W);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
diff --git a/compiler-rt/test/dasan/AMDGPU/shared-bounds.hip b/compiler-rt/test/dasan/AMDGPU/shared-bounds.hip
new file mode 100644
index 00000000000000..4cd520e694856c
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/shared-bounds.hip
@@ -0,0 +1,112 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+
+// Group objects are measured against their own extent, not the segment's.
+__global__ void write_past(int *Out, int I) {
+  __shared__ int Buf[16];
+  __shared__ int Other[16];
+  unsigned T = threadIdxX();
+  Buf[T] = T;
+  Other[T] = 0;
+  syncThreads();
+  if (T == 0)
+    Buf[I] = 0x41;
+  syncThreads();
+  *Out = Buf[0] + Other[0];
+}
+
+__global__ void write_before(int *Out, int I) {
+  __shared__ int First[16];
+  __shared__ int Second[16];
+  unsigned T = threadIdxX();
+  First[T] = T;
+  Second[T] = T;
+  syncThreads();
+  if (T == 0)
+    Second[I] = 0x41;
+  syncThreads();
+  *Out = First[0] + Second[0];
+}
+
+// The first object sits at offset 0, so a negative index wraps the 32-bit LDS
+// address. Needs to be reported as underflow.
+__global__ void write_under_zero(int *Out, int I) {
+  __shared__ int Buf[4];
+  Buf[0] = 1;
+  Buf[I] = 9;
+  *Out = Buf[0];
+}
+
+// The extent of dynamic group memory comes from the dispatch packet.
+__global__ void write_dynamic(int *Out, int I) {
+  extern __shared__ int Dyn[];
+  unsigned T = threadIdxX();
+  Dyn[T] = T;
+  syncThreads();
+  if (T == 0)
+    Dyn[I] = 0x41;
+  syncThreads();
+  *Out = Dyn[0];
+}
+
+// TODO: An LDS pointer that escapes into a callee is checked as heap, which
+// does not cover the group segment, so these are not reported.
+__device__ __attribute__((noinline)) void poke(int *P, int I) { P[I] = 0x41; }
+__device__ __attribute__((noinline)) void poke0(int *P) { *P = 0x41; }
+
+__global__ void escape(int *Out, int I) {
+  __shared__ int Buf[16];
+  __shared__ int Other[16];
+  unsigned T = threadIdxX();
+  Buf[T] = T;
+  Other[T] = 0;
+  syncThreads();
+  if (T == 0) {
+    poke(Buf, I);
+    poke0(Buf + I);
+  }
+  syncThreads();
+  *Out = Buf[0] + Other[0];
+}
+
+int main() {
+  int *Out = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+
+  write_past<<<1, 16>>>(Out, 16);
+  write_before<<<1, 16>>>(Out, -1);
+  write_under_zero<<<1, 1>>>(Out, -1);
+  write_dynamic<<<1, 8, 8 * sizeof(int)>>>(Out, 8);
+  escape<<<1, 16>>>(Out, 16);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: shared-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_past{{.*}}shared-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after 64-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: shared-buffer-overflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: shared-buffer-underflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_before{{.*}}shared-bounds.hip
+// CHECK: [[ADDR]] is located 4 bytes before 64-byte region
+// CHECK: the region is group memory
+// CHECK: SUMMARY: DeviceAddressSanitizer: shared-buffer-underflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: shared-buffer-underflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_under_zero{{.*}}shared-bounds.hip
+// CHECK: [[ADDR]] is located 4 bytes before 16-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: shared-buffer-underflow
+
+// CHECK: ERROR: DeviceAddressSanitizer: shared-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in write_dynamic{{.*}}shared-bounds.hip
+// CHECK: [[ADDR]] is located 0 bytes after 32-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: shared-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/shared-overlay.hip b/compiler-rt/test/dasan/AMDGPU/shared-overlay.hip
new file mode 100644
index 00000000000000..46a7cbd59f76e5
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/shared-overlay.hip
@@ -0,0 +1,34 @@
+// RUN: %clang_hip_dasan --offload-device-only --no-gpu-bundle-output \
+// RUN:   -foffload-lto %s -o %t.elf
+// RUN: llvm-readelf --notes %t.elf | FileCheck %s \
+// RUN:   --implicit-check-not='group_segment_fixed_size: {{[23][0-9][0-9][0-9][0-9]}}'
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %run %t
+
+#define N 16
+
+__global__ void k1(float *Out, int I) {
+  __shared__ float S[N][N][N];
+  float *P = &S[0][0][0];
+  P[I] = 1.f;
+  *Out = S[0][0][0];
+}
+
+__global__ void k2(float *Out, int I) {
+  __shared__ float T[N][N][N];
+  float *P = &T[0][0][0];
+  P[I] = 2.f;
+  *Out = T[0][0][0];
+}
+
+int main() {
+  float *Out = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(float)));
+  k1<<<1, 1>>>(Out, 0);
+  k2<<<1, 1>>>(Out, 0);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: .group_segment_fixed_size: 16384
+// CHECK: .group_segment_fixed_size: 16384
diff --git a/compiler-rt/test/dasan/AMDGPU/size-classes.hip b/compiler-rt/test/dasan/AMDGPU/size-classes.hip
new file mode 100644
index 00000000000000..360a650c32144b
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/size-classes.hip
@@ -0,0 +1,49 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 \
+// RUN:   | FileCheck %s --implicit-check-not=SUMMARY
+
+template <int> __global__ void touch(char *P, long I) { P[I] = 1; }
+
+static const long Sizes[] = {16, 1000, 4096, 100000};
+
+template <int Id> static void Touch(char *P, long N) {
+  touch<Id><<<1, 1>>>(P, N - 1);
+  touch<Id><<<1, 1>>>(P, N);
+}
+
+int main() {
+  int Id = 0;
+  for (long N : Sizes) {
+    char *P = nullptr;
+    CHECK_HIP(hipMalloc((void **)&P, N));
+    if ((unsigned long)P % 256)
+      return 1;
+    if (Id == 0)
+      Touch<0>(P, N);
+    else if (Id == 1)
+      Touch<1>(P, N);
+    else if (Id == 2)
+      Touch<2>(P, N);
+    else
+      Touch<3>(P, N);
+    ++Id;
+  }
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}touch{{.*}}size-classes.hip
+// CHECK: is located 0 bytes after 16-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}touch{{.*}}size-classes.hip
+// CHECK: is located 0 bytes after 1000-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}touch{{.*}}size-classes.hip
+// CHECK: is located 0 bytes after 4096-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
+// CHECK: #0 {{0x[0-9a-f]+}} in {{.*}}touch{{.*}}size-classes.hip
+// CHECK: is located 0 bytes after 100000-byte region
+// CHECK: SUMMARY: DeviceAddressSanitizer: heap-buffer-overflow
diff --git a/compiler-rt/test/dasan/AMDGPU/stress.hip b/compiler-rt/test/dasan/AMDGPU/stress.hip
new file mode 100644
index 00000000000000..414981809406a3
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/stress.hip
@@ -0,0 +1,127 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %run %t
+// RUN: not %run %t trip 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %run %t
+// RUN: not %run %t trip 2>&1 | FileCheck %s
+// RUN: %clang_hip_dasan -fsanitize-recover=daddress %s -o %t %hip_libs
+// RUN: %run %t storm 2>&1 | FileCheck %s --check-prefix=STORM
+// RUN: %clang_hip_dasan -fsanitize-recover=daddress -O1 %s -o %t %hip_libs
+// RUN: %run %t storm 2>&1 | FileCheck %s --check-prefix=STORM
+
+__device__ int Global[1024];
+__constant__ int Konst[64] = {1};
+
+__global__ void sweep(char *P, unsigned long N, unsigned long Stride,
+                      unsigned long long *Sum) {
+  unsigned long T = threadIdxX() + (unsigned long)blockIdxX() * 256;
+  unsigned long long Acc = 0;
+  for (unsigned long I = T; I < N; I += Stride) {
+    P[I] = (char)(I + 1);
+    Acc += (unsigned char)P[I];
+  }
+  __atomic_fetch_add(Sum, Acc, __ATOMIC_RELAXED);
+}
+
+__global__ void shuffle(char *Dst, const char *Src, unsigned long N) {
+  if (threadIdxX() == 0) {
+    __builtin_memset(Dst, 0x41, N);
+    __builtin_memcpy(Dst, Src, N);
+  }
+}
+
+__global__ void locals(unsigned long long *Sum) {
+  __shared__ int Buf[256];
+  extern __shared__ int Dyn[];
+  unsigned T = threadIdxX();
+
+  Buf[T] = T;
+  Dyn[T] = T;
+  Global[T + 256 * blockIdxX()] = T;
+  syncThreads();
+
+  unsigned long long Acc = Buf[(T + 1) % 256] + Dyn[(T + 1) % 256] +
+                           Global[T + 256 * blockIdxX()] + Konst[T % 64];
+  __atomic_fetch_add(Sum, Acc, __ATOMIC_RELAXED);
+}
+
+__global__ void around(int *Q, int Half) {
+#pragma clang loop unroll(disable)
+  for (int I = -Half; I < Half; ++I)
+    Q[I] = I;
+}
+
+__global__ void poke(char *P, long I) { P[I] = 1; }
+
+static const unsigned long Sizes[] = {16,   64,   252,   256,
+                                      1000, 4096, 33000, 100000};
+static const int NumSizes = sizeof(Sizes) / sizeof(Sizes[0]);
+
+int main(int argc, char **argv) {
+  const char Mode = argc > 1 ? argv[1][0] : 0;
+
+  unsigned long long *Sum = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Sum, sizeof(*Sum)));
+  CHECK_HIP(hipMemset(Sum, 0, sizeof(*Sum)));
+
+  char *Live[NumSizes] = {nullptr};
+  unsigned long long Want = 0;
+  const int Rounds = Mode ? 2 : 4;
+
+  for (int R = 0; R < Rounds; ++R) {
+    for (int S = 0; S < NumSizes; ++S) {
+      const unsigned long N = Sizes[S];
+      char *P = nullptr;
+      CHECK_HIP(hipMalloc((void **)&P, N));
+      if ((unsigned long)P % 256)
+        return 1;
+
+      const unsigned Blocks = N > 4096 ? 8 : 2;
+      sweep<<<Blocks, 256>>>(P, N, 256UL * Blocks, Sum);
+      for (unsigned long I = 0; I < N; ++I)
+        Want += (unsigned char)(I + 1);
+
+      if (Live[S]) {
+        shuffle<<<1, 1>>>(P, Live[S], N);
+        CHECK_HIP(hipFree(Live[S]));
+        Live[S] = nullptr;
+      }
+
+      if (N >= 256)
+        around<<<1, 1>>>((int *)P + N / 8, (int)(N / 8));
+
+      // Half are kept for the next round so every round both frees and holds.
+      if (S & 1)
+        Live[S] = P;
+      else
+        CHECK_HIP(hipFree(P));
+    }
+
+    locals<<<4, 256, 256 * sizeof(int)>>>(Sum);
+    for (int T = 0; T < 4 * 256; ++T)
+      Want += (T % 256) + (T % 256) + (T % 256) + (T % 64 ? 0 : 1);
+  }
+  CHECK_HIP(hipDeviceSynchronize());
+
+  unsigned long long Got = 0;
+  CHECK_HIP(hipMemcpy(&Got, Sum, sizeof(Got), hipMemcpyDeviceToHost));
+  if (Got != Want)
+    return 1;
+
+  if (Mode == 't')
+    poke<<<1, 1>>>(Live[7], (long)Sizes[7]);
+  // The launches share a program counter, so the site is reported once.
+  if (Mode == 's')
+    for (int R = 0; R < 4; ++R)
+      poke<<<8, 64>>>(Live[5], (long)Sizes[5]);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// CHECK: ERROR: DeviceAddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]]
+// CHECK: #0 {{0x[0-9a-f]+}} in poke{{.*}}stress.hip
+// CHECK: [[ADDR]] is located 0 bytes after 100000-byte region
+
+// STORM: ERROR: DeviceAddressSanitizer: heap-buffer-overflow
+// STORM: is located 0 bytes after 4096-byte region
diff --git a/compiler-rt/test/dasan/AMDGPU/symbol-abi.hip b/compiler-rt/test/dasan/AMDGPU/symbol-abi.hip
new file mode 100644
index 00000000000000..7c7d5c97f79458
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/symbol-abi.hip
@@ -0,0 +1,57 @@
+// RUN: %clang_hip_dasan %s -o %t %hip_libs
+// RUN: %run %t | FileCheck %s
+// RUN: %clang_hip_dasan -O1 %s -o %t %hip_libs
+// RUN: %run %t | FileCheck %s
+
+// A placed global's symbol describes the object alone, not its padded storage,
+// so the host's view of its size and address is unchanged.
+__device__ int Table[16];
+
+__global__ void sum(int *Out) {
+  int S = 0;
+  for (int I = 0; I < 16; ++I)
+    S += Table[I];
+  *Out = S;
+}
+
+int main() {
+  unsigned long Size = 0;
+  CHECK_HIP(hipGetSymbolSize(&Size, (const void *)&Table));
+  printf("size %lu\n", Size);
+
+  void *Addr = nullptr;
+  CHECK_HIP(hipGetSymbolAddress(&Addr, (const void *)&Table));
+  printf("aligned %d\n", (int)((unsigned long)Addr % 64 == 0));
+
+  int Host[16];
+  for (int I = 0; I < 16; ++I)
+    Host[I] = I + 1;
+
+  CHECK_HIP(hipMemcpyToSymbol((const void *)&Table, Host, sizeof(Host)));
+  CHECK_HIP(hipMemcpyToSymbol((const void *)&Table, Host, sizeof(Host) / 2,
+                              sizeof(Host) / 2));
+  printf("refused %d\n",
+         (int)(hipMemcpyToSymbol((const void *)&Table, Host, Size + 1) != 0));
+  hipGetLastError();
+
+  int Back[8] = {0};
+  CHECK_HIP(hipMemcpyFromSymbol(Back, (const void *)&Table, sizeof(Back),
+                                sizeof(Back)));
+  printf("offset %d\n", (int)(Back[0] == 1 && Back[7] == 8));
+
+  int *Out = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+  sum<<<1, 1>>>(Out);
+  CHECK_HIP(hipDeviceSynchronize());
+
+  int Got = 0;
+  CHECK_HIP(hipMemcpy(&Got, Out, sizeof(Got), hipMemcpyDeviceToHost));
+  printf("sum %d\n", Got);
+  return 0;
+}
+
+// CHECK: size 64
+// CHECK: aligned 1
+// CHECK: refused 1
+// CHECK: offset 1
+// CHECK: sum 72
diff --git a/compiler-rt/test/dasan/AMDGPU/two-modules.hip b/compiler-rt/test/dasan/AMDGPU/two-modules.hip
new file mode 100644
index 00000000000000..cabebe435c5c10
--- /dev/null
+++ b/compiler-rt/test/dasan/AMDGPU/two-modules.hip
@@ -0,0 +1,46 @@
+// RUN: %clang_hip_dasan %s %S/../Inputs/second-module.hip -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s \
+// RUN:   --implicit-check-not=SUMMARY \
+// RUN:   %if dasan-globals %{ --check-prefixes=GLOBAL,SHARED %} %else %{ --check-prefix=SHARED %}
+// RUN: %clang_hip_dasan -O1 %s %S/../Inputs/second-module.hip -o %t %hip_libs
+// RUN: %env_dasan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s \
+// RUN:   --implicit-check-not=SUMMARY \
+// RUN:   %if dasan-globals %{ --check-prefixes=GLOBAL,SHARED %} %else %{ --check-prefix=SHARED %}
+
+// Everything the pass adds is per module, so two instrumented translation
+// units in one image must coexist and keep their own extents.
+extern __global__ void write_other(int I);
+extern __global__ void write_other_shared(int *Out, int I);
+
+__device__ int Mine[16];
+
+__global__ void write_mine(int I) { Mine[I] = 1; }
+
+int main() {
+  int *Out = nullptr;
+  CHECK_HIP(hipMalloc((void **)&Out, sizeof(int)));
+
+  write_mine<<<1, 1>>>(15);
+  write_other<<<1, 1>>>(15);
+  write_other_shared<<<1, 16>>>(Out, 15);
+  write_mine<<<1, 1>>>(16);
+  write_other<<<1, 1>>>(16);
+  write_other_shared<<<1, 16>>>(Out, 16);
+  CHECK_HIP(hipDeviceSynchronize());
+  return 0;
+}
+
+// GLOBAL: ERROR: DeviceAddressSanitizer: global-buffer-overflow
+// GLOBAL: in write_mine{{.*}}two-modules.hip
+// GLOBAL: is located 0 bytes after global variable 'Mine' {{.*}} of size 64
+// GLOBAL: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// GLOBAL: ERROR: DeviceAddressSanitizer: global-buffer-overflow
+// GLOBAL: in write_other{{.*}}second-module.hip
+// GLOBAL: is located 0 bytes after global variable 'Other' {{.*}} of size 64
+// GLOBAL: SUMMARY: DeviceAddressSanitizer: global-buffer-overflow
+
+// SHARED: ERROR: DeviceAddressSanitizer: shared-buffer-overflow
+// SHARED: in write_other_shared{{.*}}second-module.hip
+// SHARED: is located 0 bytes after 64-byte region
+// SHARED: SUMMARY: DeviceAddressSanitizer: shared-buffer-overflow
diff --git a/compiler-rt/test/dasan/CMakeLists.txt b/compiler-rt/test/dasan/CMakeLists.txt
new file mode 100644
index 00000000000000..c41b36dfa4bdd9
--- /dev/null
+++ b/compiler-rt/test/dasan/CMakeLists.txt
@@ -0,0 +1,56 @@
+set(DASAN_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
+
+# Checking globals requires HSA Loader API 1.04 to be availabe.
+include(CheckCSourceRuns)
+include(CMakePushCheckState)
+cmake_push_check_state()
+set(CMAKE_REQUIRED_LIBRARIES hsa-runtime64)
+check_c_source_runs([[
+#include <stdbool.h>
+#include <stdint.h>
+typedef uint32_t hsa_status_t;
+hsa_status_t hsa_init(void);
+hsa_status_t hsa_shut_down(void);
+hsa_status_t hsa_system_extension_supported(uint16_t, uint16_t, uint16_t,
+                                            bool *);
+int main(void) {
+  if (hsa_init())
+    return 1;
+  bool ok = false;
+  hsa_status_t st = hsa_system_extension_supported(0x201, 1, 4, &ok);
+  hsa_shut_down();
+  return st == 0 && ok ? 0 : 1;
+}
+]] DASAN_GLOBAL_SUPPORT)
+cmake_pop_check_state()
+if(DASAN_GLOBAL_SUPPORT)
+  set(DASAN_GLOBAL_SUPPORT True)
+else()
+  set(DASAN_GLOBAL_SUPPORT False)
+endif()
+
+set(DASAN_TESTSUITES)
+set(DASAN_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
+# Only the host half. A device test also links the copy of this runtime built
+# for the GPU, which another runtimes build produces and which nothing here can
+# depend on, so the two are kept in step by building the runtimes together.
+list(APPEND DASAN_TEST_DEPS dasan)
+set(DASAN_TEST_ARCH ${DASAN_SUPPORTED_ARCH})
+
+foreach(arch ${DASAN_TEST_ARCH})
+  set(DASAN_TEST_TARGET_ARCH ${arch})
+  string(TOLOWER "-${arch}-${OS_NAME}" DASAN_TEST_CONFIG_SUFFIX)
+  get_test_cc_for_arch(${arch} DASAN_TEST_TARGET_CC DASAN_TEST_TARGET_CFLAGS)
+  string(TOUPPER ${arch} ARCH_UPPER_CASE)
+  set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
+
+  configure_lit_site_cfg(
+    ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
+    ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)
+  list(APPEND DASAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
+endforeach()
+
+add_lit_testsuite(check-dasan "Running the DeviceAddressSanitizer tests"
+  ${DASAN_TESTSUITES}
+  DEPENDS ${DASAN_TEST_DEPS})
+set_target_properties(check-dasan PROPERTIES FOLDER "compiler-rt Misc")
diff --git a/compiler-rt/test/dasan/Inputs/second-module.hip b/compiler-rt/test/dasan/Inputs/second-module.hip
new file mode 100644
index 00000000000000..aec10731481e10
--- /dev/null
+++ b/compiler-rt/test/dasan/Inputs/second-module.hip
@@ -0,0 +1,14 @@
+__device__ int Other[16];
+
+__global__ void write_other(int I) { Other[I] = 1; }
+
+__global__ void write_other_shared(int *Out, int I) {
+  __shared__ int Buf[16];
+  unsigned T = threadIdxX();
+  Buf[T] = T;
+  syncThreads();
+  if (T == 0)
+    Buf[I] = 0x41;
+  syncThreads();
+  *Out = Buf[0];
+}
diff --git a/compiler-rt/test/dasan/hsa-load-order.c b/compiler-rt/test/dasan/hsa-load-order.c
new file mode 100644
index 00000000000000..1c00348b854d3b
--- /dev/null
+++ b/compiler-rt/test/dasan/hsa-load-order.c
@@ -0,0 +1,33 @@
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: %clang -DBUILD_HSA -fPIC -shared %s -o %t/libhsa-runtime64.so.1 \
+// RUN:   -Wl,-soname,libhsa-runtime64.so.1
+// RUN: %clang_dasan -DBUILD_DSO -shared-libsan -fPIC -shared %s \
+// RUN:   -o %t/libdso.so %t/libhsa-runtime64.so.1 -Wl,-rpath,%t
+// RUN: %clang %s -o %t/early %t/libdso.so -Wl,-rpath,%t
+// RUN: %run %t/early 2>&1 | FileCheck %s --check-prefix=EARLY
+// RUN: %clang %s -o %t/late %t/libhsa-runtime64.so.1 %t/libdso.so \
+// RUN:   -Wl,-rpath,%t
+// RUN: %run %t/late 2>&1 | FileCheck %s --check-prefix=LATE
+
+// The shared runtime must warn if HSA is loaded ahead of it, since the
+// interceptors are then bypassed.
+
+#include <stdio.h>
+
+#if defined(BUILD_HSA)
+int hsa_init(void) { return 42; }
+#elif defined(BUILD_DSO)
+int dso(void) { return 0; }
+#else
+int dso(void);
+int main(void) {
+  fprintf(stderr, "DONE\n");
+  return dso();
+}
+#endif
+
+// EARLY-NOT: WARNING
+// EARLY: DONE
+
+// LATE: WARNING: DeviceAddressSanitizer: the runtime is loaded too late
+// LATE: DONE
diff --git a/compiler-rt/test/dasan/hsa-missing.c b/compiler-rt/test/dasan/hsa-missing.c
new file mode 100644
index 00000000000000..6848d667d16141
--- /dev/null
+++ b/compiler-rt/test/dasan/hsa-missing.c
@@ -0,0 +1,23 @@
+// RUN: %clang_dasan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+// RUN: %clang_dasan -shared-libsan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// The runtime exports the HSA wrappers to every program. Without HSA loaded
+// they must report failure rather than kill the process.
+
+#include <dlfcn.h>
+#include <stdio.h>
+
+int main(void) {
+  int (*Init)(void) = (int (*)(void))dlsym(RTLD_DEFAULT, "hsa_init");
+  int (*ShutDown)(void) = (int (*)(void))dlsym(RTLD_DEFAULT, "hsa_shut_down");
+  if (!Init || !ShutDown)
+    return 1;
+  fprintf(stderr, "init: %s\n", Init() ? "failed" : "succeeded");
+  fprintf(stderr, "shut_down: %s\n", ShutDown() ? "failed" : "succeeded");
+  return 0;
+}
+
+// CHECK: init: failed
+// CHECK: shut_down: failed
diff --git a/compiler-rt/test/dasan/hsa-two-runtimes.c b/compiler-rt/test/dasan/hsa-two-runtimes.c
new file mode 100644
index 00000000000000..5002f21467cc35
--- /dev/null
+++ b/compiler-rt/test/dasan/hsa-two-runtimes.c
@@ -0,0 +1,53 @@
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: %clang -DBUILD_HSA -fPIC -shared %s -o %t/libhsa-runtime64.so.1 \
+// RUN:   -Wl,-soname,libhsa-runtime64.so.1
+// RUN: %clang_dasan -DBUILD_DSO -shared-libsan -fPIC -shared %s \
+// RUN:   -o %t/libhip.so %t/libhsa-runtime64.so.1 \
+// RUN:   -Wl,--disable-new-dtags -Wl,-rpath,%t
+// The executable's static runtime and the shared runtime loaded by the
+// sanitized library are the mix ASan rejects.
+// RUN: %clang_dasan %s -o %t/two %t/libhip.so \
+// RUN:   -Wl,--disable-new-dtags -Wl,-rpath,%t
+// RUN: not %run %t/two 2>&1 | FileCheck %s --check-prefix=MIX
+// One shared runtime is the supported link.
+// RUN: %clang -DSHARED %s -o %t/shared %t/libhip.so \
+// RUN:   -Wl,--disable-new-dtags -Wl,-rpath,%t
+// RUN: %run %t/shared 2>&1 | FileCheck %s --check-prefix=ONE
+
+#include <stdio.h>
+
+#if defined(BUILD_HSA)
+static int Calls;
+int hsa_init(void) {
+  fprintf(stderr, "real %d\n", ++Calls);
+  return 7;
+}
+#elif defined(BUILD_DSO)
+int hip_touch(void) { return 0; }
+#elif defined(SHARED)
+#  include <dlfcn.h>
+int hip_touch(void);
+int main(void) {
+  hip_touch();
+  int (*Init)(void) = (int (*)(void))dlsym(RTLD_DEFAULT, "hsa_init");
+  if (!Init)
+    return 1;
+  fprintf(stderr, "status %d\n", Init());
+  return 0;
+}
+#else
+int hip_touch(void);
+int hsa_init(void);
+int main(void) {
+  hip_touch();
+  fprintf(stderr, "status %d\n", hsa_init());
+  return 0;
+}
+#endif
+
+// MIX: ERROR: DeviceAddressSanitizer: incompatible runtimes
+// MIX-NOT: real
+
+// ONE: real 1
+// ONE-NOT: real 2
+// ONE: status 7
diff --git a/compiler-rt/test/dasan/initialize.c b/compiler-rt/test/dasan/initialize.c
new file mode 100644
index 00000000000000..d96e4f13ac1c7f
--- /dev/null
+++ b/compiler-rt/test/dasan/initialize.c
@@ -0,0 +1,6 @@
+// RUN: %clang_dasan %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+int main(void) { return 0; }
+
+// CHECK: DeviceAddressSanitizer: initialized
diff --git a/compiler-rt/test/dasan/lit.cfg.py b/compiler-rt/test/dasan/lit.cfg.py
new file mode 100644
index 00000000000000..43d94974815e77
--- /dev/null
+++ b/compiler-rt/test/dasan/lit.cfg.py
@@ -0,0 +1,49 @@
+config.name = "DASAN" + config.name_suffix
+config.test_source_root = os.path.dirname(__file__)
+config.suffixes = [".c"]
+config.excludes = ["Inputs"]
+
+default_dasan_opts = "verbosity=1"
+config.environment["DASAN_OPTIONS"] = default_dasan_opts
+config.substitutions.append(
+    ("%env_dasan_opts=", "env DASAN_OPTIONS=" + default_dasan_opts + ":")
+)
+
+
+def build_invocation(compile_flags):
+    return " " + " ".join([config.clang] + compile_flags) + " "
+
+
+target_cflags = [config.target_cflags]
+config.substitutions.append(("%clang ", build_invocation(target_cflags)))
+config.substitutions.append(
+    ("%clang_dasan ", build_invocation(target_cflags + ["-fsanitize=daddress"]))
+)
+
+if config.target_os != "Linux" or config.target_arch not in ["x86_64", "aarch64"]:
+    config.unsupported = True
+
+if "dasan" in config.gpu_runtimes:
+    if "hip" in config.available_features:
+        config.available_features.add("dasan-hip")
+    if "openmp-offload" in config.available_features:
+        config.available_features.add("dasan-openmp-offload")
+
+
+def add_dasan_substitution(name, base):
+    for pattern, replacement in config.substitutions:
+        if pattern == base:
+            config.substitutions.append(
+                (name, replacement.rstrip() + " -fsanitize=daddress ")
+            )
+            return
+    lit_config.fatal("Missing substitution %r" % base)
+
+
+if "dasan-hip" in config.available_features:
+    add_dasan_substitution("%clang_hip_dasan ", "%clang_hip ")
+if "dasan-openmp-offload" in config.available_features:
+    add_dasan_substitution("%clang_omp_offload_dasan ", "%clang_omp_offload ")
+
+if config.dasan_global_support:
+    config.available_features.add("dasan-globals")
diff --git a/compiler-rt/test/dasan/lit.site.cfg.py.in b/compiler-rt/test/dasan/lit.site.cfg.py.in
new file mode 100644
index 00000000000000..832cc162c76aa4
--- /dev/null
+++ b/compiler-rt/test/dasan/lit.site.cfg.py.in
@@ -0,0 +1,14 @@
+ at LIT_SITE_CFG_IN_HEADER@
+
+# Tool-specific config options.
+config.name_suffix = "@DASAN_TEST_CONFIG_SUFFIX@"
+config.dasan_lit_source_dir = "@DASAN_LIT_SOURCE_DIR@"
+config.dasan_global_support = @DASAN_GLOBAL_SUPPORT@
+config.target_cflags = "@DASAN_TEST_TARGET_CFLAGS@"
+config.target_arch = "@DASAN_TEST_TARGET_ARCH@"
+
+# Load common config for all compiler-rt lit tests
+lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")
+
+# Load tool-specific config that would do the real work.
+lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py")



More information about the llvm-branch-commits mailing list