blob: b4ded339287fa535106d76bf3157158652f36d7b [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
Nick Kralevichc6eb9852013-06-24 11:44:00 -070095TEST(Fortify1_Clang_DeathTest, sprintf_fortified) {
96 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
97 char buf[10];
98 char source_buf[15];
99 memcpy(source_buf, "12345678901234", 15);
100 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
101}
102
103TEST(Fortify1_Clang_DeathTest, sprintf2_fortified) {
104 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
105 char buf[5];
106 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
107}
108
Nick Kralevich16d1af12013-06-17 14:49:19 -0700109TEST(Fortify1_Clang_DeathTest, strncat_fortified) {
110 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
111 char buf[10];
112 size_t n = atoi("10"); // avoid compiler optimizations
113 strncpy(buf, "012345678", n);
114 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
115}
116
117TEST(Fortify1_Clang_DeathTest, strncat2_fortified) {
118 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
119 char buf[10];
120 buf[0] = '\0';
121 size_t n = atoi("10"); // avoid compiler optimizations
122 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
123}
124
125TEST(Fortify1_Clang_DeathTest, strcat_fortified) {
126 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
127 char src[11];
128 strcpy(src, "0123456789");
129 char buf[10];
130 buf[0] = '\0';
131 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
132}
133
134TEST(Fortify1_Clang_DeathTest, memmove_fortified) {
135 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
136 char buf[20];
137 strcpy(buf, "0123456789");
138 size_t n = atoi("10");
139 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
140}
141
142TEST(Fortify1_Clang_DeathTest, memcpy_fortified) {
143 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
144 char bufa[10];
145 char bufb[10];
146 strcpy(bufa, "012345678");
147 size_t n = atoi("11");
148 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
149}
150
151TEST(Fortify1_Clang_DeathTest, strncpy_fortified) {
152 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
153 char bufa[15];
154 char bufb[10];
155 strcpy(bufa, "01234567890123");
156 size_t n = strlen(bufa);
157 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
158}
159
160extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
161extern "C" char* __strcat_chk(char*, const char*, size_t);
162
163TEST(Fortify1_Clang, strncat) {
164 char buf[10];
165 memset(buf, 'A', sizeof(buf));
166 buf[0] = 'a';
167 buf[1] = '\0';
168 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
169 ASSERT_EQ(buf, res);
170 ASSERT_EQ('a', buf[0]);
171 ASSERT_EQ('0', buf[1]);
172 ASSERT_EQ('1', buf[2]);
173 ASSERT_EQ('2', buf[3]);
174 ASSERT_EQ('3', buf[4]);
175 ASSERT_EQ('4', buf[5]);
176 ASSERT_EQ('\0', buf[6]);
177 ASSERT_EQ('A', buf[7]);
178 ASSERT_EQ('A', buf[8]);
179 ASSERT_EQ('A', buf[9]);
180}
181
182TEST(Fortify1_Clang, strncat2) {
183 char buf[10];
184 memset(buf, 'A', sizeof(buf));
185 buf[0] = 'a';
186 buf[1] = '\0';
187 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
188 ASSERT_EQ(buf, res);
189 ASSERT_EQ('a', buf[0]);
190 ASSERT_EQ('0', buf[1]);
191 ASSERT_EQ('1', buf[2]);
192 ASSERT_EQ('2', buf[3]);
193 ASSERT_EQ('3', buf[4]);
194 ASSERT_EQ('4', buf[5]);
195 ASSERT_EQ('\0', buf[6]);
196 ASSERT_EQ('A', buf[7]);
197 ASSERT_EQ('A', buf[8]);
198 ASSERT_EQ('A', buf[9]);
199}
200
201TEST(Fortify1_Clang, strncat3) {
202 char buf[10];
203 memset(buf, 'A', sizeof(buf));
204 buf[0] = '\0';
205 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
206 ASSERT_EQ(buf, res);
207 ASSERT_EQ('0', buf[0]);
208 ASSERT_EQ('1', buf[1]);
209 ASSERT_EQ('2', buf[2]);
210 ASSERT_EQ('3', buf[3]);
211 ASSERT_EQ('4', buf[4]);
212 ASSERT_EQ('\0', buf[5]);
213 ASSERT_EQ('A', buf[6]);
214 ASSERT_EQ('A', buf[7]);
215 ASSERT_EQ('A', buf[8]);
216 ASSERT_EQ('A', buf[9]);
217}
218
219TEST(Fortify1_Clang, strncat4) {
220 char buf[10];
221 memset(buf, 'A', sizeof(buf));
222 buf[9] = '\0';
223 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
224 ASSERT_EQ(buf, res);
225 ASSERT_EQ('A', buf[0]);
226 ASSERT_EQ('A', buf[1]);
227 ASSERT_EQ('A', buf[2]);
228 ASSERT_EQ('A', buf[3]);
229 ASSERT_EQ('A', buf[4]);
230 ASSERT_EQ('A', buf[5]);
231 ASSERT_EQ('A', buf[6]);
232 ASSERT_EQ('A', buf[7]);
233 ASSERT_EQ('A', buf[8]);
234 ASSERT_EQ('\0', buf[9]);
235}
236
237TEST(Fortify1_Clang, strncat5) {
238 char buf[10];
239 memset(buf, 'A', sizeof(buf));
240 buf[0] = 'a';
241 buf[1] = '\0';
242 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
243 ASSERT_EQ(buf, res);
244 ASSERT_EQ('a', buf[0]);
245 ASSERT_EQ('0', buf[1]);
246 ASSERT_EQ('1', buf[2]);
247 ASSERT_EQ('2', buf[3]);
248 ASSERT_EQ('3', buf[4]);
249 ASSERT_EQ('4', buf[5]);
250 ASSERT_EQ('5', buf[6]);
251 ASSERT_EQ('6', buf[7]);
252 ASSERT_EQ('7', buf[8]);
253 ASSERT_EQ('\0', buf[9]);
254}
255
256TEST(Fortify1_Clang, strncat6) {
257 char buf[10];
258 memset(buf, 'A', sizeof(buf));
259 buf[0] = 'a';
260 buf[1] = '\0';
261 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
262 ASSERT_EQ(buf, res);
263 ASSERT_EQ('a', buf[0]);
264 ASSERT_EQ('0', buf[1]);
265 ASSERT_EQ('1', buf[2]);
266 ASSERT_EQ('2', buf[3]);
267 ASSERT_EQ('3', buf[4]);
268 ASSERT_EQ('4', buf[5]);
269 ASSERT_EQ('5', buf[6]);
270 ASSERT_EQ('6', buf[7]);
271 ASSERT_EQ('7', buf[8]);
272 ASSERT_EQ('\0', buf[9]);
273}
274
275
276TEST(Fortify1_Clang, strcat) {
277 char buf[10];
278 memset(buf, 'A', sizeof(buf));
279 buf[0] = 'a';
280 buf[1] = '\0';
281 char* res = __strcat_chk(buf, "01234", sizeof(buf));
282 ASSERT_EQ(buf, res);
283 ASSERT_EQ('a', buf[0]);
284 ASSERT_EQ('0', buf[1]);
285 ASSERT_EQ('1', buf[2]);
286 ASSERT_EQ('2', buf[3]);
287 ASSERT_EQ('3', buf[4]);
288 ASSERT_EQ('4', buf[5]);
289 ASSERT_EQ('\0', buf[6]);
290 ASSERT_EQ('A', buf[7]);
291 ASSERT_EQ('A', buf[8]);
292 ASSERT_EQ('A', buf[9]);
293}
294
295TEST(Fortify1_Clang, strcat2) {
296 char buf[10];
297 memset(buf, 'A', sizeof(buf));
298 buf[0] = 'a';
299 buf[1] = '\0';
300 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
301 ASSERT_EQ(buf, res);
302 ASSERT_EQ('a', buf[0]);
303 ASSERT_EQ('0', buf[1]);
304 ASSERT_EQ('1', buf[2]);
305 ASSERT_EQ('2', buf[3]);
306 ASSERT_EQ('3', buf[4]);
307 ASSERT_EQ('4', buf[5]);
308 ASSERT_EQ('5', buf[6]);
309 ASSERT_EQ('6', buf[7]);
310 ASSERT_EQ('7', buf[8]);
311 ASSERT_EQ('\0', buf[9]);
312}
313
314__BIONIC_FORTIFY_INLINE
315size_t test_fortify_inline(char* buf) {
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700316 return __bos(buf);
Nick Kralevich16d1af12013-06-17 14:49:19 -0700317}
318
319TEST(Fortify1_Clang, fortify_inline) {
320 char buf[1024];
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700321 // no-op. Prints nothing. Needed to prevent the compiler
322 // from optimizing out buf.
323 buf[0] = '\0';
324 printf("%s", buf);
325 ASSERT_EQ(sizeof(buf), test_fortify_inline(buf));
Nick Kralevich16d1af12013-06-17 14:49:19 -0700326}