• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; Test a pile of objectsize bounds checking.
2; RUN: opt < %s -instcombine -S | FileCheck %s
3; We need target data to get the sizes of the arrays and structures.
4target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
5
6@a = private global [60 x i8] zeroinitializer, align 1 ; <[60 x i8]*>
7@.str = private constant [8 x i8] c"abcdefg\00"   ; <[8 x i8]*>
8define i32 @foo() nounwind {
9; CHECK-LABEL: @foo(
10; CHECK-NEXT: ret i32 60
11  %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false)
12  ret i32 %1
13}
14
15define i8* @bar() nounwind {
16; CHECK-LABEL: @bar(
17entry:
18  %retval = alloca i8*
19  %0 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @a, i32 0, i32 0), i1 false)
20  %cmp = icmp ne i32 %0, -1
21; CHECK: br i1 true
22  br i1 %cmp, label %cond.true, label %cond.false
23
24cond.true:
25  %1 = load i8*, i8** %retval
26  ret i8* %1
27
28cond.false:
29  %2 = load i8*, i8** %retval
30  ret i8* %2
31}
32
33define i32 @f() nounwind {
34; CHECK-LABEL: @f(
35; CHECK-NEXT: ret i32 0
36  %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr ([60 x i8], [60 x i8]* @a, i32 1, i32 0), i1 false)
37  ret i32 %1
38}
39
40@window = external global [0 x i8]
41
42define i1 @baz() nounwind {
43; CHECK-LABEL: @baz(
44; CHECK-NEXT: objectsize
45  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 0), i1 false)
46  %2 = icmp eq i32 %1, -1
47  ret i1 %2
48}
49
50define void @test1(i8* %q, i32 %x) nounwind noinline {
51; CHECK-LABEL: @test1(
52; CHECK: objectsize.i32.p0i8
53entry:
54  %0 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr inbounds ([0 x i8], [0 x i8]* @window, i32 0, i32 10), i1 false) ; <i64> [#uses=1]
55  %1 = icmp eq i32 %0, -1                         ; <i1> [#uses=1]
56  br i1 %1, label %"47", label %"46"
57
58"46":                                             ; preds = %entry
59  unreachable
60
61"47":                                             ; preds = %entry
62  unreachable
63}
64
65@.str5 = private constant [9 x i32] [i32 97, i32 98, i32 99, i32 100, i32 0, i32
66 101, i32 102, i32 103, i32 0], align 4
67define i32 @test2() nounwind {
68; CHECK-LABEL: @test2(
69; CHECK-NEXT: ret i32 34
70  %1 = call i32 @llvm.objectsize.i32.p0i8(i8* getelementptr (i8, i8* bitcast ([9 x i32]* @.str5 to i8*), i32 2), i1 false)
71  ret i32 %1
72}
73
74; rdar://7674946
75@array = internal global [480 x float] zeroinitializer ; <[480 x float]*> [#uses=1]
76
77declare i8* @__memcpy_chk(i8*, i8*, i32, i32) nounwind
78
79declare i32 @llvm.objectsize.i32.p0i8(i8*, i1) nounwind readonly
80
81declare i8* @__inline_memcpy_chk(i8*, i8*, i32) nounwind inlinehint
82
83define void @test3() nounwind {
84; CHECK-LABEL: @test3(
85entry:
86  br i1 undef, label %bb11, label %bb12
87
88bb11:
89  %0 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1]
90  %1 = bitcast float* %0 to i8*                   ; <i8*> [#uses=1]
91  %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false) ; <i32> [#uses=1]
92  %3 = call i8* @__memcpy_chk(i8* undef, i8* undef, i32 512, i32 %2) nounwind ; <i8*> [#uses=0]
93; CHECK: unreachable
94  unreachable
95
96bb12:
97  %4 = getelementptr inbounds float, float* getelementptr inbounds ([480 x float], [480 x float]* @array, i32 0, i32 128), i32 -127 ; <float*> [#uses=1]
98  %5 = bitcast float* %4 to i8*                   ; <i8*> [#uses=1]
99  %6 = call i8* @__inline_memcpy_chk(i8* %5, i8* undef, i32 512) nounwind inlinehint ; <i8*> [#uses=0]
100; CHECK: @__inline_memcpy_chk
101  unreachable
102}
103
104; rdar://7718857
105
106%struct.data = type { [100 x i32], [100 x i32], [1024 x i8] }
107
108define i32 @test4(i8** %esc) nounwind ssp {
109; CHECK-LABEL: @test4(
110entry:
111  %0 = alloca %struct.data, align 8
112  %1 = bitcast %struct.data* %0 to i8*
113  %2 = call i32 @llvm.objectsize.i32.p0i8(i8* %1, i1 false) nounwind
114; CHECK-NOT: @llvm.objectsize
115; CHECK: @llvm.memset.p0i8.i32(i8* %1, i8 0, i32 1824, i32 8, i1 false)
116  %3 = call i8* @__memset_chk(i8* %1, i32 0, i32 1824, i32 %2) nounwind
117  store i8* %1, i8** %esc
118  ret i32 0
119}
120
121; rdar://7782496
122@s = external global i8*
123
124define i8* @test5(i32 %n) nounwind ssp {
125; CHECK-LABEL: @test5(
126entry:
127  %0 = tail call noalias i8* @malloc(i32 20) nounwind
128  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false)
129  %2 = load i8*, i8** @s, align 8
130; CHECK-NOT: @llvm.objectsize
131; CHECK: @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* %1, i32 10, i32 1, i1 false)
132  %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 10, i32 %1) nounwind
133  ret i8* %0
134}
135
136define void @test6(i32 %n) nounwind ssp {
137; CHECK-LABEL: @test6(
138entry:
139  %0 = tail call noalias i8* @malloc(i32 20) nounwind
140  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %0, i1 false)
141  %2 = load i8*, i8** @s, align 8
142; CHECK-NOT: @llvm.objectsize
143; CHECK: @__memcpy_chk(i8* %0, i8* %1, i32 30, i32 20)
144  %3 = tail call i8* @__memcpy_chk(i8* %0, i8* %2, i32 30, i32 %1) nounwind
145  ret void
146}
147
148declare i8* @__memset_chk(i8*, i32, i32, i32) nounwind
149
150declare noalias i8* @malloc(i32) nounwind
151
152define i32 @test7(i8** %esc) {
153; CHECK-LABEL: @test7(
154  %alloc = call noalias i8* @malloc(i32 48) nounwind
155  store i8* %alloc, i8** %esc
156  %gep = getelementptr inbounds i8, i8* %alloc, i32 16
157  %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false) nounwind readonly
158; CHECK: ret i32 32
159  ret i32 %objsize
160}
161
162declare noalias i8* @calloc(i32, i32) nounwind
163
164define i32 @test8(i8** %esc) {
165; CHECK-LABEL: @test8(
166  %alloc = call noalias i8* @calloc(i32 5, i32 7) nounwind
167  store i8* %alloc, i8** %esc
168  %gep = getelementptr inbounds i8, i8* %alloc, i32 5
169  %objsize = call i32 @llvm.objectsize.i32.p0i8(i8* %gep, i1 false) nounwind readonly
170; CHECK: ret i32 30
171  ret i32 %objsize
172}
173
174declare noalias i8* @strdup(i8* nocapture) nounwind
175declare noalias i8* @strndup(i8* nocapture, i32) nounwind
176
177; CHECK-LABEL: @test9(
178define i32 @test9(i8** %esc) {
179  %call = tail call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0)) nounwind
180  store i8* %call, i8** %esc, align 8
181  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true)
182; CHECK: ret i32 8
183  ret i32 %1
184}
185
186; CHECK-LABEL: @test10(
187define i32 @test10(i8** %esc) {
188  %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 3) nounwind
189  store i8* %call, i8** %esc, align 8
190  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true)
191; CHECK: ret i32 4
192  ret i32 %1
193}
194
195; CHECK-LABEL: @test11(
196define i32 @test11(i8** %esc) {
197  %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7) nounwind
198  store i8* %call, i8** %esc, align 8
199  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true)
200; CHECK: ret i32 8
201  ret i32 %1
202}
203
204; CHECK-LABEL: @test12(
205define i32 @test12(i8** %esc) {
206  %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 8) nounwind
207  store i8* %call, i8** %esc, align 8
208  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true)
209; CHECK: ret i32 8
210  ret i32 %1
211}
212
213; CHECK-LABEL: @test13(
214define i32 @test13(i8** %esc) {
215  %call = tail call i8* @strndup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 57) nounwind
216  store i8* %call, i8** %esc, align 8
217  %1 = tail call i32 @llvm.objectsize.i32.p0i8(i8* %call, i1 true)
218; CHECK: ret i32 8
219  ret i32 %1
220}
221
222@globalalias = internal alias [60 x i8], [60 x i8]* @a
223
224; CHECK-LABEL: @test18(
225; CHECK-NEXT: ret i32 60
226define i32 @test18() {
227  %bc = bitcast [60 x i8]* @globalalias to i8*
228  %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false)
229  ret i32 %1
230}
231
232@globalalias2 = weak alias [60 x i8], [60 x i8]* @a
233
234; CHECK-LABEL: @test19(
235; CHECK: llvm.objectsize
236define i32 @test19() {
237  %bc = bitcast [60 x i8]* @globalalias2 to i8*
238  %1 = call i32 @llvm.objectsize.i32.p0i8(i8* %bc, i1 false)
239  ret i32 %1
240}
241
242