1 /*
2 * Copyright (C) 2018 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 <private/bionic_config.h>
18 #include <private/bionic_globals.h>
19
20 #include <stdio.h>
21
22 #include <async_safe/log.h>
23
24 #include "native_bridge_support/vdso/vdso.h"
25
26 #if !defined(LIBC_STATIC)
malloc_info_impl(int options,FILE * fp)27 static int malloc_info_impl(int options, FILE* fp) {
28 // FILE objects cannot cross architecture boundary!
29 // HACK: extract underlying file descriptor and use it instead.
30 // TODO(b/146494184): at the moment malloc_info succeeds but writes nothing to memory streams!
31 fflush(fp);
32 int fd = fileno(fp);
33 if (fd == -1) {
34 return 0;
35 }
36
37 typedef int (*fn_t)(int options, int fd);
38 static fn_t fn = reinterpret_cast<fn_t>(
39 native_bridge_find_proxy_library_symbol("libc.so", "native_bridge_malloc_info"));
40 return fn(options, fd);
41 }
42
malloc_init_impl(libc_globals * globals)43 static void malloc_init_impl(libc_globals* globals) {
44 static const MallocDispatch malloc_default_dispatch __attribute__((unused)) = {
45 reinterpret_cast<MallocCalloc>(native_bridge_find_proxy_library_symbol("libc.so", "calloc")),
46 reinterpret_cast<MallocFree>(native_bridge_find_proxy_library_symbol("libc.so", "free")),
47 reinterpret_cast<MallocMallinfo>(
48 native_bridge_find_proxy_library_symbol("libc.so", "mallinfo")),
49 reinterpret_cast<MallocMalloc>(native_bridge_find_proxy_library_symbol("libc.so", "malloc")),
50 reinterpret_cast<MallocMallocUsableSize>(
51 native_bridge_find_proxy_library_symbol("libc.so", "malloc_usable_size")),
52 reinterpret_cast<MallocMemalign>(
53 native_bridge_find_proxy_library_symbol("libc.so", "memalign")),
54 reinterpret_cast<MallocPosixMemalign>(
55 native_bridge_find_proxy_library_symbol("libc.so", "posix_memalign")),
56 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
57 reinterpret_cast<MallocPvalloc>(native_bridge_find_proxy_library_symbol("libc.so", "pvalloc")),
58 #endif
59 reinterpret_cast<MallocRealloc>(native_bridge_find_proxy_library_symbol("libc.so", "realloc")),
60 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
61 reinterpret_cast<MallocValloc>(native_bridge_find_proxy_library_symbol("libc.so", "valloc")),
62 #endif
63 reinterpret_cast<MallocIterate>(
64 native_bridge_find_proxy_library_symbol("libc.so", "malloc_iterate")),
65 reinterpret_cast<MallocMallocDisable>(
66 native_bridge_find_proxy_library_symbol("libc.so", "malloc_disable")),
67 reinterpret_cast<MallocMallocEnable>(
68 native_bridge_find_proxy_library_symbol("libc.so", "malloc_enable")),
69 reinterpret_cast<MallocMallopt>(native_bridge_find_proxy_library_symbol("libc.so", "mallopt")),
70 reinterpret_cast<MallocAlignedAlloc>(
71 native_bridge_find_proxy_library_symbol("libc.so", "aligned_alloc")),
72 malloc_info_impl,
73 };
74 globals->malloc_dispatch_table = malloc_default_dispatch;
75 globals->current_dispatch_table = &globals->malloc_dispatch_table;
76 }
77
78 // Initializes memory allocation framework.
79 // This routine is called from __libc_init routines in libc_init_dynamic.cpp.
__libc_init_malloc(libc_globals * globals)80 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
81 malloc_init_impl(globals);
82 }
83 #endif // !LIBC_STATIC
84