void *chk_realloc(void *ptr, size_t size) { struct hdr *hdr; // log_message("%s: %s\n", __FILE__, __FUNCTION__); if (!size) { chk_free(ptr); return NULL; } if (!ptr) return chk_malloc(size); hdr = meta(ptr); if (del(hdr) < 0) { intptr_t bt[MAX_BACKTRACE_DEPTH]; int depth; depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); if (hdr->tag == BACKLOG_TAG) { log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n", user(hdr), size, hdr->size); log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", user(hdr), hdr->size); print_backtrace(hdr->bt, hdr->bt_depth); /* hdr->freed_bt_depth should be nonzero here */ log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", user(hdr), hdr->size); print_backtrace(hdr->freed_bt, hdr->freed_bt_depth); log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n", user(hdr), hdr->size); print_backtrace(bt, depth); /* We take the memory out of the backlog and fall through so the * reallocation below succeeds. Since we didn't really free it, we * can default to this behavior. */ del_from_backlog(hdr); } else { log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", user(hdr), size); print_backtrace(bt, depth); // just get a whole new allocation and leak the old one return dlrealloc(0, size); // return dlrealloc(user(hdr), size); // assuming it was allocated externally } } hdr = dlrealloc(hdr, sizeof(struct hdr) + size + sizeof(struct ftr)); if (hdr) { hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); add(hdr, size); return user(hdr); } return NULL; }
/* ENTRY POINT */ int main(int argc, char **argv) { struct execute_context exec_ctx; pid_t c_pid; debug_printf(stderr,"start!\n"); parse_opt(argc, argv, &exec_ctx); fprintf(stderr, "[Tracer] Target program is %s\n", exec_ctx.passing_args[0]); c_pid = fork(); exec_ctx.target_pid = c_pid; if (c_pid == -1) { return -1; } else if(c_pid == 0) { target_proc(&exec_ctx); } else { debugger_proc(c_pid, &exec_ctx); } chk_free(exec_ctx.passing_args); return 0; }
extern "C" void* chk_realloc(void* ptr, size_t size) { // log_message("%s: %s\n", __FILE__, __FUNCTION__); if (!ptr) { return chk_malloc(size); } #ifdef REALLOC_ZERO_BYTES_FREE if (!size) { chk_free(ptr); return NULL; } #endif hdr_t* hdr = meta(ptr); if (del(hdr) < 0) { uintptr_t bt[MAX_BACKTRACE_DEPTH]; int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); if (hdr->tag == BACKLOG_TAG) { log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n", user(hdr), size, hdr->size); log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", user(hdr), hdr->size); log_backtrace(hdr->bt, hdr->bt_depth); /* hdr->freed_bt_depth should be nonzero here */ log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", user(hdr), hdr->size); log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n", user(hdr), hdr->size); log_backtrace(bt, depth); /* We take the memory out of the backlog and fall through so the * reallocation below succeeds. Since we didn't really free it, we * can default to this behavior. */ del_from_backlog(hdr); } else { log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", user(hdr), size); log_backtrace(bt, depth); // just get a whole new allocation and leak the old one return dlrealloc(0, size); // return dlrealloc(user(hdr), size); // assuming it was allocated externally } } if (hdr->base != hdr) { // An allocation from memalign, so create another allocation and // copy the data out. void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)); if (newMem) { memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size); dlfree(hdr->base); hdr = static_cast<hdr_t*>(newMem); } else { dlfree(hdr->base); hdr = NULL; } } else { hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t))); } if (hdr) { hdr->base = hdr; hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); add(hdr, size); return user(hdr); } return NULL; }
void cir_free() { str_cir_pt=lst_ok(str_cir_bs); while(str_cir_pt--) chk_free(str_cir_bs[str_cir_pt]); lfree(str_cir_bs); }