From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#59426: 29.0.50; [tree-sitter] Some functions exceed maximum recursion limit Date: Mon, 21 Nov 2022 19:16:06 +0200 Message-ID: <83bkp02ppl.fsf@gnu.org> References: <837czo4f8y.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4290"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 59426@debbugs.gnu.org To: Yuan Fu Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Nov 21 18:17:13 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oxAPw-0000sD-EP for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 21 Nov 2022 18:17:12 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oxAPo-0005h7-GK; Mon, 21 Nov 2022 12:17:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxAPm-0005gr-VN for bug-gnu-emacs@gnu.org; Mon, 21 Nov 2022 12:17:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oxAPl-0002QN-N9 for bug-gnu-emacs@gnu.org; Mon, 21 Nov 2022 12:17:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oxAPl-00066V-JG for bug-gnu-emacs@gnu.org; Mon, 21 Nov 2022 12:17:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 21 Nov 2022 17:17:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59426 X-GNU-PR-Package: emacs Original-Received: via spool by 59426-submit@debbugs.gnu.org id=B59426.166905096323350 (code B ref 59426); Mon, 21 Nov 2022 17:17:01 +0000 Original-Received: (at 59426) by debbugs.gnu.org; 21 Nov 2022 17:16:03 +0000 Original-Received: from localhost ([127.0.0.1]:48689 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oxAOp-00064O-10 for submit@debbugs.gnu.org; Mon, 21 Nov 2022 12:16:03 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:56966) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oxAOn-00063k-39 for 59426@debbugs.gnu.org; Mon, 21 Nov 2022 12:16:01 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxAOh-000231-P6; Mon, 21 Nov 2022 12:15:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=QxPBcBEBk1TB+yCqn7CaQ8wScGugf5G2ltmwyLQozJQ=; b=Mi6aLNLcJxvqGrwVJSW5 DyQ+ASERy1SL6/iBMOuGzLBHBqXfxgeQds7ISv+dOdlcxUbYeEcU2YoGBKUIuJonrjE/YKaENzKL0 wZVWRnAMXUWI50Kud6ndGBAYeu7iJyfQhMWAxVfB0hfIRgLaX9z8nE8RG3s6l5I7XCVu/aip4dOnu O/gDjJQjXe5uYalT5yA9a62eyDxRLHYc92+/4+NwAjlH6cTrbMopeQrYO62jmoKF2X0cA+AHHL5iG PyujfBck6QZcsrYf6AytIWRt2J8qYfQCXMmS13h/MA4oheB4atEKn7xIULViHWA9P3DfPx9jBT9xB gGoaM2jjO3lHCg==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxAOg-0006ba-VI; Mon, 21 Nov 2022 12:15:55 -0500 In-Reply-To: (message from Yuan Fu on Mon, 21 Nov 2022 08:52:53 -0800) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:248554 Archived-At: > From: Yuan Fu > Date: Mon, 21 Nov 2022 08:52:53 -0800 > Cc: 59426@debbugs.gnu.org > > > > > On Nov 21, 2022, at 5:19 AM, Eli Zaretskii wrote: > > > >> From: Yuan Fu > >> Date: Sun, 20 Nov 2022 16:53:45 -0800 > >> > >> > >> Emacs crashed on a very large C file when c-ts-mode is on, because > >> the function building the imenu list tries to walk through the whole > >> parse tree, and end up recusing ~10k times because of how deep the parse > >> tree is. These recursive functions should have a built-in limit. Does > >> Emacs already have some way to determined the max recursion limit on > >> each system? Or should we come up with some hard-coded numbers? > > > > Is the recursion in our code, or is it in libtree-sitter? > > In our code, when we walk the parse tree. > > > > > If the former, one solution, albeit a crude one, is to track the recursion > > level and error out if it becomes too deep. Another solution is to handle > > the stack in our code, in which case the stack can be allocated on the heap. > > That’s my idea, hence my asking for a reasonable way to get a limit. I think a hard limit is totally reasonable, because there is no way for a “normal” parse tree to be 10k levels deep (that means the source program is 10k levels deep, ver unlikely for any program a human would write or a machine would generated). The one I observed is likely due to the parser misunderstanding the source (due to errors in the code). Plus, I don’t think any user would want to walk that deep into the parse tree either. If someone expects to walk that deep into a parse tree, her program is ill-designed. How many bytes does each recursive invocation need on the stack? With that number at hand, we can estimate a safe value for the limit. And don't forget that GC is also highly recursive and eats up a lot of stack space. I guess GC can happen during building of the imenu list?