diff --git a/bridge/ghidra/ReaGhidraBridge.java b/bridge/ghidra/ReaGhidraBridge.java index 1012ce40b4..0d5be066ff 100644 --- a/bridge/ghidra/ReaGhidraBridge.java +++ b/bridge/ghidra/ReaGhidraBridge.java @@ -2731,13 +2731,13 @@ public final class ReaGhidraBridge extends HeadlessScript { * by their qualified names, sorted. */ private static ProcedureNameMatch matchProcedureNames(ProcedureEntry entry, ValueMatcher matcher) { - boolean primary = spellingMatches(entry.leafName(), entry.value(), matcher); + boolean primary = spellingMatches(entry.leafName(), entry.value(), entry.leafIsSuffix(), matcher); if (entry.aliases().isEmpty()) { return new ProcedureNameMatch(primary, List.of()); } List matchedAliases = null; for (ProcedureAlias alias : entry.aliases()) { - if (!spellingMatches(alias.leaf(), alias.qualified(), matcher)) continue; + if (!spellingMatches(alias.leaf(), alias.qualified(), alias.leafIsSuffix(), matcher)) continue; if (alias.qualified().equals(entry.value())) { primary = true; continue; @@ -2750,9 +2750,19 @@ public final class ReaGhidraBridge extends HeadlessScript { return new ProcedureNameMatch(primary, matchedAliases == null ? List.of() : matchedAliases); } - /** A leaf is the suffix of its qualified spelling, so a literal found in it is found in both. */ - private static boolean spellingMatches(String leaf, String qualified, ValueMatcher matcher) { - return matcher.matches(qualified) || (!leaf.equals(qualified) && matcher.matches(leaf)); + /** When a leaf ends its qualified spelling, substring matching on qualified already implies leaf coverage. */ + private static boolean spellingMatches( + String leaf, + String qualified, + boolean leafIsSuffix, + ValueMatcher matcher) { + if (matcher.matches(qualified)) { + return true; + } + if (leaf.equals(qualified) || (!matcher.requiresLeafCheck() && leafIsSuffix)) { + return false; + } + return matcher.matches(leaf); } private List names() throws Exception { @@ -6003,7 +6013,7 @@ public final class ReaGhidraBridge extends HeadlessScript { } private static ValueMatcher literalMatcher(String pattern) { - return value -> value.contains(pattern); + return nonLeafMatcher(value -> value.contains(pattern)); } // REA's host owns ECMAScript /iu simple case folding. Across all 1,114,112 @@ -6012,12 +6022,12 @@ public final class ReaGhidraBridge extends HeadlessScript { // an ASCII fold, so only source strings containing non-ASCII stay eligible. private static ValueMatcher literalCandidateMatcher(String asciiPattern) { if (asciiPattern == null) { - return value -> { + return nonLeafMatcher(value -> { for (int index = 0; index < value.length(); index++) { if (value.charAt(index) > 127) return true; } return false; - }; + }); } int patternLength = asciiPattern.length(); for (int index = 0; index < patternLength; index++) { @@ -6026,10 +6036,10 @@ public final class ReaGhidraBridge extends HeadlessScript { } } if (patternLength == 0) { - return value -> true; + return nonLeafMatcher(value -> true); } char firstTarget = asciiPattern.charAt(0); - return value -> { + return nonLeafMatcher(value -> { int valueLength = value.length(); if (valueLength < patternLength) { return false; @@ -6049,6 +6059,24 @@ public final class ReaGhidraBridge extends HeadlessScript { } } return false; + }); + } + + private interface StringCheck { + boolean test(String value); + } + + private static ValueMatcher nonLeafMatcher(StringCheck check) { + return new ValueMatcher() { + @Override + public boolean matches(String value) { + return check.test(value); + } + + @Override + public boolean requiresLeafCheck() { + return false; + } }; } @@ -6433,6 +6461,10 @@ public final class ReaGhidraBridge extends HeadlessScript { private interface ValueMatcher { boolean matches(String value); + + default boolean requiresLeafCheck() { + return true; + } } private interface InventoryFacts { @@ -6525,16 +6557,33 @@ public final class ReaGhidraBridge extends HeadlessScript { } private record NameEntry(Address address, String value, SymbolFacts facts) {} - private record ProcedureAlias(String leaf, String qualified) {} + private record ProcedureAlias(String leaf, String qualified, boolean leafIsSuffix) { + ProcedureAlias(String leaf, String qualified) { + this(leaf, qualified, qualified.endsWith(leaf)); + } + } private record ProcedureEntry( Function function, Address address, String value, String leafName, + boolean leafIsSuffix, List aliases, ProcedureFacts facts, SymbolFacts symbol - ) {} + ) { + ProcedureEntry( + Function function, + Address address, + String value, + String leafName, + List aliases, + ProcedureFacts facts, + SymbolFacts symbol + ) { + this(function, address, value, leafName, value.endsWith(leafName), aliases, facts, symbol); + } + } private record StringEntry(Address address, String value, StringFacts facts) {} private record AddressNameRequest(String inputAddress, Address address, String name) {} private record AddressNameDestination(long namespace, String leaf) {} diff --git a/tests/boundary/providers/ghidra/ghidraBridgeHelpers.fixture.ts b/tests/boundary/providers/ghidra/ghidraBridgeHelpers.fixture.ts index 99ed7c8488..963d84c31e 100644 --- a/tests/boundary/providers/ghidra/ghidraBridgeHelpers.fixture.ts +++ b/tests/boundary/providers/ghidra/ghidraBridgeHelpers.fixture.ts @@ -160,6 +160,8 @@ import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.TreeMap; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; public final class ReaGhidraBridgeHelperProbe { `; @@ -753,11 +755,59 @@ const JAVA_PROBE_DRIVER = String.raw` } } + private static void runSpellingMatch() throws IOException { + for (String line : readStdinLines()) { + String[] fields = line.split("\\|", -1); + String factory = fields[0]; + String expr = decodeHex(fields[1]); + String qualified = decodeHex(fields[2]); + String leaf = decodeHex(fields[3]); + String aliasQualified = decodeHex(fields[4]); + String aliasLeaf = decodeHex(fields[5]); + ValueMatcher delegate = switch (factory) { + case "literal" -> literalMatcher(expr); + case "candidate-ascii" -> literalCandidateMatcher(expr); + case "candidate-null" -> literalCandidateMatcher(null); + case "regex-cs" -> regexMatcher(expr, true); + case "regex-ci" -> regexMatcher(expr, false); + default -> throw new IllegalArgumentException("Unknown matcher factory: " + factory); + }; + int[] calls = new int[1]; + ValueMatcher matcher = new ValueMatcher() { + @Override + public boolean matches(String value) { + calls[0]++; + return delegate.matches(value); + } + + @Override + public boolean requiresLeafCheck() { + return delegate.requiresLeafCheck(); + } + }; + ProcedureEntry entry = new ProcedureEntry( + null, + null, + qualified, + leaf, + qualified.endsWith(leaf), + aliasQualified.isEmpty() + ? List.of() + : List.of(new ProcedureAlias(aliasLeaf, aliasQualified)), + new ProcedureFacts(), + new SymbolFacts() + ); + ProcedureNameMatch result = matchProcedureNames(entry, matcher); + System.out.println(result.primary() + "|" + String.join(",", result.aliases()) + "|" + calls[0]); + } + } + public static void main(String[] args) throws Exception { switch (args[0]) { case "fold-char" -> runFoldChar(); case "literal-batch" -> runLiteralBatch(); case "literal-direct" -> runLiteralDirect(); + case "spelling-match" -> runSpellingMatch(); case "write-response" -> runWriteResponse(); case "surrogate-writer" -> runSurrogateWriterChecks(); case "write-failures" -> runWriteFailures(); @@ -774,10 +824,15 @@ const buildBridgeProbeSource = (bridgeSource: string): string => { " private Map procedureIndex() {", " private Function resolveProcedure(String value) throws Exception {", ); + const matchMethods = extractBridgeSlice( + bridgeSource, + " private static ProcedureNameMatch matchProcedureNames(", + " private List names() throws Exception {", + ); const literalMethods = extractBridgeSlice( bridgeSource, " private static ValueMatcher literalMatcher(String pattern) {", - " private static ValueMatcher regexMatcher(String expression, boolean caseSensitive) {", + " private static SessionDescriptor readDescriptor(", ); const responseMethods = extractBridgeSlice( bridgeSource, @@ -794,6 +849,11 @@ const buildBridgeProbeSource = (bridgeSource: string): string => { " private record ProcedureAlias(", " private record StringEntry(", ); + const procedureNameMatchRecord = extractBridgeSlice( + bridgeSource, + " private record ProcedureNameMatch(", + " private record FunctionAnalysisObservation(", + ); const requestFailureClass = extractRequestFailureSlice(bridgeSource); return [ JAVA_HEADER, @@ -801,7 +861,9 @@ const buildBridgeProbeSource = (bridgeSource: string): string => { JAVA_GHIDRA_STUBS, valueMatcherInterface, procedureRecords, + procedureNameMatchRecord, procedureMethods, + matchMethods, literalMethods, responseMethods, requestFailureClass, diff --git a/tests/boundary/providers/ghidra/ghidraBridgeHelpers.test.ts b/tests/boundary/providers/ghidra/ghidraBridgeHelpers.test.ts index 332746ed86..37158cea94 100644 --- a/tests/boundary/providers/ghidra/ghidraBridgeHelpers.test.ts +++ b/tests/boundary/providers/ghidra/ghidraBridgeHelpers.test.ts @@ -409,3 +409,73 @@ describe.skipIf(jdk === undefined)( }); }, ); + +describe.skipIf(jdk === undefined)( + "ReaGhidraBridge spellingMatches and ValueMatcher.requiresLeafCheck", + () => { + it("exercises production literalMatcher, literalCandidateMatcher, and regexMatcher across leaf checks", async () => { + const cases = [ + // [factory, expr, qualified, leaf, aliasQualified, aliasLeaf, expected] + // literalMatcher skips redundant leaf scan when leaf is a suffix of qualified + ["literal", "missing", "ns::work", "work", "", "", "false||1"], + ["literal", "work", "ns::work", "work", "", "", "true||1"], + // literalCandidateMatcher(null / ascii) also skips redundant suffix leaf scans + ["candidate-ascii", "miss", "ns::work", "work", "", "", "false||1"], + ["candidate-null", "", "ns::work", "work", "", "", "false||1"], + [ + "candidate-ascii", + "sort", + "ns::other", + "other", + "alt::\u017fort", + "\u017fort", + "false|alt::\u017fort|2", + ], + // Equal primary spelling still runs only 1 check even with requiresLeafCheck=true + ["regex-cs", "^missing$", "plain", "plain", "", "", "false||1"], + // regexMatcher preserves requiresLeafCheck=true so anchored ^work$ matches the leaf of ns::work + ["regex-cs", "^work$", "ns::work", "work", "", "", "true||2"], + ["regex-ci", "^WORK$", "ns::work", "work", "", "", "true||2"], + ["regex-cs", "^missing$", "ns::work", "work", "", "", "false||2"], + // Non-suffix leaf still falls back to leaf scan even for literalMatcher + ["literal", "weird", "ns::work", "weird_leaf", "", "", "true||2"], + // Primary miss + suffixed alias miss runs 2 qualified checks total under literalMatcher + [ + "literal", + "missing", + "ns::work", + "work", + "alt::alias", + "alias", + "false||2", + ], + [ + "literal", + "alias", + "ns::work", + "work", + "alt::alias", + "alias", + "false|alt::alias|2", + ], + ] as const; + const stdout = await runBridgeProbe( + requireProbe(), + "spelling-match", + cases + .map((row) => + [ + row[0], + encodeUtf16Hex(row[1]), + encodeUtf16Hex(row[2]), + encodeUtf16Hex(row[3]), + encodeUtf16Hex(row[4]), + encodeUtf16Hex(row[5]), + ].join("|"), + ) + .join("\n"), + ); + expect(splitProbeLines(stdout)).toEqual(cases.map((row) => row[6])); + }); + }, +);