Ghidra is a software reverse engineering (SRE) framework
GP-322 - Updated program location action context to handle a null location
dragonmacher5ee95bc518e7
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/context/NavigatableContextAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/context/NavigatableContextAction.javaindex 101a980ae2..914130ea82 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/context/NavigatableContextAction.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/context/NavigatableContextAction.java@@ -62,16 +62,15 @@ public abstract class NavigatableContextAction extends DockingAction {}protected boolean isEnabledForContext(NavigatableActionContext context) {- return true;+ // assume that all Navigatable context actions require a valid program location+ return context.getLocation() != null;}protected boolean isAddToPopup(NavigatableActionContext context) {return isEnabledForContext(context);}- protected void actionPerformed(NavigatableActionContext context) {- // optional for subclasses- }+ protected abstract void actionPerformed(NavigatableActionContext context);@Overridepublic boolean shouldAddToWindow(boolean isMainWindow, Set<Class<?>> contextTypes) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/context/ProgramLocationContextAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/context/ProgramLocationContextAction.javaindex bc855f4833..44a1c8d09f 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/context/ProgramLocationContextAction.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/context/ProgramLocationContextAction.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -22,29 +21,29 @@ import docking.ActionContext;import docking.action.DockingAction;public abstract class ProgramLocationContextAction extends DockingAction {-+public ProgramLocationContextAction(String name, String owner) {super(name, owner);}-+@Override- public boolean isEnabledForContext(ActionContext context) {- Object contextObject = context.getContextObject();+ public boolean isEnabledForContext(ActionContext context) {+ Object contextObject = context.getContextObject();if (!(contextObject instanceof ProgramLocationActionContext)) {return false;}- return isEnabledForContext((ProgramLocationActionContext)contextObject);+ return isEnabledForContext((ProgramLocationActionContext) contextObject);}@Overridepublic void actionPerformed(ActionContext actionContext) {- actionPerformed((ProgramLocationActionContext)actionContext.getContextObject());+ actionPerformed((ProgramLocationActionContext) actionContext.getContextObject());}-+@Overridepublic boolean isValidContext(ActionContext context) {if (context instanceof ProgramLocationActionContext) {- return isValidContext((ProgramLocationActionContext)context);+ return isValidContext((ProgramLocationActionContext) context);}return false;}@@ -52,7 +51,7 @@ public abstract class ProgramLocationContextAction extends DockingAction {@Overridepublic boolean isAddToPopup(ActionContext context) {if (context instanceof ProgramLocationActionContext) {- return isAddToPopup((ProgramLocationActionContext)context);+ return isAddToPopup((ProgramLocationActionContext) context);}return false;}@@ -66,13 +65,13 @@ public abstract class ProgramLocationContextAction extends DockingAction {}protected boolean isEnabledForContext(ProgramLocationActionContext context) {- return true;+ // assume that all ProgramLocation context actions require a valid program location+ return context.getLocation() != null;}- protected void actionPerformed(ProgramLocationActionContext context) {-- }-+ // a version of actionPerformed() that takes a more specific context than our parent+ protected abstract void actionPerformed(ProgramLocationActionContext context);+@Overridepublic boolean shouldAddToWindow(boolean isMainWindow, Set<Class<?>> contextTypes) {for (Class<?> class1 : contextTypes) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.javaindex 22c1012097..f5f49f52ba 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeViewerProvider.java@@ -363,13 +363,15 @@ public class CodeViewerProvider extends NavigatableComponentProviderAdapterPoint p = e.getLocation();ProgramLocation loc = listingPanel.getProgramLocation(p);+ if (loc == null) {+ return false;+ }+CodeViewerActionContext context = new CodeViewerActionContext(this, loc);- if (loc != null) {- for (ProgramDropProvider dropProvider : dropProviders) {- if (dropProvider.isDropOk(context, e)) {- curDropProvider = dropProvider;- return true;- }+ for (ProgramDropProvider dropProvider : dropProviders) {+ if (dropProvider.isDropOk(context, e)) {+ curDropProvider = dropProvider;+ return true;}}return false;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/searchmem/mask/MnemonicSearchPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/searchmem/mask/MnemonicSearchPlugin.javaindex 0450d887ba..2ea6968f31 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/searchmem/mask/MnemonicSearchPlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/searchmem/mask/MnemonicSearchPlugin.java@@ -15,7 +15,6 @@*/package ghidra.app.plugin.core.searchmem.mask;-import docking.ActionContext;import docking.action.MenuData;import ghidra.app.CorePluginPackage;import ghidra.app.context.NavigatableActionContext;@@ -33,7 +32,7 @@ import ghidra.util.Msg;/*** Defines a set of actions that can be performed on a selection to initiate a memory search. All- * actions will ultimately open the {@link MemSearchDialog} with the search string field+ * actions will ultimately open the {@code MemSearchDialog} with the search string field* pre-populated.**/@@ -57,76 +56,67 @@ public class MnemonicSearchPlugin extends Plugin {static final String MENU_PULLRIGHT = "For Matching Instructions";static final String POPUP_MENU_GROUP = "Search";- // Actions (accessible via Tools menu).+ // Actions (accessible via Tools menu)private NavigatableContextAction setSearchMnemonicOpsNoConstAction;private NavigatableContextAction setSearchMnemonicOpsConstAction;private NavigatableContextAction setSearchMnemonicNoOpsNoConstAction;- // Final bit string used to populate the mem search dialog.+ // Final bit string used to populate the memory search dialog.public String maskedBitString;/*** Constructor.*- * @param tool+ * @param tool the tool*/public MnemonicSearchPlugin(PluginTool tool) {super(tool);createActions();}- /**+ /** Retrieves the selection region from the program, builds the search string, and pops- * up the {@link MemSearchDialog}.- *- * @param context- * @param useOps- * @param useConsts+ * up the MemSearchDialog*/- private void processAction(ActionContext context, boolean useOps, boolean useConsts) {-- // Make sure the action is of the right type. Users may create a selection from- // different windows, which will create different action context objects (ie:- // ListingActionContext, CodeViewerActionContext, etc...). Ultimately they are all- // NavigatableActionContext's, so use that.- if (context != null && context.getContextObject() instanceof NavigatableActionContext) {- NavigatableActionContext newContext =- (NavigatableActionContext) context.getContextObject();-- // Grab the program and selection from the context.- Program program = newContext.getProgram();- ProgramSelection selection = newContext.getSelection();-- // If there are multiple regions selected, let the user know via popup and- // exit. This is not allowed.- // Note: We could disable the menu items and not allow this operation to- // be initiated at all, but the decision was made to do it this way- // so it's more obvious to the user why the operation is invalid.- if (selection.getNumAddressRanges() > 1) {- Msg.showInfo(this, context.getComponentProvider().getComponent(),- "Mnemonic Search Error",- "Multiple selected regions are not allowed; please limit to one.");- return;- }+ private void processAction(NavigatableActionContext context, boolean useOps,+ boolean useConsts) {++ NavigatableActionContext newContext =+ (NavigatableActionContext) context.getContextObject();++ // Grab the program and selection from the context.+ Program program = newContext.getProgram();+ ProgramSelection selection = newContext.getSelection();++ // If there are multiple regions selected, let the user know via popup and+ // exit. This is not allowed.+ // Note: We could disable the menu items and not allow this operation to+ // be initiated at all, but the decision was made to do it this way+ // so it's more obvious to the user why the operation is invalid.+ if (selection.getNumAddressRanges() > 1) {+ Msg.showInfo(this, context.getComponentProvider().getComponent(),+ "Mnemonic Search Error",+ "Multiple selected regions are not allowed; please limit to one.");+ return;+ }- // Store the mask information (this is based solely on the menu action- // that initiated this whole operation.- SLMaskControl maskControl = new SLMaskControl(useOps, useConsts);- MaskGenerator generator = new MaskGenerator(maskControl);- MaskValue mask = generator.getMask(program, selection);-- // Now build the search string and set up the search service. This preps the mem search- // dialog with the proper search string.- if (mask != null) {- maskedBitString = createMaskedBitString(mask.getValue(), mask.getMask());- byte[] maskedBytes = maskedBitString.getBytes();-- MemorySearchService memorySearchService =- tool.getService(MemorySearchService.class);- memorySearchService.setIsMnemonic(true);- memorySearchService.search(maskedBytes, newContext);- memorySearchService.setSearchText(maskedBitString);- }+ // Store the mask information (this is based solely on the menu action+ // that initiated this whole operation.+ SLMaskControl maskControl = new SLMaskControl(useOps, useConsts);+ MaskGenerator generator = new MaskGenerator(maskControl);+ MaskValue mask = generator.getMask(program, selection);++ // Now build the search string and set up the search service. This preps the mem search+ // dialog with the proper search string.+ if (mask != null) {+ maskedBitString = createMaskedBitString(mask.getValue(), mask.getMask());+ byte[] maskedBytes = maskedBitString.getBytes();++ MemorySearchService memorySearchService =+ tool.getService(MemorySearchService.class);+ memorySearchService.setIsMnemonic(true);+ memorySearchService.search(maskedBytes, newContext);+ memorySearchService.setSearchText(maskedBitString);}}@@ -148,7 +138,7 @@ public class MnemonicSearchPlugin extends Plugin {new NavigatableContextAction("Include Operands (except constants)", getName()) {@Override- public void actionPerformed(ActionContext context) {+ public void actionPerformed(NavigatableActionContext context) {processAction(context, true, false);}@@ -167,18 +157,19 @@ public class MnemonicSearchPlugin extends Plugin {//// ACTION 2: Search for instructions, including operands.//- setSearchMnemonicOpsConstAction = new NavigatableContextAction("Include Operands", getName()) {+ setSearchMnemonicOpsConstAction =+ new NavigatableContextAction("Include Operands", getName()) {- @Override- public void actionPerformed(ActionContext context) {- processAction(context, true, true);- }+ @Override+ public void actionPerformed(NavigatableActionContext context) {+ processAction(context, true, true);+ }- @Override- protected boolean isEnabledForContext(NavigatableActionContext context) {- return context.hasSelection();- }- };+ @Override+ protected boolean isEnabledForContext(NavigatableActionContext context) {+ return context.hasSelection();+ }+ };setSearchMnemonicOpsConstAction.setMenuBarData(new MenuData(new String[] { "&Search",MENU_PULLRIGHT, "Include Operands" }, null, group, MenuData.NO_MNEMONIC, "2"));@@ -191,7 +182,7 @@ public class MnemonicSearchPlugin extends Plugin {new NavigatableContextAction("Exclude Operands", getName()) {@Override- public void actionPerformed(ActionContext context) {+ public void actionPerformed(NavigatableActionContext context) {processAction(context, false, false);}@@ -214,12 +205,8 @@ public class MnemonicSearchPlugin extends Plugin {tool.setMenuGroup(new String[] { MENU_PULLRIGHT }, POPUP_MENU_GROUP);}- /**- * Returns a single string based on the masked bits.- *- * @param values- * @param masks- * @return+ /*+ * Returns a single string based on the masked bits*/private String createMaskedBitString(byte values[], byte masks[]) {diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.javaindex ca3f4f750c..ef2b18edb8 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerController.java@@ -214,6 +214,13 @@ public class DecompilerController {decompilerMgr.decompile(program, location, null, debugFile, true);}+ public boolean hasDecompileResults() {+ if (currentDecompileData != null) {+ return currentDecompileData.hasDecompileResults();+ }+ return false;+ }+public ClangTokenGroup getCCodeModel() {return currentDecompileData.getCCodeMarkup();}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.javaindex fd2aa59117..1439aa0df9 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java@@ -224,6 +224,10 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterif (function == null) {return null;}+ if (!controller.hasDecompileResults()) {+ return null;+ }+Address entryPoint = function.getEntryPoint();boolean isDecompiling = controller.isDecompiling();return new DecompilerActionContext(this, entryPoint, isDecompiling);