From: Michael Vrable Date: Thu, 2 Dec 2010 19:39:56 +0000 (-0800) Subject: Try to avoid accessing profiling objects after they are freed. X-Git-Url: https://git.vrable.net/?a=commitdiff_plain;h=1dc8b4fc9e5d7e2b2ed51ce16da67f3d5e16d240;p=bluesky.git Try to avoid accessing profiling objects after they are freed. --- diff --git a/bluesky/bluesky.h b/bluesky/bluesky.h index bf01abe..ae751fd 100644 --- a/bluesky/bluesky.h +++ b/bluesky/bluesky.h @@ -369,6 +369,7 @@ void bluesky_debug_dump(BlueSkyFS *fs); /* Request response time tracking. */ typedef struct BlueSkyProfile { + int magic; GMutex *lock; char *description; GList *events; diff --git a/bluesky/cloudlog.c b/bluesky/cloudlog.c index 4c94c49..64183b7 100644 --- a/bluesky/cloudlog.c +++ b/bluesky/cloudlog.c @@ -485,6 +485,7 @@ static void cloudlog_flush_complete(BlueSkyStoreAsync *async, async2->op = STORE_OP_PUT; async2->key = g_strdup(async->key); async2->data = record->data; + async2->profile = async->profile; bluesky_string_ref(record->data); bluesky_store_async_submit(async2); bluesky_store_async_add_notifier(async2, diff --git a/bluesky/log.c b/bluesky/log.c index 6d5c590..89d4258 100644 --- a/bluesky/log.c +++ b/bluesky/log.c @@ -469,6 +469,7 @@ static void cloudlog_partial_fetch_start(BlueSkyCacheFile *cachefile, async->key = g_strdup(cachefile->filename); async->start = offset; async->len = length; + async->profile = bluesky_profile_get(); bluesky_store_async_add_notifier(async, (GFunc)cloudlog_partial_fetch_complete, cachefile); @@ -550,6 +551,7 @@ static void cloudlog_fetch_start(BlueSkyCacheFile *cachefile) BlueSkyStoreAsync *async = bluesky_store_async_new(cachefile->fs->store); async->op = STORE_OP_GET; async->key = g_strdup(cachefile->filename); + async->profile = bluesky_profile_get(); bluesky_store_async_add_notifier(async, (GFunc)cloudlog_partial_fetch_complete, cachefile); diff --git a/bluesky/store.c b/bluesky/store.c index 1ee3916..a8548ea 100644 --- a/bluesky/store.c +++ b/bluesky/store.c @@ -112,7 +112,7 @@ BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store) async->notifier_count = 0; async->barrier = NULL; async->store_private = NULL; - async->profile = bluesky_profile_get(); + async->profile = NULL; return async; } diff --git a/bluesky/util.c b/bluesky/util.c index d854460..bf1d09e 100644 --- a/bluesky/util.c +++ b/bluesky/util.c @@ -349,21 +349,30 @@ typedef struct { char *message; } RTEvent; +/* To catch attempts to access to invalid profile structures. */ +#define PROFILE_MAGIC 0x439929d8 + BlueSkyProfile *bluesky_profile_new() { BlueSkyProfile *profile = g_new0(BlueSkyProfile, 1); profile->lock = g_mutex_new(); + profile->magic = PROFILE_MAGIC; return profile; } void bluesky_profile_free(BlueSkyProfile *profile) { + if (profile->magic != PROFILE_MAGIC) { + g_warning("Access to invalid BlueSkyProfile object!"); + return; + } while (profile->events != NULL) { RTEvent *event = (RTEvent *)profile->events->data; g_free(event->message); g_free(event); profile->events = g_list_delete_link(profile->events, profile->events); } + profile->magic = 0; g_mutex_free(profile->lock); g_free(profile->description); g_free(profile); @@ -373,6 +382,10 @@ void bluesky_profile_add_event(BlueSkyProfile *profile, char *message) { g_return_if_fail(profile != NULL); + if (profile->magic != PROFILE_MAGIC) { + g_warning("Access to invalid BlueSkyProfile object!"); + return; + } g_mutex_lock(profile->lock); RTEvent *event = g_new(RTEvent, 1); event->timestamp = bluesky_now_hires(); @@ -399,6 +412,11 @@ void bluesky_profile_print(BlueSkyProfile *profile) g_return_if_fail(profile != NULL); + if (profile->magic != PROFILE_MAGIC) { + g_warning("Access to invalid BlueSkyProfile object!"); + return; + } + g_mutex_lock(profile->lock); g_static_mutex_lock(&profiling_print_lock); fprintf(stream, "Event Timeline: %s\n", profile->description);