Skip to content

Commit 5675447

Browse files
committed
Reapply "[HWASAN] Implement selective instrumentation based on profiling information (#83503)"
Added REQUIRES: asserts, which should fix the build problem. This reverts commit f6f79d4.
1 parent 98418c2 commit 5675447

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
#include "llvm/ADT/MapVector.h"
1616
#include "llvm/ADT/STLExtras.h"
1717
#include "llvm/ADT/SmallVector.h"
18+
#include "llvm/ADT/Statistic.h"
1819
#include "llvm/ADT/StringExtras.h"
1920
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/Analysis/BlockFrequencyInfo.h"
2022
#include "llvm/Analysis/DomTreeUpdater.h"
2123
#include "llvm/Analysis/GlobalsModRef.h"
2224
#include "llvm/Analysis/PostDominators.h"
25+
#include "llvm/Analysis/ProfileSummaryInfo.h"
2326
#include "llvm/Analysis/StackSafetyAnalysis.h"
2427
#include "llvm/Analysis/TargetLibraryInfo.h"
2528
#include "llvm/Analysis/ValueTracking.h"
@@ -177,6 +180,18 @@ static cl::opt<bool> ClWithTls(
177180
"platforms that support this"),
178181
cl::Hidden, cl::init(true));
179182

183+
static cl::opt<bool>
184+
CSkipHotCode("hwasan-skip-hot-code",
185+
cl::desc("Do not instument hot functions based on FDO."),
186+
cl::Hidden, cl::init(false));
187+
188+
static cl::opt<int> HotPercentileCutoff("hwasan-percentile-cutoff-hot",
189+
cl::init(0));
190+
191+
STATISTIC(NumTotalFuncs, "Number of total funcs HWASAN");
192+
STATISTIC(NumInstrumentedFuncs, "Number of HWASAN instrumented funcs");
193+
STATISTIC(NumNoProfileSummaryFuncs, "Number of HWASAN funcs without PS");
194+
180195
// Mode for selecting how to insert frame record info into the stack ring
181196
// buffer.
182197
enum RecordStackHistoryMode {
@@ -1507,6 +1522,27 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
15071522
if (!F.hasFnAttribute(Attribute::SanitizeHWAddress))
15081523
return;
15091524

1525+
if (F.empty())
1526+
return;
1527+
1528+
NumTotalFuncs++;
1529+
if (CSkipHotCode) {
1530+
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1531+
ProfileSummaryInfo *PSI =
1532+
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
1533+
if (PSI && PSI->hasProfileSummary()) {
1534+
auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
1535+
if ((HotPercentileCutoff.getNumOccurrences() && HotPercentileCutoff >= 0)
1536+
? PSI->isFunctionHotInCallGraphNthPercentile(HotPercentileCutoff,
1537+
&F, BFI)
1538+
: PSI->isFunctionHotInCallGraph(&F, BFI))
1539+
return;
1540+
} else {
1541+
++NumNoProfileSummaryFuncs;
1542+
}
1543+
}
1544+
NumInstrumentedFuncs++;
1545+
15101546
LLVM_DEBUG(dbgs() << "Function: " << F.getName() << "\n");
15111547

15121548
SmallVector<InterestingMemoryOperand, 16> OperandsToInstrument;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -stats 2>&1 \
2+
; RUN: -hwasan-skip-hot-code=0 | FileCheck %s --check-prefix=FULL
3+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -stats 2>&1 \
4+
; RUN: -hwasan-skip-hot-code=1 | FileCheck %s --check-prefix=SELSAN
5+
6+
; REQUIRES: asserts
7+
8+
; FULL: 1 hwasan - Number of HWASAN instrumented funcs
9+
; FULL: 1 hwasan - Number of total funcs HWASAN
10+
11+
; SELSAN: 1 hwasan - Number of HWASAN instrumented funcs
12+
; SELSAN: 1 hwasan - Number of HWASAN funcs without PS
13+
; SELSAN: 1 hwasan - Number of total funcs HWASAN
14+
15+
define void @not_sanitized() { ret void }
16+
define void @sanitized_no_ps() sanitize_hwaddress { ret void }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -stats 2>&1 \
2+
; RUN: -hwasan-skip-hot-code=1 | FileCheck %s --check-prefix=DEFAULT
3+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -stats 2>&1 \
4+
; RUN: -hwasan-skip-hot-code=1 -hwasan-percentile-cutoff-hot=700000 | FileCheck %s --check-prefix=PERCENT
5+
6+
; REQUIRES: asserts
7+
8+
; DEFAULT: 1 hwasan - Number of total funcs HWASAN
9+
10+
; PERCENT: 1 hwasan - Number of HWASAN instrumented funcs
11+
; PERCENT: 1 hwasan - Number of total funcs HWASAN
12+
13+
define void @sanitized() sanitize_hwaddress !prof !36 { ret void }
14+
15+
!llvm.module.flags = !{!6}
16+
!6 = !{i32 1, !"ProfileSummary", !7}
17+
!7 = !{!8, !9, !10, !11, !12, !13, !14, !17}
18+
!8 = !{!"ProfileFormat", !"InstrProf"}
19+
!9 = !{!"TotalCount", i64 30000}
20+
!10 = !{!"MaxCount", i64 10000}
21+
!11 = !{!"MaxInternalCount", i64 10000}
22+
!12 = !{!"MaxFunctionCount", i64 10000}
23+
!13 = !{!"NumCounts", i64 3}
24+
!14 = !{!"NumFunctions", i64 5}
25+
!17 = !{!"DetailedSummary", !18}
26+
!18 = !{!19, !29, !30, !32, !34}
27+
!19 = !{i32 10000, i64 10000, i32 3}
28+
!29 = !{i32 950000, i64 5000, i32 3}
29+
!30 = !{i32 990000, i64 500, i32 4}
30+
!32 = !{i32 999900, i64 250, i32 4}
31+
!34 = !{i32 999999, i64 1, i32 6}
32+
33+
!36 = !{!"function_entry_count", i64 1000}

0 commit comments

Comments
 (0)