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), %[[REG1:[^ ]*]]
52; MSVC-X86-O0: xorl %esp, %[[REG1]]
53; MSVC-X86-O0: movl %[[REG1]], %ecx
54; MSVC-X86-O0: calll @__security_check_cookie@4
55; MSVC-X86-O0: retl
56
57; MSVC-X64-O0-LABEL: test:
58; MSVC-X64-O0: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
59; MSVC-X64-O0: xorq %rsp, %[[REG1]]
60; MSVC-X64-O0: movq %[[REG1]], [[SLOT:[0-9]*]](%rsp)
61; MSVC-X64-O0: callq strcpy
62; MSVC-X64-O0: movq [[SLOT]](%rsp), %[[REG1:[^ ]*]]
63; MSVC-X64-O0: xorq %rsp, %[[REG1]]
64; MSVC-X64-O0: movq %[[REG1]], %rcx
65; MSVC-X64-O0: callq __security_check_cookie
66; MSVC-X64-O0: retq
67
68
69declare void @escape(i32*)
70
71define void @test_vla(i32 %n) nounwind ssp {
72  %vla = alloca i32, i32 %n
73  call void @escape(i32* %vla)
74  ret void
75}
76
77; MSVC-X86-LABEL: _test_vla:
78; MSVC-X86: pushl %ebp
79; MSVC-X86: movl %esp, %ebp
80; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
81; MSVC-X86: xorl %ebp, %[[REG1]]
82; MSVC-X86: movl %[[REG1]], [[SLOT:-[0-9]*]](%ebp)
83; MSVC-X86: calll __chkstk
84; MSVC-X86: pushl
85; MSVC-X86: calll _escape
86; MSVC-X86: movl [[SLOT]](%ebp), %ecx
87; MSVC-X86: xorl %ebp, %ecx
88; MSVC-X86: calll @__security_check_cookie@4
89; MSVC-X86: movl %ebp, %esp
90; MSVC-X86: popl %ebp
91; MSVC-X86: retl
92
93; MSVC-X64-LABEL: test_vla:
94; MSVC-X64: pushq %rbp
95; MSVC-X64: subq $16, %rsp
96; MSVC-X64: leaq 16(%rsp), %rbp
97; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
98; MSVC-X64: xorq %rbp, %[[REG1]]
99; MSVC-X64: movq %[[REG1]], [[SLOT:-[0-9]*]](%rbp)
100; MSVC-X64: callq __chkstk
101; MSVC-X64: callq escape
102; MSVC-X64: movq [[SLOT]](%rbp), %rcx
103; MSVC-X64: xorq %rbp, %rcx
104; MSVC-X64: callq __security_check_cookie
105; MSVC-X64: retq
106
107
108; This case is interesting because we address local variables with RBX but XOR
109; the guard value with RBP. That's fine, either value will do, as long as they
110; are the same across the life of the frame.
111
112define void @test_vla_realign(i32 %n) nounwind ssp {
113  %realign = alloca i32, align 32
114  %vla = alloca i32, i32 %n
115  call void @escape(i32* %realign)
116  call void @escape(i32* %vla)
117  ret void
118}
119
120; MSVC-X86-LABEL: _test_vla_realign:
121; MSVC-X86: pushl %ebp
122; MSVC-X86: movl %esp, %ebp
123; MSVC-X86: pushl %esi
124; MSVC-X86: andl $-32, %esp
125; MSVC-X86: subl $32, %esp
126; MSVC-X86: movl %esp, %esi
127; MSVC-X86: movl ___security_cookie, %[[REG1:[^ ]*]]
128; MSVC-X86: xorl %ebp, %[[REG1]]
129; MSVC-X86: movl %[[REG1]], [[SLOT:[0-9]*]](%esi)
130; MSVC-X86: calll __chkstk
131; MSVC-X86: pushl
132; MSVC-X86: calll _escape
133; MSVC-X86: movl [[SLOT]](%esi), %ecx
134; MSVC-X86: xorl %ebp, %ecx
135; MSVC-X86: calll @__security_check_cookie@4
136; MSVC-X86: leal -8(%ebp), %esp
137; MSVC-X86: popl %esi
138; MSVC-X86: popl %ebp
139; MSVC-X86: retl
140
141; MSVC-X64-LABEL: test_vla_realign:
142; MSVC-X64: pushq %rbp
143; MSVC-X64: pushq %rbx
144; MSVC-X64: subq $32, %rsp
145; MSVC-X64: leaq 32(%rsp), %rbp
146; MSVC-X64: andq $-32, %rsp
147; MSVC-X64: movq %rsp, %rbx
148; MSVC-X64: movq __security_cookie(%rip), %[[REG1:[^ ]*]]
149; MSVC-X64: xorq %rbp, %[[REG1]]
150; MSVC-X64: movq %[[REG1]], [[SLOT:[0-9]*]](%rbx)
151; MSVC-X64: callq __chkstk
152; MSVC-X64: callq escape
153; MSVC-X64: movq [[SLOT]](%rbx), %rcx
154; MSVC-X64: xorq %rbp, %rcx
155; MSVC-X64: callq __security_check_cookie
156; MSVC-X64: retq
157
158
159declare i8* @strcpy(i8*, i8*) nounwind
160
161declare i32 @printf(i8*, ...) nounwind
162
163