blob: fed9f13db9e3ae064a30c2d38feeded089cbe073 [file] [log] [blame]
Nick Kralevich16d1af12013-06-17 14:49:19 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 1
19
20#include <gtest/gtest.h>
21#include <string.h>
22
23#if __BIONIC__
24// We have to say "DeathTest" here so gtest knows to run this test (which exits)
25// in its own process.
26
27// multibyte target where we over fill (should fail)
28TEST(Fortify1_Clang_DeathTest, strcpy_fortified) {
29 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
30 char buf[10];
31 char *orig = strdup("0123456789");
32 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
33 free(orig);
34}
35
36// zero sized target with "\0" source (should fail)
37TEST(Fortify1_Clang_DeathTest, strcpy2_fortified) {
38 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
39 char buf[0];
40 char *orig = strdup("");
41 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
42 free(orig);
43}
44
45// zero sized target with longer source (should fail)
46TEST(Fortify1_Clang_DeathTest, strcpy3_fortified) {
47 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
48 char buf[0];
49 char *orig = strdup("1");
50 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
51 free(orig);
52}
53
54// one byte target with longer source (should fail)
55TEST(Fortify1_Clang_DeathTest, strcpy4_fortified) {
56 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
57 char buf[1];
58 char *orig = strdup("12");
59 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
60 free(orig);
61}
62
63TEST(Fortify1_Clang_DeathTest, strlen_fortified) {
64 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
65 char buf[10];
66 memcpy(buf, "0123456789", sizeof(buf));
67 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
68}
69
70TEST(Fortify1_Clang_DeathTest, strchr_fortified) {
71 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
72 char buf[10];
73 memcpy(buf, "0123456789", sizeof(buf));
74 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
75}
76
77TEST(Fortify1_Clang_DeathTest, strrchr_fortified) {
78 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
79 char buf[10];
80 memcpy(buf, "0123456789", sizeof(buf));
81 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
82}
Nick Kralevich8bafa742013-06-20 12:17:44 -070083
84TEST(Fortify1_Clang_DeathTest, strlcpy_fortified) {
85 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
86 char bufa[15];
87 char bufb[10];
88 strcpy(bufa, "01234567890123");
89 size_t n = strlen(bufa);
90 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
91}
92
Nick Kralevich16d1af12013-06-17 14:49:19 -070093#endif
94
95TEST(Fortify1_Clang_DeathTest, strncat_fortified) {
96 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
97 char buf[10];
98 size_t n = atoi("10"); // avoid compiler optimizations
99 strncpy(buf, "012345678", n);
100 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
101}
102
103TEST(Fortify1_Clang_DeathTest, strncat2_fortified) {
104 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105 char buf[10];
106 buf[0] = '\0';
107 size_t n = atoi("10"); // avoid compiler optimizations
108 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
109}
110
111TEST(Fortify1_Clang_DeathTest, strcat_fortified) {
112 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
113 char src[11];
114 strcpy(src, "0123456789");
115 char buf[10];
116 buf[0] = '\0';
117 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
118}
119
120TEST(Fortify1_Clang_DeathTest, memmove_fortified) {
121 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
122 char buf[20];
123 strcpy(buf, "0123456789");
124 size_t n = atoi("10");
125 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
126}
127
128TEST(Fortify1_Clang_DeathTest, memcpy_fortified) {
129 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
130 char bufa[10];
131 char bufb[10];
132 strcpy(bufa, "012345678");
133 size_t n = atoi("11");
134 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
135}
136
137TEST(Fortify1_Clang_DeathTest, strncpy_fortified) {
138 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
139 char bufa[15];
140 char bufb[10];
141 strcpy(bufa, "01234567890123");
142 size_t n = strlen(bufa);
143 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
144}
145
146extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
147extern "C" char* __strcat_chk(char*, const char*, size_t);
148
149TEST(Fortify1_Clang, strncat) {
150 char buf[10];
151 memset(buf, 'A', sizeof(buf));
152 buf[0] = 'a';
153 buf[1] = '\0';
154 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
155 ASSERT_EQ(buf, res);
156 ASSERT_EQ('a', buf[0]);
157 ASSERT_EQ('0', buf[1]);
158 ASSERT_EQ('1', buf[2]);
159 ASSERT_EQ('2', buf[3]);
160 ASSERT_EQ('3', buf[4]);
161 ASSERT_EQ('4', buf[5]);
162 ASSERT_EQ('\0', buf[6]);
163 ASSERT_EQ('A', buf[7]);
164 ASSERT_EQ('A', buf[8]);
165 ASSERT_EQ('A', buf[9]);
166}
167
168TEST(Fortify1_Clang, strncat2) {
169 char buf[10];
170 memset(buf, 'A', sizeof(buf));
171 buf[0] = 'a';
172 buf[1] = '\0';
173 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
174 ASSERT_EQ(buf, res);
175 ASSERT_EQ('a', buf[0]);
176 ASSERT_EQ('0', buf[1]);
177 ASSERT_EQ('1', buf[2]);
178 ASSERT_EQ('2', buf[3]);
179 ASSERT_EQ('3', buf[4]);
180 ASSERT_EQ('4', buf[5]);
181 ASSERT_EQ('\0', buf[6]);
182 ASSERT_EQ('A', buf[7]);
183 ASSERT_EQ('A', buf[8]);
184 ASSERT_EQ('A', buf[9]);
185}
186
187TEST(Fortify1_Clang, strncat3) {
188 char buf[10];
189 memset(buf, 'A', sizeof(buf));
190 buf[0] = '\0';
191 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
192 ASSERT_EQ(buf, res);
193 ASSERT_EQ('0', buf[0]);
194 ASSERT_EQ('1', buf[1]);
195 ASSERT_EQ('2', buf[2]);
196 ASSERT_EQ('3', buf[3]);
197 ASSERT_EQ('4', buf[4]);
198 ASSERT_EQ('\0', buf[5]);
199 ASSERT_EQ('A', buf[6]);
200 ASSERT_EQ('A', buf[7]);
201 ASSERT_EQ('A', buf[8]);
202 ASSERT_EQ('A', buf[9]);
203}
204
205TEST(Fortify1_Clang, strncat4) {
206 char buf[10];
207 memset(buf, 'A', sizeof(buf));
208 buf[9] = '\0';
209 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
210 ASSERT_EQ(buf, res);
211 ASSERT_EQ('A', buf[0]);
212 ASSERT_EQ('A', buf[1]);
213 ASSERT_EQ('A', buf[2]);
214 ASSERT_EQ('A', buf[3]);
215 ASSERT_EQ('A', buf[4]);
216 ASSERT_EQ('A', buf[5]);
217 ASSERT_EQ('A', buf[6]);
218 ASSERT_EQ('A', buf[7]);
219 ASSERT_EQ('A', buf[8]);
220 ASSERT_EQ('\0', buf[9]);
221}
222
223TEST(Fortify1_Clang, strncat5) {
224 char buf[10];
225 memset(buf, 'A', sizeof(buf));
226 buf[0] = 'a';
227 buf[1] = '\0';
228 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
229 ASSERT_EQ(buf, res);
230 ASSERT_EQ('a', buf[0]);
231 ASSERT_EQ('0', buf[1]);
232 ASSERT_EQ('1', buf[2]);
233 ASSERT_EQ('2', buf[3]);
234 ASSERT_EQ('3', buf[4]);
235 ASSERT_EQ('4', buf[5]);
236 ASSERT_EQ('5', buf[6]);
237 ASSERT_EQ('6', buf[7]);
238 ASSERT_EQ('7', buf[8]);
239 ASSERT_EQ('\0', buf[9]);
240}
241
242TEST(Fortify1_Clang, strncat6) {
243 char buf[10];
244 memset(buf, 'A', sizeof(buf));
245 buf[0] = 'a';
246 buf[1] = '\0';
247 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
248 ASSERT_EQ(buf, res);
249 ASSERT_EQ('a', buf[0]);
250 ASSERT_EQ('0', buf[1]);
251 ASSERT_EQ('1', buf[2]);
252 ASSERT_EQ('2', buf[3]);
253 ASSERT_EQ('3', buf[4]);
254 ASSERT_EQ('4', buf[5]);
255 ASSERT_EQ('5', buf[6]);
256 ASSERT_EQ('6', buf[7]);
257 ASSERT_EQ('7', buf[8]);
258 ASSERT_EQ('\0', buf[9]);
259}
260
261
262TEST(Fortify1_Clang, strcat) {
263 char buf[10];
264 memset(buf, 'A', sizeof(buf));
265 buf[0] = 'a';
266 buf[1] = '\0';
267 char* res = __strcat_chk(buf, "01234", sizeof(buf));
268 ASSERT_EQ(buf, res);
269 ASSERT_EQ('a', buf[0]);
270 ASSERT_EQ('0', buf[1]);
271 ASSERT_EQ('1', buf[2]);
272 ASSERT_EQ('2', buf[3]);
273 ASSERT_EQ('3', buf[4]);
274 ASSERT_EQ('4', buf[5]);
275 ASSERT_EQ('\0', buf[6]);
276 ASSERT_EQ('A', buf[7]);
277 ASSERT_EQ('A', buf[8]);
278 ASSERT_EQ('A', buf[9]);
279}
280
281TEST(Fortify1_Clang, strcat2) {
282 char buf[10];
283 memset(buf, 'A', sizeof(buf));
284 buf[0] = 'a';
285 buf[1] = '\0';
286 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
287 ASSERT_EQ(buf, res);
288 ASSERT_EQ('a', buf[0]);
289 ASSERT_EQ('0', buf[1]);
290 ASSERT_EQ('1', buf[2]);
291 ASSERT_EQ('2', buf[3]);
292 ASSERT_EQ('3', buf[4]);
293 ASSERT_EQ('4', buf[5]);
294 ASSERT_EQ('5', buf[6]);
295 ASSERT_EQ('6', buf[7]);
296 ASSERT_EQ('7', buf[8]);
297 ASSERT_EQ('\0', buf[9]);
298}
299
300__BIONIC_FORTIFY_INLINE
301size_t test_fortify_inline(char* buf) {
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700302 return __bos(buf);
Nick Kralevich16d1af12013-06-17 14:49:19 -0700303}
304
305TEST(Fortify1_Clang, fortify_inline) {
306 char buf[1024];
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700307 // no-op. Prints nothing. Needed to prevent the compiler
308 // from optimizing out buf.
309 buf[0] = '\0';
310 printf("%s", buf);
311 ASSERT_EQ(sizeof(buf), test_fortify_inline(buf));
Nick Kralevich16d1af12013-06-17 14:49:19 -0700312}