blob: 54a6e5a5a66bcbaa8d06d420d9ea591e032d6eff [file] [log] [blame]
Nick Kralevich16d1af12013-06-17 14:49:19 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef _FORTIFY_SOURCE
18#define _FORTIFY_SOURCE 2
19
20#include <gtest/gtest.h>
21#include <string.h>
22
23struct foo {
24 char empty[0];
25 char one[1];
26 char a[10];
27 char b[10];
28};
29
30// We have to say "DeathTest" here so gtest knows to run this test (which exits)
31// in its own process.
32TEST(Fortify2_Clang_DeathTest, strncat3_fortified2) {
33 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
34 foo myfoo;
35 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
36 myfoo.b[0] = '\0';
37 size_t n = atoi("10"); // avoid compiler optimizations
38 ASSERT_EXIT(strncat(myfoo.b, myfoo.a, n), testing::KilledBySignal(SIGABRT), "");
39}
40
41TEST(Fortify2_Clang_DeathTest, strcat2_fortified2) {
42 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
43 foo myfoo;
44 memcpy(myfoo.a, "0123456789", sizeof(myfoo.a)); // unterminated string
45 myfoo.b[0] = '\0';
46 ASSERT_EXIT(strcat(myfoo.b, myfoo.a), testing::KilledBySignal(SIGABRT), "");
47}
48
49/*****************************************************************/
50/* TESTS BELOW HERE DUPLICATE TESTS FROM fortify1_test_clang.cpp */
51/*****************************************************************/
52
53#if __BIONIC__
54// multibyte target where we over fill (should fail)
55TEST(Fortify2_Clang_DeathTest, strcpy_fortified) {
56 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
57 char buf[10];
58 char *orig = strdup("0123456789");
59 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
60 free(orig);
61}
62
63// zero sized target with "\0" source (should fail)
64TEST(Fortify2_Clang_DeathTest, strcpy2_fortified) {
65 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
66 char buf[0];
67 char *orig = strdup("");
68 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
69 free(orig);
70}
71
72// zero sized target with longer source (should fail)
73TEST(Fortify2_Clang_DeathTest, strcpy3_fortified) {
74 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
75 char buf[0];
76 char *orig = strdup("1");
77 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
78 free(orig);
79}
80
81// one byte target with longer source (should fail)
82TEST(Fortify2_Clang_DeathTest, strcpy4_fortified) {
83 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
84 char buf[1];
85 char *orig = strdup("12");
86 ASSERT_EXIT(strcpy(buf, orig), testing::KilledBySignal(SIGABRT), "");
87 free(orig);
88}
89
90TEST(Fortify2_Clang_DeathTest, strlen_fortified) {
91 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
92 char buf[10];
93 memcpy(buf, "0123456789", sizeof(buf));
94 ASSERT_EXIT(printf("%d", strlen(buf)), testing::KilledBySignal(SIGABRT), "");
95}
96
97TEST(Fortify2_Clang_DeathTest, strchr_fortified) {
98 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
99 char buf[10];
100 memcpy(buf, "0123456789", sizeof(buf));
101 ASSERT_EXIT(printf("%s", strchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
102}
103
104TEST(Fortify2_Clang_DeathTest, strrchr_fortified) {
105 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
106 char buf[10];
107 memcpy(buf, "0123456789", sizeof(buf));
108 ASSERT_EXIT(printf("%s", strrchr(buf, 'a')), testing::KilledBySignal(SIGABRT), "");
109}
110#endif
111
Nick Kralevichc6eb9852013-06-24 11:44:00 -0700112TEST(Fortify2_Clang_DeathTest, sprintf_fortified) {
113 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
114 char buf[10];
115 char source_buf[15];
116 memcpy(source_buf, "12345678901234", 15);
117 ASSERT_EXIT(sprintf(buf, "%s", source_buf), testing::KilledBySignal(SIGABRT), "");
118}
119
120TEST(Fortify2_Clang_DeathTest, sprintf2_fortified) {
121 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
122 char buf[5];
123 ASSERT_EXIT(sprintf(buf, "aaaaa"), testing::KilledBySignal(SIGABRT), "");
124}
125
Nick Kralevich16d1af12013-06-17 14:49:19 -0700126TEST(Fortify2_Clang_DeathTest, strncat_fortified) {
127 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
128 char buf[10];
129 size_t n = atoi("10"); // avoid compiler optimizations
130 strncpy(buf, "012345678", n);
131 ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), "");
132}
133
134TEST(Fortify2_Clang_DeathTest, strncat2_fortified) {
135 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
136 char buf[10];
137 buf[0] = '\0';
138 size_t n = atoi("10"); // avoid compiler optimizations
139 ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), "");
140}
141
142TEST(Fortify2_Clang_DeathTest, strcat_fortified) {
143 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
144 char src[11];
145 strcpy(src, "0123456789");
146 char buf[10];
147 buf[0] = '\0';
148 ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), "");
149}
150
151TEST(Fortify2_Clang_DeathTest, memmove_fortified) {
152 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
153 char buf[20];
154 strcpy(buf, "0123456789");
155 size_t n = atoi("10");
156 ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), "");
157}
158
159TEST(Fortify2_Clang_DeathTest, memcpy_fortified) {
160 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
161 char bufa[10];
162 char bufb[10];
163 strcpy(bufa, "012345678");
164 size_t n = atoi("11");
165 ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
166}
167
168TEST(Fortify2_Clang_DeathTest, strncpy_fortified) {
169 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
170 char bufa[15];
171 char bufb[10];
172 strcpy(bufa, "01234567890123");
173 size_t n = strlen(bufa);
174 ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), "");
175}
176
177__BIONIC_FORTIFY_INLINE
178size_t test_fortify2_inline(char* buf) {
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700179 return __bos(buf);
Nick Kralevich16d1af12013-06-17 14:49:19 -0700180}
181
182TEST(Fortify2_Clang, fortify_inline) {
183 char buf[1024];
Nick Kralevich3cd4cac2013-06-19 10:25:44 -0700184 // no-op. Prints nothing. Needed to prevent the compiler
185 // from optimizing out buf.
186 buf[0] = '\0';
187 printf("%s", buf);
188 ASSERT_EQ(sizeof(buf), test_fortify2_inline(buf));
Nick Kralevich16d1af12013-06-17 14:49:19 -0700189}