Skip to content

Commit

Permalink
Merge pull request #49 from silvan-flum/refactor/rename-variables
Browse files Browse the repository at this point in the history
Renaming variables
  • Loading branch information
riesentoaster authored Jun 6, 2023
2 parents cec3899 + a654013 commit 401acd5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
61 changes: 30 additions & 31 deletions Ghidrion/src/main/java/ghidrion/GhidrionHookListingContextMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,22 @@ private ListingContextAction getAddHookAction(Mode mode, Program program) {
protected void actionPerformed(ListingActionContext context) {
Address entryAddress = context.getLocation().getAddress();
Address leaveAddress = program.getListing().getInstructionAfter(entryAddress).getAddress();
Optional<Function> f = getFunction(context, program);
Optional<Function> function = getFunctionAtSelectedLocation(context, program);
String name = function.get().getName(); // checks are done in isValidContext
String libraryName = JOptionPane.showInputDialog("Input library name", "libc");
traceFile.getHooks().add(new Hook(libraryName, f.get().getName(), entryAddress, leaveAddress, mode));
traceFile.getHooks().add(new Hook(libraryName, name, entryAddress, leaveAddress, mode));
}

@Override
protected boolean isValidContext(ListingActionContext context) {
Address a = context.getLocation().getAddress();
Optional<Function> f = getFunction(context, plugin.getCurrentProgram());
return f.isPresent()
&& f.get().isExternal()
Address address = context.getLocation().getAddress();
Optional<Function> function = getFunctionAtSelectedLocation(context, plugin.getCurrentProgram());
return function.isPresent()
&& function.get().isExternal()
&& traceFile
.getHooks()
.stream()
.filter(hook -> hook.getEntryAddress().equals(a))
.filter(hook -> hook.getEntryAddress().equals(address))
.count() == 0;
}
};
Expand All @@ -92,27 +93,28 @@ private ListingContextAction getChangeHookAction(Mode mode, Program program) {
protected void actionPerformed(ListingActionContext context) {
Address entryAddress = context.getLocation().getAddress();
Address leaveAddress = program.getListing().getInstructionAfter(entryAddress).getAddress();
Optional<Function> f = getFunction(context, program);
Optional<Function> function = getFunctionAtSelectedLocation(context, program);
String name = function.get().getName(); // checks are done in isValidContext
String libraryName = traceFile
.getHooks()
.stream()
.filter(hook -> hook.getEntryAddress().equals(entryAddress))
.findFirst()
.get()
.getLibraryName();
traceFile.getHooks().update(new Hook(libraryName, f.get().getName(), entryAddress, leaveAddress, mode));
traceFile.getHooks().update(new Hook(libraryName, name, entryAddress, leaveAddress, mode));
}

@Override
protected boolean isValidContext(ListingActionContext context) {
Address a = context.getLocation().getAddress();
Optional<Function> f = getFunction(context, plugin.getCurrentProgram());
return f.isPresent()
&& f.get().isExternal()
Address address = context.getLocation().getAddress();
Optional<Function> function = getFunctionAtSelectedLocation(context, plugin.getCurrentProgram());
return function.isPresent()
&& function.get().isExternal()
&& traceFile
.getHooks()
.stream()
.filter(hook -> hook.getEntryAddress().equals(a))
.filter(hook -> hook.getEntryAddress().equals(address))
.filter(hook -> !hook.getMode().equals(mode))
.count() > 0;
}
Expand All @@ -123,41 +125,38 @@ private ListingContextAction getDeleteHookAction(Program program) {
return new ListingContextAction(LISTENING_CONTEXT_ACTION_NAME, getName()) {
@Override
protected void actionPerformed(ListingActionContext context) {
Address a = context.getLocation().getAddress();
Optional<Hook> toDelete = traceFile
Address address = context.getLocation().getAddress();
traceFile
.getHooks()
.stream()
.filter(hook -> hook.getEntryAddress().equals(a))
.findFirst();
if (toDelete.isPresent())
traceFile.getHooks().remove(toDelete.get());
.filter(hook -> hook.getEntryAddress().equals(address))
.forEach(hook -> traceFile.getHooks().remove(hook));
}

@Override
protected boolean isValidContext(ListingActionContext context) {
Address a = context.getLocation().getAddress();
Optional<Function> f = getFunction(context, plugin.getCurrentProgram());
return f.isPresent()
&& f.get().isExternal()
Address address = context.getLocation().getAddress();
Optional<Function> function = getFunctionAtSelectedLocation(context, plugin.getCurrentProgram());
return function.isPresent()
&& function.get().isExternal()
&& traceFile
.getHooks()
.stream()
.filter(hook -> hook.getEntryAddress().equals(a))
.filter(hook -> hook.getEntryAddress().equals(address))
.count() > 0;
}
};
}

private static Optional<Function> getFunction(ListingActionContext context, Program p) {
private static Optional<Function> getFunctionAtSelectedLocation(ListingActionContext context, Program p) {
Reference[] references = p.getReferenceManager()
.getReferencesFrom(context.getLocation().getAddress());
if (references.length != 1)
return Optional.empty();
Function f = p.getFunctionManager().getFunctionAt(references[0].getToAddress());
if (f == null)
Function function = p.getFunctionManager().getFunctionAt(references[0].getToAddress());
if (function == null)
return Optional.empty();
f = f.isThunk() ? f.getThunkedFunction(true) : f;
return Optional.of(f);

function = function.isThunk() ? function.getThunkedFunction(true) : function;
return Optional.of(function);
}
}
4 changes: 2 additions & 2 deletions Ghidrion/src/main/java/model/HookableFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public int compareTo(HookableFunction o) {
}

/**
* @param program program to consider
* @param program to gather functions from
* @return all hookable functions in the provided program that are linked to an
* external function.
*/
public static Set<HookableFunction> getFunctions(Program program) {
public static Set<HookableFunction> getHookableFunctions(Program program) {
FunctionManager functionManager = program.getFunctionManager();
ReferenceManager referenceManager = program.getReferenceManager();
Memory memory = program.getMemory();
Expand Down
2 changes: 1 addition & 1 deletion Ghidrion/src/main/java/ui/ctrl/CreateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public CreateController(GhidrionPlugin plugin, MorionInitTraceFile traceFile) {

plugin.addProgramOpenendListener(p -> {
allHookableFunctions.clear();
allHookableFunctions.addAll(HookableFunction.getFunctions(p));
allHookableFunctions.addAll(HookableFunction.getHookableFunctions(p));
traceFile.getHooks().clear(); // trigger update of lists
});
traceFile.getHooks().addObserver(alreadyHooked -> {
Expand Down

0 comments on commit 401acd5

Please sign in to comment.