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 #if !defined(LIBC_STATIC)
25 extern "C" void* native_bridge_calloc(size_t, size_t);
26 extern "C" void native_bridge_free(void*);
27 extern "C" struct mallinfo native_bridge_mallinfo();
28 extern "C" void* native_bridge_malloc(size_t);
29 extern "C" size_t native_bridge_malloc_usable_size(const void*);
30 extern "C" void* native_bridge_memalign(size_t, size_t);
31 extern "C" int native_bridge_posix_memalign(void**, size_t, size_t);
32 extern "C" void* native_bridge_realloc(void*, size_t);
33 extern "C" int native_bridge_malloc_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*);
34 extern "C" void native_bridge_malloc_disable();
35 extern "C" void native_bridge_malloc_enable();
36 extern "C" int native_bridge_mallopt(int, int);
37 extern "C" void* native_bridge_aligned_alloc(size_t, size_t);
38
39 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
40 extern "C" void* native_bridge_pvalloc(size_t);
41 extern "C" void* native_bridge_valloc(size_t);
42 #endif
43
44 extern "C" int native_bridge_malloc_info_helper(int options, int fd);
45
native_bridge_malloc_info(int options,FILE * fp)46 static int native_bridge_malloc_info(int options, FILE* fp) {
47 // FILE objects cannot cross architecture boundary!
48 // HACK: extract underlying file descriptor and use it instead.
49 // TODO(b/146494184): at the moment malloc_info succeeds but writes nothing to memory streams!
50 fflush(fp);
51 int fd = fileno(fp);
52 if (fd == -1) {
53 return 0;
54 }
55
56 return native_bridge_malloc_info_helper(options, fd);
57 }
58
malloc_init_impl(libc_globals * globals)59 static void malloc_init_impl(libc_globals* globals) {
60 static const MallocDispatch malloc_default_dispatch __attribute__((unused)) = {
61 native_bridge_calloc,
62 native_bridge_free,
63 native_bridge_mallinfo,
64 native_bridge_malloc,
65 native_bridge_malloc_usable_size,
66 native_bridge_memalign,
67 native_bridge_posix_memalign,
68 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
69 native_bridge_pvalloc,
70 #endif
71 native_bridge_realloc,
72 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
73 native_bridge_valloc,
74 #endif
75 native_bridge_malloc_iterate,
76 native_bridge_malloc_disable,
77 native_bridge_malloc_enable,
78 native_bridge_mallopt,
79 native_bridge_aligned_alloc,
80 native_bridge_malloc_info,
81 };
82
83 globals->malloc_dispatch_table = malloc_default_dispatch;
84 atomic_store(&globals->default_dispatch_table, &malloc_default_dispatch);
85 atomic_store(&globals->current_dispatch_table, &malloc_default_dispatch);
86 }
87
88 // Initializes memory allocation framework.
89 // This routine is called from __libc_init routines in libc_init_dynamic.cpp.
__libc_init_malloc(libc_globals * globals)90 __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
91 malloc_init_impl(globals);
92 }
93 #endif // !LIBC_STATIC
94