/* 构建时由 objc_method_baseline.py 从签名前的镜像生成。 */ #ifndef IG_METHOD_MANIFEST_H #define IG_METHOD_MANIFEST_H #include "../dispatch/IGDispatch.h" #include #include #include #include #include #include #include #include #include #include #include typedef struct { unsigned char key[32], uuid[16]; uint64_t offset; uint32_t length; unsigned char digest[CC_SHA256_DIGEST_LENGTH], reserved[4]; } IGMethodRecord; _Static_assert(sizeof(IGMethodRecord) == 96, "IGMethods row layout"); static const IGMethodRecord *ig_records; static size_t ig_record_count; static pthread_once_t ig_manifest_once = PTHREAD_ONCE_INIT; static const uint8_t *ig_method_image_uuid(const struct mach_header *header) { if (!header || header->magic != MH_MAGIC_64) return NULL; const uint8_t *p = (const uint8_t *)header + sizeof(struct mach_header_64); const uint8_t *end = p + header->sizeofcmds; for (uint32_t i = 0; i < header->ncmds && (size_t)(end-p) >= sizeof(struct load_command); ++i) { const struct load_command *cmd = (const void *)p; if (cmd->cmdsize < sizeof(*cmd) || cmd->cmdsize > (size_t)(end-p)) return NULL; if (cmd->cmd == LC_UUID && cmd->cmdsize == sizeof(struct uuid_command)) return ((const struct uuid_command *)cmd)->uuid; p += cmd->cmdsize; } return NULL; } static void ig_load_method_manifest(void) { char path[PATH_MAX]; uint32_t size = sizeof(path); if (_NSGetExecutablePath(path, &size)) return; char *slash = strrchr(path, '/'); if (!slash || sizeof("IGMethods.bin") > sizeof(path)-(size_t)(slash+1-path)) return; memcpy(slash+1, "IGMethods.bin", sizeof("IGMethods.bin")); int fd = IG_dispatch_open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); if (fd < 0) return; struct stat st; if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && st.st_size >= 24 && (st.st_size-24) % sizeof(IGMethodRecord) == 0) { size_t len = (size_t)st.st_size; void *mem = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); if (mem != MAP_FAILED) { const uint8_t *bytes = mem; const uint8_t *uuid = ig_method_image_uuid(_dyld_get_image_header(0)); if (uuid && !memcmp(bytes, "IGMB0001", 8) && !memcmp(bytes+8, uuid, 16)) { ig_records = (const void *)(bytes+24); ig_record_count = (len-24) / sizeof(IGMethodRecord); } else { munmap(mem, len); } } } IG_dispatch_close(fd); } static int ig_compare_method_key(const void *key, const void *record) { return memcmp(key, ((const IGMethodRecord *)record)->key, 32); } static const IGMethodRecord *ig_expected_method(Class cls, SEL sel, int class_method, Method method) { pthread_once(&ig_manifest_once, ig_load_method_manifest); if (!ig_records) return NULL; Class owner = class_method ? object_getClass((id)cls) : cls; Class parent; while ((parent = class_getSuperclass(owner)) && class_getInstanceMethod(parent, sel) == method) owner = parent; char name[1024]; int n = snprintf(name, sizeof(name), "%c%s %s", class_method ? '+' : '-', class_getName(owner), sel_getName(sel)); if (n < 0 || (size_t)n >= sizeof(name)) return NULL; unsigned char key[CC_SHA256_DIGEST_LENGTH]; CC_SHA256(name, (CC_LONG)n, key); return bsearch(key, ig_records, ig_record_count, sizeof(*ig_records), ig_compare_method_key); } #endif