Merge remote-tracking branch 'origin/Ghidra_11.4'

This commit is contained in:
ghidra1
2025-06-05 17:55:06 -04:00
3 changed files with 95 additions and 85 deletions

View File

@@ -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<LogicalLocation> runLocs = run.getLogicalLocations();
if (runLocs != null) {
@@ -369,40 +371,42 @@ public class SarifUtils {
runLocs.toArray(llocs);
}
Set<Graph> rgraphs = run.getGraphs();
for (Graph rg : rgraphs) {
Set<Edge> 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<String> set = edgeDescs.get(desc);
if (set == null) {
set = new HashSet<>();
edgeDescs.put(desc, set);
}
set.add(id);
}
Set<Node> nodes = rg.getNodes();
for (Node n : nodes) {
String id = n.getId();
Location loc = n.getLocation();
if (loc != null) {
Set<LogicalLocation> 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<Edge> 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<String> set = edgeDescs.get(desc);
if (set == null) {
set = new HashSet<>();
edgeDescs.put(desc, set);
}
set.add(id);
}
Set<Node> nodes = rg.getNodes();
for (Node n : nodes) {
String id = n.getId();
Location loc = n.getLocation();
if (loc != null) {
Set<LogicalLocation> 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);
}
}
}

View File

@@ -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));
}
}
}
}

View File

@@ -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("<EXTERNAL>");
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("<EXTERNAL>");
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<String, Object> 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...
}
}