1 /*
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
29 // This file perpetuates the mistakes of the past.
30
31 #include <ctype.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <pthread.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/resource.h>
41 #include <sys/syscall.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <unistd.h>
46 #include <wchar.h>
47
48 #include "platform/bionic/macros.h"
49
50 extern "C" {
51
52 // LP64 doesn't need to support any legacy cruft.
53 #if !defined(__LP64__)
54
55 // By the time any NDK-built code is running, there are plenty of threads.
56 int __isthreaded = 1;
57
58 // 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.
61 unsigned int __page_size = PAGE_SIZE;
62 unsigned int __page_shift = 12;
63
64 // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
__wait4(pid_t pid,int * status,int options,struct rusage * rusage)65 pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
66 return wait4(pid, status, options, rusage);
67 }
68
69 // TODO: does anything still need this?
__open()70 int __open() {
71 abort();
72 }
73
74 // TODO: does anything still need this?
__get_tls()75 void** __get_tls() {
76 #include "platform/bionic/tls.h"
77 return __get_tls();
78 }
79
80 // This non-standard function was in our <string.h> for some reason.
memswap(void * m1,void * m2,size_t n)81 void memswap(void* m1, void* m2, size_t n) {
82 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
pthread_attr_setstackaddr(pthread_attr_t *,void *)94 int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
95 // 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
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** stack_addr)100 int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
101 // 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
107 // Non-standard cruft that should only ever have been in system/core/toolbox.
strtotimeval(const char * str,struct timeval * ts)108 char* strtotimeval(const char* str, struct timeval* ts) {
109 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
digitval(int ch)134 static 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.
strntoumax(const char * nptr,char ** endptr,int base,size_t n)150 uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
151 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.
strntoimax(const char * nptr,char ** endptr,int base,size_t n)198 intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
199 return (intmax_t) strntoumax(nptr, endptr, base, n);
200 }
201
202 // POSIX calls this dprintf, but LP32 Android had fdprintf instead.
fdprintf(int fd,const char * fmt,...)203 int fdprintf(int fd, const char* fmt, ...) {
204 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.
vfdprintf(int fd,const char * fmt,va_list ap)212 int vfdprintf(int fd, const char* fmt, va_list ap) {
213 return vdprintf(fd, fmt, ap);
214 }
215
216 #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
221
222 // This used to be in <sys/atomics.h>.
__futex_wake(volatile void * ftx,int count)223 int __futex_wake(volatile void* ftx, int count) {
224 return __real_futex_wake(ftx, count);
225 }
226
227 // This used to be in <sys/atomics.h>.
__futex_wait(volatile void * ftx,int value,const struct timespec * timeout)228 int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
229 return __real_futex_wait(ftx, value, timeout);
230 }
231
232 // Unity's libmono uses this.
tkill(pid_t tid,int sig)233 int tkill(pid_t tid, int sig) {
234 return syscall(__NR_tkill, tid, sig);
235 }
236
237 // This was removed from POSIX 2008.
wcswcs(wchar_t * haystack,wchar_t * needle)238 wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
239 return wcsstr(haystack, needle);
240 }
241
242 // This was removed from POSIX 2008.
bsd_signal(int signum,sighandler_t handler)243 sighandler_t bsd_signal(int signum, sighandler_t handler) {
244 return signal(signum, handler);
245 }
246
247 // This was removed from POSIX 2008.
248 #undef bcopy
bcopy(const void * src,void * dst,size_t n)249 void bcopy(const void* src, void* dst, size_t n) {
250 memmove(dst, src, n);
251 }
252
253 // This was removed from POSIX 2008.
254 #undef bzero
bzero(void * dst,size_t n)255 void bzero(void* dst, size_t n) {
256 memset(dst, 0, n);
257 }
258
259 // sysv_signal() was never in POSIX.
260 extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags);
sysv_signal(int signum,sighandler_t handler)261 sighandler_t sysv_signal(int signum, sighandler_t handler) {
262 return _signal(signum, handler, SA_RESETHAND);
263 }
264
265 // This is a system call that was never in POSIX. Use readdir(3) instead.
266 int __getdents64(unsigned int, dirent*, unsigned int);
getdents(unsigned int fd,dirent * dirp,unsigned int count)267 int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
268 return __getdents64(fd, dirp, count);
269 }
270
271 // This is a BSDism that we never implemented correctly. Used by Firefox.
issetugid()272 int issetugid() {
273 return 0;
274 }
275
276 // This was removed from POSIX 2004.
wait3(int * status,int options,struct rusage * rusage)277 pid_t wait3(int* status, int options, struct rusage* rusage) {
278 return wait4(-1, status, options, rusage);
279 }
280
281 // This was removed from POSIX 2004.
getdtablesize()282 int getdtablesize() {
283 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
292 // A leaked BSD stdio implementation detail that's now a no-op.
__sinit()293 void __sinit() {}
294 int __sdidinit = 1;
295
296 // Only used by ftime, which was removed from POSIX 2008.
297 struct timeb {
298 time_t time;
299 unsigned short millitm;
300 short timezone;
301 short dstflag;
302 };
303
304 // This was removed from POSIX 2008.
ftime(struct timeb * tb)305 int ftime(struct timeb* tb) {
306 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
326 // This was removed from POSIX 2008.
index(const char * str,int ch)327 char* index(const char* str, int ch) {
328 return const_cast<char*>(strchr(str, ch));
329 }
330
331 // This was removed from BSD.
arc4random_stir(void)332 void arc4random_stir(void) {
333 // The current implementation stirs itself as needed.
334 }
335
336 // This was removed from BSD.
arc4random_addrandom(unsigned char *,int)337 void arc4random_addrandom(unsigned char*, int) {
338 // The current implementation adds randomness as needed.
339 }
340
341 // 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.
dlmalloc_usable_size(void * ptr)344 size_t dlmalloc_usable_size(void* ptr) {
345 return malloc_usable_size(ptr);
346 }
347
348 // In L we added a public pthread_gettid_np, but some apps were using the private API.
__pthread_gettid(pthread_t t)349 pid_t __pthread_gettid(pthread_t t) {
350 return pthread_gettid_np(t);
351 }
352
353 // Older versions of apportable used dlmalloc directly instead of malloc,
354 // so export this compatibility shim that simply calls malloc.
dlmalloc(size_t size)355 void* dlmalloc(size_t size) {
356 return malloc(size);
357 }
358
359 } // extern "C"
360
361 #define __get_thread __real_get_thread
362 #include "pthread_internal.h"
363 #undef __get_thread
364
365 extern "C" {
366
367 // Various third-party apps contain a backport of our pthread_rwlock implementation that uses this.
__get_thread()368 pthread_internal_t* __get_thread() {
369 return __real_get_thread();
370 }
371
372 // This one exists only for the LP32 NDK and is not present anywhere else.
373 extern long __set_errno_internal(int);
__set_errno(int n)374 long __set_errno(int n) {
375 return __set_errno_internal(n);
376 }
377
378 // Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems
379 // that use dlmalloc, be consistent and export them everywhere.
dlmalloc_inspect_all(void (*)(void *,void *,size_t,void *),void *)380 void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) {
381 }
dlmalloc_trim(size_t)382 int dlmalloc_trim(size_t) {
383 return 0;
384 }
385
386 // LP32's <stdio.h> had putw (but not getw).
putw(int value,FILE * fp)387 int putw(int value, FILE* fp) {
388 return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : EOF;
389 }
390
391 #endif // !defined (__LP64__)
392
393 } // extern "C"
394