#import #import #import #import #import #include "anti_hook/IGObjCBaseline.h" // 优化器能把实例方法编译成一条合法的尾调用。 __attribute__((noinline)) static int readValue(void) { static volatile int value = 7; return value; } @interface IGManifestParent : NSObject - (int)value; + (int)classValue; - (int)other; @end @implementation IGManifestParent - (int)value { return readValue(); } + (int)classValue { return readValue(); } - (int)other { return 42; } @end @interface IGManifestChild : IGManifestParent @end @implementation IGManifestChild @end @interface IGManifestChild (Extra) - (int)extra; @end @implementation IGManifestChild (Extra) - (int)extra { return readValue(); } @end typedef struct { int changed, unavailable; } Counts; static int collect(const IGFinding *finding, void *ctx) { Counts *counts = ctx; counts->changed += finding->status == IG_DETECTED; counts->unavailable += finding->status == IG_UNAVAILABLE; return 0; } int main(int argc, const char **argv) { if (argc != 2) return 1; // 强制 dyld 装入 Swift 类,与真实应用一致地通过运行时名称登记。 extern void ig_load_swift_fixture(void); extern const char *ig_swift_nested_name(void); extern const char *ig_swift_private_name(void); ig_load_swift_fixture(); IGObjCWatch watches[] = { {"IGManifestChild", "-value"}, {"IGManifestChild", "+classValue"}, {"IGManifestChild", "extra"}, {"Fixture.SwiftProbe", "swiftValue"}, {ig_swift_nested_name(), "nestedValue"}, {ig_swift_private_name(), "privateValue"}, }; Method method = class_getInstanceMethod(IGManifestParent.class, @selector(value)); IMP other = class_getMethodImplementation(IGManifestParent.class, @selector(other)); BOOL before = !strcmp(argv[1], "before"), after = !strcmp(argv[1], "after"); BOOL before_block = !strcmp(argv[1], "before-block"), before_anon = !strcmp(argv[1], "before-anon"); void *anon = NULL; if (before) method_setImplementation(method, other); if (before_block) { IMP blk = imp_implementationWithBlock(^int(id self) { (void)self; return 99; }); method_setImplementation(method, blk); } if (before_anon) { anon = mmap(NULL, 4096, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0); if (anon == MAP_FAILED) return 3; Dl_info di; if (dladdr(anon, &di)) return 4; method_setImplementation(method, (IMP)anon); } if (ig_objc_baseline_capture(watches, 6) != 6) return 2; if (after) method_setImplementation(method, other); BOOL traced = !strcmp(argv[1], "different-bytes-traced"); if (traced && ptrace(PT_TRACE_ME, 0, NULL, 0) != 0) return 5; Counts counts = {0}; ig_objc_baseline_check(collect, &counts); if (anon) munmap(anon, 4096); fprintf(stderr, "changed=%d unavailable=%d\n", counts.changed, counts.unavailable); int expect_changed = before || before_block || before_anon || after || !strcmp(argv[1], "different-bytes"); return counts.changed != expect_changed || counts.unavailable != (!strcmp(argv[1], "unavailable") ? 6 : 0); }