blob: 6cbc69585fb217816c92102d8ce36fa3ee071f67 [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
Elliott Hughesd036e942015-02-02 11:18:58 -080030#if __BIONIC__
31#define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
32#else
33#define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "")
34#endif
35
Yabin Cui9df70402014-11-05 18:01:01 -080036// Fortify test code needs to run multiple times, so TEST_NAME macro is used to
37// distinguish different tests. TEST_NAME is defined in compilation command.
Nick Kralevich5bcf3982013-06-28 10:34:09 -070038#define DEATHTEST_PASTER(name) name##_DeathTest
39#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
40#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
41
Yabin Cui9df70402014-11-05 18:01:01 -080042class DEATHTEST : public BionicDeathTest {};
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070043
Nick Kralevich5bcf3982013-06-28 10:34:09 -070044#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
45struct foo {
46 char empty[0];
47 char one[1];
48 char a[10];
49 char b[10];
50};
51
52#ifndef __clang__
53// This test is disabled in clang because clang doesn't properly detect
54// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070055TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070056 foo myfoo;
57 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080058 ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
Christopher Ferris950a58e2014-04-04 14:38:18 -070059}
60#endif
61
62#ifndef __clang__
63// This test is disabled in clang because clang doesn't properly detect
64// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070065TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070066 foo myfoo;
67 memset(&myfoo, 0, sizeof(myfoo));
68 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080069 ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Christopher Ferris950a58e2014-04-04 14:38:18 -070070}
71#endif
72
73#ifndef __clang__
74// This test is disabled in clang because clang doesn't properly detect
75// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070076TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070077 foo myfoo;
78 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080079 ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070080}
81#endif
82
83#ifndef __clang__
84// This test is disabled in clang because clang doesn't properly detect
85// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070086TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070087 foo myfoo;
88 memset(&myfoo, 0, sizeof(myfoo));
89 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080090 ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Nick Kralevich93501d32013-08-28 10:47:43 -070091}
92#endif
93
94#ifndef __clang__
95// This test is disabled in clang because clang doesn't properly detect
96// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070097TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070098 foo myfoo;
99 char source_buf[15];
100 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800101 ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700102}
103#endif
104
105#ifndef __clang__
Nick Kralevich884a3de2014-10-06 00:39:47 +0000106// This test is disabled in clang because clang doesn't properly detect
107// this buffer overflow. TODO: Fix clang.
108TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000109 foo myfoo;
Elliott Hughesd036e942015-02-02 11:18:58 -0800110 ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000111}
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) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800129 ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700130}
131
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700132TEST_F(DEATHTEST, vsprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800133 ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700134}
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) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800153 ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700154}
155
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700156TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800157 ASSERT_FORTIFY(vsnprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700158}
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("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800169 ASSERT_FORTIFY(stpcpy(myfoo.empty, src));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700170 free(src);
171#else // __BIONIC__
172 GTEST_LOG_(INFO) << "This test does nothing.\n";
173#endif // __BIONIC__
174}
175#endif
176
177#ifndef __clang__
178// zero sized target with "\0" source (should fail)
179// This test is disabled in clang because clang doesn't properly detect
180// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700181TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800182#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700183 foo myfoo;
184 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800185 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700186 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800187#else // __BIONIC__
188 GTEST_LOG_(INFO) << "This test does nothing.\n";
189#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700190}
191#endif
192
193#ifndef __clang__
194// zero sized target with longer source (should fail)
195// This test is disabled in clang because clang doesn't properly detect
196// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700197TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800198#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700199 foo myfoo;
200 char* src = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800201 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700202 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800203#else // __BIONIC__
204 GTEST_LOG_(INFO) << "This test does nothing.\n";
205#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700206}
207#endif
208
209#ifndef __clang__
210// one byte target with longer source (should fail)
211// This test is disabled in clang because clang doesn't properly detect
212// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700213TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800214#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700215 foo myfoo;
216 char* src = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800217 ASSERT_FORTIFY(strcpy(myfoo.one, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700218 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800219#else // __BIONIC__
220 GTEST_LOG_(INFO) << "This test does nothing.\n";
221#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700222}
223#endif
224
225#ifndef __clang__
226// This test is disabled in clang because clang doesn't properly detect
227// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700228TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800229#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700230 foo myfoo;
231 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
232 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800233 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800234#else // __BIONIC__
235 GTEST_LOG_(INFO) << "This test does nothing.\n";
236#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700237}
238#endif
239
240#ifndef __clang__
241// This test is disabled in clang because clang doesn't properly detect
242// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700243TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800244#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700245 foo myfoo;
246 memcpy(myfoo.a, "0123456789", 10);
247 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800248 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800249#else // __BIONIC__
250 GTEST_LOG_(INFO) << "This test does nothing.\n";
251#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700252}
253#endif
254
255#ifndef __clang__
256// This test is disabled in clang because clang doesn't properly detect
257// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700258TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800259#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700260 foo myfoo;
261 strcpy(myfoo.a, "01");
262 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800263 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800264#else // __BIONIC__
265 GTEST_LOG_(INFO) << "This test does nothing.\n";
266#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700267}
268#endif
269
Nick Kralevicha6cde392013-06-29 08:15:25 -0700270#ifndef __clang__
271// This test is disabled in clang because clang doesn't properly detect
272// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700273TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800274#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700275 foo myfoo;
276 strcpy(myfoo.a, "01");
277 myfoo.one[0] = '\0';
278 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800279 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800280#else // __BIONIC__
281 GTEST_LOG_(INFO) << "This test does nothing.\n";
282#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700283}
284#endif
285
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700286#ifndef __clang__
287// This test is disabled in clang because clang doesn't properly detect
288// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700289TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700290 foo myfoo;
291 size_t n = atoi("10"); // avoid compiler optimizations
292 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800293 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700294}
295#endif
296
297#ifndef __clang__
298// This test is disabled in clang because clang doesn't properly detect
299// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700300TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700301 foo myfoo;
302 myfoo.a[0] = '\0';
303 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800304 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700305}
306#endif
307
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700308TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700309 foo myfoo;
310 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
311 myfoo.b[0] = '\0';
312 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800313 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700314}
315
316#ifndef __clang__
317// This test is disabled in clang because clang doesn't properly detect
318// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700319TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700320 char src[11];
321 strcpy(src, "0123456789");
322 foo myfoo;
323 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800324 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700325}
326#endif
327
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700328TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700329 foo myfoo;
330 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
331 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800332 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700333}
334
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700335TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700336 foo myfoo;
337 strcpy(myfoo.a, "012345678");
338 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800339 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700340}
341
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700342TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700343 foo myfoo;
344 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
345 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800346 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700347}
348
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700349#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
350
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700351// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700352TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800353#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700354 char buf[10];
355 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800356 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700357 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800358#else // __BIONIC__
359 GTEST_LOG_(INFO) << "This test does nothing.\n";
360#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700361}
362
363// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700364TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800365#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700366 char buf[0];
367 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800368 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700369 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800370#else // __BIONIC__
371 GTEST_LOG_(INFO) << "This test does nothing.\n";
372#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700373}
374
375// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700376TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800377#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700378 char buf[0];
379 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800380 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700381 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800382#else // __BIONIC__
383 GTEST_LOG_(INFO) << "This test does nothing.\n";
384#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700385}
386
387// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700388TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800389#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700390 char buf[1];
391 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800392 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700393 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800394#else // __BIONIC__
395 GTEST_LOG_(INFO) << "This test does nothing.\n";
396#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700397}
398
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700399TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800400#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700401 char buf[10];
402 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800403 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800404#else // __BIONIC__
405 GTEST_LOG_(INFO) << "This test does nothing.\n";
406#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700407}
408
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700409TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800410#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700411 char buf[10];
412 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800413 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800414#else // __BIONIC__
415 GTEST_LOG_(INFO) << "This test does nothing.\n";
416#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700417}
418
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700419TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800420#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700421 char buf[10];
422 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800423 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800424#else // __BIONIC__
425 GTEST_LOG_(INFO) << "This test does nothing.\n";
426#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700427}
428
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700429TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800430#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700431 char bufa[15];
432 char bufb[10];
433 strcpy(bufa, "01234567890123");
434 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800435 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800436#else // __BIONIC__
437 GTEST_LOG_(INFO) << "This test does nothing.\n";
438#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700439}
440
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700441TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800442#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700443 char bufa[15];
444 char bufb[10];
445 bufb[0] = '\0';
446 strcpy(bufa, "01234567890123");
447 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800448 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800449#else // __BIONIC__
450 GTEST_LOG_(INFO) << "This test does nothing.\n";
451#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700452}
453
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700454TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700455 char buf[10];
456 char source_buf[15];
457 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800458 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700459}
460
Nick Kralevichb91791d2013-10-02 14:14:40 -0700461#ifndef __clang__
462// This test is disabled in clang because clang doesn't properly detect
463// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700464TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700465 char* buf = (char *) malloc(10);
466 char source_buf[11];
467 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800468 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700469 free(buf);
470}
471#endif
472
Nick Kralevich884a3de2014-10-06 00:39:47 +0000473TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000474 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800475 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000476}
477
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700478static int vsprintf_helper(const char *fmt, ...) {
479 char buf[10];
480 va_list va;
481 int result;
482
483 va_start(va, fmt);
484 result = vsprintf(buf, fmt, va); // should crash here
485 va_end(va);
486 return result;
487}
488
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700489TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800490 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700491}
492
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700493TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800494 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700495}
496
497static int vsnprintf_helper(const char *fmt, ...) {
498 char buf[10];
499 va_list va;
500 int result;
501 size_t size = atoi("11");
502
503 va_start(va, fmt);
504 result = vsnprintf(buf, size, fmt, va); // should crash here
505 va_end(va);
506 return result;
507}
508
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700509TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800510 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700511}
512
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700513TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800514 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700515}
516
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700517TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700518 char buf[10];
519 size_t n = atoi("10"); // avoid compiler optimizations
520 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800521 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700522}
523
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700524TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700525 char buf[10];
526 buf[0] = '\0';
527 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800528 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700529}
530
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700531TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700532 char src[11];
533 strcpy(src, "0123456789");
534 char buf[10];
535 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800536 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700537}
538
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700539TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700540 char buf[20];
541 strcpy(buf, "0123456789");
542 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800543 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700544}
545
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700546TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700547 char bufa[10];
548 char bufb[10];
549 strcpy(bufa, "012345678");
550 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800551 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700552}
553
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700554TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700555 char bufa[15];
556 char bufb[10];
557 strcpy(bufa, "01234567890123");
558 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800559 ASSERT_FORTIFY(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700560}
561
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700562TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700563 char dest[11];
564 char src[10];
565 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800566 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700567}
568
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700569TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700570 char bufa[15];
571 char bufb[10];
572 strcpy(bufa, "01234567890123");
573 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800574 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700575}
576
Christopher Ferris950a58e2014-04-04 14:38:18 -0700577
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700578TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700579 char dest[11];
580 char src[10];
581 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800582 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700583}
584
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700585TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700586 char bufa[15];
587 char bufb[10];
588 strcpy(bufa, "0123456789");
589 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800590 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700591}
592
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700593TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700594 char buf[10];
595 memcpy(buf, "0123456789", sizeof(buf));
596 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800597 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700598}
599
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700600TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700601 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800602 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700603}
604
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700605TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700606 size_t data_len = atoi("11"); // suppress compiler optimizations
607 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800608 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700609}
610
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700611TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700612#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700613 fd_set set;
614 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800615 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700616#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700617}
618
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700619TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700620 char buf[1];
621 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800622 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700623}
624
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700625TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700626 char buf[1];
627 size_t ct = atoi("2"); // prevent optimizations
628 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800629 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700630 close(fd);
631}
632
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700633extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
634extern "C" char* __strcat_chk(char*, const char*, size_t);
635
636TEST(TEST_NAME, strncat) {
637 char buf[10];
638 memset(buf, 'A', sizeof(buf));
639 buf[0] = 'a';
640 buf[1] = '\0';
641 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
642 ASSERT_EQ(buf, res);
643 ASSERT_EQ('a', buf[0]);
644 ASSERT_EQ('0', buf[1]);
645 ASSERT_EQ('1', buf[2]);
646 ASSERT_EQ('2', buf[3]);
647 ASSERT_EQ('3', buf[4]);
648 ASSERT_EQ('4', buf[5]);
649 ASSERT_EQ('\0', buf[6]);
650 ASSERT_EQ('A', buf[7]);
651 ASSERT_EQ('A', buf[8]);
652 ASSERT_EQ('A', buf[9]);
653}
654
655TEST(TEST_NAME, strncat2) {
656 char buf[10];
657 memset(buf, 'A', sizeof(buf));
658 buf[0] = 'a';
659 buf[1] = '\0';
660 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
661 ASSERT_EQ(buf, res);
662 ASSERT_EQ('a', buf[0]);
663 ASSERT_EQ('0', buf[1]);
664 ASSERT_EQ('1', buf[2]);
665 ASSERT_EQ('2', buf[3]);
666 ASSERT_EQ('3', buf[4]);
667 ASSERT_EQ('4', buf[5]);
668 ASSERT_EQ('\0', buf[6]);
669 ASSERT_EQ('A', buf[7]);
670 ASSERT_EQ('A', buf[8]);
671 ASSERT_EQ('A', buf[9]);
672}
673
674TEST(TEST_NAME, strncat3) {
675 char buf[10];
676 memset(buf, 'A', sizeof(buf));
677 buf[0] = '\0';
678 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
679 ASSERT_EQ(buf, res);
680 ASSERT_EQ('0', buf[0]);
681 ASSERT_EQ('1', buf[1]);
682 ASSERT_EQ('2', buf[2]);
683 ASSERT_EQ('3', buf[3]);
684 ASSERT_EQ('4', buf[4]);
685 ASSERT_EQ('\0', buf[5]);
686 ASSERT_EQ('A', buf[6]);
687 ASSERT_EQ('A', buf[7]);
688 ASSERT_EQ('A', buf[8]);
689 ASSERT_EQ('A', buf[9]);
690}
691
692TEST(TEST_NAME, strncat4) {
693 char buf[10];
694 memset(buf, 'A', sizeof(buf));
695 buf[9] = '\0';
696 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
697 ASSERT_EQ(buf, res);
698 ASSERT_EQ('A', buf[0]);
699 ASSERT_EQ('A', buf[1]);
700 ASSERT_EQ('A', buf[2]);
701 ASSERT_EQ('A', buf[3]);
702 ASSERT_EQ('A', buf[4]);
703 ASSERT_EQ('A', buf[5]);
704 ASSERT_EQ('A', buf[6]);
705 ASSERT_EQ('A', buf[7]);
706 ASSERT_EQ('A', buf[8]);
707 ASSERT_EQ('\0', buf[9]);
708}
709
710TEST(TEST_NAME, strncat5) {
711 char buf[10];
712 memset(buf, 'A', sizeof(buf));
713 buf[0] = 'a';
714 buf[1] = '\0';
715 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
716 ASSERT_EQ(buf, res);
717 ASSERT_EQ('a', buf[0]);
718 ASSERT_EQ('0', buf[1]);
719 ASSERT_EQ('1', buf[2]);
720 ASSERT_EQ('2', buf[3]);
721 ASSERT_EQ('3', buf[4]);
722 ASSERT_EQ('4', buf[5]);
723 ASSERT_EQ('5', buf[6]);
724 ASSERT_EQ('6', buf[7]);
725 ASSERT_EQ('7', buf[8]);
726 ASSERT_EQ('\0', buf[9]);
727}
728
729TEST(TEST_NAME, strncat6) {
730 char buf[10];
731 memset(buf, 'A', sizeof(buf));
732 buf[0] = 'a';
733 buf[1] = '\0';
734 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
735 ASSERT_EQ(buf, res);
736 ASSERT_EQ('a', buf[0]);
737 ASSERT_EQ('0', buf[1]);
738 ASSERT_EQ('1', buf[2]);
739 ASSERT_EQ('2', buf[3]);
740 ASSERT_EQ('3', buf[4]);
741 ASSERT_EQ('4', buf[5]);
742 ASSERT_EQ('5', buf[6]);
743 ASSERT_EQ('6', buf[7]);
744 ASSERT_EQ('7', buf[8]);
745 ASSERT_EQ('\0', buf[9]);
746}
747
748
749TEST(TEST_NAME, strcat) {
750 char buf[10];
751 memset(buf, 'A', sizeof(buf));
752 buf[0] = 'a';
753 buf[1] = '\0';
754 char* res = __strcat_chk(buf, "01234", sizeof(buf));
755 ASSERT_EQ(buf, res);
756 ASSERT_EQ('a', buf[0]);
757 ASSERT_EQ('0', buf[1]);
758 ASSERT_EQ('1', buf[2]);
759 ASSERT_EQ('2', buf[3]);
760 ASSERT_EQ('3', buf[4]);
761 ASSERT_EQ('4', buf[5]);
762 ASSERT_EQ('\0', buf[6]);
763 ASSERT_EQ('A', buf[7]);
764 ASSERT_EQ('A', buf[8]);
765 ASSERT_EQ('A', buf[9]);
766}
767
768TEST(TEST_NAME, strcat2) {
769 char buf[10];
770 memset(buf, 'A', sizeof(buf));
771 buf[0] = 'a';
772 buf[1] = '\0';
773 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
774 ASSERT_EQ(buf, res);
775 ASSERT_EQ('a', buf[0]);
776 ASSERT_EQ('0', buf[1]);
777 ASSERT_EQ('1', buf[2]);
778 ASSERT_EQ('2', buf[3]);
779 ASSERT_EQ('3', buf[4]);
780 ASSERT_EQ('4', buf[5]);
781 ASSERT_EQ('5', buf[6]);
782 ASSERT_EQ('6', buf[7]);
783 ASSERT_EQ('7', buf[8]);
784 ASSERT_EQ('\0', buf[9]);
785}
Nick Kralevich93501d32013-08-28 10:47:43 -0700786
Christopher Ferris950a58e2014-04-04 14:38:18 -0700787TEST(TEST_NAME, stpncpy) {
788 char src[10];
789 char dst[10];
790 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
791 stpncpy(dst, src, sizeof(dst));
792 ASSERT_EQ('0', dst[0]);
793 ASSERT_EQ('1', dst[1]);
794 ASSERT_EQ('2', dst[2]);
795 ASSERT_EQ('3', dst[3]);
796 ASSERT_EQ('4', dst[4]);
797 ASSERT_EQ('5', dst[5]);
798 ASSERT_EQ('6', dst[6]);
799 ASSERT_EQ('7', dst[7]);
800 ASSERT_EQ('8', dst[8]);
801 ASSERT_EQ('9', dst[9]);
802}
803
804TEST(TEST_NAME, stpncpy2) {
805 char src[10];
806 char dst[15];
807 memcpy(src, "012345678\0", sizeof(src));
808 stpncpy(dst, src, sizeof(dst));
809 ASSERT_EQ('0', dst[0]);
810 ASSERT_EQ('1', dst[1]);
811 ASSERT_EQ('2', dst[2]);
812 ASSERT_EQ('3', dst[3]);
813 ASSERT_EQ('4', dst[4]);
814 ASSERT_EQ('5', dst[5]);
815 ASSERT_EQ('6', dst[6]);
816 ASSERT_EQ('7', dst[7]);
817 ASSERT_EQ('8', dst[8]);
818 ASSERT_EQ('\0', dst[9]);
819 ASSERT_EQ('\0', dst[10]);
820 ASSERT_EQ('\0', dst[11]);
821 ASSERT_EQ('\0', dst[12]);
822 ASSERT_EQ('\0', dst[13]);
823 ASSERT_EQ('\0', dst[14]);
824}
825
Nick Kralevich93501d32013-08-28 10:47:43 -0700826TEST(TEST_NAME, strncpy) {
827 char src[10];
828 char dst[10];
829 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
830 strncpy(dst, src, sizeof(dst));
831 ASSERT_EQ('0', dst[0]);
832 ASSERT_EQ('1', dst[1]);
833 ASSERT_EQ('2', dst[2]);
834 ASSERT_EQ('3', dst[3]);
835 ASSERT_EQ('4', dst[4]);
836 ASSERT_EQ('5', dst[5]);
837 ASSERT_EQ('6', dst[6]);
838 ASSERT_EQ('7', dst[7]);
839 ASSERT_EQ('8', dst[8]);
840 ASSERT_EQ('9', dst[9]);
841}
842
843TEST(TEST_NAME, strncpy2) {
844 char src[10];
845 char dst[15];
846 memcpy(src, "012345678\0", sizeof(src));
847 strncpy(dst, src, sizeof(dst));
848 ASSERT_EQ('0', dst[0]);
849 ASSERT_EQ('1', dst[1]);
850 ASSERT_EQ('2', dst[2]);
851 ASSERT_EQ('3', dst[3]);
852 ASSERT_EQ('4', dst[4]);
853 ASSERT_EQ('5', dst[5]);
854 ASSERT_EQ('6', dst[6]);
855 ASSERT_EQ('7', dst[7]);
856 ASSERT_EQ('8', dst[8]);
857 ASSERT_EQ('\0', dst[9]);
858 ASSERT_EQ('\0', dst[10]);
859 ASSERT_EQ('\0', dst[11]);
860 ASSERT_EQ('\0', dst[12]);
861 ASSERT_EQ('\0', dst[13]);
862 ASSERT_EQ('\0', dst[14]);
863}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700864
865TEST(TEST_NAME, strcat_chk_max_int_size) {
866 char buf[10];
867 memset(buf, 'A', sizeof(buf));
868 buf[0] = 'a';
869 buf[1] = '\0';
870 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
871 ASSERT_EQ(buf, res);
872 ASSERT_EQ('a', buf[0]);
873 ASSERT_EQ('0', buf[1]);
874 ASSERT_EQ('1', buf[2]);
875 ASSERT_EQ('2', buf[3]);
876 ASSERT_EQ('3', buf[4]);
877 ASSERT_EQ('4', buf[5]);
878 ASSERT_EQ('5', buf[6]);
879 ASSERT_EQ('6', buf[7]);
880 ASSERT_EQ('7', buf[8]);
881 ASSERT_EQ('\0', buf[9]);
882}
883
Christopher Ferris950a58e2014-04-04 14:38:18 -0700884extern "C" char* __stpcpy_chk(char*, const char*, size_t);
885
886TEST(TEST_NAME, stpcpy_chk_max_int_size) {
887 char buf[10];
888 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
889 ASSERT_EQ(buf + strlen("012345678"), res);
890 ASSERT_STREQ("012345678", buf);
891}
892
Christopher Ferris16e185c2013-09-10 16:56:34 -0700893extern "C" char* __strcpy_chk(char*, const char*, size_t);
894
895TEST(TEST_NAME, strcpy_chk_max_int_size) {
896 char buf[10];
897 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
898 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700899 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700900}
901
902extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
903
904TEST(TEST_NAME, memcpy_chk_max_int_size) {
905 char buf[10];
906 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
907 ASSERT_EQ((void*)buf, res);
908 ASSERT_EQ('0', buf[0]);
909 ASSERT_EQ('1', buf[1]);
910 ASSERT_EQ('2', buf[2]);
911 ASSERT_EQ('3', buf[3]);
912 ASSERT_EQ('4', buf[4]);
913 ASSERT_EQ('5', buf[5]);
914 ASSERT_EQ('6', buf[6]);
915 ASSERT_EQ('7', buf[7]);
916 ASSERT_EQ('8', buf[8]);
917 ASSERT_EQ('\0', buf[9]);
918}
Stephen Hines6e380722013-10-11 00:45:24 -0700919
920// Verify that macro expansion is done properly for sprintf/snprintf (which
921// are defined as macros in stdio.h under clang).
922#define CONTENTS "macro expansion"
923#define BUF_AND_SIZE(A) A, sizeof(A)
924#define BUF_AND_CONTENTS(A) A, CONTENTS
925#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
926TEST(TEST_NAME, s_n_printf_macro_expansion) {
927 char buf[BUFSIZ];
928 snprintf(BUF_AND_SIZE(buf), CONTENTS);
929 EXPECT_STREQ(CONTENTS, buf);
930
931 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
932 EXPECT_STREQ(CONTENTS, buf);
933
934 sprintf(BUF_AND_CONTENTS(buf));
935 EXPECT_STREQ(CONTENTS, buf);
936}
Elliott Hughes4674e382015-02-02 09:15:19 -0800937
938TEST_F(DEATHTEST, poll_fortified) {
939 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
940 pollfd buf[1] = {{0, POLLIN, 0}};
Elliott Hughesd036e942015-02-02 11:18:58 -0800941 ASSERT_FORTIFY(poll(buf, fd_count, -1));
Elliott Hughes4674e382015-02-02 09:15:19 -0800942}
943
944TEST_F(DEATHTEST, ppoll_fortified) {
945 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
946 pollfd buf[1] = {{0, POLLIN, 0}};
Elliott Hughesd036e942015-02-02 11:18:58 -0800947 ASSERT_FORTIFY(ppoll(buf, fd_count, NULL, NULL));
Elliott Hughes4674e382015-02-02 09:15:19 -0800948}