proxygen
Demangle.cpp
Go to the documentation of this file.
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 
17 #include <folly/Demangle.h>
18 
19 #include <algorithm>
20 #include <cstring>
21 
22 #include <folly/detail/Demangle.h>
24 
25 #if FOLLY_DETAIL_HAVE_DEMANGLE_H
26 
27 #include <cxxabi.h>
28 
29 #endif
30 
31 namespace folly {
32 
33 #if FOLLY_DETAIL_HAVE_DEMANGLE_H
34 
35 fbstring demangle(const char* name) {
36 #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
37  // GCC's __cxa_demangle() uses on-stack data structures for the
38  // parser state which are linear in the number of components of the
39  // symbol. For extremely long symbols, this can cause a stack
40  // overflow. We set an arbitrary symbol length limit above which we
41  // just return the mangled name.
42  size_t mangledLen = strlen(name);
43  if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
44  return fbstring(name, mangledLen);
45  }
46 #endif
47 
48  int status;
49  size_t len = 0;
50  // malloc() memory for the demangled type name
51  char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
52  if (status != 0) {
53  return name;
54  }
55  // len is the length of the buffer (including NUL terminator and maybe
56  // other junk)
57  return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
58 }
59 
60 namespace {
61 
62 struct DemangleBuf {
63  char* dest;
64  size_t remaining;
65  size_t total;
66 };
67 
68 void demangleCallback(const char* str, size_t size, void* p) {
69  DemangleBuf* buf = static_cast<DemangleBuf*>(p);
70  size_t n = std::min(buf->remaining, size);
71  memcpy(buf->dest, str, n);
72  buf->dest += n;
73  buf->remaining -= n;
74  buf->total += size;
75 }
76 
77 } // namespace
78 
79 size_t demangle(const char* name, char* out, size_t outSize) {
80 #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
81  size_t mangledLen = strlen(name);
82  if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
83  if (outSize) {
84  size_t n = std::min(mangledLen, outSize - 1);
85  memcpy(out, name, n);
86  out[n] = '\0';
87  }
88  return mangledLen;
89  }
90 #endif
91 
92  DemangleBuf dbuf;
93  dbuf.dest = out;
94  dbuf.remaining = outSize ? outSize - 1 : 0; // leave room for null term
95  dbuf.total = 0;
96 
97  // Unlike most library functions, this returns 1 on success and 0 on failure
98  int status =
99  detail::cplus_demangle_v3_callback_wrapper(name, demangleCallback, &dbuf);
100  if (status == 0) { // failed, return original
101  return folly::strlcpy(out, name, outSize);
102  }
103  if (outSize != 0) {
104  *dbuf.dest = '\0';
105  }
106  return dbuf.total;
107 }
108 
109 #else
110 
111 fbstring demangle(const char* name) {
112  return name;
113 }
114 
115 size_t demangle(const char* name, char* out, size_t outSize) {
116  return folly::strlcpy(out, name, outSize);
117 }
118 
119 #endif
120 
121 size_t strlcpy(char* dest, const char* const src, size_t size) {
122  size_t len = strlen(src);
123  if (size != 0) {
124  size_t n = std::min(len, size - 1); // always null terminate!
125  memcpy(dest, src, n);
126  dest[n] = '\0';
127  }
128  return len;
129 }
130 
131 } // namespace folly
dest
Definition: upload.py:394
#define FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
Definition: folly-config.h:67
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
AcquireMallocatedString
Definition: FBString.h:221
int cplus_demangle_v3_callback_wrapper(char const *const mangled, void(*const cbref)(char const *, std::size_t, void *), void *const opaque)
Definition: Demangle.cpp:32
const char * name
Definition: http_parser.c:437
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
LogLevel min
Definition: LogLevel.cpp:30
size_t strlcpy(char *dest, const char *const src, size_t size)
Definition: Demangle.cpp:121
basic_fbstring< char > fbstring
Definition: FBString.h:2904
fbstring demangle(const char *name)
Definition: Demangle.cpp:111