Skip to content

Commit 923c05f

Browse files
committed
restore GIL_MINSIZE
1 parent bfb5436 commit 923c05f

10 files changed

Lines changed: 130 additions & 1 deletion

File tree

Lib/test/support/hashlib_helper.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import hashlib
3+
import importlib
34
import unittest
45
from test.support.import_helper import import_module
56

@@ -307,3 +308,22 @@ def sha3_384(self):
307308
@property
308309
def sha3_512(self):
309310
return self._find_constructor_in("_sha3","sha3_512")
311+
312+
313+
def find_gil_minsize(modules_names, default=2048):
314+
"""Get the largest GIL_MINSIZE value for the given cryptographic modules.
315+
316+
The valid module names are the following:
317+
318+
- _hashlib
319+
- _md5, _sha1, _sha2, _sha3, _blake2
320+
- _hmac
321+
"""
322+
sizes = []
323+
for module_name in modules_names:
324+
try:
325+
module = importlib.import_module(module_name)
326+
except ImportError:
327+
continue
328+
sizes.append(getattr(module, '_GIL_MINSIZE', default))
329+
return max(sizes, default=default)

Lib/test/test_hashlib.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import unittest
2121
from test import support
2222
from test.support import _4G, bigmemtest
23+
from test.support import hashlib_helper
2324
from test.support.import_helper import import_fresh_module
2425
from test.support import requires_resource
2526
from test.support import threading_helper
@@ -408,7 +409,7 @@ def test_large_update(self):
408409
aas = b'a' * 128
409410
bees = b'b' * 127
410411
cees = b'c' * 126
411-
dees = b'd' * 2048
412+
dees = b'd' * 2048 # HASHLIB_GIL_MINSIZE
412413

413414
for cons in self.hash_constructors:
414415
m1 = cons(usedforsecurity=False)
@@ -989,6 +990,40 @@ def test_case_shake256_vector(self):
989990
for msg, md in read_vectors('shake_256'):
990991
self.check('shake_256', msg, md, True)
991992

993+
def test_gil(self):
994+
# Check things work fine with an input larger than the size required
995+
# for multithreaded operation. Currently, all cryptographic modules
996+
# have the same constant value (2048) but in the future it might not
997+
# be the case.
998+
mods = ['_md5', '_sha1', '_sha2', '_sha3', '_blake2', '_hashlib']
999+
gil_minsize = hashlib_helper.find_gil_minsize(mods)
1000+
for cons in self.hash_constructors:
1001+
# constructors belong to one of the above modules
1002+
m = cons(usedforsecurity=False)
1003+
m.update(b'1')
1004+
m.update(b'#' * gil_minsize)
1005+
m.update(b'1')
1006+
1007+
m = cons(b'x' * gil_minsize, usedforsecurity=False)
1008+
m.update(b'1')
1009+
1010+
def test_sha256_gil(self):
1011+
gil_minsize = hashlib_helper.find_gil_minsize(['_sha2', '_hashlib'])
1012+
m = hashlib.sha256()
1013+
m.update(b'1')
1014+
m.update(b'#' * gil_minsize)
1015+
m.update(b'1')
1016+
self.assertEqual(
1017+
m.hexdigest(),
1018+
'1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94'
1019+
)
1020+
1021+
m = hashlib.sha256(b'1' + b'#' * gil_minsize + b'1')
1022+
self.assertEqual(
1023+
m.hexdigest(),
1024+
'1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94'
1025+
)
1026+
9921027
@threading_helper.reap_threads
9931028
@threading_helper.requires_working_threading()
9941029
def test_threaded_hashing(self):

Lib/test/test_hmac.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,11 @@ def HMAC(self, key, msg=None):
11331133
"""Create a HMAC object."""
11341134
raise NotImplementedError
11351135

1136+
@property
1137+
def gil_minsize(self):
1138+
"""Get the maximal input length for the GIL to be held."""
1139+
raise NotImplementedError
1140+
11361141
def check_update(self, key, chunks):
11371142
chunks = list(chunks)
11381143
msg = b''.join(chunks)
@@ -1150,6 +1155,13 @@ def test_update(self):
11501155
with self.subTest(key=key, msg=msg):
11511156
self.check_update(key, [msg])
11521157

1158+
def test_update_large(self):
1159+
gil_minsize = self.gil_minsize
1160+
key = random.randbytes(16)
1161+
top = random.randbytes(gil_minsize + 1)
1162+
bot = random.randbytes(gil_minsize + 1)
1163+
self.check_update(key, [top, bot])
1164+
11531165
def test_update_exceptions(self):
11541166
h = self.HMAC(b"key")
11551167
for msg in ['invalid msg', 123, (), []]:
@@ -1163,13 +1175,21 @@ class PyUpdateTestCase(PyModuleMixin, UpdateTestCaseMixin, unittest.TestCase):
11631175
def HMAC(self, key, msg=None):
11641176
return self.hmac.HMAC(key, msg, digestmod='sha256')
11651177

1178+
@property
1179+
def gil_minsize(self):
1180+
return sha2._GIL_MINSIZE
1181+
11661182

11671183
@hashlib_helper.requires_openssl_hashdigest('sha256')
11681184
class OpenSSLUpdateTestCase(UpdateTestCaseMixin, unittest.TestCase):
11691185

11701186
def HMAC(self, key, msg=None):
11711187
return _hashlib.hmac_new(key, msg, digestmod='sha256')
11721188

1189+
@property
1190+
def gil_minsize(self):
1191+
return _hashlib._GIL_MINSIZE
1192+
11731193

11741194
class BuiltinUpdateTestCase(BuiltinModuleMixin,
11751195
UpdateTestCaseMixin, unittest.TestCase):
@@ -1179,6 +1199,10 @@ def HMAC(self, key, msg=None):
11791199
# are still built, making it possible to use SHA-2 hashes.
11801200
return self.hmac.new(key, msg, digestmod='sha256')
11811201

1202+
@property
1203+
def gil_minsize(self):
1204+
return self.hmac._GIL_MINSIZE
1205+
11821206

11831207
class CopyBaseTestCase:
11841208

Modules/_hashopenssl.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,17 @@ hashlib_exception(PyObject *module)
24582458
return 0;
24592459
}
24602460

2461+
static int
2462+
hashlib_constants(PyObject *module)
2463+
{
2464+
if (PyModule_AddIntConstant(module, "_GIL_MINSIZE",
2465+
HASHLIB_GIL_MINSIZE) < 0)
2466+
{
2467+
return -1;
2468+
}
2469+
return 0;
2470+
}
2471+
24612472
static PyModuleDef_Slot hashlib_slots[] = {
24622473
{Py_mod_exec, hashlib_init_hashtable},
24632474
{Py_mod_exec, hashlib_init_HASH_type},
@@ -2466,6 +2477,7 @@ static PyModuleDef_Slot hashlib_slots[] = {
24662477
{Py_mod_exec, hashlib_md_meth_names},
24672478
{Py_mod_exec, hashlib_init_constructors},
24682479
{Py_mod_exec, hashlib_exception},
2480+
{Py_mod_exec, hashlib_constants},
24692481
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
24702482
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
24712483
{0, NULL}

Modules/blake2module.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ blake2_exec(PyObject *m)
227227
} \
228228
} while (0)
229229

230+
ADD_INT_CONST("_GIL_MINSIZE", HASHLIB_GIL_MINSIZE);
231+
230232
st->blake2b_type = (PyTypeObject *)PyType_FromModuleAndSpec(
231233
m, &blake2b_type_spec, NULL);
232234

Modules/hmacmodule.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,20 @@ hmacmodule_init_strings(hmacmodule_state *state)
15341534
return 0;
15351535
}
15361536

1537+
static int
1538+
hmacmodule_init_globals(PyObject *module, hmacmodule_state *state)
1539+
{
1540+
#define ADD_INT_CONST(NAME, VALUE) \
1541+
do { \
1542+
if (PyModule_AddIntConstant(module, (NAME), (VALUE)) < 0) { \
1543+
return -1; \
1544+
} \
1545+
} while (0)
1546+
ADD_INT_CONST("_GIL_MINSIZE", HASHLIB_GIL_MINSIZE);
1547+
#undef ADD_INT_CONST
1548+
return 0;
1549+
}
1550+
15371551
static void
15381552
hmacmodule_init_cpu_features(hmacmodule_state *state)
15391553
{
@@ -1624,6 +1638,9 @@ hmacmodule_exec(PyObject *module)
16241638
if (hmacmodule_init_strings(state) < 0) {
16251639
return -1;
16261640
}
1641+
if (hmacmodule_init_globals(module, state) < 0) {
1642+
return -1;
1643+
}
16271644
hmacmodule_init_cpu_features(state);
16281645
return 0;
16291646
}

Modules/md5module.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ md5_exec(PyObject *m)
352352
if (PyModule_AddObjectRef(m, "MD5Type", (PyObject *)st->md5_type) < 0) {
353353
return -1;
354354
}
355+
if (PyModule_AddIntConstant(m, "_GIL_MINSIZE", HASHLIB_GIL_MINSIZE) < 0) {
356+
return -1;
357+
}
355358

356359
return 0;
357360
}

Modules/sha1module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ _sha1_exec(PyObject *module)
355355
{
356356
return -1;
357357
}
358+
if (PyModule_AddIntConstant(module,
359+
"_GIL_MINSIZE",
360+
HASHLIB_GIL_MINSIZE) < 0)
361+
{
362+
return -1;
363+
}
358364

359365
return 0;
360366
}

Modules/sha2module.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,13 @@ static int sha2_exec(PyObject *module)
877877
return -1;
878878
}
879879

880+
if (PyModule_AddIntConstant(module,
881+
"_GIL_MINSIZE",
882+
HASHLIB_GIL_MINSIZE) < 0)
883+
{
884+
return -1;
885+
}
886+
880887
return 0;
881888
}
882889

Modules/sha3module.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ _sha3_exec(PyObject *m)
630630
if (PyModule_AddStringConstant(m, "implementation", "HACL") < 0) {
631631
return -1;
632632
}
633+
if (PyModule_AddIntConstant(m, "_GIL_MINSIZE", HASHLIB_GIL_MINSIZE) < 0) {
634+
return -1;
635+
}
633636

634637
return 0;
635638
}

0 commit comments

Comments
 (0)