Skip to content

Load container-owned Mach-O MH_EXECUTE files - #780

Open
zardus wants to merge 1 commit into
masterfrom
feature/fix-cle-macho-container-mh-execute
Open

zardus wants to merge 1 commit into
masterfrom
feature/fix-cle-macho-container-mh-execute

Conversation

@zardus

@zardus zardus commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

cle cannot load a Mach-O executable from a container. The failure occurs with
binaries/tests/x86_64/fauxware.macho.cart:

EXCEPTION: AssertionError: An file of type MH_EXECUTE should be the main bin, this should not happen
  raised at: File ".../cle/backends/macho/macho.py", line 182, in __init__

Every CaRT-wrapped Mach-O executable reaches this assertion during construction.

Cause

            if self.pic and self.filetype == MachoFiletype.MH_EXECUTE:
                assert self.is_main_bin, "An file of type MH_EXECUTE should be the main bin, this should not happen"

A container registers itself as loader._main_object before it loads its children.
The child is therefore constructed with is_main_bin=False. The loader later
promotes the child through force_main_object. The assertion runs before that
promotion.

Fix

This change removes the assertion. The guarded branch sets linked_base and
mapped_base to 0x100000000. These values do not depend on the object being
the main binary. A bare assert also disappears under python -O, while cle
uses CLEError for load failures.

main object: MachO os=macos mapped_base=0x100000000 entry=0x100000de0

universal2.py has a workaround for the same assertion. Removing that workaround
changes fat-binary placement, so it belongs in a separate change.

Validation: #780 (comment)

sync: angr/binaries#190

@zardus

zardus commented Aug 24, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This record covers head 0df1c1713a6682328a3ebd463ea8080a115750f0
against baseline 0e77ade3c39a3cee05f65051e57955675e1ac21b, with
angr/binaries#190 checked out.

The measurements were recorded at 5abacfce777d557d945ffb2c47df97f8bbf33197
on baseline 6951e9221554f987e3971aa51f9c280a67d60324. Later rebases
replayed the commits without conflicts or fixups. The range diffs reported the
commits unchanged, and the added and removed lines stayed byte-identical.

unpatched:  AssertionError: An file of type MH_EXECUTE should be the main bin, this should not happen
patched:    MachO mapped_base=0x100000000 entry=0x100000de0 os=macos

Unpatched master also loads the object under python -O:

$ python -O -c "cle.Loader(<cart>, main_opts={'arc4_key': ...}, auto_load_libs=False)"
OK type=MachO is_main_bin=True mapped_base=0x100000000 entry=0x100000de0 os=macos

The CaRT load and the direct payload load recorded the same fields:

                              via CaRT      raw payload
linked_base / mapped_base     0x100000000   0x100000000
min / max addr                0x100000000 / 0x1037d3851   same
entry                         0x100094850   same
segments / sections           5 / 15        same
symbols / relocations         28647 / 111521  same
base 6951e92   238 passed, 1 failed, 9 skipped
patched        239 passed, 1 failed, 9 skipped
Lint       cle/backends/macho/macho.py   10.00 -> 10.00
           tests/test_cart.py            10.00 -> 10.00
Typecheck  cle/backends/macho/macho.py   0.17133956 -> 0.17120623
           tests/test_cart.py            0.0 -> 0.0

The fixture in angr/binaries#190 wraps the existing
tests/x86_64/fauxware.macho file in a CaRT container. It uses the same ARC4
key as the other .cart fixtures. It unpacks byte for byte to the existing
file, adds 1773 bytes, and adds no new binary content.

This change touches the same hunk as #727 and #728. If one lands first, the
other needs a rebase. #728 removes the self.pic guard but keeps the assertion.
That change would extend this failure to non-PIC executables loaded from a
container.

@angr-bot

Copy link
Copy Markdown
Member

Corpus decompilation diffs can be found at angr/dec-snapshots@master...angr/cle_780

@zardus
zardus force-pushed the feature/fix-cle-macho-container-mh-execute branch from 5abacfc to cd51b11 Compare August 26, 2026 22:46
@zardus

zardus commented Aug 28, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This reproducer loads a Mach-O MH_EXECUTE from a CaRT container. The fixture
comes from the angr/binaries pull request linked in the description.

Reproducer
import logging, os
logging.getLogger("cle").setLevel(logging.CRITICAL)
import cle

BIN = os.environ["BINARIES"]   # a checkout of angr/binaries
T = lambda *p: os.path.join(BIN, "tests", *p)

path = T("x86_64", "fauxware.macho.cart")
ld = cle.Loader(path, auto_load_libs=False,
                main_opts={"arc4_key": b"\x02\xf53asdf\x00\x00\x00\x00\x00\x00\x00\x00\x00"})
obj = ld.main_object
print(f"main object: {type(obj).__name__} os={obj.os} "
      f"mapped_base={obj.mapped_base:#x} entry={obj.entry:#x}")

Before this change, the container owns the child until the loader promotes it.
The child therefore starts with is_main_bin=False, and the assertion fails.

cle master at 46a37333f4f59b0facf8774ee743ebc4cc074e9b
EXCEPTION: AssertionError: An file of type MH_EXECUTE should be the main bin, this should not happen
  raised at: File "/home/yans/angr/mega-corpus/scratch/sh20-05/wt/cle-before/cle/backends/macho/macho.py", line 182, in __init__

After this change, cle loads the executable at the same base and entry as a
direct load.

with this change, at cd51b112557aaf21eb6b599812c55f08cb372230
main object: MachO os=macos mapped_base=0x100000000 entry=0x100000de0

@zardus
zardus force-pushed the feature/fix-cle-macho-container-mh-execute branch 3 times, most recently from 570ea2b to 5c0a030 Compare August 30, 2026 04:46
A container backend (CaRT, universal binary, static archive) registers itself as
the loader's main object before calling _load_object_isolated, so every child it
loads is constructed with is_main_bin=False. For a CaRT-wrapped Mach-O the loader
then promotes the child through force_main_object, but only after the backend has
already been constructed, so MachO.__init__ saw is_main_bin=False and asserted.

Nothing in MachO needs the fact. The two other uses of is_main_bin are in the
MH_DYLIB branches, which this branch excludes, and the base address this branch
computes -- 2**32 for a 64-bit PIC executable, whose segment vaddrs are relative
to 0x100000000 -- is correct whether or not the object ends up being the main
binary; Loader._map_object already places a non-main PIC object with a free
linked_base at that address, and rebases it elsewhere when the address is taken.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@zardus
zardus force-pushed the feature/fix-cle-macho-container-mh-execute branch from 5c0a030 to 0df1c17 Compare September 4, 2026 15:45
@zardus zardus changed the title Load a Mach-O MH_EXECUTE that a container backend owns Load container-owned Mach-O MH_EXECUTE files Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants