1; RUN: llc -mtriple=i386-pc-windows-msvc < %s -o - | FileCheck -check-prefix=MSVC-X86 %s
2; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s -o - | FileCheck -check-prefix=MSVC-X64 %s
3
4; Make sure fastisel falls back and does something secure.
5; RUN: llc -mtriple=i686-pc-windows-msvc -O0 < %s -o - | FileCheck -check-prefix=MSVC-X86-O0 %s
6; RUN: llc -mtriple=x86_64-pc-windows-msvc -O0 < %s -o - | FileCheck -check-prefix=MSVC-X64-O0 %s
7
8@"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00"    ; <[11 x i8]*> [#uses=1]
9
10define void @test(i8* %a) nounwind ssp {
11entry:
12 %a_addr = alloca i8*    ; <i8**> [#uses=2]
13 %buf = alloca [8 x i8]    ; <[8 x i8]*> [#uses=2]
14 store i8* %a, i8** %a_addr
15 %buf1 = bitcast [8 x i8]* %buf to i8*   ; <i8*> [#uses=1]
16 %0 = load i8*, i8** %a_addr, align 4    ; <i8*> [#uses=1]
17 %1 = call i8* @strcpy(i8* %buf1, i8* %0) nounwind   ; <i8*> [#uses=0]
18  %buf2 = bitcast [8 x i8]* %buf to i8*    ; <i8*> [#uses=1]
19 %2 = call i32 (i8*, ...) @printf(i8* getelementptr ([11 x i8], [11 x i8]* @"\01LC", i32 0, i32 0), i8* %buf2) nounwind    ; <i32> [#uses=0]
20 br label %return
21
22return:    ; preds = %entry
23 ret void
24}
25
26; MSVC-X86-LABEL: _test:
27; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
28; MSVC-X86: xorl %esp, %[[REG1]]
29; MSVC-X86: movl %[[REG1]], [[SLOT:[0-9]*]](%esp)
30; MSVC-X86: calll _strcpy
31; MSVC-X86: movl [[SLOT]](%esp), %ecx
32; MSVC-X86: xorl %esp, %ecx
33; MSVC-X86: calll @__security_check_cookie@4
34; MSVC-X86: retl
35
36; MSVC-X64-LABEL: test:
37; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
38; MSVC-X64: xorq %rsp, %[[REG1]]
39; MSVC-X64: movq %[[REG1]], [[SLOT:[0-9]*]](%rsp)
40; MSVC-X64: callq strcpy
41; MSVC-X64: movq [[SLOT]](%rsp), %rcx
42; MSVC-X64: xorq %rsp, %rcx
43; MSVC-X64: callq __security_check_cookie
44; MSVC-X64: retq
45
46; MSVC-X86-O0-LABEL: _test:
47; MSVC-X86-O0: movl ___security_cookie, %[[REG1:[^ ]*]]
48; MSVC-X86-O0: xorl %esp, %[[REG1]]
49; MSVC-X86-O0: movl %[[REG1]], [[SLOT:[0-9]*]](%esp)
50; MSVC-X86-O0: calll _strcpy
51; MSVC-X86-O0: movl [[SLOT]](%esp), %ecx
52; MSVC-X86-O0: xorl %esp, %ecx
53; MSVC-X86-O0: calll @__security_check_cookie@4
54; MSVC-X86-O0: retl
55
56; MSVC-X64-O0-LABEL: test:
57; MSVC-X64-O0: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
58; MSVC-X64-O0: xorq %rsp, %[[REG1]]
59; MSVC-X64-O0: movq %[[REG1]], [[SLOT:[0-9]*]](%rsp)
60; MSVC-X64-O0: callq strcpy
61; MSVC-X64-O0: movq [[SLOT]](%rsp), %rcx
62; MSVC-X64-O0: xorq %rsp, %rcx
63; MSVC-X64-O0: callq __security_check_cookie
64; MSVC-X64-O0: retq
65
66
67declare void @escape(i32*)
68
69define void @test_vla(i32 %n) nounwind ssp {
70  %vla = alloca i32, i32 %n
71  call void @escape(i32* %vla)
72  ret void
73}
74
75; MSVC-X86-LABEL: _test_vla:
76; MSVC-X86: pushl %ebp
77; MSVC-X86: movl %esp, %ebp
78; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
79; MSVC-X86: xorl %ebp, %[[REG1]]
80; MSVC-X86: movl %[[REG1]], [[SLOT:-[0-9]*]](%ebp)
81; MSVC-X86: calll __chkstk
82; MSVC-X86: pushl
83; MSVC-X86: calll _escape
84; MSVC-X86: movl [[SLOT]](%ebp), %ecx
85; MSVC-X86: xorl %ebp, %ecx
86; MSVC-X86: calll @__security_check_cookie@4
87; MSVC-X86: movl %ebp, %esp
88; MSVC-X86: popl %ebp
89; MSVC-X86: retl
90
91; MSVC-X64-LABEL: test_vla:
92; MSVC-X64: pushq %rbp
93; MSVC-X64: subq $16, %rsp
94; MSVC-X64: leaq 16(%rsp), %rbp
95; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
96; MSVC-X64: xorq %rbp, %[[REG1]]
97; MSVC-X64: movq %[[REG1]], [[SLOT:-[0-9]*]](%rbp)
98; MSVC-X64: callq __chkstk
99; MSVC-X64: callq escape
100; MSVC-X64: movq [[SLOT]](%rbp), %rcx
101; MSVC-X64: xorq %rbp, %rcx
102; MSVC-X64: callq __security_check_cookie
103; MSVC-X64: retq
104
105
106; This case is interesting because we address local variables with RBX but XOR
107; the guard value with RBP. That's fine, either value will do, as long as they
108; are the same across the life of the frame.
109
110define void @test_vla_realign(i32 %n) nounwind ssp {
111  %realign = alloca i32, align 32
112  %vla = alloca i32, i32 %n
113  call void @escape(i32* %realign)
114  call void @escape(i32* %vla)
115  ret void
116}
117
118; MSVC-X86-LABEL: _test_vla_realign:
119; MSVC-X86: pushl %ebp
120; MSVC-X86: movl %esp, %ebp
121; MSVC-X86: pushl %esi
122; MSVC-X86: andl $-32, %esp
123; MSVC-X86: subl $32, %esp
124; MSVC-X86: movl %esp, %esi
125; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
126; MSVC-X86: xorl %ebp, %[[REG1]]
127; MSVC-X86: movl %[[REG1]], [[SLOT:[0-9]*]](%esi)
128; MSVC-X86: calll __chkstk
129; MSVC-X86: pushl
130; MSVC-X86: calll _escape
131; MSVC-X86: movl [[SLOT]](%esi), %ecx
132; MSVC-X86: xorl %ebp, %ecx
133; MSVC-X86: calll @__security_check_cookie@4
134; MSVC-X86: leal -8(%ebp), %esp
135; MSVC-X86: popl %esi
136; MSVC-X86: popl %ebp
137; MSVC-X86: retl
138
139; MSVC-X64-LABEL: test_vla_realign:
140; MSVC-X64: pushq %rbp
141; MSVC-X64: pushq %rbx
142; MSVC-X64: subq $32, %rsp
143; MSVC-X64: leaq 32(%rsp), %rbp
144; MSVC-X64: andq $-32, %rsp
145; MSVC-X64: movq %rsp, %rbx
146; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
147; MSVC-X64: xorq %rbp, %[[REG1]]
148; MSVC-X64: movq %[[REG1]], [[SLOT:[0-9]*]](%rbx)
149; MSVC-X64: callq __chkstk
150; MSVC-X64: callq escape
151; MSVC-X64: movq [[SLOT]](%rbx), %rcx
152; MSVC-X64: xorq %rbp, %rcx
153; MSVC-X64: callq __security_check_cookie
154; MSVC-X64: retq
155
156
157declare i8* @strcpy(i8*, i8*) nounwind
158
159declare i32 @printf(i8*, ...) nounwind
160
161