Files
reverse-engineering-anything/scripts/verify/keyed-first-match.mjs
T
morluto 5d5ab53536 refactor(native): select ELF sections through the keyed owner (#10780)
* refactor(native): select ELF sections through the keyed owner

* test(native): pin returned exports and versions for repeated version sections

* refactor(nativeaot): read neither of repeated frozen object regions (#10790)

* refactor(nativeaot): read neither of repeated frozen object regions through the keyed owner

* refactor(parsers): retire every first-match section lookup (#10793)

* fix(nativeaot): report an ambiguous frozen object region as partial (#10812)

* refactor(native): select program headers and load commands through the keyed owner (#10827)

* refactor(native): select the GOT base and dependency segments through the keyed owner (#10847)

* refactor(native): select the GOT base and dependency segments through the keyed owner

resolveGotBase selects the SHT_DYNAMIC section and the allocated .got.plt/.got sections with uniqueKeyed, so a repeat leaves the GOT base unknown instead of taking the first match; elfDependencies reads PT_LOAD segments through the same keyed program table as PT_DYNAMIC. A new keyed-first-match guard recognizes a find/findLast or filter-then-[0] whose predicate compares a key field, whatever the table is called; existing matches are baselined. The unwind fixture can declare PT_DYNAMIC headers and repeated .dynamic/.got.plt sections.

* test(native): pin the PT_DYNAMIC path and destructured keyed lookups

The keyed-first-match guard moves to scripts/verify/keyed-first-match.mjs with focused tests, and now recognizes a destructured key in any position and a destructured filter predicate. The single PT_DYNAMIC unwind test declares .dynamic as SHT_PROGBITS so only the program header supplies DT_PLTGOT, and asserts the resolved ranges.

* fix(macho): refuse repeated chained-fixup and dyld-info tables (#10850)

parseMachoLayout kept the last LC_DYLD_CHAINED_FIXUPS range and merged the rebase/bind streams of every LC_DYLD_INFO(_ONLY) command. A repeat now refuses the slice as malformed. NativeMachoInspection's chained import-name reader already declined repeated LC_DYLD_CHAINED_FIXUPS; that rule is now covered by a test.

* fix(verify): count a filter index only on the filter's own result
2026-11-15 17:03:25 +08:00

76 lines
2.8 KiB
JavaScript

// A first-match lookup by key field: `find`/`findLast`, or `filter(...)`
// followed by an optional `.map(...)` and `[0]`, whose arrow predicate compares
// the parameter's key field (`x.name === …`) or a destructured key
// (`({ offset, name }) => name === …`). Recognized by shape, whatever the
// table variable is called. Syntactic: a predicate that compares through a
// helper, or a renamed destructured key, is not recognized.
const KEY = String.raw`(?:name|type|cmd|command|tag)`;
// `x`, `(x)` or `(x: Type)`; group 1 names the parameter.
const PLAIN = String.raw`\(?\s*(\w+)\s*(?::[^)=]*)?\)?\s*=>\s*\1\.${KEY}\s*===`;
// `({ ..., key, ... })`, optionally typed; group 1 names the key. A renamed
// key (`{ name: alias }`) binds another identifier and is skipped.
const DESTRUCTURED = String.raw`\(\s*\{[^}]*?\b(${KEY})\b(?!\s*:)[^}]*\}\s*(?::[^)]*)?\)\s*=>\s*\1\s*===`;
const FIND = String.raw`\.find(?:Last)?\(\s*`;
const FILTER = String.raw`\.filter\(\s*`;
const FIND_PATTERNS = [
new RegExp(FIND + PLAIN, "gu"),
new RegExp(FIND + DESTRUCTURED, "gu"),
];
const FILTER_PATTERNS = [
new RegExp(FILTER + PLAIN, "gu"),
new RegExp(FILTER + DESTRUCTURED, "gu"),
];
/** Index just past the parenthesis that closes the one at `open`, or -1. */
const closingParen = (content, open) => {
let depth = 0;
for (let index = open; index < content.length; index += 1) {
const character = content[index];
if (character === "(") depth += 1;
else if (character === ")") {
depth -= 1;
if (depth === 0) return index + 1;
}
}
return -1;
};
const skipSpace = (content, index) => {
while (index < content.length && /\s/u.test(content[index])) index += 1;
return index;
};
// Whether the `filter(` call opening at `open` is closed, optionally followed
// by one `.map(...)`, and then indexed with `[0]`. Matching the parentheses
// keeps a `[0]` inside the predicate or the map callback from counting.
const indexesFirstResult = (content, open) => {
let end = closingParen(content, open);
if (end < 0) return false;
end = skipSpace(content, end);
if (content.startsWith(".map(", end)) {
end = closingParen(content, end + ".map".length);
if (end < 0) return false;
end = skipSpace(content, end);
}
return content.startsWith("[0]", end);
};
const count = (content, pattern, accept) =>
[...content.matchAll(pattern)].filter(accept).length;
/** Number of first-match lookups by key field in `content`. */
export const countKeyedFirstMatches = (content) =>
FIND_PATTERNS.reduce(
(total, pattern) => total + count(content, pattern, () => true),
0,
) +
FILTER_PATTERNS.reduce(
(total, pattern) =>
total +
count(content, pattern, (match) =>
indexesFirstResult(content, match.index + ".filter".length),
),
0,
);