blob: b15a3171f87249ec1bc5698ff1f7578d670b090a [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 Hughesefbdb532014-04-07 15:17:19 -070031#include <ctype.h>
Elliott Hughesd1ead2a2014-06-06 15:24:20 -070032#include <dirent.h>
Elliott Hughes4c5891d2015-02-19 22:49:44 -080033#include <errno.h>
Elliott Hughesefbdb532014-04-07 15:17:19 -070034#include <inttypes.h>
Calin Juravlea4eafa62014-03-10 18:10:04 +000035#include <pthread.h>
Dan Albert205dd7d2014-06-04 10:14:19 -070036#include <signal.h>
Elliott Hughesfcac8ff2014-05-22 01:24:30 -070037#include <stdio.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070038#include <stdlib.h>
David 'Digit' Turner891dedb2014-06-13 12:28:11 +020039#include <string.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070040#include <sys/resource.h>
Elliott Hughesbd3a98c2014-05-24 17:19:36 -070041#include <sys/syscall.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070042#include <sys/time.h>
43#include <sys/types.h>
44#include <sys/wait.h>
45#include <unistd.h>
Dan Albert001f8f02014-06-04 09:53:06 -070046#include <wchar.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070047
Josh Gao4956c372019-12-19 16:35:51 -080048#include "platform/bionic/macros.h"
Yabin Cui7fb680b2015-02-24 13:18:25 -080049
Elliott Hughescfd5a462015-12-04 15:57:51 -080050extern "C" {
51
Elliott Hughes5ffed9b2016-08-10 14:06:14 -070052// LP64 doesn't need to support any legacy cruft.
53#if !defined(__LP64__)
Yabin Cui7fb680b2015-02-24 13:18:25 -080054
Elliott Hughesdfcb82d2017-05-11 15:29:03 -070055// By the time any NDK-built code is running, there are plenty of threads.
56int __isthreaded = 1;
57
Elliott Hughescf346532020-07-31 10:35:03 -070058// These were accidentally declared in <unistd.h> because we used to inline
59// getpagesize() and __getpageshift(). Needed for backwards compatibility
60// with old NDK apps.
Elliott Hughescfd5a462015-12-04 15:57:51 -080061unsigned int __page_size = PAGE_SIZE;
62unsigned int __page_shift = 12;
Elliott Hughes567a8de2013-10-24 17:14:55 -070063
64// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
Elliott Hughescfd5a462015-12-04 15:57:51 -080065pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
Elliott Hughes567a8de2013-10-24 17:14:55 -070066 return wait4(pid, status, options, rusage);
67}
68
Elliott Hughes06209252013-11-06 16:20:54 -080069// TODO: does anything still need this?
Elliott Hughescfd5a462015-12-04 15:57:51 -080070int __open() {
Elliott Hughes567a8de2013-10-24 17:14:55 -070071 abort();
72}
73
Elliott Hughes06209252013-11-06 16:20:54 -080074// TODO: does anything still need this?
Elliott Hughescfd5a462015-12-04 15:57:51 -080075void** __get_tls() {
Christopher Ferrisc5d3a432019-09-25 17:50:36 -070076#include "platform/bionic/tls.h"
Elliott Hughes06209252013-11-06 16:20:54 -080077 return __get_tls();
78}
79
Elliott Hughes152b9de2014-03-10 15:54:40 -070080// This non-standard function was in our <string.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -080081void memswap(void* m1, void* m2, size_t n) {
Elliott Hughes152b9de2014-03-10 15:54:40 -070082 char* p = reinterpret_cast<char*>(m1);
83 char* p_end = p + n;
84 char* q = reinterpret_cast<char*>(m2);
85 while (p < p_end) {
86 char tmp = *p;
87 *p = *q;
88 *q = tmp;
89 p++;
90 q++;
91 }
92}
93
Elliott Hughescfd5a462015-12-04 15:57:51 -080094int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
Calin Juravlea4eafa62014-03-10 18:10:04 +000095 // This was removed from POSIX.1-2008, and is not implemented on bionic.
96 // Needed for ABI compatibility with the NDK.
97 return ENOSYS;
98}
99
Elliott Hughescfd5a462015-12-04 15:57:51 -0800100int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
Calin Juravlea4eafa62014-03-10 18:10:04 +0000101 // This was removed from POSIX.1-2008.
102 // Needed for ABI compatibility with the NDK.
103 *stack_addr = (char*)attr->stack_base + attr->stack_size;
104 return 0;
105}
106
Elliott Hughesefbdb532014-04-07 15:17:19 -0700107// Non-standard cruft that should only ever have been in system/core/toolbox.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800108char* strtotimeval(const char* str, struct timeval* ts) {
Elliott Hughesefbdb532014-04-07 15:17:19 -0700109 char* s;
110 ts->tv_sec = strtoumax(str, &s, 10);
111
112 long fractional_seconds = 0;
113 if (*s == '.') {
114 s++;
115 int count = 0;
116
117 // Read up to 6 digits (microseconds).
118 while (*s && isdigit(*s)) {
119 if (++count < 7) {
120 fractional_seconds = fractional_seconds*10 + (*s - '0');
121 }
122 s++;
123 }
124
125 for (; count < 6; count++) {
126 fractional_seconds *= 10;
127 }
128 }
129
130 ts->tv_usec = fractional_seconds;
131 return s;
132}
133
Elliott Hugheseae59022014-04-22 17:56:42 -0700134static inline int digitval(int ch) {
135 unsigned d;
136
137 d = (unsigned)(ch - '0');
138 if (d < 10) return (int)d;
139
140 d = (unsigned)(ch - 'a');
141 if (d < 6) return (int)(d+10);
142
143 d = (unsigned)(ch - 'A');
144 if (d < 6) return (int)(d+10);
145
146 return -1;
147}
148
149// This non-standard function was in our <inttypes.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800150uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
Elliott Hugheseae59022014-04-22 17:56:42 -0700151 const unsigned char* p = (const unsigned char *)nptr;
152 const unsigned char* end = p + n;
153 int minus = 0;
154 uintmax_t v = 0;
155 int d;
156
157 while (p < end && isspace(*p)) {
158 p++;
159 }
160
161 if (p < end) {
162 char c = p[0];
163 if (c == '-' || c == '+') {
164 minus = (c == '-');
165 p++;
166 }
167 }
168
169 if (base == 0) {
170 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
171 p += 2;
172 base = 16;
173 } else if (p+1 < end && p[0] == '0') {
174 p += 1;
175 base = 8;
176 } else {
177 base = 10;
178 }
179 } else if (base == 16) {
180 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
181 p += 2;
182 }
183 }
184
185 while (p < end && (d = digitval(*p)) >= 0 && d < base) {
186 v = v*base + d;
187 p += 1;
188 }
189
190 if (endptr) {
191 *endptr = (char*) p;
192 }
193
194 return minus ? -v : v;
195}
196
197// This non-standard function was in our <inttypes.h> for some reason.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800198intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
Elliott Hugheseae59022014-04-22 17:56:42 -0700199 return (intmax_t) strntoumax(nptr, endptr, base, n);
200}
201
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700202// POSIX calls this dprintf, but LP32 Android had fdprintf instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800203int fdprintf(int fd, const char* fmt, ...) {
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700204 va_list ap;
205 va_start(ap, fmt);
206 int rc = vdprintf(fd, fmt, ap);
207 va_end(ap);
208 return rc;
209}
210
211// POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800212int vfdprintf(int fd, const char* fmt, va_list ap) {
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700213 return vdprintf(fd, fmt, ap);
214}
215
Elliott Hughesb30aff42014-05-28 19:35:33 +0000216#define __futex_wake __real_futex_wake
217#define __futex_wait __real_futex_wait
218#include "private/bionic_futex.h"
219#undef __futex_wake
220#undef __futex_wait
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700221
222// This used to be in <sys/atomics.h>.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800223int __futex_wake(volatile void* ftx, int count) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000224 return __real_futex_wake(ftx, count);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700225}
226
227// This used to be in <sys/atomics.h>.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800228int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000229 return __real_futex_wait(ftx, value, timeout);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700230}
231
Anthony King00170732014-05-24 16:47:14 +0000232// Unity's libmono uses this.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800233int tkill(pid_t tid, int sig) {
Anthony King00170732014-05-24 16:47:14 +0000234 return syscall(__NR_tkill, tid, sig);
235}
236
Elliott Hughes1628eb12014-08-06 10:47:33 -0700237// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800238wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
Dan Albert001f8f02014-06-04 09:53:06 -0700239 return wcsstr(haystack, needle);
240}
241
Dan Albert205dd7d2014-06-04 10:14:19 -0700242// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800243sighandler_t bsd_signal(int signum, sighandler_t handler) {
Dan Albert205dd7d2014-06-04 10:14:19 -0700244 return signal(signum, handler);
245}
246
Elliott Hughes76f89162015-01-26 13:34:58 -0800247// This was removed from POSIX 2008.
Logan Chienb33952c2019-08-27 20:50:24 -0700248#undef bcopy
249void bcopy(const void* src, void* dst, size_t n) {
Rohit Agrawald51a0b02015-12-05 12:39:54 -0800250 memmove(dst, src, n);
Elliott Hughes76f89162015-01-26 13:34:58 -0800251}
252
Elliott Hughes01d5b942016-03-02 17:18:18 -0800253// This was removed from POSIX 2008.
Logan Chienb33952c2019-08-27 20:50:24 -0700254#undef bzero
255void bzero(void* dst, size_t n) {
Elliott Hughes01d5b942016-03-02 17:18:18 -0800256 memset(dst, 0, n);
257}
258
Dan Albert205dd7d2014-06-04 10:14:19 -0700259// sysv_signal() was never in POSIX.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800260extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags);
261sighandler_t sysv_signal(int signum, sighandler_t handler) {
Dan Albert205dd7d2014-06-04 10:14:19 -0700262 return _signal(signum, handler, SA_RESETHAND);
263}
264
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700265// This is a system call that was never in POSIX. Use readdir(3) instead.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800266int __getdents64(unsigned int, dirent*, unsigned int);
267int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700268 return __getdents64(fd, dirp, count);
269}
270
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700271// This is a BSDism that we never implemented correctly. Used by Firefox.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800272int issetugid() {
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700273 return 0;
274}
275
Dan Albert8229ae42014-06-13 16:04:41 -0700276// This was removed from POSIX 2004.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800277pid_t wait3(int* status, int options, struct rusage* rusage) {
Dan Albert8229ae42014-06-13 16:04:41 -0700278 return wait4(-1, status, options, rusage);
279}
280
Dan Albert462abab2014-06-13 16:51:24 -0700281// This was removed from POSIX 2004.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800282int getdtablesize() {
Dan Albert462abab2014-06-13 16:51:24 -0700283 struct rlimit r;
284
285 if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
286 return sysconf(_SC_OPEN_MAX);
287 }
288
289 return r.rlim_cur;
290}
291
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800292// A leaked BSD stdio implementation detail that's now a no-op.
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800293void __sinit() {}
294int __sdidinit = 1;
Elliott Hughesbb46afd2015-12-04 18:03:12 -0800295
Elliott Hughes1628eb12014-08-06 10:47:33 -0700296// Only used by ftime, which was removed from POSIX 2008.
Dan Albertac646752014-06-05 02:10:49 +0000297struct timeb {
298 time_t time;
299 unsigned short millitm;
300 short timezone;
301 short dstflag;
302};
303
304// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800305int ftime(struct timeb* tb) {
Dan Albertac646752014-06-05 02:10:49 +0000306 struct timeval tv;
307 struct timezone tz;
308
309 if (gettimeofday(&tv, &tz) < 0)
310 return -1;
311
312 tb->time = tv.tv_sec;
313 tb->millitm = (tv.tv_usec + 500) / 1000;
314
315 if (tb->millitm == 1000) {
316 ++tb->time;
317 tb->millitm = 0;
318 }
319
320 tb->timezone = tz.tz_minuteswest;
321 tb->dstflag = tz.tz_dsttime;
322
323 return 0;
324}
325
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200326// This was removed from POSIX 2008.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800327char* index(const char* str, int ch) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700328 return const_cast<char*>(strchr(str, ch));
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200329}
330
Elliott Hughes5dea4722014-09-03 15:53:11 -0700331// This was removed from BSD.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800332void arc4random_stir(void) {
Elliott Hughes5dea4722014-09-03 15:53:11 -0700333 // The current implementation stirs itself as needed.
334}
335
Elliott Hughesfc829732014-09-08 10:25:33 -0700336// This was removed from BSD.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800337void arc4random_addrandom(unsigned char*, int) {
Elliott Hughesfc829732014-09-08 10:25:33 -0700338 // The current implementation adds randomness as needed.
339}
340
Christopher Ferrisf9035582014-09-05 16:39:22 -0700341// Old versions of the NDK did not export malloc_usable_size, but did
342// export dlmalloc_usable_size. We are moving away from dlmalloc in L
343// so make this call malloc_usable_size.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800344size_t dlmalloc_usable_size(void* ptr) {
Christopher Ferrisf9035582014-09-05 16:39:22 -0700345 return malloc_usable_size(ptr);
346}
347
Elliott Hughes0f001b62014-09-12 11:35:05 -0700348// In L we added a public pthread_gettid_np, but some apps were using the private API.
Dimitry Ivanovbba39542016-01-21 22:25:32 +0000349pid_t __pthread_gettid(pthread_t t) {
Elliott Hughes0f001b62014-09-12 11:35:05 -0700350 return pthread_gettid_np(t);
351}
352
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700353// Older versions of apportable used dlmalloc directly instead of malloc,
Christopher Ferrisf183f952014-10-08 22:48:20 -0700354// so export this compatibility shim that simply calls malloc.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800355void* dlmalloc(size_t size) {
Christopher Ferrisf183f952014-10-08 22:48:20 -0700356 return malloc(size);
357}
358
Ryan Prichard16455b52019-01-18 01:00:59 -0800359} // extern "C"
360
Yabin Cui2f836d42015-03-18 14:14:02 -0700361#define __get_thread __real_get_thread
362#include "pthread_internal.h"
363#undef __get_thread
Ryan Prichard16455b52019-01-18 01:00:59 -0800364
365extern "C" {
366
Yabin Cui2f836d42015-03-18 14:14:02 -0700367// Various third-party apps contain a backport of our pthread_rwlock implementation that uses this.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800368pthread_internal_t* __get_thread() {
Yabin Cui2f836d42015-03-18 14:14:02 -0700369 return __real_get_thread();
370}
371
Christopher Ferris9978a9a2015-10-29 18:11:32 -0700372// This one exists only for the LP32 NDK and is not present anywhere else.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800373extern long __set_errno_internal(int);
374long __set_errno(int n) {
Christopher Ferris9978a9a2015-10-29 18:11:32 -0700375 return __set_errno_internal(n);
376}
377
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700378// Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems
379// that use dlmalloc, be consistent and export them everywhere.
Elliott Hughescfd5a462015-12-04 15:57:51 -0800380void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) {
Christopher Ferrisf9554a12015-06-05 17:12:17 -0700381}
Elliott Hughescfd5a462015-12-04 15:57:51 -0800382int dlmalloc_trim(size_t) {
Elliott Hughesfb8fd502015-10-12 17:53:48 -0700383 return 0;
384}
Elliott Hughesfb8fd502015-10-12 17:53:48 -0700385
Elliott Hughes53cf3482016-08-09 13:06:41 -0700386// LP32's <stdio.h> had putw (but not getw).
Elliott Hughes53cf3482016-08-09 13:06:41 -0700387int putw(int value, FILE* fp) {
388 return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : EOF;
389}
Elliott Hughes5ffed9b2016-08-10 14:06:14 -0700390
391#endif // !defined (__LP64__)
Elliott Hughes53cf3482016-08-09 13:06:41 -0700392
Elliott Hughescfd5a462015-12-04 15:57:51 -0800393} // extern "C"