// Linux / Android 兼容层实现:compat_linux.h 与 include/ 替身头里声明的函数。 // Linux / Android compatibility layer implementation: the functions declared in compat_linux.h and the stand-in headers under include/. #include "compat_linux.h" #include #include #include #include #include #include #include #include #include #include // ---- 读自己进程的内存:process_vm_readv 读自己,不可读返回 EFAULT 而不是崩 ---- // ---- Reading our own memory: process_vm_readv on ourselves returns EFAULT for unreadable ranges instead of faulting ---- kern_return_t vm_read_overwrite(mach_port_t task, vm_address_t addr, vm_size_t size, vm_address_t data, vm_size_t *outsize) { (void)task; struct iovec l = {(void *)data, size}, r = {(void *)addr, size}; ssize_t n = process_vm_readv(getpid(), &l, 1, &r, 1, 0); if (n < 0 && errno == ENOSYS) { // 退路:写进管道再读出;write 遇不可读源地址返回 EFAULT | Fallback: write into a pipe and read back; write returns EFAULT on an unreadable source int p[2]; if (pipe(p)) return KERN_FAILURE; n = size <= 65536 ? write(p[1], (void *)addr, size) : -1; if (n > 0 && read(p[0], (void *)data, (size_t)n) != n) n = -1; close(p[0]); close(p[1]); } if (n != (ssize_t)size) return KERN_INVALID_ADDRESS; if (outsize) *outsize = size; return KERN_SUCCESS; } kern_return_t task_info(mach_port_t task, task_flavor_t flavor, task_info_t out, mach_msg_type_number_t *count) { (void)task; if (flavor != TASK_VM_INFO || *count < TASK_VM_INFO_COUNT) return KERN_FAILURE; unsigned long long vsz = 0, rss = 0; FILE *f = fopen("/proc/self/statm", "r"); if (!f) return KERN_FAILURE; int ok = fscanf(f, "%llu %llu", &vsz, &rss) == 2; fclose(f); if (!ok) return KERN_FAILURE; uint64_t pg = (uint64_t)sysconf(_SC_PAGESIZE); task_vm_info_data_t *vi = (task_vm_info_data_t *)out; vi->virtual_size = vsz * pg; vi->resident_size = vi->phys_footprint = rss * pg; return KERN_SUCCESS; } // ---- pthread 栈:Darwin 的 stackaddr 是栈顶 ---- // ---- pthread stack: Darwin's stackaddr is the stack top ---- static int stack_of(pthread_t t, void **lo, size_t *sz) { pthread_attr_t a; if (pthread_getattr_np(t, &a)) return -1; int r = pthread_attr_getstack(&a, lo, sz); pthread_attr_destroy(&a); return r; } void *pthread_get_stackaddr_np(pthread_t t) { void *lo; size_t sz; return stack_of(t, &lo, &sz) ? NULL : (char *)lo + sz; } size_t pthread_get_stacksize_np(pthread_t t) { void *lo; size_t sz; return stack_of(t, &lo, &sz) ? 0 : sz; } // ---- 文件 ---- // ---- Files ---- int dm_fcntl_getpath(int fd, char *out) { char p[64]; snprintf(p, sizeof p, "/proc/self/fd/%d", fd); ssize_t n = readlink(p, out, PATH_MAX - 1); if (n < 0) return -1; out[n] = 0; return 0; } // 缓冲一律放堆上:clonefile 由存档挂载(svc 门)调用,跑在客体线程的小栈上 // All buffers live on the heap: clonefile is called by save mounting (an svc gate) and runs on a guest thread's small stack static int copy_xattrs(int sfd, int dfd) { char *names = malloc(4096), *v = malloc(4096); int r = 0; ssize_t n = names && v ? flistxattr(sfd, names, 4096) : -1; for (char *k = names; n > 0 && k < names + n; k += strlen(k) + 1) { if (strncmp(k, "user.", 5)) continue; ssize_t vn = fgetxattr(sfd, k, v, 4096); if (vn >= 0 && fsetxattr(dfd, k, v, (size_t)vn, 0)) { r = -1; break; } } free(names); free(v); return r; } static int copy_file(const char *src, const char *dst, mode_t mode) { int sfd = open(src, O_RDONLY), dfd = sfd < 0 ? -1 : open(dst, O_WRONLY | O_CREAT | O_EXCL, mode); char *buf = malloc(65536); int r = sfd < 0 || dfd < 0 || !buf ? -1 : 0; ssize_t n; while (!r && (n = read(sfd, buf, 65536)) != 0) { if (n < 0) { r = -1; break; } for (ssize_t o = 0; o < n;) { ssize_t w = write(dfd, buf + o, (size_t)(n - o)); if (w < 0) { r = -1; break; } o += w; } } if (!r) r = copy_xattrs(sfd, dfd); free(buf); if (sfd >= 0) close(sfd); if (dfd >= 0) close(dfd); return r; } static int copy_tree(const char *src, const char *dst) { struct stat st; if (lstat(src, &st)) return -1; if (S_ISREG(st.st_mode)) return copy_file(src, dst, st.st_mode & 07777); if (!S_ISDIR(st.st_mode)) { errno = ENOTSUP; return -1; } if (mkdir(dst, st.st_mode & 07777)) return -1; DIR *d = opendir(src); if (!d) return -1; char *s2 = NULL, *d2 = NULL; struct dirent *e; int r = 0; while (!r && (e = readdir(d))) { if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; if (asprintf(&s2, "%s/%s", src, e->d_name) < 0 || asprintf(&d2, "%s/%s", dst, e->d_name) < 0) r = -1; else r = copy_tree(s2, d2); free(s2); free(d2); s2 = d2 = NULL; } closedir(d); if (!r) { int sfd = open(src, O_RDONLY | O_DIRECTORY), dfd = open(dst, O_RDONLY | O_DIRECTORY); if (sfd >= 0 && dfd >= 0) r = copy_xattrs(sfd, dfd); if (sfd >= 0) close(sfd); if (dfd >= 0) close(dfd); } return r; } int clonefile(const char *src, const char *dst, uint32_t flags) { (void)flags; struct stat st; if (!lstat(dst, &st)) { errno = EEXIST; return -1; } return copy_tree(src, dst); } int renamex_np(const char *from, const char *to, unsigned flags) { if (flags & RENAME_SWAP) return (int)syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, 2 /* RENAME_EXCHANGE */); return rename(from, to); } // ---- 进程映像 ---- // ---- Process image ---- // 安卓 App 里 /proc/self/exe 是 app_process:GameActivity 在加载 libd2r.so 前设好 DM_ROOT(数据根),这里把「可执行文件」报成 /dmhost, // 与 adb 无头时 dmhost 和 dm/ 同目录的布局一致——库构造函数(dm/derived.tsv 的经验表等)在 chdir 之前就要用它 // In the Android app /proc/self/exe is app_process: GameActivity sets DM_ROOT (the data root) before loading libd2r.so, and the "executable" is reported as /dmhost, // matching the adb headless layout where dmhost sits next to dm/ -- library constructors (the EXP table from dm/derived.tsv, etc.) need it before the chdir int _NSGetExecutablePath(char *buf, uint32_t *size) { char p[PATH_MAX]; ssize_t n; #ifdef __ANDROID__ const char *root = getenv("DM_ROOT"); if (root && *root) n = snprintf(p, sizeof p, "%s/dmhost", root); else #endif n = readlink("/proc/self/exe", p, sizeof p - 1); if (n < 0 || n >= (ssize_t)sizeof p) return -1; p[n] = 0; if ((uint32_t)n + 1 > *size) { *size = (uint32_t)n + 1; return -1; } memcpy(buf, p, (size_t)n + 1); return 0; } const struct mach_header_64 _mh_execute_header = {0xfeedfacf}; // Mach-O "__DATA,<名>" 登记段 → ELF 段 <名>(DM_SECT),链接器给 __start_<名> / __stop_<名>;未用到的段弱引用为空 // Mach-O "__DATA," registration section -> ELF section (DM_SECT); the linker provides __start_ / __stop_; unused sections are weak and empty #define SECT(n) extern uint8_t __start_##n[] __attribute__((weak)), __stop_##n[] __attribute__((weak)); SECT(swhle) SECT(swforce) SECT(swnvn) #undef SECT uint8_t *getsectiondata(const struct mach_header_64 *mh, const char *seg, const char *sect, unsigned long *size) { (void)mh; (void)seg; uint8_t *a = NULL, *b = NULL; if (!strcmp(sect, "swhle")) a = __start_swhle, b = __stop_swhle; else if (!strcmp(sect, "swforce")) a = __start_swforce, b = __stop_swforce; else if (!strcmp(sect, "swnvn")) a = __start_swnvn, b = __stop_swnvn; *size = a && b ? (unsigned long)(b - a) : 0; return *size ? a : NULL; } // ---- 摘要:MD5 / SHA-1 / SHA-256(只做校验与诊断用,不涉安全) ---- // ---- Digests: MD5 / SHA-1 / SHA-256 (only for checksums and diagnostics, not security) ---- #define ROL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) static void md5_block(uint32_t h[4], const uint8_t *p) { static const uint32_t K[64] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; static const uint8_t S[16] = {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21}; uint32_t m[16]; for (int i = 0; i < 16; i++) m[i] = p[4 * i] | p[4 * i + 1] << 8 | p[4 * i + 2] << 16 | (uint32_t)p[4 * i + 3] << 24; uint32_t a = h[0], b = h[1], c = h[2], d = h[3]; for (int i = 0; i < 64; i++) { uint32_t f; int g; if (i < 16) f = (b & c) | (~b & d), g = i; else if (i < 32) f = (d & b) | (~d & c), g = (5 * i + 1) & 15; else if (i < 48) f = b ^ c ^ d, g = (3 * i + 5) & 15; else f = c ^ (b | ~d), g = (7 * i) & 15; uint32_t t = d; d = c; c = b; b = b + ROL(a + f + K[i] + m[g], S[(i >> 4) * 4 + (i & 3)]); a = t; } h[0] += a; h[1] += b; h[2] += c; h[3] += d; } // 通用 Merkle–Damgård 外壳:64 字节块,长度尾按 big 端或 little 端 // Generic Merkle-Damgard wrapper: 64-byte blocks, length trailer big- or little-endian static void md_run(const uint8_t *p, uint64_t len, int big, void (*blk)(uint32_t *, const uint8_t *), uint32_t *h) { uint64_t full = len & ~63ull; for (uint64_t o = 0; o < full; o += 64) blk(h, p + o); uint8_t t[128] = {0}; size_t r = (size_t)(len - full); memcpy(t, p + full, r); t[r] = 0x80; size_t tl = r + 9 <= 64 ? 64 : 128; uint64_t bits = len * 8; for (int i = 0; i < 8; i++) t[tl - 8 + i] = big ? (uint8_t)(bits >> (56 - 8 * i)) : (uint8_t)(bits >> (8 * i)); for (size_t o = 0; o < tl; o += 64) blk(h, t + o); } static void md5_blk(uint32_t *h, const uint8_t *p) { md5_block(h, p); } unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md) { uint32_t h[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; md_run(data, len, 0, md5_blk, h); for (int i = 0; i < 16; i++) md[i] = (uint8_t)(h[i / 4] >> (8 * (i & 3))); return md; } static void sha1_blk(uint32_t *h, const uint8_t *p) { uint32_t w[80]; for (int i = 0; i < 16; i++) w[i] = (uint32_t)p[4 * i] << 24 | p[4 * i + 1] << 16 | p[4 * i + 2] << 8 | p[4 * i + 3]; for (int i = 16; i < 80; i++) w[i] = ROL(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4]; for (int i = 0; i < 80; i++) { uint32_t f, k; if (i < 20) f = (b & c) | (~b & d), k = 0x5a827999; else if (i < 40) f = b ^ c ^ d, k = 0x6ed9eba1; else if (i < 60) f = (b & c) | (b & d) | (c & d), k = 0x8f1bbcdc; else f = b ^ c ^ d, k = 0xca62c1d6; uint32_t t = ROL(a, 5) + f + e + k + w[i]; e = d; d = c; c = ROL(b, 30); b = a; a = t; } h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; } unsigned char *CC_SHA1(const void *data, CC_LONG len, unsigned char *md) { uint32_t h[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0}; md_run(data, len, 1, sha1_blk, h); for (int i = 0; i < 20; i++) md[i] = (uint8_t)(h[i / 4] >> (24 - 8 * (i & 3))); return md; } static void sha256_blk(uint32_t *h, const uint8_t *p) { static const uint32_t K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; uint32_t w[64]; for (int i = 0; i < 16; i++) w[i] = (uint32_t)p[4 * i] << 24 | p[4 * i + 1] << 16 | p[4 * i + 2] << 8 | p[4 * i + 3]; for (int i = 16; i < 64; i++) { uint32_t s0 = ROR(w[i - 15], 7) ^ ROR(w[i - 15], 18) ^ (w[i - 15] >> 3), s1 = ROR(w[i - 2], 17) ^ ROR(w[i - 2], 19) ^ (w[i - 2] >> 10); w[i] = w[i - 16] + s0 + w[i - 7] + s1; } uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], hh = h[7]; for (int i = 0; i < 64; i++) { uint32_t t1 = hh + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]; uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); hh = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; } h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; h[5] += f; h[6] += g; h[7] += hh; } int CC_SHA256_Init(CC_SHA256_CTX *c) { static const uint32_t H0[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; memcpy(c->h, H0, sizeof H0); c->len = 0; c->n = 0; return 1; } int CC_SHA256_Update(CC_SHA256_CTX *c, const void *data, CC_LONG len) { const uint8_t *p = data; c->len += len; while (len) { if (!c->n && len >= 64) { sha256_blk(c->h, p); p += 64; len -= 64; continue; } uint32_t k = 64 - c->n < len ? 64 - c->n : len; memcpy(c->buf + c->n, p, k); c->n += k; p += k; len -= k; if (c->n == 64) { sha256_blk(c->h, c->buf); c->n = 0; } } return 1; } int CC_SHA256_Final(unsigned char *md, CC_SHA256_CTX *c) { uint8_t t[128] = {0}; memcpy(t, c->buf, c->n); t[c->n] = 0x80; size_t tl = c->n + 9 <= 64 ? 64 : 128; uint64_t bits = c->len * 8; for (int i = 0; i < 8; i++) t[tl - 8 + i] = (uint8_t)(bits >> (56 - 8 * i)); for (size_t o = 0; o < tl; o += 64) sha256_blk(c->h, t + o); for (int i = 0; i < 32; i++) md[i] = (uint8_t)(c->h[i / 4] >> (24 - 8 * (i & 3))); return 1; } // ---- dispatch 计数信号量 ---- // ---- dispatch counting semaphore ---- struct dm_dsema { sem_t s; }; dispatch_semaphore_t dispatch_semaphore_create(long value) { dispatch_semaphore_t d = malloc(sizeof *d); if (d && sem_init(&d->s, 0, (unsigned)value)) { free(d); d = NULL; } return d; } long dispatch_semaphore_signal(dispatch_semaphore_t d) { sem_post(&d->s); return 0; } long dispatch_semaphore_wait(dispatch_semaphore_t d, dispatch_time_t timeout) { if (timeout == DISPATCH_TIME_FOREVER) { while (sem_wait(&d->s) && errno == EINTR) {} return 0; } if (timeout == DISPATCH_TIME_NOW) return sem_trywait(&d->s) ? 1 : 0; struct timespec ts = {(time_t)(timeout / 1000000000ull), (long)(timeout % 1000000000ull)}; // 绝对 CLOCK_REALTIME 纳秒 | absolute CLOCK_REALTIME ns while (sem_timedwait(&d->s, &ts)) if (errno != EINTR) return 1; return 0; } void dispatch_release(void *obj) { struct dm_dsema *d = obj; if (d) { sem_destroy(&d->s); free(d); } } uint64_t mach_absolute_time(void) { return clock_gettime_nsec_np(CLOCK_MONOTONIC); }