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

fix: Iterate all debug directory entries #740

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/modulefinder/sentry_modulefinder_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,48 @@ extract_pdb_info(uintptr_t module_addr, sentry_value_t module)
nt_headers->OptionalHeader.SizeOfImage);
sentry_value_set_by_key(module, "code_id", sentry_value_new_string(id_buf));

uint32_t relative_addr
= nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG]
.VirtualAddress;
IMAGE_DATA_DIRECTORY debug_entry
= nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];

size_t relative_addr = (size_t)debug_entry.VirtualAddress;
if (!relative_addr) {
return;
}

PIMAGE_DEBUG_DIRECTORY debug_dict
= (PIMAGE_DEBUG_DIRECTORY)(module_addr + relative_addr);
if (!debug_dict || debug_dict->Type != IMAGE_DEBUG_TYPE_CODEVIEW) {
size_t table_size = (size_t)debug_entry.Size;
size_t entry_size = sizeof(IMAGE_DEBUG_DIRECTORY);
if (table_size % entry_size != 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to somehow iterate over some entries and just drop the one that was somehow malformed and has fewer bytes than necessary? Or is it not possible to tell which one would even be broken at this point?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing the same thing as crashpad here is okay. These structs have a fixed size, and the table should always be a multiple of that, otherwise the compiler did something wrong

return;
}

struct CodeViewRecord70 *debug_info
= (struct CodeViewRecord70 *)(module_addr
+ debug_dict->AddressOfRawData);
if (!debug_info || debug_info->signature != CV_SIGNATURE) {
return;
}
for (size_t offset = 0; offset < table_size; offset += entry_size) {
PIMAGE_DEBUG_DIRECTORY debug_dict
= (PIMAGE_DEBUG_DIRECTORY)(module_addr + relative_addr + offset);

if (debug_dict->Type != IMAGE_DEBUG_TYPE_CODEVIEW) {
continue;
}

sentry_value_set_by_key(module, "debug_file",
sentry_value_new_string(debug_info->pdb_filename));
struct CodeViewRecord70 *debug_info
= (struct CodeViewRecord70 *)(module_addr
+ debug_dict->AddressOfRawData);
if (debug_info->signature != CV_SIGNATURE) {
continue;
}

sentry_uuid_t debug_id_base
= sentry__uuid_from_native(&debug_info->pdb_signature);
sentry_uuid_as_string(&debug_id_base, id_buf);
id_buf[36] = '-';
snprintf(id_buf + 37, 10, "%x", debug_info->pdb_age);
sentry_value_set_by_key(
module, "debug_id", sentry_value_new_string(id_buf));
sentry_value_set_by_key(module, "debug_file",
sentry_value_new_string(debug_info->pdb_filename));

sentry_uuid_t debug_id_base
= sentry__uuid_from_native(&debug_info->pdb_signature);
sentry_uuid_as_string(&debug_id_base, id_buf);
id_buf[36] = '-';
snprintf(id_buf + 37, 10, "%x", debug_info->pdb_age);
sentry_value_set_by_key(
module, "debug_id", sentry_value_new_string(id_buf));

return;
}
}

static void
Expand Down