File tree 2 files changed +20
-3
lines changed
2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -754,6 +754,12 @@ struct _is {
754
754
* and should be placed at the beginning. */
755
755
struct _ceval_state ceval ;
756
756
757
+ /* This structure is carefully allocated so that it's correctly aligned
758
+ * to avoid undefined behaviors during LOAD and STORE. The '_malloced'
759
+ * field stores the allocated pointer address that will later be freed.
760
+ */
761
+ void * _malloced ;
762
+
757
763
PyInterpreterState * next ;
758
764
759
765
int64_t id ;
Original file line number Diff line number Diff line change @@ -569,11 +569,19 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
569
569
return _PyStatus_OK ();
570
570
}
571
571
572
-
573
572
static PyInterpreterState *
574
573
alloc_interpreter (void )
575
574
{
576
- return PyMem_RawCalloc (1 , sizeof (PyInterpreterState ));
575
+ size_t alignment = _Alignof(PyInterpreterState );
576
+ size_t allocsize = sizeof (PyInterpreterState ) + alignment - 1 ;
577
+ void * mem = PyMem_RawCalloc (1 , allocsize );
578
+ if (mem == NULL ) {
579
+ return NULL ;
580
+ }
581
+ PyInterpreterState * interp = _Py_ALIGN_UP (mem , alignment );
582
+ assert (_Py_IS_ALIGNED (interp , alignment ));
583
+ interp -> _malloced = mem ;
584
+ return interp ;
577
585
}
578
586
579
587
static void
@@ -587,12 +595,15 @@ free_interpreter(PyInterpreterState *interp)
587
595
PyMem_RawFree (interp -> obmalloc );
588
596
interp -> obmalloc = NULL ;
589
597
}
590
- PyMem_RawFree (interp );
598
+ assert (_Py_IS_ALIGNED (interp , _Alignof(PyInterpreterState )));
599
+ PyMem_RawFree (interp -> _malloced );
591
600
}
592
601
}
602
+
593
603
#ifndef NDEBUG
594
604
static inline int check_interpreter_whence (long );
595
605
#endif
606
+
596
607
/* Get the interpreter state to a minimal consistent state.
597
608
Further init happens in pylifecycle.c before it can be used.
598
609
All fields not initialized here are expected to be zeroed out,
You can’t perform that action at this time.
0 commit comments