Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an address space id field to BSim objects #6897

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions Ghidra/Features/BSim/ghidra_scripts/LocalBSimQueryScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ghidra.features.bsim.query.client.Configuration;
import ghidra.features.bsim.query.description.FunctionDescription;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.*;

//TODO: docs
Expand Down Expand Up @@ -176,17 +177,18 @@ private List<LocalBSimMatch> getMatchesCurrentProgram(Set<Function> funcs)

//...but use sourceFuncAddrs to ensure that source functions are in the
//funcs set
Set<Long> sourceFuncAddrs = new HashSet<>();
Set<Address> sourceFuncAddrs = new HashSet<>();
for (Function func : funcs) {
sourceFuncAddrs.add(func.getEntryPoint().getOffset());
sourceFuncAddrs.add(func.getEntryPoint());
}
Iterator<FunctionDescription> sourceDescripts =
gensig.getDescriptionManager().listAllFunctions();
VectorCompare vecCompare = new VectorCompare();
while (sourceDescripts.hasNext()) {
FunctionDescription srcDesc = sourceDescripts.next();
Address srcAddress = getAddress(currentProgram, srcDesc.getSpaceID(), srcDesc.getAddress());
//skip if not in selection
if (!sourceFuncAddrs.contains(srcDesc.getAddress())) {
if (!sourceFuncAddrs.contains(srcAddress)) {
continue;
}
//skip if self-significance too small
Expand All @@ -196,14 +198,17 @@ private List<LocalBSimMatch> getMatchesCurrentProgram(Set<Function> funcs)
}
Iterator<FunctionDescription> targetDescripts =
gensig.getDescriptionManager().listAllFunctions();
Function srcFunc = getFunction(currentProgram, srcDesc.getAddress());
Function srcFunc = getFunction(currentProgram, srcDesc.getSpaceID(), srcDesc.getAddress());
while (targetDescripts.hasNext()) {
//skip if target before srcFunc in address order
//AND target is one of the source functions (i.e., in funcs)
FunctionDescription targetDesc = targetDescripts.next();
long targetAddress = targetDesc.getAddress();
//skip if target is one of the source functions (i.e., in funcs)
//AND src and target functions reside in the same Address Space
//AND target before srcFunc in address order
FunctionDescription targetDesc = targetDescripts.next();;
Address targetAddress = getAddress(currentProgram, targetDesc.getSpaceID(), targetDesc.getAddress());

if (sourceFuncAddrs.contains(targetAddress) &&
targetAddress <= srcDesc.getAddress()) {
targetDesc.getSpaceID() == srcDesc.getSpaceID() &&
targetDesc.getAddress() <= srcDesc.getAddress()) {
continue;
}
//skip if self-significance too small
Expand All @@ -215,7 +220,7 @@ private List<LocalBSimMatch> getMatchesCurrentProgram(Set<Function> funcs)
double sig = vectorFactory.calculateSignificance(vecCompare);
if (sig >= MATCH_CONFIDENCE_LOWER_BOUND && MATCH_SIMILARITY_LOWER_BOUND <= sim &&
sim <= MATCH_SIMILARITY_UPPER_BOUND) {
Function targetFunc = getFunction(currentProgram, targetDesc.getAddress());
Function targetFunc = getFunction(currentProgram, targetDesc.getSpaceID(), targetDesc.getAddress());
bsimMatches.add(new LocalBSimMatch(srcFunc, targetFunc, sim, sig));
}
}
Expand Down Expand Up @@ -245,7 +250,7 @@ private List<LocalBSimMatch> getMatchesTwoPrograms(Set<Function> srcFuncs,
}
Iterator<FunctionDescription> targetDescripts =
targetSigs.getDescriptionManager().listAllFunctions();
Function srcFunc = getFunction(sourceProgram, srcDesc.getAddress());
Function srcFunc = getFunction(sourceProgram, srcDesc.getSpaceID(), srcDesc.getAddress());
while (targetDescripts.hasNext()) {
FunctionDescription targetDesc = targetDescripts.next();
//skip if self-significance too small
Expand All @@ -257,16 +262,21 @@ private List<LocalBSimMatch> getMatchesTwoPrograms(Set<Function> srcFuncs,
double sig = vectorFactory.calculateSignificance(vecCompare);
if (sig >= MATCH_CONFIDENCE_LOWER_BOUND && MATCH_SIMILARITY_LOWER_BOUND <= sim &&
sim <= MATCH_SIMILARITY_UPPER_BOUND) {
Function targetFunc = getFunction(targetProgram, targetDesc.getAddress());
Function targetFunc = getFunction(targetProgram, targetDesc.getSpaceID(), targetDesc.getAddress());
bsimMatches.add(new LocalBSimMatch(srcFunc, targetFunc, sim, sig));
}
}
}
return bsimMatches;
}

private Function getFunction(Program program, long offset) {
Address addr = program.getAddressFactory().getDefaultAddressSpace().getAddress(offset);
private Address getAddress(Program program, int spaceid, long offset) {
Address addr = program.getAddressFactory().getAddress(spaceid, offset);
return addr;
}

private Function getFunction(Program program, int spaceid, long offset) {
Address addr = getAddress(program, spaceid, offset);
return program.getFunctionManager().getFunctionAt(addr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import ghidra.features.bsim.query.protocol.PreFilter;
import ghidra.program.database.symbol.FunctionSymbol;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.SourceType;
import ghidra.util.exception.CancelledException;
Expand Down Expand Up @@ -272,9 +273,11 @@ private HashSet<FunctionSymbol> getFunctionsToQuery(Program program) {
* @return true if the symbol is NOT an analysis source type
*/
public static boolean isNotAnalysisSourceType(Program program, FunctionDescription funcDesc) {
Address address =
program.getAddressFactory().getDefaultAddressSpace().getAddress(funcDesc.getAddress());

AddressSpace space = program.getAddressFactory().getAddressSpace(funcDesc.getSpaceID());
if (space == null) {
space = program.getAddressFactory().getDefaultAddressSpace();
}
Address address = space.getAddress(funcDesc.getAddress());
Function function = program.getFunctionManager().getFunctionAt(address);
if (function == null || !function.getName().equals(funcDesc.getFunctionName())) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ghidra.framework.plugintool.PluginTool;
import ghidra.framework.plugintool.ServiceProvider;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
import ghidra.program.model.symbol.Namespace;
Expand Down Expand Up @@ -195,8 +196,11 @@ void clear() {
* @return the entry point address of the function (if it exists), or just the address within the default space
*/
public static Address recoverAddress(FunctionDescription desc, Program prog) {
Address address =
prog.getAddressFactory().getDefaultAddressSpace().getAddress(desc.getAddress());
AddressSpace space = prog.getAddressFactory().getAddressSpace(desc.getSpaceID());
if (space == null) {
space = prog.getAddressFactory().getDefaultAddressSpace();
}
Address address = space.getAddress(desc.getAddress());
// Verify that we got the right function
Function func = prog.getFunctionManager().getFunctionAt(address);
if (func != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.database.symbol.FunctionSymbol;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.*;
import ghidra.util.HelpLocation;
import ghidra.util.Msg;
Expand Down Expand Up @@ -526,9 +527,12 @@ private Function getMatchFunction(BSimMatchResult resultRow, Set<Program> opened
resultRow.getSimilarFunctionName() + ".");
}
FunctionDescription matchFunctionDescription = resultRow.getMatchFunctionDescription();
long matchOffset = matchFunctionDescription.getAddress();
Address matchEntryPoint =
matchProgram.getAddressFactory().getDefaultAddressSpace().getAddress(matchOffset);

AddressSpace space = program.getAddressFactory().getAddressSpace(matchFunctionDescription.getSpaceID());
if (space == null) {
space = program.getAddressFactory().getDefaultAddressSpace();
}
Address matchEntryPoint = space.getAddress(matchFunctionDescription.getAddress());
FunctionManager matchFunctionManager = matchProgram.getFunctionManager();
Function matchFunction = matchFunctionManager.getFunctionAt(matchEntryPoint);
if (matchFunction == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ private Function getRemoteFunction(BSimMatchResult result) {

FunctionDescription matchDescription = result.getMatchFunctionDescription();
long addressOffset = matchDescription.getAddress();
AddressSpace space = remoteProgram.getAddressFactory().getDefaultAddressSpace();
int spaceid = matchDescription.getSpaceID();
AddressSpace space = remoteProgram.getAddressFactory().getAddressSpace(spaceid);
if (space == null) {
space = remoteProgram.getAddressFactory().getDefaultAddressSpace();
}
Address address = space.getAddress(addressOffset);
FunctionManager remoteFunctionManager = remoteProgram.getFunctionManager();
Function matchFunction = remoteFunctionManager.getFunctionAt(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ private CallRecord fillinProperties(Address addr) {
}
if (hasbody) { // Internal call
callRecord.exerec = exerec; // Within the same executable
callRecord.spaceid = rootAddr.getAddressSpace().getSpaceID();
callRecord.address = rootAddr.getOffset();
callRecord.funcname = rootSymbol.getName(true);
}
Expand Down Expand Up @@ -413,17 +414,19 @@ private boolean hasBody(Address addr) {

private synchronized void writeToManager(Function func, int[] hash, List<CallRecord> callrecs,
int flags) {

Address entryPoint = func.getEntryPoint();
FunctionDescription fdesc = manager.newFunctionDescription(func.getName(true),
func.getEntryPoint().getOffset(), exerec);
manager.setFunctionDescriptionFlags(fdesc, flags);
entryPoint.getAddressSpace().getSpaceID(), entryPoint.getOffset(), exerec);
manager.setFunctionDescriptionFlags(fdesc, flags);
if (hash != null) {
LSHVector vec = vectorFactory.buildVector(hash);
SignatureRecord sigrec = manager.newSignature(vec, 0);
manager.attachSignature(fdesc, sigrec);
}
for (CallRecord callRecord : callrecs) {
FunctionDescription destfunc = manager.newFunctionDescription(callRecord.funcname,
callRecord.address, callRecord.exerec);
callRecord.spaceid, callRecord.address, callRecord.exerec);
manager.makeCallgraphLink(fdesc, destfunc, 0);
}
}
Expand All @@ -439,8 +442,9 @@ public int transferCachedFunctions(DescriptionManager otherman, Iterator<Functio
Function func = functions.next();
String name = func.getName(true);
long address = func.getEntryPoint().getOffset();
int spaceid = func.getEntryPoint().getAddressSpace().getSpaceID();
try {
desc = manager.findFunction(name, address, exerec);
desc = manager.findFunction(name, spaceid, address, exerec);
}
catch (LSHException e) { // This exception is thrown if the manager does not contain a function of this name
continue; // Basically we skip the function in this case
Expand Down Expand Up @@ -524,9 +528,11 @@ public void scanFunctionsMetadata(Iterator<Function> iter, TaskMonitor monitor)
continue;
}
int flags = recoverAttributes(func);
Address entryPoint = func.getEntryPoint();
FunctionDescription fdesc = manager.newFunctionDescription(func.getName(true),
func.getEntryPoint().getOffset(), exerec);
entryPoint.getAddressSpace().getSpaceID(), entryPoint.getOffset(), exerec);
manager.setFunctionDescriptionFlags(fdesc, flags);

}
}

Expand Down Expand Up @@ -629,13 +635,14 @@ else if (size1.equals("64")) {

/**
* Info for resolving a call to a unique function in the database.
* For normal functions you need the triple (executable, function name, address)
* For normal functions you need the triple (executable, function name, spaceid, address)
* For calls to library (external) functions, only the library executable
* and the function name are needed, and the address is filled in with -1
*/
private static class CallRecord {
public ExecutableRecord exerec;
public String funcname;
public int spaceid;
public long address;
}

Expand Down Expand Up @@ -685,7 +692,7 @@ public void decompile(Function func, TaskMonitor monitor) {
return;
}
FunctionDescription fdesc =
manager.containsDescription(func.getName(true), entryPoint.getOffset(), exerec);
manager.containsDescription(func.getName(true), entryPoint, exerec);
if (fdesc != null && fdesc.getSignatureRecord() != null) { // Is signature for this function already present
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ private boolean markPreviouslyStoredFunctions(DescriptionManager input,
continue;
}
DescriptionRow row = descTable.queryFuncNameAddr(erec.getRowId().getLong(),
func.getFunctionName(), func.getAddress());
func.getFunctionName(), func.getSpaceID(), func.getAddress());
if (row == null) {
newfuncs = true;
continue;
Expand Down Expand Up @@ -714,14 +714,14 @@ private int updateExecutable(DescriptionManager manage, ExecutableRecord erec,
List<FunctionDescription> funclist = new ArrayList<FunctionDescription>();
queryAllFunc(funclist, erec_db, dbmanage, 0);

// Create a map from address to executables
Map<Long, FunctionDescription> addrmap =
// Create a map from address spaces, to address offsets, to executables
Map<Integer, TreeMap<Long, FunctionDescription>> spacemap =
FunctionDescription.createAddressToFunctionMap(funclist.iterator());

// Match new functions to old functions via the address
List<FunctionDescription.Update> updatelist;
updatelist =
FunctionDescription.generateUpdates(manage.listFunctions(erec), addrmap, badfunc);
FunctionDescription.generateUpdates(manage.listFunctions(erec), spacemap, badfunc);

if (!has_exe_update && updatelist.isEmpty()) {
return 0; // All updates are in place already
Expand Down Expand Up @@ -930,7 +930,7 @@ long recoverExternalFunctionId(String exename, String functionname, String repar
throw new LSHException("Could not resolve filter specifying executable: " + exename);
}

DescriptionRow descRow = descTable.queryFuncNameAddr(row.rowid, functionname, -1);
DescriptionRow descRow = descTable.queryFuncNameAddr(row.rowid, functionname, 0, -1);
if (descRow == null) {
throw new LSHException(
"Could not resolve filter specifying function: [" + exename + "]" + functionname);
Expand Down Expand Up @@ -1441,16 +1441,17 @@ private void queryByName(List<FunctionDescription> functionDescriptions,
* @param manager - container for record
* @param erec - previously queried ExecutableRecord
* @param funcname - name of function to query for
* @param spaceid - id of the address space of the function
* @param address - address of function to query for
* @param sigs - true if signature of function should also be returned
* @return the resulting FunctionDescription or null
* @throws SQLException if there is an error issuing the query
*/
private FunctionDescription queryByNameAddress(DescriptionManager manager,
ExecutableRecord erec, String funcname, long address, boolean sigs)
ExecutableRecord erec, String funcname, int spaceid, long address, boolean sigs)
throws SQLException {
DescriptionRow row =
descTable.queryFuncNameAddr(erec.getRowId().getLong(), funcname, address);
descTable.queryFuncNameAddr(erec.getRowId().getLong(), funcname, spaceid, address);
FunctionDescription func = DescriptionTable.convertDescriptionRow(row, erec, manager, null);
if (sigs) {
queryAssociatedSignature(func, manager, null);
Expand Down Expand Up @@ -1693,7 +1694,7 @@ private void fdbQueryPair(QueryPair query) throws SQLException, LSHException {
}
else {
funcA = queryByNameAddress(resManage, erec, pairInput.funcA.funcName,
pairInput.funcA.address, true);
pairInput.funcA.spaceid, pairInput.funcA.address, true);
if (funcA == null) {
accumulator.missedFunc += 1;
}
Expand All @@ -1705,7 +1706,7 @@ private void fdbQueryPair(QueryPair query) throws SQLException, LSHException {
}
else {
funcB = queryByNameAddress(resManage, erec, pairInput.funcB.funcName,
pairInput.funcB.address, true);
pairInput.funcB.spaceid, pairInput.funcB.address, true);
if (funcB == null) {
accumulator.missedFunc += 1;
}
Expand Down Expand Up @@ -2078,7 +2079,7 @@ private void fdbQueryChildren(QueryChildren query) throws LSHException, SQLExcep
}
for (FunctionEntry entry : query.functionKeys) {
FunctionDescription func =
queryByNameAddress(response.manage, exe, entry.funcName, entry.address, true);
queryByNameAddress(response.manage, exe, entry.funcName, entry.spaceid, entry.address, true);
if (func == null) {
throw new LSHException("Could not find function: " + entry.funcName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private QueryNearestVector buildVectorQuery(LSHVector vector, double threshold)
ExecutableRecord exeRecord = query.manage.newExecutableRecord(
"bbbbaaaabbbbaaaabbbbaaaabbbbaaaa", null, null, null, null, null, null, null);
FunctionDescription function =
query.manage.newFunctionDescription("tmp", 0x1000L, exeRecord);
query.manage.newFunctionDescription("tmp", 344, 0x1000L, exeRecord);
SignatureRecord signature = query.manage.newSignature(vector, 1);
query.manage.attachSignature(function, signature);

Expand Down
Loading