Skip to content

Reject three-byte integers in Arch.struct_fmt - #364

Open
zardus wants to merge 2 commits into
masterfrom
feature/fix-word-width
Open

zardus wants to merge 2 commits into
masterfrom
feature/fix-word-width

Conversation

@zardus

@zardus zardus commented Aug 9, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

Arch.struct_fmt() returns Z for a three-byte word. Python's struct module does not define Z, so callers raise when they use the result. This affects explicit struct_fmt(size=3) calls. It also affects 14 of pypcode's 187 SLEIGH languages, including the PIC, dsPIC, HCS-12, and extended AVR families:

ArchPcode('HCS-12:BE:24:default')  bits=24 bytes=3
    .struct_fmt()                          -> '>Z'
    struct.calcsize('>Z')                  -> error: bad char in struct format
    struct.unpack('>Z', b'\x01\x02\x03')   -> error: bad char in struct format

The exception occurs in the caller's struct call instead of in the architecture that created the format string.

Root cause

archinfo/arch.py maps size 3 to a character that struct does not define:

elif size == 3:
    fmt_size = "Z"  # special case for 24-bit architectures like AVR8

struct defines integer formats for 1, 2, 4, and 8 bytes. It has no integer format for 3 bytes. The existing else branch already rejects sizes 5, 6, 7, and 16. Size 3 was the only unsupported size that returned a format string.

Fix

The change removes the size == 3 branch. A three-byte word now reaches the existing else branch:

ArchPcode('HCS-12:BE:24:default')  bits=24 bytes=3
    .struct_fmt()                          -> ValueError: Invalid size: struct has no format character for a 3-byte integer

The error now names the unsupported size. The docstring now states this constraint. Callers that need a 24-bit word must assemble it from bytes because struct_fmt cannot express it.

Related change: angr/cle#721

Details: #364 (comment)

sync: angr/cle#721

session: sharpen

@zardus

zardus commented Aug 9, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

At head 02d70fa9910ff5aa521fc186be057f96b5827c88, Arch.struct_fmt() raises ValueError for a three-byte integer. Baseline bf85c7e47bb469878c564480e28c677abbe35acc returns Z, which struct rejects.

Of the 183 p-code languages in pypcode 4.0.1.dev0, 14 report a three-byte word. They are HCS-12, HCS12, HCS-12X, PIC-18, PIC-24E/F/H, dsPIC30F, dsPIC33C/E/F, and the three extended AVR languages. All of them returned Z on the baseline. The affected path includes Clemory.unpack_word in angr/cle#721.

The earlier branch state used head 6faa1ee404c78c908317aac60868f35955b2ea02 and baseline da171ca0dd8eec1e16dd9c4f04576b2c958e2aea. The current branch uses head 02d70fa9910ff5aa521fc186be057f96b5827c88 and baseline bf85c7e47bb469878c564480e28c677abbe35acc. git range-diff da171ca0dd8eec1e16dd9c4f04576b2c958e2aea..6faa1ee404c78c908317aac60868f35955b2ea02 bf85c7e47bb469878c564480e28c677abbe35acc..02d70fa9910ff5aa521fc186be057f96b5827c88 reports that every commit is unchanged. git diff 6faa1ee404c78c908317aac60868f35955b2ea02 02d70fa9910ff5aa521fc186be057f96b5827c88 contains only changes from master: 4 files changed, 24 insertions, and 7 deletions. Master changed tests/test_pcode.py but did not change archinfo/arch.py.

@angr-bot

angr-bot commented Aug 9, 2026

Copy link
Copy Markdown
Member

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

@zardus

zardus commented Aug 28, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Before this change, Arch.struct_fmt() returned a format string with Z for a three-byte word. struct does not define Z, so the caller raised an error.

Before

archinfo master
archinfo: archinfo at master f92307b

ArchPcode('HCS-12:BE:24:default')  bits=24 bytes=3
    .struct_fmt()                          -> '>Z'
    struct.calcsize('>Z')                  -> error: bad char in struct format
    struct.unpack('>Z', b'\x01\x02\x03')   -> error: bad char in struct format
ArchPcode('PIC-24E:LE:24:default')  bits=24 bytes=3
    .struct_fmt()                          -> '<Z'
    struct.calcsize('<Z')                  -> error: bad char in struct format
    struct.unpack('<Z', b'\x01\x02\x03')   -> error: bad char in struct format
ArchPcode('avr8:LE:16:extended')  bits=24 bytes=3
    .struct_fmt()                          -> '<Z'
    struct.calcsize('<Z')                  -> error: bad char in struct format
    struct.unpack('<Z', b'\x01\x02\x03')   -> error: bad char in struct format

ArchAMD64().struct_fmt(size=1) -> '<B', struct.calcsize -> 1
ArchAMD64().struct_fmt(size=2) -> '<H', struct.calcsize -> 2
ArchAMD64().struct_fmt(size=3) -> '<Z', struct.calcsize -> error: bad char in struct format
ArchAMD64().struct_fmt(size=4) -> '<I', struct.calcsize -> 4
ArchAMD64().struct_fmt(size=8) -> '<Q', struct.calcsize -> 8

After this change, Arch.struct_fmt() raises ValueError for a three-byte word. The supported sizes keep the same format strings.

After

with this change
archinfo: archinfo at this branch

ArchPcode('HCS-12:BE:24:default')  bits=24 bytes=3
    .struct_fmt()                          -> ValueError: Invalid size: struct has no format character for a 3-byte integer
ArchPcode('PIC-24E:LE:24:default')  bits=24 bytes=3
    .struct_fmt()                          -> ValueError: Invalid size: struct has no format character for a 3-byte integer
ArchPcode('avr8:LE:16:extended')  bits=24 bytes=3
    .struct_fmt()                          -> ValueError: Invalid size: struct has no format character for a 3-byte integer

ArchAMD64().struct_fmt(size=1) -> '<B', struct.calcsize -> 1
ArchAMD64().struct_fmt(size=2) -> '<H', struct.calcsize -> 2
ArchAMD64().struct_fmt(size=3) -> ValueError: Invalid size: struct has no format character for a 3-byte integer
ArchAMD64().struct_fmt(size=4) -> '<I', struct.calcsize -> 4
ArchAMD64().struct_fmt(size=8) -> '<Q', struct.calcsize -> 8

struct_fmt() returned "Z" for a 3-byte word. That is not a struct format
character, so every 24-bit architecture handed its callers a format string
that raises "bad char in struct format" on use. struct has integer format
characters for 1, 2, 4 and 8 bytes only and no way to describe a 3-byte
integer, so report the size as invalid instead of inventing a character for
it; such a word has to be assembled from bytes by the caller.
@zardus
zardus force-pushed the feature/fix-word-width branch from 02d70fa to a6e6c48 Compare August 29, 2026 06:45
register_arch documents regexes as "str or SRE_Pattern", but the type check
used re._pattern_type, which CPython removed in 3.7, so passing a compiled
pattern raised AttributeError instead of registering the architecture. Use the
public re.Pattern.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@zardus

zardus commented Aug 29, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Deleting the two elif size == 3 statements changed the pylint score for archinfo/arch.py from 9.70 to 9.69. The pylint messages did not change. The score uses 10 - 10*(5*E + W + R + C)/statements, so two fewer statements changed the result.

              base b8ffe85   head a6e6c48
score         9.70           9.69
statements    526            524
messages      12 (1E 7W 3R 1C) -- identical set, diff of the two lists is empty

Commit 2b741b0 removes an existing pylint message. At arch.py:843, the code used isinstance(rx, re._pattern_type). CPython removed re._pattern_type after version 3.7, and archinfo requires Python 3.12 or later. As a result, the compiled-pattern argument described by register_arch raised AttributeError. The code now uses re.Pattern. All in-tree callers pass strings, so they did not expose this problem.

The merge-base-relative scores are:

ok   archinfo/arch.py: 9.70 -> 9.79
ok   tests/test_pcode.py: 10.00 -> 10.00
ok   tests/test_struct_fmt.py: new file scores 10.00/10.00
ok   archinfo/arch.py: pyright badness 0.2217529 -> 0.2107482

session: sharpen

@zardus zardus changed the title Reject word sizes struct cannot express Reject three-byte integers in Arch.struct_fmt 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