[msan] Add a test for r168873.
[oota-llvm.git] / test / Instrumentation / MemorySanitizer / msan_basic.ll
1 ; RUN: opt < %s -msan -S | FileCheck %s
2 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
3
4 ; Check the presence of __msan_init
5 ; CHECK: @llvm.global_ctors {{.*}} @__msan_init
6
7 ; load followed by cmp: check that we load the shadow and call __msan_warning.
8 define void @LoadAndCmp(i32* nocapture %a) nounwind uwtable {
9 entry:
10   %0 = load i32* %a, align 4
11   %tobool = icmp eq i32 %0, 0
12   br i1 %tobool, label %if.end, label %if.then
13
14 if.then:                                          ; preds = %entry
15   tail call void (...)* @foo() nounwind
16   br label %if.end
17
18 if.end:                                           ; preds = %entry, %if.then
19   ret void
20 }
21
22 declare void @foo(...)
23
24 ; CHECK: define void @LoadAndCmp
25 ; CHECK: = load
26 ; CHECK: = load
27 ; CHECK: call void @__msan_warning_noreturn()
28 ; CHECK-NEXT: call void asm sideeffect
29 ; CHECK-NEXT: unreachable
30 ; CHECK: }
31
32 ; Check that we store the shadow for the retval.
33 define i32 @ReturnInt() nounwind uwtable readnone {
34 entry:
35   ret i32 123
36 }
37
38 ; CHECK: define i32 @ReturnInt()
39 ; CHECK: store i32 0,{{.*}}__msan_retval_tls
40 ; CHECK: }
41
42 ; Check that we get the shadow for the retval.
43 define void @CopyRetVal(i32* nocapture %a) nounwind uwtable {
44 entry:
45   %call = tail call i32 @ReturnInt() nounwind
46   store i32 %call, i32* %a, align 4
47   ret void
48 }
49
50 ; CHECK: define void @CopyRetVal
51 ; CHECK: load{{.*}}__msan_retval_tls
52 ; CHECK: store
53 ; CHECK: store
54 ; CHECK: }
55
56
57 ; Check that we generate PHIs for shadow.
58 define void @FuncWithPhi(i32* nocapture %a, i32* %b, i32* nocapture %c) nounwind uwtable {
59 entry:
60   %tobool = icmp eq i32* %b, null
61   br i1 %tobool, label %if.else, label %if.then
62
63   if.then:                                          ; preds = %entry
64   %0 = load i32* %b, align 4
65   br label %if.end
66
67   if.else:                                          ; preds = %entry
68   %1 = load i32* %c, align 4
69   br label %if.end
70
71   if.end:                                           ; preds = %if.else, %if.then
72   %t.0 = phi i32 [ %0, %if.then ], [ %1, %if.else ]
73   store i32 %t.0, i32* %a, align 4
74   ret void
75 }
76
77 ; CHECK: define void @FuncWithPhi
78 ; CHECK: = phi
79 ; CHECK-NEXT: = phi
80 ; CHECK: store
81 ; CHECK: store
82 ; CHECK: }
83
84 ; Compute shadow for "x << 10"
85 define void @ShlConst(i32* nocapture %x) nounwind uwtable {
86 entry:
87   %0 = load i32* %x, align 4
88   %1 = shl i32 %0, 10
89   store i32 %1, i32* %x, align 4
90   ret void
91 }
92
93 ; CHECK: define void @ShlConst
94 ; CHECK: = load
95 ; CHECK: = load
96 ; CHECK: shl
97 ; CHECK: shl
98 ; CHECK: store
99 ; CHECK: store
100 ; CHECK: }
101
102 ; Compute shadow for "10 << x": it should have 'sext i1'.
103 define void @ShlNonConst(i32* nocapture %x) nounwind uwtable {
104 entry:
105   %0 = load i32* %x, align 4
106   %1 = shl i32 10, %0
107   store i32 %1, i32* %x, align 4
108   ret void
109 }
110
111 ; CHECK: define void @ShlNonConst
112 ; CHECK: = load
113 ; CHECK: = load
114 ; CHECK: = sext i1
115 ; CHECK: store
116 ; CHECK: store
117 ; CHECK: }
118
119 ; SExt
120 define void @SExt(i32* nocapture %a, i16* nocapture %b) nounwind uwtable {
121 entry:
122   %0 = load i16* %b, align 2
123   %1 = sext i16 %0 to i32
124   store i32 %1, i32* %a, align 4
125   ret void
126 }
127
128 ; CHECK: define void @SExt
129 ; CHECK: = load
130 ; CHECK: = load
131 ; CHECK: = sext
132 ; CHECK: = sext
133 ; CHECK: store
134 ; CHECK: store
135 ; CHECK: }
136
137
138 ; memset
139 define void @MemSet(i8* nocapture %x) nounwind uwtable {
140 entry:
141   call void @llvm.memset.p0i8.i64(i8* %x, i8 42, i64 10, i32 1, i1 false)
142   ret void
143 }
144
145 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
146
147 ; CHECK: define void @MemSet
148 ; CHECK: call i8* @__msan_memset
149 ; CHECK: }
150
151
152 ; memcpy
153 define void @MemCpy(i8* nocapture %x, i8* nocapture %y) nounwind uwtable {
154 entry:
155   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %x, i8* %y, i64 10, i32 1, i1 false)
156   ret void
157 }
158
159 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
160
161 ; CHECK: define void @MemCpy
162 ; CHECK: call i8* @__msan_memcpy
163 ; CHECK: }
164
165
166 ; memmove is lowered to a call
167 define void @MemMove(i8* nocapture %x, i8* nocapture %y) nounwind uwtable {
168 entry:
169   call void @llvm.memmove.p0i8.p0i8.i64(i8* %x, i8* %y, i64 10, i32 1, i1 false)
170   ret void
171 }
172
173 declare void @llvm.memmove.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
174
175 ; CHECK: define void @MemMove
176 ; CHECK: call i8* @__msan_memmove
177 ; CHECK: }
178
179
180 ; Check that we propagate shadow for "select"
181
182 define i32 @Select(i32 %a, i32 %b, i32 %c) nounwind uwtable readnone {
183 entry:
184   %tobool = icmp ne i32 %c, 0
185   %cond = select i1 %tobool, i32 %a, i32 %b
186   ret i32 %cond
187 }
188
189 ; CHECK: define i32 @Select
190 ; CHECK: select
191 ; CHECK-NEXT: select
192 ; CHECK: }
193
194
195 define i8* @IntToPtr(i64 %x) nounwind uwtable readnone {
196 entry:
197   %0 = inttoptr i64 %x to i8*
198   ret i8* %0
199 }
200
201 ; CHECK: define i8* @IntToPtr
202 ; CHECK: load i64*{{.*}}__msan_param_tls
203 ; CHECK-NEXT: inttoptr
204 ; CHECK-NEXT: store i64{{.*}}__msan_retval_tls
205 ; CHECK: }
206
207
208 define i8* @IntToPtr_ZExt(i16 %x) nounwind uwtable readnone {
209 entry:
210   %0 = inttoptr i16 %x to i8*
211   ret i8* %0
212 }
213
214 ; CHECK: define i8* @IntToPtr_ZExt
215 ; CHECK: zext
216 ; CHECK-NEXT: inttoptr
217 ; CHECK: }
218
219
220 ; Check that we insert exactly one check on udiv
221 ; (2nd arg shadow is checked, 1st arg shadow is propagated)
222
223 define i32 @Div(i32 %a, i32 %b) nounwind uwtable readnone {
224 entry:
225   %div = udiv i32 %a, %b
226   ret i32 %div
227 }
228
229 ; CHECK: define i32 @Div
230 ; CHECK: icmp
231 ; CHECK: br
232 ; CHECK-NOT: icmp
233 ; CHECK: udiv
234 ; CHECK-NOT: icmp
235 ; CHECK: }