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

Fatal error in type union #88649

Closed
serhiy-storchaka opened this issue Jun 22, 2021 · 6 comments
Closed

Fatal error in type union #88649

serhiy-storchaka opened this issue Jun 22, 2021 · 6 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@serhiy-storchaka
Copy link
Member

BPO 44483
Nosy @serhiy-storchaka, @miss-islington, @Fidget-Spinner
PRs
  • bpo-44483: Fix crash in union object with bad __module__ #26848
  • [3.10] bpo-44483: Fix crash in union object with bad __module__ (GH-26848) #26852
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2021-06-23.09:44:11.996>
    created_at = <Date 2021-06-22.06:32:26.859>
    labels = ['interpreter-core', '3.10', 'type-crash', '3.11']
    title = 'Fatal error in type union'
    updated_at = <Date 2021-06-23.09:55:58.266>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-06-23.09:55:58.266>
    actor = 'kj'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2021-06-23.09:44:11.996>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2021-06-22.06:32:26.859>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44483
    keywords = ['patch']
    message_count = 6.0
    messages = ['396307', '396320', '396333', '396395', '396396', '396398']
    nosy_count = 3.0
    nosy_names = ['serhiy.storchaka', 'miss-islington', 'kj']
    pr_nums = ['26848', '26852']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue44483'
    versions = ['Python 3.10', 'Python 3.11']

    @serhiy-storchaka
    Copy link
    Member Author

    The following example crashes:

    class TypeVar:
        @property
        def __module__(self):
            1/0

    str | TypeVar()

    Output:
    Fatal Python error: _Py_CheckSlotResult: Slot | of type type succeeded with an exception set
    Python runtime state: initialized
    Traceback (most recent call last):
      File "<stdin>", line 4, in __module__
    ZeroDivisionError: division by zero
    Aborted (core dumped)

    The problem in Objects/unionobject.c is that is_typing_module() (and therefore is_typevar() and is_special_form()) can return not only 0 and 1, but -1 as a signal of error, but is_unionable() does not check results for error and interprets it as boolean true.

    @serhiy-storchaka serhiy-storchaka added 3.10 only security fixes 3.11 only security fixes labels Jun 22, 2021
    @serhiy-storchaka serhiy-storchaka self-assigned this Jun 22, 2021
    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump 3.10 only security fixes 3.11 only security fixes labels Jun 22, 2021
    @serhiy-storchaka serhiy-storchaka self-assigned this Jun 22, 2021
    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels Jun 22, 2021
    @Fidget-Spinner
    Copy link
    Member

    A possible simple fix is to change these lines https://github.com/python/cpython/blob/main/Objects/unionobject.c#L294-L301

    to:

        return (
            is_typevar(obj) |
            is_new_type(obj) |
            is_special_form(obj) |
            PyType_Check(obj) |
            PyObject_TypeCheck(obj, &Py_GenericAliasType) |
            (int)(type == &_Py_UnionType));
    

    However, that may slow down union a little since we lose the short-circuiting that || provides over |, and all checks have to be evaluated.

    Checking each result individually and mimicking the short circuiting behavior works too, so I did that. What do you think Serhiy?

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset adfa1ba by Ken Jin in branch 'main':
    bpo-44483: Fix crash in union object with bad __module__ (GH-26848)
    adfa1ba

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 7e6cad7 by Miss Islington (bot) in branch '3.10':
    bpo-44483: Fix crash in union object with bad __module__ (GH-26848) (GH-26852)
    7e6cad7

    @serhiy-storchaka
    Copy link
    Member Author

    Yes, Checking each result individually is a right way. Not only because performance, but because calling any Python code while an error is set will cause a crash in debug build and weird bugs in release build. It is better to return as fast as you have an error.

    Thank you for your PR Ken Jin.

    @Fidget-Spinner
    Copy link
    Member

    Oh, that's a good point too. Thanks for the explanation, reviews and merge Serhiy.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants