Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5749: Corrected SARIF import/export errors

d-millar7fe22b8092a2
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 688691a108..2f768e9004 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
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -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("<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;
- }
- }
- 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());
+ 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;
}
- } 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...
}
}