Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native): keep export names ending in a carriage return (#3457)

HawkForge2425493e1032
diff --git a/src/native/NativeMachoInspection.ts b/src/native/NativeMachoInspection.ts
index 6cccf0bfa..75f39d5cd 100644
--- a/src/native/NativeMachoInspection.ts
+++ b/src/native/NativeMachoInspection.ts
@@ -379,9 +379,10 @@ const namesEveryExport = (
): boolean => {
const names = new Set(parseNmExports(nmOutput).map(({ name }) => name));
return sliceOutput(dyldExports)
- .split(/\r?\n/u)
+ .split("\n")
.every((rawLine, index) => {
- const line = rawLine.trim();
+ // Keep a trailing carriage return: it belongs to the exported name.
+ const line = rawLine.trimStart();
if (
line.length === 0 ||
(index === 0 && line.endsWith(":")) ||
@@ -392,14 +393,15 @@ const namesEveryExport = (
const rows = parseDyldSymbols(line, "exports", null, names);
// The parser withholds a row only when nm names both of its readings.
if (rows.length === 0)
- return names.has(/^0x[\da-f]+\s+(.+)$/iu.exec(line)?.[1] ?? "");
+ return names.has(/^0x[\da-f]+\s+(.+)$/isu.exec(line)?.[1] ?? "");
return rows.length === 1 && names.has(rows[0]!.name);
});
};
+// nm -j ends each name with LF; a carriage return before it is part of the name.
const parseNmExports = (output: string) =>
output
- .split(/\r?\n/u)
+ .split("\n")
.filter((name) => name.length > 0)
.map((name) => ({
name,
diff --git a/src/native/parsers/dyldInfo.ts b/src/native/parsers/dyldInfo.ts
index 9742c66a5..bf710c4e4 100644
--- a/src/native/parsers/dyldInfo.ts
+++ b/src/native/parsers/dyldInfo.ts
@@ -16,8 +16,10 @@ export const parseDyldSymbols = (
knownExportNames: ReadonlySet<string> = new Set(),
) => {
const offsets = /^\s*offset\s+symbol\s*$/mu.test(output);
- return output.split(/\r?\n/u).flatMap<ParsedDyldSymbol>((rawLine) => {
- const line = rawLine.trim();
+ // dyld_info ends rows with LF. A carriage return before it belongs to the
+ // symbol name, so export rows keep their trailing text.
+ return output.split("\n").flatMap<ParsedDyldSymbol>((rawLine) => {
+ const line = mode === "exports" ? rawLine.trimStart() : rawLine.trim();
if (mode === "imports") {
const imported =
/^(?:0x[\da-f]+\s+)?(.+?)(?:\s+\[([^\]]+)\])?\s+\(from (.+)\)$/iu.exec(
@@ -54,9 +56,9 @@ export const parseDyldSymbols = (
: { sourceSymbol: reexport.sourceSymbol }),
},
];
- const exported = /^(0x[\da-f]+)\s+(.+?)(?:\s+\[([^\]]+)\])?$/iu.exec(line);
+ const exported = /^(0x[\da-f]+)\s+(.+?)(?:\s+\[([^\]]+)\])?$/isu.exec(line);
if (exported?.[1] === undefined || exported[2] === undefined) return [];
- const rawName = /^0x[\da-f]+\s+(.+)$/iu.exec(line)?.[1];
+ const rawName = /^0x[\da-f]+\s+(.+)$/isu.exec(line)?.[1];
const literalName = rawName !== undefined && knownExportNames.has(rawName);
// If nm establishes both interpretations, the textual row cannot bind an address or flags.
// Let the caller retain the exact nm names without guessing which one owns this row.
diff --git a/tests/boundary/providers/native/nativeInventory.golden.test.ts b/tests/boundary/providers/native/nativeInventory.golden.test.ts
index cee8136c3..b30e9d1a1 100644
--- a/tests/boundary/providers/native/nativeInventory.golden.test.ts
+++ b/tests/boundary/providers/native/nativeInventory.golden.test.ts
@@ -708,6 +708,40 @@ it("reads exports from the symbol table without llvm-nm's dyld-info pass", async
expect(nmCommands(execution.value.result)).toEqual(nmArguments);
});
+it("keeps an export name ending in a carriage return distinct", async () => {
+ // Captured from a clang executable exporting "trailing\r" and "trailing".
+ const names = "__mh_execute_header\n_main\ntrailing\ntrailing\r\n";
+ const { runner, nmArguments } = symbolTableRunner(
+ {
+ "dyld_info:-exports": [
+ "fixture [arm64]:",
+ " -exports:",
+ " offset symbol",
+ " 0x00000000 __mh_execute_header",
+ " 0x00000338 _main",
+ " 0x00000330 trailing",
+ " 0x00000328 trailing\r",
+ "",
+ ].join("\n"),
+ },
+ { symbolTable: names, withDyldInfo: names },
+ );
+ const execution = await new NativeMacOSProvider({}, runner, "darwin")
+ .createClient(machoTarget("/private/fixture"))
+ .execute("inspect_macho", {});
+ if (!execution.ok) throw execution.error;
+ expect(nmArguments).toHaveLength(1);
+ const exported = (
+ execution.value.result as {
+ exports: { items: { name: string; address: string | null }[] };
+ }
+ ).exports.items.filter(({ name }) => name.startsWith("trailing"));
+ expect(exported).toEqual([
+ expect.objectContaining({ name: "trailing", address: "0x100000330" }),
+ expect.objectContaining({ name: "trailing\r", address: "0x100000328" }),
+ ]);
+});
+
it("reruns nm with its dyld-info pass when a stripped symbol table omits trie exports", async () => {
const trieNames = await fixture("dyld-inventory/stripped-swift-nm.txt");
const { runner, nmArguments } = symbolTableRunner(