blob: 8a93fa117a03acdd95d8730dd1576fc938e7a1e7 [file] [log] [blame]
Chandler Carruth5da53432012-07-02 18:37:59 +00001; RUN: opt -basicaa -lint -disable-output < %s 2>&1 | FileCheck %s
Dan Gohman98bc4372010-04-08 18:47:09 +00002target datalayout = "e-p:64:64:64"
3
4declare fastcc void @bar()
Dan Gohmana20a5cd2010-05-26 22:21:25 +00005declare void @llvm.stackrestore(i8*)
Pete Cooper67cf9a72015-11-19 05:56:52 +00006declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
Dan Gohman0fa67e42010-05-28 21:43:57 +00007declare void @has_sret(i8* sret %p)
8declare void @has_noaliases(i32* noalias %p, i32* %q)
9declare void @one_arg(i32)
Dan Gohman98bc4372010-04-08 18:47:09 +000010
Dan Gohman299e7b92010-04-30 19:05:00 +000011@CG = constant i32 7
Duncan Sands5e561bb2012-09-30 07:30:10 +000012@E = external global i8
Dan Gohman299e7b92010-04-30 19:05:00 +000013
Dan Gohman98bc4372010-04-08 18:47:09 +000014define i32 @foo() noreturn {
Duncan Sands3f4d0b12012-09-25 10:00:49 +000015 %buf = alloca i8
16 %buf2 = alloca {i8, i8}, align 2
Dan Gohman98bc4372010-04-08 18:47:09 +000017; CHECK: Caller and callee calling convention differ
18 call void @bar()
19; CHECK: Null pointer dereference
20 store i32 0, i32* null
21; CHECK: Null pointer dereference
David Blaikiea79ac142015-02-27 21:17:42 +000022 %t = load i32, i32* null
Dan Gohman7808d492010-04-08 23:05:57 +000023; CHECK: Undef pointer dereference
24 store i32 0, i32* undef
25; CHECK: Undef pointer dereference
David Blaikiea79ac142015-02-27 21:17:42 +000026 %u = load i32, i32* undef
Dan Gohman0fa67e42010-05-28 21:43:57 +000027; CHECK: All-ones pointer dereference
28 store i32 0, i32* inttoptr (i64 -1 to i32*)
29; CHECK: Address one pointer dereference
30 store i32 0, i32* inttoptr (i64 1 to i32*)
Dan Gohman98bc4372010-04-08 18:47:09 +000031; CHECK: Memory reference address is misaligned
Duncan Sands3f4d0b12012-09-25 10:00:49 +000032 store i8 0, i8* %buf, align 2
33; CHECK: Memory reference address is misaligned
David Blaikie79e6c742015-02-27 19:29:02 +000034 %gep = getelementptr {i8, i8}, {i8, i8}* %buf2, i32 0, i32 1
Duncan Sands3f4d0b12012-09-25 10:00:49 +000035 store i8 0, i8* %gep, align 2
Dan Gohman98bc4372010-04-08 18:47:09 +000036; CHECK: Division by zero
37 %sd = sdiv i32 2, 0
38; CHECK: Division by zero
39 %ud = udiv i32 2, 0
40; CHECK: Division by zero
41 %sr = srem i32 2, 0
42; CHECK: Division by zero
43 %ur = urem i32 2, 0
Dan Gohman7808d492010-04-08 23:05:57 +000044; CHECK: extractelement index out of range
45 %ee = extractelement <4 x i32> zeroinitializer, i32 4
46; CHECK: insertelement index out of range
47 %ie = insertelement <4 x i32> zeroinitializer, i32 0, i32 4
48; CHECK: Shift count out of range
49 %r = lshr i32 0, 32
50; CHECK: Shift count out of range
51 %q = ashr i32 0, 32
52; CHECK: Shift count out of range
53 %l = shl i32 0, 32
Dan Gohman9ba08a42010-04-09 01:39:53 +000054; CHECK: xor(undef, undef)
55 %xx = xor i32 undef, undef
56; CHECK: sub(undef, undef)
57 %xs = sub i32 undef, undef
Dan Gohman299e7b92010-04-30 19:05:00 +000058
59; CHECK: Write to read-only memory
60 store i32 8, i32* @CG
61; CHECK: Write to text section
62 store i32 8, i32* bitcast (i32()* @foo to i32*)
63; CHECK: Load from block address
David Blaikiea79ac142015-02-27 21:17:42 +000064 %lb = load i32, i32* bitcast (i8* blockaddress(@foo, %next) to i32*)
Dan Gohman299e7b92010-04-30 19:05:00 +000065; CHECK: Call to block address
David Blaikie23af6482015-04-16 23:24:18 +000066 call void() bitcast (i8* blockaddress(@foo, %next) to void()*)()
Dan Gohmana20a5cd2010-05-26 22:21:25 +000067; CHECK: Undefined behavior: Null pointer dereference
68 call void @llvm.stackrestore(i8* null)
Dan Gohman0fa67e42010-05-28 21:43:57 +000069; CHECK: Undefined behavior: Null pointer dereference
70 call void @has_sret(i8* null)
71; CHECK: Unusual: noalias argument aliases another argument
72 call void @has_noaliases(i32* @CG, i32* @CG)
73; CHECK: Call argument count mismatches callee argument count
David Blaikie23af6482015-04-16 23:24:18 +000074 call void (i32, i32) bitcast (void (i32)* @one_arg to void (i32, i32)*)(i32 0, i32 0)
Dan Gohman0fa67e42010-05-28 21:43:57 +000075; CHECK: Call argument count mismatches callee argument count
David Blaikie23af6482015-04-16 23:24:18 +000076 call void () bitcast (void (i32)* @one_arg to void ()*)()
Dan Gohman0fa67e42010-05-28 21:43:57 +000077; CHECK: Call argument type mismatches callee parameter type
David Blaikie23af6482015-04-16 23:24:18 +000078 call void (float) bitcast (void (i32)* @one_arg to void (float)*)(float 0.0)
Dan Gohman299e7b92010-04-30 19:05:00 +000079
Dan Gohmanc575ec62010-05-28 17:44:00 +000080; CHECK: Write to read-only memory
Pete Cooper67cf9a72015-11-19 05:56:52 +000081 call void @llvm.memcpy.p0i8.p0i8.i64(i8* bitcast (i32* @CG to i8*), i8* bitcast (i32* @CG to i8*), i64 1, i32 1, i1 0)
Dan Gohmanc575ec62010-05-28 17:44:00 +000082
Duncan Sandsa221eea2012-09-26 07:45:36 +000083; CHECK: Undefined behavior: Buffer overflow
84 %wider = bitcast i8* %buf to i16*
85 store i16 0, i16* %wider
86; CHECK: Undefined behavior: Buffer overflow
David Blaikie79e6c742015-02-27 19:29:02 +000087 %inner = getelementptr {i8, i8}, {i8, i8}* %buf2, i32 0, i32 1
Duncan Sandsa221eea2012-09-26 07:45:36 +000088 %wider2 = bitcast i8* %inner to i16*
89 store i16 0, i16* %wider2
90; CHECK: Undefined behavior: Buffer overflow
David Blaikie79e6c742015-02-27 19:29:02 +000091 %before = getelementptr i8, i8* %buf, i32 -1
Duncan Sandsa221eea2012-09-26 07:45:36 +000092 %wider3 = bitcast i8* %before to i16*
93 store i16 0, i16* %wider3
94
Dan Gohman98bc4372010-04-08 18:47:09 +000095 br label %next
96
97next:
98; CHECK: Static alloca outside of entry block
99 %a = alloca i32
100; CHECK: Return statement in function with noreturn attribute
101 ret i32 0
Dan Gohman9ba08a42010-04-09 01:39:53 +0000102
103foo:
Duncan Sands5e561bb2012-09-30 07:30:10 +0000104; CHECK-NOT: Undefined behavior: Buffer overflow
105; CHECK-NOT: Memory reference address is misaligned
106 %e = bitcast i8* @E to i64*
107 store i64 0, i64* %e
Dan Gohman9ba08a42010-04-09 01:39:53 +0000108 %z = add i32 0, 0
109; CHECK: unreachable immediately preceded by instruction without side effects
110 unreachable
111}
112
113; CHECK: Unnamed function with non-local linkage
114define void @0() nounwind {
115 ret void
116}
117
118; CHECK: va_start called in a non-varargs function
119declare void @llvm.va_start(i8*)
120define void @not_vararg(i8* %p) nounwind {
121 call void @llvm.va_start(i8* %p)
122 ret void
Dan Gohman98bc4372010-04-08 18:47:09 +0000123}
Dan Gohman299e7b92010-04-30 19:05:00 +0000124
Dan Gohman1249adf2010-05-26 21:46:36 +0000125; CHECK: Undefined behavior: Branch to non-blockaddress
Dan Gohman299e7b92010-04-30 19:05:00 +0000126define void @use_indbr() {
127 indirectbr i8* bitcast (i32()* @foo to i8*), [label %block]
128block:
129 unreachable
130}
Dan Gohman1249adf2010-05-26 21:46:36 +0000131
Dan Gohmancef9fc32010-05-28 16:34:49 +0000132; CHECK: Undefined behavior: Call with "tail" keyword references alloca
Dan Gohman1249adf2010-05-26 21:46:36 +0000133declare void @tailcallee(i8*)
134define void @use_tail(i8* %valist) {
135 %t = alloca i8
136 tail call void @tailcallee(i8* %t)
Dan Gohman1249adf2010-05-26 21:46:36 +0000137 ret void
138}
Dan Gohmanddba4b72010-05-28 04:33:42 +0000139
Dan Gohmancef9fc32010-05-28 16:34:49 +0000140; CHECK: Unusual: Returning alloca value
Dan Gohmanddba4b72010-05-28 04:33:42 +0000141define i8* @return_local(i32 %n, i32 %m) {
142 %t = alloca i8, i32 %n
David Blaikie79e6c742015-02-27 19:29:02 +0000143 %s = getelementptr i8, i8* %t, i32 %m
Dan Gohmanddba4b72010-05-28 04:33:42 +0000144 ret i8* %s
145}
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000146
Dan Gohmancef9fc32010-05-28 16:34:49 +0000147; CHECK: Unusual: Returning alloca value
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000148define i32* @return_obscured_local() {
149entry:
150 %retval = alloca i32*
151 %x = alloca i32
152 store i32* %x, i32** %retval
153 br label %next
154next:
David Blaikiea79ac142015-02-27 21:17:42 +0000155 %t0 = load i32*, i32** %retval
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000156 %t1 = insertvalue { i32, i32, i32* } zeroinitializer, i32* %t0, 2
157 %t2 = extractvalue { i32, i32, i32* } %t1, 2
158 br label %exit
159exit:
160 %t3 = phi i32* [ %t2, %next ]
161 %t4 = bitcast i32* %t3 to i32*
162 %t5 = ptrtoint i32* %t4 to i64
163 %t6 = add i64 %t5, 0
164 %t7 = inttoptr i64 %t6 to i32*
165 ret i32* %t7
166}
Dan Gohman862f0342010-05-28 16:45:33 +0000167
168; CHECK: Undefined behavior: Undef pointer dereference
169define i32* @self_reference() {
170entry:
171 unreachable
172exit:
173 %t3 = phi i32* [ %t4, %exit ]
174 %t4 = bitcast i32* %t3 to i32*
David Blaikiea79ac142015-02-27 21:17:42 +0000175 %x = load volatile i32, i32* %t3
Dan Gohman862f0342010-05-28 16:45:33 +0000176 br label %exit
177}
Dan Gohmanc128e702010-07-12 18:02:04 +0000178
179; CHECK: Call return type mismatches callee return type
180%struct = type { double, double }
181declare i32 @nonstruct_callee() nounwind
182define void @struct_caller() nounwind {
183entry:
184 call %struct bitcast (i32 ()* @foo to %struct ()*)()
Dan Gohmand8968da2010-08-02 23:06:43 +0000185
186 ; CHECK: Undefined behavior: indirectbr with no destinations
187 indirectbr i8* null, []
Dan Gohmanc128e702010-07-12 18:02:04 +0000188}