| 1 |
use std::collections::HashSet; |
| 2 |
use std::env; |
| 3 |
use std::fs; |
| 4 |
use std::io::{self, BufRead, Write}; |
| 5 |
use std::path::{self, Path}; |
| 6 |
|
| 7 |
fn get_crate_dir() -> io::Result<path::PathBuf> { |
| 8 |
Ok(path::PathBuf::from( |
| 9 |
env::var("CARGO_MANIFEST_DIR") |
| 10 |
.map_err(|_| io::Error::new(io::ErrorKind::Other, "no CARGO_MANIFEST_DIR"))?, |
| 11 |
)) |
| 12 |
} |
| 13 |
|
| 14 |
fn get_out_dir() -> io::Result<path::PathBuf> { |
| 15 |
Ok(path::PathBuf::from(env::var("OUT_DIR").map_err(|_| { |
| 16 |
io::Error::new(io::ErrorKind::Other, "no OUT_DIR") |
| 17 |
})?)) |
| 18 |
} |
| 19 |
|
| 20 |
fn get_crate_test_path(file_name: &str) -> io::Result<path::PathBuf> { |
| 21 |
let mut test_path = get_crate_dir()?; |
| 22 |
test_path.push("tests"); |
| 23 |
assert!(test_path.is_dir()); |
| 24 |
test_path.push(file_name); |
| 25 |
Ok(test_path) |
| 26 |
} |
| 27 |
|
| 28 |
fn get_test_path(file_name: &str) -> io::Result<path::PathBuf> { |
| 29 |
let mut test_path = get_out_dir()?; |
| 30 |
assert!(test_path.is_dir()); |
| 31 |
test_path.push(file_name); |
| 32 |
Ok(test_path) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
fn generate_sanity_tests_from_afl_seeds() -> io::Result<()> { |
| 39 |
let mut in_dir = get_crate_dir()?; |
| 40 |
in_dir.push("in"); |
| 41 |
if !in_dir.is_dir() { |
| 42 |
|
| 43 |
|
| 44 |
return Ok(()); |
| 45 |
} |
| 46 |
|
| 47 |
let test_path = get_test_path("afl_seeds.rs")?; |
| 48 |
let mut test_file = fs::File::create(test_path)?; |
| 49 |
|
| 50 |
writeln!( |
| 51 |
&mut test_file, |
| 52 |
" |
| 53 |
extern crate cpp_demangle; |
| 54 |
use std::fs; |
| 55 |
use std::io::Read; |
| 56 |
" |
| 57 |
)?; |
| 58 |
|
| 59 |
println!("cargo:rerun-if-changed=tests/afl_seeds.rs"); |
| 60 |
|
| 61 |
let entries = fs::read_dir(in_dir)?; |
| 62 |
|
| 63 |
for entry in entries { |
| 64 |
let entry = entry?; |
| 65 |
|
| 66 |
let path = entry.path(); |
| 67 |
let file_name = path.file_name().ok_or_else(|| { |
| 68 |
io::Error::new( |
| 69 |
io::ErrorKind::Other, |
| 70 |
"no file name for AFL.rs seed test case", |
| 71 |
) |
| 72 |
})?; |
| 73 |
println!( |
| 74 |
"cargo:rerun-if-changed=in/{}", |
| 75 |
Path::new(file_name).display() |
| 76 |
); |
| 77 |
|
| 78 |
|
| 79 |
let path = path.to_string_lossy().replace("\\", "\\\\"); |
| 80 |
|
| 81 |
writeln!( |
| 82 |
&mut test_file, |
| 83 |
r#" |
| 84 |
#[test] |
| 85 |
fn test_afl_seed_{}() {{ |
| 86 |
let mut file = fs::File::open("{}").unwrap(); |
| 87 |
let mut contents = Vec::new(); |
| 88 |
file.read_to_end(&mut contents).unwrap(); |
| 89 |
let _ = cpp_demangle::Symbol::new(contents); |
| 90 |
assert!(true, "did not panic when parsing"); |
| 91 |
}} |
| 92 |
"#, |
| 93 |
file_name.to_string_lossy(), |
| 94 |
path |
| 95 |
)?; |
| 96 |
} |
| 97 |
|
| 98 |
Ok(()) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
fn generate_compatibility_tests_from_libiberty() -> io::Result<()> { |
| 110 |
let mut tests_dir = get_crate_dir()?; |
| 111 |
tests_dir.push("tests"); |
| 112 |
if !tests_dir.is_dir() { |
| 113 |
|
| 114 |
|
| 115 |
return Ok(()); |
| 116 |
} |
| 117 |
|
| 118 |
println!("cargo:rerun-if-changed=tests/libiberty-demangle-expected"); |
| 119 |
|
| 120 |
let test_path = get_test_path("libiberty.rs")?; |
| 121 |
let _ = fs::remove_file(&test_path); |
| 122 |
let mut test_file = fs::File::create(test_path)?; |
| 123 |
|
| 124 |
writeln!( |
| 125 |
&mut test_file, |
| 126 |
" |
| 127 |
extern crate cpp_demangle; |
| 128 |
extern crate diff; |
| 129 |
" |
| 130 |
)?; |
| 131 |
|
| 132 |
|
| 133 |
let libiberty_passing_tests = { |
| 134 |
let mut s: HashSet<_> = (0..86).collect(); |
| 135 |
s.extend(87..90); |
| 136 |
s.extend(91..93); |
| 137 |
s.extend(94..113); |
| 138 |
s.extend(115..118); |
| 139 |
s.extend(119..120); |
| 140 |
s.extend(121..123); |
| 141 |
s.extend(127..131); |
| 142 |
s.extend(133..134); |
| 143 |
s.extend(137..138); |
| 144 |
s.extend(139..140); |
| 145 |
s.extend(141..144); |
| 146 |
s.extend(146..147); |
| 147 |
s.extend(149..154); |
| 148 |
s.extend(158..168); |
| 149 |
s.extend(169..180); |
| 150 |
s.extend(181..183); |
| 151 |
s.extend(184..186); |
| 152 |
s.extend(187..190); |
| 153 |
s.extend(199..200); |
| 154 |
s.extend(201..202); |
| 155 |
s.extend(203..204); |
| 156 |
s |
| 157 |
}; |
| 158 |
|
| 159 |
let libiberty_tests = get_crate_test_path("libiberty-demangle-expected")?; |
| 160 |
let libiberty_tests = fs::File::open(libiberty_tests)?; |
| 161 |
let libiberty_tests = io::BufReader::new(libiberty_tests); |
| 162 |
|
| 163 |
let mut lines = libiberty_tests |
| 164 |
.lines() |
| 165 |
.filter(|line| line.as_ref().map(|l| !l.starts_with('#')).unwrap_or(true)); |
| 166 |
|
| 167 |
let mut n = 0; |
| 168 |
|
| 169 |
loop { |
| 170 |
let options = match lines.next() { |
| 171 |
None => break, |
| 172 |
Some(Ok(line)) => line, |
| 173 |
Some(Err(e)) => return Err(e), |
| 174 |
}; |
| 175 |
|
| 176 |
let mangled = match lines.next() { |
| 177 |
Some(Ok(line)) => line, |
| 178 |
None => { |
| 179 |
return Err(io::Error::new( |
| 180 |
io::ErrorKind::Other, |
| 181 |
"expected a line with a mangled symbol", |
| 182 |
)) |
| 183 |
} |
| 184 |
Some(Err(e)) => return Err(e), |
| 185 |
}; |
| 186 |
|
| 187 |
let demangled = match lines.next() { |
| 188 |
Some(Ok(line)) => line, |
| 189 |
None => { |
| 190 |
return Err(io::Error::new( |
| 191 |
io::ErrorKind::Other, |
| 192 |
"expected a line with the demangled symbol", |
| 193 |
)) |
| 194 |
} |
| 195 |
Some(Err(e)) => return Err(e), |
| 196 |
}; |
| 197 |
|
| 198 |
if options.contains("--no-params") { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
match lines.next() { |
| 203 |
Some(Ok(_)) => {} |
| 204 |
None => { |
| 205 |
return Err(io::Error::new( |
| 206 |
io::ErrorKind::Other, |
| 207 |
"expected a line with the demangled symbol without parameters", |
| 208 |
)) |
| 209 |
} |
| 210 |
Some(Err(e)) => return Err(e), |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
if !options.contains("--format=gnu-v3") |
| 216 |
|| options.contains("--is-v3-ctor") |
| 217 |
|| options.contains("--is-v3-dtor") |
| 218 |
|| options.contains("--ret-postfix") |
| 219 |
{ |
| 220 |
continue; |
| 221 |
} |
| 222 |
|
| 223 |
let cfg = if libiberty_passing_tests.contains(&n) { |
| 224 |
"" |
| 225 |
} else { |
| 226 |
r###"#[cfg(feature = "run_libiberty_tests")]"### |
| 227 |
}; |
| 228 |
|
| 229 |
writeln!( |
| 230 |
test_file, |
| 231 |
r###" |
| 232 |
{} |
| 233 |
#[test] |
| 234 |
fn test_libiberty_demangle_{}_() {{ |
| 235 |
let mangled = br#"{}"#; |
| 236 |
let mangled_str = String::from_utf8_lossy(mangled).into_owned(); |
| 237 |
println!("Parsing mangled symbol: {{}}", mangled_str); |
| 238 |
|
| 239 |
let expected = r#"{}"#; |
| 240 |
|
| 241 |
let sym = match cpp_demangle::Symbol::new(&mangled[..]) {{ |
| 242 |
Ok(sym) => sym, |
| 243 |
Err(_) if mangled_str == expected => return, |
| 244 |
Err(e) => panic!("Should parse mangled symbol {{}}", e), |
| 245 |
}}; |
| 246 |
|
| 247 |
let actual = match sym.demangle() {{ |
| 248 |
Ok(a) => a, |
| 249 |
Err(e) => {{ |
| 250 |
panic!("Error while demangling '{{}}': {{}}", |
| 251 |
mangled_str, |
| 252 |
e); |
| 253 |
}} |
| 254 |
}}; |
| 255 |
|
| 256 |
println!(" Expect demangled symbol: {{}}", expected); |
| 257 |
println!("Actually demangled symbol as: {{}}", actual); |
| 258 |
|
| 259 |
if expected != actual {{ |
| 260 |
println!(""); |
| 261 |
println!("Diff:"); |
| 262 |
println!("--- expected"); |
| 263 |
print!("+++ actual"); |
| 264 |
|
| 265 |
let mut last = None; |
| 266 |
for cmp in diff::chars(expected, &actual) {{ |
| 267 |
match (last, cmp.clone()) {{ |
| 268 |
(Some(diff::Result::Left(_)), diff::Result::Left(_)) | |
| 269 |
(Some(diff::Result::Both(..)), diff::Result::Both(..)) | |
| 270 |
(Some(diff::Result::Right(_)), diff::Result::Right(_)) => {{}} |
| 271 |
|
| 272 |
(_, diff::Result::Left(_)) => print!("\n-"), |
| 273 |
(_, diff::Result::Both(..)) => print!("\n "), |
| 274 |
(_, diff::Result::Right(_)) => print!("\n+"), |
| 275 |
}}; |
| 276 |
match cmp.clone() {{ |
| 277 |
diff::Result::Left(c) | |
| 278 |
diff::Result::Both(c, _) | |
| 279 |
diff::Result::Right(c) => print!("{{}}", c), |
| 280 |
}} |
| 281 |
last = Some(cmp); |
| 282 |
}} |
| 283 |
println!(""); |
| 284 |
}} |
| 285 |
|
| 286 |
assert_eq!(*expected, actual); |
| 287 |
}} |
| 288 |
"###, |
| 289 |
cfg, |
| 290 |
n, |
| 291 |
mangled.trim(), |
| 292 |
demangled.trim() |
| 293 |
)?; |
| 294 |
|
| 295 |
n += 1; |
| 296 |
} |
| 297 |
|
| 298 |
Ok(()) |
| 299 |
} |
| 300 |
|
| 301 |
fn main() { |
| 302 |
println!("cargo:rerun-if-changed=build.rs"); |
| 303 |
|
| 304 |
generate_sanity_tests_from_afl_seeds() |
| 305 |
.expect("should generate sanity tests from AFL.rs seed test cases"); |
| 306 |
|
| 307 |
generate_compatibility_tests_from_libiberty() |
| 308 |
.expect("should generate compatibility tests from tests/libiberty-demangle-expected"); |
| 309 |
} |