preload.c 13 KB

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