diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java b/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java index 441dbbe209..a9f5c62edb 100644 --- a/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java +++ b/Ghidra/Features/Sarif/src/main/java/sarif/SarifUtils.java @@ -119,9 +119,9 @@ public class SarifUtils { } public static Address locationToAddress(Location location, Program program, boolean useOverlays) { - if (!populating) { - throw new RuntimeException("Locations valid only during population phase"); - } + //if (!populating) { + // Msg.warn(program, "Locations valid only during population phase"); + //} Long addr = -1L; PhysicalLocation physicalLocation = location.getPhysicalLocation(); if (location.getPhysicalLocation() != null) { @@ -358,10 +358,12 @@ public class SarifUtils { edgeDsts.clear(); currentRun = run; addresses = run.getAddresses(); - for (com.contrastsecurity.sarif.Address sarifAddr : addresses) { - Long offset = sarifAddr.getAbsoluteAddress(); - String fqname = sarifAddr.getFullyQualifiedName(); - nameToOffset.put(fqname, offset); + if (addresses != null) { + for (com.contrastsecurity.sarif.Address sarifAddr : addresses) { + Long offset = sarifAddr.getAbsoluteAddress(); + String fqname = sarifAddr.getFullyQualifiedName(); + nameToOffset.put(fqname, offset); + } } Set runLocs = run.getLogicalLocations(); if (runLocs != null) { @@ -369,40 +371,42 @@ public class SarifUtils { runLocs.toArray(llocs); } Set rgraphs = run.getGraphs(); - for (Graph rg : rgraphs) { - Set edges = rg.getEdges(); - for (Edge e : edges) { - String id = e.getId(); - String src = e.getSourceNodeId(); - String dst = e.getTargetNodeId(); - String desc = e.getLabel().getText(); - edgeSrcs.put(id, src); - edgeDsts.put(id, dst); - Set set = edgeDescs.get(desc); - if (set == null) { - set = new HashSet<>(); - edgeDescs.put(desc, set); - } - set.add(id); - } - Set nodes = rg.getNodes(); - for (Node n : nodes) { - String id = n.getId(); - Location loc = n.getLocation(); - if (loc != null) { - Set logicalLocations = loc.getLogicalLocations(); - LogicalLocation[] nodells = new LogicalLocation[logicalLocations.size()]; - int i = 0; - for (LogicalLocation ll : logicalLocations) { - // NB: These have to be derefenced immediately as they will be invalid for subsequent queries - if (ll.getFullyQualifiedName() != null) { - nodells[i++] = ll; - } - else { - nodells[i++] = llocs[ll.getIndex().intValue()]; - } + if (rgraphs != null) { + for (Graph rg : rgraphs) { + Set edges = rg.getEdges(); + for (Edge e : edges) { + String id = e.getId(); + String src = e.getSourceNodeId(); + String dst = e.getTargetNodeId(); + String desc = e.getLabel().getText(); + edgeSrcs.put(id, src); + edgeDsts.put(id, dst); + Set set = edgeDescs.get(desc); + if (set == null) { + set = new HashSet<>(); + edgeDescs.put(desc, set); + } + set.add(id); + } + Set nodes = rg.getNodes(); + for (Node n : nodes) { + String id = n.getId(); + Location loc = n.getLocation(); + if (loc != null) { + Set logicalLocations = loc.getLogicalLocations(); + LogicalLocation[] nodells = new LogicalLocation[logicalLocations.size()]; + int i = 0; + for (LogicalLocation ll : logicalLocations) { + // NB: These have to be derefenced immediately as they will be invalid for subsequent queries + if (ll.getFullyQualifiedName() != null) { + nodells[i++] = ll; + } + else { + nodells[i++] = llocs[ll.getIndex().intValue()]; + } + } + nodeLocs.put(id, nodells); } - nodeLocs.put(id, nodells); } } } 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 index 324fcc90b6..003c868de4 100644 --- a/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifClassesNamespaceWriter.java +++ b/Ghidra/Features/Sarif/src/main/java/sarif/export/extlib/SarifClassesNamespaceWriter.java @@ -77,14 +77,16 @@ public class SarifClassesNamespaceWriter extends AbstractExtWriter { objects.add(getTree(sarif)); SymbolIterator symbols = symbolTable.getSymbols(cls); - while (symbols.hasNext()) { - Symbol sym = symbols.next(); - if (cls.isExternal()) { + if (cls.isExternal()) { + while (symbols.hasNext()) { + Symbol sym = symbols.next(); 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)); + if (loc != null) { + ExtLibraryLocation obj = new ExtLibraryLocation(loc); + SarifObject sarif2 = new SarifObject(ExternalLibSarifMgr.SUBKEY1, ExternalLibSarifMgr.KEY, getTree(obj), + loc.getAddress(), loc.getAddress()); + objects.add(getTree(sarif2)); + } } } } diff --git a/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java b/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java index 92208bdd10..104b53479b 100644 --- a/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java +++ b/Ghidra/Features/Sarif/src/main/java/sarif/managers/FunctionsSarifMgr.java @@ -54,22 +54,10 @@ public class FunctionsSarifMgr extends SarifMgr { FID_BOOKMARK_CATEGORY); private DtParser dtParser; - private Library extenalNamespace; + private Library externalNamespace; FunctionsSarifMgr(Program program, MessageLog log) { super(KEY, program, log); - int txId = program.startTransaction("SARIF FunctionMgr"); - try { - SymbolTable symbolTable = program.getSymbolTable(); - Symbol extLib = symbolTable.getLibrarySymbol(""); - if (extLib == null) { - extenalNamespace = symbolTable.createExternalLibrary(Library.UNKNOWN, SourceType.IMPORTED); - } - } catch (Exception e) { - log.appendException(e); - } finally { - program.endTransaction(txId, true); - } } //////////////////////////// @@ -233,6 +221,24 @@ public class FunctionsSarifMgr extends SarifMgr { return func; } + private Library getExternalNamespace(Program program, MessageLog log) { + if (externalNamespace == null) { + int txId = program.startTransaction("SARIF FunctionMgr"); + try { + SymbolTable symbolTable = program.getSymbolTable(); + Symbol extLib = symbolTable.getLibrarySymbol(""); + if (extLib == null) { + externalNamespace = symbolTable.createExternalLibrary(Library.UNKNOWN, SourceType.IMPORTED); + } + } catch (Exception e) { + log.appendException(e); + } finally { + program.endTransaction(txId, true); + } + } + return externalNamespace; + } + private void setName(Address entryPoint, Function func, String name, Map result) { SymbolPath path = new SymbolPath(name); if (name != null) { @@ -248,33 +254,31 @@ public class FunctionsSarifMgr extends SarifMgr { } 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; - } + 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... } + if (path.getName().contains(Library.UNKNOWN)) { + ns = getExternalNamespace(program, log); + } + 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... } }