@@ -255,7 +255,8 @@ py_hashentry_table_new(void) {
255255 return NULL ;
256256}
257257
258- /* Module state */
258+ // --- Module state -----------------------------------------------------------
259+
259260static PyModuleDef _hashlibmodule ;
260261
261262typedef struct {
@@ -277,35 +278,33 @@ get_hashlib_state(PyObject *module)
277278 return (_hashlibstate * )state ;
278279}
279280
281+ // --- Module objects ---------------------------------------------------------
282+
280283typedef struct {
281- PyObject_HEAD
284+ HASHLIB_OBJECT_HEAD
282285 EVP_MD_CTX * ctx ; /* OpenSSL message digest context */
283- // Prevents undefined behavior via multiple threads entering the C API.
284- bool use_mutex ;
285- PyMutex mutex ; /* OpenSSL context lock */
286286} HASHobject ;
287287
288288#define HASHobject_CAST (op ) ((HASHobject *)(op))
289289
290290typedef struct {
291- PyObject_HEAD
291+ HASHLIB_OBJECT_HEAD
292292 HMAC_CTX * ctx ; /* OpenSSL hmac context */
293- // Prevents undefined behavior via multiple threads entering the C API.
294- bool use_mutex ;
295- PyMutex mutex ; /* HMAC context lock */
296293} HMACobject ;
297294
298295#define HMACobject_CAST (op ) ((HMACobject *)(op))
299296
300- #include "clinic/_hashopenssl.c.h"
297+ // --- Module clinic configuration --------------------------------------------
298+
301299/*[clinic input]
302300module _hashlib
303- class _hashlib.HASH "HASHobject *" "((_hashlibstate *)PyModule_GetState(module))->HASH_type "
304- class _hashlib.HASHXOF "HASHobject *" "((_hashlibstate *)PyModule_GetState(module))->HASHXOF_type "
305- class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))->HMAC_type "
301+ class _hashlib.HASH "HASHobject *" "&PyType_Type "
302+ class _hashlib.HASHXOF "HASHobject *" "&PyType_Type "
303+ class _hashlib.HMAC "HMACobject *" "&PyType_Type "
306304[clinic start generated code]*/
307- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=eb805ce4b90b1b31 ]*/
305+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6b5c9ce5c28bdc58 ]*/
308306
307+ #include "clinic/_hashopenssl.c.h"
309308
310309/* LCOV_EXCL_START */
311310
@@ -700,9 +699,9 @@ static int
700699_hashlib_HASH_copy_locked (HASHobject * self , EVP_MD_CTX * new_ctx_p )
701700{
702701 int result ;
703- ENTER_HASHLIB (self );
702+ HASHLIB_ACQUIRE_LOCK (self );
704703 result = EVP_MD_CTX_copy (new_ctx_p , self -> ctx );
705- LEAVE_HASHLIB (self );
704+ HASHLIB_RELEASE_LOCK (self );
706705 if (result == 0 ) {
707706 notify_smart_ssl_error_occurred_in (Py_STRINGIFY (EVP_MD_CTX_copy ));
708707 return -1 ;
@@ -802,27 +801,13 @@ _hashlib_HASH_update_impl(HASHobject *self, PyObject *obj)
802801{
803802 int result ;
804803 Py_buffer view ;
805-
806804 GET_BUFFER_VIEW_OR_ERROUT (obj , & view );
807-
808- if (!self -> use_mutex && view .len >= HASHLIB_GIL_MINSIZE ) {
809- self -> use_mutex = true;
810- }
811- if (self -> use_mutex ) {
812- Py_BEGIN_ALLOW_THREADS
813- PyMutex_Lock (& self -> mutex );
814- result = _hashlib_HASH_hash (self , view .buf , view .len );
815- PyMutex_Unlock (& self -> mutex );
816- Py_END_ALLOW_THREADS
817- } else {
818- result = _hashlib_HASH_hash (self , view .buf , view .len );
819- }
820-
805+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED (
806+ self , view .len ,
807+ result = _hashlib_HASH_hash (self , view .buf , view .len )
808+ );
821809 PyBuffer_Release (& view );
822-
823- if (result == -1 )
824- return NULL ;
825- Py_RETURN_NONE ;
810+ return result < 0 ? NULL : Py_None ;
826811}
827812
828813static PyMethodDef HASH_methods [] = {
@@ -1144,15 +1129,12 @@ _hashlib_HASH(PyObject *module, const char *digestname, PyObject *data_obj,
11441129 }
11451130
11461131 if (view .buf && view .len ) {
1147- if (view .len >= HASHLIB_GIL_MINSIZE ) {
1148- /* We do not initialize self->lock here as this is the constructor
1149- * where it is not yet possible to have concurrent access. */
1150- Py_BEGIN_ALLOW_THREADS
1151- result = _hashlib_HASH_hash (self , view .buf , view .len );
1152- Py_END_ALLOW_THREADS
1153- } else {
1154- result = _hashlib_HASH_hash (self , view .buf , view .len );
1155- }
1132+ /* Do not use self->mutex here as this is the constructor
1133+ * where it is not yet possible to have concurrent access. */
1134+ HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED (
1135+ view .len ,
1136+ result = _hashlib_HASH_hash (self , view .buf , view .len )
1137+ );
11561138 if (result == -1 ) {
11571139 assert (PyErr_Occurred ());
11581140 Py_CLEAR (self );
@@ -1813,9 +1795,9 @@ static int
18131795locked_HMAC_CTX_copy (HMAC_CTX * new_ctx_p , HMACobject * self )
18141796{
18151797 int result ;
1816- ENTER_HASHLIB (self );
1798+ HASHLIB_ACQUIRE_LOCK (self );
18171799 result = HMAC_CTX_copy (new_ctx_p , self -> ctx );
1818- LEAVE_HASHLIB (self );
1800+ HASHLIB_RELEASE_LOCK (self );
18191801 if (result == 0 ) {
18201802 notify_smart_ssl_error_occurred_in (Py_STRINGIFY (HMAC_CTX_copy ));
18211803 return -1 ;
@@ -1846,24 +1828,12 @@ _hmac_update(HMACobject *self, PyObject *obj)
18461828 Py_buffer view = {0 };
18471829
18481830 GET_BUFFER_VIEW_OR_ERROR (obj , & view , return 0 );
1849-
1850- if (!self -> use_mutex && view .len >= HASHLIB_GIL_MINSIZE ) {
1851- self -> use_mutex = true;
1852- }
1853- if (self -> use_mutex ) {
1854- Py_BEGIN_ALLOW_THREADS
1855- PyMutex_Lock (& self -> mutex );
1856- r = HMAC_Update (self -> ctx ,
1857- (const unsigned char * )view .buf ,
1858- (size_t )view .len );
1859- PyMutex_Unlock (& self -> mutex );
1860- Py_END_ALLOW_THREADS
1861- } else {
1862- r = HMAC_Update (self -> ctx ,
1863- (const unsigned char * )view .buf ,
1864- (size_t )view .len );
1865- }
1866-
1831+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED (
1832+ self , view .len ,
1833+ r = HMAC_Update (
1834+ self -> ctx , (const unsigned char * )view .buf , (size_t )view .len
1835+ )
1836+ );
18671837 PyBuffer_Release (& view );
18681838
18691839 if (r == 0 ) {
0 commit comments