blob: bc06d9dc657a868fbda932c363cd5e44c800df84 [file] [log] [blame]
Elliott Hughes567a8de2013-10-24 17:14:55 -07001/*
2 * Copyright (C) 2013 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
Yabin Cui7fb680b2015-02-24 13:18:25 -080029// This file perpetuates the mistakes of the past.
Elliott Hughes567a8de2013-10-24 17:14:55 -070030
Elliott Hughese286cf62025-02-27 12:38:19 -080031// LP64 doesn't need to support any legacy cruft.
32#if !defined(__LP64__)
33
Sharjeel Khan9d29c7e2025-03-26 19:22:19 +000034#define __BIONIC_DISABLE_MALLOC_USABLE_SIZE_FORTIFY_WARNINGS
35
Elliott Hughesefbdb532014-04-07 15:17:19 -070036#include <ctype.h>
Elliott Hughesd1ead2a2014-06-06 15:24:20 -070037#include <dirent.h>
Elliott Hughes4c5891d2015-02-19 22:49:44 -080038#include <errno.h>
Elliott Hughesefbdb532014-04-07 15:17:19 -070039#include <inttypes.h>
Calin Juravlea4eafa62014-03-10 18:10:04 +000040#include <pthread.h>
Dan Albert205dd7d2014-06-04 10:14:19 -070041#include <signal.h>
Elliott Hughesfcac8ff2014-05-22 01:24:30 -070042#include <stdio.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070043#include <stdlib.h>
David 'Digit' Turner891dedb2014-06-13 12:28:11 +020044#include <string.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070045#include <sys/resource.h>
Elliott Hughesbd3a98c2014-05-24 17:19:36 -070046#include <sys/syscall.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070047#include <sys/time.h>
48#include <sys/types.h>
49#include <sys/wait.h>
50#include <unistd.h>
Dan Albert001f8f02014-06-04 09:53:06 -070051#include <wchar.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070052
Josh Gao4956c372019-12-19 16:35:51 -080053#include "platform/bionic/macros.h"
Yabin Cui7fb680b2015-02-24 13:18:25 -080054
Elliott Hughese286cf62025-02-27 12:38:19 -080055#define __futex_wake __real_futex_wake
56#define __futex_wait __real_futex_wait
57#include "private/bionic_futex.h"
58#undef __futex_wake
59#undef __futex_wait
Elliott Hughescfd5a462015-12-04 15:57:51 -080060
Elliott Hughese286cf62025-02-27 12:38:19 -080061#define __get_thread __real_get_thread
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080062#define __get_tls __real_get_tls
Elliott Hughese286cf62025-02-27 12:38:19 -080063#include "pthread_internal.h"
64#undef __get_thread
Elliott Hughese286cf62025-02-27 12:38:19 -080065#undef __get_tls
66
67extern "C" {
Yabin Cui7fb680b2015-02-24 13:18:25 -080068
Elliott Hughesdfcb82d2017-05-11 15:29:03 -070069// By the time any NDK-built code is running, there are plenty of threads.
70int __isthreaded = 1;
71
Elliott Hughescf346532020-07-31 10:35:03 -070072// These were accidentally declared in <unistd.h> because we used to inline
73// getpagesize() and __getpageshift(). Needed for backwards compatibility
74// with old NDK apps.
Elliott Hughescfd5a462015-12-04 15:57:51 -080075unsigned int __page_size = PAGE_SIZE;
76unsigned int __page_shift = 12;
Elliott Hughes567a8de2013-10-24 17:14:55 -070077
78// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
Elliott Hughescfd5a462015-12-04 15:57:51 -080079pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
Elliott Hughes567a8de2013-10-24 17:14:55 -070080 return wait4(pid, status, options, rusage);
81}
82
Elliott Hughes06209252013-11-06 16:20:54 -080083// TODO: does anything still need this?
Elliott Hughescfd5a462015-12-04 15:57:51 -080084int __open() {
Elliott Hughes567a8de2013-10-24 17:14:55 -070085 abort();
86}
87
Elliott Hughes06209252013-11-06 16:20:54 -080088// TODO: does anything still need this?
Elliott Hughescfd5a462015-12-04 15:57:51 -080089void** __get_tls() {
Elliott Hughese286cf62025-02-27 12:38:19 -080090 return __real_get_tls();
Elliott Hughes06209252013-11-06 16:20:54 -080091}
92
Elliott Hughes152b9de2014-03-10 15:54:40 -070093// This non-standard function was in our <string.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -080094void memswap(void* m1, void* m2, size_t n) {
Elliott Hughes152b9de2014-03-10 15:54:40 -070095 char* p = reinterpret_cast<char*>(m1);
96 char* p_end = p + n;
97 char* q = reinterpret_cast<char*>(m2);
98 while (p < p_end) {
99 char tmp = *p;
100 *p = *q;
101 *q = tmp;
102 p++;
103 q++;
104 }
105}
106
Elliott Hughescfd5a462015-12-04 15:57:51 -0800107int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
Calin Juravlea4eafa62014-03-10 18:10:04 +0000108 // This was removed from POSIX.1-2008, and is not implemented on bionic.
109 // Needed for ABI compatibility with the NDK.
110 return ENOSYS;
111}
112
Elliott Hughescfd5a462015-12-04 15:57:51 -0800113int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
Calin Juravlea4eafa62014-03-10 18:10:04 +0000114 // This was removed from POSIX.1-2008.
115 // Needed for ABI compatibility with the NDK.
116 *stack_addr = (char*)attr->stack_base + attr->stack_size;
117 return 0;
118}
119
Elliott Hughesefbdb532014-04-07 15:17:19 -0700120// Non-standard cruft that should only ever have been in system/core/toolbox.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800121char* strtotimeval(const char* str, struct timeval* ts) {
Elliott Hughesefbdb532014-04-07 15:17:19 -0700122 char* s;
123 ts->tv_sec = strtoumax(str, &s, 10);
124
125 long fractional_seconds = 0;
126 if (*s == '.') {
127 s++;
128 int count = 0;
129
130 // Read up to 6 digits (microseconds).
131 while (*s && isdigit(*s)) {
132 if (++count < 7) {
133 fractional_seconds = fractional_seconds*10 + (*s - '0');
134 }
135 s++;
136 }
137
138 for (; count < 6; count++) {
139 fractional_seconds *= 10;
140 }
141 }
142
143 ts->tv_usec = fractional_seconds;
144 return s;
145}
146
Elliott Hugheseae59022014-04-22 17:56:42 -0700147static inline int digitval(int ch) {
148 unsigned d;
149
150 d = (unsigned)(ch - '0');
151 if (d < 10) return (int)d;
152
153 d = (unsigned)(ch - 'a');
154 if (d < 6) return (int)(d+10);
155
156 d = (unsigned)(ch - 'A');
157 if (d < 6) return (int)(d+10);
158
159 return -1;
160}
161
162// This non-standard function was in our <inttypes.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800163uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
Elliott Hugheseae59022014-04-22 17:56:42 -0700164 const unsigned char* p = (const unsigned char *)nptr;
165 const unsigned char* end = p + n;
166 int minus = 0;
167 uintmax_t v = 0;
168 int d;
169
170 while (p < end && isspace(*p)) {
171 p++;
172 }
173
174 if (p < end) {
175 char c = p[0];
176 if (c == '-' || c == '+') {
177 minus = (c == '-');
178 p++;
179 }
180 }
181
182 if (base == 0) {
183 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
184 p += 2;
185 base = 16;
186 } else if (p+1 < end && p[0] == '0') {
187 p += 1;
188 base = 8;
189 } else {
190 base = 10;
191 }
192 } else if (base == 16) {
193 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
194 p += 2;
195 }
196 }
197
198 while (p < end && (d = digitval(*p)) >= 0 && d < base) {
199 v = v*base + d;
200 p += 1;
201 }
202
203 if (endptr) {
204 *endptr = (char*) p;
205 }
206
207 return minus ? -v : v;
208}
209
210// This non-standard function was in our <inttypes.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800211intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
Elliott Hugheseae59022014-04-22 17:56:42 -0700212 return (intmax_t) strntoumax(nptr, endptr, base, n);
213}
214
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700215// POSIX calls this dprintf, but LP32 Android had fdprintf instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800216int fdprintf(int fd, const char* fmt, ...) {
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700217 va_list ap;
218 va_start(ap, fmt);
219 int rc = vdprintf(fd, fmt, ap);
220 va_end(ap);
221 return rc;
222}
223
224// POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800225int vfdprintf(int fd, const char* fmt, va_list ap) {
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700226 return vdprintf(fd, fmt, ap);
227}
228
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700229// This used to be in <sys/atomics.h>.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800230int __futex_wake(volatile void* ftx, int count) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000231 return __real_futex_wake(ftx, count);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700232}
233
234// This used to be in <sys/atomics.h>.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800235int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000236 return __real_futex_wait(ftx, value, timeout);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700237}
238
Anthony King00170732014-05-24 16:47:14 +0000239// Unity's libmono uses this.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800240int tkill(pid_t tid, int sig) {
Anthony King00170732014-05-24 16:47:14 +0000241 return syscall(__NR_tkill, tid, sig);
242}
243
Elliott Hughes1628eb12014-08-06 10:47:33 -0700244// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800245wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
Dan Albert001f8f02014-06-04 09:53:06 -0700246 return wcsstr(haystack, needle);
247}
248
Dan Albert205dd7d2014-06-04 10:14:19 -0700249// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800250sighandler_t bsd_signal(int signum, sighandler_t handler) {
Dan Albert205dd7d2014-06-04 10:14:19 -0700251 return signal(signum, handler);
252}
253
Elliott Hughes76f89162015-01-26 13:34:58 -0800254// This was removed from POSIX 2008.
Logan Chienb33952c2019-08-27 20:50:24 -0700255#undef bcopy
256void bcopy(const void* src, void* dst, size_t n) {
Rohit Agrawald51a0b02015-12-05 12:39:54 -0800257 memmove(dst, src, n);
Elliott Hughes76f89162015-01-26 13:34:58 -0800258}
259
Elliott Hughes01d5b942016-03-02 17:18:18 -0800260// This was removed from POSIX 2008.
Logan Chienb33952c2019-08-27 20:50:24 -0700261#undef bzero
262void bzero(void* dst, size_t n) {
Elliott Hughes01d5b942016-03-02 17:18:18 -0800263 memset(dst, 0, n);
264}
265
Dan Albert205dd7d2014-06-04 10:14:19 -0700266// sysv_signal() was never in POSIX.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800267extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags);
268sighandler_t sysv_signal(int signum, sighandler_t handler) {
Dan Albert205dd7d2014-06-04 10:14:19 -0700269 return _signal(signum, handler, SA_RESETHAND);
270}
271
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700272// This is a system call that was never in POSIX. Use readdir(3) instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800273int __getdents64(unsigned int, dirent*, unsigned int);
274int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700275 return __getdents64(fd, dirp, count);
276}
277
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700278// This is a BSDism that we never implemented correctly. Used by Firefox.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800279int issetugid() {
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700280 return 0;
281}
282
Dan Albert8229ae42014-06-13 16:04:41 -0700283// This was removed from POSIX 2004.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800284pid_t wait3(int* status, int options, struct rusage* rusage) {
Dan Albert8229ae42014-06-13 16:04:41 -0700285 return wait4(-1, status, options, rusage);
286}
287
Dan Albert462abab2014-06-13 16:51:24 -0700288// This was removed from POSIX 2004.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800289int getdtablesize() {
Dan Albert462abab2014-06-13 16:51:24 -0700290 struct rlimit r;
291
292 if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
293 return sysconf(_SC_OPEN_MAX);
294 }
295
296 return r.rlim_cur;
297}
298
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800299// A leaked BSD stdio implementation detail that's now a no-op.
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800300void __sinit() {}
301int __sdidinit = 1;
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800302
Elliott Hughes1628eb12014-08-06 10:47:33 -0700303// Only used by ftime, which was removed from POSIX 2008.
Dan Albertac646752014-06-05 02:10:49 +0000304struct timeb {
305 time_t time;
306 unsigned short millitm;
307 short timezone;
308 short dstflag;
309};
310
311// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800312int ftime(struct timeb* tb) {
Dan Albertac646752014-06-05 02:10:49 +0000313 struct timeval tv;
314 struct timezone tz;
315
316 if (gettimeofday(&tv, &tz) < 0)
317 return -1;
318
319 tb->time = tv.tv_sec;
320 tb->millitm = (tv.tv_usec + 500) / 1000;
321
322 if (tb->millitm == 1000) {
323 ++tb->time;
324 tb->millitm = 0;
325 }
326
327 tb->timezone = tz.tz_minuteswest;
328 tb->dstflag = tz.tz_dsttime;
329
330 return 0;
331}
332
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200333// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800334char* index(const char* str, int ch) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700335 return const_cast<char*>(strchr(str, ch));
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200336}
337
Elliott Hughes5dea4722014-09-03 15:53:11 -0700338// This was removed from BSD.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800339void arc4random_stir(void) {
Elliott Hughes5dea4722014-09-03 15:53:11 -0700340 // The current implementation stirs itself as needed.
341}
342
Elliott Hughesfc829732014-09-08 10:25:33 -0700343// This was removed from BSD.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800344void arc4random_addrandom(unsigned char*, int) {
Elliott Hughesfc829732014-09-08 10:25:33 -0700345 // The current implementation adds randomness as needed.
346}
347
Christopher Ferrisf9035582014-09-05 16:39:22 -0700348// Old versions of the NDK did not export malloc_usable_size, but did
349// export dlmalloc_usable_size. We are moving away from dlmalloc in L
350// so make this call malloc_usable_size.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800351size_t dlmalloc_usable_size(void* ptr) {
Christopher Ferrisf9035582014-09-05 16:39:22 -0700352 return malloc_usable_size(ptr);
353}
354
Elliott Hughes0f001b62014-09-12 11:35:05 -0700355// In L we added a public pthread_gettid_np, but some apps were using the private API.
Dimitry Ivanovbba39542016-01-21 22:25:32 +0000356pid_t __pthread_gettid(pthread_t t) {
Elliott Hughes0f001b62014-09-12 11:35:05 -0700357 return pthread_gettid_np(t);
358}
359
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700360// Older versions of apportable used dlmalloc directly instead of malloc,
Christopher Ferrisf183f952014-10-08 22:48:20 -0700361// so export this compatibility shim that simply calls malloc.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800362void* dlmalloc(size_t size) {
Christopher Ferrisf183f952014-10-08 22:48:20 -0700363 return malloc(size);
364}
365
Yabin Cui2f836d42015-03-18 14:14:02 -0700366// Various third-party apps contain a backport of our pthread_rwlock implementation that uses this.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800367pthread_internal_t* __get_thread() {
Yabin Cui2f836d42015-03-18 14:14:02 -0700368 return __real_get_thread();
369}
370
Christopher Ferris9978a9a2015-10-29 18:11:32 -0700371// This one exists only for the LP32 NDK and is not present anywhere else.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800372extern long __set_errno_internal(int);
373long __set_errno(int n) {
Christopher Ferris9978a9a2015-10-29 18:11:32 -0700374 return __set_errno_internal(n);
375}
376
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700377// Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems
378// that use dlmalloc, be consistent and export them everywhere.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800379void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) {
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700380}
Elliott Hughescfd5a462015-12-04 15:57:51 -0800381int dlmalloc_trim(size_t) {
Elliott Hughesfb8fd502015-10-12 17:53:48 -0700382 return 0;
383}
Elliott Hughesfb8fd502015-10-12 17:53:48 -0700384
Elliott Hughes53cf3482016-08-09 13:06:41 -0700385// LP32's <stdio.h> had putw (but not getw).
Elliott Hughes53cf3482016-08-09 13:06:41 -0700386int putw(int value, FILE* fp) {
387 return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : EOF;
388}
Elliott Hughes5ffed9b2016-08-10 14:06:14 -0700389
Elliott Hughescfd5a462015-12-04 15:57:51 -0800390} // extern "C"
Elliott Hughese286cf62025-02-27 12:38:19 -0800391
392#endif // !defined (__LP64__)