git: b916d2b31c4c - stable/12 - Merge llvm commits for kernel address and memory sanitizer support

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Sat, 25 Dec 2021 11:55:10 UTC
The branch stable/12 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=b916d2b31c4ca1b52ffb1170280212685675880d

commit b916d2b31c4ca1b52ffb1170280212685675880d
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2021-04-27 19:18:13 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2021-12-25 11:51:00 +0000

    Merge llvm commits for kernel address and memory sanitizer support
    
    Merge commit 99eca1bd9c7a from llvm git (by Mark Johnston):
    
      [Driver] Enable kernel address and memory sanitizers on FreeBSD
    
      Test Plan: using kernel ASAN and MSAN implementations in FreeBSD
    
      Reviewed By: emaste, dim, arichardson
    
      Differential Revision: https://reviews.llvm.org/D98286
    
    Merge commit f511dc75e4c1 from llvm git (by Mark Johnston):
    
      [asan] Add an offset for the kernel address sanitizer on FreeBSD
    
      This is based on a port of the sanitizer runtime to the FreeBSD kernel
      that has been commited as https://cgit.freebsd.org/src/commit/?id=38da497a4dfcf1979c8c2b0e9f3fa0564035c147
      and the following commits.
    
      Reviewed By: emaste, dim
      Differential Revision: https://reviews.llvm.org/D98285
    
    Requested by:   markj
    MFC after:      3 days
    
    (cherry picked from commit 1b00608b264f1fac846e571e23d8ef7134834638)
---
 contrib/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp   |  8 +++++++-
 .../llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp   | 10 +++++++---
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/contrib/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp b/contrib/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp
index 57744bac37da..c14f34b6107c 100644
--- a/contrib/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/contrib/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -462,6 +462,7 @@ bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; }
 bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
 
 SanitizerMask FreeBSD::getSupportedSanitizers() const {
+  const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64;
   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
   const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
   const bool IsMIPS64 = getTriple().isMIPS64();
@@ -480,8 +481,13 @@ SanitizerMask FreeBSD::getSupportedSanitizers() const {
     Res |= SanitizerKind::Fuzzer;
     Res |= SanitizerKind::FuzzerNoLink;
   }
-  if (IsX86_64)
+  if (IsAArch64 || IsX86_64) {
+    Res |= SanitizerKind::KernelAddress;
+    Res |= SanitizerKind::KernelMemory;
+  }
+  if (IsX86_64) {
     Res |= SanitizerKind::Memory;
+  }
   return Res;
 }
 
diff --git a/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 1557fad4d372..5ce21cc3616a 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -107,6 +107,7 @@ static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
 static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
 static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
 static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
+static const uint64_t kFreeBSDKasan_ShadowOffset64 = 0xdffff7c000000000;
 static const uint64_t kNetBSD_ShadowOffset32 = 1ULL << 30;
 static const uint64_t kNetBSD_ShadowOffset64 = 1ULL << 46;
 static const uint64_t kNetBSDKasan_ShadowOffset64 = 0xdfff900000000000;
@@ -489,9 +490,12 @@ static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
       Mapping.Offset = kPPC64_ShadowOffset64;
     else if (IsSystemZ)
       Mapping.Offset = kSystemZ_ShadowOffset64;
-    else if (IsFreeBSD && !IsMIPS64)
-      Mapping.Offset = kFreeBSD_ShadowOffset64;
-    else if (IsNetBSD) {
+    else if (IsFreeBSD && !IsMIPS64) {
+      if (IsKasan)
+        Mapping.Offset = kFreeBSDKasan_ShadowOffset64;
+      else
+        Mapping.Offset = kFreeBSD_ShadowOffset64;
+    } else if (IsNetBSD) {
       if (IsKasan)
         Mapping.Offset = kNetBSDKasan_ShadowOffset64;
       else