1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
use std::collections::HashMap;
use pdb::FallibleIterator;
fn setup<F>(func: F)
where
F: FnOnce(&pdb::TypeInformation<'_>),
{
let file = if let Ok(filename) = std::env::var("PDB_FILE") {
std::fs::File::open(filename)
} else {
std::fs::File::open("fixtures/self/foo.pdb")
}
.expect("opening file");
let mut pdb = pdb::PDB::open(file).expect("opening pdb");
let type_information = pdb.type_information().expect("type information");
func(&type_information);
}
#[test]
fn iteration() {
setup(|type_information| {
let len = type_information.len();
let mut count: usize = 0;
let mut last_index = pdb::TypeIndex(4095);
let mut iter = type_information.iter();
while let Some(typ) = iter.next().expect("next type") {
assert_eq!(typ.index().0, last_index.0 + 1);
last_index = typ.index();
count += 1;
}
assert_eq!(len, count);
});
}
#[test]
fn type_finder() {
setup(|type_information| {
let mut type_finder = type_information.finder();
let mut map: HashMap<pdb::TypeIndex, pdb::Type<'_>> = HashMap::new();
assert_eq!(type_finder.max_index().0 >> 3, 4096 >> 3);
// iterate over all the types
let mut iter = type_information.iter();
while let Some(typ) = iter.next().expect("next type") {
assert_eq!(type_finder.max_index().0 >> 3, typ.index().0 >> 3);
// update the type finder
type_finder.update(&iter);
// record this type in our map
map.insert(typ.index(), typ);
}
// iterate over the map -- which is randomized -- making sure the type finder finds identical types
for (index, typ) in map.iter() {
let found = type_finder.find(*index).expect("find");
assert_eq!(*typ, found);
}
})
}
#[test]
fn find_classes() {
setup(|type_information| {
let mut type_finder = type_information.finder();
// iterate over all the types
let mut iter = type_information.iter();
while let Some(typ) = iter.next().expect("next type") {
// update the type finder
type_finder.update(&iter);
// parse the type record
match typ.parse() {
Ok(pdb::TypeData::Class(pdb::ClassType {
name,
fields: Some(fields),
..
})) => {
// this Type describes a class-like type with fields
println!("class {} (type {}):", name, typ.index());
// fields is presently a TypeIndex
// find and parse the list of fields
match type_finder.find(fields).expect("find fields").parse() {
Ok(pdb::TypeData::FieldList(list)) => {
for field in list.fields {
println!(" - {:?}", field);
}
if let Some(c) = list.continuation {
println!("TODO: follow to type {}", c);
}
}
Ok(value) => {
panic!("expected a field list, got {:?}", value);
}
Err(e) => {
println!("field parse error: {}", e);
}
}
}
Ok(pdb::TypeData::Enumeration(data)) => {
println!("enum {} (type {}):", data.name, data.fields);
// fields is presently a TypeIndex
match type_finder.find(data.fields).expect("find fields").parse() {
Ok(pdb::TypeData::FieldList(list)) => {
for field in list.fields {
println!(" - {:?}", field);
}
if let Some(c) = list.continuation {
println!("TODO: follow to type {}", c);
}
}
Ok(value) => {
panic!("expected a field list, got {:?}", value);
}
Err(e) => {
println!("field parse error: {}", e);
}
}
}
Ok(pdb::TypeData::FieldList(_)) => {
// ignore, since we find these by class
}
Ok(_) => {
//println!("type: {:?}", data);
}
Err(pdb::Error::UnimplementedTypeKind(kind)) => {
println!("unimplemented: 0x{:04x}", kind);
// TODO: parse everything
// ignore for now
}
Err(e) => {
// other parse error
println!(
"other parse error on type {} (raw type {:04x}): {}",
typ.index(),
typ.raw_kind(),
e
);
panic!("dying due to parse error");
}
}
}
// hooah!
})
}
/*
#[bench]
fn bench_type_finder(b: &mut test::Bencher) {
setup(|type_information| {
let mut type_finder = type_information.finder();
assert_eq!(type_finder.max_index() >> 3, 4096 >> 3);
// iterate over all the types
let mut iter = type_information.iter();
while let Some(typ) = iter.next().expect("next type") {
assert_eq!(type_finder.max_index() >> 3, typ.index() >> 3);
type_finder.update(&iter);
}
let mut rng = rand::thread_rng();
let count: pdb::TypeIndex = type_information.len() as pdb::TypeIndex;
let base: pdb::TypeIndex = 4096;
// time how long it takes to build a map
b.iter(|| {
let lucky = rng.gen_range(base, base + count);
let found = type_finder.find(lucky).expect("find");
test::black_box(&found);
});
})
}
*/
/*
#[test]
fn type_length_histogram() {
setup(|type_information| {
let mut lens: Vec<usize> = Vec::new();
lens.resize(1025, 0);
// iterate over all the types
let mut iter = type_information.iter();
while let Some(typ) = iter.next().expect("next type") {
let mut len = typ.len() + 2;
if len > 1024 {
len = 1024;
}
lens[len] += 1;
}
for (len, count) in lens.as_slice().iter().enumerate() {
println!("{}\t{}", len, count);
}
panic!();
})
}
*/
|