xref: /aosp_15_r20/system/unwinding/libunwindstack/tests/TestUtils.cpp (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1*eb293b8fSAndroid Build Coastguard Worker /*
2*eb293b8fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*eb293b8fSAndroid Build Coastguard Worker  *
4*eb293b8fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*eb293b8fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*eb293b8fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*eb293b8fSAndroid Build Coastguard Worker  *
8*eb293b8fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*eb293b8fSAndroid Build Coastguard Worker  *
10*eb293b8fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*eb293b8fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*eb293b8fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*eb293b8fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*eb293b8fSAndroid Build Coastguard Worker  * limitations under the License.
15*eb293b8fSAndroid Build Coastguard Worker  */
16*eb293b8fSAndroid Build Coastguard Worker 
17*eb293b8fSAndroid Build Coastguard Worker #include <dlfcn.h>
18*eb293b8fSAndroid Build Coastguard Worker #include <malloc.h>
19*eb293b8fSAndroid Build Coastguard Worker #include <stdint.h>
20*eb293b8fSAndroid Build Coastguard Worker 
21*eb293b8fSAndroid Build Coastguard Worker #include <string>
22*eb293b8fSAndroid Build Coastguard Worker 
23*eb293b8fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
24*eb293b8fSAndroid Build Coastguard Worker 
25*eb293b8fSAndroid Build Coastguard Worker #include "TestUtils.h"
26*eb293b8fSAndroid Build Coastguard Worker 
27*eb293b8fSAndroid Build Coastguard Worker namespace unwindstack {
28*eb293b8fSAndroid Build Coastguard Worker 
TestCheckForLeaks(void (* unwind_func)(void *),void * data)29*eb293b8fSAndroid Build Coastguard Worker void TestCheckForLeaks(void (*unwind_func)(void*), void* data) {
30*eb293b8fSAndroid Build Coastguard Worker   static constexpr size_t kNumLeakLoops = 200;
31*eb293b8fSAndroid Build Coastguard Worker   static constexpr size_t kMaxAllowedLeakBytes = 32 * 1024;
32*eb293b8fSAndroid Build Coastguard Worker 
33*eb293b8fSAndroid Build Coastguard Worker   size_t first_allocated_bytes = 0;
34*eb293b8fSAndroid Build Coastguard Worker   size_t last_allocated_bytes = 0;
35*eb293b8fSAndroid Build Coastguard Worker   for (size_t i = 0; i < kNumLeakLoops; i++) {
36*eb293b8fSAndroid Build Coastguard Worker     unwind_func(data);
37*eb293b8fSAndroid Build Coastguard Worker 
38*eb293b8fSAndroid Build Coastguard Worker     size_t allocated_bytes = mallinfo().uordblks;
39*eb293b8fSAndroid Build Coastguard Worker     if (first_allocated_bytes == 0) {
40*eb293b8fSAndroid Build Coastguard Worker       first_allocated_bytes = allocated_bytes;
41*eb293b8fSAndroid Build Coastguard Worker     } else if (last_allocated_bytes > first_allocated_bytes) {
42*eb293b8fSAndroid Build Coastguard Worker       // Check that the memory did not increase too much over the first loop.
43*eb293b8fSAndroid Build Coastguard Worker       ASSERT_LE(last_allocated_bytes - first_allocated_bytes, kMaxAllowedLeakBytes)
44*eb293b8fSAndroid Build Coastguard Worker           << "Failed on loop " << i + 1;
45*eb293b8fSAndroid Build Coastguard Worker     }
46*eb293b8fSAndroid Build Coastguard Worker     last_allocated_bytes = allocated_bytes;
47*eb293b8fSAndroid Build Coastguard Worker   }
48*eb293b8fSAndroid Build Coastguard Worker }
49*eb293b8fSAndroid Build Coastguard Worker 
GetTestLibHandle()50*eb293b8fSAndroid Build Coastguard Worker void* GetTestLibHandle() {
51*eb293b8fSAndroid Build Coastguard Worker   std::string testlib(testing::internal::GetArgvs()[0]);
52*eb293b8fSAndroid Build Coastguard Worker   auto const value = testlib.find_last_of('/');
53*eb293b8fSAndroid Build Coastguard Worker   if (value != std::string::npos) {
54*eb293b8fSAndroid Build Coastguard Worker     testlib = testlib.substr(0, value + 1);
55*eb293b8fSAndroid Build Coastguard Worker   } else {
56*eb293b8fSAndroid Build Coastguard Worker     testlib = "";
57*eb293b8fSAndroid Build Coastguard Worker   }
58*eb293b8fSAndroid Build Coastguard Worker   testlib += "libunwindstack_local.so";
59*eb293b8fSAndroid Build Coastguard Worker 
60*eb293b8fSAndroid Build Coastguard Worker   return dlopen(testlib.c_str(), RTLD_NOW);
61*eb293b8fSAndroid Build Coastguard Worker }
62*eb293b8fSAndroid Build Coastguard Worker 
63*eb293b8fSAndroid Build Coastguard Worker }  // namespace unwindstack
64