Skip to content

[AUTOMATED] type_match: credit any struct pointer when the source type is a pointer to a struct - #110

Open
mahaloz wants to merge 1 commit into
mainfrom
feat/typematch-struct-pointee
Open

mahaloz wants to merge 1 commit into
mainfrom
feat/typematch-struct-pointee

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

A stripped binary keeps no struct names, so a decompiler can recover "this argument points at a struct" but never WORD *. type_match compares pointee names only, so a recovered struct_0 * scores the same miss as long. With this change, a decompiled pointer to any struct matches a ground-truth pointer to a struct at the same depth. Primitive pointees (char *, void *, size_t *) still need the exact type, so typing every pointer as a struct pointer still loses the char * match below:

cat > w.c <<'EOF'
typedef struct Word { char *text; int length; } WORD;
int put_word(WORD *w, char *s) { return w->length + s[0]; }
int main(void) { WORD w = {"hi", 2}; return put_word(&w, "x"); }
EOF
gcc -g -O0 w.c -o w
python - <<'EOF'
from pathlib import Path
from decbench.metrics.type_match import TypeMatchMetric, extract_ground_truth_types
from decbench.models.decompilation import FunctionDecompilation, VariableInfo
gt = extract_ground_truth_types(Path("w"))["put_word"]
for a0, a1 in [("struct_0 *", "char *"), ("struct_0 *", "struct_0 *")]:
    fn = FunctionDecompilation(
        name="put_word", address=0,
        decompiled_code="typedef struct struct_0 { char *f0; int f8; } struct_0;\n",
        variables=[VariableInfo(name="a0", type=a0, kind="arg", arg_index=0),
                   VariableInfo(name="a1", type=a1, kind="arg", arg_index=1)])
    md = TypeMatchMetric().compute_for_function(fn, ground_truth_vars=gt, backend="angr").metadata
    print(f"{a0}, {a1}: tp={md['tp']} fp={md['fp']} tp_kind_only={md.get('tp_kind_only')}")
EOF
# main:   struct_0 *, char *: tp=1 fp=1 tp_kind_only=None
#         struct_0 *, struct_0 *: tp=0 fp=2 tp_kind_only=None
# branch: struct_0 *, char *: tp=2 fp=0 tp_kind_only=1
#         struct_0 *, struct_0 *: tp=1 fp=1 tp_kind_only=1

The fix:

  • Ground truth: a pointer (any depth, references included) to a struct, class or union, through typedefs and qualifiers, gets a kind form <struct>*, <struct>** or <union>*. By-value structs, arrays and primitive pointees get none. A struct pointer never matches a union pointer.
  • Decompiled side: the kind comes only from the decompiler's own output. That means a struct/union keyword in the spelling, or a struct/union declaration of the pointee name in decompiled_code (angr prints typedef struct struct_0 {...} struct_0;). A bare struct_0 * or FILE * with no declaration gets nothing. Native, legacy and text-only producers all use the same rule.
  • No backend type-system field is added: the rule reads stored output, so published results can be re-evaluated from checkpoints without re-decompiling. Classifying keywordless names from the binary's own DWARF as a stand-in for backend type systems added only 2 TPs on coreutils O0 and O2.
  • tp_kind_only in function metadata counts kind-only matches, so recovering the real name still shows.
  • cache_version goes from 17 to 18. If another type_match change lands first, this needs one more bump on rebase.

Impact on coreutils O0 and O2, replayed from the published checkpoints. No function lost a TP.

  • angr: +2,355 TPs; perfect functions 389 → 726; O0 mean 0.386 → 0.580. This moves angr from fifth to first on coreutils O0.
  • IDA (+93 TPs) and Binja (+56 TPs): all are struct stat *-style spellings that Improve C type parsing and attribution #106 also matches by name.
  • Ghidra and kuna: unchanged.

The only conflict with #106 is the cache_version lines.

Tests: tests/test_type_match_struct_pointee.py builds gcc and g++ DWARF fixtures:

  • Pointee types: struct, union, typedef-to-pointer, const struct **, by-value, FILE *, char *, void *, size_t *, function pointer, and a C++ class and reference.
  • Cases: depth mismatch, struct vs union, an undeclared struct_0 *, and a decompiler that types every pointer as struct_0 * losing every primitive-pointer match.

All six fail on main.

Fixes #93.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Cw8GgP4ynoKxZeBJ7RWZiN

…o-struct ground truth

A stripped binary keeps no struct names, so a pointer-to-struct variable
could only match by an exact pointee name. Ground truth now carries a
kind form (<struct>*, <union>**) for pointers to structs, classes and
unions, and a decompiled pointer matches it at the same depth when its
own output says the pointee is a struct or union: a struct/union keyword
in the spelling or a declaration in decompiled_code. Primitive pointees
keep the exact rule. tp_kind_only reports kind-only matches separately.

Fixes #93

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Cw8GgP4ynoKxZeBJ7RWZiN

This branch has not been deployed

No deployments
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.

[AUTOMATED] type_match: credit any struct pointer when the source type is a pointer to a struct

1 participant