Skip to content

Commit

Permalink
[Instrumentation] Do not run sanitizers for naked functions
Browse files Browse the repository at this point in the history
Sanitizers instrumentation is incompatible with naked functions,
which are expected to contain only inline asm.
  • Loading branch information
antoniofrighetto committed Sep 13, 2024
1 parent c0e308b commit d76f9d7
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1545,8 +1545,9 @@ bool DataFlowSanitizer::runImpl(
SmallPtrSet<Function *, 2> FnsWithForceZeroLabel;
SmallPtrSet<Constant *, 1> PersonalityFns;
for (Function &F : M)
// Do not apply any instrumentation for naked functions or if disabled.
if (!F.isIntrinsic() && !DFSanRuntimeFunctions.contains(&F) &&
!LibAtomicFunction(F) &&
!LibAtomicFunction(F) && !F.hasFnAttribute(Attribute::Naked) &&
!F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation)) {
FnsToInstrument.push_back(&F);
if (F.hasPersonalityFn())
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6078,6 +6078,10 @@ bool MemorySanitizer::sanitizeFunction(Function &F, TargetLibraryInfo &TLI) {
if (!CompileKernel && F.getName() == kMsanModuleCtorName)
return false;

// Do not apply any instrumentation for naked functions.
if (F.hasFnAttribute(Attribute::Naked))
return false;

if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
49 changes: 49 additions & 0 deletions llvm/test/Instrumentation/sanitizers-no-naked.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -passes=asan -S | FileCheck %s
; RUN: opt < %s -passes=tsan -S | FileCheck %s
; RUN: opt < %s -passes=dfsan -dfsan-track-origins=1 -S | FileCheck %s
; RUN: opt < %s -passes='module(sancov-module)' -sanitizer-coverage-level=3 -sanitizer-coverage-control-flow -S | FileCheck %s
; RUN: opt < %s -passes='module(sanmd-module)' -sanitizer-metadata-atomics -S | FileCheck %s

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-LABEL: define void @naked_function(
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: call void asm sideeffect "nop", ""()
; CHECK-NEXT: unreachable
;
call void asm sideeffect "nop", ""()
unreachable
}

define void @naked_function_with_asan() sanitize_address naked {
; CHECK-LABEL: define void @naked_function_with_asan(
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
; CHECK-NEXT: call void asm sideeffect "nop", ""()
; CHECK-NEXT: unreachable
;
call void asm sideeffect "nop", ""()
unreachable
}

define void @naked_function_with_tsan() sanitize_thread naked {
; CHECK-LABEL: define void @naked_function_with_tsan(
; CHECK-SAME: ) #[[ATTR2:[0-9]+]] {
; CHECK-NEXT: call void asm sideeffect "nop", ""()
; CHECK-NEXT: unreachable
;
call void asm sideeffect "nop", ""()
unreachable
}

define void @naked_function_with_msan() sanitize_memory naked {
; CHECK-LABEL: define void @naked_function_with_msan(
; CHECK-SAME: ) #[[ATTR3:[0-9]+]] {
; CHECK-NEXT: call void asm sideeffect "nop", ""()
; CHECK-NEXT: unreachable
;
call void asm sideeffect "nop", ""()
unreachable
}

0 comments on commit d76f9d7

Please sign in to comment.