Home | History | Annotate | Download | only in bionic
      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, but only for 32-bit targets.
     30 #if !defined(__LP64__)
     31 
     32 #include <ctype.h>
     33 #include <dirent.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 // These were accidentally declared in <unistd.h> because we stupidly used to inline
     49 // getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps.
     50 extern "C" {
     51   unsigned int __page_size = PAGE_SIZE;
     52   unsigned int __page_shift = 12;
     53 }
     54 
     55 // TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
     56 extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
     57   return wait4(pid, status, options, rusage);
     58 }
     59 
     60 // TODO: does anything still need this?
     61 extern "C" int __open() {
     62   abort();
     63 }
     64 
     65 // TODO: does anything still need this?
     66 extern "C" void** __get_tls() {
     67 #include "private/__get_tls.h"
     68   return __get_tls();
     69 }
     70 
     71 // This non-standard function was in our <string.h> for some reason.
     72 extern "C" void memswap(void* m1, void* m2, size_t n) {
     73   char* p = reinterpret_cast<char*>(m1);
     74   char* p_end = p + n;
     75   char* q = reinterpret_cast<char*>(m2);
     76   while (p < p_end) {
     77     char tmp = *p;
     78     *p = *q;
     79     *q = tmp;
     80     p++;
     81     q++;
     82   }
     83 }
     84 
     85 extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
     86   // This was removed from POSIX.1-2008, and is not implemented on bionic.
     87   // Needed for ABI compatibility with the NDK.
     88   return ENOSYS;
     89 }
     90 
     91 extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
     92   // This was removed from POSIX.1-2008.
     93   // Needed for ABI compatibility with the NDK.
     94   *stack_addr = (char*)attr->stack_base + attr->stack_size;
     95   return 0;
     96 }
     97 
     98 // Non-standard cruft that should only ever have been in system/core/toolbox.
     99 extern "C" char* strtotimeval(const char* str, struct timeval* ts) {
    100   char* s;
    101   ts->tv_sec = strtoumax(str, &s, 10);
    102 
    103   long fractional_seconds = 0;
    104   if (*s == '.') {
    105     s++;
    106     int count = 0;
    107 
    108     // Read up to 6 digits (microseconds).
    109     while (*s && isdigit(*s)) {
    110       if (++count < 7) {
    111         fractional_seconds = fractional_seconds*10 + (*s - '0');
    112       }
    113       s++;
    114     }
    115 
    116     for (; count < 6; count++) {
    117       fractional_seconds *= 10;
    118     }
    119   }
    120 
    121   ts->tv_usec = fractional_seconds;
    122   return s;
    123 }
    124 
    125 static inline int digitval(int ch) {
    126   unsigned d;
    127 
    128   d = (unsigned)(ch - '0');
    129   if (d < 10) return (int)d;
    130 
    131   d = (unsigned)(ch - 'a');
    132   if (d < 6) return (int)(d+10);
    133 
    134   d = (unsigned)(ch - 'A');
    135   if (d < 6) return (int)(d+10);
    136 
    137   return -1;
    138 }
    139 
    140 // This non-standard function was in our <inttypes.h> for some reason.
    141 extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
    142   const unsigned char*  p   = (const unsigned char *)nptr;
    143   const unsigned char*  end = p + n;
    144   int                   minus = 0;
    145   uintmax_t             v = 0;
    146   int                   d;
    147 
    148   while (p < end && isspace(*p)) {
    149     p++;
    150   }
    151 
    152   if (p < end) {
    153     char c = p[0];
    154     if (c == '-' || c == '+') {
    155       minus = (c == '-');
    156       p++;
    157     }
    158   }
    159 
    160   if (base == 0) {
    161     if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
    162       p += 2;
    163       base = 16;
    164     } else if (p+1 < end && p[0] == '0') {
    165       p   += 1;
    166       base = 8;
    167     } else {
    168       base = 10;
    169     }
    170   } else if (base == 16) {
    171     if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
    172       p += 2;
    173     }
    174   }
    175 
    176   while (p < end && (d = digitval(*p)) >= 0 && d < base) {
    177     v = v*base + d;
    178     p += 1;
    179   }
    180 
    181   if (endptr) {
    182     *endptr = (char*) p;
    183   }
    184 
    185   return minus ? -v : v;
    186 }
    187 
    188 // This non-standard function was in our <inttypes.h> for some reason.
    189 extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
    190   return (intmax_t) strntoumax(nptr, endptr, base, n);
    191 }
    192 
    193 // POSIX calls this dprintf, but LP32 Android had fdprintf instead.
    194 extern "C" int fdprintf(int fd, const char* fmt, ...) {
    195   va_list ap;
    196   va_start(ap, fmt);
    197   int rc = vdprintf(fd, fmt, ap);
    198   va_end(ap);
    199   return rc;
    200 }
    201 
    202 // POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
    203 extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) {
    204   return vdprintf(fd, fmt, ap);
    205 }
    206 
    207 #define __futex_wake __real_futex_wake
    208 #define __futex_wait __real_futex_wait
    209 #include "private/bionic_futex.h"
    210 #undef __futex_wake
    211 #undef __futex_wait
    212 
    213 // This used to be in <sys/atomics.h>.
    214 extern "C" int __futex_wake(volatile void* ftx, int count) {
    215   return __real_futex_wake(ftx, count);
    216 }
    217 
    218 // This used to be in <sys/atomics.h>.
    219 extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
    220   return __real_futex_wait(ftx, value, timeout);
    221 }
    222 
    223 // Unity's libmono uses this.
    224 extern "C" int tkill(pid_t tid, int sig) {
    225   return syscall(__NR_tkill, tid, sig);
    226 }
    227 
    228 // This was removed from POSIX 2008.
    229 extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
    230   return wcsstr(haystack, needle);
    231 }
    232 
    233 // This was removed from POSIX 2008.
    234 extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) {
    235   return signal(signum, handler);
    236 }
    237 
    238 // sysv_signal() was never in POSIX.
    239 extern sighandler_t _signal(int signum, sighandler_t handler, int flags);
    240 extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) {
    241   return _signal(signum, handler, SA_RESETHAND);
    242 }
    243 
    244 // This is a system call that was never in POSIX. Use readdir(3) instead.
    245 extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
    246 extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
    247   return __getdents64(fd, dirp, count);
    248 }
    249 
    250 // This is a BSDism that we never implemented correctly. Used by Firefox.
    251 extern "C" int issetugid() {
    252   return 0;
    253 }
    254 
    255 // This was removed from POSIX 2004.
    256 extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) {
    257   return wait4(-1, status, options, rusage);
    258 }
    259 
    260 // This was removed from POSIX 2004.
    261 extern "C" int getdtablesize() {
    262   struct rlimit r;
    263 
    264   if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
    265     return sysconf(_SC_OPEN_MAX);
    266   }
    267 
    268   return r.rlim_cur;
    269 }
    270 
    271 // Only used by ftime, which was removed from POSIX 2008.
    272 struct timeb {
    273   time_t          time;
    274   unsigned short  millitm;
    275   short           timezone;
    276   short           dstflag;
    277 };
    278 
    279 // This was removed from POSIX 2008.
    280 extern "C" int ftime(struct timeb* tb) {
    281   struct timeval  tv;
    282   struct timezone tz;
    283 
    284   if (gettimeofday(&tv, &tz) < 0)
    285     return -1;
    286 
    287   tb->time    = tv.tv_sec;
    288   tb->millitm = (tv.tv_usec + 500) / 1000;
    289 
    290   if (tb->millitm == 1000) {
    291     ++tb->time;
    292     tb->millitm = 0;
    293   }
    294 
    295   tb->timezone = tz.tz_minuteswest;
    296   tb->dstflag  = tz.tz_dsttime;
    297 
    298   return 0;
    299 }
    300 
    301 // This was removed from POSIX 2008.
    302 extern "C" char* index(const char* str, int ch) {
    303   return strchr(str, ch);
    304 }
    305 
    306 // This was removed from BSD.
    307 extern "C" void arc4random_stir(void) {
    308   // The current implementation stirs itself as needed.
    309 }
    310 
    311 // This was removed from BSD.
    312 extern "C" void arc4random_addrandom(unsigned char*, int) {
    313   // The current implementation adds randomness as needed.
    314 }
    315 
    316 // Old versions of the NDK did not export malloc_usable_size, but did
    317 // export dlmalloc_usable_size. We are moving away from dlmalloc in L
    318 // so make this call malloc_usable_size.
    319 extern "C" size_t dlmalloc_usable_size(void* ptr) {
    320   return malloc_usable_size(ptr);
    321 }
    322 
    323 // Older versions of appportable used dlmalloc directly instead of malloc,
    324 // so export this compatibility shim that simply calls malloc.
    325 extern "C" void* dlmalloc(size_t size) {
    326   return malloc(size);
    327 }
    328 
    329 #endif
    330