angr.analyses.decompiler.known_patterns.std_string_cstrΒΆ
Inlined MSVC std::string::c_str() / data(): the small-string
optimization (SSO) select.
The MSVC STL stores the character buffer in a 16-byte union _Bx at offset 0
(either an inline _Buf[16] or, for long strings, a heap _Ptr), the size
at offset 16, and the capacity _Myres one word further on. c_str()
returns the inline buffer address when the string is short and the heap pointer
when it is long, i.e.:
result = &s->_Bx; // == s (the buffer address)
if (s->_Myres >= 16) // capacity past the SSO threshold?
result = s->_Bx._Ptr; // == *(void**)s (the heap pointer)
which the compiler lowers to a control-flow diamond. In clean SSA it is:
entry: if (Load(s + cap_off) < 16) goto merge else goto heap
heap: h = Load(s)
merge: result = Phi(h [heap], s [entry]) // Phi(*s, s)
so it is a PGraphPat (entry + heap; the phi merge is the frontier)
rather than an expression idiom. This matches the SSA form before de-phi, the
form the KnownPatternOutliner pass sees when it runs at BEFORE_VARIABLE_RECOVERY,
so the idiom is recognized and outlined during decompilation. (SSA
destruction later inserts res = s / h2 = h copies that would obscure this
shape; matching the clean form avoids depending on them and needs no cross-copy
unification.) The offsets are word-scaled (cap_off is 24 on x64, 20 on x86); the
16-byte SSO threshold is fixed for char strings.
libstdc++ c_str() is a bare _M_p load (no SSO branch) and is not matched
here. Opt-in, because a control-flow select is fairly generic. Calibrated
against
tests/x86_64/windows/known_patterns_msvc_string_cstr.exe (MSVC 19 /O2).