Debian Codegraph @

third-party/rust-demangle/test-harness/tests/v0.rs

raw
1 //! Tests copied from `https://github.com/rust-lang/rustc-demangle`'s
2 //! `src/v0.rs` at `fd906f850f90f6d4845c7b8219d218293e0ab3ed`.
3 //!
4 //! These are the only changes made to the tests:
5 //! * `::` absolute paths -> `rust_demangle_c_test_harness::`
6 //! * `#[cfg(unsupported_tests)]` was added to tests that couldn't compile
7 //! * `#[ignore = "stack overflow"]` was added to tests that overflow the stack
8 //! * `#[should_panic]` was added to tests that don't pass yet
9
10 use rust_demangle_c_test_harness::assert_contains;
11
12 macro_rules! t {
13 ($a:expr, $b:expr) => {{
14 assert_eq!(
15 format!("{}", rust_demangle_c_test_harness::demangle($a)),
16 $b
17 );
18 }};
19 }
20 macro_rules! t_nohash {
21 ($a:expr, $b:expr) => {{
22 assert_eq!(
23 format!("{:#}", rust_demangle_c_test_harness::demangle($a)),
24 $b
25 );
26 }};
27 }
28 macro_rules! t_nohash_type {
29 ($a:expr, $b:expr) => {
30 t_nohash!(concat!("_RMC0", $a), concat!("<", $b, ">"))
31 };
32 }
33 macro_rules! t_const {
34 ($mangled:expr, $value:expr) => {
35 t_nohash!(
36 concat!("_RIC0K", $mangled, "E"),
37 concat!("::<", $value, ">")
38 )
39 };
40 }
41 macro_rules! t_const_suffixed {
42 ($mangled:expr, $value:expr, $value_ty_suffix:expr) => {{
43 t_const!($mangled, $value);
44 t!(
45 concat!("_RIC0K", $mangled, "E"),
46 concat!("[0]::<", $value, $value_ty_suffix, ">")
47 );
48 }};
49 }
50
51 #[test]
52 fn demangle_crate_with_leading_digit() {
53 t_nohash!("_RNvC6_123foo3bar", "123foo::bar");
54 }
55
56 #[test]
57 fn demangle_utf8_idents() {
58 t_nohash!(
59 "_RNqCs4fqI2P2rA04_11utf8_identsu30____7hkackfecea1cbdathfdh9hlq6y",
60 "utf8_idents::საჭმელად_გემრიელი_სადილი"
61 );
62 }
63
64 #[test]
65 fn demangle_closure() {
66 t_nohash!(
67 "_RNCNCNgCs6DXkGYLi8lr_2cc5spawn00B5_",
68 "cc::spawn::{closure#0}::{closure#0}"
69 );
70 t_nohash!(
71 "_RNCINkXs25_NgCsbmNqQUJIY6D_4core5sliceINyB9_4IterhENuNgNoBb_4iter8iterator8Iterator9rpositionNCNgNpB9_6memchr7memrchrs_0E0Bb_",
72 "<core::slice::Iter<u8> as core::iter::iterator::Iterator>::rposition::<core::slice::memchr::memrchr::{closure#1}>::{closure#0}"
73 );
74 }
75
76 #[test]
77 fn demangle_dyn_trait() {
78 t_nohash!(
79 "_RINbNbCskIICzLVDPPb_5alloc5alloc8box_freeDINbNiB4_5boxed5FnBoxuEp6OutputuEL_ECs1iopQbuBiw2_3std",
80 "alloc::alloc::box_free::<dyn alloc::boxed::FnBox<(), Output = ()>>"
81 );
82 }
83
84 #[test]
85 fn demangle_const_generics_preview() {
86 // NOTE(eddyb) this was hand-written, before rustc had working
87 // const generics support (but the mangling format did include them).
88 t_nohash_type!(
89 "INtC8arrayvec8ArrayVechKj7b_E",
90 "arrayvec::ArrayVec<u8, 123>"
91 );
92 t_const_suffixed!("j7b_", "123", "usize");
93 }
94
95 #[test]
96 fn demangle_min_const_generics() {
97 t_const!("p", "_");
98 t_const_suffixed!("hb_", "11", "u8");
99 t_const_suffixed!("off00ff00ff00ff00ff_", "0xff00ff00ff00ff00ff", "u128");
100 t_const_suffixed!("s98_", "152", "i16");
101 t_const_suffixed!("anb_", "-11", "i8");
102 t_const!("b0_", "false");
103 t_const!("b1_", "true");
104 t_const!("c76_", "'v'");
105 t_const!("c22_", r#"'"'"#);
106 t_const!("ca_", "'\\n'");
107 t_const!("c2202_", "'∂'");
108 }
109
110 #[test]
111 fn demangle_const_str() {
112 t_const!("e616263_", "{*\"abc\"}");
113 t_const!("e27_", r#"{*"'"}"#);
114 t_const!("e090a_", "{*\"\\t\\n\"}");
115 t_const!("ee28882c3bc_", "{*\"∂ü\"}");
116 t_const!(
117 "ee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839b\
118 e183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_",
119 "{*\"საჭმელად_გემრიელი_სადილი\"}"
120 );
121 t_const!(
122 "ef09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e298\
123 95f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_",
124 "{*\"🐊🦈🦆🐮 § 🐶👒☕🔥 § 🧡💛💚💙💜\"}"
125 );
126 }
127
128 // NOTE(eddyb) this uses the same strings as `demangle_const_str` and should
129 // be kept in sync with it - while a macro could be used to generate both
130 // `str` and `&str` tests, from a single list of strings, this seems clearer.
131 #[test]
132 fn demangle_const_ref_str() {
133 t_const!("Re616263_", "\"abc\"");
134 t_const!("Re27_", r#""'""#);
135 t_const!("Re090a_", "\"\\t\\n\"");
136 t_const!("Ree28882c3bc_", "\"∂ü\"");
137 t_const!(
138 "Ree183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839b\
139 e183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_",
140 "\"საჭმელად_გემრიელი_სადილი\""
141 );
142 t_const!(
143 "Ref09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e298\
144 95f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_",
145 "\"🐊🦈🦆🐮 § 🐶👒☕🔥 § 🧡💛💚💙💜\""
146 );
147 }
148
149 #[test]
150 fn demangle_const_ref() {
151 t_const!("Rp", "{&_}");
152 t_const!("Rh7b_", "{&123}");
153 t_const!("Rb0_", "{&false}");
154 t_const!("Rc58_", "{&'X'}");
155 t_const!("RRRh0_", "{&&&0}");
156 t_const!("RRRe_", "{&&\"\"}");
157 t_const!("QAE", "{&mut []}");
158 }
159
160 #[test]
161 fn demangle_const_array() {
162 t_const!("AE", "{[]}");
163 t_const!("Aj0_E", "{[0]}");
164 t_const!("Ah1_h2_h3_E", "{[1, 2, 3]}");
165 t_const!("ARe61_Re62_Re63_E", "{[\"a\", \"b\", \"c\"]}");
166 t_const!("AAh1_h2_EAh3_h4_EE", "{[[1, 2], [3, 4]]}");
167 }
168
169 #[test]
170 fn demangle_const_tuple() {
171 t_const!("TE", "{()}");
172 t_const!("Tj0_E", "{(0,)}");
173 t_const!("Th1_b0_E", "{(1, false)}");
174 t_const!(
175 "TRe616263_c78_RAh1_h2_h3_EE",
176 "{(\"abc\", 'x', &[1, 2, 3])}"
177 );
178 }
179
180 #[test]
181 fn demangle_const_adt() {
182 t_const!(
183 "VNvINtNtC4core6option6OptionjE4NoneU",
184 "{core::option::Option::<usize>::None}"
185 );
186 t_const!(
187 "VNvINtNtC4core6option6OptionjE4SomeTj0_E",
188 "{core::option::Option::<usize>::Some(0)}"
189 );
190 t_const!(
191 "VNtC3foo3BarS1sRe616263_2chc78_5sliceRAh1_h2_h3_EE",
192 "{foo::Bar { s: \"abc\", ch: 'x', slice: &[1, 2, 3] }}"
193 );
194 }
195
196 #[test]
197 fn demangle_exponential_explosion() {
198 // NOTE(eddyb) because of the prefix added by `t_nohash_type!` is
199 // 3 bytes long, `B2_` refers to the start of the type, not `B_`.
200 // 6 backrefs (`B8_E` through `B3_E`) result in 2^6 = 64 copies of `_`.
201 // Also, because the `p` (`_`) type is after all of the starts of the
202 // backrefs, it can be replaced with any other type, independently.
203 t_nohash_type!(
204 concat!("TTTTTT", "p", "B8_E", "B7_E", "B6_E", "B5_E", "B4_E", "B3_E"),
205 "((((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _)))), \
206 ((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _))))), \
207 (((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _)))), \
208 ((((_, _), (_, _)), ((_, _), (_, _))), (((_, _), (_, _)), ((_, _), (_, _))))))"
209 );
210 }
211
212 #[test]
213 fn demangle_thinlto() {
214 t_nohash!("_RC3foo.llvm.9D1C9369", "foo");
215 t_nohash!("_RC3foo.llvm.9D1C9369@@16", "foo");
216 t_nohash!("_RNvC9backtrace3foo.llvm.A5310EB9", "backtrace::foo");
217 }
218
219 #[test]
220 fn demangle_extra_suffix() {
221 // From alexcrichton/rustc-demangle#27:
222 t_nohash!(
223 "_RNvNtNtNtNtCs92dm3009vxr_4rand4rngs7adapter9reseeding4fork23FORK_HANDLER_REGISTERED.0.0",
224 "rand::rngs::adapter::reseeding::fork::FORK_HANDLER_REGISTERED.0.0"
225 );
226 }
227
228 // FIXME(eddyb) get this working with the `rust-demangle.c` test harness.
229 #[cfg(unsupported_tests)]
230 #[test]
231 fn demangling_limits() {
232 // Stress tests found via fuzzing.
233
234 for sym in include_str!("v0-large-test-symbols/early-recursion-limit")
235 .lines()
236 .filter(|line| !line.is_empty() && !line.starts_with('#'))
237 {
238 assert_eq!(
239 super::demangle(sym).map(|_| ()),
240 Err(super::ParseError::RecursedTooDeep)
241 );
242 }
243
244 assert_contains!(
245 ::demangle(
246 "RIC20tRYIMYNRYFG05_EB5_B_B6_RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\
247 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRB_E",
248 )
249 .to_string(),
250 "{recursion limit reached}"
251 );
252 }
253
254 // FIXME(eddyb) get this working with the `rust-demangle.c` test harness.
255 #[cfg(unsupported_tests)]
256 #[test]
257 fn recursion_limit_leaks() {
258 // NOTE(eddyb) this test checks that both paths and types support the
259 // recursion limit correctly, i.e. matching `push_depth` and `pop_depth`,
260 // and don't leak "recursion levels" and trip the limit.
261 // The test inputs are generated on the fly, using a repeated pattern,
262 // as hardcoding the actual strings would be too verbose.
263 // Also, `MAX_DEPTH` can be directly used, instead of assuming its value.
264 for &(sym_leaf, expected_leaf) in &[("p", "_"), ("Rp", "&_"), ("C1x", "x")] {
265 let mut sym = format!("_RIC0p");
266 let mut expected = format!("::<_");
267 for _ in 0..(super::MAX_DEPTH * 2) {
268 sym.push_str(sym_leaf);
269 expected.push_str(", ");
270 expected.push_str(expected_leaf);
271 }
272 sym.push('E');
273 expected.push('>');
274
275 t_nohash!(&sym, expected);
276 }
277 }
278
279 // FIXME(eddyb) port recursion limits to C.
280 #[ignore = "stack overflow"]
281 #[test]
282 fn recursion_limit_backref_free_bypass() {
283 // NOTE(eddyb) this test checks that long symbols cannot bypass the
284 // recursion limit by not using backrefs, and cause a stack overflow.
285
286 // This value was chosen to be high enough that stack overflows were
287 // observed even with `cargo test --release`.
288 let depth = 100_000;
289
290 // In order to hide the long mangling from the initial "shallow" parse,
291 // it's nested in an identifier (crate name), preceding its use.
292 let mut sym = format!("_RIC{}", depth);
293 let backref_start = sym.len() - 2;
294 for _ in 0..depth {
295 sym.push('R');
296 }
297
298 // Write a backref to just after the length of the identifier.
299 sym.push('B');
300 sym.push(char::from_digit((backref_start - 1) as u32, 36).unwrap());
301 sym.push('_');
302
303 // Close the `I` at the start.
304 sym.push('E');
305
306 assert_contains!(
307 rust_demangle_c_test_harness::demangle(&sym).to_string(),
308 "{recursion limit reached}"
309 );
310 }