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