From 1546e31684445b3b54995a923398d8c8ae8bd822 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 23 Nov 2026 16:48:05 -0400 Subject: [PATCH] fix(size): Stop named __cstring symbols from absorbing following literals In __cstring, literals are mostly unnamed, so the distance-to-next-symbol heuristic charged every following literal to the preceding named constant (e.g. a 17-byte hex lookup table reported as ~79 KB). Clamp the size of a symbol in a C-string-literal section to its NUL terminator. --- .../size/symbols/macho_symbol_sizes.py | 24 ++++++ tests/unit/size/test_macho_symbol_sizes.py | 75 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/unit/size/test_macho_symbol_sizes.py diff --git a/src/launchpad/size/symbols/macho_symbol_sizes.py b/src/launchpad/size/symbols/macho_symbol_sizes.py index 206a0b79..8a07242e 100644 --- a/src/launchpad/size/symbols/macho_symbol_sizes.py +++ b/src/launchpad/size/symbols/macho_symbol_sizes.py @@ -76,6 +76,27 @@ def _is_measurable(self, sym: lief.MachO.Symbol) -> bool: return is_measurable + @staticmethod + def _is_cstring_section(section: lief.MachO.Section) -> bool: + """Whether the section holds NUL-terminated C string literals (``__cstring``).""" + return section.type == lief.MachO.Section.TYPE.CSTRING_LITERALS + + @staticmethod + def _cstring_literal_end(section: lief.MachO.Section, start: int, end: int) -> int: + """Clamp ``end`` to the NUL terminator of the literal starting at ``start``. + + Literals in ``__cstring`` are usually unnamed, so the distance-to-next-symbol + heuristic would otherwise charge every following literal to a named one. + """ + # Slice the memoryview before copying so only this symbol's span is + # materialized, not the whole (possibly multi-MB) section per symbol. + rel_start = start - section.virtual_address + chunk = bytes(section.content[rel_start : rel_start + (end - start)]) + nul = chunk.find(b"\x00") + if nul == -1: + return end + return start + nul + 1 + def _symbol_sizes(self, bin: lief.MachO.Binary) -> Generator[_SymbolSizeData]: """Yield symbol size data via the distance-to-next-symbol heuristic.""" @@ -129,6 +150,9 @@ def _symbol_sizes(self, bin: lief.MachO.Binary) -> Generator[_SymbolSizeData]: cached_section = None cached_section_va = None + if self._is_cstring_section(section): + end = self._cstring_literal_end(section, start, end) + offset_end = bin.virtual_address_to_offset(end) offset_start = bin.virtual_address_to_offset(start) size = 0 diff --git a/tests/unit/size/test_macho_symbol_sizes.py b/tests/unit/size/test_macho_symbol_sizes.py new file mode 100644 index 00000000..d6e3e53d --- /dev/null +++ b/tests/unit/size/test_macho_symbol_sizes.py @@ -0,0 +1,75 @@ +from types import SimpleNamespace + +import lief + +from launchpad.size.symbols.macho_symbol_sizes import MachOSymbolSizes + + +def _section(name: str, segment: str, virtual_address: int, content: bytes, section_type=None): + return SimpleNamespace( + name=name, + segment=SimpleNamespace(name=segment), + virtual_address=virtual_address, + size=len(content), + content=memoryview(content), + type=section_type, + ) + + +def _symbol(name: str, value: int): + return SimpleNamespace( + name=name, + value=value, + origin=lief.MachO.Symbol.ORIGIN.SYMTAB, + type=lief.MachO.Symbol.TYPE.SECTION, + ) + + +class _FakeBinary: + """Minimal stand-in for lief.MachO.Binary where virtual address == file offset.""" + + def __init__(self, sections, symbols): + self._sections = sections + self.symbols = symbols + + def section_from_virtual_address(self, address: int): + for section in self._sections: + if section.virtual_address <= address < section.virtual_address + section.size: + return section + return None + + def virtual_address_to_offset(self, address: int) -> int: + return address + + +def test_named_cstring_literal_does_not_absorb_following_unnamed_literals(): + # Mirrors getsentry/launchpad#678: `static const char hex[] = "0123456789abcdef";` + # is followed in __cstring by literals that have no symbol-table entries. + content = b"0123456789abcdef\x000x\x00writeToFile:atomically:\x00B28@?0@8@\x00" + cstring = _section("__cstring", "__TEXT", 0x1000, content, lief.MachO.Section.TYPE.CSTRING_LITERALS) + binary = _FakeBinary([cstring], [_symbol("_addressToString.hex", 0x1000)]) + + sizes = MachOSymbolSizes(binary).get_symbol_sizes() # type: ignore[arg-type] + + assert len(sizes) == 1 + assert sizes[0].mangled_name == "_addressToString.hex" + assert sizes[0].size == len(b"0123456789abcdef\x00") + + +def test_cstring_literal_size_stops_at_next_symbol_when_closer(): + content = b"first\x00second\x00third\x00" + cstring = _section("__cstring", "__TEXT", 0x2000, content, lief.MachO.Section.TYPE.CSTRING_LITERALS) + binary = _FakeBinary([cstring], [_symbol("_a", 0x2000), _symbol("_b", 0x2006)]) + + sizes = {s.mangled_name: s.size for s in MachOSymbolSizes(binary).get_symbol_sizes()} # type: ignore[arg-type] + + assert sizes == {"_a": 6, "_b": 7} + + +def test_non_cstring_symbol_still_uses_distance_to_section_end(): + text = _section("__text", "__TEXT", 0x3000, b"\x00" * 0x40, lief.MachO.Section.TYPE.REGULAR) + binary = _FakeBinary([text], [_symbol("_func", 0x3000)]) + + sizes = MachOSymbolSizes(binary).get_symbol_sizes() # type: ignore[arg-type] + + assert sizes[0].size == 0x40