From patchwork Thu Apr 27 21:11:55 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 20187 Received: (qmail 67355 invoked by alias); 27 Apr 2017 21:13:31 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 67343 invoked by uid 89); 27 Apr 2017 21:13:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_PASS autolearn=ham version=3.3.2 spammy=refresh X-HELO: sessmg22.ericsson.net Received: from sessmg22.ericsson.net (HELO sessmg22.ericsson.net) (193.180.251.58) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 27 Apr 2017 21:13:28 +0000 Received: from ESESSHC015.ericsson.se (Unknown_Domain [153.88.183.63]) by sessmg22.ericsson.net (Symantec Mail Security) with SMTP id E9.E3.20517.7FE52095; Thu, 27 Apr 2017 23:13:28 +0200 (CEST) Received: from EUR02-VE1-obe.outbound.protection.outlook.com (153.88.183.145) by oa.msg.ericsson.com (153.88.183.63) with Microsoft SMTP Server (TLS) id 14.3.339.0; Thu, 27 Apr 2017 23:12:17 +0200 Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=ericsson.com; Received: from elxcz23q12-y4.ca.am.ericsson.se (192.75.88.130) by DB5PR07MB1720.eurprd07.prod.outlook.com (2603:10a6:0:12::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1061.6; Thu, 27 Apr 2017 21:12:14 +0000 From: Simon Marchi To: CC: Simon Marchi Subject: [pushed master+8.0] Add missing incref when creating Inferior Python object Date: Thu, 27 Apr 2017 17:11:55 -0400 Message-ID: <20170427211155.29204-1-simon.marchi@ericsson.com> In-Reply-To: <81b15bf924ea176fc601dea7be679200@polymtl.ca> References: <81b15bf924ea176fc601dea7be679200@polymtl.ca> MIME-Version: 1.0 X-ClientProxiedBy: DM5PR18CA0031.namprd18.prod.outlook.com (2603:10b6:3:b0::17) To DB5PR07MB1720.eurprd07.prod.outlook.com (2603:10a6:0:12::25) X-MS-Office365-Filtering-Correlation-Id: 77933994-8cdc-4920-a4c0-08d48db2143e X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(201703131423075)(201703031133081); SRVR:DB5PR07MB1720; X-Microsoft-Exchange-Diagnostics: 1; DB5PR07MB1720; 3:fIzi/1SsT/HUnK8kUgd9UAPGbfc1Kix3x39viHhNo/x4eda3Ym/BSongfPIaO5fTrzeY0NWX+fpaIe+YH2Pbb2rT1QH/UCCoFZP3cJvsjuIe1qYVixnrYl0Q9L6GuX/vtiO2ta3jBjZxlTyf34RhD3NzvxKzF1kN+VQeH2gT5j6CkOhoW+TB6+3ZM3tta8hWita41hS/7tQbFxqIFO8XJsgE9NGi3nrAUN/Na3RHrNqtkMnWuQJ+WH8seCcPi8Tl54YWGc2Y7FfudN7CEEWMUBWDV7HzS9jA6DEV38tcfVDCb2A0LFAam37Eho7b53MPyGN7sEUkCxnfHMS/0g5x2g==; 25:EEHugsDhdudt7ohqtY9qhos0YdxgXN5zNJzz5COiOEneXTfQbQfRFfRry/rZy5a6tkvG7glR2hfBOhz3hDa96Qq3C20/EnY3w6o//H++LlrKo/znZ0xVZLtxWmRuyg5pMD9iIhTehXVvbjN9o6UK52Q+WA1dKRPo8lGdt0nGG7tCTVMq/Vmx5dnGfXtv3o1W/rXrcmQvTflFL5wSGz4xo4LJVe4QGDo9vq3zdAOSstHASwLsmHeEQ9zcrklgKbRGieI5RjADa/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(37575265505322); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(6041248)(20161123560025)(20161123564025)(20161123555025)(20161123562025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(6072148); SRVR:DB5PR07MB1720; BCL:0; PCL:0; RULEID:; SRVR:DB5PR07MB1720; X-Microsoft-Exchange-Diagnostics: 1; DB5PR07MB1720; 4:HbF0LizUr6W/dnXJGFm02DfqvOj89gDxctdMY/w/M/m02S0wHuaDsBRUuNG+CZpjjdv+hg4FZhU8n0xSNPqGhtd29ops+45Zz4LargAi1TeQR1xQFSCfCWRQydij7nBUWZmZZh7M9rw2R4ZR3qowG7ApKQFdMoIdp+F6F1Exs0MRQgBdgrz1yiyanSaFrk8e+3kt6OrzxUnSLrOzdXpztort36BlAIXdSZmlyE5Y4IiN8g3CLmwtzzFbsTaxETAcfzhaJKgD8I6014XqyyPzb+jlV/Ji4BmEU2eKl5lRZw6lPhpAsJd/jhxWvJfV97lKnGVXNGSN4wLEpCubQ9n34leevTyvZFn0nEOHctcHd+mn2v2qWIYlKcWOIByLm4t64v/VI5ROs4EjZxLhiMvwSXYCMdZggW0g5gmNwV5xCM35N/LXeAnHUNRrbwIdVIDlQ41gz8K5u/Aa+nxec35z6lf4VtpuDXy/BfTpWzNchCuXIjcRpPdizXOBcMM0gdaKuG4xHXqKCuEy3ZWhtOwySohyOvoO9DxZKiZ6U3zjI7+RY2iNkkt2Pe53AcQphNGc8MTWuzXz2g+duovyEIZcSi71c/V6lTX3rzvt1eQxYVoNWlvL/DtmhuzitQq+Ea0RDAQrXw+5eKBVWXAvnV3sQqCnPvUW1xOjJ9umpQJfbp7gN9LYDV7TUI5ap2gh0Ol1/TqHN2xB8263ROSo/LPRlFUjdSQojM1VuSa2Oowhw/xRKqjQxUAfPDXi32UwHueCw80+POWPXeH4jRFH/r//MwL0sY0dgHux+cpYXlYGJ4fgYbCVR0dEU9DB+U5VUYNXFW9m67pqRtxaSj1Va5Qtag== X-Forefront-PRVS: 029097202E X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(39840400002)(39410400002)(39450400003)(39860400002)(39400400002)(39850400002)(377424004)(50226002)(42186005)(6666003)(2950100002)(6916009)(33646002)(86362001)(575784001)(2351001)(189998001)(50466002)(2906002)(8676002)(81166006)(305945005)(1076002)(76176999)(50986999)(38730400002)(110136004)(3846002)(47776003)(6486002)(6506006)(5003940100001)(25786009)(4326008)(66066001)(53936002)(36756003)(6512007)(5660300001)(7736002); DIR:OUT; SFP:1101; SCL:1; SRVR:DB5PR07MB1720; H:elxcz23q12-y4.ca.am.ericsson.se; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DB5PR07MB1720; 23:+W2vxK+8j+/121jNx7CSF7zRV/oUFe/ye9/Jj0Jvs?= =?us-ascii?Q?rmMo0rF5VaAfH/B7qHRpLQcBJ8oh7EhH1kCBLbhOAwFqDPp3tsOmAD/hvuNS?= =?us-ascii?Q?DU9rt+CbBBGw42zNu2Up21up4BJnLhibFo1xJmch00VBAJJbB9iGG3uXhaHB?= =?us-ascii?Q?w+RErkCPaz+i3Xsmb73VKZy4YBpwvhoCEl75lhACXClBVE4QVKDdHOSKFAEl?= =?us-ascii?Q?31M5Ny6C8KiyMWDDJ2RSWye1Z7RL+FyOLqXthjXiXINNXwiEf8PiKDPTKVgU?= =?us-ascii?Q?MN+QOFxBgkSOkclniPkGQU/SjdaMon1ri19uZTRtbVsGrClmilZYsJO+6Lzi?= =?us-ascii?Q?9Qy3yyy/5MTKJN8/jG6IUv8Ftn1jFnm1feE+LbHycrIzQF1dnIFar7GfNdnh?= =?us-ascii?Q?DyCt+RSSoCqgsON4uaiH4R70VZaA1A3eHDOniNXW1REBWTfgGH4E2gxTslpx?= =?us-ascii?Q?McoGgweL3opGHpWLu6X/VnReEYxwQQa4RFuB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S+JpaEypOC4pTm+a4YceBlIllgl5fdNkDzWaxPyiJM9Jr7TZJBwJ1lA7pp3rUiOsMQHtUV7WZMC38Eac4SjzSD4z70Y2lXoTr5P6sgXHB0H1XgBnQFts/mK9x8paiH+2OI7I5mhBe2BLF59Qg71C+GQsGIucNtfFlo5zYugdWf58t7/5LkFxiUrBnel5p8KukM+vsmh/UZ19GqckybiTzThZH+xNbCgnAC2A==; 5:i9pm0aXU1WpUIBggXcYsRRkZDd7KdH41qdB0Iol9+5UQihFKCZl77mDYswjT5SNF7ETYvC2KRKiRVfqG1sb7SRWc/hg8CzoGW1YepmkaFr4ymxQlmvwUxHwjtuqI6bQmrn5XlTDA/FBBYIWlgN3xBQ==; 24:HXOtNuZrNiib02WBCwFFpN0IJdH0HVXl79O3ZmTz06ixQyiBBe8Wuxvm/ruHbXHECX8+iMENSRe9nLgaPb5Z22RBt3jtMB9h6vc7GRcc+sg= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DB5PR07MB1720; 7:0wZl8XcVtd+azBAaa8sHva41luW6aEUW1IEqhCM2zS2sjvzvqoyF+lgSz56rqWqo9WXwIpCFljSIcnMa9YW35BKECU0/N5U7aOvKkvE+sa6YXtU5gExcoqM2oqFmSQKocHDE9fworbg8DLhUMRNlBEFSkIWPAmJJD5eGKYZIymmx7PxPl/7Fkw9yb3OQtLs8+qNLMga9om8a78hsFhk+sINK1qTZq65GC7Y8JIVotkWaSsnukr/s5TFJC5UayPvGNc+1FNidc0AZHjYzmhluRx6n/rm1xLjLa7Sb8XcMBTjC/uV//QnAu3SeX3ZIALjwivM4csqQbk3UP4h+Ur3GFw== X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Apr 2017 21:12:14.3489 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5PR07MB1720 X-OriginatorOrg: ericsson.com X-IsSubscribed: yes I have pushed this patch (by itself) to master and 8.0. It just needed a little refresh, here's the pushed version. The test py-inferior.exp fails when using a debug build of Python 3.6. I don't see it failing with my system's default Python, but it might be related to the different memory allocation scheme used when doing a build with pydebug. The issue is that we are missing a Py_INCREF in inferior_to_inferior_object. The PyObject_New function initializes the object with a refcount of 1. If we assume that this refcount corresponds to the reference we are returning, then we are missing an incref for the reference in the inferior data. The counterpart for the incref that corresponds to the reference in the inferior data is in py_free_inferior, in the form the gdbpy_ref instance. Here's how I can get it to crash (with some debug output): $ ./gdb -nx -ex "set debug python 1" (gdb) add-inferior Added inferior 2 (gdb) python infs = gdb.inferiors() Creating Python Inferior object inf = 1 Creating Python Inferior object inf = 2 (gdb) remove-inferiors 2 py_free_inferior inf = 2 infpy_dealloc inf = (gdb) python infs = None Fatal Python error: Objects/tupleobject.c:243 object at 0x7f9cf1a568d8 has negative ref count -1 Current thread 0x00007f9cf1b68780 (most recent call first): File "", line 1 in [1] 408 abort (core dumped) ./gdb -nx -ex "set debug python 1" After having created the inferiors object, their refcount is 1 (which comes from PyObject_New), but it should be two. The gdb inferior object has a reference and the "infs" list has a reference. When invoking remove-inferiors, py_free_inferior gets called. It does the decref that corresponds to the reference that the gdb inferior object kept. At this moment, the refcount drops to 0 and the object gets deallocated, even though the "infs" list still has a reference. When we set "infs" to None, Python tries to decref the already zero refcount and the assert triggers. With this patch, it looks better: (gdb) add-inferior Added inferior 2 (gdb) python infs = gdb.inferiors() Creating Python Inferior object inf = 1 Creating Python Inferior object inf = 2 (gdb) remove-inferiors 2 py_free_inferior inf = 2 (gdb) python infs = None infpy_dealloc inf = gdb/ChangeLog: * python/py-inferior.c (inferior_to_inferior_object): Increment reference count when creating the object. --- gdb/ChangeLog | 5 +++++ gdb/python/py-inferior.c | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 2b265a1301..e356e0b7ea 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2017-04-27 Simon Marchi + + * python/py-inferior.c (inferior_to_inferior_object): Increment reference + count when creating the object. + 2017-04-25 Yao Qi * aarch64-tdep.c (aarch64_gdbarch_init): Don't call diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 3d2cb1ddbe..f6a24a090f 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -223,11 +223,14 @@ inferior_to_inferior_object (struct inferior *inferior) inf_obj->threads = NULL; inf_obj->nthreads = 0; + /* PyObject_New initializes the new object with a refcount of 1. This + counts for the reference we are keeping in the inferior data. */ set_inferior_data (inferior, infpy_inf_data_key, inf_obj); } - else - Py_INCREF ((PyObject *)inf_obj); + + /* We are returning a new reference. */ + Py_INCREF ((PyObject *)inf_obj); return (PyObject *) inf_obj; }