mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-02-19 02:43:33 +00:00
1. Added logging for Windows Cert Store Error cases
2. Added db corruption detection
This commit is contained in:
@@ -70,6 +70,7 @@ limitations under the License.
|
||||
#endif
|
||||
|
||||
#include <Dbghelp.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
@@ -2590,6 +2591,7 @@ ILibExportMethod void ILibChain_EndContinue(void *chain)
|
||||
}
|
||||
|
||||
char* g_ILibCrashID = NULL;
|
||||
char* g_ILibCrashID_HASH = NULL;
|
||||
char* g_ILibCrashDump_path = NULL;
|
||||
|
||||
#if defined(WIN32)
|
||||
@@ -2684,7 +2686,7 @@ void ILib_WindowsExceptionDebugEx(ILib_DumpEnabledContext *dumpEnabledExceptionC
|
||||
psym->MaxNameLen = MAX_SYM_NAME;
|
||||
pimg->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
|
||||
|
||||
len = sprintf_s(buffer, sizeof(buffer), "FATAL EXCEPTION [%s] @ ", (g_ILibCrashID != NULL ? g_ILibCrashID : ""));
|
||||
len = sprintf_s(buffer, sizeof(buffer), "FATAL EXCEPTION @ ");
|
||||
#ifdef WIN64
|
||||
len += sprintf_s(buffer + len, sizeof(buffer) - len, "[FuncAddr: 0x%016llx / BaseAddr: 0x%016llx / Delta: %lld]\n", (unsigned __int64)StackFrame.AddrPC.Offset, (unsigned __int64)&ILibCreateChain, (unsigned __int64)&ILibCreateChain - (unsigned __int64)StackFrame.AddrPC.Offset);
|
||||
#else
|
||||
@@ -2731,18 +2733,6 @@ void ILib_POSIX_CrashHandler(int code)
|
||||
{
|
||||
memcpy_s(msgBuffer + msgLen, sizeof(msgBuffer) - msgLen, "** CRASH **\n", 12);
|
||||
msgLen += 12;
|
||||
if (g_ILibCrashID != NULL)
|
||||
{
|
||||
int idlen = strnlen_s(g_ILibCrashID, 255);
|
||||
memcpy_s(msgBuffer + msgLen, sizeof(msgBuffer) - msgLen, "[", 1);
|
||||
msgLen += 1;
|
||||
|
||||
memcpy_s(msgBuffer + msgLen, sizeof(msgBuffer) - msgLen, g_ILibCrashID, idlen);
|
||||
msgLen += idlen;
|
||||
|
||||
memcpy_s(msgBuffer + msgLen, sizeof(msgBuffer) - msgLen, "]\n", 2);
|
||||
msgLen += 2;
|
||||
}
|
||||
}
|
||||
else if (code == 254)
|
||||
{
|
||||
@@ -9428,6 +9418,36 @@ void ILibGetDiskFreeSpace(void *i64FreeBytesToCaller, void *i64TotalBytes)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int ILibFile_CopyTo(char *source, char *destination)
|
||||
{
|
||||
#ifdef WIN32
|
||||
WCHAR SourceW[4096];
|
||||
WCHAR DestW[4096];
|
||||
|
||||
ILibUTF8ToWideEx(source, -1, SourceW, (int)sizeof(SourceW) / 2);
|
||||
ILibUTF8ToWideEx(destination, -1, DestW, (int)sizeof(DestW) / 2);
|
||||
return(CopyFileW(SourceW, DestW, FALSE) ? 0 : 1);
|
||||
#else
|
||||
FILE *from = fopen(source, "rb");
|
||||
FILE *to = fopen(destination, "wb");
|
||||
size_t bytesRead;
|
||||
int ret = 0;
|
||||
while ((bytesRead = fread(ILibScratchPad, 1, sizeof(ILibScratchPad), from)) > 0)
|
||||
{
|
||||
ret = (fwrite(ILibScratchPad, 1, bytesRead, to) > 0 ? 0 : 1);
|
||||
}
|
||||
|
||||
fclose(to);
|
||||
fclose(from);
|
||||
return(ret);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
int ILibGetMillisecondTimeSpan(struct timeval *tv1, struct timeval *tv2)
|
||||
{
|
||||
struct timeval a;
|
||||
@@ -10692,11 +10712,11 @@ char* ILibCriticalLog (const char* msg, const char* file, int line, int user1, i
|
||||
int len = ILibGetLocalTime((char*)timeStamp, (int)sizeof(timeStamp));
|
||||
if (file != NULL)
|
||||
{
|
||||
len = sprintf_s(ILibCriticalLogBuffer, sizeof(ILibCriticalLogBuffer), "\r\n[%s] %s:%d (%d,%d) %s", timeStamp, file, line, user1, user2, msg);
|
||||
len = sprintf_s(ILibCriticalLogBuffer, sizeof(ILibCriticalLogBuffer), "\r\n[%s] [%s] %s:%d (%d,%d) %s", timeStamp, g_ILibCrashID_HASH !=NULL? g_ILibCrashID_HASH :"", file, line, user1, user2, msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
len = sprintf_s(ILibCriticalLogBuffer, sizeof(ILibCriticalLogBuffer), "\r\n[%s] %s", timeStamp, msg);
|
||||
len = sprintf_s(ILibCriticalLogBuffer, sizeof(ILibCriticalLogBuffer), "\r\n[%s] [%s] %s", timeStamp, g_ILibCrashID_HASH != NULL ? g_ILibCrashID_HASH : "", msg);
|
||||
}
|
||||
if (len > 0 && len < (int)sizeof(ILibCriticalLogBuffer) && ILibCriticalLogFilename != NULL) ILibAppendStringToDiskEx(ILibCriticalLogFilename, ILibCriticalLogBuffer, len);
|
||||
if (file != NULL)
|
||||
|
||||
@@ -930,7 +930,7 @@ int ILibIsRunningOnChainThread(void* chain);
|
||||
void ILibWriteStringToDiskEx(char *FileName, char *data, int dataLen);
|
||||
void ILibDeleteFileFromDisk(char *FileName);
|
||||
void ILibGetDiskFreeSpace(void *i64FreeBytesToCaller, void *i64TotalBytes);
|
||||
|
||||
int ILibFile_CopyTo(char *source, char *destination);
|
||||
|
||||
/*! \defgroup StackGroup Stack
|
||||
\ingroup DataStructures
|
||||
@@ -1572,6 +1572,7 @@ int ILibIsRunningOnChainThread(void* chain);
|
||||
void ILibChain_DebugOffset(char *buffer, int bufferLen, uint64_t addrOffset);
|
||||
char* ILibChain_Debug(void *chain, char* buffer, int bufferLen);
|
||||
extern char* g_ILibCrashID;
|
||||
extern char* g_ILibCrashID_HASH;
|
||||
extern char* g_ILibCrashDump_path;
|
||||
|
||||
#if defined(WIN32)
|
||||
|
||||
@@ -274,12 +274,13 @@ ILibSimpleDataStore_RecordHeader_NG* ILibSimpleDataStore_ReadNextRecord(ILibSimp
|
||||
|
||||
ILibSimpleDataStore_RecordHeader_NG *node;
|
||||
size_t nodeSize;
|
||||
uint64_t currentOffset;
|
||||
|
||||
if (root == NULL) return NULL;
|
||||
node = (ILibSimpleDataStore_RecordHeader_NG*)(root->scratchPad + sizeof(uint64_t));
|
||||
|
||||
// If the current position is the end of the file, exit now.
|
||||
if (ILibSimpleDataStore_GetPosition(root->dataFile) == root->fileSize) return NULL;
|
||||
if ((currentOffset = ILibSimpleDataStore_GetPosition(root->dataFile)) == root->fileSize) return NULL;
|
||||
|
||||
// Read sizeof(ILibSimpleDataStore_RecordNode) bytes to get record Size
|
||||
switch (legacySize)
|
||||
@@ -296,8 +297,11 @@ ILibSimpleDataStore_RecordHeader_NG* ILibSimpleDataStore_ReadNextRecord(ILibSimp
|
||||
}
|
||||
|
||||
i = (int)fread((void*)node, 1, nodeSize, root->dataFile);
|
||||
if (i < (int)nodeSize) return NULL;
|
||||
|
||||
if (i < (int)nodeSize)
|
||||
{
|
||||
ILibSimpleDataStore_SeekPosition(root->dataFile, currentOffset, SEEK_SET);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Correct the struct, valueHash stays the same
|
||||
node->nodeSize = (int)ntohl(node->nodeSize);
|
||||
@@ -308,13 +312,17 @@ ILibSimpleDataStore_RecordHeader_NG* ILibSimpleDataStore_ReadNextRecord(ILibSimp
|
||||
if (node->keyLen > (int)((sizeof(ILibScratchPad) - nodeSize - sizeof(uint64_t))))
|
||||
{
|
||||
// Invalid record
|
||||
ILibSimpleDataStore_SeekPosition(root->dataFile, currentOffset, SEEK_SET);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
// Read the key name
|
||||
i = (int)fread((char*)node + nodeSize, 1, node->keyLen, root->dataFile);
|
||||
if (i != node->keyLen) return NULL; // Reading Key Failed
|
||||
|
||||
if (i != node->keyLen)
|
||||
{
|
||||
ILibSimpleDataStore_SeekPosition(root->dataFile, currentOffset, SEEK_SET);
|
||||
return NULL; // Reading Key Failed
|
||||
}
|
||||
// Validate Data, in 4k chunks at a time
|
||||
bytesLeft = node->valueLength;
|
||||
|
||||
@@ -342,6 +350,7 @@ ILibSimpleDataStore_RecordHeader_NG* ILibSimpleDataStore_ReadNextRecord(ILibSimp
|
||||
}
|
||||
}
|
||||
|
||||
ILibSimpleDataStore_SeekPosition(root->dataFile, currentOffset, SEEK_SET);
|
||||
return NULL; // Data is corrupt
|
||||
}
|
||||
return node;
|
||||
@@ -365,12 +374,14 @@ void ILibSimpleDataStore_RebuildKeyTable(ILibSimpleDataStore_Root *root)
|
||||
ILibSimpleDataStore_RecordHeader_NG *node = NULL;
|
||||
ILibSimpleDataStore_TableEntry *entry;
|
||||
int count;
|
||||
uint64_t newoffset;
|
||||
|
||||
if (root == NULL) return;
|
||||
|
||||
ILibHashtable_ClearEx(root->keyTable, ILibSimpleDataStore_TableClear_Sink, root); // Wipe the key table, we will rebulit it
|
||||
fseek(root->dataFile, 0, SEEK_END); // See the start of the file
|
||||
root->fileSize = ILibSimpleDataStore_GetPosition(root->dataFile);
|
||||
fseek(root->dataFile, 0, SEEK_SET); // See the start of the file
|
||||
root->fileSize = -1; // Indicate we can't write to the data store
|
||||
|
||||
|
||||
// First, try NG Format
|
||||
@@ -475,7 +486,19 @@ void ILibSimpleDataStore_RebuildKeyTable(ILibSimpleDataStore_Root *root)
|
||||
else
|
||||
{
|
||||
// No need to convert db format, because we're already NG format
|
||||
root->fileSize = ILibSimpleDataStore_GetPosition(root->dataFile);
|
||||
if ((newoffset = ILibSimpleDataStore_GetPosition(root->dataFile)) != root->fileSize)
|
||||
{
|
||||
// DB corruption detected
|
||||
ILIBLOGMESSAGEX("DB Corruption Detected");
|
||||
char *dest = ILibString_Replace(root->filePath, strnlen_s(root->filePath, sizeof(ILibScratchPad)), ".db", 3, ".corrupt.db", 11);
|
||||
ILibFile_CopyTo(root->filePath, dest);
|
||||
free(dest);
|
||||
#ifdef WIN32
|
||||
_chsize_s(_fileno(root->dataFile), newoffset);
|
||||
#else
|
||||
ftruncate(fileno(root->dataFile), newoffset);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user