This is an archive of the discontinued LLVM Phabricator instance.

sanitizers: call __cxa_demangle only on mangled external names
Needs ReviewPublic

Authored by emaste on May 2 2016, 11:41 AM.

Details

Reviewers
samsonov
eugenis
Summary

Explicitly check for mangled external names (starting with "_Z") as __cxa_demangle also handles type mangling, which conflicts with unmangled names such as "f" or "m".

For reference D2552 is the same issue in lldb. See also the discussion at https://lists.freebsd.org/pipermail/freebsd-toolchain/2014-June/001153.html.

Diff Detail

Event Timeline

emaste updated this revision to Diff 55855.May 2 2016, 11:41 AM
emaste retitled this revision from to sanitizers: call __cxa_demangle only on mangled external names.
emaste updated this object.
emaste added reviewers: samsonov, eugenis.
emaste set the repository for this revision to rL LLVM.
emaste added a subscriber: dim.
eugenis edited edge metadata.May 2 2016, 12:02 PM

Will this work on Mac?
I think they add leading underscores, but it's only for C symbols, or is it not?

Will this work on Mac?
I think they add leading underscores, but it's only for C symbols, or is it not?

Hrm. Indeed, on OS X nm reports __Z2fni as the mangled name for fn(int). In the case of lldb the extra leading _ must be stripped off earlier.

Can we return nullptr in case 'name' is nullptr? __cxa_demangle does that and this removes this behavior.

emaste updated this revision to Diff 55878.May 2 2016, 1:07 PM
emaste edited edge metadata.
emaste removed rL LLVM as the repository for this revision.

Restore handling of name = nullptr.

filcab added a subscriber: filcab.May 2 2016, 1:08 PM

Can you create a C test with a function called f, i, m, whatever that
triggered the demangler and doesn't trigger anymore?

Thank you,

Filipe
samsonov edited edge metadata.May 2 2016, 1:09 PM

I am not sure, but IIRC somewhere we depend on the ability of __cxa_demangle to demangle the type names.

That is, even if we want to add this check, it should be closer to the call site, where we know for sure we're demangling the variable name.

AFAICT, the only place we might be expecting something that doesn't
start with _Z to be demangled is in lib/ubsan/ubsan_diag.cc:188
But I'm not sure we're calling it in that way.

Ed: I guess the best thing would be to either:

  • Audit usages of Demangle (and its users :-) ), and see if we

actually use it for demangling type names.

  • If we use it only once or twice for that, maybe another function

for those cases would work

  • Or just do what Alexey asked an check before you call Demangle.

Thank you,

Filipe