blob: d16270fc685805a559632ead3e0815316f24b070 [file] [log] [blame]
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
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
7 * are met:
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 copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <fcntl.h>
30#include <malloc.h>
31#include <stdlib.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34#include <unistd.h>
35
36#include <string>
37#include <vector>
38
39#include <gtest/gtest.h>
40
Christopher Ferris2b0638e2019-09-11 19:05:29 -070041#include <bionic/malloc.h>
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -080042#include <private/bionic_malloc_dispatch.h>
43#include <tests/utils.h>
44
45__BEGIN_DECLS
46
47void get_malloc_leak_info(uint8_t**, size_t*, size_t*, size_t*, size_t*);
48void free_malloc_leak_info(uint8_t*);
49int malloc_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*);
50void malloc_enable();
51void malloc_disable();
52ssize_t malloc_backtrace(void*, uintptr_t*, size_t);
53
54__END_DECLS
55
56class MallocHooksTest : public ::testing::Test {
57 protected:
58 void SetUp() override {
59 ASSERT_EQ(0, setenv("LIBC_HOOKS_ENABLE", "1", true));
60 initialized_ = false;
61 }
62
63 void TearDown() override {
64 if (initialized_) {
65 Reset();
66 }
67 ASSERT_EQ(0, unsetenv("LIBC_HOOKS_ENABLE"));
68 }
69
70 void Init() {
71 initialized_ = true;
72 malloc_hook_called_ = false;
73 free_hook_called_ = false;
74 realloc_hook_called_ = false;
75 memalign_hook_called_ = false;
76
77 void_arg_ = nullptr;
78
79 orig_malloc_hook_ = __malloc_hook;
80 orig_free_hook_ = __free_hook;
81 orig_realloc_hook_ = __realloc_hook;
82 orig_memalign_hook_ = __memalign_hook;
83 }
84
85 void Reset() {
86 __malloc_hook = orig_malloc_hook_;
87 __free_hook = orig_free_hook_;
88 __realloc_hook = orig_realloc_hook_;
89 __memalign_hook = orig_memalign_hook_;
90 }
91
92 void Exec(std::vector<const char*> args);
93 void RunTest(std::string test_name);
94
95 public:
96 bool initialized_;
97
98 int fd_;
99 std::string raw_output_;
100 pid_t pid_;
101
102 static bool malloc_hook_called_;
103 static bool free_hook_called_;
104 static bool realloc_hook_called_;
105 static bool memalign_hook_called_;
106 static const void* void_arg_;
107
108 static void* (*orig_malloc_hook_)(size_t, const void*);
109 static void (*orig_free_hook_)(void*, const void*);
110 static void* (*orig_realloc_hook_)(void*, size_t, const void*);
111 static void* (*orig_memalign_hook_)(size_t, size_t, const void*);
112
113 static void* test_malloc_hook(size_t, const void*);
114 static void* test_realloc_hook(void* ptr, size_t, const void*);
115 static void* test_memalign_hook(size_t, size_t, const void*);
116 static void test_free_hook(void*, const void*);
117};
118
119bool MallocHooksTest::malloc_hook_called_;
120bool MallocHooksTest::free_hook_called_;
121bool MallocHooksTest::realloc_hook_called_;
122bool MallocHooksTest::memalign_hook_called_;
123const void* MallocHooksTest::void_arg_;
124
125void* (*MallocHooksTest::orig_malloc_hook_)(size_t, const void*);
126void (*MallocHooksTest::orig_free_hook_)(void*, const void*);
127void* (*MallocHooksTest::orig_realloc_hook_)(void*, size_t, const void*);
128void* (*MallocHooksTest::orig_memalign_hook_)(size_t, size_t, const void*);
129
130static void GetExe(std::string* exe_name) {
131 char path[PATH_MAX];
132 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
133 ASSERT_TRUE(path_len >= 0);
134 *exe_name = std::string(path, path_len);
135}
136
137void MallocHooksTest::RunTest(std::string test_name) {
138 std::vector<const char*> args;
139 args.push_back("--gtest_also_run_disabled_tests");
140 std::string filter_arg("--gtest_filter=" + test_name);
141 args.push_back(filter_arg.c_str());
142
143 ExecTestHelper test;
144 test.Run(
145 [&]() {
146 std::string exe_name;
147 GetExe(&exe_name);
148 args.insert(args.begin(), exe_name.c_str());
149 args.push_back(nullptr);
150 execv(args[0], reinterpret_cast<char* const*>(const_cast<char**>(args.data())));
151 exit(1);
152 },
153 0, nullptr);
154}
155
156void* MallocHooksTest::test_malloc_hook(size_t size, const void* arg) {
157 malloc_hook_called_ = true;
158 void_arg_ = arg;
159 return orig_malloc_hook_(size, arg);
160}
161
162void* MallocHooksTest::test_realloc_hook(void* ptr, size_t size, const void* arg) {
163 realloc_hook_called_ = true;
164 void_arg_ = arg;
165 return orig_realloc_hook_(ptr, size, arg);
166}
167
168void* MallocHooksTest::test_memalign_hook(size_t alignment, size_t size, const void* arg) {
169 memalign_hook_called_ = true;
170 void_arg_ = arg;
171 return orig_memalign_hook_(alignment, size, arg);
172}
173
174void MallocHooksTest::test_free_hook(void* ptr, const void* arg) {
175 free_hook_called_ = true;
176 void_arg_ = arg;
177 return orig_free_hook_(ptr, arg);
178}
179
180TEST_F(MallocHooksTest, other_malloc_functions) {
Florian Mayer24f27622022-04-06 00:24:31 +0000181 SKIP_WITH_HWASAN; // HWASan does not implement mallinfo.
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800182 RunTest("*.DISABLED_other_malloc_functions");
183}
184
185// Call other intercepted functions to make sure there are no crashes.
186TEST_F(MallocHooksTest, DISABLED_other_malloc_functions) {
187 struct mallinfo info = mallinfo();
188 EXPECT_NE(0U, info.uordblks);
189
190 EXPECT_EQ(0, mallopt(-1000, -1000));
191
192 void* ptr = malloc(1024);
193 EXPECT_LE(1024U, malloc_usable_size(ptr));
194 free(ptr);
195}
196
197TEST_F(MallocHooksTest, extended_functions) {
198 RunTest("*.DISABLED_extended_functions");
199}
200
201TEST_F(MallocHooksTest, DISABLED_extended_functions) {
Christopher Ferris30659fd2019-04-15 19:01:08 -0700202 android_mallopt_leak_info_t leak_info;
203 ASSERT_TRUE(android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info)));
204 EXPECT_EQ(nullptr, leak_info.buffer);
205 EXPECT_EQ(0U, leak_info.overall_size);
206 EXPECT_EQ(0U, leak_info.info_size);
207 EXPECT_EQ(0U, leak_info.total_memory);
208 EXPECT_EQ(0U, leak_info.backtrace_size);
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800209
Christopher Ferris30659fd2019-04-15 19:01:08 -0700210 ASSERT_TRUE(android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info)));
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800211
212 malloc_enable();
213 malloc_disable();
214
215 EXPECT_EQ(0, malloc_iterate(0, 0, nullptr, nullptr));
216
217 // Malloc hooks doesn't support any backtracing.
218 EXPECT_EQ(0, malloc_backtrace(nullptr, nullptr, 10));
219}
220
221TEST_F(MallocHooksTest, malloc_hook) {
222 RunTest("*.DISABLED_malloc_hook");
223}
224
225TEST_F(MallocHooksTest, DISABLED_malloc_hook) {
226 Init();
227 ASSERT_TRUE(__malloc_hook != nullptr);
228 __malloc_hook = test_malloc_hook;
229
230 void* ptr = malloc(1024);
231 ASSERT_TRUE(ptr != nullptr);
232 write(0, ptr, 0);
233 free(ptr);
234
235 EXPECT_TRUE(malloc_hook_called_) << "The malloc hook was not called.";
236 EXPECT_TRUE(void_arg_ != nullptr) << "The malloc hook was called with a nullptr.";
237}
238
239TEST_F(MallocHooksTest, free_hook) {
240 RunTest("*.DISABLED_free_hook");
241}
242
243TEST_F(MallocHooksTest, DISABLED_free_hook) {
244 Init();
245 ASSERT_TRUE(__free_hook != nullptr);
246 __free_hook = test_free_hook;
247
248 void* ptr = malloc(1024);
249 ASSERT_TRUE(ptr != nullptr);
250 free(ptr);
251 write(0, ptr, 0);
252
253 EXPECT_TRUE(free_hook_called_) << "The free hook was not called.";
254 EXPECT_TRUE(void_arg_ != nullptr) << "The free hook was called with a nullptr.";
255}
256
257TEST_F(MallocHooksTest, realloc_hook) {
258 RunTest("*.DISABLED_realloc_hook");
259}
260
261TEST_F(MallocHooksTest, DISABLED_realloc_hook) {
262 Init();
263 ASSERT_TRUE(__realloc_hook != nullptr);
264 __realloc_hook = test_realloc_hook;
265
266 void* ptr = malloc(1024);
267 ASSERT_TRUE(ptr != nullptr);
268 ptr = realloc(ptr, 2048);
269 free(ptr);
270 write(0, ptr, 0);
271
272 EXPECT_TRUE(realloc_hook_called_) << "The realloc hook was not called.";
273 EXPECT_TRUE(void_arg_ != nullptr) << "The realloc hook was called with a nullptr.";
274}
275
276TEST_F(MallocHooksTest, memalign_hook) {
277 RunTest("*.DISABLED_memalign_hook");
278}
279
280TEST_F(MallocHooksTest, DISABLED_memalign_hook) {
281 Init();
282 ASSERT_TRUE(__memalign_hook != nullptr);
283 __memalign_hook = test_memalign_hook;
284
285 void* ptr = memalign(32, 1024);
286 ASSERT_TRUE(ptr != nullptr);
287 free(ptr);
288 write(0, ptr, 0);
289
290 EXPECT_TRUE(memalign_hook_called_) << "The memalign hook was not called.";
291 EXPECT_TRUE(void_arg_ != nullptr) << "The memalign hook was called with a nullptr.";
292}
293
294TEST_F(MallocHooksTest, posix_memalign_hook) {
295 RunTest("*.DISABLED_posix_memalign_hook");
296}
297
298TEST_F(MallocHooksTest, DISABLED_posix_memalign_hook) {
299 Init();
300 ASSERT_TRUE(__memalign_hook != nullptr);
301 __memalign_hook = test_memalign_hook;
302
303 void* ptr;
304 ASSERT_EQ(0, posix_memalign(&ptr, 32, 1024));
305 ASSERT_TRUE(ptr != nullptr);
306 free(ptr);
307 write(0, ptr, 0);
308
309 EXPECT_TRUE(memalign_hook_called_) << "The memalign hook was not called when running posix_memalign.";
310 EXPECT_TRUE(void_arg_ != nullptr) << "The memalign hook was called with a nullptr.";
311}
312
313TEST_F(MallocHooksTest, posix_memalign_hook_error) {
314 RunTest("*.DISABLED_posix_memalign_hook_error");
315}
316
317TEST_F(MallocHooksTest, DISABLED_posix_memalign_hook_error) {
318 Init();
319 ASSERT_TRUE(__memalign_hook != nullptr);
320 __memalign_hook = test_memalign_hook;
321
322 void* ptr;
323 ASSERT_EQ(EINVAL, posix_memalign(&ptr, 11, 1024));
324 write(0, ptr, 0);
325
326 EXPECT_FALSE(memalign_hook_called_)
327 << "The memalign hook was called when running posix_memalign with an error.";
328 EXPECT_FALSE(void_arg_ != nullptr)
329 << "The memalign hook was called with a nullptr with an error.";
330}
331
332TEST_F(MallocHooksTest, aligned_alloc_hook) {
333 RunTest("*.DISABLED_aligned_alloc_hook");
334}
335
336TEST_F(MallocHooksTest, DISABLED_aligned_alloc_hook) {
337 Init();
338 ASSERT_TRUE(__memalign_hook != nullptr);
339 __memalign_hook = test_memalign_hook;
340
341 void* ptr = aligned_alloc(32, 1024);
342 ASSERT_TRUE(ptr != nullptr);
343 free(ptr);
344 write(0, ptr, 0);
345
346 EXPECT_TRUE(memalign_hook_called_) << "The memalign hook was not called when running aligned_alloc.";
347 EXPECT_TRUE(void_arg_ != nullptr) << "The memalign hook was called with a nullptr.";
348}
349
350TEST_F(MallocHooksTest, aligned_alloc_hook_error) {
351 RunTest("*.DISABLED_aligned_alloc_hook_error");
352}
353
Pirama Arumuga Nainar44dd9fa2022-01-28 13:20:27 -0800354// Allow deliberate call with non-power-of-two alignment in test code.
355#pragma clang diagnostic push
356#pragma clang diagnostic ignored "-Wnon-power-of-two-alignment"
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800357TEST_F(MallocHooksTest, DISABLED_aligned_alloc_hook_error) {
358 Init();
359 ASSERT_TRUE(__memalign_hook != nullptr);
360 __memalign_hook = test_memalign_hook;
361
362 void* ptr = aligned_alloc(11, 1024);
363 ASSERT_TRUE(ptr == nullptr);
364 EXPECT_EQ(EINVAL, errno);
365 write(0, ptr, 0);
366
367 EXPECT_FALSE(memalign_hook_called_)
368 << "The memalign hook was called when running aligned_alloc with an error.";
369 EXPECT_FALSE(void_arg_ != nullptr)
370 << "The memalign hook was called with a nullptr with an error.";
371}
Pirama Arumuga Nainar44dd9fa2022-01-28 13:20:27 -0800372#pragma clang diagnostic pop
Christopher Ferrisd6a1dc22018-02-07 18:42:14 -0800373
374#if !defined(__LP64__)
375TEST_F(MallocHooksTest, pvalloc_hook) {
376 RunTest("*.DISABLED_pvalloc_hook");
377}
378
379extern "C" void* pvalloc(size_t);
380
381TEST_F(MallocHooksTest, DISABLED_pvalloc_hook) {
382 Init();
383 ASSERT_TRUE(__memalign_hook != nullptr);
384 __memalign_hook = test_memalign_hook;
385
386 void* ptr = pvalloc(1024);
387 ASSERT_TRUE(ptr != nullptr);
388 write(0, ptr, 0);
389 free(ptr);
390
391 EXPECT_TRUE(memalign_hook_called_) << "The memalign hook was not called for pvalloc.";
392 EXPECT_TRUE(void_arg_ != nullptr) << "The memalign hook was called with a nullptr.";
393}
394
395TEST_F(MallocHooksTest, valloc_hook) {
396 RunTest("*.DISABLED_valloc_hook");
397}
398
399extern "C" void* valloc(size_t);
400
401TEST_F(MallocHooksTest, DISABLED_valloc_hook) {
402 Init();
403 ASSERT_TRUE(__memalign_hook != nullptr);
404 __memalign_hook = test_memalign_hook;
405
406 void* ptr = valloc(1024);
407 ASSERT_TRUE(ptr != nullptr);
408 write(0, ptr, 0);
409 free(ptr);
410
411 EXPECT_TRUE(memalign_hook_called_) << "The memalign hook was not called for valloc.";
412 EXPECT_TRUE(void_arg_ != nullptr) << "The memalign hook was called with a nullptr.";
413}
414#endif