Nick Kralevich | 16d1af1 | 2013-06-17 14:49:19 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 23 | struct 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. |
| 32 | TEST(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 | |
| 41 | TEST(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) |
| 55 | TEST(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) |
| 64 | TEST(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) |
| 73 | TEST(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) |
| 82 | TEST(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 | |
| 90 | TEST(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 | |
| 97 | TEST(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 | |
| 104 | TEST(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 | |
| 112 | TEST(Fortify2_Clang_DeathTest, strncat_fortified) { |
| 113 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 114 | char buf[10]; |
| 115 | size_t n = atoi("10"); // avoid compiler optimizations |
| 116 | strncpy(buf, "012345678", n); |
| 117 | ASSERT_EXIT(strncat(buf, "9", n), testing::KilledBySignal(SIGABRT), ""); |
| 118 | } |
| 119 | |
| 120 | TEST(Fortify2_Clang_DeathTest, strncat2_fortified) { |
| 121 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 122 | char buf[10]; |
| 123 | buf[0] = '\0'; |
| 124 | size_t n = atoi("10"); // avoid compiler optimizations |
| 125 | ASSERT_EXIT(strncat(buf, "0123456789", n), testing::KilledBySignal(SIGABRT), ""); |
| 126 | } |
| 127 | |
| 128 | TEST(Fortify2_Clang_DeathTest, strcat_fortified) { |
| 129 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 130 | char src[11]; |
| 131 | strcpy(src, "0123456789"); |
| 132 | char buf[10]; |
| 133 | buf[0] = '\0'; |
| 134 | ASSERT_EXIT(strcat(buf, src), testing::KilledBySignal(SIGABRT), ""); |
| 135 | } |
| 136 | |
| 137 | TEST(Fortify2_Clang_DeathTest, memmove_fortified) { |
| 138 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 139 | char buf[20]; |
| 140 | strcpy(buf, "0123456789"); |
| 141 | size_t n = atoi("10"); |
| 142 | ASSERT_EXIT(memmove(buf + 11, buf, n), testing::KilledBySignal(SIGABRT), ""); |
| 143 | } |
| 144 | |
| 145 | TEST(Fortify2_Clang_DeathTest, memcpy_fortified) { |
| 146 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 147 | char bufa[10]; |
| 148 | char bufb[10]; |
| 149 | strcpy(bufa, "012345678"); |
| 150 | size_t n = atoi("11"); |
| 151 | ASSERT_EXIT(memcpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 152 | } |
| 153 | |
| 154 | TEST(Fortify2_Clang_DeathTest, strncpy_fortified) { |
| 155 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
| 156 | char bufa[15]; |
| 157 | char bufb[10]; |
| 158 | strcpy(bufa, "01234567890123"); |
| 159 | size_t n = strlen(bufa); |
| 160 | ASSERT_EXIT(strncpy(bufb, bufa, n), testing::KilledBySignal(SIGABRT), ""); |
| 161 | } |
| 162 | |
| 163 | __BIONIC_FORTIFY_INLINE |
| 164 | size_t test_fortify2_inline(char* buf) { |
| 165 | return __bos(buf); |
| 166 | } |
| 167 | |
| 168 | TEST(Fortify2_Clang, fortify_inline) { |
| 169 | char buf[1024]; |
| 170 | ASSERT_EQ(test_fortify2_inline(buf), sizeof(buf)); |
| 171 | } |