• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Demangler for the Rust programming language
2    Copyright (C) 2016-2017 Free Software Foundation, Inc.
3    Written by David Tolnay (dtolnay@gmail.com).
4 
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10 
11 In addition to the permissions in the GNU Library General Public
12 License, the Free Software Foundation gives you unlimited permission
13 to link the compiled version of this file into combinations with other
14 programs, and to distribute those combinations without any restriction
15 coming from the use of this file.  (The Library Public License
16 restrictions do apply in other respects; for example, they cover
17 modification of the file, and distribution when not linked into a
18 combined executable.)
19 
20 Libiberty is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 Library General Public License for more details.
24 
25 You should have received a copy of the GNU Library General Public
26 License along with libiberty; see the file COPYING.LIB.
27 If not, see <http://www.gnu.org/licenses/>.  */
28 
29 
30 #if 0 /* in valgrind */
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 #endif /* ! in valgrind */
35 
36 #if 0 /* in valgrind */
37 #include "safe-ctype.h"
38 #endif /* ! in valgrind */
39 
40 #if 0 /* in valgrind */
41 #include <sys/types.h>
42 #include <string.h>
43 #include <stdio.h>
44 #endif /* ! in valgrind */
45 
46 #if 0 /* in valgrind */
47 #ifdef HAVE_STRING_H
48 #include <string.h>
49 #else
50 extern size_t strlen(const char *s);
51 extern int strncmp(const char *s1, const char *s2, size_t n);
52 extern void *memset(void *s, int c, size_t n);
53 #endif
54 #endif /* ! in valgrind */
55 
56 #if 0 /* in valgrind */
57 #include <demangle.h>
58 #include "libiberty.h"
59 #endif /* ! in valgrind */
60 
61 #include "vg_libciface.h"
62 
63 #include "ansidecl.h"
64 #include "demangle.h"
65 #include "safe-ctype.h"
66 
67 /* Mangled Rust symbols look like this:
68      _$LT$std..sys..fd..FileDesc$u20$as$u20$core..ops..Drop$GT$::drop::hc68340e1baa4987a
69 
70    The original symbol is:
71      <std::sys::fd::FileDesc as core::ops::Drop>::drop
72 
73    The last component of the path is a 64-bit hash in lowercase hex,
74    prefixed with "h". Rust does not have a global namespace between
75    crates, an illusion which Rust maintains by using the hash to
76    distinguish things that would otherwise have the same symbol.
77 
78    Any path component not starting with a XID_Start character is
79    prefixed with "_".
80 
81    The following escape sequences are used:
82 
83    ","  =>  $C$
84    "@"  =>  $SP$
85    "*"  =>  $BP$
86    "&"  =>  $RF$
87    "<"  =>  $LT$
88    ">"  =>  $GT$
89    "("  =>  $LP$
90    ")"  =>  $RP$
91    " "  =>  $u20$
92    "\"" =>  $u22$
93    "'"  =>  $u27$
94    "+"  =>  $u2b$
95    ";"  =>  $u3b$
96    "["  =>  $u5b$
97    "]"  =>  $u5d$
98    "{"  =>  $u7b$
99    "}"  =>  $u7d$
100    "~"  =>  $u7e$
101 
102    A double ".." means "::" and a single "." means "-".
103 
104    The only characters allowed in the mangled symbol are a-zA-Z0-9 and _.:$  */
105 
106 static const char *hash_prefix = "::h";
107 static const size_t hash_prefix_len = 3;
108 static const size_t hash_len = 16;
109 
110 static int is_prefixed_hash (const char *start);
111 static int looks_like_rust (const char *sym, size_t len);
112 static int unescape (const char **in, char **out, const char *seq, char value);
113 
114 /* INPUT: sym: symbol that has been through C++ (gnu v3) demangling
115 
116    This function looks for the following indicators:
117 
118    1. The hash must consist of "h" followed by 16 lowercase hex digits.
119 
120    2. As a sanity check, the hash must use between 5 and 15 of the 16
121       possible hex digits. This is true of 99.9998% of hashes so once
122       in your life you may see a false negative. The point is to
123       notice path components that could be Rust hashes but are
124       probably not, like "haaaaaaaaaaaaaaaa". In this case a false
125       positive (non-Rust symbol has an important path component
126       removed because it looks like a Rust hash) is worse than a false
127       negative (the rare Rust symbol is not demangled) so this sets
128       the balance in favor of false negatives.
129 
130    3. There must be no characters other than a-zA-Z0-9 and _.:$
131 
132    4. There must be no unrecognized $-sign sequences.
133 
134    5. There must be no sequence of three or more dots in a row ("...").  */
135 
136 int
rust_is_mangled(const char * sym)137 rust_is_mangled (const char *sym)
138 {
139   size_t len, len_without_hash;
140 
141   if (!sym)
142     return 0;
143 
144   len = strlen (sym);
145   if (len <= hash_prefix_len + hash_len)
146     /* Not long enough to contain "::h" + hash + something else */
147     return 0;
148 
149   len_without_hash = len - (hash_prefix_len + hash_len);
150   if (!is_prefixed_hash (sym + len_without_hash))
151     return 0;
152 
153   return looks_like_rust (sym, len_without_hash);
154 }
155 
156 /* A hash is the prefix "::h" followed by 16 lowercase hex digits. The
157    hex digits must comprise between 5 and 15 (inclusive) distinct
158    digits.  */
159 
160 static int
is_prefixed_hash(const char * str)161 is_prefixed_hash (const char *str)
162 {
163   const char *end;
164   char seen[16];
165   size_t i;
166   int count;
167 
168   if (strncmp (str, hash_prefix, hash_prefix_len))
169     return 0;
170   str += hash_prefix_len;
171 
172   memset (seen, 0, sizeof(seen));
173   for (end = str + hash_len; str < end; str++)
174     if (*str >= '0' && *str <= '9')
175       seen[*str - '0'] = 1;
176     else if (*str >= 'a' && *str <= 'f')
177       seen[*str - 'a' + 10] = 1;
178     else
179       return 0;
180 
181   /* Count how many distinct digits seen */
182   count = 0;
183   for (i = 0; i < 16; i++)
184     if (seen[i])
185       count++;
186 
187   return count >= 5 && count <= 15;
188 }
189 
190 static int
looks_like_rust(const char * str,size_t len)191 looks_like_rust (const char *str, size_t len)
192 {
193   const char *end = str + len;
194 
195   while (str < end)
196     switch (*str)
197       {
198       case '$':
199 	if (!strncmp (str, "$C$", 3))
200 	  str += 3;
201 	else if (!strncmp (str, "$SP$", 4)
202 		 || !strncmp (str, "$BP$", 4)
203 		 || !strncmp (str, "$RF$", 4)
204 		 || !strncmp (str, "$LT$", 4)
205 		 || !strncmp (str, "$GT$", 4)
206 		 || !strncmp (str, "$LP$", 4)
207 		 || !strncmp (str, "$RP$", 4))
208 	  str += 4;
209 	else if (!strncmp (str, "$u20$", 5)
210 		 || !strncmp (str, "$u22$", 5)
211 		 || !strncmp (str, "$u27$", 5)
212 		 || !strncmp (str, "$u2b$", 5)
213 		 || !strncmp (str, "$u3b$", 5)
214 		 || !strncmp (str, "$u5b$", 5)
215 		 || !strncmp (str, "$u5d$", 5)
216 		 || !strncmp (str, "$u7b$", 5)
217 		 || !strncmp (str, "$u7d$", 5)
218 		 || !strncmp (str, "$u7e$", 5))
219 	  str += 5;
220 	else
221 	  return 0;
222 	break;
223       case '.':
224 	/* Do not allow three or more consecutive dots */
225 	if (!strncmp (str, "...", 3))
226 	  return 0;
227 	/* Fall through */
228       case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
229       case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
230       case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
231       case 's': case 't': case 'u': case 'v': case 'w': case 'x':
232       case 'y': case 'z':
233       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
234       case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
235       case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
236       case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
237       case 'Y': case 'Z':
238       case '0': case '1': case '2': case '3': case '4': case '5':
239       case '6': case '7': case '8': case '9':
240       case '_':
241       case ':':
242 	str++;
243 	break;
244       default:
245 	return 0;
246       }
247 
248   return 1;
249 }
250 
251 /*
252   INPUT: sym: symbol for which rust_is_mangled(sym) returned 1.
253 
254   The input is demangled in-place because the mangled name is always
255   longer than the demangled one.  */
256 
257 void
rust_demangle_sym(char * sym)258 rust_demangle_sym (char *sym)
259 {
260   const char *in;
261   char *out;
262   const char *end;
263 
264   if (!sym)
265     return;
266 
267   in = sym;
268   out = sym;
269   end = sym + strlen (sym) - (hash_prefix_len + hash_len);
270 
271   while (in < end)
272     switch (*in)
273       {
274       case '$':
275 	if (!(unescape (&in, &out, "$C$", ',')
276 	      || unescape (&in, &out, "$SP$", '@')
277 	      || unescape (&in, &out, "$BP$", '*')
278 	      || unescape (&in, &out, "$RF$", '&')
279 	      || unescape (&in, &out, "$LT$", '<')
280 	      || unescape (&in, &out, "$GT$", '>')
281 	      || unescape (&in, &out, "$LP$", '(')
282 	      || unescape (&in, &out, "$RP$", ')')
283 	      || unescape (&in, &out, "$u20$", ' ')
284 	      || unescape (&in, &out, "$u22$", '\"')
285 	      || unescape (&in, &out, "$u27$", '\'')
286 	      || unescape (&in, &out, "$u2b$", '+')
287 	      || unescape (&in, &out, "$u3b$", ';')
288 	      || unescape (&in, &out, "$u5b$", '[')
289 	      || unescape (&in, &out, "$u5d$", ']')
290 	      || unescape (&in, &out, "$u7b$", '{')
291 	      || unescape (&in, &out, "$u7d$", '}')
292 	      || unescape (&in, &out, "$u7e$", '~'))) {
293 	  /* unexpected escape sequence, not looks_like_rust. */
294 	  goto fail;
295 	}
296 	break;
297       case '_':
298 	/* If this is the start of a path component and the next
299 	   character is an escape sequence, ignore the underscore. The
300 	   mangler inserts an underscore to make sure the path
301 	   component begins with a XID_Start character. */
302 	if ((in == sym || in[-1] == ':') && in[1] == '$')
303 	  in++;
304 	else
305 	  *out++ = *in++;
306 	break;
307       case '.':
308 	if (in[1] == '.')
309 	  {
310 	    /* ".." becomes "::" */
311 	    *out++ = ':';
312 	    *out++ = ':';
313 	    in += 2;
314 	  }
315 	else
316 	  {
317 	    /* "." becomes "-" */
318 	    *out++ = '-';
319 	    in++;
320 	  }
321 	break;
322       case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
323       case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
324       case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
325       case 's': case 't': case 'u': case 'v': case 'w': case 'x':
326       case 'y': case 'z':
327       case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
328       case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
329       case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
330       case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
331       case 'Y': case 'Z':
332       case '0': case '1': case '2': case '3': case '4': case '5':
333       case '6': case '7': case '8': case '9':
334       case ':':
335 	*out++ = *in++;
336 	break;
337       default:
338 	/* unexpected character in symbol, not looks_like_rust.  */
339 	goto fail;
340       }
341   goto done;
342 
343 fail:
344   *out++ = '?'; /* This is pretty lame, but it's hard to do better. */
345 done:
346   *out = '\0';
347 }
348 
349 static int
unescape(const char ** in,char ** out,const char * seq,char value)350 unescape (const char **in, char **out, const char *seq, char value)
351 {
352   size_t len = strlen (seq);
353 
354   if (strncmp (*in, seq, len))
355     return 0;
356 
357   **out = value;
358 
359   *in += len;
360   *out += 1;
361 
362   return 1;
363 }
364