[llvm] [Instrumentation] Do not run sanitizers for naked functions (PR #108552)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 14 08:30:19 PDT 2024


https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/108552

>From 6226bd68ed6e85fa153cc6551998fdde3f261680 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Sat, 14 Sep 2024 16:45:42 +0200
Subject: [PATCH 1/2] [Instrumentation] Precommit tests for PR108552 (NFC)

---
 llvm/test/Instrumentation/sanitizers-naked.ll | 94 +++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 llvm/test/Instrumentation/sanitizers-naked.ll

diff --git a/llvm/test/Instrumentation/sanitizers-naked.ll b/llvm/test/Instrumentation/sanitizers-naked.ll
new file mode 100644
index 00000000000000..aad6f98a379c81
--- /dev/null
+++ b/llvm/test/Instrumentation/sanitizers-naked.ll
@@ -0,0 +1,94 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=asan -S | FileCheck %s --check-prefixes=CHECK-ASAN
+; RUN: opt < %s -passes=tsan -S | FileCheck %s --check-prefixes=CHECK-TSAN
+; RUN: opt < %s -passes=msan -S | FileCheck %s --check-prefixes=CHECK-MSAN
+; RUN: opt < %s -passes=hwasan -S | FileCheck %s --check-prefixes=CHECK-HWASAN
+; RUN: opt < %s -passes=dfsan -dfsan-track-origins=1  -S | FileCheck %s --check-prefixes=CHECK-DFSAN
+; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-control-flow -S | FileCheck %s --check-prefixes=CHECK-SANCOV
+; RUN: opt < %s -passes='module(sanmd-module)' -sanitizer-metadata-atomics -S | FileCheck %s --check-prefixes=CHECK-SANMD
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @naked_function() naked {
+; CHECK-ASAN-LABEL: define void @naked_function(
+; CHECK-ASAN-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-ASAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-ASAN-NEXT:    unreachable
+;
+; CHECK-TSAN-LABEL: define void @naked_function(
+; CHECK-TSAN-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-TSAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-TSAN-NEXT:    unreachable
+;
+; CHECK-MSAN-LABEL: define void @naked_function(
+; CHECK-MSAN-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-MSAN-NEXT:    call void @llvm.donothing()
+; CHECK-MSAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-MSAN-NEXT:    unreachable
+;
+; CHECK-HWASAN-LABEL: define void @naked_function(
+; CHECK-HWASAN-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-HWASAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-HWASAN-NEXT:    unreachable
+;
+; CHECK-SANCOV-LABEL: define void @naked_function(
+; CHECK-SANCOV-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-SANCOV-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-SANCOV-NEXT:    unreachable
+;
+; CHECK-SANMD-LABEL: define void @naked_function(
+; CHECK-SANMD-SAME: ) #[[ATTR0:[0-9]+]] !pcsections [[META0:![0-9]+]] {
+; CHECK-SANMD-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-SANMD-NEXT:    unreachable
+;
+  call void asm sideeffect "nop", ""()
+  unreachable
+}
+
+define void @naked_function_with_asan() sanitize_address naked {
+; CHECK-ASAN-LABEL: define void @naked_function_with_asan(
+; CHECK-ASAN-SAME: ) #[[ATTR1:[0-9]+]] {
+; CHECK-ASAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-ASAN-NEXT:    unreachable
+;
+  call void asm sideeffect "nop", ""()
+  unreachable
+}
+
+define void @naked_function_with_tsan() sanitize_thread naked {
+; CHECK-TSAN-LABEL: define void @naked_function_with_tsan(
+; CHECK-TSAN-SAME: ) #[[ATTR2:[0-9]+]] {
+; CHECK-TSAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-TSAN-NEXT:    unreachable
+;
+  call void asm sideeffect "nop", ""()
+  unreachable
+}
+
+define void @naked_function_with_msan() sanitize_memory naked {
+; CHECK-MSAN-LABEL: define void @naked_function_with_msan(
+; CHECK-MSAN-SAME: ) #[[ATTR3:[0-9]+]] {
+; CHECK-MSAN-NEXT:    call void @llvm.donothing()
+; CHECK-MSAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-MSAN-NEXT:    unreachable
+;
+  call void asm sideeffect "nop", ""()
+  unreachable
+}
+
+define void @naked_function_with_hwasan() sanitize_hwaddress naked {
+; CHECK-HWASAN-LABEL: define void @naked_function_with_hwasan(
+; CHECK-HWASAN-SAME: ) #[[ATTR4:[0-9]+]] {
+; CHECK-HWASAN-NEXT:    call void asm sideeffect "nop", ""()
+; CHECK-HWASAN-NEXT:    unreachable
+;
+  call void asm sideeffect "nop", ""()
+  unreachable
+}
+;.
+; CHECK-SANMD: [[META0]] = !{!"sanmd_covered2!C", [[META1:![0-9]+]]}
+; CHECK-SANMD: [[META1]] = !{i64 1}
+;.
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; CHECK-DFSAN: {{.*}}

>From bd80c387bad78e43ff3a56b4bfa0d8fc0fee3a7c Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Sat, 14 Sep 2024 17:27:18 +0200
Subject: [PATCH 2/2] [Instrumentation] Do not request sanitizers for naked
 functions

Sanitizers instrumentation may be incompatible with naked functions,
which lack of standard prologue/epilogue.
---
 llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp  | 4 ++++
 .../lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 4 ++++
 .../Instrumentation/SanitizerBinaryMetadata.cpp           | 3 +++
 llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp | 3 +++
 llvm/test/Instrumentation/sanitizers-naked.ll             | 8 ++------
 5 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index 5de0a78d087968..1e9e2d03c6ce42 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -2960,6 +2960,10 @@ bool AddressSanitizer::instrumentFunction(Function &F,
 
   bool FunctionModified = false;
 
+  // Do not apply any instrumentation for naked functions.
+  if (F.hasFnAttribute(Attribute::Naked))
+    return FunctionModified;
+
   // If needed, insert __asan_init before checking for SanitizeAddress attr.
   // This function needs to be called even if the function body is not
   // instrumented.
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index a7e7f9a570dac7..df645b85a9b6ec 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1598,6 +1598,10 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
   if (&F == HwasanCtorFunction)
     return;
 
+  // Do not apply any instrumentation for naked functions.
+  if (F.hasFnAttribute(Attribute::Naked))
+    return;
+
   if (!F.hasFnAttribute(Attribute::SanitizeHWAddress))
     return;
 
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
index e326f30ad88eeb..eb346254f30dd7 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
@@ -258,6 +258,9 @@ bool SanitizerBinaryMetadata::run() {
 void SanitizerBinaryMetadata::runOn(Function &F, MetadataInfoSet &MIS) {
   if (F.empty())
     return;
+  // Do not apply any instrumentation for naked functions.
+  if (F.hasFnAttribute(Attribute::Naked))
+    return;
   if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
     return;
   if (Ignorelist && Ignorelist->inSection("metadata", "fun", F.getName()))
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 6a89cee9aaf6cc..db4bf709c9cc9c 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -629,6 +629,9 @@ void ModuleSanitizerCoverage::instrumentFunction(Function &F) {
     return;
   if (Blocklist && Blocklist->inSection("coverage", "fun", F.getName()))
     return;
+  // Do not apply any instrumentation for naked functions.
+  if (F.hasFnAttribute(Attribute::Naked))
+    return;
   if (F.hasFnAttribute(Attribute::NoSanitizeCoverage))
     return;
   if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
diff --git a/llvm/test/Instrumentation/sanitizers-naked.ll b/llvm/test/Instrumentation/sanitizers-naked.ll
index aad6f98a379c81..27acd70219abc3 100644
--- a/llvm/test/Instrumentation/sanitizers-naked.ll
+++ b/llvm/test/Instrumentation/sanitizers-naked.ll
@@ -38,7 +38,7 @@ define void @naked_function() naked {
 ; CHECK-SANCOV-NEXT:    unreachable
 ;
 ; CHECK-SANMD-LABEL: define void @naked_function(
-; CHECK-SANMD-SAME: ) #[[ATTR0:[0-9]+]] !pcsections [[META0:![0-9]+]] {
+; CHECK-SANMD-SAME: ) #[[ATTR0:[0-9]+]] {
 ; CHECK-SANMD-NEXT:    call void asm sideeffect "nop", ""()
 ; CHECK-SANMD-NEXT:    unreachable
 ;
@@ -79,16 +79,12 @@ define void @naked_function_with_msan() sanitize_memory naked {
 
 define void @naked_function_with_hwasan() sanitize_hwaddress naked {
 ; CHECK-HWASAN-LABEL: define void @naked_function_with_hwasan(
-; CHECK-HWASAN-SAME: ) #[[ATTR4:[0-9]+]] {
+; CHECK-HWASAN-SAME: ) #[[ATTR4:[0-9]+]] personality ptr @__hwasan_personality_thunk {
 ; CHECK-HWASAN-NEXT:    call void asm sideeffect "nop", ""()
 ; CHECK-HWASAN-NEXT:    unreachable
 ;
   call void asm sideeffect "nop", ""()
   unreachable
 }
-;.
-; CHECK-SANMD: [[META0]] = !{!"sanmd_covered2!C", [[META1:![0-9]+]]}
-; CHECK-SANMD: [[META1]] = !{i64 1}
-;.
 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
 ; CHECK-DFSAN: {{.*}}



More information about the llvm-commits mailing list