Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2011, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // ---- |
| 32 | // Author: llib@google.com (Bill Clarke) |
| 33 | |
| 34 | #include "config_for_unittests.h" |
| 35 | #include <assert.h> |
| 36 | #include <stdio.h> |
| 37 | #ifdef HAVE_MMAP |
| 38 | #include <sys/mman.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_UNISTD_H |
| 41 | #include <unistd.h> // for sleep() |
| 42 | #endif |
| 43 | #include <algorithm> |
| 44 | #include <string> |
| 45 | #include <vector> |
| 46 | #include <gperftools/malloc_hook.h> |
| 47 | #include "malloc_hook-inl.h" |
| 48 | #include "base/logging.h" |
| 49 | #include "base/simple_mutex.h" |
| 50 | #include "base/sysinfo.h" |
| 51 | #include "tests/testutil.h" |
| 52 | |
| 53 | // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old |
| 54 | // form of the name instead. |
| 55 | #ifndef MAP_ANONYMOUS |
| 56 | # define MAP_ANONYMOUS MAP_ANON |
| 57 | #endif |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | using std::string; |
| 62 | using std::vector; |
| 63 | |
| 64 | vector<void (*)()> g_testlist; // the tests to run |
| 65 | |
| 66 | #define TEST(a, b) \ |
| 67 | struct Test_##a##_##b { \ |
| 68 | Test_##a##_##b() { g_testlist.push_back(&Run); } \ |
| 69 | static void Run(); \ |
| 70 | }; \ |
| 71 | static Test_##a##_##b g_test_##a##_##b; \ |
| 72 | void Test_##a##_##b::Run() |
| 73 | |
| 74 | |
| 75 | static int RUN_ALL_TESTS() { |
| 76 | vector<void (*)()>::const_iterator it; |
| 77 | for (it = g_testlist.begin(); it != g_testlist.end(); ++it) { |
| 78 | (*it)(); // The test will error-exit if there's a problem. |
| 79 | } |
| 80 | fprintf(stderr, "\nPassed %d tests\n\nPASS\n", |
| 81 | static_cast<int>(g_testlist.size())); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | void Sleep(int seconds) { |
| 86 | #ifdef _MSC_VER |
| 87 | _sleep(seconds * 1000); // Windows's _sleep takes milliseconds argument |
| 88 | #else |
| 89 | sleep(seconds); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | using std::min; |
| 94 | using base::internal::kHookListMaxValues; |
| 95 | |
| 96 | // Since HookList is a template and is defined in malloc_hook.cc, we can only |
| 97 | // use an instantiation of it from malloc_hook.cc. We then reinterpret those |
| 98 | // values as integers for testing. |
| 99 | typedef base::internal::HookList<MallocHook::NewHook> TestHookList; |
| 100 | |
| 101 | int TestHookList_Traverse(const TestHookList& list, uintptr_t* output_array, int n) { |
| 102 | MallocHook::NewHook values_as_hooks[kHookListMaxValues]; |
| 103 | int result = list.Traverse(values_as_hooks, min(n, kHookListMaxValues)); |
| 104 | for (int i = 0; i < result; ++i) { |
| 105 | output_array[i] = reinterpret_cast<const uintptr_t>(*values_as_hooks[i]); |
| 106 | } |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | bool TestHookList_Add(TestHookList* list, int val) { |
| 111 | return list->Add(reinterpret_cast<MallocHook::NewHook>(val)); |
| 112 | } |
| 113 | |
| 114 | bool TestHookList_Remove(TestHookList* list, int val) { |
| 115 | return list->Remove(reinterpret_cast<MallocHook::NewHook>(val)); |
| 116 | } |
| 117 | |
| 118 | // Note that this is almost the same as INIT_HOOK_LIST in malloc_hook.cc without |
| 119 | // the cast. |
| 120 | #define INIT_HOOK_LIST(initial_value) { 1, { initial_value } } |
| 121 | |
| 122 | TEST(HookListTest, InitialValueExists) { |
| 123 | TestHookList list = INIT_HOOK_LIST(69); |
| 124 | uintptr_t values[2] = { 0, 0 }; |
| 125 | EXPECT_EQ(1, TestHookList_Traverse(list, values, 2)); |
| 126 | EXPECT_EQ(69, values[0]); |
| 127 | EXPECT_EQ(1, list.priv_end); |
| 128 | } |
| 129 | |
| 130 | TEST(HookListTest, CanRemoveInitialValue) { |
| 131 | TestHookList list = INIT_HOOK_LIST(69); |
| 132 | ASSERT_TRUE(TestHookList_Remove(&list, 69)); |
| 133 | EXPECT_EQ(0, list.priv_end); |
| 134 | |
| 135 | uintptr_t values[2] = { 0, 0 }; |
| 136 | EXPECT_EQ(0, TestHookList_Traverse(list, values, 2)); |
| 137 | } |
| 138 | |
| 139 | TEST(HookListTest, AddAppends) { |
| 140 | TestHookList list = INIT_HOOK_LIST(69); |
| 141 | ASSERT_TRUE(TestHookList_Add(&list, 42)); |
| 142 | EXPECT_EQ(2, list.priv_end); |
| 143 | |
| 144 | uintptr_t values[2] = { 0, 0 }; |
| 145 | EXPECT_EQ(2, TestHookList_Traverse(list, values, 2)); |
| 146 | EXPECT_EQ(69, values[0]); |
| 147 | EXPECT_EQ(42, values[1]); |
| 148 | } |
| 149 | |
| 150 | TEST(HookListTest, RemoveWorksAndWillClearSize) { |
| 151 | TestHookList list = INIT_HOOK_LIST(69); |
| 152 | ASSERT_TRUE(TestHookList_Add(&list, 42)); |
| 153 | |
| 154 | ASSERT_TRUE(TestHookList_Remove(&list, 69)); |
| 155 | EXPECT_EQ(2, list.priv_end); |
| 156 | |
| 157 | uintptr_t values[2] = { 0, 0 }; |
| 158 | EXPECT_EQ(1, TestHookList_Traverse(list, values, 2)); |
| 159 | EXPECT_EQ(42, values[0]); |
| 160 | |
| 161 | ASSERT_TRUE(TestHookList_Remove(&list, 42)); |
| 162 | EXPECT_EQ(0, list.priv_end); |
| 163 | EXPECT_EQ(0, TestHookList_Traverse(list, values, 2)); |
| 164 | } |
| 165 | |
| 166 | TEST(HookListTest, AddPrependsAfterRemove) { |
| 167 | TestHookList list = INIT_HOOK_LIST(69); |
| 168 | ASSERT_TRUE(TestHookList_Add(&list, 42)); |
| 169 | |
| 170 | ASSERT_TRUE(TestHookList_Remove(&list, 69)); |
| 171 | EXPECT_EQ(2, list.priv_end); |
| 172 | |
| 173 | ASSERT_TRUE(TestHookList_Add(&list, 7)); |
| 174 | EXPECT_EQ(2, list.priv_end); |
| 175 | |
| 176 | uintptr_t values[2] = { 0, 0 }; |
| 177 | EXPECT_EQ(2, TestHookList_Traverse(list, values, 2)); |
| 178 | EXPECT_EQ(7, values[0]); |
| 179 | EXPECT_EQ(42, values[1]); |
| 180 | } |
| 181 | |
| 182 | TEST(HookListTest, InvalidAddRejected) { |
| 183 | TestHookList list = INIT_HOOK_LIST(69); |
| 184 | EXPECT_FALSE(TestHookList_Add(&list, 0)); |
| 185 | |
| 186 | uintptr_t values[2] = { 0, 0 }; |
| 187 | EXPECT_EQ(1, TestHookList_Traverse(list, values, 2)); |
| 188 | EXPECT_EQ(69, values[0]); |
| 189 | EXPECT_EQ(1, list.priv_end); |
| 190 | } |
| 191 | |
| 192 | TEST(HookListTest, FillUpTheList) { |
| 193 | TestHookList list = INIT_HOOK_LIST(69); |
| 194 | int num_inserts = 0; |
| 195 | while (TestHookList_Add(&list, ++num_inserts)) |
| 196 | ; |
| 197 | EXPECT_EQ(kHookListMaxValues, num_inserts); |
| 198 | EXPECT_EQ(kHookListMaxValues, list.priv_end); |
| 199 | |
| 200 | uintptr_t values[kHookListMaxValues + 1]; |
| 201 | EXPECT_EQ(kHookListMaxValues, TestHookList_Traverse(list, values, |
| 202 | kHookListMaxValues)); |
| 203 | EXPECT_EQ(69, values[0]); |
| 204 | for (int i = 1; i < kHookListMaxValues; ++i) { |
| 205 | EXPECT_EQ(i, values[i]); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void MultithreadedTestThread(TestHookList* list, int shift, |
| 210 | int thread_num) { |
| 211 | string message; |
| 212 | char buf[64]; |
| 213 | for (int i = 1; i < 1000; ++i) { |
| 214 | // In each loop, we insert a unique value, check it exists, remove it, and |
| 215 | // check it doesn't exist. We also record some stats to log at the end of |
| 216 | // each thread. Each insertion location and the length of the list is |
| 217 | // non-deterministic (except for the very first one, over all threads, and |
| 218 | // after the very last one the list should be empty). |
| 219 | int value = (i << shift) + thread_num; |
| 220 | EXPECT_TRUE(TestHookList_Add(list, value)); |
| 221 | sched_yield(); // Ensure some more interleaving. |
| 222 | uintptr_t values[kHookListMaxValues + 1]; |
| 223 | int num_values = TestHookList_Traverse(*list, values, kHookListMaxValues); |
| 224 | EXPECT_LT(0, num_values); |
| 225 | int value_index; |
| 226 | for (value_index = 0; |
| 227 | value_index < num_values && values[value_index] != value; |
| 228 | ++value_index) |
| 229 | ; |
| 230 | EXPECT_LT(value_index, num_values); // Should have found value. |
| 231 | snprintf(buf, sizeof(buf), "[%d/%d; ", value_index, num_values); |
| 232 | message += buf; |
| 233 | sched_yield(); |
| 234 | EXPECT_TRUE(TestHookList_Remove(list, value)); |
| 235 | sched_yield(); |
| 236 | num_values = TestHookList_Traverse(*list, values, kHookListMaxValues); |
| 237 | for (value_index = 0; |
| 238 | value_index < num_values && values[value_index] != value; |
| 239 | ++value_index) |
| 240 | ; |
| 241 | EXPECT_EQ(value_index, num_values); // Should not have found value. |
| 242 | snprintf(buf, sizeof(buf), "%d]", num_values); |
| 243 | message += buf; |
| 244 | sched_yield(); |
| 245 | } |
| 246 | fprintf(stderr, "thread %d: %s\n", thread_num, message.c_str()); |
| 247 | } |
| 248 | |
| 249 | static volatile int num_threads_remaining; |
| 250 | static TestHookList list = INIT_HOOK_LIST(69); |
| 251 | static Mutex threadcount_lock; |
| 252 | |
| 253 | void MultithreadedTestThreadRunner(int thread_num) { |
| 254 | // Wait for all threads to start running. |
| 255 | { |
| 256 | MutexLock ml(&threadcount_lock); |
| 257 | assert(num_threads_remaining > 0); |
| 258 | --num_threads_remaining; |
| 259 | |
| 260 | // We should use condvars and the like, but for this test, we'll |
| 261 | // go simple and busy-wait. |
| 262 | while (num_threads_remaining > 0) { |
| 263 | threadcount_lock.Unlock(); |
| 264 | Sleep(1); |
| 265 | threadcount_lock.Lock(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // shift is the smallest number such that (1<<shift) > kHookListMaxValues |
| 270 | int shift = 0; |
| 271 | for (int i = kHookListMaxValues; i > 0; i >>= 1) |
| 272 | shift += 1; |
| 273 | |
| 274 | MultithreadedTestThread(&list, shift, thread_num); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | TEST(HookListTest, MultithreadedTest) { |
| 279 | ASSERT_TRUE(TestHookList_Remove(&list, 69)); |
| 280 | ASSERT_EQ(0, list.priv_end); |
| 281 | |
| 282 | // Run kHookListMaxValues thread, each running MultithreadedTestThread. |
| 283 | // First, we need to set up the rest of the globals. |
| 284 | num_threads_remaining = kHookListMaxValues; // a global var |
| 285 | RunManyThreadsWithId(&MultithreadedTestThreadRunner, num_threads_remaining, |
| 286 | 1 << 15); |
| 287 | |
| 288 | uintptr_t values[kHookListMaxValues + 1]; |
| 289 | EXPECT_EQ(0, TestHookList_Traverse(list, values, kHookListMaxValues)); |
| 290 | EXPECT_EQ(0, list.priv_end); |
| 291 | } |
| 292 | |
| 293 | // We only do mmap-hooking on (some) linux systems. |
| 294 | #if defined(HAVE_MMAP) && defined(__linux) && \ |
| 295 | (defined(__i386__) || defined(__x86_64__) || defined(__PPC__)) |
| 296 | |
| 297 | int mmap_calls = 0; |
| 298 | int mmap_matching_calls = 0; |
| 299 | int munmap_calls = 0; |
| 300 | int munmap_matching_calls = 0; |
| 301 | const int kMmapMagicFd = 1; |
| 302 | void* const kMmapMagicPointer = reinterpret_cast<void*>(1); |
| 303 | |
| 304 | int MmapReplacement(const void* start, |
| 305 | size_t size, |
| 306 | int protection, |
| 307 | int flags, |
| 308 | int fd, |
| 309 | off_t offset, |
| 310 | void** result) { |
| 311 | ++mmap_calls; |
| 312 | if (fd == kMmapMagicFd) { |
| 313 | ++mmap_matching_calls; |
| 314 | *result = kMmapMagicPointer; |
| 315 | return true; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | int MunmapReplacement(const void* ptr, size_t size, int* result) { |
| 321 | ++munmap_calls; |
| 322 | if (ptr == kMmapMagicPointer) { |
| 323 | ++munmap_matching_calls; |
| 324 | *result = 0; |
| 325 | return true; |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | TEST(MallocMookTest, MmapReplacements) { |
| 331 | mmap_calls = mmap_matching_calls = munmap_calls = munmap_matching_calls = 0; |
| 332 | MallocHook::SetMmapReplacement(&MmapReplacement); |
| 333 | MallocHook::SetMunmapReplacement(&MunmapReplacement); |
| 334 | EXPECT_EQ(kMmapMagicPointer, mmap(NULL, 1, PROT_READ, MAP_PRIVATE, |
| 335 | kMmapMagicFd, 0)); |
| 336 | EXPECT_EQ(1, mmap_matching_calls); |
| 337 | |
| 338 | char* ptr = reinterpret_cast<char*>( |
| 339 | mmap(NULL, 1, PROT_READ | PROT_WRITE, |
| 340 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); |
| 341 | EXPECT_EQ(2, mmap_calls); |
| 342 | EXPECT_EQ(1, mmap_matching_calls); |
| 343 | ASSERT_NE(MAP_FAILED, ptr); |
| 344 | *ptr = 'a'; |
| 345 | |
| 346 | EXPECT_EQ(0, munmap(kMmapMagicPointer, 1)); |
| 347 | EXPECT_EQ(1, munmap_calls); |
| 348 | EXPECT_EQ(1, munmap_matching_calls); |
| 349 | |
| 350 | EXPECT_EQ(0, munmap(ptr, 1)); |
| 351 | EXPECT_EQ(2, munmap_calls); |
| 352 | EXPECT_EQ(1, munmap_matching_calls); |
| 353 | |
| 354 | // The DEATH test below is flaky, because we've just munmapped the memory, |
| 355 | // making it available for mmap()ing again. There is no guarantee that it |
| 356 | // will stay unmapped, and in fact it gets reused ~10% of the time. |
| 357 | // It the area is reused, then not only we don't die, but we also corrupt |
| 358 | // whoever owns that memory now. |
| 359 | // EXPECT_DEATH(*ptr = 'a', "SIGSEGV"); |
| 360 | } |
| 361 | #endif // #ifdef HAVE_MMAP && linux && ... |
| 362 | |
| 363 | } // namespace |
| 364 | |
| 365 | int main(int argc, char** argv) { |
| 366 | return RUN_ALL_TESTS(); |
| 367 | } |