Skip to content

Commit

Permalink
implemented forgotten distinct builtin. added boundary check for goto…
Browse files Browse the repository at this point in the history
… builtin. various enhancements
  • Loading branch information
0ffffffffh committed May 19, 2019
1 parent 793ccda commit 2b5aa01
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 12 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ The following API documentations and their behaviors may change until reached fi
| Description | Locates the current address selection by given offset. Real address value calculated by adding the offset value to the image base value. |
| Aliases | None |

***Note***: *This built-in does not perform any boundary check for now. I have to implement strict boundary check for each executable sections. If you give a value that out of valid boundary, you may experience undefined behavior.*



**import(** *String* : filePathOrCoverageName **)**
Expand Down
2 changes: 1 addition & 1 deletion extension.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name=@extname@
name=Dragon Dance
description=Coverage data visualizer plugin.
author=Oguz Kartal
createdOn=
Expand Down
1 change: 1 addition & 0 deletions src/main/java/dragondance/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Globals {
public static boolean EnableStdoutLog=false;
public static boolean DumpInstructions=false;

public static String LastFileDialogPath="";

public static final float MIN_HUE = 190.0f;
public static final float MAX_HUE = 360.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface GuiAffectedOpInterface {
public CoverageData loadCoverage(String coverageDataFile) throws FileNotFoundException;
public boolean removeCoverage(int id);
public boolean visualizeCoverage(CoverageData coverage);
public void goTo(long offset);
public boolean goTo(long offset);
}
10 changes: 8 additions & 2 deletions src/main/java/dragondance/components/MainDockProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,14 @@ public boolean visualizeCoverage(CoverageData coverage) {
}

@Override
public void goTo(long offset) {
DragonHelper.goToAddress(DragonHelper.getImageBase().getOffset() + offset);
public boolean goTo(long offset) {
boolean success = DragonHelper.goToAddress(DragonHelper.getImageBase().getOffset() + offset);

if (!success) {
DragonHelper.showWarning("offset 0x%x is not valid",offset);
}

return success;
}


Expand Down
46 changes: 40 additions & 6 deletions src/main/java/dragondance/eng/DragonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dragondance.Globals;
import dragondance.StringResources;
import dragondance.exceptions.InvalidInstructionAddress;
import dragondance.util.Util;
import generic.concurrent.GThreadPool;
import generic.jar.ResourceFile;
import ghidra.app.plugin.core.colorizer.ColorizingService;
Expand Down Expand Up @@ -217,13 +218,34 @@ public static void printConsole(String format, Object... args) {

}

public static void goToAddress(long addr) {
public static boolean isValidExecutableSectionAddress(long addr) {
if (addr < getImageBase().getOffset())
return false;

if (addr >= getImageEnd().getOffset())
return false;

return isCodeSectionAddress(addr);
}


public static boolean goToAddress(long addr) {
GoToService gotoService = tool.getService(GoToService.class);

if (gotoService==null)
return;
return false;

if (!isValidExecutableSectionAddress(addr)) {
showWarning("%x is not valid offset.",addr);
return false;
}


if (getInstructionNoThrow(getAddress(addr),true) == null) {
return false;
}

gotoService.goTo(getAddress(addr));
return gotoService.goTo(getAddress(addr));
}

public static Address getAddress(long addrValue) {
Expand All @@ -233,9 +255,12 @@ public static Address getAddress(long addrValue) {
public static String askFile(Component parent, String title, String okButtonText) {

GhidraFileChooser gfc = new GhidraFileChooser(parent);
File def = new File("D:\\Tools\\coveragetools\\pintool");

gfc.setSelectedFile(def);
if (!Globals.LastFileDialogPath.isEmpty()) {
File def = new File(Globals.LastFileDialogPath);
gfc.setSelectedFile(def);
}

gfc.setTitle(title);
gfc.setApproveButtonText(okButtonText);
gfc.setFileSelectionMode(GhidraFileChooserMode.FILES_ONLY);
Expand All @@ -249,6 +274,11 @@ public static String askFile(Component parent, String title, String okButtonText
if (!file.exists())
return null;

Globals.LastFileDialogPath = Util.getDirectoryOfFile(file.getAbsolutePath());

if (Globals.LastFileDialogPath == null)
Globals.LastFileDialogPath = System.getProperty("user.dir");

return file.getAbsolutePath();
}

Expand All @@ -275,6 +305,10 @@ public static Address getImageBase() {
return fapi.getCurrentProgram().getImageBase();
}

public static Address getImageEnd() {
return fapi.getCurrentProgram().getMaxAddress();
}

public static InstructionContext getInstruction(long addr, boolean throwEx) throws InvalidInstructionAddress {
return getInstruction(fapi.toAddr(addr),throwEx);
}
Expand Down Expand Up @@ -414,7 +448,7 @@ public static boolean isCodeSectionAddress(long addr) {
List<MemoryBlock> execBlocks = getExecutableMemoryBlocks();

for (MemoryBlock mb : execBlocks) {
if (addr >= mb.getStart().getOffset() && addr <= mb.getEnd().getOffset()) {
if (addr >= mb.getStart().getOffset() && addr < mb.getEnd().getOffset()) {
status=true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dragondance.scripting.functions.impl;

import dragondance.datasource.CoverageData;
import dragondance.scripting.functions.BuiltinAlias;
import dragondance.scripting.functions.BuiltinFunctionBase;

Expand All @@ -9,5 +10,23 @@ public class BuiltinFunctionDistinct extends BuiltinFunctionBase {
public BuiltinFunctionDistinct() {
super("distinct");
}

@Override
public int requiredArgCount(boolean minimum) {
if (minimum)
return 2;

return -1;
}

@Override
public CoverageData execute() {
CoverageData[] finalArgs = prepareFinalArguments();

setReturn(CoverageData.distinct(finalArgs));

return super.execute();
}


}
9 changes: 9 additions & 0 deletions src/main/java/dragondance/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ public static String getObjectNameFromPath(String path) {
return path.substring(p1+1, p2);
}

public static String getDirectoryOfFile(String path) {
File file = new File(path);

if (file.isDirectory())
return file.getAbsolutePath();

return file.getParent();
}

public static String md5(String sval) {
return md5(sval.getBytes());
}
Expand Down

0 comments on commit 2b5aa01

Please sign in to comment.