Skip to content

Commit 4c7fd7e

Browse files
[llvm][ARM][AArch64] Add attributes to synthetic functions. (#83153)
Module flags represent the original intention. Depends on #82819
1 parent e15d67c commit 4c7fd7e

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
@@ -407,6 +407,35 @@ Function *Function::createWithDefaultAttr(FunctionType *Ty,
407407
StringRef DefaultFeatures = F->getContext().getDefaultTargetFeatures();
408408
if (!DefaultFeatures.empty())
409409
B.addAttribute("target-features", DefaultFeatures);
410+
411+
// Check if the module attribute is present and not zero.
412+
auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool {
413+
const auto *Attr =
414+
mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr));
415+
return Attr && !Attr->isZero();
416+
};
417+
418+
auto AddAttributeIfSet = [&](const StringRef &ModAttr) {
419+
if (isModuleAttributeSet(ModAttr))
420+
B.addAttribute(ModAttr);
421+
};
422+
423+
StringRef SignType = "none";
424+
if (isModuleAttributeSet("sign-return-address"))
425+
SignType = "non-leaf";
426+
if (isModuleAttributeSet("sign-return-address-all"))
427+
SignType = "all";
428+
if (SignType != "none") {
429+
B.addAttribute("sign-return-address", SignType);
430+
B.addAttribute("sign-return-address-key",
431+
isModuleAttributeSet("sign-return-address-with-bkey")
432+
? "b_key"
433+
: "a_key");
434+
}
435+
AddAttributeIfSet("branch-target-enforcement");
436+
AddAttributeIfSet("branch-protection-pauth-lr");
437+
AddAttributeIfSet("guarded-control-stack");
438+
410439
F->addFnAttrs(B);
411440
return F;
412441
}
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)