-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfake_libc.c
2439 lines (1942 loc) · 64.6 KB
/
fake_libc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include "helpers.h"
#include <link.h>
#include <dlfcn.h>
#include <stdarg.h>
#include <errno.h>
#include <stdint.h>
#include <linux/posix_types.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <syscall.h>
#define LM_ID_BASE 0
#define LM_ID_NEW -1
/* Base address to check for Android specifics */
#define ANDROID_TOP_ADDR_VALUE_MUTEX 0xFFFF
#define ANDROID_TOP_ADDR_VALUE_COND 0xFFFF
#define ANDROID_TOP_ADDR_VALUE_RWLOCK 0xFFFF
#define ANDROID_MUTEX_SHARED_MASK 0x2000
#define ANDROID_COND_SHARED_MASK 0x0001
#define ANDROID_RWLOCKATTR_SHARED_MASK 0x0010
/* For the static initializer types */
#define ANDROID_PTHREAD_MUTEX_INITIALIZER 0
#define ANDROID_PTHREAD_RECURSIVE_MUTEX_INITIALIZER 0x4000
#define ANDROID_PTHREAD_ERRORCHECK_MUTEX_INITIALIZER 0x8000
#define ANDROID_PTHREAD_COND_INITIALIZER 0
#define ANDROID_PTHREAD_RWLOCK_INITIALIZER 0
enum
{
PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_ADAPTIVE_NP
//#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
,
PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
//#endif
#ifdef __USE_GNU
/* For compatibility. */
, PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
#endif
};
typedef unsigned long int bionic_fpos_t;
struct __sbuf {
unsigned char *_base;
int _size;
};
typedef struct __sFILE {
unsigned char *_p; /* current position in (some) buffer */
int _r; /* read space left for getc() */
int _w; /* write space left for putc() */
short _flags; /* flags, below; this FILE is free if 0 */
short _file; /* fileno, if Unix descriptor, else -1 */
struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
int _lbfsize; /* 0 or -_bf._size, for inline putc */
/* operations */
void *_cookie; /* cookie passed to io functions */
int (*_close)(void *);
int (*_read)(void *, char *, int);
bionic_fpos_t (*_seek)(void *, bionic_fpos_t, int);
int (*_write)(void *, const char *, int);
/* extension data, to avoid further ABI breakage */
struct __sbuf _ext;
/* data for long sequences of ungetc() */
unsigned char *_up; /* saved _p when _p is doing ungetc data */
int _ur; /* saved _r when _r is counting ungetc data */
/* tricks to meet minimum requirements even when malloc() fails */
unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
unsigned char _nbuf[1]; /* guarantee a getc() buffer */
/* separate buffer for fgetln() when line crosses buffer boundary */
struct __sbuf _lb; /* buffer for fgetln() */
/* Unix stdio files get aligned to block boundaries on fseek() */
int _blksize; /* stat.st_blksize (may be != _bf._size) */
bionic_fpos_t _offset; /* current lseek offset */
} BIONIC_FILE;
typedef long unsigned int* _Unwind_Ptr;
typedef __WCHAR_TYPE__ wchar_t;
typedef struct {
uint8_t __seq[4];
#ifdef __LP64__
char __reserved[4];
#endif
} mbstate_t;
typedef long wctype_t;
void *glibc_handle = NULL;
void *rt_handle = NULL;
void *gcc_handle = NULL;
void *pthread_handle = NULL;
void *setjmp_handle = NULL;
// glibc protoypes
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
int *__h_errno_location(void);
int clock_gettime(int clk_id, void *res);
void abort(void) __attribute__((noreturn));
char* __strncpy_chk(char *a, const char* b, size_t c, size_t d);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
// dynamically loaded glibc/rt/pthread/setjmp functions
static void (*glibc___cxa_finalize)(void *dso_handle) = NULL;
static int (*glibc_getc)(void *stream) = NULL;
static int (*glibc_ungetc)(int c, void *stream) = NULL;
static __WINT_TYPE__ (*glibc_getwc)(void *) = NULL;
static __WINT_TYPE__ (*glibc_putwc)(wchar_t, void *) = NULL;
static double (*glibc_wcstod)(const wchar_t*, wchar_t**) = NULL;
static float (*glibc_wcstof)(const wchar_t*, wchar_t**) = NULL;
static long double (*glibc_wcstold)(const wchar_t*, wchar_t**) = NULL;
static double (*glibc_atof)(const char*a) = NULL;
static double (*glibc_strtod)(const char*, char**) = NULL;
static float (*glibc_strtof)(const char*, char**) = NULL;
static long double (*glibc_strtold)(const char*, char**) = NULL;
static long double (*glibc_strtold_l)(const char *, char **, void*) = NULL;
static long double (*glibc_wcstold_l)(const wchar_t *a, wchar_t **b, void* c) = NULL;
static double (*glibc_difftime)(time_t, time_t) = NULL;
static ssize_t (*glibc_getline)(char **lineptr, size_t *n, void *stream) = NULL;
static int (*glibc_vswprintf)(wchar_t*, size_t, const wchar_t*, va_list) = NULL;
static int (*glibc_vswscanf)(const wchar_t*, const wchar_t*, va_list) = NULL;
static int (*glibc_vwprintf)(const wchar_t*, va_list) = NULL;
static int (*glibc_vwscanf)(const wchar_t*, va_list) = NULL;
static int (*glibc_vscanf)(const char *format, va_list ap) = NULL;
static int (*glibc_vsscanf)(const char *str, const char *format, va_list ap) = NULL;
static int (*glibc_sscanf)(const char *str, const char *format, ...) = NULL;
static int (*glibc___vsprintf_chk)(char * str, int flag, size_t strlen, const char * format, va_list ap) = NULL;
static int (*glibc___vsnprintf_chk)(char* dest, size_t supplied_size, int flags, size_t dest_len_from_compiler, const char* format, ...) = NULL;
static int (*glibc_vasprintf)(char **strp, const char *fmt, va_list ap) = NULL;
static int (*glibc_vfprintf)(void *fp, const char *fmt, va_list ap) = NULL;
int (*glibc_fprintf)(void *stream, const char *format, ...) = NULL;
static double (*glibc_ldexp)(double, int) = NULL;
static int (*glibc_fputs)(const char *s, void *stream) = NULL;
static int (*glibc_fputc)(int c, void *stream) = NULL;
int (*glibc_pthread_mutex_lock)(void *__mutex) = NULL;
int (*glibc_pthread_mutex_unlock)(void *__mutex) = NULL;
static int (*glibc_pthread_cond_timedwait)(void *restrict cond, void *restrict mutex, void *restrict abstime) = NULL;
static int (*glibc_pthread_cond_wait)(void *restrict cond, void *restrict mutex) = NULL;
static int (*glibc_pthread_cond_broadcast)(void *cond) = NULL;
static int (*glibc_pthread_cond_signal)(void *cond) = NULL;
static int (*glibc_pthread_cond_destroy)(void *cond) = NULL;
static int (*glibc_pthread_cond_init)(void *restrict cond, const void *restrict attr) = NULL;
static int (*glibc_fgetc)(void *stream) = NULL;
static char *(*glibc_fgets)(char *s, int size, void *stream) = NULL;
static size_t (*glibc_fwrite)(const void *ptr, size_t size, size_t nmemb, void *stream) = NULL;
static size_t (*glibc_fread)(void *ptr, size_t size, size_t nmemb, void *stream) = NULL;
static int (*glibc_fflush)(void *stream) = NULL;
static int (*glibc_vfscanf)(void *stream, const char *format, va_list ap) = NULL;
static __WINT_TYPE__ (*glibc_fgetwc)(void *) = NULL;
static wchar_t *(*glibc_fgetws)(wchar_t *, int, void *) = NULL;
static __WINT_TYPE__ (*glibc_fputwc)(wchar_t, void *) = NULL;
static int (*glibc_fputws)(const wchar_t *, void *) = NULL;
static int (*glibc_fwide)(void *, int) = NULL;
static int (*glibc_vfwprintf)(void*, const wchar_t*, va_list) = NULL;
static int (*glibc_vfwscanf)(void*, const wchar_t*, va_list) = NULL;
static double (*glibc_drand48)(void) = NULL;
static double (*glibc_erand48)(unsigned short a[3]) = NULL;
static int (*glibc_pthread_mutex_destroy)(void *mutex) = NULL;
static int (*glibc_pthread_mutex_init)(void *restrict mutex, const void *restrict attr) = NULL;
static int (*glibc_pthread_mutex_trylock)(void *mutex) = NULL;
//static int (*glibc_printf)(const char *format, ...) = NULL;
//static int (*glibc___sprintf_chk)(char * str, int flag, size_t sl, const char * format, ...) = NULL;
//static int (*glibc_dprintf)(int fd, const char *format, ...) = NULL;
//static int (*glibc_sprintf)(char *str, const char *format, ...) = NULL;
//static int (*glibc_snprintf)(char *str, size_t size, const char *format, ...) = NULL;
static int (*glibc_vprintf)(const char *format, va_list ap) = NULL;
static int (*glibc_vdprintf)(int fd, const char *format, va_list ap) = NULL;
static int (*glibc_vsprintf)(char *str, const char *format, va_list ap) = NULL;
static int (*glibc_vsnprintf)(char *str, size_t size, const char *format, va_list ap) = NULL;
static long (*glibc_sysconf)(int name) = NULL;
static void *(*glibc_fdopen)(int fd, const char *mode) = NULL;
static void *(*glibc_freopen)(const char *path, const char *mode, void *stream) = NULL;
static void *(*glibc_fopen)(const char *path, const char *mode) = NULL;
static int (*glibc_fclose)(void *stream) = NULL;
static int (*glibc_feof)(void *) = NULL;
static int (*rt_pthread_create)(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
long (*glibc_syscall)(long number, ...) = NULL;
static int (*glibc_pthread_attr_init)(pthread_attr_t *__attr) = NULL;
static int (*glibc_pthread_attr_destroy)(pthread_attr_t *__attr) = NULL;
static int (*glibc_pthread_attr_setdetachstate)(pthread_attr_t *__attr, int state) = NULL;
static int (*glibc_pthread_attr_getdetachstate)(pthread_attr_t const *__attr, int *state) = NULL;
static int (*glibc_pthread_attr_setschedpolicy)(pthread_attr_t *__attr, int policy) = NULL;
static int (*glibc_pthread_attr_getschedpolicy)(pthread_attr_t const *__attr, int *policy) = NULL;
static int (*glibc_pthread_attr_setschedparam)(pthread_attr_t *__attr, void const *param) = NULL;
static int (*glibc_pthread_attr_getschedparam)(pthread_attr_t const *__attr, void *param) = NULL;
static int (*glibc_pthread_attr_setstacksize)(pthread_attr_t *__attr, size_t stack_size) = NULL;
static int (*glibc_pthread_attr_getstacksize)(pthread_attr_t const *__attr, size_t *stack_size) = NULL;
static int (*glibc_pthread_attr_setstackaddr)(pthread_attr_t *__attr, void *stack_addr) = NULL;
static int (*glibc_pthread_attr_getstackaddr)(pthread_attr_t const *__attr, void **stack_addr) = NULL;
static int (*glibc_pthread_attr_setstack)(pthread_attr_t *__attr, void *stack_base, size_t stack_size) = NULL;
static int (*glibc_pthread_attr_getstack)(pthread_attr_t const *__attr, void **stack_base, size_t *stack_size) = NULL;
static int (*glibc_pthread_attr_setguardsize)(pthread_attr_t *__attr, size_t guard_size) = NULL;
static int (*glibc_pthread_attr_getguardsize)(pthread_attr_t const *__attr, size_t *guard_size) = NULL;
static int (*glibc_pthread_attr_setscope)(pthread_attr_t *__attr, int scope) = NULL;
static int (*glibc_pthread_attr_getscope)(pthread_attr_t const *__attr, int *scope) = NULL;
static int (*glibc_pthread_condattr_init)(pthread_condattr_t *attr) = NULL;
int (*glibc_readdir_r)(void *dir, void *entry, void **result) = NULL;
void *(*glibc_readdir)(void *dir) = NULL;
static int (*glibc_ferror)(void *stream) = NULL;
static int (*glibc_finite)(double x) = NULL;
static int (*glibc_finitef)(float x) = NULL;
static int (*glibc_finitel)(long double x) = NULL;
static int (*glibc_isinf)(double x) = NULL;
static int (*glibc_isinff)(float x) = NULL;
static int (*glibc_isinfl)(long double x) = NULL;
static int (*glibc_isnan)(double x) = NULL;
static int (*glibc_isnanf)(float x) = NULL;
static int (*glibc_isnanl)(long double x) = NULL;
int (*glibc_getaddrinfo)(const char *hostname, const char *servname,
const void *hints, void **res) = NULL;
int (*glibc_freeaddrinfo)(void *__ai) = NULL;
static int (*glibc_fseek)(void *stream, long offset, int whence) = NULL;
static int (*glibc_fseeko)(void *stream, unsigned long int offset, int whence) = NULL;
static long (*glibc_ftell)(void *stream) = NULL;
static int (*glibc_fileno)(void *stream) = NULL;
static void (*glibc_rewind)(BIONIC_FILE *stream) = NULL;
static unsigned long int (*glibc_ftello)(void *stream) = NULL;
static void *(*glibc_funopen)(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int), unsigned long int (*seekfn)(void *, unsigned long int, int), int (*closefn)(void *)) = NULL;
static int (*glibc_fsetpos)(void *stream, const void *pos) = NULL;
static int (*glibc_fgetpos)(void *stream, void *pos) = NULL;
static int *(*glibc___h_errno_location)(void) = NULL;
static int *(*glibc___errno_location)(void) = NULL;
void **glibc_stdin = NULL;
void **glibc_stdout = NULL;
void **glibc_stderr = NULL;
void *(*glibc_dlmopen)(int lmid, const char *filename, int flags) = NULL;
void *(*glibc_dlsym)(void *handle, const char *symbol) = NULL;
void *(*glibc___tls_get_addr)(void *a) = NULL;
int android_dl_namespace_id = 0;
#define LOAD_GLIBC() \
if(glibc_handle == NULL) \
{ \
glibc_handle = glibc_dlmopen(LM_ID_BASE, "/lib/libc.so.6", RTLD_LAZY); \
}
#ifdef DEBUG
#define SHOW_FUNCTION(f, ptr) \
if(!glibc_fprintf) glibc_fprintf = glibc_dlsym(glibc_handle, "fprintf"); \
if(!glibc_stderr) glibc_stderr = glibc_dlsym(glibc_handle, "stderr"); \
glibc_fprintf(*glibc_stderr, "%s is at %p called from %s\n", #f, ptr, __FUNCTION__);
#else
#define SHOW_FUNCTION(...)
#endif
#define LOAD_GLIBC_SYMBOL(f) \
if(glibc_ ## f == NULL) \
{ \
LOAD_GLIBC(); \
glibc_ ## f = glibc_dlsym(glibc_handle, #f); \
assert(glibc_ ## f); \
} \
SHOW_FUNCTION(f, glibc_ ## f)
#define LOAD_PTHREAD() \
if(pthread_handle == NULL) \
{ \
pthread_handle = glibc_dlmopen(LM_ID_BASE, "/lib/libpthread.so.0", RTLD_LAZY); \
}
#define LOAD_RT() \
if(rt_handle == NULL) \
{ \
rt_handle = glibc_dlmopen(LM_ID_BASE, "/lib/librt.so.1", RTLD_LAZY); \
}
#define LOAD_RT_SYMBOL(f) \
if(rt_ ## f == NULL) \
{ \
LOAD_RT(); \
rt_ ## f = glibc_dlsym(rt_handle, #f); \
assert(rt_ ## f); \
} \
SHOW_FUNCTION(f, rt_ ## f)
#define LOAD_GCC() \
if(gcc_handle == NULL) \
{ \
gcc_handle = glibc_dlmopen(LM_ID_BASE, "/usr/lib/libgcc_s.so", RTLD_LAZY); \
}
#define LOAD_GCC_SYMBOL(f) \
if(gcc_ ## f == NULL) \
{ \
LOAD_GCC(); \
gcc_ ## f = glibc_dlsym(gcc_handle, #f); \
assert(gcc_ ## f); \
} \
SHOW_FUNCTION(f, gcc_ ## f)
#define LOAD_SETJMP() \
if(setjmp_handle == NULL) \
{ \
setjmp_handle = glibc_dlmopen(android_dl_namespace_id, "libsetjmp.so", RTLD_LAZY); \
}
#define LOAD_SETJMP_SYMBOL(f) \
if(setjmp_ ## f == NULL) \
{ \
LOAD_SETJMP(); \
setjmp_ ## f = glibc_dlsym(setjmp_handle, STRINGIFY(my_ ##f); \
assert(setjmp_ ## f); \
} \
SHOW_FUNCTION(f, setjmp_ ## f)
static void (*init_dlfunctions)(int a_dl_ns_id, void *op, void *sym, void *addr, void *err, void *clo, void *it);
void init_setjmp()
{
LOAD_SETJMP();
}
// defined below in android symbol section
extern BIONIC_FILE __sF[3];
extern char *__progname;
// provide dlopen and friends for fake libdl.so
void init_dl(int a_dl_ns_id, void *op, void *sym, void *addr, void *err, void *clo, void *it, char *programname, void *tls)
{
glibc_dlmopen = op;
glibc_dlsym = sym;
glibc___tls_get_addr = tls;
LOAD_GLIBC();
LOAD_RT();
LOAD_GCC();
LOAD_PTHREAD();
android_dl_namespace_id = a_dl_ns_id;
void *libdl = glibc_dlmopen(android_dl_namespace_id, "libdl.so", RTLD_LAZY);
init_dlfunctions = glibc_dlsym(libdl, "init_dlfunctions");
init_dlfunctions(android_dl_namespace_id, op, sym, addr, err, clo, it);
LOAD_GLIBC_SYMBOL(syscall);
__progname = programname;
// for assert
LOAD_GLIBC_SYMBOL(fprintf);
LOAD_GLIBC_SYMBOL(stderr);
}
static void *_get_actual_fp(BIONIC_FILE *fp)
{
LOAD_GLIBC_SYMBOL(stdin);
LOAD_GLIBC_SYMBOL(stdout);
LOAD_GLIBC_SYMBOL(stderr);
if (fp == &__sF[0])
return *glibc_stdin;
else if (fp == &__sF[1])
return *glibc_stdout;
else if (fp == &__sF[2])
return *glibc_stderr;
return fp;
}
// symbols for android
char *__progname = NULL;
uintptr_t __stack_chk_guard = 0;
BIONIC_FILE __sF[3];
BIONIC_FILE *stdin = &__sF[0];
BIONIC_FILE *stdout = &__sF[1];
BIONIC_FILE *stderr = &__sF[2];
void *_get_actual_fp(BIONIC_FILE *fp);
int __sprintf_chk(char * str, int flag, size_t sl, const char * format, ...)
{
LOAD_GLIBC_SYMBOL(__vsprintf_chk);
int ret = 0;
va_list ap;
va_start(ap, format);
ret = glibc___vsprintf_chk(str, flag, sl, format, ap);
va_end(ap);
return ret;
}
int fprintf(BIONIC_FILE *fp, const char *fmt, ...) SOFTFP;
int vfprintf(BIONIC_FILE *fp, const char *fmt, va_list ap) SOFTFP;
int vfprintf(BIONIC_FILE *fp, const char *fmt, va_list ap)
{
LOAD_GLIBC_SYMBOL(vfprintf);
return glibc_vfprintf(_get_actual_fp(fp), fmt, ap);
}
int fprintf(BIONIC_FILE *fp, const char *fmt, ...)
{
LOAD_GLIBC_SYMBOL(vfprintf);
int ret = 0;
va_list args;
va_start(args, fmt);
ret = glibc_vfprintf(_get_actual_fp(fp), fmt, args);
va_end(args);
return ret;
}
double ldexp(double, int) SOFTFP;
double ldexp(double a, int b)
{
LOAD_GLIBC_SYMBOL(ldexp);
return glibc_ldexp(a, b);
}
int fputs(const char *s, void *stream) SOFTFP;
int fputs(const char *s, void *stream)
{
LOAD_GLIBC_SYMBOL(fputs);
return glibc_fputs(s, _get_actual_fp(stream));
}
int fputc(int c, void *stream) SOFTFP;
int fputc(int c, void *stream)
{
LOAD_GLIBC_SYMBOL(fputc);
return glibc_fputc(c, _get_actual_fp(stream));
}
int vasprintf(char **strp, const char *fmt, va_list ap) SOFTFP;
int vasprintf(char **strp, const char *fmt, va_list ap)
{
LOAD_GLIBC_SYMBOL(vasprintf);
return glibc_vasprintf(strp, fmt, ap);
}
int asprintf(char **strp, const char *fmt, ...) SOFTFP;
int asprintf(char **strp, const char *fmt, ...)
{
LOAD_GLIBC_SYMBOL(vasprintf);
int ret = 0;
va_list ap;
va_start(ap, fmt);
ret = glibc_vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
void android_set_abort_message(const char* msg) SOFTFP;
void android_set_abort_message(const char* msg)
{
LOAD_GLIBC_SYMBOL(fprintf);
LOAD_GLIBC_SYMBOL(stderr);
glibc_fprintf(*glibc_stderr, "stub: android_set_abort_message(%s)\n", msg);
}
void openlog(const char* log_tag, int options, int facility) SOFTFP;
void openlog(const char* log_tag, int options, int facility)
{
LOAD_GLIBC_SYMBOL(fprintf);
LOAD_GLIBC_SYMBOL(stderr);
glibc_fprintf(*glibc_stderr, "stub: openlog(%s %d %d)\n", log_tag, options, facility);
}
void syslog(int priority, const char* fmt, ...) SOFTFP;
void syslog(int priority, const char* fmt, ...)
{
LOAD_GLIBC_SYMBOL(fprintf);
LOAD_GLIBC_SYMBOL(vfprintf);
va_list args;
va_start(args, fmt);
glibc_vfprintf(*glibc_stderr, fmt, args);
va_end(args);
}
void closelog(void) SOFTFP;
void closelog()
{
LOAD_GLIBC_SYMBOL(fprintf);
LOAD_GLIBC_SYMBOL(stderr);
glibc_fprintf(*glibc_stderr, "stub: closelog()\n");
}
int __snprintf_chk(char* dest, size_t supplied_size, int flags, size_t dest_len_from_compiler, const char* format, ...) SOFTFP;
int __snprintf_chk(char* dest, size_t supplied_size, int flags, size_t dest_len_from_compiler, const char* format, ...)
{
LOAD_GLIBC_SYMBOL(__vsnprintf_chk);
int ret = 0;
va_list va;
va_start(va, format);
ret = glibc___vsnprintf_chk(dest, supplied_size, flags, dest_len_from_compiler, format, va);
va_end(va);
return ret;
}
int __vsnprintf_chk(char* dest, size_t supplied_size, int flags, size_t dest_len_from_compiler, const char* format, va_list va) SOFTFP;
int __vsnprintf_chk(char* dest, size_t supplied_size, int flags, size_t dest_len_from_compiler, const char* format, va_list va)
{
LOAD_GLIBC_SYMBOL(__vsnprintf_chk);
return glibc___vsnprintf_chk(dest, supplied_size, flags, dest_len_from_compiler, format, va);
}
static void hybris_set_mutex_attr(unsigned int android_value, void *attr)
{
/* Init already sets as PTHREAD_MUTEX_NORMAL */
pthread_mutexattr_init(attr);
if (android_value & ANDROID_PTHREAD_RECURSIVE_MUTEX_INITIALIZER) {
pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
} else if (android_value & ANDROID_PTHREAD_ERRORCHECK_MUTEX_INITIALIZER) {
pthread_mutexattr_settype(attr, PTHREAD_MUTEX_ERRORCHECK);
}
}
static void* hybris_alloc_init_mutex(unsigned int android_mutex)
{
LOAD_GLIBC_SYMBOL(pthread_mutex_init);
void *realmutex = malloc(sizeof(pthread_mutex_t));
pthread_mutexattr_t attr;
hybris_set_mutex_attr(android_mutex, &attr);
glibc_pthread_mutex_init(realmutex, &attr);
return realmutex;
}
int pthread_attr_init(pthread_attr_t *__attr)
{
LOAD_GLIBC_SYMBOL(pthread_attr_init);
pthread_attr_t *realattr;
realattr = malloc(sizeof(pthread_attr_t));
*((unsigned int *)__attr) = (unsigned int) realattr;
return glibc_pthread_attr_init(realattr);
}
int pthread_attr_destroy(pthread_attr_t *__attr)
{
int ret;
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_destroy);
ret = glibc_pthread_attr_destroy(realattr);
/* We need to release the memory allocated at pthread_attr_init
* Possible side effects if destroy is called without our init */
free(realattr);
return ret;
}
int pthread_attr_setdetachstate(pthread_attr_t *__attr, int state)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setdetachstate);
return glibc_pthread_attr_setdetachstate(realattr, state);
}
int pthread_attr_getdetachstate(pthread_attr_t const *__attr, int *state)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getdetachstate);
return glibc_pthread_attr_getdetachstate(realattr, state);
}
int pthread_attr_setschedpolicy(pthread_attr_t *__attr, int policy)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setschedpolicy);
return glibc_pthread_attr_setschedpolicy(realattr, policy);
}
int pthread_attr_getschedpolicy(pthread_attr_t const *__attr, int *policy)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getschedpolicy);
return glibc_pthread_attr_getschedpolicy(realattr, policy);
}
int pthread_attr_setschedparam(pthread_attr_t *__attr, void const *param)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setschedparam);
return glibc_pthread_attr_setschedparam(realattr, param);
}
int pthread_attr_getschedparam(pthread_attr_t const *__attr, void *param)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getschedparam);
return glibc_pthread_attr_getschedparam(realattr, param);
}
int pthread_attr_setstacksize(pthread_attr_t *__attr, size_t stack_size)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setstacksize);
return glibc_pthread_attr_setstacksize(realattr, stack_size);
}
int pthread_attr_getstacksize(pthread_attr_t const *__attr, size_t *stack_size)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getstacksize);
return glibc_pthread_attr_getstacksize(realattr, stack_size);
}
int pthread_attr_setstackaddr(pthread_attr_t *__attr, void *stack_addr)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setstackaddr);
return glibc_pthread_attr_setstackaddr(realattr, stack_addr);
}
int pthread_attr_getstackaddr(pthread_attr_t const *__attr, void **stack_addr)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getstackaddr);
return glibc_pthread_attr_getstackaddr(realattr, stack_addr);
}
int pthread_attr_setstack(pthread_attr_t *__attr, void *stack_base, size_t stack_size)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setstack);
return glibc_pthread_attr_setstack(realattr, stack_base, stack_size);
}
int pthread_attr_getstack(pthread_attr_t const *__attr, void **stack_base, size_t *stack_size)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getstack);
return glibc_pthread_attr_getstack(realattr, stack_base, stack_size);
}
int pthread_attr_setguardsize(pthread_attr_t *__attr, size_t guard_size)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setguardsize);
return glibc_pthread_attr_setguardsize(realattr, guard_size);
}
int pthread_attr_getguardsize(pthread_attr_t const *__attr, size_t *guard_size)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getguardsize);
return glibc_pthread_attr_getguardsize(realattr, guard_size);
}
int pthread_attr_setscope(pthread_attr_t *__attr, int scope)
{
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_setscope);
return glibc_pthread_attr_setscope(realattr, scope);
}
int pthread_attr_getscope(pthread_attr_t const *__attr)
{
int scope;
pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
LOAD_GLIBC_SYMBOL(pthread_attr_getscope);
/* Android doesn't have the scope attribute because it always
* returns PTHREAD_SCOPE_SYSTEM */
glibc_pthread_attr_getscope(realattr, &scope);
return scope;
}
// PTHREAD TODO!
int pthread_mutex_lock(void *__mutex) SOFTFP;
int pthread_mutex_lock(void *__mutex)
{
LOAD_GLIBC_SYMBOL(pthread_mutex_lock);
unsigned int value = (*(unsigned int*)__mutex);
if(value <= ANDROID_TOP_ADDR_VALUE_MUTEX)
{
*(unsigned int*)__mutex = (unsigned int)hybris_alloc_init_mutex(value);
value = *(unsigned int*)__mutex;
}
int ret = glibc_pthread_mutex_lock((void*)value);
return ret;
}
int pthread_mutex_unlock(void *__mutex) SOFTFP;
int pthread_mutex_unlock(void *__mutex)
{
LOAD_GLIBC_SYMBOL(pthread_mutex_unlock);
int ret = glibc_pthread_mutex_unlock((void*)*(unsigned int*)__mutex);
return ret;
}
long __set_errno_internal(int e)
{
LOAD_GLIBC_SYMBOL(__errno_location);
(*glibc___errno_location()) = e;
return -1;
}
void *my_start_routine(void *con)
{
void **container = con;
void *(*start_routine)(void *) = container[0];
void *arg = container[1];
pthread_t thread = pthread_self();
pid_t pid = glibc_syscall(__NR_gettid);
my_settid_to_thread(thread, pid);
container[2] = 0;
start_routine(arg);
my_remtid(thread);
}
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) SOFTFP;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
{
LOAD_RT_SYMBOL(pthread_create);
LOAD_GLIBC_SYMBOL(pthread_mutex_lock);
LOAD_GLIBC_SYMBOL(pthread_mutex_unlock);
pthread_attr_t *realattr = NULL;
if (attr != NULL)
realattr = (pthread_attr_t *) *(unsigned int *) attr;
// we need to keep container until it has been processed
// therefore the crazy locking and waiting
volatile void * volatile container[3];
container[0] = start_routine;
container[1] = arg;
container[2] = 1;
int ret = rt_pthread_create(thread, realattr, my_start_routine, &container);
while(container[2] == 1){}
return ret;
}
static pthread_cond_t* hybris_alloc_init_cond(void)
{
pthread_cond_t *realcond = malloc(sizeof(pthread_cond_t));
LOAD_GLIBC_SYMBOL(pthread_condattr_init);
LOAD_GLIBC_SYMBOL(pthread_cond_init);
pthread_condattr_t attr;
glibc_pthread_condattr_init(&attr);
glibc_pthread_cond_init(realcond, &attr);
return realcond;
}
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
/* Both cond and mutex can be statically initialized, check for both */
unsigned int cvalue = (*(unsigned int *) cond);
unsigned int mvalue = (*(unsigned int *) mutex);
LOAD_GLIBC_SYMBOL(pthread_cond_wait);
pthread_cond_t *realcond = (pthread_cond_t *) cvalue;
if (cvalue <= ANDROID_TOP_ADDR_VALUE_COND) {
realcond = hybris_alloc_init_cond();
*((unsigned int *) cond) = (unsigned int) realcond;
}
pthread_mutex_t *realmutex = (pthread_mutex_t *) mvalue;
if (mvalue <= ANDROID_TOP_ADDR_VALUE_MUTEX) {
realmutex = hybris_alloc_init_mutex(mvalue);
*((unsigned int *) mutex) = (unsigned int) realmutex;
}
return glibc_pthread_cond_wait(realcond, realmutex);
}
int pthread_cond_timedwait(pthread_cond_t *cond,
pthread_mutex_t *mutex, const struct timespec *abstime)
{
/* Both cond and mutex can be statically initialized, check for both */
unsigned int cvalue = (*(unsigned int *) cond);
unsigned int mvalue = (*(unsigned int *) mutex);
LOAD_GLIBC_SYMBOL(pthread_cond_timedwait);
pthread_cond_t *realcond = (pthread_cond_t *) cvalue;
if (cvalue <= ANDROID_TOP_ADDR_VALUE_COND) {
realcond = hybris_alloc_init_cond();
*((unsigned int *) cond) = (unsigned int) realcond;
}
pthread_mutex_t *realmutex = (pthread_mutex_t *) mvalue;
if (mvalue <= ANDROID_TOP_ADDR_VALUE_MUTEX) {
realmutex = hybris_alloc_init_mutex(mvalue);
*((unsigned int *) mutex) = (unsigned int) realmutex;
}
return glibc_pthread_cond_timedwait(realcond, realmutex, (void*)abstime);
}
int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
pthread_mutex_t *mutex, const struct timespec *reltime)
{
/* Both cond and mutex can be statically initialized, check for both */
unsigned int cvalue = (*(unsigned int *) cond);
unsigned int mvalue = (*(unsigned int *) mutex);
LOAD_GLIBC_SYMBOL(pthread_cond_timedwait);
pthread_cond_t *realcond = (pthread_cond_t *) cvalue;
if (cvalue <= ANDROID_TOP_ADDR_VALUE_COND) {
realcond = hybris_alloc_init_cond();
*((unsigned int *) cond) = (unsigned int) realcond;
}
pthread_mutex_t *realmutex = (pthread_mutex_t *) mvalue;
if (mvalue <= ANDROID_TOP_ADDR_VALUE_MUTEX) {
realmutex = hybris_alloc_init_mutex(mvalue);
*((unsigned int *) mutex) = (unsigned int) realmutex;
}
#define CLOCK_REALTIME 0
struct timespec tv;
// rt_
clock_gettime(CLOCK_REALTIME, &tv);
tv.tv_sec += reltime->tv_sec;
tv.tv_nsec += reltime->tv_nsec;
if (tv.tv_nsec >= 1000000000) {
tv.tv_sec++;
tv.tv_nsec -= 1000000000;
}
return glibc_pthread_cond_timedwait(realcond, realmutex, &tv);
}
int pthread_cond_destroy(pthread_cond_t *cond)
{
int ret;
pthread_cond_t *realcond = (pthread_cond_t *) *(unsigned int *) cond;
LOAD_GLIBC_SYMBOL(pthread_cond_destroy);
if (!realcond) {
return EINVAL;
}
glibc_pthread_cond_destroy(realcond);
*((unsigned int *)cond) = 0;
return ret;
}
int pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *attr)
{
pthread_cond_t *realcond = NULL;
LOAD_GLIBC_SYMBOL(pthread_cond_init);
realcond = malloc(sizeof(pthread_cond_t));
*((unsigned int *) cond) = (unsigned int) realcond;
return glibc_pthread_cond_init(realcond, attr);
}
int pthread_cond_broadcast(pthread_cond_t *cond)
{
unsigned int value = (*(unsigned int *) cond);
LOAD_GLIBC_SYMBOL(pthread_cond_broadcast);
pthread_cond_t *realcond = (pthread_cond_t *) value;
if (value <= ANDROID_TOP_ADDR_VALUE_COND) {
realcond = hybris_alloc_init_cond();
*((unsigned int *) cond) = (unsigned int) realcond;
}
return glibc_pthread_cond_broadcast(realcond);
}
int pthread_cond_signal(pthread_cond_t *cond)
{
unsigned int value = (*(unsigned int *) cond);
LOAD_GLIBC_SYMBOL(pthread_cond_signal);
pthread_cond_t *realcond = (pthread_cond_t *) value;
if (value <= ANDROID_TOP_ADDR_VALUE_COND) {
realcond = hybris_alloc_init_cond();
*((unsigned int *) cond) = (unsigned int) realcond;
}
return glibc_pthread_cond_signal(realcond);
}
int fgetc(void *stream) SOFTFP;
int fgetc(void *stream)
{
LOAD_GLIBC_SYMBOL(fgetc);
return glibc_fgetc(_get_actual_fp(stream));
}
char *fgets(char *s, int size, void *stream) SOFTFP;
char *fgets(char *s, int size, void *stream)
{
LOAD_GLIBC_SYMBOL(fgets);
return glibc_fgets(s, size, _get_actual_fp(stream));
}
int getc(void *stream) SOFTFP;