• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // LP64 doesn't need to support any legacy cruft.
32 #if !defined(__LP64__)
33 
34 #include <ctype.h>
35 #include <dirent.h>
36 #include <errno.h>
37 #include <inttypes.h>
38 #include <pthread.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/resource.h>
44 #include <sys/syscall.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <sys/wait.h>
48 #include <unistd.h>
49 #include <wchar.h>
50 
51 #include "platform/bionic/macros.h"
52 
53 #define __futex_wake __real_futex_wake
54 #define __futex_wait __real_futex_wait
55 #include "private/bionic_futex.h"
56 #undef __futex_wake
57 #undef __futex_wait
58 
59 #define __get_thread __real_get_thread
60 #define __get_tls __real_get_tls
61 #include "pthread_internal.h"
62 #undef __get_thread
63 #undef __get_tls
64 
65 extern "C" {
66 
67 // By the time any NDK-built code is running, there are plenty of threads.
68 int __isthreaded = 1;
69 
70 // These were accidentally declared in <unistd.h> because we used to inline
71 // getpagesize() and __getpageshift(). Needed for backwards compatibility
72 // with old NDK apps.
73 unsigned int __page_size = PAGE_SIZE;
74 unsigned int __page_shift = 12;
75 
76 // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
__wait4(pid_t pid,int * status,int options,struct rusage * rusage)77 pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
78   return wait4(pid, status, options, rusage);
79 }
80 
81 // TODO: does anything still need this?
__open()82 int __open() {
83   abort();
84 }
85 
86 // TODO: does anything still need this?
__get_tls()87 void** __get_tls() {
88   return __real_get_tls();
89 }
90 
91 // This non-standard function was in our <string.h> for some reason.
memswap(void * m1,void * m2,size_t n)92 void memswap(void* m1, void* m2, size_t n) {
93   char* p = reinterpret_cast<char*>(m1);
94   char* p_end = p + n;
95   char* q = reinterpret_cast<char*>(m2);
96   while (p < p_end) {
97     char tmp = *p;
98     *p = *q;
99     *q = tmp;
100     p++;
101     q++;
102   }
103 }
104 
pthread_attr_setstackaddr(pthread_attr_t *,void *)105 int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
106   // This was removed from POSIX.1-2008, and is not implemented on bionic.
107   // Needed for ABI compatibility with the NDK.
108   return ENOSYS;
109 }
110 
pthread_attr_getstackaddr(const pthread_attr_t * attr,void ** stack_addr)111 int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
112   // This was removed from POSIX.1-2008.
113   // Needed for ABI compatibility with the NDK.
114   *stack_addr = (char*)attr->stack_base + attr->stack_size;
115   return 0;
116 }
117 
118 // Non-standard cruft that should only ever have been in system/core/toolbox.
strtotimeval(const char * str,struct timeval * ts)119 char* strtotimeval(const char* str, struct timeval* ts) {
120   char* s;
121   ts->tv_sec = strtoumax(str, &s, 10);
122 
123   long fractional_seconds = 0;
124   if (*s == '.') {
125     s++;
126     int count = 0;
127 
128     // Read up to 6 digits (microseconds).
129     while (*s && isdigit(*s)) {
130       if (++count < 7) {
131         fractional_seconds = fractional_seconds*10 + (*s - '0');
132       }
133       s++;
134     }
135 
136     for (; count < 6; count++) {
137       fractional_seconds *= 10;
138     }
139   }
140 
141   ts->tv_usec = fractional_seconds;
142   return s;
143 }
144 
digitval(int ch)145 static inline int digitval(int ch) {
146   unsigned d;
147 
148   d = (unsigned)(ch - '0');
149   if (d < 10) return (int)d;
150 
151   d = (unsigned)(ch - 'a');
152   if (d < 6) return (int)(d+10);
153 
154   d = (unsigned)(ch - 'A');
155   if (d < 6) return (int)(d+10);
156 
157   return -1;
158 }
159 
160 // This non-standard function was in our <inttypes.h> for some reason.
strntoumax(const char * nptr,char ** endptr,int base,size_t n)161 uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
162   const unsigned char*  p   = (const unsigned char *)nptr;
163   const unsigned char*  end = p + n;
164   int                   minus = 0;
165   uintmax_t             v = 0;
166   int                   d;
167 
168   while (p < end && isspace(*p)) {
169     p++;
170   }
171 
172   if (p < end) {
173     char c = p[0];
174     if (c == '-' || c == '+') {
175       minus = (c == '-');
176       p++;
177     }
178   }
179 
180   if (base == 0) {
181     if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
182       p += 2;
183       base = 16;
184     } else if (p+1 < end && p[0] == '0') {
185       p   += 1;
186       base = 8;
187     } else {
188       base = 10;
189     }
190   } else if (base == 16) {
191     if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
192       p += 2;
193     }
194   }
195 
196   while (p < end && (d = digitval(*p)) >= 0 && d < base) {
197     v = v*base + d;
198     p += 1;
199   }
200 
201   if (endptr) {
202     *endptr = (char*) p;
203   }
204 
205   return minus ? -v : v;
206 }
207 
208 // This non-standard function was in our <inttypes.h> for some reason.
strntoimax(const char * nptr,char ** endptr,int base,size_t n)209 intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
210   return (intmax_t) strntoumax(nptr, endptr, base, n);
211 }
212 
213 // POSIX calls this dprintf, but LP32 Android had fdprintf instead.
fdprintf(int fd,const char * fmt,...)214 int fdprintf(int fd, const char* fmt, ...) {
215   va_list ap;
216   va_start(ap, fmt);
217   int rc = vdprintf(fd, fmt, ap);
218   va_end(ap);
219   return rc;
220 }
221 
222 // POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
vfdprintf(int fd,const char * fmt,va_list ap)223 int vfdprintf(int fd, const char* fmt, va_list ap) {
224   return vdprintf(fd, fmt, ap);
225 }
226 
227 // This used to be in <sys/atomics.h>.
__futex_wake(volatile void * ftx,int count)228 int __futex_wake(volatile void* ftx, int count) {
229   return __real_futex_wake(ftx, count);
230 }
231 
232 // This used to be in <sys/atomics.h>.
__futex_wait(volatile void * ftx,int value,const struct timespec * timeout)233 int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
234   return __real_futex_wait(ftx, value, timeout);
235 }
236 
237 // Unity's libmono uses this.
tkill(pid_t tid,int sig)238 int tkill(pid_t tid, int sig) {
239   return syscall(__NR_tkill, tid, sig);
240 }
241 
242 // This was removed from POSIX 2008.
wcswcs(wchar_t * haystack,wchar_t * needle)243 wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
244   return wcsstr(haystack, needle);
245 }
246 
247 // This was removed from POSIX 2008.
bsd_signal(int signum,sighandler_t handler)248 sighandler_t bsd_signal(int signum, sighandler_t handler) {
249   return signal(signum, handler);
250 }
251 
252 // This was removed from POSIX 2008.
253 #undef bcopy
bcopy(const void * src,void * dst,size_t n)254 void bcopy(const void* src, void* dst, size_t n) {
255   memmove(dst, src, n);
256 }
257 
258 // This was removed from POSIX 2008.
259 #undef bzero
bzero(void * dst,size_t n)260 void bzero(void* dst, size_t n) {
261   memset(dst, 0, n);
262 }
263 
264 // sysv_signal() was never in POSIX.
265 extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags);
sysv_signal(int signum,sighandler_t handler)266 sighandler_t sysv_signal(int signum, sighandler_t handler) {
267   return _signal(signum, handler, SA_RESETHAND);
268 }
269 
270 // This is a system call that was never in POSIX. Use readdir(3) instead.
271 int __getdents64(unsigned int, dirent*, unsigned int);
getdents(unsigned int fd,dirent * dirp,unsigned int count)272 int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
273   return __getdents64(fd, dirp, count);
274 }
275 
276 // This is a BSDism that we never implemented correctly. Used by Firefox.
issetugid()277 int issetugid() {
278   return 0;
279 }
280 
281 // This was removed from POSIX 2004.
wait3(int * status,int options,struct rusage * rusage)282 pid_t wait3(int* status, int options, struct rusage* rusage) {
283   return wait4(-1, status, options, rusage);
284 }
285 
286 // This was removed from POSIX 2004.
getdtablesize()287 int getdtablesize() {
288   struct rlimit r;
289 
290   if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
291     return sysconf(_SC_OPEN_MAX);
292   }
293 
294   return r.rlim_cur;
295 }
296 
297 // A leaked BSD stdio implementation detail that's now a no-op.
__sinit()298 void __sinit() {}
299 int __sdidinit = 1;
300 
301 // Only used by ftime, which was removed from POSIX 2008.
302 struct timeb {
303   time_t          time;
304   unsigned short  millitm;
305   short           timezone;
306   short           dstflag;
307 };
308 
309 // This was removed from POSIX 2008.
ftime(struct timeb * tb)310 int ftime(struct timeb* tb) {
311   struct timeval  tv;
312   struct timezone tz;
313 
314   if (gettimeofday(&tv, &tz) < 0)
315     return -1;
316 
317   tb->time    = tv.tv_sec;
318   tb->millitm = (tv.tv_usec + 500) / 1000;
319 
320   if (tb->millitm == 1000) {
321     ++tb->time;
322     tb->millitm = 0;
323   }
324 
325   tb->timezone = tz.tz_minuteswest;
326   tb->dstflag  = tz.tz_dsttime;
327 
328   return 0;
329 }
330 
331 // This was removed from POSIX 2008.
index(const char * str,int ch)332 char* index(const char* str, int ch) {
333   return const_cast<char*>(strchr(str, ch));
334 }
335 
336 // This was removed from BSD.
arc4random_stir(void)337 void arc4random_stir(void) {
338   // The current implementation stirs itself as needed.
339 }
340 
341 // This was removed from BSD.
arc4random_addrandom(unsigned char *,int)342 void arc4random_addrandom(unsigned char*, int) {
343   // The current implementation adds randomness as needed.
344 }
345 
346 // Old versions of the NDK did not export malloc_usable_size, but did
347 // export dlmalloc_usable_size. We are moving away from dlmalloc in L
348 // so make this call malloc_usable_size.
dlmalloc_usable_size(void * ptr)349 size_t dlmalloc_usable_size(void* ptr) {
350   return malloc_usable_size(ptr);
351 }
352 
353 // In L we added a public pthread_gettid_np, but some apps were using the private API.
__pthread_gettid(pthread_t t)354 pid_t __pthread_gettid(pthread_t t) {
355   return pthread_gettid_np(t);
356 }
357 
358 // Older versions of apportable used dlmalloc directly instead of malloc,
359 // so export this compatibility shim that simply calls malloc.
dlmalloc(size_t size)360 void* dlmalloc(size_t size) {
361   return malloc(size);
362 }
363 
364 // Various third-party apps contain a backport of our pthread_rwlock implementation that uses this.
__get_thread()365 pthread_internal_t* __get_thread() {
366   return __real_get_thread();
367 }
368 
369 // This one exists only for the LP32 NDK and is not present anywhere else.
370 extern long __set_errno_internal(int);
__set_errno(int n)371 long __set_errno(int n) {
372   return __set_errno_internal(n);
373 }
374 
375 // Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems
376 // that use dlmalloc, be consistent and export them everywhere.
dlmalloc_inspect_all(void (*)(void *,void *,size_t,void *),void *)377 void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) {
378 }
dlmalloc_trim(size_t)379 int dlmalloc_trim(size_t) {
380     return 0;
381 }
382 
383 // LP32's <stdio.h> had putw (but not getw).
putw(int value,FILE * fp)384 int putw(int value, FILE* fp) {
385     return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : EOF;
386 }
387 
388 } // extern "C"
389 
390 #endif // !defined (__LP64__)
391