blob: 664e057a6d52ad2db315b57fa474d92c3e38f0d8 [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>
Yabin Cuif4fe6932015-02-03 17:52:32 -080029#include <time.h>
Yabin Cui9df70402014-11-05 18:01:01 -080030
Elliott Hughesd036e942015-02-02 11:18:58 -080031#if __BIONIC__
32#define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
33#else
34#define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "")
35#endif
36
Yabin Cui9df70402014-11-05 18:01:01 -080037// Fortify test code needs to run multiple times, so TEST_NAME macro is used to
38// distinguish different tests. TEST_NAME is defined in compilation command.
Nick Kralevich5bcf3982013-06-28 10:34:09 -070039#define DEATHTEST_PASTER(name) name##_DeathTest
40#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
41#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
42
Yabin Cui9df70402014-11-05 18:01:01 -080043class DEATHTEST : public BionicDeathTest {};
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070044
Nick Kralevich5bcf3982013-06-28 10:34:09 -070045#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
46struct foo {
47 char empty[0];
48 char one[1];
49 char a[10];
50 char b[10];
51};
52
53#ifndef __clang__
54// This test is disabled in clang because clang doesn't properly detect
55// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070056TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070057 foo myfoo;
58 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080059 ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
Christopher Ferris950a58e2014-04-04 14:38:18 -070060}
61#endif
62
63#ifndef __clang__
64// This test is disabled in clang because clang doesn't properly detect
65// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070066TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070067 foo myfoo;
68 memset(&myfoo, 0, sizeof(myfoo));
69 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080070 ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Christopher Ferris950a58e2014-04-04 14:38:18 -070071}
72#endif
73
74#ifndef __clang__
75// This test is disabled in clang because clang doesn't properly detect
76// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070077TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070078 foo myfoo;
79 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080080 ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070081}
82#endif
83
84#ifndef __clang__
85// This test is disabled in clang because clang doesn't properly detect
86// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070087TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070088 foo myfoo;
89 memset(&myfoo, 0, sizeof(myfoo));
90 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080091 ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Nick Kralevich93501d32013-08-28 10:47:43 -070092}
93#endif
94
95#ifndef __clang__
96// This test is disabled in clang because clang doesn't properly detect
97// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070098TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070099 foo myfoo;
100 char source_buf[15];
101 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800102 ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700103}
104#endif
105
106#ifndef __clang__
Nick Kralevich884a3de2014-10-06 00:39:47 +0000107// This test is disabled in clang because clang doesn't properly detect
108// this buffer overflow. TODO: Fix clang.
109TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000110 foo myfoo;
Elliott Hughesd036e942015-02-02 11:18:58 -0800111 ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000112}
113#endif
114
115#ifndef __clang__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700116// These tests are disabled in clang because clang doesn't properly detect
117// this buffer overflow. TODO: Fix clang.
118static int vsprintf_helper2(const char *fmt, ...) {
119 foo myfoo;
120 va_list va;
121 int result;
122
123 va_start(va, fmt);
124 result = vsprintf(myfoo.a, fmt, va); // should crash here
125 va_end(va);
126 return result;
127}
128
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700129TEST_F(DEATHTEST, vsprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800130 ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700131}
132
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700133TEST_F(DEATHTEST, vsprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800134 ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700135}
136#endif
137
138#ifndef __clang__
139// These tests are disabled in clang because clang doesn't properly detect
140// this buffer overflow. TODO: Fix clang.
141static int vsnprintf_helper2(const char *fmt, ...) {
142 foo myfoo;
143 va_list va;
144 int result;
145 size_t size = atoi("11");
146
147 va_start(va, fmt);
148 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
149 va_end(va);
150 return result;
151}
152
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700153TEST_F(DEATHTEST, vsnprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800154 ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700155}
156
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700157TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800158 ASSERT_FORTIFY(vsnprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700159}
160#endif
161
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700162#ifndef __clang__
163// zero sized target with "\0" source (should fail)
164// This test is disabled in clang because clang doesn't properly detect
165// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700166TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700167#if defined(__BIONIC__)
Christopher Ferris950a58e2014-04-04 14:38:18 -0700168 foo myfoo;
169 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800170 ASSERT_FORTIFY(stpcpy(myfoo.empty, src));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700171 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("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800186 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700187 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800188#else // __BIONIC__
189 GTEST_LOG_(INFO) << "This test does nothing.\n";
190#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700191}
192#endif
193
194#ifndef __clang__
195// zero sized target with longer source (should fail)
196// This test is disabled in clang because clang doesn't properly detect
197// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700198TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800199#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700200 foo myfoo;
201 char* src = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800202 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700203 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800204#else // __BIONIC__
205 GTEST_LOG_(INFO) << "This test does nothing.\n";
206#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700207}
208#endif
209
210#ifndef __clang__
211// one byte target with longer source (should fail)
212// This test is disabled in clang because clang doesn't properly detect
213// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700214TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800215#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700216 foo myfoo;
217 char* src = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800218 ASSERT_FORTIFY(strcpy(myfoo.one, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700219 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800220#else // __BIONIC__
221 GTEST_LOG_(INFO) << "This test does nothing.\n";
222#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700223}
224#endif
225
226#ifndef __clang__
227// This test is disabled in clang because clang doesn't properly detect
228// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700229TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800230#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700231 foo myfoo;
232 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
233 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800234 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800235#else // __BIONIC__
236 GTEST_LOG_(INFO) << "This test does nothing.\n";
237#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700238}
239#endif
240
241#ifndef __clang__
242// This test is disabled in clang because clang doesn't properly detect
243// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700244TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800245#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700246 foo myfoo;
247 memcpy(myfoo.a, "0123456789", 10);
248 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800249 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800250#else // __BIONIC__
251 GTEST_LOG_(INFO) << "This test does nothing.\n";
252#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700253}
254#endif
255
256#ifndef __clang__
257// This test is disabled in clang because clang doesn't properly detect
258// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700259TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800260#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700261 foo myfoo;
262 strcpy(myfoo.a, "01");
263 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800264 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800265#else // __BIONIC__
266 GTEST_LOG_(INFO) << "This test does nothing.\n";
267#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700268}
269#endif
270
Nick Kralevicha6cde392013-06-29 08:15:25 -0700271#ifndef __clang__
272// This test is disabled in clang because clang doesn't properly detect
273// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700274TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800275#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700276 foo myfoo;
277 strcpy(myfoo.a, "01");
278 myfoo.one[0] = '\0';
279 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800280 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800281#else // __BIONIC__
282 GTEST_LOG_(INFO) << "This test does nothing.\n";
283#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700284}
285#endif
286
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700287#ifndef __clang__
288// This test is disabled in clang because clang doesn't properly detect
289// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700290TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700291 foo myfoo;
292 size_t n = atoi("10"); // avoid compiler optimizations
293 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800294 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700295}
296#endif
297
298#ifndef __clang__
299// This test is disabled in clang because clang doesn't properly detect
300// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700301TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700302 foo myfoo;
303 myfoo.a[0] = '\0';
304 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800305 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700306}
307#endif
308
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700309TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700310 foo myfoo;
311 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
312 myfoo.b[0] = '\0';
313 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800314 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700315}
316
317#ifndef __clang__
318// This test is disabled in clang because clang doesn't properly detect
319// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700320TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700321 char src[11];
322 strcpy(src, "0123456789");
323 foo myfoo;
324 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800325 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700326}
327#endif
328
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700329TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700330 foo myfoo;
331 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
332 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800333 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700334}
335
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700336TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700337 foo myfoo;
338 strcpy(myfoo.a, "012345678");
339 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800340 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700341}
342
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700343TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700344 foo myfoo;
345 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
346 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800347 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700348}
349
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700350#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
351
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700352// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700353TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800354#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700355 char buf[10];
356 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800357 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700358 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800359#else // __BIONIC__
360 GTEST_LOG_(INFO) << "This test does nothing.\n";
361#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700362}
363
364// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700365TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800366#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700367 char buf[0];
368 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800369 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700370 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800371#else // __BIONIC__
372 GTEST_LOG_(INFO) << "This test does nothing.\n";
373#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700374}
375
376// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700377TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800378#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700379 char buf[0];
380 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800381 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700382 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800383#else // __BIONIC__
384 GTEST_LOG_(INFO) << "This test does nothing.\n";
385#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700386}
387
388// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700389TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800390#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700391 char buf[1];
392 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800393 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700394 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800395#else // __BIONIC__
396 GTEST_LOG_(INFO) << "This test does nothing.\n";
397#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700398}
399
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700400TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800401#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700402 char buf[10];
403 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800404 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800405#else // __BIONIC__
406 GTEST_LOG_(INFO) << "This test does nothing.\n";
407#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700408}
409
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700410TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800411#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700412 char buf[10];
413 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800414 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800415#else // __BIONIC__
416 GTEST_LOG_(INFO) << "This test does nothing.\n";
417#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700418}
419
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700420TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800421#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700422 char buf[10];
423 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800424 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800425#else // __BIONIC__
426 GTEST_LOG_(INFO) << "This test does nothing.\n";
427#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700428}
429
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700430TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800431#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700432 char bufa[15];
433 char bufb[10];
434 strcpy(bufa, "01234567890123");
435 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800436 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800437#else // __BIONIC__
438 GTEST_LOG_(INFO) << "This test does nothing.\n";
439#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700440}
441
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700442TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800443#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700444 char bufa[15];
445 char bufb[10];
446 bufb[0] = '\0';
447 strcpy(bufa, "01234567890123");
448 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800449 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800450#else // __BIONIC__
451 GTEST_LOG_(INFO) << "This test does nothing.\n";
452#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700453}
454
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700455TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700456 char buf[10];
457 char source_buf[15];
458 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800459 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700460}
461
Nick Kralevichb91791d2013-10-02 14:14:40 -0700462#ifndef __clang__
463// This test is disabled in clang because clang doesn't properly detect
464// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700465TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700466 char* buf = (char *) malloc(10);
467 char source_buf[11];
468 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800469 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700470 free(buf);
471}
472#endif
473
Nick Kralevich884a3de2014-10-06 00:39:47 +0000474TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000475 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800476 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000477}
478
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700479static int vsprintf_helper(const char *fmt, ...) {
480 char buf[10];
481 va_list va;
482 int result;
483
484 va_start(va, fmt);
485 result = vsprintf(buf, fmt, va); // should crash here
486 va_end(va);
487 return result;
488}
489
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700490TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800491 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700492}
493
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700494TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800495 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700496}
497
498static int vsnprintf_helper(const char *fmt, ...) {
499 char buf[10];
500 va_list va;
501 int result;
502 size_t size = atoi("11");
503
504 va_start(va, fmt);
505 result = vsnprintf(buf, size, fmt, va); // should crash here
506 va_end(va);
507 return result;
508}
509
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700510TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800511 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700512}
513
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700514TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800515 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700516}
517
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700518TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700519 char buf[10];
520 size_t n = atoi("10"); // avoid compiler optimizations
521 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800522 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700523}
524
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700525TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700526 char buf[10];
527 buf[0] = '\0';
528 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800529 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700530}
531
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700532TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700533 char src[11];
534 strcpy(src, "0123456789");
535 char buf[10];
536 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800537 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700538}
539
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700540TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700541 char buf[20];
542 strcpy(buf, "0123456789");
543 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800544 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700545}
546
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700547TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700548 char bufa[10];
549 char bufb[10];
550 strcpy(bufa, "012345678");
551 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800552 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700553}
554
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700555TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700556 char bufa[15];
557 char bufb[10];
558 strcpy(bufa, "01234567890123");
559 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800560 ASSERT_FORTIFY(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700561}
562
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700563TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700564 char dest[11];
565 char src[10];
566 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800567 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700568}
569
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700570TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700571 char bufa[15];
572 char bufb[10];
573 strcpy(bufa, "01234567890123");
574 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800575 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700576}
577
Christopher Ferris950a58e2014-04-04 14:38:18 -0700578
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700579TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700580 char dest[11];
581 char src[10];
582 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800583 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700584}
585
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700586TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700587 char bufa[15];
588 char bufb[10];
589 strcpy(bufa, "0123456789");
590 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800591 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700592}
593
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700594TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700595 char buf[10];
596 memcpy(buf, "0123456789", sizeof(buf));
597 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800598 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700599}
600
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700601TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700602 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800603 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700604}
605
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700606TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700607 size_t data_len = atoi("11"); // suppress compiler optimizations
608 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800609 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700610}
611
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700612TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700613#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700614 fd_set set;
615 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800616 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700617#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700618}
619
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700620TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700621 char buf[1];
622 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800623 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700624}
625
Daniel Micaye7e1c872015-04-16 09:07:45 -0400626TEST_F(DEATHTEST, pread_fortified) {
627 char buf[1];
628 size_t ct = atoi("2"); // prevent optimizations
629 int fd = open("/dev/null", O_RDONLY);
630 ASSERT_FORTIFY(pread(fd, buf, ct, 0));
631 close(fd);
632}
633
634TEST_F(DEATHTEST, pread64_fortified) {
635 char buf[1];
636 size_t ct = atoi("2"); // prevent optimizations
637 int fd = open("/dev/null", O_RDONLY);
638 ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
639 close(fd);
640}
641
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700642TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700643 char buf[1];
644 size_t ct = atoi("2"); // prevent optimizations
645 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800646 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700647 close(fd);
648}
649
Daniel Micayfed26592015-07-18 13:55:51 -0400650TEST_F(DEATHTEST, fread_fortified) {
651 char buf[1];
652 size_t ct = atoi("2"); // prevent optimizations
653 FILE* fp = fopen("/dev/null", "r");
654 ASSERT_FORTIFY(fread(buf, 1, ct, fp));
655 fclose(fp);
656}
657
658TEST_F(DEATHTEST, fwrite_fortified) {
659 char buf[1] = {0};
660 size_t ct = atoi("2"); // prevent optimizations
661 FILE* fp = fopen("/dev/null", "w");
662 ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
663 fclose(fp);
664}
665
Daniel Micay42281882015-04-17 11:26:36 -0400666TEST_F(DEATHTEST, readlink_fortified) {
667 char buf[1];
668 size_t ct = atoi("2"); // prevent optimizations
669 ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
670}
671
672TEST_F(DEATHTEST, readlinkat_fortified) {
673 char buf[1];
674 size_t ct = atoi("2"); // prevent optimizations
675 ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
676}
677
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700678extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
679extern "C" char* __strcat_chk(char*, const char*, size_t);
680
681TEST(TEST_NAME, strncat) {
682 char buf[10];
683 memset(buf, 'A', sizeof(buf));
684 buf[0] = 'a';
685 buf[1] = '\0';
686 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
687 ASSERT_EQ(buf, res);
688 ASSERT_EQ('a', buf[0]);
689 ASSERT_EQ('0', buf[1]);
690 ASSERT_EQ('1', buf[2]);
691 ASSERT_EQ('2', buf[3]);
692 ASSERT_EQ('3', buf[4]);
693 ASSERT_EQ('4', buf[5]);
694 ASSERT_EQ('\0', 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, strncat2) {
701 char buf[10];
702 memset(buf, 'A', sizeof(buf));
703 buf[0] = 'a';
704 buf[1] = '\0';
705 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
706 ASSERT_EQ(buf, res);
707 ASSERT_EQ('a', buf[0]);
708 ASSERT_EQ('0', buf[1]);
709 ASSERT_EQ('1', buf[2]);
710 ASSERT_EQ('2', buf[3]);
711 ASSERT_EQ('3', buf[4]);
712 ASSERT_EQ('4', buf[5]);
713 ASSERT_EQ('\0', buf[6]);
714 ASSERT_EQ('A', buf[7]);
715 ASSERT_EQ('A', buf[8]);
716 ASSERT_EQ('A', buf[9]);
717}
718
719TEST(TEST_NAME, strncat3) {
720 char buf[10];
721 memset(buf, 'A', sizeof(buf));
722 buf[0] = '\0';
723 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
724 ASSERT_EQ(buf, res);
725 ASSERT_EQ('0', buf[0]);
726 ASSERT_EQ('1', buf[1]);
727 ASSERT_EQ('2', buf[2]);
728 ASSERT_EQ('3', buf[3]);
729 ASSERT_EQ('4', buf[4]);
730 ASSERT_EQ('\0', buf[5]);
731 ASSERT_EQ('A', buf[6]);
732 ASSERT_EQ('A', buf[7]);
733 ASSERT_EQ('A', buf[8]);
734 ASSERT_EQ('A', buf[9]);
735}
736
737TEST(TEST_NAME, strncat4) {
738 char buf[10];
739 memset(buf, 'A', sizeof(buf));
740 buf[9] = '\0';
741 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
742 ASSERT_EQ(buf, res);
743 ASSERT_EQ('A', buf[0]);
744 ASSERT_EQ('A', buf[1]);
745 ASSERT_EQ('A', buf[2]);
746 ASSERT_EQ('A', buf[3]);
747 ASSERT_EQ('A', buf[4]);
748 ASSERT_EQ('A', buf[5]);
749 ASSERT_EQ('A', buf[6]);
750 ASSERT_EQ('A', buf[7]);
751 ASSERT_EQ('A', buf[8]);
752 ASSERT_EQ('\0', buf[9]);
753}
754
755TEST(TEST_NAME, strncat5) {
756 char buf[10];
757 memset(buf, 'A', sizeof(buf));
758 buf[0] = 'a';
759 buf[1] = '\0';
760 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
761 ASSERT_EQ(buf, res);
762 ASSERT_EQ('a', buf[0]);
763 ASSERT_EQ('0', buf[1]);
764 ASSERT_EQ('1', buf[2]);
765 ASSERT_EQ('2', buf[3]);
766 ASSERT_EQ('3', buf[4]);
767 ASSERT_EQ('4', buf[5]);
768 ASSERT_EQ('5', buf[6]);
769 ASSERT_EQ('6', buf[7]);
770 ASSERT_EQ('7', buf[8]);
771 ASSERT_EQ('\0', buf[9]);
772}
773
774TEST(TEST_NAME, strncat6) {
775 char buf[10];
776 memset(buf, 'A', sizeof(buf));
777 buf[0] = 'a';
778 buf[1] = '\0';
779 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
780 ASSERT_EQ(buf, res);
781 ASSERT_EQ('a', buf[0]);
782 ASSERT_EQ('0', buf[1]);
783 ASSERT_EQ('1', buf[2]);
784 ASSERT_EQ('2', buf[3]);
785 ASSERT_EQ('3', buf[4]);
786 ASSERT_EQ('4', buf[5]);
787 ASSERT_EQ('5', buf[6]);
788 ASSERT_EQ('6', buf[7]);
789 ASSERT_EQ('7', buf[8]);
790 ASSERT_EQ('\0', buf[9]);
791}
792
793
794TEST(TEST_NAME, strcat) {
795 char buf[10];
796 memset(buf, 'A', sizeof(buf));
797 buf[0] = 'a';
798 buf[1] = '\0';
799 char* res = __strcat_chk(buf, "01234", sizeof(buf));
800 ASSERT_EQ(buf, res);
801 ASSERT_EQ('a', buf[0]);
802 ASSERT_EQ('0', buf[1]);
803 ASSERT_EQ('1', buf[2]);
804 ASSERT_EQ('2', buf[3]);
805 ASSERT_EQ('3', buf[4]);
806 ASSERT_EQ('4', buf[5]);
807 ASSERT_EQ('\0', buf[6]);
808 ASSERT_EQ('A', buf[7]);
809 ASSERT_EQ('A', buf[8]);
810 ASSERT_EQ('A', buf[9]);
811}
812
813TEST(TEST_NAME, strcat2) {
814 char buf[10];
815 memset(buf, 'A', sizeof(buf));
816 buf[0] = 'a';
817 buf[1] = '\0';
818 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
819 ASSERT_EQ(buf, res);
820 ASSERT_EQ('a', buf[0]);
821 ASSERT_EQ('0', buf[1]);
822 ASSERT_EQ('1', buf[2]);
823 ASSERT_EQ('2', buf[3]);
824 ASSERT_EQ('3', buf[4]);
825 ASSERT_EQ('4', buf[5]);
826 ASSERT_EQ('5', buf[6]);
827 ASSERT_EQ('6', buf[7]);
828 ASSERT_EQ('7', buf[8]);
829 ASSERT_EQ('\0', buf[9]);
830}
Nick Kralevich93501d32013-08-28 10:47:43 -0700831
Christopher Ferris950a58e2014-04-04 14:38:18 -0700832TEST(TEST_NAME, stpncpy) {
833 char src[10];
834 char dst[10];
835 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
836 stpncpy(dst, src, sizeof(dst));
837 ASSERT_EQ('0', dst[0]);
838 ASSERT_EQ('1', dst[1]);
839 ASSERT_EQ('2', dst[2]);
840 ASSERT_EQ('3', dst[3]);
841 ASSERT_EQ('4', dst[4]);
842 ASSERT_EQ('5', dst[5]);
843 ASSERT_EQ('6', dst[6]);
844 ASSERT_EQ('7', dst[7]);
845 ASSERT_EQ('8', dst[8]);
846 ASSERT_EQ('9', dst[9]);
847}
848
849TEST(TEST_NAME, stpncpy2) {
850 char src[10];
851 char dst[15];
852 memcpy(src, "012345678\0", sizeof(src));
853 stpncpy(dst, src, sizeof(dst));
854 ASSERT_EQ('0', dst[0]);
855 ASSERT_EQ('1', dst[1]);
856 ASSERT_EQ('2', dst[2]);
857 ASSERT_EQ('3', dst[3]);
858 ASSERT_EQ('4', dst[4]);
859 ASSERT_EQ('5', dst[5]);
860 ASSERT_EQ('6', dst[6]);
861 ASSERT_EQ('7', dst[7]);
862 ASSERT_EQ('8', dst[8]);
863 ASSERT_EQ('\0', dst[9]);
864 ASSERT_EQ('\0', dst[10]);
865 ASSERT_EQ('\0', dst[11]);
866 ASSERT_EQ('\0', dst[12]);
867 ASSERT_EQ('\0', dst[13]);
868 ASSERT_EQ('\0', dst[14]);
869}
870
Nick Kralevich93501d32013-08-28 10:47:43 -0700871TEST(TEST_NAME, strncpy) {
872 char src[10];
873 char dst[10];
874 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
875 strncpy(dst, src, sizeof(dst));
876 ASSERT_EQ('0', dst[0]);
877 ASSERT_EQ('1', dst[1]);
878 ASSERT_EQ('2', dst[2]);
879 ASSERT_EQ('3', dst[3]);
880 ASSERT_EQ('4', dst[4]);
881 ASSERT_EQ('5', dst[5]);
882 ASSERT_EQ('6', dst[6]);
883 ASSERT_EQ('7', dst[7]);
884 ASSERT_EQ('8', dst[8]);
885 ASSERT_EQ('9', dst[9]);
886}
887
888TEST(TEST_NAME, strncpy2) {
889 char src[10];
890 char dst[15];
891 memcpy(src, "012345678\0", sizeof(src));
892 strncpy(dst, src, sizeof(dst));
893 ASSERT_EQ('0', dst[0]);
894 ASSERT_EQ('1', dst[1]);
895 ASSERT_EQ('2', dst[2]);
896 ASSERT_EQ('3', dst[3]);
897 ASSERT_EQ('4', dst[4]);
898 ASSERT_EQ('5', dst[5]);
899 ASSERT_EQ('6', dst[6]);
900 ASSERT_EQ('7', dst[7]);
901 ASSERT_EQ('8', dst[8]);
902 ASSERT_EQ('\0', dst[9]);
903 ASSERT_EQ('\0', dst[10]);
904 ASSERT_EQ('\0', dst[11]);
905 ASSERT_EQ('\0', dst[12]);
906 ASSERT_EQ('\0', dst[13]);
907 ASSERT_EQ('\0', dst[14]);
908}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700909
910TEST(TEST_NAME, strcat_chk_max_int_size) {
911 char buf[10];
912 memset(buf, 'A', sizeof(buf));
913 buf[0] = 'a';
914 buf[1] = '\0';
915 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
916 ASSERT_EQ(buf, res);
917 ASSERT_EQ('a', buf[0]);
918 ASSERT_EQ('0', buf[1]);
919 ASSERT_EQ('1', buf[2]);
920 ASSERT_EQ('2', buf[3]);
921 ASSERT_EQ('3', buf[4]);
922 ASSERT_EQ('4', buf[5]);
923 ASSERT_EQ('5', buf[6]);
924 ASSERT_EQ('6', buf[7]);
925 ASSERT_EQ('7', buf[8]);
926 ASSERT_EQ('\0', buf[9]);
927}
928
Christopher Ferris950a58e2014-04-04 14:38:18 -0700929extern "C" char* __stpcpy_chk(char*, const char*, size_t);
930
931TEST(TEST_NAME, stpcpy_chk_max_int_size) {
932 char buf[10];
933 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
934 ASSERT_EQ(buf + strlen("012345678"), res);
935 ASSERT_STREQ("012345678", buf);
936}
937
Christopher Ferris16e185c2013-09-10 16:56:34 -0700938extern "C" char* __strcpy_chk(char*, const char*, size_t);
939
940TEST(TEST_NAME, strcpy_chk_max_int_size) {
941 char buf[10];
942 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
943 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700944 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700945}
946
947extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
948
949TEST(TEST_NAME, memcpy_chk_max_int_size) {
950 char buf[10];
951 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
952 ASSERT_EQ((void*)buf, res);
953 ASSERT_EQ('0', buf[0]);
954 ASSERT_EQ('1', buf[1]);
955 ASSERT_EQ('2', buf[2]);
956 ASSERT_EQ('3', buf[3]);
957 ASSERT_EQ('4', buf[4]);
958 ASSERT_EQ('5', buf[5]);
959 ASSERT_EQ('6', buf[6]);
960 ASSERT_EQ('7', buf[7]);
961 ASSERT_EQ('8', buf[8]);
962 ASSERT_EQ('\0', buf[9]);
963}
Stephen Hines6e380722013-10-11 00:45:24 -0700964
965// Verify that macro expansion is done properly for sprintf/snprintf (which
966// are defined as macros in stdio.h under clang).
967#define CONTENTS "macro expansion"
968#define BUF_AND_SIZE(A) A, sizeof(A)
969#define BUF_AND_CONTENTS(A) A, CONTENTS
970#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
971TEST(TEST_NAME, s_n_printf_macro_expansion) {
972 char buf[BUFSIZ];
973 snprintf(BUF_AND_SIZE(buf), CONTENTS);
974 EXPECT_STREQ(CONTENTS, buf);
975
976 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
977 EXPECT_STREQ(CONTENTS, buf);
978
979 sprintf(BUF_AND_CONTENTS(buf));
980 EXPECT_STREQ(CONTENTS, buf);
981}
Elliott Hughes4674e382015-02-02 09:15:19 -0800982
983TEST_F(DEATHTEST, poll_fortified) {
984 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
985 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800986 // Set timeout to zero to prevent waiting in poll when fortify test fails.
987 ASSERT_FORTIFY(poll(buf, fd_count, 0));
Elliott Hughes4674e382015-02-02 09:15:19 -0800988}
989
990TEST_F(DEATHTEST, ppoll_fortified) {
991 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
992 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800993 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
994 timespec timeout;
995 timeout.tv_sec = timeout.tv_nsec = 0;
996 ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, NULL));
Elliott Hughes4674e382015-02-02 09:15:19 -0800997}