diff options
Diffstat (limited to 'src/mongo/base/secure_allocator.cpp')
-rw-r--r-- | src/mongo/base/secure_allocator.cpp | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/mongo/base/secure_allocator.cpp b/src/mongo/base/secure_allocator.cpp index 06a0187d695..47837cb5c7d 100644 --- a/src/mongo/base/secure_allocator.cpp +++ b/src/mongo/base/secure_allocator.cpp @@ -33,6 +33,7 @@ #include "mongo/base/secure_allocator.h" +#include <fmt/format.h> #include <memory> #ifdef _WIN32 @@ -58,6 +59,11 @@ namespace mongo::secure_allocator_details { namespace { +std::string fmtError(StringData prefix) { + auto ec = lastSystemError(); + return format(FMT_STRING("{}: {}"), prefix, errorMessage(ec)); +} + /** * NOTE(jcarey): Why not new/delete? * @@ -74,7 +80,7 @@ namespace { void EnablePrivilege(const wchar_t* name) { LUID luid; if (!LookupPrivilegeValueW(nullptr, name, &luid)) { - auto str = errnoWithPrefix("Failed to LookupPrivilegeValue"); + auto str = fmtError("Failed to LookupPrivilegeValue"); LOGV2_WARNING(23704, "{str}", "str"_attr = str); return; } @@ -82,7 +88,7 @@ void EnablePrivilege(const wchar_t* name) { // Get the access token for the current process. HANDLE accessToken; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &accessToken)) { - auto str = errnoWithPrefix("Failed to OpenProcessToken"); + auto str = fmtError("Failed to OpenProcessToken"); LOGV2_WARNING(23705, "{str}", "str"_attr = str); return; } @@ -97,7 +103,7 @@ void EnablePrivilege(const wchar_t* name) { if (!AdjustTokenPrivileges( accessToken, false, &privileges, sizeof(privileges), nullptr, nullptr)) { - auto str = errnoWithPrefix("Failed to AdjustTokenPrivileges"); + auto str = fmtError("Failed to AdjustTokenPrivileges"); LOGV2_WARNING(23706, "{str}", "str"_attr = str); } @@ -133,7 +139,7 @@ void growWorkingSize(std::size_t bytes) { stdx::lock_guard<stdx::mutex> lock(workingSizeMutex); if (!GetProcessWorkingSetSize(GetCurrentProcess(), &minWorkingSetSize, &maxWorkingSetSize)) { - auto str = errnoWithPrefix("Failed to GetProcessWorkingSetSize"); + auto str = fmtError("Failed to GetProcessWorkingSetSize"); LOGV2_FATAL(40285, "{str}", "str"_attr = str); } @@ -147,7 +153,7 @@ void growWorkingSize(std::size_t bytes) { maxWorkingSetSize, QUOTA_LIMITS_HARDWS_MIN_ENABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE)) { - auto str = errnoWithPrefix("Failed to SetProcessWorkingSetSizeEx"); + auto str = fmtError("Failed to SetProcessWorkingSetSizeEx"); LOGV2_FATAL(40286, "{str}", "str"_attr = str); } } @@ -166,7 +172,7 @@ void* systemAllocate(std::size_t bytes) { auto ptr = VirtualAlloc(nullptr, bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (!ptr) { - auto str = errnoWithPrefix("Failed to VirtualAlloc"); + auto str = fmtError("Failed to VirtualAlloc"); LOGV2_FATAL(28835, "{str}", "str"_attr = str); } @@ -182,7 +188,7 @@ void* systemAllocate(std::size_t bytes) { } } - auto str = errnoWithPrefix("Failed to VirtualLock"); + auto str = fmtError("Failed to VirtualLock"); LOGV2_FATAL(28828, "{str}", "str"_attr = str); } @@ -191,14 +197,14 @@ void* systemAllocate(std::size_t bytes) { void systemDeallocate(void* ptr, std::size_t bytes) { if (VirtualUnlock(ptr, bytes) == 0) { - auto str = errnoWithPrefix("Failed to VirtualUnlock"); + auto str = fmtError("Failed to VirtualUnlock"); LOGV2_FATAL(28829, "{str}", "str"_attr = str); } // VirtualFree needs to take 0 as the size parameter for MEM_RELEASE // (that's how the api works). if (VirtualFree(ptr, 0, MEM_RELEASE) == 0) { - auto str = errnoWithPrefix("Failed to VirtualFree"); + auto str = fmtError("Failed to VirtualFree"); LOGV2_FATAL(28830, "{str}", "str"_attr = str); } } @@ -237,14 +243,14 @@ void* systemAllocate(std::size_t bytes) { mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MONGO_MAP_ANONYMOUS, -1, 0); if (!ptr) { - auto str = errnoWithPrefix("Failed to mmap"); + auto str = fmtError("Failed to mmap"); LOGV2_FATAL(23714, "{str}", "str"_attr = str); fassertFailed(28831); } if (mlock(ptr, bytes) != 0) { const int err = errno; - auto str = errnoWithPrefix( + auto str = fmtError( "Failed to mlock: Cannot allocate locked memory. For more details see: " "https://dochub.mongodb.org/core/cannot-allocate-locked-memory"); LOGV2_FATAL(23715, "{str}", "str"_attr = str, "errno"_attr = err); @@ -267,16 +273,17 @@ void systemDeallocate(void* ptr, std::size_t bytes) { #endif if (munlock(ptr, bytes) != 0) { + auto str = fmtError("Failed to munlock"); LOGV2_FATAL(28833, "{errnoWithPrefix_Failed_to_munlock}", - "errnoWithPrefix_Failed_to_munlock"_attr = - errnoWithPrefix("Failed to munlock")); + "errnoWithPrefix_Failed_to_munlock"_attr = str); } if (munmap(ptr, bytes) != 0) { + auto str = fmtError("Failed to munmap"); LOGV2_FATAL(28834, "{errnoWithPrefix_Failed_to_munmap}", - "errnoWithPrefix_Failed_to_munmap"_attr = errnoWithPrefix("Failed to munmap")); + "errnoWithPrefix_Failed_to_munmap"_attr = str); } } |