Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3832 importer/exporter for SARIF

d-millar31ca84453a12
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/DataTypeWriterTask.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/DataTypeWriterTask.java
new file mode 100644
index 0000000000..56aeb1d088
--- /dev/null
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/DataTypeWriterTask.java
@@ -0,0 +1,71 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.plugin.core.datamgr.actions;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+
+import com.google.gson.JsonObject;
+
+import docking.widgets.tree.GTree;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.ISF.IsfDataTypeWriter;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.Task;
+import ghidra.util.task.TaskMonitor;
+
+public class DataTypeWriterTask extends Task {
+
+ private final DataTypeManager programDataTypeMgr;
+ private final List<DataType> dataTypeList;
+ private final File file;
+ private final GTree gTree;
+
+ public DataTypeWriterTask(GTree gTree, DataTypeManager programDataTypeMgr, List<DataType> dataTypeList, File file) {
+ super("Export Data Types", true, false, true);
+ this.gTree = gTree;
+ this.programDataTypeMgr = programDataTypeMgr;
+ this.dataTypeList = dataTypeList;
+ this.file = file;
+ }
+
+ @Override
+ public void run(TaskMonitor monitor) {
+ try {
+ //monitor.setMessage("Export to " + file.getName() + "...");
+ FileWriter baseWriter = file == null ? null : new FileWriter(file);
+ IsfDataTypeWriter dataTypeWriter = new IsfDataTypeWriter(programDataTypeMgr, dataTypeList, baseWriter);
+
+ try {
+ JsonObject object = dataTypeWriter.getRootObject(monitor);
+ if (file != null) {
+ dataTypeWriter.write(object);
+ }
+ } finally {
+ dataTypeWriter.close();
+ }
+ } catch (CancelledException e) {
+ // user cancelled; ignore
+ } catch (IOException e) {
+ Msg.showError(getClass(), gTree, "Export Data Types Failed", "Error exporting Data Types: " + e);
+ return;
+ }
+ }
+}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/ExportToIsfAction.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/ExportToIsfAction.java
index 893caf3bea..61be0bd957 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/ExportToIsfAction.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/app/plugin/core/datamgr/actions/ExportToIsfAction.java
@@ -188,14 +188,14 @@ public class ExportToIsfAction extends DockingAction {
fileChooser.dispose();
}
- private class DataTypeWriterTask extends Task {
+ public class DataTypeWriterTask extends Task {
private final DataTypeManager programDataTypeMgr;
private final List<DataType> dataTypeList;
private final File file;
private final GTree gTree;
- DataTypeWriterTask(GTree gTree, DataTypeManager programDataTypeMgr,
+ public DataTypeWriterTask(GTree gTree, DataTypeManager programDataTypeMgr,
List<DataType> dataTypeList, File file) {
super("Export Data Types", true, false, true);
this.gTree = gTree;
@@ -209,12 +209,9 @@ public class ExportToIsfAction extends DockingAction {
try {
monitor.setMessage("Export to " + file.getName() + "...");
IsfDataTypeWriter dataTypeWriter =
- new IsfDataTypeWriter(programDataTypeMgr, new FileWriter(file));
+ new IsfDataTypeWriter(programDataTypeMgr, dataTypeList, new FileWriter(file));
try {
- for (DataType dataType : dataTypeList) {
- dataTypeWriter.requestType(dataType);
- }
JsonObject object = dataTypeWriter.getRootObject(monitor);
dataTypeWriter.write(object);
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/dbg/isf/IsfClientHandler.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/dbg/isf/IsfClientHandler.java
index 05f1b64011..c2a747a7ed 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/dbg/isf/IsfClientHandler.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/dbg/isf/IsfClientHandler.java
@@ -130,7 +130,7 @@ public class IsfClientHandler {
private String lookType(String ns, String key) throws IOException {
IsfDataTypeWriter isfWriter = createDataTypeWriter(server.getDataTypeManager(ns));
isfWriter.setSkipSymbols(true);
- isfWriter.requestType(key);
+ //isfWriter.requestType(key);
return writeFrom(isfWriter);
}
@@ -198,7 +198,7 @@ public class IsfClientHandler {
private IsfDataTypeWriter createDataTypeWriter(DataTypeManager dtm) throws IOException {
StringWriter out = new StringWriter();
- return new IsfDataTypeWriter(dtm, out);
+ return new IsfDataTypeWriter(dtm, null, out);
}
private String writeFrom(IsfDataTypeWriter dataTypeWriter) throws IOException {
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/AbstractIsfObject.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/AbstractIsfObject.java
new file mode 100644
index 0000000000..e793e7d3eb
--- /dev/null
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/AbstractIsfObject.java
@@ -0,0 +1,60 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.program.model.data.ISF;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.docking.settings.Settings;
+import ghidra.docking.settings.SettingsDefinition;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfWriter.Exclude;
+
+public abstract class AbstractIsfObject implements IsfObject {
+
+ @Exclude
+ public String name;
+ @Exclude
+ public String location;
+ @Exclude
+ public List<IsfSetting> settings;
+
+ public AbstractIsfObject(DataType dt) {
+ if (dt != null) {
+ name = dt.getName();
+ location = dt.getCategoryPath().getPath();
+ Settings defaultSettings = dt.getDefaultSettings();
+ processSettings(dt, defaultSettings);
+ }
+ }
+
+ protected void processSettings(DataType dt, Settings defaultSettings) {
+ SettingsDefinition[] settingsDefinitions = dt.getSettingsDefinitions();
+ for (SettingsDefinition def : settingsDefinitions) {
+ if (def.hasValue(defaultSettings)) {
+ settings = new ArrayList<>();
+ String[] names = defaultSettings.getNames();
+ for (String n : names) {
+ Object value = defaultSettings.getValue(n);
+ if (value != null) {
+ IsfSetting setting = new IsfSetting(n, value);
+ settings.add(setting);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/AbstractIsfWriter.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/AbstractIsfWriter.java
new file mode 100644
index 0000000000..9afd82d11e
--- /dev/null
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/AbstractIsfWriter.java
@@ -0,0 +1,108 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.program.model.data.ISF;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Writer;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldAttributes;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.stream.JsonWriter;
+
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+
+public abstract class AbstractIsfWriter implements Closeable {
+
+ protected JsonWriter writer;
+ protected Gson gson = new GsonBuilder().setPrettyPrinting().create();
+
+ protected JsonObject root = new JsonObject();
+ protected JsonArray objects = new JsonArray();
+
+ public AbstractIsfWriter(Writer baseWriter) throws IOException {
+ if (writer != null) {
+ this.writer = new JsonWriter(baseWriter);
+ writer.setIndent(" ");
+ }
+ this.gson = new GsonBuilder().addSerializationExclusionStrategy(strategy).setPrettyPrinting().create();
+ }
+
+ protected abstract void genRoot(TaskMonitor monitor) throws CancelledException, IOException;
+
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.FIELD)
+ public @interface Exclude {
+ // EMPTY
+ }
+
+ // Am setting this as the default, but it's possible we may want more latitude
+ // in the future
+ protected boolean STRICT = true;
+
+ // @Exclude used for properties that might be desirable for a non-STRICT
+ // implementation.
+ ExclusionStrategy strategy = new ExclusionStrategy() {
+ @Override
+ public boolean shouldSkipClass(Class<?> clazz) {
+ return false;
+ }
+
+ @Override
+ public boolean shouldSkipField(FieldAttributes field) {
+ return STRICT && field.getAnnotation(Exclude.class) != null;
+ }
+ };
+
+ public JsonObject getRootObject(TaskMonitor monitor) throws CancelledException, IOException {
+ genRoot(monitor);
+ return root;
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+ public JsonElement getTree(Object obj) {
+ return gson.toJsonTree(obj);
+ }
+
+ public Object getObject(JsonElement element, Class<? extends Object> clazz) {
+ return gson.fromJson(element, clazz);
+ }
+
+ public void write(JsonObject object) {
+ gson.toJson(object, writer);
+ }
+
+ public void close() throws IOException {
+ if (writer != null) {
+ writer.flush();
+ writer.close();
+ }
+ }
+
+}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfBuiltIn.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfBuiltIn.java
index b4311bd6ee..394d19f544 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfBuiltIn.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfBuiltIn.java
@@ -17,16 +17,15 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.BuiltInDataType;
-public class IsfBuiltIn implements IsfObject {
+public class IsfBuiltIn extends AbstractIsfObject {
public Integer size;
- public Boolean signed;
public String kind;
public String endian;
public IsfBuiltIn(BuiltInDataType builtin) {
+ super(builtin);
size = IsfUtilities.getLength(builtin);
- signed = IsfUtilities.getSigned(builtin);
kind = IsfUtilities.getBuiltInKind(builtin);
endian = IsfUtilities.getEndianness(builtin);
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComponent.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComponent.java
index 1c5f07b49c..87bd7cd8a0 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComponent.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComponent.java
@@ -16,9 +16,9 @@
package ghidra.program.model.data.ISF;
import ghidra.program.model.data.DataTypeComponent;
-import ghidra.program.model.data.ISF.IsfDataTypeWriter.Exclude;
+import ghidra.program.model.data.ISF.AbstractIsfWriter.Exclude;
-public class IsfComponent implements IsfObject {
+public class IsfComponent extends AbstractIsfObject {
public Integer offset;
public IsfObject type;
@@ -30,16 +30,25 @@ public class IsfComponent implements IsfObject {
@Exclude
public String field_name;
@Exclude
+ public Boolean noFieldName;
+ @Exclude
public String comment;
+
public IsfComponent(DataTypeComponent component, IsfObject typeObj) {
+ super(component.getDataType());
offset = component.getOffset();
type = typeObj;
field_name = component.getFieldName();
+ if (field_name == null || field_name.equals("")) {
+ noFieldName = true;
+ }
ordinal = component.getOrdinal();
length = component.getLength();
comment = component.getComment();
+
+ processSettings(component.getDataType(), component.getDefaultSettings());
}
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComposite.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComposite.java
index 26d91e92d7..fe8cfd17a6 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComposite.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfComposite.java
@@ -15,52 +15,50 @@
*/
package ghidra.program.model.data.ISF;
-import java.util.*;
-
import com.google.gson.JsonObject;
-import ghidra.program.model.data.*;
-import ghidra.program.model.data.ISF.IsfDataTypeWriter.Exclude;
+import ghidra.program.model.data.Composite;
+import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.data.Structure;
import ghidra.util.task.TaskMonitor;
-public class IsfComposite implements IsfObject {
+public class IsfComposite extends AbstractIsfObject {
public String kind;
public Integer size;
public JsonObject fields;
-
- @Exclude
- public int alignment;
-
+
public IsfComposite(Composite composite, IsfDataTypeWriter writer, TaskMonitor monitor) {
+ super(composite);
size = composite.getLength();
kind = composite instanceof Structure ? "struct" : "union";
- alignment = composite.getAlignment();
DataTypeComponent[] components = composite.getComponents();
- Map<String, DataTypeComponent> comps = new HashMap<>();
- for (DataTypeComponent component : components) {
- String key = component.getFieldName();
- if (key == null) {
- key = component.getDefaultFieldName();
- }
- comps.put(key, component);
+ if (components.length == 0) {
+ // NB: composite.getLength always returns > 0
+ size = 0;
}
- ArrayList<String> keylist = new ArrayList<>(comps.keySet());
- Collections.sort(keylist);
-
fields = new JsonObject();
- for (String key : keylist) {
+ for (DataTypeComponent component : components) {
if (monitor.isCancelled()) {
break;
}
- DataTypeComponent component = comps.get(key);
IsfObject type = writer.getObjectTypeDeclaration(component);
- IsfComponent cobj = new IsfComponent(component, type);
+ IsfComponent cobj = getComponent(component, type);
+ String key = component.getFieldName();
+ if (key == null) {
+ key = DataTypeComponent.DEFAULT_FIELD_NAME_PREFIX + component.getOrdinal();
+ if (component.getParent() instanceof Structure) {
+ key += "_0x" + Integer.toHexString(component.getOffset());
+ }
+ }
fields.add(key, writer.getTree(cobj));
}
+ }
+ protected IsfComponent getComponent(DataTypeComponent component, IsfObject type) {
+ return new IsfComponent(component, type);
}
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeArray.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeArray.java
index 5478d3b574..7551629b82 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeArray.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeArray.java
@@ -17,13 +17,14 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.Array;
-public class IsfDataTypeArray implements IsfObject {
+public class IsfDataTypeArray extends AbstractIsfObject {
public String kind;
public Integer count;
public IsfObject subtype;
public IsfDataTypeArray(Array arr, IsfObject typeObj) {
+ super(arr);
kind = IsfUtilities.getKind(arr);
count = arr.getNumElements();
subtype = typeObj;
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeBitField.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeBitField.java
index 9ee584f02e..ff74ec0819 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeBitField.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeBitField.java
@@ -16,9 +16,9 @@
package ghidra.program.model.data.ISF;
import ghidra.program.model.data.BitFieldDataType;
-import ghidra.program.model.data.ISF.IsfDataTypeWriter.Exclude;
+import ghidra.program.model.data.ISF.AbstractIsfWriter.Exclude;
-public class IsfDataTypeBitField implements IsfObject {
+public class IsfDataTypeBitField extends AbstractIsfObject {
public String kind;
public Integer bit_length;
@@ -31,6 +31,7 @@ public class IsfDataTypeBitField implements IsfObject {
private int storage_size;
public IsfDataTypeBitField(BitFieldDataType bf, int componentOffset, IsfObject typeObj) {
+ super(bf);
kind = IsfUtilities.getKind(bf);
bit_length = bf.getBitSize();
bit_offset = bf.getBitOffset();
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeDefault.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeDefault.java
index 2fb90b2912..67bd6ae636 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeDefault.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeDefault.java
@@ -17,14 +17,15 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.DataType;
-public class IsfDataTypeDefault implements IsfObject {
+public class IsfDataTypeDefault extends AbstractIsfObject {
public String kind;
- public String name;
+ int size;
public IsfDataTypeDefault(DataType dt) {
+ super(dt);
kind = IsfUtilities.getKind(dt);
- name = dt.getName();
+ size = dt.getLength();
}
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeTypeDef.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeTypeDef.java
index 82e5b32857..c37fff6355 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeTypeDef.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeTypeDef.java
@@ -17,12 +17,13 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.DataType;
-public class IsfDataTypeTypeDef implements IsfObject {
+public class IsfDataTypeTypeDef extends AbstractIsfObject {
public String kind;
public IsfObject subtype;
public IsfDataTypeTypeDef(DataType dt, IsfObject typeObj) {
+ super(dt);
kind = IsfUtilities.getKind(dt);
subtype = typeObj;
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeWriter.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeWriter.java
index de19527332..e12ebf42a1 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeWriter.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDataTypeWriter.java
@@ -17,20 +17,43 @@ package ghidra.program.model.data.ISF;
import java.io.IOException;
import java.io.Writer;
-import java.lang.annotation.*;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import java.util.Map.Entry;
-import com.google.gson.*;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
import com.google.gson.stream.JsonWriter;
import ghidra.program.database.data.ProgramDataTypeManager;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressFormatException;
-import ghidra.program.model.data.*;
+import ghidra.program.model.data.Array;
+import ghidra.program.model.data.BitFieldDataType;
+import ghidra.program.model.data.BuiltInDataType;
+import ghidra.program.model.data.Composite;
+import ghidra.program.model.data.DataOrganization;
+import ghidra.program.model.data.DataOrganizationImpl;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.Dynamic;
import ghidra.program.model.data.Enum;
+import ghidra.program.model.data.FactoryDataType;
+import ghidra.program.model.data.FunctionDefinition;
+import ghidra.program.model.data.Pointer;
+import ghidra.program.model.data.TypeDef;
import ghidra.program.model.listing.Program;
-import ghidra.program.model.symbol.*;
+import ghidra.program.model.symbol.Reference;
+import ghidra.program.model.symbol.ReferenceIterator;
+import ghidra.program.model.symbol.ReferenceManager;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolIterator;
+import ghidra.program.model.symbol.SymbolTable;
import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor;
@@ -40,29 +63,28 @@ import ghidra.util.task.TaskMonitor;
*
* The ISF JSON should be valid for Volatility is STRICT==true.
*/
-public class IsfDataTypeWriter {
+public class IsfDataTypeWriter extends AbstractIsfWriter {
- private Map<DataType, IsfObject> resolved = new HashMap<>();
+ protected Map<DataType, IsfObject> resolved = new HashMap<>();
private Map<String, DataType> resolvedTypeMap = new HashMap<>();
- private List<String> deferredKeys = new ArrayList<>();
+ public List<String> deferredKeys = new ArrayList<>();
private Writer baseWriter;
- private JsonWriter writer;
- private Gson gson = new GsonBuilder().setPrettyPrinting().create();
- private DataTypeManager dtm;
+ protected DataTypeManager dtm;
private DataOrganization dataOrganization;
- private JsonObject data = new JsonObject();
- private JsonObject metadata = new JsonObject();
- private JsonObject baseTypes = new JsonObject();
- private JsonObject userTypes = new JsonObject();
- private JsonObject enums = new JsonObject();
- private JsonObject symbols = new JsonObject();
+ protected JsonObject data = new JsonObject();
+ protected JsonElement metadata;
+ protected JsonElement baseTypes;
+ protected JsonElement userTypes;
+ protected JsonElement enums;
+ protected JsonElement functions;
+ protected JsonElement symbols;
private List<Address> requestedAddresses = new ArrayList<>();
private List<String> requestedSymbols = new ArrayList<>();
- private List<String> requestedTypes = new ArrayList<>();
+ // private List<String> requestedTypes = new ArrayList<>();
private List<DataType> requestedDataTypes = new ArrayList<>();
private boolean skipSymbols = false;
private boolean skipTypes = false;
@@ -70,11 +92,13 @@ public class IsfDataTypeWriter {
/**
* Constructs a new instance of this class using the given writer
*
- * @param dtm data-type manager corresponding to target program or null for default
+ * @param dtm data-type manager corresponding to target program or null
+ * for default
* @param baseWriter the writer to use when writing data types
* @throws IOException if there is an exception writing the output
*/
- public IsfDataTypeWriter(DataTypeManager dtm, Writer baseWriter) throws IOException {
+ public IsfDataTypeWriter(DataTypeManager dtm, List<DataType> target, Writer baseWriter) throws IOException {
+ super(baseWriter);
this.dtm = dtm;
if (dtm != null) {
dataOrganization = dtm.getDataOrganization();
@@ -82,66 +106,43 @@ public class IsfDataTypeWriter {
if (dataOrganization == null) {
dataOrganization = DataOrganizationImpl.getDefaultOrganization();
}
- this.baseWriter = baseWriter;
- this.writer = new JsonWriter(baseWriter);
- writer.setIndent(" ");
- this.gson = new GsonBuilder()
- .addSerializationExclusionStrategy(strategy)
- .setPrettyPrinting()
- .create();
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.FIELD)
- public @interface Exclude {
- //EMPTY
+ metadata = new JsonObject();
+ baseTypes = new JsonObject();
+ userTypes = new JsonObject();
+ enums = new JsonObject();
+ functions = new JsonObject();
+ symbols = new JsonObject();
+ requestedDataTypes = target;
+ STRICT = true;
}
- // Am setting this as the default, but it's possible we may want more latitude in the future
- private boolean STRICT = true;
-
- // @Exclude used for properties that might be desirable for a non-STRICT implementation.
- ExclusionStrategy strategy = new ExclusionStrategy() {
- @Override
- public boolean shouldSkipClass(Class<?> clazz) {
- return false;
- }
-
- @Override
- public boolean shouldSkipField(FieldAttributes field) {
- return STRICT && field.getAnnotation(Exclude.class) != null;
- }
- };
-
- /**
- * Exports all data types in the list as ISF JSON.
- *
- * @param monitor the task monitor
- * @return the resultant JSON object
- * @throws IOException if there is an exception writing the output
- * @throws CancelledException if the action is cancelled by the user
- */
- public JsonObject getRootObject(TaskMonitor monitor)
- throws IOException, CancelledException {
-
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
genMetadata();
genTypes(monitor);
- genSymbols();
- genRoot();
+ genSymbols(monitor);
- return data;
- }
-
- private void genRoot() {
data.add("metadata", metadata);
data.add("base_types", baseTypes);
data.add("user_types", userTypes);
data.add("enums", enums);
- // Would be nice to support this in the futere, but Volatility does not
- //data.add("typedefs", typedefs);
+ // Would be nice to support this in the future, but Volatility does not
+ // data.add("functions", functions);
data.add("symbols", symbols);
}
+ public void add(JsonElement parent, String optKey, JsonElement child) {
+ if (parent instanceof JsonObject) {
+ JsonObject p = (JsonObject) parent;
+ p.add(optKey, child);
+ }
+ if (parent instanceof JsonArray) {
+ JsonArray p = (JsonArray) parent;
+ p.add(child);
+ }
+ }
+
private void genMetadata() {
String oskey = "UNKNOWN";
if (dtm instanceof ProgramDataTypeManager) {
@@ -154,20 +155,21 @@ public class IsfDataTypeWriter {
oskey = metaData.get("Compiler ID");
if (metaData.containsKey("PDB Loaded")) {
os = gson.toJsonTree(new IsfWinOS(metaData));
- }
- else if (metaData.containsKey("Executable Format")) {
+ } else if (metaData.containsKey("Executable Format")) {
if (metaData.get("Executable Format").contains("ELF")) {
oskey = "linux";
os = gson.toJsonTree(new IsfLinuxOS(gson, metaData));
}
}
- metadata.addProperty("format", "6.2.0");
- metadata.add("producer", producer);
- metadata.add(oskey, os);
+ if (metadata instanceof JsonObject) {
+ ((JsonObject) metadata).addProperty("format", "6.2.0");
+ }
+ add(metadata, "producer", producer);
+ add(metadata, oskey, os);
}
}
- private void genSymbols() {
+ private void genSymbols(TaskMonitor monitor) {
if (!skipSymbols && dtm instanceof ProgramDataTypeManager) {
ProgramDataTypeManager pgmDtm = (ProgramDataTypeManager) dtm;
Program program = pgmDtm.getProgram();
@@ -194,18 +196,15 @@ public class IsfDataTypeWriter {
Symbol symbol = iterator.next();
symbolToJson(imageBase, symbolTable, linkages, map, symbol);
}
- }
- else {
+ } else {
for (Address addr : requestedAddresses) {
- Symbol[] symsFromAddr =
- symbolTable.getSymbols(addr.add(imageBase.getOffset()));
+ Symbol[] symsFromAddr = symbolTable.getSymbols(addr.add(imageBase.getOffset()));
for (Symbol symbol : symsFromAddr) {
symbolToJson(imageBase, symbolTable, linkages, map, symbol);
}
}
}
- }
- else {
+ } else {
for (String key : requestedSymbols) {
SymbolIterator iter = symbolTable.getSymbols(key);
while (iter.hasNext()) {
@@ -215,33 +214,29 @@ public class IsfDataTypeWriter {
}
}
for (Entry<String, JsonObject> entry : map.entrySet()) {
- symbols.add(entry.getKey(), entry.getValue());
+ add(symbols, entry.getKey(), entry.getValue());
}
for (Entry<String, JsonObject> entry : map.entrySet()) {
if (entry.getKey().startsWith("_")) {
String nu = entry.getKey().substring(1);
- if (symbols.get(nu) == null) {
- symbols.add(nu, entry.getValue());
- }
+ add(symbols, nu, entry.getValue());
}
}
}
}
- private void genTypes(TaskMonitor monitor)
- throws CancelledException, IOException {
+ private void genTypes(TaskMonitor monitor) throws CancelledException, IOException {
if (skipTypes) {
return;
}
Map<String, DataType> map = new HashMap<>();
if (requestedDataTypes.isEmpty()) {
dtm.getAllDataTypes(requestedDataTypes);
- baseTypes.add("pointer", getTree(new IsfTypedefPointer()));
- baseTypes.add("undefined", getTree(new IsfTypedefPointer()));
+ addSingletons();
}
monitor.initialize(requestedDataTypes.size());
for (DataType dataType : requestedDataTypes) {
- String key = dataType.getName();
+ String key = dataType.getPathName();
map.put(key, dataType);
}
@@ -260,10 +255,9 @@ public class IsfDataTypeWriter {
private void processMap(Map<String, DataType> map, List<String> keylist, TaskMonitor monitor)
throws CancelledException, IOException {
JsonObject obj = new JsonObject();
- int cnt = 0;
+ monitor.setMaximum(keylist.size());
for (String key : keylist) {
DataType dataType = map.get(key);
- monitor.checkCancelled();
if (key.contains(".conflict")) {
continue;
}
@@ -272,36 +266,29 @@ public class IsfDataTypeWriter {
continue;
}
if (dataType instanceof FunctionDefinition) {
- // Would be nice to support this in the futere, but Volatility does not
- //typedefs.add(dataType.getName(), obj);
- }
- else if (IsfUtilities.isBaseDataType(dataType)) {
- baseTypes.add(dataType.getName(), obj);
- }
- else if (dataType instanceof TypeDef) {
+ // Would be nice to support this in the future, but Volatility does not
+ add(functions, dataType.getPathName(), obj);
+ } else if (IsfUtilities.isBaseDataType(dataType)) {
+ add(baseTypes, dataType.getPathName(), obj);
+ } else if (dataType instanceof TypeDef) {
DataType baseDataType = ((TypeDef) dataType).getBaseDataType();
if (IsfUtilities.isBaseDataType(baseDataType)) {
- baseTypes.add(dataType.getName(), obj);
- }
- else if (baseDataType instanceof Enum) {
- enums.add(dataType.getName(), obj);
+ add(baseTypes, dataType.getPathName(), obj);
+ } else if (baseDataType instanceof Enum) {
+ add(enums, dataType.getPathName(), obj);
+ } else {
+ add(userTypes, dataType.getPathName(), obj);
}
- else {
- userTypes.add(dataType.getName(), obj);
- }
- }
- else if (dataType instanceof Enum) {
- enums.add(dataType.getName(), obj);
- }
- else if (dataType instanceof Composite) {
- userTypes.add(dataType.getName(), obj);
+ } else if (dataType instanceof Enum) {
+ add(enums, dataType.getPathName(), obj);
+ } else if (dataType instanceof Composite) {
+ add(userTypes, dataType.getPathName(), obj);
}
- monitor.setProgress(++cnt);
+ monitor.increment();
}
}
- private void symbolToJson(Address imageBase, SymbolTable symbolTable,
- Map<String, Symbol> linkages,
+ private void symbolToJson(Address imageBase, SymbolTable symbolTable, Map<String, Symbol> linkages,
Map<String, JsonObject> map, Symbol symbol) {
String key = symbol.getName();
Address address = symbol.getAddress();
@@ -313,9 +300,12 @@ public class IsfDataTypeWriter {
sym.addProperty("linkage_name", linkage.getName());
sym.addProperty("address", linkage.getAddress().getOffset());
}
- }
- else {
- sym.addProperty("address", address.subtract(imageBase));
+ } else {
+ if (address.getAddressSpace().equals(imageBase.getAddressSpace())) {
+ sym.addProperty("address", address.subtract(imageBase));
+ } else {
+ sym.addProperty("address", address.getOffset());
+ }
}
map.put(symbol.getName(), sym);
if (!symbol.isPrimary()) {
@@ -332,8 +322,12 @@ public class IsfDataTypeWriter {
gson.toJson(obj, writer);
}
- JsonObject getObjectForDataType(DataType dt, TaskMonitor monitor)
- throws IOException, CancelledException {
+ protected void addSingletons() {
+ add(baseTypes, "pointer", getTree(newTypedefPointer(null)));
+ add(baseTypes, "undefined", getTree(newTypedefPointer(null)));
+ }
+
+ protected JsonObject getObjectForDataType(DataType dt, TaskMonitor monitor) throws IOException, CancelledException {
IsfObject isf = getIsfObject(dt, monitor);
if (isf != null) {
JsonObject jobj = (JsonObject) getTree(isf);
@@ -344,26 +338,27 @@ public class IsfDataTypeWriter {
}
/**
- * Writes the data type as ISF JSON using the underlying writer. For now, ignoring top-level
- * bit-fields and function defs as unsupported by ISF. Typedefs really deserve their own
- * category, but again unsupported.
+ * Writes the data type as ISF JSON using the underlying writer. For now,
+ * ignoring top-level bit-fields and function defs as unsupported by ISF.
+ * Typedefs really deserve their own category, but again unsupported.
*
- * @param dt the data type to write as ISF JSON
+ * @param dt the data type to write as ISF JSON
* @param monitor the task monitor
* @throws IOException if there is an exception writing the output
*/
- private IsfObject getIsfObject(DataType dt, TaskMonitor monitor)
- throws IOException, CancelledException {
+ protected IsfObject getIsfObject(DataType dt, TaskMonitor monitor) throws IOException, CancelledException {
if (dt == null) {
- Msg.error(this, "Shouldn't get here - null datatype passed");
- return null;
+ throw new IOException("Null datatype passed to getIsfObject");
}
if (dt instanceof FactoryDataType) {
Msg.error(this, "Factory data types may not be written - type: " + dt);
}
- if (dt instanceof Pointer || dt instanceof Array || dt instanceof BitFieldDataType) {
+ if (dt instanceof BitFieldDataType) {
+ Msg.error(this, "BitField data types may not be written - type: " + dt);
+ }
+ if (dt instanceof Pointer || dt instanceof Array) {
IsfObject type = getObjectDataType(IsfUtilities.getBaseDataType(dt));
- IsfObject obj = new IsfTypedObject(dt, type);
+ IsfObject obj = newTypedObject(dt, type);
return obj;
}
@@ -377,41 +372,33 @@ public class IsfDataTypeWriter {
if (dt instanceof Dynamic dynamic) {
DataType rep = dynamic.getReplacementBaseType();
return rep == null ? null : getIsfObject(rep, monitor);
- }
- else if (dt instanceof TypeDef typedef) {
+ } else if (dt instanceof TypeDef typedef) {
return getObjectTypeDef(typedef, monitor);
- }
- else if (dt instanceof Composite composite) {
+ } else if (dt instanceof Composite composite) {
return new IsfComposite(composite, this, monitor);
- }
- else if (dt instanceof Enum enumm) {
+ } else if (dt instanceof Enum enumm) {
return new IsfEnum(enumm);
- }
- else if (dt instanceof BuiltInDataType builtin) {
+ } else if (dt instanceof BuiltInDataType builtin) {
return new IsfBuiltIn(builtin);
- }
- else if (dt instanceof BitFieldDataType) {
+ } else if (dt instanceof BitFieldDataType) {
// skip - not hit
- }
- else if (dt instanceof FunctionDefinition) { ///FAIL
+ } else if (dt instanceof FunctionDefinition) { /// FAIL
// skip - not hit
- }
- else if (dt.equals(DataType.DEFAULT)) {
+ } else if (dt.equals(DataType.DEFAULT)) {
// skip - not hit
- }
- else {
+ } else {
Msg.warn(this, "Unable to write datatype. Type unrecognized: " + dt.getClass());
}
return null;
}
- private IsfObject resolve(DataType dt) {
+ public IsfObject resolve(DataType dt) {
if (resolved.containsKey(dt)) {
return resolved.get(dt);
}
- DataType resolvedType = resolvedTypeMap.get(dt.getName());
+ DataType resolvedType = resolvedTypeMap.get(dt.getPathName());
if (resolvedType != null) {
if (resolvedType.isEquivalent(dt)) {
return resolved.get(dt); // skip equivalent type with same name as a resolved type
@@ -425,31 +412,32 @@ public class IsfDataTypeWriter {
}
}
}
- Msg.warn(this, "WARNING! conflicting data type names: " + dt.getPathName() +
- " - " + resolvedType.getPathName());
+ Msg.warn(this,
+ "WARNING! conflicting data type names: " + dt.getPathName() + " - " + resolvedType.getPathName());
return resolved.get(dt);
}
- resolvedTypeMap.put(dt.getName(), dt);
+ resolvedTypeMap.put(dt.getPathName(), dt);
return null;
}
private void clearResolve(String typedefName, DataType baseType) {
if (baseType instanceof Composite || baseType instanceof Enum) {
// auto-typedef generated with composite and enum
- if (typedefName.equals(baseType.getName())) {
+ if (typedefName.equals(baseType.getPathName())) {
resolvedTypeMap.remove(typedefName);
return;
}
}
- // Inherited from DataTypeWriter (logic lost to time):
- // A comment explaining the special 'P' case would be helpful!! Smells like fish.
+ // Inherited from DataTypeWriter (logic lost to time):
+ // A comment explaining the special 'P' case would be helpful!! Smells like
+ // fish.
else if (baseType instanceof Pointer && typedefName.startsWith("P")) {
DataType dt = ((Pointer) baseType).getDataType();
if (dt instanceof TypeDef) {
dt = ((TypeDef) dt).getBaseDataType();
}
- if (dt instanceof Composite && dt.getName().equals(typedefName.substring(1))) {
+ if (dt instanceof Composite && dt.getPathName().equals(typedefName.substring(1))) {
// auto-pointer-typedef generated with composite
resolvedTypeMap.remove(typedefName);
return;
@@ -469,13 +457,11 @@ public class IsfDataTypeWriter {
int elementLen = replacementBaseType.getLength();
if (elementLen > 0) {
int elementCnt = (component.getLength() + elementLen - 1) / elementLen;
- return new IsfDynamicComponent(dynamic, type, elementCnt);
+ return newIsfDynamicComponent(dynamic, type, elementCnt);
}
- Msg.error(this,
- dynamic.getClass().getSimpleName() +
- " returned bad replacementBaseType: " +
- replacementBaseType.getClass().getSimpleName());
+ Msg.error(this, dynamic.getClass().getSimpleName() + " returned bad replacementBaseType: "
+ + replacementBaseType.getClass().getSimpleName());
}
}
return null;
@@ -492,7 +478,7 @@ public class IsfDataTypeWriter {
return getObjectDataType(dataType, -1);
}
- private IsfObject getObjectDataType(DataType dataType, int componentOffset) {
+ public IsfObject getObjectDataType(DataType dataType, int componentOffset) {
if (dataType == null) {
return new IsfDataTypeNull();
}
@@ -509,9 +495,9 @@ public class IsfDataTypeWriter {
IsfObject baseObject = getObjectDataType(IsfUtilities.getBaseDataType(dataType));
return new IsfDataTypeTypeDef(dataType, baseObject);
}
- if (dataType.getName().contains(".conflict")) {
- if (!deferredKeys.contains(dataType.getName())) {
- deferredKeys.add(dataType.getName());
+ if (dataType.getPathName().contains(".conflict")) {
+ if (!deferredKeys.contains(dataType.getPathName())) {
+ deferredKeys.add(dataType.getPathName());
}
}
return new IsfDataTypeDefault(dataType);
@@ -522,27 +508,21 @@ public class IsfDataTypeWriter {
*
* @throws CancelledException if the action is cancelled by the user
*/
- private IsfObject getObjectTypeDef(TypeDef typeDef, TaskMonitor monitor)
- throws CancelledException {
- //UNVERIFIED
+ protected IsfObject getObjectTypeDef(TypeDef typeDef, TaskMonitor monitor) throws CancelledException {
DataType dataType = typeDef.getDataType();
- String typedefName = typeDef.getDisplayName();
- String dataTypeName = dataType.getDisplayName();
- if (IsfUtilities.isIntegral(typedefName, dataTypeName)) {
- return new IsfTypedefIntegral(typeDef);
- }
+ String typedefName = typeDef.getPathName();
- DataType baseType = typeDef.getBaseDataType();
+ DataType baseType = typeDef.getDataType();
try {
if (baseType instanceof BuiltInDataType builtin) {
- return new IsfTypedefBase(typeDef);
+ return newTypedefBase(typeDef);
}
if (!(baseType instanceof Pointer)) {
- return getIsfObject(dataType, monitor);
+ IsfObject isfObject = getIsfObject(dataType, monitor);
+ return newTypedefUser(typeDef, isfObject);
}
- return new IsfTypedefPointer();
- }
- catch (Exception e) {
+ return newTypedefPointer(typeDef);
+ } catch (Exception e) {
Msg.error(this, "TypeDef error: " + e);
}
clearResolve(typedefName, baseType);
@@ -550,11 +530,7 @@ public class IsfDataTypeWriter {
return null;
}
- public JsonElement getTree(Object obj) {
- return gson.toJsonTree(obj);
- }
-
- public void requestAddress(String key) {
+ public void requestAddress(String key) throws IOException {
if (dtm instanceof ProgramDataTypeManager pgmDtm) {
try {
Address address = pgmDtm.getProgram().getMinAddress().getAddress(key);
@@ -563,9 +539,8 @@ public class IsfDataTypeWriter {
return;
}
requestedAddresses.add(address);
- }
- catch (AddressFormatException e) {
- e.printStackTrace();
+ } catch (AddressFormatException e) {
+ throw new IOException("Bad address format: " + key);
}
}
}
@@ -578,24 +553,6 @@ public class IsfDataTypeWriter {
requestedSymbols.add(symbol);
}
- public void requestType(String path) {
- requestedTypes.add(path);
- DataType dataType = dtm.getDataType(path);
- if (dataType == null) {
- Msg.error(this, path + " not found");
- return;
- }
- requestedDataTypes.add(dataType);
- }
-
- public void requestType(DataType dataType) {
- if (dataType == null) {
- Msg.error(this, dataType + " not found");
- return;
- }
- requestedDataTypes.add(dataType);
- }
-
public JsonWriter getWriter() {
return writer;
}
@@ -605,16 +562,6 @@ public class IsfDataTypeWriter {
return baseWriter.toString();
}
- public void close() {
- try {
- writer.flush();
- writer.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
-
public void setSkipSymbols(boolean val) {
skipSymbols = val;
}
@@ -623,7 +570,28 @@ public class IsfDataTypeWriter {
skipTypes = val;
}
- public void setStrict(boolean val) {
- STRICT = val;
+ public IsfTypedefBase newTypedefBase(TypeDef typeDef) {
+ return new IsfTypedefBase(typeDef);
}
+
+// public IsfTypedefIntegral newTypedefIntegral(TypeDef typeDef) {
+// return new IsfTypedefIntegral(typeDef);
+// }
+
+ public IsfTypedefPointer newTypedefPointer(TypeDef typeDef) {
+ return new IsfTypedefPointer(typeDef);
+ }
+
+ public IsfObject newTypedefUser(TypeDef typeDef, IsfObject object) {
+ return object;
+ }
+
+ public IsfTypedObject newTypedObject(DataType dt, IsfObject type) {
+ return new IsfTypedObject(dt, type);
+ }
+
+ public IsfObject newIsfDynamicComponent(Dynamic dynamic, IsfObject type, int elementCnt) {
+ return new IsfDynamicComponent(dynamic, type, elementCnt);
+ }
+
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDynamicComponent.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDynamicComponent.java
index d25a0afe0e..fcad5d72ce 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDynamicComponent.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfDynamicComponent.java
@@ -17,13 +17,14 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.Dynamic;
-public class IsfDynamicComponent implements IsfObject {
+public class IsfDynamicComponent extends AbstractIsfObject {
public String kind;
public Integer count;
public IsfObject subtype;
public IsfDynamicComponent(Dynamic dynamicType, IsfObject type, int elementCnt) {
+ super(dynamicType);
kind = "array";
subtype = type;
count = elementCnt;
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfEnum.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfEnum.java
index 801a7620bb..0c450a6899 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfEnum.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfEnum.java
@@ -19,13 +19,14 @@ import com.google.gson.JsonObject;
import ghidra.program.model.data.Enum;
-public class IsfEnum implements IsfObject {
+public class IsfEnum extends AbstractIsfObject {
public Integer size;
public String base;
public JsonObject constants = new JsonObject();
public IsfEnum(Enum enumm) {
+ super(enumm);
size = enumm.getLength();
base = "int";
String[] names = enumm.getNames();
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunction.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunction.java
index 4af99c709b..bcd0f2aded 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunction.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunction.java
@@ -15,11 +15,14 @@
*/
package ghidra.program.model.data.ISF;
-public class IsfFunction implements IsfObject {
+import ghidra.program.model.data.FunctionDefinition;
+
+public class IsfFunction extends AbstractIsfObject {
public String kind;
- public IsfFunction() {
+ public IsfFunction(FunctionDefinition def) {
+ super(def);
kind = "function";
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunctionPointer.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunctionPointer.java
index 74183df1a2..8660301a0e 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunctionPointer.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfFunctionPointer.java
@@ -18,14 +18,15 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.FunctionDefinition;
-public class IsfFunctionPointer implements IsfObject {
+public class IsfFunctionPointer extends AbstractIsfObject {
public String kind;
public IsfObject subtype;
public IsfFunctionPointer(FunctionDefinition def, DataType dt) {
+ super(def);
kind = "pointer";
- subtype = new IsfFunction();
+ subtype = new IsfFunction(def);
//TODO?
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfObject.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfObject.java
index fbecf2737c..e6ec8b5638 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfObject.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfObject.java
@@ -17,6 +17,6 @@ package ghidra.program.model.data.ISF;
public interface IsfObject {
- // EMPTY by design
-
+ // EMPTY
+
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfPointer.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfPointer.java
new file mode 100644
index 0000000000..02119eb5d9
--- /dev/null
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfPointer.java
@@ -0,0 +1,32 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.program.model.data.ISF;
+
+import ghidra.program.model.data.Pointer;
+
+public class IsfPointer implements IsfObject {
+
+ public Integer size;
+ public String kind;
+ public String endian;
+
+ public IsfPointer(Pointer ptr) {
+ size = ptr.hasLanguageDependantLength() ? -1 : IsfUtilities.getLength(ptr);
+ kind = "pointer";
+ endian = IsfUtilities.getEndianness(ptr);
+ }
+
+}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfSetting.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfSetting.java
new file mode 100644
index 0000000000..ac41d8a61b
--- /dev/null
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfSetting.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.program.model.data.ISF;
+
+public class IsfSetting implements IsfObject {
+
+ public String name;
+ public String kind;
+ public String value;
+
+ public IsfSetting(String name, Object value) {
+ this.name = name;
+ this.value = value.toString();
+ this.kind = value instanceof String ? "string" : "long";
+ }
+
+}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedObject.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedObject.java
index 9f7ee6c1d4..bd8aa66044 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedObject.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedObject.java
@@ -17,15 +17,16 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.DataType;
-public class IsfTypedObject implements IsfObject {
+public class IsfTypedObject extends AbstractIsfObject {
public String kind;
public Integer size;
public IsfObject type;
public IsfTypedObject(DataType dt, IsfObject typeObj) {
+ super(dt);
kind = IsfUtilities.getKind(dt);
- size = dt.getLength();
+ size = dt.hasLanguageDependantLength() ? -1 : dt.getLength();
type = typeObj;
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefBase.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefBase.java
index 03fae5baac..6309010245 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefBase.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefBase.java
@@ -18,18 +18,17 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.BuiltInDataType;
import ghidra.program.model.data.TypeDef;
-public class IsfTypedefBase implements IsfObject {
+public class IsfTypedefBase extends AbstractIsfObject {
public Integer size;
public String kind;
- public Boolean signed;
public String endian;
public IsfTypedefBase(TypeDef typeDef) {
+ super(typeDef);
BuiltInDataType builtin = (BuiltInDataType) typeDef.getBaseDataType();
size = typeDef.getLength();
kind = IsfUtilities.getBuiltInKind(builtin);
- signed = IsfUtilities.getSigned(typeDef);
endian = IsfUtilities.getEndianness(typeDef);
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefIntegral.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefIntegral.java
index 8ebdb46d77..a63107ca1f 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefIntegral.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefIntegral.java
@@ -17,11 +17,12 @@ package ghidra.program.model.data.ISF;
import ghidra.program.model.data.TypeDef;
-public class IsfTypedefIntegral implements IsfObject {
+public class IsfTypedefIntegral extends AbstractIsfObject {
public Integer size;
public IsfTypedefIntegral(TypeDef td) {
+ super(td);
size = td.getLength();
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefPointer.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefPointer.java
index 573292042a..6e029fd6ea 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefPointer.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefPointer.java
@@ -15,21 +15,30 @@
*/
package ghidra.program.model.data.ISF;
+import ghidra.program.model.data.Pointer;
import ghidra.program.model.data.PointerDataType;
+import ghidra.program.model.data.TypeDef;
-public class IsfTypedefPointer implements IsfObject {
+public class IsfTypedefPointer extends AbstractIsfObject {
public Integer size;
- public Boolean signed;
public String kind;
public String endian;
+ public IsfObject type;
- public IsfTypedefPointer() {
- PointerDataType ptr = new PointerDataType();
- size = ptr.getLength();
- signed = false; //IsfUtilities.getSigned(ptr);
- kind = IsfUtilities.getBuiltInKind(ptr);
+ public IsfTypedefPointer(TypeDef typeDef) {
+ super(typeDef);
+ Pointer ptr;
+ if (typeDef != null) {
+ ptr = (Pointer) typeDef.getBaseDataType();
+ }
+ else {
+ ptr = new PointerDataType();
+ }
+ size = ptr.hasLanguageDependantLength() ? -1 : ptr.getLength();
+ kind = "typedef";
endian = IsfUtilities.getEndianness(ptr);
+ type = new IsfPointer(ptr);
}
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefUser.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefUser.java
index 84c664d5a5..4c0d73f634 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefUser.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfTypedefUser.java
@@ -15,19 +15,19 @@
*/
package ghidra.program.model.data.ISF;
-import ghidra.program.model.data.DataType;
import ghidra.program.model.data.TypeDef;
-public class IsfTypedefUser implements IsfObject {
+public class IsfTypedefUser extends AbstractIsfObject {
public Integer size;
public String kind;
public IsfObject type;
public IsfTypedefUser(TypeDef typeDef, IsfObject typeObj) {
- DataType baseType = typeDef.getBaseDataType();
+ super(typeDef);
size = typeDef.getLength();
- kind = IsfUtilities.getKind(baseType);
+ kind = "typedef";
+ //kind = IsfUtilities.getKind(baseType);
type = typeObj;
}
diff --git a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfUtilities.java b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfUtilities.java
index 531a5ac070..202bcdd407 100644
--- a/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfUtilities.java
+++ b/Ghidra/Debug/Debugger-isf/src/main/java/ghidra/program/model/data/ISF/IsfUtilities.java
@@ -20,46 +20,6 @@ import ghidra.program.model.data.Enum;
public class IsfUtilities {
- // list of Ghidra built-in type names which correspond to C primitive types
- private static String[] INTEGRAL_TYPES = { "char", "short", "int", "long", "long long",
- "__int64", "float", "double", "long double", "void" };
-
- private static String[] INTEGRAL_MODIFIERS =
- { "signed", "unsigned", "const", "static", "volatile", "mutable", };
-
- public static boolean isIntegral(String typedefName, String basetypeName) {
- for (String type : INTEGRAL_TYPES) {
- if (typedefName.equals(type)) {
- return true;
- }
- }
-
- boolean endsWithIntegralType = false;
- for (String type : INTEGRAL_TYPES) {
- if (typedefName.endsWith(" " + type)) {
- endsWithIntegralType = true;
- break;
- }
- }
- boolean containsIntegralModifier = false;
- for (String modifier : INTEGRAL_MODIFIERS) {
- if (typedefName.indexOf(modifier + " ") >= 0 ||
- typedefName.indexOf(" " + modifier) >= 0) {
- return true;
- }
- }
-
- if (endsWithIntegralType && containsIntegralModifier) {
- return true;
- }
-
- if (typedefName.endsWith(" " + basetypeName)) {
- return containsIntegralModifier;
- }
-
- return false;
- }
-
public static DataType getBaseDataType(DataType dt) {
while (dt != null) {
if (dt instanceof Array) {
@@ -117,7 +77,7 @@ public class IsfUtilities {
return "enum";
}
if (dt instanceof TypeDef) {
- return "base"; //"typedef";
+ return "typedef";
}
if (dt instanceof FunctionDefinition) {
return "function";
@@ -133,7 +93,7 @@ public class IsfUtilities {
public static String getBuiltInKind(BuiltInDataType dt) {
if (dt instanceof AbstractIntegerDataType) {
- return dt.getLength() == 1 ? "char" : "int";
+ return dt.getName();
}
if (dt instanceof AbstractFloatDataType) {
return "float";
@@ -145,7 +105,7 @@ public class IsfUtilities {
return "char"; // "string";
}
if (dt instanceof PointerDataType) {
- return "void"; //"pointer";
+ return "pointer";
}
if (dt instanceof VoidDataType) {
return "void";
@@ -185,11 +145,48 @@ public class IsfUtilities {
return dt.getLength();
}
- public static Boolean getSigned(DataType dt) {
- return dt.getDataOrganization().isSignedChar();
- }
-
public static String getEndianness(DataType dt) {
return dt.getDataOrganization().isBigEndian() ? "big" : "little";
}
+
+// // list of Ghidra built-in type names which correspond to C primitive types
+// private static String[] INTEGRAL_TYPES = { "char", "short", "int", "long", "long long",
+// "__int64", "float", "double", "long double", "void" };
+//
+// private static String[] INTEGRAL_MODIFIERS =
+// { "signed", "unsigned", "const", "static", "volatile", "mutable", };
+//
+// public static boolean isIntegral(String typedefName, String basetypeName) {
+// for (String type : INTEGRAL_TYPES) {
+// if (typedefName.equals(type)) {
+// return true;
+// }
+// }
+//
+// boolean endsWithIntegralType = false;
+// for (String type : INTEGRAL_TYPES) {
+// if (typedefName.endsWith(" " + type)) {
+// endsWithIntegralType = true;
+// break;
+// }
+// }
+// boolean containsIntegralModifier = false;
+// for (String modifier : INTEGRAL_MODIFIERS) {
+// if (typedefName.indexOf(modifier + " ") >= 0 ||
+// typedefName.indexOf(" " + modifier) >= 0) {
+// return true;
+// }
+// }
+//
+// if (endsWithIntegralType && containsIntegralModifier) {
+// return true;
+// }
+//
+// if (typedefName.endsWith(" " + basetypeName)) {
+// return containsIntegralModifier;
+// }
+//
+// return false;
+// }
+
}
diff --git a/Ghidra/Features/Base/certification.manifest b/Ghidra/Features/Base/certification.manifest
index 608491ab2a..9b63f669c3 100644
--- a/Ghidra/Features/Base/certification.manifest
+++ b/Ghidra/Features/Base/certification.manifest
@@ -389,7 +389,6 @@ src/main/help/help/topics/MemoryMapPlugin/images/MoveMemory.png||GHIDRA||||END|
src/main/help/help/topics/MemoryMapPlugin/images/SetImageBaseDialog.png||GHIDRA||||END|
src/main/help/help/topics/MemoryMapPlugin/images/SplitMemoryBlock.png||GHIDRA||||END|
src/main/help/help/topics/Misc/Appendix.htm||GHIDRA||||END|
-src/main/help/help/topics/Misc/Tips.htm||NONE||||END|
src/main/help/help/topics/Misc/Welcome_to_Ghidra_Help.htm||GHIDRA||||END|
src/main/help/help/topics/Navigation/Navigation.htm||GHIDRA||||END|
src/main/help/help/topics/Navigation/images/GoToDialog.png||GHIDRA||||END|
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ExporterPlugin/exporter.htm b/Ghidra/Features/Base/src/main/help/help/topics/ExporterPlugin/exporter.htm
index f6bbdec7bd..2242bc1a0e 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/ExporterPlugin/exporter.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/ExporterPlugin/exporter.htm
@@ -37,6 +37,8 @@
<LI><A href="#binary">Raw Bytes</A></LI>
<LI><A href="#xml">XML Export Format</A></LI>
+
+ <LI><A href="#sarif">SARIF Export Format</A></LI>
</UL>
<H2>Export Action</H2>
@@ -414,6 +416,18 @@
XML Options are identical the <A href=
"help/topics/ImporterPlugin/importer.htm#xml_options">XML Importer Options</A>.</I></P>
</BLOCKQUOTE>
+
+ <H3><A name="sarif"/><A name="Options_SARIF"/>SARIF</H3>
+
+ <BLOCKQUOTE>
+ <P>The SARIF Exporter creates SARIF files that conform to Ghidra's Program DTD. You can
+ re-import files in this format using the <A href=
+ "help/topics/ImporterPlugin/importer.htm">SARIF Importer</A>.</P>
+
+ <P><A name="sarif_options"></A> <I><IMG alt="" border="0" src="help/shared/note.png">The
+ SARIF Options are identical the <A href=
+ "help/topics/ImporterPlugin/importer.htm#sarif_options">SARIF Importer Options</A>.</I></P>
+ </BLOCKQUOTE>
</BLOCKQUOTE>
<P class="relatedtopic">Related Topics:</P>
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm b/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm
index fb439d9c46..7761843a34 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/ImporterPlugin/importer.htm
@@ -51,6 +51,7 @@
<LI>Raw Binary</LI>
<LI>Relocatable Object Module Format (OMF)</LI>
<LI>XML Input Format</LI>
+ <LI>SARIF Input Format</LI>
</UL>
</BLOCKQUOTE>
@@ -680,6 +681,16 @@
Names</A>.</P>
</BLOCKQUOTE>
</BLOCKQUOTE>
+
+ <H3>SARIF Options <A name="sarif_options"><A name="Options_SARIF_Input_Format"/></A></H3>
+
+ <BLOCKQUOTE>
+ <P>The SARIF format is used to load from a SARIF formatted file. The options are simply
+ switches for which types of program information to import and are identical to the options
+ specified above for XML.</P>
+ </BLOCKQUOTE>
+
+
</BLOCKQUOTE>
<H2><A name="Library_Paths"></A>Library Search Path</H2>
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java
index a7c2764abd..2846aa1915 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/exporter/ExporterDialog.java
@@ -64,8 +64,12 @@ import ghidra.util.task.*;
public class ExporterDialog extends DialogComponentProvider implements AddressFactoryService {
private static final String XML_WARNING =
- " Warning: XML is lossy and intended only for transfering data to external tools. " +
- "GZF is the recommended format for saving and sharing program data.";
+ " Warning: XML is lossy and intended only for transfering data to external tools. " +
+ "GZF is the recommended format for saving and sharing program data.";
+
+ private static final String SARIF_WARNING =
+ " Warning: SARIF is lossy and intended only for transfering data to external tools. " +
+ "GZF is the recommended format for saving and sharing program data.";
private static String lastUsedExporterName = GzfExporter.NAME; // default to GZF first time
@@ -394,6 +398,9 @@ public class ExporterDialog extends DialogComponentProvider implements AddressFa
if (getSelectedExporter().getName().contains("XML")) {
setStatusText(XML_WARNING);
}
+ if (getSelectedExporter().getName().contains("SARIF")) {
+ setStatusText(SARIF_WARNING);
+ }
setOkEnabled(true);
}
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java b/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java
index 0239959224..9fc22790f5 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/program/database/code/CodeManagerTest.java
@@ -470,7 +470,7 @@ public class CodeManagerTest extends AbstractGenericTest {
Instruction inst = listing.getInstructionAt(addr(0x1100));
inst.setProperty("Numbers", 12);
- PropertyMap map = listing.getPropertyMap("Numbers");
+ PropertyMap<?> map = listing.getPropertyMap("Numbers");
assertNotNull(map);
inst.setProperty("FavoriteColor", new SaveableColor(Palette.RED));
diff --git a/Ghidra/Features/Sarif/Module.manifest b/Ghidra/Features/Sarif/Module.manifest
new file mode 100644
index 0000000000..1d58d92fad
--- /dev/null
+++ b/Ghidra/Features/Sarif/Module.manifest
@@ -0,0 +1 @@
+MODULE FILE LICENSE: lib/java-sarif-2.1.jar MIT
diff --git a/Ghidra/Features/Sarif/build.gradle b/Ghidra/Features/Sarif/build.gradle
new file mode 100644
index 0000000000..9a3162a450
--- /dev/null
+++ b/Ghidra/Features/Sarif/build.gradle
@@ -0,0 +1,36 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+apply from: "$rootProject.projectDir/gradle/distributableGhidraModule.gradle"
+apply from: "$rootProject.projectDir/gradle/javaProject.gradle"
+apply from: "$rootProject.projectDir/gradle/jacocoProject.gradle"
+apply from: "$rootProject.projectDir/gradle/javaTestProject.gradle"
+apply plugin: 'eclipse'
+
+eclipse.project.name = 'Features Sarif'
+
+dependencies {
+ api project(':Base')
+ api project(':Debugger-isf')
+ //api "javax.activation:activation-1.1.1"
+ api "com.contrastsecurity.sarif:java-sarif-2.1"
+}
+
+test {
+ // temporary to prevent test from running when building
+ // specify a pattern that doesn't match any test files.
+ include "dontruntests"
+}
+
diff --git a/Ghidra/Features/Sarif/certification.manifest b/Ghidra/Features/Sarif/certification.manifest
new file mode 100644
index 0000000000..c54edb4baf
--- /dev/null
+++ b/Ghidra/Features/Sarif/certification.manifest
@@ -0,0 +1,6 @@
+##VERSION: 2.0
+Module.manifest||GHIDRA||||END|
+data/ExtensionPoint.manifest||GHIDRA||||END|
+src/main/help/help/TOC_Source.xml||GHIDRA||||END|
+src/main/help/help/topics/Sarif/SARIF.htm||GHIDRA||||END|
+src/main/java/sarif/DEVNOTES.txt||GHIDRA||||END|
diff --git a/Ghidra/Features/Sarif/data/ExtensionPoint.manifest b/Ghidra/Features/Sarif/data/ExtensionPoint.manifest
new file mode 100644
index 0000000000..10d0b4ff57
--- /dev/null
+++ b/Ghidra/Features/Sarif/data/ExtensionPoint.manifest
@@ -0,0 +1,2 @@
+RunHandler
+ResultHandler
diff --git a/Ghidra/Features/Sarif/src/main/help/help/TOC_Source.xml b/Ghidra/Features/Sarif/src/main/help/help/TOC_Source.xml
new file mode 100644
index 0000000000..5e2d0e0008
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/help/help/TOC_Source.xml
@@ -0,0 +1,56 @@
+<?xml version='1.0' encoding='ISO-8859-1' ?>
+<!--
+
+ This is an XML file intended to be parsed by the Ghidra help system. It is loosely based
+ upon the JavaHelp table of contents document format. The Ghidra help system uses a
+ TOC_Source.xml file to allow a module with help to define how its contents appear in the
+ Ghidra help viewer's table of contents. The main document (in the Base module)
+ defines a basic structure for the
+ Ghidra table of contents system. Other TOC_Source.xml files may use this structure to insert
+ their files directly into this structure (and optionally define a substructure).
+
+
+ In this document, a tag can be either a <tocdef> or a <tocref>. The former is a definition
+ of an XML item that may have a link and may contain other <tocdef> and <tocref> children.
+ <tocdef> items may be referred to in other documents by using a <tocref> tag with the
+ appropriate id attribute value. Using these two tags allows any module to define a place
+ in the table of contents system (<tocdef>), which also provides a place for
+ other TOC_Source.xml files to insert content (<tocref>).
+
+ During the help build time, all TOC_Source.xml files will be parsed and validated to ensure
+ that all <tocref> tags point to valid <tocdef> tags. From these files will be generated
+ <module name>_TOC.xml files, which are table of contents files written in the format
+ desired by the JavaHelp system. Additionally, the generated files will be merged together
+ as they are loaded by the JavaHelp system. In the end, when displaying help in the Ghidra
+ help GUI, there will be one table of contents that has been created from the definitions in
+ all of the modules' TOC_Source.xml files.
+
+
+ Tags and Attributes
+
+ <tocdef>
+ -id - the name of the definition (this must be unique across all TOC_Source.xml files)
+ -text - the display text of the node, as seen in the help GUI
+ -target** - the file to display when the node is clicked in the GUI
+ -sortgroup - this is a string that defines where a given node should appear under a given
+ parent. The string values will be sorted by the JavaHelp system using
+ a javax.text.RulesBasedCollator. If this attribute is not specified, then
+ the text of attribute will be used.
+
+ <tocref>
+ -id - The id of the <tocdef> that this reference points to
+
+ **The URL for the target is relative and should start with 'help/topics'. This text is
+ used by the Ghidra help system to provide a universal starting point for all links so that
+ they can be resolved at runtime, across modules.
+
+
+-->
+
+
+<tocroot>
+ <tocref id="Program Annotation">
+ <tocdef id="SARIF" sortgroup="q" text="SARIF" target="help/topics/Sarif/SARIF.htm" >
+ </tocdef>
+ </tocref>
+</tocroot>
diff --git a/Ghidra/Features/Sarif/src/main/help/help/topics/Sarif/SARIF.htm b/Ghidra/Features/Sarif/src/main/help/help/topics/Sarif/SARIF.htm
new file mode 100644
index 0000000000..da6bed2120
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/help/help/topics/Sarif/SARIF.htm
@@ -0,0 +1,81 @@
+<!DOCTYPE doctype PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">
+
+<HTML>
+ <HEAD>
+ <META name="generator" content=
+ "HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net">
+
+ <TITLE>Static Analysis Results Interchange Format (SARIF)</TITLE>
+ <LINK rel="stylesheet" type="text/css" href="help/shared/DefaultStyle.css">
+ </HEAD>
+
+ <BODY lang="EN-US">
+ <H1><A name="Using_SARIF_Files"></A>Using SARIF Files</H1>
+
+ <P>SARIF is an OASIS standard for exchanging the results of static analysis performed against
+ a target executable. While not a perfect match for exchanging Ghidra program analysis results,
+ it has at least some level of community acceptance and support within the reverse engineering
+ communities at large. SARIF can be created in any number of ways, but, within Ghidra, SARIF
+ creation is done by means of any exporter. As with all of our exporters, a dialog appears when
+ you request an export, with an options panel for selecting which features you would like to export.
+ Currently, we support bookmarks, code, comments, data types, defined data, entry points, equates,
+ external libraries, functions, memory maps, properties, references, registers, relocations,
+ symbols, and program trees. </P>
+
+ <P>SARIF files can be re-ingested in two ways. A matching importer uses the same options to
+ create a new program or add features to an existing one. The process is probably lossy,
+ although efforts have been made to re-create exported programs with some fidelity. Known issues
+ are documented in the DEVNOTES file. The second way to ingest SARIF files is to configure a tool
+ with the SarifPlugin. The plugin provides a single "Read File" toolbar action. After selecting
+ a file, the SARIF results are displayed in table format, with associated ingest actions applicable
+ to a table selection. The features provide by the SARIF importer and exporter all share the action
+ "Add To Program".</P>
+
+ <H1><A name="Custom_SARIF_Handlers"></A>Writing Custom SARIF Handlers</H1>
+
+ <P>While the main SarifProgramResultHandler class handles the majority of program-derived
+ information used on import/export, there are many occasions where you might want to apply the
+ results of some external analysis to a Ghidra program. The "Features Sarif" project contains
+ templates (in the form of abstract classes) for custom handlers: SarifResultHandler.java and
+ SarifRunHandler.java. Both handler types extend ExtensionPoint and are discovered automatically
+ when the a file is read by the SarifPlugin.</P>
+
+ <P>Per-result handlers are used to populate a table displayed to the user and to associate
+ actions with a column in the table. The "getKey" method returns the name for a column, and the
+ "getActionName" a name for the action to be taken. When the user selects that action (in this case,
+ "Commit"), the table provider executes the program task specified by "getTask". In this example,
+ the task grabs the selected rows from the table and, for each row, applies the return type in the
+ column "return_type" to the address in the column "address".</P>
+
+ <P>A more complicated use case might involve the "parse" method, which is called for every result.
+ The default "handle" method takes anything returned by the "parse" method, creates key-value pairs
+ (stored as a map, but...) using "getKey" and the value returned, and adds them to a list of results
+ stored in the data frame. These can be retrieved programmatically later or processed immediately.
+ For example, the SarifPropertyResultHandler looks for results with "AdditionalProperties" labelled
+ either "viewer/table/xxx" or "listing/[comment, highlight, bookmark]". Items in the second category
+ are applied immediately to the current program. Items in the first category are added to the table
+ under column "xxx".</P>
+
+ <P> A third and significantly more complicated example is the ProgramResultHandler, which overrides
+ the "handle" method. This handler converts the set of "AdditionalProperties" into a key-value map.
+ The map is added to a list of results for the data frame, but also converted to a map of lists based
+ on the result's "RuleId". The frame ultimately accrues a map of list of maps, keyed on "RuleId".
+ Its "Add to Program" task takes each per-rule list and applies them to the program using the values
+ stored in the per-result maps.</P>
+
+ <P>A final example, the SarifGraphRunHandler, is a per-run handler and is generally called only once
+ (unless the SARIF files contains multiple runs). This example retrieves a SARIF Graph from the run,
+ converts it to a Ghidra graph object, and displays it. Controls for how and when SARIF graphs are
+ displayed are stored under Edit->Tool Options->Graph->Sarif.</P>
+
+
+ <P class="relatedtopic">Related Topics:</P>
+
+ <UL>
+ <LI><A href="help/topics/ImporterPlugin/importer.htm">Importers</A></LI>
+ <LI><A href="help/topics/ExporterPlugin/importer.htm">Exporters</A></LI>
+ </UL>
+
+ <P>&nbsp;</P>
+ </BODY>
+</HTML>
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/DEVNOTES.txt b/Ghidra/Features/Sarif/src/main/java/sarif/DEVNOTES.txt
new file mode 100644
index 0000000000..24af15197d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/DEVNOTES.txt
@@ -0,0 +1,13 @@
+Random notes:
+
+1. Symbols require a pre- and post-function pass, pre- to guarantee the correct naming of globals
+ including functions and libraries and post- to guarantee namespaces have already been created
+ for locals
+
+Code differences from export/re-import:
+
+1. There may be missing parameter datatypes for parameters in a FunctionDefinition used inside a structure or union.
+
+2. Datatypes like "RTTIBaseDescriptor *32 _((image-base-relative)) *32 _((image-base-relative))" are not handled correctly.
+
+3. Modified/multiple ProgramTrees will not be imported correctly. Appears to be no way to fix this with the current API.
\ No newline at end of file
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/SarifController.java b/Ghidra/Features/Sarif/src/main/java/sarif/SarifController.java
new file mode 100644
index 0000000000..b5b260c2af
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/SarifController.java
@@ -0,0 +1,289 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.awt.Color;
+import java.awt.image.BufferedImage;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.contrastsecurity.sarif.Location;
+import com.contrastsecurity.sarif.LogicalLocation;
+import com.contrastsecurity.sarif.Result;
+import com.contrastsecurity.sarif.SarifSchema210;
+
+import docking.widgets.table.ObjectSelectedListener;
+import ghidra.app.plugin.core.colorizer.ColorizingService;
+import ghidra.app.services.GraphDisplayBroker;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.BookmarkManager;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.Program;
+import ghidra.service.graph.AttributedGraph;
+import ghidra.service.graph.EmptyGraphType;
+import ghidra.service.graph.GraphDisplay;
+import ghidra.service.graph.GraphDisplayOptions;
+import ghidra.util.Msg;
+import ghidra.util.classfinder.ClassSearcher;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.GraphException;
+import resources.ResourceManager;
+import sarif.handlers.SarifResultHandler;
+import sarif.handlers.SarifRunHandler;
+import sarif.managers.ProgramSarifMgr;
+import sarif.model.SarifDataFrame;
+import sarif.view.ImageArtifactDisplay;
+import sarif.view.SarifResultsTableProvider;
+
+/**
+ * Controller for handling interactions between the SARIF log file and Ghidra
+ */
+public class SarifController implements ObjectSelectedListener<Map<String, Object>> {
+
+ private SarifPlugin plugin;
+ private Program program;
+
+ private ColorizingService coloringService;
+ private BookmarkManager bookmarkManager;
+ private ProgramSarifMgr programManager;
+
+ private Set<SarifResultsTableProvider> providers = new HashSet<>();
+ public Set<ImageArtifactDisplay> artifacts = new HashSet<>();
+ public Set<GraphDisplay> graphs = new HashSet<>();
+
+ public Set<SarifResultHandler> getSarifResultHandlers() {
+ Set<SarifResultHandler> set = new HashSet<>();
+ set.addAll(ClassSearcher.getInstances(SarifResultHandler.class));
+ return set;
+ }
+
+ public Set<SarifRunHandler> getSarifRunHandlers() {
+ Set<SarifRunHandler> set = new HashSet<>();
+ set.addAll(ClassSearcher.getInstances(SarifRunHandler.class));
+ return set;
+ }
+
+ public SarifController(Program program, SarifPlugin plugin) {
+ this.program = program;
+ this.plugin = plugin;
+ this.coloringService = plugin.getTool().getService(ColorizingService.class);
+ this.programManager = new ProgramSarifMgr(program);
+ }
+
+ public SarifController(ProgramSarifMgr manager) {
+ this.program = null;
+ this.plugin = null;
+ this.coloringService = null; // plugin.getTool().getService(ColorizingService.class);
+ this.programManager = manager;
+ }
+
+ public void dispose() {
+ Set<SarifResultsTableProvider> copyProviders = new HashSet<>();
+ copyProviders.addAll(providers);
+ for (SarifResultsTableProvider p : copyProviders) {
+ p.dispose();
+ }
+ Set<ImageArtifactDisplay> copyArtifacts = new HashSet<>();
+ copyArtifacts.addAll(artifacts);
+ for (ImageArtifactDisplay a : copyArtifacts) {
+ a.dispose();
+ }
+ for (GraphDisplay g : graphs) {
+ g.close();
+ }
+ }
+
+ public void showTable(boolean makeVisible) {
+ for (SarifResultsTableProvider p : providers) {
+ p.setVisible(makeVisible);
+ }
+ }
+
+ public void showTable(String logName, SarifSchema210 sarif) {
+ SarifDataFrame df = new SarifDataFrame(sarif, this, false);
+ SarifResultsTableProvider provider = new SarifResultsTableProvider(logName, this.plugin, this, df);
+ provider.filterTable.addSelectionListener(this);
+ provider.addToTool();
+ provider.setVisible(true);
+ provider.setTitle(logName);
+ if (!providers.contains(provider)) {
+ providers.add(provider);
+ }
+ }
+
+ public void showImage(String key, BufferedImage img) {
+ if (plugin.displayArtifacts()) {
+ ImageArtifactDisplay display = new ImageArtifactDisplay(plugin.getTool(), key, "Sarif Parse", img);
+ display.setVisible(true);
+ artifacts.add(display);
+ }
+ }
+
+ public void showGraph(AttributedGraph graph) {
+ try {
+ GraphDisplayBroker service = this.plugin.getTool().getService(GraphDisplayBroker.class);
+ boolean append = plugin.appendToGraph();
+ GraphDisplay display = service.getDefaultGraphDisplay(append, null);
+ GraphDisplayOptions graphOptions = new GraphDisplayOptions(new EmptyGraphType());
+ graphOptions.setMaxNodeCount(plugin.getGraphSize());
+
+ if (plugin.displayGraphs()) {
+ display.setGraph(graph, graphOptions, graph.getDescription(), append, null);
+ graphs.add(display);
+ }
+ } catch (GraphException | CancelledException e) {
+ Msg.error(this, "showGraph failed " + e.getMessage());
+ }
+ }
+
+ /**
+ * If a results has "listing/<something>" in a SARIF result, this handles
+ * defining our custom API for those
+ *
+ * @param log
+ * @param result
+ * @param key
+ * @param value
+ */
+ public void handleListingAction(Result result, String key, Object value) {
+ List<Address> addrs = getListingAddresses(result);
+ for (Address addr : addrs) {
+ switch (key) {
+ case "comment":
+ /* @formatter:off
+ * docs/GhidraAPI_javadoc/api/constant-values.html#ghidra.program.model.listing.CodeUnit
+ * EOL_COMMENT 0
+ * PRE_COMMENT 1
+ * POST_COMMENT 2
+ * PLATE_COMMENT 3
+ * REPEATABLE_COMMENT 4
+ * @formatter:on
+ */
+ String comment = (String) value;
+ getProgram().getListing().setComment(addr, CodeUnit.PLATE_COMMENT, comment);
+ break;
+ case "highlight":
+ Color color = Color.decode((String) value);
+ coloringService.setBackgroundColor(addr, addr, color);
+ break;
+ case "bookmark":
+ String bookmark = (String) value;
+ getProgram().getBookmarkManager().setBookmark(addr, "Sarif", result.getRuleId(), bookmark);
+ break;
+ }
+ }
+ }
+
+ public void colorBackground(AddressSetView set, Color color) {
+ coloringService.setBackgroundColor(set, color);
+ }
+
+ public void colorBackground(Address addr, Color color) {
+ coloringService.setBackgroundColor(addr, addr, color);
+ }
+
+ public Address longToAddress(Object lval) {
+ if (lval instanceof Long) {
+ return getProgram().getAddressFactory().getDefaultAddressSpace().getAddress((Long) lval);
+ }
+ return getProgram().getAddressFactory().getDefaultAddressSpace().getAddress((Integer) lval);
+ }
+
+ /**
+ * Get listing addresses associated with a result
+ *
+ * @param result
+ * @return
+ */
+ public List<Address> getListingAddresses(Result result) {
+ List<Address> addrs = new ArrayList<>();
+ if (result.getLocations() != null && result.getLocations().size() > 0) {
+ List<Location> locations = result.getLocations();
+ for (Location loc : locations) {
+ Address addr = locationToAddress(loc);
+ if (addr != null) {
+ addrs.add(addr);
+ }
+ }
+ }
+ return addrs;
+ }
+
+ public Address locationToAddress(Location loc) {
+ if (loc.getPhysicalLocation() != null) {
+ return longToAddress(loc.getPhysicalLocation().getAddress().getAbsoluteAddress());
+ }
+ if (loc.getLogicalLocations() != null) {
+ Set<LogicalLocation> logicalLocations = loc.getLogicalLocations();
+ for (LogicalLocation logLoc : logicalLocations) {
+ switch (logLoc.getKind()) {
+ case "function":
+ String fname = logLoc.getName();
+ for (Function func : getProgram().getFunctionManager().getFunctions(true)) {
+ if (fname.equals(func.getName())) {
+ return func.getEntryPoint();
+ }
+ }
+ break;
+ default:
+ Msg.error(this, "Unknown logical location to handle: " + logLoc.toString());
+ }
+ }
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void objectSelected(Map<String, Object> row) {
+ if (row != null) {
+ if (row.containsKey("CodeFlows")) {
+ for (List<Address> flow : (List<List<Address>>) row.get("CodeFlows")) {
+ this.plugin.makeSelection(flow);
+ }
+ }
+ if (row.containsKey("Graphs")) {
+ for (AttributedGraph graph : (List<AttributedGraph>) row.get("Graphs")) {
+ this.showGraph(graph);
+ }
+ }
+ }
+ }
+
+ public void removeProvider(SarifResultsTableProvider provider) {
+ providers.remove(provider);
+ }
+
+ public ProgramSarifMgr getProgramSarifMgr() {
+ return programManager;
+ }
+
+ public Program getProgram() {
+ return program;
+ }
+
+ public void setProgram(Program program) {
+ this.program = program;
+ this.bookmarkManager = program.getBookmarkManager();
+ bookmarkManager.defineType("Sarif", ResourceManager.loadImage("images/peach_16.png"), Color.pink, 0);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/SarifLoader.java b/Ghidra/Features/Sarif/src/main/java/sarif/SarifLoader.java
new file mode 100644
index 0000000000..6a2ef8cfb1
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/SarifLoader.java
@@ -0,0 +1,350 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import ghidra.app.plugin.core.analysis.AnalysisWorker;
+import ghidra.app.plugin.core.analysis.AutoAnalysisManager;
+import ghidra.app.util.Option;
+import ghidra.app.util.OptionException;
+import ghidra.app.util.bin.ByteProvider;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.app.util.opinion.AbstractProgramLoader;
+import ghidra.app.util.opinion.LoadException;
+import ghidra.app.util.opinion.LoadSpec;
+import ghidra.app.util.opinion.Loaded;
+import ghidra.app.util.opinion.LoaderTier;
+import ghidra.framework.model.DomainObject;
+import ghidra.framework.model.Project;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.lang.CompilerSpec;
+import ghidra.program.model.lang.CompilerSpecDescription;
+import ghidra.program.model.lang.CompilerSpecNotFoundException;
+import ghidra.program.model.lang.Endian;
+import ghidra.program.model.lang.ExternalLanguageCompilerSpecQuery;
+import ghidra.program.model.lang.Language;
+import ghidra.program.model.lang.LanguageCompilerSpecPair;
+import ghidra.program.model.lang.LanguageDescription;
+import ghidra.program.model.lang.LanguageNotFoundException;
+import ghidra.program.model.listing.Program;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.SarifObject;
+import sarif.managers.ProgramInfo;
+import sarif.managers.ProgramSarifMgr;
+
+public class SarifLoader extends AbstractProgramLoader {
+
+ private static final String FILE_EXTENSION = SarifObject.SARIF ? ".sarif" : ".json";
+ public final static String SARIF_SRC_NAME = "SARIF Input Format";
+
+ @Override
+ public LoaderTier getTier() {
+ return LoaderTier.SPECIALIZED_TARGET_LOADER;
+ }
+
+ @Override
+ public int getTierPriority() {
+ return 50;
+ }
+
+ @Override
+ public boolean supportsLoadIntoProgram() {
+ return true;
+ }
+
+ @Override
+ public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
+
+ List<LoadSpec> loadSpecs = new ArrayList<>();
+
+ //
+ // Unusual Code Alert!: the parse() method below uses Processor to
+ // location processors
+ // by name when reading SARIF. The Processor class is not fully
+ // populated until the languages have been loaded.
+ //
+ getLanguageService();
+
+ ParseResult result = parse(provider);
+
+ ProgramInfo info = result.lastInfo;
+ if (info == null) {
+ return loadSpecs;
+ }
+
+ if (info.languageID != null) {// non-external language
+ // got a language ID, good...
+ try {
+ LanguageDescription languageDescription =
+ getLanguageService().getLanguageDescription(info.languageID);
+
+ boolean preferred = false;
+ if (info.compilerSpecID == null) {
+ // no compiler spec ID, try to pick "default" (embedded
+ // magic string!!! BAD)
+ for (CompilerSpecDescription csd : languageDescription.getCompatibleCompilerSpecDescriptions()) {
+ LanguageCompilerSpecPair pair = new LanguageCompilerSpecPair(
+ languageDescription.getLanguageID(), csd.getCompilerSpecID());
+ loadSpecs.add(new LoadSpec(this, 0, pair, preferred));
+ }
+ }
+ else {
+ // test existence; throw exception on failure
+ languageDescription.getCompilerSpecDescriptionByID(info.compilerSpecID);
+ // good, we know exactly what this is (make it preferred)
+ LanguageCompilerSpecPair pair =
+ new LanguageCompilerSpecPair(info.languageID, info.compilerSpecID);
+ preferred = true;
+ loadSpecs.add(new LoadSpec(this, 0, pair, preferred));
+ }
+ }
+ catch (CompilerSpecNotFoundException | LanguageNotFoundException lnfe) {
+ // ignore
+ // should fall into loadSpecs.isEmpty() case below
+ }
+
+ }
+ else if (info.processorName != null) {// external language
+ // no ID, look by processor/possibly endian
+ Integer size = extractSize(info.addressModel);
+ Endian endian = Endian.toEndian(info.endian);
+ ExternalLanguageCompilerSpecQuery broadQuery =
+ new ExternalLanguageCompilerSpecQuery(info.processorName,
+ info.getNormalizedExternalToolName(), endian, size, info.compilerSpecID);
+ List<LanguageCompilerSpecPair> pairs =
+ getLanguageService().getLanguageCompilerSpecPairs(broadQuery);
+
+ if (!pairs.isEmpty()) {
+ boolean preferred = false;
+ if (pairs.size() == 1) {
+ preferred = true;
+ }
+ for (LanguageCompilerSpecPair pair : pairs) {
+ loadSpecs.add(new LoadSpec(this, 0, pair, preferred));
+ }
+ }
+ }
+
+ if (loadSpecs.isEmpty() && provider.getName().endsWith(FILE_EXTENSION)) {
+ // just put 'em all in (give endianess preference)
+ List<LanguageDescription> languageDescriptions =
+ getLanguageService().getLanguageDescriptions(false);
+ for (LanguageDescription languageDescription : languageDescriptions) {
+ Collection<CompilerSpecDescription> compilerSpecDescriptions =
+ languageDescription.getCompatibleCompilerSpecDescriptions();
+ for (CompilerSpecDescription compilerSpecDescription : compilerSpecDescriptions) {
+ LanguageCompilerSpecPair pair =
+ new LanguageCompilerSpecPair(languageDescription.getLanguageID(),
+ compilerSpecDescription.getCompilerSpecID());
+ loadSpecs.add(new LoadSpec(this, 0, pair, false));
+ }
+ }
+ }
+ return loadSpecs;
+ }
+
+ private static Pattern ADDRESS_MODEL_PATTERN = Pattern.compile("(\\d+)-bit");
+
+ private Integer extractSize(String addressModel) {
+ if (addressModel != null) {
+ Matcher matcher = ADDRESS_MODEL_PATTERN.matcher(addressModel);
+ if (matcher.find()) {
+ return Integer.parseInt(matcher.group(1));
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String getPreferredFileName(ByteProvider provider) {
+ String name = provider.getName();
+ if (name.toLowerCase().endsWith(FILE_EXTENSION)) {
+ return name.substring(0, name.length() - FILE_EXTENSION.length());
+ }
+ return name;
+ }
+
+ @Override
+ protected List<Loaded<Program>> loadProgram(ByteProvider provider, String programName,
+ Project project, String programFolderPath, LoadSpec loadSpec, List<Option> options,
+ MessageLog log, Object consumer, TaskMonitor monitor)
+ throws IOException, LoadException, CancelledException {
+
+ //throw new RuntimeException("SARIF importer supports only 'Add To Program'");
+ LanguageCompilerSpecPair pair = loadSpec.getLanguageCompilerSpec();
+ Language importerLanguage = getLanguageService().getLanguage(pair.languageID);
+ CompilerSpec importerCompilerSpec =
+ importerLanguage.getCompilerSpecByID(pair.compilerSpecID);
+
+ ParseResult result = parse(provider);
+
+ Address imageBase = null;
+ if (result.lastInfo.imageBase != null) {
+ imageBase = importerLanguage.getAddressFactory().getAddress(result.lastInfo.imageBase);
+ }
+ Program prog = createProgram(provider, programName, imageBase, getName(), importerLanguage,
+ importerCompilerSpec, consumer);
+ List<Loaded<Program>> loadedList =
+ List.of(new Loaded<>(prog, programName, programFolderPath));
+ boolean success = false;
+ try {
+ success = doImport(result.lastSarifMgr, options, log, prog, monitor, false);
+ if (success) {
+ createDefaultMemoryBlocks(prog, importerLanguage, log);
+ return loadedList;
+ }
+ throw new LoadException("Failed to load");
+ }
+ finally {
+ if (!success) {
+ release(loadedList, consumer);
+ }
+ }
+ }
+
+ @Override
+ protected void loadProgramInto(ByteProvider provider, LoadSpec loadSpec,
+ List<Option> options, MessageLog log, Program prog, TaskMonitor monitor)
+ throws IOException, LoadException, CancelledException {
+ File file = provider.getFile();
+ doImport(new ProgramSarifMgr(prog, file), options, log, prog, monitor, true);
+ }
+
+ private boolean doImportWork(final ProgramSarifMgr mgr, final List<Option> options,
+ final MessageLog log, Program prog, TaskMonitor monitor,
+ final boolean isAddToProgram) throws LoadException {
+ boolean success = true;
+ try {
+ SarifProgramOptions sarifOptions = mgr.getOptions();
+ sarifOptions.setOptions(options);
+ sarifOptions.setAddToProgram(isAddToProgram);
+ mgr.read(prog, monitor);
+ success = true;
+ }
+ catch (Exception e) {
+ String message = "(empty)";
+ if (log != null && !"".equals(log.toString())) {
+ message = log.toString();
+ }
+ Msg.warn(this, "SARIF import exception, log: " + message, e);
+ throw new LoadException(e.getMessage());
+ }
+ return success;
+ }
+
+ private boolean doImport(final ProgramSarifMgr mgr, final List<Option> options,
+ final MessageLog log, Program prog, TaskMonitor monitor, final boolean isAddToProgram)
+ throws IOException {
+
+ if (!AutoAnalysisManager.hasAutoAnalysisManager(prog)) {
+ int txId = prog.startTransaction("SARIF Import");
+ try {
+ return doImportWork(mgr, options, log, prog, monitor, isAddToProgram);
+ }
+ finally {
+ prog.endTransaction(txId, true);
+ }
+ }
+
+ AutoAnalysisManager analysisMgr = AutoAnalysisManager.getAnalysisManager(prog);
+ try {
+ return analysisMgr.scheduleWorker(new AnalysisWorker() {
+
+ @Override
+ public String getWorkerName() {
+ return "SARIF Importer";
+ }
+
+ @Override
+ public boolean analysisWorkerCallback(Program program, Object workerContext,
+ TaskMonitor taskMonitor) throws Exception, CancelledException {
+ return doImportWork(mgr, options, log, program, taskMonitor, isAddToProgram);
+ }
+ }, null, false, monitor);
+
+ }
+ catch (CancelledException e) {
+ return false;
+ }
+ catch (InvocationTargetException e) {
+ Throwable cause = e.getCause();
+ if (cause instanceof IOException) {
+ throw (IOException) cause;
+ }
+ throw new RuntimeException(e);
+ }
+ catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static class ParseResult {
+ final ProgramSarifMgr lastSarifMgr;
+ final ProgramInfo lastInfo;
+
+ ParseResult(ProgramSarifMgr lastSarifMgr, ProgramInfo lastInfo) {
+ this.lastSarifMgr = lastSarifMgr;
+ this.lastInfo = lastInfo;
+ }
+ }
+
+ private ParseResult parse(ByteProvider provider) {
+ try {
+ ProgramSarifMgr lastSarifMgr = new ProgramSarifMgr(provider);
+ ProgramInfo lastInfo = lastSarifMgr.getProgramInfo();
+ return new ParseResult(lastSarifMgr, lastInfo);
+ }
+ catch (Throwable e) {
+ // This can happen during the import process when this loader attempts to load
+ // a non-SARIF file (there really should be 2 methods, a speculative version and
+ // a version that expects no exception)
+ Msg.trace(this, "Unable to parse SARIF for " + provider.getName(), e);
+ return new ParseResult(null, null);
+ }
+ }
+
+ @Override
+ public List<Option> getDefaultOptions(ByteProvider provider, LoadSpec loadSpec,
+ DomainObject domainObject, boolean loadIntoProgram) {
+ return new SarifProgramOptions().getOptions(loadIntoProgram);
+ }
+
+ @Override
+ public String getName() {
+ return SARIF_SRC_NAME;
+ }
+
+ @Override
+ public String validateOptions(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program program) {
+ try {
+ new SarifProgramOptions().setOptions(options);
+ }
+ catch (OptionException e) {
+ return e.getMessage();
+ }
+ return null;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/SarifPlugin.java b/Ghidra/Features/Sarif/src/main/java/sarif/SarifPlugin.java
new file mode 100644
index 0000000000..34e1d8ec4d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/SarifPlugin.java
@@ -0,0 +1,250 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.swing.Icon;
+
+import com.contrastsecurity.sarif.SarifSchema210;
+import com.google.gson.JsonSyntaxException;
+
+import docking.action.builder.ActionBuilder;
+import docking.tool.ToolConstants;
+import docking.widgets.filechooser.GhidraFileChooser;
+import ghidra.MiscellaneousPluginPackage;
+import ghidra.app.events.ProgramActivatedPluginEvent;
+import ghidra.app.events.ProgramClosedPluginEvent;
+import ghidra.app.events.ProgramOpenedPluginEvent;
+import ghidra.app.events.ProgramVisibilityChangePluginEvent;
+import ghidra.app.plugin.PluginCategoryNames;
+import ghidra.app.plugin.ProgramPlugin;
+import ghidra.framework.options.Options;
+import ghidra.framework.options.OptionsChangeListener;
+import ghidra.framework.options.ToolOptions;
+import ghidra.framework.plugintool.PluginEvent;
+import ghidra.framework.plugintool.PluginInfo;
+import ghidra.framework.plugintool.PluginTool;
+import ghidra.framework.plugintool.util.PluginStatus;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.listing.Program;
+import ghidra.util.HelpLocation;
+import ghidra.util.Msg;
+import ghidra.util.bean.opteditor.OptionsVetoException;
+import resources.ResourceManager;
+import sarif.io.SarifGsonIO;
+import sarif.io.SarifIO;
+
+//@formatter:off
+@PluginInfo(
+ status = PluginStatus.RELEASED,
+ packageName = MiscellaneousPluginPackage.NAME,
+ category = PluginCategoryNames.ANALYSIS,
+ shortDescription = "Sarif Plugin.",
+ description = "From sarif parsing to DL modelling"
+)
+//@formatter:on
+
+/**
+ * A {@link ProgramPlugin} for reading in sarif files
+ */
+public class SarifPlugin extends ProgramPlugin implements OptionsChangeListener {
+ public static final String NAME = "Sarif";
+ public static final Icon SARIF_ICON = ResourceManager.loadImage("images/peach_16.png");
+
+ private Map<Program, SarifController> sarifControllers;
+ private SarifIO io;
+
+ public SarifPlugin(PluginTool tool) {
+ super(tool);
+ this.sarifControllers = new HashMap<Program, SarifController>();
+ this.io = new SarifGsonIO();
+ }
+
+ @Override
+ protected void init() {
+ createActions();
+ initializeOptions();
+ }
+
+ public void readFile(File file) {
+ if (file != null) {
+ try {
+ showSarif(file.getName(), io.readSarif(file));
+ } catch (JsonSyntaxException | IOException e) {
+ Msg.showError(this, tool.getActiveWindow(), "File parse error", "Invalid Sarif File");
+ }
+ }
+ }
+
+ @Override
+ public void processEvent(PluginEvent event) {
+ super.processEvent(event);
+ Program eventProgram = null;
+ if (event instanceof ProgramActivatedPluginEvent) {
+ ProgramActivatedPluginEvent ev = (ProgramActivatedPluginEvent) event;
+ eventProgram = ev.getActiveProgram();
+ }
+ else if (event instanceof ProgramOpenedPluginEvent) {
+ ProgramOpenedPluginEvent ev = (ProgramOpenedPluginEvent) event;
+ eventProgram = ev.getProgram();
+ }
+ else if (event instanceof ProgramClosedPluginEvent) {
+ ProgramClosedPluginEvent ev = (ProgramClosedPluginEvent) event;
+ eventProgram = ev.getProgram();
+ }
+ else if (event instanceof ProgramClosedPluginEvent) {
+ ProgramClosedPluginEvent ev = (ProgramClosedPluginEvent) event;
+ eventProgram = ev.getProgram();
+ }
+ else if (event instanceof ProgramVisibilityChangePluginEvent) {
+ ProgramVisibilityChangePluginEvent ev = (ProgramVisibilityChangePluginEvent) event;
+ eventProgram = ev.getProgram();
+ }
+ SarifController controller = sarifControllers.get(eventProgram);
+ if (controller != null) {
+ if (event instanceof ProgramClosedPluginEvent) {
+ controller.dispose();
+ sarifControllers.remove(eventProgram, controller);
+ }
+ else if (event instanceof ProgramVisibilityChangePluginEvent) {
+ ProgramVisibilityChangePluginEvent ev = (ProgramVisibilityChangePluginEvent) event;
+ controller.showTable(ev.isProgramVisible());
+ }
+ else {
+ controller.showTable(true);
+ }
+ }
+ }
+
+
+ /**
+ * Ultimately both selections end up calling this to actually show something on
+ * the Ghidra gui
+ *
+ * @param logName
+ * @param sarif
+ */
+ public void showSarif(String logName, SarifSchema210 sarif) {
+ currentProgram = getCurrentProgram();
+ if (currentProgram != null) {
+ if (!sarifControllers.containsKey(currentProgram)) {
+ SarifController controller = new SarifController(currentProgram, this);
+ sarifControllers.put(currentProgram, controller);
+ }
+ SarifController currentController = sarifControllers.get(currentProgram);
+ if (currentController != null) {
+ currentController.showTable(logName, sarif);
+ return;
+ }
+ }
+ Msg.showError(this, tool.getActiveWindow(), "File parse error", "No current program");
+ }
+
+ public void makeSelection(List<Address> addrs) {
+ AddressSet selection = new AddressSet();
+ for (Address addr : addrs) {
+ selection.add(addr);
+ }
+ this.setSelection(selection);
+ }
+
+ private void createActions() {
+ //@formatter:off
+ new ActionBuilder("Read", getName())
+ .menuPath("Sarif", "Read File")
+ .menuGroup("sarif", "1")
+ .enabledWhen(ctx -> getCurrentProgram() != null)
+ .onAction(e -> {
+ GhidraFileChooser chooser = new GhidraFileChooser(tool.getActiveWindow());
+ this.readFile(chooser.getSelectedFile());
+ })
+ .buildAndInstall(tool);
+ //@formatter:on
+ }
+
+ private void initializeOptions() {
+ ToolOptions options = tool.getOptions(ToolConstants.GRAPH_OPTIONS);
+ options.addOptionsChangeListener(this);
+
+ HelpLocation help = new HelpLocation(getName(), "Options");
+
+ Options sarifOptions = options.getOptions(NAME);
+ registerOptions(sarifOptions, help);
+ loadOptions(sarifOptions);
+ }
+
+ @Override
+ public void optionsChanged(ToolOptions options, String optionName, Object oldValue,
+ Object newValue) throws OptionsVetoException {
+
+ Options sarifOptions = options.getOptions(NAME);
+ loadOptions(sarifOptions);
+ }
+
+ public void registerOptions(Options options, HelpLocation help) {
+
+ options.setOptionsHelpLocation(help);
+
+ options.registerOption("Display Artifacts", displayArtifacts(), help,
+ "Display artifacts by default");
+
+ options.registerOption("Display Graphs", displayGraphs(), help,
+ "Display graphs by default");
+
+ options.registerOption("Max Graph Size", getGraphSize(), help,
+ "Maximum number of nodes per graph");
+
+ options.registerOption("Append Graphs", appendToGraph(), help,
+ "Append to existing graph");
+
+ }
+
+ public void loadOptions(Options options) {
+
+ displayArtifactsByDefault = options.getBoolean("Display Artifacts", displayArtifacts());
+ displayGraphsByDefault = options.getBoolean("Display Graphs", displayGraphs());
+ maxGraphSize = options.getInt("Max Graph Size", getGraphSize());
+ appendToCurrentGraph = options.getBoolean("Append Graphs", appendToGraph());
+
+ }
+
+ private boolean displayGraphsByDefault = false;
+ public boolean displayGraphs() {
+ return displayGraphsByDefault;
+ }
+
+ private boolean displayArtifactsByDefault = false;
+ public boolean displayArtifacts() {
+ return displayArtifactsByDefault;
+ }
+
+ private int maxGraphSize = 1000;
+ public int getGraphSize() {
+ return maxGraphSize;
+ }
+
+ private boolean appendToCurrentGraph = false;
+ public boolean appendToGraph() {
+ return appendToCurrentGraph;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/SarifProgramOptions.java b/Ghidra/Features/Sarif/src/main/java/sarif/SarifProgramOptions.java
new file mode 100644
index 0000000000..d0d4274a9e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/SarifProgramOptions.java
@@ -0,0 +1,611 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.app.util.Option;
+import ghidra.app.util.OptionException;
+
+/**
+ * A class to hold SARIF options.
+ *
+ */
+public class SarifProgramOptions {
+ /**Flag to indicate reading/writing memory blocks*/
+ public final static long OPT_MEMORY_BLOCKS = 0x00000001L;
+ /**Flag to indicate reading/writing memory contents*/
+ public final static long OPT_MEMORY_CONTENTS = 0x00000002L;
+ /**Flag to indicate reading/writing instructions*/
+ public final static long OPT_CODE = 0x00000004L;
+ /**Flag to indicate reading/writing root*/
+ public final static long OPT_DATA = 0x00000008L;
+ /**Flag to indicate reading/writing symbols*/
+ public final static long OPT_SYMBOLS = 0x00000010L;
+ /**Flag to indicate reading/writing equates*/
+ public final static long OPT_EQUATES = 0x00000020L;
+ /**Flag to indicate reading/writing comments*/
+ public final static long OPT_COMMENTS = 0x00000040L;
+ /**Flag to indicate reading/writing properties*/
+ public final static long OPT_PROPERTIES = 0x00000080L;
+ /**Flag to indicate reading/writing trees*/
+ public final static long OPT_TREES = 0x00000100L;
+ /**Flag to indicate reading/writing empty program tree nodes*/
+ public final static long OPT_EMPTY_TREE_NODES = 0x00000200L;
+ /**Flag to indicate reading/writing references*/
+ public final static long OPT_REFERENCES = 0x00000400L;
+ /**Flag to indicate reading/writing functions*/
+ public final static long OPT_FUNCTIONS = 0x00000800L;
+ /**
+ * Used to signify that symbols should be overwritten when
+ * necessary. This value is not being included in
+ * the <code>ALL</code> constant.
+ */
+ public final static long OVERWRITE_SYMBOLS = 0x20000000L;
+
+ /**
+ * Used to signify that references should be overwritten when
+ * necessary. This value is not being included in
+ * the <code>ALL</code> constant.
+ */
+ public final static long OVERWRITE_REFS = 0x40000000L;
+
+ /**
+ * Used to signify that an existing program is being
+ * updated. This value is not being included in
+ * the <code>ALL</code> constant.
+ */
+ public final static long ADD_2_PROG = 0x80000000L;
+
+ private boolean addToProgram = false;
+ private boolean memoryBlocks = true;
+ private boolean memoryContents = true;
+ private boolean overwriteMemoryConflicts = false;
+ private boolean instructions = true;
+ private boolean overwriteDataConflicts = true;
+ private boolean data = true;
+ private boolean symbols = true;
+ private boolean overwriteSymbolConflicts = true;
+ private boolean equates = true;
+ private boolean comments = true;
+ private boolean properties = true;
+ private boolean overwritePropertyConflicts = true;
+ private boolean bookmarks = true;
+ private boolean overwriteBookmarkConflicts = true;
+ private boolean trees = true;
+ private boolean references = true;
+ private boolean overwriteReferenceConflicts = true;
+ private boolean functions = true;
+ private boolean registers = true;
+ private boolean relocationTable = true;
+ private boolean entryPoints = true;
+ private boolean externalLibraries = true;
+
+ /**
+ * Returns an array of importer options representing
+ * the flags in this class.
+ * @param isAddToProgram if true then adding to existing program
+ * @return the array of importer options
+ */
+ public List<Option> getOptions(boolean isAddToProgram) {
+ this.addToProgram = isAddToProgram;
+
+ ArrayList<Option> optionList = new ArrayList<>();
+
+ optionList.add(new Option("Memory Blocks", Boolean.valueOf(isMemoryBlocks())));
+ optionList.add(new Option("Memory Contents", Boolean.valueOf(isMemoryContents())));
+ if (isAddToProgram) {
+ optionList.add(new Option("Overwrite Memory Conflicts",
+ Boolean.valueOf(isOverwriteMemoryConflicts())));
+ }
+ optionList.add(new Option("Instructions", Boolean.valueOf(isInstructions())));
+ optionList.add(new Option("Data", Boolean.valueOf(isData())));
+ if (isAddToProgram) {
+ optionList.add(
+ new Option("Overwrite Data Conflicts", Boolean.valueOf(isOverwriteDataConflicts())));
+ }
+ optionList.add(new Option("Symbols", Boolean.valueOf(isSymbols())));
+ if (isAddToProgram) {
+ optionList.add(new Option("Overwrite Symbol Conflicts",
+ Boolean.valueOf(isOverwriteSymbolConflicts())));
+ }
+ optionList.add(new Option("Equates", Boolean.valueOf(isEquates())));
+ optionList.add(new Option("Comments", Boolean.valueOf(isComments())));
+ optionList.add(new Option("Properties", Boolean.valueOf(isProperties())));
+ if (isAddToProgram) {
+ optionList.add(new Option("Overwrite Property Conflicts",
+ Boolean.valueOf(isOverwritePropertyConflicts())));
+ }
+ optionList.add(new Option("Bookmarks", Boolean.valueOf(isBookmarks())));
+ if (isAddToProgram) {
+ optionList.add(new Option("Overwrite Bookmark Conflicts",
+ Boolean.valueOf(isOverwriteBookmarkConflicts())));
+ }
+ optionList.add(new Option("Trees", Boolean.valueOf(isTrees())));
+ optionList.add(new Option("References", Boolean.valueOf(isReferences())));
+ if (isAddToProgram) {
+ optionList.add(new Option("Overwrite Reference Conflicts",
+ Boolean.valueOf(isOverwriteReferenceConflicts())));
+ }
+ optionList.add(new Option("Functions", Boolean.valueOf(isFunctions())));
+ optionList.add(new Option("Registers", Boolean.valueOf(isRegisters())));
+ optionList.add(new Option("Relocation Table", Boolean.valueOf(isRelocationTable())));
+ optionList.add(new Option("Entry Points", Boolean.valueOf(isEntryPoints())));
+ optionList.add(new Option("External Libraries", Boolean.valueOf(isExternalLibraries())));
+
+ return optionList;
+ }
+
+ /**
+ * Sets the options. This method is not for defining the options, but
+ * rather for setting the values of options. If invalid options
+ * are passed in, then OptionException should be thrown.
+ * @param options the option values for SARIF
+ * @throws OptionException if invalid options are passed in
+ */
+ public void setOptions(List<Option> options) throws OptionException {
+ for (Option option : options) {
+ String optName = option.getName();
+ Object optValue = option.getValue();
+
+ if (!(optValue instanceof Boolean)) {
+ throw new OptionException("Invalid type for option: " + optName);
+ }
+
+ boolean val = ((Boolean) optValue).booleanValue();
+
+ if (optName.equals("Memory Blocks")) {
+ setMemoryBlocks(val);
+ }
+ else if (optName.equals("Memory Contents")) {
+ setMemoryContents(val);
+ }
+ else if (optName.equals("Overwrite Memory Conflicts")) {
+ setOverwriteMemoryConflicts(val);
+ }
+ else if (optName.equals("Instructions")) {
+ setInstructions(val);
+ }
+ else if (optName.equals("Data")) {
+ setData(val);
+ }
+ else if (optName.equals("Overwrite Data Conflicts")) {
+ setOverwriteDataConflicts(val);
+ }
+ else if (optName.equals("Symbols")) {
+ setSymbols(val);
+ }
+ else if (optName.equals("Overwrite Symbol Conflicts")) {
+ setOverwriteSymbolConflicts(val);
+ }
+ else if (optName.equals("Equates")) {
+ setEquates(val);
+ }
+ else if (optName.equals("Comments")) {
+ setComments(val);
+ }
+ else if (optName.equals("Properties")) {
+ setProperties(val);
+ }
+ else if (optName.equals("Overwrite Property Conflicts")) {
+ setOverwritePropertyConflicts(val);
+ }
+ else if (optName.equals("Bookmarks")) {
+ setBookmarks(val);
+ }
+ else if (optName.equals("Overwrite Bookmark Conflicts")) {
+ setOverwriteBookmarkConflicts(val);
+ }
+ else if (optName.equals("Trees")) {
+ setTrees(val);
+ }
+ else if (optName.equals("References")) {
+ setReferences(val);
+ }
+ else if (optName.equals("Overwrite Reference Conflicts")) {
+ setOverwriteReferenceConflicts(val);
+ }
+ else if (optName.equals("Functions")) {
+ setFunctions(val);
+ }
+ else if (optName.equals("Registers")) {
+ setRegisters(val);
+ }
+ else if (optName.equals("Relocation Table")) {
+ setRelocationTable(val);
+ }
+ else if (optName.equals("Entry Points")) {
+ setEntryPoints(val);
+ }
+ else if (optName.equals("External Libraries")) {
+ setExternalLibraries(val);
+ }
+ else {
+ throw new OptionException("Unknown option: " + optName);
+ }
+ }
+ }
+
+ /**
+ * Returns true if importing to an existing program.
+ * Importing to an existing program creates a new
+ * set of potential conflicts. For example, memory block
+ * may collide. When this options is true, additional
+ * options are visible.
+ * @return true if importing to an existing program
+ */
+ public boolean isAddToProgram() {
+ return addToProgram;
+ }
+
+ /**
+ * If true, then instructions should be read/written.
+ * @return true if instructions should be read/written
+ */
+ public boolean isInstructions() {
+ return instructions;
+ }
+
+ /**
+ * If true, then comments should be read/written.
+ * @return true if comments should be read/written
+ */
+ public boolean isComments() {
+ return comments;
+ }
+
+ /**
+ * If true, then root should be read/written.
+ * @return true if root should be read/written
+ */
+ public boolean isData() {
+ return data;
+ }
+
+ /**
+ * If true, then equates should be read/written.
+ * @return true if equates should be read/written
+ */
+ public boolean isEquates() {
+ return equates;
+ }
+
+ /**
+ * If true, then functions should be read/written.
+ * @return true if functions should be read/written
+ */
+ public boolean isFunctions() {
+ return functions;
+ }
+
+ /**
+ * If true, then memory blocks should be read/written.
+ * @return true if memory blocks should be read/written
+ */
+ public boolean isMemoryBlocks() {
+ return memoryBlocks;
+ }
+
+ /**
+ * If true, then memory contents should be read/written.
+ * @return true if memory contents should be read/written
+ */
+ public boolean isMemoryContents() {
+ return memoryContents;
+ }
+
+ /**
+ * If true, then properties should be read/written.
+ * @return true if properties should be read/written
+ */
+ public boolean isProperties() {
+ return properties;
+ }
+
+ /**
+ * If true, then references (memory, stack, external) should be read/written.
+ * @return true if references should be read/written
+ */
+ public boolean isReferences() {
+ return references;
+ }
+
+ /**
+ * If true, then symbols should be read/written.
+ * @return true if symbols should be read/written
+ */
+ public boolean isSymbols() {
+ return symbols;
+ }
+
+ /**
+ * If true, then program trees should be read/written.
+ * @return true if program trees should be read/written
+ */
+ public boolean isTrees() {
+ return trees;
+ }
+
+ /**
+ * Sets instructions to be read/written.
+ * @param b true if instructions should read/written
+ */
+ public void setInstructions(boolean b) {
+ instructions = b;
+ }
+
+ /**
+ * Sets comments to be read/written.
+ * @param b true if comments should read/written
+ */
+ public void setComments(boolean b) {
+ comments = b;
+ }
+
+ /**
+ * Sets root to be read/written.
+ * @param b true if root should read/written
+ */
+ public void setData(boolean b) {
+ data = b;
+ }
+
+ /**
+ * Sets equates to be read/written.
+ * @param b true if equates should read/written
+ */
+ public void setEquates(boolean b) {
+ equates = b;
+ }
+
+ /**
+ * Sets functions to be read/written.
+ * @param b true if functions should read/written
+ */
+ public void setFunctions(boolean b) {
+ functions = b;
+ }
+
+ /**
+ * Sets memory blocks to be read/written.
+ * @param b true if memory blocks should read/written
+ */
+ public void setMemoryBlocks(boolean b) {
+ memoryBlocks = b;
+ }
+
+ /**
+ * Sets memory contents to be read/written.
+ * @param b true if memory contents should read/written
+ */
+ public void setMemoryContents(boolean b) {
+ memoryContents = b;
+ }
+
+ /**
+ * Sets properties to be read/written.
+ * @param b true if properties should read/written
+ */
+ public void setProperties(boolean b) {
+ properties = b;
+ }
+
+ /**
+ * Sets references to be read/written.
+ * @param b true if references should read/written
+ */
+ public void setReferences(boolean b) {
+ references = b;
+ }
+
+ /**
+ * Sets symbols to be read/written.
+ * @param b true if symbols should read/written
+ */
+ public void setSymbols(boolean b) {
+ symbols = b;
+ }
+
+ /**
+ * Sets program trees to be read/written.
+ * @param b true if program trees should read/written
+ */
+ public void setTrees(boolean b) {
+ trees = b;
+ }
+
+ /**
+ * If true, then bookmarks should be read/written.
+ * @return true if bookmarks should be read/written
+ */
+ public boolean isBookmarks() {
+ return bookmarks;
+ }
+
+ /**
+ * If true, then registers should be read/written.
+ * @return true if registers should be read/written
+ */
+ public boolean isRegisters() {
+ return registers;
+ }
+
+ /**
+ * If true, then the relocation table should be read/written.
+ * @return true if the relocation table should be read/written
+ */
+ public boolean isRelocationTable() {
+ return relocationTable;
+ }
+
+ /**
+ * Sets bookmarks to be read/written.
+ * @param b true if bookmarks should read/written
+ */
+ public void setBookmarks(boolean b) {
+ bookmarks = b;
+ }
+
+ /**
+ * Sets registers to be read/written.
+ * @param b true if registers should read/written
+ */
+ public void setRegisters(boolean b) {
+ registers = b;
+ }
+
+ /**
+ * Sets relocation tables to be read/written.
+ * @param b true if relocation table should read/written
+ */
+ public void setRelocationTable(boolean b) {
+ relocationTable = b;
+ }
+
+ /**
+ * If true, then the entry points should be read/written.
+ * @return true if the entry points should be read/written
+ */
+ public boolean isEntryPoints() {
+ return entryPoints;
+ }
+
+ /**
+ * If true, then the external libraries should be read/written.
+ * @return true if the external libraries should be read/written
+ */
+ public boolean isExternalLibraries() {
+ return externalLibraries;
+ }
+
+ /**
+ * Sets entry points to be read/written.
+ * @param b true if entry points should read/written
+ */
+ public void setEntryPoints(boolean b) {
+ entryPoints = b;
+ }
+
+ /**
+ * Sets external libraries to be read/written.
+ * @param b true if external libraries should read/written
+ */
+ public void setExternalLibraries(boolean b) {
+ externalLibraries = b;
+ }
+
+ /**
+ * If true, then property conflicts will be overwritten.
+ * @return true if property conflicts will be overwritten
+ */
+ public boolean isOverwritePropertyConflicts() {
+ return overwritePropertyConflicts;
+ }
+
+ /**
+ * If true, then bookmark conflicts will be overwritten.
+ * @return true if bookmark conflicts will be overwritten
+ */
+ public boolean isOverwriteBookmarkConflicts() {
+ return overwriteBookmarkConflicts;
+ }
+
+ /**
+ * If true, then symbol conflicts will be overwritten.
+ * @return true if symbol conflicts will be overwritten
+ */
+ public boolean isOverwriteSymbolConflicts() {
+ return overwriteSymbolConflicts;
+ }
+
+ /**
+ * If true, then reference conflicts will be overwritten.
+ * @return true if reference conflicts will be overwritten
+ */
+ public boolean isOverwriteReferenceConflicts() {
+ return overwriteReferenceConflicts;
+ }
+
+ /**
+ * If true, then memory conflicts will be overwritten.
+ * @return true if memory conflicts will be overwritten
+ */
+ public boolean isOverwriteMemoryConflicts() {
+ return overwriteMemoryConflicts;
+ }
+
+ /**
+ * If true, then root conflicts will be overwritten.
+ * @return true if root conflicts will be overwritten
+ */
+ public boolean isOverwriteDataConflicts() {
+ return overwriteDataConflicts;
+ }
+
+ /**
+ * Sets bookmark conflicts to always be overwritten.
+ * @param b true if bookmark conflicts should always be overwritten
+ */
+ public void setOverwriteBookmarkConflicts(boolean b) {
+ overwriteBookmarkConflicts = b;
+ }
+
+ /**
+ * Sets memory conflicts to always be overwritten.
+ * @param b true if memory conflicts should always be overwritten
+ */
+ public void setOverwriteMemoryConflicts(boolean b) {
+ overwriteMemoryConflicts = b;
+ }
+
+ /**
+ * Sets root conflicts to always be overwritten.
+ * @param b true if root conflicts should always be overwritten
+ */
+ public void setOverwriteDataConflicts(boolean b) {
+ overwriteDataConflicts = b;
+ }
+
+ /**
+ * Sets property conflicts to always be overwritten.
+ * @param b true if property conflicts should always be overwritten
+ */
+ public void setOverwritePropertyConflicts(boolean b) {
+ overwritePropertyConflicts = b;
+ }
+
+ /**
+ * Sets reference conflicts to always be overwritten.
+ * @param b true if reference conflicts should always be overwritten
+ */
+ public void setOverwriteReferenceConflicts(boolean b) {
+ overwriteReferenceConflicts = b;
+ }
+
+ /**
+ * Sets symbol conflicts to always be overwritten.
+ * @param b true if symbol conflicts should always be overwritten
+ */
+ public void setOverwriteSymbolConflicts(boolean b) {
+ overwriteSymbolConflicts = b;
+ }
+
+ public void setAddToProgram(boolean addToProgram) {
+ this.addToProgram = addToProgram;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java b/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java
new file mode 100644
index 0000000000..bf71814d19
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java
@@ -0,0 +1,72 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.io.ByteArrayInputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.bouncycastle.util.encoders.Base64;
+
+import com.contrastsecurity.sarif.Artifact;
+import com.contrastsecurity.sarif.ArtifactContent;
+import com.contrastsecurity.sarif.ReportingDescriptor;
+import com.contrastsecurity.sarif.ReportingDescriptorReference;
+import com.contrastsecurity.sarif.Run;
+import com.contrastsecurity.sarif.ToolComponent;
+
+public class SarifUtils {
+
+ public static ByteArrayInputStream getArtifactContent(Artifact artifact) {
+ ArtifactContent content = artifact.getContents();
+ String b64 = content.getBinary();
+ byte[] decoded = Base64.decode(b64);
+ return new ByteArrayInputStream(decoded);
+ }
+
+ public static ReportingDescriptor getTaxaValue(ReportingDescriptorReference taxa, ToolComponent taxonomy) {
+ List<ReportingDescriptor> view = new ArrayList<>(taxonomy.getTaxa());
+ return view.get(taxa.getIndex().intValue());
+ }
+
+ public static ToolComponent getTaxonomy(ReportingDescriptorReference taxa, Set<ToolComponent> taxonomies) {
+ Object idx = taxa.getToolComponent().getIndex();
+ if (idx == null) {
+ List<ToolComponent> view = new ArrayList<>(taxonomies);
+ idx= taxa.getIndex();
+ return view.get(idx instanceof Long ? ((Long)idx).intValue() : (Integer) idx);
+ }
+ for (ToolComponent taxonomy : taxonomies) {
+ if (taxonomy.getName().equals(taxa.getToolComponent().getName())) {
+ return taxonomy;
+ }
+ }
+ return null;
+ }
+
+ public static List<String> getTaxonomyNames(Run sarifRun) {
+ List<String> names = new ArrayList<>();
+ Set<ToolComponent> taxonomies = sarifRun.getTaxonomies();
+ if (taxonomies != null) {
+ for (ToolComponent taxonomy : sarifRun.getTaxonomies()) {
+ names.add(taxonomy.getName());
+ }
+ }
+ return names;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/AbstractExtWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/AbstractExtWriter.java
new file mode 100644
index 0000000000..058db7ef0b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/AbstractExtWriter.java
@@ -0,0 +1,35 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import ghidra.program.model.data.ISF.AbstractIsfWriter;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+
+public abstract class AbstractExtWriter extends AbstractIsfWriter {
+
+ public AbstractExtWriter(Writer baseWriter) throws IOException {
+ super(baseWriter);
+ STRICT = false;
+ }
+
+ @Override
+ protected abstract void genRoot(TaskMonitor monitor) throws CancelledException, IOException;
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifExporter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifExporter.java
new file mode 100644
index 0000000000..2e8eed31a0
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifExporter.java
@@ -0,0 +1,92 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import ghidra.app.util.DomainObjectService;
+import ghidra.app.util.Option;
+import ghidra.app.util.OptionException;
+import ghidra.app.util.exporter.Exporter;
+import ghidra.app.util.exporter.ExporterException;
+import ghidra.framework.model.DomainObject;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.util.HelpLocation;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.managers.ProgramSarifMgr;
+
+/**
+ * An implementation of exporter that creates
+ * an SARIF representation of the program.
+ */
+public class SarifExporter extends Exporter {
+ private SarifProgramOptions options = new SarifProgramOptions();
+
+ /**
+ * Constructs a new SARIF exporter.
+ */
+ public SarifExporter() {
+ super("SARIF", "sarif", new HelpLocation("ExporterPlugin", "sarif"));
+ }
+
+ @Override
+ public List<Option> getOptions(DomainObjectService domainObjectService) {
+ if (options == null) {
+ options = new SarifProgramOptions();
+ }
+ return options.getOptions(false);
+ }
+
+ @Override
+ public void setOptions(List<Option> options) throws OptionException {
+ this.options.setOptions(options);
+ }
+
+ @Override
+ public boolean export(File file, DomainObject domainObj, AddressSetView addrSet, TaskMonitor monitor)
+ throws IOException, ExporterException {
+
+ log.clear();
+
+ if (!(domainObj instanceof Program)) {
+ log.appendMsg("Unsupported type: "+domainObj.getClass().getName());
+ return false;
+ }
+ Program program = (Program)domainObj;
+
+ if (addrSet == null) {
+ addrSet = program.getMemory();
+ }
+
+ ProgramSarifMgr mgr = new ProgramSarifMgr(program, file);
+
+ try {
+ log = mgr.write(program, addrSet, monitor, options);
+ }
+ catch (CancelledException e) {
+ throw new ExporterException("User cancelled SARIF export.");
+ }
+
+ options = null;
+
+ return true;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifObject.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifObject.java
new file mode 100644
index 0000000000..808f5bb57a
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifObject.java
@@ -0,0 +1,123 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.address.AddressSpace;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class SarifObject implements IsfObject {
+
+ public static boolean SARIF = true;
+
+ protected JsonObject message;
+ protected String kind;
+ protected String level;
+ protected String ruleId;
+ protected JsonArray locations;
+ protected JsonObject properties;
+
+ protected JsonObject element;
+
+ public SarifObject(String key, String ruleKey, JsonElement element) {
+ if (SARIF) {
+ message = new JsonObject();
+ message.addProperty("text", key);
+ kind = "INFORMATIONAL";
+ level = "NONE";
+ ruleId = ruleKey;
+ properties = new JsonObject();
+ properties.add("additionalProperties", element);
+ } else {
+ this.element = (JsonObject) element;
+ this.element.addProperty("key", key);
+ this.element.addProperty("rule", ruleKey);
+ }
+ }
+
+ public SarifObject(String key, String ruleKey, JsonElement tree, Address min, Address max) {
+ this(key, ruleKey, tree);
+ if (min != null) {
+ writeLocations(min, max);
+ }
+ }
+
+ public SarifObject(String key, String ruleKey, JsonElement tree, AddressSetView body) {
+ this(key, ruleKey, tree);
+ if (body != null) {
+ writeLocations(body);
+ }
+ }
+
+ protected void writeLocations(Address min, Address max) {
+ if (SARIF) {
+ locations = new JsonArray();
+ JsonObject element = new JsonObject();
+ locations.add(element);
+ JsonObject ploc = new JsonObject();
+ element.add("physicalLocation", ploc);
+ JsonObject address = new JsonObject();
+ ploc.add("address", address);
+ address.addProperty("absoluteAddress", min.getOffset());
+ address.addProperty("length", max.subtract(min) + 1);
+ Address minAddress = min;
+ if (minAddress.getAddressSpace().getType() != AddressSpace.TYPE_RAM) {
+ JsonObject artifact = new JsonObject();
+ ploc.add("artifactLocation", artifact);
+ artifact.addProperty("uri", minAddress.toString());
+ }
+ }
+ else {
+ element.addProperty("startAddress", min.toString(true));
+ element.addProperty("stopAddress", max.toString(true));
+ }
+ }
+
+ protected void writeLocations(AddressSetView set) {
+ if (SARIF) {
+ locations = new JsonArray();
+ AddressRangeIterator addressRanges = set.getAddressRanges();
+ while (addressRanges.hasNext()) {
+ JsonObject element = new JsonObject();
+ locations.add(element);
+ AddressRange next = addressRanges.next();
+ JsonObject ploc = new JsonObject();
+ element.add("physicalLocation", ploc);
+ JsonObject address = new JsonObject();
+ ploc.add("address", address);
+ address.addProperty("absoluteAddress", next.getMinAddress().getOffset());
+ address.addProperty("length", next.getLength());
+ Address minAddress = next.getMinAddress();
+ if (minAddress.getAddressSpace().getType() != AddressSpace.TYPE_RAM) {
+ JsonObject artifact = new JsonObject();
+ ploc.add("artifactLocation", artifact);
+ artifact.addProperty("uri", minAddress.toString());
+ }
+ }
+ }
+ else {
+ element.addProperty("startAddress", set.getMinAddress().toString(true));
+ element.addProperty("stopAddress", set.getMaxAddress().toString(true));
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifWriterTask.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifWriterTask.java
new file mode 100644
index 0000000000..703a5e9617
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/SarifWriterTask.java
@@ -0,0 +1,62 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export;
+
+import java.io.IOException;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+
+import ghidra.program.model.data.ISF.AbstractIsfWriter;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.Task;
+import ghidra.util.task.TaskMonitor;
+
+public class SarifWriterTask extends Task {
+
+ protected AbstractIsfWriter writer;
+ protected JsonArray results;
+
+ public SarifWriterTask(String tag, AbstractIsfWriter writer, JsonArray results) {
+ super(tag, true, false, true);
+ this.writer = writer;
+ this.results = results;
+ }
+
+ @Override
+ public void run(TaskMonitor monitor) {
+ try {
+ try {
+ writer.getRootObject(monitor);
+ JsonArray res = writer.getResults();
+ for (JsonElement element : res) {
+ if (monitor.isCancelled()) {
+ break;
+ }
+ results.add(element);
+ }
+ } finally {
+ writer.close();
+ }
+ } catch (CancelledException e) {
+ // user cancelled; ignore
+ } catch (IOException e) {
+ Msg.error("Export Data Types Failed", "Error exporting Data Types: " + e);
+ return;
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/bkmk/ExtBookmark.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/bkmk/ExtBookmark.java
new file mode 100644
index 0000000000..95a84f42ef
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/bkmk/ExtBookmark.java
@@ -0,0 +1,39 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.bkmk;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.Bookmark;
+
+public class ExtBookmark implements IsfObject {
+
+ String name;
+ String comment;
+ String kind;
+
+ public ExtBookmark(Bookmark b) {
+ String category = b.getCategory();
+ String comment = b.getComment();
+ if (category != null && category.length() != 0) {
+ name = category;
+ }
+ if (comment != null && comment.length() != 0) {
+ this.comment = comment;
+ }
+ kind = b.getType().getTypeString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/bkmk/SarifBookmarkWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/bkmk/SarifBookmarkWriter.java
new file mode 100644
index 0000000000..dc8164fde6
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/bkmk/SarifBookmarkWriter.java
@@ -0,0 +1,54 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.bkmk;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.program.model.listing.Bookmark;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.BookmarksSarifMgr;
+
+public class SarifBookmarkWriter extends AbstractExtWriter {
+
+ private List<Bookmark> bookmarks = new ArrayList<>();
+
+ public SarifBookmarkWriter(List<Bookmark> target, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ bookmarks = target;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genBookmarks(monitor);
+ root.add("bookmarks", objects);
+ }
+
+ private void genBookmarks(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(bookmarks.size());
+ for (Bookmark b : bookmarks) {
+ ExtBookmark isf = new ExtBookmark(b);
+ SarifObject sarif = new SarifObject(BookmarksSarifMgr.SUBKEY, BookmarksSarifMgr.KEY, getTree(isf), b.getAddress(), b.getAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/code/ExtCodeBlock.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/code/ExtCodeBlock.java
new file mode 100644
index 0000000000..88702b0358
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/code/ExtCodeBlock.java
@@ -0,0 +1,26 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.code;
+
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class ExtCodeBlock implements IsfObject {
+
+ public ExtCodeBlock(AddressRange range) {
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/code/ExtCodeOverride.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/code/ExtCodeOverride.java
new file mode 100644
index 0000000000..8f404edb33
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/code/ExtCodeOverride.java
@@ -0,0 +1,31 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.code;
+
+import generic.stl.Pair;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.FlowOverride;
+import ghidra.program.model.listing.Instruction;
+
+public class ExtCodeOverride implements IsfObject {
+
+ String kind;
+
+ public ExtCodeOverride(Pair<Instruction, FlowOverride> pair) {
+ kind = pair.second.name();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/code/SarifCodeWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/code/SarifCodeWriter.java
new file mode 100644
index 0000000000..934fda3077
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/code/SarifCodeWriter.java
@@ -0,0 +1,70 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.code;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import generic.stl.Pair;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.listing.FlowOverride;
+import ghidra.program.model.listing.Instruction;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.CodeSarifMgr;
+
+public class SarifCodeWriter extends AbstractExtWriter {
+
+ private List<AddressRange> blocks;
+ private List<Pair<Instruction, FlowOverride>> overrides;
+
+ public SarifCodeWriter(List<AddressRange> target0, List<Pair<Instruction, FlowOverride>> target1, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.blocks = target0;
+ this.overrides = target1;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genCode(monitor);
+ genOverrides(monitor);
+ root.add("code", objects);
+ }
+
+ private void genCode(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(blocks.size());
+ for (AddressRange range : blocks) {
+ ExtCodeBlock isf = new ExtCodeBlock(range);
+ SarifObject sarif = new SarifObject(CodeSarifMgr.SUBKEY, CodeSarifMgr.KEY, getTree(isf), range.getMinAddress(), range.getMaxAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+ private void genOverrides(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(overrides.size());
+ for (Pair<Instruction, FlowOverride> pair : overrides) {
+ Instruction inst = pair.first;
+ ExtCodeOverride isf = new ExtCodeOverride(pair);
+ SarifObject sarif = new SarifObject(CodeSarifMgr.SUBKEY2, CodeSarifMgr.KEY, getTree(isf), inst.getMinAddress(), inst.getMaxAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/comments/ExtComment.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/comments/ExtComment.java
new file mode 100644
index 0000000000..ec197e1ca3
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/comments/ExtComment.java
@@ -0,0 +1,33 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.comments;
+
+import generic.stl.Pair;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class ExtComment implements IsfObject {
+
+ String kind;
+ String value;
+ boolean standard;
+
+ public ExtComment(Pair<String, String> pair, boolean standard) {
+ kind = pair.first;
+ value = pair.second;
+ this.standard = standard;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/comments/SarifCommentWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/comments/SarifCommentWriter.java
new file mode 100644
index 0000000000..8655226cce
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/comments/SarifCommentWriter.java
@@ -0,0 +1,82 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.comments;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import generic.stl.Pair;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.CommentsSarifMgr;
+
+public class SarifCommentWriter extends AbstractExtWriter {
+
+ private List<Pair<CodeUnit, Pair<String, String>>> comments0;
+ private List<Pair<Address, Pair<String, String>>> comments1;
+
+ public SarifCommentWriter(List<Pair<CodeUnit, Pair<String, String>>> target, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.comments0 = target;
+ }
+
+ public SarifCommentWriter(Writer baseWriter, List<Pair<Address, Pair<String, String>>> target) throws IOException {
+ super(baseWriter);
+ this.comments1 = target;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genComments0(monitor);
+ genComments1(monitor);
+ root.add("comments", objects);
+ }
+
+ private void genComments0(TaskMonitor monitor) throws CancelledException, IOException {
+ if (comments0 == null) {
+ return;
+ }
+ monitor.initialize(comments0.size());
+ for (Pair<CodeUnit, Pair<String, String>> pair : comments0) {
+ CodeUnit cu = pair.first;
+ ExtComment isf = new ExtComment(pair.second, true);
+ SarifObject sarif = new SarifObject(CommentsSarifMgr.SUBKEY, CommentsSarifMgr.KEY, getTree(isf), cu.getMinAddress(),
+ cu.getMaxAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+ private void genComments1(TaskMonitor monitor) throws CancelledException, IOException {
+ if (comments1 == null) {
+ return;
+ }
+ monitor.initialize(comments1.size());
+ for (Pair<Address, Pair<String, String>> pair : comments1) {
+ Address addr = pair.first;
+ ExtComment isf = new ExtComment(pair.second, false);
+ SarifObject sarif = new SarifObject(CommentsSarifMgr.SUBKEY, CommentsSarifMgr.KEY, getTree(isf), addr, addr);
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtDataTypeWriterTask.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtDataTypeWriterTask.java
new file mode 100644
index 0000000000..fa9ecde6f1
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtDataTypeWriterTask.java
@@ -0,0 +1,63 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.List;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.ISF.IsfDataTypeWriter;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.Task;
+import ghidra.util.task.TaskMonitor;
+
+public class ExtDataTypeWriterTask extends Task {
+
+ private final DataTypeManager programDataTypeMgr;
+ private final List<DataType> dataTypeList;
+ private final File file;
+
+ public ExtDataTypeWriterTask(DataTypeManager programDataTypeMgr, List<DataType> dataTypeList, File file) {
+ super("Export Data Types", true, false, true);
+ this.programDataTypeMgr = programDataTypeMgr;
+ this.dataTypeList = dataTypeList;
+ this.file = file;
+ }
+
+ @Override
+ public void run(TaskMonitor monitor) {
+ try {
+ monitor.setMessage("Export to " + file.getName() + "...");
+ IsfDataTypeWriter dataTypeWriter = new ExtIsfDataTypeWriter(programDataTypeMgr, dataTypeList, new FileWriter(file));
+
+ try {
+ dataTypeWriter.getRootObject(monitor);
+ //dataTypeWriter.write(object);
+ } finally {
+ dataTypeWriter.close();
+ }
+ } catch (CancelledException e) {
+ // user cancelled; ignore
+ } catch (IOException e) {
+ Msg.error("Export Data Types Failed", "Error exporting Data Types: " + e);
+ return;
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfComponent.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfComponent.java
new file mode 100644
index 0000000000..58f71ddd91
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfComponent.java
@@ -0,0 +1,37 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.BitFieldDataType;
+import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.data.ISF.IsfComponent;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class ExtIsfComponent extends IsfComponent {
+
+ Integer bitOffset;
+ Integer bitSize;
+
+ public ExtIsfComponent(DataTypeComponent component, IsfObject typeObj) {
+ super(component, typeObj);
+ if (component.isBitFieldComponent()) {
+ BitFieldDataType dt = (BitFieldDataType) component.getDataType();
+ bitOffset = dt.getBitOffset();
+ bitSize = dt.getBitSize();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfComposite.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfComposite.java
new file mode 100644
index 0000000000..8d9b3e81c7
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfComposite.java
@@ -0,0 +1,50 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.Composite;
+import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.data.ISF.IsfComponent;
+import ghidra.program.model.data.ISF.IsfComposite;
+import ghidra.program.model.data.ISF.IsfDataTypeWriter;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.util.task.TaskMonitor;
+
+public class ExtIsfComposite extends IsfComposite {
+
+ String packed;
+ Integer explicitPackingValue;
+ String alignment;
+ Integer explicitMinimumAlignment;
+
+ public ExtIsfComposite(Composite composite, IsfDataTypeWriter writer, TaskMonitor monitor) {
+ super(composite, writer, monitor);
+ name = composite.getName();
+ location = composite.getCategoryPath().getPath();
+ packed = Boolean.toString(composite.isPackingEnabled());
+ int epval = composite.getExplicitPackingValue();
+ explicitPackingValue = epval > 0 ? epval : null;
+ alignment = Integer.toHexString(composite.getAlignment());
+ int maval = composite.getExplicitMinimumAlignment();
+ explicitMinimumAlignment = maval > 0 ? maval : null;
+ }
+
+ @Override
+ protected IsfComponent getComponent(DataTypeComponent component, IsfObject type) {
+ return new ExtIsfComponent(component, type);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfDataTypeWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfDataTypeWriter.java
new file mode 100644
index 0000000000..4f94119d6d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfDataTypeWriter.java
@@ -0,0 +1,144 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import ghidra.program.model.data.Array;
+import ghidra.program.model.data.BitFieldDataType;
+import ghidra.program.model.data.BuiltInDataType;
+import ghidra.program.model.data.Composite;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.Dynamic;
+import ghidra.program.model.data.Enum;
+import ghidra.program.model.data.FactoryDataType;
+import ghidra.program.model.data.FunctionDefinition;
+import ghidra.program.model.data.Pointer;
+import ghidra.program.model.data.TypeDef;
+import ghidra.program.model.data.ISF.IsfBuiltIn;
+import ghidra.program.model.data.ISF.IsfDataTypeWriter;
+import ghidra.program.model.data.ISF.IsfEnum;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.data.ISF.IsfTypedObject;
+import ghidra.program.model.data.ISF.IsfTypedefBase;
+import ghidra.program.model.data.ISF.IsfTypedefPointer;
+import ghidra.program.model.data.ISF.IsfUtilities;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+
+public class ExtIsfDataTypeWriter extends IsfDataTypeWriter {
+
+ public ExtIsfDataTypeWriter(DataTypeManager dtm, List<DataType> target, Writer baseWriter) throws IOException {
+ super(dtm, target, baseWriter);
+ STRICT = false;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ super.genRoot(monitor);
+ data.add("functions", functions);
+ }
+
+ /**
+ * Writes the root type as ISF JSON using the underlying writer. For now, ignoring top-level
+ * bit-fields and function defs as unsupported by ISF. Typedefs really deserve their own
+ * category, but again unsupported.
+ *
+ * @param dt the root type to write as ISF JSON
+ * @param monitor the task monitor
+ * @throws IOException if there is an exception writing the output
+ */
+ @Override
+ protected IsfObject getIsfObject(DataType dt, TaskMonitor monitor)
+ throws IOException, CancelledException {
+ if (dt == null) {
+ throw new IOException("Null datatype passed to getIsfObject");
+ }
+ if (dt instanceof FactoryDataType) {
+ Msg.error(this, "Factory root types may not be written - type: " + dt);
+ }
+ if (dt instanceof BitFieldDataType) {
+ Msg.error(this, "BitField data types may not be written - type: " + dt);
+ }
+ if (dt instanceof Pointer || dt instanceof Array) {
+ IsfObject type = getObjectDataType(IsfUtilities.getBaseDataType(dt));
+ IsfObject obj = new ExtIsfTypedObject(dt, type);
+ return obj;
+ }
+
+ dt = dt.clone(dtm); // force resize/repack for target root organization
+
+ IsfObject res = resolve(dt);
+ if (res != null) {
+ return res;
+ }
+
+ if (dt instanceof Dynamic dynamic) {
+ DataType rep = dynamic.getReplacementBaseType();
+ return rep == null ? null : getIsfObject(rep, monitor);
+ }
+ else if (dt instanceof BuiltInDataType builtin) {
+ return new IsfBuiltIn(builtin);
+ }
+ else if (dt instanceof TypeDef typedef) {
+ return getObjectTypeDef(typedef, monitor);
+ }
+ else if (dt instanceof Composite composite) {
+ return new ExtIsfComposite(composite, this, monitor);
+ }
+ else if (dt instanceof Enum enumm) {
+ return new IsfEnum(enumm);
+ }
+ else if (dt instanceof FunctionDefinition funcDef) {
+ return new ExtIsfFunction(funcDef);
+ }
+ else {
+ Msg.warn(this, "Unable to write datatype. Type unrecognized: " + dt.getClass());
+ }
+
+ return null;
+ }
+
+ @Override
+ public IsfTypedefBase newTypedefBase(TypeDef typeDef) {
+ return new ExtIsfTypedefBase(typeDef);
+ }
+
+ @Override
+ public IsfTypedefPointer newTypedefPointer(TypeDef typeDef) {
+ return new ExtIsfTypedefPointer(typeDef);
+ }
+
+ public IsfObject newTypedefUser(TypeDef typeDef, IsfObject object) {
+ return new ExtIsfTypedefUser(typeDef, object);
+ }
+
+ @Override
+ public IsfTypedObject newTypedObject(DataType dt, IsfObject type) {
+ return new ExtIsfTypedObject(dt, type);
+ }
+
+ @Override
+ public IsfObject newIsfDynamicComponent(Dynamic dynamic, IsfObject type, int elementCnt) {
+ return new ExtIsfDynamicComponent(dynamic, type, elementCnt);
+ }
+
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfDynamicComponent.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfDynamicComponent.java
new file mode 100644
index 0000000000..48950a3a51
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfDynamicComponent.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.Dynamic;
+import ghidra.program.model.data.ISF.IsfDynamicComponent;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class ExtIsfDynamicComponent extends IsfDynamicComponent {
+
+ boolean isVariableLength = true;
+
+ public ExtIsfDynamicComponent(Dynamic dt, IsfObject type, int elementCnt) {
+ super(dt, type, elementCnt);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfFunction.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfFunction.java
new file mode 100644
index 0000000000..7906e93bb6
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfFunction.java
@@ -0,0 +1,71 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.FunctionDefinition;
+import ghidra.program.model.data.ParameterDefinition;
+import ghidra.program.model.data.ISF.IsfFunction;
+import ghidra.program.model.data.ISF.IsfUtilities;
+
+public class ExtIsfFunction extends IsfFunction {
+
+ String comment;
+ String callingConventionName;
+ boolean hasVarArgs;
+ boolean hasNoReturn;
+ JsonObject retType;
+ JsonArray params;
+
+ public ExtIsfFunction(FunctionDefinition funcDef) {
+ super(funcDef);
+ comment = funcDef.getDescription();
+ callingConventionName = funcDef.getCallingConventionName();
+ hasVarArgs = funcDef.hasVarArgs();
+ hasNoReturn = funcDef.hasNoReturn();
+
+ retType = new JsonObject();
+ DataType rt = funcDef.getReturnType();
+ if (rt != null && rt != DataType.DEFAULT) {
+ retType.addProperty("name", rt.getName());
+ retType.addProperty("location", rt.getCategoryPath().getPath());
+ retType.addProperty("kind", IsfUtilities.getKind(rt));
+ retType.addProperty("size", rt.getLength());
+ }
+
+ params = new JsonArray();
+ ParameterDefinition[] vars = funcDef.getArguments();
+ for (ParameterDefinition var : vars) {
+ JsonObject param = new JsonObject();
+ params.add(param);
+ DataType dt = var.getDataType();
+ param.addProperty("name", var.getName());
+ param.addProperty("size", var.getLength());
+ param.addProperty("ordinal", var.getOrdinal());
+ param.addProperty("comment", var.getComment());
+ if (dt != null) {
+ param.addProperty("name", dt.getName());
+ param.addProperty("location", dt.getCategoryPath().getPath());
+ param.addProperty("kind", IsfUtilities.getKind(dt));
+ }
+ }
+
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedObject.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedObject.java
new file mode 100644
index 0000000000..dfccbe4b68
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedObject.java
@@ -0,0 +1,58 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.IsfBuiltIn;
+import ghidra.program.model.data.ISF.IsfDataTypeDefault;
+import ghidra.program.model.data.ISF.IsfEnum;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.data.ISF.IsfTypedObject;
+
+public class ExtIsfTypedObject extends IsfTypedObject {
+
+ String typeLocation;
+
+ public ExtIsfTypedObject(DataType dt, IsfObject typeObj) {
+ super(dt, typeObj);
+ if (typeObj instanceof IsfDataTypeDefault) {
+ typeLocation = ((IsfDataTypeDefault) typeObj).location;
+ return;
+ }
+ if (typeObj instanceof ExtIsfComposite) {
+ typeLocation = ((ExtIsfComposite) typeObj).location;
+ }
+ if (typeObj instanceof IsfEnum) {
+ typeLocation = ((IsfEnum) typeObj).location;
+ }
+ if (typeObj instanceof ExtIsfTypedefBase) {
+ typeLocation = ((ExtIsfTypedefBase) typeObj).location;
+ }
+// if (typeObj instanceof IsfTypedefIntegral) {
+// typeLocation = ((IsfTypedefIntegral) typeObj).location;
+// }
+ if (typeObj instanceof ExtIsfTypedefPointer) {
+ typeLocation = ((ExtIsfTypedefPointer) typeObj).location;
+ }
+ if (typeObj instanceof ExtIsfTypedObject) {
+ typeLocation = ((ExtIsfTypedObject) typeObj).location;
+ }
+ if (typeObj instanceof IsfBuiltIn) {
+ typeLocation = ((IsfBuiltIn) typeObj).location;
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefBase.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefBase.java
new file mode 100644
index 0000000000..9adf550da5
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefBase.java
@@ -0,0 +1,38 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.BuiltInDataType;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.TypeDef;
+import ghidra.program.model.data.ISF.IsfBuiltIn;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.data.ISF.IsfTypedefBase;
+
+public class ExtIsfTypedefBase extends IsfTypedefBase {
+
+ public IsfObject type;
+
+ public ExtIsfTypedefBase(TypeDef typedef) {
+ super(typedef);
+ DataType bdt = typedef.getDataType();
+ if (bdt instanceof BuiltInDataType) {
+ type = new IsfBuiltIn((BuiltInDataType) bdt);
+ kind = "typedef";
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefPointer.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefPointer.java
new file mode 100644
index 0000000000..872e4d95ac
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefPointer.java
@@ -0,0 +1,40 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.TypeDef;
+import ghidra.program.model.data.ISF.IsfTypedefPointer;
+
+public class ExtIsfTypedefPointer extends IsfTypedefPointer {
+
+ String typeName;
+ String typeLocation;
+ boolean autoNamed;
+ String displayName;
+
+ public ExtIsfTypedefPointer(TypeDef typedef) {
+ super(typedef);
+ if (typedef != null) {
+ DataType base = typedef.getDataType();
+ typeName = base.getName();
+ typeLocation = base.getCategoryPath().getPath();
+ autoNamed = typedef.isAutoNamed();
+ displayName = typedef.getDisplayName();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefUser.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefUser.java
new file mode 100644
index 0000000000..c4a56650d3
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/ExtIsfTypedefUser.java
@@ -0,0 +1,36 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.TypeDef;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.data.ISF.IsfTypedefUser;
+
+public class ExtIsfTypedefUser extends IsfTypedefUser {
+
+ String typeName;
+ String typeLocation;
+
+ public ExtIsfTypedefUser(TypeDef typedef, IsfObject typeObj) {
+ super(typedef, typeObj);
+ DataType baseDataType = typedef.getDataType();
+ kind = "typedef";
+ typeName = baseDataType.getName();
+ typeLocation = baseDataType.getCategoryPath().getPath();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/SarifDataType.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/SarifDataType.java
new file mode 100644
index 0000000000..fe67784c85
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/SarifDataType.java
@@ -0,0 +1,63 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import ghidra.program.model.data.ISF.IsfBuiltIn;
+import ghidra.program.model.data.ISF.IsfComposite;
+import ghidra.program.model.data.ISF.IsfDataTypeWriter;
+import ghidra.program.model.data.ISF.IsfEnum;
+import ghidra.program.model.data.ISF.IsfFunction;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.data.ISF.IsfTypedObject;
+import ghidra.program.model.data.ISF.IsfTypedefBase;
+import ghidra.program.model.data.ISF.IsfTypedefPointer;
+import ghidra.program.model.data.ISF.IsfTypedefUser;
+import sarif.export.SarifObject;
+import sarif.managers.DataTypesSarifMgr;
+
+public class SarifDataType extends SarifObject {
+
+ public SarifDataType(IsfObject obj, IsfDataTypeWriter writer) {
+ super("DataType", DataTypesSarifMgr.KEY, writer.getTree(obj));
+ message.addProperty("text", objToMessage(obj));
+ }
+
+ private String objToMessage(IsfObject obj) {
+ if (obj instanceof IsfComposite) {
+ return ((IsfComposite) obj).kind.equals("struct") ? "DT.Struct" : "DT.Union";
+ }
+ if (obj instanceof IsfEnum) {
+ return "DT.Enum";
+ }
+ if (obj instanceof IsfFunction) {
+ return "DT.Function";
+ }
+ if (obj instanceof IsfTypedefBase ||
+ //obj instanceof IsfTypedefIntegral ||
+ obj instanceof IsfTypedefPointer ||
+ obj instanceof IsfTypedefUser) {
+ return "DT.Typedef";
+ }
+ if (obj instanceof IsfTypedObject) {
+ return "DT.TypedObject";
+ }
+ if (obj instanceof IsfBuiltIn) {
+ return "DT.Builtin";
+ }
+ return "DT.Base";
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/data/SarifDataTypeWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/SarifDataTypeWriter.java
new file mode 100644
index 0000000000..9adaaf0fdd
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/data/SarifDataTypeWriter.java
@@ -0,0 +1,64 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.data;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+
+public class SarifDataTypeWriter extends ExtIsfDataTypeWriter {
+
+ private JsonArray types = new JsonArray();
+
+ public SarifDataTypeWriter(DataTypeManager dtm, List<DataType> target, Writer baseWriter) throws IOException {
+ super(dtm, target, baseWriter);
+ metadata = new JsonObject();
+ baseTypes = types;
+ userTypes = types;
+ enums = types;
+ functions = types;
+ symbols = new JsonObject();
+ }
+
+ @Override
+ protected void addSingletons() {
+ add(baseTypes, "pointer", getTree(new SarifDataType(newTypedefPointer(null), this)));
+ add(baseTypes, "undefined", getTree(new SarifDataType(newTypedefPointer(null), this)));
+ }
+
+ @Override
+ protected JsonObject getObjectForDataType(DataType dt, TaskMonitor monitor)
+ throws IOException, CancelledException {
+ IsfObject isf = new SarifDataType(getIsfObject(dt, monitor), this);
+ JsonObject jobj = (JsonObject) getTree(isf);
+ resolved.put(dt, isf);
+ return jobj;
+ }
+
+ public JsonArray getResults() {
+ return types;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtComment.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtComment.java
new file mode 100644
index 0000000000..4235be079a
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtComment.java
@@ -0,0 +1,31 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.dd;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.Data;
+
+public class ExtComment implements IsfObject {
+
+ int commentType;
+ String comment;
+
+ public ExtComment(Data data, int type) {
+ commentType = type;
+ comment = data.getComment(type);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtCommentSet.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtCommentSet.java
new file mode 100644
index 0000000000..37cb7d93a5
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtCommentSet.java
@@ -0,0 +1,75 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.dd;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.data.ISF.IsfSetting;
+import ghidra.program.model.listing.Data;
+import sarif.managers.CommentsSarifMgr;
+
+public class ExtCommentSet implements IsfObject {
+
+ List<ExtComment> comment;
+ List<IsfSetting> setting;
+ Map<Integer, ExtCommentSet> embedded;
+
+ public ExtCommentSet(Data data) {
+ exportComments(data);
+ int n = data.getNumComponents();
+ if (n > 0) {
+ for (int i = 0; i < n; i++) {
+ Data component = data.getComponent(i);
+ ExtCommentSet cs = new ExtCommentSet(component);
+ if (cs.comment != null || cs.setting != null || cs.embedded != null) {
+ if (embedded == null) {
+ embedded = new HashMap<>();
+ }
+ embedded.put(i, cs);
+ }
+ }
+ }
+ }
+
+ private void exportComments(Data data) {
+ for (int i = 0; i < CommentsSarifMgr.COMMENT_TYPES.length; i++) {
+ int type = CommentsSarifMgr.COMMENT_TYPES[i];
+ String cval = data.getComment(type);
+ if (cval != null) {
+ if (comment == null) {
+ comment = new ArrayList<>();
+ }
+ ExtComment isf = new ExtComment(data, type);
+ comment.add(isf);
+ }
+ }
+ for (String n : data.getNames()) {
+ Object value = data.getValue(n);
+ if (value != null) {
+ if (setting == null) {
+ setting = new ArrayList<>();
+ }
+ IsfSetting isf = new IsfSetting(n, value);
+ setting.add(isf);
+ }
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtData.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtData.java
new file mode 100644
index 0000000000..fdf254b4fd
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/ExtData.java
@@ -0,0 +1,36 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.dd;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfObject;
+import ghidra.program.model.listing.Data;
+
+public class ExtData extends AbstractIsfObject {
+
+ String typeName;
+ String typeLocation;
+ ExtCommentSet nested;
+
+ public ExtData(Data data) {
+ super(data.getDataType());
+ DataType dt = data.getDataType();
+ typeName = dt.getName();
+ typeLocation = dt.getCategoryPath().getPath();
+ nested = new ExtCommentSet(data);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/SarifDataWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/SarifDataWriter.java
new file mode 100644
index 0000000000..ca076105a1
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/dd/SarifDataWriter.java
@@ -0,0 +1,55 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.dd;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.program.model.listing.Data;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.DefinedDataSarifMgr;
+
+public class SarifDataWriter extends AbstractExtWriter {
+
+ private List<Data> definedData = new ArrayList<>();
+
+ public SarifDataWriter(List<Data> target, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.definedData = target;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genData(monitor);
+ root.add("definedData", objects);
+ }
+
+ private void genData(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(definedData.size());
+ for (Data d : definedData) {
+ ExtData isf = new ExtData(d);
+ SarifObject sarif = new SarifObject("DefinedData", DefinedDataSarifMgr.KEY, getTree(isf), d.getMinAddress(), d.getMaxAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ep/ExtEntryPoint.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ep/ExtEntryPoint.java
new file mode 100644
index 0000000000..e125aaff54
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ep/ExtEntryPoint.java
@@ -0,0 +1,26 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ep;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class ExtEntryPoint implements IsfObject {
+
+ public ExtEntryPoint(Address addr) {
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ep/SarifEntryPointWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ep/SarifEntryPointWriter.java
new file mode 100644
index 0000000000..9d967a065b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ep/SarifEntryPointWriter.java
@@ -0,0 +1,54 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ep;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import ghidra.program.model.address.Address;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.ExtEntryPointSarifMgr;
+
+public class SarifEntryPointWriter extends AbstractExtWriter {
+
+ private List<Address> entryPoints;
+
+ public SarifEntryPointWriter(List<Address> request, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.entryPoints = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genCode(monitor);
+ root.add("entryPoints", objects);
+ }
+
+ private void genCode(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(entryPoints.size());
+ for (Address addr : entryPoints) {
+ ExtEntryPoint isf = new ExtEntryPoint(addr);
+ SarifObject sarif = new SarifObject(ExtEntryPointSarifMgr.SUBKEY, ExtEntryPointSarifMgr.KEY, getTree(isf), addr, addr);
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/equates/ExtEquate.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/equates/ExtEquate.java
new file mode 100644
index 0000000000..12132ab261
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/equates/ExtEquate.java
@@ -0,0 +1,31 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.equates;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.symbol.Equate;
+
+public class ExtEquate implements IsfObject {
+
+ String name;
+ long value;
+
+ public ExtEquate(Equate equate) {
+ name = equate.getName();
+ value = equate.getValue();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/equates/SarifEquateWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/equates/SarifEquateWriter.java
new file mode 100644
index 0000000000..989534d888
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/equates/SarifEquateWriter.java
@@ -0,0 +1,54 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.equates;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import ghidra.program.model.symbol.Equate;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.EquatesSarifMgr;
+
+public class SarifEquateWriter extends AbstractExtWriter {
+
+ private List<Equate> equates;
+
+ public SarifEquateWriter(List<Equate> request, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.equates = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genCode(monitor);
+ root.add("equates", objects);
+ }
+
+ private void genCode(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(equates.size());
+ for (Equate equate : equates) {
+ ExtEquate isf = new ExtEquate(equate);
+ SarifObject sarif = new SarifObject(EquatesSarifMgr.SUBKEY, EquatesSarifMgr.KEY, getTree(isf), null);
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/ExtLibrary.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/ExtLibrary.java
new file mode 100644
index 0000000000..1015bb7fce
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/ExtLibrary.java
@@ -0,0 +1,33 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.extlib;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.symbol.SourceType;
+
+public class ExtLibrary implements IsfObject {
+
+ public String name;
+ public String location;
+ public String sourceType;
+
+ public ExtLibrary(String name, String path, SourceType sourceType) {
+ this.name = name;
+ location = path;
+ this.sourceType = sourceType.toString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/ExtLibraryLocation.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/ExtLibraryLocation.java
new file mode 100644
index 0000000000..eca75b3b6c
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/ExtLibraryLocation.java
@@ -0,0 +1,54 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.extlib;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.symbol.ExternalLocation;
+
+public class ExtLibraryLocation implements IsfObject {
+
+ String name;
+ String location;
+ String originalImportedName;
+ String externalAddress;
+ String symbol;
+ boolean isFunction = false;
+ boolean isClass = false;
+ String source;
+
+ public ExtLibraryLocation(ExternalLocation extLoc) {
+ name = extLoc.getLabel();
+ originalImportedName = extLoc.getOriginalImportedName();
+ location = extLoc.getParentNameSpace().getName(true);
+ externalAddress = extLoc.getExternalSpaceAddress().toString();
+ isFunction = extLoc.getFunction() != null;
+ isClass = extLoc.getClass() != null;
+ source = extLoc.getSource().toString();
+ symbol = extLoc.getSymbol().getName();
+ }
+
+// public ExtLibraryLocation(GhidraClass cls, ExternalLocation extLoc) {
+// name = extLoc.getLabel();
+// originalImportedName = extLoc.getOriginalImportedName();
+// location = extLoc.getParentName();
+// //location = cls.getParentNamespace().getName();
+// externalAddress = extLoc.getExternalSpaceAddress().toString();
+// Function f = extLoc.getFunction();
+// isFunction = f != null;
+// source = extLoc.getSource().toString();
+// symbol = extLoc.getSymbol().getName(true);
+// }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifClassesNamespaceWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifClassesNamespaceWriter.java
new file mode 100644
index 0000000000..688691a108
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifClassesNamespaceWriter.java
@@ -0,0 +1,96 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.extlib;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.listing.GhidraClass;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolIterator;
+import ghidra.program.model.symbol.SymbolTable;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.ExternalLibSarifMgr;
+
+public class SarifClassesNamespaceWriter extends AbstractExtWriter {
+
+ private List<GhidraClass> classes = new ArrayList<>();
+ private ExternalManager externalManager;
+ private SymbolTable symbolTable;
+
+ public SarifClassesNamespaceWriter(ExternalManager externalManager, SymbolTable symbolTable, List<GhidraClass> request, Writer baseWriter)
+ throws IOException {
+ super(baseWriter);
+ this.externalManager = externalManager;
+ this.symbolTable = symbolTable;
+ this.classes = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genClasses(monitor);
+ root.add("definedData", objects);
+ }
+
+ private void genClasses(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(classes.size());
+ Iterator<GhidraClass> classNamespaces = symbolTable.getClassNamespaces();
+ while (classNamespaces.hasNext()) {
+ GhidraClass next = classNamespaces.next();
+ walkSymbols(next);
+ monitor.increment();
+ }
+ }
+
+ private void walkSymbols(GhidraClass cls) {
+ String clsName = cls.getName(true);
+ String path = externalManager.getExternalLibraryPath(clsName);
+ if (path == null) {
+ path = "";
+ }
+ ExtLibrary lib = new ExtLibrary(clsName, path, SourceType.DEFAULT);
+ SarifObject sarif = new SarifObject(ExternalLibSarifMgr.SUBKEY0, ExternalLibSarifMgr.KEY, getTree(lib), null);
+ objects.add(getTree(sarif));
+
+ SymbolIterator symbols = symbolTable.getSymbols(cls);
+ while (symbols.hasNext()) {
+ Symbol sym = symbols.next();
+ if (cls.isExternal()) {
+ ExternalLocation loc = externalManager.getExternalLocation(sym);
+ ExtLibraryLocation obj = new ExtLibraryLocation(loc);
+ SarifObject sarif2 = new SarifObject(ExternalLibSarifMgr.SUBKEY1, ExternalLibSarifMgr.KEY, getTree(obj),
+ loc.getAddress(), loc.getAddress());
+ objects.add(getTree(sarif2));
+ }
+ }
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifExternalLibraryWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifExternalLibraryWriter.java
new file mode 100644
index 0000000000..52aa70e5fd
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifExternalLibraryWriter.java
@@ -0,0 +1,79 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.extlib;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalLocationIterator;
+import ghidra.program.model.symbol.ExternalManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.ExternalLibSarifMgr;
+
+public class SarifExternalLibraryWriter extends AbstractExtWriter {
+
+ private List<String> externalNames = new ArrayList<>();
+ private ExternalManager externalManager;
+
+ public SarifExternalLibraryWriter(ExternalManager externalManager, List<String> request, Writer baseWriter)
+ throws IOException {
+ super(baseWriter);
+ this.externalManager = externalManager;
+ this.externalNames = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genLibraries(monitor);
+ root.add("definedData", objects);
+ }
+
+ private void genLibraries(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(externalNames.size());
+ for (String n : externalNames) {
+ String path = externalManager.getExternalLibraryPath(n);
+ if (path == null) {
+ path = "";
+ }
+ ExtLibrary lib = new ExtLibrary(n, path, SourceType.DEFAULT);
+ SarifObject sarif = new SarifObject(ExternalLibSarifMgr.SUBKEY0, ExternalLibSarifMgr.KEY, getTree(lib), null);
+ objects.add(getTree(sarif));
+
+ ExternalLocationIterator externalLocations = externalManager.getExternalLocations(n);
+ while (externalLocations.hasNext()) {
+ ExternalLocation loc = externalLocations.next();
+ ExtLibraryLocation obj = new ExtLibraryLocation(loc);
+ SarifObject sarif2 = new SarifObject(ExternalLibSarifMgr.SUBKEY1, ExternalLibSarifMgr.KEY, getTree(obj), loc.getAddress(), loc.getAddress());
+ objects.add(getTree(sarif2));
+ }
+ monitor.increment();
+ }
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtDataType.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtDataType.java
new file mode 100644
index 0000000000..157521f103
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtDataType.java
@@ -0,0 +1,33 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfObject;
+import ghidra.program.model.data.ISF.IsfUtilities;
+
+public class ExtDataType extends AbstractIsfObject{
+
+ String kind;
+ int size;
+
+ public ExtDataType(DataType dt) {
+ super(dt);
+ kind = IsfUtilities.getKind(dt);
+ size = dt.getLength();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunction.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunction.java
new file mode 100644
index 0000000000..b8b56b6f7c
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunction.java
@@ -0,0 +1,120 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.GlobalNamespace;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.GhidraClass;
+import ghidra.program.model.listing.Parameter;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.util.task.TaskMonitor;
+
+public class ExtFunction implements IsfObject {
+
+ String name;
+ String namespace;
+ boolean namespaceIsClass;
+ String location;
+ String comment;
+ String repeatableComment;
+ String value;
+
+ String callingConvention;
+ String callFixup;
+ String signatureSource;
+ String sourceType;
+
+ boolean hasVarArgs;
+ boolean isInline;
+ boolean hasNoReturn;
+ boolean hasCustomStorage;
+ boolean isStackPurgeSizeValid;
+ boolean isLibrary;
+ boolean isGlobal;
+ boolean isExternal;
+
+ boolean isThunk;
+ String thunkAddress;
+
+ ExtFunctionStack stack;
+ List<ExtFunctionRegVar> regVars = new ArrayList<>();
+
+ ExtFunctionParam ret;
+ List<ExtFunctionParam> params = new ArrayList<>();
+
+ public ExtFunction(Function func, TaskMonitor monitor) {
+ super();
+ name = func.getName(true);
+ location = func.getEntryPoint().toString();
+ comment = func.getComment();
+ repeatableComment = func.getRepeatableComment();
+
+ signatureSource = func.getSignatureSource().toString();
+ SourceType srcType = func.getSymbol().getSource();
+ sourceType = srcType.toString();
+ if (srcType != SourceType.DEFAULT) {
+ name = func.getName();
+ }
+ Namespace ns = func.getParentNamespace();
+ if (!(ns instanceof GlobalNamespace)) {
+ namespace = ns.getName(true);
+ if (ns instanceof GhidraClass) {
+ namespaceIsClass = true;
+ }
+ }
+ if (func.getSignatureSource() != SourceType.DEFAULT) {
+ value = func.getPrototypeString(true, true);
+ }
+ callingConvention = func.getCallingConventionName();
+ callFixup = func.getCallFixup();
+
+ hasVarArgs = func.hasVarArgs();
+ isInline = func.isInline();
+ hasNoReturn = func.hasNoReturn();
+ hasCustomStorage = func.hasCustomVariableStorage();
+ isStackPurgeSizeValid = func.isStackPurgeSizeValid();
+ isLibrary = func.isLibrary();
+ isGlobal = func.isGlobal();
+ isExternal = func.isExternal();
+ isThunk = func.isThunk();
+ if (func.isThunk()) {
+ Address thunkAddr = func.getThunkedFunction(false).getEntryPoint();
+ thunkAddress = thunkAddr.toString();
+ }
+
+ stack = new ExtFunctionStack(func.getStackFrame(), hasCustomStorage);
+ if (func.isStackPurgeSizeValid()) {
+ stack.setPurgeSize(func.getStackPurgeSize());
+ }
+
+ Parameter rp = func.getReturn();
+ ret = new ExtFunctionParam(rp);
+ Parameter[] fnParams = func.getParameters();
+ for (Parameter param : fnParams) {
+ if (param.isRegisterVariable()) {
+ regVars.add(new ExtFunctionRegVar(param));
+ }
+ params.add(new ExtFunctionParam(param));
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionParam.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionParam.java
new file mode 100644
index 0000000000..5be0e66ce6
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionParam.java
@@ -0,0 +1,81 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfObject;
+import ghidra.program.model.lang.Register;
+import ghidra.program.model.listing.Parameter;
+
+public class ExtFunctionParam extends AbstractIsfObject {
+
+ String comment;
+ int ordinal;
+ int size;
+
+ String typeName;
+ String typeLocation;
+ ExtDataType type;
+
+ String formalTypeName;
+ String formalTypeLocation;
+ ExtDataType formalType;
+
+ boolean isAutoParameter;
+ boolean isForcedIndirect;
+
+ List<String> registers;
+ int stackOffset;
+
+ public ExtFunctionParam(Parameter p) {
+ super(p.getDataType());
+ name = p.getName();
+ ordinal = p.getOrdinal();
+ size = p.getLength();
+ comment = p.getComment();
+
+ isAutoParameter = p.isAutoParameter();
+ isForcedIndirect = p.isForcedIndirect();
+
+ DataType dataType = p.getDataType();
+ typeName = dataType.getName();
+ typeLocation = dataType.getCategoryPath().getPath();
+ type = new ExtDataType(dataType);
+
+ dataType = p.getFormalDataType();
+ formalTypeName = dataType.getName();
+ formalTypeLocation = dataType.getCategoryPath().getPath();
+ formalType = new ExtDataType(dataType);
+
+ List<Register> regs = p.getRegisters();
+ if (regs != null) {
+ registers = new ArrayList<>();
+ for (Register r : regs) {
+ registers.add(r.getName());
+ }
+ }
+ try {
+ stackOffset = p.getStackOffset();
+ } catch (UnsupportedOperationException uoe) {
+ stackOffset = -1;
+ }
+
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionRegVar.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionRegVar.java
new file mode 100644
index 0000000000..5e8f48fbb9
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionRegVar.java
@@ -0,0 +1,45 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfObject;
+import ghidra.program.model.listing.Parameter;
+
+public class ExtFunctionRegVar extends AbstractIsfObject {
+
+ String register;
+ String comment;
+ int size;
+
+ String typeName;
+ String typeLocation;
+ ExtDataType type;
+
+ public ExtFunctionRegVar(Parameter var) {
+ super(var.getDataType());
+ name = var.getName();
+ register = var.getRegister().getName();
+ size = var.getLength();
+ comment = var.getComment();
+
+ DataType dataType = var.getDataType();
+ typeName = dataType.getName();
+ typeLocation = dataType.getCategoryPath().getPath();
+ type = new ExtDataType(dataType);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionRet.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionRet.java
new file mode 100644
index 0000000000..d98fdbd71c
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionRet.java
@@ -0,0 +1,33 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfObject;
+import ghidra.program.model.data.ISF.IsfUtilities;
+
+public class ExtFunctionRet extends AbstractIsfObject {
+
+ String kind;
+ int size;
+
+ public ExtFunctionRet(DataType rt) {
+ super(rt);
+ kind = IsfUtilities.getKind(rt);
+ size = rt.getLength();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionStack.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionStack.java
new file mode 100644
index 0000000000..70a87ca6ee
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionStack.java
@@ -0,0 +1,63 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.Parameter;
+import ghidra.program.model.listing.StackFrame;
+import ghidra.program.model.listing.Variable;
+
+public class ExtFunctionStack implements IsfObject {
+
+ int localVarSize;
+ int parameterOffset;
+ int returnAddressOffset;
+ int purgeSize;
+
+ List<ExtFunctionStackVar> stackVars = new ArrayList<>();
+
+ public ExtFunctionStack(StackFrame stackFrame, boolean hasCustomStorage) {
+ localVarSize = stackFrame.getLocalSize();
+ parameterOffset = stackFrame.getParameterOffset();
+ returnAddressOffset = stackFrame.getReturnAddressOffset();
+
+ Variable[] vars = stackFrame.getStackVariables();
+ if (hasCustomStorage) {
+ Arrays.sort(vars, new Comparator<Variable>() {
+ @Override
+ public int compare(Variable o1, Variable o2) {
+ if (o1 instanceof Parameter p1 && o2 instanceof Parameter p2) {
+ return p1.getOrdinal() - p2.getOrdinal();
+ }
+ return o1.getStackOffset() - o2.getStackOffset();
+ }
+ });
+ }
+ for (Variable var : vars) {
+ stackVars.add(new ExtFunctionStackVar(var));
+ }
+ }
+
+ public void setPurgeSize(int purgeSize) {
+ this.purgeSize = purgeSize;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionStackVar.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionStackVar.java
new file mode 100644
index 0000000000..952646a039
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionStackVar.java
@@ -0,0 +1,45 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.ISF.AbstractIsfObject;
+import ghidra.program.model.listing.Variable;
+
+public class ExtFunctionStackVar extends AbstractIsfObject {
+
+ String comment;
+ int size;
+ int offset;
+
+ String typeName;
+ String typeLocation;
+ ExtDataType type;
+
+ public ExtFunctionStackVar(Variable var) {
+ super(var.getDataType());
+ name = var.getName();
+ size = var.getLength();
+ offset = var.getStackOffset();
+ comment = var.getComment();
+
+ DataType dataType = var.getDataType();
+ typeName = dataType.getName();
+ typeLocation = dataType.getCategoryPath().getPath();
+ type = new ExtDataType(dataType);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionWriter.java
new file mode 100644
index 0000000000..f8c1a6ae43
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/ExtFunctionWriter.java
@@ -0,0 +1,51 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import com.google.gson.JsonObject;
+
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+
+public class ExtFunctionWriter {
+
+ protected JsonObject data = new JsonObject();
+
+ public ExtFunctionWriter(DataTypeManager dtm, Writer baseWriter) throws IOException {
+ }
+
+ /**
+ * Exports all root types in the list as ISF JSON.
+ *
+ * @param monitor the task monitor
+ * @return the resultant JSON object
+ * @throws IOException if there is an exception writing the output
+ * @throws CancelledException if the action is cancelled by the user
+ */
+ public JsonObject getRootObject(TaskMonitor monitor) throws IOException, CancelledException {
+ genFunctions(monitor);
+ return data;
+ }
+
+ private void genFunctions(TaskMonitor monitor) {
+ // TODO Auto-generated method stub
+
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/func/SarifFunctionWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/SarifFunctionWriter.java
new file mode 100644
index 0000000000..6542b66bbc
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/func/SarifFunctionWriter.java
@@ -0,0 +1,85 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.func;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.FunctionManager;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.export.symbols.ExtSymbol;
+import sarif.managers.FunctionsSarifMgr;
+import sarif.managers.SymbolTableSarifMgr;
+
+public class SarifFunctionWriter extends AbstractExtWriter {
+
+ private List<Function> requestedFunctions = new ArrayList<>();
+
+ public SarifFunctionWriter(FunctionManager mgr, List<Function> requestedFunctions, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.requestedFunctions = requestedFunctions;
+ }
+
+ public void requestFunction(Function next) {
+ requestedFunctions.add(next);
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genFunctions(monitor);
+ root.add("functions", objects);
+ }
+
+ private void genFunctions(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(requestedFunctions.size());
+ for (Function f : requestedFunctions) {
+ addSymbol(f.getSymbol());
+ ExtFunction isf = new ExtFunction(f, monitor);
+ SarifObject sarif = new SarifObject("Function", FunctionsSarifMgr.KEY, getTree(isf), f.getBody());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+ private void addSymbol(Symbol s) {
+ Namespace pspace = s.getParentNamespace();
+ if (pspace == null) {
+ return;
+ }
+ if (!pspace.isGlobal()) {
+ Symbol p = s.getParentSymbol();
+ addSymbol(p);
+ }
+ ExtSymbol lib = new ExtSymbol(s);
+ SarifObject sarif = new SarifObject("Symbol", SymbolTableSarifMgr.KEY, getTree(lib), s.getAddress(), s.getAddress());
+ objects.add(getTree(sarif));
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/mm/ExtMemoryMap.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/mm/ExtMemoryMap.java
new file mode 100644
index 0000000000..ee05bbfd5e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/mm/ExtMemoryMap.java
@@ -0,0 +1,78 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.mm;
+
+import java.io.IOException;
+
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressSpace;
+import ghidra.program.model.address.OverlayAddressSpace;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.mem.MemoryBlock;
+import ghidra.program.model.mem.MemoryBlockSourceInfo;
+import ghidra.program.model.mem.MemoryBlockType;
+import sarif.managers.MemoryMapBytesFile;
+
+public class ExtMemoryMap implements IsfObject {
+
+ String name;
+ String kind;
+ String overlaySpace;
+ String overlayedSpace;
+ String comment;
+ boolean isVolatile;
+ String type;
+ String location;
+
+ public ExtMemoryMap(AddressRange range, MemoryBlock block, MemoryMapBytesFile bf, boolean write) throws IOException {
+
+ String permissions = "";
+ if (block.isRead()) {
+ permissions += "r";
+ }
+ if (block.isWrite()) {
+ permissions += "w";
+ }
+ if (block.isExecute()) {
+ permissions += "x";
+ }
+
+ name = block.getName();
+ kind = permissions;
+ AddressSpace space = range.getAddressSpace();
+ if (space instanceof OverlayAddressSpace) {
+ OverlayAddressSpace oSpace = (OverlayAddressSpace) space;
+ overlaySpace = oSpace.getName();
+ overlayedSpace = oSpace.getOverlayedSpace().getName();
+ }
+ if (block.getComment() != null) {
+ comment = block.getComment();
+ }
+ if (block.isVolatile()) {
+ isVolatile = true;
+ }
+ type = block.getType().name();
+ if (block.getType() == MemoryBlockType.BIT_MAPPED || block.getType() == MemoryBlockType.BYTE_MAPPED) {
+ // bit mapped blocks can only have one sub-block
+ MemoryBlockSourceInfo info = block.getSourceInfos().get(0);
+ location = info.getMappedRange().get().getMinAddress().toString();
+ } else if (block.isInitialized() && write) {
+ location = bf.getFileName() + ":" + bf.getOffset();
+ bf.writeBytes(range);
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/mm/SarifMemoryMapWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/mm/SarifMemoryMapWriter.java
new file mode 100644
index 0000000000..eb92676bce
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/mm/SarifMemoryMapWriter.java
@@ -0,0 +1,64 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.mm;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import generic.stl.Pair;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.mem.MemoryBlock;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.MemoryMapBytesFile;
+import sarif.managers.MemoryMapSarifMgr;
+
+public class SarifMemoryMapWriter extends AbstractExtWriter {
+
+ private List<Pair<AddressRange, MemoryBlock>> memory;
+ private MemoryMapBytesFile bytesFile;
+ private boolean write;
+
+ public SarifMemoryMapWriter(List<Pair<AddressRange, MemoryBlock>> request, Writer baseWriter,
+ MemoryMapBytesFile bytes, boolean isWriteContents) throws IOException {
+ super(baseWriter);
+ this.memory = request;
+ this.bytesFile = bytes;
+ this.write = isWriteContents;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genMaps(monitor);
+ root.add("memory", objects);
+ }
+
+ private void genMaps(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(memory.size());
+ for (Pair<AddressRange, MemoryBlock> m : memory) {
+ AddressRange range = m.first;
+ ExtMemoryMap isf = new ExtMemoryMap(m.first, m.second, bytesFile, write);
+ SarifObject sarif = new SarifObject(MemoryMapSarifMgr.SUBKEY, MemoryMapSarifMgr.KEY, getTree(isf),
+ range.getMinAddress(), range.getMaxAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/props/ExtProperty.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/props/ExtProperty.java
new file mode 100644
index 0000000000..5aa34c6414
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/props/ExtProperty.java
@@ -0,0 +1,178 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.props;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.io.File;
+import java.util.Date;
+
+import javax.swing.KeyStroke;
+
+import ghidra.framework.options.CustomOption;
+import ghidra.framework.options.OptionType;
+import ghidra.framework.options.Options;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.util.exception.AssertException;
+
+public class ExtProperty implements IsfObject {
+
+ String name;
+ String type;
+ String value;
+
+ public ExtProperty(String name, String type, String value) {
+ this.name = name;
+ this.type = type;
+ this.value = value;
+ }
+
+ public ExtProperty(String name, Options propList) {
+ this.name = name;
+ OptionType optionType = propList.getType(name);
+ switch (optionType) {
+ case INT_TYPE:
+ type = "int";
+ value = Integer.toString(propList.getInt(name, 0));
+ break;
+ case LONG_TYPE:
+ type = "long";
+ value = Long.toString(propList.getLong(name, 0));
+ break;
+ case STRING_TYPE:
+ type = "string";
+ value = propList.getString(name, "");
+ break;
+ case BOOLEAN_TYPE:
+ type = "bool";
+ value = Boolean.toString(propList.getBoolean(name, true));
+ break;
+ case DOUBLE_TYPE:
+ type = "double";
+ value = Double.toString(propList.getDouble(name, 0));
+ break;
+ case FLOAT_TYPE:
+ type = "float";
+ value = Float.toString(propList.getFloat(name, 0f));
+ break;
+ case DATE_TYPE:
+ type = "date";
+ Date date = propList.getDate(name, (Date) null);
+ long time = date == null ? 0 : date.getTime();
+ value = Long.toHexString(time);
+ break;
+ case COLOR_TYPE:
+ type = "color";
+ Color color = propList.getColor(name, null);
+ int rgb = color.getRGB();
+ value = Integer.toHexString(rgb);
+ break;
+ case ENUM_TYPE:
+ type = "enum";
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ Enum enuum = propList.getEnum(name, null);
+ String enumString = OptionType.ENUM_TYPE.convertObjectToString(enuum);
+ value = escapeElementEntities(enumString);
+ break;
+ case FILE_TYPE:
+ type = "file";
+ File file = propList.getFile(name, null);
+ String path = file.getAbsolutePath();
+ value = path;
+ break;
+ case FONT_TYPE:
+ type = "font";
+ Font font = propList.getFont(name, null);
+ enumString = OptionType.FONT_TYPE.convertObjectToString(font);
+ value = escapeElementEntities(enumString);
+ break;
+ case KEYSTROKE_TYPE:
+ type = "keyStroke";
+ KeyStroke keyStroke = propList.getKeyStroke(name, null);
+ enumString = OptionType.KEYSTROKE_TYPE.convertObjectToString(keyStroke);
+ value = escapeElementEntities(enumString);
+ break;
+ case CUSTOM_TYPE:
+ type = "custom";
+ CustomOption custom = propList.getCustomOption(name, null);
+ enumString = OptionType.CUSTOM_TYPE.convertObjectToString(custom);
+ value = escapeElementEntities(enumString);
+ break;
+ case BYTE_ARRAY_TYPE:
+ type = "bytes";
+ byte[] bytes = propList.getByteArray(name, null);
+ enumString = OptionType.BYTE_ARRAY_TYPE.convertObjectToString(bytes);
+ value = escapeElementEntities(enumString);
+ break;
+ case NO_TYPE:
+ break;
+ default:
+ throw new AssertException();
+ }
+ }
+
+ private static final String LESS_THAN = "&lt;";
+ private static final String GREATER_THAN = "&gt;";
+ private static final String APOSTROPHE = "&apos;";
+ private static final String QUOTE = "&quot;";
+ private static final String AMPERSAND = "&amp;";
+
+ /**
+ * Converts any special or reserved characters in the specified SARIF string
+ * into the equivalent Unicode encoding.
+ *
+ * @param sarif the SARIF string
+ * @return the encoded SARIF string
+ */
+ public static String escapeElementEntities(String sarif) {
+ StringBuilder buffer = new StringBuilder();
+ for (int offset = 0; offset < sarif.length();) {
+ int codePoint = sarif.codePointAt(offset);
+ offset += Character.charCount(codePoint);
+
+ if ((codePoint < ' ') && (codePoint != 0x09) && (codePoint != 0x0A) && (codePoint != 0x0D)) {
+ continue;
+ }
+ if (codePoint >= 0x7F) {
+ buffer.append("&#x");
+ buffer.append(Integer.toString(codePoint, 16).toUpperCase());
+ buffer.append(";");
+ continue;
+ }
+ switch (codePoint) {
+ case '<':
+ buffer.append(LESS_THAN);
+ break;
+ case '>':
+ buffer.append(GREATER_THAN);
+ break;
+ case '\'':
+ buffer.append(APOSTROPHE);
+ break;
+ case '"':
+ buffer.append(QUOTE);
+ break;
+ case '&':
+ buffer.append(AMPERSAND);
+ break;
+ default:
+ buffer.appendCodePoint(codePoint);
+ break;
+ }
+ }
+ return buffer.toString();
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/props/SarifPropertyListWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/props/SarifPropertyListWriter.java
new file mode 100644
index 0000000000..f565866fb8
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/props/SarifPropertyListWriter.java
@@ -0,0 +1,72 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.props;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Collections;
+import java.util.List;
+
+import ghidra.framework.options.Options;
+import ghidra.program.model.listing.Program;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.PropertiesSarifMgr;
+
+public class SarifPropertyListWriter extends AbstractExtWriter {
+
+ Program program;
+ List<String> options;
+
+ public SarifPropertyListWriter(Program program, List<String> request, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.program = program;
+ this.options = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genList(monitor);
+ root.add("properties", objects);
+ }
+
+ private void genList(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(options.size());
+ for (String listName : options) {
+ Options propList = program.getOptions(listName);
+ List<String> propNames = propList.getOptionNames();
+ Collections.sort(propNames);
+ for (String name : propNames) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ if (propList.isAlias(name)) { // don't write out properties that are just mirrors of some other property
+ continue;
+ }
+ if (propList.isDefaultValue(name)) { // don't write out default properties.
+ continue;
+ }
+ String keyName = listName + Options.DELIMITER_STRING + name;
+ ExtProperty isf = new ExtProperty(keyName, propList);
+ SarifObject sarif = new SarifObject(PropertiesSarifMgr.SUBKEY, PropertiesSarifMgr.KEY, getTree(isf), null);
+ objects.add(getTree(sarif));
+ }
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/props/SarifPropertyMapWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/props/SarifPropertyMapWriter.java
new file mode 100644
index 0000000000..d040498992
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/props/SarifPropertyMapWriter.java
@@ -0,0 +1,169 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.props;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressIterator;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.util.IntPropertyMap;
+import ghidra.program.model.util.LongPropertyMap;
+import ghidra.program.model.util.ObjectPropertyMap;
+import ghidra.program.model.util.PropertyMap;
+import ghidra.program.model.util.StringPropertyMap;
+import ghidra.program.model.util.VoidPropertyMap;
+import ghidra.util.SaveableColor;
+import ghidra.util.SaveablePoint;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.NoValueException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.PropertiesSarifMgr;
+
+public class SarifPropertyMapWriter extends AbstractExtWriter {
+
+ List<PropertyMap<?>> maps;
+ Program program;
+ AddressSetView set;
+
+ public SarifPropertyMapWriter(List<PropertyMap<?>> request, Program program, AddressSetView set, Writer baseWriter)
+ throws IOException {
+ super(baseWriter);
+ this.maps = request;
+ this.program = program;
+ this.set = set;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genMap(monitor);
+ root.add("properties", objects);
+ }
+
+ private void genMap(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(maps.size());
+ for (PropertyMap<?> map : maps) {
+ if (map instanceof VoidPropertyMap) {
+ genVoidMap((VoidPropertyMap) map, monitor);
+ } else if (map instanceof IntPropertyMap) {
+ genIntMap((IntPropertyMap) map, monitor);
+ } else if (map instanceof LongPropertyMap) {
+ genLongMap((LongPropertyMap) map, monitor);
+ } else if (map instanceof StringPropertyMap) {
+ genStringMap((StringPropertyMap) map, monitor);
+ } else if (map instanceof ObjectPropertyMap) {
+ genObjectMap((ObjectPropertyMap<?>) map, monitor);
+ }
+ }
+ }
+
+ private void genVoidMap(VoidPropertyMap map, TaskMonitor monitor) throws CancelledException {
+ AddressIterator iter = set != null ? map.getPropertyIterator(set) : map.getPropertyIterator();
+ while (iter.hasNext()) {
+ Address addr = iter.next();
+ ExtProperty isf = new ExtProperty(map.getName(), "void", null);
+ SarifObject sarif = new SarifObject(PropertiesSarifMgr.SUBKEY, PropertiesSarifMgr.KEY, getTree(isf), addr,
+ addr);
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+ private void genIntMap(IntPropertyMap map, TaskMonitor monitor) throws CancelledException {
+ AddressIterator iter = set != null ? map.getPropertyIterator(set) : map.getPropertyIterator();
+ while (iter.hasNext()) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ try {
+ Address addr = iter.next();
+ int value = map.getInt(addr);
+ ExtProperty isf = new ExtProperty(map.getName(), "int", Integer.toHexString(value));
+ SarifObject sarif = new SarifObject(PropertiesSarifMgr.SUBKEY, PropertiesSarifMgr.KEY, getTree(isf),
+ addr, addr);
+ objects.add(getTree(sarif));
+ } catch (NoValueException e) {
+ // skip
+ }
+ }
+ }
+
+ private void genLongMap(LongPropertyMap map, TaskMonitor monitor) throws CancelledException {
+ AddressIterator iter = set != null ? map.getPropertyIterator(set) : map.getPropertyIterator();
+ while (iter.hasNext()) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ try {
+ Address addr = iter.next();
+ long value = map.getLong(addr);
+ ExtProperty isf = new ExtProperty(map.getName(), "long", Long.toHexString(value));
+ SarifObject sarif = new SarifObject(PropertiesSarifMgr.SUBKEY, PropertiesSarifMgr.KEY, getTree(isf),
+ addr, addr);
+ objects.add(getTree(sarif));
+ } catch (NoValueException e) {
+ // skip
+ }
+ }
+
+ }
+
+ private void genStringMap(StringPropertyMap map, TaskMonitor monitor) throws CancelledException {
+ AddressIterator iter = set != null ? map.getPropertyIterator(set) : map.getPropertyIterator();
+ while (iter.hasNext()) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ Address addr = iter.next();
+ String value = map.getString(addr);
+ ExtProperty isf = new ExtProperty(map.getName(), "string", value);
+ SarifObject sarif = new SarifObject(PropertiesSarifMgr.SUBKEY, PropertiesSarifMgr.KEY, getTree(isf), addr,
+ addr);
+ objects.add(getTree(sarif));
+ }
+ }
+
+
+ private void genObjectMap(ObjectPropertyMap<?> map, TaskMonitor monitor) throws CancelledException {
+ AddressIterator iter = set != null ? map.getPropertyIterator(set) : map.getPropertyIterator();
+ while (iter.hasNext()) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ Address addr = iter.next();
+ Object value = map.get(addr);
+ ExtProperty isf;
+ if (value instanceof SaveablePoint) {
+ isf = new ExtProperty(map.getName(), "point", value.toString());
+ }
+ else if (value instanceof SaveableColor) {
+ isf = new ExtProperty(map.getName(), "color", value.toString());
+ }
+ else {
+ return;
+ }
+ SarifObject sarif = new SarifObject(PropertiesSarifMgr.SUBKEY, PropertiesSarifMgr.KEY, getTree(isf), addr,
+ addr);
+ objects.add(getTree(sarif));
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtEquateReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtEquateReference.java
new file mode 100644
index 0000000000..16482c82b1
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtEquateReference.java
@@ -0,0 +1,32 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.symbol.EquateReference;
+
+public class ExtEquateReference {
+
+ String name;
+ int opIndex;
+ long value;
+
+ public ExtEquateReference(EquateReference ref, String name, long value) {
+ this.name = name;
+ this.value = value;
+ opIndex = ref.getOpIndex();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtExternalReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtExternalReference.java
new file mode 100644
index 0000000000..f76b677ec4
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtExternalReference.java
@@ -0,0 +1,55 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalReference;
+
+public class ExtExternalReference extends ExtReference {
+
+ String name;
+ String origImport;
+ boolean isClass;
+ boolean isFunction;
+
+ String libLabel;
+ String libAddr;
+ String libExtAddr;
+
+ public ExtExternalReference(ExternalReference ref) {
+ super(ref);
+ ExternalLocation extLoc = ref.getExternalLocation();
+ String label = extLoc.getLabel();
+ Address addr = extLoc.getAddress();
+ Address extAddr = extLoc.getExternalSpaceAddress();
+
+ name = extLoc.getParentNameSpace().getName(true);
+ origImport = extLoc.getOriginalImportedName();
+ isClass = extLoc.getClass() != null;
+ isFunction = extLoc.getFunction() != null;
+ if (label != null) {
+ libLabel = label;
+ }
+ if (addr != null) {
+ libAddr = addr.toString();
+ }
+ if (extAddr != null) {
+ libExtAddr = extAddr.toString();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtMemoryReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtMemoryReference.java
new file mode 100644
index 0000000000..5f24bb11e7
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtMemoryReference.java
@@ -0,0 +1,39 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.symbol.OffsetReference;
+import ghidra.program.model.symbol.Reference;
+
+public class ExtMemoryReference extends ExtReference {
+
+ String to;
+ String base;
+ long offset;
+ boolean primary;
+
+ public ExtMemoryReference(Reference ref) {
+ super(ref);
+ to = ref.getToAddress().toString();
+ if (ref.isOffsetReference()) {
+ OffsetReference oref = (OffsetReference) ref;
+ base = oref.getBaseAddress().toString();
+ offset = oref.getOffset();
+ }
+ primary = ref.isPrimary();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtReference.java
new file mode 100644
index 0000000000..c8ca034b07
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtReference.java
@@ -0,0 +1,35 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.symbol.Reference;
+
+public abstract class ExtReference implements IsfObject {
+
+ String index;
+ String kind;
+ int opIndex;
+ String sourceType;
+
+ public ExtReference(Reference ref) {
+ index = Byte.toString(ref.getReferenceType().getValue());
+ kind = ref.getReferenceType().getName();
+ opIndex = ref.getOperandIndex();
+ sourceType = ref.getSource().toString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtRegisterReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtRegisterReference.java
new file mode 100644
index 0000000000..fd0d415141
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtRegisterReference.java
@@ -0,0 +1,31 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.symbol.Reference;
+
+public class ExtRegisterReference extends ExtReference {
+
+ String to;
+ boolean primary;
+
+ public ExtRegisterReference(Reference ref) {
+ super(ref);
+ to = ref.getToAddress().toString();
+ primary = ref.isPrimary();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtShiftedReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtShiftedReference.java
new file mode 100644
index 0000000000..1f5832352b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtShiftedReference.java
@@ -0,0 +1,31 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.symbol.ShiftedReference;
+
+public class ExtShiftedReference extends ExtReference {
+
+ int shift;
+ long value;
+
+ public ExtShiftedReference(ShiftedReference ref) {
+ super(ref);
+ shift = ref.getShift();
+ value = ref.getValue();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtStackReference.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtStackReference.java
new file mode 100644
index 0000000000..d16e8ddf5a
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/ExtStackReference.java
@@ -0,0 +1,29 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import ghidra.program.model.symbol.StackReference;
+
+public class ExtStackReference extends ExtReference {
+
+ int offset;
+
+ public ExtStackReference(StackReference ref) {
+ super(ref);
+ offset = ref.getStackOffset();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifEquateRefWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifEquateRefWriter.java
new file mode 100644
index 0000000000..6a7534d2d7
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifEquateRefWriter.java
@@ -0,0 +1,81 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.symbol.Equate;
+import ghidra.program.model.symbol.EquateReference;
+import ghidra.program.model.symbol.EquateTable;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+
+public class SarifEquateRefWriter extends AbstractExtWriter {
+
+ private AddressSetView set;
+ private EquateTable equateTable;
+
+ public SarifEquateRefWriter(EquateTable equateTable, AddressSetView set, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.equateTable = equateTable;
+ this.set = set;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genReferences(monitor);
+ root.add("equates", objects);
+ }
+
+ private void genReferences(TaskMonitor monitor) throws CancelledException, IOException{
+
+ Iterator<Equate> iter = equateTable.getEquates();
+ while (iter.hasNext()) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ Equate equate = iter.next();
+ String name = equate.getName();
+ long value = equate.getValue();
+ EquateReference[] refs = equate.getReferences();
+ for (int i = 0; i < refs.length; i++) {
+ if (monitor.isCancelled()) {
+ return;
+ }
+ Address addr = refs[i].getAddress();
+ if (!set.contains(addr)) {
+ continue;
+ }
+ ExtEquateReference eref = new ExtEquateReference(refs[i], name, value);
+ SarifObject sarif = new SarifObject("Ref.Equate", "REFERENCES", getTree(eref), addr, addr);
+ objects.add(getTree(sarif));
+ }
+ }
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifEquateWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifEquateWriter.java
new file mode 100644
index 0000000000..a5f1dbbdce
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifEquateWriter.java
@@ -0,0 +1,81 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.symbol.Equate;
+import ghidra.program.model.symbol.EquateReference;
+import ghidra.program.model.symbol.EquateTable;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+
+public class SarifEquateWriter extends AbstractExtWriter {
+
+ private AddressSetView set;
+ private EquateTable equateTable;
+
+ public SarifEquateWriter(EquateTable equateTable, AddressSetView set, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.equateTable = equateTable;
+ this.set = set;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genReferences(monitor);
+ root.add("equates", objects);
+ }
+
+ private void genReferences(TaskMonitor monitor) throws CancelledException, IOException{
+
+ Iterator<Equate> iter = equateTable.getEquates();
+ while (iter.hasNext()) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ Equate equate = iter.next();
+ String name = equate.getName();
+ long value = equate.getValue();
+ EquateReference[] refs = equate.getReferences();
+ for (int i = 0; i < refs.length; i++) {
+ if (monitor.isCancelled()) {
+ return;
+ }
+ Address addr = refs[i].getAddress();
+ if (!set.contains(addr)) {
+ continue;
+ }
+ ExtEquateReference eref = new ExtEquateReference(refs[i], name, value);
+ SarifObject sarif = new SarifObject("Ref.Equate", "REFERENCES", getTree(eref), addr, addr);
+ objects.add(getTree(sarif));
+ }
+ }
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifReferenceWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifReferenceWriter.java
new file mode 100644
index 0000000000..f6b5bd457d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/ref/SarifReferenceWriter.java
@@ -0,0 +1,114 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.ref;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalReference;
+import ghidra.program.model.symbol.Reference;
+import ghidra.program.model.symbol.ReferenceManager;
+import ghidra.program.model.symbol.ShiftedReference;
+import ghidra.program.model.symbol.StackReference;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.export.extlib.ExtLibraryLocation;
+import sarif.managers.ExternalLibSarifMgr;
+import sarif.managers.MarkupSarifMgr;
+
+public class SarifReferenceWriter extends AbstractExtWriter {
+
+ private List<Address> references = new ArrayList<>();
+ private ReferenceManager referenceManager;
+
+ public SarifReferenceWriter(ReferenceManager referenceManager, List<Address> request, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.referenceManager = referenceManager;
+ this.references = request;
+ }
+
+ public void requestFunction(Address next) {
+ references.add(next);
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genReferences(monitor);
+ root.add("references", objects);
+ }
+
+ private void genReferences(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(references.size());
+ for (Address addr : references) {
+ Reference[] refs = referenceManager.getReferencesFrom(addr);
+ for (int i = 0; i < refs.length; i++) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ Reference ref = refs[i];
+ if (ref.isRegisterReference()) {
+ ExtRegisterReference mref = new ExtRegisterReference(ref);
+ SarifObject sarif = new SarifObject("Ref.Register", MarkupSarifMgr.KEY, getTree(mref), ref.getFromAddress(), ref.getFromAddress());
+ objects.add(getTree(sarif));
+ }
+ if (ref.isMemoryReference()) {
+ ExtMemoryReference mref = new ExtMemoryReference(ref);
+ SarifObject sarif = new SarifObject("Ref.Memory", MarkupSarifMgr.KEY, getTree(mref), ref.getFromAddress(), ref.getFromAddress());
+ objects.add(getTree(sarif));
+ }
+ if (ref.isStackReference()) {
+ ExtStackReference sref = new ExtStackReference((StackReference) ref);
+ SarifObject sarif = new SarifObject("Ref.Stack", MarkupSarifMgr.KEY, getTree(sref), ref.getFromAddress(), ref.getFromAddress());
+ objects.add(getTree(sarif));
+ }
+ if (ref.isShiftedReference()) {
+ ExtShiftedReference sref = new ExtShiftedReference((ShiftedReference) ref);
+ SarifObject sarif = new SarifObject("Ref.Shifted", MarkupSarifMgr.KEY, getTree(sref), ref.getFromAddress(), ref.getFromAddress());
+ objects.add(getTree(sarif));
+ }
+ if (ref.isExternalReference()) {
+ ExternalReference extRef = (ExternalReference) ref;
+
+ // OK, this is overkill, but some of these locations are not written by
+ // either the SarifClassesNamspaceWriter or the SarifExternalLibraryWriter
+ ExternalLocation extLoc = extRef.getExternalLocation();
+ ExtLibraryLocation obj = new ExtLibraryLocation(extLoc);
+ SarifObject sarif0 = new SarifObject(ExternalLibSarifMgr.SUBKEY1, ExternalLibSarifMgr.KEY, getTree(obj),
+ extLoc.getAddress(), extLoc.getAddress());
+ objects.add(getTree(sarif0));
+
+ ExtExternalReference xref = new ExtExternalReference(extRef);
+ SarifObject sarif = new SarifObject("Ref.External", MarkupSarifMgr.KEY, getTree(xref), ref.getFromAddress(), ref.getFromAddress());
+ objects.add(getTree(sarif));
+ }
+ }
+ monitor.increment();
+ }
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/registers/ExtRegisterValue.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/registers/ExtRegisterValue.java
new file mode 100644
index 0000000000..ce9d8b9b49
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/registers/ExtRegisterValue.java
@@ -0,0 +1,32 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.registers;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.lang.Register;
+
+public class ExtRegisterValue implements IsfObject {
+
+ String name;
+ String value;
+
+ public ExtRegisterValue(Register reg, String value) {
+ name = reg.getName();
+ this.value = value;
+ }
+
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/registers/SarifRegisterValueWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/registers/SarifRegisterValueWriter.java
new file mode 100644
index 0000000000..c6576c6845
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/registers/SarifRegisterValueWriter.java
@@ -0,0 +1,75 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.registers;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.math.BigInteger;
+import java.util.List;
+
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.lang.Register;
+import ghidra.program.model.listing.ProgramContext;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.RegisterValuesSarifMgr;
+
+public class SarifRegisterValueWriter extends AbstractExtWriter {
+
+ List<AddressRange> ranges;
+ ProgramContext context;
+ List<Register> registers;
+
+ public SarifRegisterValueWriter(ProgramContext context, List<Register> registers, List<AddressRange> request,
+ Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.ranges = request;
+ this.context = context;
+ this.registers = registers;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genRegisters(monitor);
+ root.add("registers", objects);
+ }
+
+ private void genRegisters(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(ranges.size());
+ for (AddressRange r : ranges) {
+ for (Register reg : registers) {
+ AddressRangeIterator it = context.getRegisterValueAddressRanges(reg, r.getMinAddress(),
+ r.getMaxAddress());
+ while (it.hasNext()) {
+ monitor.checkCancelled();
+ AddressRange valueRange = it.next();
+ BigInteger value = context.getValue(reg, valueRange.getMinAddress(), false);
+ if (value == null) {
+ continue;
+ }
+ ExtRegisterValue isf = new ExtRegisterValue(reg, value.toString(16));
+ SarifObject sarif = new SarifObject(RegisterValuesSarifMgr.SUBKEY, RegisterValuesSarifMgr.KEY, getTree(isf), valueRange.getMinAddress(), valueRange.getMaxAddress());
+ objects.add(getTree(sarif));
+ }
+ }
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/relocs/ExtRelocation.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/relocs/ExtRelocation.java
new file mode 100644
index 0000000000..8062c10f17
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/relocs/ExtRelocation.java
@@ -0,0 +1,71 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.relocs;
+
+import org.apache.commons.lang3.StringUtils;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.reloc.Relocation;
+
+public class ExtRelocation implements IsfObject {
+
+ String name;
+ String kind;
+ String value;
+ String bytes;
+
+ public ExtRelocation(Relocation reloc) {
+ kind = Integer.toString(reloc.getType());
+ value = pack(reloc.getValues());
+ String packedBytes = pack(reloc.getBytes());
+ if (packedBytes != null) {
+ bytes = packedBytes;
+ }
+ String symName = reloc.getSymbolName();
+ if (!StringUtils.isEmpty(symName)) {
+ name = reloc.getSymbolName();
+ }
+ }
+
+ private String pack(long[] values) {
+ if (values == null || values.length == 0) {
+ return "";
+ }
+ StringBuffer buf = new StringBuffer();
+ for (long v : values) {
+ if (buf.length() != 0) {
+ buf.append(',');
+ }
+ buf.append("0x" + Long.toHexString(v));
+ }
+ return buf.toString();
+ }
+
+ private String pack(byte[] values) {
+ if (values == null || values.length == 0) {
+ return null;
+ }
+ StringBuffer buf = new StringBuffer();
+ for (byte v : values) {
+ if (buf.length() != 0) {
+ buf.append(',');
+ }
+ buf.append("0x" + Integer.toHexString(v & 0xff));
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/relocs/SarifRelocationWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/relocs/SarifRelocationWriter.java
new file mode 100644
index 0000000000..1e1215cb6d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/relocs/SarifRelocationWriter.java
@@ -0,0 +1,55 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.relocs;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import ghidra.program.model.reloc.Relocation;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.RelocationTableSarifMgr;
+
+public class SarifRelocationWriter extends AbstractExtWriter {
+
+ List<Relocation> relocs;
+
+ public SarifRelocationWriter(List<Relocation> request, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.relocs = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genRelocation(monitor);
+ root.add("relocations", objects);
+ }
+
+ private void genRelocation(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(relocs.size());
+ for (Relocation r : relocs) {
+ ExtRelocation isf = new ExtRelocation(r);
+ SarifObject sarif = new SarifObject(RelocationTableSarifMgr.SUBKEY, RelocationTableSarifMgr.KEY,
+ getTree(isf), r.getAddress(), r.getAddress());
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/symbols/ExtSymbol.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/symbols/ExtSymbol.java
new file mode 100644
index 0000000000..2ba4590908
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/symbols/ExtSymbol.java
@@ -0,0 +1,84 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.symbols;
+
+import ghidra.program.database.symbol.ClassSymbol;
+import ghidra.program.database.symbol.LibrarySymbol;
+import ghidra.program.database.symbol.NamespaceSymbol;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.GhidraClass;
+import ghidra.program.model.listing.Library;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.Symbol;
+
+public class ExtSymbol implements IsfObject {
+
+ String name;
+ String location;
+ Boolean namespaceIsClass;
+ String kind;
+ String type;
+ String sourceType;
+ boolean primary;
+
+ public ExtSymbol(Symbol symbol) {
+ name = symbol.getName();
+ location = getNamespace(symbol);
+ kind = checkGlobal(symbol) ? "global" : "local";
+ sourceType = symbol.getSource().toString();
+ primary = symbol.isPrimary();
+ if (symbol instanceof ClassSymbol) {
+ type = "class";
+ } else if (symbol instanceof LibrarySymbol) {
+ type = "library";
+ } else if (symbol instanceof NamespaceSymbol) {
+ type = "namespace";
+ }
+ // NB: DO NOT add type==function, as this will affect the execution order
+ //if (symbol instanceof FunctionSymbol fs) {
+ // type = "function";
+ //}
+ }
+
+ /**
+ * Returns the name of symbol qualified with any namespace information. For
+ * example, "User32.dll::SomeClass::printf".
+ */
+ private String getNamespace(Symbol symbol) {
+ StringBuffer buffer = new StringBuffer();
+ Namespace namespace = symbol.getParentNamespace();
+ while (!namespace.isGlobal()) {
+ buffer.insert(0, namespace.getName() + "::");
+ if (namespace instanceof GhidraClass) {
+ namespaceIsClass = true;
+ }
+ namespace = namespace.getParentNamespace();
+ }
+ return buffer.toString();
+ }
+
+ private boolean checkGlobal(Symbol symbol) {
+ if (symbol.isGlobal()) {
+ return true;
+ }
+ Namespace parent = symbol.getParentNamespace();
+ if (parent instanceof Library) {
+ return true;
+ }
+ return false;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/symbols/SarifSymbolWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/symbols/SarifSymbolWriter.java
new file mode 100644
index 0000000000..b9960545f5
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/symbols/SarifSymbolWriter.java
@@ -0,0 +1,80 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.symbols;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.gson.JsonArray;
+
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolType;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.SymbolTableSarifMgr;
+
+public class SarifSymbolWriter extends AbstractExtWriter {
+
+ private List<Symbol> symbols = new ArrayList<>();
+
+ public SarifSymbolWriter(List<Symbol> request, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.symbols = request;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genSymbols(monitor);
+ root.add("definedData", objects);
+ }
+
+ private void genSymbols(TaskMonitor monitor) throws CancelledException, IOException{
+ monitor.initialize(symbols.size());
+ for (Symbol s : symbols) {
+ SymbolType symbolType = s.getSymbolType();
+ if (s.getSource() == SourceType.DEFAULT) {
+ continue;
+ }
+
+ if (symbolType != SymbolType.LABEL && symbolType != SymbolType.FUNCTION) {
+ continue;
+ }
+
+ addSymbol(s);
+ monitor.increment();
+ }
+ }
+
+ private void addSymbol(Symbol s) {
+ if (!s.getParentNamespace().isGlobal()) {
+ Symbol p = s.getParentSymbol();
+ addSymbol(p);
+ }
+ ExtSymbol lib = new ExtSymbol(s);
+ SarifObject sarif = new SarifObject("Symbol", SymbolTableSarifMgr.KEY, getTree(lib), s.getAddress(), s.getAddress());
+ objects.add(getTree(sarif));
+ }
+
+ public JsonArray getResults() {
+ return objects;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtFragment.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtFragment.java
new file mode 100644
index 0000000000..86b1dcfe8c
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtFragment.java
@@ -0,0 +1,42 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.trees;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.ProgramFragment;
+
+public class ExtFragment implements IsfObject {
+
+ String name;
+ List<ExtFragmentRange> ranges = new ArrayList<>();
+
+ public ExtFragment(ProgramFragment f, List<Object> visited) {
+ this.name = f.getName();
+ visited.add(f);
+ AddressRangeIterator iter = f.getAddressRanges();
+ while (iter.hasNext()) {
+ AddressRange range = iter.next();
+ ExtFragmentRange r = new ExtFragmentRange(range);
+ ranges.add(r);
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtFragmentRange.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtFragmentRange.java
new file mode 100644
index 0000000000..e0f3fbd4f1
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtFragmentRange.java
@@ -0,0 +1,31 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.trees;
+
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.data.ISF.IsfObject;
+
+public class ExtFragmentRange implements IsfObject {
+
+ String start;
+ String end;
+
+ public ExtFragmentRange(AddressRange range) {
+ start = range.getMinAddress().toString();
+ end = range.getMaxAddress().toString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtModule.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtModule.java
new file mode 100644
index 0000000000..43038a07c4
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/ExtModule.java
@@ -0,0 +1,51 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.trees;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import ghidra.program.model.data.ISF.IsfObject;
+import ghidra.program.model.listing.Group;
+import ghidra.program.model.listing.ProgramFragment;
+import ghidra.program.model.listing.ProgramModule;
+
+public class ExtModule implements IsfObject {
+
+ String name;
+ String value;
+
+ List<ExtModule> modules = new ArrayList<>();
+ List<ExtFragment> fragments = new ArrayList<>();
+
+ public ExtModule(String name, ProgramModule m, List<Object> visited) {
+ this.name = name;
+ if (!visited.contains(m)) {
+ visited.add(m);
+ Group[] children = m.getChildren();
+ for (Group child : children) {
+ if (child instanceof ProgramModule cm) {
+ ExtModule module = new ExtModule(child.getName(), cm, visited);
+ modules.add(module);
+ } else if (child instanceof ProgramFragment cf) {
+ ExtFragment fragment = new ExtFragment(cf, visited);
+ fragments.add(fragment);
+ }
+ }
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/SarifTreeWriter.java b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/SarifTreeWriter.java
new file mode 100644
index 0000000000..d1c4659734
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/trees/SarifTreeWriter.java
@@ -0,0 +1,57 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.export.trees;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
+
+import generic.stl.Pair;
+import ghidra.program.model.listing.ProgramModule;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+import sarif.export.AbstractExtWriter;
+import sarif.export.SarifObject;
+import sarif.managers.ProgramTreeSarifMgr;
+
+public class SarifTreeWriter extends AbstractExtWriter {
+
+ private List<Pair<String, ProgramModule>> modules;
+ private List<Object> visited = new ArrayList<>();
+
+ public SarifTreeWriter(List<Pair<String, ProgramModule>> target, Writer baseWriter) throws IOException {
+ super(baseWriter);
+ this.modules = target;
+ }
+
+ @Override
+ protected void genRoot(TaskMonitor monitor) throws CancelledException, IOException {
+ genTree(monitor);
+ root.add("trees", objects);
+ }
+
+ private void genTree(TaskMonitor monitor) throws CancelledException, IOException {
+ monitor.initialize(modules.size());
+ for (Pair<String, ProgramModule> pair : modules) {
+ ExtModule isf = new ExtModule(pair.first, pair.second, visited);
+ SarifObject sarif = new SarifObject(ProgramTreeSarifMgr.SUBKEY, ProgramTreeSarifMgr.KEY, getTree(isf), null);
+ objects.add(getTree(sarif));
+ monitor.increment();
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/SarifResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/SarifResultHandler.java
new file mode 100644
index 0000000000..b2728b9313
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/SarifResultHandler.java
@@ -0,0 +1,106 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.contrastsecurity.sarif.PropertyBag;
+import com.contrastsecurity.sarif.Result;
+import com.contrastsecurity.sarif.Run;
+
+import docking.ActionContext;
+import docking.action.DockingAction;
+import docking.action.MenuData;
+import ghidra.program.util.ProgramTask;
+import ghidra.util.classfinder.ExtensionPoint;
+import ghidra.util.task.TaskLauncher;
+import sarif.SarifController;
+import sarif.model.SarifDataFrame;
+import sarif.view.SarifResultsTableProvider;
+
+abstract public class SarifResultHandler implements ExtensionPoint {
+
+ protected List<String> headers = new ArrayList<>();
+ protected SarifDataFrame df;
+ protected SarifController controller;
+ protected Run run;
+ protected Result result;
+ protected SarifResultsTableProvider provider;
+
+ public abstract String getKey();
+
+ public boolean isEnabled() {
+ return true;
+ }
+
+ public void handle(SarifDataFrame df, Run run, Result result, Map<String, Object> map) {
+ this.df = df;
+ this.controller = df.getController();
+ this.run = run;
+ this.result = result;
+ Object res = parse();
+ if (res != null) {
+ map.put(getKey(), res);
+ }
+ }
+
+ protected abstract Object parse();
+
+ public String getActionName() {
+ return null;
+ }
+
+ protected Object getProperty(String key) {
+ PropertyBag properties = result.getProperties();
+ if (properties == null) {
+ return null;
+ }
+ Map<String, Object> additionalProperties = properties.getAdditionalProperties();
+ if (additionalProperties == null) {
+ return null;
+ }
+ return additionalProperties.get(key);
+ }
+
+ public ProgramTask getTask(SarifResultsTableProvider provider) {
+ return null;
+ }
+
+ public DockingAction createAction(SarifResultsTableProvider provider) {
+ this.provider = provider;
+ DockingAction rightClick = new DockingAction(getActionName(), getKey()) {
+ @Override
+ public void actionPerformed(ActionContext context) {
+ ProgramTask task = getTask(provider);
+ TaskLauncher.launch(task);
+ }
+
+ @Override
+ public boolean isEnabledForContext(ActionContext context) {
+ return true;
+ }
+
+ @Override
+ public boolean isAddToPopup(ActionContext context) {
+ return true;
+ }
+ };
+ rightClick.setPopupMenuData(new MenuData(new String[] { getActionName() }));
+ return rightClick;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/SarifRunHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/SarifRunHandler.java
new file mode 100644
index 0000000000..5d8775435d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/SarifRunHandler.java
@@ -0,0 +1,45 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers;
+
+import com.contrastsecurity.sarif.Run;
+
+import ghidra.util.classfinder.ExtensionPoint;
+import sarif.SarifController;
+import sarif.model.SarifDataFrame;
+
+abstract public class SarifRunHandler implements ExtensionPoint {
+
+ protected SarifDataFrame df;
+ protected SarifController controller;
+ protected Run run;
+
+ public abstract String getKey();
+
+ public boolean isEnabled() {
+ return true;
+ }
+
+ public void handle(SarifDataFrame df, Run run) {
+ this.df = df;
+ this.controller = df.getController();
+ this.run = run;
+ parse();
+ }
+
+ protected abstract Object parse();
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifAddressResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifAddressResultHandler.java
new file mode 100644
index 0000000000..d61b18fda8
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifAddressResultHandler.java
@@ -0,0 +1,38 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result
+;
+
+import java.util.List;
+
+import ghidra.program.model.address.Address;
+import sarif.handlers.SarifResultHandler;
+
+public class SarifAddressResultHandler extends SarifResultHandler {
+
+ // If we can parse a listing Address we can make the table navigate there when
+ // selected
+
+ public String getKey() {
+ return "Address";
+ }
+
+ public Address parse() {
+ List<Address> listingAddresses = controller.getListingAddresses(result);
+ return listingAddresses.isEmpty() ? null : listingAddresses.get(0);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifCodeFlowResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifCodeFlowResultHandler.java
new file mode 100644
index 0000000000..0fd1f6973f
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifCodeFlowResultHandler.java
@@ -0,0 +1,59 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.contrastsecurity.sarif.CodeFlow;
+import com.contrastsecurity.sarif.ThreadFlow;
+import com.contrastsecurity.sarif.ThreadFlowLocation;
+
+import ghidra.program.model.address.Address;
+import sarif.handlers.SarifResultHandler;
+
+public class SarifCodeFlowResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "CodeFlows";
+ }
+
+ public List<Map<String, List<Address>>> parse() {
+ List<Map<String, List<Address>>> res = new ArrayList<>();
+ List<CodeFlow> codeFlows = result.getCodeFlows();
+ if (codeFlows != null) {
+ for (CodeFlow f : codeFlows) {
+ Map<String, List<Address>> map = new HashMap<>();
+ parseCodeFlow(f, map);
+ res.add(map);
+ }
+ }
+ return res;
+ }
+
+ private void parseCodeFlow(CodeFlow f, Map<String, List<Address>> map) {
+ for (ThreadFlow t : f.getThreadFlows()) {
+ List<Address> addrs = new ArrayList<Address>();
+ for (ThreadFlowLocation loc : t.getLocations()) {
+ addrs.add(controller.locationToAddress(loc.getLocation()));
+ }
+ map.put(t.getId(), addrs);
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifGraphResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifGraphResultHandler.java
new file mode 100644
index 0000000000..068caaffc5
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifGraphResultHandler.java
@@ -0,0 +1,63 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.contrastsecurity.sarif.Edge;
+import com.contrastsecurity.sarif.Graph;
+import com.contrastsecurity.sarif.Node;
+
+import ghidra.service.graph.AttributedGraph;
+import ghidra.service.graph.AttributedVertex;
+import ghidra.service.graph.EmptyGraphType;
+import sarif.handlers.SarifResultHandler;
+
+public class SarifGraphResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "Graphs";
+ }
+
+ public List<AttributedGraph> parse() {
+ List<AttributedGraph> res = new ArrayList<AttributedGraph>();
+ Set<Graph> graphs = result.getGraphs();
+ if (graphs != null) {
+ for (Graph g : graphs) {
+ res.add(parseGraph(g));
+ }
+ }
+ return res;
+ }
+
+ private AttributedGraph parseGraph(Graph g) {
+ AttributedGraph graph = new AttributedGraph(controller.getProgram().getDescription(), new EmptyGraphType());
+ Map<String, AttributedVertex> nodeMap = new HashMap<String, AttributedVertex>();
+ for (Node n : g.getNodes()) {
+ // AttributedVertex node = graph.addVertex(n.getId(), n.getLabel().getText());
+ // node.
+ nodeMap.put(n.getId(), graph.addVertex(n.getId(), n.getLabel().getText()));
+ }
+ for (Edge e : g.getEdges()) {
+ graph.addEdge(nodeMap.get(e.getSourceNodeId()), nodeMap.get(e.getTargetNodeId()));
+ }
+ return graph;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifKindResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifKindResultHandler.java
new file mode 100644
index 0000000000..9570bcf193
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifKindResultHandler.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import sarif.handlers.SarifResultHandler;
+
+public class SarifKindResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "Kind";
+ }
+
+ public String parse() {
+ return result.getKind().toString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifLevelResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifLevelResultHandler.java
new file mode 100644
index 0000000000..a88a320e01
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifLevelResultHandler.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import sarif.handlers.SarifResultHandler;
+
+public class SarifLevelResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "Level";
+ }
+
+ public String parse() {
+ return result.getLevel().toString();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifProgramResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifProgramResultHandler.java
new file mode 100644
index 0000000000..2591fb200c
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifProgramResultHandler.java
@@ -0,0 +1,115 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import com.contrastsecurity.sarif.Location;
+import com.contrastsecurity.sarif.PropertyBag;
+import com.contrastsecurity.sarif.Result;
+import com.contrastsecurity.sarif.Run;
+
+import ghidra.program.util.ProgramTask;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.handlers.SarifResultHandler;
+import sarif.managers.ProgramSarifMgr;
+import sarif.model.SarifDataFrame;
+import sarif.view.SarifResultsTableProvider;
+
+public class SarifProgramResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "Message";
+ }
+
+ public void handle(SarifDataFrame df, Run run, Result result, Map<String, Object> map) {
+ this.df = df;
+ this.controller = df.getController();
+
+ this.run = run;
+ this.result = result;
+ map.put(getKey(), result.getMessage().getText());
+ List<Location> locs = result.getLocations();
+ if (locs != null) {
+ map.put("Locations", locs);
+ }
+ PropertyBag properties = result.getProperties();
+ if (properties != null) {
+ Map<String, Object> additionalProperties = properties.getAdditionalProperties();
+ if (additionalProperties != null) {
+ for (Entry<String, Object> entry : additionalProperties.entrySet()) {
+ map.put(entry.getKey(), entry.getValue());
+ }
+ }
+ }
+ }
+
+ @Override
+ protected Object parse() {
+ // UNUSED
+ return null;
+ }
+
+ @Override
+ public String getActionName() {
+ return "Add To Program";
+ }
+
+ @Override
+ public ProgramTask getTask(SarifResultsTableProvider provider) {
+ return new CommitToProgramTask(provider);
+ }
+
+ private class CommitToProgramTask extends ProgramTask {
+
+ private SarifResultsTableProvider provider;
+ private ProgramSarifMgr programMgr;
+
+ protected CommitToProgramTask(SarifResultsTableProvider provider) {
+ super(provider.getController().getProgram(), "CommitToProgramTask", true, true, true);
+ this.provider = provider;
+ this.programMgr = provider.getController().getProgramSarifMgr();
+ programMgr.addManagers();
+ }
+
+ protected void doRun(TaskMonitor monitor) {
+ int[] selected = provider.filterTable.getTable().getSelectedRows();
+ Map<String, List<Map<String, Object>>> results = new HashMap<>();
+ for (int row : selected) {
+ Map<String, Object> result = provider.getRow(row);
+ String key = (String) result.get("RuleId");
+ List<Map<String, Object>> list = results.get(key);
+ if (list == null) {
+ list = new ArrayList<>();
+ }
+ list.add(result);
+ results.put(key, list);
+ }
+ try {
+ programMgr.readResults(monitor, (SarifProgramOptions) null, results);
+ } catch (IOException e) {
+ throw new RuntimeException("Read failed");
+ }
+ }
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifPropertyResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifPropertyResultHandler.java
new file mode 100644
index 0000000000..6ea8f9bc0e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifPropertyResultHandler.java
@@ -0,0 +1,79 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.contrastsecurity.sarif.PropertyBag;
+import com.contrastsecurity.sarif.Result;
+import com.contrastsecurity.sarif.Run;
+
+import db.Transaction;
+import ghidra.program.model.address.Address;
+import sarif.handlers.SarifResultHandler;
+import sarif.model.SarifColumnKey;
+import sarif.model.SarifDataFrame;
+
+public class SarifPropertyResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "Property";
+ }
+
+ public List<Address> parse() {
+ return controller.getListingAddresses(result);
+ }
+
+ @Override
+ public void handle(SarifDataFrame df, Run run, Result result, Map<String, Object> map) {
+ this.controller = df.getController();
+ List<SarifColumnKey> columns = df.getColumns();
+ List<String> columnNames = new ArrayList<>();
+ for (SarifColumnKey c : columns) {
+ columnNames.add(c.getName());
+ }
+ PropertyBag properties = result.getProperties();
+ if (properties == null) {
+ return;
+ }
+ Map<String, Object> additional = properties.getAdditionalProperties();
+ if (additional == null) {
+ return;
+ }
+ try (Transaction t = controller.getProgram().openTransaction("SARIF custom properties.")) {
+ for (String key : additional.keySet()) {
+ String[] splits = key.split("/");
+ switch (splits[0]) {
+ case "viewer":
+ switch (splits[1]) {
+ case "table":
+ if (!columnNames.contains(splits[2])) {
+ columns.add(new SarifColumnKey(splits[2], false));
+ }
+ map.put(splits[2], additional.get(key));
+ }
+ break;
+ case "listing":
+ controller.handleListingAction(result, splits[1], additional.get(key));
+ break;
+ }
+ }
+ t.commit();
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifRuleIdResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifRuleIdResultHandler.java
new file mode 100644
index 0000000000..af8c9bbc0e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifRuleIdResultHandler.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import sarif.handlers.SarifResultHandler;
+
+public class SarifRuleIdResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "RuleId";
+ }
+
+ public String parse() {
+ return result.getRuleId();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifToolResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifToolResultHandler.java
new file mode 100644
index 0000000000..867c750af4
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/SarifToolResultHandler.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result;
+
+import sarif.handlers.SarifResultHandler;
+
+public class SarifToolResultHandler extends SarifResultHandler {
+
+ public String getKey() {
+ return "Tool";
+ }
+
+ public String parse() {
+ return run.getTool().getDriver().getName();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/sample/SarifReturnTypeResultHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/sample/SarifReturnTypeResultHandler.java
new file mode 100644
index 0000000000..6c66e75baf
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/result/sample/SarifReturnTypeResultHandler.java
@@ -0,0 +1,140 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.result.sample;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.contrastsecurity.sarif.ReportingDescriptor;
+import com.contrastsecurity.sarif.ReportingDescriptorReference;
+import com.contrastsecurity.sarif.ToolComponent;
+
+import ghidra.program.model.data.BooleanDataType;
+import ghidra.program.model.data.CharDataType;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DoubleDataType;
+import ghidra.program.model.data.IntegerDataType;
+import ghidra.program.model.data.LongDataType;
+import ghidra.program.model.data.LongDoubleDataType;
+import ghidra.program.model.data.PointerDataType;
+import ghidra.program.model.data.UnsignedIntegerDataType;
+import ghidra.program.model.data.UnsignedLongDataType;
+import ghidra.program.model.data.VoidDataType;
+import ghidra.program.model.listing.Function;
+import ghidra.program.util.ProgramTask;
+import ghidra.util.exception.InvalidInputException;
+import ghidra.util.task.TaskMonitor;
+import sarif.handlers.SarifResultHandler;
+import sarif.view.SarifResultsTableProvider;
+
+public class SarifReturnTypeResultHandler extends SarifResultHandler {
+
+ @Override
+ public String getKey() {
+ return "return type";
+ }
+
+ @Override
+ public String parse() {
+ ToolComponent tc = df.getComponentMap().get(getKey());
+ ReportingDescriptorReference ref = df.getTaxa().get(getKey());
+ if (tc != null) {
+ List<ReportingDescriptor> view = new ArrayList<>(tc.getTaxa());
+ if (ref != null) {
+ int index = ref.getIndex().intValue();
+ if (index < view.size()) {
+ return view.get(index).getId();
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String getActionName() {
+ return "Commit";
+ }
+
+ @Override
+ public ProgramTask getTask(SarifResultsTableProvider provider) {
+ return new ReturnTypeTaxonomyTask(provider);
+ }
+
+ private class ReturnTypeTaxonomyTask extends ProgramTask {
+
+ private SarifResultsTableProvider provider;
+
+ protected ReturnTypeTaxonomyTask(SarifResultsTableProvider provider) {
+ super(provider.getController().getProgram(), "ReturnTypeTaxonomyTask", true, true, true);
+ this.provider = provider;
+ }
+
+ protected void doRun(TaskMonitor monitor) {
+ int col = provider.getIndex("return type");
+ int[] selected = provider.filterTable.getTable().getSelectedRows();
+ for (int row : selected) {
+ Function func = provider.getController().getProgram().getFunctionManager()
+ .getFunctionContaining(provider.model.getAddress(row));
+ String value = (String) provider.getValue(row, col);
+ setReturnType(func, value);
+ }
+ }
+
+ private boolean setReturnType(Function func, String type) {
+ if (type != null) {
+ try {
+ func.setReturnType(parseDataType(type), func.getSignatureSource());
+ return true;
+ } catch (InvalidInputException e) {
+ throw new RuntimeException("Error setting return type for "+func);
+ }
+ }
+ return false;
+ }
+
+ private DataType parseDataType(String datatype) {
+ switch (datatype) {
+ case "int":
+ return new IntegerDataType();
+ case "uint":
+ case "__ssize_t":
+ return new UnsignedIntegerDataType();
+ case "bool":
+ return new BooleanDataType();
+ case "char":
+ return new CharDataType();
+ case "char *":
+ case "FILE *":
+ case "void *":
+ case "whcar_t *":
+ case "tm *":
+ return new PointerDataType();
+ case "void":
+ return new VoidDataType();
+ case "double":
+ return new DoubleDataType();
+ case "long":
+ return new LongDataType();
+ case "longdouble":
+ return new LongDoubleDataType();
+ case "ulong":
+ return new UnsignedLongDataType();
+ }
+ return null;
+ }
+
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/handlers/run/SarifGraphRunHandler.java b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/run/SarifGraphRunHandler.java
new file mode 100644
index 0000000000..b2289176de
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/handlers/run/SarifGraphRunHandler.java
@@ -0,0 +1,76 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.handlers.run;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.contrastsecurity.sarif.Edge;
+import com.contrastsecurity.sarif.Graph;
+import com.contrastsecurity.sarif.Node;
+import com.contrastsecurity.sarif.Run;
+
+import ghidra.service.graph.AttributedGraph;
+import ghidra.service.graph.AttributedVertex;
+import ghidra.service.graph.EmptyGraphType;
+import sarif.handlers.SarifRunHandler;
+import sarif.model.SarifDataFrame;
+
+public class SarifGraphRunHandler extends SarifRunHandler {
+
+ @Override
+ public String getKey() {
+ return "graphs";
+ }
+
+ @Override
+ public List<AttributedGraph> parse() {
+ List<AttributedGraph> res = new ArrayList<>();
+ Set<Graph> graphs = run.getGraphs();
+ if (graphs != null) {
+ for (Graph g : graphs) {
+ String description = g.getDescription() == null ? controller.getProgram().getDescription()
+ : g.getDescription().getText();
+ AttributedGraph graph = new AttributedGraph(description, new EmptyGraphType());
+ Map<String, AttributedVertex> nodeMap = new HashMap<String, AttributedVertex>();
+ for (Node n : g.getNodes()) {
+ nodeMap.put(n.getId(), graph.addVertex(n.getId(), n.getLabel().getText()));
+ }
+ for (Edge e : g.getEdges()) {
+ graph.addEdge(nodeMap.get(e.getSourceNodeId()), nodeMap.get(e.getTargetNodeId()));
+ }
+ res.add(graph);
+ }
+ }
+ return res;
+ }
+
+ @Override
+ public void handle(SarifDataFrame df, Run run) {
+ this.df = df;
+ this.controller = df.getController();
+ this.run = run;
+ List<AttributedGraph> res = parse();
+ if (res != null) {
+ for (AttributedGraph g : res) {
+ controller.showGraph(g);
+ }
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifGsonIO.java b/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifGsonIO.java
new file mode 100644
index 0000000000..9f59efe4fb
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifGsonIO.java
@@ -0,0 +1,38 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.io;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+
+import com.contrastsecurity.sarif.SarifSchema210;
+import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
+
+public class SarifGsonIO implements SarifIO {
+
+ public SarifSchema210 readSarif(File file) throws JsonSyntaxException, IOException {
+ Gson gson = new Gson();
+ return gson.fromJson(Files.readString(file.toPath()), SarifSchema210.class);
+ }
+
+ public SarifSchema210 readSarif(String str) throws JsonSyntaxException, IOException {
+ Gson gson = new Gson();
+ return gson.fromJson(str, SarifSchema210.class);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifIO.java b/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifIO.java
new file mode 100644
index 0000000000..480581e466
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifIO.java
@@ -0,0 +1,30 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.io;
+
+import java.io.File;
+import java.io.IOException;
+
+import com.contrastsecurity.sarif.SarifSchema210;
+import com.google.gson.JsonSyntaxException;
+
+public interface SarifIO {
+
+ public SarifSchema210 readSarif(File file) throws JsonSyntaxException, IOException;
+
+ public SarifSchema210 readSarif(String str) throws JsonSyntaxException, IOException;
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifJacksonIO.java b/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifJacksonIO.java
new file mode 100644
index 0000000000..f0c4f68c48
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/io/SarifJacksonIO.java
@@ -0,0 +1,38 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.io;
+
+import java.io.File;
+import java.io.IOException;
+
+import com.contrastsecurity.sarif.SarifSchema210;
+import com.google.gson.JsonSyntaxException;
+
+public class SarifJacksonIO implements SarifIO {
+
+ public SarifSchema210 readSarif(File file) throws JsonSyntaxException, IOException {
+// ObjectMapper mapper = new ObjectMapper();
+// return mapper.readValue(Files.readString(file.toPath()), SarifSchema210.class);
+ throw new IOException("Jackson not currently supported");
+ }
+
+ public SarifSchema210 readSarif(String str) throws JsonSyntaxException, IOException {
+// ObjectMapper mapper = new ObjectMapper();
+// return mapper.readValue(str, SarifSchema210.class);
+ throw new IOException("Jackson not currently supported");
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/BookmarksSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/BookmarksSarifMgr.java
new file mode 100644
index 0000000000..be3f713ca6
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/BookmarksSarifMgr.java
@@ -0,0 +1,132 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressIterator;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Bookmark;
+import ghidra.program.model.listing.BookmarkManager;
+import ghidra.program.model.listing.BookmarkType;
+import ghidra.program.model.listing.Program;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.bkmk.SarifBookmarkWriter;
+
+public class BookmarksSarifMgr extends SarifMgr {
+
+ public static String KEY = "BOOKMARKS";
+ public static String SUBKEY = "Bookmark";
+
+ private BookmarkManager bookmarkMgr;
+
+ public BookmarksSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ this.bookmarkMgr = program.getBookmarkManager();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ processBookmark(result, options == null || options.isOverwriteBookmarkConflicts());
+ return true;
+ }
+
+ private void processBookmark(Map<String, Object> result, boolean overwrite) {
+
+ Address addr = null;
+ try {
+ addr = getLocation(result);
+ } catch (AddressOverflowException e1) {
+ log.appendException(e1);
+ }
+
+ String type = (String) result.getOrDefault("kind", BookmarkType.NOTE);
+ String category = (String) result.getOrDefault("name", "");
+ String comment = (String) result.getOrDefault("comment", "");
+
+ try {
+ boolean hasExistingBookmark = bookmarkMgr.getBookmark(addr, type, category) != null;
+ if (overwrite || !hasExistingBookmark) {
+ bookmarkMgr.setBookmark(addr, type, category, comment);
+ }
+ if (!overwrite && hasExistingBookmark) {
+ log.appendMsg("Conflicting '" + type + "' BOOKMARK ignored at: " + addr);
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ return;
+ }
+
+ }
+
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing BOOKMARKS ...");
+ //writeBookmarks(results, set, monitor);
+
+ List<Bookmark> request = new ArrayList<>();
+ BookmarkType[] types = bookmarkMgr.getBookmarkTypes();
+ for (int i = 0; i < types.length; i++) {
+ if (monitor.isCancelled()) {
+ throw new CancelledException();
+ }
+ String typeStr = types[i].getTypeString();
+ AddressSetView bmSet = bookmarkMgr.getBookmarkAddresses(typeStr);
+ if (set != null) {
+ bmSet = set.intersect(bmSet);
+ }
+ AddressIterator iter = bmSet.getAddresses(true);
+ while (iter.hasNext()) {
+ Address addr = iter.next();
+ Bookmark[] bookmarks = bookmarkMgr.getBookmarks(addr, typeStr);
+ for (int n = 0; n < bookmarks.length; n++) {
+ request.add(bookmarks[n]);
+ }
+ }
+ }
+
+ writeAsSARIF(request, results);
+ }
+
+ public static void writeAsSARIF(List<Bookmark> request, JsonArray results)
+ throws IOException {
+ SarifBookmarkWriter writer = new SarifBookmarkWriter(request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/CodeSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/CodeSarifMgr.java
new file mode 100644
index 0000000000..6e0492b856
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/CodeSarifMgr.java
@@ -0,0 +1,167 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import generic.stl.Pair;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.disassemble.Disassembler;
+import ghidra.program.disassemble.DisassemblerMessageListener;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeImpl;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.FlowOverride;
+import ghidra.program.model.listing.Instruction;
+import ghidra.program.model.listing.InstructionIterator;
+import ghidra.program.model.listing.Listing;
+import ghidra.program.model.listing.Program;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.code.SarifCodeWriter;
+
+public class CodeSarifMgr extends SarifMgr implements DisassemblerMessageListener {
+
+ public static String KEY = "CODE";
+ public static String SUBKEY = "Code";
+ public static String SUBKEY2 = "Override";
+
+ public CodeSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+
+ AddressSet set = new AddressSet();
+
+ try {
+ getLocations(result, set);
+ } catch (AddressOverflowException e) {
+ log.appendException(e);
+ }
+
+ String msg = (String) result.get("Message");
+
+ AddressSet disset = set.intersect(program.getMemory());
+ if (!disset.equals(set)) {
+ log.appendMsg("Disassembly address set changed to " + disset.toString());
+ }
+ disassemble(disset, monitor);
+
+ if (msg.equals(SUBKEY2)) {
+ Instruction inst = program.getListing().getInstructionAt(set.getMinAddress());
+ String override = (String) result.get("kind");
+ inst.setFlowOverride(FlowOverride.valueOf(override));
+ }
+ return true;
+ }
+
+ public void disassemble(AddressSet set, TaskMonitor monitor) {
+ Disassembler disassembler = Disassembler.getDisassembler(program, monitor, this);
+ try {
+ Listing listing = program.getListing();
+ while (!set.isEmpty() && !monitor.isCancelled()) {
+ Address start = set.getMinAddress();
+ AddressSet disset = disassembler.disassemble(start, set);
+ if (disset.isEmpty()) {
+ Instruction instr = listing.getInstructionAt(start);
+ if (instr == null) {
+ AddressRange skipRange = set.iterator().next();
+ log.appendMsg("Expected valid Instruction at " + start);
+ log.appendMsg("...skipping code range " + skipRange.getMinAddress() + " to "
+ + skipRange.getMaxAddress());
+ set.delete(skipRange);
+ } else {
+ set.deleteRange(instr.getMinAddress(), instr.getMaxAddress());
+ }
+ } else {
+ set.delete(disset);
+ }
+ }
+ } catch (Exception e) {
+ log.appendMsg("Error during disassembly: " + e.getMessage());
+ }
+ }
+
+ @Override
+ public void disassembleMessageReported(String msg) {
+ log.appendMsg("Error from disassembler: " + msg);
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing CODE ...");
+
+ List<AddressRange> request = new ArrayList<>();
+ List<Pair<Instruction, FlowOverride>> requestOverride = new ArrayList<>();
+ InstructionIterator it = null;
+ if (set == null) {
+ it = program.getListing().getInstructions(true);
+ } else {
+ it = program.getListing().getInstructions(set, true);
+ }
+
+ while (it.hasNext()) {
+ Instruction inst = it.next();
+ Address start = inst.getMinAddress();
+ Address end = inst.getMaxAddress();
+ while (it.hasNext()) {
+ inst = it.next();
+ FlowOverride override = inst.getFlowOverride();
+ if (!override.equals(FlowOverride.NONE)) {
+ requestOverride.add(new Pair<Instruction, FlowOverride>(inst, override));
+ }
+
+ if (!end.isSuccessor(inst.getMinAddress())) {
+ request.add(new AddressRangeImpl(start, end));
+ start = inst.getMinAddress();
+ }
+ end = inst.getMaxAddress();
+ monitor.checkCancelled();
+ }
+ request.add(new AddressRangeImpl(start, end));
+ }
+
+ writeAsSARIF(request, requestOverride, results);
+ }
+
+ public static void writeAsSARIF(List<AddressRange> request, List<Pair<Instruction, FlowOverride>> request2,
+ JsonArray results) throws IOException {
+ SarifCodeWriter writer = new SarifCodeWriter(request, request2, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/CommentsSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/CommentsSarifMgr.java
new file mode 100644
index 0000000000..5d69f48f82
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/CommentsSarifMgr.java
@@ -0,0 +1,217 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import generic.stl.Pair;
+import ghidra.app.util.CommentTypes;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressIterator;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.program.model.listing.CodeUnitIterator;
+import ghidra.program.model.listing.Instruction;
+import ghidra.program.model.listing.Program;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.comments.SarifCommentWriter;
+
+/**
+ * SARIF manager for all types of comments.
+ */
+public class CommentsSarifMgr extends SarifMgr {
+
+ public static String KEY = "COMMENTS";
+ public static String SUBKEY = "Comment";
+
+ public static int[] COMMENT_TYPES;
+ public static String[] COMMENT_TAGS;
+
+ public static String[] COMMENT_TAGS2 = { "end-of-line", "pre", "post", "plate", "repeatable" };
+
+ static {
+ COMMENT_TYPES = CommentTypes.getTypes();
+
+ COMMENT_TAGS = new String[COMMENT_TYPES.length];
+ for (int i = 0; i < COMMENT_TAGS.length; i++) {
+
+ switch (COMMENT_TYPES[i]) {
+ case CodeUnit.PRE_COMMENT:
+ COMMENT_TAGS[i] = "pre";
+ break;
+ case CodeUnit.POST_COMMENT:
+ COMMENT_TAGS[i] = "post";
+ break;
+ case CodeUnit.EOL_COMMENT:
+ COMMENT_TAGS[i] = "end-of-line";
+ break;
+ case CodeUnit.PLATE_COMMENT:
+ COMMENT_TAGS[i] = "plate";
+ break;
+ case CodeUnit.REPEATABLE_COMMENT:
+ COMMENT_TAGS[i] = "repeatable";
+ break;
+ }
+ }
+ }
+
+ CommentsSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ /**
+ * Process the entry point section of the SARIF file.
+ *
+ * @param result sarif reader
+ * @param monitor monitor that can be canceled
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ processComment(result, result);
+ return true;
+ }
+
+ private void processComment(Map<String, Object> result, Map<String, Object> result2) throws AddressFormatException {
+ try {
+ Address addr = getLocation(result);
+ String typeStr = (String) result.get("kind");
+ String comment = (String) result.get("value");
+ boolean standard = (boolean) result.get("standard");
+ int commentType = getCommentType(typeStr);
+ if (commentType < 0) {
+ log.appendMsg("Unknown comment type: " + typeStr);
+ return;
+ }
+ if (standard) {
+ CodeUnit cu = listing.getCodeUnitContaining(addr);
+ if (cu != null) {
+ // if a comment already exists, then merge...
+ //
+ String currCmt = cu.getComment(commentType);
+ if (currCmt == null || currCmt.length() == 0) {
+ cu.setComment(commentType, comment);
+ } else if (currCmt.indexOf(comment) < 0) {
+ log.appendMsg("Merged " + typeStr + " comment at " + addr);
+ cu.setComment(commentType, currCmt + "\n" + comment);
+ }
+ }
+ } else {
+ String currCmt = listing.getComment(commentType, addr);
+ if (currCmt == null || currCmt.length() == 0) {
+ listing.setComment(addr, commentType, comment);
+ } else if (currCmt.indexOf(comment) < 0) {
+ log.appendMsg("Merged " + typeStr + " comment at " + addr);
+ listing.setComment(addr, commentType, currCmt + "\n" + comment);
+ }
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ private int getCommentType(String typeStr) {
+ for (int i = 0; i < COMMENT_TAGS.length; i++) {
+ if (COMMENT_TAGS[i].equals(typeStr)) {
+ return COMMENT_TYPES[i];
+ }
+ }
+ return -1; // unknown comment
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the external entry points.
+ *
+ * @param results writer for SARIF
+ * @param set address set that is either the entire program or a selection
+ * @param monitor monitor that can be canceled should be written
+ * @throws IOException
+ */
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing COMMENTS ...");
+
+ if (set == null) {
+ set = program.getMemory();
+ }
+
+ List<Pair<CodeUnit, Pair<String, String>>> request0 = new ArrayList<>();
+ CodeUnitIterator iter = listing.getCodeUnitIterator(CodeUnit.COMMENT_PROPERTY, set, true);
+
+ while (iter.hasNext()) {
+ monitor.checkCancelled();
+ CodeUnit cu = iter.next();
+ for (int i = 0; i < COMMENT_TYPES.length; i++) {
+ String[] comments = cu.getCommentAsArray(COMMENT_TYPES[i]);
+ for (String c : comments) {
+ Pair<String, String> pair = new Pair<>(COMMENT_TAGS[i], c);
+ request0.add(new Pair<CodeUnit, Pair<String, String>>(cu, pair));
+ }
+ }
+ }
+
+ writeAsSARIF0(request0, results);
+
+ List<Pair<Address, Pair<String, String>>> request1 = new ArrayList<>();
+ for (int i = 0; i < COMMENT_TAGS2.length; i++) {
+ AddressIterator aiter = listing.getCommentAddressIterator(i, set, true);
+ while (aiter.hasNext()) {
+ monitor.checkCancelled();
+ Address a = aiter.next();
+ CodeUnit cu = listing.getCodeUnitContaining(a);
+ if (cu instanceof Instruction && !a.equals(cu.getMinAddress())) {
+ String c = listing.getComment(i, a);
+ Pair<String, String> pair = new Pair<>(COMMENT_TAGS2[i], c);
+ request1.add(new Pair<Address, Pair<String, String>>(a, pair));
+ }
+ }
+ }
+
+ writeAsSARIF1(request1, results);
+
+ }
+
+ public static void writeAsSARIF0(List<Pair<CodeUnit, Pair<String, String>>> request, JsonArray results)
+ throws IOException {
+ SarifCommentWriter writer = new SarifCommentWriter(request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+ public static void writeAsSARIF1(List<Pair<Address, Pair<String, String>>> request, JsonArray results)
+ throws IOException {
+ SarifCommentWriter writer = new SarifCommentWriter(null, request);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/DataTypesSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/DataTypesSarifMgr.java
new file mode 100644
index 0000000000..86b5d49140
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/DataTypesSarifMgr.java
@@ -0,0 +1,708 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.xml.sax.SAXParseException;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.docking.settings.Settings;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.data.ArrayDataType;
+import ghidra.program.model.data.CategoryPath;
+import ghidra.program.model.data.CharDataType;
+import ghidra.program.model.data.Composite;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.data.DataTypeConflictHandler;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.Dynamic;
+import ghidra.program.model.data.Enum;
+import ghidra.program.model.data.EnumDataType;
+import ghidra.program.model.data.FunctionDefinition;
+import ghidra.program.model.data.FunctionDefinitionDataType;
+import ghidra.program.model.data.InvalidDataTypeException;
+import ghidra.program.model.data.LongDoubleDataType;
+import ghidra.program.model.data.PascalString255DataType;
+import ghidra.program.model.data.PascalStringDataType;
+import ghidra.program.model.data.PascalUnicodeDataType;
+import ghidra.program.model.data.PointerDataType;
+import ghidra.program.model.data.Structure;
+import ghidra.program.model.data.StructureDataType;
+import ghidra.program.model.data.TypeDef;
+import ghidra.program.model.data.TypedefDataType;
+import ghidra.program.model.data.Union;
+import ghidra.program.model.data.UnionDataType;
+import ghidra.program.model.data.UnsignedInteger3DataType;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.util.InvalidNameException;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.exception.InvalidInputException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.data.SarifDataTypeWriter;
+
+/**
+ * This manager is responsible for reading and writing datatypes in SARIF.
+ */
+public class DataTypesSarifMgr extends SarifMgr {
+
+ public static String KEY = "DATATYPE";
+
+ private final static int MAX_PASSES = 10;
+ private final static int DEFAULT_SIZE = 1;
+
+ private static Map<String, DataType> foreignTypedefs = Map.of("ascii", CharDataType.dataType, "string1",
+ PascalString255DataType.dataType, "string2", PascalStringDataType.dataType,
+ // string4 - pascal string with 4-byte length prefix
+ "unicode2", PascalUnicodeDataType.dataType,
+ // unicode4 - pascal unicode string with 4-byte length prefix
+ "tbyte", LongDoubleDataType.dataType, // 10-byte float
+ // oword - 16-byte value
+ // packed real
+ "3byte", UnsignedInteger3DataType.dataType);
+
+ private DataTypeManager dataManager;
+ private DtParser dtParser;
+ private Map<String, DataType> dataTypes = new HashMap<>();
+ private Map<String, Boolean> isPacked = new HashMap<>();
+ private Map<String, Integer> packingValue = new HashMap<>();
+
+ /**
+ * Constructs a new root types SARIF manager.
+ *
+ * @param dataManager the root type manager to read from or write to
+ * @param log the message log for recording datatype warnings
+ */
+ public DataTypesSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ this.dataManager = program.getListing().getDataTypeManager();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ protected void readResults(List<Map<String, Object>> list, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ if (list != null) {
+ monitor.setMessage("Processing " + key + "...");
+ boolean processedAll;
+
+ int pass = 0;
+ do {
+ monitor.setMaximum(list.size() * MAX_PASSES);
+ monitor.checkCancelled();
+ processedAll = true;
+ for (Map<String, Object> result : list) {
+ if (monitor.isCancelled()) {
+ break;
+ }
+ boolean res = read(result, options, monitor);
+ // if (!res) System.err.println(result);
+ processedAll &= res;
+ monitor.increment();
+ }
+ pass++;
+ } while (!processedAll && pass < MAX_PASSES);
+ } else {
+ monitor.setMessage("Skipping over " + key + " ...");
+ }
+ }
+
+ /**
+ * Reads the datatypes encoded in SARIF from the specified SARIF parser and
+ * recreates them in a datatype manager.
+ *
+ * @param result the SARIF parser
+ * @param monitor the task monitor
+ * @throws SAXParseException if an SARIF parse error occurs
+ * @throws CancelledException if the user cancels the read operation
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ try {
+ dtParser = new DtParser(dataManager);
+ return process(result);
+ } finally {
+ dataManager.close();
+ dtParser = null;
+ }
+ }
+
+ private boolean process(Map<String, Object> result) {
+ String name = (String) result.get("Message");
+
+ try {
+ if (name.equals("DT.Struct")) {
+ return processStructure(result);
+ }
+ if (name.equals("DT.Union")) {
+ return processUnion(result);
+ }
+ if (name.equals("DT.Enum")) {
+ return processEnum(result);
+ }
+ if (name.equals("DT.Typedef")) {
+ return processTypeDef(result);
+ }
+ if (name.equals("DT.TypedObject")) {
+ return processTypedObject(result);
+ }
+ if (name.equals("DT.Builtin")) {
+ return processBuiltin(result);
+ }
+ if (name.equals("DT.Function")) {
+ return processFunctionDef(result);
+ }
+ log.appendMsg("Unrecognized datatype tag: " + name);
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ return true;
+ }
+
+ public void addDataType(String key, DataType dt) {
+ Boolean packed = isPacked.get(key);
+ if (packed != null) {
+ Composite composite = (Composite) dt;
+ composite.setPackingEnabled(packed);
+ Integer packVal = packingValue.get(key);
+ if (packVal != null) {
+ composite.setExplicitPackingValue(packVal);
+ }
+ }
+ dataTypes.put(key, dt);
+ dataManager.addDataType(dt, DataTypeConflictHandler.REPLACE_HANDLER);
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processFunctionDef(Map<String, Object> result) throws InvalidInputException {
+ boolean processedAll = true;
+ String name = (String) result.get("name");
+ CategoryPath path = getCategoryPath(result);
+ FunctionDefinition fd = new FunctionDefinitionDataType(path, name, dataManager);
+ processSettings(result, fd.getDefaultSettings());
+ fd.setVarArgs((boolean) result.get("hasVarArgs"));
+ fd.setNoReturn((boolean) result.get("hasNoReturn"));
+ fd.setCallingConvention((String) result.get("callingConventionName"));
+
+ dataTypes.put(getPath(fd), fd);
+
+ Map<String, Object> retType = (Map<String, Object>) result.get("retType");
+ DataType returnType = findDataType(retType);
+ if (returnType != null) {
+ fd.setReturnType(returnType);
+ }
+
+ List<Map<String, Object>> params = (List<Map<String, Object>>) result.get("params");
+ for (Map<String, Object> param : params) {
+ processedAll &= processFunctionMembers(param, fd);
+ }
+
+ addDataType(getPath(fd), fd);
+ return processedAll;
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processEnum(Map<String, Object> result) {
+ String name = (String) result.get("name");
+ String enuumComment = (String) result.get("comment");
+ CategoryPath cp = getCategoryPath(result);
+ int size = (int) (double) result.get("size");
+
+ EnumDataType enuum = new EnumDataType(cp, name, size, dataManager);
+ processSettings(result, enuum.getDefaultSettings());
+
+ enuum.setDescription(enuumComment);
+ Map<String, Object> constants = (Map<String, Object>) result.get("constants");
+ for (Entry<String, Object> entry : constants.entrySet()) {
+ processEnumMembers(entry, enuum);
+ }
+
+ addDataType(getPath(enuum), enuum);
+ return true;
+ }
+
+ private boolean processTypeDef(Map<String, Object> result) throws InvalidNameException, DuplicateNameException {
+ String name = (String) result.get("name");
+ // String displayName = (String) result.get("displayName");
+ Boolean isAutoNamed = (Boolean) result.get("autoNamed");
+ String typeLoc = (String) result.get("typeLocation");
+ CategoryPath cp = typeLoc == null ? CategoryPath.ROOT : new CategoryPath(typeLoc);
+ DataType dt = findDataType(result);
+ if (dt == null) {
+ log.appendMsg(name + " NOT FOUND");
+ return false;
+ }
+
+ int dtSize = dt.getLength();
+ int size = (int) (double) result.get("size");
+ if (size != -1 && size != dtSize) {
+ log.appendMsg("SIZE=" + result.get("size") + " specified on type-def " + name
+ + " does not agree with length of datatype " + dt.getPathName() + " (" + dtSize + ")");
+ }
+
+ cp = getCategoryPath(result);
+ TypeDef td = new TypedefDataType(cp, name, dt, dataManager);
+ processSettings(result, td.getDefaultSettings());
+ if (isAutoNamed != null && isAutoNamed) {
+ td.enableAutoNaming();
+ }
+ // if (displayName != null) {
+ // td.setName(displayName);
+ // }
+
+ try {
+ if (name.equals(dt.getPathName()) && dt instanceof TypeDef) {
+ td = (TypeDef) dt;
+ }
+ td.setCategoryPath(cp);
+ dt = td;
+ } catch (DuplicateNameException e) {
+ log.appendMsg("Unable to place typedef '" + name + "' in category '" + cp + "'");
+ }
+
+ addDataType(getPath(td), td);
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processStructure(Map<String, Object> result) throws InvalidDataTypeException {
+ String name = (String) result.get("name");
+ CategoryPath path = getCategoryPath(result);
+ int size = DEFAULT_SIZE;
+ if (result.get("size") != null) {
+ size = (int) (double) result.get("size");
+ }
+ Structure struct = new StructureDataType(path, name, size, dataManager);
+ processSettings(result, struct.getDefaultSettings());
+
+ String comment = getRegularComment(result);
+ if (comment != null) {
+ struct.setDescription(comment);
+ }
+ Object alignmentMin = result.get("explicitMinimumAlignment");
+ if (alignmentMin != null) {
+ struct.setExplicitMinimumAlignment((int) (double) alignmentMin);
+ }
+
+ String packing = (String) result.get("packed");
+ if (packing != null) {
+ isPacked.put(getPath(struct), Boolean.valueOf(packing));
+ // NB: not this - struct.setPackingEnabled(Boolean.valueOf(packing));
+ Object epval = result.get("explicitPackingValue");
+ if (epval != null) {
+ packingValue.put(getPath(struct), (int) (double) epval);
+ }
+ }
+
+ dataTypes.put(getPath(struct), struct);
+
+ boolean processedAll = true;
+ Map<String, Object> fields = (Map<String, Object>) result.get("fields");
+ for (Entry<String, Object> entry : fields.entrySet()) {
+ processedAll &= processStructMembers(entry, struct);
+ }
+ if (processedAll) {
+ addDataType(getPath(struct), struct);
+ } else {
+ dataTypes.put(getPath(struct), struct);
+ }
+ return processedAll;
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processUnion(Map<String, Object> result) throws InvalidDataTypeException {
+ String name = (String) result.get("name");
+ CategoryPath path = getCategoryPath(result);
+ String comment = getRegularComment(result);
+ Union union = new UnionDataType(path, name);
+ processSettings(result, union.getDefaultSettings());
+
+ if (comment != null) {
+ union.setDescription(comment);
+ }
+ Object alignmentMin = result.get("explicitMinimumAlignment");
+ if (alignmentMin != null) {
+ union.setExplicitMinimumAlignment((int) (double) alignmentMin);
+ }
+
+ String packing = (String) result.get("packed");
+ if (packing != null) {
+ isPacked.put(getPath(union), Boolean.valueOf(packing));
+ // NB: not this - struct.setPackingEnabled(Boolean.valueOf(packing));
+ Object epval = result.get("explicitPackingValue");
+ if (epval != null) {
+ packingValue.put(getPath(union), (int) (double) epval);
+ }
+ }
+
+ dataTypes.put(getPath(union), union);
+
+ boolean processedAll = true;
+ Map<String, Object> fields = (Map<String, Object>) result.get("fields");
+ for (Entry<String, Object> entry : fields.entrySet()) {
+ processedAll &= processUnionMembers(entry, union);
+ }
+ if (processedAll) {
+ addDataType(getPath(union), union);
+ } else {
+ dataTypes.put(getPath(union), union);
+ }
+ return processedAll;
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processTypedObject(Map<String, Object> result) {
+ // String name = (String) result.get("name");
+ CategoryPath cp = new CategoryPath((String) result.get("typeLocation"));
+ String kind = (String) result.get("kind");
+ int size = (int) (double) result.get("size");
+
+ Map<String, Object> type = (Map<String, Object>) result.get("type");
+ DataType baseType = findDataType(type, cp);
+ if (baseType != null) {
+ if (kind.equals("pointer")) {
+ DataType p = new PointerDataType(baseType, size, dataManager);
+ addDataType(getPath(p), p);
+ return true;
+ }
+ throw new RuntimeException("Unexpected baseType kind=" + kind);
+ }
+ return false;
+ }
+
+ private boolean processBuiltin(Map<String, Object> result) {
+ String name = (String) result.get("name");
+ CategoryPath cp = getCategoryPath(result);
+ DataType dt = findDataType(result, cp);
+ if (dt != null) {
+ addDataType(getPath(cp, name), dt);
+ return true;
+ }
+ return false;
+ }
+
+ private String getRegularComment(Map<String, Object> result) {
+ return (String) result.get("comment");
+ }
+
+ private boolean processFunctionMembers(Map<String, Object> param, FunctionDefinition fn) {
+ DataType dt = findDataType(param);
+ if (dt != null) {
+ int ordinal = (int) (double) param.get("ordinal");
+ String name = (String) param.get("name");
+ String comment = (String) param.get("comment");
+ int size = dt.getLength();
+ if (size <= 0) {
+ size = (int) (double) param.get("size");
+ }
+ fn.replaceArgument(ordinal, name, dt, comment, SourceType.USER_DEFINED);
+ return true;
+ }
+ return false;
+ }
+
+ private void processEnumMembers(Entry<String, Object> entry, Enum enuum) {
+ String entryName = (String) entry.getKey();
+ Long entryValue = (long) (double) entry.getValue();
+ enuum.add(entryName, entryValue, null);
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processStructMembers(Entry<String, Object> entry, Structure struct)
+ throws InvalidDataTypeException {
+ boolean processedAll = true;
+ Map<String, Object> field = (Map<String, Object>) entry.getValue();
+ int offset = (int) (double) field.get("offset");
+ Map<String, Object> type = (Map<String, Object>) field.get("type");
+ DataType memberDT = findDataType(type);
+ if (memberDT != null) {
+ processSettings(type, memberDT.getDefaultSettings());
+ if (memberDT instanceof Dynamic dynamicDT) {
+ if (!dynamicDT.canSpecifyLength()) {
+ return false;
+ }
+ } else if (memberDT.getLength() <= 0) {
+ return false;
+ }
+ String memberName = entry.getKey();
+ Boolean noFieldName = (Boolean) field.get("hasNoFieldName");
+ if (noFieldName != null && noFieldName) {
+ memberName = null;
+ }
+ String memberComment = (String) field.get("comment");
+ int compSize = (int) (double) field.get("length");
+
+ // NOTE: Size consistency checking was removed since some types are filled-out
+ // lazily and may not have there ultimate size at this point.
+
+ if (field.get("bitOffset") != null) {
+ int bitOffset = (int) (double) field.get("bitOffset");
+ int bitSize = (int) (double) field.get("bitSize");
+ // NB: we're using "insert" and the team has suggested "add" is a better choice,
+ // but, because of the multi-pass approach, I don't think I can guarantee an
+ // in-order load.
+ DataTypeComponent dtc = struct.insertBitFieldAt(offset, memberDT.getLength(), bitOffset, memberDT,
+ bitSize, memberName, memberComment);
+ processSettings(field, dtc.getDefaultSettings());
+ return processedAll;
+ }
+
+ DataTypeComponent dtc;
+ if (offset == struct.getLength()) {
+ dtc = struct.add(memberDT, compSize, memberName, memberComment);
+ } else {
+ dtc = struct.replaceAtOffset(offset, memberDT, compSize, memberName, memberComment);
+ }
+
+ processSettings(field, dtc.getDefaultSettings());
+ } else {
+ processedAll = type.get("kind").equals("pointer");
+ }
+ return processedAll;
+ }
+
+ @SuppressWarnings("unchecked")
+ private boolean processUnionMembers(Entry<String, Object> entry, Union union) throws InvalidDataTypeException {
+ boolean processedAll = true;
+ Map<String, Object> member = (Map<String, Object>) entry.getValue();
+ String memberName = (String) entry.getKey();
+ Boolean noFieldName = (Boolean) member.get("hasNoFieldName");
+ if (noFieldName != null && noFieldName) {
+ memberName = null;
+ }
+
+ Map<String, Object> type = (Map<String, Object>) member.get("type");
+ DataType memberDT = findDataType(type);
+ if (memberDT != null) {
+ processSettings(type, memberDT.getDefaultSettings());
+ String memberComment = (String) member.get("comment");
+ int dtSize = memberDT.getLength();
+ if (member.get("bitSize") != null) {
+ int bitSize = (int) (double) member.get("bitSize");
+ union.addBitField(memberDT, bitSize, memberName, memberComment);
+ return processedAll;
+ }
+ DataTypeComponent dtc = union.add(memberDT, dtSize, memberName, memberComment);
+ processSettings(member, dtc.getDefaultSettings());
+ }
+ return processedAll;
+ }
+
+ private CategoryPath getCategoryPath(Map<String, Object> result) {
+ String nameSpace = (String) result.get("location");
+ CategoryPath cp = nameSpace == null ? CategoryPath.ROOT : new CategoryPath(nameSpace);
+ return cp;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processSettings(Map<String, Object> result, Settings defaultSettings) {
+ List<Map<String, Object>> settings = (List<Map<String, Object>>) result.get("settings");
+ if (settings != null) {
+ for (Map<String, Object> map : settings) {
+ String settingName = (String) map.get("name");
+ String settingValue = (String) map.get("value");
+ if (map.get("kind").equals("long")) {
+ long val = 0;
+ try {
+ val = Long.valueOf(settingValue);
+ } catch (NumberFormatException nfe) {
+ Msg.error(this, nfe);
+ }
+ if (!settingName.equals("ptr_type")) {
+ defaultSettings.setLong(settingName, val);
+ }
+ } else {
+ defaultSettings.setString(settingName, settingValue);
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private DataType findDataType(Map<String, Object> type) {
+ String loc = (String) type.get("location");
+ if (loc == null) {
+ Map<String, Object> subtype = (Map<String, Object>) type.get("subtype");
+ if (subtype != null) {
+ loc = (String) subtype.get("location");
+ }
+ }
+ return findDataType(type, new CategoryPath(loc));
+ }
+
+ @SuppressWarnings("unchecked")
+ private DataType findDataType(Map<String, Object> type, CategoryPath cp) {
+ String kind = (String) type.get("kind");
+ String name = (String) type.get("name");
+ if (kind == null) {
+ return null;
+ }
+ if (kind.equals("pointer") || (kind.equals("array"))) {
+ DataType byName = findExistingDataType(cp, kind, name);
+ if (byName != null) {
+ if (byName instanceof FunctionDefinition) {
+ return new PointerDataType(byName, dataManager);
+ }
+ return byName;
+ }
+ Map<String, Object> subtype = (Map<String, Object>) type.get("subtype");
+ if (subtype != null) {
+ DataType base = findDataType(subtype, cp);
+ if (base == null) {
+ return null;
+ // throw new RuntimeException("Subtype not found: " + subtype);
+ }
+ if (kind.equals("pointer")) {
+ return new PointerDataType(base, dataManager);
+ } else {
+ int count = (int) (double) type.get("count");
+ return new ArrayDataType(base, count, base.getLength(), dataManager);
+ }
+ }
+ }
+ if (name == null) {
+ return null;
+ }
+ if (kind.equals("typedef")) {
+ Map<String, Object> subtype = (Map<String, Object>) type.get("type");
+ String typeName = (String) type.get("typeName");
+ if (typeName != null) {
+ cp = new CategoryPath((String) type.get("typeLocation"));
+ name = typeName;
+ } else if (subtype != null) {
+ DataType base = findDataType(subtype);
+ if (base == null) {
+ return null;
+ }
+ return base;
+ }
+ }
+ if (kind.equals("bitfield")) {
+ Map<String, Object> subtype = (Map<String, Object>) type.get("type");
+ String typeName = (String) type.get("typeName");
+ if (typeName != null) {
+ cp = new CategoryPath((String) type.get("typeLocation"));
+ name = typeName;
+ } else if (subtype != null) {
+ DataType base = findDataType(subtype);
+ if (base == null) {
+ return null;
+ }
+ return base;
+ }
+ }
+ return findExistingDataType(cp, kind, name);
+ }
+
+ private DataType findExistingDataType(CategoryPath cp, String kind, String name) {
+ DataType dt = dtParser.parseDataType(name, cp, -1);
+ if (dt == null && addForeignTypedefIfNeeded(name)) {
+ dt = dtParser.parseDataType(name, cp, -1);
+ }
+ if (dt != null) {
+ return dt;
+ }
+ dt = dataTypes.get(getPath(cp, name));
+ if (dt == null && kind.equals("typedef")) {
+ dt = dataTypes.get(cp + "/functions/" + name);
+ }
+ if (dt != null) {
+ return dt;
+ }
+ return dataTypes.get("/" + name);
+ }
+
+ private boolean addForeignTypedefIfNeeded(String dtName) {
+ int ptrIndex = dtName.indexOf('*');
+ int index = dtName.indexOf('[');
+ String baseName = dtName.trim();
+ if (index < 0 || index > ptrIndex) {
+ index = ptrIndex;
+ }
+ if (index > 0) {
+ baseName = dtName.substring(0, index).trim();
+ }
+ DataType ourType = foreignTypedefs.get(baseName);
+ if (ourType != null && dataManager.getDataType("/" + baseName) == null) {
+ TypedefDataType newTypedef = new TypedefDataType(CategoryPath.ROOT, baseName, ourType, dataManager);
+ dataManager.resolve(newTypedef, null);
+ return true;
+ }
+ return false;
+ }
+
+ private String getPath(DataType dt) {
+ String displayName = dt.getPathName();
+ String path = dt.getCategoryPath().getPath();
+ if (!path.equals("/")) {
+ path += "/";
+ }
+ return path + displayName;
+ }
+
+ private String getPath(CategoryPath cp, String displayName) {
+ String path = cp.getPath();
+ if (!path.equals("/")) {
+ path += "/";
+ }
+ return path + displayName;
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Writes datatypes into SARIF using the specified SARIF writer.
+ *
+ * @param results the SARIF writer
+ * @param monitor the task monitor
+ * @throws CancelledException if the user cancels the write operation
+ */
+ public void write(JsonArray results, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing DATA TYPES ...");
+
+ List<DataType> dataTypeList = new ArrayList<>();
+ dataManager.getAllDataTypes(dataTypeList);
+
+ writeAsSARIF(program, dataTypeList, results);
+ }
+
+ public static void writeAsSARIF(Program program, List<DataType> dataTypeList, JsonArray results)
+ throws IOException {
+ SarifDataTypeWriter writer = new SarifDataTypeWriter(program.getDataTypeManager(), dataTypeList, null);
+ new TaskLauncher(new SarifWriterTask("DataTypes", writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/DefinedDataSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/DefinedDataSarifMgr.java
new file mode 100644
index 0000000000..9052d8e427
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/DefinedDataSarifMgr.java
@@ -0,0 +1,224 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.docking.settings.Settings;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.CategoryPath;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeInstance;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.program.model.listing.CodeUnitIterator;
+import ghidra.program.model.listing.Data;
+import ghidra.program.model.listing.DataIterator;
+import ghidra.program.model.listing.Listing;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.DumbMemBufferImpl;
+import ghidra.program.model.util.CodeUnitInsertionException;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.dd.SarifDataWriter;
+
+public class DefinedDataSarifMgr extends SarifMgr {
+
+ public static String KEY = "DEFINED_DATA";
+ public static String SUBKEY = "Defined Data";
+
+ DefinedDataSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+
+ Listing listing = program.getListing();
+ DataTypeManager dataManager = program.getDataTypeManager();
+
+ try {
+ DtParser dtParser = new DtParser(dataManager);
+
+ AddressSet set = getLocations(result, null);
+ Address addr = set.getMinAddress();
+ if (addr == null) {
+ return false;
+ }
+ int size = (int) set.getMaxAddress().subtract(addr) + 1;
+
+ String dataTypeName = (String) result.get("typeName");
+ String typeLocation = (String) result.get("typeLocation");
+ CategoryPath path = typeLocation != null ? new CategoryPath(typeLocation) : CategoryPath.ROOT;
+
+ DataType dt = dtParser.parseDataType(dataTypeName, path, size);
+ if (dt == null) {
+ if (dataTypeName.contains(" ")) {
+ dataTypeName = dataTypeName.substring(0, dataTypeName.indexOf(" "));
+ dt = dtParser.parseDataType(dataTypeName, path, size);
+ }
+ }
+ if (dt == null) {
+ log.appendMsg("Defined root: unknown datatype: " + dataTypeName + " in category: " + path);
+ return false;
+ }
+
+ try {
+ if (options == null || options.isOverwriteDataConflicts()) {
+ clearExistingData(addr, size, dt, listing);
+ }
+
+ Data data = listing.createData(addr, dt, size);
+ processSettings(result, data);
+ Map<String, Object> comments = (Map<String, Object>) result.get("nested");
+ processComment(comments, data);
+
+ } catch (CodeUnitInsertionException e) {
+ Data d = listing.getDefinedDataAt(addr);
+ if (d == null || !d.getDataType().isEquivalent(dt)) {
+ log.appendMsg(e.getMessage());
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ } catch (AddressOverflowException e1) {
+ log.appendException(e1);
+ }
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processSettings(Map<String, Object> res, Data data) {
+ List<Map<String, Object>> settingsMap = (List<Map<String, Object>>) res.get("settings");
+ if (settingsMap == null) {
+ return;
+ }
+ for (Map<String, Object> map : settingsMap) {
+ Settings settings = data.getDefaultSettings();
+ if (map != null) {
+ String name = (String) map.get("name");
+ String kind = (String) map.get("kind");
+ String value = (String) map.get("value");
+ Object existing = settings.getValue(name);
+ if (existing == null || !existing.toString().equals(value)) {
+ settings.setValue(name, kind.equals("long") ? Long.parseLong(value) : value);
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processComment(Map<String, Object> c, Data data) {
+ if (c == null || c.isEmpty()) {
+ return;
+ }
+ List<Map<String, Object>> localComments = (List<Map<String, Object>>) c.get("comment");
+ if (localComments != null) {
+ for (Map<String, Object> lmap : localComments) {
+ String comment = (String) lmap.get("comment");
+ int type = (int) (double) lmap.get("commentType");
+ String existing = data.getComment(type);
+ if (existing == null || !existing.equals(comment)) {
+ data.setComment(type, comment);
+ }
+ }
+ }
+ List<Map<String, Object>> localSettings = (List<Map<String, Object>>) c.get("setting");
+ if (localSettings != null) {
+ for (Map<String, Object> lmap : localSettings) {
+ String name = (String) lmap.get("name");
+ String kind = (String) lmap.get("kind");
+ String value = (String) lmap.get("value");
+ Object existing = data.getValue(name);
+ if (existing == null || !existing.toString().equals(value)) {
+ data.setValue(name, kind.equals("long") ? Long.parseLong(value) : value);
+ }
+ }
+ }
+ Map<String, Map<String, Object>> embedded = (Map<String, Map<String, Object>>) c.get("embedded");
+ if (embedded != null) {
+ for (String index : embedded.keySet()) {
+ Data component = data.getComponent(Integer.parseInt(index));
+ Map<String, Object> c2 = embedded.get(index);
+ processComment(c2, component);
+ }
+ }
+ }
+
+ private void clearExistingData(Address addr, int size, DataType dt, Listing listing) {
+ DumbMemBufferImpl buf = new DumbMemBufferImpl(program.getMemory(), addr);
+ DataTypeInstance dti = DataTypeInstance.getDataTypeInstance(dt, buf, size, false);
+ if (dti != null) {
+ boolean doClear = false;
+ Address maxAddr = addr.add(dti.getLength() - 1);
+ CodeUnitIterator codeUnits = listing.getCodeUnitIterator(CodeUnit.DEFINED_DATA_PROPERTY, new AddressSet(addr, maxAddr), true);
+ while (codeUnits.hasNext()) {
+ CodeUnit cu = codeUnits.next();
+ if (cu instanceof Data) {
+ if (((Data) cu).isDefined()) {
+ doClear = true;
+ }
+ } else {
+ return; // don't clear instructions
+ }
+ }
+ if (doClear) {
+ listing.clearCodeUnits(addr, maxAddr, false);
+ }
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView addrset, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing DATA ...");
+
+ List<Data> request = new ArrayList<>();
+ Listing listing = program.getListing();
+ DataIterator iter = listing.getDefinedData(addrset, true);
+ while (iter.hasNext()) {
+ request.add(iter.next());
+ }
+
+ writeAsSARIF(program, request, results);
+ }
+
+ public static void writeAsSARIF(Program program, List<Data> request, JsonArray results) throws IOException {
+ SarifDataWriter writer = new SarifDataWriter(request, null);
+ new TaskLauncher(new SarifWriterTask("DefinedData", writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/DtParser.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/DtParser.java
new file mode 100644
index 0000000000..55b3e5023b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/DtParser.java
@@ -0,0 +1,55 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import ghidra.program.model.data.CategoryPath;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.InvalidDataTypeException;
+import ghidra.util.data.DataTypeParser;
+import ghidra.util.data.DataTypeParser.AllowedDataTypes;
+import ghidra.util.exception.CancelledException;
+
+/**
+ * DtParser
+ */
+class DtParser {
+
+ private DataTypeParser parser;
+
+ DtParser(DataTypeManager dtManager) {
+ this.parser = new DataTypeParser(dtManager, dtManager, null, AllowedDataTypes.DYNAMIC);
+ }
+
+ /**
+ * Parse the specified dtName within the specified category.
+ * @param dtName
+ * @param category
+ * @param size optional root-type size, or -1 for unspecified
+ * @return root-type if found, or null if not found
+ */
+ DataType parseDataType(String dtName, CategoryPath category, int size) {
+ DataType dt;
+ try {
+ dt = parser.parse(dtName, category);
+ }
+ catch (InvalidDataTypeException | CancelledException e) {
+ return null;
+ }
+ return dt;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/EquatesSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/EquatesSarifMgr.java
new file mode 100644
index 0000000000..2a1fa98209
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/EquatesSarifMgr.java
@@ -0,0 +1,118 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.Equate;
+import ghidra.program.model.symbol.EquateTable;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.equates.SarifEquateWriter;
+
+/**
+ * SARIF manager for Equates.
+ */
+public class EquatesSarifMgr extends SarifMgr {
+
+ public static String KEY = "EQUATES";
+ public static String SUBKEY = "Equate";
+
+ private EquateTable equateTable;
+
+ EquatesSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ this.equateTable = program.getEquateTable();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ /**
+ * Process the entry point section of the SARIF file.
+ *
+ * @param result sarif reader
+ * @param monitor monitor that can be canceled
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ processEquate(result, monitor);
+ return true;
+ }
+
+ private void processEquate(Map<String, Object> result, TaskMonitor monitor) {
+ String name = (String) result.get("name");
+ long value = (long) (double) result.get("value");
+
+ try {
+ equateTable.createEquate(name, value);
+ } catch (DuplicateNameException e) {
+ Equate eq = equateTable.getEquate(name);
+ long prevVal = eq.getValue();
+ if (prevVal != value) {
+ log.appendMsg("Cannot create equate [" + name + "] with value [" + value
+ + "]; previously defined with value [" + prevVal + "]");
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the Equates.
+ *
+ * @param results writer for SARIF
+ * @param set address set that is either the entire program or a selection
+ * @param monitor monitor that can be canceled should be written
+ * @throws IOException
+ */
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing EQUATES ...");
+
+ List<Equate> request = new ArrayList<>();
+ Iterator<Equate> iter = equateTable.getEquates();
+ while (iter.hasNext()) {
+ request.add(iter.next());
+ }
+
+ writeAsSARIF(request, results);
+ }
+
+ public static void writeAsSARIF(List<Equate> request, JsonArray results) throws IOException {
+ SarifEquateWriter writer = new SarifEquateWriter(request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/ExtEntryPointSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ExtEntryPointSarifMgr.java
new file mode 100644
index 0000000000..9b4f605801
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ExtEntryPointSarifMgr.java
@@ -0,0 +1,106 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressIterator;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.SymbolTable;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.ep.SarifEntryPointWriter;
+
+/**
+ * SARIF manager for External Entry Points.
+ */
+public class ExtEntryPointSarifMgr extends SarifMgr {
+
+ public static String KEY = "ENTRY_POINTS";
+ public static String SUBKEY = "Entry Point";
+
+ private SymbolTable symbolTable;
+
+ ExtEntryPointSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ symbolTable = program.getSymbolTable();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ /**
+ * Process the entry point section of the SARIF file.
+ *
+ * @param result sarif
+ * @param monitor monitor that can be canceled
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ try {
+ Address addr = getLocation(result);
+ symbolTable.addExternalEntryPoint(addr);
+ return true;
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ return false;
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the external entry points.
+ *
+ * @param results writer for SARIF
+ * @param set address set that is either the entire program or a selection
+ * @param monitor monitor that can be canceled should be written
+ * @throws IOException
+ */
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing ENTRY POINTS ...");
+
+ List<Address> request = new ArrayList<>();
+ AddressIterator iter = symbolTable.getExternalEntryPointIterator();
+ while (iter.hasNext()) {
+ request.add(iter.next());
+ }
+
+ writeAsSARIF(request, results);
+ }
+
+ public static void writeAsSARIF(List<Address> request, JsonArray results) throws IOException {
+ SarifEntryPointWriter writer = new SarifEntryPointWriter(request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/ExternalLibSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ExternalLibSarifMgr.java
new file mode 100644
index 0000000000..0b3f776017
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ExternalLibSarifMgr.java
@@ -0,0 +1,235 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.listing.GhidraClass;
+import ghidra.program.model.listing.Library;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalManager;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.exception.InvalidInputException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.extlib.SarifClassesNamespaceWriter;
+import sarif.export.extlib.SarifExternalLibraryWriter;
+
+/**
+ * SARIF for external library table for resolved external references.
+ */
+public class ExternalLibSarifMgr extends SarifMgr {
+
+ public static String KEY = "EXT_LIBRARY";
+ public static String SUBKEY0 = "External.Library";
+ public static String SUBKEY1 = "External.Location";
+
+ private ExternalManager extManager;
+ private boolean libraries = true;
+
+ ExternalLibSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ extManager = program.getExternalManager();
+ externalMap = new HashMap<>();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ protected void readResults(List<Map<String, Object>> list, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ if (list != null) {
+ monitor.setMessage("Processing " + key + "...");
+ monitor.setMaximum(list.size() * 2);
+
+ for (Map<String, Object> result : list) {
+ monitor.checkCancelled();
+ read(result, options, monitor);
+ monitor.increment();
+ }
+ libraries = false;
+ for (Map<String, Object> result : list) {
+ monitor.checkCancelled();
+ read(result, options, monitor);
+ monitor.increment();
+ }
+
+ } else {
+ monitor.setMessage("Skipping over " + key + " ...");
+ }
+ }
+
+ /**
+ * Process the entry point section of the SARIF file.
+ *
+ * @param result sarif reader
+ * @param monitor monitor that can be canceled
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ try {
+ String symbol = (String) result.get("symbol");
+ if (libraries && symbol == null) {
+ processExternalLib(result);
+ return true;
+ }
+ if (!libraries && symbol != null) {
+ processExternalLocation(result);
+ return true;
+ }
+ return false;
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ return false;
+ }
+
+ private void processExternalLib(Map<String, Object> result) throws InvalidInputException, DuplicateNameException {
+
+ String progName = (String) result.get("name");
+ // check to make sure that we do not clear any
+ // external refs that have already been resolved
+ Library library = extManager.getExternalLibrary(progName);
+ if (library != null) {
+ return; // already has a value--don't override it
+ }
+
+ // NB: Can't use "DEFAULT" here or result.get("sourceType") which may be
+ // "DEFAULT"
+ String source = (String) result.get("sourceType");
+ SourceType sourceType = getSourceType(source);
+ if (sourceType.equals(SourceType.DEFAULT)) {
+ sourceType = SourceType.IMPORTED;
+ }
+ library = extManager.addExternalLibraryName(progName, sourceType);
+ }
+
+ private void processExternalLocation(Map<String, Object> result) throws IOException {
+
+ try {
+ String name = (String) result.get("name");
+ String location = (String) result.get("location");
+ Address address = getLocation(result);
+ String extern = (String) result.get("externalAddress");
+ String source = (String) result.get("source");
+ SourceType sourceType = getSourceType(source);
+ if (sourceType.equals(SourceType.DEFAULT)) {
+ sourceType = SourceType.IMPORTED;
+ }
+ boolean isClass = (boolean) result.get("isClass");
+ Namespace p = walkNamespace(program.getGlobalNamespace(), location + "::", address, sourceType, isClass);
+ String name0 = (String) result.get("originalImportedName");
+
+ ExternalLocation loc = addExternal(result, name, address, sourceType, p, name0);
+ externalMap.put(extern, loc);
+ } catch (InvalidInputException | AddressOverflowException e) {
+ log.appendException(e);
+ }
+ }
+
+ private ExternalLocation addExternal(Map<String, Object> result, String name, Address address, SourceType sourceType,
+ Namespace p, String name0) throws InvalidInputException {
+ Library lib = getLibrary(p);
+ ExternalLocation loc;
+ if ((boolean) result.get("isFunction")) {
+ if (name0 != null) {
+ loc = extManager.addExtFunction(lib == null ? p : lib, name0, address, sourceType, false);
+ loc.setName(p, name, sourceType);
+ } else {
+ loc = extManager.addExtFunction(p, name, address, sourceType, true);
+ }
+ } else {
+ if (name0 != null) {
+ loc = extManager.addExtLocation(lib == null ? p : lib, name0, address, sourceType, false);
+ loc.setName(p, name, sourceType);
+ } else {
+ loc = extManager.addExtLocation(p, name, address, sourceType, true);
+ }
+ }
+ return loc;
+ }
+
+ private Library getLibrary(Namespace p) {
+ if (p instanceof Library lib) {
+ return lib;
+ }
+ Namespace parent = p.getParentNamespace();
+ return parent == null ? null : getLibrary(parent);
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the external library table.
+ *
+ * @param results writer for SARIF
+ * @param monitor monitor that can be canceled should be written
+ * @throws IOException
+ */
+ void write(JsonArray results, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing EXTERNAL LIBRARIES ...");
+
+ String[] externalNames = extManager.getExternalLibraryNames();
+ List<String> request0 = new ArrayList<>();
+ for (String name : externalNames) {
+ request0.add(name);
+ }
+
+ writeExtAsSARIF(program, request0, results);
+
+ Iterator<GhidraClass> classNamespaces = program.getSymbolTable().getClassNamespaces();
+ List<GhidraClass> request1 = new ArrayList<>();
+ while (classNamespaces.hasNext()) {
+ request1.add(classNamespaces.next());
+ }
+
+ writeNamespaceAsSARIF(program, request1, results);
+
+ }
+
+ public static void writeExtAsSARIF(Program program, List<String> request, JsonArray results) throws IOException {
+ SarifExternalLibraryWriter writer = new SarifExternalLibraryWriter(program.getExternalManager(), request, null);
+ new TaskLauncher(new SarifWriterTask("Libraries", writer, results), null);
+ }
+
+ public static void writeNamespaceAsSARIF(Program program, List<GhidraClass> request, JsonArray results) throws IOException {
+ SarifClassesNamespaceWriter writer = new SarifClassesNamespaceWriter(program.getExternalManager(), program.getSymbolTable(), request, null);
+ new TaskLauncher(new SarifWriterTask("Libraries", writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java
new file mode 100644
index 0000000000..2c56988a65
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java
@@ -0,0 +1,668 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.swing.Icon;
+
+import com.google.gson.JsonArray;
+
+import generic.theme.GIcon;
+import generic.theme.GThemeDefaults.Colors.Palette;
+import ghidra.app.cmd.function.CreateFunctionCmd;
+import ghidra.app.util.NamespaceUtils;
+import ghidra.app.util.SymbolPath;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.database.function.OverlappingFunctionException;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.BuiltInDataTypeManager;
+import ghidra.program.model.data.CategoryPath;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.DataTypeManager;
+import ghidra.program.model.data.Undefined;
+import ghidra.program.model.lang.Register;
+import ghidra.program.model.listing.BookmarkManager;
+import ghidra.program.model.listing.BookmarkType;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.Function.FunctionUpdateType;
+import ghidra.program.model.listing.FunctionIterator;
+import ghidra.program.model.listing.Library;
+import ghidra.program.model.listing.LocalVariableImpl;
+import ghidra.program.model.listing.ParameterImpl;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.listing.ProgramContext;
+import ghidra.program.model.listing.ReturnParameterImpl;
+import ghidra.program.model.listing.StackFrame;
+import ghidra.program.model.listing.Variable;
+import ghidra.program.model.listing.VariableStorage;
+import ghidra.program.model.listing.VariableUtilities;
+import ghidra.program.model.pcode.Varnode;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolTable;
+import ghidra.program.model.symbol.SymbolType;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.exception.InvalidInputException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.func.SarifFunctionWriter;
+
+public class FunctionsSarifMgr extends SarifMgr {
+
+ public static String KEY = "FUNCTIONS";
+
+ public final static String LIB_BOOKMARK_CATEGORY = "Library Identification";
+ public final static String FID_BOOKMARK_CATEGORY = "Function ID Analyzer";
+ public static final Set<String> LIBRARY_BOOKMARK_CATEGORY_STRINGS = Set.of(LIB_BOOKMARK_CATEGORY,
+ FID_BOOKMARK_CATEGORY);
+
+ private DtParser dtParser;
+ private Library extenalNamespace;
+
+ FunctionsSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ int txId = program.startTransaction("SARIF FunctionMgr");
+ try {
+ SymbolTable symbolTable = program.getSymbolTable();
+ Symbol extLib = symbolTable.getLibrarySymbol("<EXTERNAL>");
+ if (extLib == null) {
+ extenalNamespace = symbolTable.createExternalLibrary(Library.UNKNOWN, SourceType.IMPORTED);
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ } finally {
+ program.endTransaction(txId, true);
+ }
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ protected void readResults(List<Map<String, Object>> list, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ if (list != null) {
+ monitor.setMessage("Processing " + key + "...");
+ monitor.setMaximum(list.size() * 2);
+ firstPass = true;
+ for (Map<String, Object> result : list) {
+ monitor.checkCancelled();
+ read(result, options, monitor);
+ monitor.increment();
+ }
+ firstPass = false;
+ for (Map<String, Object> result : list) {
+ monitor.checkCancelled();
+ read(result, options, monitor);
+ monitor.increment();
+ }
+ monitor.incrementProgress();
+ } else {
+ monitor.setMessage("Skipping over " + key + " ...");
+ }
+ }
+
+ /**
+ * Parses a list of {@link Function function} definitions from SARIF and creates
+ * then, adding them to the current {@link Program program}.
+ * <p>
+ * Information from a TYPEINFO_COMMENT is used in preference to information from
+ * RETURN_TYPE and STACK_FRAME/STACK_VARs that are tagged as parameters.
+ * <p>
+ * DTD for the FUNCTION element:
+ *
+ * <pre>
+ * <code>
+ * &lt;!ELEMENT FUNCTION (RETURN_TYPE?, ADDRESS_RANGE*, REGULAR_CMT?, REPEATABLE_CMT?, TYPEINFO_CMT?, STACK_FRAME?, REGISTER_VAR*)&gt;
+ * </code>
+ * </pre>
+ * <p>
+ *
+ * @param result the parser
+ * @param overwriteConflicts true to overwrite any conflicts
+ * @param monitor the task monitor
+ * @throws AddressFormatException if any address is not parsable
+ * @throws CancelledException if the operation is cancelled through the
+ * monitor
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+
+ DataTypeManager dataManager = listing.getDataTypeManager();
+ BuiltInDataTypeManager builtInMgr = BuiltInDataTypeManager.getDataTypeManager();
+
+ try {
+ dtParser = new DtParser(dataManager);
+
+ try {
+ String key = (String) result.get("Message");
+ boolean isThunk = (boolean) result.get("isThunk");
+ boolean process = (firstPass && !isThunk) || (!firstPass && isThunk);
+ if (!key.equals("Function") || !process) {
+ return true;
+ }
+
+ Address entryPoint = getEntryPoint(result);
+ // Grab the original symbols
+ Symbol[] symbols = program.getSymbolTable().getSymbols(entryPoint);
+
+ Function func = createFunction(result, isThunk, entryPoint);
+
+ // Restore the original labels
+ for (Symbol symbol : symbols) {
+ SourceType srcType = symbol.getSource();
+ if (!srcType.equals(SourceType.DEFAULT)) {
+ program.getSymbolTable().createLabel(entryPoint, symbol.getName(true), srcType);
+ }
+ }
+
+ String source = (String) result.get("sourceType");
+ SourceType sourceType = source.equals("DEFAULT") ? SourceType.IMPORTED : getSourceType(source);
+ String typeInfoComment = setProperties(result, func, entryPoint);
+
+ // Process stack
+ List<Variable> stackParams = new ArrayList<>();
+ List<Map<String, Object>> regVars = (List<Map<String, Object>>) result.get("regVars");
+ for (Map<String, Object> var : regVars) {
+ readRegisterVars(var, func, stackParams);
+ }
+
+ Map<String, Object> stack = (Map<String, Object>) result.get("stack");
+ readStackFrame(stack, func, stackParams, sourceType);
+
+ List<Map<String, Object>> parms = (List<Map<String, Object>>) result.get("params");
+ List<Variable> formalParams = new ArrayList<>();
+ for (Map<String, Object> p : parms) {
+ readParameter(p, func, formalParams);
+ }
+
+ // Process formal parameters and return
+ Map<String, Object> ret = (Map<String, Object>) result.get("ret");
+ ReturnParameterImpl retImpl = null;
+ if (ret != null) {
+ retImpl = readReturnType(ret, func);
+ }
+ List<Variable> plist = stackParams;
+ if (typeInfoComment != null) {
+ plist = formalParams;
+ }
+
+ if (plist != null) {
+ updateFunction(func, retImpl, plist, sourceType);
+ }
+
+ postProcess(result, func, stack);
+
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ } finally {
+ builtInMgr.close();
+ }
+ return true;
+ }
+
+ private Address getEntryPoint(Map<String, Object> result) throws AddressFormatException {
+ String entryPointStr = (String) result.get("location");
+ if (entryPointStr == null) {
+ throw new RuntimeException("No entry point provided.");
+ }
+ Address entryPoint = parseAddress(factory, entryPointStr);
+ if (entryPoint == null) {
+ throw new AddressFormatException("Incompatible Function Entry Point Address: " + entryPointStr);
+ }
+ return entryPoint;
+ }
+
+ private Function createFunction(Map<String, Object> result, boolean isThunk, Address entryPoint)
+ throws InvalidInputException, OverlappingFunctionException, DuplicateNameException,
+ AddressOverflowException {
+ AddressSet body = new AddressSet(entryPoint, entryPoint);
+ getLocations(result, body);
+ Function func = program.getFunctionManager().getFunctionAt(entryPoint);
+ if (func == null) {
+ String source = (String) result.get("sourceType");
+ SourceType sourceType = getSourceType(source);
+ func = program.getFunctionManager().createFunction(null, null, entryPoint, body, sourceType);
+ }
+
+ String name = (String) result.get("name");
+ if (isThunk) {
+ createThunk(result, func);
+ }
+ setName(entryPoint, func, name, result);
+ return func;
+ }
+
+ private void setName(Address entryPoint, Function func, String name, Map<String, Object> result) {
+ SymbolPath path = new SymbolPath(name);
+ if (name != null) {
+ String nss = (String) result.get("namespace");
+ boolean isThunk = (boolean) result.get("isThunk");
+ if (nss != null && !isThunk) {
+ SymbolPath parentPath = new SymbolPath(nss);
+ if (!name.startsWith(nss)) {
+ path = new SymbolPath(parentPath, name);
+ }
+ }
+ name = path.getName();
+ }
+
+ Symbol symbol = func.getSymbol();
+ if (path != null) {
+ try {
+ Namespace ns = NamespaceUtils.getFunctionNamespaceAt(program, path, entryPoint);
+ if (ns == null) {
+ ns = program.getGlobalNamespace();
+ SymbolPath parent = path.getParent();
+ if (parent != null && !parent.getName().equals(ns.getName())) {
+ Boolean isClass = (Boolean) result.get("namespaceIsClass");
+ String source = (String) result.get("sourceType");
+ SourceType sourceType = source.equals("DEFAULT") ? SourceType.IMPORTED : getSourceType(source);
+ ns = walkNamespace(program.getGlobalNamespace(), parent.getPath() + "::", entryPoint,
+ sourceType, isClass);
+ symbol.setNameAndNamespace(name, ns, getSourceType("DEFAULT"));
+ return;
+ }
+ }
+ if (path != null && path.getName().contains(Library.UNKNOWN)) {
+ ns = extenalNamespace;
+ }
+ if (ns.getParentNamespace() == null) {
+ symbol.setName(name, getSourceType("DEFAULT"));
+ } else {
+ symbol.setNameAndNamespace(name, ns.getParentNamespace(), getSourceType("DEFAULT")); // symbol.getSource());
+ }
+ } catch (Exception e) {
+ // name may already be set if symbols were loaded...
+ }
+ }
+ }
+
+ private String setProperties(Map<String, Object> result, Function func, Address entryPoint)
+ throws InvalidInputException {
+ boolean isLibrary = (boolean) result.get("isLibrary");
+ if (isLibrary) {
+ BookmarkManager bm = program.getBookmarkManager();
+ BookmarkType bt = bm.getBookmarkType("IMPORTED");
+ if (bt == null) {
+ Icon icon = new GIcon("icon.base.util.xml.functions.bookmark");
+ bt = bm.defineType("IMPORTED", icon, Palette.DARK_GRAY, 0);
+ }
+ bm.setBookmark(entryPoint, "IMPORTED", LIB_BOOKMARK_CATEGORY, "Library function");
+ }
+
+ String callingConvention = (String) result.get("callingConvention");
+ if (callingConvention != null) {
+ func.setCallingConvention(callingConvention);
+ }
+
+ boolean hasVarArgs = (boolean) result.get("hasVarArgs");
+ boolean isInline = (boolean) result.get("isInline");
+ boolean hasNoReturn = (boolean) result.get("hasNoReturn");
+ boolean hasCustomStorage = (boolean) result.get("hasCustomStorage");
+ func.setVarArgs(hasVarArgs);
+ func.setInline(isInline);
+ func.setNoReturn(hasNoReturn);
+ func.setCustomVariableStorage(hasCustomStorage);
+
+ String regularComment = (String) result.get("comment");
+ func.setComment(regularComment);
+ String repeatableComment = (String) result.get("repeatableComment");
+ func.setRepeatableComment(repeatableComment);
+ String typeInfoComment = (String) result.get("value");
+ return typeInfoComment;
+ }
+
+ private void updateFunction(Function func, ReturnParameterImpl retImpl, List<Variable> plist,
+ SourceType sourceType) {
+ try {
+ Variable[] arr = new Variable[plist.size()];
+ int i = 0;
+ for (Variable variable : plist) {
+ arr[i++] = variable;
+ }
+ FunctionUpdateType type = func.hasCustomVariableStorage() ? FunctionUpdateType.CUSTOM_STORAGE
+ : FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS;
+ func.updateFunction(func.getCallingConventionName(), retImpl, type, true, SourceType.IMPORTED, arr);
+ } catch (DuplicateNameException e) {
+ log.appendMsg("Could not set name of a parameter in function: " + funcDesc(func) + ": " + e.getMessage());
+ } catch (InvalidInputException iie) {
+ log.appendMsg("Bad parameter definition in function: " + funcDesc(func) + ": " + iie.getMessage());
+ }
+ }
+
+ private void postProcess(Map<String, Object> result, Function func, Map<String, Object> stack) {
+ String signatureSource = (String) result.get("signatureSource");
+ if (signatureSource != null) {
+ SourceType signatureSourceType = getSourceType(signatureSource);
+ func.setSignatureSource(signatureSourceType);
+ }
+
+ Boolean purgeValid = (Boolean) result.get("isStackPurgeSizeValid");
+ Double purgeSize = (Double) stack.get("purgeSize");
+ if (purgeSize != null) {
+ if (purgeValid == null) {
+ func.setStackPurgeSize((int) (double) purgeSize);
+ } else {
+ func.setStackPurgeSize(purgeValid ? (int) (double) purgeSize : Function.UNKNOWN_STACK_DEPTH_CHANGE);
+ }
+ }
+ }
+
+ private void createThunk(Map<String, Object> result, Function func)
+ throws InvalidInputException, DuplicateNameException {
+ String thunkStr = (String) result.get("thunkAddress");
+ if (thunkStr == null) {
+ throw new RuntimeException("No thunk address provided.");
+ }
+ Function thunkFn = null;
+ Address thunk = parseAddress(factory, thunkStr);
+ if (thunk.isExternalAddress()) {
+ Symbol symbol = externalMap.get(thunkStr).getSymbol();
+ if (symbol.getSymbolType() == SymbolType.FUNCTION) {
+ thunkFn = (Function) symbol.getObject();
+ func.setThunkedFunction(thunkFn);
+ }
+ } else {
+ thunkFn = program.getFunctionManager().getFunctionAt(thunk);
+ if (thunkFn == null) {
+ CreateFunctionCmd cmd = new CreateFunctionCmd(thunk);
+ if (!cmd.applyTo(program)) {
+ Msg.error(this, "Failed to create function at " + thunk + ": " + cmd.getStatusMsg());
+ }
+ thunkFn = cmd.getFunction();
+ }
+ func.setThunkedFunction(thunkFn);
+ }
+ }
+
+ private void addLocalVar(Function function, Variable v, SourceType sourceType, boolean overwriteConflicts) throws InvalidInputException {
+ VariableUtilities.checkVariableConflict(function, v, v.getVariableStorage(), overwriteConflicts);
+
+ try {
+ function.addLocalVariable(v, sourceType);
+ } catch (DuplicateNameException e) {
+ log.appendMsg("Could not add local variable to function " + funcDesc(function) + ": " + v.getName() + ": "
+ + e.getMessage());
+ }
+ }
+
+ private static String funcDesc(Function func) {
+ return func.getName() + "[" + func.getEntryPoint().toString() + "]";
+ }
+
+ private DataType findDataType(Map<String, Object> result) {
+ String name = (String) result.get("name");
+ if (name == null) {
+ return DataType.DEFAULT;
+ }
+ CategoryPath cp = new CategoryPath((String) result.get("location"));
+ Double size = (Double) result.get("size");
+ return dtParser.parseDataType(name, cp, size == null ? -1 : (int) (double) size);
+ }
+
+ @SuppressWarnings("unchecked")
+ private void readStackFrame(Map<String, Object> result, Function func, List<Variable> stackParams, SourceType sourceType) {
+ if (func == null) {
+ return;
+ }
+ StackFrame frame = func.getStackFrame();
+
+ Double localVarSize = (Double) result.get("localVarSize");
+ if (localVarSize != null) {
+ frame.setLocalSize((int) (double) localVarSize);
+ }
+// Double paramOffset = (Double) result.get("parameterOffset");
+// if (paramOffset != null) {
+// frame.setParameterOffset((int) (double) paramOffset));
+// }
+ Double retOffset = (Double) result.get("returnAddressOffset");
+ if (retOffset != null) {
+ frame.setReturnAddressOffset((int) (double) retOffset);
+ }
+// Double purgeSize = (Double) result.get("purgeSize");
+// if (purgeSize != null && validPurge) {
+// func.setStackPurgeSize((int) (double) purgeSize);
+// }
+ List<Map<String, Object>> stackVars = (List<Map<String, Object>>) result.get("stackVars");
+ for (Map<String, Object> var : stackVars) {
+ readVariable(var, func, stackParams, sourceType);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void readVariable(Map<String, Object> result, Function function, List<Variable> stackParams, SourceType sourceType) {
+
+ int offset = (int) (double) result.get("offset");
+ int size = (int) (double) result.get("size");
+
+ Map<String, Object> type = (Map<String, Object>) result.get("type");
+ DataType dt = findDataType(type);
+ if (dt == null) {
+ log.appendMsg("Missing datatype: " + type.get("name"));
+ dt = Undefined.getUndefinedDataType(size);
+ }
+
+ String name = (String) result.get("name");
+ if (name != null) {
+ name = getUniqueVarName(function, name, offset);
+ }
+
+ try {
+ Variable var = new LocalVariableImpl(name, dt, offset, program);
+ VariableUtilities.checkVariableConflict(function, var, var.getVariableStorage(), true);
+
+ StackFrame stackFrame = function.getStackFrame();
+ boolean isParameter = stackFrame.isParameterOffset(offset);
+ if (!isParameter) {
+ isParameter = stackFrame.isParameterOffset(offset + size - 1);
+ }
+ if (isParameter) {
+ var = new ParameterImpl(name, dt, offset, program);
+ stackParams.add(var);
+ } else {
+ var = new LocalVariableImpl(name, dt, offset, program);
+ addLocalVar(function, var, sourceType, true);
+ }
+ String regularComment = (String) result.get("comment");
+ var.setComment(regularComment);
+ } catch (InvalidInputException e) {
+ log.appendException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private DataType readParameter(Map<String, Object> result, Function function, List<Variable> formalParams) {
+
+ String name = (String) result.get("name");
+ // int ordinal = (int) (double) result.get("ordinal");
+ int size = (int) (double) result.get("size");
+ String regularComment = (String) result.get("comment");
+
+ Map<String, Object> type = (Map<String, Object>) result.get("formalType");
+ DataType dt = findDataType(type);
+ if (dt == null) {
+ log.appendMsg("Missing datatype: " + type.get("name"));
+ dt = Undefined.getUndefinedDataType(size);
+ }
+
+ try {
+ ParameterImpl var;
+
+ ProgramContext context = program.getProgramContext();
+ List<String> rnames = (List<String>) result.get("registers");
+ if (rnames != null) {
+ if (formalParams != null) {
+ for (String r : rnames) {
+ Register register = context.getRegister(r);
+ var = new ParameterImpl(name, dt, register, program);
+ var.setComment(regularComment);
+ formalParams.add(var);
+ }
+ }
+ return dt;
+ }
+
+ int offset = (int) (double) result.get("stackOffset");
+ if (formalParams != null) {
+ var = new ParameterImpl(name, dt, offset, program);
+ var.setComment(regularComment);
+ formalParams.add(var);
+ }
+ return dt;
+ } catch (InvalidInputException e) {
+ log.appendException(e);
+ return null;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private ReturnParameterImpl readReturnType(Map<String, Object> result, Function function) {
+
+ int size = (int) (double) result.get("size");
+ int offset = (int) (double) result.get("stackOffset");
+
+ Map<String, Object> type = (Map<String, Object>) result.get("formalType");
+ DataType dt = findDataType(type);
+ if (dt == null) {
+ log.appendMsg("Missing datatype: " + type.get("name"));
+ dt = Undefined.getUndefinedDataType(size);
+ }
+
+ try {
+ List<String> rnames = (List<String>) result.get("registers");
+ if (rnames != null) {
+ List<Varnode> vnodes = convertRegisterListToVarnodeStorage(rnames, dt.getLength(), offset);
+ VariableStorage returnStorage = new VariableStorage(program, vnodes);
+ return new ReturnParameterImpl(dt, returnStorage, true, program);
+ }
+
+ if (offset >= 0) {
+ return new ReturnParameterImpl(dt, offset, program);
+ }
+ return new ReturnParameterImpl(dt, program);
+ } catch (InvalidInputException e) {
+ log.appendException(e);
+ return null;
+ }
+ }
+
+ private String getUniqueVarName(Function function, String name, int offset) {
+ Symbol s = program.getSymbolTable().getVariableSymbol(name, function);
+ if (s == null) {
+ return name;
+ }
+ SymbolType st = s.getSymbolType();
+ if (st == SymbolType.LOCAL_VAR || st == SymbolType.PARAMETER) {
+ Variable v = (Variable) s.getObject();
+ if (v.isStackVariable() && offset == v.getStackOffset()) {
+ return name;
+ }
+ }
+ return name + "_" + offset;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void readRegisterVars(Map<String, Object> result, Function function, List<Variable> stackParams) {
+ try {
+ ProgramContext context = program.getProgramContext();
+ String name = (String) result.get("name");
+ String registerName = (String) result.get("register");
+ if (registerName == null) {
+ return;
+ }
+ Register register = context.getRegister(registerName);
+
+ Map<String, Object> type = (Map<String, Object>) result.get("type");
+ DataType dt = findDataType(type);
+ if (dt != null && dt.getLength() > register.getMinimumByteSize()) {
+ log.appendMsg("Data type [" + result.get("type") + "] too large for register [" + registerName + "]");
+ dt = null;
+ }
+
+ Variable registerParam = new ParameterImpl(name, dt, register, program);
+ String comment = (String) result.get("comment");
+ registerParam.setComment(comment);
+
+ stackParams.add(registerParam);
+ } catch (InvalidInputException e) {
+ log.appendException(e);
+ } catch (IllegalArgumentException e) {
+ log.appendException(e);
+ }
+ }
+
+ public List<Varnode> convertRegisterListToVarnodeStorage(List<String> registNames, int dataTypeSize,
+ int stackOffset) {
+ List<Varnode> results = new ArrayList<>();
+ ProgramContext context = program.getProgramContext();
+ for (String rname : registNames) {
+ Register reg = context.getRegister(rname);
+ int regSize = reg.getMinimumByteSize();
+ int bytesUsed = Math.min(dataTypeSize, regSize);
+ Address addr = reg.getAddress();
+ if (reg.isBigEndian() && bytesUsed < regSize) {
+ addr = addr.add(regSize - bytesUsed);
+ }
+ results.add(new Varnode(addr, bytesUsed));
+ dataTypeSize -= bytesUsed;
+ }
+ if (dataTypeSize != 0 && stackOffset >= 0) {
+ results.add(new Varnode(program.getAddressFactory().getStackSpace().getAddress(stackOffset), dataTypeSize));
+ }
+ return results;
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView addrs, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing FUNCTIONS ...");
+
+ List<Function> request = new ArrayList<>();
+ FunctionIterator iter = listing.getFunctions(addrs, true);
+ while (iter.hasNext()) {
+ request.add(iter.next());
+ }
+
+ writeAsSARIF(program, request, results);
+ }
+
+ public static void writeAsSARIF(Program program, List<Function> request, JsonArray results) throws IOException {
+ SarifFunctionWriter writer = new SarifFunctionWriter(program.getFunctionManager(), request, null);
+ new TaskLauncher(new SarifWriterTask("Functions", writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/MarkupSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/MarkupSarifMgr.java
new file mode 100644
index 0000000000..4bd9d6b518
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/MarkupSarifMgr.java
@@ -0,0 +1,549 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressIterator;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.lang.Register;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.program.model.listing.Instruction;
+import ghidra.program.model.listing.Library;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.scalar.Scalar;
+import ghidra.program.model.symbol.Equate;
+import ghidra.program.model.symbol.EquateTable;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalManager;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.RefType;
+import ghidra.program.model.symbol.RefTypeFactory;
+import ghidra.program.model.symbol.Reference;
+import ghidra.program.model.symbol.ReferenceManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.util.Msg;
+import ghidra.util.exception.AssertException;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.exception.InvalidInputException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.ref.SarifEquateRefWriter;
+import sarif.export.ref.SarifReferenceWriter;
+
+/**
+ * SARIF manager for all references ("markup" for operand substitution).
+ */
+public class MarkupSarifMgr extends SarifMgr {
+
+ public static String KEY = "REFERENCES";
+
+ private ReferenceManager refManager;
+ private EquateTable equateTable;
+
+ MarkupSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ refManager = program.getReferenceManager();
+ equateTable = program.getEquateTable();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ String tagName = (String) result.get("Message");
+ boolean overwrite = options == null || options.isOverwriteReferenceConflicts();
+ if (tagName.equals("Ref.Memory")) {
+ processMemoryReference(result, overwrite);
+ } else if (tagName.equals("Ref.Shifted")) {
+ processShiftedReference(result, overwrite);
+ } else if (tagName.equals("Ref.Register")) {
+ processRegisterReference(result, overwrite);
+ } else if (tagName.equals("Ref.Stack")) {
+ if (options == null || options.isFunctions()) { // TODO && !ignoreStackReferences) {
+ processStackReference(result, overwrite);
+ }
+ } else if (tagName.equals("Ref.External")) {
+ if (options == null || options.isExternalLibraries()) {
+ processExtLibraryReference(result, overwrite);
+ }
+ } else if (tagName.equals("Ref.Equate")) {
+ processEquateReference(result, overwrite);
+ }
+ return true;
+ }
+
+ private RefType getRefType(int type) {
+ return RefTypeFactory.get((byte) type);
+ }
+
+ /**
+ * @param result
+ * @param language
+ * @param overwrite
+ */
+ private void processMemoryReference(Map<String, Object> result, boolean overwrite) {
+ try {
+ Address fromAddr = getLocation(result);
+ if (fromAddr == null) {
+ throw new AddressFormatException("Incompatible Memory Reference FROM Address");
+ }
+
+ String toAddrStr = (String) result.get("to");
+ if (toAddrStr == null) {
+ throw new RuntimeException("TO_ADDRESS attribute missing for MEMORY_REFERENCE element");
+ }
+ Address toAddr = parseAddress(factory, toAddrStr);
+ if (toAddr == null) {
+ throw new AddressFormatException("Incompatible Memory Reference TO Address: " + toAddrStr);
+ }
+
+ int opIndex = CodeUnit.MNEMONIC;
+ if (result.get("opIndex") != null) {
+ opIndex = (int) (double) result.get("opIndex");
+ }
+
+ boolean primary = false;
+ if (result.get("primary") != null) {
+ primary = (boolean) result.get("primary");
+ }
+
+ Address baseAddr = null;
+ if (result.get("base") != null) {
+ baseAddr = parseAddress(factory, (String) result.get("base"));
+ }
+
+ if (!overwrite) {
+ Reference[] existingMemRefs = refManager.getReferencesFrom(fromAddr, opIndex);
+ if (existingMemRefs != null && existingMemRefs.length != 0 && !existingMemRefs[0].isMemoryReference()) {
+ log.appendMsg("Reference already exists from [" + fromAddr + "] to [" + toAddr
+ + "] on operand [" + opIndex + "]");
+ return;
+ }
+ }
+
+ int index = Integer.parseInt((String) result.get("index"));
+ RefType refType = getRefType(index);
+
+ SourceType sourceType = getSourceType((String) result.get("sourceType"));
+ ReferenceManager refMgr = program.getReferenceManager();
+ Reference ref;
+ if (baseAddr != null) {
+ long offset = (long) (double) result.get("offset");
+ // long offset = toAddr.subtract(baseAddr);
+ ref = refMgr.addOffsetMemReference(fromAddr, toAddr, toAddr.equals(baseAddr), offset, refType,
+ sourceType, opIndex);
+ } else {
+ ref = refMgr.addMemoryReference(fromAddr, toAddr, refType, sourceType, opIndex);
+ }
+ refMgr.setPrimary(ref, primary);
+
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ /**
+ * @param result
+ * @param language
+ * @param overwrite
+ */
+ private void processRegisterReference(Map<String, Object> result, boolean overwrite) {
+ try {
+ Address fromAddr = getLocation(result);
+ if (fromAddr == null) {
+ throw new AddressFormatException("Incompatible Memory Reference FROM Address");
+ }
+
+ String toAddrStr = (String) result.get("to");
+ if (toAddrStr == null) {
+ throw new RuntimeException("TO_ADDRESS attribute missing for REGISTER_REFERENCE element");
+ }
+ Address toAddr = parseAddress(factory, toAddrStr);
+ if (toAddr == null) {
+ throw new AddressFormatException("Incompatible Register Reference TO Address: " + toAddrStr);
+ }
+ Register[] registers = program.getLanguage().getRegisters(toAddr);
+ Register reg = registers[0];
+
+ int opIndex = CodeUnit.MNEMONIC;
+ if (result.get("opIndex") != null) {
+ opIndex = (int) (double) result.get("opIndex");
+ }
+
+ boolean primary = false;
+ if (result.get("primary") != null) {
+ primary = (boolean) result.get("primary");
+ }
+
+ if (!overwrite) {
+ Reference[] existingRefs = refManager.getReferencesFrom(fromAddr, opIndex);
+ if (existingRefs != null && existingRefs.length != 0) {
+ log.appendMsg("Memory reference already existed from [" + fromAddr + "] to [" + toAddr
+ + "] on operand [" + opIndex + "]");
+ return;
+ }
+ }
+
+ int index = Integer.parseInt((String) result.get("index"));
+ RefType refType = getRefType(index);
+
+ SourceType sourceType = getSourceType((String) result.get("sourceType"));
+ ReferenceManager refMgr = program.getReferenceManager();
+ Reference ref = refMgr.addRegisterReference(fromAddr, opIndex, reg, refType, sourceType);
+ refMgr.setPrimary(ref, primary);
+
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ private void processStackReference(Map<String, Object> result, boolean overwrite) {
+ try {
+ Address addr = getLocation(result);
+ if (addr == null) {
+ throw new AddressFormatException("Incompatible Stack Reference Address");
+ }
+ int opIndex = CodeUnit.MNEMONIC;
+ if (result.get("opIndex") != null) {
+ opIndex = (int) (double) result.get("opIndex");
+ }
+ CodeUnit cu = listing.getCodeUnitContaining(addr);
+ if (cu == null) {
+ log.appendMsg("No codeunit at " + addr);
+ return;
+ }
+ if (!overwrite) {
+ Reference[] existingRefs = refManager.getReferencesFrom(addr, opIndex);
+ if (existingRefs != null && existingRefs.length != 0) {
+ log.appendMsg("Reference already exists from [" + addr + "] on operand [" + opIndex + "]");
+ return;
+ }
+ }
+ Reference ref = cu.getPrimaryReference(opIndex);
+ if (ref != null) {
+ if (!overwrite) {
+ return;
+ }
+ }
+
+ int offset = (int) (double) result.get("offset");
+ int index = Integer.parseInt((String) result.get("index"));
+ RefType refType = getRefType(index);
+
+ SourceType sourceType = getSourceType((String) result.get("sourceType"));
+ refManager.addStackReference(addr, opIndex, offset, refType, sourceType);
+
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ private void processShiftedReference(Map<String, Object> result, boolean overwrite) {
+ try {
+ Address addr = getLocation(result);
+ if (addr == null) {
+ throw new AddressFormatException("Incompatible Shifted Reference Address");
+ }
+ int opIndex = CodeUnit.MNEMONIC;
+ if (result.get("opIndex") != null) {
+ opIndex = (int) (double) result.get("opIndex");
+ }
+ CodeUnit cu = listing.getCodeUnitContaining(addr);
+ if (cu == null) {
+ log.appendMsg("No codeunit at " + addr);
+ return;
+ }
+ if (!overwrite) {
+ Reference[] existingRefs = refManager.getReferencesFrom(addr, opIndex);
+ if (existingRefs != null && existingRefs.length != 0) {
+ log.appendMsg("Reference already exists from [" + addr + "] on operand [" + opIndex + "]");
+ return;
+ }
+ }
+ Reference ref = cu.getPrimaryReference(opIndex);
+ if (ref != null) {
+ if (!overwrite) {
+ return;
+ }
+ }
+
+ int shift = (int) (double) result.get("shift");
+ long value = (long) (double) result.get("value");
+ Address toAddr = addr.getNewAddress(value);
+ int index = Integer.parseInt((String) result.get("index"));
+ RefType refType = getRefType(index);
+
+ SourceType sourceType = getSourceType((String) result.get("sourceType"));
+ refManager.addShiftedMemReference(addr, toAddr, shift, refType, sourceType, opIndex);
+
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ private void processExtLibraryReference(Map<String, Object> result, boolean overwrite) {
+ try {
+ Address addr = getLocation(result);
+ if (addr == null) {
+ throw new AddressFormatException("Incompatible External Reference Address");
+ }
+ int opIndex = CodeUnit.MNEMONIC;
+ if (result.get("opIndex") != null) {
+ opIndex = (int) (double) result.get("opIndex");
+ }
+
+ String namespacePath = (String) result.get("name");
+ String label = (String) result.get("libLabel");
+ String libAddress = (String) result.get("libAddr");
+ Address libAddr = libAddress != null ? factory.getAddress(libAddress) : null;
+ String libExtAddress = (String) result.get("libExtAddr");
+ String origImport = (String) result.get("origImport");
+ // boolean isClass = (boolean) result.get("isClass");
+ boolean isFunction = (boolean) result.get("isFunction");
+
+ if (label == null && libAddr == null) {
+ log.appendMsg("External library reference for address " + addr
+ + " does not have a label or an external address specified. External reference will not be created");
+ return;
+ }
+ CodeUnit cu = listing.getCodeUnitContaining(addr);
+ if (cu == null) {
+ log.appendMsg("No codeunit at " + addr);
+ return;
+ }
+ Reference ref = cu.getExternalReference(opIndex);
+ if (ref != null) {
+ if (!overwrite) {
+ return;
+ }
+ program.getReferenceManager().delete(ref);
+ }
+
+ RefType refType = null;
+ String rt = (String) result.get("kind");
+ if (rt != null) {
+ int index = Integer.parseInt((String) result.get("index"));
+ refType = getRefType(index);
+ } else {
+ refType = RefType.EXTERNAL_REF;
+ }
+
+ SourceType sourceType = getSourceType((String) result.get("sourceType"));
+ ExternalLocation extLoc = externalMap.get(libExtAddress);
+ if (extLoc == null) {
+ extLoc = addExternal(label, namespacePath, libAddr, sourceType, origImport, isFunction);
+ externalMap.put(libExtAddress, extLoc);
+ }
+ if (origImport != null && !origImport.equals(extLoc.getOriginalImportedName())) {
+ log.appendMsg("Retrieving incorrect external location - known bug");
+ }
+ refManager.addExternalReference(addr, opIndex, extLoc, sourceType, refType);
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ // This seems like this should be unnecessary but external classes are not
+ // listen under ExternalLocations
+ // (Possible forcing locations to be written per-reference means this is no
+ // longer called, but...
+ private ExternalLocation addExternal(String name, String namespacePath, Address address, SourceType sourceType,
+ String name0, boolean isFunction) throws InvalidInputException, IOException {
+ Namespace p = walkNamespace(program.getGlobalNamespace(), namespacePath + "::", address, sourceType, true);
+ Library lib = getLibrary(p);
+ ExternalLocation loc;
+ ExternalManager extManager = program.getExternalManager();
+ if (isFunction) {
+ if (name0 != null) {
+ loc = extManager.addExtFunction(lib == null ? p : lib, name0, address, sourceType, false);
+ loc.setName(p, name, sourceType);
+ } else {
+ loc = extManager.addExtFunction(p, name, address, sourceType, true);
+ }
+ } else {
+ if (name0 != null) {
+ loc = extManager.addExtLocation(lib == null ? p : lib, name0, address, sourceType, false);
+ loc.setName(p, name, sourceType);
+ } else {
+ loc = extManager.addExtLocation(p, name, address, sourceType, true);
+ }
+ }
+ return loc;
+ }
+
+ private Library getLibrary(Namespace p) {
+ if (p instanceof Library lib) {
+ return lib;
+ }
+ Namespace parent = p.getParentNamespace();
+ return parent == null ? null : getLibrary(parent);
+ }
+
+ /**
+ * @param result
+ * @param language
+ * @param overwrite
+ */
+ private void processEquateReference(Map<String, Object> result, boolean overwrite) {
+ try {
+ Address addr = getLocation(result);
+ if (addr == null) {
+ throw new AddressFormatException("Incompatible Equate Reference Address");
+ }
+
+ if (listing.isUndefined(addr, addr)) {
+ log.appendMsg("BAD EQUATE REFERENCE: defined code unit not found at " + addr);
+ return;
+ }
+
+ CodeUnit cu = listing.getCodeUnitContaining(addr);
+ if (cu == null) {
+ log.appendMsg("No codeunit at " + addr);
+ return;
+ }
+
+ String equateName = (String) result.get("name");
+
+ int opIndex = CodeUnit.MNEMONIC;
+
+ List<Scalar> instrScalars = new LinkedList<Scalar>();
+ if (result.get("OpIndex") != null) {
+ opIndex = (int) (double) result.get("opIndex");
+ if (opIndex != CodeUnit.MNEMONIC) {
+ Scalar tempScalar = cu.getScalar(opIndex);
+ if (tempScalar != null) {
+ instrScalars.add(tempScalar);
+ } else if (cu instanceof Instruction) {
+ Instruction instr = (Instruction) cu;
+ Object[] opObjects = instr.getOpObjects(opIndex);
+ for (int i = 0; i < opObjects.length; i++) {
+ if (opObjects[i] instanceof Scalar) {
+ instrScalars.add((Scalar) opObjects[i]);
+ }
+ }
+
+ if (instrScalars.size() == 0) {
+ log.appendMsg("BAD EQUATE REFERENCE: operand " + "[" + opIndex + "] at address " + "["
+ + addr + "] is not a scalar.");
+ return;
+ }
+ }
+ }
+ }
+
+ long value = 0;
+ if (result.get("value") != null) {
+ value = (long) result.get("value");
+ Scalar matchingScalar = null;
+ Iterator<Scalar> itr = instrScalars.iterator();
+ while (matchingScalar == null && itr.hasNext()) {
+ matchingScalar = itr.next();
+ if (matchingScalar.getSignedValue() != value) {
+ matchingScalar = null;
+ }
+ }
+
+ if (matchingScalar == null) {
+ log.appendMsg("BAD EQUATE REFERENCE: equate [" + equateName + "] value [0x"
+ + Long.toHexString(value) + "]" + " does not match scalar on operand [" + opIndex
+ + "] at address [" + addr + "]");
+ return;
+ }
+ } else if (instrScalars.size() > 0) {
+ // use scalar value as default - seems like a bad idea
+ Msg.warn(this, "NO VALUE SPECIFIED");
+ value = instrScalars.get(0).getSignedValue();
+ } else {
+ log.appendMsg("BAD EQUATE REFERENCE: either the VALUE or OPERAND_INDEX must be specified");
+ return;
+ }
+
+ Equate equate = equateTable.getEquate(equateName);
+ if (equate != null) {
+ if (value != equate.getValue()) {
+ log.appendMsg("BAD EQUATE REFERENCE: equate [" + equateName + "] value [0x"
+ + Long.toHexString(value) + "] conflicts with existing equate value [0x"
+ + Long.toHexString(equate.getValue()) + "].");
+ return;
+ }
+ } else {
+ try {
+ equate = equateTable.createEquate(equateName, value);
+ } catch (DuplicateNameException e) {
+ throw new AssertException("Got duplicate name while creating equate " + equateName);
+ } catch (InvalidInputException e) {
+ log.appendMsg("Invalid name for equate " + equateName);
+ return;
+ }
+ }
+
+ Equate existingEquate = equateTable.getEquate(addr, opIndex, equate.getValue());
+ if (existingEquate != null && overwrite) {
+ existingEquate.removeReference(addr, opIndex);
+ }
+ equate.addReference(addr, opIndex);
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ if (set == null) {
+ set = program.getMemory();
+ }
+
+ monitor.setMessage("Exporting References...");
+ AddressIterator iter = refManager.getReferenceSourceIterator(set, true);
+ List<Address> request = new ArrayList<>();
+ while (iter.hasNext()) {
+ request.add(iter.next());
+ }
+ writeAsSARIF(program, request, results);
+ writeAsSARIF(program, set, results);
+ }
+
+ public static void writeAsSARIF(Program program, List<Address> request, JsonArray results) throws IOException {
+ SarifReferenceWriter writer = new SarifReferenceWriter(program.getReferenceManager(), request, null);
+ new TaskLauncher(new SarifWriterTask("References", writer, results), null);
+ }
+
+ public static void writeAsSARIF(Program program, AddressSetView set, JsonArray results) throws IOException {
+ SarifEquateRefWriter ewriter = new SarifEquateRefWriter(program.getEquateTable(), set, null);
+ new TaskLauncher(new SarifWriterTask("References", ewriter, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/MemoryMapBytesFile.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/MemoryMapBytesFile.java
new file mode 100644
index 0000000000..3075a14e4b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/MemoryMapBytesFile.java
@@ -0,0 +1,83 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.Memory;
+import ghidra.program.model.mem.MemoryAccessException;
+
+public class MemoryMapBytesFile {
+
+ private OutputStream os;
+ private String fileName;
+ private int bytesWritten;
+ private Memory memory;
+
+ public MemoryMapBytesFile(Program program, String fileName) throws IOException {
+ memory = program.getMemory();
+ fileName += ".bytes";
+ File file = new File(fileName);
+ this.fileName = file.getName();
+ if (file.exists()) {
+ file.delete();
+ }
+ os = new BufferedOutputStream(new FileOutputStream(file));
+ }
+
+ void close() throws IOException {
+ os.close();
+ }
+
+ public String getFileName() {
+ return fileName;
+ }
+
+ public int getOffset() {
+ return bytesWritten;
+ }
+
+ public void writeBytes(AddressRange range) throws IOException {
+ try {
+ int BUFSIZE = 32 * 1024;
+ long size = range.getLength();
+ byte[] buf = new byte[(int) Math.min(size, BUFSIZE)];
+ Address addr = range.getMinAddress();
+ int n = 0;
+ while (size > 0) {
+ addr = addr.addNoWrap(n);
+ n = memory.getBytes(addr, buf);
+ os.write(buf, 0, n);
+ bytesWritten += n;
+ size -= n;
+ }
+ } catch (AddressOverflowException e) {
+ throw new IOException(e.getMessage());
+ } catch (MemoryAccessException e) {
+ throw new IOException(e.getMessage());
+ } catch (IOException e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/MemoryMapSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/MemoryMapSarifMgr.java
new file mode 100644
index 0000000000..1bb529a561
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/MemoryMapSarifMgr.java
@@ -0,0 +1,246 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import generic.stl.Pair;
+import ghidra.app.util.MemoryBlockUtils;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.framework.store.LockException;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFactory;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeImpl;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.address.AddressSpace;
+import ghidra.program.model.address.OverlayAddressSpace;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.Memory;
+import ghidra.program.model.mem.MemoryBlock;
+import ghidra.util.InvalidNameException;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.mm.SarifMemoryMapWriter;
+
+public class MemoryMapSarifMgr extends SarifMgr {
+
+ public static String KEY = "MEMORY_MAP";
+ public static String SUBKEY = "MemorySection";
+
+ private ProgramSarifMgr programMgr;
+
+ private MemoryMapBytesFile bf;
+
+ MemoryMapSarifMgr(ProgramSarifMgr programMgr, Program program, MessageLog log) {
+ super(KEY, program, log);
+ this.programMgr = programMgr;
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ try {
+ processMemoryBlock(result, result, programMgr.getDirectory(), program, monitor);
+ return true;
+ } catch (FileNotFoundException | AddressOverflowException e) {
+ log.appendException(e);
+ }
+ return false;
+ }
+
+ private void processMemoryBlock(Map<String, Object> result, Map<String, Object> result2, String directory,
+ Program program, TaskMonitor monitor) throws FileNotFoundException, AddressOverflowException {
+
+ String name = (String) result.get("name");
+ AddressSet set = getLocations(result, null);
+ Address addr = set.getMinAddress();
+ int length = (int) set.getMaxAddress().subtract(addr) + 1;
+
+ String permissions = (String) result.get("kind");
+ if (permissions == null) {
+ permissions = "r";
+ }
+ boolean r = permissions.indexOf("r") >= 0;
+ boolean w = permissions.indexOf("w") >= 0;
+ boolean x = permissions.indexOf("x") >= 0;
+
+ boolean isVolatile = (boolean) result.get("isVolatile");
+
+ String comment = (String) result.get("comment");
+ String type = (String) result.get("type");
+ String loc = (String) result.get("location");
+ String overlayName = (String) result.get("overlaySpace");
+
+ Address blockAddress = addr;
+ if (overlayName != null) {
+ AddressSpace addressSpace = program.getAddressFactory().getAddressSpace(overlayName);
+ if (addressSpace == null) {
+ try {
+ addressSpace = program.createOverlaySpace(overlayName, addr.getAddressSpace());
+ } catch (IllegalStateException | DuplicateNameException | InvalidNameException | LockException e) {
+ throw new RuntimeException("Attempt to create " + overlayName + " failed!");
+ }
+ } else if (!addressSpace.isOverlaySpace()) {
+ throw new RuntimeException(overlayName + " is not a valid overlay space!");
+ }
+ blockAddress = ((OverlayAddressSpace) addressSpace).getAddressInThisSpaceOnly(addr.getOffset());
+ }
+
+ try {
+ byte[] bytes = new byte[length];
+ Arrays.fill(bytes, (byte) 0xff);
+ MemoryBlock block = null;
+ if (type.equals("DEFAULT") && (loc != null)) {
+ Address startAddr = addr;
+ String[] split = loc.split(":");
+ String fileName = split[0];
+ int fileOffset = Integer.parseInt(split[1]);
+ setData(bytes, (int) startAddr.subtract(addr), directory, fileName, fileOffset, length, log);
+
+ block = MemoryBlockUtils.createInitializedBlock(program, false, name, blockAddress,
+ new ByteArrayInputStream(bytes), bytes.length, comment, null, r, w, x, log, monitor);
+ } else if (type.equals("BIT_MAPPED")) {
+ Address sourceAddr = factory.getAddress(loc);
+ block = MemoryBlockUtils.createBitMappedBlock(program, name, blockAddress, sourceAddr, length, comment,
+ comment, r, w, x, false, log);
+ } else if (type.equals("BYTE_MAPPED")) {
+ Address sourceAddr = factory.getAddress(loc);
+ block = MemoryBlockUtils.createByteMappedBlock(program, name, blockAddress, sourceAddr, length, comment,
+ comment, r, w, x, false, log);
+ } else {
+ block = MemoryBlockUtils.createUninitializedBlock(program, false, name, blockAddress, length, comment,
+ null, r, w, x, log);
+ }
+ if (block != null) {
+ block.setVolatile(isVolatile);
+ }
+ } catch (FileNotFoundException e) {
+ throw e;
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ private void setData(byte[] bytes, int offset, String directory, String fileName, int fileOffset, int length,
+ MessageLog log) throws IOException {
+ File f = new File(directory, fileName);
+ RandomAccessFile binfile = new RandomAccessFile(f, "r");
+ try {
+ int pos = 0;
+ while (pos < length) {
+ int readLen = (512 * 1024);
+ if ((readLen + pos) > length) {
+ readLen = length - pos;
+ }
+ binfile.seek(fileOffset + pos);
+ readLen = binfile.read(bytes, offset + pos, readLen);
+ if (readLen <= 0) {
+ break;
+ }
+ pos += readLen;
+ }
+ } catch (IndexOutOfBoundsException e) {
+ log.appendMsg("Invalid bin file offset " + offset + " with length " + length);
+ }
+ binfile.close();
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView addrs, TaskMonitor monitor, boolean isWriteContents, String filePath)
+ throws IOException, CancelledException {
+ monitor.setMessage("Writing MEMORY MAP ...");
+
+ bf = isWriteContents ? new MemoryMapBytesFile(program, filePath) : null;
+
+ List<Pair<AddressRange, MemoryBlock>> request = new ArrayList<>();
+ AddressRangeIterator iter = addrs.getAddressRanges();
+ while (iter.hasNext()) {
+ monitor.checkCancelled();
+ AddressRange ranges = iter.next();
+ RangeBlock rb = new RangeBlock(program.getAddressFactory(), program.getMemory(), ranges);
+ for (int i = 0; i < rb.getRanges().length; ++i) {
+ AddressRange range = rb.getRanges()[i];
+ MemoryBlock block = rb.getBlocks()[i];
+ request.add(new Pair<AddressRange, MemoryBlock>(range, block));
+ }
+ }
+
+ writeAsSARIF(request, bf, isWriteContents, results);
+
+ if (isWriteContents) {
+ bf.close();
+ }
+ }
+
+ public static void writeAsSARIF(List<Pair<AddressRange, MemoryBlock>> request, MemoryMapBytesFile bytes,
+ boolean isWriteContents, JsonArray results) throws IOException {
+ SarifMemoryMapWriter writer = new SarifMemoryMapWriter(request, null, bytes, isWriteContents);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
+
+class RangeBlock {
+ private ArrayList<AddressRange> rangeList = new ArrayList<>();
+ private ArrayList<MemoryBlock> blockList = new ArrayList<>();
+
+ RangeBlock(AddressFactory af, Memory memory, AddressRange range) {
+ AddressSet set = new AddressSet(range);
+ while (!set.isEmpty()) {
+ MemoryBlock block = memory.getBlock(set.getMinAddress());
+ set.deleteRange(block.getStart(), block.getEnd());
+ rangeList.add(range.intersect(new AddressRangeImpl(block.getStart(), block.getEnd())));
+ blockList.add(block);
+ }
+ }
+
+ AddressRange[] getRanges() {
+ AddressRange[] ranges = new AddressRange[rangeList.size()];
+ rangeList.toArray(ranges);
+ return ranges;
+ }
+
+ MemoryBlock[] getBlocks() {
+ MemoryBlock[] blocks = new MemoryBlock[rangeList.size()];
+ blockList.toArray(blocks);
+ return blocks;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramInfo.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramInfo.java
new file mode 100644
index 0000000000..b3f39ab7b4
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramInfo.java
@@ -0,0 +1,141 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import ghidra.program.model.lang.CompilerSpecID;
+import ghidra.program.model.lang.LanguageID;
+
+/**
+ * This class stores values pulled from the
+ * PROGRAM, INFO_SOURCE, and LANGUAGE tag inside a ghidra program SARIF file.
+ *
+ * Please see PROGRAM.DTD
+ *
+ *
+ */
+public class ProgramInfo {
+ /**The family name of the program's processor (eg, "Intel").*/
+ public String family;
+ /**The program's processor (eg, Processor.PROCESSOR_X86).*/
+ public String processorName;
+ /**The program's language id, e.g. "x86:LE:32:default".*/
+ public LanguageID languageID;
+ /**The program's compilerSpec id, e.g. "gcc".*/
+ public CompilerSpecID compilerSpecID;
+ /**The preferred name of the Program when loaded back into Ghidra.*/
+ public String programName;
+ /**The timestamp of when the SARIF file was created.*/
+ public String timestamp;
+ /**The ID of the user that created the SARIF file.*/
+ public String user;
+ /**The tool that generated the SARIF file (eg, "Ghidra", etc.).*/
+ private String tool;
+ /**This is the name of the tool normalized into known categories ("IDA-PRO" or "GHIDRA") if appropriate.*/
+ private String normalizedExternalToolName;
+ /** The SARIF version. @deprecated since version 2.1.*/
+ public String version;
+ /**The size of the addressing (eg, "32 bit"). @deprecated since version 2.1.*/
+ public String addressModel;
+ /**The endianess (eg, big or little).*/
+ public String endian;
+ /**The absolute path of where the original executable was imported.*/
+ public String exePath;
+ /**The format of the original executable (eg, PE or ELF).*/
+ public String exeFormat;
+ /**The image base of the program.*/
+ public String imageBase;
+
+ @Override
+ public String toString() {
+ return "processor=" + processorName + "\nfamily=" + family + "\ncompiler=" +
+ compilerSpecID + "\n" + "address model=" + addressModel + "\nendian=" + endian +
+ "\nprogram=" + programName;
+ }
+
+ /**whether the SarifMgr should process stack frames and references.*/
+ public boolean shouldProcessStack() {
+ return true;
+ }
+
+ /**
+ * Returns true if the tool was IDA-PRO.
+ * @return true if the tool was IDA-PRO
+ */
+ boolean isIdaPro() {
+ return "IDA-PRO".equalsIgnoreCase(this.normalizedExternalToolName);
+ }
+
+ boolean isGhidra() {
+ return "GHIDRA".equalsIgnoreCase(this.normalizedExternalToolName);
+ }
+
+ private String translateCompiler(String compiler) {
+ if (isIdaPro()) {
+ return translateIDACompilerName(compiler);
+ }
+ return compiler;
+ }
+
+ public void setCompilerSpecID(String compiler) {
+ this.compilerSpecID = null;
+ if (compiler != null) {
+ String translated = this.translateCompiler(compiler);
+ CompilerSpecID cspec = new CompilerSpecID(translated);
+ this.compilerSpecID = cspec;
+ }
+ }
+
+ private String translateIDACompilerName(String compiler) {
+ if ("Visual C++".equals(compiler)) {
+ return "windows";
+ }
+ return compiler;
+ }
+
+ /**
+ * Returns tool field. This is the name of the tool exactly as written in the SARIF being imported.
+ * @return tool field
+ */
+ public String getTool() {
+ return tool;
+ }
+
+ /**
+ * Sets tool field.
+ * Also sets normalizedExternalToolName to normalized tool names "IDA-PRO" or "GHIDRA" if appropriate, or just sets it to the value of tool.
+ */
+ public void setTool(String tool) {
+
+ this.tool = tool;
+ this.normalizedExternalToolName = tool;
+
+ if (this.tool != null && this.tool.toUpperCase().startsWith("IDA-PRO")) {
+ this.normalizedExternalToolName = "IDA-PRO";
+ }
+ else if (this.tool != null && this.tool.toUpperCase().startsWith("GHIDRA")) {
+ this.normalizedExternalToolName = null; //"GHIDRA"; // null, not external
+ }
+
+ }
+
+ /**
+ * Returns normalizedExternalToolName field. This is the name of the tool normalized into known categories ("IDA-PRO" or "GHIDRA") if appropriate.
+ * @return normalizedExternalToolName
+ */
+ public String getNormalizedExternalToolName() {
+ return this.normalizedExternalToolName;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramSarifMgr.java
new file mode 100644
index 0000000000..7c6f9bd041
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramSarifMgr.java
@@ -0,0 +1,638 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.contrastsecurity.sarif.Artifact;
+import com.contrastsecurity.sarif.Run;
+import com.contrastsecurity.sarif.SarifSchema210;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.stream.JsonWriter;
+
+import ghidra.app.util.bin.ByteProvider;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.app.util.opinion.ElfLoader;
+import ghidra.app.util.opinion.MzLoader;
+import ghidra.app.util.opinion.NeLoader;
+import ghidra.app.util.opinion.PeLoader;
+import ghidra.framework.Application;
+import ghidra.program.database.module.TreeManager;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.lang.CompilerSpec;
+import ghidra.program.model.lang.CompilerSpecID;
+import ghidra.program.model.lang.Language;
+import ghidra.program.model.lang.LanguageID;
+import ghidra.program.model.listing.Listing;
+import ghidra.program.model.listing.Program;
+import ghidra.program.util.GhidraProgramUtilities;
+import ghidra.util.Msg;
+import ghidra.util.SystemUtilities;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifController;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifObject;
+import sarif.io.SarifGsonIO;
+import sarif.io.SarifIO;
+import sarif.model.SarifDataFrame;
+
+/**
+ * The manager responsible for reading and writing a program in SARIF.
+ */
+public class ProgramSarifMgr {
+
+ private static final String SARIF_URL = "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json";
+ private static final String SARIF_VERSION = "2.1.0";
+ private static final int N_MANAGERS = 16;
+
+ private ProgramInfo info;
+ private File file;
+ private File tempFile;
+ private String fileContents;
+ private SarifSchema210 sarif;
+ private SarifController controller;
+ private SarifDataFrame df;
+ private SarifMgr[] mgrs;
+ private Boolean[] opts;
+ private Program program;
+ private SarifProgramOptions options;
+ private Map<String, Boolean> keys = new HashMap<>();
+ private Writer baseWriter;
+
+ /**
+ * Constructs a new program SARIF manager for applying results to an existing
+ * program - used by the SarifPlugin
+ */
+ public ProgramSarifMgr(Program program) {
+ this.program = program;
+ options = new SarifProgramOptions();
+ addManagers();
+ }
+
+ /**
+ * Constructs a new program SARIF manager using the specified file for export.
+ * The file should be an SARIF file.
+ *
+ * @param file the SARIF file
+ */
+ public ProgramSarifMgr(Program program, File file) {
+ this(program);
+ this.file = file;
+ }
+
+ /**
+ * Constructs a new program SARIF manager using the specified file as import for
+ * creating a new program object
+ *
+ * @param bp byte provider
+ */
+ public ProgramSarifMgr(ByteProvider bp) {
+ options = new SarifProgramOptions();
+ this.file = (bp.getFSRL() != null && bp.getFSRL().getNestingDepth() == 1) ? new File(bp.getFSRL().getPath())
+ : bp.getFile();
+ }
+
+ /**
+ * Returns the program info from the underlying file. T``his method does not
+ * make sense to invoke if a write is being performed to a new file.
+ *
+ * @return the program info
+ * @throws IOException if an I/O error occurs
+ */
+ public ProgramInfo getProgramInfo() throws IOException {
+ if (info != null) {
+ return info;
+ }
+
+ info = new ProgramInfo();
+
+ SarifIO io = new SarifGsonIO();
+ sarif = getSarif(io);
+ controller = new SarifController(this);
+ df = new SarifDataFrame(sarif, controller, true);
+ List<Run> runs = sarif.getRuns();
+ Set<Artifact> artifacts = runs.get(0).getArtifacts();
+ Iterator<Artifact> iterator = artifacts.iterator();
+ while(iterator.hasNext()) {
+ Artifact next = iterator.next();
+ Map<String, Object> props = next.getProperties().getAdditionalProperties();
+ info.imageBase = (String) props.get("imageBase");
+ }
+
+ info.programName = "";
+ info.exePath = "";
+ info.exeFormat = "";
+ info.user = "";
+ info.setTool("");
+ info.timestamp = "";
+ info.version = "";
+ info.languageID = new LanguageID(df.getSourceLanguage());
+ info.compilerSpecID = new CompilerSpecID(df.getCompiler());
+ info.processorName = "";
+ info.family = "";
+ info.addressModel = "";
+ info.endian = "";
+
+ return info;
+ }
+
+ private SarifSchema210 getSarif(SarifIO io) throws IOException {
+ return file != null ? io.readSarif(file) : io.readSarif(getFileContents());
+ }
+
+ private Writer getWriter() throws IOException {
+ return file != null ? new FileWriter(file) : new StringWriter(1000);
+ }
+
+
+ public void addManagers() {
+ int mgrCount = 0;
+ MessageLog log = new MessageLog();
+ mgrs = new SarifMgr[N_MANAGERS+1];
+ opts = new Boolean[N_MANAGERS+1];
+ mgrs[mgrCount] = new DataTypesSarifMgr(program, log);
+ opts[mgrCount++] = options.isData();
+ mgrs[mgrCount] = new MemoryMapSarifMgr(this, program, log);
+ opts[mgrCount++] = options.isMemoryBlocks();
+ mgrs[mgrCount] = new RegisterValuesSarifMgr(program, log);
+ opts[mgrCount++] = options.isRegisters();
+ mgrs[mgrCount] = new CodeSarifMgr(program, log);
+ opts[mgrCount++] = options.isInstructions();
+ mgrs[mgrCount] = new DefinedDataSarifMgr(program, log);
+ opts[mgrCount++] = options.isData();
+ mgrs[mgrCount] = new EquatesSarifMgr(program, log);
+ opts[mgrCount++] = options.isEquates();
+ mgrs[mgrCount] = new CommentsSarifMgr(program, log);
+ opts[mgrCount++] = options.isComments();
+ mgrs[mgrCount] = new PropertiesSarifMgr(program, log);
+ opts[mgrCount++] = options.isProperties();
+ mgrs[mgrCount] = new BookmarksSarifMgr(program, log);
+ opts[mgrCount++] = options.isBookmarks();
+ mgrs[mgrCount] = new ProgramTreeSarifMgr(program, log);
+ opts[mgrCount++] = options.isTrees();
+ mgrs[mgrCount] = new ExtEntryPointSarifMgr(program, log);
+ opts[mgrCount++] = options.isEntryPoints();
+ mgrs[mgrCount] = new RelocationTableSarifMgr(program, log);
+ opts[mgrCount++] = options.isRelocationTable();
+ mgrs[mgrCount] = new SymbolTableSarifMgr(program, log, true);
+ opts[mgrCount++] = options.isSymbols();
+ mgrs[mgrCount] = new ExternalLibSarifMgr(program, log);
+ opts[mgrCount++] = options.isExternalLibraries();
+ mgrs[mgrCount] = new FunctionsSarifMgr(program, log);
+ opts[mgrCount++] = options.isFunctions();
+ mgrs[mgrCount] = new SymbolTableSarifMgr(program, log, false);
+ opts[mgrCount++] = options.isSymbols();
+ mgrs[mgrCount] = new MarkupSarifMgr(program, log);
+ opts[mgrCount++] = options.isReferences();
+ assert (mgrCount == N_MANAGERS+1);
+
+ Map<String, Boolean> columnKeys = SarifMgr.getColumnKeys();
+ for (String k : columnKeys.keySet()) {
+ keys.put(k, columnKeys.get(k));
+ }
+ }
+
+ public Map<String, Boolean> getKeys() {
+ return keys;
+ }
+
+ /**
+ * Reads from the underlying SARIF file and populates the specified program.
+ *
+ * @param program the program to load the SARIF into
+ * @param monitor the task monitor
+ * @param options the SARIF options, which features to load and to ignore
+ * @return the message log containing any warning/error messages
+ * @throws SAXException if an SARIF error occurs
+ * @throws IOException if an I/O occurs
+ * @throws AddressFormatException if an invalid address is specified in the
+ * SARIF
+ */
+ public MessageLog read(Program program, TaskMonitor monitor)
+ throws IOException, AddressFormatException {
+
+ if (mgrs == null || program != this.program) {
+ this.program = program;
+ addManagers();
+ }
+ if (df == null) {
+ SarifIO io = new SarifGsonIO();
+ sarif = getSarif(io);
+ controller = new SarifController(this);
+ }
+ controller.setProgram(program);
+ df = new SarifDataFrame(sarif, controller, false);
+
+ Map<String, List<Map<String, Object>>> tableResults = getDataFrame().getTableResultsAsMap();
+
+ readResults(monitor, options, tableResults);
+
+ createDefaultTree(program, options);
+
+ // if instructions were imported, then remove the "needs analyzed" property
+ if (options.isInstructions()) {
+ GhidraProgramUtilities.markProgramAnalyzed(program);
+ }
+
+ return null;
+ }
+
+ public void readResults(TaskMonitor monitor, SarifProgramOptions options,
+ Map<String, List<Map<String, Object>>> tableResults) throws IOException {
+ try {
+ for (int i = 0; i < mgrs.length; i++) {
+ if (opts[i]) {
+ List<Map<String, Object>> list = tableResults.get(mgrs[i].getKey());
+ mgrs[i].readResults(list, options, monitor);
+ }
+ }
+ } catch (Exception e) {
+ throw new IOException("SARIF Read Cancelled");
+ }
+
+ }
+
+ /**
+ * Converts from a generic format name to standard Ghidra names;
+ *
+ * @param name the generic format name
+ * @return the equivalent Ghidra name
+ */
+ public static String getStandardName(String name) {
+ if (name == null) {
+ return "Unknown";
+ } else if (name.toLowerCase().indexOf("portable executable") >= 0 && name.toLowerCase().indexOf("(pe)") >= 0) {
+ return PeLoader.PE_NAME;
+ } else if (name.toLowerCase().indexOf("(elf)") != -1) {
+ return ElfLoader.ELF_NAME;
+ } else if (name.toLowerCase().indexOf("dos executable") >= 0) {
+ return MzLoader.MZ_NAME;
+ } else if (name.toLowerCase().indexOf("new executable") >= 0) {
+ return NeLoader.NE_NAME;
+ }
+ return name;
+ }
+
+ private void createDefaultTree(Program program, SarifProgramOptions options) {
+
+ if (options.isAddToProgram()) {
+ return;
+ }
+
+ Listing listing = program.getListing();
+ if (listing.getTreeNames().length == 0) {
+ try {
+ listing.createRootModule(TreeManager.DEFAULT_TREE_NAME);
+ } catch (DuplicateNameException e) {
+ // shouldn't happen since we checked the tree names above
+ Msg.debug(this, "Unable to create default module", e);
+ }
+ }
+
+ }
+
+ /**
+ * Writes the specified program in SARIF into the underlying file.
+ *
+ * @param program the program to write into SARIF
+ * @param addrSet an address set to limit areas of program that written, or null
+ * for entire program
+ * @param monitor the task monitor
+ * @param options the SARIF options to limit what is and is not written out
+ * @return the message log containing any warning/error messages
+ * @throws IOException if an I/O occurs
+ * @throws CancelledException if the user cancels the read
+ */
+ public MessageLog write(Program program, AddressSetView addrSet, TaskMonitor monitor, SarifProgramOptions options)
+ throws IOException, CancelledException {
+
+ MessageLog log = new MessageLog();
+
+ baseWriter = getWriter();
+ JsonWriter writer = new JsonWriter(getBaseWriter());
+ writer.setIndent(" ");
+ Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().serializeNulls()
+ .disableHtmlEscaping().create();
+ try {
+ if (SarifObject.SARIF) {
+ JsonObject sarif = new JsonObject();
+ JsonArray results = new JsonArray();
+ writeSarifHeader(program, sarif, results);
+ writeResults(program, addrSet, results, monitor, log, options);
+ monitor.setMessage("Results written...exporting to JSON");
+ gson.toJson(sarif, writer);
+ monitor.setMessage("JSON completed");
+ } else {
+ JsonObject json = new JsonObject();
+ json.addProperty("name", program.getDomainFile().getName());
+ json.addProperty("exe_path", program.getExecutablePath());
+ json.addProperty("exe_format", program.getExecutableFormat());
+ json.addProperty("image_base", program.getImageBase().toString());
+ writeInfoSource(json, monitor);
+ writeProcessor(program, json, monitor);
+ JsonArray results = new JsonArray();
+ json.add("results", results);
+ writeResults(program, addrSet, results, monitor, log, options);
+ monitor.setMessage("Results written...exporting to JSON");
+ gson.toJson(json, writer);
+ monitor.setMessage("JSON completed");
+ }
+ } finally {
+ writer.flush();
+ writer.close();
+ }
+
+ return log;
+ }
+
+ private void writeSarifHeader(Program program, JsonObject sarif, JsonArray results) {
+ sarif.addProperty("$schema", SARIF_URL);
+ sarif.addProperty("version", SARIF_VERSION);
+ sarif.add("properties", new JsonObject());
+ JsonArray runs = new JsonArray();
+ sarif.add("runs", runs);
+ JsonObject run = new JsonObject();
+ runs.add(run);
+ writeToolInfo(program, run);
+ run.add("results", results);
+ }
+
+ private void writeToolInfo(Program program, JsonObject run) {
+ JsonObject tool = new JsonObject();
+ run.add("tool", tool);
+ JsonObject driver = new JsonObject();
+ tool.add("driver", driver);
+ driver.addProperty("name", Application.getName());
+ driver.addProperty("version", Application.getApplicationVersion());
+ driver.addProperty("informationUri", "https://github.com/NationalSecurityAgency/ghidra");
+
+ JsonArray artifacts = new JsonArray();
+ run.add("artifacts", artifacts);
+ JsonObject artifact = new JsonObject();
+ artifacts.add(artifact);
+ JsonObject location = new JsonObject();
+ artifact.add("location", location);
+ location.addProperty("uri", program.getExecutablePath());
+
+ JsonObject properties = new JsonObject();
+ artifact.add("properties", properties);
+ JsonObject additionalProperties = new JsonObject();
+ properties.add("additionalProperties", additionalProperties);
+ additionalProperties.addProperty("imageBase", program.getImageBase().toString());
+
+ artifact.addProperty("sourceLanguage", program.getLanguageID().getIdAsString());
+
+ JsonObject description = new JsonObject();
+ artifact.add("description", description);
+ // JsonObject message = new JsonObject();
+ // description.add("message", message);
+ description.addProperty("text", program.getMetadata().get("Compiler ID"));
+ }
+
+ private void writeResults(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ writeCodeBlocks(program, addrSet, results, monitor, log, options);
+ writeComments(program, addrSet, results, monitor, log, options);
+ writeDataTypes(program, addrSet, results, monitor, log, options);
+ writeMemoryMap(program, addrSet, results, monitor, log, options);
+ writeRegisters(program, addrSet, results, monitor, log, options);
+ writeDefinedData(program, addrSet, results, monitor, log, options);
+ writeEquates(program, addrSet, results, monitor, log, options);
+ writeProperties(program, addrSet, results, monitor, log, options);
+ writeBookmarks(program, addrSet, results, monitor, log, options);
+ writeTrees(program, addrSet, results, monitor, log, options);
+ writeEntryPoints(program, addrSet, results, monitor, log, options);
+ writeRelocations(program, addrSet, results, monitor, log, options);
+ writeFunctions(program, addrSet, results, monitor, log, options);
+ writeSymbols(program, addrSet, results, monitor, log, options);
+ writeReferences(program, addrSet, results, monitor, log, options);
+ writeExtLibraries(program, addrSet, results, monitor, log, options);
+ }
+
+ private void writeCodeBlocks(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isInstructions()) {
+ CodeSarifMgr mgr = new CodeSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeComments(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isComments()) {
+ CommentsSarifMgr mgr = new CommentsSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeDataTypes(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isData()) {
+ DataTypesSarifMgr mgr = new DataTypesSarifMgr(program, log);
+ mgr.write(results, monitor);
+ }
+ }
+
+ private void writeMemoryMap(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isMemoryBlocks()) {
+ MemoryMapSarifMgr mgr = new MemoryMapSarifMgr(this, program, log);
+ String path = file != null ? file.getAbsolutePath() : tempFile.getAbsolutePath();
+ mgr.write(results, addrSet, monitor, options.isMemoryContents(), path);
+ }
+ }
+
+ private void writeEquates(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isEquates()) {
+ EquatesSarifMgr mgr = new EquatesSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeProperties(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isProperties()) {
+ PropertiesSarifMgr mgr = new PropertiesSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeBookmarks(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isBookmarks()) {
+ BookmarksSarifMgr mgr = new BookmarksSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeRegisters(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isRegisters()) {
+ RegisterValuesSarifMgr mgr = new RegisterValuesSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeTrees(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isTrees()) {
+ ProgramTreeSarifMgr mgr = new ProgramTreeSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeEntryPoints(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isEntryPoints()) {
+ ExtEntryPointSarifMgr mgr = new ExtEntryPointSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeRelocations(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isRelocationTable()) {
+ RelocationTableSarifMgr mgr = new RelocationTableSarifMgr(program, log);
+ mgr.write(results, monitor);
+ }
+ }
+
+ private void writeDefinedData(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isData()) {
+ DefinedDataSarifMgr mgr = new DefinedDataSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeFunctions(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isFunctions()) {
+ FunctionsSarifMgr mgr = new FunctionsSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeSymbols(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isSymbols()) {
+ SymbolTableSarifMgr mgr = new SymbolTableSarifMgr(program, log, true);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeReferences(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isReferences()) {
+ MarkupSarifMgr mgr = new MarkupSarifMgr(program, log);
+ mgr.write(results, addrSet, monitor);
+ }
+ }
+
+ private void writeExtLibraries(Program program, AddressSetView addrSet, JsonArray results, TaskMonitor monitor,
+ MessageLog log, SarifProgramOptions options) throws IOException, CancelledException {
+ if (options.isExternalLibraries()) {
+ ExternalLibSarifMgr mgr = new ExternalLibSarifMgr(program, log);
+ mgr.write(results, monitor);
+ }
+ }
+
+ private void writeInfoSource(JsonObject json, TaskMonitor monitor) {
+ monitor.setMessage("Writing INFO SOURCE ...");
+
+ JsonObject attrs = new JsonObject();
+ String user = SystemUtilities.getUserName();
+ if (user != null) {
+ attrs.addProperty("USER", user);
+ }
+ attrs.addProperty("TOOL", "Ghidra " + Application.getApplicationVersion());
+ attrs.addProperty("TIMESTAMP", new Date().toString());
+
+ json.add("INFO_SOURCE", attrs);
+ }
+
+ private void writeProcessor(Program program, JsonObject json, TaskMonitor monitor) {
+ monitor.setMessage("Writing PROCESSOR ...");
+
+ JsonObject attrs = new JsonObject();
+ Language language = program.getLanguage();
+ CompilerSpec compilerSpec = program.getCompilerSpec();
+
+ attrs.addProperty("NAME", language.getProcessor().toString());
+ attrs.addProperty("LANGUAGE_PROVIDER",
+ language.getLanguageID().getIdAsString() + ":" + compilerSpec.getCompilerSpecID().getIdAsString());
+ attrs.addProperty("ENDIAN", language.isBigEndian() ? "big" : "little");
+
+ json.add("PROCESSOR", attrs);
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public String getDirectory() {
+ return file != null ? file.getParent() : tempFile.getParent();
+ }
+
+ public SarifDataFrame getDataFrame() {
+ return df;
+ }
+
+ public SarifProgramOptions getOptions() {
+ return options;
+ }
+
+ public String getFileContents() {
+ return fileContents;
+ }
+
+ public void setFileContents(String fileContents) {
+ this.fileContents = fileContents;
+ }
+
+ public Writer getBaseWriter() {
+ return baseWriter;
+ }
+
+ // FOR TESTING
+ public void useTempFileForBytes(String tempDir) throws IOException {
+ File dir = new File(tempDir);
+ String fileName = "SARIF_TEST";
+ String suffixName = ".sarif.bytes";
+ String filename = fileName + '.';
+ tempFile = File.createTempFile(filename, suffixName, dir);
+ tempFile.deleteOnExit();
+ }
+
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramTreeSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramTreeSarifMgr.java
new file mode 100644
index 0000000000..2c54df5fc7
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/ProgramTreeSarifMgr.java
@@ -0,0 +1,280 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import generic.stl.Pair;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.DuplicateGroupException;
+import ghidra.program.model.listing.Group;
+import ghidra.program.model.listing.Listing;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.listing.ProgramFragment;
+import ghidra.program.model.listing.ProgramModule;
+import ghidra.util.Msg;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.exception.NotEmptyException;
+import ghidra.util.exception.NotFoundException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.trees.SarifTreeWriter;
+
+/**
+ * SARIF manager for program trees.
+ */
+public class ProgramTreeSarifMgr extends SarifMgr {
+
+ public static String KEY = "PROGRAM_TREES";
+ public static String SUBKEY = "ProgramTree";
+
+ private List<String> fragmentNameList;
+ private String treeName;
+ private TaskMonitor monitor;
+
+ /**
+ * Constructor
+ *
+ * @param program program
+ * @param log message log
+ */
+ ProgramTreeSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ /**
+ * Read the Program tree section from an SARIF file.
+ *
+ * @param result parser for the SARIF
+ * @param m monitor that can be canceled
+ * @param addToProgram true if we are adding trees to an existing program
+ * @throws CancelledException if cancelled
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor m)
+ throws CancelledException {
+ this.monitor = m;
+
+ // remove the default tree that is created when a program is instantiated
+ listing.removeTree(Listing.DEFAULT_TREE_NAME);
+
+ processTree(result, result);
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processTree(Map<String, Object> result, Map<String, Object> result2) {
+ treeName = (String) result.get("name");
+ fragmentNameList = new ArrayList<>();
+
+ ProgramModule root = listing.getRootModule(treeName);
+ try {
+ try {
+ if (root == null) {
+ root = listing.createRootModule(treeName);
+ }
+ String name = root.getName();
+ if (name.endsWith(".sarif")) {
+ name = name.substring(0, name.indexOf(".sarif"));
+ }
+ if (name.endsWith(".json")) {
+ name = name.substring(0, name.indexOf(".json"));
+ }
+ root.setName(name);
+ } catch (DuplicateNameException dne) {
+ // if name already existed, then attempt to create a
+ // unique "one-up" name
+ //
+ int oneUp = 1;
+ while (true) {
+ try {
+ root = listing.createRootModule(treeName + "(" + oneUp + ")");
+ break; // we located a unique name...
+ } catch (DuplicateNameException e) {
+ ++oneUp;
+ }
+ }
+ treeName = root.getTreeName();
+ }
+
+ List<Map<String, Object>> modules = (List<Map<String, Object>>) result.get("modules");
+ for (Map<String, Object> m : modules) {
+ monitor.checkCancelled();
+ processModule(root, m);
+ }
+ List<Map<String, Object>> fragments = (List<Map<String, Object>>) result.get("fragments");
+ for (Map<String, Object> f : fragments) {
+ monitor.checkCancelled();
+ processFragment(root, f);
+ }
+ removeEmptyFragments(root);
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processModule(ProgramModule parent, Map<String, Object> module) {
+ String name = (String) module.get("name");
+ ProgramModule newModule = null;
+ try {
+ try {
+ newModule = parent.createModule(name);
+ } catch (DuplicateNameException dne) {
+ newModule = listing.getModule(parent.getTreeName(), name);
+ if (newModule == null) {
+ Msg.error(this, "Duplicate name for " + name);
+ return;
+ }
+ parent.add(newModule);
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ List<Map<String, Object>> modules = (List<Map<String, Object>>) module.get("modules");
+ for (Map<String, Object> m : modules) {
+ processModule(newModule, m);
+ }
+ List<Map<String, Object>> fragments = (List<Map<String, Object>>) module.get("fragments");
+ for (Map<String, Object> f : fragments) {
+ processFragment(newModule, f);
+ }
+ removeEmptyFragments(newModule);
+ }
+
+ private void processFragment(ProgramModule parent, Map<String, Object> fragment) {
+ String name = (String) fragment.get("name");
+ if (!fragmentNameList.contains(name)) {
+ fragmentNameList.add(name);
+ }
+ ProgramFragment frag = null;
+ try {
+ frag = parent.createFragment(name);
+ } catch (DuplicateNameException dne) {
+ frag = listing.getFragment(parent.getTreeName(), name);
+ try {
+ parent.add(frag);
+ } catch (DuplicateGroupException dge) {
+ // ignore, we are trying to add a fragment to
+ // a module that already contains it
+ }
+ }
+ try {
+ processFragmentRange(fragment, frag);
+ } catch (NotFoundException e) {
+ log.appendMsg(e.getMessage());
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processFragmentRange(Map<String, Object> fragment, ProgramFragment frag)
+ throws AddressFormatException, NotFoundException {
+
+ List<Map<String, Object>> ranges = (List<Map<String, Object>>) fragment.get("ranges");
+ for (Map<String, Object> r : ranges) {
+ if (monitor.isCancelled()) {
+ break;
+ }
+ String startStr = (String) r.get("start");
+ String endStr = (String) r.get("end");
+
+ Address start = parseAddress(factory, startStr);
+ Address end = parseAddress(factory, endStr);
+
+ if (start == null || end == null) {
+ throw new AddressFormatException(
+ "Incompatible Fragment Address Range: [" + startStr + "," + endStr + "]");
+ }
+
+ frag.move(start, end);
+ }
+ }
+
+ /**
+ * Method removeEmptyFragments.
+ */
+ private void removeEmptyFragments(ProgramModule module) {
+ Group[] groups = module.getChildren();
+ for (Group group : groups) {
+ if (group instanceof ProgramFragment) {
+ String name = group.getName();
+ if (!fragmentNameList.contains(name)) {
+ try {
+ module.removeChild(name);
+ } catch (NotEmptyException e) {
+ log.appendMsg("Warning: Extra Program Tree fragment '" + name
+ + "' did not exist in imported SARIF file");
+ }
+ }
+ } else {
+ removeEmptyFragments((ProgramModule) group);
+ }
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the program trees.
+ *
+ * @param results writer for SARIF
+ * @param addrs the addresses
+ * @param m monitor that can be canceled should be written
+ * @throws CancelledException if cancelled
+ */
+ void write(JsonArray results, AddressSetView addrs, TaskMonitor m) throws IOException, CancelledException {
+ this.monitor = m;
+ m.setMessage("Writing PROGRAM TREES ...");
+
+ List<Pair<String, ProgramModule>> request = new ArrayList<>();
+ String[] treeNames = listing.getTreeNames();
+
+ for (String n : treeNames) {
+ if (m.isCancelled()) {
+ throw new CancelledException();
+ }
+ ProgramModule root = listing.getRootModule(n);
+ request.add(new Pair<String, ProgramModule>(n, root));
+ }
+
+ writeAsSARIF(request, results);
+ }
+
+ public static void writeAsSARIF(List<Pair<String, ProgramModule>> request, JsonArray results) throws IOException {
+ SarifTreeWriter writer = new SarifTreeWriter(request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/PropertiesSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/PropertiesSarifMgr.java
new file mode 100644
index 0000000000..be0b13c143
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/PropertiesSarifMgr.java
@@ -0,0 +1,304 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Point;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.swing.KeyStroke;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.framework.options.CustomOption;
+import ghidra.framework.options.OptionType;
+import ghidra.framework.options.Options;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Bookmark;
+import ghidra.program.model.listing.BookmarkManager;
+import ghidra.program.model.listing.BookmarkType;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.util.IntPropertyMap;
+import ghidra.program.model.util.LongPropertyMap;
+import ghidra.program.model.util.ObjectPropertyMap;
+import ghidra.program.model.util.PropertyMap;
+import ghidra.program.model.util.PropertyMapManager;
+import ghidra.program.model.util.StringPropertyMap;
+import ghidra.program.model.util.VoidPropertyMap;
+import ghidra.util.ColorUtils;
+import ghidra.util.SaveableColor;
+import ghidra.util.SaveablePoint;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.props.SarifPropertyListWriter;
+import sarif.export.props.SarifPropertyMapWriter;
+
+public class PropertiesSarifMgr extends SarifMgr {
+
+ public static String KEY = "PROPERTIES";
+ public static String SUBKEY = "Property";
+
+ private final static String PROPERTY_LIST_CATEGORY_DELIMITER = Options.DELIMITER_STRING;
+
+ private PropertyMapManager propMapMgr;
+
+ PropertiesSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ this.propMapMgr = program.getUsrPropertyManager();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ processProperty(result, options == null || options.isOverwritePropertyConflicts());
+ return true;
+ }
+
+ private void processProperty(Map<String, Object> result, boolean overwrite) {
+
+ String name = (String) result.get("name");
+ try {
+ Address addr = getLocation(result);
+ if (addr != null) {
+ processPropertyMapEntry(addr, name, result, overwrite);
+ } else {
+ processPropertyListEntry(name, result, overwrite);
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processPropertyMapEntry(Address addr, String name, Map<String, Object> result, boolean overwrite)
+ throws DuplicateNameException {
+
+ String type = (String) result.get("type");
+ if (type != null) {
+ type = type.toLowerCase();
+ }
+
+ if (!overwrite && !"bookmarks".equals(type)) {
+ PropertyMap<?> map = propMapMgr.getPropertyMap(name);
+ if (map != null && map.hasProperty(addr)) {
+ log.appendMsg("Conflicting '" + name + "' PROPERTY ignored at: " + addr);
+ return; // skip - property conflicts
+ }
+ }
+
+ String val = (String) result.get("value");
+ if (type == null || "void".equals(type)) {
+ if (val != null) {
+ log.appendMsg("VALUE attribute ignored for void property");
+ }
+ VoidPropertyMap voidMap = propMapMgr.getVoidPropertyMap(name);
+ if (voidMap == null) {
+ voidMap = propMapMgr.createVoidPropertyMap(name);
+ }
+ voidMap.add(addr);
+ } else if ("int".equals(type)) {
+ int value = Integer.parseInt(val, 16);
+ IntPropertyMap intMap = propMapMgr.getIntPropertyMap(name);
+ if (intMap == null) {
+ intMap = propMapMgr.createIntPropertyMap(name);
+ }
+ intMap.add(addr, value);
+ } else if ("long".equals(type)) {
+ long value = Long.parseLong(val, 16);
+ LongPropertyMap longMap = propMapMgr.getLongPropertyMap(name);
+ if (longMap == null) {
+ longMap = propMapMgr.createLongPropertyMap(name);
+ }
+ longMap.add(addr, value);
+ } else if ("string".equals(type)) {
+ String str = val;
+ StringPropertyMap strMap = propMapMgr.getStringPropertyMap(name);
+ if (strMap == null) {
+ strMap = propMapMgr.createStringPropertyMap(name);
+ }
+ strMap.add(addr, str);
+ } else if ("color".equals(type)) {
+ ObjectPropertyMap<SaveableColor> objMap = (ObjectPropertyMap<SaveableColor>) propMapMgr
+ .getObjectPropertyMap(name);
+ if (objMap == null) {
+ objMap = propMapMgr.createObjectPropertyMap(name, SaveableColor.class);
+ }
+ objMap.add(addr, new SaveableColor(Color.decode(val)));
+ } else if ("point".equals(type)) {
+ String xstr = val.substring(val.indexOf("[x="), val.indexOf(","));
+ String ystr = val.substring(val.indexOf("y="), val.indexOf("]"));
+ ObjectPropertyMap<SaveablePoint> objMap = (ObjectPropertyMap<SaveablePoint>) propMapMgr
+ .getObjectPropertyMap(name);
+ if (objMap == null) {
+ objMap = propMapMgr.createObjectPropertyMap(name, SaveablePoint.class);
+ }
+ objMap.add(addr, new SaveablePoint(new Point(Integer.parseInt(xstr), Integer.parseInt(ystr))));
+ } else if ("bookmarks".equals(type)) {
+ // Must retain for backward compatibility with old Ver-1 Note bookmarks which
+ // were saved as simple properties
+ BookmarkManager bmMgr = program.getBookmarkManager();
+ if (!overwrite) {
+ Bookmark[] bookmarks = bmMgr.getBookmarks(addr, BookmarkType.NOTE);
+ if (bookmarks.length != 0) {
+ log.appendMsg("Conflicting BOOKMARK ignored at: " + addr);
+ return; // skip - bookmark conflicts
+ }
+ }
+ bmMgr.setBookmark(addr, BookmarkType.NOTE, name, val);
+ } else {
+ log.appendMsg("Unsupported PROPERTY usage");
+ }
+ }
+
+ private String getPropertyList(String path) {
+ StringTokenizer st = new StringTokenizer(path, PROPERTY_LIST_CATEGORY_DELIMITER);
+ if (st.hasMoreElements()) {
+ return st.nextToken();
+ }
+ return null;
+ }
+
+ private String getPropertyName(String path) {
+ int ix = path.indexOf(PROPERTY_LIST_CATEGORY_DELIMITER);
+ if (ix >= 0) {
+ if (path.length() > (ix + 1)) {
+ return path.substring(ix + 1);
+ }
+ return null;
+ }
+ return path;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void processPropertyListEntry(String pathname, Map<String, Object> result, boolean overwrite)
+ throws Exception {
+
+ String listName = getPropertyList(pathname);
+ String name = getPropertyName(pathname);
+ if (listName == null || name == null) {
+ log.appendMsg("Property NAME attribute must contain both category prefix and property name");
+ return;
+ }
+ Options list = program.getOptions(listName);
+ if (!overwrite && list.contains(name)) {
+ log.appendMsg("Conflicting PROPERTY ignored: " + pathname);
+ return; // skip - property conflicts
+ }
+ String type = (String) result.get("type");
+ if (type != null) {
+ type = type.toLowerCase();
+ }
+ Object val = result.get("value");
+ if (type == null || "void".equals(type)) {
+ log.appendMsg("Unsupported PROPERTY usage");
+ } else if ("int".equals(type)) {
+ list.setInt(name, Integer.parseInt((String) val, 16));
+ } else if ("long".equals(type)) {
+ list.setLong(name, Long.parseLong((String) val, 16));
+ } else if ("double".equals(type)) {
+ list.setDouble(name, Double.parseDouble((String) val));
+ } else if ("float".equals(type)) {
+ list.setFloat(name, Float.parseFloat((String) val));
+ } else if ("bool".equals(type)) {
+ list.setBoolean(name, Boolean.parseBoolean((String) val));
+ } else if ("string".equals(type)) {
+ list.setString(name, (String) val);
+ } else if ("date".equals(type)) {
+ list.setDate(name, new Date(Long.parseLong((String) val, 16)));
+ } else if ("color".equals(type)) {
+ Color color = ColorUtils.getColor((Integer) val);
+ list.setColor(name, color);
+ } else if ("file".equals(type)) {
+ File file = new File((String) val);
+ list.setFile(name, file);
+ } else if ("enum".equals(type)) {
+ String sarifString = unEscapeElementEntities((String) val);
+ @SuppressWarnings("rawtypes")
+ Enum enuum = (Enum) OptionType.ENUM_TYPE.convertStringToObject(sarifString);
+ list.setEnum(name, enuum);
+ } else if ("font".equals(type)) {
+ String sarifString = unEscapeElementEntities((String) val);
+ Font font = (Font) OptionType.FONT_TYPE.convertStringToObject(sarifString);
+ list.setFont(name, font);
+ } else if ("keyStroke".equals(type)) {
+ String sarifString = unEscapeElementEntities((String) val);
+ KeyStroke keyStroke = (KeyStroke) OptionType.KEYSTROKE_TYPE.convertStringToObject(sarifString);
+ list.setKeyStroke(name, keyStroke);
+ } else if ("custom".equals(type)) {
+ String sarifString = unEscapeElementEntities((String) val);
+ CustomOption custom = (CustomOption) OptionType.CUSTOM_TYPE.convertStringToObject(sarifString);
+ list.setCustomOption(name, custom);
+ } else if ("bytes".equals(type)) {
+ String sarifString = unEscapeElementEntities((String) val);
+ byte[] bytes = (byte[]) OptionType.BYTE_ARRAY_TYPE.convertStringToObject(sarifString);
+ list.setByteArray(name, bytes);
+ } else {
+ log.appendMsg("Unsupported PROPERTY usage");
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing PROPERTIES ...");
+
+ List<String> request = program.getOptionsNames();
+
+ writeAsSARIF(program, request, results);
+
+ List<PropertyMap<?>> mapRequest = new ArrayList<>();
+ Iterator<String> mapNames = propMapMgr.propertyManagers();
+ while (mapNames.hasNext()) {
+ monitor.checkCancelled();
+ mapRequest.add(propMapMgr.getPropertyMap(mapNames.next()));
+ }
+
+ writeAsSARIF(program, set, mapRequest, results);
+ }
+
+ public static void writeAsSARIF(Program program, List<String> request, JsonArray results) throws IOException {
+ SarifPropertyListWriter writer = new SarifPropertyListWriter(program, request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+ public static void writeAsSARIF(Program program, AddressSetView set, List<PropertyMap<?>> request,
+ JsonArray results) throws IOException {
+ SarifPropertyMapWriter writer = new SarifPropertyMapWriter(request, program, set, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/RegisterValuesSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/RegisterValuesSarifMgr.java
new file mode 100644
index 0000000000..dff936175e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/RegisterValuesSarifMgr.java
@@ -0,0 +1,157 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.lang.Register;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.listing.ProgramContext;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.registers.SarifRegisterValueWriter;
+
+/**
+ * SARIF manager for register values.
+ */
+public class RegisterValuesSarifMgr extends SarifMgr {
+
+ public static String KEY = "REGISTER_VALUES";
+ public static String SUBKEY = "Registers";
+
+ private ProgramContext context;
+ private Set<String> undefinedRegisterNames;
+
+ RegisterValuesSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ context = program.getProgramContext();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ /**
+ * Process the entry point section of the SARIF file.
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ processRegisterValues(result, result);
+ return true;
+ }
+
+ /**
+ * Returns list of unique registers which do not overlap any smaller registers.
+ */
+ private List<Register> getUniqueRegisters() {
+
+ ArrayList<Register> regs = new ArrayList<>(context.getRegisters());
+ Collections.sort(regs, new Comparator<Register>() {
+ @Override
+ public int compare(Register r1, Register r2) {
+ int size1 = r1.getMinimumByteSize();
+ int size2 = r2.getMinimumByteSize();
+ if (size1 != size2) {
+ return size1 - size2;
+ }
+ return r1.getOffset() - r2.getOffset();
+ }
+ });
+
+ return regs;
+ }
+
+ private void processRegisterValues(Map<String, Object> result, Map<String, Object> result2) {
+ try {
+ AddressSet set = getLocations(result, null);
+ Address addr = set.getMinAddress();
+ int len = (int) set.getMaxAddress().subtract(addr) + 1;
+ String regName = (String) result.get("name");
+ String valueStr = (String) result.get("value");
+ if (valueStr.startsWith("0x") || valueStr.startsWith("0X")) {
+ valueStr = valueStr.substring(2);
+ }
+ BigInteger value = new BigInteger(valueStr, 16);
+
+ Register reg = context.getRegister(regName);
+ if (reg == null) {
+ if (undefinedRegisterNames.add(regName)) {
+ log.appendMsg("REGISTER [" + regName + "] is not defined by " + program.getLanguageID()
+ + ", register values will be ignored");
+ }
+ return;
+ }
+
+ context.setValue(reg, addr, addr.addNoWrap(len - 1), value);
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the Equates.
+ *
+ * @param results writer for SARIF
+ * @param set address set that is either the entire program or a selection
+ * @param monitor monitor that can be canceled should be written
+ * @throws IOException
+ */
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+
+ List<Register> regs = getUniqueRegisters();
+ if (set == null) {
+ set = program.getMemory();
+ }
+ List<AddressRange> request = new ArrayList<>();
+ AddressRangeIterator rangeIter = set.getAddressRanges();
+ while (rangeIter.hasNext()) {
+ monitor.checkCancelled();
+ request.add(rangeIter.next());
+ }
+
+ writeAsSARIF(context, regs, request, results);
+ }
+
+ public static void writeAsSARIF(ProgramContext context, List<Register> registers, List<AddressRange> request,
+ JsonArray results) throws IOException {
+ SarifRegisterValueWriter writer = new SarifRegisterValueWriter(context, registers, request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/RelocationTableSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/RelocationTableSarifMgr.java
new file mode 100644
index 0000000000..c672a5f8d9
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/RelocationTableSarifMgr.java
@@ -0,0 +1,131 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.reloc.Relocation;
+import ghidra.program.model.reloc.Relocation.Status;
+import ghidra.program.model.reloc.RelocationTable;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.relocs.SarifRelocationWriter;
+
+public class RelocationTableSarifMgr extends SarifMgr {
+
+ public static String KEY = "RELOCATIONS";
+ public static String SUBKEY = "Relocation";
+
+ RelocationTableSarifMgr(Program program, MessageLog log) {
+ super(KEY, program, log);
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ RelocationTable relocTable = program.getRelocationTable();
+
+ try {
+ Address addr = getLocation(result);
+ int type = Integer.parseInt((String) result.get("kind"));
+ long[] values = unpackLongs((String) result.get("value"));
+ byte[] bytes = unpackBytes((String) result.get("bytes")); // optional
+ String symbolName = (String) result.get("name"); // optional
+
+ Status status = Status.UNKNOWN;
+ if (bytes == null) {
+ if (status != null && status.hasBytes()) {
+ log.appendMsg("Relocation at " + addr + " missing required bytes - forced UNKNOWN status.");
+ status = Status.UNKNOWN;
+ }
+ } else if (status == null) {
+ status = type == 0 ? Status.APPLIED_OTHER : Status.APPLIED;
+ }
+
+ relocTable.add(addr, status, type, values, bytes, symbolName);
+ } catch (AddressOverflowException e) {
+ log.appendException(e);
+ }
+ return true;
+ }
+
+ private long[] unpackLongs(String attrValue) {
+ if (attrValue == null) {
+ return null;
+ }
+ StringTokenizer st = new StringTokenizer(attrValue, ",");
+ long[] values = new long[st.countTokens()];
+ int index = 0;
+ while (st.hasMoreTokens()) {
+ values[index++] = parseLong(st.nextToken());
+ }
+ return values;
+ }
+
+ private byte[] unpackBytes(String attrValue) {
+ if (attrValue == null) {
+ return null;
+ }
+ StringTokenizer st = new StringTokenizer(attrValue, ",");
+ byte[] values = new byte[st.countTokens()];
+ int index = 0;
+ while (st.hasMoreTokens()) {
+ values[index++] = (byte) parseLong(st.nextToken());
+ }
+ return values;
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ void write(JsonArray results, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing RELOCATION TABLE ...");
+
+ List<Relocation> request = new ArrayList<>();
+ Iterator<Relocation> iter = program.getRelocationTable().getRelocations();
+ while (iter.hasNext()) {
+ monitor.checkCancelled();
+ request.add(iter.next());
+ }
+
+ writeAsSARIF(request, results);
+ }
+
+ public static void writeAsSARIF(List<Relocation> request, JsonArray results) throws IOException {
+ SarifRelocationWriter writer = new SarifRelocationWriter(request, null);
+ new TaskLauncher(new SarifWriterTask(SUBKEY, writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/SarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/SarifMgr.java
new file mode 100644
index 0000000000..6b39fb332e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/SarifMgr.java
@@ -0,0 +1,323 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.contrastsecurity.sarif.ArtifactLocation;
+import com.contrastsecurity.sarif.Location;
+import com.contrastsecurity.sarif.PhysicalLocation;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+import ghidra.app.util.NamespaceUtils;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFactory;
+import ghidra.program.model.address.AddressFormatException;
+import ghidra.program.model.address.AddressOverflowException;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeImpl;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.address.AddressSpace;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.Listing;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.util.NumericUtilities;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.exception.InvalidInputException;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+
+public abstract class SarifMgr {
+
+ protected static final Boolean USE_SARIF = true;
+
+ protected String key;
+ static protected Map<String, Boolean> columnKeys = new HashMap<>();
+ protected Program program;
+ protected Listing listing;
+ protected AddressFactory factory;
+ protected static Map<String, ExternalLocation> externalMap;
+ protected boolean firstPass = true;
+
+ protected MessageLog log = new MessageLog() {
+ @Override
+ public void appendException(Throwable t) {
+ appendMsg(t.toString());
+ }
+ };
+
+ public SarifMgr(String key, Program program, MessageLog log) {
+ this.key = key;
+ this.program = program;
+ this.listing = program.getListing();
+ this.factory = program.getAddressFactory();
+ this.log = log;
+ columnKeys.put("name", false);
+ columnKeys.put("location", true);
+ columnKeys.put("kind", false);
+ columnKeys.put("type", true);
+ columnKeys.put("value", false);
+ columnKeys.put("size", true);
+ columnKeys.put("comment", true);
+ columnKeys.put("typeName", true);
+ columnKeys.put("typeLocation", true);
+ }
+
+ protected void writeLocation(JsonObject result, Address start, Address end) {
+ JsonArray locs = new JsonArray();
+ result.add("locations", locs);
+ JsonObject element = new JsonObject();
+ locs.add(element);
+ JsonObject ploc = new JsonObject();
+ element.add("physicalLocation", ploc);
+ JsonObject address = new JsonObject();
+ ploc.add("address", address);
+ address.addProperty("absoluteAddress", start.getOffset());
+ if (end != null) {
+ address.addProperty("length", end.subtract(start) + 1);
+ if (!start.getAddressSpace().equals(program.getAddressFactory().getDefaultAddressSpace())) {
+ JsonObject artifact = new JsonObject();
+ ploc.add("artifactLocation", artifact);
+ artifact.addProperty("uri", start.toString());
+ }
+ }
+ }
+
+ protected void writeLocations(JsonObject result, AddressSetView set) {
+ JsonArray locs = new JsonArray();
+ result.add("locations", locs);
+ AddressRangeIterator addressRanges = set.getAddressRanges();
+ while (addressRanges.hasNext()) {
+ JsonObject element = new JsonObject();
+ locs.add(element);
+ AddressRange next = addressRanges.next();
+ JsonObject ploc = new JsonObject();
+ element.add("physicalLocation", ploc);
+ JsonObject address = new JsonObject();
+ ploc.add("address", address);
+ address.addProperty("absoluteAddress", next.getMinAddress().getOffset());
+ address.addProperty("length", next.getLength());
+ Address minAddress = next.getMinAddress();
+ if (!minAddress.getAddressSpace().equals(program.getAddressFactory().getDefaultAddressSpace())) {
+ JsonObject artifact = new JsonObject();
+ ploc.add("artifactLocation", artifact);
+ artifact.addProperty("uri", minAddress.toString());
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected AddressSet getLocations(Map<String, Object> result, AddressSet set) throws AddressOverflowException {
+ if (set == null) {
+ set = new AddressSet();
+ }
+ AddressFactory af = program.getAddressFactory();
+ AddressSpace space = af.getDefaultAddressSpace();
+ String namespace = (String) result.get("location");
+ if (namespace != null) {
+ boolean isExternal = namespace.contains("<EXTERNAL>");
+ if (isExternal) {
+ space = af.getAddressSpace("EXTERNAL");
+ }
+ }
+ String ospace = (String) result.get("overlayedSpace");
+ if (ospace != null) {
+ space = af.getAddressSpace(ospace);
+ }
+ List<Location> locations = (List<Location>) result.get("Locations");
+ if (locations == null) {
+ return set;
+ }
+ for (Location location : locations) {
+ PhysicalLocation physicalLocation = location.getPhysicalLocation();
+ Object addr = physicalLocation.getAddress().getAbsoluteAddress();
+ Address address = longToAddress(space, addr);
+ long len = (long) physicalLocation.getAddress().getLength();
+ ArtifactLocation artifact = physicalLocation.getArtifactLocation();
+ if (artifact != null) {
+ String uri = artifact.getUri();
+ if (uri != null) {
+ Address test = program.getAddressFactory().getAddress(uri);
+ if (test != null) {
+ address = test;
+ }
+ }
+ }
+ set.add(new AddressRangeImpl(address, len));
+ }
+ return set;
+ }
+
+ protected Address getLocation(Map<String, Object> result) throws AddressOverflowException {
+ AddressSet set = new AddressSet();
+ getLocations(result, set);
+ return set.getMinAddress();
+ }
+
+ protected void readResults(List<Map<String, Object>> list, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException {
+ if (list != null) {
+ monitor.setMessage("Processing " + key + "...");
+ monitor.setMaximum(list.size());
+ for (Map<String, Object> result : list) {
+ read(result, options, monitor);
+ monitor.increment();
+ }
+ } else {
+ monitor.setMessage("Skipping over " + key + " ...");
+ }
+ }
+
+ public abstract boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws AddressFormatException, CancelledException;
+
+ public String getKey() {
+ return key;
+ }
+
+ protected Namespace walkNamespace(Namespace parent, String namespace, Address addr, SourceType sourceType,
+ Boolean isClass) throws IOException {
+ int sep = namespace.indexOf("::");
+ if (sep <= 0) {
+ return parent;
+ }
+ String tag = namespace.substring(0, sep);
+ if (addr != null) {
+ Function func = program.getFunctionManager().getFunctionContaining(addr);
+ if (func != null) {
+ if (func.getName(true).equals(tag)) {
+ return walkNamespace(func, namespace.substring(sep + 2), addr, sourceType, isClass);
+ }
+ } else if (tag.startsWith("FUN_")) {
+ return null; // Defer this until later
+ }
+ }
+
+ Namespace child = program.getSymbolTable().getNamespace(tag, parent);
+ if (child == null) {
+ try {
+ if (isClass != null && isClass) {
+ child = program.getSymbolTable().createClass(parent, tag, sourceType);
+ } else {
+ child = NamespaceUtils.createNamespaceHierarchy(tag, parent, program, sourceType);
+ }
+ } catch (InvalidInputException | DuplicateNameException e) {
+ throw new IOException("Error creating namespace for " + tag);
+ }
+ }
+ return walkNamespace(child, namespace.substring(sep + 2), addr, sourceType, isClass);
+ }
+
+ protected SourceType getSourceType(String signatureSource) {
+ SourceType sourceType = SourceType.IMPORTED;
+ if (signatureSource == null) {
+ return sourceType;
+ }
+ try {
+ if (signatureSource != null) {
+ sourceType = SourceType.valueOf(signatureSource);
+ }
+ } catch (IllegalArgumentException iae) {
+ log.appendMsg("Unknown SourceType: " + signatureSource);
+ }
+ return sourceType;
+ }
+
+ public static Map<String, Boolean> getColumnKeys() {
+ return columnKeys;
+ }
+
+ public static Address parseAddress(AddressFactory factory, String addrString) {
+ if (addrString == null) {
+ return null;
+ }
+ Address addr = factory.getAddress(addrString);
+ if (addr == null) {
+ int index = addrString.indexOf("::");
+ if (index > 0) {
+ addr = factory.getAddress(addrString.substring(index + 2));
+ }
+ }
+ return addr;
+ }
+
+ public static long parseLong(String longStr) {
+ boolean isNegative = longStr.startsWith("-");
+ if (isNegative) {
+ longStr = longStr.substring(1);
+ }
+ int radix = 10;
+ if (longStr.startsWith("0x")) {
+ longStr = longStr.substring(2);
+ radix = 16;
+ }
+ long val = (radix == 10) ? NumericUtilities.parseLong(longStr) : NumericUtilities.parseHexLong(longStr);
+ if (isNegative) {
+ val *= -1;
+ }
+ return val;
+ }
+
+ private static final String LESS_THAN = "&lt;";
+ private static final String GREATER_THAN = "&gt;";
+ private static final String APOSTROPHE = "&apos;";
+ private static final String QUOTE = "&quot;";
+ private static final String AMPERSAND = "&amp;";
+
+ private static final Pattern HEX_DIGIT_PATTERN = Pattern.compile("[&][#][x]([\\da-fA-F]+)[;]");
+
+ public static String unEscapeElementEntities(String escapedSARIFString) {
+
+ Matcher matcher = HEX_DIGIT_PATTERN.matcher(escapedSARIFString);
+ StringBuilder buffy = new StringBuilder();
+ while (matcher.find()) {
+ int codePoint = Integer.parseInt(matcher.group(1), 16);
+ matcher.appendReplacement(buffy, Character.toString(codePoint));
+ }
+ matcher.appendTail(buffy);
+
+ String unescapedStr = buffy.toString();
+
+ unescapedStr = unescapedStr.replaceAll(LESS_THAN, "<");
+ unescapedStr = unescapedStr.replaceAll(GREATER_THAN, ">");
+ unescapedStr = unescapedStr.replaceAll(APOSTROPHE, "'");
+ unescapedStr = unescapedStr.replaceAll(QUOTE, "\"");
+ unescapedStr = unescapedStr.replaceAll(AMPERSAND, "&");
+
+ return unescapedStr;
+ }
+
+ public Address longToAddress(AddressSpace space, Object addr) {
+ if (addr instanceof Long) {
+ return space.getAddress((Long) addr);
+ }
+ return space.getAddress((Integer) addr);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/SymbolTableSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/SymbolTableSarifMgr.java
new file mode 100644
index 0000000000..025e1fc9b9
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/SymbolTableSarifMgr.java
@@ -0,0 +1,171 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.managers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gson.JsonArray;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolIterator;
+import ghidra.program.model.symbol.SymbolTable;
+import ghidra.program.model.symbol.SymbolUtilities;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskLauncher;
+import ghidra.util.task.TaskMonitor;
+import sarif.SarifProgramOptions;
+import sarif.export.SarifWriterTask;
+import sarif.export.symbols.SarifSymbolWriter;
+
+/**
+ * SARIF manager for the Symbol Table.
+ *
+ */
+public class SymbolTableSarifMgr extends SarifMgr {
+
+ public static String KEY = "SYMBOLS";
+ public static String SUBKEY = "Symbols";
+
+ private SymbolTable symbolTable;
+ private boolean overwritePrimary;
+ private boolean preFunction;
+
+ SymbolTableSarifMgr(Program program, MessageLog log, boolean preFunction) {
+ super(KEY, program, log);
+ this.preFunction = preFunction;
+ symbolTable = program.getSymbolTable();
+ }
+
+ ////////////////////////////
+ // SARIF READ CURRENT DTD //
+ ////////////////////////////
+
+ /**
+ * Process the symbol table section of the SARIF file.
+ */
+ @Override
+ public boolean read(Map<String, Object> result, SarifProgramOptions options, TaskMonitor monitor)
+ throws CancelledException {
+ this.overwritePrimary = options == null || options.isOverwriteSymbolConflicts();
+ processSymbol(result, preFunction);
+ return true;
+ }
+
+ private void processSymbol(Map<String, Object> result, boolean firstPass) {
+ try {
+ String kind = (String) result.get("kind");
+ boolean isLocal = (kind != null) && kind.equalsIgnoreCase("local");
+ String type = (String) result.get("type");
+
+ boolean isPrimary = (boolean) result.get("primary");
+ String sourceTypeString = (String) result.get("sourceType");
+ SourceType sourceType = getSourceType(sourceTypeString);
+
+ String name = (String) result.get("name");
+ boolean processFirstPass = true;
+ if (isLocal && type == null) {
+ processFirstPass = false;
+ }
+ if (sourceType.equals(SourceType.DEFAULT)) {
+ processFirstPass = false;
+ }
+
+ if (firstPass && !processFirstPass) {
+ return;
+ }
+ if (!firstPass && processFirstPass) {
+ return;
+ }
+
+ Address addr = getLocation(result);
+ String namespace = (String) result.get("location");
+ Boolean isClass = (Boolean) result.get("namespaceIsClass");
+ Namespace scope = program.getGlobalNamespace(); // default to global scope
+ scope = isLocal ? walkNamespace(scope, namespace, addr, sourceType, isClass) : scope;
+ if (scope == null) {
+ return;
+ }
+
+ if (type != null) {
+ if (type.equals("namespace")) {
+ if (symbolTable.getNamespace(name, scope) == null) {
+ symbolTable.createNameSpace(scope, name, sourceType);
+ }
+ return;
+ }
+ if (type.equals("class")) {
+ if (symbolTable.getClassSymbol(name, scope) == null) {
+ symbolTable.createClass(scope, name, sourceType);
+ }
+ return;
+ }
+ if (type.equals("library")) {
+ if (symbolTable.getLibrarySymbol(name) == null) {
+ symbolTable.createExternalLibrary(name, sourceType);
+ }
+ return;
+ }
+ }
+
+ if (symbolTable.getSymbol(name, addr, scope) == null) {
+ Symbol s = SymbolUtilities.createPreferredLabelOrFunctionSymbol(program, addr, scope, name, sourceType);
+ if (s != null && isPrimary && overwritePrimary) {
+ s.setPrimary();
+ }
+ }
+ } catch (Exception e) {
+ log.appendException(e);
+ }
+ }
+
+ /////////////////////////////
+ // SARIF WRITE CURRENT DTD //
+ /////////////////////////////
+
+ /**
+ * Write out the SARIF for the symbol table.
+ *
+ * @param results writer for SARIF
+ * @param monitor monitor that can be canceled should be written
+ * @throws IOException
+ */
+ void write(JsonArray results, AddressSetView set, TaskMonitor monitor) throws IOException, CancelledException {
+ monitor.setMessage("Writing SYMBOL TABLE ...");
+
+ SymbolIterator iter = symbolTable.getSymbolIterator();
+ List<Symbol> request = new ArrayList<>();
+ while (iter.hasNext()) {
+ request.add(iter.next());
+ }
+
+ writeAsSARIF(program, request, results);
+ }
+
+ public static void writeAsSARIF(Program program, List<Symbol> request, JsonArray results) throws IOException {
+ SarifSymbolWriter writer = new SarifSymbolWriter(request, null);
+ new TaskLauncher(new SarifWriterTask("Symbols", writer, results), null);
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifColumnKey.java b/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifColumnKey.java
new file mode 100644
index 0000000000..2d8f1fe1f2
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifColumnKey.java
@@ -0,0 +1,36 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.model;
+
+public class SarifColumnKey {
+
+ private String name;
+ private boolean isHidden;
+
+ public SarifColumnKey(String name, boolean isHidden) {
+ this.name = name;
+ this.isHidden = isHidden;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isHidden() {
+ return isHidden;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifDataFrame.java b/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifDataFrame.java
new file mode 100644
index 0000000000..047fff984b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifDataFrame.java
@@ -0,0 +1,183 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.model;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import com.contrastsecurity.sarif.Artifact;
+import com.contrastsecurity.sarif.ReportingDescriptorReference;
+import com.contrastsecurity.sarif.Result;
+import com.contrastsecurity.sarif.Run;
+import com.contrastsecurity.sarif.SarifSchema210;
+import com.contrastsecurity.sarif.ToolComponent;
+import com.contrastsecurity.sarif.ToolComponentReference;
+
+import sarif.SarifController;
+import sarif.handlers.SarifResultHandler;
+import sarif.handlers.SarifRunHandler;
+import sarif.managers.ProgramSarifMgr;
+
+/**
+ * Parse the SARIF log into easier to use root structures. And helper functions.
+ * {@link SarifController} to do any additional gui interactions
+ */
+public class SarifDataFrame {
+ private List<SarifColumnKey> columns;
+ private List<Map<String, Object>> tableResults;
+ private Map<String, List<Map<String, Object>>> tableResultsAsMap;
+ private SarifController controller;
+ private Map<String, ToolComponent> componentMap;
+ private Map<String, ReportingDescriptorReference> taxaMap;
+ private String sourceLanguage;
+ private String compiler;
+
+ public SarifDataFrame(SarifSchema210 sarifLog, SarifController controller, boolean parseHeaderOnly) {
+ this.controller = controller;
+
+ columns = new ArrayList<>();
+ tableResults = new ArrayList<>();
+ tableResultsAsMap = new HashMap<>();
+ columns.add(new SarifColumnKey("Tool", true));
+ columns.add(new SarifColumnKey("RuleId", true));
+ columns.add(new SarifColumnKey("Address", false));
+ columns.add(new SarifColumnKey("Message", false));
+ columns.add(new SarifColumnKey("Kind", true));
+ columns.add(new SarifColumnKey("Level", true));
+
+ Set<SarifResultHandler> resultHandlers = controller.getSarifResultHandlers();
+ for (Run run : sarifLog.getRuns()) {
+ parseHeader(run);
+ if (parseHeaderOnly) {
+ continue;
+ }
+ compileComponentMap(run);
+ for (String name :getComponentMap().keySet()) {
+ columns.add(new SarifColumnKey(name, false));
+ }
+ ProgramSarifMgr programMgr = controller.getProgramSarifMgr();
+ for (Entry<String, Boolean> entry : programMgr.getKeys().entrySet()) {
+ columns.add(new SarifColumnKey(entry.getKey(), entry.getValue()));
+ }
+ for (Result result : run.getResults()) {
+ compileTaxaMap(run, result);
+
+ Map<String, Object> curTableResult = new HashMap<>();
+ for (SarifResultHandler handler : resultHandlers) {
+ if (handler.isEnabled()) {
+ handler.handle(this, run, result, curTableResult);
+ }
+ }
+ tableResults.add(curTableResult);
+ String ruleid = (String) curTableResult.get("RuleId");
+ List<Map<String, Object>> list = tableResultsAsMap.get(ruleid);
+ if (list == null) {
+ list = new ArrayList<>();
+ tableResultsAsMap.put(ruleid, list);
+ }
+ list.add(curTableResult);
+ }
+ for (SarifRunHandler handler : controller.getSarifRunHandlers()) {
+ if (handler.isEnabled()) {
+ handler.handle(this, run);
+ }
+ }
+ }
+ }
+
+ private void parseHeader(Run run) {
+ Set<Artifact> artifacts = run.getArtifacts();
+ if (artifacts == null) {
+ return;
+ }
+ Iterator<Artifact> iterator = artifacts.iterator();
+ while (iterator.hasNext()) {
+ Artifact next = iterator.next();
+ sourceLanguage = next.getSourceLanguage();
+ compiler = next.getDescription().getText();
+ }
+ }
+
+ private void compileComponentMap(Run run) {
+ componentMap = new HashMap<>();
+ Set<ToolComponent> taxonomies = run.getTaxonomies();
+ if (taxonomies != null) {
+ for (ToolComponent tc : taxonomies) {
+ componentMap.put(tc.getName(), tc);
+ }
+ }
+ }
+
+ private void compileTaxaMap(Run run, Result result) {
+ taxaMap = new HashMap<>();
+ Set<ToolComponent> taxonomies = run.getTaxonomies();
+ if (taxonomies == null) {
+ return;
+ }
+ List<ToolComponent> view = new ArrayList<>(taxonomies);
+ Set<ReportingDescriptorReference> taxa = result.getTaxa();
+ if (taxa != null) {
+ for (ReportingDescriptorReference ref : taxa) {
+ long idx = (long) ref.getToolComponent().getIndex();
+ if (idx >= 0 && idx < view.size()) {
+ ToolComponent tc = view.get((int) idx);
+ taxaMap.put(tc.getName(), ref);
+ } else {
+ ToolComponentReference tc = ref.getToolComponent();
+ taxaMap.put(tc.getName(), ref);
+ }
+ }
+ }
+ }
+
+ public List<SarifColumnKey> getColumns() {
+ return columns;
+ }
+
+ public List<Map<String, Object>> getTableResults() {
+ return tableResults;
+ }
+
+ public Map<String, List<Map<String, Object>>> getTableResultsAsMap() {
+ return tableResultsAsMap;
+ }
+
+ public SarifController getController() {
+ return controller;
+ }
+
+ public Map<String, ToolComponent> getComponentMap() {
+ return componentMap;
+ }
+
+ public Map<String, ReportingDescriptorReference> getTaxa() {
+ return taxaMap;
+ }
+
+ public String getSourceLanguage() {
+ return sourceLanguage;
+ }
+
+ public String getCompiler() {
+ return compiler;
+ }
+}
+
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifResultsTableModelFactory.java b/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifResultsTableModelFactory.java
new file mode 100644
index 0000000000..04a99bd95d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/model/SarifResultsTableModelFactory.java
@@ -0,0 +1,129 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.model;
+
+import java.util.List;
+import java.util.Map;
+
+import docking.widgets.table.AbstractDynamicTableColumn;
+import docking.widgets.table.TableColumnDescriptor;
+import ghidra.docking.settings.Settings;
+import ghidra.framework.plugintool.PluginTool;
+import ghidra.framework.plugintool.ServiceProvider;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.listing.Program;
+import ghidra.util.datastruct.Accumulator;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.table.AddressBasedTableModel;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Just a generic table model that can handle an arbitrary number and type of
+ * columns
+ *
+ */
+public class SarifResultsTableModelFactory {
+ /*
+ * Created a factory because we supply an arbitrary list of columns, and so to
+ * dynamically create the TableModel it needs to know which columns it needs in
+ * `TableColumnDescriptor` but you can't set the local variable before the
+ * `super` call
+ */
+ private List<SarifColumnKey> sColumns; // Used for the TableColumnDescriptor
+
+ public SarifResultsTableModelFactory(List<SarifColumnKey> cols) {
+ sColumns = cols;
+ }
+
+ public SarifResultsTableModel createModel(String description, PluginTool tool, Program program, SarifDataFrame df) {
+ return new SarifResultsTableModel(description, tool, program, df);
+ }
+
+ @SuppressWarnings("serial")
+ public class SarifResultsTableModel extends AddressBasedTableModel<Map<String, Object>> {
+ private SarifDataFrame df;
+
+ public SarifResultsTableModel(String description, PluginTool tool, Program program, SarifDataFrame df) {
+ super(description, tool, program, null);
+ this.df = df;
+ }
+
+ public SarifDataFrame getDataFrame() {
+ return this.df;
+ }
+
+ @Override
+ public Address getAddress(int row) {
+ return (Address) this.getRowObject(row).get("Address");
+ }
+
+ @Override
+ protected void doLoad(Accumulator<Map<String, Object>> accumulator, TaskMonitor monitor)
+ throws CancelledException {
+ for (Map<String, Object> result : df.getTableResults()) {
+ accumulator.add(result);
+ }
+ }
+
+ @Override
+ protected TableColumnDescriptor<Map<String, Object>> createTableColumnDescriptor() {
+ TableColumnDescriptor<Map<String, Object>> descriptor = new TableColumnDescriptor<>();
+ for (SarifColumnKey column : sColumns) {
+ if (column.getName().equals("Address")) {
+ descriptor.addVisibleColumn(new Column<Address>(column.getName(), Address.class));
+ } else if (column.isHidden()) {
+ descriptor.addHiddenColumn(new Column<String>(column.getName(), String.class));
+ } else {
+ descriptor.addVisibleColumn(new Column<String>(column.getName(), String.class));
+ }
+ }
+ return descriptor;
+ }
+
+ public class Column<T> extends AbstractDynamicTableColumn<Map<String, Object>, T, Object> {
+ private String name;
+ private Class<T> type;
+
+ /**
+ * @param name
+ * @param type Need to specify the type so that the filter table behaves how you
+ * would expect
+ */
+ public Column(String name, Class<T> type) {
+ this.name = name;
+ this.type = type;
+ }
+
+ @Override
+ public String getColumnName() {
+ return this.name;
+ }
+
+ @Override
+ public Class<T> getColumnClass() {
+ return type;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public T getValue(Map<String, Object> rowObject, Settings settings, Object data,
+ ServiceProvider serviceProvider) throws IllegalArgumentException {
+ Object object = rowObject.get(this.name);
+ return (T) object;
+ }
+ }
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/view/ImageArtifactDisplay.java b/Ghidra/Features/Sarif/src/main/java/sarif/view/ImageArtifactDisplay.java
new file mode 100644
index 0000000000..1405f88bfa
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/view/ImageArtifactDisplay.java
@@ -0,0 +1,47 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.view;
+
+import java.awt.image.BufferedImage;
+
+import javax.swing.ImageIcon;
+import javax.swing.JComponent;
+
+import docking.ComponentProvider;
+import docking.widgets.imagepanel.ImagePanel;
+import ghidra.framework.plugintool.PluginTool;
+
+/**
+ * For displaying Image artifacts from a SARIF file
+ *
+ */
+public class ImageArtifactDisplay extends ComponentProvider {
+ public ImagePanel label;
+
+ public ImageArtifactDisplay(PluginTool tool, String name, String owner, BufferedImage img) {
+ super(tool, name, owner);
+ label = new ImagePanel(new ImageIcon(img).getImage());
+ }
+
+ @Override
+ public JComponent getComponent() {
+ return label;
+ }
+
+ public void dispose() {
+ closeComponent();
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/view/SarifResultsTableProvider.java b/Ghidra/Features/Sarif/src/main/java/sarif/view/SarifResultsTableProvider.java
new file mode 100644
index 0000000000..c96d500f20
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/main/java/sarif/view/SarifResultsTableProvider.java
@@ -0,0 +1,143 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif.view;
+
+import java.awt.BorderLayout;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.swing.JComponent;
+import javax.swing.JPanel;
+
+import docking.ComponentProvider;
+import docking.action.DockingAction;
+import ghidra.app.services.GoToService;
+import ghidra.framework.plugintool.Plugin;
+import ghidra.program.model.listing.Program;
+import ghidra.util.table.GhidraFilterTable;
+import ghidra.util.table.GhidraTable;
+import ghidra.util.table.actions.MakeProgramSelectionAction;
+import sarif.SarifController;
+import sarif.handlers.SarifResultHandler;
+import sarif.model.SarifColumnKey;
+import sarif.model.SarifDataFrame;
+import sarif.model.SarifResultsTableModelFactory;
+import sarif.model.SarifResultsTableModelFactory.SarifResultsTableModel;
+
+/**
+ * Show the SARIF result as a table and build possible actions on the table
+ *
+ */
+public class SarifResultsTableProvider extends ComponentProvider {
+
+ private JComponent component;
+ public SarifResultsTableModel model;
+ public GhidraFilterTable<Map<String, Object>> filterTable;
+ public Program program;
+ private Plugin plugin;
+ private SarifController controller;
+
+ public SarifResultsTableProvider(String description, Plugin plugin, SarifController controller, SarifDataFrame df) {
+ super(plugin.getTool(), controller.getProgram().getName(), plugin.getName());
+ this.plugin = plugin;
+ this.controller = controller;
+ this.program = controller.getProgram();
+ SarifResultsTableModelFactory factory = new SarifResultsTableModelFactory(df.getColumns());
+ this.model = factory.createModel(description, plugin.getTool(), program, df);
+ this.component = buildPanel();
+ filterTable.getTable().getSelectionModel().addListSelectionListener(e -> plugin.getTool().contextChanged(this));
+ this.createActions();
+ this.setTransient();
+ }
+
+ private JComponent buildPanel() {
+ JPanel panel = new JPanel(new BorderLayout());
+ filterTable = new GhidraFilterTable<>(this.model);
+ GhidraTable table = (GhidraTable) filterTable.getTable();
+
+ GoToService goToService = this.getTool().getService(GoToService.class);
+ table.installNavigation(plugin.getTool(), goToService.getDefaultNavigatable());
+ table.setNavigateOnSelectionEnabled(true);
+ panel.add(filterTable);
+ return panel;
+ }
+
+ public void dispose() {
+ filterTable.dispose();
+ closeComponent();
+ }
+
+ public void closeComponent() {
+ super.closeComponent();
+ getController().removeProvider(this);
+ }
+
+ @Override
+ public JComponent getComponent() {
+ return component;
+ }
+
+ /**
+ * Columns are added to the table based on if they are required by the SARIF
+ * format or are a taxonomy that the SARIF file defines We "support" certain
+ * taxonomies here by if the names match adding additional context actions that
+ * can be performed
+ */
+ public void createActions() {
+ DockingAction selectionAction = new MakeProgramSelectionAction(this.plugin,
+ (GhidraTable) filterTable.getTable());
+ this.addLocalAction(selectionAction);
+ Set<SarifResultHandler> resultHandlers = controller.getSarifResultHandlers();
+ List<SarifColumnKey> columns = model.getDataFrame().getColumns();
+ List<String> keyNames = new ArrayList<>();
+ for (SarifColumnKey key : columns) {
+ keyNames.add(key.getName());
+ }
+ for (SarifResultHandler handler : resultHandlers) {
+ if (keyNames.contains(handler.getKey())) {
+ if (handler.getActionName() != null) {
+ this.addLocalAction(handler.createAction(this));
+ }
+ }
+ }
+ }
+
+
+ public int getIndex(String key) {
+ List<SarifColumnKey> columns = model.getDataFrame().getColumns();
+ for (SarifColumnKey c : columns) {
+ if (c.getName().equals(key)) {
+ columns.indexOf(c);
+ }
+ }
+ return -1;
+ }
+
+ public Object getValue(int x, int y) {
+ return model.getColumnValueForRow(model.getRowObject(x), y);
+ }
+
+ public Map<String, Object> getRow(int x) {
+ return model.getRowObject(x);
+ }
+
+ public SarifController getController() {
+ return controller;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/AbstractSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/AbstractSarifTest.java
new file mode 100644
index 0000000000..f287ff9b9d
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/AbstractSarifTest.java
@@ -0,0 +1,143 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.StringWriter;
+
+import org.junit.After;
+import org.junit.Before;
+
+import ghidra.program.database.ProgramBuilder;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressFactory;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.MemoryBlock;
+import ghidra.program.util.ProgramDiff;
+import ghidra.test.AbstractGhidraHeadedIntegrationTest;
+import ghidra.util.task.DummyCancellableTaskMonitor;
+import sarif.managers.ProgramSarifMgr;
+
+public class AbstractSarifTest extends AbstractGhidraHeadedIntegrationTest {
+
+ protected ProgramBuilder builder;
+ protected Program program, program2;
+ protected int txIdOut;
+ protected int txIdIn;
+
+ protected Address entry;
+ protected MemoryBlock block;
+ protected DummyCancellableTaskMonitor monitor;
+ protected ProgramSarifMgr mgr;
+ protected AddressSet addrSet;
+
+ protected byte[] asm = { (byte) 0x55, // PUSH EBP
+ (byte) 0x8b, (byte) 0xec, // MOV EBP, ESP
+ (byte) 0x81, (byte) 0xec, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x00, // SUB ESP, 0x200
+ (byte) 0x6a, (byte) 0x00, // PUSH 0x0
+ (byte) 0x68, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, // PUSH 0x100
+ (byte) 0xeb, (byte) 0xee, // JUMP <-
+ (byte) 0xcc, (byte) 0xcc, (byte) 0xcc, (byte) 0xcc, (byte) 0xcc, (byte) 0xcc, // HACK: buffer requires 2 x
+ (byte) 0x80 // proto length
+ };
+
+ protected int[] instOffsets = { 0, 1, 3, 9, 11, 16, 17 };
+
+ public AbstractSarifTest() {
+ super();
+ }
+
+ public ProgramDiff readWriteCompare() throws Exception {
+ SarifProgramOptions options = new SarifProgramOptions();
+ mgr.write(program, addrSet, monitor, options);
+ StringWriter w = (StringWriter) mgr.getBaseWriter();
+ StringBuffer result = w.getBuffer();
+
+ reset();
+ mgr.setFileContents(result.toString());
+ mgr.read(program2, monitor);
+
+ ProgramDiff programDiff = new ProgramDiff(program, program2);
+ AddressSet addressesOnlyInOne = programDiff.getAddressesOnlyInOne();
+ AddressSet addressesOnlyInTwo = programDiff.getAddressesOnlyInTwo();
+ assert (addressesOnlyInOne.isEmpty());
+ assert (addressesOnlyInTwo.isEmpty());
+ return programDiff;
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ program = getProgram("TestOutProgram");
+ program.addConsumer(this);
+
+ txIdOut = program.startTransaction("TestOut");
+
+ if (program.getMemory().isEmpty()) {
+ AddressFactory af = program.getAddressFactory();
+ entry = af.getAddress("01002000");
+ block = program.getMemory().createInitializedBlock("EMPTY", entry, 0x4000, (byte) 0, null, false);
+ assertNotNull(block);
+ }
+
+ addrSet = new AddressSet(program.getMemory());
+
+ monitor = new DummyCancellableTaskMonitor();
+ mgr = new ProgramSarifMgr(program);
+ mgr.useTempFileForBytes(getTestDirectoryPath());
+ }
+
+ protected Program getProgram(String progName) throws Exception {
+ builder = new ProgramBuilder(progName, ProgramBuilder._X86);
+ return builder.getProgram();
+ }
+
+ public void reset() throws Exception {
+ if (program != null) {
+ if (txIdOut != -1) {
+ program.endTransaction(txIdOut, true);
+ }
+ txIdOut = -1;
+ }
+
+ builder = new ProgramBuilder("TestInProgram", ProgramBuilder._X86);
+ program2 = builder.getProgram();
+ program2.addConsumer(this);
+
+ txIdIn = program2.startTransaction("TestIn");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ if (program2 != null) {
+ if (txIdIn != -1) {
+ program2.endTransaction(txIdIn, true);
+ }
+ program2.release(this);
+ }
+ if (program != null) {
+ if (txIdOut != -1) {
+ program.endTransaction(txIdOut, true);
+ }
+ program.release(this);
+ }
+ }
+
+ protected Address addr(long l) {
+ return entry.add(l);
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/BookmarkSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/BookmarkSarifTest.java
new file mode 100644
index 0000000000..436ee9af62
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/BookmarkSarifTest.java
@@ -0,0 +1,47 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Bookmark;
+import ghidra.program.model.listing.BookmarkManager;
+import ghidra.program.util.ProgramDiff;
+
+public class BookmarkSarifTest extends AbstractSarifTest {
+
+ public BookmarkSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testBookMarks() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+
+ BookmarkManager bookmarkManager = program.getBookmarkManager();
+ bookmarkManager.setBookmark(entry, "Alpha", "Assembly Succeeded", "first");
+ bookmarkManager.setBookmark(entry.add(1), "Beta", "Assembly Failed", "second");
+ Bookmark mark = bookmarkManager.setBookmark(entry.add(2), "Gamma", "Bookmark Removed", "third");
+ bookmarkManager.removeBookmark(mark);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/CodeSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/CodeSarifTest.java
new file mode 100644
index 0000000000..26f9f4a544
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/CodeSarifTest.java
@@ -0,0 +1,77 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.AddressRange;
+import ghidra.program.model.address.AddressRangeIterator;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.data.ByteDataType;
+import ghidra.program.model.data.DWordDataType;
+import ghidra.program.model.data.ShortDataType;
+import ghidra.program.model.data.WordDataType;
+import ghidra.program.util.ProgramDiff;
+import sarif.managers.CodeSarifMgr;
+
+public class CodeSarifTest extends AbstractSarifTest
+{
+ public CodeSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testBasic() throws Exception {
+ readWriteCompare();
+ }
+
+ @Test
+ public void testCodeUnits() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+ ProgramDiff programDiff = readWriteCompare();
+ assert(programDiff.memoryMatches());
+ }
+
+ @Test
+ public void testInstructions() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+ MessageLog log = new MessageLog();
+ CodeSarifMgr mgr = new CodeSarifMgr(program, log);
+ AddressSet set = new AddressSet();
+ AddressRangeIterator addressRanges = program.getMemory().getAddressRanges();
+ for (AddressRange r : addressRanges) {
+ set.add(r);
+ }
+ mgr.disassemble(set, monitor);
+
+ ProgramDiff programDiff = readWriteCompare();
+ assert(programDiff.memoryMatches());
+ }
+
+ @Test
+ public void testData() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+ program.getListing().createData(entry, new ByteDataType());
+ program.getListing().createData(entry.add(instOffsets[1]), new ShortDataType());
+ program.getListing().createData(entry.add(instOffsets[2]), new WordDataType());
+ program.getListing().createData(entry.add(instOffsets[3]), new DWordDataType());
+
+ ProgramDiff programDiff = readWriteCompare();
+ assert(programDiff.memoryMatches());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/CommentSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/CommentSarifTest.java
new file mode 100644
index 0000000000..a6a4eb978f
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/CommentSarifTest.java
@@ -0,0 +1,95 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.ArrayDataType;
+import ghidra.program.model.data.ByteDataType;
+import ghidra.program.model.data.DWordDataType;
+import ghidra.program.model.data.ShortDataType;
+import ghidra.program.model.data.Structure;
+import ghidra.program.model.data.StructureDataType;
+import ghidra.program.model.data.Union;
+import ghidra.program.model.data.UnionDataType;
+import ghidra.program.model.data.WordDataType;
+import ghidra.program.model.listing.CodeUnit;
+import ghidra.program.model.listing.Listing;
+import ghidra.program.util.ProgramDiff;
+
+public class CommentSarifTest extends AbstractSarifTest {
+
+ public CommentSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testComments() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+
+ Listing listing = program.getListing();
+ listing.setComment(entry.add(1), CodeUnit.EOL_COMMENT, "My EOL comment");
+ listing.setComment(entry.add(2), CodeUnit.PRE_COMMENT, "My Pre comment");
+ listing.setComment(entry.add(3), CodeUnit.POST_COMMENT, "My Post comment");
+ listing.setComment(entry.add(4), CodeUnit.PLATE_COMMENT, "My Plate comment");
+ listing.setComment(entry.add(5), CodeUnit.REPEATABLE_COMMENT, "My Repeatable comment");
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCommentsInData() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+
+ Listing listing = program.getListing();
+ Structure struct = new StructureDataType("My Struct", 0);
+ Union union = new UnionDataType("My Union");
+ ArrayDataType array = new ArrayDataType(new ShortDataType(), 2, 2);
+ union.add(array, "shortArrayName", "shortArrayComment");
+ union.add(new DWordDataType(), "dwordName", "dwordComment");
+ struct.add(new WordDataType(), "wordName", "wordComment");
+ struct.add(union, "unionName", "unionComment");
+ struct.add(new ByteDataType(), "byteName", "byteComment");
+ listing.createData(entry, struct);
+ listing.setComment(entry.add(4), CodeUnit.PLATE_COMMENT, "My Plate comment");
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCommentsNoYouCantDoThis() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+
+ Listing listing = program.getListing();
+ Structure struct = new StructureDataType("My Struct", 0);
+ struct.add(new WordDataType(), "wordName", "wordComment");
+ struct.add(new ByteDataType(), "byteName", "byteComment");
+ listing.setComment(entry.add(1), CodeUnit.EOL_COMMENT, "My EOL comment");
+ listing.createData(entry, struct);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(!differences.isEmpty());
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/DataTypesSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/DataTypesSarifTest.java
new file mode 100644
index 0000000000..127808a500
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/DataTypesSarifTest.java
@@ -0,0 +1,180 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.Array;
+import ghidra.program.model.data.ArrayDataType;
+import ghidra.program.model.data.ByteDataType;
+import ghidra.program.model.data.CategoryPath;
+import ghidra.program.model.data.FunctionDefinition;
+import ghidra.program.model.data.FunctionDefinitionDataType;
+import ghidra.program.model.data.Pointer;
+import ghidra.program.model.data.Pointer32DataType;
+import ghidra.program.model.data.PointerDataType;
+import ghidra.program.model.data.ProgramBasedDataTypeManager;
+import ghidra.program.model.data.Structure;
+import ghidra.program.model.data.StructureDataType;
+import ghidra.program.model.data.TypeDef;
+import ghidra.program.model.data.TypedefDataType;
+import ghidra.program.model.data.Union;
+import ghidra.program.model.data.UnionDataType;
+import ghidra.program.util.ProgramDiff;
+
+public class DataTypesSarifTest extends AbstractSarifTest {
+
+ public DataTypesSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testCreateCategoryHierarchy() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ String fullName = "/cat1/cat2/cat3/cat4/cat5";
+ CategoryPath cp = new CategoryPath(fullName);
+ dataMgr.createCategory(cp);
+ assertTrue(dataMgr.containsCategory(cp));
+
+ dataMgr.createCategory(cp);
+ assertTrue(dataMgr.containsCategory(cp));
+
+ dataMgr.createCategory(cp);
+ assertTrue(dataMgr.containsCategory(cp));
+
+ cp = new CategoryPath("/A/B/C/D/E/F/G/H");
+ dataMgr.createCategory(cp);
+ assertTrue(dataMgr.containsCategory(cp));
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateArray() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ ArrayDataType adt = new ArrayDataType(new ByteDataType(), 3, 1);
+ Array array = (Array) dataMgr.addDataType(adt, null);
+ assertNotNull(array);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateTypedef() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ ArrayDataType adt = new ArrayDataType(new ByteDataType(), 3, 1);
+ Array array = (Array) dataMgr.addDataType(adt, null);
+ TypedefDataType tdt = new TypedefDataType("ArrayTypedef", array);
+ TypeDef td = (TypeDef) dataMgr.addDataType(tdt, null);
+ assertNotNull(td);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreatePointer() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ ArrayDataType adt = new ArrayDataType(new ByteDataType(), 5, 1);
+ Array array = (Array) dataMgr.addDataType(adt, null);
+ TypedefDataType tdt = new TypedefDataType("ArrayTypedef", array);
+ TypeDef td = (TypeDef) dataMgr.addDataType(tdt, null);
+ PointerDataType pdt = new Pointer32DataType(td);
+ Pointer ptr = (Pointer) dataMgr.addDataType(pdt, null);
+ assertNotNull(ptr);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreatePointers() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ Array array = new ArrayDataType(new ByteDataType(), 5, 1);
+ TypeDef td = new TypedefDataType("ByteTypedef", array);
+ Pointer p = new Pointer32DataType(td);
+ Pointer p2 = new Pointer32DataType(p);
+
+ Pointer ptr = (Pointer) dataMgr.resolve(p2, null);
+ assertNotNull(ptr);
+ assertEquals("ByteTypedef * *", ptr.getMnemonic(null));
+
+ p = new Pointer32DataType(null);
+ ptr = (Pointer) dataMgr.resolve(p, null);
+ assertNotNull(ptr);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateStructure() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ StructureDataType sdt = new StructureDataType("test", 0);
+ Structure struct = (Structure) dataMgr.addDataType(sdt, null);
+ assertNotNull(struct);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateUnion() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ UnionDataType udt = new UnionDataType("test");
+ Union union = (Union) dataMgr.addDataType(udt, null);
+ assertNotNull(union);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateFunctionDef() throws Exception {
+ ProgramBasedDataTypeManager dataMgr = program.getDataTypeManager();
+ FunctionDefinitionDataType fdt =
+ new FunctionDefinitionDataType(new FunctionDefinitionDataType("test"));
+ FunctionDefinition funcDef = (FunctionDefinition) dataMgr.addDataType(fdt, null);
+ assertNotNull(funcDef);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/DefinedDataSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/DefinedDataSarifTest.java
new file mode 100644
index 0000000000..a9a6371976
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/DefinedDataSarifTest.java
@@ -0,0 +1,94 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.ArrayDataType;
+import ghidra.program.model.data.ByteDataType;
+import ghidra.program.model.data.CharDataType;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.IntegerDataType;
+import ghidra.program.model.data.ProgramBasedDataTypeManager;
+import ghidra.program.model.data.ShortDataType;
+import ghidra.program.model.data.Structure;
+import ghidra.program.model.data.StructureDataType;
+import ghidra.program.util.ProgramDiff;
+
+public class DefinedDataSarifTest extends AbstractSarifTest {
+
+ public DefinedDataSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testDefinedData() throws Exception {
+ Structure structA = new StructureDataType("structA", 0);
+ structA.add(IntegerDataType.dataType, "a0", null);
+ structA.add(new ArrayDataType(CharDataType.dataType, 0, -1), "az", null);
+ structA.add(DataType.DEFAULT);
+ structA.add(ByteDataType.dataType, "a1", null);
+ structA.add(new ArrayDataType(IntegerDataType.dataType, 0, -1), "aflex", null);
+
+ /**
+ * /structB
+ * pack()
+ * Structure structB {
+ * 0 short 2 b0 ""
+ * 2 byte[0] 0 bfs ""
+ * 4 int:4(0) 1 bf1 ""
+ * 4 int:6(4) 2 bf2 ""
+ * 5 int:2(2) 1 bf3 ""
+ * 6 structA 6 b1 ""
+ * }
+ * Size = 12 Actual Alignment = 4
+ */
+ Structure structB = new StructureDataType("structB", 0);
+ structB.setPackingEnabled(true);
+ structB.add(ShortDataType.dataType, "b0", null);
+ structB.add(new ArrayDataType(ByteDataType.dataType, 0, -1), "bfs", null);
+ structB.addBitField(IntegerDataType.dataType, 4, "bf1", null);
+ structB.addBitField(IntegerDataType.dataType, 6, "bf2", null);
+ structB.addBitField(IntegerDataType.dataType, 2, "bf3", null);
+ structB.add(structA, "b1", null);
+
+ /**
+ * /structC
+ * pack()
+ * Structure structC {
+ * 0 char 1 c0 ""
+ * 4 structB 12 c1 ""
+ * }
+ * Size = 16 Actual Alignment = 4
+ */
+ Structure structC = new StructureDataType("structC", 0);
+ structC.setPackingEnabled(true);
+ structC.add(CharDataType.dataType, "c0", null);
+ structC.add(structB, "c1", null);
+
+ ProgramBasedDataTypeManager dtm = program.getDataTypeManager();
+ structC = (Structure) dtm.resolve(structC, null);
+
+ program.getListing().createData(addr(0x100), structC);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/EquatesSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/EquatesSarifTest.java
new file mode 100644
index 0000000000..710c9dad4e
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/EquatesSarifTest.java
@@ -0,0 +1,42 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.symbol.EquateTable;
+import ghidra.program.util.ProgramDiff;
+
+public class EquatesSarifTest extends AbstractSarifTest {
+
+ public EquatesSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testCreateEquate() throws Exception {
+ EquateTable equateTable = program.getEquateTable();
+ equateTable.createEquate("Test", 100);
+ equateTable.createEquate("Test-2", 101);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/ExternalsSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/ExternalsSarifTest.java
new file mode 100644
index 0000000000..dfc252c054
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/ExternalsSarifTest.java
@@ -0,0 +1,70 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.symbol.ExternalLocation;
+import ghidra.program.model.symbol.ExternalManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.exception.InvalidInputException;
+
+public class ExternalsSarifTest extends AbstractSarifTest {
+
+ public ExternalsSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testExtLocation() throws Exception {
+ ExternalManager extMgr = program.getExternalManager();
+ ExternalLocation loc1 = extMgr.addExtLocation("ext1", "label0", null, SourceType.USER_DEFINED);
+ assertEquals("ext1", loc1.getLibraryName());
+ assertEquals("label0", loc1.getLabel());
+
+ ExternalLocation loc2 = extMgr.addExtLocation("ext1", "label1", addr(1000), SourceType.USER_DEFINED);
+ assertEquals("ext1", loc2.getLibraryName());
+ assertEquals("label1", loc2.getLabel());
+
+ ExternalLocation loc3 = extMgr.addExtLocation("ext2", "label1", null, SourceType.USER_DEFINED);
+ assertEquals("ext2", loc3.getLibraryName());
+ assertEquals("label1", loc3.getLabel());
+
+ extMgr.addExtLocation("ext2", "label2", null, SourceType.USER_DEFINED);
+
+ extMgr.addExtLocation("ext2", null, addr(2000), SourceType.USER_DEFINED);
+
+ try {
+ extMgr.addExtLocation("ext2", null, null, SourceType.USER_DEFINED);
+ Assert.fail();
+ } catch (InvalidInputException e) {
+ // expected
+ }
+
+ extMgr.addExtLocation("ext1", "label1", addr(1500), SourceType.USER_DEFINED);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/FunctionSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/FunctionSarifTest.java
new file mode 100644
index 0000000000..def6388327
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/FunctionSarifTest.java
@@ -0,0 +1,150 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import ghidra.program.database.function.OverlappingFunctionException;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSet;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.FunctionManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolTable;
+import ghidra.program.model.symbol.SymbolType;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.exception.InvalidInputException;
+
+public class FunctionSarifTest extends AbstractSarifTest {
+
+ public FunctionSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testCreateFunction() throws Exception {
+ FunctionManager functionManager = program.getFunctionManager();
+ SymbolTable symbolTable = program.getSymbolTable();
+ symbolTable.createLabel(addr(100), "foo", SourceType.USER_DEFINED);
+
+ createFunction("foo", addr(100), new AddressSet(addr(100), addr(200)));
+
+ Symbol[] symbols = symbolTable.getSymbols(addr(100));
+ assertEquals(1, symbols.length); // label should be converted to function
+
+ Symbol s = symbolTable.createLabel(addr(100), "foo", SourceType.USER_DEFINED);
+ assertEquals(SymbolType.FUNCTION, s.getSymbolType());
+ symbols = symbolTable.getSymbols(addr(100));
+ assertEquals(1, symbols.length); // should still be just a function
+
+ // Overlapping functions - not allowed
+ try {
+ functionManager.createFunction("foo1", addr(50), new AddressSet(addr(50), addr(100)),
+ SourceType.USER_DEFINED);
+ Assert.fail();
+ }
+ catch (OverlappingFunctionException e) {
+ // Expected
+ }
+ try {
+ functionManager.createFunction("foo2", addr(200), new AddressSet(addr(200), addr(250)),
+ SourceType.USER_DEFINED);
+ Assert.fail();
+ }
+ catch (OverlappingFunctionException e) {
+ // Expected
+ }
+ try {
+ functionManager.createFunction("foo3", addr(150), new AddressSet(addr(150), addr(250)),
+ SourceType.USER_DEFINED);
+ Assert.fail();
+ }
+ catch (OverlappingFunctionException e) {
+ // Expected
+ }
+
+ // Invalid entry address
+ try {
+ createFunction("foo4", addr(250), new AddressSet(addr(300), addr(350)));
+ Assert.fail();
+ }
+ catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ createFunction("foo4", addr(50), new AddressSet(addr(50), addr(99)));
+ createFunction("foo5", addr(201), new AddressSet(addr(201), addr(250)));
+
+ // try duplicate name
+ createFunction("foo5", addr(500), new AddressSet(addr(500), addr(600)));
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateVarArgFunction() throws Exception {
+ Function f = createFunction("foo", addr(100), new AddressSet(addr(100), addr(200)));
+ f.setVarArgs(true);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateInlineFunction() throws Exception {
+ Function f = createFunction("foo", addr(100), new AddressSet(addr(100), addr(200)));
+ f.setInline(true);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateNoReturnFunction() throws Exception {
+ Function f = createFunction("foo", addr(100), new AddressSet(addr(100), addr(200)));
+ assertEquals(false, f.hasNoReturn());
+ f.setNoReturn(true);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ private Function createFunction(String name, Address entryPt, AddressSetView body)
+ throws InvalidInputException, OverlappingFunctionException {
+
+ FunctionManager functionManager = program.getFunctionManager();
+ functionManager.createFunction(name, entryPt, body, SourceType.USER_DEFINED);
+ Function f = functionManager.getFunctionAt(entryPt);
+ assertEquals(name, f.getName());
+ assertEquals(entryPt, f.getEntryPoint());
+ assertEquals(body, f.getBody());
+ return f;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MarkupSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MarkupSarifTest.java
new file mode 100644
index 0000000000..ec852b07d3
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MarkupSarifTest.java
@@ -0,0 +1,86 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.symbol.RefType;
+import ghidra.program.model.symbol.Reference;
+import ghidra.program.model.symbol.ReferenceManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.util.ProgramDiff;
+
+public class MarkupSarifTest extends AbstractSarifTest {
+
+ public MarkupSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testExternalReferences() throws Exception {
+ ReferenceManager refMgr = program.getReferenceManager();
+ refMgr.addExternalReference(entry.add(100), "io.dll", "label", null, SourceType.USER_DEFINED, 0, RefType.DATA);
+
+ refMgr.addExternalReference(entry.add(100), "foo.dll", "ABC", null, SourceType.ANALYSIS, 1, RefType.DATA);
+
+ refMgr.addExternalReference(entry.add(500), "foo.dll", null, entry.add(4000), SourceType.IMPORTED, 2,
+ RefType.READ_WRITE);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+ @Test
+ public void testMemReference() throws Exception {
+ ReferenceManager refMgr = program.getReferenceManager();
+ Reference ref = refMgr.addMemoryReference(entry.add(512), entry.add(256), RefType.FLOW, SourceType.DEFAULT, 2);
+ refMgr.setPrimary(ref, false);
+
+ ref = refMgr.addMemoryReference(entry.add(784), entry.add(256), RefType.FLOW, SourceType.USER_DEFINED, -1);
+ refMgr.setPrimary(ref, false);
+
+ refMgr.addMemoryReference(entry.add(600), entry.add(256), RefType.UNCONDITIONAL_CALL, SourceType.USER_DEFINED, 2);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+ @Test
+ public void testOffsetReference() throws Exception {
+ ReferenceManager refMgr = program.getReferenceManager();
+ refMgr.addOffsetMemReference(entry.add(100), entry.add(600), false, 100,
+ RefType.COMPUTED_JUMP, SourceType.USER_DEFINED, 0);
+
+ refMgr.addMemoryReference(entry.add(100), entry.add(1000), RefType.COMPUTED_JUMP,
+ SourceType.USER_DEFINED, 0);
+
+ refMgr.addMemoryReference(entry.add(100), entry.add(1005), RefType.COMPUTED_JUMP,
+ SourceType.USER_DEFINED, -1);
+
+ refMgr.removeAllReferencesFrom(entry.add(100));
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MemoryMapSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MemoryMapSarifTest.java
new file mode 100644
index 0000000000..74e7cf5221
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MemoryMapSarifTest.java
@@ -0,0 +1,124 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.mem.Memory;
+import ghidra.program.model.mem.MemoryBlock;
+import ghidra.program.model.mem.MemoryBlockType;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.task.TaskMonitor;
+
+public class MemoryMapSarifTest extends AbstractSarifTest {
+
+ public MemoryMapSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testCreateBlock() throws Exception {
+ Memory mem = program.getMemory();
+
+ MemoryBlock block = createBlock("Test", addr(0x4000), 100);
+ addrSet.add(block.getAddressRange());
+
+ // need to create a buffer with junk and free it
+ // to verify that clean buffer is later produced
+ MemoryBlock block2 = createBlock("Test2", addr(0x6000), 0x1000);
+ mem.removeBlock(block2, TaskMonitor.DUMMY);
+ addrSet.add(block2.getAddressRange());
+
+ // Verify buffer
+ block2 = mem.createBlock(block, "Test2", addr(0x6000), 0x1000);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateUninitializedBlock() throws Exception {
+ Memory mem = program.getMemory();
+
+ MemoryBlock ablock = mem.createUninitializedBlock("A", addr(0x4000), 10, false);
+ addrSet.add(ablock.getAddressRange());
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateOverlayBlock() throws Exception {
+ Memory mem = program.getMemory();
+ MemoryBlock oblock = mem.createInitializedBlock(".overlay", addr(0), 0x1000, (byte) 0xa, TaskMonitor.DUMMY,
+ true);
+ addrSet.add(oblock.getAddressRange());
+ assertEquals(MemoryBlockType.DEFAULT, oblock.getType());
+ assertTrue(oblock.isOverlay());
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateBitMappedBlock() throws Exception {
+ Memory mem = program.getMemory();
+ MemoryBlock bitBlock = mem.createBitMappedBlock("bit", addr(0x4000), addr(0xf00), 0x1000, false);
+ addrSet.add(bitBlock.getAddressRange());
+
+ assertEquals(MemoryBlockType.BIT_MAPPED, bitBlock.getType());
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+ @Test
+ public void testCreateByteMappedBlock() throws Exception {
+ Memory mem = program.getMemory();
+ MemoryBlock byteBlock = mem.createByteMappedBlock("byte", addr(0x4000), addr(0xf00), 0x200, false);
+ addrSet.add(byteBlock.getAddressRange());
+
+ assertEquals(MemoryBlockType.BYTE_MAPPED, byteBlock.getType());
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert (differences.isEmpty());
+ }
+
+
+ private MemoryBlock createBlock(String name, Address start, long size) throws Exception {
+ return createBlock(name, start, size, 0);
+ }
+
+ private MemoryBlock createBlock(String name, Address start, long size, int initialValue) throws Exception {
+ return program.getMemory().createInitializedBlock(name, start, size, (byte) initialValue, TaskMonitor.DUMMY,
+ false);
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MergeCalcOverlaySarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MergeCalcOverlaySarifTest.java
new file mode 100644
index 0000000000..87ea4ca2c6
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MergeCalcOverlaySarifTest.java
@@ -0,0 +1,83 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+import generic.test.AbstractGenericTest;
+import ghidra.program.database.ProgramBuilder;
+import ghidra.program.database.ProgramDB;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.UniversalID;
+import ghidra.util.UniversalIdGenerator;
+import ghidra.util.exception.AssertException;
+
+public class MergeCalcOverlaySarifTest extends AbstractSarifTest {
+
+ private UniversalID lastGeneratedUniversalID;
+ private Object consumer;
+
+
+ public MergeCalcOverlaySarifTest() {
+ super();
+ }
+
+ @Test
+ public void testCalcOverlay() throws Exception {
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ protected Program getProgram(String progName) throws Exception {
+ return buildOverlayCalcExeProgram();
+ }
+
+ private ProgramDB buildOverlayCalcExeProgram() throws Exception {
+
+ ProgramBuilder builder = new ProgramBuilder("calc.exe", ProgramBuilder._TOY, consumer);
+
+ builder.createMemory(".text", "0x1001000", 0x12600);
+ builder.createMemory(".data", "0x1014000", 0xc00);
+ builder.createMemory(".data", "0x10150bf", 0x4c0);
+ builder.createMemory(".rsrc", "0x1018bff", 0x2c00);
+ builder.createOverlayMemory("TextOverlay", "0x01001630", 0x200);
+
+ builder.setProperty(Program.DATE_CREATED, new Date(100000000)); // arbitrary, but consistent
+
+ ProgramDB program = builder.getProgram();
+
+ AbstractGenericTest.setInstanceField("recordChanges", program, Boolean.TRUE);
+
+ UniversalID ID = UniversalIdGenerator.nextID();
+
+ if (lastGeneratedUniversalID != null) {
+ if (!lastGeneratedUniversalID.equals(ID)) {
+ // if this happens, update initializeStaticUniversalIDUsage()
+ throw new AssertException("Expected Test UniversalID has changed. "
+ + "This is probably due to an new static usage of the UniversalIDGenerator.");
+ }
+ }
+
+ return program;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MergeCalcSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MergeCalcSarifTest.java
new file mode 100644
index 0000000000..6bd76dac90
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MergeCalcSarifTest.java
@@ -0,0 +1,81 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+import generic.test.AbstractGenericTest;
+import ghidra.program.database.ProgramBuilder;
+import ghidra.program.database.ProgramDB;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.UniversalID;
+import ghidra.util.UniversalIdGenerator;
+import ghidra.util.exception.AssertException;
+
+public class MergeCalcSarifTest extends AbstractSarifTest {
+
+ private UniversalID lastGeneratedUniversalID;
+ private Object consumer;
+
+
+ public MergeCalcSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testCalc() throws Exception {
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ protected Program getProgram(String progName) throws Exception {
+ return buildCalcExeProgram();
+ }
+
+ private ProgramDB buildCalcExeProgram() throws Exception {
+
+ ProgramBuilder builder = new ProgramBuilder("calc.exe", ProgramBuilder._TOY, consumer);
+
+ builder.createMemory(".text", "0x1001000", 0x12600);
+ builder.createMemory(".data", "0x1014000", 0xc00);
+ builder.createMemory(".data", "0x10150bf", 0x4c0);
+ builder.createMemory(".rsrc", "0x1018bff", 0x2c00);
+
+ builder.setProperty(Program.DATE_CREATED, new Date(100000000)); // arbitrary, but consistent
+
+ ProgramDB program = builder.getProgram();
+
+ AbstractGenericTest.setInstanceField("recordChanges", program, Boolean.TRUE);
+
+ UniversalID ID = UniversalIdGenerator.nextID();
+
+ if (lastGeneratedUniversalID != null) {
+ if (!lastGeneratedUniversalID.equals(ID)) {
+ // if this happens, update initializeStaticUniversalIDUsage()
+ throw new AssertException("Expected Test UniversalID has changed. "
+ + "This is probably due to an new static usage of the UniversalIDGenerator.");
+ }
+ }
+
+ return program;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MergeMipsSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MergeMipsSarifTest.java
new file mode 100644
index 0000000000..a49de87487
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MergeMipsSarifTest.java
@@ -0,0 +1,98 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+import generic.test.AbstractGenericTest;
+import ghidra.program.database.ProgramBuilder;
+import ghidra.program.database.ProgramDB;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.Program;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.UniversalID;
+import ghidra.util.UniversalIdGenerator;
+import ghidra.util.exception.AssertException;
+
+public class MergeMipsSarifTest extends AbstractSarifTest {
+
+ private UniversalID lastGeneratedUniversalID;
+ private Object consumer;
+
+
+ public MergeMipsSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testMips() throws Exception {
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ protected Program getProgram(String progName) throws Exception {
+ return buildR4000Program();
+ }
+
+ public void reset() throws Exception {
+ if (program != null) {
+ if (txIdOut != -1) {
+ program.endTransaction(txIdOut, true);
+ }
+ txIdOut = -1;
+ }
+
+ builder = new ProgramBuilder("TestInProgram", ProgramBuilder._MIPS);
+ program2 = builder.getProgram();
+ program2.addConsumer(this);
+
+ txIdIn = program2.startTransaction("TestIn");
+ }
+
+ private ProgramDB buildR4000Program() throws Exception {
+
+ ProgramBuilder builder = new ProgramBuilder("r4000", ProgramBuilder._MIPS, consumer);
+
+ builder.createMemory("label", "08000", 0x1000);
+ builder.setBytes(
+ "808c",
+ "0c 10 d0 2b 24 04 00 33 0c 10 7e a4 24 16 00 30 af a2 00 44 40 04 78 00 00 00 00 00 00 04 12 02 30 42 00 ff",
+ true);
+ builder.setBytes("80b0", "24 03 00 27 14 43 00 0a 30 82 00 ff 2c 42 00 21 14 40 00 07",
+ true);
+ builder.setProperty(Program.DATE_CREATED, new Date(100000000)); // arbitrary, but consistent
+
+ ProgramDB program = builder.getProgram();
+
+ AbstractGenericTest.setInstanceField("recordChanges", program, Boolean.TRUE);
+
+ UniversalID ID = UniversalIdGenerator.nextID();
+
+ if (lastGeneratedUniversalID != null) {
+ if (!lastGeneratedUniversalID.equals(ID)) {
+ // if this happens, update initializeStaticUniversalIDUsage()
+ throw new AssertException("Expected Test UniversalID has changed. "
+ + "This is probably due to an new static usage of the UniversalIDGenerator.");
+ }
+ }
+
+ return program;
+ }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MergeNotepadSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MergeNotepadSarifTest.java
new file mode 100644
index 0000000000..5c0a87e53f
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MergeNotepadSarifTest.java
@@ -0,0 +1,223 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+import generic.test.AbstractGenericTest;
+import ghidra.program.database.ProgramBuilder;
+import ghidra.program.database.ProgramDB;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.IntegerDataType;
+import ghidra.program.model.data.Pointer32DataType;
+import ghidra.program.model.data.Undefined1DataType;
+import ghidra.program.model.data.Undefined4DataType;
+import ghidra.program.model.lang.CompilerSpec;
+import ghidra.program.model.listing.BookmarkType;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.ParameterImpl;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.RefType;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.UniversalID;
+import ghidra.util.UniversalIdGenerator;
+import ghidra.util.exception.AssertException;
+
+public class MergeNotepadSarifTest extends AbstractSarifTest {
+
+ private UniversalID lastGeneratedUniversalID;
+ private Object consumer;
+
+
+ public MergeNotepadSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testNotepad() throws Exception {
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ protected Program getProgram(String progName) throws Exception {
+ return buildNotepadMergeListingTest_X86Program();
+ }
+
+ private ProgramDB buildNotepadMergeListingTest_X86Program() throws Exception {
+ ProgramBuilder builder =
+ new ProgramBuilder("buildNotepadMergeListingTest_X86Program", ProgramBuilder._X86,
+ consumer);
+
+ builder.createMemory(".text", "0x1001000", 0x6600);
+ builder.createMemory(".data", "0x1008000", 0x600);
+ builder.createMemory(".data", "0x1008600", 0x1344);
+ builder.createMemory(".rsrc", "0x100a000", 0x5400);
+
+ builder.createNamespace("FirstNamespace");
+ builder.createNamespace("SecondNamespace");
+ builder.createNamespace("EmptyNamespace");
+ builder.createNamespace("SubNamespace", "SecondNamespace", SourceType.USER_DEFINED);
+
+ builder.createLabel("0x100e483", "AAA");
+
+ Function function =
+ builder.createEmptyFunction("FUN_100248f", "0x100248f", 1214, new Undefined1DataType());
+
+ String namespace = function.getName();
+ builder.createLabel("0x1002691", "AAA", namespace);
+
+ DataType dt = new Undefined4DataType();
+ function = builder.createEmptyFunction("entry", "0x1006420", 396, new Undefined1DataType());
+ namespace = function.getName();
+ builder.createLocalVariable(function, "local_1", dt, -0x6c);
+ builder.createLocalVariable(function, "local_2", dt, -0x68);
+ builder.createLocalVariable(function, "local_3", dt, -0x70);
+
+ builder.createLabel("0x1006420", "ABC", namespace);
+
+ function =
+ builder.createEmptyFunction("FUN_10033f6", "0x10033f6", 87, new Undefined1DataType());
+ namespace = function.getName();
+ builder.createLabel("0x1003439", "BBB", namespace);
+
+ builder.createLabel("0x10044d0", "DDD");
+ builder.createLabel("0x1006420", "DEF");
+
+ function =
+ builder.createEmptyFunction("FUN_1004444", "0x1004444", 652, new Undefined1DataType());
+ namespace = function.getName();
+ builder.createLabel("0x10044d0", "DDD6");
+
+ builder.createLabel("0x1004bdc", "EEE");
+
+ function =
+ builder.createEmptyFunction("FUN_1004bc0", "FirstNamespace", "0x1004bc0", 93,
+ new Undefined1DataType());
+ namespace = function.getName();
+ builder.createLabel("0x1004bdc", "EE4", namespace);
+
+ builder.createLabel("0x1003075", "QQQ"); // Instruction Label?
+ builder.createLabel("0x1002721", "XXX");
+ builder.createLabel("0x1003075", "YYY");
+ builder.createLabel("0x1003075", "ZZZ");
+
+ // bookmarks
+ builder.createBookmark("0x1001978", BookmarkType.ANALYSIS, "Found Code",
+ "Found code from operand reference");
+ builder.createBookmark("0x100248f", BookmarkType.ANALYSIS, "Found Code",
+ "Found code from operand reference");
+ builder.createBookmark("0x1001978", BookmarkType.ANALYSIS, "Found Code",
+ "Found code from operand reference");
+ builder.createBookmark("0x1002f01", BookmarkType.ANALYSIS, "Found Code",
+ "Found code from operand reference");
+
+ //
+ //
+ //
+ builder.setProperty(Program.DATE_CREATED, new Date(100000000)); // arbitrary, but consistent
+
+ ParameterImpl p = new ParameterImpl(null, dt, builder.getProgram());
+ builder.createEmptyFunction(null, "1002c93", 20, null, p, p, p);
+ Function fun = builder.createEmptyFunction(null, "100415a", 20, null, p, p, p);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+ builder.createLocalVariable(fun, "local_2", dt, -12);
+ builder.createLocalVariable(fun, "local_3", dt, -16);
+
+ fun = builder.createEmptyFunction(null, "1002a91", 20, null, p);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+ builder.createLocalVariable(fun, "local_2", dt, -12);
+ builder.createLocalVariable(fun, "local_3", dt, -16);
+
+ fun = builder.createEmptyFunction(null, "1002b44", 20, null, p);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+
+ fun = builder.createEmptyFunction(null, "1003ac0", 20, null, p, p, p, p);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+ builder.createLocalVariable(fun, "local_2", dt, -12);
+
+ fun = builder.createEmptyFunction(null, "1004c1d", 20, null, p, p);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+ builder.createLocalVariable(fun, "local_2", dt, -12);
+
+ builder.createEmptyFunction(null, "0x10021f3", 10, null);
+ builder.createEmptyFunction(null, "0x1001ae3", 1721, null);
+
+ fun = builder.createEmptyFunction(null, "SecondNamespace", "0x1002239", 10, null, p);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+
+ fun =
+ builder.createEmptyFunction("FUN_1003bed", "0x1003bed", 783, new Undefined1DataType(),
+ p);
+ namespace = fun.getName();
+ builder.createLabel("0x1003e25", "CCC", namespace);
+ builder.applyDataType("0x1001004", new Pointer32DataType(), 4);
+ builder.setBytes("100642f", "64 a1 a0 00 00 00 50 64 89 25 00 00 00", true);
+
+ // RefMergeRegTest
+ builder.createEmptyFunction(null, "10018a0", 47, null);
+ builder.createRegisterReference("10018a6", "ESI", 0);
+
+ builder.createEmptyFunction(null, "1002cf5", 40, null);
+ builder.setBytes("1002d0b", "8b f8", true);
+ builder.createRegisterReference("1002d0b", "EDI", 0);
+ builder.createRegisterReference("1002d0b", "EAX", 1);
+ builder.setBytes("1002d18", "8d 44 00 02", true);
+
+ builder.createEmptyFunction(null, "1002950", 40, null);
+
+ builder.setBytes("100295a", "66 85 c9", true);
+ builder.createRegisterReference("100295a", "CX", 1);
+
+// builder.createEmptyFunction(null, "10033f6", 40, null);
+ builder.createRegisterReference("10033fe", "EDI", 0);
+
+ builder.createStackReference("1001af5", RefType.READ, -0x24c, SourceType.USER_DEFINED, 0);
+ builder.setBytes("1001aec", "66 a1 98 33 00 01", true);
+ builder.setBytes("1001b03", "8d bd ba fd ff ff", true);
+ builder.createStackReference("1001b03", RefType.READ, -0x24a, SourceType.DEFAULT, 1);
+
+ builder.createStackReference("1002125", RefType.READ, -0x10, SourceType.USER_DEFINED, 0);
+
+ builder.createClassNamespace("MyClass", null, SourceType.USER_DEFINED);
+
+ builder.createEmptyFunction(null, "MyClass", CompilerSpec.CALLING_CONVENTION_thiscall,
+ "0x1002249", 10, IntegerDataType.dataType, IntegerDataType.dataType);
+ builder.createLocalVariable(fun, "local_1", dt, -8);
+
+ ProgramDB program = builder.getProgram();
+
+ AbstractGenericTest.setInstanceField("recordChanges", program, Boolean.TRUE);
+
+ UniversalID ID = UniversalIdGenerator.nextID();
+
+ if (lastGeneratedUniversalID != null) {
+ if (!lastGeneratedUniversalID.equals(ID)) {
+ // if this happens, update initializeStaticUniversalIDUsage()
+ throw new AssertException("Expected Test UniversalID has changed. "
+ + "This is probably due to an new static usage of the UniversalIDGenerator.");
+ }
+ }
+
+ return program;
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/MergeWallaceSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/MergeWallaceSarifTest.java
new file mode 100644
index 0000000000..26b861ce5b
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/MergeWallaceSarifTest.java
@@ -0,0 +1,113 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+import generic.test.AbstractGenericTest;
+import ghidra.program.database.ProgramBuilder;
+import ghidra.program.database.ProgramDB;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.PointerDataType;
+import ghidra.program.model.data.Undefined4DataType;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.Parameter;
+import ghidra.program.model.listing.ParameterImpl;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.symbol.RefType;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.UniversalID;
+import ghidra.util.UniversalIdGenerator;
+import ghidra.util.exception.AssertException;
+
+public class MergeWallaceSarifTest extends AbstractSarifTest {
+
+ private UniversalID lastGeneratedUniversalID;
+ private Object consumer;
+
+
+ public MergeWallaceSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testWallace() throws Exception {
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ protected Program getProgram(String progName) throws Exception {
+ return buildWallaceSrcProgram();
+ }
+
+ private ProgramDB buildWallaceSrcProgram() throws Exception {
+
+ ProgramBuilder builder = new ProgramBuilder("wallaceSrc", ProgramBuilder._X86, consumer);
+
+ builder.createMemory(".text", "0x401000", 0xc00);
+ builder.createMemory(".rdata", "0x402000", 0x800);
+ builder.createMemory(".data", "0x403000", 0x200);
+ builder.createMemory(".data", "0x403200", 0x190);
+ Program program = builder.getProgram();
+ DataType dt = new PointerDataType();
+ Parameter p1 = new ParameterImpl(null, dt, program);
+ Parameter p_list = new ParameterImpl("list", dt, program);
+ Parameter p_personName = new ParameterImpl("personName", dt, program);
+
+ builder.createEmptyFunction("deplayGadget", "4011f0", 204, null);
+ builder.createEmptyFunction("Gadget", "401000", 48, null, p1);
+ builder.createEmptyFunction("use", "401040", 48, null, p1);
+ builder.createEmptyFunction("addPerson", "4011a0", 48, null, p_list, p_personName);
+ builder.createEmptyFunction("__SEH_prolog4", "4019ac", 69, null, p1, p1, p1);
+ builder.createEmptyFunction("main", "4012c0", 53, null);
+ builder.createStackReference("4012ef", RefType.READ, -8, SourceType.USER_DEFINED, 0);
+ Function fun = program.getFunctionManager().getFunctionAt(builder.addr("4012c0"));
+ builder.createLocalVariable(fun, null, new Undefined4DataType(), -0x8);
+
+ fun = builder.createEmptyFunction("initializePeople", "401150", 69, null, p1);
+ builder.createLocalVariable(fun, null, new Undefined4DataType(), -0x8);
+
+ fun = builder.createEmptyFunction("Canary_Init_00401a58", "401a58", 15, null);
+ builder.createLocalVariable(fun, null, new Undefined4DataType(), -0x8);
+ builder.createLocalVariable(fun, null, new Undefined4DataType(), -0xc);
+
+ //
+ //
+ //
+ builder.setProperty(Program.DATE_CREATED, new Date(100000000)); // arbitrary, but consistent
+ program = builder.getProgram();
+ AbstractGenericTest.setInstanceField("recordChanges", program, Boolean.TRUE);
+
+ UniversalID ID = UniversalIdGenerator.nextID();
+
+ if (lastGeneratedUniversalID != null) {
+ if (!lastGeneratedUniversalID.equals(ID)) {
+ // if this happens, update initializeStaticUniversalIDUsage()
+ throw new AssertException("Expected Test UniversalID has changed. "
+ + "This is probably due to an new static usage of the UniversalIDGenerator.");
+ }
+ }
+
+ return builder.getProgram();
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/ProgramTreeSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/ProgramTreeSarifTest.java
new file mode 100644
index 0000000000..92f1149483
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/ProgramTreeSarifTest.java
@@ -0,0 +1,54 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.util.ProgramDiff;
+
+public class ProgramTreeSarifTest extends AbstractSarifTest {
+
+ public ProgramTreeSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testProgamTrees() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+
+ builder.createProgramTree("Main Tree");
+ builder.getOrCreateModule("Main Tree", "A");
+ builder.getOrCreateModule("Main Tree", "A.B");
+ builder.createProgramTree("Tree One");
+ builder.createProgramTree("Tree Two");
+ builder.createProgramTree("Tree Three");
+ builder.getOrCreateModule("Tree Three", "Strings");
+
+ builder.createFragment("Main Tree", "A", "a", "0x1002000", "0x1002002");
+ builder.createFragment("Main Tree", "A", "b", "0x1002003", "0x1002005");
+ builder.createFragment("Main Tree", "A", "c", "0x1002010", "0x1002020");
+ builder.createFragment("Main Tree", "A.B", "d", "0x1002024", "0x1002026");
+ builder.createFragment("Main Tree", "A.B", "e", "0x1002026", "0x1002028");
+ builder.createFragment("Main Tree", "A.B", "f", "0x1002028", "0x100202a");
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/PropertiesSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/PropertiesSarifTest.java
new file mode 100644
index 0000000000..a92ae38eea
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/PropertiesSarifTest.java
@@ -0,0 +1,67 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import generic.theme.GThemeDefaults.Colors.Palette;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.util.LongPropertyMap;
+import ghidra.program.model.util.PropertyMapManager;
+import ghidra.program.model.util.VoidPropertyMap;
+import ghidra.program.util.ProgramDiff;
+import ghidra.util.SaveableColor;
+
+public class PropertiesSarifTest extends AbstractSarifTest {
+
+ public PropertiesSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testPropertyMaps() throws Exception {
+ block.putBytes(entry, asm, 0, asm.length);
+
+ PropertyMapManager propertyManager = program.getUsrPropertyManager();
+ LongPropertyMap longMap = propertyManager.createLongPropertyMap("LongMap");
+ longMap.add(entry.add(3), 6L);
+ longMap.add(entry.add(4), 12L);
+ VoidPropertyMap voidMap = propertyManager.createVoidPropertyMap("VoidMap");
+ voidMap.add(entry.add(10));
+
+ builder.setIntProperty(entry.add(1).toString(), "IntMap", 5);
+ builder.setIntProperty(entry.add(2).toString(), "IntMap", 10);
+ builder.setStringProperty(entry.add(5).toString(), "StringMap", "TESTING");
+ builder.setStringProperty(entry.add(6).toString(), "StringMap", "1");
+ builder.setObjectProperty(entry.add(7).toString(), "ObjectMap", new SaveableColor(Palette.CYAN));
+ builder.setObjectProperty(entry.add(8).toString(), "ObjectMap", new SaveableColor(Palette.BLACK));
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+// @Test
+// public void testPropertyList() throws Exception {
+// block.putBytes(entry, asm, 0, asm.length);
+//
+// ProgramDiff programDiff = readWriteCompare();
+//
+// AddressSetView differences = programDiff.getDifferences(monitor);
+// assert(differences.isEmpty());
+// }
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/RegisterValuesSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/RegisterValuesSarifTest.java
new file mode 100644
index 0000000000..fbe5997996
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/RegisterValuesSarifTest.java
@@ -0,0 +1,44 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import java.math.BigInteger;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.listing.ProgramContext;
+import ghidra.program.util.ProgramDiff;
+
+public class RegisterValuesSarifTest extends AbstractSarifTest {
+
+ public RegisterValuesSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testRegisterValues() throws Exception {
+ ProgramContext programContext = program.getProgramContext();
+ programContext.setValue(programContext.getBaseContextRegister(), addr(0x1f00), addr(0x1fff),
+ BigInteger.ONE);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}
diff --git a/Ghidra/Features/Sarif/src/test/java/sarif/SymbolSarifTest.java b/Ghidra/Features/Sarif/src/test/java/sarif/SymbolSarifTest.java
new file mode 100644
index 0000000000..baa2ce8ae5
--- /dev/null
+++ b/Ghidra/Features/Sarif/src/test/java/sarif/SymbolSarifTest.java
@@ -0,0 +1,93 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sarif;
+
+import org.junit.Test;
+
+import ghidra.program.model.address.Address;
+import ghidra.program.model.address.AddressSetView;
+import ghidra.program.model.symbol.Namespace;
+import ghidra.program.model.symbol.RefType;
+import ghidra.program.model.symbol.ReferenceManager;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
+import ghidra.program.model.symbol.SymbolTable;
+import ghidra.program.util.ProgramDiff;
+
+public class SymbolSarifTest extends AbstractSarifTest {
+
+ public SymbolSarifTest() {
+ super();
+ }
+
+ @Test
+ public void testSymbolWhereDefault() throws Exception {
+ Address addr = addr(0x200);
+ ReferenceManager refMgr = program.getReferenceManager();
+ refMgr.addMemoryReference(addr(0x220), addr(0x200), RefType.FLOW, SourceType.USER_DEFINED,
+ 0);
+
+ SymbolTable st = program.getSymbolTable();
+ Symbol[] symbols = st.getSymbols(addr);
+
+ st.createLabel(addr, "lamp", SourceType.USER_DEFINED);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+
+ st.removeSymbolSpecial(symbols[0]);
+
+ programDiff = readWriteCompare();
+
+ differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testSymbolsWhereNoDefault() throws Exception {
+ Address addr = addr(0x0200);
+
+ SymbolTable st = program.getSymbolTable();
+ st.createLabel(addr, "lamp", SourceType.USER_DEFINED);
+
+ st.createLabel(addr, "shade", SourceType.ANALYSIS);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+ @Test
+ public void testSymbolsNamespace() throws Exception {
+ Address addr = addr(0);
+
+ SymbolTable st = program.getSymbolTable();
+ Namespace newspace = st.createNameSpace(null, "other", SourceType.USER_DEFINED);
+
+ st.createLabel(addr, "foo", newspace, SourceType.USER_DEFINED);
+
+ st.createLabel(addr, "bar", SourceType.IMPORTED);
+
+ ProgramDiff programDiff = readWriteCompare();
+
+ AddressSetView differences = programDiff.getDifferences(monitor);
+ assert(differences.isEmpty());
+ }
+
+}