preload.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #define _GNU_SOURCE
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <dlfcn.h>
  5. #include <stdlib.h>
  6. #include <sysexits.h>
  7. #include "include/liblockdep/mutex.h"
  8. #include "../../../include/linux/rbtree.h"
  9. /**
  10. * struct lock_lookup - liblockdep's view of a single unique lock
  11. * @orig: pointer to the original pthread lock, used for lookups
  12. * @dep_map: lockdep's dep_map structure
  13. * @key: lockdep's key structure
  14. * @node: rb-tree node used to store the lock in a global tree
  15. * @name: a unique name for the lock
  16. */
  17. struct lock_lookup {
  18. void *orig; /* Original pthread lock, used for lookups */
  19. struct lockdep_map dep_map; /* Since all locks are dynamic, we need
  20. * a dep_map and a key for each lock */
  21. /*
  22. * Wait, there's no support for key classes? Yup :(
  23. * Most big projects wrap the pthread api with their own calls to
  24. * be compatible with different locking methods. This means that
  25. * "classes" will be brokes since the function that creates all
  26. * locks will point to a generic locking function instead of the
  27. * actual code that wants to do the locking.
  28. */
  29. struct lock_class_key key;
  30. struct rb_node node;
  31. #define LIBLOCKDEP_MAX_LOCK_NAME 22
  32. char name[LIBLOCKDEP_MAX_LOCK_NAME];
  33. };
  34. /* This is where we store our locks */
  35. static struct rb_root locks = RB_ROOT;
  36. static pthread_rwlock_t locks_rwlock = PTHREAD_RWLOCK_INITIALIZER;
  37. /* pthread mutex API */
  38. #ifdef __GLIBC__
  39. extern int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
  40. extern int __pthread_mutex_lock(pthread_mutex_t *mutex);
  41. extern int __pthread_mutex_trylock(pthread_mutex_t *mutex);
  42. extern int __pthread_mutex_unlock(pthread_mutex_t *mutex);
  43. extern int __pthread_mutex_destroy(pthread_mutex_t *mutex);
  44. #else
  45. #define __pthread_mutex_init NULL
  46. #define __pthread_mutex_lock NULL
  47. #define __pthread_mutex_trylock NULL
  48. #define __pthread_mutex_unlock NULL
  49. #define __pthread_mutex_destroy NULL
  50. #endif
  51. static int (*ll_pthread_mutex_init)(pthread_mutex_t *mutex,
  52. const pthread_mutexattr_t *attr) = __pthread_mutex_init;
  53. static int (*ll_pthread_mutex_lock)(pthread_mutex_t *mutex) = __pthread_mutex_lock;
  54. static int (*ll_pthread_mutex_trylock)(pthread_mutex_t *mutex) = __pthread_mutex_trylock;
  55. static int (*ll_pthread_mutex_unlock)(pthread_mutex_t *mutex) = __pthread_mutex_unlock;
  56. static int (*ll_pthread_mutex_destroy)(pthread_mutex_t *mutex) = __pthread_mutex_destroy;
  57. /* pthread rwlock API */
  58. #ifdef __GLIBC__
  59. extern int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
  60. extern int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
  61. extern int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
  62. extern int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
  63. extern int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
  64. extern int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
  65. extern int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
  66. #else
  67. #define __pthread_rwlock_init NULL
  68. #define __pthread_rwlock_destroy NULL
  69. #define __pthread_rwlock_wrlock NULL
  70. #define __pthread_rwlock_trywrlock NULL
  71. #define __pthread_rwlock_rdlock NULL
  72. #define __pthread_rwlock_tryrdlock NULL
  73. #define __pthread_rwlock_unlock NULL
  74. #endif
  75. static int (*ll_pthread_rwlock_init)(pthread_rwlock_t *rwlock,
  76. const pthread_rwlockattr_t *attr) = __pthread_rwlock_init;
  77. static int (*ll_pthread_rwlock_destroy)(pthread_rwlock_t *rwlock) = __pthread_rwlock_destroy;
  78. static int (*ll_pthread_rwlock_rdlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_rdlock;
  79. static int (*ll_pthread_rwlock_tryrdlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_tryrdlock;
  80. static int (*ll_pthread_rwlock_trywrlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_trywrlock;
  81. static int (*ll_pthread_rwlock_wrlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_wrlock;
  82. static int (*ll_pthread_rwlock_unlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_unlock;
  83. enum { none, prepare, done, } __init_state;
  84. static void init_preload(void);
  85. static void try_init_preload(void)
  86. {
  87. if (__init_state != done)
  88. init_preload();
  89. }
  90. static struct rb_node **__get_lock_node(void *lock, struct rb_node **parent)
  91. {
  92. struct rb_node **node = &locks.rb_node;
  93. struct lock_lookup *l;
  94. *parent = NULL;
  95. while (*node) {
  96. l = rb_entry(*node, struct lock_lookup, node);
  97. *parent = *node;
  98. if (lock < l->orig)
  99. node = &l->node.rb_left;
  100. else if (lock > l->orig)
  101. node = &l->node.rb_right;
  102. else
  103. return node;
  104. }
  105. return node;
  106. }
  107. #ifndef LIBLOCKDEP_STATIC_ENTRIES
  108. #define LIBLOCKDEP_STATIC_ENTRIES 1024
  109. #endif
  110. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  111. static struct lock_lookup __locks[LIBLOCKDEP_STATIC_ENTRIES];
  112. static int __locks_nr;
  113. static inline bool is_static_lock(struct lock_lookup *lock)
  114. {
  115. return lock >= __locks && lock < __locks + ARRAY_SIZE(__locks);
  116. }
  117. static struct lock_lookup *alloc_lock(void)
  118. {
  119. if (__init_state != done) {
  120. /*
  121. * Some programs attempt to initialize and use locks in their
  122. * allocation path. This means that a call to malloc() would
  123. * result in locks being initialized and locked.
  124. *
  125. * Why is it an issue for us? dlsym() below will try allocating
  126. * to give us the original function. Since this allocation will
  127. * result in a locking operations, we have to let pthread deal
  128. * with it, but we can't! we don't have the pointer to the
  129. * original API since we're inside dlsym() trying to get it
  130. */
  131. int idx = __locks_nr++;
  132. if (idx >= ARRAY_SIZE(__locks)) {
  133. fprintf(stderr,
  134. "LOCKDEP error: insufficient LIBLOCKDEP_STATIC_ENTRIES\n");
  135. exit(EX_UNAVAILABLE);
  136. }
  137. return __locks + idx;
  138. }
  139. return malloc(sizeof(struct lock_lookup));
  140. }
  141. static inline void free_lock(struct lock_lookup *lock)
  142. {
  143. if (likely(!is_static_lock(lock)))
  144. free(lock);
  145. }
  146. /**
  147. * __get_lock - find or create a lock instance
  148. * @lock: pointer to a pthread lock function
  149. *
  150. * Try to find an existing lock in the rbtree using the provided pointer. If
  151. * one wasn't found - create it.
  152. */
  153. static struct lock_lookup *__get_lock(void *lock)
  154. {
  155. struct rb_node **node, *parent;
  156. struct lock_lookup *l;
  157. ll_pthread_rwlock_rdlock(&locks_rwlock);
  158. node = __get_lock_node(lock, &parent);
  159. ll_pthread_rwlock_unlock(&locks_rwlock);
  160. if (*node) {
  161. return rb_entry(*node, struct lock_lookup, node);
  162. }
  163. /* We didn't find the lock, let's create it */
  164. l = alloc_lock();
  165. if (l == NULL)
  166. return NULL;
  167. l->orig = lock;
  168. /*
  169. * Currently the name of the lock is the ptr value of the pthread lock,
  170. * while not optimal, it makes debugging a bit easier.
  171. *
  172. * TODO: Get the real name of the lock using libdwarf
  173. */
  174. sprintf(l->name, "%p", lock);
  175. lockdep_init_map(&l->dep_map, l->name, &l->key, 0);
  176. ll_pthread_rwlock_wrlock(&locks_rwlock);
  177. /* This might have changed since the last time we fetched it */
  178. node = __get_lock_node(lock, &parent);
  179. rb_link_node(&l->node, parent, node);
  180. rb_insert_color(&l->node, &locks);
  181. ll_pthread_rwlock_unlock(&locks_rwlock);
  182. return l;
  183. }
  184. static void __del_lock(struct lock_lookup *lock)
  185. {
  186. ll_pthread_rwlock_wrlock(&locks_rwlock);
  187. rb_erase(&lock->node, &locks);
  188. ll_pthread_rwlock_unlock(&locks_rwlock);
  189. free_lock(lock);
  190. }
  191. int pthread_mutex_init(pthread_mutex_t *mutex,
  192. const pthread_mutexattr_t *attr)
  193. {
  194. int r;
  195. /*
  196. * We keep trying to init our preload module because there might be
  197. * code in init sections that tries to touch locks before we are
  198. * initialized, in that case we'll need to manually call preload
  199. * to get us going.
  200. *
  201. * Funny enough, kernel's lockdep had the same issue, and used
  202. * (almost) the same solution. See look_up_lock_class() in
  203. * kernel/locking/lockdep.c for details.
  204. */
  205. try_init_preload();
  206. r = ll_pthread_mutex_init(mutex, attr);
  207. if (r == 0)
  208. /*
  209. * We do a dummy initialization here so that lockdep could
  210. * warn us if something fishy is going on - such as
  211. * initializing a held lock.
  212. */
  213. __get_lock(mutex);
  214. return r;
  215. }
  216. int pthread_mutex_lock(pthread_mutex_t *mutex)
  217. {
  218. int r;
  219. try_init_preload();
  220. lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL,
  221. (unsigned long)_RET_IP_);
  222. /*
  223. * Here's the thing with pthread mutexes: unlike the kernel variant,
  224. * they can fail.
  225. *
  226. * This means that the behaviour here is a bit different from what's
  227. * going on in the kernel: there we just tell lockdep that we took the
  228. * lock before actually taking it, but here we must deal with the case
  229. * that locking failed.
  230. *
  231. * To do that we'll "release" the lock if locking failed - this way
  232. * we'll get lockdep doing the correct checks when we try to take
  233. * the lock, and if that fails - we'll be back to the correct
  234. * state by releasing it.
  235. */
  236. r = ll_pthread_mutex_lock(mutex);
  237. if (r)
  238. lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_);
  239. return r;
  240. }
  241. int pthread_mutex_trylock(pthread_mutex_t *mutex)
  242. {
  243. int r;
  244. try_init_preload();
  245. lock_acquire(&__get_lock(mutex)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
  246. r = ll_pthread_mutex_trylock(mutex);
  247. if (r)
  248. lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_);
  249. return r;
  250. }
  251. int pthread_mutex_unlock(pthread_mutex_t *mutex)
  252. {
  253. int r;
  254. try_init_preload();
  255. lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_);
  256. /*
  257. * Just like taking a lock, only in reverse!
  258. *
  259. * If we fail releasing the lock, tell lockdep we're holding it again.
  260. */
  261. r = ll_pthread_mutex_unlock(mutex);
  262. if (r)
  263. lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
  264. return r;
  265. }
  266. int pthread_mutex_destroy(pthread_mutex_t *mutex)
  267. {
  268. try_init_preload();
  269. /*
  270. * Let's see if we're releasing a lock that's held.
  271. *
  272. * TODO: Hook into free() and add that check there as well.
  273. */
  274. debug_check_no_locks_freed(mutex, sizeof(*mutex));
  275. __del_lock(__get_lock(mutex));
  276. return ll_pthread_mutex_destroy(mutex);
  277. }
  278. /* This is the rwlock part, very similar to what happened with mutex above */
  279. int pthread_rwlock_init(pthread_rwlock_t *rwlock,
  280. const pthread_rwlockattr_t *attr)
  281. {
  282. int r;
  283. try_init_preload();
  284. r = ll_pthread_rwlock_init(rwlock, attr);
  285. if (r == 0)
  286. __get_lock(rwlock);
  287. return r;
  288. }
  289. int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
  290. {
  291. try_init_preload();
  292. debug_check_no_locks_freed(rwlock, sizeof(*rwlock));
  293. __del_lock(__get_lock(rwlock));
  294. return ll_pthread_rwlock_destroy(rwlock);
  295. }
  296. int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
  297. {
  298. int r;
  299. init_preload();
  300. lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_);
  301. r = ll_pthread_rwlock_rdlock(rwlock);
  302. if (r)
  303. lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
  304. return r;
  305. }
  306. int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
  307. {
  308. int r;
  309. init_preload();
  310. lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_);
  311. r = ll_pthread_rwlock_tryrdlock(rwlock);
  312. if (r)
  313. lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
  314. return r;
  315. }
  316. int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
  317. {
  318. int r;
  319. init_preload();
  320. lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
  321. r = ll_pthread_rwlock_trywrlock(rwlock);
  322. if (r)
  323. lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
  324. return r;
  325. }
  326. int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
  327. {
  328. int r;
  329. init_preload();
  330. lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
  331. r = ll_pthread_rwlock_wrlock(rwlock);
  332. if (r)
  333. lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
  334. return r;
  335. }
  336. int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
  337. {
  338. int r;
  339. init_preload();
  340. lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
  341. r = ll_pthread_rwlock_unlock(rwlock);
  342. if (r)
  343. lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
  344. return r;
  345. }
  346. __attribute__((constructor)) static void init_preload(void)
  347. {
  348. if (__init_state == done)
  349. return;
  350. #ifndef __GLIBC__
  351. __init_state = prepare;
  352. ll_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
  353. ll_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
  354. ll_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
  355. ll_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
  356. ll_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
  357. ll_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init");
  358. ll_pthread_rwlock_destroy = dlsym(RTLD_NEXT, "pthread_rwlock_destroy");
  359. ll_pthread_rwlock_rdlock = dlsym(RTLD_NEXT, "pthread_rwlock_rdlock");
  360. ll_pthread_rwlock_tryrdlock = dlsym(RTLD_NEXT, "pthread_rwlock_tryrdlock");
  361. ll_pthread_rwlock_wrlock = dlsym(RTLD_NEXT, "pthread_rwlock_wrlock");
  362. ll_pthread_rwlock_trywrlock = dlsym(RTLD_NEXT, "pthread_rwlock_trywrlock");
  363. ll_pthread_rwlock_unlock = dlsym(RTLD_NEXT, "pthread_rwlock_unlock");
  364. #endif
  365. lockdep_init();
  366. __init_state = done;
  367. }