FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-122928: asserting that the tstate pointer is non-NULL using assert by Wulian233 · Pull Request #122929 · python/cpython · GitHub

/ cpython Public

gh-122928: asserting that the tstate pointer is non-NULL using assert - #122929

Closed
Wulian233 wants to merge 5 commits into
python:mainfrom
Wulian233:tstate
Closed

gh-122928: asserting that the tstate pointer is non-NULL using assert#122929
Wulian233 wants to merge 5 commits into
python:mainfrom
Wulian233:tstate

Conversation

Wulian233 commented Aug 12, 2024
edited
Loading

Copy link
Copy Markdown
Contributor

To prevent dangling pointer issues, adding a check for NULL ensures that invalid pointers are not dereferenced

This is my first time contributing to the C part of python and maybe there will be some issues

Copy link
Copy Markdown
Member

I'm not totally sure this fix is necessary :(

A NULL check doesn't prevent any problems with dangling pointers, since they are non-NULL.

Comment thread Python/pystate.c

kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Adding NULL pointer checks doesn't prevent dangling pointers.

bedevere-app Bot commented Aug 12, 2024

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

ZeroIntensity commented Aug 12, 2024
edited
Loading

Copy link
Copy Markdown
Member

It might be best to just turn this into an assertion assert(tstate != NULL)

Wulian233 commented Aug 13, 2024
edited
Loading

Copy link
Copy Markdown
Contributor Author

Can I solve this by

int
_PyThreadState_MustExit(PyThreadState *tstate)
{
    static inline PyThreadState *
    get_current_tstate(void)
    {
        PyThreadState *tstate = _PyThreadState_GET();
        if (tstate == NULL) {
            (void)check_interp(NULL);
            return NULL;
        }
        return check_interp(tstate->interp) ? tstate : NULL;
    }

    PyThreadState *current_tstate = get_current_tstate();
    if (current_tstate == NULL) {
        return 1;
    }

   ...
}

(code from https://github.com/python/cpython/blob/main/Python/_warnings.c#L45-L45)

Copy link
Copy Markdown
Member

No, check_interp just makes sure that it's non-NULL, not dangling. It's very difficult to safely detect if something is an invalid pointer at runtime, you should leave that to tools like valgrind. (FWIW, C doesn't support inline functions like in your snippet.)

Comment thread Python/pystate.c Outdated

Copy link
Copy Markdown
Contributor Author

I see. Finally, let's use assert(tstate! = NULL) Thank you for your review

Copy link
Copy Markdown
Member

FWIW, I think this should be a "skip news."
We are just enforcing a pre-condition, as @picnixz mentioned, users will not see these changes.

Copy link
Copy Markdown
Contributor Author

deleted

ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good, now we're just validating a precondition. @kumaraditya303, could you re-review?

Wulian233 changed the title gh-122928: Make thread state(tstate) pointer safe gh-122928: asserting that the tstate pointer is non-NULL using assert Aug 15, 2024

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This change is wrong. There are exactly 4 calls to this function. 1 in _threadmodule.c, 3 in ceval_gil.c. All code paths check that tstate is not NULL before calling _PyThreadState_MustExit().

Moreover, I don't understand which problem you are trying to solve here. Dangling pointers cannot be avoided by checking a pointer value (NULL or anything else).

vstinner closed this Sep 18, 2024

Copy link
Copy Markdown
Member

Yeah, this was originally conceived as a way to prevent dangling pointers, but there was some misunderstanding on the difference between a NULL and dangling pointer. It eventually just got turned into an assertion.

Wulian233 deleted the tstate branch September 18, 2024 22:20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL