1*f4a2713aSLionel Sambuc // Test CodeGen for Security Check Overflow Builtins.
2*f4a2713aSLionel Sambuc // rdar://13421498
3*f4a2713aSLionel Sambuc
4*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple "i686-unknown-unknown" -emit-llvm -x c %s -o - | FileCheck %s
5*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple "x86_64-unknown-unknown" -emit-llvm -x c %s -o - | FileCheck %s
6*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple "x86_64-mingw32" -emit-llvm -x c %s -o - | FileCheck %s
7*f4a2713aSLionel Sambuc
8*f4a2713aSLionel Sambuc extern unsigned UnsignedErrorCode;
9*f4a2713aSLionel Sambuc extern unsigned long UnsignedLongErrorCode;
10*f4a2713aSLionel Sambuc extern unsigned long long UnsignedLongLongErrorCode;
11*f4a2713aSLionel Sambuc extern int IntErrorCode;
12*f4a2713aSLionel Sambuc extern long LongErrorCode;
13*f4a2713aSLionel Sambuc extern long long LongLongErrorCode;
14*f4a2713aSLionel Sambuc
test_uadd_overflow(unsigned x,unsigned y)15*f4a2713aSLionel Sambuc unsigned test_uadd_overflow(unsigned x, unsigned y) {
16*f4a2713aSLionel Sambuc // CHECK: @test_uadd_overflow
17*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %{{.+}}, i32 %{{.+}})
18*f4a2713aSLionel Sambuc unsigned result;
19*f4a2713aSLionel Sambuc if (__builtin_uadd_overflow(x, y, &result))
20*f4a2713aSLionel Sambuc return UnsignedErrorCode;
21*f4a2713aSLionel Sambuc return result;
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc
test_uaddl_overflow(unsigned long x,unsigned long y)24*f4a2713aSLionel Sambuc unsigned long test_uaddl_overflow(unsigned long x, unsigned long y) {
25*f4a2713aSLionel Sambuc // CHECK: @test_uaddl_overflow([[UL:i32|i64]] %x
26*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { [[UL]], i1 } @llvm.uadd.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %{{.+}})
27*f4a2713aSLionel Sambuc unsigned long result;
28*f4a2713aSLionel Sambuc if (__builtin_uaddl_overflow(x, y, &result))
29*f4a2713aSLionel Sambuc return UnsignedLongErrorCode;
30*f4a2713aSLionel Sambuc return result;
31*f4a2713aSLionel Sambuc }
32*f4a2713aSLionel Sambuc
test_uaddll_overflow(unsigned long long x,unsigned long long y)33*f4a2713aSLionel Sambuc unsigned long long test_uaddll_overflow(unsigned long long x, unsigned long long y) {
34*f4a2713aSLionel Sambuc // CHECK: @test_uaddll_overflow
35*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i64, i1 } @llvm.uadd.with.overflow.i64(i64 %{{.+}}, i64 %{{.+}})
36*f4a2713aSLionel Sambuc unsigned long long result;
37*f4a2713aSLionel Sambuc if (__builtin_uaddll_overflow(x, y, &result))
38*f4a2713aSLionel Sambuc return UnsignedLongLongErrorCode;
39*f4a2713aSLionel Sambuc return result;
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc
test_usub_overflow(unsigned x,unsigned y)42*f4a2713aSLionel Sambuc unsigned test_usub_overflow(unsigned x, unsigned y) {
43*f4a2713aSLionel Sambuc // CHECK: @test_usub_overflow
44*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 %{{.+}}, i32 %{{.+}})
45*f4a2713aSLionel Sambuc unsigned result;
46*f4a2713aSLionel Sambuc if (__builtin_usub_overflow(x, y, &result))
47*f4a2713aSLionel Sambuc return UnsignedErrorCode;
48*f4a2713aSLionel Sambuc return result;
49*f4a2713aSLionel Sambuc }
50*f4a2713aSLionel Sambuc
test_usubl_overflow(unsigned long x,unsigned long y)51*f4a2713aSLionel Sambuc unsigned long test_usubl_overflow(unsigned long x, unsigned long y) {
52*f4a2713aSLionel Sambuc // CHECK: @test_usubl_overflow([[UL:i32|i64]] %x
53*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { [[UL]], i1 } @llvm.usub.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %{{.+}})
54*f4a2713aSLionel Sambuc unsigned long result;
55*f4a2713aSLionel Sambuc if (__builtin_usubl_overflow(x, y, &result))
56*f4a2713aSLionel Sambuc return UnsignedLongErrorCode;
57*f4a2713aSLionel Sambuc return result;
58*f4a2713aSLionel Sambuc }
59*f4a2713aSLionel Sambuc
test_usubll_overflow(unsigned long long x,unsigned long long y)60*f4a2713aSLionel Sambuc unsigned long long test_usubll_overflow(unsigned long long x, unsigned long long y) {
61*f4a2713aSLionel Sambuc // CHECK: @test_usubll_overflow
62*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %{{.+}}, i64 %{{.+}})
63*f4a2713aSLionel Sambuc unsigned long long result;
64*f4a2713aSLionel Sambuc if (__builtin_usubll_overflow(x, y, &result))
65*f4a2713aSLionel Sambuc return UnsignedLongLongErrorCode;
66*f4a2713aSLionel Sambuc return result;
67*f4a2713aSLionel Sambuc }
68*f4a2713aSLionel Sambuc
test_umul_overflow(unsigned x,unsigned y)69*f4a2713aSLionel Sambuc unsigned test_umul_overflow(unsigned x, unsigned y) {
70*f4a2713aSLionel Sambuc // CHECK: @test_umul_overflow
71*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %{{.+}}, i32 %{{.+}})
72*f4a2713aSLionel Sambuc unsigned result;
73*f4a2713aSLionel Sambuc if (__builtin_umul_overflow(x, y, &result))
74*f4a2713aSLionel Sambuc return UnsignedErrorCode;
75*f4a2713aSLionel Sambuc return result;
76*f4a2713aSLionel Sambuc }
77*f4a2713aSLionel Sambuc
test_umull_overflow(unsigned long x,unsigned long y)78*f4a2713aSLionel Sambuc unsigned long test_umull_overflow(unsigned long x, unsigned long y) {
79*f4a2713aSLionel Sambuc // CHECK: @test_umull_overflow([[UL:i32|i64]] %x
80*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { [[UL]], i1 } @llvm.umul.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %{{.+}})
81*f4a2713aSLionel Sambuc unsigned long result;
82*f4a2713aSLionel Sambuc if (__builtin_umull_overflow(x, y, &result))
83*f4a2713aSLionel Sambuc return UnsignedLongErrorCode;
84*f4a2713aSLionel Sambuc return result;
85*f4a2713aSLionel Sambuc }
86*f4a2713aSLionel Sambuc
test_umulll_overflow(unsigned long long x,unsigned long long y)87*f4a2713aSLionel Sambuc unsigned long long test_umulll_overflow(unsigned long long x, unsigned long long y) {
88*f4a2713aSLionel Sambuc // CHECK: @test_umulll_overflow
89*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i64, i1 } @llvm.umul.with.overflow.i64(i64 %{{.+}}, i64 %{{.+}})
90*f4a2713aSLionel Sambuc unsigned long long result;
91*f4a2713aSLionel Sambuc if (__builtin_umulll_overflow(x, y, &result))
92*f4a2713aSLionel Sambuc return UnsignedLongLongErrorCode;
93*f4a2713aSLionel Sambuc return result;
94*f4a2713aSLionel Sambuc }
95*f4a2713aSLionel Sambuc
test_sadd_overflow(int x,int y)96*f4a2713aSLionel Sambuc int test_sadd_overflow(int x, int y) {
97*f4a2713aSLionel Sambuc // CHECK: @test_sadd_overflow
98*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %{{.+}}, i32 %{{.+}})
99*f4a2713aSLionel Sambuc int result;
100*f4a2713aSLionel Sambuc if (__builtin_sadd_overflow(x, y, &result))
101*f4a2713aSLionel Sambuc return IntErrorCode;
102*f4a2713aSLionel Sambuc return result;
103*f4a2713aSLionel Sambuc }
104*f4a2713aSLionel Sambuc
test_saddl_overflow(long x,long y)105*f4a2713aSLionel Sambuc long test_saddl_overflow(long x, long y) {
106*f4a2713aSLionel Sambuc // CHECK: @test_saddl_overflow([[UL:i32|i64]] %x
107*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { [[UL]], i1 } @llvm.sadd.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %{{.+}})
108*f4a2713aSLionel Sambuc long result;
109*f4a2713aSLionel Sambuc if (__builtin_saddl_overflow(x, y, &result))
110*f4a2713aSLionel Sambuc return LongErrorCode;
111*f4a2713aSLionel Sambuc return result;
112*f4a2713aSLionel Sambuc }
113*f4a2713aSLionel Sambuc
test_saddll_overflow(long long x,long long y)114*f4a2713aSLionel Sambuc long long test_saddll_overflow(long long x, long long y) {
115*f4a2713aSLionel Sambuc // CHECK: @test_saddll_overflow
116*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %{{.+}}, i64 %{{.+}})
117*f4a2713aSLionel Sambuc long long result;
118*f4a2713aSLionel Sambuc if (__builtin_saddll_overflow(x, y, &result))
119*f4a2713aSLionel Sambuc return LongLongErrorCode;
120*f4a2713aSLionel Sambuc return result;
121*f4a2713aSLionel Sambuc }
122*f4a2713aSLionel Sambuc
test_ssub_overflow(int x,int y)123*f4a2713aSLionel Sambuc int test_ssub_overflow(int x, int y) {
124*f4a2713aSLionel Sambuc // CHECK: @test_ssub_overflow
125*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %{{.+}}, i32 %{{.+}})
126*f4a2713aSLionel Sambuc int result;
127*f4a2713aSLionel Sambuc if (__builtin_ssub_overflow(x, y, &result))
128*f4a2713aSLionel Sambuc return IntErrorCode;
129*f4a2713aSLionel Sambuc return result;
130*f4a2713aSLionel Sambuc }
131*f4a2713aSLionel Sambuc
test_ssubl_overflow(long x,long y)132*f4a2713aSLionel Sambuc long test_ssubl_overflow(long x, long y) {
133*f4a2713aSLionel Sambuc // CHECK: @test_ssubl_overflow([[UL:i32|i64]] %x
134*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { [[UL]], i1 } @llvm.ssub.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %{{.+}})
135*f4a2713aSLionel Sambuc long result;
136*f4a2713aSLionel Sambuc if (__builtin_ssubl_overflow(x, y, &result))
137*f4a2713aSLionel Sambuc return LongErrorCode;
138*f4a2713aSLionel Sambuc return result;
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc
test_ssubll_overflow(long long x,long long y)141*f4a2713aSLionel Sambuc long long test_ssubll_overflow(long long x, long long y) {
142*f4a2713aSLionel Sambuc // CHECK: @test_ssubll_overflow
143*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i64, i1 } @llvm.ssub.with.overflow.i64(i64 %{{.+}}, i64 %{{.+}})
144*f4a2713aSLionel Sambuc long long result;
145*f4a2713aSLionel Sambuc if (__builtin_ssubll_overflow(x, y, &result))
146*f4a2713aSLionel Sambuc return LongLongErrorCode;
147*f4a2713aSLionel Sambuc return result;
148*f4a2713aSLionel Sambuc }
149*f4a2713aSLionel Sambuc
test_smul_overflow(int x,int y)150*f4a2713aSLionel Sambuc int test_smul_overflow(int x, int y) {
151*f4a2713aSLionel Sambuc // CHECK: @test_smul_overflow
152*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %{{.+}}, i32 %{{.+}})
153*f4a2713aSLionel Sambuc int result;
154*f4a2713aSLionel Sambuc if (__builtin_smul_overflow(x, y, &result))
155*f4a2713aSLionel Sambuc return IntErrorCode;
156*f4a2713aSLionel Sambuc return result;
157*f4a2713aSLionel Sambuc }
158*f4a2713aSLionel Sambuc
test_smull_overflow(long x,long y)159*f4a2713aSLionel Sambuc long test_smull_overflow(long x, long y) {
160*f4a2713aSLionel Sambuc // CHECK: @test_smull_overflow([[UL:i32|i64]] %x
161*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { [[UL]], i1 } @llvm.smul.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %{{.+}})
162*f4a2713aSLionel Sambuc long result;
163*f4a2713aSLionel Sambuc if (__builtin_smull_overflow(x, y, &result))
164*f4a2713aSLionel Sambuc return LongErrorCode;
165*f4a2713aSLionel Sambuc return result;
166*f4a2713aSLionel Sambuc }
167*f4a2713aSLionel Sambuc
test_smulll_overflow(long long x,long long y)168*f4a2713aSLionel Sambuc long long test_smulll_overflow(long long x, long long y) {
169*f4a2713aSLionel Sambuc // CHECK: @test_smulll_overflow
170*f4a2713aSLionel Sambuc // CHECK: %{{.+}} = call { i64, i1 } @llvm.smul.with.overflow.i64(i64 %{{.+}}, i64 %{{.+}})
171*f4a2713aSLionel Sambuc long long result;
172*f4a2713aSLionel Sambuc if (__builtin_smulll_overflow(x, y, &result))
173*f4a2713aSLionel Sambuc return LongLongErrorCode;
174*f4a2713aSLionel Sambuc return result;
175*f4a2713aSLionel Sambuc }
176