diff --git a/src/Log.c b/src/Log.c index b37870d..6a441e0 100644 --- a/src/Log.c +++ b/src/Log.c @@ -124,7 +124,6 @@ LogConfigFree(LogConfig * config) } pthread_mutex_destroy(&config->lock); - Free(config->tsFmt); Free(config); if (config == globalConfig) diff --git a/src/Memory.c b/src/Memory.c index e3ad8d7..706dc87 100644 --- a/src/Memory.c +++ b/src/Memory.c @@ -27,6 +27,9 @@ #include #include #include +#include + +#include #include #include @@ -154,7 +157,7 @@ MemoryDelete(MemoryInfo * a) } static int -MemoryCheck(MemoryInfo *a) +MemoryCheck(MemoryInfo * a) { if (MEM_BOUND_LOWER(a->pointer) != MEM_BOUND || MEM_BOUND_UPPER(a->pointer, a->size - (2 * sizeof(MEM_BOUND_TYPE))) != MEM_BOUND) @@ -458,6 +461,64 @@ void pthread_mutex_unlock(&lock); } +static size_t +HexPtr(unsigned long ptr, char *out, size_t len) +{ + size_t i = len - 1; + size_t j = 0; + + do + { + out[i] = "0123456789abcdef"[ptr % 16]; + i--; + ptr /= 16; + } while (ptr > 0); + + while (++i < len) + { + out[j++] = out[i]; + } + + out[j] = '\0'; + + return j; +} + +void +MemoryDefaultHook(MemoryAction a, MemoryInfo * i, void *args) +{ + char buf[64]; + unsigned long ptr = (unsigned long) MemoryInfoGetPointer(i); + + size_t len = HexPtr(ptr, buf, sizeof(buf)); + + (void) args; + + switch (a) + { + case MEMORY_BAD_POINTER: + write(STDERR_FILENO, "Bad pointer: 0x", 15); + break; + case MEMORY_CORRUPTED: + write(STDERR_FILENO, "Corrupted block: 0x", 19); + break; + default: + return; + } + + write(STDERR_FILENO, buf, len); + write(STDERR_FILENO, " to 0x", 6); + len = HexPtr(MemoryInfoGetSize(i), buf, sizeof(buf)); + write(STDERR_FILENO, buf, len); + write(STDERR_FILENO, " bytes at ", 10); + write(STDERR_FILENO, MemoryInfoGetFile(i), strlen(MemoryInfoGetFile(i))); + write(STDERR_FILENO, ":0x", 3); + len = HexPtr(MemoryInfoGetLine(i), buf, sizeof(buf)); + write(STDERR_FILENO, buf, len); + write(STDERR_FILENO, "\n", 1); + raise(SIGSEGV); +} + void MemoryHexDump(MemoryInfo * info, void (*printFunc) (size_t, char *, char *, void *), void *args) { diff --git a/src/RtStub.c b/src/RtStub.c index f4497a8..f734df8 100644 --- a/src/RtStub.c +++ b/src/RtStub.c @@ -70,6 +70,8 @@ main(int argc, char **argv) MainArgs args; + MemoryHook(MemoryDefaultHook, NULL); + args.args = NULL; args.env = NULL; diff --git a/src/include/Memory.h b/src/include/Memory.h index 035358f..5d5ecec 100644 --- a/src/include/Memory.h +++ b/src/include/Memory.h @@ -199,6 +199,15 @@ extern void MemoryIterate(void (*) (MemoryInfo *, void *), void *); */ extern void MemoryHook(void (*) (MemoryAction, MemoryInfo *, void *), void *); +/** + * The default memory hook, which has sane behavior and is installed + * at runtime. This function does not use any memory on the heap, + * except for the MemoryInfo passed to it, which it assumes to be + * valid. Everything else happens on the stack only, to ensure that + * the hook doesn't make any memory problems worse. + */ +extern void MemoryDefaultHook(MemoryAction, MemoryInfo *, void *); + /** * Read over the block of memory represented by the given memory info * structure and generate a hexadecimal and ASCII string for each