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
|
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <limits.h>
#include <math.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/user.h>
#include <netdb.h>
#include "wrapper/verbose.h"
struct bionic_dirent {
uint64_t d_ino;
int64_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
#ifdef ANDROID_X86_LINKER
typedef unsigned long bionic_sigset_t;
struct bionic_sigaction {
union {
void (*bsa_handler)(int);
void (*bsa_sigaction)(int, void*, void*);
};
bionic_sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
#else
# error "not implemented for this platform"
#endif
void
__assert2(const char* file, int line, const char* function, const char* failed_expression)
{
fprintf(stderr, "%s:%d: %s: assertion \"%s\" failed\n", file, line, function, failed_expression);
abort();
}
pid_t
gettid(void)
{
return syscall(SYS_gettid);
}
int
tgkill(int tgid, int tid, int sig)
{
verbose("%d, %d, %d", tgid, tid, sig);
return syscall(SYS_tgkill, tgid, tid, sig);
}
int
tkill(int tid, int sig)
{
verbose("%d, %d", tid, sig);
return syscall(SYS_tkill, tid, sig);
}
#include "libc-sha1.h"
size_t __real_IO_file_xsputn(FILE *f, const void *buf, size_t n) {}
#include "libc-ctype.h"
const unsigned int bionic___page_size = PAGE_SIZE;
__attribute_const__
int*
bionic___errno(void)
{
return __errno_location();
}
__attribute_const__
int*
bionic___get_h_errno(void)
{
return &h_errno;
}
int
bionic_stat(const char *restrict path, struct stat *restrict buf)
{
verbose("%s", path);
return stat(path, buf);
}
int
bionic_lstat(const char *restrict path, struct stat *restrict buf)
{
verbose("%s", path);
return lstat(path, buf);
}
int
bionic_fstat(int fd, struct stat *buf)
{
verbose("%d", fd);
return fstat(fd, buf);
}
int
bionic_fstatat(int dirfd, const char *pathname, void *buf, int flags)
{
verbose("%d, %s", dirfd, pathname);
return fstatat(dirfd, pathname, buf, flags);
}
static void
glibc_dirent_to_bionic_dirent(const struct dirent *de, struct bionic_dirent *bde)
{
assert(bde && de);
*bde = (struct bionic_dirent){
.d_ino = de->d_ino,
.d_off = de->d_off,
.d_reclen = de->d_reclen,
.d_type = de->d_type,
};
_Static_assert(sizeof(bde->d_name) >= sizeof(de->d_name), "bionic_dirent can't hold dirent's d_name");
memcpy(bde->d_name, de->d_name, sizeof(bde->d_name));
}
struct bionic_dirent*
bionic_readdir(DIR *dirp)
{
assert(dirp);
static struct bionic_dirent bde;
struct dirent *de;
if (!(de = readdir(dirp)))
return NULL;
glibc_dirent_to_bionic_dirent(de, &bde);
return &bde;
}
int
bionic_readdir_r(DIR *dirp, struct bionic_dirent *entry, struct bionic_dirent **result)
{
assert(dirp && entry && result);
struct dirent de, *der = NULL;
int ret;
if ((ret = readdir_r(dirp, &de, &der)) != 0 || !der) {
*result = NULL;
return ret;
}
glibc_dirent_to_bionic_dirent(der, entry);
*result = entry;
return 0;
}
int
bionic_sigaddset(const bionic_sigset_t *set, int sig)
{
int bit = sig - 1;
unsigned long *local_set = (unsigned long*)set;
if (!set || bit < 0 || bit >= (int)(8 * sizeof(*set))) {
errno = EINVAL;
return -1;
}
local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
return 0;
}
int
bionic_sigismember(const bionic_sigset_t *set, int sig)
{
int bit = sig - 1;
const unsigned long *local_set = (const unsigned long*)set;
if (!set || bit < 0 || bit >= (int)(8 * sizeof(*set))) {
errno = EINVAL;
return -1;
}
return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
}
int
bionic_sigaction(int sig, const struct bionic_sigaction *restrict act, struct bionic_sigaction *restrict oact)
{
verbose("%d, %p, %p", sig, (void*)act, (void*)oact);
if (sig == 33)
sig = SIGRTMIN;
struct sigaction goact = {0}, gact = {0};
if (act) {
gact.sa_handler = act->bsa_handler;
gact.sa_flags = act->sa_flags;
gact.sa_restorer = act->sa_restorer;
assert(35 < SIGRTMAX);
for (int signo = 35; signo < SIGRTMAX; ++signo) {
if (bionic_sigismember(&act->sa_mask, signo))
sigaddset(&gact.sa_mask, signo);
}
}
const int ret = sigaction(sig, (act ? &gact : NULL), (oact ? &goact : NULL));
if (oact) {
*oact = (struct bionic_sigaction){0};
oact->bsa_handler = goact.sa_handler;
oact->sa_flags = goact.sa_flags;
oact->sa_restorer = goact.sa_restorer;
for (int signo = SIGRTMIN + 3; signo < SIGRTMAX; ++signo) {
if (sigismember(&goact.sa_mask, signo))
bionic_sigaddset(&oact->sa_mask, signo);
}
}
return ret;
}
int
bionic___isfinitef(float f)
{
return isfinite(f);
}
uintptr_t bionic___stack_chk_guard = 4;
__attribute__((noreturn))
void bionic___stack_chk_fail(void)
{
abort();
}
static inline int
bionic_sysconf_to_glibc_sysconf(int name)
{
switch(name) {
case 0x0000: return _SC_ARG_MAX;
case 0x0001: return _SC_BC_BASE_MAX;
case 0x0002: return _SC_BC_DIM_MAX;
case 0x0003: return _SC_BC_SCALE_MAX;
case 0x0004: return _SC_BC_STRING_MAX;
case 0x0005: return _SC_CHILD_MAX;
case 0x0006: return _SC_CLK_TCK;
case 0x0007: return _SC_COLL_WEIGHTS_MAX;
case 0x0008: return _SC_EXPR_NEST_MAX;
case 0x0009: return _SC_LINE_MAX;
case 0x000a: return _SC_NGROUPS_MAX;
case 0x000b: return _SC_OPEN_MAX;
case 0x000c: return _SC_PASS_MAX;
case 0x000d: return _SC_2_C_BIND;
case 0x000e: return _SC_2_C_DEV;
case 0x000f: return _SC_2_C_VERSION;
case 0x0010: return _SC_2_CHAR_TERM;
case 0x001d: return _SC_XOPEN_CRYPT;
case 0x001e: return _SC_XOPEN_ENH_I18N;
case 0x001f: return _SC_XOPEN_SHM;
case 0x0020: return _SC_XOPEN_VERSION;
case 0x0021: return _SC_XOPEN_XCU_VERSION;
case 0x0022: return _SC_XOPEN_REALTIME;
case 0x0023: return _SC_XOPEN_REALTIME_THREADS;
case 0x0024: return _SC_XOPEN_LEGACY;
case 0x0025: return _SC_ATEXIT_MAX;
case 0x0026: return _SC_IOV_MAX;
case 0x0027: return _SC_PAGESIZE;
case 0x0028: return _SC_PAGE_SIZE;
case 0x0029: return _SC_XOPEN_UNIX;
case 0x002a: return _SC_XBS5_ILP32_OFF32;
case 0x002b: return _SC_XBS5_ILP32_OFFBIG;
case 0x002c: return _SC_XBS5_LP64_OFF64;
case 0x002d: return _SC_XBS5_LPBIG_OFFBIG;
case 0x002e: return _SC_AIO_LISTIO_MAX;
case 0x002f: return _SC_AIO_MAX;
case 0x0030: return _SC_AIO_PRIO_DELTA_MAX;
case 0x0031: return _SC_DELAYTIMER_MAX;
case 0x0032: return _SC_MQ_OPEN_MAX;
case 0x0033: return _SC_MQ_PRIO_MAX;
case 0x0034: return _SC_RTSIG_MAX;
case 0x0035: return _SC_SEM_NSEMS_MAX;
case 0x0036: return _SC_SEM_VALUE_MAX;
case 0x0037: return _SC_SIGQUEUE_MAX;
case 0x0038: return _SC_TIMER_MAX;
case 0x0039: return _SC_ASYNCHRONOUS_IO;
case 0x003a: return _SC_FSYNC;
case 0x003b: return _SC_MAPPED_FILES;
case 0x003c: return _SC_MEMLOCK;
case 0x003d: return _SC_MEMLOCK_RANGE;
case 0x003e: return _SC_MEMORY_PROTECTION;
case 0x003f: return _SC_MESSAGE_PASSING;
case 0x0040: return _SC_PRIORITIZED_IO;
case 0x0041: return _SC_PRIORITY_SCHEDULING;
case 0x0042: return _SC_REALTIME_SIGNALS;
case 0x0043: return _SC_SEMAPHORES;
case 0x0044: return _SC_SHARED_MEMORY_OBJECTS;
case 0x0045: return _SC_SYNCHRONIZED_IO;
case 0x0046: return _SC_TIMERS;
case 0x0047: return _SC_GETGR_R_SIZE_MAX;
case 0x0048: return _SC_GETPW_R_SIZE_MAX;
case 0x0049: return _SC_LOGIN_NAME_MAX;
case 0x004a: return _SC_THREAD_DESTRUCTOR_ITERATIONS;
case 0x004b: return _SC_THREAD_KEYS_MAX;
case 0x004c: return _SC_THREAD_STACK_MIN;
case 0x004d: return _SC_THREAD_THREADS_MAX;
case 0x004e: return _SC_TTY_NAME_MAX;
case 0x004f: return _SC_THREADS;
case 0x0050: return _SC_THREAD_ATTR_STACKADDR;
case 0x0051: return _SC_THREAD_ATTR_STACKSIZE;
case 0x0052: return _SC_THREAD_PRIORITY_SCHEDULING;
case 0x0053: return _SC_THREAD_PRIO_INHERIT;
case 0x0054: return _SC_THREAD_PRIO_PROTECT;
case 0x0055: return _SC_THREAD_SAFE_FUNCTIONS;
case 0x0060: return _SC_NPROCESSORS_CONF;
case 0x0061: return _SC_NPROCESSORS_ONLN;
case 0x0062: return _SC_PHYS_PAGES;
case 0x0063: return _SC_AVPHYS_PAGES;
case 0x0064: return _SC_MONOTONIC_CLOCK;
case 0x0065: return _SC_2_PBS;
case 0x0066: return _SC_2_PBS_ACCOUNTING;
case 0x0067: return _SC_2_PBS_CHECKPOINT;
case 0x0068: return _SC_2_PBS_LOCATE;
case 0x0069: return _SC_2_PBS_MESSAGE;
case 0x006a: return _SC_2_PBS_TRACK;
case 0x006b: return _SC_ADVISORY_INFO;
case 0x006c: return _SC_BARRIERS;
case 0x006d: return _SC_CLOCK_SELECTION;
case 0x006e: return _SC_CPUTIME;
case 0x006f: return _SC_HOST_NAME_MAX;
case 0x0070: return _SC_IPV6;
case 0x0071: return _SC_RAW_SOCKETS;
case 0x0072: return _SC_READER_WRITER_LOCKS;
case 0x0073: return _SC_REGEXP;
case 0x0074: return _SC_SHELL;
case 0x0075: return _SC_SPAWN;
case 0x0076: return _SC_SPIN_LOCKS;
case 0x0077: return _SC_SPORADIC_SERVER;
case 0x0078: return _SC_SS_REPL_MAX;
case 0x0079: return _SC_SYMLOOP_MAX;
case 0x007a: return _SC_THREAD_CPUTIME;
case 0x007b: return _SC_THREAD_PROCESS_SHARED;
case 0x007c: return _SC_THREAD_ROBUST_PRIO_INHERIT;
case 0x007d: return _SC_THREAD_ROBUST_PRIO_PROTECT;
case 0x007e: return _SC_THREAD_SPORADIC_SERVER;
case 0x007f: return _SC_TIMEOUTS;
case 0x0080: return _SC_TRACE;
case 0x0081: return _SC_TRACE_EVENT_FILTER;
case 0x0082: return _SC_TRACE_EVENT_NAME_MAX;
case 0x0083: return _SC_TRACE_INHERIT;
case 0x0084: return _SC_TRACE_LOG;
case 0x0085: return _SC_TRACE_NAME_MAX;
case 0x0086: return _SC_TRACE_SYS_MAX;
case 0x0087: return _SC_TRACE_USER_EVENT_MAX;
case 0x0088: return _SC_TYPED_MEMORY_OBJECTS;
case 0x0089: return _SC_V7_ILP32_OFF32;
case 0x008a: return _SC_V7_ILP32_OFFBIG;
case 0x008b: return _SC_V7_LP64_OFF64;
case 0x008c: return _SC_V7_LPBIG_OFFBIG;
case 0x008d: return _SC_XOPEN_STREAMS;
case 0x008f: return _SC_LEVEL1_ICACHE_SIZE;
case 0x0090: return _SC_LEVEL1_ICACHE_ASSOC;
case 0x0091: return _SC_LEVEL1_ICACHE_LINESIZE;
case 0x0092: return _SC_LEVEL1_DCACHE_SIZE;
case 0x0093: return _SC_LEVEL1_DCACHE_ASSOC;
case 0x0094: return _SC_LEVEL1_DCACHE_LINESIZE;
case 0x0095: return _SC_LEVEL2_CACHE_SIZE;
case 0x0096: return _SC_LEVEL2_CACHE_ASSOC;
case 0x0097: return _SC_LEVEL2_CACHE_LINESIZE;
case 0x0098: return _SC_LEVEL3_CACHE_SIZE;
case 0x0099: return _SC_LEVEL3_CACHE_ASSOC;
case 0x009a: return _SC_LEVEL3_CACHE_LINESIZE;
case 0x009b: return _SC_LEVEL4_CACHE_SIZE;
case 0x009c: return _SC_LEVEL4_CACHE_ASSOC;
case 0x009d: return _SC_LEVEL4_CACHE_LINESIZE;
default:
assert(0 && "sysconf constant (%d) is not mapped");
}
return 0xFFFF;
}
long
bionic_sysconf(int name)
{
verbose("0x%x", name);
return sysconf(bionic_sysconf_to_glibc_sysconf(name));
}
#ifdef VERBOSE_FUNCTIONS
# include "libc-verbose.h"
#endif
|