blob: 86b282cfead8e6545d16d7749272fc03adf53745 [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
234#ifndef __clang__
235// This test is disabled in clang because clang doesn't properly detect
236// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700237TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800238#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700239 foo myfoo;
240 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
241 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800242 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800243#else // __BIONIC__
244 GTEST_LOG_(INFO) << "This test does nothing.\n";
245#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700246}
247#endif
248
249#ifndef __clang__
250// This test is disabled in clang because clang doesn't properly detect
251// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700252TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800253#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700254 foo myfoo;
255 memcpy(myfoo.a, "0123456789", 10);
256 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800257 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800258#else // __BIONIC__
259 GTEST_LOG_(INFO) << "This test does nothing.\n";
260#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700261}
262#endif
263
264#ifndef __clang__
265// This test is disabled in clang because clang doesn't properly detect
266// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700267TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800268#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700269 foo myfoo;
270 strcpy(myfoo.a, "01");
271 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800272 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800273#else // __BIONIC__
274 GTEST_LOG_(INFO) << "This test does nothing.\n";
275#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700276}
277#endif
278
Nick Kralevicha6cde392013-06-29 08:15:25 -0700279#ifndef __clang__
280// This test is disabled in clang because clang doesn't properly detect
281// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700282TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800283#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700284 foo myfoo;
285 strcpy(myfoo.a, "01");
286 myfoo.one[0] = '\0';
287 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800288 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800289#else // __BIONIC__
290 GTEST_LOG_(INFO) << "This test does nothing.\n";
291#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700292}
293#endif
294
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700295#ifndef __clang__
296// This test is disabled in clang because clang doesn't properly detect
297// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700298TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700299 foo myfoo;
300 size_t n = atoi("10"); // avoid compiler optimizations
301 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800302 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700303}
304#endif
305
306#ifndef __clang__
307// This test is disabled in clang because clang doesn't properly detect
308// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700309TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700310 foo myfoo;
311 myfoo.a[0] = '\0';
312 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800313 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700314}
315#endif
316
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700317TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700318 foo myfoo;
319 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
320 myfoo.b[0] = '\0';
321 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800322 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700323}
324
325#ifndef __clang__
326// This test is disabled in clang because clang doesn't properly detect
327// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700328TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700329 char src[11];
330 strcpy(src, "0123456789");
331 foo myfoo;
332 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800333 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700334}
335#endif
336
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700337TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700338 foo myfoo;
339 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
340 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800341 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700342}
343
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700344TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700345 foo myfoo;
346 strcpy(myfoo.a, "012345678");
347 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800348 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700349}
350
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700351TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700352 foo myfoo;
353 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
354 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800355 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700356}
357
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700358#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
359
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700360// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700361TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800362#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700363 char buf[10];
364 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800365 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700366 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800367#else // __BIONIC__
368 GTEST_LOG_(INFO) << "This test does nothing.\n";
369#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700370}
371
372// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700373TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800374#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700375 char buf[0];
376 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800377 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700378 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800379#else // __BIONIC__
380 GTEST_LOG_(INFO) << "This test does nothing.\n";
381#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700382}
383
384// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700385TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800386#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700387 char buf[0];
388 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800389 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700390 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800391#else // __BIONIC__
392 GTEST_LOG_(INFO) << "This test does nothing.\n";
393#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700394}
395
396// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700397TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800398#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700399 char buf[1];
400 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800401 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700402 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800403#else // __BIONIC__
404 GTEST_LOG_(INFO) << "This test does nothing.\n";
405#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700406}
407
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700408TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800409#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700410 char buf[10];
411 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800412 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800413#else // __BIONIC__
414 GTEST_LOG_(INFO) << "This test does nothing.\n";
415#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700416}
417
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700418TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800419#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700420 char buf[10];
421 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800422 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800423#else // __BIONIC__
424 GTEST_LOG_(INFO) << "This test does nothing.\n";
425#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700426}
427
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700428TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800429#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700430 char buf[10];
431 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800432 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800433#else // __BIONIC__
434 GTEST_LOG_(INFO) << "This test does nothing.\n";
435#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700436}
437
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700438TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800439#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700440 char bufa[15];
441 char bufb[10];
442 strcpy(bufa, "01234567890123");
443 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800444 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800445#else // __BIONIC__
446 GTEST_LOG_(INFO) << "This test does nothing.\n";
447#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700448}
449
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700450TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800451#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700452 char bufa[15];
453 char bufb[10];
454 bufb[0] = '\0';
455 strcpy(bufa, "01234567890123");
456 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800457 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800458#else // __BIONIC__
459 GTEST_LOG_(INFO) << "This test does nothing.\n";
460#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700461}
462
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700463TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700464 char buf[10];
465 char source_buf[15];
466 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800467 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700468}
469
Nick Kralevichb91791d2013-10-02 14:14:40 -0700470#ifndef __clang__
471// This test is disabled in clang because clang doesn't properly detect
472// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700473TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700474 char* buf = (char *) malloc(10);
475 char source_buf[11];
476 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800477 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700478 free(buf);
479}
480#endif
481
Nick Kralevich884a3de2014-10-06 00:39:47 +0000482TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000483 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800484 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000485}
486
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700487static int vsprintf_helper(const char *fmt, ...) {
488 char buf[10];
489 va_list va;
490 int result;
491
492 va_start(va, fmt);
493 result = vsprintf(buf, fmt, va); // should crash here
494 va_end(va);
495 return result;
496}
497
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700498TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800499 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700500}
501
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700502TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800503 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700504}
505
506static int vsnprintf_helper(const char *fmt, ...) {
507 char buf[10];
508 va_list va;
509 int result;
510 size_t size = atoi("11");
511
512 va_start(va, fmt);
513 result = vsnprintf(buf, size, fmt, va); // should crash here
514 va_end(va);
515 return result;
516}
517
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700518TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800519 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700520}
521
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700522TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800523 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700524}
525
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700526TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700527 char buf[10];
528 size_t n = atoi("10"); // avoid compiler optimizations
529 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800530 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700531}
532
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700533TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700534 char buf[10];
535 buf[0] = '\0';
536 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800537 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700538}
539
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700540TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700541 char src[11];
542 strcpy(src, "0123456789");
543 char buf[10];
544 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800545 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700546}
547
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700548TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700549 char buf[20];
550 strcpy(buf, "0123456789");
551 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800552 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700553}
554
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700555TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700556 char bufa[10];
557 char bufb[10];
558 strcpy(bufa, "012345678");
559 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800560 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700561}
562
Elliott Hughes62e59642016-03-01 11:22:42 -0800563TEST_F(DEATHTEST, memset_fortified) {
564 char buf[10];
565 size_t n = atoi("11");
566 ASSERT_FORTIFY(memset(buf, 0, n));
567}
568
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700569TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -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(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700575}
576
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700577TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700578 char dest[11];
579 char src[10];
580 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800581 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700582}
583
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700584TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700585 char bufa[15];
586 char bufb[10];
587 strcpy(bufa, "01234567890123");
588 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800589 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700590}
591
Christopher Ferris950a58e2014-04-04 14:38:18 -0700592
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700593TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700594 char dest[11];
595 char src[10];
596 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800597 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700598}
599
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700600TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700601 char bufa[15];
602 char bufb[10];
603 strcpy(bufa, "0123456789");
604 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800605 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700606}
607
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700608TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700609 char buf[10];
610 memcpy(buf, "0123456789", sizeof(buf));
611 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800612 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700613}
614
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700615TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700616 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800617 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700618}
619
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700620TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700621 size_t data_len = atoi("11"); // suppress compiler optimizations
622 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800623 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700624}
625
Daniel Micay95b59c52017-02-13 17:27:59 -0800626TEST_F(DEATHTEST, send_fortified) {
627 size_t data_len = atoi("11"); // suppress compiler optimizations
628 char buf[10] = {0};
629 ASSERT_FORTIFY(send(0, buf, data_len, 0));
630}
631
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700632TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700633#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700634 fd_set set;
635 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800636 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700637#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700638}
639
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700640TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700641 char buf[1];
642 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800643 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700644}
645
Daniel Micay9101b002015-05-20 15:31:26 -0400646TEST_F(DEATHTEST, getcwd_fortified) {
647 char buf[1];
648 size_t ct = atoi("2"); // prevent optimizations
649 ASSERT_FORTIFY(getcwd(buf, ct));
650}
651
Daniel Micaye7e1c872015-04-16 09:07:45 -0400652TEST_F(DEATHTEST, pread_fortified) {
653 char buf[1];
654 size_t ct = atoi("2"); // prevent optimizations
655 int fd = open("/dev/null", O_RDONLY);
656 ASSERT_FORTIFY(pread(fd, buf, ct, 0));
657 close(fd);
658}
659
660TEST_F(DEATHTEST, pread64_fortified) {
661 char buf[1];
662 size_t ct = atoi("2"); // prevent optimizations
663 int fd = open("/dev/null", O_RDONLY);
664 ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
665 close(fd);
666}
667
Daniel Micayafdd1542015-07-20 21:37:29 -0400668TEST_F(DEATHTEST, pwrite_fortified) {
669 char buf[1] = {0};
670 size_t ct = atoi("2"); // prevent optimizations
671 int fd = open("/dev/null", O_WRONLY);
672 ASSERT_FORTIFY(pwrite(fd, buf, ct, 0));
673 close(fd);
674}
675
676TEST_F(DEATHTEST, pwrite64_fortified) {
677 char buf[1] = {0};
678 size_t ct = atoi("2"); // prevent optimizations
679 int fd = open("/dev/null", O_WRONLY);
680 ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0));
681 close(fd);
682}
683
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700684TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700685 char buf[1];
686 size_t ct = atoi("2"); // prevent optimizations
687 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800688 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700689 close(fd);
690}
691
Daniel Micayafdd1542015-07-20 21:37:29 -0400692TEST_F(DEATHTEST, write_fortified) {
693 char buf[1] = {0};
694 size_t ct = atoi("2"); // prevent optimizations
695 int fd = open("/dev/null", O_WRONLY);
696 ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
697 close(fd);
698}
699
Daniel Micayfed26592015-07-18 13:55:51 -0400700TEST_F(DEATHTEST, fread_fortified) {
701 char buf[1];
702 size_t ct = atoi("2"); // prevent optimizations
703 FILE* fp = fopen("/dev/null", "r");
704 ASSERT_FORTIFY(fread(buf, 1, ct, fp));
705 fclose(fp);
706}
707
708TEST_F(DEATHTEST, fwrite_fortified) {
709 char buf[1] = {0};
710 size_t ct = atoi("2"); // prevent optimizations
711 FILE* fp = fopen("/dev/null", "w");
712 ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
713 fclose(fp);
714}
715
Daniel Micay42281882015-04-17 11:26:36 -0400716TEST_F(DEATHTEST, readlink_fortified) {
717 char buf[1];
718 size_t ct = atoi("2"); // prevent optimizations
719 ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
720}
721
722TEST_F(DEATHTEST, readlinkat_fortified) {
723 char buf[1];
724 size_t ct = atoi("2"); // prevent optimizations
725 ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
726}
727
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700728extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
729extern "C" char* __strcat_chk(char*, const char*, size_t);
730
731TEST(TEST_NAME, strncat) {
732 char buf[10];
733 memset(buf, 'A', sizeof(buf));
734 buf[0] = 'a';
735 buf[1] = '\0';
736 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
737 ASSERT_EQ(buf, res);
738 ASSERT_EQ('a', buf[0]);
739 ASSERT_EQ('0', buf[1]);
740 ASSERT_EQ('1', buf[2]);
741 ASSERT_EQ('2', buf[3]);
742 ASSERT_EQ('3', buf[4]);
743 ASSERT_EQ('4', buf[5]);
744 ASSERT_EQ('\0', buf[6]);
745 ASSERT_EQ('A', buf[7]);
746 ASSERT_EQ('A', buf[8]);
747 ASSERT_EQ('A', buf[9]);
748}
749
750TEST(TEST_NAME, strncat2) {
751 char buf[10];
752 memset(buf, 'A', sizeof(buf));
753 buf[0] = 'a';
754 buf[1] = '\0';
755 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
756 ASSERT_EQ(buf, res);
757 ASSERT_EQ('a', buf[0]);
758 ASSERT_EQ('0', buf[1]);
759 ASSERT_EQ('1', buf[2]);
760 ASSERT_EQ('2', buf[3]);
761 ASSERT_EQ('3', buf[4]);
762 ASSERT_EQ('4', buf[5]);
763 ASSERT_EQ('\0', buf[6]);
764 ASSERT_EQ('A', buf[7]);
765 ASSERT_EQ('A', buf[8]);
766 ASSERT_EQ('A', buf[9]);
767}
768
769TEST(TEST_NAME, strncat3) {
770 char buf[10];
771 memset(buf, 'A', sizeof(buf));
772 buf[0] = '\0';
773 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
774 ASSERT_EQ(buf, res);
775 ASSERT_EQ('0', buf[0]);
776 ASSERT_EQ('1', buf[1]);
777 ASSERT_EQ('2', buf[2]);
778 ASSERT_EQ('3', buf[3]);
779 ASSERT_EQ('4', buf[4]);
780 ASSERT_EQ('\0', buf[5]);
781 ASSERT_EQ('A', buf[6]);
782 ASSERT_EQ('A', buf[7]);
783 ASSERT_EQ('A', buf[8]);
784 ASSERT_EQ('A', buf[9]);
785}
786
787TEST(TEST_NAME, strncat4) {
788 char buf[10];
789 memset(buf, 'A', sizeof(buf));
790 buf[9] = '\0';
791 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
792 ASSERT_EQ(buf, res);
793 ASSERT_EQ('A', buf[0]);
794 ASSERT_EQ('A', buf[1]);
795 ASSERT_EQ('A', buf[2]);
796 ASSERT_EQ('A', buf[3]);
797 ASSERT_EQ('A', buf[4]);
798 ASSERT_EQ('A', buf[5]);
799 ASSERT_EQ('A', buf[6]);
800 ASSERT_EQ('A', buf[7]);
801 ASSERT_EQ('A', buf[8]);
802 ASSERT_EQ('\0', buf[9]);
803}
804
805TEST(TEST_NAME, strncat5) {
806 char buf[10];
807 memset(buf, 'A', sizeof(buf));
808 buf[0] = 'a';
809 buf[1] = '\0';
810 char* res = __strncat_chk(buf, "01234567", 8, sizeof(buf));
811 ASSERT_EQ(buf, res);
812 ASSERT_EQ('a', buf[0]);
813 ASSERT_EQ('0', buf[1]);
814 ASSERT_EQ('1', buf[2]);
815 ASSERT_EQ('2', buf[3]);
816 ASSERT_EQ('3', buf[4]);
817 ASSERT_EQ('4', buf[5]);
818 ASSERT_EQ('5', buf[6]);
819 ASSERT_EQ('6', buf[7]);
820 ASSERT_EQ('7', buf[8]);
821 ASSERT_EQ('\0', buf[9]);
822}
823
824TEST(TEST_NAME, strncat6) {
825 char buf[10];
826 memset(buf, 'A', sizeof(buf));
827 buf[0] = 'a';
828 buf[1] = '\0';
829 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
830 ASSERT_EQ(buf, res);
831 ASSERT_EQ('a', buf[0]);
832 ASSERT_EQ('0', buf[1]);
833 ASSERT_EQ('1', buf[2]);
834 ASSERT_EQ('2', buf[3]);
835 ASSERT_EQ('3', buf[4]);
836 ASSERT_EQ('4', buf[5]);
837 ASSERT_EQ('5', buf[6]);
838 ASSERT_EQ('6', buf[7]);
839 ASSERT_EQ('7', buf[8]);
840 ASSERT_EQ('\0', buf[9]);
841}
842
843
844TEST(TEST_NAME, strcat) {
845 char buf[10];
846 memset(buf, 'A', sizeof(buf));
847 buf[0] = 'a';
848 buf[1] = '\0';
849 char* res = __strcat_chk(buf, "01234", sizeof(buf));
850 ASSERT_EQ(buf, res);
851 ASSERT_EQ('a', buf[0]);
852 ASSERT_EQ('0', buf[1]);
853 ASSERT_EQ('1', buf[2]);
854 ASSERT_EQ('2', buf[3]);
855 ASSERT_EQ('3', buf[4]);
856 ASSERT_EQ('4', buf[5]);
857 ASSERT_EQ('\0', buf[6]);
858 ASSERT_EQ('A', buf[7]);
859 ASSERT_EQ('A', buf[8]);
860 ASSERT_EQ('A', buf[9]);
861}
862
863TEST(TEST_NAME, strcat2) {
864 char buf[10];
865 memset(buf, 'A', sizeof(buf));
866 buf[0] = 'a';
867 buf[1] = '\0';
868 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
869 ASSERT_EQ(buf, res);
870 ASSERT_EQ('a', buf[0]);
871 ASSERT_EQ('0', buf[1]);
872 ASSERT_EQ('1', buf[2]);
873 ASSERT_EQ('2', buf[3]);
874 ASSERT_EQ('3', buf[4]);
875 ASSERT_EQ('4', buf[5]);
876 ASSERT_EQ('5', buf[6]);
877 ASSERT_EQ('6', buf[7]);
878 ASSERT_EQ('7', buf[8]);
879 ASSERT_EQ('\0', buf[9]);
880}
Nick Kralevich93501d32013-08-28 10:47:43 -0700881
Christopher Ferris950a58e2014-04-04 14:38:18 -0700882TEST(TEST_NAME, stpncpy) {
883 char src[10];
884 char dst[10];
885 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
886 stpncpy(dst, src, sizeof(dst));
887 ASSERT_EQ('0', dst[0]);
888 ASSERT_EQ('1', dst[1]);
889 ASSERT_EQ('2', dst[2]);
890 ASSERT_EQ('3', dst[3]);
891 ASSERT_EQ('4', dst[4]);
892 ASSERT_EQ('5', dst[5]);
893 ASSERT_EQ('6', dst[6]);
894 ASSERT_EQ('7', dst[7]);
895 ASSERT_EQ('8', dst[8]);
896 ASSERT_EQ('9', dst[9]);
897}
898
899TEST(TEST_NAME, stpncpy2) {
900 char src[10];
901 char dst[15];
902 memcpy(src, "012345678\0", sizeof(src));
903 stpncpy(dst, src, sizeof(dst));
904 ASSERT_EQ('0', dst[0]);
905 ASSERT_EQ('1', dst[1]);
906 ASSERT_EQ('2', dst[2]);
907 ASSERT_EQ('3', dst[3]);
908 ASSERT_EQ('4', dst[4]);
909 ASSERT_EQ('5', dst[5]);
910 ASSERT_EQ('6', dst[6]);
911 ASSERT_EQ('7', dst[7]);
912 ASSERT_EQ('8', dst[8]);
913 ASSERT_EQ('\0', dst[9]);
914 ASSERT_EQ('\0', dst[10]);
915 ASSERT_EQ('\0', dst[11]);
916 ASSERT_EQ('\0', dst[12]);
917 ASSERT_EQ('\0', dst[13]);
918 ASSERT_EQ('\0', dst[14]);
919}
920
Nick Kralevich93501d32013-08-28 10:47:43 -0700921TEST(TEST_NAME, strncpy) {
922 char src[10];
923 char dst[10];
924 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
925 strncpy(dst, src, sizeof(dst));
926 ASSERT_EQ('0', dst[0]);
927 ASSERT_EQ('1', dst[1]);
928 ASSERT_EQ('2', dst[2]);
929 ASSERT_EQ('3', dst[3]);
930 ASSERT_EQ('4', dst[4]);
931 ASSERT_EQ('5', dst[5]);
932 ASSERT_EQ('6', dst[6]);
933 ASSERT_EQ('7', dst[7]);
934 ASSERT_EQ('8', dst[8]);
935 ASSERT_EQ('9', dst[9]);
936}
937
938TEST(TEST_NAME, strncpy2) {
939 char src[10];
940 char dst[15];
941 memcpy(src, "012345678\0", sizeof(src));
942 strncpy(dst, src, sizeof(dst));
943 ASSERT_EQ('0', dst[0]);
944 ASSERT_EQ('1', dst[1]);
945 ASSERT_EQ('2', dst[2]);
946 ASSERT_EQ('3', dst[3]);
947 ASSERT_EQ('4', dst[4]);
948 ASSERT_EQ('5', dst[5]);
949 ASSERT_EQ('6', dst[6]);
950 ASSERT_EQ('7', dst[7]);
951 ASSERT_EQ('8', dst[8]);
952 ASSERT_EQ('\0', dst[9]);
953 ASSERT_EQ('\0', dst[10]);
954 ASSERT_EQ('\0', dst[11]);
955 ASSERT_EQ('\0', dst[12]);
956 ASSERT_EQ('\0', dst[13]);
957 ASSERT_EQ('\0', dst[14]);
958}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700959
960TEST(TEST_NAME, strcat_chk_max_int_size) {
961 char buf[10];
962 memset(buf, 'A', sizeof(buf));
963 buf[0] = 'a';
964 buf[1] = '\0';
965 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
966 ASSERT_EQ(buf, res);
967 ASSERT_EQ('a', buf[0]);
968 ASSERT_EQ('0', buf[1]);
969 ASSERT_EQ('1', buf[2]);
970 ASSERT_EQ('2', buf[3]);
971 ASSERT_EQ('3', buf[4]);
972 ASSERT_EQ('4', buf[5]);
973 ASSERT_EQ('5', buf[6]);
974 ASSERT_EQ('6', buf[7]);
975 ASSERT_EQ('7', buf[8]);
976 ASSERT_EQ('\0', buf[9]);
977}
978
Christopher Ferris950a58e2014-04-04 14:38:18 -0700979extern "C" char* __stpcpy_chk(char*, const char*, size_t);
980
981TEST(TEST_NAME, stpcpy_chk_max_int_size) {
982 char buf[10];
983 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
984 ASSERT_EQ(buf + strlen("012345678"), res);
985 ASSERT_STREQ("012345678", buf);
986}
987
Christopher Ferris16e185c2013-09-10 16:56:34 -0700988extern "C" char* __strcpy_chk(char*, const char*, size_t);
989
990TEST(TEST_NAME, strcpy_chk_max_int_size) {
991 char buf[10];
992 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
993 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700994 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700995}
996
997extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
998
999TEST(TEST_NAME, memcpy_chk_max_int_size) {
1000 char buf[10];
1001 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
1002 ASSERT_EQ((void*)buf, res);
1003 ASSERT_EQ('0', buf[0]);
1004 ASSERT_EQ('1', buf[1]);
1005 ASSERT_EQ('2', buf[2]);
1006 ASSERT_EQ('3', buf[3]);
1007 ASSERT_EQ('4', buf[4]);
1008 ASSERT_EQ('5', buf[5]);
1009 ASSERT_EQ('6', buf[6]);
1010 ASSERT_EQ('7', buf[7]);
1011 ASSERT_EQ('8', buf[8]);
1012 ASSERT_EQ('\0', buf[9]);
1013}
Stephen Hines6e380722013-10-11 00:45:24 -07001014
1015// Verify that macro expansion is done properly for sprintf/snprintf (which
1016// are defined as macros in stdio.h under clang).
1017#define CONTENTS "macro expansion"
1018#define BUF_AND_SIZE(A) A, sizeof(A)
1019#define BUF_AND_CONTENTS(A) A, CONTENTS
1020#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
1021TEST(TEST_NAME, s_n_printf_macro_expansion) {
1022 char buf[BUFSIZ];
1023 snprintf(BUF_AND_SIZE(buf), CONTENTS);
1024 EXPECT_STREQ(CONTENTS, buf);
1025
1026 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
1027 EXPECT_STREQ(CONTENTS, buf);
1028
1029 sprintf(BUF_AND_CONTENTS(buf));
1030 EXPECT_STREQ(CONTENTS, buf);
1031}
Elliott Hughes4674e382015-02-02 09:15:19 -08001032
1033TEST_F(DEATHTEST, poll_fortified) {
1034 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1035 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -08001036 // Set timeout to zero to prevent waiting in poll when fortify test fails.
1037 ASSERT_FORTIFY(poll(buf, fd_count, 0));
Elliott Hughes4674e382015-02-02 09:15:19 -08001038}
1039
1040TEST_F(DEATHTEST, ppoll_fortified) {
1041 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
1042 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -08001043 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
1044 timespec timeout;
1045 timeout.tv_sec = timeout.tv_nsec = 0;
1046 ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, NULL));
Elliott Hughes4674e382015-02-02 09:15:19 -08001047}