From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.bugs Subject: bug#59574: 29.0.50; Emacs crashes when using tree-sitter-based mode in an empty buffer Date: Fri, 25 Nov 2022 19:18:09 -0800 Message-ID: <6350D0DE-63CD-410A-AA48-56D924ED67EA@gmail.com> References: <837czjulc4.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27341"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 59574@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 26 04:19:30 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 1oyliy-0006wb-SZ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 26 Nov 2022 04:19:28 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oylih-0003MK-Oq; Fri, 25 Nov 2022 22:19:11 -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 1oyliZ-0003Lh-Kn for bug-gnu-emacs@gnu.org; Fri, 25 Nov 2022 22:19:10 -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 1oyliY-0002p1-9k for bug-gnu-emacs@gnu.org; Fri, 25 Nov 2022 22:19:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oyliX-0008Rb-RI for bug-gnu-emacs@gnu.org; Fri, 25 Nov 2022 22:19:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 26 Nov 2022 03:19:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59574 X-GNU-PR-Package: emacs Original-Received: via spool by 59574-submit@debbugs.gnu.org id=B59574.166943270132412 (code B ref 59574); Sat, 26 Nov 2022 03:19:01 +0000 Original-Received: (at 59574) by debbugs.gnu.org; 26 Nov 2022 03:18:21 +0000 Original-Received: from localhost ([127.0.0.1]:37591 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oylhs-0008Qi-Oc for submit@debbugs.gnu.org; Fri, 25 Nov 2022 22:18:21 -0500 Original-Received: from mail-pl1-f172.google.com ([209.85.214.172]:40896) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oylho-0008QK-EM for 59574@debbugs.gnu.org; Fri, 25 Nov 2022 22:18:19 -0500 Original-Received: by mail-pl1-f172.google.com with SMTP id d6so5456765pll.7 for <59574@debbugs.gnu.org>; Fri, 25 Nov 2022 19:18:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=lrmHcYzQxlYylA2IEvsJgcZc4S7L/qTp1F9tsSnXPRw=; b=UtUiwljeWf6Sj6irZ9FtTR8pJYNq2Ph5lNUOyJ/R6qZpizmJWKmCddW+Asfkohrh6n ievHQoYHWi//gI+f56cWR9jFbsTEYXy9QQ4w/43Dx0uBijAoufkXa4eKIV4bA/QIkTU3 y0lEWSl7ql4LTChjADbCwodA9VmWLBI7ZFVS3gIdSs85CJZ/z9egBUeMX7NrnSSUjFgq 7etzkc1EUvejP/lRvl/Sc4KH1JKNWs7V49VOJ4YWyD8XirPiRtnB6yjUvTHuzbaFIoOt jS/+VTOfXIPicWxpFx2FWYTIzgYVhhb1Mn00TK53p1PAwwTPeDQFSO2MY080xOKsWmra jQcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lrmHcYzQxlYylA2IEvsJgcZc4S7L/qTp1F9tsSnXPRw=; b=l6SJDJsWPo8mOpT6JL1EjCheNWPmw9Lxe7W6MsKyfCWavtoteOUQXeJKchuPSAQbgv vXIPkFjaHLKETKJoCNnUOj+nyMrc571VyXMC65mZZn4gbg5sgPiPZHbFjCNtO2wGlu9c 0vDu0XA2DCS0eDx92nVPNloKpgJvmJZ8u/IcJGj0uXMPaGR6NAuehRDB9Qa2UZdrXjlf e/fVK21i71IGzWr/peJbTgEpgZ79eaz8d+CybuYDLURP8C9WvpLAEiLHlcAO22NLqqeH SnieKvrDxiwUnoKWAIjxRlJaeTaryLW08AoqlH95yU40EzF8jgSktUjvdGJr/XoeqwV/ HYzg== X-Gm-Message-State: ANoB5pmKrvB/CmBWZM2zocMVaX/LfVhCcTKSYzBOCZ45hI1X029wmhhg IpcFSXq4+jCFedfmftuOhVE= X-Google-Smtp-Source: AA0mqf7jF2h3+NdmVLZ9Gq8wX86ECO679IflMXvrGoaZ08uDhxMQDJOZ7SnzAYfIEz+hxVDtqiRjQQ== X-Received: by 2002:a17:902:a616:b0:189:46b1:fe0b with SMTP id u22-20020a170902a61600b0018946b1fe0bmr15076447plq.117.1669432690427; Fri, 25 Nov 2022 19:18:10 -0800 (PST) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id 77-20020a621850000000b005722f4dfbefsm3744297pfy.193.2022.11.25.19.18.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Nov 2022 19:18:10 -0800 (PST) In-Reply-To: <837czjulc4.fsf@gnu.org> X-Mailer: Apple Mail (2.3696.120.41.1.1) 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:249040 Archived-At: > On Nov 25, 2022, at 7:04 AM, Eli Zaretskii wrote: >=20 > To reproduce: >=20 > emacs -Q > C-x C-f foo.c RET > M-x c-ts-mode RET > Type "in" Thanks for finding this out!=20 >=20 > Make sure foo.c doesn't exist, so you start from an empty buffer. As = soon > as you type the second character of "in", there's an assertion = violation: >=20 > treesit.c:1383: Emacs fatal error: assertion failed: end_byte <=3D = BUF_ZV_BYTE (bu > ffer) >=20 > Thread 1 hit Breakpoint 1, terminate_due_to_signal (sig=3D22, = backtrace_limit=3D2147483647) at emacs.c:427 > 427 signal (sig, SIG_DFL); > (gdb) up > #1 0x01230802 in die ( > msg=3D0x18e6778 "end_byte <=3D = BUF_ZV_BYTE (buffer)", file=3D0x18e5fcc = "treesit.c", line=3D1383) > at alloc.c:7697 > 7697 terminate_due_to_signal (SIGABRT, INT_MAX); > (gdb) > #2 0x01355636 in treesit_make_ranges (ranges=3D0x856a778, len=3D1, > buffer=3D0x7fe94b0) at treesit.c:1383 > 1383 eassert (end_byte <=3D BUF_ZV_BYTE (buffer)); > (gdb) p end_byte > $1 =3D 4 > (gdb) p BUF_ZV_BYTE(buffer) > $2 =3D 3 >=20 > Interestingly, this only happens once, when the buffer includes = exactly 1 > byte and an additional character is inserted. If you get past this > assertion, further characters can be inserted without any problems, = and > end_byte always equals BUF_ZV_BYTE. >=20 > The backtrace is below, if it is interesting. >=20 > I couldn't figure out where did tree-sitter take the range it returns = to us. > Yuan, can you describe how does the parser get the range it needs to > consider? If I put a breakpoint in = treesit-parser-set-included-ranges, the > breakpoint never breaks, so this doesn't seem to be how the range is = set in > this scenario. After we parse the buffer (in treesit_ensure_parsed) we compute the = ranges that has changed since last parse, by calling = ts_tree_get_changed_ranges, and pass the ranges to notifier functions = (those added by treesit-parser-add-notifier). This range is different = from the range within which a parser operates. That range is set by = treesit-parser-set-included-ranges, and is not involved with the = parsing, treesit_record_changes, visible_beg/end stuff. Both feature happens to use treesit_make_ranges as a helper function, = but the similarity ends there. > There's also something strange in treesit_record_change: when it is = called > for the first time in a buffer which was empty and you insert one = character, > we bypass the updating of visible_beg and visible_end fields of the = Lisp > parser object, because XTS_PARSER (lisp_parser)->tree is NULL. But it = looks > to me that we should still update these two fields regardless, no? = Only the > call to treesit_tree_edit_1 needs the tree. (I thought that maybe = this lack > of update explains the assertion, but even if I move the condition to = guard > only treesit_tree_edit_1, the assertion still happens, so I guess my > hypothesis eats dust.) We don=E2=80=99t need to update visible_beg/end in treesit_record_change = if tree is NULL, because visible_beg/end represents the range of buffer = that the tree sees, so if there is no tree, visible_beg/end can be = considered uninitialized. However you are right about needing to update = visible_beg/end, but in treesit_ensure_position_synced (I renamed it to = treesit_sync_visible_region): that=E2=80=99s where we ensure = visible_beg/end equals to BUF_BEGV_BYTE/friends.=20 The problem is we don=E2=80=99t update visible_beg/end for the very = first parse, when tree is NULL. I also added some comments, hopefully they sufficiently explain = everything. Yuan