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

tp_descr_get(self, obj, type) is called without owning a reference to "self" #69936

Closed
jdemeyer opened this issue Nov 27, 2015 · 25 comments
Closed
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@jdemeyer
Copy link
Contributor

BPO 25750
Nosy @pitrou, @scoder, @vstinner, @embray, @serhiy-storchaka, @jdemeyer, @miss-islington
PRs
  • bpo-25750: fix refcounts in type_getattro() #6118
  • bpo-25750: add testcase on bad descriptor __get__() #9084
  • [3.7] bpo-25750: fix refcounts in type_getattro() (GH-6118) #9087
  • [3.6] bpo-25750: fix refcounts in type_getattro() (GH-6118) #9088
  • [2.7] bpo-25750: fix refcounts in type_getattro() (GH-6118) #9089
  • [2.7] bpo-25750: fix refcounts in type_getattro() (GH-6118) #9091
  • bpo-25750: Fix a compiler warning introduced in #9084. #10234
  • Files
  • descr_ref.patch: Patch
  • descr_ref-2.patch
  • 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 2018-10-28.17:53:12.898>
    created_at = <Date 2015-11-27.19:56:53.263>
    labels = ['interpreter-core', 'type-crash']
    title = 'tp_descr_get(self, obj, type) is called without owning a reference to "self"'
    updated_at = <Date 2018-11-20.18:45:43.539>
    user = 'https://github.com/jdemeyer'

    bugs.python.org fields:

    activity = <Date 2018-11-20.18:45:43.539>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2018-10-28.17:53:12.898>
    closer = 'vstinner'
    components = ['Interpreter Core']
    creation = <Date 2015-11-27.19:56:53.263>
    creator = 'jdemeyer'
    dependencies = []
    files = ['41173', '46125']
    hgrepos = []
    issue_num = 25750
    keywords = ['patch']
    message_count = 25.0
    messages = ['255480', '255565', '255566', '256352', '274202', '284140', '284570', '284571', '284573', '284574', '284575', '284576', '324722', '324723', '324724', '324728', '324729', '328065', '328176', '328539', '328717', '328898', '328899', '329286', '330144']
    nosy_count = 10.0
    nosy_names = ['pitrou', 'scoder', 'vstinner', 'devurandom', 'fbissey', 'erik.bray', 'serhiy.storchaka', 'jdemeyer', 'Claudio.Freire', 'miss-islington']
    pr_nums = ['6118', '9084', '9087', '9088', '9089', '9091', '10234']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue25750'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    @jdemeyer
    Copy link
    Contributor Author

    type_getattro() calls tp_descr_get(self, obj, type) without actually owning a reference to "self". In very rare cases, this can cause a segmentation fault if "self" is deleted by the descriptor.

    Downstream: [http://trac.sagemath.org/ticket/19633]

    @jdemeyer jdemeyer added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels Nov 27, 2015
    @arigo
    Copy link
    Mannequin

    arigo mannequin commented Nov 29, 2015

    This is a known general issue which is documented in Lib/test/crashers/borrowed_ref_1 inside the 2.7 branch. In trunk, I see that this file has been deleted, although the issue has not been solved in general. Only the particular crash in the file has been solved. (Somebody with motivation should restore the crashers that have been solved superficially only. But finding actual crashing code takes more effort than I'm willing to put into this rather pointless process, so CPython still gets the occasional bug report like this one instead.)

    @jdemeyer
    Copy link
    Contributor Author

    Thanks for the pointer. My patch does fix the crash in Lib/test/crashers/borrowed_ref_2.py on Python 2.7.10.

    @fbissey
    Copy link
    Mannequin

    fbissey mannequin commented Dec 13, 2015

    Will Jeroen's patch make it into 2.7.12 or are you expecting more stuff before committing a change?

    @scoder
    Copy link
    Contributor

    scoder commented Sep 2, 2016

    I haven't seen any crashes in the wild here, but this is still the case in the latest code base. The change doesn't seem invasive, so I don't see why it shouldn't get implemented.

    @serhiy-storchaka serhiy-storchaka self-assigned this Nov 29, 2016
    @ClaudioFreire
    Copy link
    Mannequin

    ClaudioFreire mannequin commented Dec 27, 2016

    I cannot be 100% sure, but we have ample evidence suggesting we're experiencing this same crash in production.

    We have a big system that mixes Cython and pure-python coroutines, and in one version we started seeing segfaults that strongly hint at this root cause.

    Adding pure-python indirections (that keep the arguments alive, I'd wager), fixes those segfaults.

    I cannot share the codebase (and in any case it's too big, and the crash is too difficult to reproduce in isolation, without real traffic), but I'd add my +1 on applying this fix.

    We're currently testing to try and reproduce the segfaults on 2.7.13, after that I'll try jdemeyer's patch and report the results.

    @vstinner
    Copy link
    Member

    vstinner commented Jan 3, 2017

    descr_ref-2.patch: patch rebased on the 2.7 branch.

    @vstinner
    Copy link
    Member

    vstinner commented Jan 3, 2017

    Claudio: "We're currently testing to try and reproduce the segfaults on 2.7.13, after that I'll try jdemeyer's patch and report the results."

    Cool! Keep us in touch ;-)

    @ClaudioFreire
    Copy link
    Mannequin

    ClaudioFreire mannequin commented Jan 3, 2017

    The crash (the one we're experiencing) still happens with 2.7.13. But at this point it's not clear whether it's a Python bug or a Cython bug, as jdemeyer's patch doesn't fix it. We're having a hard time getting accurate backtraces to actually debug this thing, as it only happens on production servers.

    @vstinner
    Copy link
    Member

    vstinner commented Jan 3, 2017

    The crash (the one we're experiencing) still happens with 2.7.13. But at this point it's not clear whether it's a Python bug or a Cython bug, as jdemeyer's patch doesn't fix it. We're having a hard time getting accurate backtraces to actually debug this thing, as it only happens on production servers.

    I suggest you to try faulthandler to try to get a traceback ;-)

    @jdemeyer
    Copy link
    Contributor Author

    jdemeyer commented Jan 3, 2017

    <shameless plug> If you are on POSIX, you could also use cysignals to get a traceback (simply import cysignals, which will install a handler for fatal signals like SIGSEGV).

    @ClaudioFreire
    Copy link
    Mannequin

    ClaudioFreire mannequin commented Jan 3, 2017

    Nice ideas, will give them a try

    @vstinner
    Copy link
    Member

    vstinner commented Sep 7, 2018

    New changeset 8f73548 by Victor Stinner (jdemeyer) in branch 'master':
    bpo-25750: fix refcounts in type_getattro() (GH-6118)
    8f73548

    @miss-islington
    Copy link
    Contributor

    New changeset f862f3a by Miss Islington (bot) in branch '3.7':
    bpo-25750: fix refcounts in type_getattro() (GH-6118)
    f862f3a

    @vstinner
    Copy link
    Member

    vstinner commented Sep 7, 2018

    New changeset 3ee0743 by Victor Stinner (Miss Islington (bot)) in branch '3.6':
    bpo-25750: fix refcounts in type_getattro() (GH-6118) (GH-9088)
    3ee0743

    @vstinner
    Copy link
    Member

    vstinner commented Sep 7, 2018

    New changeset bf2bd8f by Victor Stinner in branch '2.7':
    bpo-25750: fix refcounts in type_getattro() (GH-6118) (GH-9091)
    bf2bd8f

    @vstinner
    Copy link
    Member

    vstinner commented Sep 7, 2018

    The bug has been fixed in 2.7, 3.6, 3.7 and master branches. Thanks Jeroen Demeyer for your tenacity and hard work :-)

    Let's see what we do with the unit test: PR 9084.

    @vstinner
    Copy link
    Member

    New changeset 5a30620 by Victor Stinner (jdemeyer) in branch 'master':
    bpo-25750: Add test on bad descriptor __get__() (GH-9084)
    5a30620

    @serhiy-storchaka
    Copy link
    Member

    This change introduced a compiler warning.

    /home/serhiy/py/cpython/Modules/_testcapimodule.c:5042:17: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
    {"bad_get", bad_get, METH_FASTCALL},
    ^~~~~~~
    /home/serhiy/py/cpython/Modules/_testcapimodule.c:5042:17: note: (near initialization for ‘TestMethods[173].ml_meth’)

    @vstinner
    Copy link
    Member

    Comment on the commit:
    5a30620#commitcomment-31057082

    "bad_get should be explicitly cast to PyCFunction here, or else the compiler will balk; see https://bugs.python.org/msg328176 "

    @vstinner
    Copy link
    Member

    The cast warning is not specific to this issue, it's a more general issue that will be address in bpo-33012. I close again the bug.

    @serhiy-storchaka
    Copy link
    Member

    bpo-33012 is about more strong warnings in gcc 8. This issue introduced a warning in gcc 7.

    @serhiy-storchaka
    Copy link
    Member

    Is it necessary to use METH_FASTCALL?

    @jdemeyer
    Copy link
    Contributor Author

    jdemeyer commented Nov 5, 2018

    Is it necessary to use METH_FASTCALL?

    In Python 3, the bug only occurs with METH_FASTCALL. The issue is a reference counting bug and the temporary tuple used for a METH_VARARGS method avoids the bug.

    @serhiy-storchaka
    Copy link
    Member

    New changeset b1dede3 by Serhiy Storchaka in branch 'master':
    bpo-25750: Fix a compiler warning introduced in #53330. (GH-10234)
    b1dede3

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

    5 participants