fix: Attempt to disassemble memory before creating functions
This commit is contained in:
parent
4eadbc9859
commit
25f353a4f3
@ -506,6 +506,27 @@ public class FunctionEndpoints extends AbstractEndpoint {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to disassemble the code at the specified address before creating a function
|
||||||
|
try {
|
||||||
|
TransactionHelper.executeInTransaction(program, "Disassemble Before Function Creation", () -> {
|
||||||
|
// Check if there's already a defined instruction at the address
|
||||||
|
if (program.getListing().getInstructionAt(address) == null) {
|
||||||
|
// Attempt to directly disassemble at the address
|
||||||
|
try {
|
||||||
|
ghidra.app.cmd.disassemble.DisassembleCommand cmd =
|
||||||
|
new ghidra.app.cmd.disassemble.DisassembleCommand(address, null, true);
|
||||||
|
cmd.applyTo(program);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Msg.warn(this, "Basic disassembly failed: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Log the error but proceed with function creation attempt anyway
|
||||||
|
Msg.warn(this, "Disassembly before function creation failed: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
// Create function
|
// Create function
|
||||||
Function function;
|
Function function;
|
||||||
try {
|
try {
|
||||||
@ -513,9 +534,32 @@ public class FunctionEndpoints extends AbstractEndpoint {
|
|||||||
return program.getFunctionManager().createFunction(null, address, null, null);
|
return program.getFunctionManager().createFunction(null, address, null, null);
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
sendErrorResponse(exchange, 400, "Failed to create function: " + e.getMessage(), "CREATE_FAILED");
|
// If function creation initially fails, try a different approach
|
||||||
|
try {
|
||||||
|
Msg.info(this, "Initial function creation failed, attempting with code unit clearing");
|
||||||
|
|
||||||
|
// Clear any existing data at this location and try disassembling again
|
||||||
|
TransactionHelper.executeInTransaction(program, "Clear and Disassemble", () -> {
|
||||||
|
// Clear existing data at the address
|
||||||
|
program.getListing().clearCodeUnits(address, address, false);
|
||||||
|
|
||||||
|
// Try disassembling again
|
||||||
|
ghidra.app.cmd.disassemble.DisassembleCommand cmd =
|
||||||
|
new ghidra.app.cmd.disassemble.DisassembleCommand(address, null, true);
|
||||||
|
cmd.applyTo(program);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Try creating the function again
|
||||||
|
function = TransactionHelper.executeInTransaction(program, "Create Function Retry", () -> {
|
||||||
|
return program.getFunctionManager().createFunction(null, address, null, null);
|
||||||
|
});
|
||||||
|
} catch (Exception e2) {
|
||||||
|
// Both attempts failed, return the error
|
||||||
|
sendErrorResponse(exchange, 400, "Failed to create function after multiple attempts: " + e.getMessage(), "CREATE_FAILED");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (function == null) {
|
if (function == null) {
|
||||||
sendErrorResponse(exchange, 500, "Failed to create function", "CREATE_FAILED");
|
sendErrorResponse(exchange, 500, "Failed to create function", "CREATE_FAILED");
|
||||||
@ -940,6 +984,27 @@ public class FunctionEndpoints extends AbstractEndpoint {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempt to disassemble the code at the specified address before creating a function
|
||||||
|
try {
|
||||||
|
TransactionHelper.executeInTransaction(program, "Disassemble Before Function Creation", () -> {
|
||||||
|
// Check if there's already a defined instruction at the address
|
||||||
|
if (program.getListing().getInstructionAt(address) == null) {
|
||||||
|
// Attempt to directly disassemble at the address
|
||||||
|
try {
|
||||||
|
ghidra.app.cmd.disassemble.DisassembleCommand cmd =
|
||||||
|
new ghidra.app.cmd.disassemble.DisassembleCommand(address, null, true);
|
||||||
|
cmd.applyTo(program);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Msg.warn(this, "Basic disassembly failed: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Log the error but proceed with function creation attempt anyway
|
||||||
|
Msg.warn(this, "Disassembly before function creation failed: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
// Create function
|
// Create function
|
||||||
Function function;
|
Function function;
|
||||||
try {
|
try {
|
||||||
@ -947,9 +1012,32 @@ public class FunctionEndpoints extends AbstractEndpoint {
|
|||||||
return program.getFunctionManager().createFunction(null, address, null, null);
|
return program.getFunctionManager().createFunction(null, address, null, null);
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
sendErrorResponse(exchange, 400, "Failed to create function: " + e.getMessage(), "CREATE_FAILED");
|
// If function creation initially fails, try a different approach
|
||||||
|
try {
|
||||||
|
Msg.info(this, "Initial function creation failed, attempting with code unit clearing");
|
||||||
|
|
||||||
|
// Clear any existing data at this location and try disassembling again
|
||||||
|
TransactionHelper.executeInTransaction(program, "Clear and Disassemble", () -> {
|
||||||
|
// Clear existing data at the address
|
||||||
|
program.getListing().clearCodeUnits(address, address, false);
|
||||||
|
|
||||||
|
// Try disassembling again
|
||||||
|
ghidra.app.cmd.disassemble.DisassembleCommand cmd =
|
||||||
|
new ghidra.app.cmd.disassemble.DisassembleCommand(address, null, true);
|
||||||
|
cmd.applyTo(program);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Try creating the function again
|
||||||
|
function = TransactionHelper.executeInTransaction(program, "Create Function Retry", () -> {
|
||||||
|
return program.getFunctionManager().createFunction(null, address, null, null);
|
||||||
|
});
|
||||||
|
} catch (Exception e2) {
|
||||||
|
// Both attempts failed, return the error
|
||||||
|
sendErrorResponse(exchange, 400, "Failed to create function after multiple attempts: " + e.getMessage(), "CREATE_FAILED");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (function == null) {
|
if (function == null) {
|
||||||
sendErrorResponse(exchange, 500, "Failed to create function", "CREATE_FAILED");
|
sendErrorResponse(exchange, 500, "Failed to create function", "CREATE_FAILED");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user