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