angr.analyses.decompiler.known_patterns.stl_accessors2

C++ STL accessors, round two: std::string capacity/back/front and std::vector<T>::back().

std::string::capacity() is the only control-flow idiom here. libstdc++ lays std::string out as { _M_p , _M_string_length , union { _M_local_buf[16] | _M_allocated_capacity } }, so capacity() is the SSO select:

_M_p == &_M_local_buf ? 15 : _M_allocated_capacity

i.e. a diamond. Unlike the MSVC c_str() triangle (see std_string_cstr), both arms of this diamond assign a value, so ITERegionConverter, which runs at AFTER_GLOBAL_SIMPLIFICATION, before the KnownPatternOutliner at BEFORE_VARIABLE_RECOVERY, has already folded the region into a single x = c ? a : b assignment by the time patterns are matched. The idiom is therefore a PITE expression pattern, not a PGraphPat. (When gcc’s tail duplication copies the join into both arms instead of merging them, no ITE and no single-exit region exists and the idiom is not matched; that is intended, since there is nothing to outline.)

The comparison Load(s) == s + 16, a loaded pointer tested against the object’s own address plus the union offset, is what makes the pattern self-guarding; the SSO threshold 15 is fixed for char strings, and the union offset is word-scaled, so one definition covers 32- and 64-bit.

back()/front() are plain expression idioms: *(_M_p + size - 1) and *_M_p. std::vector<T>::back() is *(_M_finish - sizeof(T)).

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

angr.analyses.decompiler.known_patterns.stl_accessors2.make_std_vector_back_template(elt_name, elt_size, returnty)

A std::vector<elt_name>::back template: *(_M_finish - sizeof(T)).

Parameters:
  • elt_name (str)

  • elt_size (int)

  • returnty (str)