ndk_cruft.cpp revision bc2e88a85d5fd3e59c041a667283378c1503a16c
1ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski/* 2ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * Copyright (C) 2013 The Android Open Source Project 3ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * All rights reserved. 4ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * 5ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * Redistribution and use in source and binary forms, with or without 6ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * modification, are permitted provided that the following conditions 7ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * are met: 8ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * * Redistributions of source code must retain the above copyright 9ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * notice, this list of conditions and the following disclaimer. 10ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * * Redistributions in binary form must reproduce the above copyright 11ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * notice, this list of conditions and the following disclaimer in 12ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * the documentation and/or other materials provided with the 13ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * distribution. 14ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * 15ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 256cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 266cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski * SUCH DAMAGE. 276cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski */ 286cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski 296cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski// This file perpetuates the mistakes of the past. 306cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski 316cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <ctype.h> 326cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <dirent.h> 336cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <errno.h> 346cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <inttypes.h> 356cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <pthread.h> 366cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <signal.h> 376cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <stdio.h> 386cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <stdlib.h> 396cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <string.h> 406cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <sys/resource.h> 416cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#include <sys/syscall.h> 42ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include <sys/time.h> 43ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include <sys/types.h> 44467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski#include <sys/wait.h> 45ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include <unistd.h> 46ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include <wchar.h> 47ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 48ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include "private/libc_logging.h" 49ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 50ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskiextern "C" { 51ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 526cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski// Brillo and LP64 don't need to support any legacy cruft. 536cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski#if !defined(__BRILLO__) && !defined(__LP64__) 54ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 55b274e35abfbbd09e0fce983a215c11522c56cce2Adam Lesinski// These were accidentally declared in <unistd.h> because we stupidly used to inline 56b274e35abfbbd09e0fce983a215c11522c56cce2Adam Lesinski// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps. 57b274e35abfbbd09e0fce983a215c11522c56cce2Adam Lesinskiunsigned int __page_size = PAGE_SIZE; 58ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskiunsigned int __page_shift = 12; 59ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 60ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries). 61ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskipid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) { 62ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski return wait4(pid, status, options, rusage); 63ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 64ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 65ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski// TODO: does anything still need this? 66ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskiint __open() { 67ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski abort(); 68ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 69ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 70ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski// TODO: does anything still need this? 71ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskivoid** __get_tls() { 72ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski#include "private/__get_tls.h" 73ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski return __get_tls(); 74ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 75ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 76ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski// This non-standard function was in our <string.h> for some reason. 77ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskivoid memswap(void* m1, void* m2, size_t n) { 78ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski char* p = reinterpret_cast<char*>(m1); 79ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski char* p_end = p + n; 80ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski char* q = reinterpret_cast<char*>(m2); 81ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski while (p < p_end) { 82ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski char tmp = *p; 83ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski *p = *q; 84ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski *q = tmp; 85ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski p++; 86ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski q++; 87ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski } 88ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 89ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 90ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskiint pthread_attr_setstackaddr(pthread_attr_t*, void*) { 91467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski // This was removed from POSIX.1-2008, and is not implemented on bionic. 92ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski // Needed for ABI compatibility with the NDK. 93ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski return ENOSYS; 94ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 95ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 96ca5638fd85098c3d0a699492751043545f75553aAdam Lesinskiint pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) { 97ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski // This was removed from POSIX.1-2008. 98ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski // Needed for ABI compatibility with the NDK. 99ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski *stack_addr = (char*)attr->stack_base + attr->stack_size; 100ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski return 0; 101ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 102ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 103ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski// Non-standard cruft that should only ever have been in system/core/toolbox. 1046cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinskichar* strtotimeval(const char* str, struct timeval* ts) { 1056cbfb1de493e42d937158ed57495c9656864ccbaAdam Lesinski char* s; 106ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski ts->tv_sec = strtoumax(str, &s, 10); 107803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski 108ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski long fractional_seconds = 0; 109ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski if (*s == '.') { 110ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski s++; 111ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski int count = 0; 112803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski 113ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski // Read up to 6 digits (microseconds). 114803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski while (*s && isdigit(*s)) { 115803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski if (++count < 7) { 116803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski fractional_seconds = fractional_seconds*10 + (*s - '0'); 117ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski } 118ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski s++; 119ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski } 120ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 121803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski for (; count < 6; count++) { 122803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski fractional_seconds *= 10; 123803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski } 124ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski } 125803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski 126ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski ts->tv_usec = fractional_seconds; 127ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski return s; 128ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski} 129ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 130b274e35abfbbd09e0fce983a215c11522c56cce2Adam Lesinskistatic inline int digitval(int ch) { 131803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski unsigned d; 132803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski 133803c7c807969bea1f1c50f348832f5b60ad05d8eAdam Lesinski d = (unsigned)(ch - '0'); 134ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski if (d < 10) return (int)d; 135ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 136ca5638fd85098c3d0a699492751043545f75553aAdam Lesinski 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. 146uintmax_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. 194intmax_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. 199int 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. 208int 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>. 219int __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>. 224int __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. 229int tkill(pid_t tid, int sig) { 230 return syscall(__NR_tkill, tid, sig); 231} 232 233// This was removed from POSIX 2008. 234wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) { 235 return wcsstr(haystack, needle); 236} 237 238// This was removed from POSIX 2008. 239sighandler_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 246void bcopy(const void* src, void* dst, size_t n) { 247 memmove(dst, src, n); 248} 249#else 250// x86 has an assembler implementation. 251#endif 252 253// sysv_signal() was never in POSIX. 254extern "C++" sighandler_t _signal(int signum, sighandler_t handler, int flags); 255sighandler_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. 260int __getdents64(unsigned int, dirent*, unsigned int); 261int 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. 266int issetugid() { 267 return 0; 268} 269 270// This was removed from POSIX 2004. 271pid_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. 276int 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// A leaked BSD stdio implementation detail that's now a no-op. 287void __sinit() {} 288int __sdidinit = 1; 289 290// Only used by ftime, which was removed from POSIX 2008. 291struct timeb { 292 time_t time; 293 unsigned short millitm; 294 short timezone; 295 short dstflag; 296}; 297 298// This was removed from POSIX 2008. 299int ftime(struct timeb* tb) { 300 struct timeval tv; 301 struct timezone tz; 302 303 if (gettimeofday(&tv, &tz) < 0) 304 return -1; 305 306 tb->time = tv.tv_sec; 307 tb->millitm = (tv.tv_usec + 500) / 1000; 308 309 if (tb->millitm == 1000) { 310 ++tb->time; 311 tb->millitm = 0; 312 } 313 314 tb->timezone = tz.tz_minuteswest; 315 tb->dstflag = tz.tz_dsttime; 316 317 return 0; 318} 319 320// This was removed from POSIX 2008. 321char* index(const char* str, int ch) { 322 return strchr(str, ch); 323} 324 325// This was removed from BSD. 326void arc4random_stir(void) { 327 // The current implementation stirs itself as needed. 328} 329 330// This was removed from BSD. 331void arc4random_addrandom(unsigned char*, int) { 332 // The current implementation adds randomness as needed. 333} 334 335// Old versions of the NDK did not export malloc_usable_size, but did 336// export dlmalloc_usable_size. We are moving away from dlmalloc in L 337// so make this call malloc_usable_size. 338size_t dlmalloc_usable_size(void* ptr) { 339 return malloc_usable_size(ptr); 340} 341 342// In L we added a public pthread_gettid_np, but some apps were using the private API. 343pid_t __pthread_gettid(pthread_t t) { 344 return pthread_gettid_np(t); 345} 346 347// Older versions of apportable used dlmalloc directly instead of malloc, 348// so export this compatibility shim that simply calls malloc. 349void* dlmalloc(size_t size) { 350 return malloc(size); 351} 352 353#define __get_thread __real_get_thread 354#include "pthread_internal.h" 355#undef __get_thread 356// Various third-party apps contain a backport of our pthread_rwlock implementation that uses this. 357pthread_internal_t* __get_thread() { 358 return __real_get_thread(); 359} 360 361// This one exists only for the LP32 NDK and is not present anywhere else. 362extern long __set_errno_internal(int); 363long __set_errno(int n) { 364 return __set_errno_internal(n); 365} 366 367// This was never implemented in bionic, only needed for ABI compatibility with the NDK. 368// In the M time frame, over 1000 apps have a reference to this! 369void endpwent() { } 370 371// Since dlmalloc_inspect_all and dlmalloc_trim are exported for systems 372// that use dlmalloc, be consistent and export them everywhere. 373#if defined(USE_JEMALLOC) 374void dlmalloc_inspect_all(void (*)(void*, void*, size_t, void*), void*) { 375} 376int dlmalloc_trim(size_t) { 377 return 0; 378} 379#else 380void dlmalloc_inspect_all_real(void (*)(void*, void*, size_t, void*), void*); 381void dlmalloc_inspect_all(void (*handler)(void*, void*, size_t, void*), void* arg) { 382 dlmalloc_inspect_all_real(handler, arg); 383} 384int dlmalloc_trim_real(size_t); 385int dlmalloc_trim(size_t pad) { 386 return dlmalloc_trim_real(pad); 387} 388#endif 389 390#endif // !defined(__BRILLO__) && !defined (__LP64__) 391 392} // extern "C" 393