Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: Speed and pitch, audio effects and Music Haptics reach Spotify's output on jailbreaks with a read-only import page, through ElleKit (#137)

My-Name-Is-Jeff922125651725
diff --git a/tweak/Sources/Core/SGRebind.h b/tweak/Sources/Core/SGRebind.h
index 5f5d838e..bd2c5312 100644
--- a/tweak/Sources/Core/SGRebind.h
+++ b/tweak/Sources/Core/SGRebind.h
@@ -13,3 +13,16 @@
// Returns YES when at least one slot named `symbol` (without the leading underscore) was repointed.
// `original` receives what the first slot held, which is what the replacement calls on.
BOOL SGRebindImport(const char *symbol, void *replacement, void **original);
+
+// Where the import page cannot be written (a jailbreak that makes Spotify's __DATA_CONST unchangeable: an iPhone 11 on
+// Dopamine, iOS 17.5.1), the function itself is hooked with the jailbreak's own hooker, ElleKit's (Substrate's)
+// MSHookFunction, when it is in the process or can be loaded. Every caller then goes through `replacement`, the
+// system's frameworks too, so a replacement tells Spotify's calls by SGCalledFromMainExecutable and hands the rest to
+// `original`, which must be NULL before the call. NO unless SGJailbroken().
+BOOL SGHookFunctionInline(void *function, void *replacement, void **original);
+// Whether the hooker found is the jailbreak's, not one the app brought inside its bundle: a jailbroken phone, whatever
+// way Vitrine came onto it.
+BOOL SGJailbroken(void);
+// Whether `returnAddress` (__builtin_return_address(0) of a hooked function) is in the code of Spotify's binary, the
+// one SGRebindImport rebinds.
+BOOL SGCalledFromMainExecutable(void *returnAddress);
diff --git a/tweak/Sources/Core/SGRebind.m b/tweak/Sources/Core/SGRebind.m
index fba3294b..2a8d16a9 100644
--- a/tweak/Sources/Core/SGRebind.m
+++ b/tweak/Sources/Core/SGRebind.m
@@ -1,7 +1,13 @@
+#import <dlfcn.h>
+#if __has_feature(ptrauth_calls)
+#import <ptrauth.h>
+#endif
#import <mach/mach.h>
#import <mach-o/dyld.h>
+#import <mach-o/getsect.h>
#import <mach-o/loader.h>
#import <mach-o/nlist.h>
+#import <stdatomic.h>
#import <string.h>
#import "SGRebind.h"
#import "SGLog.h"
@@ -42,8 +48,25 @@ static const struct mach_header_64 *spotifyImage(intptr_t *slide) {
return NULL;
}
+// What protection the page holding `address` has now and at most, for the log when a write is refused.
+static NSString *protectionOf(vm_address_t address) {
+ vm_address_t region = address;
+ vm_size_t size = 0;
+ vm_region_basic_info_data_64_t info;
+ mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
+ mach_port_t object = MACH_PORT_NULL;
+ if (vm_region_64(mach_task_self(), &region, &size, VM_REGION_BASIC_INFO_64, (vm_region_info_t)&info, &count, &object) != KERN_SUCCESS) return @"unknown";
+ // vm_region_64 moves on to the next mapping when there is none at `address`.
+ if (region > address) return @"unmapped";
+ return [NSString stringWithFormat:@"now %d, at most %d, shared %d", info.protection, info.max_protection, info.shared];
+}
+
// Writes one pointer, making its page writable first; a page dyld made read only after binding
-// (__DATA_CONST, __AUTH_CONST) is copied and made read only again.
+// (__DATA_CONST, __AUTH_CONST) is copied and made read only again. Where the copy is refused (KERN_PROTECTION_FAILURE),
+// the page is made writable without the copy; failing that, the slot is left as it is. On an iPhone 11 with a jailbreak (iOS 17.5.1) both were
+// refused (KERN_PROTECTION_FAILURE on a page read only now and readable and writable at most), and the page must not
+// be replaced instead: a copy of it mapped over it with vm_remap left the range with no mapping at all, and Spotify
+// died of SIGBUS at launch (the phone, 2026-10-23).
static BOOL writeSlot(void **slot, void *value, BOOL readOnly) {
vm_size_t page = vm_page_size;
vm_address_t start = (vm_address_t)slot & ~(page - 1);
@@ -54,7 +77,8 @@ static BOOL writeSlot(void **slot, void *value, BOOL readOnly) {
if (retry == KERN_SUCCESS) result = KERN_SUCCESS;
}
if (result != KERN_SUCCESS) {
- SGLog(@"rebind: vm_protect failed (%d)", result);
+ static atomic_int logged;
+ if (atomic_fetch_add(&logged, 1) < 3) SGLog(@"rebind: vm_protect failed (%d), the page %@", result, protectionOf(start));
return NO;
}
*slot = value;
@@ -131,3 +155,73 @@ BOOL SGRebindImport(const char *symbol, void *replacement, void **original) {
if (unbound) SGLog(@"rebind: %s is not bound yet, left as it is", symbol);
return rebound;
}
+
+#pragma mark - the jailbreak's hooker
+
+typedef void (*SGMSHookFunction)(void *symbol, void *replace, void **result);
+
+// MSHookFunction as the process has it, or from ElleKit's libraries where a rootless or a rootful jailbreak keeps them.
+static SGMSHookFunction hooker(void) {
+ static SGMSHookFunction found;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ // The jailbreak's paths come first: an IPA can bring a substrate framework of its own, and that one is in the
+ // process on every phone.
+ const char *paths[] = {"/var/jb/usr/lib/libellekit.dylib", "/var/jb/usr/lib/libsubstrate.dylib",
+ "/usr/lib/libellekit.dylib", "/usr/lib/libsubstrate.dylib"};
+ for (size_t i = 0; !found && i < sizeof paths / sizeof *paths; i++) {
+ void *handle = dlopen(paths[i], RTLD_NOW);
+ if (handle) found = (SGMSHookFunction)dlsym(handle, "MSHookFunction");
+ }
+ if (!found) found = (SGMSHookFunction)dlsym(RTLD_DEFAULT, "MSHookFunction");
+ SGLog(@"rebind: MSHookFunction %@", found ? @"is there" : @"is not there");
+ });
+ return found;
+}
+
+// A hooker that came inside the app (an IPA put together with a substrate framework of its own) is no jailbreak: the
+// one counted is the jailbreak's, loaded from outside the app's bundle (the phone, 2026-10-24: Everywhere showed on a
+// phone with none).
+BOOL SGJailbroken(void) {
+ static BOOL jailbroken;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ SGMSHookFunction hook = hooker();
+ Dl_info info;
+ if (!hook || !dladdr((const void *)hook, &info) || !info.dli_fname) return;
+ NSString *image = [NSString stringWithUTF8String:info.dli_fname];
+ // The app's own bundle, or the app's folder under LiveContainer (/var/mobile/Containers/...). Not every
+ // /var/containers path: roothide keeps the jailbreak's files in /var/containers/Bundle/Application/.jbroot-*.
+ NSString *bundle = NSBundle.mainBundle.bundlePath;
+ jailbroken = !(bundle.length && [image hasPrefix:bundle]) && ![image containsString:@"/mobile/Containers/"];
+ SGLog(@"rebind: MSHookFunction comes from %@, %@", image, jailbroken ? @"the jailbreak's" : @"the app's own: no jailbreak");
+ });
+ return jailbroken;
+}
+
+BOOL SGHookFunctionInline(void *function, void *replacement, void **original) {
+ if (!SGJailbroken()) return NO;
+ // Finds Spotify's code now, not on the first hooked call, which can come on an audio thread.
+ SGCalledFromMainExecutable(NULL);
+ hooker()(function, replacement, original);
+ return *original != NULL;
+}
+
+BOOL SGCalledFromMainExecutable(void *returnAddress) {
+ static uintptr_t start, end;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ intptr_t slide = 0;
+ const struct mach_header_64 *header = spotifyImage(&slide);
+ if (!header) return;
+ unsigned long size = 0;
+ start = (uintptr_t)getsegmentdata(header, SEG_TEXT, &size);
+ end = start + size;
+ });
+ // A return address carries a pointer authentication code in its top bits on arm64e.
+#if __has_feature(ptrauth_calls)
+ returnAddress = ptrauth_strip(returnAddress, ptrauth_key_return_address);
+#endif
+ uintptr_t address = (uintptr_t)returnAddress;
+ return address >= start && address < end;
+}
diff --git a/tweak/Sources/Shared/Player/SpeedPitch.x b/tweak/Sources/Shared/Player/SpeedPitch.x
index 8ea5ccb5..5640f6ed 100644
--- a/tweak/Sources/Shared/Player/SpeedPitch.x
+++ b/tweak/Sources/Shared/Player/SpeedPitch.x
@@ -120,7 +120,7 @@ static _Atomic(Output *) sg_music; // the output carrying the processo
static pthread_mutex_t sg_outputsLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
static dispatch_queue_t sg_outputsQueue;
static SGPlayerOutputWatcher sg_watchers[kMaxWatchers];
-static BOOL sg_reachable; // Spotify's AudioOutputUnitStart could be rebound
+static BOOL sg_reachable; // Spotify's AudioOutputUnitStart could be rebound or hooked
// The unit in use and whether the render thread runs it: in the music's chain (pull), else in place (pitch only).
static _Atomic(SGTimePitch *) sg_pull, sg_inPlace;
@@ -1166,6 +1166,41 @@ BOOL SGPlayerMusicClientFormat(AudioStreamBasicDescription *format) {
return read;
}
+// The jailbreak's way in (SGHookFunctionInline), where Spotify's import page cannot be written: the functions
+// themselves are hooked, every caller comes through, and only Spotify's own calls go to the replacements above,
+// as only they would through its imports. The rest, the system's frameworks and the mod's own units, go straight
+// to the original.
+#define SG_FROM_SPOTIFY SGCalledFromMainExecutable(__builtin_return_address(0))
+
+static OSStatus inlineSetProperty(AudioUnit unit, AudioUnitPropertyID property, AudioUnitScope scope, AudioUnitElement element,
+ const void *data, UInt32 size) {
+ return SG_FROM_SPOTIFY ? setProperty(unit, property, scope, element, data, size) : sg_setProperty(unit, property, scope, element, data, size);
+}
+
+static OSStatus inlineStartOutput(AudioUnit unit) {
+ return SG_FROM_SPOTIFY ? startOutput(unit) : sg_startOutput(unit);
+}
+
+static OSStatus inlineStopOutput(AudioUnit unit) {
+ return SG_FROM_SPOTIFY ? stopOutput(unit) : sg_stopOutput(unit);
+}
+
+static OSStatus inlineDisposeUnit(AudioComponentInstance unit) {
+ return SG_FROM_SPOTIFY ? disposeUnit(unit) : sg_dispose(unit);
+}
+
+// Start, stop and dispose all or none: a start seen without its stop or its dispose would leave an output the mod
+// thinks is running. A hook cannot be undone, so stop and dispose go first, then start, which makes the output the
+// mod's, and only then the optional speed hook, which would otherwise take Spotify's output with no start seen.
+static BOOL hookInline(void) {
+ if (!SGHookFunctionInline((void *)AudioOutputUnitStop, (void *)inlineStopOutput, (void **)&sg_stopOutput)) return NO;
+ if (!SGHookFunctionInline((void *)AudioComponentInstanceDispose, (void *)inlineDisposeUnit, (void **)&sg_dispose)) return NO;
+ if (!SGHookFunctionInline((void *)AudioOutputUnitStart, (void *)inlineStartOutput, (void **)&sg_startOutput)) return NO;
+ if (!sg_setProperty) SGHookFunctionInline((void *)AudioUnitSetProperty, (void *)inlineSetProperty, (void **)&sg_setProperty);
+ SGLog(@"audio: Spotify's output reached through the jailbreak's hooks (speed %@)", sg_setProperty ? @"too" : @"not");
+ return YES;
+}
+
static void rebind(void) {
static dispatch_once_t once;
dispatch_once(&once, ^{
@@ -1173,20 +1208,20 @@ static void rebind(void) {
mach_timebase_info(&timebase);
sg_secondsPerTick = (double)timebase.numer / timebase.denom / 1e9;
sg_outputsQueue = dispatch_queue_create("spotifyglass.outputs", dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0));
- if (!SGRebindImport("AudioUnitSetProperty", setProperty, (void **)&sg_setProperty) || !sg_setProperty) {
- sg_setProperty = NULL;
- SGLog(@"redesign speed: Spotify does not import AudioUnitSetProperty, the menu offers pitch only");
- }
+ if (!SGRebindImport("AudioUnitSetProperty", setProperty, (void **)&sg_setProperty) || !sg_setProperty) sg_setProperty = NULL;
sg_reachable = SGRebindImport("AudioOutputUnitStart", startOutput, (void **)&sg_startOutput) && sg_startOutput;
if (!sg_reachable) {
- SGLog(@"audio: Spotify does not import AudioOutputUnitStart, its output cannot be reached");
- return;
- }
- if (!SGRebindImport("AudioOutputUnitStop", stopOutput, (void **)&sg_stopOutput) || !sg_stopOutput) sg_stopOutput = AudioOutputUnitStop;
- if (!SGRebindImport("AudioComponentInstanceDispose", disposeUnit, (void **)&sg_dispose) || !sg_dispose) {
- sg_dispose = AudioComponentInstanceDispose;
- SGLog(@"audio: Spotify does not import AudioComponentInstanceDispose, a disposed output is not seen");
+ sg_startOutput = NULL;
+ sg_reachable = hookInline();
+ if (!sg_reachable) SGLog(@"audio: Spotify's AudioOutputUnitStart could be neither rebound nor hooked, its output cannot be reached");
+ } else {
+ if (!SGRebindImport("AudioOutputUnitStop", stopOutput, (void **)&sg_stopOutput) || !sg_stopOutput) sg_stopOutput = AudioOutputUnitStop;
+ if (!SGRebindImport("AudioComponentInstanceDispose", disposeUnit, (void **)&sg_dispose) || !sg_dispose) {
+ sg_dispose = AudioComponentInstanceDispose;
+ SGLog(@"audio: Spotify does not import AudioComponentInstanceDispose, a disposed output is not seen");
+ }
}
+ if (!sg_setProperty) SGLog(@"redesign speed: Spotify's AudioUnitSetProperty could be neither rebound nor hooked, the menu offers pitch only");
});
}