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