From 0673642cab6b6a9eec20d4ea4ee6bc46db47e04c Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Mon, 23 Sep 2024 17:13:25 -0700 Subject: [PATCH] [hwasan] Replace "-hwasan-with-ifunc" and "-hwasan-with-tls" options (#109619) Relationship between "-hwasan-mapping-offset", "-hwasan-with-ifunc", and "-hwasan-with-tls" can be to hard to understand. Now we will have "-hwasan-mapping-offset", presense of which will imply fixed shadow. If "-hwasan-mapping-offset-dynamic" will set one of 3 available dynamic shadows. As-is "-hwasan-mapping-offset" has precedence over "-hwasan-mapping-offset-dynamic". In follow up patches we need to use the one with last occurrence. --- .../Instrumentation/HWAddressSanitizer.cpp | 43 ++++++++----------- .../HWAddressSanitizer/RISCV/alloca.ll | 2 +- .../HWAddressSanitizer/RISCV/basic.ll | 4 +- .../HWAddressSanitizer/alloca.ll | 4 +- .../HWAddressSanitizer/basic.ll | 4 +- .../HWAddressSanitizer/prologue.ll | 8 ++-- 6 files changed, 30 insertions(+), 35 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp index 2224397d089155..49d463a07553f2 100644 --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -86,6 +86,15 @@ static const size_t kDefaultShadowScale = 4; static const unsigned kShadowBaseAlignment = 32; +namespace { +enum class OffsetKind { + kFixed = 0, + kGlobal, + kIfunc, + kTls, +}; +} + static cl::opt ClMemoryAccessCallbackPrefix("hwasan-memory-access-callback-prefix", cl::desc("Prefix for memory access callbacks"), @@ -169,19 +178,14 @@ static cl::opt static cl::opt ClMappingOffset("hwasan-mapping-offset", cl::desc("HWASan shadow mapping offset [EXPERIMENTAL]"), - cl::Hidden, cl::init(0)); + cl::Hidden); -static cl::opt - ClWithIfunc("hwasan-with-ifunc", - cl::desc("Access dynamic shadow through an ifunc global on " - "platforms that support this"), - cl::Hidden, cl::init(false)); - -static cl::opt ClWithTls( - "hwasan-with-tls", - cl::desc("Access dynamic shadow through an thread-local pointer on " - "platforms that support this"), - cl::Hidden, cl::init(true)); +static cl::opt ClMappingOffsetDynamic( + "hwasan-mapping-offset-dynamic", + cl::desc("HWASan shadow mapping dynamic offset location"), cl::Hidden, + cl::values(clEnumValN(OffsetKind::kGlobal, "global", "Use global"), + clEnumValN(OffsetKind::kIfunc, "ifunc", "Use ifunc global"), + clEnumValN(OffsetKind::kTls, "tls", "Use TLS"))); static cl::opt ClHotPercentileCutoff("hwasan-percentile-cutoff-hot", cl::desc("Hot percentile cuttoff.")); @@ -398,12 +402,6 @@ class HWAddressSanitizer { /// If WithFrameRecord is true, then __hwasan_tls will be used to access the /// ring buffer for storing stack allocations on targets that support it. class ShadowMapping { - enum class OffsetKind { - kFixed = 0, - kGlobal, - kIfunc, - kTls, - }; OffsetKind Kind; uint64_t Offset; uint8_t Scale; @@ -1940,11 +1938,8 @@ void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple, } else if (ClEnableKhwasan || InstrumentWithCalls) { SetFixed(0); WithFrameRecord = false; - } else if (ClWithIfunc) { - Kind = OffsetKind::kIfunc; - WithFrameRecord = false; - } else if (!ClWithTls) { - Kind = OffsetKind::kGlobal; - WithFrameRecord = false; + } else if (ClMappingOffsetDynamic.getNumOccurrences() > 0) { + Kind = ClMappingOffsetDynamic; + WithFrameRecord = isInTls(); } } diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/alloca.ll b/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/alloca.ll index 5fd9dc6eede211..24a89af97cffeb 100644 --- a/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/alloca.ll +++ b/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/alloca.ll @@ -2,7 +2,7 @@ ; Test alloca instrumentation. Command line includes check-globals so that ; changes to debug-info are detectable. ; -; RUN: opt < %s -passes=hwasan -hwasan-with-ifunc=1 -S | FileCheck %s --check-prefixes=DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-mapping-offset-dynamic=ifunc -S | FileCheck %s --check-prefixes=DYNAMIC-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=ZERO-BASED-SHADOW target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/basic.ll b/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/basic.ll index 5415b081286631..e0eb1115854aba 100644 --- a/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/basic.ll +++ b/llvm/test/Instrumentation/HWAddressSanitizer/RISCV/basic.ll @@ -4,8 +4,8 @@ ; RUN: opt < %s -passes=hwasan -S | FileCheck %s ; RUN: opt < %s -passes=hwasan -hwasan-inline-fast-path-checks=0 -S | FileCheck %s --check-prefixes=NOFASTPATH ; RUN: opt < %s -passes=hwasan -hwasan-inline-fast-path-checks=1 -S | FileCheck %s --check-prefixes=FASTPATH -; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-with-ifunc=1 -hwasan-with-tls=0 -S | FileCheck %s --check-prefixes=ABORT-DYNAMIC-SHADOW -; RUN: opt < %s -passes=hwasan -hwasan-recover=1 -hwasan-with-ifunc=1 -hwasan-with-tls=0 -S | FileCheck %s --check-prefixes=RECOVER-DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-mapping-offset-dynamic=ifunc -S | FileCheck %s --check-prefixes=ABORT-DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-recover=1 -hwasan-mapping-offset-dynamic=ifunc -S | FileCheck %s --check-prefixes=RECOVER-DYNAMIC-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=ABORT-ZERO-BASED-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-recover=1 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=RECOVER-ZERO-BASED-SHADOW diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/alloca.ll b/llvm/test/Instrumentation/HWAddressSanitizer/alloca.ll index 73f56de707b216..4d0cce72470b96 100644 --- a/llvm/test/Instrumentation/HWAddressSanitizer/alloca.ll +++ b/llvm/test/Instrumentation/HWAddressSanitizer/alloca.ll @@ -2,10 +2,10 @@ ; Test alloca instrumentation. Command line includes check-globals so that ; changes to debug-info are detectable. ; -; RUN: opt < %s -passes=hwasan -hwasan-with-ifunc=1 -S | FileCheck %s --check-prefixes=DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-mapping-offset-dynamic=ifunc -S | FileCheck %s --check-prefixes=DYNAMIC-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=ZERO-BASED-SHADOW -; RUN: opt < %s -passes=hwasan -hwasan-with-ifunc=1 -S --try-experimental-debuginfo-iterators | FileCheck %s --check-prefixes=DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-mapping-offset-dynamic=ifunc -S --try-experimental-debuginfo-iterators | FileCheck %s --check-prefixes=DYNAMIC-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-mapping-offset=0 -S --try-experimental-debuginfo-iterators | FileCheck %s --check-prefixes=ZERO-BASED-SHADOW target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll b/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll index afbb8f50011141..355e3b94978b39 100644 --- a/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll +++ b/llvm/test/Instrumentation/HWAddressSanitizer/basic.ll @@ -4,8 +4,8 @@ ; RUN: opt < %s -passes=hwasan -S | FileCheck %s ; RUN: opt < %s -passes=hwasan -hwasan-inline-fast-path-checks=0 -S | FileCheck %s --check-prefixes=NOFASTPATH ; RUN: opt < %s -passes=hwasan -hwasan-inline-fast-path-checks=1 -S | FileCheck %s --check-prefixes=FASTPATH -; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-with-ifunc=1 -hwasan-with-tls=0 -S | FileCheck %s --check-prefixes=ABORT-DYNAMIC-SHADOW -; RUN: opt < %s -passes=hwasan -hwasan-recover=1 -hwasan-with-ifunc=1 -hwasan-with-tls=0 -S | FileCheck %s --check-prefixes=RECOVER-DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-mapping-offset-dynamic=ifunc -S | FileCheck %s --check-prefixes=ABORT-DYNAMIC-SHADOW +; RUN: opt < %s -passes=hwasan -hwasan-recover=1 -hwasan-mapping-offset-dynamic=ifunc -S | FileCheck %s --check-prefixes=RECOVER-DYNAMIC-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-recover=0 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=ABORT-ZERO-BASED-SHADOW ; RUN: opt < %s -passes=hwasan -hwasan-recover=1 -hwasan-mapping-offset=0 -S | FileCheck %s --check-prefixes=RECOVER-ZERO-BASED-SHADOW diff --git a/llvm/test/Instrumentation/HWAddressSanitizer/prologue.ll b/llvm/test/Instrumentation/HWAddressSanitizer/prologue.ll index 49f0bf739cb692..005a11b00c7a56 100644 --- a/llvm/test/Instrumentation/HWAddressSanitizer/prologue.ll +++ b/llvm/test/Instrumentation/HWAddressSanitizer/prologue.ll @@ -3,13 +3,13 @@ ; ; RUN: opt -passes=hwasan -S < %s | \ ; RUN: FileCheck %s -; RUN: opt -passes=hwasan -S -hwasan-with-ifunc=0 -hwasan-with-tls=1 -hwasan-record-stack-history=instr < %s | \ +; RUN: opt -passes=hwasan -S -hwasan-mapping-offset-dynamic=tls -hwasan-record-stack-history=instr < %s | \ ; RUN: FileCheck %s --check-prefixes=NOIFUNC-TLS-HISTORY -; RUN: opt -passes=hwasan -S -hwasan-with-ifunc=0 -hwasan-with-tls=1 -hwasan-record-stack-history=none < %s | \ +; RUN: opt -passes=hwasan -S -hwasan-mapping-offset-dynamic=tls -hwasan-record-stack-history=none < %s | \ ; RUN: FileCheck %s --check-prefixes=NOIFUNC-TLS-NOHISTORY -; RUN: opt -passes=hwasan -S -hwasan-with-ifunc=0 -hwasan-with-tls=0 < %s | \ +; RUN: opt -passes=hwasan -S -hwasan-mapping-offset-dynamic=global < %s | \ ; RUN: FileCheck %s --check-prefixes=NOIFUNC-NOTLS -; RUN: opt -passes=hwasan -S -hwasan-with-ifunc=1 -hwasan-with-tls=0 < %s | \ +; RUN: opt -passes=hwasan -S -hwasan-mapping-offset-dynamic=ifunc < %s | \ ; RUN: FileCheck %s --check-prefixes=IFUNC-NOTLS ; RUN: opt -passes=hwasan -S -mtriple=aarch64-fuchsia < %s | \ ; RUN: FileCheck %s --check-prefixes=FUCHSIA