Skip to content

x86-16 afva does not reconcile typed BP arguments after changing the calling conventionΒ #26760

Closed

Description

@andreibelov

Environment

$ date
r2 -v
uname -ms
Fri 18 Sep 01:14:14 BST 2026
radare2 6.2.2 +37545 abi:142 @ linux-x86_64
birth: git.6.2.2 2026-09-17__17:41:32
commit: ad27058877024389292fddf12e1db6e13824ba34
options: gpl -O? cs:5 cl:2 make
Linux x86_64

Description

On x86-16, the default calling convention is fastcall.
If a function is first analysed with af, radare2 recovers a generic BP-relative argument under that convention.
Changing the function to cdecl afterwards and running afva does not reconcile the existing autogenerated variable with the known typed prototype.

The function signature is correct: int typed16 (short value);
but the recovered variable remains generic: arg int16_t arg_4h @ bp+0x4

The expected variable is:

arg short value @ bp+0x4

Repeated afva calls leave the stale name and type unchanged.

This was found while testing #26747 after the typed-function recovery change.

Further debugging shows that the 2-byte slot calculation is correct: during cdecl recovery, ptr=4, bp_off=2, and frame_off=2. The problem is that the existing generic variable causes recovery to return before the prototype argument can be applied.

This appears related to, but distinct from, #19595. #19595 reports a false-positive ax register argument in x86-16 analysis; this report is about stale BP-variable metadata after changing the function calling convention.

Potential area of change

When a function's calling convention changes, invalidate or reconcile autogenerated argument variables.
For typed functions, afva could replace a default arg_* name/type when the variable's recovered storage slot matches a
declared prototype argument, while continuing to preserve explicitly renamed or retyped variables.

Test

The test uses a conventional 16-bit frame:

push bp
mov bp, sp
sub sp, 2
mov ax, word [bp+4]
mov word [bp-2], ax
leave
ret

Minimal reproduction:

r2 -NN -q -a x86 -b 16 malloc://32
wx 5589e583ec028b46048946fec9c3
f typed16
td int typed16(short value);
af
afc cdecl
afva
afcf
afvb

Actual output:

int typed16 (short value);
arg int16_t arg_4h @ bp+0x4
var int16_t var_2h @ bp-0x2

Running afva again produces the same result.

Control

If variable recovery is disabled until after the correct calling convention is
selected, the prototype is mapped correctly:

wx 5589e583ec028b46048946fec9c3
f typed16
td int typed16(short value);
e anal.vars=false
af
afc cdecl
e anal.vars=true
afva
afcf
afvb

Output:

int typed16 (short value);
arg short value @ bp+0x4
var int16_t var_2h @ bp-0x2

Deleting the recovered variables before retrying also works:

afc cdecl
afv-*
afva

Activity

  1. added 2 commits that reference this issue on Sep 20, 2026
    9bdae45
    063dc7b
  2. trufae commented on Sep 20, 2026

    @trufae
    Collaborator

    Can you try now? The pr is merged and took the libeety to extend the thing for 8bit cpua like the gb

  3. andreibelov commented on Sep 20, 2026

    @andreibelov
    Author

    Confirmed on the merged commit 4b3b55103b.

    The original x86-16 reproducer now reports:

    r2 int typed16 (short value); arg short value @ bp+0x4 var int16_t var_2h @ bp-0x2 
    

    Also:

    • Repeated recovery is idempotent: repeated afva calls, including a 200-pass check, keep the same two variables with no duplicates.

    • Explicit user edits are preserved: renamed and retyped variables remain unchanged after further afva passes.

    • db/anal/x86_16_vars and db/anal/small_register_vars pass 6/6 on the merged fix;

    • test_anal_x86 passes, as do the adjacent variable/calling-convention/function unit tests exercised by the reviews.

    • The x86-16 tests also pass through Zydis.

    • Additional spot checks covered overlapping byte-register halves, both overwrite orders, conditional writes, memory base/index reads, XOR/SUB self-zeroing, forwarded calls, and the generalised Game Boy byte/pair register handling.

    Results matched the PR's intended behaviour.

    One tiny observation: after a default variable has already been reconciled to a prototype, changing that prototype again does not overwrite the now non-default variable.

    wx 5589e583ec028b46048946fec9c3
    f typed16
    td int typed16(short value);
    af
    afc cdecl
    afva
    
    afcf
    afvb
    
    # Initial result:
    int typed16 (short value);
    arg short value @ bp+0x4
    var int16_t var_2h @ bp-0x2
    
    # replace the prototype and recover again:
    td int typed16(int other);
    afva
    
    afcf
    afvb
    tk func.typed16.arg.0
    
    # result:
    int typed16 (int other);
    arg short value @ bp+0x4
    var int16_t var_2h @ bp-0x2
    int,other
    

    That is consistent with the user-edit preservation rule, but may be worth documenting or handling explicitly if repeated prototype replacement is intended.

    Excellent work extending the solution beyond the original x86-16 case to the small-register analysis shared with 8-bit targets!
    Thank you!

  4. trufae commented on Sep 21, 2026

    @trufae
    Collaborator

    Beautiful. Thanks for confirming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions