| 1 | /**************************************************************************/ |
| 2 | /* windows_utils.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "windows_utils.h" |
| 32 | |
| 33 | #ifdef WINDOWS_ENABLED |
| 34 | |
| 35 | #include "core/error/error_macros.h" |
| 36 | #include "core/io/dir_access.h" |
| 37 | #include "core/io/file_access.h" |
| 38 | |
| 39 | #define WIN32_LEAN_AND_MEAN |
| 40 | #include <windows.h> |
| 41 | #undef FAILED // Overrides Error::FAILED |
| 42 | |
| 43 | // dbghelp is linked only in DEBUG_ENABLED builds. |
| 44 | #ifdef DEBUG_ENABLED |
| 45 | #include <dbghelp.h> |
| 46 | #endif |
| 47 | #include <winnt.h> |
| 48 | |
| 49 | HashMap<String, Vector<String>> WindowsUtils::temp_pdbs; |
| 50 | |
| 51 | Error WindowsUtils::copy_and_rename_pdb(const String &p_dll_path) { |
| 52 | #ifdef DEBUG_ENABLED |
| 53 | // 1000 ought to be enough for anybody, in case the debugger does not unblock previous PDBs. |
| 54 | // Usually no more than 2 will be used. |
| 55 | const int max_pdb_names = 1000; |
| 56 | |
| 57 | struct PDBResourceInfo { |
| 58 | uint32_t address = 0; |
| 59 | String path; |
| 60 | } pdb_info; |
| 61 | |
| 62 | // Open and read the PDB information if available. |
| 63 | { |
| 64 | ULONG dbg_info_size = 0; |
| 65 | DWORD dbg_info_position = 0; |
| 66 | |
| 67 | { |
| 68 | // The custom LoadLibraryExW is used instead of open_dynamic_library |
| 69 | // to avoid loading the original PDB into the debugger. |
| 70 | HMODULE library_ptr = LoadLibraryExW((LPCWSTR)(p_dll_path.utf16().get_data()), nullptr, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE); |
| 71 | |
| 72 | ERR_FAIL_NULL_V_MSG(library_ptr, ERR_FILE_CANT_OPEN, vformat("Failed to load library '%s'.", p_dll_path)); |
| 73 | |
| 74 | IMAGE_DEBUG_DIRECTORY *dbg_dir = (IMAGE_DEBUG_DIRECTORY *)ImageDirectoryEntryToDataEx(library_ptr, FALSE, IMAGE_DIRECTORY_ENTRY_DEBUG, &dbg_info_size, nullptr); |
| 75 | |
| 76 | bool has_debug = dbg_dir && dbg_dir->Type == IMAGE_DEBUG_TYPE_CODEVIEW; |
| 77 | if (has_debug) { |
| 78 | dbg_info_position = dbg_dir->PointerToRawData; |
| 79 | dbg_info_size = dbg_dir->SizeOfData; |
| 80 | } |
| 81 | |
| 82 | ERR_FAIL_COND_V_MSG(!FreeLibrary((HMODULE)library_ptr), FAILED, vformat("Failed to free library '%s'.", p_dll_path)); |
| 83 | |
| 84 | if (!has_debug) { |
| 85 | // Skip with no debugging symbols. |
| 86 | return ERR_SKIP; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | struct CV_HEADER { |
| 91 | DWORD Signature; |
| 92 | DWORD Offset; |
| 93 | }; |
| 94 | |
| 95 | const DWORD nb10_magic = 0x3031424e; // "01BN" (little-endian) |
| 96 | struct CV_INFO_PDB20 { |
| 97 | CV_HEADER CvHeader; // CvHeader.Signature = "NB10" |
| 98 | DWORD Signature; |
| 99 | DWORD Age; |
| 100 | BYTE PdbFileName[1]; |
| 101 | }; |
| 102 | |
| 103 | const DWORD rsds_magic = 0x53445352; // "SDSR" (little-endian) |
| 104 | struct CV_INFO_PDB70 { |
| 105 | DWORD Signature; // "RSDS" |
| 106 | BYTE Guid[16]; |
| 107 | DWORD Age; |
| 108 | BYTE PdbFileName[1]; |
| 109 | }; |
| 110 | |
| 111 | Vector<uint8_t> dll_data; |
| 112 | |
| 113 | { |
| 114 | Error err = OK; |
| 115 | Ref<FileAccess> file = FileAccess::open(p_dll_path, FileAccess::READ, &err); |
| 116 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to read library '%s'.", p_dll_path)); |
| 117 | |
| 118 | file->seek(dbg_info_position); |
| 119 | dll_data = file->get_buffer(dbg_info_size); |
| 120 | ERR_FAIL_COND_V_MSG(file->get_error() != OK, file->get_error(), vformat("Failed to read data from library '%s'.", p_dll_path)); |
| 121 | } |
| 122 | |
| 123 | const char *raw_pdb_path = nullptr; |
| 124 | int raw_pdb_offset = 0; |
| 125 | DWORD *pdb_info_signature = (DWORD *)dll_data.ptr(); |
| 126 | |
| 127 | if (*pdb_info_signature == rsds_magic) { |
| 128 | raw_pdb_path = (const char *)(((CV_INFO_PDB70 *)pdb_info_signature)->PdbFileName); |
| 129 | raw_pdb_offset = offsetof(CV_INFO_PDB70, PdbFileName); |
| 130 | } else if (*pdb_info_signature == nb10_magic) { |
| 131 | // Not even sure if this format still exists anywhere... |
| 132 | raw_pdb_path = (const char *)(((CV_INFO_PDB20 *)pdb_info_signature)->PdbFileName); |
| 133 | raw_pdb_offset = offsetof(CV_INFO_PDB20, PdbFileName); |
| 134 | } else { |
| 135 | ERR_FAIL_V_MSG(FAILED, vformat("Unknown PDB format in '%s'.", p_dll_path)); |
| 136 | } |
| 137 | |
| 138 | String utf_path; |
| 139 | Error err = utf_path.append_utf8(raw_pdb_path); |
| 140 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to read PDB path from '%s'.", p_dll_path)); |
| 141 | |
| 142 | pdb_info.path = utf_path; |
| 143 | pdb_info.address = dbg_info_position + raw_pdb_offset; |
| 144 | } |
| 145 | |
| 146 | String dll_base_dir = p_dll_path.get_base_dir(); |
| 147 | String copy_pdb_path = pdb_info.path; |
| 148 | |
| 149 | // Attempting to find the PDB by absolute and relative paths. |
| 150 | if (copy_pdb_path.is_relative_path()) { |
| 151 | copy_pdb_path = dll_base_dir.path_join(copy_pdb_path); |
| 152 | if (!FileAccess::exists(copy_pdb_path)) { |
| 153 | copy_pdb_path = dll_base_dir.path_join(copy_pdb_path.get_file()); |
| 154 | } |
| 155 | } else if (!FileAccess::exists(copy_pdb_path)) { |
| 156 | copy_pdb_path = dll_base_dir.path_join(copy_pdb_path.get_file()); |
| 157 | } |
| 158 | if (!FileAccess::exists(copy_pdb_path)) { |
| 159 | // The PDB file may be distributed separately on purpose, so we don't consider this an error. |
| 160 | WARN_VERBOSE(vformat("PDB file '%s' for library '%s' was not found, skipping copy/rename.", copy_pdb_path, p_dll_path)); |
| 161 | return ERR_SKIP; |
| 162 | } |
| 163 | |
| 164 | String new_pdb_base_name = p_dll_path.get_file().get_basename() + "_"; |
| 165 | |
| 166 | // Checking the available space for the updated string |
| 167 | // and trying to shorten it if there is not much space. |
| 168 | { |
| 169 | // e.g. 999.pdb |
| 170 | const uint8_t suffix_size = String::num_characters((int64_t)max_pdb_names - 1) + 4; |
| 171 | // e.g. ~lib_ + 1 for the \0 |
| 172 | const uint8_t min_base_size = 5 + 1; |
| 173 | int original_path_size = pdb_info.path.utf8().length(); |
| 174 | CharString utf8_name = new_pdb_base_name.utf8(); |
| 175 | int new_expected_buffer_size = utf8_name.length() + suffix_size; |
| 176 | |
| 177 | // Since we have limited space inside the DLL to patch the path to the PDB, |
| 178 | // it is necessary to limit the size based on the number of bytes occupied by the string. |
| 179 | if (new_expected_buffer_size > original_path_size) { |
| 180 | ERR_FAIL_COND_V_MSG(original_path_size < min_base_size + suffix_size, FAILED, vformat("The original PDB path size in bytes is too small: '%s'. Expected size: %d or more bytes, but available %d.", pdb_info.path, min_base_size + suffix_size, original_path_size)); |
| 181 | |
| 182 | new_pdb_base_name.clear(); |
| 183 | new_pdb_base_name.append_utf8(utf8_name.get_data(), original_path_size - suffix_size); |
| 184 | new_pdb_base_name[new_pdb_base_name.length() - 1] = '_'; // Restore the last '_' |
| 185 | WARN_PRINT(vformat("The original path size of '%s' in bytes was too small to fit the new name, so it was shortened to '%s%d.pdb'.", pdb_info.path, new_pdb_base_name, max_pdb_names - 1)); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Delete old PDB files. |
| 190 | for (const String &file : DirAccess::get_files_at(dll_base_dir)) { |
| 191 | if (file.begins_with(new_pdb_base_name) && file.ends_with(".pdb")) { |
| 192 | String path = dll_base_dir.path_join(file); |
| 193 | |
| 194 | // Just try to delete without showing any errors. |
| 195 | Error err = DirAccess::remove_absolute(path); |
| 196 | if (err == OK && temp_pdbs[p_dll_path].has(path)) { |
| 197 | temp_pdbs[p_dll_path].erase(path); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Try to copy PDB with new name and patch DLL. |
| 203 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 204 | for (int i = 0; i < max_pdb_names; i++) { |
| 205 | String new_pdb_name = vformat("%s%d.pdb", new_pdb_base_name, i); |
| 206 | String new_pdb_path = dll_base_dir.path_join(new_pdb_name); |
| 207 | Error err = OK; |
| 208 | |
| 209 | Ref<FileAccess> test_pdb_is_locked = FileAccess::open(new_pdb_path, FileAccess::READ_WRITE, &err); |
| 210 | if (err == ERR_FILE_CANT_OPEN) { |
| 211 | // If the file is blocked, continue searching. |
| 212 | continue; |
| 213 | } else if (err != OK && err != ERR_FILE_NOT_FOUND) { |
| 214 | ERR_FAIL_V_MSG(err, vformat("Failed to open '%s' to check if it is locked.", new_pdb_path)); |
| 215 | } |
| 216 | |
| 217 | err = d->copy(copy_pdb_path, new_pdb_path); |
| 218 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to copy PDB from '%s' to '%s'.", copy_pdb_path, new_pdb_path)); |
| 219 | temp_pdbs[p_dll_path].append(new_pdb_path); |
| 220 | |
| 221 | Ref<FileAccess> file = FileAccess::open(p_dll_path, FileAccess::READ_WRITE, &err); |
| 222 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to open '%s' to patch the PDB path.", p_dll_path)); |
| 223 | |
| 224 | int original_path_size = pdb_info.path.utf8().length(); |
| 225 | // Double-check file bounds. |
| 226 | ERR_FAIL_UNSIGNED_INDEX_V_MSG(pdb_info.address + original_path_size, file->get_length(), FAILED, vformat("Failed to write a new PDB path. Probably '%s' has been changed.", p_dll_path)); |
| 227 | |
| 228 | Vector<uint8_t> u8 = new_pdb_name.to_utf8_buffer(); |
| 229 | file->seek(pdb_info.address); |
| 230 | file->store_buffer(u8); |
| 231 | |
| 232 | // Terminate string and fill the remaining part of the original string with the '\0'. |
| 233 | Vector<uint8_t> padding_buffer; |
| 234 | padding_buffer.resize_initialized((int64_t)original_path_size - u8.size()); |
| 235 | file->store_buffer(padding_buffer); |
| 236 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Failed to write a new PDB path to '%s'.", p_dll_path)); |
| 237 | |
| 238 | return OK; |
| 239 | } |
| 240 | |
| 241 | ERR_FAIL_V_MSG(FAILED, vformat("Failed to find an unblocked PDB name for '%s' among %d files.", p_dll_path, max_pdb_names)); |
| 242 | #else |
| 243 | WARN_PRINT_ONCE("Renaming PDB files is only available in debug builds. If your libraries use PDB files, then the original ones will be used."); |
| 244 | return ERR_SKIP; |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | void WindowsUtils::remove_temp_pdbs(const String &p_dll_path) { |
| 249 | #ifdef DEBUG_ENABLED |
| 250 | if (temp_pdbs.has(p_dll_path)) { |
| 251 | Vector<String> removed; |
| 252 | int failed = 0; |
| 253 | const int failed_limit = 10; |
| 254 | for (const String &pdb : temp_pdbs[p_dll_path]) { |
| 255 | if (FileAccess::exists(pdb)) { |
| 256 | Error err = DirAccess::remove_absolute(pdb); |
| 257 | if (err == OK) { |
| 258 | removed.append(pdb); |
| 259 | } else { |
| 260 | failed++; |
| 261 | if (failed <= failed_limit) { |
| 262 | print_verbose("Failed to remove temp PDB: " + pdb); |
| 263 | } |
| 264 | } |
| 265 | } else { |
| 266 | removed.append(pdb); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (failed > failed_limit) { |
| 271 | print_verbose(vformat("And %d more PDB files could not be removed....", failed - failed_limit)); |
| 272 | } |
| 273 | |
| 274 | for (const String &pdb : removed) { |
| 275 | temp_pdbs[p_dll_path].erase(pdb); |
| 276 | } |
| 277 | } |
| 278 | #endif |
| 279 | } |
| 280 | |
| 281 | #endif // WINDOWS_ENABLED |