angr.analyses.decompiler.known_patterns.containing_record¶
The CONTAINING_RECORD macro (Windows drivers) / container_of (Linux kernel):
CONTAINING_RECORD(addr, type, field) == (type *)((char *)addr - offsetof(...))
which compiles to a bare pointer-minus-small-constant, (p Sub off). Too
generic to run unsolicited (any pointer arithmetic matches), so opt-in. The
matched offset is captured as an extra call argument, and the return type is a
pointer to a record struct reserving the leading off bytes (so Typehoon can
grow it from the record’s field accesses).
Arch-independent shape (the offset is a captured constant, not word-scaled).
Three qualifications keep the presentation honest, all measured against putty
(user-space C, 139 real uses, DWARF offsetof as the oracle):
One call per idiom. No compiler keeps the adjusted pointer around: it folds the subtraction into every field displacement that reads through it, so one source
container_ofreached the matcher as 3.4 separate matches on the same base, each naming a record base wrong by that field’s offset (83.5% of the non-genuine matches were explained this way, against 13.0% for random offsets).collapse_capturekeeps one match per base pointer; the largest offset is the right one to keep, since a field at member offsetmof the record appears asp - (off - m)and so can only shrink the constant. Measured: this reports the truecontainer_ofoffset for 80.1% (gcc) / 86.7% (clang) of base pointers, against 29% for the per-field matches.An offset floor.
p - 1is a decrement, not a record base; offsets below one machine word carry no record. 29 of the 85 distinct offsets emitted in container_of-free code were exactly 1.
Rejected: requiring the result to be dereferenced. The captured base is a
bare virtual variable of unknown type, so a decrement matches as happily as a
record base, and demanding that the adjusted pointer sit inside a memory address
looks like the way to tell them apart. It selects the wrong cases. The folded
form is a field load and passes; the unfolded form, the one that survives
because the code does something with the record other than read one field of it,
which is the case the pattern gets right, does not, since the pointer is handed
to a callee or stored. Measured on putty/gcc it dropped function-level recall
from 90.7% to 65.0% and site-level recall from 42.7% to 2.1%, and it also
rejects the sum_list fixture below. The offset floor already removes the
decrements it was aimed at.