hpyproject / hpy

HPy: a better API for Python
https://hpyproject.org
MIT License
1.02k stars 52 forks source link

Debug mode: add magic header to check structs consistency #294

Open steve-s opened 2 years ago

steve-s commented 2 years ago

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.

fangerer commented 1 year ago

@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.