Skip to content

Commit

Permalink
Use BOOL return for code injection handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zorgiepoo committed Oct 19, 2022
1 parent 849ac44 commit ff16c42
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
3 changes: 1 addition & 2 deletions Bit Slicer/ZGCodeInjectionWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ - (IBAction)injectCode:(id)__unused sender
NSError *error = nil;
NSData *injectedCode = [ZGDebuggerUtilities assembleInstructionText:ZGUnwrapNullableObject(_suggestedCode) atInstructionPointer:_allocatedAddress processType:_processType error:&error];

ZGCodeInjectionHandler *injectionHandler = nil;
if (injectedCode.length == 0 || error != nil || (injectionHandler = [ZGDebuggerUtilities injectCode:injectedCode intoAddress:_allocatedAddress hookingIntoOriginalInstructions:_instructions process:ZGUnwrapNullableObject(_process) processType:_processType breakPointController:_breakPointController owner:_owner undoManager:ZGUnwrapNullableObject(_undoManager) error:&error]) == nil)
if (injectedCode.length == 0 || error != nil || ![ZGDebuggerUtilities injectCode:injectedCode intoAddress:_allocatedAddress hookingIntoOriginalInstructions:_instructions process:ZGUnwrapNullableObject(_process) processType:_processType breakPointController:_breakPointController owner:_owner undoManager:ZGUnwrapNullableObject(_undoManager) error:&error])
{
NSLog(@"Error while injecting code");
NSLog(@"%@", error);
Expand Down
2 changes: 1 addition & 1 deletion Bit Slicer/ZGDebuggerUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ actionName:(nullable NSString *)actionName;

+ (NSArray<ZGInstruction *> * _Nullable)instructionsBeforeHookingIntoAddress:(ZGMemoryAddress)address injectingIntoDestination:(ZGMemoryAddress)destinationAddress inProcess:(ZGProcess *)process breakPointController:(ZGBreakPointController *)breakPointController processType:(ZGProcessType)processType;

+ (ZGCodeInjectionHandler * _Nullable)
+ (BOOL)
injectCode:(NSData *)codeData
intoAddress:(ZGMemoryAddress)allocatedAddress
hookingIntoOriginalInstructions:(NSArray<ZGInstruction *> *)hookedInstructions
Expand Down
16 changes: 8 additions & 8 deletions Bit Slicer/ZGDebuggerUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr
}

#define INJECT_ERROR_DOMAIN @"INJECT_CODE_FAILED"
+ (ZGCodeInjectionHandler * _Nullable)
+ (BOOL)
injectCode:(NSData *)codeData
intoAddress:(ZGMemoryAddress)allocatedAddress
hookingIntoOriginalInstructions:(NSArray<ZGInstruction *> *)hookedInstructions
Expand All @@ -502,7 +502,7 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr

if (hookedInstructions == nil)
{
return nil;
return NO;
}

ZGSuspendTask(process.processTask);
Expand Down Expand Up @@ -531,7 +531,7 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr

free(nopBuffer);
ZGResumeTask(process.processTask);
return nil;
return NO;
}

free(nopBuffer);
Expand All @@ -545,7 +545,7 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr
}

ZGResumeTask(process.processTask);
return nil;
return NO;
}

ZGMemorySize hookedInstructionsLength = 0;
Expand Down Expand Up @@ -606,7 +606,7 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr
{
ZG_LOG(@"Error generating nop data for jumpFromIslandData");
ZGResumeTask(process.processTask);
return nil;
return NO;
}

[newInstructionsData appendData:jumpFromIslandData];
Expand Down Expand Up @@ -682,7 +682,7 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr
{
ZG_LOG(@"Error generating jumpFromIslandData");
ZGResumeTask(process.processTask);
return nil;
return NO;
}

[newInstructionsData appendData:jumpFromIslandData];
Expand All @@ -704,13 +704,13 @@ + (BOOL)shouldInjectCodeWithRelativeBranchingWithProcess:(ZGProcess *)process pr
if (![codeInjectionHandler addBreakPointWithToIslandInstruction:firstInstruction fromIslandInstruction:instructionBack islandAddress:allocatedAddress process:process processType:processType breakPointController:breakPointController owner:owner undoManager:undoManager])
{
ZG_LOG(@"Error: Failed to add breakpoints for code injection..");
return nil;
return NO;
}
}

ZGResumeTask(process.processTask);

return codeInjectionHandler;
return YES;
}

+ (NSArray<ZGInstruction *> *)instructionsBeforeHookingIntoAddress:(ZGMemoryAddress)address injectingIntoDestination:(ZGMemoryAddress)destinationAddress inProcess:(ZGProcess *)process breakPointController:(ZGBreakPointController *)breakPointController processType:(ZGProcessType)processType
Expand Down
4 changes: 2 additions & 2 deletions Bit Slicer/ZGPyDebugger.m
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ static ZGProcessType _processTypeFromDisassemblerMode(DebuggerClass *self, char

NSError *error = nil;
BOOL injectedCode =
([ZGDebuggerUtilities
[ZGDebuggerUtilities
injectCode:codeData
intoAddress:destinationAddress
hookingIntoOriginalInstructions:originalInstructions
Expand All @@ -835,7 +835,7 @@ static ZGProcessType _processTypeFromDisassemblerMode(DebuggerClass *self, char
breakPointController:self->objcSelf->_breakPointController
owner:self->breakPointDelegate
undoManager:nil
error:&error] != nil);
error:&error];

if (!injectedCode)
{
Expand Down

0 comments on commit ff16c42

Please sign in to comment.