xref: /aosp_15_r20/bionic/libc/private/bionic_globals.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #ifndef _PRIVATE_BIONIC_GLOBALS_H
30*8d67ca89SAndroid Build Coastguard Worker #define _PRIVATE_BIONIC_GLOBALS_H
31*8d67ca89SAndroid Build Coastguard Worker 
32*8d67ca89SAndroid Build Coastguard Worker #include <inttypes.h>
33*8d67ca89SAndroid Build Coastguard Worker #include <link.h>
34*8d67ca89SAndroid Build Coastguard Worker #include <platform/bionic/malloc.h>
35*8d67ca89SAndroid Build Coastguard Worker #include <pthread.h>
36*8d67ca89SAndroid Build Coastguard Worker #include <stdatomic.h>
37*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h>
38*8d67ca89SAndroid Build Coastguard Worker 
39*8d67ca89SAndroid Build Coastguard Worker #include "private/WriteProtected.h"
40*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_allocator.h"
41*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_elf_tls.h"
42*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_fdsan.h"
43*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_malloc_dispatch.h"
44*8d67ca89SAndroid Build Coastguard Worker #include "private/bionic_vdso.h"
45*8d67ca89SAndroid Build Coastguard Worker 
46*8d67ca89SAndroid Build Coastguard Worker struct libc_globals {
47*8d67ca89SAndroid Build Coastguard Worker   vdso_entry vdso[VDSO_END];
48*8d67ca89SAndroid Build Coastguard Worker   long setjmp_cookie;
49*8d67ca89SAndroid Build Coastguard Worker   uintptr_t heap_pointer_tag;
50*8d67ca89SAndroid Build Coastguard Worker   _Atomic(bool) decay_time_enabled;
51*8d67ca89SAndroid Build Coastguard Worker   _Atomic(bool) memtag;
52*8d67ca89SAndroid Build Coastguard Worker 
53*8d67ca89SAndroid Build Coastguard Worker   // In order to allow a complete switch between dispatch tables without
54*8d67ca89SAndroid Build Coastguard Worker   // the need for copying each function by function in the structure,
55*8d67ca89SAndroid Build Coastguard Worker   // use a single atomic pointer to switch.
56*8d67ca89SAndroid Build Coastguard Worker   // The current_dispatch_table pointer can only ever be set to a complete
57*8d67ca89SAndroid Build Coastguard Worker   // table. Any dispatch table that is pointed to by current_dispatch_table
58*8d67ca89SAndroid Build Coastguard Worker   // cannot be modified after that. If the pointer changes in the future,
59*8d67ca89SAndroid Build Coastguard Worker   // the old pointer must always stay valid.
60*8d67ca89SAndroid Build Coastguard Worker   // The malloc_dispatch_table is modified by malloc debug, malloc hooks,
61*8d67ca89SAndroid Build Coastguard Worker   // and heaprofd. Only one of these modes can be active at any given time.
62*8d67ca89SAndroid Build Coastguard Worker   _Atomic(const MallocDispatch*) current_dispatch_table;
63*8d67ca89SAndroid Build Coastguard Worker   // This pointer is only used by the allocation limit code when both a
64*8d67ca89SAndroid Build Coastguard Worker   // limit is enabled and some other hook is enabled at the same time.
65*8d67ca89SAndroid Build Coastguard Worker   _Atomic(const MallocDispatch*) default_dispatch_table;
66*8d67ca89SAndroid Build Coastguard Worker   MallocDispatch malloc_dispatch_table;
67*8d67ca89SAndroid Build Coastguard Worker };
68*8d67ca89SAndroid Build Coastguard Worker 
69*8d67ca89SAndroid Build Coastguard Worker struct memtag_dynamic_entries_t {
70*8d67ca89SAndroid Build Coastguard Worker   void* memtag_globals;
71*8d67ca89SAndroid Build Coastguard Worker   size_t memtag_globalssz;
72*8d67ca89SAndroid Build Coastguard Worker   bool has_memtag_mode;
73*8d67ca89SAndroid Build Coastguard Worker   unsigned memtag_mode;
74*8d67ca89SAndroid Build Coastguard Worker   bool memtag_heap;
75*8d67ca89SAndroid Build Coastguard Worker   bool memtag_stack;
76*8d67ca89SAndroid Build Coastguard Worker };
77*8d67ca89SAndroid Build Coastguard Worker 
78*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ extern WriteProtected<libc_globals> __libc_globals;
79*8d67ca89SAndroid Build Coastguard Worker // These cannot be in __libc_globals, because we cannot access the
80*8d67ca89SAndroid Build Coastguard Worker // WriteProtected in a thread-safe way.
81*8d67ca89SAndroid Build Coastguard Worker // See b/328256432.
82*8d67ca89SAndroid Build Coastguard Worker //
83*8d67ca89SAndroid Build Coastguard Worker // __libc_memtag_stack says whether stack MTE is enabled on the process, i.e.
84*8d67ca89SAndroid Build Coastguard Worker // whether the stack pages are mapped with PROT_MTE. This is always false if
85*8d67ca89SAndroid Build Coastguard Worker // MTE is disabled for the process (i.e. libc_globals.memtag is false).
86*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ extern _Atomic(bool) __libc_memtag_stack;
87*8d67ca89SAndroid Build Coastguard Worker // __libc_memtag_stack_abi says whether the process contains any code that was
88*8d67ca89SAndroid Build Coastguard Worker // compiled with memtag-stack. This is true even if the process does not have
89*8d67ca89SAndroid Build Coastguard Worker // MTE enabled (e.g. because it was overridden using MEMTAG_OPTIONS, or because
90*8d67ca89SAndroid Build Coastguard Worker // MTE is disabled for the device).
91*8d67ca89SAndroid Build Coastguard Worker // Code compiled with memtag-stack needs a stack history buffer in
92*8d67ca89SAndroid Build Coastguard Worker // TLS_SLOT_STACK_MTE, because the codegen will emit an unconditional
93*8d67ca89SAndroid Build Coastguard Worker // (to keep the code branchless) write to it.
94*8d67ca89SAndroid Build Coastguard Worker // Protected by g_heap_creation_lock.
95*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ extern bool __libc_memtag_stack_abi;
96*8d67ca89SAndroid Build Coastguard Worker 
97*8d67ca89SAndroid Build Coastguard Worker struct abort_msg_t;
98*8d67ca89SAndroid Build Coastguard Worker struct crash_detail_page_t;
99*8d67ca89SAndroid Build Coastguard Worker namespace gwp_asan {
100*8d67ca89SAndroid Build Coastguard Worker struct AllocatorState;
101*8d67ca89SAndroid Build Coastguard Worker struct AllocationMetadata;
102*8d67ca89SAndroid Build Coastguard Worker };  // namespace gwp_asan
103*8d67ca89SAndroid Build Coastguard Worker 
104*8d67ca89SAndroid Build Coastguard Worker // Globals shared between the dynamic linker and libc.so.
105*8d67ca89SAndroid Build Coastguard Worker struct libc_shared_globals {
106*8d67ca89SAndroid Build Coastguard Worker   // Construct the shared globals using a constexpr constructor to ensure that
107*8d67ca89SAndroid Build Coastguard Worker   // the object doesn't need dynamic initialization. The object is accessed
108*8d67ca89SAndroid Build Coastguard Worker   // before the dynamic linker has relocated itself.
libc_shared_globalslibc_shared_globals109*8d67ca89SAndroid Build Coastguard Worker   constexpr libc_shared_globals() {}
110*8d67ca89SAndroid Build Coastguard Worker 
111*8d67ca89SAndroid Build Coastguard Worker   FdTable fd_table;
112*8d67ca89SAndroid Build Coastguard Worker 
113*8d67ca89SAndroid Build Coastguard Worker   // When the linker is invoked on a binary (e.g. `linker64 /system/bin/date`),
114*8d67ca89SAndroid Build Coastguard Worker   // record the number of arguments passed to the linker itself rather than to
115*8d67ca89SAndroid Build Coastguard Worker   // the program it's loading. Typically 0, sometimes 1.
116*8d67ca89SAndroid Build Coastguard Worker   int initial_linker_arg_count = 0;
117*8d67ca89SAndroid Build Coastguard Worker 
118*8d67ca89SAndroid Build Coastguard Worker   ElfW(auxv_t)* auxv = nullptr;
119*8d67ca89SAndroid Build Coastguard Worker 
120*8d67ca89SAndroid Build Coastguard Worker   pthread_mutex_t abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
121*8d67ca89SAndroid Build Coastguard Worker   abort_msg_t* abort_msg = nullptr;
122*8d67ca89SAndroid Build Coastguard Worker 
123*8d67ca89SAndroid Build Coastguard Worker   StaticTlsLayout static_tls_layout;
124*8d67ca89SAndroid Build Coastguard Worker   TlsModules tls_modules;
125*8d67ca89SAndroid Build Coastguard Worker   BionicAllocator tls_allocator;
126*8d67ca89SAndroid Build Coastguard Worker 
127*8d67ca89SAndroid Build Coastguard Worker   // Values passed from libc.so to the loader.
128*8d67ca89SAndroid Build Coastguard Worker   void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = nullptr;
129*8d67ca89SAndroid Build Coastguard Worker   void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = nullptr;
130*8d67ca89SAndroid Build Coastguard Worker   void (*set_target_sdk_version_hook)(int target) = nullptr;
131*8d67ca89SAndroid Build Coastguard Worker 
132*8d67ca89SAndroid Build Coastguard Worker   // Values passed from the linker to libc.so.
133*8d67ca89SAndroid Build Coastguard Worker   const char* init_progname = nullptr;
134*8d67ca89SAndroid Build Coastguard Worker   char** init_environ = nullptr;
135*8d67ca89SAndroid Build Coastguard Worker 
136*8d67ca89SAndroid Build Coastguard Worker   const gwp_asan::AllocatorState* gwp_asan_state = nullptr;
137*8d67ca89SAndroid Build Coastguard Worker   const gwp_asan::AllocationMetadata* gwp_asan_metadata = nullptr;
138*8d67ca89SAndroid Build Coastguard Worker   bool (*debuggerd_needs_gwp_asan_recovery)(void* fault_addr) = nullptr;
139*8d67ca89SAndroid Build Coastguard Worker   void (*debuggerd_gwp_asan_pre_crash_report)(void* fault_addr) = nullptr;
140*8d67ca89SAndroid Build Coastguard Worker   void (*debuggerd_gwp_asan_post_crash_report)(void* fault_addr) = nullptr;
141*8d67ca89SAndroid Build Coastguard Worker 
142*8d67ca89SAndroid Build Coastguard Worker   const char* scudo_stack_depot = nullptr;
143*8d67ca89SAndroid Build Coastguard Worker   const char* scudo_region_info = nullptr;
144*8d67ca89SAndroid Build Coastguard Worker   const char* scudo_ring_buffer = nullptr;
145*8d67ca89SAndroid Build Coastguard Worker   size_t scudo_ring_buffer_size = 0;
146*8d67ca89SAndroid Build Coastguard Worker   size_t scudo_stack_depot_size = 0;
147*8d67ca89SAndroid Build Coastguard Worker 
148*8d67ca89SAndroid Build Coastguard Worker   HeapTaggingLevel initial_heap_tagging_level = M_HEAP_TAGGING_LEVEL_NONE;
149*8d67ca89SAndroid Build Coastguard Worker   // See comments for __libc_memtag_stack / __libc_memtag_stack_abi above.
150*8d67ca89SAndroid Build Coastguard Worker   bool initial_memtag_stack = false;
151*8d67ca89SAndroid Build Coastguard Worker   bool initial_memtag_stack_abi = false;
152*8d67ca89SAndroid Build Coastguard Worker   int64_t heap_tagging_upgrade_timer_sec = 0;
153*8d67ca89SAndroid Build Coastguard Worker 
154*8d67ca89SAndroid Build Coastguard Worker   void (*memtag_stack_dlopen_callback)() = nullptr;
155*8d67ca89SAndroid Build Coastguard Worker   pthread_mutex_t crash_detail_page_lock = PTHREAD_MUTEX_INITIALIZER;
156*8d67ca89SAndroid Build Coastguard Worker   crash_detail_page_t* crash_detail_page = nullptr;
157*8d67ca89SAndroid Build Coastguard Worker };
158*8d67ca89SAndroid Build Coastguard Worker 
159*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals();
160*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ bool __libc_mte_enabled();
161*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_mte(const memtag_dynamic_entries_t*, const void*, size_t,
162*8d67ca89SAndroid Build Coastguard Worker                                      uintptr_t);
163*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_mte_stack(void*);
164*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_fdsan();
165*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_fdtrack();
166*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_profiling_handlers();
167*8d67ca89SAndroid Build Coastguard Worker 
168*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals);
169*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_setjmp_cookie(libc_globals* globals);
170*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_vdso(libc_globals* globals);
171*8d67ca89SAndroid Build Coastguard Worker 
172*8d67ca89SAndroid Build Coastguard Worker #if defined(__i386__)
173*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ extern void* __libc_sysinfo;
174*8d67ca89SAndroid Build Coastguard Worker extern "C" __LIBC_HIDDEN__ void __libc_int0x80();
175*8d67ca89SAndroid Build Coastguard Worker __LIBC_HIDDEN__ void __libc_init_sysinfo();
176*8d67ca89SAndroid Build Coastguard Worker #endif
177*8d67ca89SAndroid Build Coastguard Worker 
178*8d67ca89SAndroid Build Coastguard Worker #endif
179