blob: c21c9da9e501e842af24d838f4cf9d3ccf51a6d1 [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
Dan Albertf68dcbe2016-02-02 17:13:03 -080017// -Werror is on whether we like it or not, and we're intentionally doing awful
18// things in this file. GCC is dumb and doesn't have a specific error class for
19// the fortify failures (it's just -Werror), so we can't use anything more
20// constrained than disabling all the warnings in the file :( It also won't let
21// us use system_header in a .cpp file, so we have to #include this from
22// fortify_test_main.cpp.
23#pragma GCC system_header
24
Nick Kralevich5bcf3982013-06-28 10:34:09 -070025#include <gtest/gtest.h>
Yabin Cui9df70402014-11-05 18:01:01 -080026#include "BionicDeathTest.h"
Nick Kralevich5bcf3982013-06-28 10:34:09 -070027
Yabin Cui9df70402014-11-05 18:01:01 -080028#include <fcntl.h>
29#include <malloc.h>
Elliott Hughes4674e382015-02-02 09:15:19 -080030#include <poll.h>
Yabin Cui9df70402014-11-05 18:01:01 -080031#include <signal.h>
32#include <stdarg.h>
33#include <string.h>
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
Yabin Cuif4fe6932015-02-03 17:52:32 -080037#include <time.h>
Yabin Cui9df70402014-11-05 18:01:01 -080038
Elliott Hughesd036e942015-02-02 11:18:58 -080039#if __BIONIC__
40#define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "FORTIFY")
41#else
42#define ASSERT_FORTIFY(expr) ASSERT_EXIT(expr, testing::KilledBySignal(SIGABRT), "")
43#endif
44
Yabin Cui9df70402014-11-05 18:01:01 -080045// Fortify test code needs to run multiple times, so TEST_NAME macro is used to
46// distinguish different tests. TEST_NAME is defined in compilation command.
Nick Kralevich5bcf3982013-06-28 10:34:09 -070047#define DEATHTEST_PASTER(name) name##_DeathTest
48#define DEATHTEST_EVALUATOR(name) DEATHTEST_PASTER(name)
49#define DEATHTEST DEATHTEST_EVALUATOR(TEST_NAME)
50
Yabin Cui9df70402014-11-05 18:01:01 -080051class DEATHTEST : public BionicDeathTest {};
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070052
Nick Kralevich5bcf3982013-06-28 10:34:09 -070053#if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE == 2
54struct foo {
55 char empty[0];
56 char one[1];
57 char a[10];
58 char b[10];
59};
60
61#ifndef __clang__
62// This test is disabled in clang because clang doesn't properly detect
63// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070064TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070065 foo myfoo;
66 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080067 ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
Christopher Ferris950a58e2014-04-04 14:38:18 -070068}
69#endif
70
71#ifndef __clang__
72// This test is disabled in clang because clang doesn't properly detect
73// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070074TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070075 foo myfoo;
76 memset(&myfoo, 0, sizeof(myfoo));
77 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080078 ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Christopher Ferris950a58e2014-04-04 14:38:18 -070079}
80#endif
81
82#ifndef __clang__
83// This test is disabled in clang because clang doesn't properly detect
84// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070085TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070086 foo myfoo;
87 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080088 ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070089}
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, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070096 foo myfoo;
97 memset(&myfoo, 0, sizeof(myfoo));
98 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080099 ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700100}
101#endif
102
103#ifndef __clang__
104// This test is disabled in clang because clang doesn't properly detect
105// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700106TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700107 foo myfoo;
108 char source_buf[15];
109 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800110 ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700111}
112#endif
113
114#ifndef __clang__
Nick Kralevich884a3de2014-10-06 00:39:47 +0000115// This test is disabled in clang because clang doesn't properly detect
116// this buffer overflow. TODO: Fix clang.
117TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000118 foo myfoo;
Elliott Hughesd036e942015-02-02 11:18:58 -0800119 ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000120}
121#endif
122
123#ifndef __clang__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700124// These tests are disabled in clang because clang doesn't properly detect
125// this buffer overflow. TODO: Fix clang.
126static int vsprintf_helper2(const char *fmt, ...) {
127 foo myfoo;
128 va_list va;
129 int result;
130
131 va_start(va, fmt);
132 result = vsprintf(myfoo.a, fmt, va); // should crash here
133 va_end(va);
134 return result;
135}
136
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700137TEST_F(DEATHTEST, vsprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800138 ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700139}
140
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700141TEST_F(DEATHTEST, vsprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800142 ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700143}
144#endif
145
146#ifndef __clang__
147// These tests are disabled in clang because clang doesn't properly detect
148// this buffer overflow. TODO: Fix clang.
149static int vsnprintf_helper2(const char *fmt, ...) {
150 foo myfoo;
151 va_list va;
152 int result;
153 size_t size = atoi("11");
154
155 va_start(va, fmt);
156 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
157 va_end(va);
158 return result;
159}
160
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700161TEST_F(DEATHTEST, vsnprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800162 ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700163}
164
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700165TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800166 ASSERT_FORTIFY(vsnprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700167}
168#endif
169
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700170#ifndef __clang__
171// zero sized target with "\0" source (should fail)
172// This test is disabled in clang because clang doesn't properly detect
173// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700174TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700175#if defined(__BIONIC__)
Christopher Ferris950a58e2014-04-04 14:38:18 -0700176 foo myfoo;
177 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800178 ASSERT_FORTIFY(stpcpy(myfoo.empty, src));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700179 free(src);
180#else // __BIONIC__
181 GTEST_LOG_(INFO) << "This test does nothing.\n";
182#endif // __BIONIC__
183}
184#endif
185
186#ifndef __clang__
187// zero sized target with "\0" source (should fail)
188// This test is disabled in clang because clang doesn't properly detect
189// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700190TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800191#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700192 foo myfoo;
193 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800194 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700195 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800196#else // __BIONIC__
197 GTEST_LOG_(INFO) << "This test does nothing.\n";
198#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700199}
200#endif
201
202#ifndef __clang__
203// zero sized target with longer source (should fail)
204// This test is disabled in clang because clang doesn't properly detect
205// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700206TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800207#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700208 foo myfoo;
209 char* src = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800210 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700211 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800212#else // __BIONIC__
213 GTEST_LOG_(INFO) << "This test does nothing.\n";
214#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700215}
216#endif
217
218#ifndef __clang__
219// one byte target with longer source (should fail)
220// This test is disabled in clang because clang doesn't properly detect
221// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700222TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800223#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700224 foo myfoo;
225 char* src = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800226 ASSERT_FORTIFY(strcpy(myfoo.one, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700227 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800228#else // __BIONIC__
229 GTEST_LOG_(INFO) << "This test does nothing.\n";
230#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700231}
232#endif
233
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700234TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800235#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700236 foo myfoo;
237 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
238 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800239 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700240 ASSERT_FORTIFY(printf("%s", strchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800241#else // __BIONIC__
242 GTEST_LOG_(INFO) << "This test does nothing.\n";
243#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700244}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700245
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700246TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800247#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700248 foo myfoo;
249 memcpy(myfoo.a, "0123456789", 10);
250 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800251 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700252 ASSERT_FORTIFY(printf("%s", strrchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800253#else // __BIONIC__
254 GTEST_LOG_(INFO) << "This test does nothing.\n";
255#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700256}
George Burgess IVbd3d2082017-04-04 17:34:02 -0700257
258TEST_F(DEATHTEST, memchr_fortified2) {
259#if defined(__BIONIC__)
260 foo myfoo;
261 volatile int asize = sizeof(myfoo.a) + 1;
262 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
263 ASSERT_FORTIFY(printf("%s", memchr(myfoo.a, 'a', asize)));
264 ASSERT_FORTIFY(printf("%s", memchr(static_cast<const void*>(myfoo.a), 'a', asize)));
265#else // __BIONIC__
266 GTEST_LOG_(INFO) << "This test does nothing.\n";
267#endif // __BIONIC__
268}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700269
270#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, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800274#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700275 foo myfoo;
276 strcpy(myfoo.a, "01");
277 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800278 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800279#else // __BIONIC__
280 GTEST_LOG_(INFO) << "This test does nothing.\n";
281#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700282}
283#endif
284
Nick Kralevicha6cde392013-06-29 08:15:25 -0700285#ifndef __clang__
286// This test is disabled in clang because clang doesn't properly detect
287// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700288TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800289#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700290 foo myfoo;
291 strcpy(myfoo.a, "01");
292 myfoo.one[0] = '\0';
293 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800294 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800295#else // __BIONIC__
296 GTEST_LOG_(INFO) << "This test does nothing.\n";
297#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700298}
299#endif
300
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700301#ifndef __clang__
302// This test is disabled in clang because clang doesn't properly detect
303// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700304TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700305 foo myfoo;
306 size_t n = atoi("10"); // avoid compiler optimizations
307 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800308 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700309}
310#endif
311
312#ifndef __clang__
313// This test is disabled in clang because clang doesn't properly detect
314// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700315TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700316 foo myfoo;
317 myfoo.a[0] = '\0';
318 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800319 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700320}
321#endif
322
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700323TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700324 foo myfoo;
325 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
326 myfoo.b[0] = '\0';
327 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800328 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700329}
330
331#ifndef __clang__
332// This test is disabled in clang because clang doesn't properly detect
333// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700334TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700335 char src[11];
336 strcpy(src, "0123456789");
337 foo myfoo;
338 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800339 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700340}
341#endif
342
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700343TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700344 foo myfoo;
345 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
346 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800347 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700348}
349
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700350TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700351 foo myfoo;
352 strcpy(myfoo.a, "012345678");
353 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800354 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700355}
356
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700357TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700358 foo myfoo;
359 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
360 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800361 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700362}
363
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700364#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
365
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700366// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700367TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800368#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700369 char buf[10];
370 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800371 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700372 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800373#else // __BIONIC__
374 GTEST_LOG_(INFO) << "This test does nothing.\n";
375#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700376}
377
378// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700379TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800380#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700381 char buf[0];
382 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800383 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700384 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800385#else // __BIONIC__
386 GTEST_LOG_(INFO) << "This test does nothing.\n";
387#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700388}
389
390// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700391TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800392#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700393 char buf[0];
394 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800395 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700396 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800397#else // __BIONIC__
398 GTEST_LOG_(INFO) << "This test does nothing.\n";
399#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700400}
401
402// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700403TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800404#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700405 char buf[1];
406 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800407 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700408 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800409#else // __BIONIC__
410 GTEST_LOG_(INFO) << "This test does nothing.\n";
411#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700412}
413
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700414TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800415#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700416 char buf[10];
417 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800418 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800419#else // __BIONIC__
420 GTEST_LOG_(INFO) << "This test does nothing.\n";
421#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700422}
423
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700424TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800425#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700426 char buf[10];
427 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800428 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800429#else // __BIONIC__
430 GTEST_LOG_(INFO) << "This test does nothing.\n";
431#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700432}
433
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700434TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800435#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700436 char buf[10];
437 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800438 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800439#else // __BIONIC__
440 GTEST_LOG_(INFO) << "This test does nothing.\n";
441#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700442}
443
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700444TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800445#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700446 char bufa[15];
447 char bufb[10];
448 strcpy(bufa, "01234567890123");
449 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800450 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800451#else // __BIONIC__
452 GTEST_LOG_(INFO) << "This test does nothing.\n";
453#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700454}
455
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700456TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800457#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700458 char bufa[15];
459 char bufb[10];
460 bufb[0] = '\0';
461 strcpy(bufa, "01234567890123");
462 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800463 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800464#else // __BIONIC__
465 GTEST_LOG_(INFO) << "This test does nothing.\n";
466#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700467}
468
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700469TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700470 char buf[10];
471 char source_buf[15];
472 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800473 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700474}
475
Nick Kralevichb91791d2013-10-02 14:14:40 -0700476#ifndef __clang__
477// This test is disabled in clang because clang doesn't properly detect
478// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700479TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700480 char* buf = (char *) malloc(10);
481 char source_buf[11];
482 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800483 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700484 free(buf);
485}
486#endif
487
Nick Kralevich884a3de2014-10-06 00:39:47 +0000488TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000489 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800490 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000491}
492
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700493static int vsprintf_helper(const char *fmt, ...) {
494 char buf[10];
495 va_list va;
496 int result;
497
498 va_start(va, fmt);
499 result = vsprintf(buf, fmt, va); // should crash here
500 va_end(va);
501 return result;
502}
503
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700504TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800505 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700506}
507
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700508TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800509 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700510}
511
512static int vsnprintf_helper(const char *fmt, ...) {
513 char buf[10];
514 va_list va;
515 int result;
516 size_t size = atoi("11");
517
518 va_start(va, fmt);
519 result = vsnprintf(buf, size, fmt, va); // should crash here
520 va_end(va);
521 return result;
522}
523
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700524TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800525 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700526}
527
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700528TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800529 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700530}
531
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700532TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700533 char buf[10];
534 size_t n = atoi("10"); // avoid compiler optimizations
535 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800536 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700537}
538
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700539TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700540 char buf[10];
541 buf[0] = '\0';
542 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800543 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700544}
545
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700546TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700547 char src[11];
548 strcpy(src, "0123456789");
549 char buf[10];
550 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800551 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700552}
553
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700554TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700555 char buf[20];
556 strcpy(buf, "0123456789");
557 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800558 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700559}
560
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700561TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700562 char bufa[10];
563 char bufb[10];
564 strcpy(bufa, "012345678");
565 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800566 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700567}
568
Elliott Hughes62e59642016-03-01 11:22:42 -0800569TEST_F(DEATHTEST, memset_fortified) {
570 char buf[10];
571 size_t n = atoi("11");
572 ASSERT_FORTIFY(memset(buf, 0, n));
573}
574
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700575TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700576 char bufa[15];
577 char bufb[10];
578 strcpy(bufa, "01234567890123");
579 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800580 ASSERT_FORTIFY(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700581}
582
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700583TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700584 char dest[11];
585 char src[10];
586 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800587 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700588}
589
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700590TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700591 char bufa[15];
592 char bufb[10];
593 strcpy(bufa, "01234567890123");
594 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800595 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700596}
597
Christopher Ferris950a58e2014-04-04 14:38:18 -0700598
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700599TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700600 char dest[11];
601 char src[10];
602 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800603 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700604}
605
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700606TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700607 char bufa[15];
608 char bufb[10];
609 strcpy(bufa, "0123456789");
610 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800611 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700612}
613
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700614TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700615 char buf[10];
616 memcpy(buf, "0123456789", sizeof(buf));
617 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800618 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700619}
620
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700621TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700622 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800623 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700624}
625
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700626TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700627 size_t data_len = atoi("11"); // suppress compiler optimizations
628 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800629 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700630}
631
Daniel Micay95b59c52017-02-13 17:27:59 -0800632TEST_F(DEATHTEST, send_fortified) {
633 size_t data_len = atoi("11"); // suppress compiler optimizations
634 char buf[10] = {0};
635 ASSERT_FORTIFY(send(0, buf, data_len, 0));
636}
637
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700638TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700639#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700640 fd_set set;
641 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800642 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700643#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700644}
645
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700646TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700647 char buf[1];
648 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800649 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700650}
651
Daniel Micay9101b002015-05-20 15:31:26 -0400652TEST_F(DEATHTEST, getcwd_fortified) {
653 char buf[1];
654 size_t ct = atoi("2"); // prevent optimizations
655 ASSERT_FORTIFY(getcwd(buf, ct));
656}
657
Daniel Micaye7e1c872015-04-16 09:07:45 -0400658TEST_F(DEATHTEST, pread_fortified) {
659 char buf[1];
660 size_t ct = atoi("2"); // prevent optimizations
661 int fd = open("/dev/null", O_RDONLY);
662 ASSERT_FORTIFY(pread(fd, buf, ct, 0));
663 close(fd);
664}
665
666TEST_F(DEATHTEST, pread64_fortified) {
667 char buf[1];
668 size_t ct = atoi("2"); // prevent optimizations
669 int fd = open("/dev/null", O_RDONLY);
670 ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
671 close(fd);
672}
673
Daniel Micayafdd1542015-07-20 21:37:29 -0400674TEST_F(DEATHTEST, pwrite_fortified) {
675 char buf[1] = {0};
676 size_t ct = atoi("2"); // prevent optimizations
677 int fd = open("/dev/null", O_WRONLY);
678 ASSERT_FORTIFY(pwrite(fd, buf, ct, 0));
679 close(fd);
680}
681
682TEST_F(DEATHTEST, pwrite64_fortified) {
683 char buf[1] = {0};
684 size_t ct = atoi("2"); // prevent optimizations
685 int fd = open("/dev/null", O_WRONLY);
686 ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0));
687 close(fd);
688}
689
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700690TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700691 char buf[1];
692 size_t ct = atoi("2"); // prevent optimizations
693 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800694 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700695 close(fd);
696}
697
Daniel Micayafdd1542015-07-20 21:37:29 -0400698TEST_F(DEATHTEST, write_fortified) {
699 char buf[1] = {0};
700 size_t ct = atoi("2"); // prevent optimizations
701 int fd = open("/dev/null", O_WRONLY);
702 ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
703 close(fd);
704}
705
Daniel Micayfed26592015-07-18 13:55:51 -0400706TEST_F(DEATHTEST, fread_fortified) {
707 char buf[1];
708 size_t ct = atoi("2"); // prevent optimizations
709 FILE* fp = fopen("/dev/null", "r");
710 ASSERT_FORTIFY(fread(buf, 1, ct, fp));
711 fclose(fp);
712}
713
714TEST_F(DEATHTEST, fwrite_fortified) {
715 char buf[1] = {0};
716 size_t ct = atoi("2"); // prevent optimizations
717 FILE* fp = fopen("/dev/null", "w");
718 ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
719 fclose(fp);
720}
721
Daniel Micay42281882015-04-17 11:26:36 -0400722TEST_F(DEATHTEST, readlink_fortified) {
723 char buf[1];
724 size_t ct = atoi("2"); // prevent optimizations
725 ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
726}
727
728TEST_F(DEATHTEST, readlinkat_fortified) {
729 char buf[1];
730 size_t ct = atoi("2"); // prevent optimizations
731 ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
732}
733
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700734extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
735extern "C" char* __strcat_chk(char*, const char*, size_t);
736
737TEST(TEST_NAME, strncat) {
738 char buf[10];
739 memset(buf, 'A', sizeof(buf));
740 buf[0] = 'a';
741 buf[1] = '\0';
742 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, 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('\0', buf[6]);
751 ASSERT_EQ('A', buf[7]);
752 ASSERT_EQ('A', buf[8]);
753 ASSERT_EQ('A', buf[9]);
754}
755
756TEST(TEST_NAME, strncat2) {
757 char buf[10];
758 memset(buf, 'A', sizeof(buf));
759 buf[0] = 'a';
760 buf[1] = '\0';
761 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
762 ASSERT_EQ(buf, res);
763 ASSERT_EQ('a', buf[0]);
764 ASSERT_EQ('0', buf[1]);
765 ASSERT_EQ('1', buf[2]);
766 ASSERT_EQ('2', buf[3]);
767 ASSERT_EQ('3', buf[4]);
768 ASSERT_EQ('4', buf[5]);
769 ASSERT_EQ('\0', buf[6]);
770 ASSERT_EQ('A', buf[7]);
771 ASSERT_EQ('A', buf[8]);
772 ASSERT_EQ('A', buf[9]);
773}
774
775TEST(TEST_NAME, strncat3) {
776 char buf[10];
777 memset(buf, 'A', sizeof(buf));
778 buf[0] = '\0';
779 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
780 ASSERT_EQ(buf, res);
781 ASSERT_EQ('0', buf[0]);
782 ASSERT_EQ('1', buf[1]);
783 ASSERT_EQ('2', buf[2]);
784 ASSERT_EQ('3', buf[3]);
785 ASSERT_EQ('4', buf[4]);
786 ASSERT_EQ('\0', buf[5]);
787 ASSERT_EQ('A', buf[6]);
788 ASSERT_EQ('A', buf[7]);
789 ASSERT_EQ('A', buf[8]);
790 ASSERT_EQ('A', buf[9]);
791}
792
793TEST(TEST_NAME, strncat4) {
794 char buf[10];
795 memset(buf, 'A', sizeof(buf));
796 buf[9] = '\0';
797 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
798 ASSERT_EQ(buf, res);
799 ASSERT_EQ('A', buf[0]);
800 ASSERT_EQ('A', buf[1]);
801 ASSERT_EQ('A', buf[2]);
802 ASSERT_EQ('A', buf[3]);
803 ASSERT_EQ('A', buf[4]);
804 ASSERT_EQ('A', buf[5]);
805 ASSERT_EQ('A', buf[6]);
806 ASSERT_EQ('A', buf[7]);
807 ASSERT_EQ('A', buf[8]);
808 ASSERT_EQ('\0', buf[9]);
809}
810
811TEST(TEST_NAME, strncat5) {
812 char buf[10];
813 memset(buf, 'A', sizeof(buf));
814 buf[0] = 'a';
815 buf[1] = '\0';
816 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
817 ASSERT_EQ(buf, res);
818 ASSERT_EQ('a', buf[0]);
819 ASSERT_EQ('0', buf[1]);
820 ASSERT_EQ('1', buf[2]);
821 ASSERT_EQ('2', buf[3]);
822 ASSERT_EQ('3', buf[4]);
823 ASSERT_EQ('4', buf[5]);
824 ASSERT_EQ('5', buf[6]);
825 ASSERT_EQ('6', buf[7]);
826 ASSERT_EQ('7', buf[8]);
827 ASSERT_EQ('\0', buf[9]);
828}
829
830TEST(TEST_NAME, strncat6) {
831 char buf[10];
832 memset(buf, 'A', sizeof(buf));
833 buf[0] = 'a';
834 buf[1] = '\0';
835 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
836 ASSERT_EQ(buf, res);
837 ASSERT_EQ('a', buf[0]);
838 ASSERT_EQ('0', buf[1]);
839 ASSERT_EQ('1', buf[2]);
840 ASSERT_EQ('2', buf[3]);
841 ASSERT_EQ('3', buf[4]);
842 ASSERT_EQ('4', buf[5]);
843 ASSERT_EQ('5', buf[6]);
844 ASSERT_EQ('6', buf[7]);
845 ASSERT_EQ('7', buf[8]);
846 ASSERT_EQ('\0', buf[9]);
847}
848
849
850TEST(TEST_NAME, strcat) {
851 char buf[10];
852 memset(buf, 'A', sizeof(buf));
853 buf[0] = 'a';
854 buf[1] = '\0';
855 char* res = __strcat_chk(buf, "01234", sizeof(buf));
856 ASSERT_EQ(buf, res);
857 ASSERT_EQ('a', buf[0]);
858 ASSERT_EQ('0', buf[1]);
859 ASSERT_EQ('1', buf[2]);
860 ASSERT_EQ('2', buf[3]);
861 ASSERT_EQ('3', buf[4]);
862 ASSERT_EQ('4', buf[5]);
863 ASSERT_EQ('\0', buf[6]);
864 ASSERT_EQ('A', buf[7]);
865 ASSERT_EQ('A', buf[8]);
866 ASSERT_EQ('A', buf[9]);
867}
868
869TEST(TEST_NAME, strcat2) {
870 char buf[10];
871 memset(buf, 'A', sizeof(buf));
872 buf[0] = 'a';
873 buf[1] = '\0';
874 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
875 ASSERT_EQ(buf, res);
876 ASSERT_EQ('a', buf[0]);
877 ASSERT_EQ('0', buf[1]);
878 ASSERT_EQ('1', buf[2]);
879 ASSERT_EQ('2', buf[3]);
880 ASSERT_EQ('3', buf[4]);
881 ASSERT_EQ('4', buf[5]);
882 ASSERT_EQ('5', buf[6]);
883 ASSERT_EQ('6', buf[7]);
884 ASSERT_EQ('7', buf[8]);
885 ASSERT_EQ('\0', buf[9]);
886}
Nick Kralevich93501d32013-08-28 10:47:43 -0700887
Christopher Ferris950a58e2014-04-04 14:38:18 -0700888TEST(TEST_NAME, stpncpy) {
889 char src[10];
890 char dst[10];
891 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
892 stpncpy(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('9', dst[9]);
903}
904
905TEST(TEST_NAME, stpncpy2) {
906 char src[10];
907 char dst[15];
908 memcpy(src, "012345678\0", sizeof(src));
909 stpncpy(dst, src, sizeof(dst));
910 ASSERT_EQ('0', dst[0]);
911 ASSERT_EQ('1', dst[1]);
912 ASSERT_EQ('2', dst[2]);
913 ASSERT_EQ('3', dst[3]);
914 ASSERT_EQ('4', dst[4]);
915 ASSERT_EQ('5', dst[5]);
916 ASSERT_EQ('6', dst[6]);
917 ASSERT_EQ('7', dst[7]);
918 ASSERT_EQ('8', dst[8]);
919 ASSERT_EQ('\0', dst[9]);
920 ASSERT_EQ('\0', dst[10]);
921 ASSERT_EQ('\0', dst[11]);
922 ASSERT_EQ('\0', dst[12]);
923 ASSERT_EQ('\0', dst[13]);
924 ASSERT_EQ('\0', dst[14]);
925}
926
Nick Kralevich93501d32013-08-28 10:47:43 -0700927TEST(TEST_NAME, strncpy) {
928 char src[10];
929 char dst[10];
930 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
931 strncpy(dst, src, sizeof(dst));
932 ASSERT_EQ('0', dst[0]);
933 ASSERT_EQ('1', dst[1]);
934 ASSERT_EQ('2', dst[2]);
935 ASSERT_EQ('3', dst[3]);
936 ASSERT_EQ('4', dst[4]);
937 ASSERT_EQ('5', dst[5]);
938 ASSERT_EQ('6', dst[6]);
939 ASSERT_EQ('7', dst[7]);
940 ASSERT_EQ('8', dst[8]);
941 ASSERT_EQ('9', dst[9]);
942}
943
944TEST(TEST_NAME, strncpy2) {
945 char src[10];
946 char dst[15];
947 memcpy(src, "012345678\0", sizeof(src));
948 strncpy(dst, src, sizeof(dst));
949 ASSERT_EQ('0', dst[0]);
950 ASSERT_EQ('1', dst[1]);
951 ASSERT_EQ('2', dst[2]);
952 ASSERT_EQ('3', dst[3]);
953 ASSERT_EQ('4', dst[4]);
954 ASSERT_EQ('5', dst[5]);
955 ASSERT_EQ('6', dst[6]);
956 ASSERT_EQ('7', dst[7]);
957 ASSERT_EQ('8', dst[8]);
958 ASSERT_EQ('\0', dst[9]);
959 ASSERT_EQ('\0', dst[10]);
960 ASSERT_EQ('\0', dst[11]);
961 ASSERT_EQ('\0', dst[12]);
962 ASSERT_EQ('\0', dst[13]);
963 ASSERT_EQ('\0', dst[14]);
964}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700965
966TEST(TEST_NAME, strcat_chk_max_int_size) {
967 char buf[10];
968 memset(buf, 'A', sizeof(buf));
969 buf[0] = 'a';
970 buf[1] = '\0';
971 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
972 ASSERT_EQ(buf, res);
973 ASSERT_EQ('a', buf[0]);
974 ASSERT_EQ('0', buf[1]);
975 ASSERT_EQ('1', buf[2]);
976 ASSERT_EQ('2', buf[3]);
977 ASSERT_EQ('3', buf[4]);
978 ASSERT_EQ('4', buf[5]);
979 ASSERT_EQ('5', buf[6]);
980 ASSERT_EQ('6', buf[7]);
981 ASSERT_EQ('7', buf[8]);
982 ASSERT_EQ('\0', buf[9]);
983}
984
Christopher Ferris950a58e2014-04-04 14:38:18 -0700985extern "C" char* __stpcpy_chk(char*, const char*, size_t);
986
987TEST(TEST_NAME, stpcpy_chk_max_int_size) {
988 char buf[10];
989 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
990 ASSERT_EQ(buf + strlen("012345678"), res);
991 ASSERT_STREQ("012345678", buf);
992}
993
Christopher Ferris16e185c2013-09-10 16:56:34 -0700994extern "C" char* __strcpy_chk(char*, const char*, size_t);
995
996TEST(TEST_NAME, strcpy_chk_max_int_size) {
997 char buf[10];
998 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
999 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -07001000 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -07001001}
1002
1003extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
1004
1005TEST(TEST_NAME, memcpy_chk_max_int_size) {
1006 char buf[10];
1007 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
1008 ASSERT_EQ((void*)buf, res);
1009 ASSERT_EQ('0', buf[0]);
1010 ASSERT_EQ('1', buf[1]);
1011 ASSERT_EQ('2', buf[2]);
1012 ASSERT_EQ('3', buf[3]);
1013 ASSERT_EQ('4', buf[4]);
1014 ASSERT_EQ('5', buf[5]);
1015 ASSERT_EQ('6', buf[6]);
1016 ASSERT_EQ('7', buf[7]);
1017 ASSERT_EQ('8', buf[8]);
1018 ASSERT_EQ('\0', buf[9]);
1019}
Stephen Hines6e380722013-10-11 00:45:24 -07001020
1021// Verify that macro expansion is done properly for sprintf/snprintf (which
1022// are defined as macros in stdio.h under clang).
1023#define CONTENTS "macro expansion"
1024#define BUF_AND_SIZE(A) A, sizeof(A)
1025#define BUF_AND_CONTENTS(A) A, CONTENTS
1026#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
1027TEST(TEST_NAME, s_n_printf_macro_expansion) {
1028 char buf[BUFSIZ];
1029 snprintf(BUF_AND_SIZE(buf), CONTENTS);
1030 EXPECT_STREQ(CONTENTS, buf);
1031
1032 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
1033 EXPECT_STREQ(CONTENTS, buf);
1034
1035 sprintf(BUF_AND_CONTENTS(buf));
1036 EXPECT_STREQ(CONTENTS, buf);
1037}
Elliott Hughes4674e382015-02-02 09:15:19 -08001038
1039TEST_F(DEATHTEST, poll_fortified) {
1040 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1041 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -08001042 // Set timeout to zero to prevent waiting in poll when fortify test fails.
1043 ASSERT_FORTIFY(poll(buf, fd_count, 0));
Elliott Hughes4674e382015-02-02 09:15:19 -08001044}
1045
1046TEST_F(DEATHTEST, ppoll_fortified) {
1047 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1048 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -08001049 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
1050 timespec timeout;
1051 timeout.tv_sec = timeout.tv_nsec = 0;
1052 ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, NULL));
Elliott Hughes4674e382015-02-02 09:15:19 -08001053}