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