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