Skip to content

Commit

Permalink
Add option to open app with JIT
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduytran0 committed Jan 23, 2024
1 parent 0cc5ab1 commit 647f430
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion RootHelper/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ trollstorehelper_CODESIGN_FLAGS = --entitlements entitlements.plist
trollstorehelper_INSTALL_PATH = /usr/local/bin
trollstorehelper_LIBRARIES = archive
trollstorehelper_FRAMEWORKS = CoreTelephony
trollstorehelper_PRIVATE_FRAMEWORKS = SpringBoardServices BackBoardServices MobileContainerManager FrontBoardServices
trollstorehelper_PRIVATE_FRAMEWORKS = SpringBoardServices BackBoardServices MobileContainerManager FrontBoardServices RunningBoardServices

include $(THEOS_MAKE_PATH)/tool.mk
2 changes: 2 additions & 0 deletions RootHelper/entitlements.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@
<true/>
<key>com.apple.frontboard.shutdown</key>
<true/>
<key>com.apple.runningboard.process-state</key>
<true/>
</dict>
</plist>
3 changes: 3 additions & 0 deletions RootHelper/jit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#import <Foundation/Foundation.h>

int enableJIT(NSString *bundleID);
45 changes: 45 additions & 0 deletions RootHelper/jit.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@import Foundation;
@import Darwin;

@interface RBSProcessPredicate
+ (instancetype)predicateMatchingBundleIdentifier:(NSString *)bundleID;
@end

@interface RBSProcessHandle
+ (instancetype)handleForPredicate:(RBSProcessPredicate *)predicate error:(NSError **)error;
- (int)rbs_pid;
@end

#define PT_DETACH 11
#define PT_ATTACHEXC 14
int ptrace(int request, pid_t pid, caddr_t addr, int data);

int enableJIT(NSString *bundleID) {
#ifdef EMBEDDED_ROOT_HELPER
return -1;
#else
RBSProcessPredicate *predicate = [RBSProcessPredicate predicateMatchingBundleIdentifier:bundleID];
RBSProcessHandle* process = [RBSProcessHandle handleForPredicate:predicate error:nil];
int pid = process.rbs_pid;

if (!pid)
{
return ESRCH;
}

int ret = ptrace(PT_ATTACHEXC, pid, 0, 0);
if (ret == -1)
{
return errno;
}

usleep(100000);
ret = ptrace(PT_DETACH, pid, 0, 0);
if (ret == -1)
{
return errno;
}
return 0;
#endif
}

7 changes: 7 additions & 0 deletions RootHelper/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <mach-o/loader.h>
#import <mach-o/fat.h>
#import "devmode.h"
#import "jit.h"
#ifndef EMBEDDED_ROOT_HELPER
#import "codesign.h"
#import "coretrust_bug.h"
Expand Down Expand Up @@ -1573,6 +1574,12 @@ int MAIN_NAME(int argc, char *argv[], char *envp[])
// Give the system some time to reboot
sleep(1);
}
else if([cmd isEqualToString:@"enable-jit"])
{
if(args.count < 2) return -3;
NSString* userAppId = args.lastObject;
ret = enableJIT(userAppId);
}

NSLog(@"trollstorehelper returning %d", ret);
return ret;
Expand Down
1 change: 1 addition & 0 deletions TrollStore/TSAppInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- (NSAttributedString*)detailedInfoTitle;
- (NSAttributedString*)detailedInfoDescription;
//- (UIImage*)image;
- (BOOL)isDebuggable;
- (void)log;

@end
18 changes: 18 additions & 0 deletions TrollStore/TSAppInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -1165,5 +1165,23 @@ - (void)log
}];
}

- (BOOL)isDebuggable
{
[self loadEntitlements];
__block BOOL debuggable = NO;
[self enumerateAllEntitlements:^(NSString *key, NSObject *value, BOOL *stop)
{
if([key isEqualToString:@"get-task-allow"])
{
NSNumber* valueNum = (NSNumber*)value;
if(valueNum && [valueNum isKindOfClass:NSNumber.class])
{
debuggable = valueNum.boolValue;
*stop = YES;
}
}
}];
return debuggable;
}

@end
25 changes: 23 additions & 2 deletions TrollStore/TSAppTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocum
[TSInstallationController presentInstallationAlertIfEnabledForFile:pathToIPA isRemoteInstall:NO completion:nil];
}

- (void)openAppPressedForRowAtIndexPath:(NSIndexPath*)indexPath
- (void)openAppPressedForRowAtIndexPath:(NSIndexPath*)indexPath enableJIT:(BOOL)enableJIT
{
TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance];

Expand All @@ -211,6 +211,17 @@ - (void)openAppPressedForRowAtIndexPath:(NSIndexPath*)indexPath
[didFailController addAction:cancelAction];
[TSPresentationDelegate presentViewController:didFailController animated:YES completion:nil];
}
else if (enableJIT)
{
int ret = [appsManager enableJITForBundleID:appId];
if (ret != 0)
{
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:[NSString stringWithFormat:@"Error enabling JIT: trollstorejithelper returned %d", ret] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil];
[errorAlert addAction:closeAction];
[TSPresentationDelegate presentViewController:errorAlert animated:YES completion:nil];
}
}
}

- (void)showDetailsPressedForRowAtIndexPath:(NSIndexPath*)indexPath
Expand Down Expand Up @@ -424,11 +435,21 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

UIAlertAction* openAction = [UIAlertAction actionWithTitle:@"Open" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
[self openAppPressedForRowAtIndexPath:indexPath];
[self openAppPressedForRowAtIndexPath:indexPath enableJIT:NO];
[self deselectRow];
}];
[appSelectAlert addAction:openAction];

if ([appInfo isDebuggable])
{
UIAlertAction* openWithJITAction = [UIAlertAction actionWithTitle:@"Open with JIT" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
[self openAppPressedForRowAtIndexPath:indexPath enableJIT:YES];
[self deselectRow];
}];
[appSelectAlert addAction:openWithJITAction];
}

UIAlertAction* showDetailsAction = [UIAlertAction actionWithTitle:@"Show Details" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
[self showDetailsPressedForRowAtIndexPath:indexPath];
Expand Down
1 change: 1 addition & 0 deletions TrollStore/TSApplicationsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (int)uninstallApp:(NSString*)appId;
- (int)uninstallAppByPath:(NSString*)path;
- (BOOL)openApplicationWithBundleID:(NSString *)appID;
- (int)enableJITForBundleID:(NSString *)appID;
- (int)changeAppRegistration:(NSString*)appPath toState:(NSString*)newState;

@end
5 changes: 5 additions & 0 deletions TrollStore/TSApplicationsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ - (BOOL)openApplicationWithBundleID:(NSString *)appId
return [[LSApplicationWorkspace defaultWorkspace] openApplicationWithBundleID:appId];
}

- (int)enableJITForBundleID:(NSString *)appId
{
return spawnRoot(rootHelperPath(), @[@"enable-jit", appId], nil, nil);
}

- (int)changeAppRegistration:(NSString*)appPath toState:(NSString*)newState
{
if(!appPath || !newState) return -200;
Expand Down

0 comments on commit 647f430

Please sign in to comment.