From 9ff937d66561e227f063467f7549b45c9d8bbbca Mon Sep 17 00:00:00 2001 From: Michael Poole Date: Mon, 24 Jan 2005 16:45:44 +0000 Subject: [PATCH] More allocation debugging support. src/alloc-srvx.c (srvx_realloc): Reorganize. (verify): New function. src/common.h (verify): Define and/or declare suitably. src/dict-splay.c (dict_splay): Verify node at each iteration. (dict_insert, dict_remove2, dict_find, dict_delete, dict_sanity_check): Verify entire dict. (dict_sanity_check_node): Verify node as valid allocation. src/hash.c (DelChannel): Verify channel before deletion. (GetUserMode): Verify channel, user, and each modeNode. src/mod-sockcheck.c (sockcheck_free_client): Verify client. (sockcheck_timeout_client, sockcheck_advance, sockcheck_readable, sockcheck_connected, sockcheck_begin_test): Likewise. (sockcheck_queue_address): Verify cached sockcheck entries. src/proto-p10.c (DelUser): Verify user before deletion. git-archimport-id: srvx@srvx.net--2005-srvx/srvx--devo--1.3--patch-7 --- ChangeLog | 32 ++++++++++++++++++++++++++++++++ src/alloc-srvx.c | 42 ++++++++++++++++++++++++------------------ src/common.h | 8 ++++++++ src/dict-splay.c | 11 ++++++++++- src/hash.c | 6 ++++++ src/mod-sockcheck.c | 10 +++++++++- src/proto-p10.c | 2 ++ 7 files changed, 91 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index 198e29f..7944689 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,38 @@ # arch-tag: automatic-ChangeLog--srvx@srvx.net--2005-srvx/srvx--devo--1.3 # +2005-01-24 16:45:44 GMT Michael Poole patch-7 + + Summary: + More allocation debugging support. + Revision: + srvx--devo--1.3--patch-7 + + src/alloc-srvx.c (srvx_realloc): Reorganize. + (verify): New function. + + src/common.h (verify): Define and/or declare suitably. + + src/dict-splay.c (dict_splay): Verify node at each iteration. + (dict_insert, dict_remove2, dict_find, dict_delete, + dict_sanity_check): Verify entire dict. + (dict_sanity_check_node): Verify node as valid allocation. + + src/hash.c (DelChannel): Verify channel before deletion. + (GetUserMode): Verify channel, user, and each modeNode. + + src/mod-sockcheck.c (sockcheck_free_client): Verify client. + (sockcheck_timeout_client, sockcheck_advance, sockcheck_readable, + sockcheck_connected, sockcheck_begin_test): Likewise. + (sockcheck_queue_address): Verify cached sockcheck entries. + + src/proto-p10.c (DelUser): Verify user before deletion. + + modified files: + ChangeLog src/alloc-srvx.c src/common.h src/dict-splay.c + src/hash.c src/mod-sockcheck.c src/proto-p10.c + + 2005-01-21 15:10:49 GMT Michael Poole patch-6 Summary: diff --git a/src/alloc-srvx.c b/src/alloc-srvx.c index 047cb95..189b36d 100644 --- a/src/alloc-srvx.c +++ b/src/alloc-srvx.c @@ -74,19 +74,22 @@ srvx_malloc(const char *file, unsigned int line, size_t size) void * srvx_realloc(const char *file, unsigned int line, void *ptr, size_t size) { - struct alloc_header *block = NULL, *newblock; + struct alloc_header *block, *newblock; - if (ptr) { - block = (struct alloc_header *)ptr - 1; - assert(block->magic == ALLOC_MAGIC); - assert(0 == memcmp((char*)(block + 1) + block->size, redzone, sizeof(redzone))); - if (block->size >= size) - return block + 1; - } + if (!ptr) + return srvx_malloc(file, line, size); + + verify(ptr); + block = (struct alloc_header *)ptr - 1; + + if (block->size >= size) + return block + 1; newblock = malloc(sizeof(*newblock) + size + sizeof(redzone)); assert(newblock != NULL); - memset(newblock, 0, sizeof(*newblock) + size + sizeof(redzone)); + memset(newblock, 0, sizeof(*newblock)); + memcpy(newblock + 1, block + 1, block->size); + memset((char*)(newblock + 1) + block->size, 0, size - block->size); memcpy((char*)(newblock + 1) + size, redzone, sizeof(redzone)); newblock->file_id = get_file_id(file); newblock->line = line; @@ -95,15 +98,7 @@ srvx_realloc(const char *file, unsigned int line, void *ptr, size_t size) alloc_count++; alloc_size += size; - if (ptr) { - memcpy(newblock + 1, block + 1, block->size); - size = block->size + sizeof(*block); - memset(block, 0, size); - block->magic = FREE_MAGIC; - free(block); - alloc_count--; - alloc_size -= size - sizeof(*block); - } + srvx_free(block); return newblock + 1; } @@ -139,3 +134,14 @@ srvx_free(const char *file, unsigned int line, void *ptr) alloc_size -= size; (void)file; (void)line; } + +void +verify(const void *ptr) +{ + const struct alloc_header *header; + if (!ptr) + return; + header = (const struct alloc_header*)ptr - 1; + assert(header->magic == ALLOC_MAGIC); + assert(!memcmp((char*)(header + 1) + header->size, redzone, sizeof(redzone))); +} diff --git a/src/common.h b/src/common.h index a8458a3..56928ad 100644 --- a/src/common.h +++ b/src/common.h @@ -98,6 +98,14 @@ extern void *srvx_malloc(const char *, unsigned int, size_t); extern void *srvx_realloc(const char *, unsigned int, void *, size_t); extern char *srvx_strdup(const char *, unsigned int, const char *); extern void srvx_free(const char *, unsigned int, void *); +# if !defined(NDEBUG) +extern void verify(const void *ptr); +# define verify(x) verify(x) +# endif +#endif + +#ifndef verify +# define verify(ptr) (void)(ptr) #endif extern time_t now; diff --git a/src/dict-splay.c b/src/dict-splay.c index 597545b..25e7a05 100644 --- a/src/dict-splay.c +++ b/src/dict-splay.c @@ -75,12 +75,15 @@ static struct dict_node* dict_splay(struct dict_node *node, const char *key) { struct dict_node N, *l, *r, *y; + int res; + if (!node) return NULL; N.l = N.r = NULL; l = r = &N; while (1) { - int res = irccasecmp(key, node->key); + verify(node); + res = irccasecmp(key, node->key); if (!res) break; if (res < 0) { if (!node->l) break; @@ -149,6 +152,7 @@ dict_insert(dict_t dict, const char *key, void *data) struct dict_node *new_node; if (!key) return; + verify(dict); new_node = malloc(sizeof(struct dict_node)); new_node->key = key; new_node->data = data; @@ -224,6 +228,7 @@ dict_remove2(dict_t dict, const char *key, int no_dispose) if (!dict->root) return 0; + verify(dict); dict->root = dict_splay(dict->root, key); if (irccasecmp(key, dict->root->key)) return 0; @@ -264,6 +269,7 @@ dict_find(dict_t dict, const char *key, int *found) *found = 0; return NULL; } + verify(dict); dict->root = dict_splay(dict->root, key); was_found = !irccasecmp(key, dict->root->key); if (found) @@ -280,6 +286,7 @@ dict_delete(dict_t dict) dict_iterator_t it, next; if (!dict) return; + verify(dict); for (it=dict_first(dict); it; it=next) { next = iter_next(it); dict_dispose_node(it, dict->free_keys, dict->free_data); @@ -296,6 +303,7 @@ struct dict_sanity_struct { static int dict_sanity_check_node(struct dict_node *node, struct dict_sanity_struct *dss) { + verify(node); if (!node->key) { snprintf(dss->error, sizeof(dss->error), "Node %p had null key", node); return 1; @@ -328,6 +336,7 @@ dict_sanity_check(dict_t dict) dss.node_count = 0; dss.bad_node = 0; dss.error[0] = 0; + verify(dict); if (dict->root && dict_sanity_check_node(dict->root, &dss)) { return strdup(dss.error); } else if (dss.node_count != dict->count) { diff --git a/src/hash.c b/src/hash.c index fabda04..cb95ce1 100644 --- a/src/hash.c +++ b/src/hash.c @@ -425,6 +425,7 @@ DelChannel(struct chanNode *channel) { unsigned int n; + verify(channel); dict_remove(channels, channel->name); if (channel->members.used || channel->locks) { @@ -691,8 +692,12 @@ GetUserMode(struct chanNode *channel, struct userNode *user) { unsigned int n; struct modeNode *mn = NULL; + + verify(channel); + verify(user); if (channel->members.used < user->channels.used) { for (n=0; nmembers.used; n++) { + verify(channel->members.list[n]); if (user == channel->members.list[n]->user) { mn = channel->members.list[n]; break; @@ -700,6 +705,7 @@ GetUserMode(struct chanNode *channel, struct userNode *user) } } else { for (n=0; nchannels.used; n++) { + verify(user->channels.list[n]); if (channel == user->channels.list[n]->channel) { mn = user->channels.list[n]; break; diff --git a/src/mod-sockcheck.c b/src/mod-sockcheck.c index 0e03822..eeb7ada 100644 --- a/src/mod-sockcheck.c +++ b/src/mod-sockcheck.c @@ -230,7 +230,9 @@ sockcheck_free_client(struct sockcheck_client *client) if (SOCKCHECK_DEBUG) { log_module(PC_LOG, LOG_INFO, "Goodbye %s (%p)! I set you free!", client->addr->hostname, client); } - if (client->fd) ioset_close(client->fd->fd, 1); + verify(client); + if (client->fd) + ioset_close(client->fd->fd, 1); sockcheck_list_unref(client->tests); free(client->read); free(client->resp_state); @@ -248,6 +250,7 @@ sockcheck_timeout_client(void *data) if (SOCKCHECK_DEBUG) { log_module(PC_LOG, LOG_INFO, "Client %s timed out.", client->addr->hostname); } + verify(client); sockcheck_advance(client, client->state->responses.used-1); } @@ -472,6 +475,7 @@ sockcheck_advance(struct sockcheck_client *client, unsigned int next_state) { struct sockcheck_state *ns; + verify(client); timeq_del(0, sockcheck_timeout_client, client, TIMEQ_IGNORE_WHEN); if (SOCKCHECK_DEBUG) { unsigned int n, m; @@ -522,6 +526,7 @@ sockcheck_readable(struct io_fd *fd) unsigned int nn; int res; + verify(client); res = read(fd->fd, client->read + client->read_used, client->read_size - client->read_used); if (res < 0) { switch (res = errno) { @@ -628,6 +633,7 @@ static void sockcheck_connected(struct io_fd *fd, int rc) { struct sockcheck_client *client = fd->data; + verify(client); client->fd = fd; switch (rc) { default: @@ -654,6 +660,7 @@ sockcheck_begin_test(struct sockcheck_client *client) { struct io_fd *io_fd; + verify(client); if (client->fd) { ioset_close(client->fd->fd, 1); client->fd = NULL; @@ -708,6 +715,7 @@ sockcheck_queue_address(struct in_addr addr) sci = dict_find(checked_ip_dict, ipstr, NULL); if (sci) { + verify(sci); switch (sci->decision) { case CHECKING: /* We are already checking this host. */ diff --git a/src/proto-p10.c b/src/proto-p10.c index 4e49b64..a237bfa 100644 --- a/src/proto-p10.c +++ b/src/proto-p10.c @@ -1943,6 +1943,8 @@ DelUser(struct userNode* user, struct userNode *killer, int announce, const char { unsigned int n; + verify(user); + /* mark them as dead, in case anybody cares */ user->dead = 1; -- 2.20.1