mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 09:56:23 +00:00
perf(native/apple): optimize RELR loop, BFS queue, and Mach-O rebases (#13681)
perf(native/apple): optimize RELR bitmap loop, BFS queue, and Mach-O legacy rebase checks
This commit is contained in:
1 parent
ac5cdcbd18
commit
fe982a1d7c
3 files changed
+120
-39
No files matched your search
@@ -1101,6 +1101,41 @@ function* readRebaseStreamSteps(
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_SAFE_INTEGER_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
|
||||
|
||||
/**
|
||||
* Fast path for range checks over `[low, high]`: when every address in the
|
||||
* closed interval belongs unambiguously to one segment and stays within safe
|
||||
* integer file offsets, return its base file offset at `low`. Otherwise return
|
||||
* `null` to fall back to per-slot `legacyRebaseState`.
|
||||
*/
|
||||
const singleLegacySegmentFileBase = (
|
||||
segments: readonly Segment[],
|
||||
low: bigint,
|
||||
high: bigint,
|
||||
): number | null => {
|
||||
let owner: Segment | undefined;
|
||||
for (let i = 0; i < segments.length; i += 1) {
|
||||
const segment = segments[i]!;
|
||||
const segEnd = segment.address + segment.size;
|
||||
// Any segment overlapping [low, high + REBASE_POINTER_SIZE) could map a
|
||||
// slot in [low, high].
|
||||
if (high < segment.address || low + REBASE_POINTER_SIZE > segEnd) continue;
|
||||
if (owner !== undefined) return null;
|
||||
owner = segment;
|
||||
}
|
||||
if (owner === undefined) return null;
|
||||
if (
|
||||
low < owner.address ||
|
||||
high + REBASE_POINTER_SIZE > owner.address + owner.size
|
||||
)
|
||||
return null;
|
||||
const lowMapped = BigInt(owner.offset) + (low - owner.address);
|
||||
const highMapped = BigInt(owner.offset) + (high - owner.address);
|
||||
if (lowMapped < 0n || highMapped > MAX_SAFE_INTEGER_BIGINT) return null;
|
||||
return Number(lowMapped);
|
||||
};
|
||||
|
||||
/** Read loader-owned membership without interpreting a stored pointer word. */
|
||||
const legacyRebaseState = (
|
||||
segments: readonly Segment[],
|
||||
@@ -1119,7 +1154,7 @@ const legacyRebaseState = (
|
||||
reason: `Ambiguous segments map legacy rebase pointer location 0x${address.toString(16)}`,
|
||||
};
|
||||
const mapped = BigInt(segment.offset) + relative;
|
||||
if (mapped < 0n || mapped > BigInt(Number.MAX_SAFE_INTEGER))
|
||||
if (mapped < 0n || mapped > MAX_SAFE_INTEGER_BIGINT)
|
||||
return {
|
||||
kind: "unsupported",
|
||||
reason: "Legacy rebase file offset exceeds the safe integer range",
|
||||
@@ -1580,17 +1615,35 @@ const hasUnresolvedLegacyBytes = (
|
||||
)
|
||||
return true;
|
||||
if (binds.index.exhausted) return true;
|
||||
if (rebases.membership !== null)
|
||||
for (let slot = first; slot < end; slot++) {
|
||||
const state = legacyRebaseState(segments, rebases.membership, slot);
|
||||
// Text rebases own four bytes; conflicting kinds may include a pointer.
|
||||
if (
|
||||
typeof state !== "number" ||
|
||||
state === REBASE_CONFLICT ||
|
||||
(state === REBASE_NON_POINTER && slot + 4n > address)
|
||||
)
|
||||
return true;
|
||||
if (rebases.membership !== null && first < end) {
|
||||
const baseOffset = singleLegacySegmentFileBase(segments, first, end - 1n);
|
||||
if (baseOffset !== null) {
|
||||
const count = Number(end - first);
|
||||
for (let delta = 0; delta < count; delta += 1) {
|
||||
const fileOffset = baseOffset + delta;
|
||||
const state =
|
||||
((rebases.membership[Math.floor(fileOffset / 4)] ?? 0) >>
|
||||
((fileOffset % 4) * 2)) &
|
||||
3;
|
||||
if (
|
||||
state === REBASE_CONFLICT ||
|
||||
(state === REBASE_NON_POINTER && delta > 3)
|
||||
)
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
for (let slot = first; slot < end; slot++) {
|
||||
const state = legacyRebaseState(segments, rebases.membership, slot);
|
||||
// Text rebases own four bytes; conflicting kinds may include a pointer.
|
||||
if (
|
||||
typeof state !== "number" ||
|
||||
state === REBASE_CONFLICT ||
|
||||
(state === REBASE_NON_POINTER && slot + 4n > address)
|
||||
)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -1614,14 +1667,29 @@ const hasPartialLegacyPointerWrite = (
|
||||
return true;
|
||||
}
|
||||
if (binds.index.exhausted) return true;
|
||||
if (rebases.membership !== null)
|
||||
for (let at = low; at <= high; at += 1n) {
|
||||
const state = legacyRebaseState(segments, rebases.membership, at);
|
||||
if (typeof state !== "number" || state === REBASE_CONFLICT) return true;
|
||||
if (state === REBASE_NON_POINTER && at + 4n > address) return true;
|
||||
if (state === REBASE_POINTER && at !== address && at + 8n > address)
|
||||
return true;
|
||||
if (rebases.membership !== null) {
|
||||
const baseOffset = singleLegacySegmentFileBase(segments, low, high);
|
||||
if (baseOffset !== null) {
|
||||
for (let delta = 0; delta <= 14; delta += 1) {
|
||||
const fileOffset = baseOffset + delta;
|
||||
const state =
|
||||
((rebases.membership[Math.floor(fileOffset / 4)] ?? 0) >>
|
||||
((fileOffset % 4) * 2)) &
|
||||
3;
|
||||
if (state === REBASE_CONFLICT) return true;
|
||||
if (state === REBASE_NON_POINTER && delta > 3) return true;
|
||||
if (state === REBASE_POINTER && delta !== 7) return true;
|
||||
}
|
||||
} else {
|
||||
for (let at = low; at <= high; at += 1n) {
|
||||
const state = legacyRebaseState(segments, rebases.membership, at);
|
||||
if (typeof state !== "number" || state === REBASE_CONFLICT) return true;
|
||||
if (state === REBASE_NON_POINTER && at + 4n > address) return true;
|
||||
if (state === REBASE_POINTER && at !== address && at + 8n > address)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
@@ -978,30 +978,43 @@ export function* readElfRelocationWritesSteps(
|
||||
const start = relrSize === 0n ? 0 : fileOffset(relr, Number(relrSize));
|
||||
if (start === null) return malformed("RELR table is not file-backed");
|
||||
let next: bigint | undefined;
|
||||
for (let at = start; at < start + Number(relrSize); at += word) {
|
||||
const wordBig = BigInt(word);
|
||||
const addrSpace = 1n << BigInt(input.bits);
|
||||
const maxAddr = addrSpace - wordBig;
|
||||
const strideBig = BigInt((input.bits - 1) * word);
|
||||
const end = start + Number(relrSize);
|
||||
const add = (address: bigint, recordOffset: number) => {
|
||||
if (address > maxAddr)
|
||||
malformed("RELR destination overflows the ELF address space");
|
||||
const slot = target(address, word);
|
||||
return write(address, word, slot === null ? 0n : rawWord(slot), {
|
||||
source: {
|
||||
encoding: "relr",
|
||||
record_file_offset: recordOffset,
|
||||
kind: null,
|
||||
},
|
||||
symbol_index: null,
|
||||
addend: null,
|
||||
});
|
||||
};
|
||||
for (let at = start; at < end; at += word) {
|
||||
if ((at - start) % (word * 4096) === 0) yield;
|
||||
const entry = rawWord(at);
|
||||
const add = (address: bigint) => {
|
||||
if (address > (1n << BigInt(input.bits)) - BigInt(word))
|
||||
malformed("RELR destination overflows the ELF address space");
|
||||
const slot = target(address, word);
|
||||
return write(address, word, slot === null ? 0n : rawWord(slot), {
|
||||
source: { encoding: "relr", record_file_offset: at, kind: null },
|
||||
symbol_index: null,
|
||||
addend: null,
|
||||
});
|
||||
};
|
||||
if ((entry & 1n) === 0n) {
|
||||
yield add(entry);
|
||||
next = entry + BigInt(word);
|
||||
yield add(entry, at);
|
||||
next = entry + wordBig;
|
||||
} else {
|
||||
if (next === undefined)
|
||||
return malformed("RELR bitmap has no preceding address");
|
||||
for (let bit = 1; bit < input.bits; bit++)
|
||||
if ((entry & (1n << BigInt(bit))) !== 0n)
|
||||
yield add(next + BigInt((bit - 1) * word));
|
||||
next += BigInt((input.bits - 1) * word);
|
||||
if (next > 1n << BigInt(input.bits))
|
||||
let bits = entry >> 1n;
|
||||
let addr = next;
|
||||
while (bits !== 0n) {
|
||||
if ((bits & 1n) !== 0n) yield add(addr, at);
|
||||
bits >>= 1n;
|
||||
addr += wordBig;
|
||||
}
|
||||
next += strideBig;
|
||||
if (next > addrSpace)
|
||||
malformed("RELR bitmap overflows the ELF address space");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,9 +236,9 @@ export const traceNativeInvestigationGraph = (
|
||||
else entries.push(edge);
|
||||
}
|
||||
|
||||
while (queue.length > 0) {
|
||||
const current = queue.shift();
|
||||
if (current === undefined) break;
|
||||
let queueHead = 0;
|
||||
while (queueHead < queue.length) {
|
||||
const current = queue[queueHead++]!;
|
||||
const adjacent = adjacency.get(current.id) ?? [];
|
||||
|
||||
for (const edge of adjacent) {
|
||||
|
||||
Reference in new issue
Block a user