mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
perf(ghidra): skip redundant leaf matching in ReaGhidraBridge literal searches (#13439)
* perf(ghidra): skip redundant leaf matching in ReaGhidraBridge literal searches * test(ghidra): exercise production matcher factories in leaf-skip probe
This commit is contained in:
1 parent
c7f89cd8b5
commit
beff3a2842
3 files changed
+194
-13
No files matched your search
@@ -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<String> 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<NameEntry> 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<ProcedureAlias> aliases,
|
||||
ProcedureFacts facts,
|
||||
SymbolFacts symbol
|
||||
) {}
|
||||
) {
|
||||
ProcedureEntry(
|
||||
Function function,
|
||||
Address address,
|
||||
String value,
|
||||
String leafName,
|
||||
List<ProcedureAlias> 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) {}
|
||||
|
||||
@@ -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<String, Object> procedureIndex() {",
|
||||
" private Function resolveProcedure(String value) throws Exception {",
|
||||
);
|
||||
const matchMethods = extractBridgeSlice(
|
||||
bridgeSource,
|
||||
" private static ProcedureNameMatch matchProcedureNames(",
|
||||
" private List<NameEntry> 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,
|
||||
|
||||
@@ -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]));
|
||||
});
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user