Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug mode: detect HPy_Close/return of context constants #303

Merged
merged 3 commits into from
Sep 30, 2022

Conversation

steve-s
Copy link
Contributor

@steve-s steve-s commented Mar 10, 2022

Tags handles that are used as context constants (e.g., h_None) and adds checks that guards against HPy_Close(ctx, ctx->h_None) or against return ctx->h_None.

@@ -75,7 +85,7 @@ void DHPy_invalid_handle(HPyContext *dctx, DHPy dh)
{
HPyDebugInfo *info = get_info(dctx);
HPyContext *uctx = info->uctx;
assert(as_DebugHandle(dh)->is_closed);
assert(as_DebugHandle(dh)->is_closed || as_DebugHandle(dh)->is_context_constant);
if (HPy_IsNull(info->uh_on_invalid_handle)) {
// default behavior: print an error and abort
HPy_FatalError(uctx, "Invalid usage of already closed handle");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error messages could be more specific (also for other scenarios) and we could pass some ID of the error to the user provided hook, but I am leaving that for followup work

@steve-s steve-s force-pushed the ss/debug-check-ctx-constants branch from 5de4bbe to 4f96eab Compare March 11, 2022 11:57
@steve-s
Copy link
Contributor Author

steve-s commented Mar 11, 2022

Note: rebased on master, it seems the infer error was fixed on master meanwhile...

@@ -15,6 +15,8 @@ HPyDef_METH(new_generation, "new_generation", new_generation_impl, HPyFunc_NOARG
static UHPy new_generation_impl(HPyContext *uctx, UHPy self)
{
HPyContext *dctx = hpy_debug_get_ctx(uctx);
if (dctx == NULL)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks were added after error report from infer. https://github.com/hpyproject/hpy/actions/runs/1968620957/jobs/2813414188

Copy link
Contributor Author

@steve-s steve-s Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should really abort in hpy_debug_ctx_get instead of returning NULL:

{
    HPyContext *dctx = &g_debug_ctx;
    if (uctx == dctx) {
        HPy_FatalError(uctx, "hpy_debug_get_ctx: expected an universal ctx, "
                             "got a debug ctx");
    }
    if (hpy_debug_ctx_init(dctx, uctx) < 0) {
        HPyErr_SetString(uctx, uctx->h_SystemError, "Could not create debug context");
        return NULL;
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current solution has the nice property is that if you import a module and for some reason the debug ctx cannot be created, you get a nice Python exception, but I see why it causes problems with infer.
Maybe we could switch to a different scheme in which you have a clearly separate init phase (which can raise) and then an "usage phase" (in which we assume that the dctx has been successfully created and thus we can assert that it's not NULL.

That said, I don't think it's important to do it in this PR, but i'ts good to keep in mind for the future

@steve-s
Copy link
Contributor Author

steve-s commented Sep 21, 2022

Rebased on master. @antocuni can we take a look at reviewing this? :-)

Copy link
Collaborator

@antocuni antocuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant idea!
I vote for calling them "immortal" handles, but apart from this, very good job :)

@@ -15,6 +15,8 @@ HPyDef_METH(new_generation, "new_generation", new_generation_impl, HPyFunc_NOARG
static UHPy new_generation_impl(HPyContext *uctx, UHPy self)
{
HPyContext *dctx = hpy_debug_get_ctx(uctx);
if (dctx == NULL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current solution has the nice property is that if you import a module and for some reason the debug ctx cannot be created, you get a nice Python exception, but I see why it causes problems with infer.
Maybe we could switch to a different scheme in which you have a clearly separate init phase (which can raise) and then an "usage phase" (in which we assume that the dctx has been successfully created and thus we can assert that it's not NULL.

That said, I don't think it's important to do it in this PR, but i'ts good to keep in mind for the future

@@ -47,7 +47,7 @@ def generate(self):
w('{')
for var in self.api.variables:
name = var.name
w(f' dctx->{name} = DHPy_open(dctx, uctx->{name});')
w(f' dctx->{name} = DHPy_open_context_constant(dctx, uctx->{name});')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the idea, but context_constant is a bit weird IMHO. What about DHPy_open_immortal?

@steve-s
Copy link
Contributor Author

steve-s commented Sep 23, 2022

Rebased on master and changed the name to "immortal"

@steve-s steve-s merged commit d0b1454 into hpyproject:master Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants