Mercurial > libjeffpc
changeset 487:8121b8d865de
synch: each lock should contain a magic number
The magic number is simply the pointer to the mutex. This will trivially
catch two issues:
(1) locks getting clobbered
(2) locks getting moved around (via memmove/memcpy)
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sun, 06 May 2018 15:27:08 -0400 |
parents | 61a39d12d6b7 |
children | 4f8e21f0286f |
files | include/jeffpc/synch.h synch.c |
diffstat | 2 files changed, 24 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/include/jeffpc/synch.h Sun May 06 15:10:53 2018 -0400 +++ b/include/jeffpc/synch.h Sun May 06 15:27:08 2018 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2014-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,6 +28,7 @@ struct lock { pthread_mutex_t lock; + uintptr_t magic; }; struct rwlock {
--- a/synch.c Sun May 06 15:10:53 2018 -0400 +++ b/synch.c Sun May 06 15:27:08 2018 -0400 @@ -28,22 +28,44 @@ #include <jeffpc/config.h> /* + * error printing + */ +static void print_lock(struct lock *lock) +{ + cmn_err(CE_CRIT, " %p <%c>", lock, + (lock->magic != (uintptr_t) lock) ? 'M' : '.'); +} + +/* * state checking */ +static void check_lock_magic(struct lock *lock, const char *op) +{ + if (lock->magic == (uintptr_t) lock) + return; + + cmn_err(CE_CRIT, "thread trying to %s lock with bad magic", op); + print_lock(lock); +} + static void verify_lock_init(struct lock *l) { + l->magic = (uintptr_t) l; } static void verify_lock_destroy(struct lock *l) { + check_lock_magic(l, "destroy"); } static void verify_lock_lock(struct lock *l) { + check_lock_magic(l, "acquire"); } static void verify_lock_unlock(struct lock *l) { + check_lock_magic(l, "release"); } /*