git: 6702793f75bd - 2021Q3 - devel/llvm12: Add patches to enable kernel sanitizers on FreeBSD

Brooks Davis brooks at FreeBSD.org
Wed Aug 11 20:02:14 UTC 2021


The branch 2021Q3 has been updated by brooks:

URL: https://cgit.FreeBSD.org/ports/commit/?id=6702793f75bddf683e5121eb2a0f49b78e10cd50

commit 6702793f75bddf683e5121eb2a0f49b78e10cd50
Author:     Mark Johnston <markj at FreeBSD.org>
AuthorDate: 2021-08-11 20:01:10 +0000
Commit:     Brooks Davis <brooks at FreeBSD.org>
CommitDate: 2021-08-11 20:01:10 +0000

    devel/llvm12: Add patches to enable kernel sanitizers on FreeBSD
    
    I use CROSS_TOOLCHAIN quite frequently, but can't do so when building
    kernels with KASAN or KMSAN enabled since these patches didn't make it
    into LLVM 12.  This diff just backports them to the port. I believe this
    is low-risk and thus is acceptable for the port, as opposed to waiting
    for LLVM 13.
    
    Differential Revision:  https://reviews.freebsd.org/D31316
    
    (cherry picked from commit f8617a24662331dd03d72d4bc36a0238ec54d119)
---
 devel/llvm12/Makefile                              |  2 +-
 .../patch-clang_lib_Driver_ToolChains_FreeBSD.cpp  | 33 ++++++++++++++++++++++
 .../files/patch-clang_test_Driver_fsanitize.c      | 16 +++++++++++
 ...Transforms_Instrumentation_AddressSanitizer.cpp | 26 +++++++++++++++++
 4 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/devel/llvm12/Makefile b/devel/llvm12/Makefile
index 57f6aeb8fcb4..5d4e7b48d8d8 100644
--- a/devel/llvm12/Makefile
+++ b/devel/llvm12/Makefile
@@ -1,6 +1,6 @@
 PORTNAME=	llvm
 DISTVERSION=	12.0.1
-PORTREVISION=	0
+PORTREVISION=	1
 CATEGORIES=	devel lang
 MASTER_SITES=	https://github.com/llvm/llvm-project/releases/download/llvmorg-${DISTVERSION:S/rc/-rc/}/ \
 		https://${PRE_}releases.llvm.org/${LLVM_RELEASE}/${RCDIR}
diff --git a/devel/llvm12/files/patch-clang_lib_Driver_ToolChains_FreeBSD.cpp b/devel/llvm12/files/patch-clang_lib_Driver_ToolChains_FreeBSD.cpp
new file mode 100644
index 000000000000..4c13ad977b21
--- /dev/null
+++ b/devel/llvm12/files/patch-clang_lib_Driver_ToolChains_FreeBSD.cpp
@@ -0,0 +1,33 @@
+[Driver] Default to libc++ on FreeBSD
+
+Downstream may naively translate between DSL and LLVM target
+triple. If OS version is lost in the process then Clang would
+default to a version that's no longer supported by OS vendor.
+
+https://reviews.llvm.org/D77776
+
+--- clang/lib/Driver/ToolChains/FreeBSD.cpp.orig	2021-06-28 16:23:38 UTC
++++ clang/lib/Driver/ToolChains/FreeBSD.cpp
+@@ -466,6 +466,7 @@ bool FreeBSD::IsUnwindTablesDefault(const ArgList &Arg
+ 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();
+@@ -484,8 +485,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/devel/llvm12/files/patch-clang_test_Driver_fsanitize.c b/devel/llvm12/files/patch-clang_test_Driver_fsanitize.c
new file mode 100644
index 000000000000..0c616740ceca
--- /dev/null
+++ b/devel/llvm12/files/patch-clang_test_Driver_fsanitize.c
@@ -0,0 +1,16 @@
+--- clang/test/Driver/fsanitize.c.orig	2021-06-28 16:23:38 UTC
++++ clang/test/Driver/fsanitize.c
+@@ -689,7 +689,13 @@
+ // RUN: %clang -target x86_64-unknown-cloudabi -fsanitize=safe-stack %s -### 2>&1 | FileCheck %s -check-prefix=SAFESTACK-CLOUDABI
+ // SAFESTACK-CLOUDABI: "-fsanitize=safe-stack"
+ 
++// RUN: %clang -target x86_64--freebsd -fsanitize=kernel-address %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-ADDRESS-FREEBSD
++// RUN: %clang -target aarch64--freebsd -fsanitize=kernel-address %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-ADDRESS-FREEBSD
++// KERNEL-ADDRESS-FREEBSD: "-fsanitize=kernel-address"
+ 
++// RUN: %clang -target x86_64--freebsd -fsanitize=kernel-memory %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-MEMORY-FREEBSD
++// RUN: %clang -target aarch64--freebsd -fsanitize=kernel-memory %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-MEMORY-FREEBSD
++// KERNEL-MEMORY-FREEBSD: "-fsanitize=kernel-memory"
+ 
+ // * NetBSD; please keep ordered as in Sanitizers.def *
+ 
diff --git a/devel/llvm12/files/patch-llvm_lib_Transforms_Instrumentation_AddressSanitizer.cpp b/devel/llvm12/files/patch-llvm_lib_Transforms_Instrumentation_AddressSanitizer.cpp
new file mode 100644
index 000000000000..dd0b84559177
--- /dev/null
+++ b/devel/llvm12/files/patch-llvm_lib_Transforms_Instrumentation_AddressSanitizer.cpp
@@ -0,0 +1,26 @@
+--- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp.orig	2021-06-28 16:23:38 UTC
++++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+@@ -108,6 +108,7 @@ static const uint64_t kAArch64_ShadowOffset64 = 1ULL <
+ static const uint64_t kRISCV64_ShadowOffset64 = 0x20000000;
+ 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;
+@@ -484,9 +485,12 @@ static ShadowMapping getShadowMapping(Triple &TargetTr
+       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


More information about the dev-commits-ports-branches mailing list