Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve array and function parameter reconstruction #56

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions resym_core/src/pdb_types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,12 @@ impl<'p> Class<'p> {
""
},
&method.name,
method.arguments.join(", "),
method
.arguments
.iter()
.map(|(type_left, type_right)| format!("{type_left}{type_right}"))
.collect::<Vec<String>>()
.join(", "),
method.return_type_name.1,
if method.is_const { " const" } else { "" },
if method.is_volatile { " volatile" } else { "" },
Expand All @@ -537,7 +542,12 @@ impl<'p> Class<'p> {
""
},
&method.name,
method.arguments.join(", "),
method
.arguments
.iter()
.map(|(type_left, type_right)| format!("{type_left}{type_right}"))
.collect::<Vec<String>>()
.join(", "),
method.return_type_name.1,
if method.is_const { " const" } else { "" },
if method.is_volatile { " volatile" } else { "" },
Expand Down
2 changes: 1 addition & 1 deletion resym_core/src/pdb_types/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::{Result, ResymCoreError};
pub struct Method<'p> {
pub name: pdb::RawString<'p>,
pub return_type_name: (String, String),
pub arguments: Vec<String>,
pub arguments: Vec<(String, String)>,
pub is_virtual: bool,
pub is_pure_virtual: bool,
pub is_ctor: bool,
Expand Down
62 changes: 37 additions & 25 deletions resym_core/src/pdb_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn type_name(
// Resolve the complete type's index, if present in the PDB
let complete_element_type_index =
resolve_complete_type_index(type_forwarder, data.element_type);
let (base_name, mut dimensions) = array_base_name(
let ((type_left, type_right), mut dimensions) = array_base_name(
type_finder,
type_forwarder,
complete_element_type_index,
Expand All @@ -123,8 +123,9 @@ pub fn type_name(
let type_size = u32::try_from(type_size(type_finder, complete_element_type_index)?)?;
let mut divider = if type_size == 0 {
log::warn!(
"'{}' has invalid size (0), array dimensions might be incorrect",
base_name
"'{}{}' has invalid size (0), array dimensions might be incorrect",
type_left,
type_right,
);
1
} else {
Expand All @@ -149,7 +150,7 @@ pub fn type_name(
dimensions_str = format!("{dimensions_str}[{dim}]");
}

(base_name, dimensions_str)
(type_left, format!("{}{}", dimensions_str, type_right))
}

pdb::TypeData::Bitfield(data) => {
Expand Down Expand Up @@ -192,7 +193,14 @@ pub fn type_name(

(
format!("{ret_type_left}{ret_type_right} ("),
format!(")({})", arg_list.join(", ")),
format!(
")({})",
arg_list
.into_iter()
.map(|(type_left, type_right)| format!("{type_left}{type_right}"))
.collect::<Vec<String>>()
.join(", ")
),
)
}

Expand Down Expand Up @@ -227,7 +235,14 @@ pub fn type_name(

(
format!("{ret_type_left}{ret_type_right} ({class_type_left}::"),
format!(")({})", arg_list.join(", ")),
format!(
")({})",
arg_list
.into_iter()
.map(|(type_left, type_right)| format!("{type_left}{type_right}"))
.collect::<Vec<String>>()
.join(", ")
),
)
}

Expand All @@ -252,13 +267,13 @@ fn array_base_name(
type_index: pdb::TypeIndex,
primitive_flavor: &PrimitiveReconstructionFlavor,
needed_types: &mut TypeSet,
) -> Result<(String, Vec<usize>)> {
) -> Result<((String, String), Vec<usize>)> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::Array(data) => {
// Resolve the complete type's index, if present in the PDB
let complete_element_type_index =
resolve_complete_type_index(type_forwarder, data.element_type);
let (base_name, mut base_dimensions) = array_base_name(
let ((type_left, type_right), mut base_dimensions) = array_base_name(
type_finder,
type_forwarder,
complete_element_type_index,
Expand All @@ -268,8 +283,9 @@ fn array_base_name(
let type_size = u32::try_from(type_size(type_finder, complete_element_type_index)?)?;
let mut divider = if type_size == 0 {
log::warn!(
"'{}' has invalid size (0), array dimensions might be incorrect",
base_name
"'{}{}' has invalid size (0), array dimensions might be incorrect",
type_left,
type_right,
);
1
} else {
Expand All @@ -287,7 +303,7 @@ fn array_base_name(
.collect::<Vec<_>>();
base_dimensions.append(&mut dimensions_elem_count);

Ok((base_name, base_dimensions))
Ok(((type_left, type_right), base_dimensions))
}
_ => Ok((
type_name(
Expand All @@ -296,8 +312,7 @@ fn array_base_name(
type_index,
primitive_flavor,
needed_types,
)?
.0,
)?,
vec![],
)),
}
Expand All @@ -309,21 +324,18 @@ pub fn argument_list(
type_index: pdb::TypeIndex,
primitive_flavor: &PrimitiveReconstructionFlavor,
needed_types: &mut TypeSet,
) -> Result<Vec<String>> {
) -> Result<Vec<(String, String)>> {
match type_finder.find(type_index)?.parse()? {
pdb::TypeData::ArgumentList(data) => {
let mut args: Vec<String> = Vec::new();
let mut args = Vec::new();
for arg_type in data.arguments {
args.push(
type_name(
type_finder,
type_forwarder,
arg_type,
primitive_flavor,
needed_types,
)?
.0,
);
args.push(type_name(
type_finder,
type_forwarder,
arg_type,
primitive_flavor,
needed_types,
)?);
}
Ok(args)
}
Expand Down
14 changes: 12 additions & 2 deletions resym_core/src/pdb_types/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,12 @@ impl<'p> Union<'p> {
""
},
&method.name,
method.arguments.join(", "),
method
.arguments
.iter()
.map(|(type_left, type_right)| format!("{type_left}{type_right}"))
.collect::<Vec<String>>()
.join(", "),
method.return_type_name.1,
if method.is_const { " const" } else { "" },
if method.is_volatile { " volatile" } else { "" },
Expand Down Expand Up @@ -411,7 +416,12 @@ impl<'p> Union<'p> {
""
},
&method.name,
method.arguments.join(", "),
method
.arguments
.iter()
.map(|(type_left, type_right)| format!("{type_left}{type_right}"))
.collect::<Vec<String>>()
.join(", "),
method.return_type_name.1,
if method.is_const { " const" } else { "" },
if method.is_volatile { " volatile" } else { "" },
Expand Down
7 changes: 7 additions & 0 deletions resym_core/tests/data/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ struct NestedStructUnionRegression1 {
}; /* size: 0x0008 */
};

struct NtdllRegression1 {
VOID(*KernelRoutine)
(struct _KAPC* arg1, VOID (**arg2)(VOID* arg1, VOID* arg2, VOID* arg3),
VOID** arg3, VOID** arg4, VOID** arg5);
LONG (*MajorFunction[28])(struct _DEVICE_OBJECT* arg1, struct _IRP* arg2);
};

} // namespace resym_test

int main() {
Expand Down
Binary file modified resym_core/tests/data/test.pdb
Binary file not shown.
6 changes: 2 additions & 4 deletions resym_core/tests/module_dumping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const TEST_MODULE_PATH: &str = "D:\\a\\_work\\1\\s\\Intermediate\\crt\\vcstartup

#[test]
fn test_module_dumping_by_path_portable() {
let mut pdb_file =
PdbFile::load_from_file(Path::new(TEST_PDB_FILE_PATH)).expect("load test.pdb");
let pdb_file = PdbFile::load_from_file(Path::new(TEST_PDB_FILE_PATH)).expect("load test.pdb");

let module_dump = pdb_file
.reconstruct_module_by_path(TEST_MODULE_PATH, PrimitiveReconstructionFlavor::Portable)
Expand Down Expand Up @@ -50,8 +49,7 @@ fn test_module_dumping_by_index_internal(
module_index: usize,
primitives_flavor: PrimitiveReconstructionFlavor,
) {
let mut pdb_file =
PdbFile::load_from_file(Path::new(TEST_PDB_FILE_PATH)).expect("load test.pdb");
let pdb_file = PdbFile::load_from_file(Path::new(TEST_PDB_FILE_PATH)).expect("load test.pdb");

let module_dump = pdb_file
.reconstruct_module_by_index(module_index, primitives_flavor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: resym_core/tests/type_reconstruction.rs
expression: reconstructed_type
---
struct resym_test::NtdllRegression1 { /* Size=0xe8 */
/* 0x0000 */ public: VOID (* KernelRoutine)(resym_test::_KAPC*, VOID (**)(PVOID, PVOID, PVOID), PVOID*, PVOID*, PVOID*);
/* 0x0008 */ public: LONG (* MajorFunction[28])(resym_test::_DEVICE_OBJECT*, resym_test::_IRP*);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: resym_core/tests/type_reconstruction.rs
expression: reconstructed_type
---
struct resym_test::NtdllRegression1 { /* Size=0xe8 */
/* 0x0000 */ public: void (* KernelRoutine)(resym_test::_KAPC*, void (**)(void*, void*, void*), void**, void**, void**);
/* 0x0008 */ public: int32_t (* MajorFunction[28])(resym_test::_DEVICE_OBJECT*, resym_test::_IRP*);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: resym_core/tests/type_reconstruction.rs
expression: reconstructed_type
---
struct resym_test::NtdllRegression1 { /* Size=0xe8 */
/* 0x0000 */ public: void (* KernelRoutine)(resym_test::_KAPC*, void (**)(void*, void*, void*), void**, void**, void**);
/* 0x0008 */ public: long (* MajorFunction[28])(resym_test::_DEVICE_OBJECT*, resym_test::_IRP*);
};
1 change: 1 addition & 0 deletions resym_core/tests/type_reconstruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const TEST_CASES: &[&str] = &[
"resym_test::BitFieldsTest6",
"resym_test::BitFieldsTest7",
"resym_test::NestedStructUnionRegression1",
"resym_test::NtdllRegression1",
];

#[test]
Expand Down
Loading