Skip to content

Commit ee647de

Browse files
ncoghlanericsnowcurrently
authored andcommitted
Rename random.c to bootstrap_hash.c.
1 parent 406d3d2 commit ee647de

File tree

7 files changed

+66
-36
lines changed

7 files changed

+66
-36
lines changed

Include/pylifecycle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ PyAPI_FUNC(void) _PyImportHooks_Init(void);
8484
PyAPI_FUNC(int) _PyFrame_Init(void);
8585
PyAPI_FUNC(int) _PyFloat_Init(void);
8686
PyAPI_FUNC(int) PyByteArray_Init(void);
87-
PyAPI_FUNC(void) _PyRandom_Init(void);
87+
PyAPI_FUNC(void) _Py_HashRandomization_Init(void);
8888
#endif
8989

9090
/* Various internal finalizers */
@@ -106,7 +106,7 @@ PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
106106
PyAPI_FUNC(void) _PyGC_Fini(void);
107107
PyAPI_FUNC(void) PySlice_Fini(void);
108108
PyAPI_FUNC(void) _PyType_Fini(void);
109-
PyAPI_FUNC(void) _PyRandom_Fini(void);
109+
PyAPI_FUNC(void) _Py_HashRandomization_Fini(void);
110110
PyAPI_FUNC(void) PyAsyncGen_Fini(void);
111111

112112
PyAPI_DATA(PyThreadState *) _Py_Finalizing;

Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ PYTHON_OBJS= \
349349
Python/pystate.o \
350350
Python/pythonrun.o \
351351
Python/pytime.o \
352-
Python/random.o \
352+
Python/bootstrap_hash.o \
353353
Python/structmember.o \
354354
Python/symtable.o \
355355
Python/sysmodule.o \

Modules/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ Py_Main(int argc, wchar_t **argv)
395395
}
396396

397397
Py_HashRandomizationFlag = 1;
398-
_PyRandom_Init();
398+
_Py_HashRandomization_Init();
399399

400400
PySys_ResetWarnOptions();
401401
_PyOS_ResetGetOpt();

PCbuild/pythoncore.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
44
<ProjectConfiguration Include="Debug|Win32">
@@ -349,11 +349,11 @@
349349
<ClCompile Include="..\PC\getpathp.c" />
350350
<ClCompile Include="..\PC\msvcrtmodule.c" />
351351
<ClCompile Include="..\Python\pyhash.c" />
352-
<ClCompile Include="..\Python\random.c" />
353352
<ClCompile Include="..\Python\_warnings.c" />
354353
<ClCompile Include="..\Python\asdl.c" />
355354
<ClCompile Include="..\Python\ast.c" />
356355
<ClCompile Include="..\Python\bltinmodule.c" />
356+
<ClCompile Include="..\Python\bootstrap_hash.c" />
357357
<ClCompile Include="..\Python\ceval.c" />
358358
<ClCompile Include="..\Python\codecs.c" />
359359
<ClCompile Include="..\Python\compile.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
44
<Filter Include="Include">
@@ -971,7 +971,7 @@
971971
<ClCompile Include="..\Python\traceback.c">
972972
<Filter>Python</Filter>
973973
</ClCompile>
974-
<ClCompile Include="..\Python\random.c">
974+
<ClCompile Include="..\Python\bootstrap_hash.c">
975975
<Filter>Python</Filter>
976976
</ClCompile>
977977
<ClCompile Include="..\Modules\_winapi.c">

Python/random.c renamed to Python/bootstrap_hash.c

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -533,44 +533,57 @@ _PyOS_URandomNonblock(void *buffer, Py_ssize_t size)
533533
return pyurandom(buffer, size, 0, 1);
534534
}
535535

536-
void
537-
_PyRandom_Init(void)
536+
int Py_ReadHashSeed(char *seed_text,
537+
int *use_hash_seed,
538+
unsigned long *hash_seed)
538539
{
539-
char *env;
540-
unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
541-
Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
542540
Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
543-
544-
if (_Py_HashSecret_Initialized)
545-
return;
546-
_Py_HashSecret_Initialized = 1;
547-
548-
/*
549-
Hash randomization is enabled. Generate a per-process secret,
550-
using PYTHONHASHSEED if provided.
551-
*/
552-
553-
env = Py_GETENV("PYTHONHASHSEED");
554-
if (env && *env != '\0' && strcmp(env, "random") != 0) {
555-
char *endptr = env;
541+
/* Convert a text seed to a numeric one */
542+
if (seed_text && *seed_text != '\0' && strcmp(seed_text, "random") != 0) {
543+
char *endptr = seed_text;
556544
unsigned long seed;
557-
seed = strtoul(env, &endptr, 10);
545+
seed = strtoul(seed_text, &endptr, 10);
558546
if (*endptr != '\0'
559547
|| seed > 4294967295UL
560548
|| (errno == ERANGE && seed == ULONG_MAX))
561549
{
562-
Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
563-
"in range [0; 4294967295]");
550+
return -1;
564551
}
565-
if (seed == 0) {
552+
/* Use a specific hash */
553+
*use_hash_seed = 1;
554+
*hash_seed = seed;
555+
}
556+
else {
557+
/* Use a random hash */
558+
*use_hash_seed = 0;
559+
*hash_seed = 0;
560+
}
561+
return 0;
562+
}
563+
564+
static void
565+
init_hash_secret(int use_hash_seed,
566+
unsigned long hash_seed)
567+
{
568+
void *secret = &_Py_HashSecret;
569+
Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
570+
571+
if (_Py_HashSecret_Initialized)
572+
return;
573+
_Py_HashSecret_Initialized = 1;
574+
575+
if (use_hash_seed) {
576+
if (hash_seed == 0) {
566577
/* disable the randomized hash */
567578
memset(secret, 0, secret_size);
568579
}
569580
else {
570-
lcg_urandom(seed, secret, secret_size);
581+
/* use the specified hash seed */
582+
lcg_urandom(hash_seed, secret, secret_size);
571583
}
572584
}
573585
else {
586+
/* use a random hash seed */
574587
int res;
575588

576589
/* _PyRandom_Init() is called very early in the Python initialization
@@ -586,7 +599,24 @@ _PyRandom_Init(void)
586599
}
587600

588601
void
589-
_PyRandom_Fini(void)
602+
_Py_HashRandomization_Init(void)
603+
{
604+
char *seed_text;
605+
int use_hash_seed = -1;
606+
unsigned long hash_seed;
607+
608+
if (use_hash_seed < 0) {
609+
seed_text = Py_GETENV("PYTHONHASHSEED");
610+
if (Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {
611+
Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
612+
"in range [0; 4294967295]");
613+
}
614+
}
615+
init_hash_secret(use_hash_seed, hash_seed);
616+
}
617+
618+
void
619+
_Py_HashRandomization_Fini(void)
590620
{
591621
#ifdef MS_WINDOWS
592622
if (hCryptProv) {

Python/pylifecycle.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
334334
Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
335335
if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
336336
Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
337-
/* The variable is only tested for existence here; _PyRandom_Init will
338-
check its value further. */
337+
/* The variable is only tested for existence here;
338+
_Py_HashRandomization_Init will check its value further. */
339339
if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
340340
Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
341341
#ifdef MS_WINDOWS
@@ -345,7 +345,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
345345
Py_LegacyWindowsStdioFlag = add_flag(Py_LegacyWindowsStdioFlag, p);
346346
#endif
347347

348-
_PyRandom_Init();
348+
_Py_HashRandomization_Init();
349349

350350
interp = PyInterpreterState_New();
351351
if (interp == NULL)
@@ -696,7 +696,7 @@ Py_FinalizeEx(void)
696696
PyDict_Fini();
697697
PySlice_Fini();
698698
_PyGC_Fini();
699-
_PyRandom_Fini();
699+
_Py_HashRandomization_Fini();
700700
_PyArg_Fini();
701701
PyAsyncGen_Fini();
702702

0 commit comments

Comments
 (0)