1 /* 2 * Copyright (C) 2019 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 #include <dlfcn.h> 18 #include <malloc.h> 19 #include <stdint.h> 20 21 #include <string> 22 23 #include <gtest/gtest.h> 24 25 #include "TestUtils.h" 26 27 namespace unwindstack { 28 TestCheckForLeaks(void (* unwind_func)(void *),void * data)29void TestCheckForLeaks(void (*unwind_func)(void*), void* data) { 30 static constexpr size_t kNumLeakLoops = 200; 31 static constexpr size_t kMaxAllowedLeakBytes = 32 * 1024; 32 33 size_t first_allocated_bytes = 0; 34 size_t last_allocated_bytes = 0; 35 for (size_t i = 0; i < kNumLeakLoops; i++) { 36 unwind_func(data); 37 38 size_t allocated_bytes = mallinfo().uordblks; 39 if (first_allocated_bytes == 0) { 40 first_allocated_bytes = allocated_bytes; 41 } else if (last_allocated_bytes > first_allocated_bytes) { 42 // Check that the memory did not increase too much over the first loop. 43 ASSERT_LE(last_allocated_bytes - first_allocated_bytes, kMaxAllowedLeakBytes) 44 << "Failed on loop " << i + 1; 45 } 46 last_allocated_bytes = allocated_bytes; 47 } 48 } 49 GetTestLibHandle()50void* GetTestLibHandle() { 51 std::string testlib(testing::internal::GetArgvs()[0]); 52 auto const value = testlib.find_last_of('/'); 53 if (value != std::string::npos) { 54 testlib = testlib.substr(0, value + 1); 55 } else { 56 testlib = ""; 57 } 58 testlib += "libunwindstack_local.so"; 59 60 return dlopen(testlib.c_str(), RTLD_NOW); 61 } 62 63 } // namespace unwindstack 64