Adds writer test case for RCU
[folly.git] / folly / Demangle.cpp
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/portability/Config.h>
23
24 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
25 #include <cxxabi.h>
26
27 // From libiberty
28 //
29 // __attribute__((__weak__)) doesn't work, because cplus_demangle_v3_callback
30 // is exported by an object file in libiberty.a, and the ELF spec says
31 // "The link editor does not extract archive members to resolve undefined weak
32 // symbols" (but, interestingly enough, will resolve undefined weak symbols
33 // with definitions from archive members that were extracted in order to
34 // resolve an undefined global (strong) symbol)
35
36 # ifndef DMGL_NO_OPTS
37 #  define FOLLY_DEFINED_DMGL 1
38 #  define DMGL_NO_OPTS    0          /* For readability... */
39 #  define DMGL_PARAMS     (1 << 0)   /* Include function args */
40 #  define DMGL_ANSI       (1 << 1)   /* Include const, volatile, etc */
41 #  define DMGL_JAVA       (1 << 2)   /* Demangle as Java rather than C++. */
42 #  define DMGL_VERBOSE    (1 << 3)   /* Include implementation details.  */
43 #  define DMGL_TYPES      (1 << 4)   /* Also try to demangle type encodings.  */
44 #  define DMGL_RET_POSTFIX (1 << 5)  /* Print function return types (when
45                                         present) after function signature */
46 # endif
47
48 extern "C" int cplus_demangle_v3_callback(
49     const char* mangled,
50     int options,  // We use DMGL_PARAMS | DMGL_TYPES, aka 0x11
51     void (*callback)(const char*, size_t, void*),
52     void* arg);
53
54 #endif
55
56 namespace folly {
57
58 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
59
60 fbstring demangle(const char* name) {
61 #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
62   // GCC's __cxa_demangle() uses on-stack data structures for the
63   // parser state which are linear in the number of components of the
64   // symbol. For extremely long symbols, this can cause a stack
65   // overflow. We set an arbitrary symbol length limit above which we
66   // just return the mangled name.
67   size_t mangledLen = strlen(name);
68   if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
69     return fbstring(name, mangledLen);
70   }
71 #endif
72
73   int status;
74   size_t len = 0;
75   // malloc() memory for the demangled type name
76   char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
77   if (status != 0) {
78     return name;
79   }
80   // len is the length of the buffer (including NUL terminator and maybe
81   // other junk)
82   return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
83 }
84
85 namespace {
86
87 struct DemangleBuf {
88   char* dest;
89   size_t remaining;
90   size_t total;
91 };
92
93 void demangleCallback(const char* str, size_t size, void* p) {
94   DemangleBuf* buf = static_cast<DemangleBuf*>(p);
95   size_t n = std::min(buf->remaining, size);
96   memcpy(buf->dest, str, n);
97   buf->dest += n;
98   buf->remaining -= n;
99   buf->total += size;
100 }
101
102 } // namespace
103
104 size_t demangle(const char* name, char* out, size_t outSize) {
105 #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
106   size_t mangledLen = strlen(name);
107   if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
108     if (outSize) {
109       size_t n = std::min(mangledLen, outSize - 1);
110       memcpy(out, name, n);
111       out[n] = '\0';
112     }
113     return mangledLen;
114   }
115 #endif
116
117   DemangleBuf dbuf;
118   dbuf.dest = out;
119   dbuf.remaining = outSize ? outSize - 1 : 0;   // leave room for null term
120   dbuf.total = 0;
121
122   // Unlike most library functions, this returns 1 on success and 0 on failure
123   int status = cplus_demangle_v3_callback(
124       name,
125       DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES,
126       demangleCallback,
127       &dbuf);
128   if (status == 0) {  // failed, return original
129     return folly::strlcpy(out, name, outSize);
130   }
131   if (outSize != 0) {
132     *dbuf.dest = '\0';
133   }
134   return dbuf.total;
135 }
136
137 #else
138
139 fbstring demangle(const char* name) {
140   return name;
141 }
142
143 size_t demangle(const char* name, char* out, size_t outSize) {
144   return folly::strlcpy(out, name, outSize);
145 }
146
147 #endif
148
149 size_t strlcpy(char* dest, const char* const src, size_t size) {
150   size_t len = strlen(src);
151   if (size != 0) {
152     size_t n = std::min(len, size - 1);  // always null terminate!
153     memcpy(dest, src, n);
154     dest[n] = '\0';
155   }
156   return len;
157 }
158
159 } // namespace folly