Files
clang-p2996/llvm/test/Transforms/Inline/inline_ssp.ll
Nick Desaulniers bc044a88ee [Inline] prevent inlining on stack protector mismatch
It's common for code that manipulates the stack via inline assembly or
that has to set up its own stack canary (such as the Linux kernel) would
like to avoid stack protectors in certain functions. In this case, we've
been bitten by numerous bugs where a callee with a stack protector is
inlined into an attribute((no_stack_protector)) caller, which
generally breaks the caller's assumptions about not having a stack
protector. LTO exacerbates the issue.

While developers can avoid this by putting all no_stack_protector
functions in one translation unit together and compiling those with
-fno-stack-protector, it's generally not very ergonomic or as
ergonomic as a function attribute, and still doesn't work for LTO. See also:
https://lore.kernel.org/linux-pm/20200915172658.1432732-1-rkir@google.com/
https://lore.kernel.org/lkml/20200918201436.2932360-30-samitolvanen@google.com/T/#u

SSP attributes can be ordered by strength. Weakest to strongest, they
are: ssp, sspstrong, sspreq.  Callees with differing SSP attributes may be
inlined into each other, and the strongest attribute will be applied to the
caller. (No change)

After this change:
* A callee with no SSP attributes will no longer be inlined into a
  caller with SSP attributes.
* The reverse is also true: a callee with an SSP attribute will not be
  inlined into a caller with no SSP attributes.
* The alwaysinline attribute overrides these rules.

Functions that get synthesized by the compiler may not get inlined as a
result if they are not created with the same stack protector function
attribute as their callers.

Alternative approach to https://reviews.llvm.org/D87956.

Fixes pr/47479.

Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>

Reviewed By: rnk, MaskRay

Differential Revision: https://reviews.llvm.org/D91816
2020-12-02 11:00:16 -08:00

162 lines
3.9 KiB
LLVM

; RUN: opt -inline %s -S | FileCheck %s
; RUN: opt -passes='cgscc(inline)' %s -S | FileCheck %s
; Ensure SSP attributes are propagated correctly when inlining.
@.str = private unnamed_addr constant [11 x i8] c"fun_nossp\0A\00", align 1
@.str1 = private unnamed_addr constant [9 x i8] c"fun_ssp\0A\00", align 1
@.str2 = private unnamed_addr constant [15 x i8] c"fun_sspstrong\0A\00", align 1
@.str3 = private unnamed_addr constant [12 x i8] c"fun_sspreq\0A\00", align 1
; These first four functions (@fun_sspreq, @fun_sspstrong, @fun_ssp, @fun_nossp)
; are used by the remaining functions to ensure that the SSP attributes are
; propagated correctly. The caller should have its SSP attribute set as:
; strictest(caller-ssp-attr, callee-ssp-attr), where strictness is ordered as:
; sspreq > sspstrong > ssp > [no ssp]
define internal void @fun_sspreq() nounwind sspreq uwtable {
entry:
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str3, i32 0, i32 0))
ret void
}
define internal void @fun_sspstrong() nounwind sspstrong uwtable {
entry:
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str2, i32 0, i32 0))
ret void
}
define internal void @fun_ssp() nounwind ssp uwtable {
entry:
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str1, i32 0, i32 0))
ret void
}
define internal void @fun_nossp() nounwind uwtable {
entry:
%call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i32 0, i32 0))
ret void
}
; Tests start below
define void @inline_req_req() nounwind sspreq uwtable {
entry:
; CHECK: @inline_req_req() #0
call void @fun_sspreq()
ret void
}
define void @inline_req_strong() nounwind sspstrong uwtable {
entry:
; CHECK: @inline_req_strong() #0
call void @fun_sspreq()
ret void
}
define void @inline_req_ssp() nounwind ssp uwtable {
entry:
; CHECK: @inline_req_ssp() #0
call void @fun_sspreq()
ret void
}
define void @inline_req_nossp() nounwind uwtable {
entry:
; CHECK: @inline_req_nossp() #3
call void @fun_sspreq()
ret void
}
define void @inline_strong_req() nounwind sspreq uwtable {
entry:
; CHECK: @inline_strong_req() #0
call void @fun_sspstrong()
ret void
}
define void @inline_strong_strong() nounwind sspstrong uwtable {
entry:
; CHECK: @inline_strong_strong() #1
call void @fun_sspstrong()
ret void
}
define void @inline_strong_ssp() nounwind ssp uwtable {
entry:
; CHECK: @inline_strong_ssp() #1
call void @fun_sspstrong()
ret void
}
define void @inline_strong_nossp() nounwind uwtable {
entry:
; CHECK: @inline_strong_nossp() #3
call void @fun_sspstrong()
ret void
}
define void @inline_ssp_req() nounwind sspreq uwtable {
entry:
; CHECK: @inline_ssp_req() #0
call void @fun_ssp()
ret void
}
define void @inline_ssp_strong() nounwind sspstrong uwtable {
entry:
; CHECK: @inline_ssp_strong() #1
call void @fun_ssp()
ret void
}
define void @inline_ssp_ssp() nounwind ssp uwtable {
entry:
; CHECK: @inline_ssp_ssp() #2
call void @fun_ssp()
ret void
}
define void @inline_ssp_nossp() nounwind uwtable {
entry:
; CHECK: @inline_ssp_nossp() #3
call void @fun_ssp()
ret void
}
define void @inline_nossp_req() nounwind uwtable sspreq {
entry:
; CHECK: @inline_nossp_req() #0
call void @fun_nossp()
ret void
}
define void @inline_nossp_strong() nounwind sspstrong uwtable {
entry:
; CHECK: @inline_nossp_strong() #1
call void @fun_nossp()
ret void
}
define void @inline_nossp_ssp() nounwind ssp uwtable {
entry:
; CHECK: @inline_nossp_ssp() #2
call void @fun_nossp()
ret void
}
define void @inline_nossp_nossp() nounwind uwtable {
entry:
; CHECK: @inline_nossp_nossp() #3
call void @fun_nossp()
ret void
}
declare i32 @printf(i8*, ...)
; CHECK: attributes #0 = { nounwind sspreq uwtable }
; CHECK: attributes #1 = { nounwind sspstrong uwtable }
; CHECK: attributes #2 = { nounwind ssp uwtable }
; CHECK: attributes #3 = { nounwind uwtable }