Synthetic strncpy causes scalar loads to inherit array types
Description
A 17-byte x86 fixture demonstrates a decompiler failure: a four-byte stack
store followed by a four-byte scalar load becomes a
synthetic strncpy into a char[4] local, followed by returning that array
expression from an integer-returning function. The scalar load is lost.
The synthetic strncpy introduces a char * constraint on the stack address.
Instrumentation observed this constraint:
tv_400000_02 <: ptr32(sint8).load.<MAX_POINTSTO_BITS>@0In angr/analyses/typehoon/simple_solver.py, Sketch.add_constraint uses the
maximum stack-object size to expand the one-byte pointee constraint into a
four-element array. The resulting type is char[4].
The stack object's storage type is then used for the scalar value read from it. In larger functions this also propagates into register variables, array casts of scalar call results, and array-valued function return types. The small repro isolates the underlying problem without calls to external functions, register reuse, or a decoder.
Steps to reproduce the bug
The script selects malware for its pass-disabled comparisons, but that is
only a reproduction configuration, not a prerequisite. The failure was also
verified with basic, fast, full, and default. Each configuration used
a fresh project.
import sys
import angr
from angr.analyses.decompiler.optimization_passes.inlined_strcpy_simplifier import (
InlinedStrcpySimplifier,
InlinedStrcpySimplifierLate,
)
ENTRY = 0x400000
CODE = bytes.fromhex(
"83ec04" # sub esp, 4
"c704242f2e2d2c" # mov dword ptr [esp], 0x2c2d2e2f
"8b0424" # mov eax, [esp]
"83c404c3" # add esp, 4; ret
)
def main() -> None:
"""Compare independent decompilations with selected passes disabled."""
for mode, disabled in (
("default", []),
("no string copies", [InlinedStrcpySimplifier, InlinedStrcpySimplifierLate]),
):
project = angr.load_shellcode(CODE, "x86", load_address=ENTRY)
project.analyses.CFGFast(normalize=True)
project.analyses.CompleteCallingConventions(workers=0)
result = project.analyses.Decompiler(
project.kb.functions[ENTRY], preset="malware", disable_opts=disabled
)
if result.codegen is None or result.codegen.text is None:
raise RuntimeError(f"No decompilation for {mode}")
sys.stdout.write(
f"\n{mode}; expected machine return: 0x2c2d2e2f\n{result.codegen.text}\n"
)
if __name__ == "__main__":
main()The relevant comparison is:
- Default: all passes in the
malwarepreset. - No string copies: disable
InlinedStrcpySimplifierandInlinedStrcpySimplifierLate.
Both early and late string-copy passes must be disabled for the scalar-only control; disabling only the early pass allows the late pass to synthesize the copy.
Input and expected behavior
The 17-byte fixture is:
sub esp, 4
mov dword ptr [esp], 0x2c2d2e2f
mov eax, dword ptr [esp]
add esp, 4
retIt returns the integer 0x2c2d2e2f. Its bytes happen to spell "/.-," in
little-endian order, but the consumer reads a 32-bit value, not an address.
Equivalent C could simply be:
unsigned int f(void)
{
return 0x2c2d2e2f;
}Actual default output
int sub_400000(void)
{
char v0[4]; // [bp-0x4]
strncpy(v0, "/.-,", 4);
return v0;
}The copy itself writes the correct four bytes. The error is return v0:
the array expression decays to a pointer rather than reading its four bytes.
Returning it from an integer-returning function violates C's return conversion
constraints and does not represent the machine-code scalar load.
Output with both string-copy passes disabled
unsigned int sub_400000(void)
{
unsigned int v0; // [bp-0x4]
v0 = 741158447;
return v0;
}741158447 is 0x2c2d2e2f, the expected result.
Environment
Linux x86_64, Python 3.14.2, angr 10.0.0
Additional context
No response
Source: angr/angr