From a654013e3153c7b8ea6d59044c0a40d4f7d5fce6 Mon Sep 17 00:00:00 2001 From: Valentin Huber Date: Tue, 6 Jun 2023 14:07:02 +0200 Subject: [PATCH] renaming variables --- .../GhidrionHookListingContextMenu.java | 61 +++++++++---------- .../src/main/java/model/HookableFunction.java | 4 +- .../main/java/ui/ctrl/CreateController.java | 2 +- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Ghidrion/src/main/java/ghidrion/GhidrionHookListingContextMenu.java b/Ghidrion/src/main/java/ghidrion/GhidrionHookListingContextMenu.java index b23c020..0ab9c5a 100644 --- a/Ghidrion/src/main/java/ghidrion/GhidrionHookListingContextMenu.java +++ b/Ghidrion/src/main/java/ghidrion/GhidrionHookListingContextMenu.java @@ -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 f = getFunction(context, program); + Optional 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 f = getFunction(context, plugin.getCurrentProgram()); - return f.isPresent() - && f.get().isExternal() + Address address = context.getLocation().getAddress(); + Optional 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; } }; @@ -92,7 +93,8 @@ 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 f = getFunction(context, program); + Optional function = getFunctionAtSelectedLocation(context, program); + String name = function.get().getName(); // checks are done in isValidContext String libraryName = traceFile .getHooks() .stream() @@ -100,19 +102,19 @@ protected void actionPerformed(ListingActionContext context) { .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 f = getFunction(context, plugin.getCurrentProgram()); - return f.isPresent() - && f.get().isExternal() + Address address = context.getLocation().getAddress(); + Optional 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; } @@ -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 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 f = getFunction(context, plugin.getCurrentProgram()); - return f.isPresent() - && f.get().isExternal() + Address address = context.getLocation().getAddress(); + Optional 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 getFunction(ListingActionContext context, Program p) { + private static Optional 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); } } diff --git a/Ghidrion/src/main/java/model/HookableFunction.java b/Ghidrion/src/main/java/model/HookableFunction.java index 483e0be..3d8071d 100644 --- a/Ghidrion/src/main/java/model/HookableFunction.java +++ b/Ghidrion/src/main/java/model/HookableFunction.java @@ -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 getFunctions(Program program) { + public static Set getHookableFunctions(Program program) { FunctionManager functionManager = program.getFunctionManager(); ReferenceManager referenceManager = program.getReferenceManager(); Memory memory = program.getMemory(); diff --git a/Ghidrion/src/main/java/ui/ctrl/CreateController.java b/Ghidrion/src/main/java/ui/ctrl/CreateController.java index e65144f..97c879a 100644 --- a/Ghidrion/src/main/java/ui/ctrl/CreateController.java +++ b/Ghidrion/src/main/java/ui/ctrl/CreateController.java @@ -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 -> {