Ghidra is a software reverse engineering (SRE) framework
GP-0: revert GP-0: specific tests GP-0: test fixed for lldb) GP-0: test fixed for lldb) GP-6380: fixes + exit code 0 for timeouts GP-6380: lldb-21 changes
d-millarfcfbb1294a30
diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.py b/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.pyindex 0fa5cea709..98419f162b 100644--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.py+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/commands.py@@ -639,6 +639,11 @@ def quantize_pages(start: int, end: int) -> Tuple[int, int]:return (start // PAGE_SIZE * PAGE_SIZE, (end+PAGE_SIZE-1) // PAGE_SIZE*PAGE_SIZE)+def check_count(actual: int, requested: int):+ if actual != requested:+ print(f"Incomplete read: {actual} bytes")++def put_bytes(start: int, end: int, result: lldb.SBCommandReturnObject,pages: bool) -> None:trace = STATE.require_trace()@@ -661,7 +666,7 @@ def put_bytes(start: int, end: int, result: lldb.SBCommandReturnObject,if count.done():result.PutCString(f"Wrote {count.result()} bytes")else:- count.add_done_callback(lambda c: print(f"Wrong {c} bytes"))+ count.add_done_callback(lambda c: check_count(c.result(), len(buf)))result.PutCString(f"Wrong {len(buf)} bytes, perhaps in the future")else:@@ -674,11 +679,15 @@ def eval_address(address: str) -> int:try:return util.parse_and_eval(address)except BaseException as e:- raise RuntimeError(f"Cannot convert '{address}' to address: {e}")+ if "can't evaluate expressions when the process is running" not in str(e):+ raise RuntimeError(f"Cannot convert '{address}' to address: {e}")+ return Nonedef eval_range(address: str, length: str) -> Tuple[int, int]:start = eval_address(address)+ if start is None:+ return None, Nonetry:end = start + util.parse_and_eval(length)except BaseException as e:@@ -689,7 +698,8 @@ def eval_range(address: str, length: str) -> Tuple[int, int]:def putmem(address: str, length: str, result: lldb.SBCommandReturnObject,pages: bool = True) -> None:start, end = eval_range(address, length)- put_bytes(start, end, result, pages)+ if start is not None:+ put_bytes(start, end, result, pages)@convert_errors@@ -773,6 +783,8 @@ def putmem_state(address: str, length: str, state: str,trace = STATE.require_trace()trace.validate_state(state)start, end = eval_range(address, length)+ if start is None:+ returnif pages:start, end = quantize_pages(start, end)proc = util.get_process()@@ -841,6 +853,8 @@ def ghidra_trace_delmem(debugger: lldb.SBDebugger, command: str,STATE.require_tx()start, end = eval_range(address, length)+ if start is None:+ returnproc = util.get_process()base, addr = trace.extra.require_mm().map(proc, start)# Do not create the space. We're deleting stuff.@@ -1349,6 +1363,8 @@ def ghidra_trace_get_values_rng(debugger: lldb.SBDebugger, command: str,trace = STATE.require_trace()start, end = eval_range(address, length)+ if start is None:+ returnproc = util.get_process()base, addr = trace.extra.require_mm().map(proc, start)# Do not create the space. We're querying. No tx.@@ -1416,6 +1432,8 @@ def ghidra_trace_disassemble(debugger: lldb.SBDebugger, command: str,trace, tx = STATE.require_tx()start = eval_address(address)+ if start is None:+ returnproc = util.get_process()base, addr = trace.extra.require_mm().map(proc, start)if base != addr.space:diff --git a/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/hooks.py b/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/hooks.pyindex c799b939d4..636ee65288 100644--- a/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/hooks.py+++ b/Ghidra/Debug/Debugger-agent-lldb/src/main/py/src/ghidralldb/hooks.py@@ -287,35 +287,22 @@ class EventThread(threading.Thread):rc = cli.GetBroadcaster().AddListener(listener, ALL_EVENTS)if not rc:print("add listener for cli failed")- # returnrc = target.GetBroadcaster().AddListener(listener, ALL_EVENTS)if not rc:print("add listener for target failed")- # returnrc = proc.GetBroadcaster().AddListener(listener, ALL_EVENTS)if not rc:print("add listener for process failed")- # return# Not sure what effect this logic has- rc = cli.GetBroadcaster().AddInitialEventsToListener(listener, ALL_EVENTS)- if not rc:- print("add initial events for cli failed")- # return- rc = target.GetBroadcaster().AddInitialEventsToListener(listener, ALL_EVENTS)- if not rc:- print("add initial events for target failed")- # return- rc = proc.GetBroadcaster().AddInitialEventsToListener(listener, ALL_EVENTS)- if not rc:- print("add initial events for process failed")- # return+ #cli.GetBroadcaster().AddInitialEventsToListener(listener, ALL_EVENTS)+ #target.GetBroadcaster().AddInitialEventsToListener(listener, ALL_EVENTS)+ #proc.GetBroadcaster().AddInitialEventsToListener(listener, ALL_EVENTS)rc = listener.StartListeningForEventClass(util.get_debugger(), lldb.SBThread.GetBroadcasterClassName(), ALL_EVENTS)if not rc:print("add listener for threads failed")- # return# THIS WILL NOT WORK: listener = util.get_debugger().GetListener()while True:diff --git a/Ghidra/Test/DebuggerIntegrationTest/src/expPrint/c/expPrint.c b/Ghidra/Test/DebuggerIntegrationTest/src/expPrint/c/expPrint.cindex 20b49e511c..cea6b4a8bf 100644--- a/Ghidra/Test/DebuggerIntegrationTest/src/expPrint/c/expPrint.c+++ b/Ghidra/Test/DebuggerIntegrationTest/src/expPrint/c/expPrint.c@@ -21,7 +21,6 @@#define DLLEXPORT __declspec(dllexport)#else#define DLLEXPORT-#define OutputDebugString(out) puts(out)#endifDLLEXPORT volatile char overwrite[] = "Hello, World!";@@ -36,7 +35,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLineint DLLEXPORT main(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {wrapputs(overwrite);- return overwrite[0];+ return overwrite[0];}int DLLEXPORT wrapputs(volatile char* output) {diff --git a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/AbstractLldbTraceRmiTest.java b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/AbstractLldbTraceRmiTest.javaindex 8e57585d3b..2d01bb888b 100644--- a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/AbstractLldbTraceRmiTest.java+++ b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/AbstractLldbTraceRmiTest.java@@ -121,7 +121,7 @@ public abstract class AbstractLldbTraceRmiTest extends AbstractGhidraHeadedDebug""";// Connecting should be the first thing the script does, so use a tight timeout.protected static final int CONNECT_TIMEOUT_MS = 3000;- protected static final int TIMEOUT_SECONDS = SystemUtilities.isInTestingBatchMode() ? 10 : 30;+ protected static final int TIMEOUT_SECONDS = SystemUtilities.isInTestingBatchMode() ? 10 : 300;protected static final int QUIT_TIMEOUT_MS = 1000;/** Some snapshot likely to exceed the latest */@@ -353,14 +353,13 @@ public abstract class AbstractLldbTraceRmiTest extends AbstractGhidraHeadedDebug""";cmd = lfIfWindows(cmd);exec.pty.getParent().getOutputStream().write(cmd.getBytes());- Exception finalExc = null;try {try {LldbResult r = exec.future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);r.handle();}catch (Exception e) {- finalExc = e;+ Msg.error(this, e);}waitForPass(this, () -> assertTrue(connection.isClosed()), TIMEOUT_SECONDS,TimeUnit.SECONDS);@@ -373,9 +372,6 @@ public abstract class AbstractLldbTraceRmiTest extends AbstractGhidraHeadedDebugexec.pty.close();exec.lldb.destroyForcibly();exec.pumper.interrupt();- if (finalExc != null) {- throw finalExc;- }}}}@@ -460,6 +456,18 @@ public abstract class AbstractLldbTraceRmiTest extends AbstractGhidraHeadedDebugreturn xout.split(head)[1].split("---")[0].replace("(lldb)", "").trim();}+ // OK, Windows versions just behave differently re prompt+ protected String extractOutSectionWithPrompt(String out, String head) {+ String[] split = out.replace("\r", "").split("\n");+ String xout = "";+ for (String s : split) {+ if (!s.contains("script print(") && !s.equals("")) {+ xout += s + "\n";+ }+ }+ return xout.split(head)[1].split("---")[0].trim();+ }+record MemDump(long address, byte[] data) {}protected MemDump parseHexDump(String dump) throws IOException {diff --git a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbCommandsTest.java b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbCommandsTest.javaindex 0b0ff522c7..118960551a 100644--- a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbCommandsTest.java+++ b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbCommandsTest.java@@ -371,7 +371,7 @@ public class LldbCommandsTest extends AbstractLldbTraceRmiTest {tb = new ToyDBTraceBuilder((Trace) mdo.get());long snap = Unique.assertOne(tb.trace.getTimeManager().getAllSnapshots()).getKey();- String eval = extractOutSection(out, "---Start---");+ String eval = extractOutSectionWithPrompt(out, "---Start---");Address addr = tb.addr(Stream.of(eval.split("\\s+")).filter(s -> s.startsWith("0x")).mapToLong(Long::decode)@@ -408,7 +408,8 @@ public class LldbCommandsTest extends AbstractLldbTraceRmiTest {tb = new ToyDBTraceBuilder((Trace) mdo.get());long snap = Unique.assertOne(tb.trace.getTimeManager().getAllSnapshots()).getKey();- MemDump dump = parseHexDump(extractOutSection(out, "---Dump---"));+ String xout = extractOutSectionWithPrompt(out, "---Dump---");+ MemDump dump = parseHexDump(xout.substring(xout.indexOf("0x")));Arrays.fill(dump.data(), 0, 5, (byte) 0);ByteBuffer buf = ByteBuffer.allocate(dump.data().length);tb.trace.getMemoryManager().getBytes(snap, tb.addr(dump.address()), buf);@@ -926,11 +927,11 @@ public class LldbCommandsTest extends AbstractLldbTraceRmiTest {""".formatted(PREAMBLE, addr, getSpecimenPrint()));try (ManagedDomainObject mdo = openDomainObject(projectName("expPrint"))) {tb = new ToyDBTraceBuilder((Trace) mdo.get());- assertEquals("""- Parent Key Span Value Type- Test.Objects[1] vaddr [0,+inf) ram:0000dead ADDRESS\- """,- extractOutSection(out, "---GetValues---"));+ assertTrue(extractOutSectionWithPrompt(out, "---GetValues---").contains(+ """+ Parent Key Span Value Type+ Test.Objects[1] vaddr [0,+inf) ram:0000dead ADDRESS\+ """));}}@@ -981,8 +982,8 @@ public class LldbCommandsTest extends AbstractLldbTraceRmiTest {for (CodeUnit cu : tb.trace.getCodeManager().definedUnits().get(0, true)) {total += cu.getLength();}- assertEquals("Disassembled %d bytes".formatted(total),- extractOutSection(out, "---Disassemble---"));+ assertTrue(extractOutSectionWithPrompt(out, "---Disassemble---")+ .contains("Disassembled %d bytes".formatted(total)));}}@@ -1239,7 +1240,7 @@ public class LldbCommandsTest extends AbstractLldbTraceRmiTest {}}- @Test+ @Testpublic void testMinimal() throws Exception {assumeFalse(IS_WINDOWS);Function<String, String> scriptSupplier = addr -> """diff --git a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbHooksTest.java b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbHooksTest.javaindex b6dba7eaa8..4aef773004 100644--- a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbHooksTest.java+++ b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbHooksTest.java@@ -27,7 +27,6 @@ import org.junit.Test;import org.junit.experimental.categories.Category;import generic.test.category.NightlyCategory;-import generic.test.rule.Repeated;import ghidra.app.plugin.core.debug.utils.ManagedDomainObject;import ghidra.program.model.address.AddressSpace;import ghidra.trace.database.ToyDBTraceBuilder;@@ -422,6 +421,25 @@ public class LldbHooksTest extends AbstractLldbTraceRmiTest {}}+ // NB: This is basically the minimum working example required to cause timeout+ // errors in LldbAndConnection's close method. The error results (I think) from+ // the connections being torn down before 'quit' executes. We can throw an error+ // for this, but why really?+ //@Test+ //@Repeated(100)+ public void testTimeout() throws Exception {+ try (LldbAndTrace conn = startAndSyncLldb()) {+ String obj = getSpecimenPrint();+ conn.execute("file " + obj);+ conn.execute("process launch --stop-at-entry");+ conn.execute("ghidra trace sync-enable");+ conn.execute("ghidra trace sync-synth-stopped");+ txPut(conn, "processes");+ conn.success();+ }+ }++private void start(LldbAndTrace conn, String obj) {conn.execute("file " + obj);conn.execute("ghidra trace sync-enable");diff --git a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbMethodsTest.java b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbMethodsTest.javaindex 4431a9ccd3..02f9ee37ce 100644--- a/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbMethodsTest.java+++ b/Ghidra/Test/DebuggerIntegrationTest/src/test.slow/java/agent/lldb/rmi/LldbMethodsTest.java@@ -739,17 +739,18 @@ public class LldbMethodsTest extends AbstractLldbTraceRmiTest {@Testpublic void testFinish() throws Exception {+ // NB: Currently has a timing issue on Windows+ assumeTrue(OperatingSystem.CURRENT_OPERATING_SYSTEM == OperatingSystem.LINUX);try (LldbAndConnection conn = startAndConnectLldb()) {- // NB: These examples have shorter per-platform "step out"'s- conn.execute("file " + (IS_WINDOWS ? getSpecimenRead() : getSpecimenPrint()));+ conn.execute("file " + getSpecimenPrint());conn.execute("ghidra trace start");txPut(conn, "processes");- breakAt(conn, IS_WINDOWS ? "wrapread" : "wrapputs");+ breakAt(conn, "wrapputs");RemoteMethod activate = conn.getMethod("activate_thread");RemoteMethod step_out = conn.getMethod("step_out");try (ManagedDomainObject mdo =- openDomainObject(projectName(IS_WINDOWS ? "expRead" : "expPrint"))) {+ openDomainObject(projectName("expPrint"))) {tb = new ToyDBTraceBuilder((Trace) mdo.get());waitStopped(conn);waitTxDone();