morehouse created this revision.
morehouse added reviewers: vitalybuka, eugenis.
Herald added subscribers: dexonsmith, mgorny.
morehouse requested review of this revision.
Herald added projects: clang, Sanitizers.
Herald added subscribers: Sanitizers, cfe-commits.

-fsanitize=lam is intended to be a temporary flag for us to implement
LAM support in HWASan.  Once the -mlam feature flag (or something
equivalent) is added to gcc and clang, we can switch to using that
instead.

This patch also adds a "check-hwasan-lam" target to compiler-rt for
testing LAM mode.  Currently these tests must be run in an emulator that
has LAM support.  To ensure LAM mode isn't broken by future patches, I
will next set up a QEMU buildbot to run the HWASan tests in LAM.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102288

Files:
  clang/include/clang/Basic/Sanitizers.def
  clang/include/clang/Driver/SanitizerArgs.h
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Driver/ToolChains/Linux.cpp
  compiler-rt/test/hwasan/CMakeLists.txt
  compiler-rt/test/hwasan/TestCases/Linux/vfork.c
  compiler-rt/test/hwasan/lit.cfg.py
  compiler-rt/test/hwasan/lit.site.cfg.py.in

Index: compiler-rt/test/hwasan/lit.site.cfg.py.in
===================================================================
--- compiler-rt/test/hwasan/lit.site.cfg.py.in
+++ compiler-rt/test/hwasan/lit.site.cfg.py.in
@@ -6,6 +6,9 @@
 config.target_arch = "@HWASAN_TEST_TARGET_ARCH@"
 config.android_files_to_push = @HWASAN_ANDROID_FILES_TO_PUSH@
 
+# Whether to use -fsanitize=lam or not.
+config.enable_lam = lit_config.params.get("ENABLE_LAM", "@ENABLE_LAM@")
+
 # Load common config for all compiler-rt lit tests.
 lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")
 
Index: compiler-rt/test/hwasan/lit.cfg.py
===================================================================
--- compiler-rt/test/hwasan/lit.cfg.py
+++ compiler-rt/test/hwasan/lit.cfg.py
@@ -12,6 +12,11 @@
 clang_cflags = [config.target_cflags] + config.debug_info_flags
 clang_cxxflags = config.cxx_mode_flags + clang_cflags
 clang_hwasan_common_cflags = clang_cflags + ["-fsanitize=hwaddress", "-fuse-ld=lld"]
+
+if config.enable_lam == '1':
+  clang_hwasan_common_cflags += ["-fsanitize=lam"]
+if config.target_arch != 'x86_64' or config.enable_lam == '1':
+  config.available_features.add('pointer-tagging')
 if config.target_arch == 'x86_64':
   # This does basically the same thing as tagged-globals on aarch64. Because
   # the x86_64 implementation is for testing purposes only there is no
Index: compiler-rt/test/hwasan/TestCases/Linux/vfork.c
===================================================================
--- compiler-rt/test/hwasan/TestCases/Linux/vfork.c
+++ compiler-rt/test/hwasan/TestCases/Linux/vfork.c
@@ -2,9 +2,7 @@
 // RUN: %clang_hwasan -O0 %s -o %t && %run %t 2>&1
 
 // REQUIRES: aarch64-target-arch || x86_64-target-arch
-
-// Aliasing mode does not support stack tagging.
-// XFAIL: x86_64
+// REQUIRES: pointer-tagging
 
 #include <assert.h>
 #include <sys/types.h>
Index: compiler-rt/test/hwasan/CMakeLists.txt
===================================================================
--- compiler-rt/test/hwasan/CMakeLists.txt
+++ compiler-rt/test/hwasan/CMakeLists.txt
@@ -33,5 +33,15 @@
 add_lit_testsuite(check-hwasan "Running the HWAddressSanitizer tests"
   ${HWASAN_TESTSUITES}
   DEPENDS ${HWASAN_TEST_DEPS}
+  PARAMS "ENABLE_LAM=0"
   )
 set_target_properties(check-hwasan PROPERTIES FOLDER "Compiler-RT Misc")
+
+add_lit_testsuite(check-hwasan-lam
+  "Running the HWAddressSanitizer tests with Intel LAM"
+  ${HWASAN_TESTSUITES}
+  DEPENDS ${HWASAN_TEST_DEPS}
+  PARAMS "ENABLE_LAM=1"
+  EXCLUDE_FROM_CHECK_ALL
+  )
+set_target_properties(check-hwasan-lam PROPERTIES FOLDER "Compiler-RT Misc")
Index: clang/lib/Driver/ToolChains/Linux.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Linux.cpp
+++ clang/lib/Driver/ToolChains/Linux.cpp
@@ -12,6 +12,7 @@
 #include "Arch/PPC.h"
 #include "Arch/RISCV.h"
 #include "CommonArgs.h"
+#include "clang/Basic/Sanitizers.h"
 #include "clang/Config/config.h"
 #include "clang/Driver/Distro.h"
 #include "clang/Driver/Driver.h"
@@ -714,8 +715,10 @@
     Res |= SanitizerKind::Leak;
   if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64)
     Res |= SanitizerKind::Thread;
-  if (IsX86_64)
+  if (IsX86_64) {
     Res |= SanitizerKind::KernelMemory;
+    Res |= SanitizerKind::LAM;
+  }
   if (IsX86 || IsX86_64)
     Res |= SanitizerKind::Function;
   if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
Index: clang/lib/Driver/ToolChains/CommonArgs.cpp
===================================================================
--- clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -816,8 +816,12 @@
     }
     if (SanArgs.needsTsanRt() && SanArgs.linkRuntimes())
       SharedRuntimes.push_back("tsan");
-    if (SanArgs.needsHwasanRt() && SanArgs.linkRuntimes())
-      SharedRuntimes.push_back("hwasan");
+    if (SanArgs.needsHwasanRt() && SanArgs.linkRuntimes()) {
+      if (SanArgs.needsHwasanLamRt())
+        SharedRuntimes.push_back("hwasan_lam");
+      else
+        SharedRuntimes.push_back("hwasan");
+    }
   }
 
   // The stats_client library is also statically linked into DSOs.
@@ -847,9 +851,15 @@
   }
 
   if (!SanArgs.needsSharedRt() && SanArgs.needsHwasanRt() && SanArgs.linkRuntimes()) {
-    StaticRuntimes.push_back("hwasan");
-    if (SanArgs.linkCXXRuntimes())
-      StaticRuntimes.push_back("hwasan_cxx");
+    if (SanArgs.needsHwasanLamRt()) {
+      StaticRuntimes.push_back("hwasan_lam");
+      if (SanArgs.linkCXXRuntimes())
+        StaticRuntimes.push_back("hwasan_lam_cxx");
+    } else {
+      StaticRuntimes.push_back("hwasan");
+      if (SanArgs.linkCXXRuntimes())
+        StaticRuntimes.push_back("hwasan_cxx");
+    }
   }
   if (SanArgs.needsDfsanRt() && SanArgs.linkRuntimes())
     StaticRuntimes.push_back("dfsan");
Index: clang/include/clang/Driver/SanitizerArgs.h
===================================================================
--- clang/include/clang/Driver/SanitizerArgs.h
+++ clang/include/clang/Driver/SanitizerArgs.h
@@ -69,6 +69,9 @@
   bool needsHwasanRt() const {
     return Sanitizers.has(SanitizerKind::HWAddress);
   }
+  bool needsHwasanLamRt() const {
+    return needsHwasanRt() && Sanitizers.has(SanitizerKind::LAM);
+  }
   bool needsTsanRt() const { return Sanitizers.has(SanitizerKind::Thread); }
   bool needsMsanRt() const { return Sanitizers.has(SanitizerKind::Memory); }
   bool needsFuzzer() const { return Sanitizers.has(SanitizerKind::Fuzzer); }
Index: clang/include/clang/Basic/Sanitizers.def
===================================================================
--- clang/include/clang/Basic/Sanitizers.def
+++ clang/include/clang/Basic/Sanitizers.def
@@ -52,6 +52,12 @@
 // Hardware-assisted AddressSanitizer
 SANITIZER("hwaddress", HWAddress)
 
+// Utilize Intel LAM in sanitizers.  Currently only used in combination with
+// -fsanitize=hwaddress.  This is an experimental flag which may be removed in
+// the future.
+// TODO: Use -mlam instead, if/when it is supported by clang.
+SANITIZER("lam", LAM)
+
 // Kernel Hardware-assisted AddressSanitizer (KHWASan)
 SANITIZER("kernel-hwaddress", KernelHWAddress)
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to