Skip to content

Commit bd80c38

Browse files
[Instrumentation] Do not request sanitizers for naked functions
Sanitizers instrumentation may be incompatible with naked functions, which lack of standard prologue/epilogue.
1 parent 6226bd6 commit bd80c38

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2960,6 +2960,10 @@ bool AddressSanitizer::instrumentFunction(Function &F,
29602960

29612961
bool FunctionModified = false;
29622962

2963+
// Do not apply any instrumentation for naked functions.
2964+
if (F.hasFnAttribute(Attribute::Naked))
2965+
return FunctionModified;
2966+
29632967
// If needed, insert __asan_init before checking for SanitizeAddress attr.
29642968
// This function needs to be called even if the function body is not
29652969
// instrumented.

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,10 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
15981598
if (&F == HwasanCtorFunction)
15991599
return;
16001600

1601+
// Do not apply any instrumentation for naked functions.
1602+
if (F.hasFnAttribute(Attribute::Naked))
1603+
return;
1604+
16011605
if (!F.hasFnAttribute(Attribute::SanitizeHWAddress))
16021606
return;
16031607

llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ bool SanitizerBinaryMetadata::run() {
258258
void SanitizerBinaryMetadata::runOn(Function &F, MetadataInfoSet &MIS) {
259259
if (F.empty())
260260
return;
261+
// Do not apply any instrumentation for naked functions.
262+
if (F.hasFnAttribute(Attribute::Naked))
263+
return;
261264
if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
262265
return;
263266
if (Ignorelist && Ignorelist->inSection("metadata", "fun", F.getName()))

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ void ModuleSanitizerCoverage::instrumentFunction(Function &F) {
629629
return;
630630
if (Blocklist && Blocklist->inSection("coverage", "fun", F.getName()))
631631
return;
632+
// Do not apply any instrumentation for naked functions.
633+
if (F.hasFnAttribute(Attribute::Naked))
634+
return;
632635
if (F.hasFnAttribute(Attribute::NoSanitizeCoverage))
633636
return;
634637
if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))

llvm/test/Instrumentation/sanitizers-naked.ll

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ define void @naked_function() naked {
3838
; CHECK-SANCOV-NEXT: unreachable
3939
;
4040
; CHECK-SANMD-LABEL: define void @naked_function(
41-
; CHECK-SANMD-SAME: ) #[[ATTR0:[0-9]+]] !pcsections [[META0:![0-9]+]] {
41+
; CHECK-SANMD-SAME: ) #[[ATTR0:[0-9]+]] {
4242
; CHECK-SANMD-NEXT: call void asm sideeffect "nop", ""()
4343
; CHECK-SANMD-NEXT: unreachable
4444
;
@@ -79,16 +79,12 @@ define void @naked_function_with_msan() sanitize_memory naked {
7979

8080
define void @naked_function_with_hwasan() sanitize_hwaddress naked {
8181
; CHECK-HWASAN-LABEL: define void @naked_function_with_hwasan(
82-
; CHECK-HWASAN-SAME: ) #[[ATTR4:[0-9]+]] {
82+
; CHECK-HWASAN-SAME: ) #[[ATTR4:[0-9]+]] personality ptr @__hwasan_personality_thunk {
8383
; CHECK-HWASAN-NEXT: call void asm sideeffect "nop", ""()
8484
; CHECK-HWASAN-NEXT: unreachable
8585
;
8686
call void asm sideeffect "nop", ""()
8787
unreachable
8888
}
89-
;.
90-
; CHECK-SANMD: [[META0]] = !{!"sanmd_covered2!C", [[META1:![0-9]+]]}
91-
; CHECK-SANMD: [[META1]] = !{i64 1}
92-
;.
9389
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
9490
; CHECK-DFSAN: {{.*}}

0 commit comments

Comments
 (0)