diff options
Diffstat (limited to '')
-rw-r--r-- | include/pdb/dbghelp.hpp | 4 | ||||
-rw-r--r-- | include/pdb/symbol.hpp | 21 | ||||
-rw-r--r-- | src/dbghelp.cpp | 98 | ||||
-rw-r--r-- | src/module.cpp | 2 |
4 files changed, 58 insertions, 67 deletions
diff --git a/include/pdb/dbghelp.hpp b/include/pdb/dbghelp.hpp index 7b018bc..d6b6899 100644 --- a/include/pdb/dbghelp.hpp +++ b/include/pdb/dbghelp.hpp @@ -22,6 +22,8 @@ namespace pdb DbgHelp(); ~DbgHelp(); + void close(); + ModuleInfo load_pdb(const std::string& path) const; typedef std::function<void (const SymbolInfo&)> OnSymbol; @@ -30,8 +32,6 @@ namespace pdb SymbolInfo resolve_symbol(Address) const; SymbolInfo resolve_symbol(const std::string&) const; - void close(); - private: ModuleInfo get_module_info(Address offline_base) const; diff --git a/include/pdb/symbol.hpp b/include/pdb/symbol.hpp index 12061bd..fc200c7 100644 --- a/include/pdb/symbol.hpp +++ b/include/pdb/symbol.hpp @@ -37,8 +37,11 @@ namespace pdb : SymbolInfo{} { if (raw.SizeOfStruct != sizeof(raw)) - throw std::runtime_error{"unexpected symbol structure size"}; - std::memcpy(buffer, &raw, calc_size(raw)); + throw std::runtime_error{"invalid SYMBOL_INFO.SizeOfStruct"}; + const auto raw_size = calc_size(raw); + if (raw_size > sizeof(buffer)) + throw std::runtime_error{"SYMBOL_INFO is too large"}; + std::memcpy(buffer, &raw, raw_size); } explicit operator Raw&() { return raw; } @@ -73,22 +76,22 @@ namespace pdb bool is_function() const { return get_type() == Type::Function; } private: + static constexpr std::size_t max_buffer_size = sizeof(Raw) + MAX_SYM_NAME - 1; + static std::size_t calc_size(const Raw& raw) { + using namespace msl::utilities; try { - msl::utilities::SafeInt<std::size_t> size{raw.SizeOfStruct}; - size += raw.NameLen; - size -= 1; - return size; + return SafeInt<std::size_t>{raw.SizeOfStruct} + raw.NameLen - 1; } - catch (const msl::utilities::SafeIntException&) + catch (const SafeIntException&) { - throw std::runtime_error{"symbol name is too long"}; + throw std::runtime_error{"invalid SYMBOL_INFO size"}; } } - unsigned char buffer[sizeof(Raw) + MAX_SYM_NAME - 1] = {0}; + unsigned char buffer[max_buffer_size] = {0}; Address displacement = 0; protected: diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index 7d94f8b..30829d4 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -12,7 +12,6 @@ #include <cstddef> -#include <limits> #include <stdexcept> #include <string> @@ -28,15 +27,14 @@ namespace pdb void initialize(HANDLE id) { enable_debug_output(); - const auto ret = SymInitialize(id, NULL, FALSE); - if (!ret) + + if (!SymInitialize(id, NULL, FALSE)) throw error::windows(GetLastError()); } void clean_up(HANDLE id) { - const auto ret = SymCleanup(id); - if (!ret) + if (!SymCleanup(id)) throw error::windows(GetLastError()); } @@ -45,10 +43,8 @@ namespace pdb Address gen_next_offline_base(std::size_t pdb_size) { const auto base = next_offline_base; - if (!msl::utilities::SafeAdd( - next_offline_base, - pdb_size, - next_offline_base)) + using msl::utilities::SafeAdd; + if (!SafeAdd(next_offline_base, pdb_size, next_offline_base)) throw std::runtime_error{"no more PDB files can be added, the internal address space is exhausted"}; return base; } @@ -70,6 +66,25 @@ namespace pdb initialize(id); } + DbgHelp::~DbgHelp() + { + try + { + close(); + } + catch (...) + { } + } + + void DbgHelp::close() + { + if (!closed) + { + clean_up(id); + closed = true; + } + } + ModuleInfo DbgHelp::load_pdb(const std::string& path) const { DWORD size = 0; @@ -94,12 +109,10 @@ namespace pdb { ModuleInfo info; - const auto ret = SymGetModuleInfo64( - id, - offline_base, - &static_cast<ModuleInfo::Raw&>(info)); - - if (!ret) + if (!SymGetModuleInfo64( + id, + offline_base, + &static_cast<ModuleInfo::Raw&>(info))) throw error::windows(GetLastError()); return info; @@ -107,14 +120,12 @@ namespace pdb void DbgHelp::enum_symbols(const ModuleInfo& module, const OnSymbol& callback) const { - const auto ret = SymEnumSymbols( - id, - module.get_offline_base(), - NULL, - &enum_symbols_callback, - const_cast<OnSymbol*>(&callback)); - - if (!ret) + if (!SymEnumSymbols( + id, + module.get_offline_base(), + NULL, + &enum_symbols_callback, + const_cast<OnSymbol*>(&callback))) throw error::windows(GetLastError()); } @@ -123,13 +134,11 @@ namespace pdb Address displacement = 0; SymbolInfo symbol; - const auto ret = SymFromAddr( - id, - online, - &displacement, - &static_cast<SYMBOL_INFO&>(symbol)); - - if (!ret) + if (!SymFromAddr( + id, + online, + &displacement, + &static_cast<SYMBOL_INFO&>(symbol))) throw error::windows(GetLastError()); symbol.set_displacement(displacement); @@ -140,33 +149,12 @@ namespace pdb { SymbolInfo symbol; - const auto ret = SymFromName( - id, - name.c_str(), - &static_cast<SYMBOL_INFO&>(symbol)); - - if (!ret) + if (!SymFromName( + id, + name.c_str(), + &static_cast<SYMBOL_INFO&>(symbol))) throw error::windows(GetLastError()); return symbol; } - - void DbgHelp::close() - { - if (!closed) - { - clean_up(id); - closed = true; - } - } - - DbgHelp::~DbgHelp() - { - try - { - close(); - } - catch (...) - { } - } } diff --git a/src/module.cpp b/src/module.cpp index 3694e5f..2c1d26c 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -23,7 +23,7 @@ namespace pdb : raw{raw} { if (raw.SizeOfStruct != sizeof(raw)) - throw std::runtime_error{"unexpected module structure size"}; + throw std::runtime_error{"invalid IMAGEHLP_MODULE64.SizeOfStruct"}; } ModuleInfo::Raw ModuleInfo::create_raw() |