Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5477 - Decompiler - Fixed performance when using many global highlighters; updated the highlight service to allow for function-specific highlighting

dragonmacher6fa543c2e269
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompilerHighlightService.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompilerHighlightService.java
index e4d6bb1d0a..015360d6e4 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompilerHighlightService.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompilerHighlightService.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,6 +15,8 @@
*/
package ghidra.app.decompiler;
+import ghidra.program.model.listing.Function;
+
/**
* A service that allows clients to create highlights in the form of background colors for
* {@link ClangToken}s in the Decompiler UI.
@@ -24,22 +26,38 @@ package ghidra.app.decompiler;
* {@link #createHighlighter(String, CTokenHighlightMatcher)} methods of this interface.
*
* <p>There is no limit to the number of highlighters that may be installed. If multiple
- * highlights overlap, then their colors will be blended.
+ * highlights overlap, then their colors will be blended. The number of color blends may be limited
+ * for performance reasons.
*/
public interface DecompilerHighlightService {
/**
* Creates a highlighter that will use the given matcher to create highlights as functions
- * get decompiled.
+ * get decompiled. The highlighter created will be applied to every decompiled function.
+ *
+ * @param tm the matcher
+ * @return the new highlighter
+ */
+ public default DecompilerHighlighter createHighlighter(CTokenHighlightMatcher tm) {
+ return createHighlighter((Function) null, tm);
+ }
+
+ /**
+ * Creates a highlighter that will use the given matcher to create highlights as functions
+ * get decompiled. The highlighter created will only be applied to the given function.
+ *
+ * @param function the function to which the highlighter will be applied
* @param tm the matcher
* @return the new highlighter
+ * @see #createHighlighter(CTokenHighlightMatcher) for global highlights
*/
- public DecompilerHighlighter createHighlighter(CTokenHighlightMatcher tm);
+ public DecompilerHighlighter createHighlighter(Function function, CTokenHighlightMatcher tm);
/**
* A version of {@link #createHighlighter(String, CTokenHighlightMatcher)} that allows clients
* to specify an ID. This ID will be used to ensure that any existing highlighters with that
- * ID will be removed before creating a new highlighter.
+ * ID will be removed before creating a new highlighter. The highlighter created will be
+ * applied to every decompiled function.
*
* <p>This method is convenient for scripts, since a script cannot hold on to any created
* highlighters between repeated script executions. A good value for script writers to use
@@ -49,6 +67,25 @@ public interface DecompilerHighlightService {
* @param tm the matcher
* @return the new highlighter
*/
- public DecompilerHighlighter createHighlighter(String id, CTokenHighlightMatcher tm);
+ public default DecompilerHighlighter createHighlighter(String id, CTokenHighlightMatcher tm) {
+ return createHighlighter(id, null, tm);
+ }
+ /**
+ * A version of {@link #createHighlighter(String, CTokenHighlightMatcher)} that allows clients
+ * to specify an ID. This ID will be used to ensure that any existing highlighters with that
+ * ID will be removed before creating a new highlighter. The highlighter created will only be
+ * applied to the given function.
+ *
+ * <p>This method is convenient for scripts, since a script cannot hold on to any created
+ * highlighters between repeated script executions. A good value for script writers to use
+ * is the name of their script class.
+ *
+ * @param id the ID
+ * @param function the function to which the highlighter will be applied
+ * @param tm the matcher
+ * @return the new highlighter
+ */
+ public DecompilerHighlighter createHighlighter(String id, Function function,
+ CTokenHighlightMatcher tm);
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangDecompilerHighlighter.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangDecompilerHighlighter.java
index dae84a708c..3e0ef49bc8 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangDecompilerHighlighter.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangDecompilerHighlighter.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,6 +21,7 @@ import java.util.function.Supplier;
import generic.json.Json;
import ghidra.app.decompiler.*;
+import ghidra.program.model.listing.Function;
/**
* The implementation of {@link DecompilerHighlighter}. This will get created by the
@@ -38,17 +39,20 @@ class ClangDecompilerHighlighter implements DecompilerHighlighter {
protected String id;
private DecompilerPanel decompilerPanel;
private CTokenHighlightMatcher matcher;
+ private Function function; // will be null for global highlights
private Set<ClangDecompilerHighlighter> clones = new HashSet<>();
- ClangDecompilerHighlighter(DecompilerPanel panel, CTokenHighlightMatcher matcher) {
- UUID uuId = UUID.randomUUID();
- this.id = uuId.toString();
+ ClangDecompilerHighlighter(String id, DecompilerPanel panel, Function function,
+ CTokenHighlightMatcher matcher) {
+ this.id = id;
this.decompilerPanel = panel;
+ this.function = function;
this.matcher = matcher;
}
- ClangDecompilerHighlighter(String id, DecompilerPanel panel, CTokenHighlightMatcher matcher) {
- this.id = id;
+ private ClangDecompilerHighlighter(DecompilerPanel panel, CTokenHighlightMatcher matcher) {
+ UUID uuId = UUID.randomUUID();
+ this.id = uuId.toString();
this.decompilerPanel = panel;
this.matcher = matcher;
}
@@ -60,7 +64,8 @@ class ClangDecompilerHighlighter implements DecompilerHighlighter {
*/
ClangDecompilerHighlighter clone(DecompilerPanel panel) {
// note: we re-use the ID to make tracking easier
- ClangDecompilerHighlighter clone = new ClangDecompilerHighlighter(id, panel, matcher);
+ ClangDecompilerHighlighter clone =
+ new ClangDecompilerHighlighter(id, panel, function, matcher);
clones.add(clone);
return clone;
}
@@ -81,7 +86,14 @@ class ClangDecompilerHighlighter implements DecompilerHighlighter {
return; // disposed
}
- clearHighlights();
+ DecompilerController controller = decompilerPanel.getController();
+ Function decompiledFunction = controller.getFunction();
+ if (function != null && !function.equals(decompiledFunction)) {
+ return; // this is a function-specific highlighter and this is not the desired function
+ }
+
+ // This is done by the caller of this method
+ // clearHighlights();
ClangLayoutController layoutModel = decompilerPanel.getLayoutController();
ClangTokenGroup root = layoutModel.getRoot();
@@ -160,18 +172,18 @@ class ClangDecompilerHighlighter implements DecompilerHighlighter {
}
private class MappedTokenColorProvider implements ColorProvider {
-
+
private Map<ClangToken, Color> highlights;
-
+
MappedTokenColorProvider(Map<ClangToken, Color> highlights) {
this.highlights = highlights;
}
-
+
@Override
public Color getColor(ClangToken token) {
return highlights.get(token);
}
-
+
@Override
public String toString() {
return "Token Matcher Color " + matcher.toString();
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java
index 61d930e300..a1ae82005f 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/ClangHighlightController.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -40,7 +40,7 @@ import util.CollectionUtils;
*
* <p>This class maintains the following types of highlights:
* <UL>
- * <LI> Context Highlights - triggered by user clicking and some user actions; considered transient
+ * <LI>Context Highlights - triggered by user clicking and some user actions; considered transient
* and get cleared whenever the location changes. These highlights show state such as the
* current field, impact of a variable (via a slicing action), or related syntax (such as
* matching braces)
@@ -52,9 +52,10 @@ import util.CollectionUtils;
* <B>These highlights apply to the function in use when the highlight is created. Thus,
* each function has a unique set of highlights that is maintained between decompilation.</B>
* </LI>
- * <LI>Global Highlights - triggered by clients of the {@link DecompilerHighlightService}; they
- * will stay until the client of the service clears the highlight.
- * <B>These highlights apply to every function that is decompiler.</B>
+ * <LI>Service Highlights - triggered by clients of the {@link DecompilerHighlightService}; they
+ * will be stored in this class until the client of the service clears the highlight. These
+ * can be global (applied to all functions) or specific to a given function. Each user
+ * highlight will be called to generate highlights when a function is first decompiled.
* </LI>
* </UL>
*
@@ -83,6 +84,10 @@ public abstract class ClangHighlightController {
*/
private long updateId;
+ // arbitrary value chosen by guessing; this can be changed if needed
+ private int maxColorBlendSize = 5;
+ private boolean isRebuilding;
+
private List<ClangHighlightListener> listeners = new ArrayList<>();
public abstract void fieldLocationChanged(FieldLocation location, Field field,
@@ -147,13 +152,51 @@ public abstract class ClangHighlightController {
}
/**
- * Returns all global highlighters installed in this controller. The global highlighters apply
- * to all functions. This is in contrast to secondary highlighters, which are
- * function-specific.
+ * Returns all highlight service highlighters installed in this controller. The global
+ * highlighters apply to all functions. This is in contrast to secondary highlighters, which
+ * are function-specific.
* @return the highlighters
*/
- public Set<DecompilerHighlighter> getGlobalHighlighters() {
- return userHighlights.getGlobalHighlighters();
+ public Set<DecompilerHighlighter> getServiceHighlighters() {
+ return userHighlights.getServiceHighlighters();
+ }
+
+ public void reapplyAllHighlights(Function function) {
+ //
+ // Under normal operation, we rebuild colors as highlighters are added and removed. Doing
+ // this for one highlighter is fast. Doing it for a large number of highlighters can be
+ // slow. When rebuilding all highlights, disable color calculation until the rebuild is
+ // finished. This allows all highlights to calculate their matches without the color
+ // blending affecting performance.
+ //
+ isRebuilding = true;
+ Set<DecompilerHighlighter> service = getServiceHighlighters();
+ Set<DecompilerHighlighter> secondary = getSecondaryHighlighters(function);
+ Iterable<DecompilerHighlighter> it = CollectionUtils.asIterable(service, secondary);
+
+ try {
+ for (DecompilerHighlighter highlighter : it) {
+ highlighter.clearHighlights();
+ highlighter.applyHighlights();
+ }
+ }
+ finally {
+ isRebuilding = false;
+ }
+
+ // gather all highlighted tokens and then update their color
+ Set<ClangToken> allTokens = new HashSet<>();
+ it = CollectionUtils.asIterable(service, secondary);
+ for (DecompilerHighlighter highlighter : it) {
+ TokenHighlights hlTokens = userHighlights.add(highlighter);
+ for (HighlightToken hlToken : hlTokens) {
+ allTokens.add(hlToken.getToken());
+ }
+ }
+
+ for (ClangToken token : allTokens) {
+ updateHighlightColor(token);
+ }
}
/**
@@ -378,18 +421,23 @@ public abstract class ClangHighlightController {
}
private void updateHighlightColor(ClangToken t) {
+
+ if (isRebuilding) {
+ return;
+ }
+
// set the color to the current combined value of all highlight types
Color combinedColor = getCombinedColor(t);
t.setHighlight(combinedColor);
}
- private void add(List<Color> colors, HighlightToken hlToken) {
+ private void add(Set<Color> colors, HighlightToken hlToken) {
if (hlToken != null) {
colors.add(hlToken.getColor());
}
}
- private void add(List<Color> colors, Color c) {
+ private void add(Set<Color> colors, Color c) {
if (c != null) {
colors.add(c);
}
@@ -409,30 +457,25 @@ public abstract class ClangHighlightController {
HighlightToken primaryHl = contextHighlightTokens.get(t);
Color blendedHlColor = blendHighlighterColors(t);
- List<Color> allColors = new ArrayList<>();
+ Set<Color> allColors = new HashSet<>();
add(allColors, primaryHl);
add(allColors, blendedHlColor);
- Color blended = blend(allColors);
- return blended;
+ return blend(allColors);
}
- public Color blend(List<Color> colors) {
+ public Color blend(Set<Color> colors) {
if (colors.isEmpty()) {
return null;
}
- if (colors.size() == 1) {
- return CollectionUtils.any(colors);
- }
-
- Color lastColor = colors.get(0);
- for (int i = 1; i < colors.size(); i++) {
- Color nextColor = colors.get(i);
+ Iterator<Color> it = colors.iterator();
+ Color lastColor = it.next();
+ while (it.hasNext()) {
+ Color nextColor = it.next();
lastColor = ColorUtils.blend(lastColor, nextColor, .8f);
}
-
return lastColor;
}
@@ -443,10 +486,10 @@ public abstract class ClangHighlightController {
return null; // not sure if this can happen
}
- Set<DecompilerHighlighter> global = getGlobalHighlighters();
+ Set<DecompilerHighlighter> service = getServiceHighlighters();
Set<DecompilerHighlighter> secondary = getSecondaryHighlighters(function);
- Iterable<DecompilerHighlighter> it = CollectionUtils.asIterable(global, secondary);
- Color lastColor = null;
+ Iterable<DecompilerHighlighter> it = CollectionUtils.asIterable(service, secondary);
+ Set<Color> colors = new HashSet<>();
for (DecompilerHighlighter highlighter : it) {
TokenHighlights highlights = userHighlights.get(highlighter);
HighlightToken hlToken = highlights.get(token);
@@ -455,15 +498,13 @@ public abstract class ClangHighlightController {
}
Color nextColor = hlToken.getColor();
- if (lastColor != null) {
- lastColor = ColorUtils.blend(lastColor, nextColor, .8f);
- }
- else {
- lastColor = nextColor;
+ colors.add(nextColor);
+ if (colors.size() == maxColorBlendSize) {
+ break;
}
}
- return lastColor;
+ return blend(colors);
}
private Function getFunction(ClangToken t) {
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java
index 5f24bc30dc..8ff7a62df2 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerPanel.java
@@ -281,19 +281,25 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
highlightController.removeHighlighterHighlights(highlighter);
}
- public DecompilerHighlighter createHighlighter(CTokenHighlightMatcher tm) {
+ private DecompilerHighlighter createHighlighter(CTokenHighlightMatcher tm) {
+ Function function = decompileData.getFunction();
+ return createHighlighter(function, tm);
+ }
+
+ public DecompilerHighlighter createHighlighter(Function f, CTokenHighlightMatcher tm) {
UUID uuId = UUID.randomUUID();
String id = uuId.toString();
- return createHighlighter(id, tm);
+ return createHighlighter(id, f, tm);
}
- public DecompilerHighlighter createHighlighter(String id, CTokenHighlightMatcher tm) {
+ public DecompilerHighlighter createHighlighter(String id, Function f,
+ CTokenHighlightMatcher tm) {
DecompilerHighlighter currentHighlighter = highlightersById.get(id);
if (currentHighlighter != null) {
currentHighlighter.dispose();
}
- ClangDecompilerHighlighter newHighlighter = new ClangDecompilerHighlighter(id, this, tm);
+ ClangDecompilerHighlighter newHighlighter = new ClangDecompilerHighlighter(id, this, f, tm);
highlightersById.put(id, newHighlighter);
highlightController.addHighlighter(newHighlighter);
return newHighlighter;
@@ -388,11 +394,12 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
});
}
- private void cloneGlobalHighlighters(DecompilerPanel sourcePanel) {
+ private void cloneServiceHiglighters(DecompilerPanel sourcePanel) {
- Set<DecompilerHighlighter> globalHighlighters =
- sourcePanel.highlightController.getGlobalHighlighters();
- for (DecompilerHighlighter otherHighlighter : globalHighlighters) {
+ Set<DecompilerHighlighter> serviceHighlighters =
+ sourcePanel.highlightController.getServiceHighlighters();
+
+ for (DecompilerHighlighter otherHighlighter : serviceHighlighters) {
if (!(otherHighlighter instanceof ClangDecompilerHighlighter clangHighlighter)) {
continue;
@@ -422,13 +429,13 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
*/
public void cloneHighlights(DecompilerPanel sourcePanel) {
- cloneGlobalHighlighters(sourcePanel);
+ Function function = decompileData.getFunction();
+ cloneServiceHiglighters(sourcePanel);
//
// Keep only those secondary highlighters for the current function. This ensures that the
// clone will match the cloned decompiler.
//
- Function function = decompileData.getFunction();
Set<DecompilerHighlighter> secondaryHighlighters =
sourcePanel.getSecondaryHighlihgtersByFunction(function);
@@ -505,36 +512,8 @@ public class DecompilerPanel extends JPanel implements FieldMouseListener, Field
// don't highlight search results across functions
currentSearchLocation = null;
- reapplySecondaryHighlights();
- reapplyGlobalHighlights();
- }
-
- private void reapplyGlobalHighlights() {
-
- Function function = decompileData.getFunction();
- if (function == null) {
- return;
- }
-
- Set<DecompilerHighlighter> globalHighlighters = highlightController.getGlobalHighlighters();
- for (DecompilerHighlighter highlighter : globalHighlighters) {
- highlighter.clearHighlights();
- highlighter.applyHighlights();
- }
- }
-
- private void reapplySecondaryHighlights() {
-
- Function function = decompileData.getFunction();
- if (function == null) {
- return;
- }
-
- Set<DecompilerHighlighter> secondaryHighlighters =
- getSecondaryHighlihgtersByFunction(function);
- for (DecompilerHighlighter highlighter : secondaryHighlighters) {
- highlighter.clearHighlights();
- highlighter.applyHighlights();
+ if (function != null) {
+ highlightController.reapplyAllHighlights(function);
}
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenKey.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenKey.java
index ee57b2bf6e..2aec82be44 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenKey.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/TokenKey.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,20 +22,50 @@ import ghidra.app.decompiler.ClangToken;
// a key that allows us to equate tokens that are not the same instance
class TokenKey {
+
private ClangToken token;
+ private int hash;
+ private int lineNumber = -1;
+ private int indexInParent = Integer.MAX_VALUE;
TokenKey(ClangToken token) {
this.token = Objects.requireNonNull(token);
+
+ ClangLine lineParent = token.getLineParent();
+ if (lineParent != null) {
+ lineNumber = lineParent.getLineNumber();
+ }
+
+ // have the hash be more than just the token text, otherwise, the number of hash collisions
+ // can become quite large if the user is matching on the same token text for multiple tokens
+ hash = Objects.hash(token.getText());
+ hash += lineNumber;
}
public TokenKey(HighlightToken t) {
this(t.getToken());
}
+ private int getIndexInParent() {
+ if (indexInParent == Integer.MAX_VALUE) {
+ ClangLine lineParent = token.getLineParent();
+ if (lineParent != null) {
+ indexInParent = lineParent.indexOfToken(token);
+ }
+ else {
+ indexInParent = -1;
+ }
+ }
+ return indexInParent;
+ }
+
+ public ClangToken getToken() {
+ return token;
+ }
+
@Override
public int hashCode() {
- String text = token.getText();
- return text == null ? 0 : text.hashCode();
+ return hash;
}
@Override
@@ -48,7 +78,8 @@ class TokenKey {
return false;
}
- ClangToken otherToken = ((TokenKey) obj).token;
+ TokenKey otherKey = (TokenKey) obj;
+ ClangToken otherToken = otherKey.token;
if (token.getClass() != otherToken.getClass()) {
return false;
}
@@ -57,33 +88,11 @@ class TokenKey {
return false;
}
- ClangLine lineParent = token.getLineParent();
- ClangLine otherLineParent = otherToken.getLineParent();
- if (!sameLines(lineParent, otherLineParent)) {
- return false;
- }
- if (lineParent == null) {
- return false;
- }
-
- int positionInLine = lineParent.indexOfToken(token);
- int otherPositionInLine = otherLineParent.indexOfToken(otherToken);
- return positionInLine == otherPositionInLine;
- }
-
- private boolean sameLines(ClangLine l1, ClangLine l2) {
-
- if (l1 == null) {
- if (l2 != null) {
- return false;
- }
- return true;
- }
- else if (l2 == null) {
+ if (lineNumber != otherKey.lineNumber) {
return false;
}
- return l1.getLineNumber() == l2.getLineNumber();
+ return getIndexInParent() == otherKey.getIndexInParent();
}
@Override
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/UserHighlights.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/UserHighlights.java
index d40c0db699..8ada9fdc26 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/UserHighlights.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/UserHighlights.java
@@ -20,14 +20,14 @@ import java.util.*;
import org.apache.commons.collections4.map.LazyMap;
-import ghidra.app.decompiler.ClangToken;
-import ghidra.app.decompiler.DecompilerHighlighter;
+import ghidra.app.decompiler.*;
import ghidra.program.model.listing.Function;
/**
* A class to manage and track Decompiler highlights created by the user via the UI or from a
- * script. This class manages secondary and global highlights. For a description of these terms,
- * see {@link ClangHighlightController}.
+ * script. This class manages secondary highlights and highlights created from the
+ * {@link DecompilerHighlightService}, which has both global and per-function highlights. For a
+ * description of these terms, see {@link ClangHighlightController}.
* <p>
* These highlights will remain until cleared explicitly by the user or a client API call.
* Contrastingly, context highlights are cleared as the user moves the cursor around the Decompiler
@@ -42,7 +42,8 @@ public class UserHighlights {
// between secondary highlights and highlight service highlights
private Set<DecompilerHighlighter> secondaryHighlighters = new HashSet<>();
- // all highlighters, including secondary and global highlight service highlighters
+ // all highlighters, including secondary and global highlight service highlighters and per
+ // function highlight service highlighters
private Map<DecompilerHighlighter, TokenHighlights> allHighlighterHighlights = new HashMap<>();
// color supplier for secondary highlights
@@ -81,7 +82,7 @@ public class UserHighlights {
return new HashSet<>(secondaryHighlightersByFunction.get(function));
}
- Set<DecompilerHighlighter> getGlobalHighlighters() {
+ Set<DecompilerHighlighter> getServiceHighlighters() {
Set<DecompilerHighlighter> allHighlighters = allHighlighterHighlights.keySet();
Set<DecompilerHighlighter> results = new HashSet<>(allHighlighters);
results.removeAll(secondaryHighlighters);
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.java
index 5b812ea39f..a65db5a018 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
@@ -294,13 +294,14 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
//==================================================================================================
@Override
- public DecompilerHighlighter createHighlighter(CTokenHighlightMatcher tm) {
- return getDecompilerPanel().createHighlighter(tm);
+ public DecompilerHighlighter createHighlighter(Function f, CTokenHighlightMatcher tm) {
+ return getDecompilerPanel().createHighlighter(f, tm);
}
@Override
- public DecompilerHighlighter createHighlighter(String id, CTokenHighlightMatcher tm) {
- return getDecompilerPanel().createHighlighter(id, tm);
+ public DecompilerHighlighter createHighlighter(String id, Function f,
+ CTokenHighlightMatcher tm) {
+ return getDecompilerPanel().createHighlighter(id, f, tm);
}
//==================================================================================================
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/decompiler/component/DecompilerClangTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/decompiler/component/DecompilerClangTest.java
index c5e56164d2..42978491b4 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/decompiler/component/DecompilerClangTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/decompiler/component/DecompilerClangTest.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -45,8 +45,8 @@ import ghidra.app.plugin.core.decompile.DecompilerProvider;
import ghidra.app.plugin.core.decompile.actions.*;
import ghidra.app.util.AddEditDialog;
import ghidra.framework.options.ToolOptions;
-import ghidra.program.model.listing.CodeUnit;
-import ghidra.util.Msg;
+import ghidra.program.model.listing.CommentType;
+import ghidra.program.model.listing.Function;
public class DecompilerClangTest extends AbstractDecompilerTest {
@@ -191,7 +191,7 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
String linkDisplayText = "function _call_structure_A()";
String linkAddress = "100000e10";
String annotation = "{@addr " + linkAddress + " \"" + linkDisplayText + "\"}";
- setComment(commentAddress, CodeUnit.PRE_COMMENT, "This is calling " + annotation + ".");
+ setComment(commentAddress, CommentType.PRE, "This is calling " + annotation + ".");
decompile("100000d60"); // _call_structure_A()
@@ -404,8 +404,6 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
charPosition = 2;
setDecompilerLocation(line, charPosition);
- Msg.debug(this, "test - remove");
-
removeSecondaryHighlight(); // remove "printf" highlight
assertNoFieldsSecondaryHighlighted(printfText);
@@ -1869,6 +1867,141 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
assertNoFieldsSecondaryHighlighted(hlText);
}
+ @Test
+ public void testHighlightService_FunctionSpecificHighlights() {
+
+ /*
+
+ Decomp of '_call_structure_A':
+
+ 1|
+ 2| void _call_structure_A(A *a)
+ 3|
+ 4| {
+ 5| _printf("call_structure_A: %s\n",a->name);
+ 6| _printf("call_structure_A: %s\n",(a->b).name);
+ 7| _printf("call_structure_A: %s\n",(a->b).c.name);
+ 8| _printf("call_structure_A: %s\n",(a->b).c.d.name);
+ 9| _printf("call_structure_A: %s\n",(a->b).c.d.e.name);
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+
+ */
+
+ decompile("100000d60"); // '_call_structure_A'
+
+ String hlText = "_printf";
+ Color hlColor = Palette.PINK;
+ CTokenHighlightMatcher hlMatcher = token -> {
+ if (token.getText().contains(hlText)) {
+ return hlColor;
+ }
+ return null;
+ };
+ SpyCTokenHighlightMatcher spyMatcher = new SpyCTokenHighlightMatcher(hlMatcher);
+ DecompilerHighlightService hlService = getHighlightService();
+ Function function = getCurrentFunction();
+ DecompilerHighlighter highlighter = hlService.createHighlighter(function, spyMatcher);
+ highlighter.applyHighlights();
+
+ assertAllHighlighterFieldsHighlighted(spyMatcher, hlText, hlColor);
+
+ spyMatcher.clear();
+
+ // this function also has calls to '_printf'
+ decompile("100000e10"); // '_call_structure_B'
+
+ // this function should NOT show any highlights, since is not the same function that was
+ // used to create the highlights
+ assertTrue(spyMatcher.getMatchingTokens().isEmpty());
+
+ // go back to the original function and verify the highlights are there
+ spyMatcher.clear();
+ decompile("100000d60");
+ assertAllHighlighterFieldsHighlighted(spyMatcher, hlText, hlColor);
+
+ highlighter.dispose();
+ assertNoFieldsSecondaryHighlighted(hlText);
+ }
+
+ @Test
+ public void testHighlightService_FunctionSpecificHighlights_WithGlobalHighlights() {
+
+ /*
+
+ Decomp of '_call_structure_A':
+
+ 1|
+ 2| void _call_structure_A(A *a)
+ 3|
+ 4| {
+ 5| _printf("call_structure_A: %s\n",a->name);
+ 6| _printf("call_structure_A: %s\n",(a->b).name);
+ 7| _printf("call_structure_A: %s\n",(a->b).c.name);
+ 8| _printf("call_structure_A: %s\n",(a->b).c.d.name);
+ 9| _printf("call_structure_A: %s\n",(a->b).c.d.e.name);
+ 10| _call_structure_B(&a->b);
+ 11| return;
+ 12| }
+
+ */
+
+ decompile("100000d60"); // '_call_structure_A'
+
+ DecompilerHighlightService hlService = getHighlightService();
+ String globalHlText = "structure";
+ Color globalHlColor = Palette.ORANGE;
+ CTokenHighlightMatcher globalHlMatcher = token -> {
+ if (token.getText().contains(globalHlText)) {
+ return globalHlColor;
+ }
+ return null;
+ };
+ SpyCTokenHighlightMatcher globalSpyMatcher = new SpyCTokenHighlightMatcher(globalHlMatcher);
+ DecompilerHighlighter globalHighlighter = hlService.createHighlighter(globalSpyMatcher);
+ globalHighlighter.applyHighlights();
+
+ String hlText = "_printf";
+ Color hlColor = Palette.PINK;
+ CTokenHighlightMatcher hlMatcher = token -> {
+ if (token.getText().contains(hlText)) {
+ return hlColor;
+ }
+ return null;
+ };
+ SpyCTokenHighlightMatcher spyMatcher = new SpyCTokenHighlightMatcher(hlMatcher);
+ Function function = getCurrentFunction();
+ DecompilerHighlighter highlighter = hlService.createHighlighter(function, spyMatcher);
+ highlighter.applyHighlights();
+
+ assertAllHighlighterFieldsHighlighted(globalSpyMatcher, globalHlText, globalHlColor);
+ assertAllHighlighterFieldsHighlighted(spyMatcher, hlText, hlColor);
+
+ globalSpyMatcher.clear();
+ spyMatcher.clear();
+
+ // this function also has calls to '_printf' (function-specific) and 'structure' (global)
+ decompile("100000e10"); // '_call_structure_B'
+
+ // this function should NOT show function-specific highlights, since is not the same
+ // function that was used to create the highlights, but should show global highlights
+ assertAllHighlighterFieldsHighlighted(globalSpyMatcher, globalHlText, globalHlColor);
+ assertTrue(spyMatcher.getMatchingTokens().isEmpty());
+
+ // go back to the original function and verify the highlights are there
+ globalSpyMatcher.clear();
+ spyMatcher.clear();
+ decompile("100000d60");
+ assertAllHighlighterFieldsHighlighted(globalSpyMatcher, globalHlText, globalHlColor);
+ assertAllHighlighterFieldsHighlighted(spyMatcher, hlText, hlColor);
+
+ globalHighlighter.dispose();
+ highlighter.dispose();
+ assertNoFieldsSecondaryHighlighted(globalHlText);
+ assertNoFieldsSecondaryHighlighted(hlText);
+ }
+
//==================================================================================================
// Private Methods
//==================================================================================================
@@ -1946,7 +2079,7 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
private Color getBlendedColor(Color... colors) {
DecompilerPanel panel = provider.getController().getDecompilerPanel();
ClangHighlightController highlightController = panel.getHighlightController();
- List<Color> colorList = Arrays.asList(colors);
+ Set<Color> colorList = Set.of(colors);
return highlightController.blend(colorList);
}
@@ -2216,7 +2349,7 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
ClangToken token = entry.getKey();
HighlightToken hlToken = providerHighlights.get(token);
- assertNotNull("Provider is missing highlighted token", hlToken);
+ assertNotNull("Provider is missing highlighted token: " + token, hlToken);
Color color = entry.getValue();
Color combinedColor = getCombinedHighlightColor(theProvider, token);
ColorMatcher cm = new ColorMatcher(color, combinedColor);
@@ -2316,8 +2449,8 @@ public class DecompilerClangTest extends AbstractDecompilerTest {
fail("Could not find Decompiler Copy action");
}
- private void setComment(String address, int type, String comment) {
- applyCmd(program, new SetCommentCmd(addr(address), type, comment));
+ private void setComment(String address, CommentType type, String comment) {
+ applyCmd(program, new SetCommentCmd(addr(address), type.ordinal(), comment));
}
private void assertNextTokenIndex(int expectedIndex, int line, int... cols) {
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java
index 485c9acad7..33550e2724 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerTest.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,6 +29,7 @@ import docking.widgets.fieldpanel.support.FieldLocation;
import ghidra.app.decompiler.ClangToken;
import ghidra.app.decompiler.component.ClangTextField;
import ghidra.app.decompiler.component.DecompilerPanel;
+import ghidra.program.model.listing.Function;
import ghidra.program.model.pcode.HighFunction;
import ghidra.test.AbstractProgramBasedTest;
@@ -202,4 +203,9 @@ public abstract class AbstractDecompilerTest extends AbstractProgramBasedTest {
protected HighFunction getHighFunction() {
return provider.getController().getHighFunction();
}
+
+ protected Function getCurrentFunction() {
+ return provider.getController().getFunction();
+ }
+
}