From 0f0ff9e1b91b64d2fe5d35a8f7f30f427905f66e Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 13 Jul 2026 17:11:20 +0200 Subject: Fix function loader, it used to load function name that partially matches Add checks in open_pdb --- src/pdb.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/pdb.c') diff --git a/src/pdb.c b/src/pdb.c index 91c7ddb..23f69fa 100644 --- a/src/pdb.c +++ b/src/pdb.c @@ -181,6 +181,12 @@ // ====== !windows.h ====== // ======================== +#if defined(_MSC_VER) && !defined(__clang__) + #define alloca +#elif defined(__clang__) + #define alloca __builtin_alloca +#endif + enum STREAM_INDEX { OLD_DIRECTORY, PDB, @@ -193,13 +199,16 @@ extern u8* string_stack_base; extern u8* string_stack_current; void open_pdb(const i8* pdb_path, struct module* module) { - void* pdb_file = CreateFileA(pdb_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + void* pdb_file = CreateFileA(pdb_path, GENERIC_READ | FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (pdb_file == INVALID_HANDLE_VALUE) { + if (pdb_file == NULL || pdb_file == INVALID_HANDLE_VALUE) { print("Cannot open pdb file at path %s due to error %u\n", pdb_path, GetLastError()); return; } + LARGE_INTEGER file_size = { 0 }; + assert(SUCCEEDED(GetFileSizeEx(pdb_file, &file_size))); + // MSF superblock struct msf_superblock { u8 magic[32U]; @@ -211,14 +220,19 @@ void open_pdb(const i8* pdb_path, struct module* module) { u32 block_map_addr; }; struct msf_superblock superblock = { 0U }; - u8 block[4096U] = { 0U }; u32 b_read = 0U; ReadFile(pdb_file, (void*)&superblock, sizeof(superblock), &b_read, NULL); + + assert(b_read == sizeof(superblock)); + assert(((i64)superblock.num_blocks) * superblock.block_size == file_size.QuadPart); + u8* block = alloca(superblock.block_size); + // Stream directory u32 dir_addr = (superblock.block_map_addr - 1U) * sizeof(block); SetFilePointer(pdb_file, dir_addr, NULL, FILE_BEGIN); ReadFile(pdb_file, (void*)block, superblock.num_directory_bytes, &b_read, NULL); + assert(b_read == superblock.num_directory_bytes); // Loog through streams u32 num_streams = ((u32*)block)[0U]; -- cgit v1.3