0001-locks-define-lock-functions-as-a-macro.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. From 5803bfa3d4febdcf32494e2c2c8f4731cb8be7cd Mon Sep 17 00:00:00 2001
  2. From: Daiki Ueno <ueno@gnu.org>
  3. Date: Wed, 9 Mar 2022 08:07:58 +0100
  4. Subject: [PATCH] locks: define lock functions as a macro
  5. When threads are not supported, glthread_* functions are defined as
  6. no-op and thus dereferencing lock variables in inline functions will
  7. cause compilation error. This change fixes it by redefining our lock
  8. functions as a macro so it will also be compiled out.
  9. Reported by Fabrice Fontaine in:
  10. https://gitlab.com/gnutls/gnutls/-/issues/1330
  11. Signed-off-by: Daiki Ueno <ueno@gnu.org>
  12. [Retrieved from:
  13. https://gitlab.com/gnutls/gnutls/-/commit/5803bfa3d4febdcf32494e2c2c8f4731cb8be7cd]
  14. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  15. ---
  16. lib/locks.c | 54 -----------------------------------------------
  17. lib/locks.h | 28 ++++++++++++++++++------
  18. lib/pkcs11.c | 2 +-
  19. lib/priority.c | 2 +-
  20. lib/verify-tofu.c | 2 +-
  21. 5 files changed, 25 insertions(+), 63 deletions(-)
  22. diff --git a/lib/locks.c b/lib/locks.c
  23. index d61504e077..8888ed6b12 100644
  24. --- a/lib/locks.c
  25. +++ b/lib/locks.c
  26. @@ -63,57 +63,3 @@ gnutls_global_set_mutex(mutex_init_func init, mutex_deinit_func deinit,
  27. gnutls_mutex_lock = lock;
  28. gnutls_mutex_unlock = unlock;
  29. }
  30. -
  31. -int
  32. -gnutls_static_mutex_lock(gnutls_static_mutex_t lock)
  33. -{
  34. - if (unlikely(glthread_lock_lock(lock))) {
  35. - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
  36. - }
  37. - return 0;
  38. -}
  39. -
  40. -int
  41. -gnutls_static_mutex_unlock(gnutls_static_mutex_t lock)
  42. -{
  43. - if (unlikely(glthread_lock_unlock(lock))) {
  44. - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
  45. - }
  46. - return 0;
  47. -}
  48. -
  49. -int
  50. -gnutls_rwlock_rdlock(gnutls_rwlock_t rwlock)
  51. -{
  52. - if (unlikely(glthread_rwlock_rdlock(rwlock))) {
  53. - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
  54. - }
  55. - return 0;
  56. -}
  57. -
  58. -int
  59. -gnutls_rwlock_wrlock(gnutls_rwlock_t rwlock)
  60. -{
  61. - if (unlikely(glthread_rwlock_wrlock(rwlock))) {
  62. - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
  63. - }
  64. - return 0;
  65. -}
  66. -
  67. -int
  68. -gnutls_rwlock_unlock(gnutls_rwlock_t rwlock)
  69. -{
  70. - if (unlikely(glthread_rwlock_unlock(rwlock))) {
  71. - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
  72. - }
  73. - return 0;
  74. -}
  75. -
  76. -int
  77. -gnutls_once(gnutls_once_t once, void (*init_func) (void))
  78. -{
  79. - if (unlikely(glthread_once(once, init_func))) {
  80. - return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
  81. - }
  82. - return 0;
  83. -}
  84. diff --git a/lib/locks.h b/lib/locks.h
  85. index a1ff0fa9d7..907adfc134 100644
  86. --- a/lib/locks.h
  87. +++ b/lib/locks.h
  88. @@ -40,8 +40,14 @@ extern mutex_unlock_func gnutls_mutex_unlock;
  89. */
  90. #define GNUTLS_STATIC_MUTEX(lock) gl_lock_define_initialized(static, lock)
  91. typedef gl_lock_t *gnutls_static_mutex_t;
  92. -int gnutls_static_mutex_lock(gnutls_static_mutex_t lock);
  93. -int gnutls_static_mutex_unlock(gnutls_static_mutex_t lock);
  94. +
  95. +#define gnutls_static_mutex_lock(LOCK) \
  96. + (unlikely(glthread_lock_lock(LOCK)) ? \
  97. + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0)
  98. +
  99. +#define gnutls_static_mutex_unlock(LOCK) \
  100. + (unlikely(glthread_lock_unlock(LOCK)) ? \
  101. + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0)
  102. /* Unlike static mutexes, static rwlocks can be locked/unlocked with
  103. * the functions defined below, because there is no way to replace
  104. @@ -50,13 +56,23 @@ int gnutls_static_mutex_unlock(gnutls_static_mutex_t lock);
  105. #define GNUTLS_RWLOCK(rwlock) gl_rwlock_define_initialized(static, rwlock)
  106. typedef gl_rwlock_t *gnutls_rwlock_t;
  107. -int gnutls_rwlock_rdlock(gnutls_rwlock_t rwlock);
  108. -int gnutls_rwlock_wrlock(gnutls_rwlock_t rwlock);
  109. -int gnutls_rwlock_unlock(gnutls_rwlock_t rwlock);
  110. +#define gnutls_rwlock_rdlock(RWLOCK) \
  111. + (unlikely(glthread_rwlock_rdlock(RWLOCK)) ? \
  112. + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0)
  113. +
  114. +#define gnutls_rwlock_wrlock(RWLOCK) \
  115. + (unlikely(glthread_rwlock_wrlock(RWLOCK)) ? \
  116. + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0)
  117. +
  118. +#define gnutls_rwlock_unlock(RWLOCK) \
  119. + (unlikely(glthread_rwlock_unlock(RWLOCK)) ? \
  120. + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0)
  121. #define GNUTLS_ONCE(once) gl_once_define(static, once)
  122. typedef gl_once_t *gnutls_once_t;
  123. -int gnutls_once(gnutls_once_t once, void (*init_func) (void));
  124. +#define gnutls_once(ONCE, INIT_FUNC) \
  125. + (unlikely(glthread_once(ONCE, INIT_FUNC)) ? \
  126. + gnutls_assert_val(GNUTLS_E_LOCKING_ERROR) : 0)
  127. #endif /* GNUTLS_LIB_LOCKS_H */
  128. diff --git a/lib/pkcs11.c b/lib/pkcs11.c
  129. index 8dda0f07c9..ba8ac0c375 100644
  130. --- a/lib/pkcs11.c
  131. +++ b/lib/pkcs11.c
  132. @@ -351,7 +351,7 @@ int _gnutls_pkcs11_check_init(init_level_t req_level, void *priv, pkcs11_reinit_
  133. ret = sret;
  134. cleanup:
  135. - gnutls_static_mutex_unlock(&pkcs11_mutex);
  136. + (void)gnutls_static_mutex_unlock(&pkcs11_mutex);
  137. return ret;
  138. }
  139. diff --git a/lib/priority.c b/lib/priority.c
  140. index d17a923318..55ae185c1f 100644
  141. --- a/lib/priority.c
  142. +++ b/lib/priority.c
  143. @@ -2505,7 +2505,7 @@ static int set_ciphersuite_list(gnutls_priority_t priority_cache)
  144. }
  145. out:
  146. - gnutls_rwlock_unlock(&system_wide_config_rwlock);
  147. + (void)gnutls_rwlock_unlock(&system_wide_config_rwlock);
  148. return ret;
  149. }
  150. diff --git a/lib/verify-tofu.c b/lib/verify-tofu.c
  151. index 40b7acdc8a..97f47385e6 100644
  152. --- a/lib/verify-tofu.c
  153. +++ b/lib/verify-tofu.c
  154. @@ -434,7 +434,7 @@ int store_pubkey(const char *db_name, const char *host,
  155. if (fp != NULL)
  156. fclose(fp);
  157. - gnutls_static_mutex_unlock(&file_mutex);
  158. + (void)gnutls_static_mutex_unlock(&file_mutex);
  159. gnutls_free(b64key.data);
  160. return ret;
  161. --
  162. GitLab