From 8a24fb95b197e59a2e1936e2f09d0296da7948d7 Mon Sep 17 00:00:00 2001 From: gardners Date: Sat, 28 Jan 2012 11:45:45 +1030 Subject: [PATCH] Converted manifest storage to using static set of manifest structures and added manifest leak detection and some other memory corruption protections. --- rhizome.h | 7 +++- rhizome_bundle.c | 93 ++++++++++++++++++++++++++++++++++++++--- rhizome_packetformats.c | 1 + 3 files changed, 95 insertions(+), 6 deletions(-) diff --git a/rhizome.h b/rhizome.h index a581e4f6..163fe097 100644 --- a/rhizome.h +++ b/rhizome.h @@ -94,6 +94,7 @@ typedef struct rhizome_signature { #define MAX_MANIFEST_VARS 256 #define MAX_MANIFEST_BYTES 8192 typedef struct rhizome_manifest { + int manifest_record_number; int manifest_bytes; int manifest_all_bytes; unsigned char manifestdata[MAX_MANIFEST_BYTES]; @@ -170,7 +171,11 @@ long long rhizome_manifest_get_ll(rhizome_manifest *m,char *var); int rhizome_manifest_set_ll(rhizome_manifest *m,char *var,long long value); int rhizome_manifest_set(rhizome_manifest *m,char *var,char *value); long long rhizome_file_size(char *filename); -void rhizome_manifest_free(rhizome_manifest *m); +void _rhizome_manifest_free(const char *sourcefile,const char *funcname,int line, + rhizome_manifest *m); +#define rhizome_manifest_free(m) _rhizome_manifest_free(__FILE__,__FUNCTION__,__LINE__,m) +rhizome_manifest *_rhizome_new_manifest(const char *file,const char *func,int line); +#define rhizome_new_manifest() _rhizome_new_manifest(__FILE__,__FUNCTION__,__LINE__) int rhizome_manifest_pack_variables(rhizome_manifest *m); int rhizome_store_bundle(rhizome_manifest *m,char *associated_filename); int rhizome_manifest_add_group(rhizome_manifest *m,char *groupid); diff --git a/rhizome_bundle.c b/rhizome_bundle.c index 5b03c6b6..0343b264 100644 --- a/rhizome_bundle.c +++ b/rhizome_bundle.c @@ -25,7 +25,7 @@ rhizome_manifest *rhizome_read_manifest_file(char *filename,int bufferP,int flag { if (bufferP>MAX_MANIFEST_BYTES) return NULL; - rhizome_manifest *m = calloc(sizeof(rhizome_manifest),1); + rhizome_manifest *m = rhizome_new_manifest(); if (!m) return NULL; if (bufferP) { @@ -252,15 +252,94 @@ long long rhizome_file_size(char *filename) return size; } -void rhizome_manifest_free(rhizome_manifest *m) +#define MAX_RHIZOME_MANIFESTS 16 +rhizome_manifest manifests[MAX_RHIZOME_MANIFESTS]; +char manifest_free[MAX_RHIZOME_MANIFESTS]; +int manifest_first_free=-1; +const char *manifest_alloc_sourcefiles[MAX_RHIZOME_MANIFESTS]; +const char *manifest_alloc_functions[MAX_RHIZOME_MANIFESTS]; +int manifest_alloc_lines[MAX_RHIZOME_MANIFESTS]; +const char *manifest_free_sourcefiles[MAX_RHIZOME_MANIFESTS]; +const char *manifest_free_functions[MAX_RHIZOME_MANIFESTS]; +int manifest_free_lines[MAX_RHIZOME_MANIFESTS]; + +rhizome_manifest *_rhizome_new_manifest(const char *filename,const char *funcname, + int line) +{ + if (manifest_first_free<0) { + /* Setup structures */ + int i; + for(i=0;i=MAX_RHIZOME_MANIFESTS) + { + int i; + fprintf(stderr,"%s:%d:%s() call to rhizome_new_manifest() could not be serviced.\n (no free manifest records, this probably indicates a memory leak.)\n", + filename,line,funcname); + fprintf(stderr," Manifest Slot# | Last allocated by\n"); + for(i=0;imanifest_record_number=manifest_first_free; + + /* Indicate where manifest was allocated, and that it is no longer + free. */ + manifest_alloc_sourcefiles[manifest_first_free]=filename; + manifest_alloc_lines[manifest_first_free]=line; + manifest_alloc_functions[manifest_first_free]=funcname; + manifest_free[manifest_first_free]=0; + manifest_free_sourcefiles[manifest_first_free]=""; + manifest_free_functions[manifest_first_free]=""; + manifest_free_lines[manifest_first_free]=-1; + + /* Work out where next free manifest record lives */ + for(;manifest_first_freemanifest_record_number; + + if (m!=&manifests[mid]) { + fprintf(stderr,"%s:%d:%s() called rhizome_manifest_free() and asked to free" + " manifest %p, which claims to be manifest slot #%d (%p), but isn't.\n", + sourcefile,line,funcname,m,mid,&manifests[mid]); + exit(-1); + } + + /* Free variable and signature blocks. + XXX These should be moved to malloc-free storage eventually */ for(i=0;ivar_count;i++) { free(m->vars[i]); free(m->values[i]); m->vars[i]=NULL; m->values[i]=NULL; } - for(i=0;isig_count;i++) { free(m->signatories[i]); m->signatories[i]=NULL; @@ -269,7 +348,11 @@ void rhizome_manifest_free(rhizome_manifest *m) if (m->dataFileName) free(m->dataFileName); m->dataFileName=NULL; - free(m); + manifest_free[mid]=1; + manifest_free_sourcefiles[mid]=sourcefile; + manifest_free_functions[mid]=funcname; + manifest_free_lines[mid]=line; + if (mid