blob: 67103e1b215ff43c57e7472a7875cc4bde477220 [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
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070061TEST_F(DEATHTEST, stpncpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070062 foo myfoo;
63 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080064 ASSERT_FORTIFY(stpncpy(myfoo.a, "01234567890", copy_amt));
Christopher Ferris950a58e2014-04-04 14:38:18 -070065}
Christopher Ferris950a58e2014-04-04 14:38:18 -070066
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070067TEST_F(DEATHTEST, stpncpy2_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -070068 foo myfoo;
69 memset(&myfoo, 0, sizeof(myfoo));
70 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080071 ASSERT_FORTIFY(stpncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Christopher Ferris950a58e2014-04-04 14:38:18 -070072}
Christopher Ferris950a58e2014-04-04 14:38:18 -070073
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070074TEST_F(DEATHTEST, strncpy_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070075 foo myfoo;
76 int copy_amt = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -080077 ASSERT_FORTIFY(strncpy(myfoo.a, "01234567890", copy_amt));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070078}
Nick Kralevich5bcf3982013-06-28 10:34:09 -070079
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070080TEST_F(DEATHTEST, strncpy2_fortified2) {
Nick Kralevich93501d32013-08-28 10:47:43 -070081 foo myfoo;
82 memset(&myfoo, 0, sizeof(myfoo));
83 myfoo.one[0] = 'A'; // not null terminated string
Elliott Hughesd036e942015-02-02 11:18:58 -080084 ASSERT_FORTIFY(strncpy(myfoo.b, myfoo.one, sizeof(myfoo.b)));
Nick Kralevich93501d32013-08-28 10:47:43 -070085}
Nick Kralevich93501d32013-08-28 10:47:43 -070086
Nick Kralevichbe0e43b2014-07-23 13:56:23 -070087TEST_F(DEATHTEST, sprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -070088 foo myfoo;
89 char source_buf[15];
90 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -080091 ASSERT_FORTIFY(sprintf(myfoo.a, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -070092}
Nick Kralevich5bcf3982013-06-28 10:34:09 -070093
Nick Kralevich884a3de2014-10-06 00:39:47 +000094TEST_F(DEATHTEST, sprintf2_fortified2) {
Nick Kralevich884a3de2014-10-06 00:39:47 +000095 foo myfoo;
Elliott Hughesd036e942015-02-02 11:18:58 -080096 ASSERT_FORTIFY(sprintf(myfoo.a, "0123456789"));
Nick Kralevich884a3de2014-10-06 00:39:47 +000097}
Nick Kralevich884a3de2014-10-06 00:39:47 +000098
Nick Kralevich5bcf3982013-06-28 10:34:09 -070099static int vsprintf_helper2(const char *fmt, ...) {
100 foo myfoo;
101 va_list va;
102 int result;
103
104 va_start(va, fmt);
105 result = vsprintf(myfoo.a, fmt, va); // should crash here
106 va_end(va);
107 return result;
108}
109
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700110TEST_F(DEATHTEST, vsprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800111 ASSERT_FORTIFY(vsprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700112}
113
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700114TEST_F(DEATHTEST, vsprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800115 ASSERT_FORTIFY(vsprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700116}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700117
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700118static int vsnprintf_helper2(const char *fmt, ...) {
119 foo myfoo;
120 va_list va;
121 int result;
122 size_t size = atoi("11");
123
124 va_start(va, fmt);
125 result = vsnprintf(myfoo.a, size, fmt, va); // should crash here
126 va_end(va);
127 return result;
128}
129
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700130TEST_F(DEATHTEST, vsnprintf_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800131 ASSERT_FORTIFY(vsnprintf_helper2("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700132}
133
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700134TEST_F(DEATHTEST, vsnprintf2_fortified2) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800135 ASSERT_FORTIFY(vsnprintf_helper2("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700136}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700137
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700138// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700139TEST_F(DEATHTEST, stpcpy_fortified2) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700140#if defined(__BIONIC__)
Christopher Ferris950a58e2014-04-04 14:38:18 -0700141 foo myfoo;
142 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800143 ASSERT_FORTIFY(stpcpy(myfoo.empty, src));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700144 free(src);
145#else // __BIONIC__
146 GTEST_LOG_(INFO) << "This test does nothing.\n";
147#endif // __BIONIC__
148}
Christopher Ferris950a58e2014-04-04 14:38:18 -0700149
Christopher Ferris950a58e2014-04-04 14:38:18 -0700150// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700151TEST_F(DEATHTEST, strcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800152#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700153 foo myfoo;
154 char* src = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800155 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700156 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800157#else // __BIONIC__
158 GTEST_LOG_(INFO) << "This test does nothing.\n";
159#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700160}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700161
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700162// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700163TEST_F(DEATHTEST, strcpy2_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800164#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700165 foo myfoo;
166 char* src = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800167 ASSERT_FORTIFY(strcpy(myfoo.empty, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700168 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800169#else // __BIONIC__
170 GTEST_LOG_(INFO) << "This test does nothing.\n";
171#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700172}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700173
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700174// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700175TEST_F(DEATHTEST, strcpy3_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800176#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700177 foo myfoo;
178 char* src = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800179 ASSERT_FORTIFY(strcpy(myfoo.one, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700180 free(src);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800181#else // __BIONIC__
182 GTEST_LOG_(INFO) << "This test does nothing.\n";
183#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700184}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700185
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700186TEST_F(DEATHTEST, strchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800187#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700188 foo myfoo;
189 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
190 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800191 ASSERT_FORTIFY(printf("%s", strchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700192 ASSERT_FORTIFY(printf("%s", strchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800193#else // __BIONIC__
194 GTEST_LOG_(INFO) << "This test does nothing.\n";
195#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700196}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700197
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700198TEST_F(DEATHTEST, strrchr_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800199#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700200 foo myfoo;
201 memcpy(myfoo.a, "0123456789", 10);
202 memcpy(myfoo.b, "01234", 6);
Elliott Hughesd036e942015-02-02 11:18:58 -0800203 ASSERT_FORTIFY(printf("%s", strrchr(myfoo.a, 'a')));
George Burgess IVbd3d2082017-04-04 17:34:02 -0700204 ASSERT_FORTIFY(printf("%s", strrchr(static_cast<const char*>(myfoo.a), 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800205#else // __BIONIC__
206 GTEST_LOG_(INFO) << "This test does nothing.\n";
207#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700208}
George Burgess IVbd3d2082017-04-04 17:34:02 -0700209
210TEST_F(DEATHTEST, memchr_fortified2) {
211#if defined(__BIONIC__)
212 foo myfoo;
213 volatile int asize = sizeof(myfoo.a) + 1;
214 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a));
215 ASSERT_FORTIFY(printf("%s", memchr(myfoo.a, 'a', asize)));
216 ASSERT_FORTIFY(printf("%s", memchr(static_cast<const void*>(myfoo.a), 'a', asize)));
217#else // __BIONIC__
218 GTEST_LOG_(INFO) << "This test does nothing.\n";
219#endif // __BIONIC__
220}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700221
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700222// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700223TEST_F(DEATHTEST, strlcpy_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800224#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700225 foo myfoo;
226 strcpy(myfoo.a, "01");
227 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800228 ASSERT_FORTIFY(strlcpy(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800229#else // __BIONIC__
230 GTEST_LOG_(INFO) << "This test does nothing.\n";
231#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700232}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700233
Nick Kralevicha6cde392013-06-29 08:15:25 -0700234// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700235TEST_F(DEATHTEST, strlcat_fortified2) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800236#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700237 foo myfoo;
238 strcpy(myfoo.a, "01");
239 myfoo.one[0] = '\0';
240 size_t n = strlen(myfoo.a);
Elliott Hughesd036e942015-02-02 11:18:58 -0800241 ASSERT_FORTIFY(strlcat(myfoo.one, myfoo.a, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800242#else // __BIONIC__
243 GTEST_LOG_(INFO) << "This test does nothing.\n";
244#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700245}
Nick Kralevicha6cde392013-06-29 08:15:25 -0700246
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700247TEST_F(DEATHTEST, strncat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700248 foo myfoo;
249 size_t n = atoi("10"); // avoid compiler optimizations
250 strncpy(myfoo.a, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800251 ASSERT_FORTIFY(strncat(myfoo.a, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700252}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700253
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700254TEST_F(DEATHTEST, strncat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700255 foo myfoo;
256 myfoo.a[0] = '\0';
257 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800258 ASSERT_FORTIFY(strncat(myfoo.a, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700259}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700260
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700261TEST_F(DEATHTEST, strncat3_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700262 foo myfoo;
263 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
264 myfoo.b[0] = '\0';
265 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800266 ASSERT_FORTIFY(strncat(myfoo.b, myfoo.a, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700267}
268
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700269TEST_F(DEATHTEST, strcat_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700270 char src[11];
271 strcpy(src, "0123456789");
272 foo myfoo;
273 myfoo.a[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800274 ASSERT_FORTIFY(strcat(myfoo.a, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700275}
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700276
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700277TEST_F(DEATHTEST, strcat2_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700278 foo myfoo;
279 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
280 myfoo.b[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800281 ASSERT_FORTIFY(strcat(myfoo.b, myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700282}
283
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700284TEST_F(DEATHTEST, snprintf_fortified2) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700285 foo myfoo;
286 strcpy(myfoo.a, "012345678");
287 size_t n = strlen(myfoo.a) + 2;
Elliott Hughesd036e942015-02-02 11:18:58 -0800288 ASSERT_FORTIFY(snprintf(myfoo.b, n, "a%s", myfoo.a));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700289}
290
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700291TEST_F(DEATHTEST, bzero_fortified2) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700292 foo myfoo;
293 memcpy(myfoo.b, "0123456789", sizeof(myfoo.b));
294 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800295 ASSERT_FORTIFY(bzero(myfoo.b, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700296}
297
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700298#endif /* defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE=2 */
299
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700300// multibyte target where we over fill (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700301TEST_F(DEATHTEST, strcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800302#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700303 char buf[10];
304 char *orig = strdup("0123456789");
Elliott Hughesd036e942015-02-02 11:18:58 -0800305 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700306 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800307#else // __BIONIC__
308 GTEST_LOG_(INFO) << "This test does nothing.\n";
309#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700310}
311
312// zero sized target with "\0" source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700313TEST_F(DEATHTEST, strcpy2_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800314#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700315 char buf[0];
316 char *orig = strdup("");
Elliott Hughesd036e942015-02-02 11:18:58 -0800317 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700318 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800319#else // __BIONIC__
320 GTEST_LOG_(INFO) << "This test does nothing.\n";
321#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700322}
323
324// zero sized target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700325TEST_F(DEATHTEST, strcpy3_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800326#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700327 char buf[0];
328 char *orig = strdup("1");
Elliott Hughesd036e942015-02-02 11:18:58 -0800329 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700330 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800331#else // __BIONIC__
332 GTEST_LOG_(INFO) << "This test does nothing.\n";
333#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700334}
335
336// one byte target with longer source (should fail)
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700337TEST_F(DEATHTEST, strcpy4_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800338#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700339 char buf[1];
340 char *orig = strdup("12");
Elliott Hughesd036e942015-02-02 11:18:58 -0800341 ASSERT_FORTIFY(strcpy(buf, orig));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700342 free(orig);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800343#else // __BIONIC__
344 GTEST_LOG_(INFO) << "This test does nothing.\n";
345#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700346}
347
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700348TEST_F(DEATHTEST, strlen_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800349#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700350 char buf[10];
351 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800352 ASSERT_FORTIFY(printf("%zd", strlen(buf)));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800353#else // __BIONIC__
354 GTEST_LOG_(INFO) << "This test does nothing.\n";
355#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700356}
357
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700358TEST_F(DEATHTEST, strchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800359#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700360 char buf[10];
361 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800362 ASSERT_FORTIFY(printf("%s", strchr(buf, 'a')));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800363#else // __BIONIC__
364 GTEST_LOG_(INFO) << "This test does nothing.\n";
365#endif // __BIONIC__
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700366}
367
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700368TEST_F(DEATHTEST, strrchr_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800369#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700370 char buf[10];
371 memcpy(buf, "0123456789", sizeof(buf));
Elliott Hughesd036e942015-02-02 11:18:58 -0800372 ASSERT_FORTIFY(printf("%s", strrchr(buf, 'a')));
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
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700378TEST_F(DEATHTEST, strlcpy_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800379#if defined(__BIONIC__)
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700380 char bufa[15];
381 char bufb[10];
382 strcpy(bufa, "01234567890123");
383 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800384 ASSERT_FORTIFY(strlcpy(bufb, bufa, n));
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
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700390TEST_F(DEATHTEST, strlcat_fortified) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800391#if defined(__BIONIC__)
Nick Kralevicha6cde392013-06-29 08:15:25 -0700392 char bufa[15];
393 char bufb[10];
394 bufb[0] = '\0';
395 strcpy(bufa, "01234567890123");
396 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800397 ASSERT_FORTIFY(strlcat(bufb, bufa, n));
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800398#else // __BIONIC__
399 GTEST_LOG_(INFO) << "This test does nothing.\n";
400#endif // __BIONIC__
Nick Kralevicha6cde392013-06-29 08:15:25 -0700401}
402
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700403TEST_F(DEATHTEST, sprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700404 char buf[10];
405 char source_buf[15];
406 memcpy(source_buf, "12345678901234", 15);
Elliott Hughesd036e942015-02-02 11:18:58 -0800407 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700408}
409
George Burgess IV5f8a6732017-04-06 11:26:11 -0700410#ifdef __clang__
411// Exists upstream, but hasn't been pulled in yet.
412#if __has_attribute(alloc_size)
413#error "Reenable this test"
414#endif
415#else
Nick Kralevichb91791d2013-10-02 14:14:40 -0700416// This test is disabled in clang because clang doesn't properly detect
417// this buffer overflow. TODO: Fix clang.
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700418TEST_F(DEATHTEST, sprintf_malloc_fortified) {
Nick Kralevichb91791d2013-10-02 14:14:40 -0700419 char* buf = (char *) malloc(10);
420 char source_buf[11];
421 memcpy(source_buf, "1234567890", 11);
Elliott Hughesd036e942015-02-02 11:18:58 -0800422 ASSERT_FORTIFY(sprintf(buf, "%s", source_buf));
Nick Kralevichb91791d2013-10-02 14:14:40 -0700423 free(buf);
424}
425#endif
426
Nick Kralevich884a3de2014-10-06 00:39:47 +0000427TEST_F(DEATHTEST, sprintf2_fortified) {
Nick Kralevich884a3de2014-10-06 00:39:47 +0000428 char buf[5];
Elliott Hughesd036e942015-02-02 11:18:58 -0800429 ASSERT_FORTIFY(sprintf(buf, "aaaaa"));
Nick Kralevich884a3de2014-10-06 00:39:47 +0000430}
431
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700432static int vsprintf_helper(const char *fmt, ...) {
433 char buf[10];
434 va_list va;
435 int result;
436
437 va_start(va, fmt);
438 result = vsprintf(buf, fmt, va); // should crash here
439 va_end(va);
440 return result;
441}
442
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700443TEST_F(DEATHTEST, vsprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800444 ASSERT_FORTIFY(vsprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700445}
446
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700447TEST_F(DEATHTEST, vsprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800448 ASSERT_FORTIFY(vsprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700449}
450
451static int vsnprintf_helper(const char *fmt, ...) {
452 char buf[10];
453 va_list va;
454 int result;
455 size_t size = atoi("11");
456
457 va_start(va, fmt);
458 result = vsnprintf(buf, size, fmt, va); // should crash here
459 va_end(va);
460 return result;
461}
462
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700463TEST_F(DEATHTEST, vsnprintf_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800464 ASSERT_FORTIFY(vsnprintf_helper("%s", "0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700465}
466
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700467TEST_F(DEATHTEST, vsnprintf2_fortified) {
Elliott Hughesd036e942015-02-02 11:18:58 -0800468 ASSERT_FORTIFY(vsnprintf_helper("0123456789"));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700469}
470
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700471TEST_F(DEATHTEST, strncat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700472 char buf[10];
473 size_t n = atoi("10"); // avoid compiler optimizations
474 strncpy(buf, "012345678", n);
Elliott Hughesd036e942015-02-02 11:18:58 -0800475 ASSERT_FORTIFY(strncat(buf, "9", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700476}
477
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700478TEST_F(DEATHTEST, strncat2_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700479 char buf[10];
480 buf[0] = '\0';
481 size_t n = atoi("10"); // avoid compiler optimizations
Elliott Hughesd036e942015-02-02 11:18:58 -0800482 ASSERT_FORTIFY(strncat(buf, "0123456789", n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700483}
484
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700485TEST_F(DEATHTEST, strcat_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700486 char src[11];
487 strcpy(src, "0123456789");
488 char buf[10];
489 buf[0] = '\0';
Elliott Hughesd036e942015-02-02 11:18:58 -0800490 ASSERT_FORTIFY(strcat(buf, src));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700491}
492
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700493TEST_F(DEATHTEST, memmove_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700494 char buf[20];
495 strcpy(buf, "0123456789");
496 size_t n = atoi("10");
Elliott Hughesd036e942015-02-02 11:18:58 -0800497 ASSERT_FORTIFY(memmove(buf + 11, buf, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700498}
499
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700500TEST_F(DEATHTEST, memcpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700501 char bufa[10];
502 char bufb[10];
503 strcpy(bufa, "012345678");
504 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800505 ASSERT_FORTIFY(memcpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700506}
507
Elliott Hughes62e59642016-03-01 11:22:42 -0800508TEST_F(DEATHTEST, memset_fortified) {
509 char buf[10];
510 size_t n = atoi("11");
511 ASSERT_FORTIFY(memset(buf, 0, n));
512}
513
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700514TEST_F(DEATHTEST, stpncpy_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700515 char bufa[15];
516 char bufb[10];
517 strcpy(bufa, "01234567890123");
518 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800519 ASSERT_FORTIFY(stpncpy(bufb, bufa, n));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700520}
521
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700522TEST_F(DEATHTEST, stpncpy2_fortified) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700523 char dest[11];
524 char src[10];
525 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800526 ASSERT_FORTIFY(stpncpy(dest, src, sizeof(dest)));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700527}
528
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700529TEST_F(DEATHTEST, strncpy_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700530 char bufa[15];
531 char bufb[10];
532 strcpy(bufa, "01234567890123");
533 size_t n = strlen(bufa);
Elliott Hughesd036e942015-02-02 11:18:58 -0800534 ASSERT_FORTIFY(strncpy(bufb, bufa, n));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700535}
536
Christopher Ferris950a58e2014-04-04 14:38:18 -0700537
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700538TEST_F(DEATHTEST, strncpy2_fortified) {
Nick Kralevich93501d32013-08-28 10:47:43 -0700539 char dest[11];
540 char src[10];
541 memcpy(src, "0123456789", sizeof(src)); // src is not null terminated
Elliott Hughesd036e942015-02-02 11:18:58 -0800542 ASSERT_FORTIFY(strncpy(dest, src, sizeof(dest)));
Nick Kralevich93501d32013-08-28 10:47:43 -0700543}
544
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700545TEST_F(DEATHTEST, snprintf_fortified) {
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700546 char bufa[15];
547 char bufb[10];
548 strcpy(bufa, "0123456789");
549 size_t n = strlen(bufa) + 1;
Elliott Hughesd036e942015-02-02 11:18:58 -0800550 ASSERT_FORTIFY(snprintf(bufb, n, "%s", bufa));
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700551}
552
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700553TEST_F(DEATHTEST, bzero_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700554 char buf[10];
555 memcpy(buf, "0123456789", sizeof(buf));
556 size_t n = atoi("11");
Elliott Hughesd036e942015-02-02 11:18:58 -0800557 ASSERT_FORTIFY(bzero(buf, n));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700558}
559
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700560TEST_F(DEATHTEST, umask_fortified) {
Nick Kralevicha6cde392013-06-29 08:15:25 -0700561 mode_t mask = atoi("1023"); // 01777 in octal
Elliott Hughesd036e942015-02-02 11:18:58 -0800562 ASSERT_FORTIFY(umask(mask));
Nick Kralevicha6cde392013-06-29 08:15:25 -0700563}
564
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700565TEST_F(DEATHTEST, recv_fortified) {
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700566 size_t data_len = atoi("11"); // suppress compiler optimizations
567 char buf[10];
Elliott Hughesd036e942015-02-02 11:18:58 -0800568 ASSERT_FORTIFY(recv(0, buf, data_len, 0));
Nick Kralevich60f4f9a2013-09-24 16:32:07 -0700569}
570
Daniel Micay95b59c52017-02-13 17:27:59 -0800571TEST_F(DEATHTEST, send_fortified) {
572 size_t data_len = atoi("11"); // suppress compiler optimizations
573 char buf[10] = {0};
574 ASSERT_FORTIFY(send(0, buf, data_len, 0));
575}
576
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700577TEST_F(DEATHTEST, FD_ISSET_fortified) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700578#if defined(__BIONIC__) // glibc catches this at compile-time.
Nick Kralevich90201d52013-10-02 16:11:30 -0700579 fd_set set;
580 memset(&set, 0, sizeof(set));
Elliott Hughesd036e942015-02-02 11:18:58 -0800581 ASSERT_FORTIFY(FD_ISSET(-1, &set));
Elliott Hughes409588c2014-04-23 23:02:43 -0700582#endif
Nick Kralevich90201d52013-10-02 16:11:30 -0700583}
584
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700585TEST_F(DEATHTEST, FD_ISSET_2_fortified) {
Nick Kralevich7943df62013-10-03 14:08:39 -0700586 char buf[1];
587 fd_set* set = (fd_set*) buf;
Elliott Hughesd036e942015-02-02 11:18:58 -0800588 ASSERT_FORTIFY(FD_ISSET(0, set));
Nick Kralevich7943df62013-10-03 14:08:39 -0700589}
590
Daniel Micay9101b002015-05-20 15:31:26 -0400591TEST_F(DEATHTEST, getcwd_fortified) {
592 char buf[1];
593 size_t ct = atoi("2"); // prevent optimizations
594 ASSERT_FORTIFY(getcwd(buf, ct));
595}
596
Daniel Micaye7e1c872015-04-16 09:07:45 -0400597TEST_F(DEATHTEST, pread_fortified) {
598 char buf[1];
599 size_t ct = atoi("2"); // prevent optimizations
600 int fd = open("/dev/null", O_RDONLY);
601 ASSERT_FORTIFY(pread(fd, buf, ct, 0));
602 close(fd);
603}
604
605TEST_F(DEATHTEST, pread64_fortified) {
606 char buf[1];
607 size_t ct = atoi("2"); // prevent optimizations
608 int fd = open("/dev/null", O_RDONLY);
609 ASSERT_FORTIFY(pread64(fd, buf, ct, 0));
610 close(fd);
611}
612
Daniel Micayafdd1542015-07-20 21:37:29 -0400613TEST_F(DEATHTEST, pwrite_fortified) {
614 char buf[1] = {0};
615 size_t ct = atoi("2"); // prevent optimizations
616 int fd = open("/dev/null", O_WRONLY);
617 ASSERT_FORTIFY(pwrite(fd, buf, ct, 0));
618 close(fd);
619}
620
621TEST_F(DEATHTEST, pwrite64_fortified) {
622 char buf[1] = {0};
623 size_t ct = atoi("2"); // prevent optimizations
624 int fd = open("/dev/null", O_WRONLY);
625 ASSERT_FORTIFY(pwrite64(fd, buf, ct, 0));
626 close(fd);
627}
628
Nick Kralevichbe0e43b2014-07-23 13:56:23 -0700629TEST_F(DEATHTEST, read_fortified) {
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700630 char buf[1];
631 size_t ct = atoi("2"); // prevent optimizations
632 int fd = open("/dev/null", O_RDONLY);
Elliott Hughesd036e942015-02-02 11:18:58 -0800633 ASSERT_FORTIFY(read(fd, buf, ct));
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700634 close(fd);
635}
636
Daniel Micayafdd1542015-07-20 21:37:29 -0400637TEST_F(DEATHTEST, write_fortified) {
638 char buf[1] = {0};
639 size_t ct = atoi("2"); // prevent optimizations
640 int fd = open("/dev/null", O_WRONLY);
641 ASSERT_EXIT(write(fd, buf, ct), testing::KilledBySignal(SIGABRT), "");
642 close(fd);
643}
644
Daniel Micayfed26592015-07-18 13:55:51 -0400645TEST_F(DEATHTEST, fread_fortified) {
646 char buf[1];
647 size_t ct = atoi("2"); // prevent optimizations
648 FILE* fp = fopen("/dev/null", "r");
649 ASSERT_FORTIFY(fread(buf, 1, ct, fp));
650 fclose(fp);
651}
652
653TEST_F(DEATHTEST, fwrite_fortified) {
654 char buf[1] = {0};
655 size_t ct = atoi("2"); // prevent optimizations
656 FILE* fp = fopen("/dev/null", "w");
657 ASSERT_FORTIFY(fwrite(buf, 1, ct, fp));
658 fclose(fp);
659}
660
Daniel Micay42281882015-04-17 11:26:36 -0400661TEST_F(DEATHTEST, readlink_fortified) {
662 char buf[1];
663 size_t ct = atoi("2"); // prevent optimizations
664 ASSERT_FORTIFY(readlink("/dev/null", buf, ct));
665}
666
667TEST_F(DEATHTEST, readlinkat_fortified) {
668 char buf[1];
669 size_t ct = atoi("2"); // prevent optimizations
670 ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct));
671}
672
Nick Kralevich5bcf3982013-06-28 10:34:09 -0700673extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
674extern "C" char* __strcat_chk(char*, const char*, size_t);
675
676TEST(TEST_NAME, strncat) {
677 char buf[10];
678 memset(buf, 'A', sizeof(buf));
679 buf[0] = 'a';
680 buf[1] = '\0';
681 char* res = __strncat_chk(buf, "01234", sizeof(buf) - strlen(buf) - 1, sizeof(buf));
682 ASSERT_EQ(buf, res);
683 ASSERT_EQ('a', buf[0]);
684 ASSERT_EQ('0', buf[1]);
685 ASSERT_EQ('1', buf[2]);
686 ASSERT_EQ('2', buf[3]);
687 ASSERT_EQ('3', buf[4]);
688 ASSERT_EQ('4', buf[5]);
689 ASSERT_EQ('\0', buf[6]);
690 ASSERT_EQ('A', buf[7]);
691 ASSERT_EQ('A', buf[8]);
692 ASSERT_EQ('A', buf[9]);
693}
694
695TEST(TEST_NAME, strncat2) {
696 char buf[10];
697 memset(buf, 'A', sizeof(buf));
698 buf[0] = 'a';
699 buf[1] = '\0';
700 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
701 ASSERT_EQ(buf, res);
702 ASSERT_EQ('a', buf[0]);
703 ASSERT_EQ('0', buf[1]);
704 ASSERT_EQ('1', buf[2]);
705 ASSERT_EQ('2', buf[3]);
706 ASSERT_EQ('3', buf[4]);
707 ASSERT_EQ('4', buf[5]);
708 ASSERT_EQ('\0', buf[6]);
709 ASSERT_EQ('A', buf[7]);
710 ASSERT_EQ('A', buf[8]);
711 ASSERT_EQ('A', buf[9]);
712}
713
714TEST(TEST_NAME, strncat3) {
715 char buf[10];
716 memset(buf, 'A', sizeof(buf));
717 buf[0] = '\0';
718 char* res = __strncat_chk(buf, "0123456789", 5, sizeof(buf));
719 ASSERT_EQ(buf, res);
720 ASSERT_EQ('0', buf[0]);
721 ASSERT_EQ('1', buf[1]);
722 ASSERT_EQ('2', buf[2]);
723 ASSERT_EQ('3', buf[3]);
724 ASSERT_EQ('4', buf[4]);
725 ASSERT_EQ('\0', buf[5]);
726 ASSERT_EQ('A', buf[6]);
727 ASSERT_EQ('A', buf[7]);
728 ASSERT_EQ('A', buf[8]);
729 ASSERT_EQ('A', buf[9]);
730}
731
732TEST(TEST_NAME, strncat4) {
733 char buf[10];
734 memset(buf, 'A', sizeof(buf));
735 buf[9] = '\0';
736 char* res = __strncat_chk(buf, "", 5, sizeof(buf));
737 ASSERT_EQ(buf, res);
738 ASSERT_EQ('A', buf[0]);
739 ASSERT_EQ('A', buf[1]);
740 ASSERT_EQ('A', buf[2]);
741 ASSERT_EQ('A', buf[3]);
742 ASSERT_EQ('A', buf[4]);
743 ASSERT_EQ('A', buf[5]);
744 ASSERT_EQ('A', buf[6]);
745 ASSERT_EQ('A', buf[7]);
746 ASSERT_EQ('A', buf[8]);
747 ASSERT_EQ('\0', buf[9]);
748}
749
750TEST(TEST_NAME, strncat5) {
751 char buf[10];
752 memset(buf, 'A', sizeof(buf));
753 buf[0] = 'a';
754 buf[1] = '\0';
755 char* res = __strncat_chk(buf, "01234567", 8, 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('5', buf[6]);
764 ASSERT_EQ('6', buf[7]);
765 ASSERT_EQ('7', buf[8]);
766 ASSERT_EQ('\0', buf[9]);
767}
768
769TEST(TEST_NAME, strncat6) {
770 char buf[10];
771 memset(buf, 'A', sizeof(buf));
772 buf[0] = 'a';
773 buf[1] = '\0';
774 char* res = __strncat_chk(buf, "01234567", 9, sizeof(buf));
775 ASSERT_EQ(buf, res);
776 ASSERT_EQ('a', buf[0]);
777 ASSERT_EQ('0', buf[1]);
778 ASSERT_EQ('1', buf[2]);
779 ASSERT_EQ('2', buf[3]);
780 ASSERT_EQ('3', buf[4]);
781 ASSERT_EQ('4', buf[5]);
782 ASSERT_EQ('5', buf[6]);
783 ASSERT_EQ('6', buf[7]);
784 ASSERT_EQ('7', buf[8]);
785 ASSERT_EQ('\0', buf[9]);
786}
787
788
789TEST(TEST_NAME, strcat) {
790 char buf[10];
791 memset(buf, 'A', sizeof(buf));
792 buf[0] = 'a';
793 buf[1] = '\0';
794 char* res = __strcat_chk(buf, "01234", sizeof(buf));
795 ASSERT_EQ(buf, res);
796 ASSERT_EQ('a', buf[0]);
797 ASSERT_EQ('0', buf[1]);
798 ASSERT_EQ('1', buf[2]);
799 ASSERT_EQ('2', buf[3]);
800 ASSERT_EQ('3', buf[4]);
801 ASSERT_EQ('4', buf[5]);
802 ASSERT_EQ('\0', buf[6]);
803 ASSERT_EQ('A', buf[7]);
804 ASSERT_EQ('A', buf[8]);
805 ASSERT_EQ('A', buf[9]);
806}
807
808TEST(TEST_NAME, strcat2) {
809 char buf[10];
810 memset(buf, 'A', sizeof(buf));
811 buf[0] = 'a';
812 buf[1] = '\0';
813 char* res = __strcat_chk(buf, "01234567", sizeof(buf));
814 ASSERT_EQ(buf, res);
815 ASSERT_EQ('a', buf[0]);
816 ASSERT_EQ('0', buf[1]);
817 ASSERT_EQ('1', buf[2]);
818 ASSERT_EQ('2', buf[3]);
819 ASSERT_EQ('3', buf[4]);
820 ASSERT_EQ('4', buf[5]);
821 ASSERT_EQ('5', buf[6]);
822 ASSERT_EQ('6', buf[7]);
823 ASSERT_EQ('7', buf[8]);
824 ASSERT_EQ('\0', buf[9]);
825}
Nick Kralevich93501d32013-08-28 10:47:43 -0700826
Christopher Ferris950a58e2014-04-04 14:38:18 -0700827TEST(TEST_NAME, stpncpy) {
828 char src[10];
829 char dst[10];
830 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
831 stpncpy(dst, src, sizeof(dst));
832 ASSERT_EQ('0', dst[0]);
833 ASSERT_EQ('1', dst[1]);
834 ASSERT_EQ('2', dst[2]);
835 ASSERT_EQ('3', dst[3]);
836 ASSERT_EQ('4', dst[4]);
837 ASSERT_EQ('5', dst[5]);
838 ASSERT_EQ('6', dst[6]);
839 ASSERT_EQ('7', dst[7]);
840 ASSERT_EQ('8', dst[8]);
841 ASSERT_EQ('9', dst[9]);
842}
843
844TEST(TEST_NAME, stpncpy2) {
845 char src[10];
846 char dst[15];
847 memcpy(src, "012345678\0", sizeof(src));
848 stpncpy(dst, src, sizeof(dst));
849 ASSERT_EQ('0', dst[0]);
850 ASSERT_EQ('1', dst[1]);
851 ASSERT_EQ('2', dst[2]);
852 ASSERT_EQ('3', dst[3]);
853 ASSERT_EQ('4', dst[4]);
854 ASSERT_EQ('5', dst[5]);
855 ASSERT_EQ('6', dst[6]);
856 ASSERT_EQ('7', dst[7]);
857 ASSERT_EQ('8', dst[8]);
858 ASSERT_EQ('\0', dst[9]);
859 ASSERT_EQ('\0', dst[10]);
860 ASSERT_EQ('\0', dst[11]);
861 ASSERT_EQ('\0', dst[12]);
862 ASSERT_EQ('\0', dst[13]);
863 ASSERT_EQ('\0', dst[14]);
864}
865
Nick Kralevich93501d32013-08-28 10:47:43 -0700866TEST(TEST_NAME, strncpy) {
867 char src[10];
868 char dst[10];
869 memcpy(src, "0123456789", sizeof(src)); // non null terminated string
870 strncpy(dst, src, sizeof(dst));
871 ASSERT_EQ('0', dst[0]);
872 ASSERT_EQ('1', dst[1]);
873 ASSERT_EQ('2', dst[2]);
874 ASSERT_EQ('3', dst[3]);
875 ASSERT_EQ('4', dst[4]);
876 ASSERT_EQ('5', dst[5]);
877 ASSERT_EQ('6', dst[6]);
878 ASSERT_EQ('7', dst[7]);
879 ASSERT_EQ('8', dst[8]);
880 ASSERT_EQ('9', dst[9]);
881}
882
883TEST(TEST_NAME, strncpy2) {
884 char src[10];
885 char dst[15];
886 memcpy(src, "012345678\0", sizeof(src));
887 strncpy(dst, src, sizeof(dst));
888 ASSERT_EQ('0', dst[0]);
889 ASSERT_EQ('1', dst[1]);
890 ASSERT_EQ('2', dst[2]);
891 ASSERT_EQ('3', dst[3]);
892 ASSERT_EQ('4', dst[4]);
893 ASSERT_EQ('5', dst[5]);
894 ASSERT_EQ('6', dst[6]);
895 ASSERT_EQ('7', dst[7]);
896 ASSERT_EQ('8', dst[8]);
897 ASSERT_EQ('\0', dst[9]);
898 ASSERT_EQ('\0', dst[10]);
899 ASSERT_EQ('\0', dst[11]);
900 ASSERT_EQ('\0', dst[12]);
901 ASSERT_EQ('\0', dst[13]);
902 ASSERT_EQ('\0', dst[14]);
903}
Christopher Ferris16e185c2013-09-10 16:56:34 -0700904
905TEST(TEST_NAME, strcat_chk_max_int_size) {
906 char buf[10];
907 memset(buf, 'A', sizeof(buf));
908 buf[0] = 'a';
909 buf[1] = '\0';
910 char* res = __strcat_chk(buf, "01234567", (size_t)-1);
911 ASSERT_EQ(buf, res);
912 ASSERT_EQ('a', buf[0]);
913 ASSERT_EQ('0', buf[1]);
914 ASSERT_EQ('1', buf[2]);
915 ASSERT_EQ('2', buf[3]);
916 ASSERT_EQ('3', buf[4]);
917 ASSERT_EQ('4', buf[5]);
918 ASSERT_EQ('5', buf[6]);
919 ASSERT_EQ('6', buf[7]);
920 ASSERT_EQ('7', buf[8]);
921 ASSERT_EQ('\0', buf[9]);
922}
923
Christopher Ferris950a58e2014-04-04 14:38:18 -0700924extern "C" char* __stpcpy_chk(char*, const char*, size_t);
925
926TEST(TEST_NAME, stpcpy_chk_max_int_size) {
927 char buf[10];
928 char* res = __stpcpy_chk(buf, "012345678", (size_t)-1);
929 ASSERT_EQ(buf + strlen("012345678"), res);
930 ASSERT_STREQ("012345678", buf);
931}
932
Christopher Ferris16e185c2013-09-10 16:56:34 -0700933extern "C" char* __strcpy_chk(char*, const char*, size_t);
934
935TEST(TEST_NAME, strcpy_chk_max_int_size) {
936 char buf[10];
937 char* res = __strcpy_chk(buf, "012345678", (size_t)-1);
938 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700939 ASSERT_STREQ("012345678", buf);
Christopher Ferris16e185c2013-09-10 16:56:34 -0700940}
941
942extern "C" void* __memcpy_chk(void*, const void*, size_t, size_t);
943
944TEST(TEST_NAME, memcpy_chk_max_int_size) {
945 char buf[10];
946 void* res = __memcpy_chk(buf, "012345678", sizeof(buf), (size_t)-1);
947 ASSERT_EQ((void*)buf, res);
948 ASSERT_EQ('0', buf[0]);
949 ASSERT_EQ('1', buf[1]);
950 ASSERT_EQ('2', buf[2]);
951 ASSERT_EQ('3', buf[3]);
952 ASSERT_EQ('4', buf[4]);
953 ASSERT_EQ('5', buf[5]);
954 ASSERT_EQ('6', buf[6]);
955 ASSERT_EQ('7', buf[7]);
956 ASSERT_EQ('8', buf[8]);
957 ASSERT_EQ('\0', buf[9]);
958}
Stephen Hines6e380722013-10-11 00:45:24 -0700959
960// Verify that macro expansion is done properly for sprintf/snprintf (which
961// are defined as macros in stdio.h under clang).
962#define CONTENTS "macro expansion"
963#define BUF_AND_SIZE(A) A, sizeof(A)
964#define BUF_AND_CONTENTS(A) A, CONTENTS
965#define BUF_AND_SIZE_AND_CONTENTS(A) A, sizeof(A), CONTENTS
966TEST(TEST_NAME, s_n_printf_macro_expansion) {
967 char buf[BUFSIZ];
968 snprintf(BUF_AND_SIZE(buf), CONTENTS);
969 EXPECT_STREQ(CONTENTS, buf);
970
971 snprintf(BUF_AND_SIZE_AND_CONTENTS(buf));
972 EXPECT_STREQ(CONTENTS, buf);
973
974 sprintf(BUF_AND_CONTENTS(buf));
975 EXPECT_STREQ(CONTENTS, buf);
976}
Elliott Hughes4674e382015-02-02 09:15:19 -0800977
978TEST_F(DEATHTEST, poll_fortified) {
979 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
980 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800981 // Set timeout to zero to prevent waiting in poll when fortify test fails.
982 ASSERT_FORTIFY(poll(buf, fd_count, 0));
Elliott Hughes4674e382015-02-02 09:15:19 -0800983}
984
985TEST_F(DEATHTEST, ppoll_fortified) {
986 nfds_t fd_count = atoi("2"); // suppress compiler optimizations
987 pollfd buf[1] = {{0, POLLIN, 0}};
Yabin Cuif4fe6932015-02-03 17:52:32 -0800988 // Set timeout to zero to prevent waiting in ppoll when fortify test fails.
989 timespec timeout;
990 timeout.tv_sec = timeout.tv_nsec = 0;
991 ASSERT_FORTIFY(ppoll(buf, fd_count, &timeout, NULL));
Elliott Hughes4674e382015-02-02 09:15:19 -0800992}