angr.analyses.decompiler.known_patterns.std_string_dtor

The inlined std::string destructor.

~basic_string() is one line of source and, at -O2, five lines of machine code that the compiler emits at every scope exit of every local string and in every destructor of every class holding one. It is by a wide margin the most common inlined idiom in C++ binaries: the DWARF inline records of the -O2 benchmark corpus put ~basic_string / _M_dispose / _M_destroy at 5.9M sites across 245 binaries, an order of magnitude above any accessor.

libstdc++ frees the buffer only when the string is not using its small-string buffer, which is exactly the SSO test capacity() also uses:

if (_M_p != &_M_local_buf)
    operator delete(_M_p, _M_allocated_capacity + 1);

a triangle whose taken arm is a single call. Four things have to agree for a match: the same object address in the compare and in both loads, the local buffer’s offset appearing both as an address and as a field, the + 1, and the callee being operator delete. That is what makes this pattern self-guarding enough to be on by default. The + 1 is the NUL terminator: libstdc++ allocated capacity + 1 bytes and hands the same number back to the sized operator delete.

Sized deallocation is a C++14 feature and a compiler switch, so the one-argument operator delete(void *) spelling gets its own template; the two cannot share one pattern object because a statement pattern has no PChoice.

Calibrated against tests/x86_64/decompiler/known_patterns_stl5 (g++ 12.2.0 -O2).

angr.analyses.decompiler.known_patterns.std_string_dtor.OPERATOR_DELETE = frozenset({'??3@YAXPAX@Z', '??3@YAXPAXI@Z', '??3@YAXPEAX@Z', '??3@YAXPEAX_K@Z', '_ZdlPv', '_ZdlPvSt11align_val_t', '_ZdlPvj', '_ZdlPvjSt11align_val_t', '_ZdlPvm', '_ZdlPvmSt11align_val_t', 'operator delete(void*)', 'operator delete(void*, unsigned int)', 'operator delete(void*, unsigned long)'})

Every spelling of operator delete a compiler emits for this idiom. The mangled names are exact; the demangled ones are accepted too because PCall matches against both and a stripped-but-imported callee may only be known by one of them.