Add sane default memory hook to Cytoplasm, fix more leaks in json.

This commit is contained in:
Jordan Bancino 2023-05-23 22:43:37 +00:00
parent 4b85d10983
commit 061d9df739
4 changed files with 73 additions and 2 deletions

View file

@ -124,7 +124,6 @@ LogConfigFree(LogConfig * config)
} }
pthread_mutex_destroy(&config->lock); pthread_mutex_destroy(&config->lock);
Free(config->tsFmt);
Free(config); Free(config);
if (config == globalConfig) if (config == globalConfig)

View file

@ -27,6 +27,9 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include <Int.h> #include <Int.h>
@ -458,6 +461,64 @@ void
pthread_mutex_unlock(&lock); 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 void
MemoryHexDump(MemoryInfo * info, void (*printFunc) (size_t, char *, char *, void *), void *args) MemoryHexDump(MemoryInfo * info, void (*printFunc) (size_t, char *, char *, void *), void *args)
{ {

View file

@ -70,6 +70,8 @@ main(int argc, char **argv)
MainArgs args; MainArgs args;
MemoryHook(MemoryDefaultHook, NULL);
args.args = NULL; args.args = NULL;
args.env = NULL; args.env = NULL;

View file

@ -199,6 +199,15 @@ extern void MemoryIterate(void (*) (MemoryInfo *, void *), void *);
*/ */
extern void MemoryHook(void (*) (MemoryAction, 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 * Read over the block of memory represented by the given memory info
* structure and generate a hexadecimal and ASCII string for each * structure and generate a hexadecimal and ASCII string for each