Skip to content

Commit

Permalink
_availability_version_check hook
Browse files Browse the repository at this point in the history
  • Loading branch information
asdfugil committed Feb 4, 2024
1 parent f5d5483 commit e383764
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/systemhook/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,97 @@ int reboot3_hook(uint64_t howto, ...)
return 0;
}

typedef struct {
uint32_t platform;
uint32_t version;
} DyldBuildVersion;

__API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), bridgeos(3.0))
uint32_t dyld_get_active_platform(void);

#define CONSTRUCT_V(major, minor, subminor) ((major & 0xffff) << 16) | ((minor & 0xff) << 8) | (subminor & 0xff)

__API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), bridgeos(4.0))
bool _availability_version_check(uint32_t count, DyldBuildVersion versions[]);

// this hook is used to make __builtin_available work normally in platform mismatched binaries
__API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), bridgeos(4.0))
bool _availability_version_check_hook(uint32_t count, DyldBuildVersion versions[]) {
uint32_t current_plat = dyld_get_active_platform();
for (int i = 0; i < count; i++) {
DyldBuildVersion* version = &versions[i];
if (current_plat == version->platform) continue;
uint32_t major = (version->version >> 16) & 0xffff;
uint32_t minor = (version->version >> 8) & 0xff;
uint32_t subminor = version->version & 0xff;
switch (current_plat) {
case PLATFORM_IOS:
case PLATFORM_TVOS:
switch (version->platform) {
case PLATFORM_MACOS:
if (major == 10) {
major = minor - 2;
minor = subminor;
subminor = 0;
} else if (major > 10) major += 3;
else major = 2;
break;
case PLATFORM_BRIDGEOS:
major += 9;
break;
case PLATFORM_WATCHOSSIMULATOR:
major += 7;
break;
case PLATFORM_VISIONOS:
case PLATFORM_VISIONOSSIMULATOR:
major += 16;
break;
default:
break;
}
break;
case PLATFORM_BRIDGEOS:
switch (version->platform) {
case PLATFORM_MACOS:
if (major == 10) {
major = minor - 11;
minor = subminor;
subminor = 0;
} else if (major > 10) major -= 6;
else major = 2;
break;
case PLATFORM_IOS:
case PLATFORM_TVOS:
case PLATFORM_IOSSIMULATOR:
case PLATFORM_TVOSSIMULATOR:
major -= 9;
break;
case PLATFORM_WATCHOSSIMULATOR:
major -= 2;
break;
case PLATFORM_VISIONOS:
case PLATFORM_VISIONOSSIMULATOR:
major += 7;
break;
default:
break;
}
break;
default:
break;
}
version->version = CONSTRUCT_V(major, minor, subminor);
version->platform = current_plat;
}
if (!_availability_version_check) {
fprintf(stderr, "_availability_version_check unavailable\n");
return false;
}
bool retval = _availability_version_check(count, versions);
return retval;
}


DYLD_INTERPOSE(posix_spawn_hook, posix_spawn)
DYLD_INTERPOSE(posix_spawnp_hook, posix_spawnp)
DYLD_INTERPOSE(execve_hook, execve)
Expand All @@ -615,3 +706,6 @@ DYLD_INTERPOSE(vfork_hook, vfork)
DYLD_INTERPOSE(forkpty_hook, forkpty)
DYLD_INTERPOSE(daemon_hook, daemon)
DYLD_INTERPOSE(reboot3_hook, reboot3)

__API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), bridgeos(4.0))
DYLD_INTERPOSE(_availability_version_check_hook, _availability_version_check)

0 comments on commit e383764

Please sign in to comment.