#7293·angr

Decompiler: 16-byte stack buffer emitted as `int`, with byte offsets scaled by four

Author: SensorLockCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbug

Description

Decompiling the x86 function sub_436f40 from the binary 82ce4d6615793fec42a57571f6161794de24362be69a5103cf3c1192aa4b6ecb exposes two related issues:

  1. Incorrect storage declaration: a recovered 16-byte stack object has an unresolved type (SimTypeBottom) and is emitted as int v0, reserving only four bytes on this target.
  2. Incorrect constant-offset rendering: byte offsets into that object are emitted as indexing through int *, followed by a cast to char *. The cast happens after indexing, so offsets 1 through 13 become byte offsets 4 through
    1. The later indexed loops, in contrast, use one-byte strides.

Improving array recovery would avoid this instance, but code generation should also preserve byte offsets when type inference fails.

Full observed decompilation

This is the fresh decompilation produced by the analysis sequence above, without corrections.

c
int sub_436f40(void* a0)
{
    void* v4;  // esi
    char v5;  // bl
    void* ch;  // edi
    unsigned int v7;  // ebp
    void* v8;  // eax
    unsigned int i;  // ecx
    unsigned int j;  // ecx
    char v11;  // bl
    char v12;  // bl
    int v0;  // [bp-0x2c]
    char v1;  // [bp-0x1c]
    char v2;  // [bp-0x18]
    void* v3;  // [bp-0x14]

    v4 = a0;
    v5 = 0;
    if (OpenClipboard(NULL))
    {
        GetWindowLongW(NULL, 0);
        ch = GetClipboardData(1);
        v5 = 0;
        if (ch)
        {
            v8 = sub_41e150(GlobalLock(ch));
            if (v8)
            {
                v2 = v1;
                *((char *)&v0) = 193;
                *((char *)&(&v0)[1]) = 39;
                *((char *)&(&v0)[2]) = 37;
                *((char *)&(&v0)[3]) = 41;
                *((char *)&(&v0)[4]) = 222;
                *((char *)&(&v0)[5]) = 46;
                *((char *)&(&v0)[6]) = 217;
                *((char *)&(&v0)[7]) = 43;
                *((char *)&(&v0)[8]) = 216;
                *((char *)&(&v0)[9]) = 225;
                *((char *)&(&v0)[10]) = 38;
                *((char *)&(&v0)[11]) = 57;
                *((char *)&(&v0)[12]) = 36;
                *((char *)&(&v0)[13]) = 183;
                i = 14;
                do
                {
                    *((char *)&v0 + i) = 0;
                    i += 1;
                } while (i != 16);
                for (j = 0; j < 16; j += 1)
                {
                    v11 = *((char *)&v0 + j);
                    v3 = v8;
                    v8 = v3;
                    *((char *)&v0 + j) = 100 - (char)((224 - j | v11) & (3695615712 - j | v11) + j - v11 + 31);
                }
                sub_40d180(v4, &v0, v7, v8);
            }
            GlobalUnlock(ch);
            v5 = v12;
        }
        CloseClipboard();
    }
    return v5 & 1;
}

Machine-code evidence

The relevant instructions write consecutive bytes starting at esp + 0xc:

asm
00436fd4  mov byte ptr [esp + 0xc], 0xc1
00436fd9  mov byte ptr [esp + 0xd], 0x27
00436fde  mov byte ptr [esp + 0xe], 0x25
00436fe3  mov byte ptr [esp + 0xf], 0x29
00436fe8  mov byte ptr [esp + 0x10], 0xde
00436fed  mov byte ptr [esp + 0x11], 0x2e
00436ff2  mov byte ptr [esp + 0x12], 0xd9
00436ff7  mov byte ptr [esp + 0x13], 0x2b
00436ffc  mov byte ptr [esp + 0x14], 0xd8
00437001  mov byte ptr [esp + 0x15], 0xe1
00437006  mov byte ptr [esp + 0x16], 0x26
0043700b  mov byte ptr [esp + 0x17], 0x39
00437010  mov byte ptr [esp + 0x18], 0x24
00437015  mov byte ptr [esp + 0x19], 0xb7
0043701a  mov ecx, 0xe
0043701f  mov byte ptr [esp + ecx + 0xc], 0
00437024  add ecx, 1
00437027  cmp ecx, 0x10
0043702a  jne 0x43701f

The decoding loop uses the same base and byte stride:

asm
0043702c  xor ecx, ecx
0043702e  jmp 0x437030
00437030  cmp ecx, 0x10
00437033  jae 0x437072
00437035  movzx ebx, byte ptr [esp + ecx + 0xc]
          ; arithmetic omitted
00437069  mov byte ptr [esp + ecx + 0xc], bl
0043706d  add ecx, 1
00437070  jmp 0x437030

The buffer's address is subsequently formed by:

asm
00437074  lea edx, [esp + 0xc]

Thus the initialized and decoded range is 16 bytes: offsets 0 through 15. There is no four-byte stride in the initial byte stores.

Issue 1: unresolved aggregate type becomes an undersized scalar declaration

Instrumentation of the fresh decompilation showed stack-variable versions at bp-0x2c with size 16 bytes, but their types at code generation were BOT. The object extent was recovered; its usable aggregate type was not.

The initialization is represented as partial updates of a 128-bit object:

Insert(0<128>,                  0<64>, 193<8>)
Insert(vvar_264{s-44|16b},       1<64>,  39<8>)
Insert(vvar_265{s-44|16b},       2<64>,  37<8>)

In angr/analyses/variable_recovery/engine_ail.py, SimEngineVRAIL._handle_expr_Insert evaluates the base, offset, and value, then returns self._top(expr.bits). All 14 initialization updates were observed to return a result with typevar=None.

This loses type information at the partial-update boundary. It does not encode the inserted byte's offset and width as evidence about the containing object's layout. The remaining accesses did not recover a usable array type in this run.

In angr/knowledge_plugins/variables/variable_manager.py, set_variable_type replaces bottom types with scalar defaults for sizes 1, 2, 4, and 8, but not 16. The unresolved type survives. In angr/sim_type.py, SimTypeBottom.c_repr renders an unlabeled bottom type as int.

Consequently, a known 16-byte stack object becomes a four-byte C object. Even the correctly byte-scaled decoder accesses then exceed its declared extent.

Issue 2: unknown internal stride disagrees with the emitted C type

In angr/analyses/decompiler/structured_codegen/c.py, CStructuredCodeGenerator._access_constant_offset computes:

python
stride = 1 if base_type.size is None else base_type.size // self.project.arch.byte_width or 1
index, remainder = divmod(offset, stride)

Instrumentation confirmed that the accesses at offsets 1, 2, and 13 reach this code with base type BOT* and access type char. The unknown base size selects stride 1, so byte offset 1 becomes index 1.

However, the emitted declaration is int v0, making &v0 an int *. Consider:

c
*((char *)&(&v0)[1]) = 39;

&(&v0)[1] is equivalent to &v0 + 1. On this target that advances four bytes. Casting the resulting address to char * changes the access width, not the preceding pointer arithmetic. The effective address is therefore (char *)&v0 + 4, not (char *)&v0 + 1.

The same error scales offsets 1 through 13 by four. With the declaration shown, these stores also access outside v0, so the emitted C has undefined behavior. The discrepancy describes the generated C, not an equivalent out-of-bounds write in the original instructions.

Expected output

A byte-array declaration should preserve the recovered storage extent:

c
unsigned char v0[16];

The initialization should address consecutive elements:

c
v0[0] = 193;
v0[1] = 39;
v0[2] = 37;
/* ... */
v0[13] = 183;

Even without successful array inference, byte offsets must remain byte offsets. For example, the address calculation for the second write should be:

c
*((unsigned char *)&v0 + 1) = 39;

That expression fixes the address calculation only; preserving a 16-byte declaration is independently necessary.

Suggested fix boundaries and regression coverage

  1. Code generation: for unresolved or unsized pointee types, cast to a byte pointer before applying byte offsets. Do not produce typed indexing whose C stride differs from the internal stride. Cover both loads and stores, as well as references to interior locations.
  2. Storage fallback: preserve the known extent of unresolved stack-memory objects, for example with byte-array storage.
  3. Type recovery: propagate object-layout evidence through partial Insert/Extract operations and connect constant and indexed accesses to the same stack object. A byte insertion does not make the entire object a byte; forwarding the initial 128-bit zero constant's scalar type is also insufficient.

Regression tests should independently verify:

  • A 16-byte unresolved stack object is not declared as a four-byte scalar.
  • Initial writes at offsets 0 through 13 target consecutive bytes.
  • Indexed reads and writes use the same buffer and byte stride.
  • Constant-offset rendering remains correct when array inference is deliberately unavailable, rather than testing only the successful array-inference path.
  • Scalar partial updates and mixed-width structure accesses retain their intended semantics.

Steps to reproduce the bug

The sample is available from VirusTotal. Given a local copy, run:

python
import sys

import angr

project = angr.Project(sys.argv[1], auto_load_libs=False)
project.analyses.CFGFast(normalize=True)
project.analyses.CompleteCallingConventions(workers=0)
decompilation = project.analyses.Decompiler(
    project.kb.functions[0x436f40], preset="malware"
)
if decompilation.codegen is None or decompilation.codegen.text is None:
    raise RuntimeError("No decompilation produced")
print(decompilation.codegen.text)

Environment

Linux with Python 3.14.2 and angr 10.0.0

Additional context

No response