Skip to content

Commit 460e733

Browse files
Add attributes to synthetic functions.
Module flags represent the original intention.
1 parent d4fb50d commit 460e733

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

llvm/lib/IR/Function.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,35 @@ Function *Function::createWithDefaultAttr(FunctionType *Ty,
378378
}
379379
if (M->getModuleFlag("function_return_thunk_extern"))
380380
B.addAttribute(Attribute::FnRetThunkExtern);
381+
382+
// Check if the module attribute is present and not zero.
383+
auto isModuleAttributeSet = [&](const StringRef &ModAttr) -> bool {
384+
const auto *Attr =
385+
mdconst::extract_or_null<ConstantInt>(M->getModuleFlag(ModAttr));
386+
return Attr && !Attr->isZero();
387+
};
388+
389+
auto AddAttributeIfSet = [&](const StringRef &ModAttr) {
390+
if (isModuleAttributeSet(ModAttr))
391+
B.addAttribute(ModAttr);
392+
};
393+
394+
StringRef SignType = "none";
395+
if (isModuleAttributeSet("sign-return-address"))
396+
SignType = "non-leaf";
397+
if (isModuleAttributeSet("sign-return-address-all"))
398+
SignType = "all";
399+
if (SignType != "none") {
400+
B.addAttribute("sign-return-address", SignType);
401+
B.addAttribute("sign-return-address-key",
402+
isModuleAttributeSet("sign-return-address-with-bkey")
403+
? "b_key"
404+
: "a_key");
405+
}
406+
AddAttributeIfSet("branch-target-enforcement");
407+
AddAttributeIfSet("branch-protection-pauth-lr");
408+
AddAttributeIfSet("guarded-control-stack");
409+
381410
F->addFnAttrs(B);
382411
return F;
383412
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: opt < %s -passes=asan -S | FileCheck %s
2+
; REQUIRES: aarch64-registered-target
3+
4+
target triple = "aarch64-unknown-linux-gnu"
5+
6+
@g = dso_local global i32 0, align 4
7+
8+
define i32 @test_load() sanitize_address {
9+
entry:
10+
%tmp = load i32, ptr @g, align 4
11+
ret i32 %tmp
12+
}
13+
14+
!llvm.module.flags = !{!0, !1}
15+
16+
;; Due to -fasynchronous-unwind-tables.
17+
!0 = !{i32 7, !"uwtable", i32 2}
18+
19+
;; Due to -fno-omit-frame-pointer.
20+
!1 = !{i32 7, !"frame-pointer", i32 2}
21+
22+
!llvm.module.flags = !{!2, !3, !4}
23+
24+
!2 = !{i32 8, !"branch-target-enforcement", i32 1}
25+
!3 = !{i32 8, !"sign-return-address", i32 1}
26+
!4 = !{i32 8, !"sign-return-address-all", i32 0}
27+
28+
;; Set the uwtable attribute on ctor/dtor.
29+
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]]
30+
; CHECK: define internal void @asan.module_dtor() #[[#ATTR]]
31+
; 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,31 @@
1+
; RUN: opt < %s -passes=asan -S | FileCheck %s
2+
; REQUIRES: aarch64-registered-target
3+
4+
target triple = "aarch64-unknown-linux-gnu"
5+
6+
@g = dso_local global i32 0, align 4
7+
8+
define i32 @test_load() sanitize_memory {
9+
entry:
10+
%tmp = load i32, ptr @g, align 4
11+
ret i32 %tmp
12+
}
13+
14+
!llvm.module.flags = !{!0, !1}
15+
16+
;; Due to -fasynchronous-unwind-tables.
17+
!0 = !{i32 7, !"uwtable", i32 2}
18+
19+
;; Due to -fno-omit-frame-pointer.
20+
!1 = !{i32 7, !"frame-pointer", i32 2}
21+
22+
!llvm.module.flags = !{!2, !3, !4}
23+
24+
!2 = !{i32 8, !"branch-target-enforcement", i32 1}
25+
!3 = !{i32 8, !"sign-return-address", i32 1}
26+
!4 = !{i32 8, !"sign-return-address-all", i32 0}
27+
28+
;; Set the uwtable attribute on ctor/dtor.
29+
; CHECK: define internal void @asan.module_ctor() #[[#ATTR:]]
30+
; CHECK: define internal void @asan.module_dtor() #[[#ATTR]]
31+
; 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)