Skip to content

Commit b8e5d24

Browse files
Add attributes to synthetic functions.
Module flags represent the original intention.
1 parent 8f63d15 commit b8e5d24

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

llvm/lib/IR/Function.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,35 @@ Function *Function::createWithDefaultAttr(FunctionType *Ty,
403403
StringRef DefaultFeatures = F->getContext().getDefaultTargetFeatures();
404404
if (!DefaultFeatures.empty())
405405
B.addAttribute("target-features", DefaultFeatures);
406+
407+
// Check if the module attribute is present and not zero.
408+
auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool {
409+
const auto *Attr =
410+
mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr));
411+
return Attr && !Attr->isZero();
412+
};
413+
414+
auto AddAttributeIfSet = [&](const StringRef &ModAttr) {
415+
if (isModuleAttributeSet(ModAttr))
416+
B.addAttribute(ModAttr);
417+
};
418+
419+
StringRef SignType = "none";
420+
if (isModuleAttributeSet("sign-return-address"))
421+
SignType = "non-leaf";
422+
if (isModuleAttributeSet("sign-return-address-all"))
423+
SignType = "all";
424+
if (SignType != "none") {
425+
B.addAttribute("sign-return-address", SignType);
426+
B.addAttribute("sign-return-address-key",
427+
isModuleAttributeSet("sign-return-address-with-bkey")
428+
? "b_key"
429+
: "a_key");
430+
}
431+
AddAttributeIfSet("branch-target-enforcement");
432+
AddAttributeIfSet("branch-protection-pauth-lr");
433+
AddAttributeIfSet("guarded-control-stack");
434+
406435
F->addFnAttrs(B);
407436
return F;
408437
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;; Verify that the synthetic functions inherit their flags from the corresponding
2+
;; BTE and return address signing module flags.
3+
; RUN: opt < %s -passes=asan -S | FileCheck %s
4+
; REQUIRES: aarch64-registered-target
5+
6+
target triple = "aarch64-unknown-linux-gnu"
7+
8+
@g = dso_local global i32 0, align 4
9+
10+
define i32 @test_load() sanitize_address {
11+
entry:
12+
%tmp = load i32, ptr @g, align 4
13+
ret i32 %tmp
14+
}
15+
16+
!llvm.module.flags = !{!0, !1}
17+
18+
;; Due to -fasynchronous-unwind-tables.
19+
!0 = !{i32 7, !"uwtable", i32 2}
20+
21+
;; Due to -fno-omit-frame-pointer.
22+
!1 = !{i32 7, !"frame-pointer", i32 2}
23+
24+
!llvm.module.flags = !{!2, !3, !4}
25+
26+
!2 = !{i32 8, !"branch-target-enforcement", i32 1}
27+
!3 = !{i32 8, !"sign-return-address", i32 1}
28+
!4 = !{i32 8, !"sign-return-address-all", i32 0}
29+
30+
;; Set the uwtable attribute on ctor/dtor.
31+
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]]
32+
; CHECK: define internal void @asan.module_dtor() #[[#ATTR]]
33+
; CHECK: attributes #[[#ATTR]] = { nounwind uwtable "branch-target-enforcement" "frame-pointer"="all" "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;; Verify that the synthetic functions inherit their flags from the corresponding
2+
;; BTE and return address signing module flags.
3+
; RUN: opt < %s -passes=asan -S | FileCheck %s
4+
; REQUIRES: aarch64-registered-target
5+
6+
target triple = "aarch64-unknown-linux-gnu"
7+
8+
@g = dso_local global i32 0, align 4
9+
10+
define i32 @test_load() sanitize_memory {
11+
entry:
12+
%tmp = load i32, ptr @g, align 4
13+
ret i32 %tmp
14+
}
15+
16+
!llvm.module.flags = !{!0, !1}
17+
18+
;; Due to -fasynchronous-unwind-tables.
19+
!0 = !{i32 7, !"uwtable", i32 2}
20+
21+
;; Due to -fno-omit-frame-pointer.
22+
!1 = !{i32 7, !"frame-pointer", i32 2}
23+
24+
!llvm.module.flags = !{!2, !3, !4}
25+
26+
!2 = !{i32 8, !"branch-target-enforcement", i32 1}
27+
!3 = !{i32 8, !"sign-return-address", i32 1}
28+
!4 = !{i32 8, !"sign-return-address-all", i32 0}
29+
30+
;; Set the uwtable attribute on ctor/dtor.
31+
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]]
32+
; CHECK: define internal void @asan.module_dtor() #[[#ATTR]]
33+
; CHECK: attributes #[[#ATTR]] = { nounwind uwtable "branch-target-enforcement" "frame-pointer"="all" "sign-return-address"="non-leaf" "sign-return-address-key"="a_key" }

0 commit comments

Comments
 (0)