summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorAndrew Paverd <andrew.paverd@microsoft.com>2020-01-10 11:08:18 +0000
committerDavid Chisnall <David.Chisnall@microsoft.com>2020-01-10 16:04:12 +0000
commitbdd88b7ed3956534a0a71b1ea2bc88c69d48f9b7 (patch)
tree4f0a7aeaf0f6eb0a34d373c6c74c93763669d6c4 /llvm
parentd864d93496c5fd0cc473953ab825f07e3d4c4e86 (diff)
downloadbcm5719-llvm-bdd88b7ed3956534a0a71b1ea2bc88c69d48f9b7.tar.gz
bcm5719-llvm-bdd88b7ed3956534a0a71b1ea2bc88c69d48f9b7.zip
Add support for __declspec(guard(nocf))
Summary: Avoid using the `nocf_check` attribute with Control Flow Guard. Instead, use a new `"guard_nocf"` function attribute to indicate that checks should not be added on indirect calls within that function. Add support for `__declspec(guard(nocf))` following the same syntax as MSVC. Reviewers: rnk, dmajor, pcc, hans, aaron.ballman Reviewed By: aaron.ballman Subscribers: aaron.ballman, tomrittervg, hiraditya, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D72167
Diffstat (limited to 'llvm')
-rw-r--r--llvm/lib/Transforms/CFGuard/CFGuard.cpp10
-rw-r--r--llvm/test/CodeGen/AArch64/cfguard-checks.ll10
-rw-r--r--llvm/test/CodeGen/ARM/cfguard-checks.ll22
-rw-r--r--llvm/test/CodeGen/X86/cfguard-checks.ll12
4 files changed, 26 insertions, 28 deletions
diff --git a/llvm/lib/Transforms/CFGuard/CFGuard.cpp b/llvm/lib/Transforms/CFGuard/CFGuard.cpp
index 3eca00691e0..7c5e90cb53c 100644
--- a/llvm/lib/Transforms/CFGuard/CFGuard.cpp
+++ b/llvm/lib/Transforms/CFGuard/CFGuard.cpp
@@ -254,8 +254,8 @@ bool CFGuard::doInitialization(Module &M) {
bool CFGuard::runOnFunction(Function &F) {
- // Skip modules and functions for which CFGuard checks have been disabled.
- if (cfguard_module_flag != 2 || F.hasFnAttribute(Attribute::NoCfCheck))
+ // Skip modules for which CFGuard checks have been disabled.
+ if (cfguard_module_flag != 2)
return false;
SmallVector<CallBase *, 8> IndirectCalls;
@@ -267,17 +267,15 @@ bool CFGuard::runOnFunction(Function &F) {
for (BasicBlock &BB : F.getBasicBlockList()) {
for (Instruction &I : BB.getInstList()) {
auto *CB = dyn_cast<CallBase>(&I);
- if (CB && CB->isIndirectCall()) {
+ if (CB && CB->isIndirectCall() && !CB->hasFnAttr("guard_nocf")) {
IndirectCalls.push_back(CB);
CFGuardCounter++;
}
}
}
- // If no checks are needed, return early and add this attribute to indicate
- // that subsequent CFGuard passes can skip this function.
+ // If no checks are needed, return early.
if (IndirectCalls.empty()) {
- F.addFnAttr(Attribute::NoCfCheck);
return false;
}
diff --git a/llvm/test/CodeGen/AArch64/cfguard-checks.ll b/llvm/test/CodeGen/AArch64/cfguard-checks.ll
index 627741c4b6f..5ebe1dd1365 100644
--- a/llvm/test/CodeGen/AArch64/cfguard-checks.ll
+++ b/llvm/test/CodeGen/AArch64/cfguard-checks.ll
@@ -7,22 +7,22 @@
declare i32 @target_func()
-; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute.
-define i32 @func_nocf_checks() #0 {
+; Test that Control Flow Guard checks are not added on calls with the "guard_nocf" attribute.
+define i32 @func_guard_nocf() {
entry:
%func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8
- %1 = call i32 %0()
+ %1 = call i32 %0() #0
ret i32 %1
- ; CHECK-LABEL: func_nocf_checks
+ ; CHECK-LABEL: func_guard_nocf
; CHECK: adrp x8, target_func
; CHECK: add x8, x8, target_func
; CHECK-NOT: __guard_check_icall_fptr
; CHECK: blr x8
}
-attributes #0 = { nocf_check }
+attributes #0 = { "guard_nocf" }
; Test that Control Flow Guard checks are added even at -O0.
diff --git a/llvm/test/CodeGen/ARM/cfguard-checks.ll b/llvm/test/CodeGen/ARM/cfguard-checks.ll
index c75afc614c7..3fab04eb166 100644
--- a/llvm/test/CodeGen/ARM/cfguard-checks.ll
+++ b/llvm/test/CodeGen/ARM/cfguard-checks.ll
@@ -7,26 +7,27 @@
declare i32 @target_func()
-; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute.
-define i32 @func_nocf_checks() #0 {
+; Test that Control Flow Guard checks are not added on calls with the "guard_nocf" attribute.
+define i32 @func_guard_nocf() #0 {
entry:
%func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8
- %1 = call arm_aapcs_vfpcc i32 %0()
+ %1 = call arm_aapcs_vfpcc i32 %0() #1
ret i32 %1
- ; CHECK-LABEL: func_nocf_checks
+ ; CHECK-LABEL: func_guard_nocf
; CHECK: movw r0, :lower16:target_func
; CHECK: movt r0, :upper16:target_func
; CHECK-NOT: __guard_check_icall_fptr
; CHECK: blx r0
}
-attributes #0 = { nocf_check "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
+attributes #0 = { "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
+attributes #1 = { "guard_nocf" }
; Test that Control Flow Guard checks are added even at -O0.
-define i32 @func_optnone_cf() #1 {
+define i32 @func_optnone_cf() #2 {
entry:
%func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8
@@ -47,11 +48,11 @@ entry:
; CHECK: blx r1
; CHECK-NEXT: blx r4
}
-attributes #1 = { noinline optnone "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
+attributes #2 = { noinline optnone "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
; Test that Control Flow Guard checks are correctly added in optimized code (common case).
-define i32 @func_cf() #2 {
+define i32 @func_cf() #0 {
entry:
%func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8
@@ -70,11 +71,10 @@ entry:
; CHECK: blx r1
; CHECK-NEXT: blx r4
}
-attributes #2 = { "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
; Test that Control Flow Guard checks are correctly added on invoke instructions.
-define i32 @func_cf_invoke() #2 personality i8* bitcast (void ()* @h to i8*) {
+define i32 @func_cf_invoke() #0 personality i8* bitcast (void ()* @h to i8*) {
entry:
%0 = alloca i32, align 4
%func_ptr = alloca i32 ()*, align 8
@@ -112,7 +112,7 @@ declare void @h()
%struct._SETJMP_FLOAT128 = type { [2 x i64] }
@buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16
-define i32 @func_cf_setjmp() #2 {
+define i32 @func_cf_setjmp() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
diff --git a/llvm/test/CodeGen/X86/cfguard-checks.ll b/llvm/test/CodeGen/X86/cfguard-checks.ll
index 6e8d71411e8..8da06680cf5 100644
--- a/llvm/test/CodeGen/X86/cfguard-checks.ll
+++ b/llvm/test/CodeGen/X86/cfguard-checks.ll
@@ -8,26 +8,26 @@
declare i32 @target_func()
-; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute.
-define i32 @func_nocf_checks() #0 {
+; Test that Control Flow Guard checks are not added on calls with the "guard_nocf" attribute.
+define i32 @func_guard_nocf() {
entry:
%func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8
- %1 = call i32 %0()
+ %1 = call i32 %0() #0
ret i32 %1
- ; X32-LABEL: func_nocf_checks
+ ; X32-LABEL: func_guard_nocf
; X32: movl $_target_func, %eax
; X32-NOT: __guard_check_icall_fptr
; X32: calll *%eax
- ; X64-LABEL: func_nocf_checks
+ ; X64-LABEL: func_guard_nocf
; X64: leaq target_func(%rip), %rax
; X64-NOT: __guard_dispatch_icall_fptr
; X64: callq *%rax
}
-attributes #0 = { nocf_check }
+attributes #0 = { "guard_nocf" }
; Test that Control Flow Guard checks are added even at -O0.
OpenPOWER on IntegriCloud