Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-99108: Release the GIL around hashlib built-in computation #104675

Merged
merged 7 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
We now release the GIL around built-in :mod:`hashlib` computations of
reasonable size for the SHA families and MD5 hash functions, matching
what our OpenSSL backed hash computations already does.
2 changes: 1 addition & 1 deletion Modules/hashlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
PyThread_release_lock((obj)->lock); \
}

/* TODO(gps): We should probably make this a module or EVPobject attribute
/* TODO(gpshead): We should make this a module or class attribute
* to allow the user to optimize based on the platform they're using. */
#define HASHLIB_GIL_MINSIZE 2048

33 changes: 30 additions & 3 deletions Modules/md5module.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef long long MD5_INT64; /* 64-bit integer */

typedef struct {
PyObject_HEAD

PyThread_type_lock lock;
Hacl_Streaming_MD5_state *hash_state;
} MD5object;

Expand All @@ -72,6 +72,7 @@ static MD5object *
newMD5object(MD5State * st)
{
MD5object *md5 = (MD5object *)PyObject_GC_New(MD5object, st->md5_type);
md5->lock = NULL;
gpshead marked this conversation as resolved.
Show resolved Hide resolved
PyObject_GC_Track(md5);
return md5;
}
Expand All @@ -88,6 +89,9 @@ static void
MD5_dealloc(MD5object *ptr)
{
Hacl_Streaming_MD5_legacy_free(ptr->hash_state);
if (ptr->lock != NULL) {
PyThread_free_lock(ptr->lock);
}
PyTypeObject *tp = Py_TYPE(ptr);
PyObject_GC_UnTrack(ptr);
PyObject_GC_Del(ptr);
Expand Down Expand Up @@ -115,7 +119,9 @@ MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
if ((newobj = newMD5object(st))==NULL)
return NULL;

ENTER_HASHLIB(self);
newobj->hash_state = Hacl_Streaming_MD5_legacy_copy(self->hash_state);
LEAVE_HASHLIB(self);
return (PyObject *)newobj;
}

Expand All @@ -130,7 +136,9 @@ MD5Type_digest_impl(MD5object *self)
/*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/
{
unsigned char digest[MD5_DIGESTSIZE];
ENTER_HASHLIB(self);
Hacl_Streaming_MD5_legacy_finish(self->hash_state, digest);
LEAVE_HASHLIB(self);
return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
}

Expand All @@ -145,7 +153,9 @@ MD5Type_hexdigest_impl(MD5object *self)
/*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
{
unsigned char digest[MD5_DIGESTSIZE];
ENTER_HASHLIB(self);
Hacl_Streaming_MD5_legacy_finish(self->hash_state, digest);
LEAVE_HASHLIB(self);
return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);
}

Expand Down Expand Up @@ -177,7 +187,18 @@ MD5Type_update(MD5object *self, PyObject *obj)

GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);

update(self->hash_state, buf.buf, buf.len);
if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
self->lock = PyThread_allocate_lock();
}
if (self->lock != NULL) {
Py_BEGIN_ALLOW_THREADS
PyThread_acquire_lock(self->lock, 1);
gpshead marked this conversation as resolved.
Show resolved Hide resolved
update(self->hash_state, buf.buf, buf.len);
PyThread_release_lock(self->lock);
Py_END_ALLOW_THREADS
} else {
update(self->hash_state, buf.buf, buf.len);
}

PyBuffer_Release(&buf);
Py_RETURN_NONE;
Expand Down Expand Up @@ -279,7 +300,13 @@ _md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity)
return NULL;
}
if (string) {
update(new->hash_state, buf.buf, buf.len);
if (buf.len >= HASHLIB_GIL_MINSIZE) {
gpshead marked this conversation as resolved.
Show resolved Hide resolved
Py_BEGIN_ALLOW_THREADS
update(new->hash_state, buf.buf, buf.len);
Py_END_ALLOW_THREADS
} else {
update(new->hash_state, buf.buf, buf.len);
}
PyBuffer_Release(&buf);
}

Expand Down
33 changes: 30 additions & 3 deletions Modules/sha1module.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef long long SHA1_INT64; /* 64-bit integer */

typedef struct {
PyObject_HEAD

PyThread_type_lock lock;
Hacl_Streaming_SHA1_state *hash_state;
} SHA1object;

Expand All @@ -71,6 +71,7 @@ static SHA1object *
newSHA1object(SHA1State *st)
{
SHA1object *sha = (SHA1object *)PyObject_GC_New(SHA1object, st->sha1_type);
sha->lock = NULL;
PyObject_GC_Track(sha);
return sha;
}
Expand All @@ -88,6 +89,9 @@ static void
SHA1_dealloc(SHA1object *ptr)
{
Hacl_Streaming_SHA1_legacy_free(ptr->hash_state);
if (ptr->lock != NULL) {
PyThread_free_lock(ptr->lock);
}
PyTypeObject *tp = Py_TYPE(ptr);
PyObject_GC_UnTrack(ptr);
PyObject_GC_Del(ptr);
Expand Down Expand Up @@ -115,7 +119,9 @@ SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls)
if ((newobj = newSHA1object(st)) == NULL)
return NULL;

ENTER_HASHLIB(self);
newobj->hash_state = Hacl_Streaming_SHA1_legacy_copy(self->hash_state);
LEAVE_HASHLIB(self);
return (PyObject *)newobj;
}

Expand All @@ -130,7 +136,9 @@ SHA1Type_digest_impl(SHA1object *self)
/*[clinic end generated code: output=2f05302a7aa2b5cb input=13824b35407444bd]*/
{
unsigned char digest[SHA1_DIGESTSIZE];
ENTER_HASHLIB(self);
Hacl_Streaming_SHA1_legacy_finish(self->hash_state, digest);
LEAVE_HASHLIB(self);
return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
}

Expand All @@ -145,7 +153,9 @@ SHA1Type_hexdigest_impl(SHA1object *self)
/*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/
{
unsigned char digest[SHA1_DIGESTSIZE];
ENTER_HASHLIB(self);
Hacl_Streaming_SHA1_legacy_finish(self->hash_state, digest);
LEAVE_HASHLIB(self);
return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE);
}

Expand Down Expand Up @@ -177,7 +187,18 @@ SHA1Type_update(SHA1object *self, PyObject *obj)

GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);

update(self->hash_state, buf.buf, buf.len);
if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
self->lock = PyThread_allocate_lock();
}
if (self->lock != NULL) {
Py_BEGIN_ALLOW_THREADS
PyThread_acquire_lock(self->lock, 1);
update(self->hash_state, buf.buf, buf.len);
PyThread_release_lock(self->lock);
Py_END_ALLOW_THREADS
} else {
update(self->hash_state, buf.buf, buf.len);
}

PyBuffer_Release(&buf);
Py_RETURN_NONE;
Expand Down Expand Up @@ -279,7 +300,13 @@ _sha1_sha1_impl(PyObject *module, PyObject *string, int usedforsecurity)
return NULL;
}
if (string) {
update(new->hash_state, buf.buf, buf.len);
if (buf.len >= HASHLIB_GIL_MINSIZE) {
Py_BEGIN_ALLOW_THREADS
update(new->hash_state, buf.buf, buf.len);
Py_END_ALLOW_THREADS
} else {
update(new->hash_state, buf.buf, buf.len);
}
PyBuffer_Release(&buf);
}

Expand Down
Loading