Demangle.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Demangle.h>
  17. #include <algorithm>
  18. #include <cstring>
  19. #include <folly/detail/Demangle.h>
  20. #include <folly/portability/Config.h>
  21. #if FOLLY_DETAIL_HAVE_DEMANGLE_H
  22. #include <cxxabi.h>
  23. #endif
  24. namespace folly {
  25. #if FOLLY_DETAIL_HAVE_DEMANGLE_H
  26. fbstring demangle(const char* name) {
  27. #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
  28. // GCC's __cxa_demangle() uses on-stack data structures for the
  29. // parser state which are linear in the number of components of the
  30. // symbol. For extremely long symbols, this can cause a stack
  31. // overflow. We set an arbitrary symbol length limit above which we
  32. // just return the mangled name.
  33. size_t mangledLen = strlen(name);
  34. if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
  35. return fbstring(name, mangledLen);
  36. }
  37. #endif
  38. int status;
  39. size_t len = 0;
  40. // malloc() memory for the demangled type name
  41. char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
  42. if (status != 0) {
  43. return name;
  44. }
  45. // len is the length of the buffer (including NUL terminator and maybe
  46. // other junk)
  47. return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
  48. }
  49. namespace {
  50. struct DemangleBuf {
  51. char* dest;
  52. size_t remaining;
  53. size_t total;
  54. };
  55. void demangleCallback(const char* str, size_t size, void* p) {
  56. DemangleBuf* buf = static_cast<DemangleBuf*>(p);
  57. size_t n = std::min(buf->remaining, size);
  58. memcpy(buf->dest, str, n);
  59. buf->dest += n;
  60. buf->remaining -= n;
  61. buf->total += size;
  62. }
  63. } // namespace
  64. size_t demangle(const char* name, char* out, size_t outSize) {
  65. #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
  66. size_t mangledLen = strlen(name);
  67. if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
  68. if (outSize) {
  69. size_t n = std::min(mangledLen, outSize - 1);
  70. memcpy(out, name, n);
  71. out[n] = '\0';
  72. }
  73. return mangledLen;
  74. }
  75. #endif
  76. DemangleBuf dbuf;
  77. dbuf.dest = out;
  78. dbuf.remaining = outSize ? outSize - 1 : 0; // leave room for null term
  79. dbuf.total = 0;
  80. // Unlike most library functions, this returns 1 on success and 0 on failure
  81. int status =
  82. detail::cplus_demangle_v3_callback_wrapper(name, demangleCallback, &dbuf);
  83. if (status == 0) { // failed, return original
  84. return folly::strlcpy(out, name, outSize);
  85. }
  86. if (outSize != 0) {
  87. *dbuf.dest = '\0';
  88. }
  89. return dbuf.total;
  90. }
  91. #else
  92. fbstring demangle(const char* name) {
  93. return name;
  94. }
  95. size_t demangle(const char* name, char* out, size_t outSize) {
  96. return folly::strlcpy(out, name, outSize);
  97. }
  98. #endif
  99. size_t strlcpy(char* dest, const char* const src, size_t size) {
  100. size_t len = strlen(src);
  101. if (size != 0) {
  102. size_t n = std::min(len, size - 1); // always null terminate!
  103. memcpy(dest, src, n);
  104. dest[n] = '\0';
  105. }
  106. return len;
  107. }
  108. } // namespace folly