blob: de279b1a2a35d0a4e3d32183e1b4d1e7a9c88a05 [file] [log] [blame]
Nick Kralevich5bcf3982013-06-28 10:34:09 -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#include <gtest/gtest.h>
Yabin Cui9df70402014-11-05 18:01:01 -080018#include "BionicDeathTest.h"
Nick Kralevich5bcf3982013-06-28 10:34:09 -070019
Yabin Cui9df70402014-11-05 18:01:01 -080020#include <fcntl.h>
21#include <malloc.h>
Elliott Hughes4674e382015-02-02 09:15:19 -080022#include <poll.h>
Yabin Cui9df70402014-11-05 18:01:01 -080023#include <signal.h>
24#include <stdarg.h>
25#include <string.h>
26#include <sys/socket.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29
30// Fortify test code needs to run multiple times, so TEST_NAME macro is used to
31// distinguish different tests. TEST_NAME is defined in compilation command.
Nick Kralevich5bcf3982013-06-28 10:34:09 -070032#define DEATHTEST_PASTER(name) name##_DeathTest
33#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
34#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
35
Yabin Cui9df70402014-11-05 18:01:01 -080036class DEATHTEST : public BionicDeathTest {};
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070037
Nick Kralevich5bcf3982013-06-28 10:34:09 -070038#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
39struct foo {
40 char empty[0];
41 char one[1];
42 char a[10];
43 char b[10];
44};
45
46#ifndef __clang__
47// This test is disabled in clang because clang doesn't properly detect
48// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070049TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070050 foo myfoo;
51 int copy_amt = atoi("11");
52 ASSERT_EXIT(stpncpy(myfoo.a, "01234567890", copy_amt),
53 testing::KilledBySignal(SIGABRT), "");
54}
55#endif
56
57#ifndef __clang__
58// This test is disabled in clang because clang doesn't properly detect
59// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070060TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070061 foo myfoo;
62 memset(&myfoo, 0, sizeof(myfoo));
63 myfoo.one[0] = 'A'; // not null terminated string
64 ASSERT_EXIT(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)),
65 testing::KilledBySignal(SIGABRT), "");
66}
67#endif
68
69#ifndef __clang__
70// This test is disabled in clang because clang doesn't properly detect
71// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070072TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070073 foo myfoo;
74 int copy_amt = atoi("11");
75 ASSERT_EXIT(strncpy(myfoo.a, "01234567890", copy_amt),
76 testing::KilledBySignal(SIGABRT), "");
77}
78#endif
79
80#ifndef __clang__
81// This test is disabled in clang because clang doesn't properly detect
82// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070083TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070084 foo myfoo;
85 memset(&myfoo, 0, sizeof(myfoo));
86 myfoo.one[0] = 'A'; // not null terminated string
87 ASSERT_EXIT(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)),
88 testing::KilledBySignal(SIGABRT), "");
89}
90#endif
91
92#ifndef __clang__
93// This test is disabled in clang because clang doesn't properly detect
94// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070095TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070096 foo myfoo;
97 char source_buf[15];
98 memcpy(source_buf, "12345678901234", 15);
99 ASSERT_EXIT(sprintf(myfoo.a, "%s", source_buf),
100 testing::KilledBySignal(SIGABRT), "");
101}
102#endif
103
104#ifndef __clang__
Nick Kralevich884a3de2014-10-06 00:39:47 +0000105// This test is disabled in clang because clang doesn't properly detect
106// this buffer overflow. TODO: Fix clang.
107TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000108 foo myfoo;
109 ASSERT_EXIT(sprintf(myfoo.a, "0123456789"),
110 testing::KilledBySignal(SIGABRT), "");
111}
112#endif
113
114#ifndef __clang__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700115// These tests are disabled in clang because clang doesn't properly detect
116// this buffer overflow. TODO: Fix clang.
117static int vsprintf_helper2(const char *fmt, ...) {
118 foo myfoo;
119 va_list va;
120 int result;
121
122 va_start(va, fmt);
123 result = vsprintf(myfoo.a, fmt, va); // should crash here
124 va_end(va);
125 return result;
126}
127
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700128TEST_F(DEATHTEST, vsprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700129 ASSERT_EXIT(vsprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
130}
131
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700132TEST_F(DEATHTEST, vsprintf2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700133 ASSERT_EXIT(vsprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
134}
135#endif
136
137#ifndef __clang__
138// These tests are disabled in clang because clang doesn't properly detect
139// this buffer overflow. TODO: Fix clang.
140static int vsnprintf_helper2(const char *fmt, ...) {
141 foo myfoo;
142 va_list va;
143 int result;
144 size_t size = atoi("11");
145
146 va_start(va, fmt);
147 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
148 va_end(va);
149 return result;
150}
151
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700152TEST_F(DEATHTEST, vsnprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700153 ASSERT_EXIT(vsnprintf_helper2("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
154}
155
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700156TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700157 ASSERT_EXIT(vsnprintf_helper2("0123456789"), testing::KilledBySignal(SIGABRT), "");
158}
159#endif
160
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700161#ifndef __clang__
162// zero sized target with "\0" source (should fail)
163// This test is disabled in clang because clang doesn't properly detect
164// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700165TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700166#if defined(__BIONIC__)
Christopher Ferris950a58e2014-04-04 14:38:18 -0700167 foo myfoo;
168 char* src = strdup("");
169 ASSERT_EXIT(stpcpy(myfoo.empty, src),
170 testing::KilledBySignal(SIGABRT), "");
171 free(src);
172#else // __BIONIC__
173 GTEST_LOG_(INFO) << "This test does nothing.\n";
174#endif // __BIONIC__
175}
176#endif
177
178#ifndef __clang__
179// zero sized target with "\0" source (should fail)
180// This test is disabled in clang because clang doesn't properly detect
181// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700182TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800183#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700184 foo myfoo;
185 char* src = strdup("");
186 ASSERT_EXIT(strcpy(myfoo.empty, src),
187 testing::KilledBySignal(SIGABRT), "");
188 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800189#else // __BIONIC__
190 GTEST_LOG_(INFO) << "This test does nothing.\n";
191#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700192}
193#endif
194
195#ifndef __clang__
196// zero sized target with longer source (should fail)
197// This test is disabled in clang because clang doesn't properly detect
198// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700199TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800200#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700201 foo myfoo;
202 char* src = strdup("1");
203 ASSERT_EXIT(strcpy(myfoo.empty, src),
204 testing::KilledBySignal(SIGABRT), "");
205 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800206#else // __BIONIC__
207 GTEST_LOG_(INFO) << "This test does nothing.\n";
208#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700209}
210#endif
211
212#ifndef __clang__
213// one byte target with longer source (should fail)
214// This test is disabled in clang because clang doesn't properly detect
215// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700216TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800217#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700218 foo myfoo;
219 char* src = strdup("12");
220 ASSERT_EXIT(strcpy(myfoo.one, src),
221 testing::KilledBySignal(SIGABRT), "");
222 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800223#else // __BIONIC__
224 GTEST_LOG_(INFO) << "This test does nothing.\n";
225#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700226}
227#endif
228
229#ifndef __clang__
230// This test is disabled in clang because clang doesn't properly detect
231// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700232TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800233#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700234 foo myfoo;
235 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
236 myfoo.b[0] = '\0';
237 ASSERT_EXIT(printf("%s", strchr(myfoo.a, 'a')),
238 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800239#else // __BIONIC__
240 GTEST_LOG_(INFO) << "This test does nothing.\n";
241#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700242}
243#endif
244
245#ifndef __clang__
246// This test is disabled in clang because clang doesn't properly detect
247// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700248TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800249#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700250 foo myfoo;
251 memcpy(myfoo.a, "0123456789", 10);
252 memcpy(myfoo.b, "01234", 6);
253 ASSERT_EXIT(printf("%s", strrchr(myfoo.a, 'a')),
254 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800255#else // __BIONIC__
256 GTEST_LOG_(INFO) << "This test does nothing.\n";
257#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700258}
259#endif
260
261#ifndef __clang__
262// This test is disabled in clang because clang doesn't properly detect
263// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700264TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800265#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700266 foo myfoo;
267 strcpy(myfoo.a, "01");
268 size_t n = strlen(myfoo.a);
269 ASSERT_EXIT(strlcpy(myfoo.one, myfoo.a, n),
270 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800271#else // __BIONIC__
272 GTEST_LOG_(INFO) << "This test does nothing.\n";
273#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700274}
275#endif
276
Nick Kralevicha6cde392013-06-29 08:15:25 -0700277#ifndef __clang__
278// This test is disabled in clang because clang doesn't properly detect
279// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700280TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800281#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700282 foo myfoo;
283 strcpy(myfoo.a, "01");
284 myfoo.one[0] = '\0';
285 size_t n = strlen(myfoo.a);
286 ASSERT_EXIT(strlcat(myfoo.one, myfoo.a, n),
287 testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800288#else // __BIONIC__
289 GTEST_LOG_(INFO) << "This test does nothing.\n";
290#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700291}
292#endif
293
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700294#ifndef __clang__
295// This test is disabled in clang because clang doesn't properly detect
296// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700297TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700298 foo myfoo;
299 size_t n = atoi("10"); // avoid compiler optimizations
300 strncpy(myfoo.a, "012345678", n);
301 ASSERT_EXIT(strncat(myfoo.a, "9", n), testing::KilledBySignal(SIGABRT), "");
302}
303#endif
304
305#ifndef __clang__
306// This test is disabled in clang because clang doesn't properly detect
307// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700308TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700309 foo myfoo;
310 myfoo.a[0] = '\0';
311 size_t n = atoi("10"); // avoid compiler optimizations
312 ASSERT_EXIT(strncat(myfoo.a, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
313}
314#endif
315
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700316TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700317 foo myfoo;
318 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
319 myfoo.b[0] = '\0';
320 size_t n = atoi("10"); // avoid compiler optimizations
321 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
322}
323
324#ifndef __clang__
325// This test is disabled in clang because clang doesn't properly detect
326// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700327TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700328 char src[11];
329 strcpy(src, "0123456789");
330 foo myfoo;
331 myfoo.a[0] = '\0';
332 ASSERT_EXIT(strcat(myfoo.a, src), testing::KilledBySignal(SIGABRT), "");
333}
334#endif
335
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700336TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700337 foo myfoo;
338 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
339 myfoo.b[0] = '\0';
340 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
341}
342
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700343TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700344 foo myfoo;
345 strcpy(myfoo.a, "012345678");
346 size_t n = strlen(myfoo.a) + 2;
347 ASSERT_EXIT(snprintf(myfoo.b, n, "a%s", myfoo.a), testing::KilledBySignal(SIGABRT), "");
348}
349
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700350TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700351 foo myfoo;
352 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
353 size_t n = atoi("11");
354 ASSERT_EXIT(bzero(myfoo.b, n), testing::KilledBySignal(SIGABRT), "");
355}
356
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700357#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
358
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700359// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700360TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800361#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700362 char buf[10];
363 char *orig = strdup("0123456789");
364 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
365 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800366#else // __BIONIC__
367 GTEST_LOG_(INFO) << "This test does nothing.\n";
368#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700369}
370
371// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700372TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800373#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700374 char buf[0];
375 char *orig = strdup("");
376 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
377 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800378#else // __BIONIC__
379 GTEST_LOG_(INFO) << "This test does nothing.\n";
380#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700381}
382
383// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700384TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800385#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700386 char buf[0];
387 char *orig = strdup("1");
388 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
389 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800390#else // __BIONIC__
391 GTEST_LOG_(INFO) << "This test does nothing.\n";
392#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700393}
394
395// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700396TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800397#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700398 char buf[1];
399 char *orig = strdup("12");
400 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
401 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800402#else // __BIONIC__
403 GTEST_LOG_(INFO) << "This test does nothing.\n";
404#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700405}
406
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700407TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800408#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700409 char buf[10];
410 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughes5b9310e2013-10-02 16:59:05 -0700411 ASSERT_EXIT(printf("%zd", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800412#else // __BIONIC__
413 GTEST_LOG_(INFO) << "This test does nothing.\n";
414#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700415}
416
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700417TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800418#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700419 char buf[10];
420 memcpy(buf, "0123456789", sizeof(buf));
421 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800422#else // __BIONIC__
423 GTEST_LOG_(INFO) << "This test does nothing.\n";
424#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700425}
426
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700427TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800428#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700429 char buf[10];
430 memcpy(buf, "0123456789", sizeof(buf));
431 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800432#else // __BIONIC__
433 GTEST_LOG_(INFO) << "This test does nothing.\n";
434#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700435}
436
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700437TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800438#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700439 char bufa[15];
440 char bufb[10];
441 strcpy(bufa, "01234567890123");
442 size_t n = strlen(bufa);
443 ASSERT_EXIT(strlcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800444#else // __BIONIC__
445 GTEST_LOG_(INFO) << "This test does nothing.\n";
446#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700447}
448
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700449TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800450#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700451 char bufa[15];
452 char bufb[10];
453 bufb[0] = '\0';
454 strcpy(bufa, "01234567890123");
455 size_t n = strlen(bufa);
456 ASSERT_EXIT(strlcat(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800457#else // __BIONIC__
458 GTEST_LOG_(INFO) << "This test does nothing.\n";
459#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700460}
461
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700462TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700463 char buf[10];
464 char source_buf[15];
465 memcpy(source_buf, "12345678901234", 15);
466 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
467}
468
Nick Kralevichb91791d2013-10-02 14:14:40 -0700469#ifndef __clang__
470// This test is disabled in clang because clang doesn't properly detect
471// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700472TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700473 char* buf = (char *) malloc(10);
474 char source_buf[11];
475 memcpy(source_buf, "1234567890", 11);
476 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
477 free(buf);
478}
479#endif
480
Nick Kralevich884a3de2014-10-06 00:39:47 +0000481TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000482 char buf[5];
483 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
484}
485
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700486static int vsprintf_helper(const char *fmt, ...) {
487 char buf[10];
488 va_list va;
489 int result;
490
491 va_start(va, fmt);
492 result = vsprintf(buf, fmt, va); // should crash here
493 va_end(va);
494 return result;
495}
496
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700497TEST_F(DEATHTEST, vsprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700498 ASSERT_EXIT(vsprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
499}
500
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700501TEST_F(DEATHTEST, vsprintf2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700502 ASSERT_EXIT(vsprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
503}
504
505static int vsnprintf_helper(const char *fmt, ...) {
506 char buf[10];
507 va_list va;
508 int result;
509 size_t size = atoi("11");
510
511 va_start(va, fmt);
512 result = vsnprintf(buf, size, fmt, va); // should crash here
513 va_end(va);
514 return result;
515}
516
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700517TEST_F(DEATHTEST, vsnprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700518 ASSERT_EXIT(vsnprintf_helper("%s", "0123456789"), testing::KilledBySignal(SIGABRT), "");
519}
520
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700521TEST_F(DEATHTEST, vsnprintf2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700522 ASSERT_EXIT(vsnprintf_helper("0123456789"), testing::KilledBySignal(SIGABRT), "");
523}
524
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700525TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700526 char buf[10];
527 size_t n = atoi("10"); // avoid compiler optimizations
528 strncpy(buf, "012345678", n);
529 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
530}
531
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700532TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700533 char buf[10];
534 buf[0] = '\0';
535 size_t n = atoi("10"); // avoid compiler optimizations
536 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
537}
538
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700539TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700540 char src[11];
541 strcpy(src, "0123456789");
542 char buf[10];
543 buf[0] = '\0';
544 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
545}
546
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700547TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700548 char buf[20];
549 strcpy(buf, "0123456789");
550 size_t n = atoi("10");
551 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
552}
553
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700554TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700555 char bufa[10];
556 char bufb[10];
557 strcpy(bufa, "012345678");
558 size_t n = atoi("11");
559 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
560}
561
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700562TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700563 char bufa[15];
564 char bufb[10];
565 strcpy(bufa, "01234567890123");
566 size_t n = strlen(bufa);
567 ASSERT_EXIT(stpncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
568}
569
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700570TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700571 char dest[11];
572 char src[10];
573 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
574 ASSERT_EXIT(stpncpy(dest, src, sizeof(dest)), testing::KilledBySignal(SIGABRT), "");
575}
576
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700577TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700578 char bufa[15];
579 char bufb[10];
580 strcpy(bufa, "01234567890123");
581 size_t n = strlen(bufa);
582 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
583}
584
Christopher Ferris950a58e2014-04-04 14:38:18 -0700585
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700586TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700587 char dest[11];
588 char src[10];
589 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
590 ASSERT_EXIT(strncpy(dest, src, sizeof(dest)), testing::KilledBySignal(SIGABRT), "");
591}
592
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700593TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700594 char bufa[15];
595 char bufb[10];
596 strcpy(bufa, "0123456789");
597 size_t n = strlen(bufa) + 1;
598 ASSERT_EXIT(snprintf(bufb, n, "%s", bufa), testing::KilledBySignal(SIGABRT), "");
599}
600
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700601TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700602 char buf[10];
603 memcpy(buf, "0123456789", sizeof(buf));
604 size_t n = atoi("11");
605 ASSERT_EXIT(bzero(buf, n), testing::KilledBySignal(SIGABRT), "");
606}
607
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700608TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700609 mode_t mask = atoi("1023"); // 01777 in octal
610 ASSERT_EXIT(umask(mask), testing::KilledBySignal(SIGABRT), "");
611}
612
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700613TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700614 size_t data_len = atoi("11"); // suppress compiler optimizations
615 char buf[10];
616 ASSERT_EXIT(recv(0, buf, data_len, 0), testing::KilledBySignal(SIGABRT), "");
617}
618
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700619TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700620#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700621 fd_set set;
622 memset(&set, 0, sizeof(set));
623 ASSERT_EXIT(FD_ISSET(-1, &set), testing::KilledBySignal(SIGABRT), "");
Elliott Hughes409588c2014-04-23 23:02:43 -0700624#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700625}
626
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700627TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700628 char buf[1];
629 fd_set* set = (fd_set*) buf;
630 ASSERT_EXIT(FD_ISSET(0, set), testing::KilledBySignal(SIGABRT), "");
631}
632
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700633TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700634 char buf[1];
635 size_t ct = atoi("2"); // prevent optimizations
636 int fd = open("/dev/null", O_RDONLY);
637 ASSERT_EXIT(read(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
638 close(fd);
639}
640
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700641extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
642extern "C" char* __strcat_chk(char*, const char*, size_t);
643
644TEST(TEST_NAME, strncat) {
645 char buf[10];
646 memset(buf, 'A', sizeof(buf));
647 buf[0] = 'a';
648 buf[1] = '\0';
649 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
650 ASSERT_EQ(buf, res);
651 ASSERT_EQ('a', buf[0]);
652 ASSERT_EQ('0', buf[1]);
653 ASSERT_EQ('1', buf[2]);
654 ASSERT_EQ('2', buf[3]);
655 ASSERT_EQ('3', buf[4]);
656 ASSERT_EQ('4', buf[5]);
657 ASSERT_EQ('\0', buf[6]);
658 ASSERT_EQ('A', buf[7]);
659 ASSERT_EQ('A', buf[8]);
660 ASSERT_EQ('A', buf[9]);
661}
662
663TEST(TEST_NAME, strncat2) {
664 char buf[10];
665 memset(buf, 'A', sizeof(buf));
666 buf[0] = 'a';
667 buf[1] = '\0';
668 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
669 ASSERT_EQ(buf, res);
670 ASSERT_EQ('a', buf[0]);
671 ASSERT_EQ('0', buf[1]);
672 ASSERT_EQ('1', buf[2]);
673 ASSERT_EQ('2', buf[3]);
674 ASSERT_EQ('3', buf[4]);
675 ASSERT_EQ('4', buf[5]);
676 ASSERT_EQ('\0', buf[6]);
677 ASSERT_EQ('A', buf[7]);
678 ASSERT_EQ('A', buf[8]);
679 ASSERT_EQ('A', buf[9]);
680}
681
682TEST(TEST_NAME, strncat3) {
683 char buf[10];
684 memset(buf, 'A', sizeof(buf));
685 buf[0] = '\0';
686 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
687 ASSERT_EQ(buf, res);
688 ASSERT_EQ('0', buf[0]);
689 ASSERT_EQ('1', buf[1]);
690 ASSERT_EQ('2', buf[2]);
691 ASSERT_EQ('3', buf[3]);
692 ASSERT_EQ('4', buf[4]);
693 ASSERT_EQ('\0', buf[5]);
694 ASSERT_EQ('A', buf[6]);
695 ASSERT_EQ('A', buf[7]);
696 ASSERT_EQ('A', buf[8]);
697 ASSERT_EQ('A', buf[9]);
698}
699
700TEST(TEST_NAME, strncat4) {
701 char buf[10];
702 memset(buf, 'A', sizeof(buf));
703 buf[9] = '\0';
704 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
705 ASSERT_EQ(buf, res);
706 ASSERT_EQ('A', buf[0]);
707 ASSERT_EQ('A', buf[1]);
708 ASSERT_EQ('A', buf[2]);
709 ASSERT_EQ('A', buf[3]);
710 ASSERT_EQ('A', buf[4]);
711 ASSERT_EQ('A', buf[5]);
712 ASSERT_EQ('A', buf[6]);
713 ASSERT_EQ('A', buf[7]);
714 ASSERT_EQ('A', buf[8]);
715 ASSERT_EQ('\0', buf[9]);
716}
717
718TEST(TEST_NAME, strncat5) {
719 char buf[10];
720 memset(buf, 'A', sizeof(buf));
721 buf[0] = 'a';
722 buf[1] = '\0';
723 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
724 ASSERT_EQ(buf, res);
725 ASSERT_EQ('a', buf[0]);
726 ASSERT_EQ('0', buf[1]);
727 ASSERT_EQ('1', buf[2]);
728 ASSERT_EQ('2', buf[3]);
729 ASSERT_EQ('3', buf[4]);
730 ASSERT_EQ('4', buf[5]);
731 ASSERT_EQ('5', buf[6]);
732 ASSERT_EQ('6', buf[7]);
733 ASSERT_EQ('7', buf[8]);
734 ASSERT_EQ('\0', buf[9]);
735}
736
737TEST(TEST_NAME, strncat6) {
738 char buf[10];
739 memset(buf, 'A', sizeof(buf));
740 buf[0] = 'a';
741 buf[1] = '\0';
742 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
743 ASSERT_EQ(buf, res);
744 ASSERT_EQ('a', buf[0]);
745 ASSERT_EQ('0', buf[1]);
746 ASSERT_EQ('1', buf[2]);
747 ASSERT_EQ('2', buf[3]);
748 ASSERT_EQ('3', buf[4]);
749 ASSERT_EQ('4', buf[5]);
750 ASSERT_EQ('5', buf[6]);
751 ASSERT_EQ('6', buf[7]);
752 ASSERT_EQ('7', buf[8]);
753 ASSERT_EQ('\0', buf[9]);
754}
755
756
757TEST(TEST_NAME, strcat) {
758 char buf[10];
759 memset(buf, 'A', sizeof(buf));
760 buf[0] = 'a';
761 buf[1] = '\0';
762 char* res = __strcat_chk(buf, "01234", sizeof(buf));
763 ASSERT_EQ(buf, res);
764 ASSERT_EQ('a', buf[0]);
765 ASSERT_EQ('0', buf[1]);
766 ASSERT_EQ('1', buf[2]);
767 ASSERT_EQ('2', buf[3]);
768 ASSERT_EQ('3', buf[4]);
769 ASSERT_EQ('4', buf[5]);
770 ASSERT_EQ('\0', buf[6]);
771 ASSERT_EQ('A', buf[7]);
772 ASSERT_EQ('A', buf[8]);
773 ASSERT_EQ('A', buf[9]);
774}
775
776TEST(TEST_NAME, strcat2) {
777 char buf[10];
778 memset(buf, 'A', sizeof(buf));
779 buf[0] = 'a';
780 buf[1] = '\0';
781 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
782 ASSERT_EQ(buf, res);
783 ASSERT_EQ('a', buf[0]);
784 ASSERT_EQ('0', buf[1]);
785 ASSERT_EQ('1', buf[2]);
786 ASSERT_EQ('2', buf[3]);
787 ASSERT_EQ('3', buf[4]);
788 ASSERT_EQ('4', buf[5]);
789 ASSERT_EQ('5', buf[6]);
790 ASSERT_EQ('6', buf[7]);
791 ASSERT_EQ('7', buf[8]);
792 ASSERT_EQ('\0', buf[9]);
793}
Nick Kralevich93501d32013-08-28 10:47:43 -0700794
Christopher Ferris950a58e2014-04-04 14:38:18 -0700795TEST(TEST_NAME, stpncpy) {
796 char src[10];
797 char dst[10];
798 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
799 stpncpy(dst, src, sizeof(dst));
800 ASSERT_EQ('0', dst[0]);
801 ASSERT_EQ('1', dst[1]);
802 ASSERT_EQ('2', dst[2]);
803 ASSERT_EQ('3', dst[3]);
804 ASSERT_EQ('4', dst[4]);
805 ASSERT_EQ('5', dst[5]);
806 ASSERT_EQ('6', dst[6]);
807 ASSERT_EQ('7', dst[7]);
808 ASSERT_EQ('8', dst[8]);
809 ASSERT_EQ('9', dst[9]);
810}
811
812TEST(TEST_NAME, stpncpy2) {
813 char src[10];
814 char dst[15];
815 memcpy(src, "012345678\0", sizeof(src));
816 stpncpy(dst, src, sizeof(dst));
817 ASSERT_EQ('0', dst[0]);
818 ASSERT_EQ('1', dst[1]);
819 ASSERT_EQ('2', dst[2]);
820 ASSERT_EQ('3', dst[3]);
821 ASSERT_EQ('4', dst[4]);
822 ASSERT_EQ('5', dst[5]);
823 ASSERT_EQ('6', dst[6]);
824 ASSERT_EQ('7', dst[7]);
825 ASSERT_EQ('8', dst[8]);
826 ASSERT_EQ('\0', dst[9]);
827 ASSERT_EQ('\0', dst[10]);
828 ASSERT_EQ('\0', dst[11]);
829 ASSERT_EQ('\0', dst[12]);
830 ASSERT_EQ('\0', dst[13]);
831 ASSERT_EQ('\0', dst[14]);
832}
833
Nick Kralevich93501d32013-08-28 10:47:43 -0700834TEST(TEST_NAME, strncpy) {
835 char src[10];
836 char dst[10];
837 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
838 strncpy(dst, src, sizeof(dst));
839 ASSERT_EQ('0', dst[0]);
840 ASSERT_EQ('1', dst[1]);
841 ASSERT_EQ('2', dst[2]);
842 ASSERT_EQ('3', dst[3]);
843 ASSERT_EQ('4', dst[4]);
844 ASSERT_EQ('5', dst[5]);
845 ASSERT_EQ('6', dst[6]);
846 ASSERT_EQ('7', dst[7]);
847 ASSERT_EQ('8', dst[8]);
848 ASSERT_EQ('9', dst[9]);
849}
850
851TEST(TEST_NAME, strncpy2) {
852 char src[10];
853 char dst[15];
854 memcpy(src, "012345678\0", sizeof(src));
855 strncpy(dst, src, sizeof(dst));
856 ASSERT_EQ('0', dst[0]);
857 ASSERT_EQ('1', dst[1]);
858 ASSERT_EQ('2', dst[2]);
859 ASSERT_EQ('3', dst[3]);
860 ASSERT_EQ('4', dst[4]);
861 ASSERT_EQ('5', dst[5]);
862 ASSERT_EQ('6', dst[6]);
863 ASSERT_EQ('7', dst[7]);
864 ASSERT_EQ('8', dst[8]);
865 ASSERT_EQ('\0', dst[9]);
866 ASSERT_EQ('\0', dst[10]);
867 ASSERT_EQ('\0', dst[11]);
868 ASSERT_EQ('\0', dst[12]);
869 ASSERT_EQ('\0', dst[13]);
870 ASSERT_EQ('\0', dst[14]);
871}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700872
873TEST(TEST_NAME, strcat_chk_max_int_size) {
874 char buf[10];
875 memset(buf, 'A', sizeof(buf));
876 buf[0] = 'a';
877 buf[1] = '\0';
878 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
879 ASSERT_EQ(buf, res);
880 ASSERT_EQ('a', buf[0]);
881 ASSERT_EQ('0', buf[1]);
882 ASSERT_EQ('1', buf[2]);
883 ASSERT_EQ('2', buf[3]);
884 ASSERT_EQ('3', buf[4]);
885 ASSERT_EQ('4', buf[5]);
886 ASSERT_EQ('5', buf[6]);
887 ASSERT_EQ('6', buf[7]);
888 ASSERT_EQ('7', buf[8]);
889 ASSERT_EQ('\0', buf[9]);
890}
891
Christopher Ferris950a58e2014-04-04 14:38:18 -0700892extern "C" char* __stpcpy_chk(char*, const char*, size_t);
893
894TEST(TEST_NAME, stpcpy_chk_max_int_size) {
895 char buf[10];
896 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
897 ASSERT_EQ(buf + strlen("012345678"), res);
898 ASSERT_STREQ("012345678", buf);
899}
900
Christopher Ferris16e185c2013-09-10 16:56:34 -0700901extern "C" char* __strcpy_chk(char*, const char*, size_t);
902
903TEST(TEST_NAME, strcpy_chk_max_int_size) {
904 char buf[10];
905 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
906 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700907 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700908}
909
910extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
911
912TEST(TEST_NAME, memcpy_chk_max_int_size) {
913 char buf[10];
914 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
915 ASSERT_EQ((void*)buf, res);
916 ASSERT_EQ('0', buf[0]);
917 ASSERT_EQ('1', buf[1]);
918 ASSERT_EQ('2', buf[2]);
919 ASSERT_EQ('3', buf[3]);
920 ASSERT_EQ('4', buf[4]);
921 ASSERT_EQ('5', buf[5]);
922 ASSERT_EQ('6', buf[6]);
923 ASSERT_EQ('7', buf[7]);
924 ASSERT_EQ('8', buf[8]);
925 ASSERT_EQ('\0', buf[9]);
926}
Stephen Hines6e380722013-10-11 00:45:24 -0700927
928// Verify that macro expansion is done properly for sprintf/snprintf (which
929// are defined as macros in stdio.h under clang).
930#define CONTENTS "macro expansion"
931#define BUF_AND_SIZE(A) A, sizeof(A)
932#define BUF_AND_CONTENTS(A) A, CONTENTS
933#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
934TEST(TEST_NAME, s_n_printf_macro_expansion) {
935 char buf[BUFSIZ];
936 snprintf(BUF_AND_SIZE(buf), CONTENTS);
937 EXPECT_STREQ(CONTENTS, buf);
938
939 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
940 EXPECT_STREQ(CONTENTS, buf);
941
942 sprintf(BUF_AND_CONTENTS(buf));
943 EXPECT_STREQ(CONTENTS, buf);
944}
Elliott Hughes4674e382015-02-02 09:15:19 -0800945
946TEST_F(DEATHTEST, poll_fortified) {
947 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
948 pollfd buf[1] = {{0, POLLIN, 0}};
949 ASSERT_EXIT(poll(buf, fd_count, -1), testing::KilledBySignal(SIGABRT), "");
950}
951
952TEST_F(DEATHTEST, ppoll_fortified) {
953 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
954 pollfd buf[1] = {{0, POLLIN, 0}};
955 ASSERT_EXIT(ppoll(buf, fd_count, NULL, NULL), testing::KilledBySignal(SIGABRT), "");
956}