|
| 1 | +// |
| 2 | +// interpose.c |
| 3 | +// SharedExample |
| 4 | +// |
| 5 | +// Created by Kyle on 2025/10/3. |
| 6 | +// |
| 7 | + |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <dlfcn.h> |
| 11 | +#include <AttributeGraph/AttributeGraph-umbrella.h> |
| 12 | + |
| 13 | +// Forward declare the original |
| 14 | +extern bool kdebug_is_enabled(uint32_t debugid); |
| 15 | + |
| 16 | +// Our replacement |
| 17 | +static bool my_kdebug_is_enabled(uint32_t debugid) { |
| 18 | + return true; |
| 19 | +} |
| 20 | + |
| 21 | +extern const void *$s15OpenSwiftUICore5ColorV8ResolvedVN; |
| 22 | + |
| 23 | +const void *_OpenSwiftUI_ColorResolvedNTD(void) { |
| 24 | + return &$s15OpenSwiftUICore5ColorV8ResolvedVN; |
| 25 | +} |
| 26 | + |
| 27 | +extern bool swift_dynamicCast(void *dest, void *src, |
| 28 | + const void *srcType, |
| 29 | + const void *targetType, |
| 30 | + uint64_t flags); |
| 31 | + |
| 32 | +static bool my_swift_dynamicCast(void *dest, void *src, const void *srcType, const void *targetType, uint64_t flags) { |
| 33 | + CFStringRef target_description = AGTypeDescription((AGTypeID)targetType); |
| 34 | + // Check if target_description contains "Color.Resolved" |
| 35 | + if (target_description != NULL) { |
| 36 | + CFRange range = CFStringFind(target_description, CFSTR("Color.Resolved"), 0); |
| 37 | + if (range.location != kCFNotFound) { |
| 38 | + // First try the original cast, if it fails try with OpenSwiftUI's Color.Resolved |
| 39 | + return swift_dynamicCast(dest, src, srcType, targetType, flags) || |
| 40 | + swift_dynamicCast(dest, src, srcType, _OpenSwiftUI_ColorResolvedNTD(), flags); |
| 41 | + } |
| 42 | + } |
| 43 | + return swift_dynamicCast(dest, src, srcType, targetType, flags); |
| 44 | +} |
| 45 | + |
| 46 | +// Interpose using Mach-O section |
| 47 | +typedef struct interpose_s { |
| 48 | + const void *replacement; |
| 49 | + const void *original; |
| 50 | +} interpose_t; |
| 51 | + |
| 52 | +__attribute__((used)) static const interpose_t interposers[] |
| 53 | + __attribute__((section("__DATA, __interpose"))) = { |
| 54 | + // Interpose kdebug_is_enabled to always return true to perform Signpost testing with Instruments |
| 55 | + { (const void *)my_kdebug_is_enabled, (const void *)kdebug_is_enabled }, |
| 56 | + // Interpose swift_dynamicCast to handle casts to SwiftUI's internal Color.Resolved type to fix SwiftUI.ShapeLayerHelper visit check for Shape.fill API |
| 57 | + { (const void *)my_swift_dynamicCast, (const void *)swift_dynamicCast }, |
| 58 | +}; |
0 commit comments