In the debug mode HPyType_FromSpec should extend the user provided structure with a header containing magic number to check consistency (and possibly some additional debug info if there is any use for it). This is only possible for non-legacy types. Debug mode HPy_AsStruct will have to adjust the returned pointer, but will also check the magic. If the magic does not match, then the memory is either corrupted or someone is trying to access legacy type data via HPy_AsStruct instead of `HPy_AsStructLegacy, which are both nasty bugs useful to discover quickly. We can maybe sprinkle the check of the magic across other debug context API calls: e.g., when closing handles.
Note sure if we could insert some magic footer too, there may be issues with getting the alignment right.
@steve-s I think my PR #364 (which is already merged) solves the problem of invalid HPy_AsStruct_* usages. However, it does not cover the memory corruption case. Anyway, you may want to refine this issue.
In the debug mode
HPyType_FromSpec
should extend the user provided structure with a header containing magic number to check consistency (and possibly some additional debug info if there is any use for it). This is only possible for non-legacy types. Debug modeHPy_AsStruct
will have to adjust the returned pointer, but will also check the magic. If the magic does not match, then the memory is either corrupted or someone is trying to access legacy type data viaHPy_AsStruct
instead of`HPy_AsStructLegacy
, which are both nasty bugs useful to discover quickly. We can maybe sprinkle the check of the magic across other debug context API calls: e.g., when closing handles.Note sure if we could insert some magic footer too, there may be issues with getting the alignment right.