2017-12-06 04:22:20 +08:00
|
|
|
; RUN: llc -mtriple=i386-pc-windows-msvc < %s -o - | FileCheck -check-prefix=MSVC-X86 %s
|
|
|
|
; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s -o - | FileCheck -check-prefix=MSVC-X64 %s
|
[stack-protection] Add support for MSVC buffer security check
Summary:
This patch is adding support for the MSVC buffer security check implementation
The buffer security check is turned on with the '/GS' compiler switch.
* https://msdn.microsoft.com/en-us/library/8dbf701c.aspx
* To be added to clang here: http://reviews.llvm.org/D20347
Some overview of buffer security check feature and implementation:
* https://msdn.microsoft.com/en-us/library/aa290051(VS.71).aspx
* http://www.ksyash.com/2011/01/buffer-overflow-protection-3/
* http://blog.osom.info/2012/02/understanding-vs-c-compilers-buffer.html
For the following example:
```
int example(int offset, int index) {
char buffer[10];
memset(buffer, 0xCC, index);
return buffer[index];
}
```
The MSVC compiler is adding these instructions to perform stack integrity check:
```
push ebp
mov ebp,esp
sub esp,50h
[1] mov eax,dword ptr [__security_cookie (01068024h)]
[2] xor eax,ebp
[3] mov dword ptr [ebp-4],eax
push ebx
push esi
push edi
mov eax,dword ptr [index]
push eax
push 0CCh
lea ecx,[buffer]
push ecx
call _memset (010610B9h)
add esp,0Ch
mov eax,dword ptr [index]
movsx eax,byte ptr buffer[eax]
pop edi
pop esi
pop ebx
[4] mov ecx,dword ptr [ebp-4]
[5] xor ecx,ebp
[6] call @__security_check_cookie@4 (01061276h)
mov esp,ebp
pop ebp
ret
```
The instrumentation above is:
* [1] is loading the global security canary,
* [3] is storing the local computed ([2]) canary to the guard slot,
* [4] is loading the guard slot and ([5]) re-compute the global canary,
* [6] is validating the resulting canary with the '__security_check_cookie' and performs error handling.
Overview of the current stack-protection implementation:
* lib/CodeGen/StackProtector.cpp
* There is a default stack-protection implementation applied on intermediate representation.
* The target can overload 'getIRStackGuard' method if it has a standard location for the stack protector cookie.
* An intrinsic 'Intrinsic::stackprotector' is added to the prologue. It will be expanded by the instruction selection pass (DAG or Fast).
* Basic Blocks are added to every instrumented function to receive the code for handling stack guard validation and errors handling.
* Guard manipulation and comparison are added directly to the intermediate representation.
* lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
* lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
* There is an implementation that adds instrumentation during instruction selection (for better handling of sibbling calls).
* see long comment above 'class StackProtectorDescriptor' declaration.
* The target needs to override 'getSDagStackGuard' to activate SDAG stack protection generation. (note: getIRStackGuard MUST be nullptr).
* 'getSDagStackGuard' returns the appropriate stack guard (security cookie)
* The code is generated by 'SelectionDAGBuilder.cpp' and 'SelectionDAGISel.cpp'.
* include/llvm/Target/TargetLowering.h
* Contains function to retrieve the default Guard 'Value'; should be overriden by each target to select which implementation is used and provide Guard 'Value'.
* lib/Target/X86/X86ISelLowering.cpp
* Contains the x86 specialisation; Guard 'Value' used by the SelectionDAG algorithm.
Function-based Instrumentation:
* The MSVC doesn't inline the stack guard comparison in every function. Instead, a call to '__security_check_cookie' is added to the epilogue before every return instructions.
* To support function-based instrumentation, this patch is
* adding a function to get the function-based check (llvm 'Value', see include/llvm/Target/TargetLowering.h),
* If provided, the stack protection instrumentation won't be inlined and a call to that function will be added to the prologue.
* modifying (SelectionDAGISel.cpp) do avoid producing basic blocks used for inline instrumentation,
* generating the function-based instrumentation during the ISEL pass (SelectionDAGBuilder.cpp),
* if FastISEL (not SelectionDAG), using the fallback which rely on the same function-based implemented over intermediate representation (StackProtector.cpp).
Modifications
* adding support for MSVC (lib/Target/X86/X86ISelLowering.cpp)
* adding support function-based instrumentation (lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp, .h)
Results
* IR generated instrumentation:
```
clang-cl /GS test.cc /Od /c -mllvm -print-isel-input
```
```
*** Final LLVM Code input to ISel ***
; Function Attrs: nounwind sspstrong
define i32 @"\01?example@@YAHHH@Z"(i32 %offset, i32 %index) #0 {
entry:
%StackGuardSlot = alloca i8* <<<-- Allocated guard slot
%0 = call i8* @llvm.stackguard() <<<-- Loading Stack Guard value
call void @llvm.stackprotector(i8* %0, i8** %StackGuardSlot) <<<-- Prologue intrinsic call (store to Guard slot)
%index.addr = alloca i32, align 4
%offset.addr = alloca i32, align 4
%buffer = alloca [10 x i8], align 1
store i32 %index, i32* %index.addr, align 4
store i32 %offset, i32* %offset.addr, align 4
%arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %buffer, i32 0, i32 0
%1 = load i32, i32* %index.addr, align 4
call void @llvm.memset.p0i8.i32(i8* %arraydecay, i8 -52, i32 %1, i32 1, i1 false)
%2 = load i32, i32* %index.addr, align 4
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buffer, i32 0, i32 %2
%3 = load i8, i8* %arrayidx, align 1
%conv = sext i8 %3 to i32
%4 = load volatile i8*, i8** %StackGuardSlot <<<-- Loading Guard slot
call void @__security_check_cookie(i8* %4) <<<-- Epilogue function-based check
ret i32 %conv
}
```
* SelectionDAG generated instrumentation:
```
clang-cl /GS test.cc /O1 /c /FA
```
```
"?example@@YAHHH@Z": # @"\01?example@@YAHHH@Z"
# BB#0: # %entry
pushl %esi
subl $16, %esp
movl ___security_cookie, %eax <<<-- Loading Stack Guard value
movl 28(%esp), %esi
movl %eax, 12(%esp) <<<-- Store to Guard slot
leal 2(%esp), %eax
pushl %esi
pushl $204
pushl %eax
calll _memset
addl $12, %esp
movsbl 2(%esp,%esi), %esi
movl 12(%esp), %ecx <<<-- Loading Guard slot
calll @__security_check_cookie@4 <<<-- Epilogue function-based check
movl %esi, %eax
addl $16, %esp
popl %esi
retl
```
Reviewers: kcc, pcc, eugenis, rnk
Subscribers: majnemer, llvm-commits, hans, thakis, rnk
Differential Revision: http://reviews.llvm.org/D20346
llvm-svn: 272053
2016-06-08 04:15:35 +08:00
|
|
|
|
2017-12-06 04:22:20 +08:00
|
|
|
; Make sure fastisel falls back and does something secure.
|
|
|
|
; RUN: llc -mtriple=i686-pc-windows-msvc -O0 < %s -o - | FileCheck -check-prefix=MSVC-X86-O0 %s
|
|
|
|
; RUN: llc -mtriple=x86_64-pc-windows-msvc -O0 < %s -o - | FileCheck -check-prefix=MSVC-X64-O0 %s
|
[stack-protection] Add support for MSVC buffer security check
Summary:
This patch is adding support for the MSVC buffer security check implementation
The buffer security check is turned on with the '/GS' compiler switch.
* https://msdn.microsoft.com/en-us/library/8dbf701c.aspx
* To be added to clang here: http://reviews.llvm.org/D20347
Some overview of buffer security check feature and implementation:
* https://msdn.microsoft.com/en-us/library/aa290051(VS.71).aspx
* http://www.ksyash.com/2011/01/buffer-overflow-protection-3/
* http://blog.osom.info/2012/02/understanding-vs-c-compilers-buffer.html
For the following example:
```
int example(int offset, int index) {
char buffer[10];
memset(buffer, 0xCC, index);
return buffer[index];
}
```
The MSVC compiler is adding these instructions to perform stack integrity check:
```
push ebp
mov ebp,esp
sub esp,50h
[1] mov eax,dword ptr [__security_cookie (01068024h)]
[2] xor eax,ebp
[3] mov dword ptr [ebp-4],eax
push ebx
push esi
push edi
mov eax,dword ptr [index]
push eax
push 0CCh
lea ecx,[buffer]
push ecx
call _memset (010610B9h)
add esp,0Ch
mov eax,dword ptr [index]
movsx eax,byte ptr buffer[eax]
pop edi
pop esi
pop ebx
[4] mov ecx,dword ptr [ebp-4]
[5] xor ecx,ebp
[6] call @__security_check_cookie@4 (01061276h)
mov esp,ebp
pop ebp
ret
```
The instrumentation above is:
* [1] is loading the global security canary,
* [3] is storing the local computed ([2]) canary to the guard slot,
* [4] is loading the guard slot and ([5]) re-compute the global canary,
* [6] is validating the resulting canary with the '__security_check_cookie' and performs error handling.
Overview of the current stack-protection implementation:
* lib/CodeGen/StackProtector.cpp
* There is a default stack-protection implementation applied on intermediate representation.
* The target can overload 'getIRStackGuard' method if it has a standard location for the stack protector cookie.
* An intrinsic 'Intrinsic::stackprotector' is added to the prologue. It will be expanded by the instruction selection pass (DAG or Fast).
* Basic Blocks are added to every instrumented function to receive the code for handling stack guard validation and errors handling.
* Guard manipulation and comparison are added directly to the intermediate representation.
* lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
* lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
* There is an implementation that adds instrumentation during instruction selection (for better handling of sibbling calls).
* see long comment above 'class StackProtectorDescriptor' declaration.
* The target needs to override 'getSDagStackGuard' to activate SDAG stack protection generation. (note: getIRStackGuard MUST be nullptr).
* 'getSDagStackGuard' returns the appropriate stack guard (security cookie)
* The code is generated by 'SelectionDAGBuilder.cpp' and 'SelectionDAGISel.cpp'.
* include/llvm/Target/TargetLowering.h
* Contains function to retrieve the default Guard 'Value'; should be overriden by each target to select which implementation is used and provide Guard 'Value'.
* lib/Target/X86/X86ISelLowering.cpp
* Contains the x86 specialisation; Guard 'Value' used by the SelectionDAG algorithm.
Function-based Instrumentation:
* The MSVC doesn't inline the stack guard comparison in every function. Instead, a call to '__security_check_cookie' is added to the epilogue before every return instructions.
* To support function-based instrumentation, this patch is
* adding a function to get the function-based check (llvm 'Value', see include/llvm/Target/TargetLowering.h),
* If provided, the stack protection instrumentation won't be inlined and a call to that function will be added to the prologue.
* modifying (SelectionDAGISel.cpp) do avoid producing basic blocks used for inline instrumentation,
* generating the function-based instrumentation during the ISEL pass (SelectionDAGBuilder.cpp),
* if FastISEL (not SelectionDAG), using the fallback which rely on the same function-based implemented over intermediate representation (StackProtector.cpp).
Modifications
* adding support for MSVC (lib/Target/X86/X86ISelLowering.cpp)
* adding support function-based instrumentation (lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp, .h)
Results
* IR generated instrumentation:
```
clang-cl /GS test.cc /Od /c -mllvm -print-isel-input
```
```
*** Final LLVM Code input to ISel ***
; Function Attrs: nounwind sspstrong
define i32 @"\01?example@@YAHHH@Z"(i32 %offset, i32 %index) #0 {
entry:
%StackGuardSlot = alloca i8* <<<-- Allocated guard slot
%0 = call i8* @llvm.stackguard() <<<-- Loading Stack Guard value
call void @llvm.stackprotector(i8* %0, i8** %StackGuardSlot) <<<-- Prologue intrinsic call (store to Guard slot)
%index.addr = alloca i32, align 4
%offset.addr = alloca i32, align 4
%buffer = alloca [10 x i8], align 1
store i32 %index, i32* %index.addr, align 4
store i32 %offset, i32* %offset.addr, align 4
%arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %buffer, i32 0, i32 0
%1 = load i32, i32* %index.addr, align 4
call void @llvm.memset.p0i8.i32(i8* %arraydecay, i8 -52, i32 %1, i32 1, i1 false)
%2 = load i32, i32* %index.addr, align 4
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buffer, i32 0, i32 %2
%3 = load i8, i8* %arrayidx, align 1
%conv = sext i8 %3 to i32
%4 = load volatile i8*, i8** %StackGuardSlot <<<-- Loading Guard slot
call void @__security_check_cookie(i8* %4) <<<-- Epilogue function-based check
ret i32 %conv
}
```
* SelectionDAG generated instrumentation:
```
clang-cl /GS test.cc /O1 /c /FA
```
```
"?example@@YAHHH@Z": # @"\01?example@@YAHHH@Z"
# BB#0: # %entry
pushl %esi
subl $16, %esp
movl ___security_cookie, %eax <<<-- Loading Stack Guard value
movl 28(%esp), %esi
movl %eax, 12(%esp) <<<-- Store to Guard slot
leal 2(%esp), %eax
pushl %esi
pushl $204
pushl %eax
calll _memset
addl $12, %esp
movsbl 2(%esp,%esi), %esi
movl 12(%esp), %ecx <<<-- Loading Guard slot
calll @__security_check_cookie@4 <<<-- Epilogue function-based check
movl %esi, %eax
addl $16, %esp
popl %esi
retl
```
Reviewers: kcc, pcc, eugenis, rnk
Subscribers: majnemer, llvm-commits, hans, thakis, rnk
Differential Revision: http://reviews.llvm.org/D20346
llvm-svn: 272053
2016-06-08 04:15:35 +08:00
|
|
|
|
|
|
|
@"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00" ; <[11 x i8]*> [#uses=1]
|
|
|
|
|
|
|
|
define void @test(i8* %a) nounwind ssp {
|
|
|
|
entry:
|
|
|
|
%a_addr = alloca i8* ; <i8**> [#uses=2]
|
|
|
|
%buf = alloca [8 x i8] ; <[8 x i8]*> [#uses=2]
|
|
|
|
store i8* %a, i8** %a_addr
|
|
|
|
%buf1 = bitcast [8 x i8]* %buf to i8* ; <i8*> [#uses=1]
|
|
|
|
%0 = load i8*, i8** %a_addr, align 4 ; <i8*> [#uses=1]
|
|
|
|
%1 = call i8* @strcpy(i8* %buf1, i8* %0) nounwind ; <i8*> [#uses=0]
|
|
|
|
%buf2 = bitcast [8 x i8]* %buf to i8* ; <i8*> [#uses=1]
|
|
|
|
%2 = call i32 (i8*, ...) @printf(i8* getelementptr ([11 x i8], [11 x i8]* @"\01LC", i32 0, i32 0), i8* %buf2) nounwind ; <i32> [#uses=0]
|
|
|
|
br label %return
|
|
|
|
|
|
|
|
return: ; preds = %entry
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
2017-12-06 04:22:20 +08:00
|
|
|
; MSVC-X86-LABEL: _test:
|
|
|
|
; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X86: xorl %esp, %[[REG1]]
|
|
|
|
; MSVC-X86: movl %[[REG1]], [[SLOT:[0-9]*]](%esp)
|
|
|
|
; MSVC-X86: calll _strcpy
|
|
|
|
; MSVC-X86: movl [[SLOT]](%esp), %ecx
|
|
|
|
; MSVC-X86: xorl %esp, %ecx
|
|
|
|
; MSVC-X86: calll @__security_check_cookie@4
|
|
|
|
; MSVC-X86: retl
|
|
|
|
|
|
|
|
; MSVC-X64-LABEL: test:
|
|
|
|
; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X64: xorq %rsp, %[[REG1]]
|
|
|
|
; MSVC-X64: movq %[[REG1]], [[SLOT:[0-9]*]](%rsp)
|
|
|
|
; MSVC-X64: callq strcpy
|
|
|
|
; MSVC-X64: movq [[SLOT]](%rsp), %rcx
|
|
|
|
; MSVC-X64: xorq %rsp, %rcx
|
|
|
|
; MSVC-X64: callq __security_check_cookie
|
|
|
|
; MSVC-X64: retq
|
|
|
|
|
|
|
|
; MSVC-X86-O0-LABEL: _test:
|
|
|
|
; MSVC-X86-O0: movl ___security_cookie, %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X86-O0: xorl %esp, %[[REG1]]
|
|
|
|
; MSVC-X86-O0: movl %[[REG1]], [[SLOT:[0-9]*]](%esp)
|
|
|
|
; MSVC-X86-O0: calll _strcpy
|
|
|
|
; MSVC-X86-O0: movl [[SLOT]](%esp), %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X86-O0: xorl %esp, %[[REG1]]
|
|
|
|
; MSVC-X86-O0: movl %[[REG1]], %ecx
|
|
|
|
; MSVC-X86-O0: calll @__security_check_cookie@4
|
|
|
|
; MSVC-X86-O0: retl
|
|
|
|
|
|
|
|
; MSVC-X64-O0-LABEL: test:
|
|
|
|
; MSVC-X64-O0: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X64-O0: xorq %rsp, %[[REG1]]
|
|
|
|
; MSVC-X64-O0: movq %[[REG1]], [[SLOT:[0-9]*]](%rsp)
|
|
|
|
; MSVC-X64-O0: callq strcpy
|
|
|
|
; MSVC-X64-O0: movq [[SLOT]](%rsp), %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X64-O0: xorq %rsp, %[[REG1]]
|
|
|
|
; MSVC-X64-O0: movq %[[REG1]], %rcx
|
|
|
|
; MSVC-X64-O0: callq __security_check_cookie
|
|
|
|
; MSVC-X64-O0: retq
|
|
|
|
|
|
|
|
|
|
|
|
declare void @escape(i32*)
|
|
|
|
|
|
|
|
define void @test_vla(i32 %n) nounwind ssp {
|
|
|
|
%vla = alloca i32, i32 %n
|
|
|
|
call void @escape(i32* %vla)
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; MSVC-X86-LABEL: _test_vla:
|
|
|
|
; MSVC-X86: pushl %ebp
|
|
|
|
; MSVC-X86: movl %esp, %ebp
|
|
|
|
; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X86: xorl %ebp, %[[REG1]]
|
|
|
|
; MSVC-X86: movl %[[REG1]], [[SLOT:-[0-9]*]](%ebp)
|
|
|
|
; MSVC-X86: calll __chkstk
|
|
|
|
; MSVC-X86: pushl
|
|
|
|
; MSVC-X86: calll _escape
|
|
|
|
; MSVC-X86: movl [[SLOT]](%ebp), %ecx
|
|
|
|
; MSVC-X86: xorl %ebp, %ecx
|
|
|
|
; MSVC-X86: calll @__security_check_cookie@4
|
|
|
|
; MSVC-X86: movl %ebp, %esp
|
|
|
|
; MSVC-X86: popl %ebp
|
|
|
|
; MSVC-X86: retl
|
|
|
|
|
|
|
|
; MSVC-X64-LABEL: test_vla:
|
|
|
|
; MSVC-X64: pushq %rbp
|
|
|
|
; MSVC-X64: subq $16, %rsp
|
|
|
|
; MSVC-X64: leaq 16(%rsp), %rbp
|
|
|
|
; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X64: xorq %rbp, %[[REG1]]
|
|
|
|
; MSVC-X64: movq %[[REG1]], [[SLOT:-[0-9]*]](%rbp)
|
|
|
|
; MSVC-X64: callq __chkstk
|
|
|
|
; MSVC-X64: callq escape
|
|
|
|
; MSVC-X64: movq [[SLOT]](%rbp), %rcx
|
|
|
|
; MSVC-X64: xorq %rbp, %rcx
|
|
|
|
; MSVC-X64: callq __security_check_cookie
|
|
|
|
; MSVC-X64: retq
|
|
|
|
|
|
|
|
|
|
|
|
; This case is interesting because we address local variables with RBX but XOR
|
|
|
|
; the guard value with RBP. That's fine, either value will do, as long as they
|
|
|
|
; are the same across the life of the frame.
|
|
|
|
|
|
|
|
define void @test_vla_realign(i32 %n) nounwind ssp {
|
|
|
|
%realign = alloca i32, align 32
|
|
|
|
%vla = alloca i32, i32 %n
|
|
|
|
call void @escape(i32* %realign)
|
|
|
|
call void @escape(i32* %vla)
|
|
|
|
ret void
|
|
|
|
}
|
|
|
|
|
|
|
|
; MSVC-X86-LABEL: _test_vla_realign:
|
|
|
|
; MSVC-X86: pushl %ebp
|
|
|
|
; MSVC-X86: movl %esp, %ebp
|
|
|
|
; MSVC-X86: pushl %esi
|
|
|
|
; MSVC-X86: andl $-32, %esp
|
|
|
|
; MSVC-X86: subl $32, %esp
|
|
|
|
; MSVC-X86: movl %esp, %esi
|
|
|
|
; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X86: xorl %ebp, %[[REG1]]
|
|
|
|
; MSVC-X86: movl %[[REG1]], [[SLOT:[0-9]*]](%esi)
|
|
|
|
; MSVC-X86: calll __chkstk
|
|
|
|
; MSVC-X86: pushl
|
|
|
|
; MSVC-X86: calll _escape
|
|
|
|
; MSVC-X86: movl [[SLOT]](%esi), %ecx
|
|
|
|
; MSVC-X86: xorl %ebp, %ecx
|
|
|
|
; MSVC-X86: calll @__security_check_cookie@4
|
|
|
|
; MSVC-X86: leal -8(%ebp), %esp
|
|
|
|
; MSVC-X86: popl %esi
|
|
|
|
; MSVC-X86: popl %ebp
|
|
|
|
; MSVC-X86: retl
|
|
|
|
|
|
|
|
; MSVC-X64-LABEL: test_vla_realign:
|
|
|
|
; MSVC-X64: pushq %rbp
|
|
|
|
; MSVC-X64: pushq %rbx
|
|
|
|
; MSVC-X64: subq $32, %rsp
|
|
|
|
; MSVC-X64: leaq 32(%rsp), %rbp
|
|
|
|
; MSVC-X64: andq $-32, %rsp
|
|
|
|
; MSVC-X64: movq %rsp, %rbx
|
|
|
|
; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
|
|
|
|
; MSVC-X64: xorq %rbp, %[[REG1]]
|
|
|
|
; MSVC-X64: movq %[[REG1]], [[SLOT:[0-9]*]](%rbx)
|
|
|
|
; MSVC-X64: callq __chkstk
|
|
|
|
; MSVC-X64: callq escape
|
|
|
|
; MSVC-X64: movq [[SLOT]](%rbx), %rcx
|
|
|
|
; MSVC-X64: xorq %rbp, %rcx
|
|
|
|
; MSVC-X64: callq __security_check_cookie
|
|
|
|
; MSVC-X64: retq
|
|
|
|
|
|
|
|
|
[stack-protection] Add support for MSVC buffer security check
Summary:
This patch is adding support for the MSVC buffer security check implementation
The buffer security check is turned on with the '/GS' compiler switch.
* https://msdn.microsoft.com/en-us/library/8dbf701c.aspx
* To be added to clang here: http://reviews.llvm.org/D20347
Some overview of buffer security check feature and implementation:
* https://msdn.microsoft.com/en-us/library/aa290051(VS.71).aspx
* http://www.ksyash.com/2011/01/buffer-overflow-protection-3/
* http://blog.osom.info/2012/02/understanding-vs-c-compilers-buffer.html
For the following example:
```
int example(int offset, int index) {
char buffer[10];
memset(buffer, 0xCC, index);
return buffer[index];
}
```
The MSVC compiler is adding these instructions to perform stack integrity check:
```
push ebp
mov ebp,esp
sub esp,50h
[1] mov eax,dword ptr [__security_cookie (01068024h)]
[2] xor eax,ebp
[3] mov dword ptr [ebp-4],eax
push ebx
push esi
push edi
mov eax,dword ptr [index]
push eax
push 0CCh
lea ecx,[buffer]
push ecx
call _memset (010610B9h)
add esp,0Ch
mov eax,dword ptr [index]
movsx eax,byte ptr buffer[eax]
pop edi
pop esi
pop ebx
[4] mov ecx,dword ptr [ebp-4]
[5] xor ecx,ebp
[6] call @__security_check_cookie@4 (01061276h)
mov esp,ebp
pop ebp
ret
```
The instrumentation above is:
* [1] is loading the global security canary,
* [3] is storing the local computed ([2]) canary to the guard slot,
* [4] is loading the guard slot and ([5]) re-compute the global canary,
* [6] is validating the resulting canary with the '__security_check_cookie' and performs error handling.
Overview of the current stack-protection implementation:
* lib/CodeGen/StackProtector.cpp
* There is a default stack-protection implementation applied on intermediate representation.
* The target can overload 'getIRStackGuard' method if it has a standard location for the stack protector cookie.
* An intrinsic 'Intrinsic::stackprotector' is added to the prologue. It will be expanded by the instruction selection pass (DAG or Fast).
* Basic Blocks are added to every instrumented function to receive the code for handling stack guard validation and errors handling.
* Guard manipulation and comparison are added directly to the intermediate representation.
* lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
* lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
* There is an implementation that adds instrumentation during instruction selection (for better handling of sibbling calls).
* see long comment above 'class StackProtectorDescriptor' declaration.
* The target needs to override 'getSDagStackGuard' to activate SDAG stack protection generation. (note: getIRStackGuard MUST be nullptr).
* 'getSDagStackGuard' returns the appropriate stack guard (security cookie)
* The code is generated by 'SelectionDAGBuilder.cpp' and 'SelectionDAGISel.cpp'.
* include/llvm/Target/TargetLowering.h
* Contains function to retrieve the default Guard 'Value'; should be overriden by each target to select which implementation is used and provide Guard 'Value'.
* lib/Target/X86/X86ISelLowering.cpp
* Contains the x86 specialisation; Guard 'Value' used by the SelectionDAG algorithm.
Function-based Instrumentation:
* The MSVC doesn't inline the stack guard comparison in every function. Instead, a call to '__security_check_cookie' is added to the epilogue before every return instructions.
* To support function-based instrumentation, this patch is
* adding a function to get the function-based check (llvm 'Value', see include/llvm/Target/TargetLowering.h),
* If provided, the stack protection instrumentation won't be inlined and a call to that function will be added to the prologue.
* modifying (SelectionDAGISel.cpp) do avoid producing basic blocks used for inline instrumentation,
* generating the function-based instrumentation during the ISEL pass (SelectionDAGBuilder.cpp),
* if FastISEL (not SelectionDAG), using the fallback which rely on the same function-based implemented over intermediate representation (StackProtector.cpp).
Modifications
* adding support for MSVC (lib/Target/X86/X86ISelLowering.cpp)
* adding support function-based instrumentation (lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp, .h)
Results
* IR generated instrumentation:
```
clang-cl /GS test.cc /Od /c -mllvm -print-isel-input
```
```
*** Final LLVM Code input to ISel ***
; Function Attrs: nounwind sspstrong
define i32 @"\01?example@@YAHHH@Z"(i32 %offset, i32 %index) #0 {
entry:
%StackGuardSlot = alloca i8* <<<-- Allocated guard slot
%0 = call i8* @llvm.stackguard() <<<-- Loading Stack Guard value
call void @llvm.stackprotector(i8* %0, i8** %StackGuardSlot) <<<-- Prologue intrinsic call (store to Guard slot)
%index.addr = alloca i32, align 4
%offset.addr = alloca i32, align 4
%buffer = alloca [10 x i8], align 1
store i32 %index, i32* %index.addr, align 4
store i32 %offset, i32* %offset.addr, align 4
%arraydecay = getelementptr inbounds [10 x i8], [10 x i8]* %buffer, i32 0, i32 0
%1 = load i32, i32* %index.addr, align 4
call void @llvm.memset.p0i8.i32(i8* %arraydecay, i8 -52, i32 %1, i32 1, i1 false)
%2 = load i32, i32* %index.addr, align 4
%arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buffer, i32 0, i32 %2
%3 = load i8, i8* %arrayidx, align 1
%conv = sext i8 %3 to i32
%4 = load volatile i8*, i8** %StackGuardSlot <<<-- Loading Guard slot
call void @__security_check_cookie(i8* %4) <<<-- Epilogue function-based check
ret i32 %conv
}
```
* SelectionDAG generated instrumentation:
```
clang-cl /GS test.cc /O1 /c /FA
```
```
"?example@@YAHHH@Z": # @"\01?example@@YAHHH@Z"
# BB#0: # %entry
pushl %esi
subl $16, %esp
movl ___security_cookie, %eax <<<-- Loading Stack Guard value
movl 28(%esp), %esi
movl %eax, 12(%esp) <<<-- Store to Guard slot
leal 2(%esp), %eax
pushl %esi
pushl $204
pushl %eax
calll _memset
addl $12, %esp
movsbl 2(%esp,%esi), %esi
movl 12(%esp), %ecx <<<-- Loading Guard slot
calll @__security_check_cookie@4 <<<-- Epilogue function-based check
movl %esi, %eax
addl $16, %esp
popl %esi
retl
```
Reviewers: kcc, pcc, eugenis, rnk
Subscribers: majnemer, llvm-commits, hans, thakis, rnk
Differential Revision: http://reviews.llvm.org/D20346
llvm-svn: 272053
2016-06-08 04:15:35 +08:00
|
|
|
declare i8* @strcpy(i8*, i8*) nounwind
|
|
|
|
|
|
|
|
declare i32 @printf(i8*, ...) nounwind
|
|
|
|
|