- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测 - llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言) - tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化) - tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
520 lines
20 KiB
C
520 lines
20 KiB
C
#define IG_ARCHIVE_IMPL 1
|
|
#include "IGArchiveGuard.h"
|
|
#if __has_include("../include/IGHookResponse.h")
|
|
#include "../include/IGHookResponse.h"
|
|
#define IG_ARCHIVE_HAS_RESPONSE 1
|
|
#else
|
|
#define IG_ARCHIVE_HAS_RESPONSE 0
|
|
#endif
|
|
#include "../resolve/IGResolve.h"
|
|
#include "../anti_hook/IGAntiHook.h"
|
|
#include "../primitives/IGInline.h"
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <pthread.h>
|
|
#include <stdarg.h>
|
|
#include <stdatomic.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <mach/mach.h>
|
|
#include <mach-o/dyld.h>
|
|
#include <mach-o/loader.h>
|
|
#include <mach-o/nlist.h>
|
|
|
|
#if defined(__has_feature)
|
|
# if __has_feature(ptrauth_calls)
|
|
# include <ptrauth.h>
|
|
# define IG_ARCHIVE_STRIP(p) ptrauth_strip((p), ptrauth_key_asia)
|
|
# endif
|
|
#endif
|
|
#ifndef IG_ARCHIVE_STRIP
|
|
# define IG_ARCHIVE_STRIP(p) (p)
|
|
#endif
|
|
|
|
#if !IG_ARCHIVE_HAS_RESPONSE
|
|
static inline int ig_response_capture(const IGFinding *finding, void *ctx) {
|
|
if (finding->status == IG_DETECTED) *(int *)ctx = finding->reason;
|
|
return 0;
|
|
}
|
|
static inline void ig_response_handle(IGStatus status, int reason) {
|
|
(void)status;
|
|
(void)reason;
|
|
}
|
|
#endif
|
|
|
|
/* 仅混淆常量,不作为秘密。SDK GOT/Lazy 槽由 dyld 在独立镜像滑移后绑定。 */
|
|
static const unsigned char names[IG_ARCHIVE_FUNCTION_COUNT][6] = {
|
|
{0x5a,0x53,0x4c,0x59,0x52},
|
|
{0x5a,0x4e,0x59,0x5d,0x58},
|
|
{0x5a,0x4b,0x4e,0x55,0x48,0x59},
|
|
{0x5a,0x5f,0x50,0x53,0x4f,0x59},
|
|
{0x49,0x52,0x50,0x55,0x52,0x57},
|
|
{0x53,0x4c,0x59,0x52},
|
|
{0x4e,0x59,0x5d,0x58},
|
|
{0x4b,0x4e,0x55,0x48,0x59},
|
|
{0x5f,0x50,0x53,0x4f,0x59},
|
|
{0x5b,0x59,0x48,0x4c,0x55,0x58},
|
|
{0x59,0x44,0x55,0x48},
|
|
{0x5a,0x53,0x4e,0x57},
|
|
{0x58,0x50,0x53,0x4c,0x59,0x52},
|
|
{0x58,0x50,0x4f,0x45,0x51}
|
|
};
|
|
static const uint8_t name_lengths[IG_ARCHIVE_FUNCTION_COUNT] = {
|
|
5, 5, 6, 6, 6, 4, 4, 5, 5, 6, 4, 4, 6, 5
|
|
};
|
|
|
|
enum { IG_ARCHIVE_MAX_SLOTS = 1024 };
|
|
typedef struct {
|
|
uintptr_t slot_addr;
|
|
uintptr_t expected_fn;
|
|
uintptr_t text_lo;
|
|
uintptr_t text_hi;
|
|
uint8_t is_lazy;
|
|
} IGArchiveSlot;
|
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
static _Thread_local int resolving;
|
|
static _Atomic(uintptr_t) targets[IG_ARCHIVE_FUNCTION_COUNT];
|
|
static uintptr_t expected[IG_ARCHIVE_FUNCTION_COUNT];
|
|
static unsigned char prefixes[IG_ARCHIVE_FUNCTION_COUNT][16];
|
|
static IGStatus initial[IG_ARCHIVE_FUNCTION_COUNT];
|
|
static int initialReason[IG_ARCHIVE_FUNCTION_COUNT];
|
|
static IGArchiveSlot trackedSlots[IG_ARCHIVE_MAX_SLOTS];
|
|
static size_t trackedCount;
|
|
static int bindingReady;
|
|
|
|
#ifdef IG_OBSERVATION
|
|
static _Atomic(uint64_t) counters[IG_ARCHIVE_FUNCTION_COUNT * 3];
|
|
#define COUNT(i, k) atomic_fetch_add_explicit(&counters[(i)*3+(k)], 1, memory_order_relaxed)
|
|
#else
|
|
#define COUNT(i, k) ((void)0)
|
|
#endif
|
|
|
|
static int read_bytes(uintptr_t address, void *out, size_t size) {
|
|
vm_size_t copied = 0;
|
|
return vm_read_overwrite(mach_task_self(), address, size, (vm_address_t)out, &copied) == KERN_SUCCESS
|
|
&& copied == size;
|
|
}
|
|
|
|
static int is_system_image(const char *path) {
|
|
if (!path) return 0;
|
|
return ig_strncmp(path, "/usr/lib/", 9) == 0 ||
|
|
ig_strncmp(path, "/System/", 8) == 0 ||
|
|
ig_strncmp(path, "/private/preboot/Cryptexes/", 27) == 0;
|
|
}
|
|
|
|
static int has_suffix(const char *path, const char *suffix) {
|
|
if (!path || !suffix) return 0;
|
|
size_t len = ig_strlen(path), tail = ig_strlen(suffix);
|
|
return len >= tail && ig_strcmp(path + len - tail, suffix) == 0;
|
|
}
|
|
|
|
static uintptr_t expected_wrapper_for_symbol(const char *name) {
|
|
struct { const char *sym; uintptr_t fn; } table[] = {
|
|
{"_rd_a0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a0)},
|
|
{"_rd_f", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_f)},
|
|
{"_rd_a1", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a1)},
|
|
{"_rd_a2", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a2)},
|
|
{"_rd_a3", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a3)},
|
|
{"_rd_a4", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_a4)},
|
|
{"_rd_u00", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_u00)},
|
|
{"_rd_o", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_o)},
|
|
{"_rdo", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rdo)},
|
|
{"_rd_r", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_r)},
|
|
{"_rdr", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rdr)},
|
|
{"_rd_w0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_w0)},
|
|
{"_rd_w", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_w)},
|
|
{"_rd_c0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_c0)},
|
|
{"_rd_c", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_c)},
|
|
{"_rd_p00", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_p00)},
|
|
{"_rd_p0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_p0)},
|
|
{"_rd_e", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_e)},
|
|
{"_rd_k", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_k)},
|
|
{"_rd_l00", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_l00)},
|
|
{"_rd_s0", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_s0)},
|
|
{"_rd_s", (uintptr_t)IG_ARCHIVE_STRIP((const void *)&rd_s)},
|
|
};
|
|
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i++) {
|
|
if (ig_strcmp(name, table[i].sym) == 0) return table[i].fn;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* 扫描所有已加载非系统动态库的非认证 arm64 GOT/Lazy 槽;缺槽或表结构异常标为不可验证。 */
|
|
static int locate_bindings(void) {
|
|
trackedCount = 0;
|
|
uint32_t image_total = _dyld_image_count();
|
|
for (uint32_t index = 0; index < image_total; index++) {
|
|
const char *path = _dyld_get_image_name(index);
|
|
if (!path || is_system_image(path) || has_suffix(path, "/RDCoreIO.framework/RDCoreIO")) continue;
|
|
const struct mach_header_64 *header = (const void *)_dyld_get_image_header(index);
|
|
if (!header || header->magic != MH_MAGIC_64 || header->filetype != MH_DYLIB) continue;
|
|
if (header->cputype != CPU_TYPE_ARM64 || (header->cpusubtype & 0xffffff) == 2) return 0;
|
|
uintptr_t slide = (uintptr_t)_dyld_get_image_vmaddr_slide(index);
|
|
const struct segment_command_64 *linkedit = NULL, *text_seg = NULL;
|
|
const struct symtab_command *symtab = NULL;
|
|
const struct dysymtab_command *dysymtab = NULL;
|
|
const uint8_t *command = (const void *)(header + 1);
|
|
const uint8_t *end = command + header->sizeofcmds;
|
|
for (uint32_t i = 0; i < header->ncmds; i++) {
|
|
if (command + sizeof(struct load_command) > end) return 0;
|
|
const struct load_command *lc = (const void *)command;
|
|
if (lc->cmdsize < 8 || lc->cmdsize % 8 || lc->cmdsize > (size_t)(end - command)) return 0;
|
|
if (lc->cmd == LC_SEGMENT_64) {
|
|
if (lc->cmdsize < sizeof(struct segment_command_64)) return 0;
|
|
const struct segment_command_64 *seg = (const void *)lc;
|
|
if (sizeof(*seg) + (uint64_t)seg->nsects * sizeof(struct section_64) != lc->cmdsize) return 0;
|
|
if (!ig_strcmp(seg->segname, SEG_LINKEDIT)) linkedit = seg;
|
|
else if (!ig_strcmp(seg->segname, SEG_TEXT)) text_seg = seg;
|
|
} else if (lc->cmd == LC_SYMTAB && lc->cmdsize == sizeof(*symtab)) symtab = (const void *)lc;
|
|
else if (lc->cmd == LC_DYSYMTAB && lc->cmdsize == sizeof(*dysymtab)) dysymtab = (const void *)lc;
|
|
command += lc->cmdsize;
|
|
}
|
|
if (!linkedit || !text_seg || !symtab || !dysymtab || command != end) continue;
|
|
uint64_t lo = linkedit->fileoff, hi = lo + linkedit->filesize;
|
|
if (hi < lo || symtab->symoff < lo || (uint64_t)symtab->symoff + (uint64_t)symtab->nsyms * 16 > hi
|
|
|| symtab->stroff < lo || (uint64_t)symtab->stroff + symtab->strsize > hi
|
|
|| dysymtab->indirectsymoff < lo
|
|
|| (uint64_t)dysymtab->indirectsymoff + (uint64_t)dysymtab->nindirectsyms * 4 > hi) return 0;
|
|
uintptr_t base = slide + (uintptr_t)(linkedit->vmaddr - lo);
|
|
uintptr_t text_lo = slide + (uintptr_t)text_seg->vmaddr;
|
|
uintptr_t text_hi = text_lo + (uintptr_t)text_seg->vmsize;
|
|
const struct nlist_64 *symbols = (const void *)(base + symtab->symoff);
|
|
const char *strings = (const void *)(base + symtab->stroff);
|
|
const uint32_t *indirect = (const void *)(base + dysymtab->indirectsymoff);
|
|
command = (const void *)(header + 1);
|
|
for (uint32_t i = 0; i < header->ncmds; i++) {
|
|
const struct load_command *lc = (const void *)command;
|
|
if (lc->cmd == LC_SEGMENT_64) {
|
|
const struct segment_command_64 *seg = (const void *)lc;
|
|
const struct section_64 *sections = (const void *)(seg + 1);
|
|
for (uint32_t j = 0; j < seg->nsects; j++) {
|
|
const struct section_64 *s = sections + j;
|
|
uint32_t stype = s->flags & SECTION_TYPE;
|
|
if (stype != S_NON_LAZY_SYMBOL_POINTERS && stype != S_LAZY_SYMBOL_POINTERS) continue;
|
|
if (s->size % 8 || (uint64_t)s->reserved1 + s->size / 8 > dysymtab->nindirectsyms) return 0;
|
|
for (uint64_t k = 0; k < s->size / 8; k++) {
|
|
uint32_t symbol = indirect[s->reserved1 + k];
|
|
if (symbol & (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) continue;
|
|
if (symbol >= symtab->nsyms) return 0;
|
|
uint32_t strx = symbols[symbol].n_un.n_strx;
|
|
if (strx >= symtab->strsize) return 0;
|
|
const char *name = strings + strx;
|
|
if (!memchr(name, 0, symtab->strsize - strx)) return 0;
|
|
uintptr_t expected_fn = expected_wrapper_for_symbol(name);
|
|
if (!expected_fn) continue;
|
|
if (trackedCount >= IG_ARCHIVE_MAX_SLOTS) return 0;
|
|
uintptr_t slot_addr = slide + (uintptr_t)s->addr + (uintptr_t)(k * 8);
|
|
uintptr_t probe = 0;
|
|
if (!read_bytes(slot_addr, &probe, sizeof(probe))) return 0;
|
|
trackedSlots[trackedCount++] = (IGArchiveSlot){
|
|
.slot_addr = slot_addr,
|
|
.expected_fn = expected_fn,
|
|
.text_lo = text_lo,
|
|
.text_hi = text_hi,
|
|
.is_lazy = (uint8_t)(stype == S_LAZY_SYMBOL_POINTERS)
|
|
};
|
|
}
|
|
}
|
|
}
|
|
command += lc->cmdsize;
|
|
}
|
|
}
|
|
return trackedCount > 0;
|
|
}
|
|
|
|
static void initialize(void) {
|
|
bindingReady = locate_bindings();
|
|
for (unsigned i = 0; i < IG_ARCHIVE_FUNCTION_COUNT; i++) {
|
|
IGResolveResult result = ig_resolve_verified(NULL, names[i], name_lengths[i], ig_response_capture, &initialReason[i]);
|
|
initial[i] = result.status;
|
|
if (result.status != IG_CLEAR || !result.address) continue;
|
|
if (!read_bytes((uintptr_t)IG_ARCHIVE_STRIP(result.address), prefixes[i], 16)) {
|
|
initial[i] = IG_UNAVAILABLE;
|
|
continue;
|
|
}
|
|
expected[i] = (uintptr_t)result.address;
|
|
atomic_store_explicit(&targets[i], expected[i], memory_order_release);
|
|
}
|
|
}
|
|
|
|
static int prepare(void) {
|
|
if (resolving) return 0;
|
|
resolving = 1;
|
|
int result = pthread_once(&once, initialize);
|
|
resolving = 0;
|
|
return result == 0;
|
|
}
|
|
|
|
__attribute__((constructor))
|
|
static void ig_archive_warmup(void) {
|
|
(void)prepare();
|
|
}
|
|
|
|
static IGStatus validate(unsigned index, uintptr_t *address, int *reason) {
|
|
*reason = initialReason[index];
|
|
if (initial[index] != IG_CLEAR) return initial[index];
|
|
*reason = 107;
|
|
uintptr_t current = atomic_load_explicit(&targets[index], memory_order_acquire);
|
|
if (!current || current != expected[index]) return IG_DETECTED;
|
|
unsigned char bytes[16];
|
|
if (!read_bytes((uintptr_t)IG_ARCHIVE_STRIP((const void *)current), bytes, 16)) return IG_UNAVAILABLE;
|
|
if (ig_memcmp(bytes, prefixes[index], 16)) return IG_DETECTED;
|
|
*reason = IG_REASON_VERIFICATION_UNAVAILABLE;
|
|
IGStatus status = ig_verify_function((void *)current, IG_DIAG_STR("archive-target"), ig_response_capture, reason);
|
|
if (status != IG_CLEAR) return status;
|
|
*address = current;
|
|
return IG_CLEAR;
|
|
}
|
|
|
|
static IGStatus check_bindings(void) {
|
|
#ifdef IG_ARCHIVE_TESTING
|
|
/* 宿主 ABI fixture 不装载外部动态 SDK;此豁免不能编入业务包。 */
|
|
return IG_CLEAR;
|
|
#else
|
|
if (!bindingReady || trackedCount == 0) return IG_UNAVAILABLE;
|
|
for (size_t i = 0; i < trackedCount; i++) {
|
|
const IGArchiveSlot *slot = &trackedSlots[i];
|
|
uintptr_t raw = *(const volatile uintptr_t *)slot->slot_addr;
|
|
uintptr_t cur = (uintptr_t)IG_ARCHIVE_STRIP((const void *)raw);
|
|
if (cur == slot->expected_fn) continue;
|
|
if (slot->is_lazy && cur >= slot->text_lo && cur < slot->text_hi) continue;
|
|
return IG_DETECTED;
|
|
}
|
|
return IG_CLEAR;
|
|
#endif
|
|
}
|
|
|
|
__attribute__((noinline))
|
|
static uintptr_t checked(unsigned index) {
|
|
COUNT(index, 0);
|
|
if (!prepare()) { COUNT(index, 2); errno = EDEADLK; return 0; }
|
|
uintptr_t address = 0;
|
|
int reason = 107;
|
|
IGStatus status = check_bindings();
|
|
if (status == IG_CLEAR) status = validate(index, &address, &reason);
|
|
if (status != IG_CLEAR) {
|
|
COUNT(index, 2);
|
|
ig_response_handle(status, reason);
|
|
errno = status == IG_DETECTED ? EACCES : ENOSYS;
|
|
return 0;
|
|
}
|
|
COUNT(index, 1);
|
|
return address;
|
|
}
|
|
|
|
__attribute__((noinline)) FILE *rd_a0(const char *path, const char *mode) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(0);
|
|
if (!address) return NULL;
|
|
errno = incoming;
|
|
return ((FILE *(*)(const char *, const char *))address)(path, mode);
|
|
}
|
|
__attribute__((noinline)) FILE *rd_f(const char *path, const char *mode) {
|
|
return rd_a0(path, mode);
|
|
}
|
|
__attribute__((noinline)) size_t rd_a1(void *ptr, size_t size, size_t count, FILE *stream) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(1);
|
|
if (!address) return 0;
|
|
errno = incoming;
|
|
return ((size_t (*)(void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
|
}
|
|
__attribute__((noinline)) size_t rd_a2(const void *ptr, size_t size, size_t count, FILE *stream) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(2);
|
|
if (!address) return 0;
|
|
errno = incoming;
|
|
return ((size_t (*)(const void *, size_t, size_t, FILE *))address)(ptr, size, count, stream);
|
|
}
|
|
__attribute__((noinline)) int rd_a3(FILE *stream) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(3);
|
|
if (!address) return EOF;
|
|
errno = incoming;
|
|
return ((int (*)(FILE *))address)(stream);
|
|
}
|
|
__attribute__((noinline)) int rd_a4(const char *path) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(4);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((int (*)(const char *))address)(path);
|
|
}
|
|
__attribute__((noinline)) int rd_u00(const char *path) {
|
|
return rd_a4(path);
|
|
}
|
|
|
|
static int ig_open_impl(const char *path, int flags, int mode) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(5);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
int (*fn)(const char *, int, ...) = (void *)address;
|
|
return (flags & O_CREAT) ? fn(path, flags, mode) : fn(path, flags);
|
|
}
|
|
__attribute__((noinline)) int rd_o(const char *path, int flags, ...) {
|
|
int mode = 0;
|
|
if (flags & O_CREAT) {
|
|
va_list args;
|
|
va_start(args, flags);
|
|
mode = va_arg(args, int);
|
|
va_end(args);
|
|
}
|
|
return ig_open_impl(path, flags, mode);
|
|
}
|
|
__attribute__((noinline)) int rdo(const char *path, int flags, ...) {
|
|
int mode = 0;
|
|
if (flags & O_CREAT) {
|
|
va_list args;
|
|
va_start(args, flags);
|
|
mode = va_arg(args, int);
|
|
va_end(args);
|
|
}
|
|
return ig_open_impl(path, flags, mode);
|
|
}
|
|
|
|
__attribute__((noinline)) ssize_t rd_r(int fd, void *buffer, size_t count) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(6);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((ssize_t (*)(int, void *, size_t))address)(fd, buffer, count);
|
|
}
|
|
__attribute__((noinline)) ssize_t rdr(int fd, void *buffer, size_t count) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(6);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((ssize_t (*)(int, void *, size_t))address)(fd, buffer, count);
|
|
}
|
|
__attribute__((noinline)) ssize_t rd_w0(int fd, const void *buffer, size_t count) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(7);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((ssize_t (*)(int, const void *, size_t))address)(fd, buffer, count);
|
|
}
|
|
__attribute__((noinline)) ssize_t rd_w(int fd, const void *buffer, size_t count) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(7);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((ssize_t (*)(int, const void *, size_t))address)(fd, buffer, count);
|
|
}
|
|
__attribute__((noinline)) int rd_c0(int fd) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(8);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((int (*)(int))address)(fd);
|
|
}
|
|
__attribute__((noinline)) int rd_c(int fd) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(8);
|
|
if (!address) return -1;
|
|
errno = incoming;
|
|
return ((int (*)(int))address)(fd);
|
|
}
|
|
__attribute__((noinline)) pid_t rd_p00(void) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(9);
|
|
if (!address) return (pid_t)-1;
|
|
errno = incoming;
|
|
return ((pid_t (*)(void))address)();
|
|
}
|
|
__attribute__((noinline)) pid_t rd_p0(void) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(9);
|
|
if (!address) return (pid_t)-1;
|
|
errno = incoming;
|
|
return ((pid_t (*)(void))address)();
|
|
}
|
|
__attribute__((noinline, noreturn)) void rd_e(int status) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(10);
|
|
if (address) {
|
|
errno = incoming;
|
|
((void (*)(int))address)(status);
|
|
}
|
|
_exit(status);
|
|
__builtin_trap();
|
|
}
|
|
__attribute__((noinline)) pid_t rd_k(void) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(11);
|
|
if (!address) return (pid_t)-1;
|
|
errno = incoming;
|
|
return ((pid_t (*)(void))address)();
|
|
}
|
|
__attribute__((noinline)) void *rd_l00(const char *path, int mode) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(12);
|
|
if (!address) return NULL;
|
|
errno = incoming;
|
|
return ((void *(*)(const char *, int))address)(path, mode);
|
|
}
|
|
__attribute__((noinline)) void *rd_s0(void *handle, const char *symbol) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(13);
|
|
if (!address) return NULL;
|
|
errno = incoming;
|
|
return ((void *(*)(void *, const char *))address)(handle, symbol);
|
|
}
|
|
__attribute__((noinline)) void *rd_s(void *handle, const char *symbol) {
|
|
int incoming = errno;
|
|
uintptr_t address = checked(13);
|
|
if (!address) return NULL;
|
|
errno = incoming;
|
|
return ((void *(*)(void *, const char *))address)(handle, symbol);
|
|
}
|
|
|
|
/* 返回具体原因,避免把启动时的跳转形态启发式误升级为已确认篡改。 */
|
|
int rd_io_ev(void) {
|
|
int incoming = errno, result = 0;
|
|
if (!prepare()) { errno = incoming; return -1; }
|
|
IGStatus binding = check_bindings();
|
|
if (binding == IG_DETECTED) result = 107;
|
|
else if (binding == IG_UNAVAILABLE) result = -1;
|
|
for (unsigned i = 0; i < IG_ARCHIVE_FUNCTION_COUNT; i++) {
|
|
uintptr_t address;
|
|
int reason;
|
|
IGStatus current = validate(i, &address, &reason);
|
|
if (current == IG_DETECTED) {
|
|
if (reason == 100 || reason == 107) result = reason;
|
|
else if (result <= 0) result = 101;
|
|
} else if (current == IG_UNAVAILABLE && result == 0) result = -1;
|
|
}
|
|
errno = incoming;
|
|
return result;
|
|
}
|
|
|
|
int rd_io_chk(void) {
|
|
int result = rd_io_ev();
|
|
return result > 0 ? 1 : result;
|
|
}
|
|
|
|
void rd_io_cfg(int enabled, int fd) {
|
|
#if IG_ARCHIVE_HAS_RESPONSE
|
|
ig_response_configure(enabled, fd);
|
|
#else
|
|
(void)enabled;
|
|
(void)fd;
|
|
#endif
|
|
}
|
|
|
|
#ifdef IG_OBSERVATION
|
|
uint64_t ig_archive_counter(unsigned index) {
|
|
return index < IG_ARCHIVE_FUNCTION_COUNT * 3
|
|
? atomic_load_explicit(&counters[index], memory_order_relaxed) : 0;
|
|
}
|
|
#endif
|
|
|
|
#ifdef IG_ARCHIVE_TESTING
|
|
void ig_archive_test_override(unsigned index, void *address) {
|
|
if (index < IG_ARCHIVE_FUNCTION_COUNT && prepare())
|
|
atomic_store_explicit(&targets[index], (uintptr_t)address, memory_order_release);
|
|
}
|
|
void ig_archive_test_reentry(int enabled) { resolving = enabled; }
|
|
#endif
|