Skip to content

Commit df88ae3

Browse files
committed
Software Bill of Mitigations Tests
- Add tests for verifying metadata added correctly - Added new parameter to only add mitigation metadata for functions when MitigationAnalysis CGO is enabled
1 parent a4b4ee1 commit df88ae3

File tree

5 files changed

+439
-11
lines changed

5 files changed

+439
-11
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,8 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
24922492
B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables));
24932493

24942494
AttachMitigationMetadataToFunction(*F, MitigationKey::STACK_CLASH_PROTECTION,
2495-
CodeGenOpts.StackClashProtector);
2495+
CodeGenOpts.StackClashProtector,
2496+
CodeGenOpts.MitigationAnalysis);
24962497
if (CodeGenOpts.StackClashProtector)
24972498
B.addAttribute("probe-stack", "inline-asm");
24982499

@@ -2521,18 +2522,21 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
25212522
!noStackProtectionAttr &&
25222523
(isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPOn) ||
25232524
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPStrong) ||
2524-
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq)));
2525+
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq)),
2526+
CodeGenOpts.MitigationAnalysis);
25252527

25262528
AttachMitigationMetadataToFunction(
25272529
*F, MitigationKey::STACK_PROTECTOR_STRONG,
25282530
!noStackProtectionAttr &&
25292531
(isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPStrong) ||
2530-
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq)));
2532+
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq)),
2533+
CodeGenOpts.MitigationAnalysis);
25312534

25322535
AttachMitigationMetadataToFunction(
25332536
*F, MitigationKey::STACK_PROTECTOR_ALL,
25342537
!noStackProtectionAttr &&
2535-
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq));
2538+
isStackProtectorOn(LangOpts, getTriple(), LangOptions::SSPReq),
2539+
CodeGenOpts.MitigationAnalysis);
25362540

25372541
if (!D) {
25382542
// Non-entry HLSL functions must always be inlined.

clang/lib/CodeGen/MitigationTagging.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,17 @@ MitigationKeyToString(enum MitigationKey key) noexcept {
4444
}
4545
}
4646

47+
///
48+
/// Store metadata (tied to the function) related to enablement of mitigations.
49+
/// @param mitigationAnalysisEnable - if false, do not attach metadata.
50+
///
4751
void AttachMitigationMetadataToFunction(llvm::Function &F,
48-
enum MitigationKey key, bool enabled) {
52+
enum MitigationKey key, bool enabled,
53+
bool mitigationAnalysisEnable) {
54+
if (!mitigationAnalysisEnable) {
55+
return;
56+
}
57+
4958
llvm::LLVMContext &Context = F.getContext();
5059

5160
unsigned kindID = Context.getMDKindID("security_mitigations");
@@ -68,16 +77,15 @@ void AttachMitigationMetadataToFunction(llvm::Function &F,
6877
llvm::MDNode *CombinedMD = llvm::MDNode::get(Context, MDs);
6978
F.setMetadata(kindID, CombinedMD);
7079
} else {
71-
F.setMetadata(kindID, NewMD);
80+
F.setMetadata(kindID, llvm::MDNode::get(
81+
Context, std::vector<llvm::Metadata *>{NewMD}));
7282
}
7383
}
7484

7585
void AttachMitigationMetadataToFunction(CodeGenFunction &CGF,
7686
enum MitigationKey key, bool enabled) {
77-
if (!CGF.CGM.getCodeGenOpts().MitigationAnalysis) {
78-
return;
79-
}
80-
AttachMitigationMetadataToFunction(*(CGF.CurFn), key, enabled);
87+
AttachMitigationMetadataToFunction(
88+
*(CGF.CurFn), key, enabled, CGF.CGM.getCodeGenOpts().MitigationAnalysis);
8189
}
8290

8391
} // namespace CodeGen

clang/lib/CodeGen/MitigationTagging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ enum class MitigationKey {
3535
};
3636

3737
void AttachMitigationMetadataToFunction(llvm::Function &F,
38-
enum MitigationKey key, bool enabled);
38+
enum MitigationKey key, bool enabled,
39+
bool mitigationAnalysisEnable);
3940
void AttachMitigationMetadataToFunction(CodeGenFunction &CGF,
4041
enum MitigationKey key, bool enabled);
4142

clang/unittests/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_clang_unittest(ClangCodeGenTests
99
CodeGenExternalTest.cpp
1010
TBAAMetadataTest.cpp
1111
CheckTargetFeaturesTest.cpp
12+
MitigationAnalysisTest.cpp
1213
)
1314

1415
clang_target_link_libraries(ClangCodeGenTests

0 commit comments

Comments
 (0)