8000 PE: read the loading environment from the optional header by zardus · Pull Request #800 · angr/cle · GitHub
[go: up one dir, main page]

Skip to content

PE: read the loading environment from the optional header - #800

Merged
ltfish merged 1 commit into
masterfrom
feature/uefi-simos
Aug 29, 2026
Merged

ltfish merged 1 commit into
masterfrom
feature/uefi-simos

Conversation

@zardus
@zardus zardus commented Aug 29, 2026
Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

PE.os is the string "windows" for every PE, whatever the image says about
itself. That is wrong for UEFI modules, and downstream it is fatal: angr routes
on os, so HighMemDxe.efi -- a RISC-V UEFI boot service driver -- reaches
SimWindows, which asks for a Win32 syscall calling convention on an
architecture that has never run Windows:

$ python -c "import angr; angr.Project('HighMemDxe.efi', auto_load_libs=False)"
  File "angr/simos/windows.py", line 58, in __init__
    ...SYSCALL_CC[self.arch.name]["Win32"](self.arch)
KeyError: 'Win32'

The project never gets built, so nothing about the image can be analysed. A
corpus census over a sample of 11,989 objects put this at 2 objects overall and
2 of the 32 sampled from a Debian edk2 collection of 2,219.

Root cause

cle/backends/pe/pe.py assigned self.os = "windows" before it had even parsed
the headers. The optional header records which environment loads the image --
subsystems 10 through 13 are the EFI ones, and the Microsoft loader will not
load any of them -- and nothing read that field.

The same is true a second time for cle/backends/te.py: a Terse Executable is a
UEFI construct and exists nowhere else, and it set no os at all.

Fix

Read the subsystem. PE.os is "uefi" when the optional header declares one of
EFI_APPLICATION, EFI_BOOT_SERVICE_DRIVER, EFI_RUNTIME_DRIVER or EFI_ROM,
and "windows" otherwise; TE.os is "uefi" unconditionally. This is not a new
branch for an awkward case -- it replaces a constant with the field that was
always there to answer the question.

Nothing else in cle reads os for a PE, and the assignment moved to just after
the optional header is parsed, which is the first point at which it can be
answered.

Testing

tests/test_pe.py::test_uefi_image_is_not_windows loads the RISC-V driver added
in angr/binaries#218 and asserts os == "uefi" with the arch still RISCV64;
test_exe above it already asserts os == "windows" for a Windows
PE, so both directions are covered. tests/test_te.py::test_os_is_uefi covers
the two Terse Executables. On master the first fails with
assert 'windows' == 'uefi'.

python -m pytest tests: 244 passed, 9 skipped.

The consumer is the angr change linked in a comment below, which registers the
UEFI environment os == "uefi" now selects.

sync: angr/binaries#218

session: sharpen

Every PE was reported as os == "windows", so a UEFI module was handed to
angr's SimWindows, which reaches for a Win32 syscall calling convention. On
RISC-V there is no such convention and the project failed to build at all.
The optional header's subsystem says which environment loads the image; read
it. A Terse Executable only exists inside UEFI firmware, so it says so too.
@zardus
zardus commented Aug 29, 2026
Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

cle.Loader, angr.Project and CFGFast on the three UEFI images in
angr/binaries — the RISC-V driver added by angr/binaries#218 and the two Terse
Executables already there — before and after this change.

Before — the RISC-V driver raises before the project exists; the two TE
images get a bare SimOS and, on i386, the System V convention rather than the
Microsoft one the UEFI IA-32 binding specifies:

angr master + cle master
--- uefi/HighMemDxe.efi
Traceback (most recent call last):
  File "show.py", line 7, in <module>
    proj = angr.Project(p, auto_load_libs=False)
  File "angr/project.py", line 233, in __init__
    self.simos = os_mapping[self.loader.main_object.os](self)
  File "angr/simos/windows.py", line 58, in __init__
    self.fastfail.cc = SYSCALL_CC[self.arch.name]["Win32"](self.arch)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'Win32'
cle.Loader -> PE arch RISCV64 os 'windows'
--- i386/te_sections.te
cle.Loader -> TE arch X86 os None
angr.Project -> simos SimOS name None
factory.cc()  -> SimCCCdecl
CFGFast       -> 2 functions, 2 nodes
--- aarch64/te_sections.te
cle.Loader -> TE arch AARCH64 os None
angr.Project -> simos SimOS name None
factory.cc()  -> SimCCAArch64
CFGFast       -> 3 functions, 3 nodes

After — all three load as UEFI, and the driver that could not be built at
all yields 64 functions:

with this change
--- uefi/HighMemDxe.efi
cle.Loader -> PE arch RISCV64 os 'uefi'
angr.Project -> simos SimUefi name UEFI
factory.cc()  -> SimCCRISCV64
CFGFast       -> 64 functions, 360 nodes
--- i386/te_sections.te
cle.Loader -> TE arch X86 os 'uefi'
angr.Project -> simos SimUefi name UEFI
factory.cc()  -> SimCCMicrosoftCdecl
CFGFast       -> 2 functions, 2 nodes
--- aarch64/te_sections.te
cle.Loader -> TE arch AARCH64 os 'uefi'
angr.Project -> simos SimUefi name UEFI
factory.cc()  -> SimCCAArch64
CFGFast       -> 3 functions, 3 nodes

The AArch64 TE convention is unchanged, which is the point: only the two x86
bindings depart from the architecture's ordinary convention.

session: sharpen

@zardus
zardus commented Aug 29, 2026
Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Validation record for the UEFI chain: angr/binaries#218, #800, and the
angr environment change.

Base revisions: angr df4f8ba72, cle c7e0d4d, binaries a87538b.

Suites

suite result
cle python -m pytest tests 244 passed, 9 skipped
angr python -m pytest tests/simos tests/test_calling_conventions.py 24 passed, 9 subtests passed

Merge-base lint and typecheck, the comparison the hosted jobs make per
changed file:

cle   cle/backends/pe/pe.py   pylint 10.00 -> 10.00   badness 0.6419753 -> 0.6280193
cle   cle/backends/te.py      pylint 10.00 -> 10.00   badness 0.0680272 -> 0.0666667
cle   tests/test_pe.py        pylint 10.00 -> 10.00   badness 0.1250000 -> 0.1212121
cle   tests/test_te.py        pylint 10.00 -> 10.00   badness 0.0       -> 0.0
angr  angr/calling_conventions.py  pylint 9.93 -> 9.93    badness 0.0 -> 0.0
angr  angr/simos/__init__.py       pylint 10.00 -> 10.00  badness 0.0 -> 0.0
angr  angr/simos/uefi.py           new file, 10.00/10.00, badness 0.0
angr  tests/simos/test_uefi.py     new file, 10.00/10.00, badness 0.0

Both repositories' full pre-commit hook sets pass; black reformatted one blank
line in pe.py and the result is committed.

What did not run. The complete workspace gate did not run. Five corpus sweep
lanes hold the shared editable environment, and entering the workspace shell
reinstalls the editables into the toolchain those lanes are running on. The
scoped equivalent above ran in an isolated per-feature instance instead; every
in-tree shared object was snapshotted before and after and none was rewritten.
Not run, therefore: angr's full test suite, the native Rust suites, the GUI
suite, and the angr-management suites. Hosted CI covers them.

Test inputs. check-test-inputs.py passes on both repositories: no
assembled containers, and the new fixture is in angr/binaries.

session: sharpen

@ltfish
ltfish merged commit eac0e55 into master Aug 29, 2026
6 checks passed
@ltfish
ltfish deleted the feature/uefi-simos branch August 29, 2026 14:23
@angr-bot
Copy link
Copy Markdown
Member

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

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.

3 participants

0