blob: 27f95e692779e523d5b4be5c1e11f60e151eb24a [file] [log] [blame]
John Reckdc87c522016-02-29 13:31:18 -08001/*
2 * Copyright (C) 2016 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 "gtest/gtest.h"
sergeyv8bd5edf2016-05-13 15:03:35 -070018#include "gmock/gmock.h"
John Reckdc87c522016-02-29 13:31:18 -080019
20#include "Caches.h"
sergeyv8bd5edf2016-05-13 15:03:35 -070021#include "debug/GlesDriver.h"
22#include "debug/NullGlesDriver.h"
John Reckdc87c522016-02-29 13:31:18 -080023#include "thread/TaskManager.h"
24#include "tests/common/TestUtils.h"
25
26#include <memunreachable/memunreachable.h>
27
28#include <cstdio>
29#include <iostream>
30#include <map>
31#include <unordered_set>
32#include <signal.h>
33#include <unistd.h>
34
35using namespace std;
36using namespace android;
37using namespace android::uirenderer;
38
39static auto CRASH_SIGNALS = {
40 SIGABRT,
41 SIGSEGV,
42 SIGBUS,
43};
44
45static map<int, struct sigaction> gSigChain;
46
47static void gtestSigHandler(int sig, siginfo_t* siginfo, void* context) {
48 auto testinfo = ::testing::UnitTest::GetInstance()->current_test_info();
49 printf("[ FAILED ] %s.%s\n", testinfo->test_case_name(),
50 testinfo->name());
51 printf("[ FATAL! ] Process crashed, aborting tests!\n");
52 fflush(stdout);
53
54 // restore the default sighandler and re-raise
55 struct sigaction sa = gSigChain[sig];
56 sigaction(sig, &sa, nullptr);
57 raise(sig);
58}
59
60static void logUnreachable(initializer_list<UnreachableMemoryInfo> infolist) {
61 // merge them all
62 UnreachableMemoryInfo merged;
63 unordered_set<uintptr_t> addrs;
64 merged.allocation_bytes = 0;
65 merged.leak_bytes = 0;
66 merged.num_allocations = 0;
67 merged.num_leaks = 0;
68 for (auto& info : infolist) {
69 // We'll be a little hazzy about these ones and just hope the biggest
70 // is the most accurate
71 merged.allocation_bytes = max(merged.allocation_bytes, info.allocation_bytes);
72 merged.num_allocations = max(merged.num_allocations, info.num_allocations);
73 for (auto& leak : info.leaks) {
74 if (addrs.find(leak.begin) == addrs.end()) {
75 merged.leaks.push_back(leak);
76 merged.num_leaks++;
77 merged.leak_bytes += leak.size;
78 addrs.insert(leak.begin);
79 }
80 }
81 }
82
83 // Now log the result
84 if (merged.num_leaks) {
85 cout << endl << "Leaked memory!" << endl;
John Reck88737a02016-03-08 11:03:35 -080086 if (!merged.leaks[0].backtrace.num_frames) {
John Reckdc87c522016-02-29 13:31:18 -080087 cout << "Re-run with 'setprop libc.debug.malloc.program hwui_unit_test'"
88 << endl << "and 'setprop libc.debug.malloc.options backtrace=8'"
89 << " to get backtraces" << endl;
90 }
91 cout << merged.ToString(false);
92 }
93}
94
95static void checkForLeaks() {
96 // TODO: Until we can shutdown the RT thread we need to do this in
97 // two passes as GetUnreachableMemory has limited insight into
98 // thread-local caches so some leaks will not be properly tagged as leaks
99 nsecs_t before = systemTime();
100 UnreachableMemoryInfo rtMemInfo;
101 TestUtils::runOnRenderThread([&rtMemInfo](renderthread::RenderThread& thread) {
102 if (Caches::hasInstance()) {
103 Caches::getInstance().tasks.stop();
104 }
105 // Check for leaks
106 if (!GetUnreachableMemory(rtMemInfo)) {
107 cerr << "Failed to get unreachable memory!" << endl;
108 return;
109 }
110 });
111 UnreachableMemoryInfo uiMemInfo;
112 if (!GetUnreachableMemory(uiMemInfo)) {
113 cerr << "Failed to get unreachable memory!" << endl;
114 return;
115 }
116 logUnreachable({rtMemInfo, uiMemInfo});
117 nsecs_t after = systemTime();
118 cout << "Leak check took " << ns2ms(after - before) << "ms" << endl;
119}
120
121int main(int argc, char* argv[]) {
122 // Register a crash handler
123 struct sigaction sa;
124 memset(&sa, 0, sizeof(sa));
125 sa.sa_sigaction = &gtestSigHandler;
126 sa.sa_flags = SA_SIGINFO;
127 for (auto sig : CRASH_SIGNALS) {
128 struct sigaction old_sa;
129 sigaction(sig, &sa, &old_sa);
130 gSigChain.insert(pair<int, struct sigaction>(sig, old_sa));
131 }
132
sergeyv8bd5edf2016-05-13 15:03:35 -0700133 // Replace the default GLES driver
134 debug::GlesDriver::replace(std::make_unique<debug::NullGlesDriver>());
135
John Reckdc87c522016-02-29 13:31:18 -0800136 // Run the tests
137 testing::InitGoogleTest(&argc, argv);
sergeyv8bd5edf2016-05-13 15:03:35 -0700138 testing::InitGoogleMock(&argc, argv);
139
John Reckdc87c522016-02-29 13:31:18 -0800140 int ret = RUN_ALL_TESTS();
141 checkForLeaks();
142 return ret;
143}
144