void __mhandle_check_ptr(void *ptr) { struct mhandle_item_t *item; void *eff_ptr; unsigned long eff_size; /* initialization */ if (!ptr) return; mhandle_init(); /* Read item */ item = mhandle_hash_table_get(ptr); if (!item) { fprintf(stderr, "\n%s: invalid pointer %p\n", __FUNCTION__, ptr); abort(); } /* Check corruption */ if (item->corrupt_info) { eff_ptr = ptr - MHANDLE_CORRUPT_RANGE; eff_size = item->size + MHANDLE_CORRUPT_TOTAL; mhandle_check_corrupt(eff_ptr, eff_size, item->at); } /* Message */ fprintf(stderr, "libmhandle: pointer %p is safe\n", ptr); }
void mhandle_free(void *ptr, char *at) { struct mhandle_item_t *item; void *eff_ptr; unsigned long eff_size; /* initialization */ if (!ptr) return; mhandle_init(); /* Read item */ item = mhandle_hash_table_get(ptr); if (!item) { fprintf(stderr, "\n%s: free: invalid pointer %p\n", at, ptr); abort(); } /* Check corruption */ if (item->corrupt_info) { eff_ptr = ptr - MHANDLE_CORRUPT_RANGE; eff_size = item->size + MHANDLE_CORRUPT_TOTAL; mhandle_check_corrupt(eff_ptr, eff_size, item->at); memset(eff_ptr, 0, eff_size); free(eff_ptr); } else { memset(ptr, 0, item->size); free(ptr); } /* Remove pointer from data base */ mhandle_hash_table_remove(ptr, at); }
void *mhandle_realloc(void *ptr, unsigned long size, char *at) { void *eff_ptr; unsigned long eff_size; struct mhandle_item_t *item; /* Equivalent to malloc for NULL pointer */ if (!ptr) return mhandle_malloc(size, at); /* Equivalent to free for size zero */ if (!size) { mhandle_free(ptr, at); return NULL; } /* Reallocate */ mhandle_init(); /* Search pointer */ item = mhandle_hash_table_get(ptr); if (!item) { fprintf(stderr, "\n%s: realloc: invalid pointer %p\n", at, ptr); abort(); } /* Reallocation not supported for pointers registered externally, i.e., * those that don't have corruption information. */ if (!item->corrupt_info) { fprintf(stderr, "\n%s: realloc: not supported for pointers not allocated\n" "\twith malloc/calloc/realloc/strdup (%p)", at, ptr); abort(); } /* Effective sizes */ eff_ptr = ptr - MHANDLE_CORRUPT_RANGE; eff_size = item->size + MHANDLE_CORRUPT_TOTAL; /* Check corruption and remove old pointer */ mhandle_check_corrupt(eff_ptr, eff_size, at); mhandle_hash_table_remove(ptr, at); /* Reallocate */ eff_size = size + MHANDLE_CORRUPT_TOTAL; eff_ptr = realloc(eff_ptr, eff_size); if (!eff_ptr) mhandle_out_of_memory(at); /* Mark corruption */ ptr = eff_ptr + MHANDLE_CORRUPT_RANGE; mhandle_mark_corrupt(eff_ptr, eff_size); /* Record pointer and return */ mhandle_hash_table_insert(ptr, size, at, 1); return ptr; }
void __mhandle_check(char *at) { int i, count = 0; /* Check for corruption in all allocated blocks */ for (i = 0; i < mhandle_hash_table_size; i++) { if (mhandle_hash_table[i].active && !mhandle_hash_table[i].removed && mhandle_hash_table[i].corrupt_info) { mhandle_check_corrupt(mhandle_hash_table[i].ptr - MHANDLE_CORRUPT_RANGE, mhandle_hash_table[i].size, mhandle_hash_table[i].at); count++; } } fprintf(stderr, "libmhandle: %d pointers checked for corruption\n", count); }
void __mhandle_check(char *at) { int i; int count = 0; void *eff_ptr; unsigned long eff_size; /* Check for corruption in all allocated blocks */ for (i = 0; i < mhandle_hash_table_size; i++) { if (mhandle_hash_table[i].active && !mhandle_hash_table[i].removed && mhandle_hash_table[i].corrupt_info) { eff_ptr = mhandle_hash_table[i].ptr - MHANDLE_CORRUPT_RANGE; eff_size = mhandle_hash_table[i].size + MHANDLE_CORRUPT_TOTAL; mhandle_check_corrupt(eff_ptr, eff_size, mhandle_hash_table[i].at); count++; } } fprintf(stderr, "libmhandle: %d pointers checked for corruption\n", count); }