1 ; Test for handling of asm constraints in MSan instrumentation.
2 ; RUN: opt < %s -msan-kernel=1 -msan-check-access-address=0 \
3 ; RUN: -msan-handle-asm-conservative=0 -S -passes=msan 2>&1 | FileCheck \
4 ; RUN: "-check-prefix=CHECK" %s
5 ; RUN: opt < %s -msan-kernel=1 -msan-check-access-address=0 \
6 ; RUN: -msan-handle-asm-conservative=1 -S -passes=msan 2>&1 | FileCheck \
7 ; RUN: "-check-prefixes=CHECK,CHECK-CONS" %s
9 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
10 target triple = "x86_64-unknown-linux-gnu"
12 %struct.pair = type { i32, i32 }
14 @id1 = common dso_local global i32 0, align 4
15 @is1 = common dso_local global i32 0, align 4
16 @id2 = common dso_local global i32 0, align 4
17 @is2 = common dso_local global i32 0, align 4
18 @id3 = common dso_local global i32 0, align 4
19 @pair2 = common dso_local global %struct.pair zeroinitializer, align 4
20 @pair1 = common dso_local global %struct.pair zeroinitializer, align 4
21 @c2 = common dso_local global i8 0, align 1
22 @c1 = common dso_local global i8 0, align 1
23 @memcpy_d1 = common dso_local global ptr null, align 8
24 @memcpy_d2 = common dso_local global ptr null, align 8
25 @memcpy_s1 = common dso_local global ptr null, align 8
26 @memcpy_s2 = common dso_local global ptr null, align 8
28 ; The functions below were generated from a C source that contains declarations like follows:
30 ; asm("" : "=r" (id1) : "r" (is1));
32 ; with corresponding input/output constraints.
33 ; Note that the assembly statement is always empty, as MSan doesn't look at it anyway.
35 ; One input register, one output register:
36 ; asm("" : "=r" (id1) : "r" (is1));
37 define dso_local void @f_1i_1o_reg() sanitize_memory {
39 %0 = load i32, ptr @is1, align 4
40 %1 = call i32 asm "", "=r,r,~{dirflag},~{fpsr},~{flags}"(i32 %0)
41 store i32 %1, ptr @id1, align 4
45 ; CHECK-LABEL: @f_1i_1o_reg
46 ; CHECK: [[IS1_F1:%.*]] = load i32, ptr @is1, align 4
47 ; CHECK: call void @__msan_warning
48 ; CHECK: call i32 asm "",{{.*}}(i32 [[IS1_F1]])
49 ; CHECK: [[PACK1_F1:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id1{{.*}})
50 ; CHECK: [[EXT1_F1:%.*]] = extractvalue { ptr, ptr } [[PACK1_F1]], 0
51 ; CHECK: store i32 0, ptr [[EXT1_F1]]
54 ; Two input registers, two output registers:
55 ; asm("" : "=r" (id1), "=r" (id2) : "r" (is1), "r"(is2));
56 define dso_local void @f_2i_2o_reg() sanitize_memory {
58 %0 = load i32, ptr @is1, align 4
59 %1 = load i32, ptr @is2, align 4
60 %2 = call { i32, i32 } asm "", "=r,=r,r,r,~{dirflag},~{fpsr},~{flags}"(i32 %0, i32 %1)
61 %asmresult = extractvalue { i32, i32 } %2, 0
62 %asmresult1 = extractvalue { i32, i32 } %2, 1
63 store i32 %asmresult, ptr @id1, align 4
64 store i32 %asmresult1, ptr @id2, align 4
68 ; CHECK-LABEL: @f_2i_2o_reg
69 ; CHECK: [[IS1_F2:%.*]] = load i32, ptr @is1, align 4
70 ; CHECK: [[IS2_F2:%.*]] = load i32, ptr @is2, align 4
71 ; CHECK: call void @__msan_warning
72 ; CHECK: call void @__msan_warning
73 ; CHECK: call { i32, i32 } asm "",{{.*}}(i32 [[IS1_F2]], i32 [[IS2_F2]])
74 ; CHECK: [[PACK1_F2:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id1{{.*}})
75 ; CHECK: [[EXT1_F2:%.*]] = extractvalue { ptr, ptr } [[PACK1_F2]], 0
76 ; CHECK: store i32 0, ptr [[EXT1_F2]]
77 ; CHECK: [[PACK2_F2:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id2{{.*}})
78 ; CHECK: [[EXT2_F2:%.*]] = extractvalue { ptr, ptr } [[PACK2_F2]], 0
79 ; CHECK: store i32 0, ptr [[EXT2_F2]]
81 ; Input same as output, used twice:
82 ; asm("" : "=r" (id1), "=r" (id2) : "r" (id1), "r" (id2));
83 define dso_local void @f_2i_2o_reuse2_reg() sanitize_memory {
85 %0 = load i32, ptr @id1, align 4
86 %1 = load i32, ptr @id2, align 4
87 %2 = call { i32, i32 } asm "", "=r,=r,r,r,~{dirflag},~{fpsr},~{flags}"(i32 %0, i32 %1)
88 %asmresult = extractvalue { i32, i32 } %2, 0
89 %asmresult1 = extractvalue { i32, i32 } %2, 1
90 store i32 %asmresult, ptr @id1, align 4
91 store i32 %asmresult1, ptr @id2, align 4
95 ; CHECK-LABEL: @f_2i_2o_reuse2_reg
96 ; CHECK: [[ID1_F3:%.*]] = load i32, ptr @id1, align 4
97 ; CHECK: [[ID2_F3:%.*]] = load i32, ptr @id2, align 4
98 ; CHECK: call void @__msan_warning
99 ; CHECK: call void @__msan_warning
100 ; CHECK: call { i32, i32 } asm "",{{.*}}(i32 [[ID1_F3]], i32 [[ID2_F3]])
101 ; CHECK: [[PACK1_F3:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id1{{.*}})
102 ; CHECK: [[EXT1_F3:%.*]] = extractvalue { ptr, ptr } [[PACK1_F3]], 0
103 ; CHECK: store i32 0, ptr [[EXT1_F3]]
104 ; CHECK: [[PACK2_F3:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id2{{.*}})
105 ; CHECK: [[EXT2_F3:%.*]] = extractvalue { ptr, ptr } [[PACK2_F3]], 0
106 ; CHECK: store i32 0, ptr [[EXT2_F3]]
109 ; One of the input registers is also an output:
110 ; asm("" : "=r" (id1), "=r" (id2) : "r" (id1), "r"(is1));
111 define dso_local void @f_2i_2o_reuse1_reg() sanitize_memory {
113 %0 = load i32, ptr @id1, align 4
114 %1 = load i32, ptr @is1, align 4
115 %2 = call { i32, i32 } asm "", "=r,=r,r,r,~{dirflag},~{fpsr},~{flags}"(i32 %0, i32 %1)
116 %asmresult = extractvalue { i32, i32 } %2, 0
117 %asmresult1 = extractvalue { i32, i32 } %2, 1
118 store i32 %asmresult, ptr @id1, align 4
119 store i32 %asmresult1, ptr @id2, align 4
123 ; CHECK-LABEL: @f_2i_2o_reuse1_reg
124 ; CHECK: [[ID1_F4:%.*]] = load i32, ptr @id1, align 4
125 ; CHECK: [[IS1_F4:%.*]] = load i32, ptr @is1, align 4
126 ; CHECK: call void @__msan_warning
127 ; CHECK: call void @__msan_warning
128 ; CHECK: call { i32, i32 } asm "",{{.*}}(i32 [[ID1_F4]], i32 [[IS1_F4]])
129 ; CHECK: [[PACK1_F4:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id1{{.*}})
130 ; CHECK: [[EXT1_F4:%.*]] = extractvalue { ptr, ptr } [[PACK1_F4]], 0
131 ; CHECK: store i32 0, ptr [[EXT1_F4]]
132 ; CHECK: [[PACK2_F4:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id2{{.*}})
133 ; CHECK: [[EXT2_F4:%.*]] = extractvalue { ptr, ptr } [[PACK2_F4]], 0
134 ; CHECK: store i32 0, ptr [[EXT2_F4]]
137 ; One input register, three output registers:
138 ; asm("" : "=r" (id1), "=r" (id2), "=r" (id3) : "r" (is1));
139 define dso_local void @f_1i_3o_reg() sanitize_memory {
141 %0 = load i32, ptr @is1, align 4
142 %1 = call { i32, i32, i32 } asm "", "=r,=r,=r,r,~{dirflag},~{fpsr},~{flags}"(i32 %0)
143 %asmresult = extractvalue { i32, i32, i32 } %1, 0
144 %asmresult1 = extractvalue { i32, i32, i32 } %1, 1
145 %asmresult2 = extractvalue { i32, i32, i32 } %1, 2
146 store i32 %asmresult, ptr @id1, align 4
147 store i32 %asmresult1, ptr @id2, align 4
148 store i32 %asmresult2, ptr @id3, align 4
152 ; CHECK-LABEL: @f_1i_3o_reg
153 ; CHECK: [[IS1_F5:%.*]] = load i32, ptr @is1, align 4
154 ; CHECK: call void @__msan_warning
155 ; CHECK: call { i32, i32, i32 } asm "",{{.*}}(i32 [[IS1_F5]])
156 ; CHECK: [[PACK1_F5:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id1{{.*}})
157 ; CHECK: [[EXT1_F5:%.*]] = extractvalue { ptr, ptr } [[PACK1_F5]], 0
158 ; CHECK: store i32 0, ptr [[EXT1_F5]]
159 ; CHECK: [[PACK2_F5:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id2{{.*}})
160 ; CHECK: [[EXT2_F5:%.*]] = extractvalue { ptr, ptr } [[PACK2_F5]], 0
161 ; CHECK: store i32 0, ptr [[EXT2_F5]]
162 ; CHECK: [[PACK3_F5:%.*]] = call {{.*}} @__msan_metadata_ptr_for_store_4({{.*}}@id3{{.*}})
163 ; CHECK: [[EXT3_F5:%.*]] = extractvalue { ptr, ptr } [[PACK3_F5]], 0
164 ; CHECK: store i32 0, ptr [[EXT3_F5]]
167 ; 2 input memory args, 2 output memory args:
168 ; asm("" : "=m" (id1), "=m" (id2) : "m" (is1), "m"(is2))
169 define dso_local void @f_2i_2o_mem() sanitize_memory {
171 call void asm "", "=*m,=*m,*m,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) @id1, ptr elementtype(i32) @id2, ptr elementtype(i32) @is1, ptr elementtype(i32) @is2)
175 ; CHECK-LABEL: @f_2i_2o_mem
176 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@id1{{.*}}, i64 4)
177 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@id2{{.*}}, i64 4)
178 ; CHECK: call void asm "", "=*m,=*m,*m,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) @id1, ptr elementtype(i32) @id2, ptr elementtype(i32) @is1, ptr elementtype(i32) @is2)
181 ; Same input and output passed as both memory and register:
182 ; asm("" : "=r" (id1), "=m"(id1) : "r"(is1), "m"(is1));
183 define dso_local void @f_1i_1o_memreg() sanitize_memory {
185 %0 = load i32, ptr @is1, align 4
186 %1 = call i32 asm "", "=r,=*m,r,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) @id1, i32 %0, ptr elementtype(i32) @is1)
187 store i32 %1, ptr @id1, align 4
191 ; CHECK-LABEL: @f_1i_1o_memreg
192 ; CHECK: [[IS1_F7:%.*]] = load i32, ptr @is1, align 4
193 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@id1{{.*}}, i64 4)
194 ; CHECK: call void @__msan_warning
195 ; CHECK: call i32 asm "", "=r,=*m,r,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) @id1, i32 [[IS1_F7]], ptr elementtype(i32) @is1)
198 ; Three outputs, first and last returned via regs, second via mem:
199 ; asm("" : "=r" (id1), "=m"(id2), "=r" (id3):);
200 define dso_local void @f_3o_reg_mem_reg() sanitize_memory {
202 %0 = call { i32, i32 } asm "", "=r,=*m,=r,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) @id2)
203 %asmresult = extractvalue { i32, i32 } %0, 0
204 %asmresult1 = extractvalue { i32, i32 } %0, 1
205 store i32 %asmresult, ptr @id1, align 4
206 store i32 %asmresult1, ptr @id3, align 4
210 ; CHECK-LABEL: @f_3o_reg_mem_reg
211 ; CHECK-CONS: call void @__msan_instrument_asm_store(ptr @id2, i64 4)
212 ; CHECK: call { i32, i32 } asm "", "=r,=*m,=r,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) @id2)
215 ; Three inputs and three outputs of different types: a pair, a char, a function pointer.
216 ; Everything is meant to be passed in registers, but LLVM chooses to return the integer pair by pointer:
217 ; asm("" : "=r" (pair2), "=r" (c2), "=r" (memcpy_d1) : "r"(pair1), "r"(c1), "r"(memcpy_s1));
218 define dso_local void @f_3i_3o_complex_reg() sanitize_memory {
220 %0 = load i64, ptr @pair1, align 4
221 %1 = load i8, ptr @c1, align 1
222 %2 = load ptr, ptr @memcpy_s1, align 8
223 %3 = call { i8, ptr } asm "", "=*r,=r,=r,r,r,r,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(%struct.pair) @pair2, i64 %0, i8 %1, ptr %2)
224 %asmresult = extractvalue { i8, ptr } %3, 0
225 %asmresult1 = extractvalue { i8, ptr } %3, 1
226 store i8 %asmresult, ptr @c2, align 1
227 store ptr %asmresult1, ptr @memcpy_d1, align 8
231 ; CHECK-LABEL: @f_3i_3o_complex_reg
232 ; CHECK: [[PAIR1_F9:%.*]] = load {{.*}} @pair1
233 ; CHECK: [[C1_F9:%.*]] = load {{.*}} @c1
234 ; CHECK: [[MEMCPY_S1_F9:%.*]] = load {{.*}} @memcpy_s1
235 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@pair2{{.*}}, i64 8)
236 ; CHECK: call void @__msan_warning
237 ; CHECK: call void @__msan_warning
238 ; CHECK: call void @__msan_warning
239 ; CHECK: call { i8, ptr } asm "", "=*r,=r,=r,r,r,r,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(%struct.pair) @pair2, {{.*}}[[PAIR1_F9]], i8 [[C1_F9]], {{.*}} [[MEMCPY_S1_F9]])
241 ; Three inputs and three outputs of different types: a pair, a char, a function pointer.
242 ; Everything is passed in memory:
243 ; asm("" : "=m" (pair2), "=m" (c2), "=m" (memcpy_d1) : "m"(pair1), "m"(c1), "m"(memcpy_s1));
244 define dso_local void @f_3i_3o_complex_mem() sanitize_memory {
246 call void asm "", "=*m,=*m,=*m,*m,*m,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(%struct.pair) @pair2, ptr elementtype(i8) @c2, ptr elementtype(ptr) @memcpy_d1, ptr elementtype(%struct.pair) @pair1, ptr elementtype(i8) @c1, ptr elementtype(ptr) @memcpy_s1)
250 ; CHECK-LABEL: @f_3i_3o_complex_mem
251 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@pair2{{.*}}, i64 8)
252 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@c2{{.*}}, i64 1)
253 ; CHECK-CONS: call void @__msan_instrument_asm_store({{.*}}@memcpy_d1{{.*}}, i64 8)
254 ; CHECK: call void asm "", "=*m,=*m,=*m,*m,*m,*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(%struct.pair) @pair2, ptr elementtype(i8) @c2, ptr elementtype(ptr) @memcpy_d1, ptr elementtype(%struct.pair) @pair1, ptr elementtype(i8) @c1, ptr elementtype(ptr) @memcpy_s1)
257 ; A simple asm goto construct to check that callbr is handled correctly:
258 ; int asm_goto(int n) {
260 ; asm goto("cmp %0, %1; jnz %l2;" :: "r"(n), "r"(v)::skip_label);
265 ; asm goto statements can't have outputs, so just make sure we check the input
266 ; and the compiler doesn't crash.
267 define dso_local i32 @asm_goto(i32 %n) sanitize_memory {
269 callbr void asm sideeffect "cmp $0, $1; jnz ${2:l}", "r,r,!i,~{dirflag},~{fpsr},~{flags}"(i32 %n, i32 1)
270 to label %cleanup [label %skip_label]
272 skip_label: ; preds = %entry
275 cleanup: ; preds = %entry, %skip_label
276 %retval.0 = phi i32 [ 2, %skip_label ], [ 1, %entry ]
280 ; CHECK-LABEL: @asm_goto
281 ; CHECK: [[LOAD_ARG:%.*]] = load {{.*}} %_msarg
282 ; CHECK: [[CMP:%.*]] = icmp ne {{.*}} [[LOAD_ARG]], 0
283 ; CHECK: br {{.*}} [[CMP]], label %[[LABEL:.*]], label
285 ; CHECK-NEXT: call void @__msan_warning