Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/launchpad/size/symbols/macho_symbol_sizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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
Expand Down
75 changes: 75 additions & 0 deletions tests/unit/size/test_macho_symbol_sizes.py
Original file line number Diff line number Diff line change
@@ -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