Skip to content

Commit fcc8bb4

Browse files
committed
std: Use rustc_demangle from crates.io
No more need to duplicate the demangling routine between crates.io and the standard library, we can use the exact same one!
1 parent 1897657 commit fcc8bb4

File tree

5 files changed

+27
-236
lines changed

5 files changed

+27
-236
lines changed

Cargo.lock

+13-8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ dependencies = [
8787
"backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)",
8888
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
8989
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
90-
"rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
90+
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
9191
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
9292
]
9393

@@ -1813,7 +1813,7 @@ name = "rand_chacha"
18131813
version = "0.1.0"
18141814
source = "registry+https://github.com/rust-lang/crates.io-index"
18151815
dependencies = [
1816-
"rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
1816+
"rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
18171817
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
18181818
]
18191819

@@ -1835,7 +1835,7 @@ name = "rand_hc"
18351835
version = "0.1.0"
18361836
source = "registry+https://github.com/rust-lang/crates.io-index"
18371837
dependencies = [
1838-
"rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
1838+
"rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
18391839
]
18401840

18411841
[[package]]
@@ -1860,7 +1860,7 @@ name = "rand_xorshift"
18601860
version = "0.1.0"
18611861
source = "registry+https://github.com/rust-lang/crates.io-index"
18621862
dependencies = [
1863-
"rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
1863+
"rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
18641864
]
18651865

18661866
[[package]]
@@ -2195,8 +2195,12 @@ dependencies = [
21952195

21962196
[[package]]
21972197
name = "rustc-demangle"
2198-
version = "0.1.9"
2198+
version = "0.1.10"
21992199
source = "registry+https://github.com/rust-lang/crates.io-index"
2200+
dependencies = [
2201+
"compiler_builtins 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
2202+
"rustc-std-workspace-core 1.0.0",
2203+
]
22002204

22012205
[[package]]
22022206
name = "rustc-hash"
@@ -2315,7 +2319,7 @@ dependencies = [
23152319
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
23162320
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
23172321
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
2318-
"rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
2322+
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
23192323
"rustc_llvm 0.0.0",
23202324
]
23212325

@@ -2331,7 +2335,7 @@ dependencies = [
23312335
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
23322336
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
23332337
"rustc 0.0.0",
2334-
"rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
2338+
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
23352339
"rustc_allocator 0.0.0",
23362340
"rustc_apfloat 0.0.0",
23372341
"rustc_codegen_utils 0.0.0",
@@ -2892,6 +2896,7 @@ dependencies = [
28922896
"panic_unwind 0.0.0",
28932897
"profiler_builtins 0.0.0",
28942898
"rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
2899+
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
28952900
"rustc_asan 0.0.0",
28962901
"rustc_lsan 0.0.0",
28972902
"rustc_msan 0.0.0",
@@ -3578,7 +3583,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
35783583
"checksum rustc-ap-serialize 306.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b2c0e8161e956647592a737074736e6ce05ea36b70c770ea8cca3eb9cb33737"
35793584
"checksum rustc-ap-syntax 306.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1adc189e5e4500a4167b9afa04e67067f40d0039e0e05870c977bebb561f065a"
35803585
"checksum rustc-ap-syntax_pos 306.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4d42c430dbb0be4377bfe6aa5099074c63ac8796b24098562c2e2154aecc5652"
3581-
"checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395"
3586+
"checksum rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "82ae957aa1b3055d8e086486723c0ccd3d7b8fa190ae8fa2e35543b6171c810e"
35823587
"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"
35833588
"checksum rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c6d5a683c6ba4ed37959097e88d71c9e8e26659a3cb5be8b389078e7ad45306"
35843589
"checksum rustc-rayon-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40f06724db71e18d68b3b946fdf890ca8c921d9edccc1404fdfdb537b0d12649"

src/libstd/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ libc = { version = "0.2.44", default-features = false, features = ['rustc-dep-of
2121
compiler_builtins = { version = "0.1.1" }
2222
profiler_builtins = { path = "../libprofiler_builtins", optional = true }
2323
unwind = { path = "../libunwind" }
24+
rustc-demangle = { version = "0.1.10", features = ['rustc-dep-of-std'] }
2425

2526
[dev-dependencies]
2627
rand = "0.6.1"

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ pub use core::{unreachable, unimplemented, write, writeln, try};
339339
extern crate alloc as alloc_crate;
340340
#[doc(masked)]
341341
extern crate libc;
342+
extern crate rustc_demangle;
342343

343344
// We always need an unwinder currently for backtraces
344345
#[doc(masked)]

src/libstd/sys_common/backtrace.rs

+11-228
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
use env;
1515
use io::prelude::*;
1616
use io;
17+
use path::{self, Path};
18+
use ptr;
19+
use rustc_demangle::demangle;
1720
use str;
1821
use sync::atomic::{self, Ordering};
19-
use path::{self, Path};
2022
use sys::mutex::Mutex;
21-
use ptr;
2223

2324
pub use sys::backtrace::{
2425
unwind_backtrace,
@@ -191,7 +192,14 @@ fn output(w: &mut dyn Write, idx: usize, frame: Frame,
191192
PrintFormat::Short => write!(w, " {:2}: ", idx)?,
192193
}
193194
match s {
194-
Some(string) => demangle(w, string, format)?,
195+
Some(string) => {
196+
let symbol = demangle(string);
197+
match format {
198+
PrintFormat::Full => write!(w, "{}", symbol)?,
199+
// strip the trailing hash if short mode
200+
PrintFormat::Short => write!(w, "{:#}", symbol)?,
201+
}
202+
}
195203
None => w.write_all(b"<unknown>")?,
196204
}
197205
w.write_all(b"\n")
@@ -235,228 +243,3 @@ fn output_fileline(w: &mut dyn Write,
235243
w.write_all(b"\n")
236244
}
237245

238-
239-
// All rust symbols are in theory lists of "::"-separated identifiers. Some
240-
// assemblers, however, can't handle these characters in symbol names. To get
241-
// around this, we use C++-style mangling. The mangling method is:
242-
//
243-
// 1. Prefix the symbol with "_ZN"
244-
// 2. For each element of the path, emit the length plus the element
245-
// 3. End the path with "E"
246-
//
247-
// For example, "_ZN4testE" => "test" and "_ZN3foo3barE" => "foo::bar".
248-
//
249-
// We're the ones printing our backtraces, so we can't rely on anything else to
250-
// demangle our symbols. It's *much* nicer to look at demangled symbols, so
251-
// this function is implemented to give us nice pretty output.
252-
//
253-
// Note that this demangler isn't quite as fancy as it could be. We have lots
254-
// of other information in our symbols like hashes, version, type information,
255-
// etc. Additionally, this doesn't handle glue symbols at all.
256-
pub fn demangle(writer: &mut dyn Write, mut s: &str, format: PrintFormat) -> io::Result<()> {
257-
// During ThinLTO LLVM may import and rename internal symbols, so strip out
258-
// those endings first as they're one of the last manglings applied to
259-
// symbol names.
260-
let llvm = ".llvm.";
261-
if let Some(i) = s.find(llvm) {
262-
let candidate = &s[i + llvm.len()..];
263-
let all_hex = candidate.chars().all(|c| {
264-
match c {
265-
'A' ..= 'F' | '0' ..= '9' => true,
266-
_ => false,
267-
}
268-
});
269-
270-
if all_hex {
271-
s = &s[..i];
272-
}
273-
}
274-
275-
// Validate the symbol. If it doesn't look like anything we're
276-
// expecting, we just print it literally. Note that we must handle non-rust
277-
// symbols because we could have any function in the backtrace.
278-
let mut valid = true;
279-
let mut inner = s;
280-
if s.len() > 4 && s.starts_with("_ZN") && s.ends_with("E") {
281-
inner = &s[3 .. s.len() - 1];
282-
// On Windows, dbghelp strips leading underscores, so we accept "ZN...E" form too.
283-
} else if s.len() > 3 && s.starts_with("ZN") && s.ends_with("E") {
284-
inner = &s[2 .. s.len() - 1];
285-
} else {
286-
valid = false;
287-
}
288-
289-
if valid {
290-
let mut chars = inner.chars();
291-
while valid {
292-
let mut i = 0;
293-
for c in chars.by_ref() {
294-
if c.is_numeric() {
295-
i = i * 10 + c as usize - '0' as usize;
296-
} else {
297-
break
298-
}
299-
}
300-
if i == 0 {
301-
valid = chars.next().is_none();
302-
break
303-
} else if chars.by_ref().take(i - 1).count() != i - 1 {
304-
valid = false;
305-
}
306-
}
307-
}
308-
309-
// Alright, let's do this.
310-
if !valid {
311-
writer.write_all(s.as_bytes())?;
312-
} else {
313-
// remove the `::hfc2edb670e5eda97` part at the end of the symbol.
314-
if format == PrintFormat::Short {
315-
// The symbol in still mangled.
316-
let mut split = inner.rsplitn(2, "17h");
317-
match (split.next(), split.next()) {
318-
(Some(addr), rest) => {
319-
if addr.len() == 16 &&
320-
addr.chars().all(|c| c.is_digit(16))
321-
{
322-
inner = rest.unwrap_or("");
323-
}
324-
}
325-
_ => (),
326-
}
327-
}
328-
329-
let mut first = true;
330-
while !inner.is_empty() {
331-
if !first {
332-
writer.write_all(b"::")?;
333-
} else {
334-
first = false;
335-
}
336-
let mut rest = inner;
337-
while rest.chars().next().unwrap().is_numeric() {
338-
rest = &rest[1..];
339-
}
340-
let i: usize = inner[.. (inner.len() - rest.len())].parse().unwrap();
341-
inner = &rest[i..];
342-
rest = &rest[..i];
343-
if rest.starts_with("_$") {
344-
rest = &rest[1..];
345-
}
346-
while !rest.is_empty() {
347-
if rest.starts_with(".") {
348-
if let Some('.') = rest[1..].chars().next() {
349-
writer.write_all(b"::")?;
350-
rest = &rest[2..];
351-
} else {
352-
writer.write_all(b".")?;
353-
rest = &rest[1..];
354-
}
355-
} else if rest.starts_with("$") {
356-
macro_rules! demangle {
357-
($($pat:expr => $demangled:expr),*) => ({
358-
$(if rest.starts_with($pat) {
359-
writer.write_all($demangled)?;
360-
rest = &rest[$pat.len()..];
361-
} else)*
362-
{
363-
writer.write_all(rest.as_bytes())?;
364-
break;
365-
}
366-
367-
})
368-
}
369-
370-
// see src/librustc/back/link.rs for these mappings
371-
demangle! (
372-
"$SP$" => b"@",
373-
"$BP$" => b"*",
374-
"$RF$" => b"&",
375-
"$LT$" => b"<",
376-
"$GT$" => b">",
377-
"$LP$" => b"(",
378-
"$RP$" => b")",
379-
"$C$" => b",",
380-
381-
// in theory we can demangle any Unicode code point, but
382-
// for simplicity we just catch the common ones.
383-
"$u7e$" => b"~",
384-
"$u20$" => b" ",
385-
"$u27$" => b"'",
386-
"$u5b$" => b"[",
387-
"$u5d$" => b"]",
388-
"$u7b$" => b"{",
389-
"$u7d$" => b"}",
390-
"$u3b$" => b";",
391-
"$u2b$" => b"+",
392-
"$u22$" => b"\""
393-
)
394-
} else {
395-
let idx = match rest.char_indices().find(|&(_, c)| c == '$' || c == '.') {
396-
None => rest.len(),
397-
Some((i, _)) => i,
398-
};
399-
writer.write_all(rest[..idx].as_bytes())?;
400-
rest = &rest[idx..];
401-
}
402-
}
403-
}
404-
}
405-
406-
Ok(())
407-
}
408-
409-
#[cfg(test)]
410-
mod tests {
411-
use sys_common;
412-
macro_rules! t { ($a:expr, $b:expr) => ({
413-
let mut m = Vec::new();
414-
sys_common::backtrace::demangle(&mut m,
415-
$a,
416-
super::PrintFormat::Full).unwrap();
417-
assert_eq!(String::from_utf8(m).unwrap(), $b);
418-
}) }
419-
420-
#[test]
421-
fn demangle() {
422-
t!("test", "test");
423-
t!("_ZN4testE", "test");
424-
t!("_ZN4test", "_ZN4test");
425-
t!("_ZN4test1a2bcE", "test::a::bc");
426-
}
427-
428-
#[test]
429-
fn demangle_dollars() {
430-
t!("_ZN4$RP$E", ")");
431-
t!("_ZN8$RF$testE", "&test");
432-
t!("_ZN8$BP$test4foobE", "*test::foob");
433-
t!("_ZN9$u20$test4foobE", " test::foob");
434-
t!("_ZN35Bar$LT$$u5b$u32$u3b$$u20$4$u5d$$GT$E", "Bar<[u32; 4]>");
435-
}
436-
437-
#[test]
438-
fn demangle_many_dollars() {
439-
t!("_ZN13test$u20$test4foobE", "test test::foob");
440-
t!("_ZN12test$BP$test4foobE", "test*test::foob");
441-
}
442-
443-
#[test]
444-
fn demangle_windows() {
445-
t!("ZN4testE", "test");
446-
t!("ZN13test$u20$test4foobE", "test test::foob");
447-
t!("ZN12test$RF$test4foobE", "test&test::foob");
448-
}
449-
450-
#[test]
451-
fn demangle_elements_beginning_with_underscore() {
452-
t!("_ZN13_$LT$test$GT$E", "<test>");
453-
t!("_ZN28_$u7b$$u7b$closure$u7d$$u7d$E", "{{closure}}");
454-
t!("_ZN15__STATIC_FMTSTRE", "__STATIC_FMTSTR");
455-
}
456-
457-
#[test]
458-
fn demangle_trait_impls() {
459-
t!("_ZN71_$LT$Test$u20$$u2b$$u20$$u27$static$u20$as$u20$foo..Bar$LT$Test$GT$$GT$3barE",
460-
"<Test + 'static as foo::Bar<Test>>::bar");
461-
}
462-
}

src/tools/tidy/src/deps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const WHITELIST: &[Crate] = &[
7979
Crate("chalk-macros"),
8080
Crate("cloudabi"),
8181
Crate("cmake"),
82+
Crate("compiler_builtins"),
8283
Crate("crc"),
8384
Crate("crc32fast"),
8485
Crate("crossbeam-deque"),

0 commit comments

Comments
 (0)