Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iS1hs-0005aS-JN for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Nov 2019 16:29:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iS1hr-00007C-Be for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Nov 2019 16:29:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iS1hr-00005A-1V for pgsql-hackers@lists.postgresql.org; Tue, 05 Nov 2019 16:29:23 +0000 Received: from [212.85.157.172] (helo=mail.dalibo.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iS1ho-0001vE-Vd for pgsql-hackers@lists.postgresql.org; Tue, 05 Nov 2019 16:29:22 +0000 Received: from firost (unknown [88.190.0.6]) by mail.dalibo.com (Postfix) with ESMTPSA id ACAE020415; Tue, 5 Nov 2019 17:29:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1572971359; bh=hzhkRtC1TG3gKRVdceEmYFaS5XcmMgNn+WuhKIXts7o=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Udsmp9FBClrIsgNvTheyoTi7yvhwTE/23KE8esu4weKAd2z0c2adsG1qftqbOXXtV d0BL561JpK4eRcjvDnTbcmXNJQluzrgWGU3ZR9JR1F8fyfMr+21OoZEf+ThMnHBK00 /wLGNP8PiUKoDOkAwj+NvIB+YtQnDN4wyc08r0rk= Date: Tue, 5 Nov 2019 17:29:18 +0100 From: Jehan-Guillaume de Rorthais To: Tom Lane Cc: pgsql-hackers@lists.postgresql.org, Subject: Re: segmentation fault when cassert enabled Message-ID: <20191105172918.3e32a446@firost> In-Reply-To: <18714.1572020918@sss.pgh.pa.us> References: <20191025175929.7e90dbf5@firost> <18714.1572020918@sss.pgh.pa.us> Organization: Dalibo MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 212.85.157.172 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 25 Oct 2019 12:28:38 -0400 Tom Lane wrote: > Jehan-Guillaume de Rorthais writes: > > When investigating for the bug reported in thread "logical replication - > > negative bitmapset member not allowed", I found a way to seg fault > > postgresql only when cassert is enabled. > > ... > > I hadn't time to digg further yet. However, I don't understand why this > > crash is triggered when cassert is enabled. > > Most likely, it's not so much assertions that provoke the crash as > CLOBBER_FREED_MEMORY, ie the actual problem here is use of already-freed > memory. Thank you. Indeed, enabling CLOBBER_FREED_MEMORY on its own is enough to trigger the segfault. In fact, valgrind detect it as an uninitialised value, no matter CLOBBER_FREED_MEMORY is defined or not: Conditional jump or move depends on uninitialised value(s) at 0x43F410: slot_modify_cstrings (worker.c:398) by 0x43FBE9: apply_handle_update (worker.c:744) by 0x440088: apply_dispatch (worker.c:968) by 0x4405D7: LogicalRepApplyLoop (worker.c:1175) by 0x440CD0: ApplyWorkerMain (worker.c:1733) by 0x411C34: StartBackgroundWorker (bgworker.c:834) by 0x41EA24: do_start_bgworker (postmaster.c:5763) by 0x41EB6F: maybe_start_bgworkers (postmaster.c:5976) by 0x41F562: sigusr1_handler (postmaster.c:5161) by 0x48A072F: ??? (in /lib/x86_64-linux-gnu/libpthread-2.28.so) by 0x4B31FF6: select (select.c:41) by 0x41FDDE: ServerLoop (postmaster.c:1668) Uninitialised value was created by a heap allocation at 0x5C579B: palloc (mcxt.c:949) by 0x437116: logicalrep_rel_open (relation.c:270) by 0x43FA8F: apply_handle_update (worker.c:684) by 0x440088: apply_dispatch (worker.c:968) by 0x4405D7: LogicalRepApplyLoop (worker.c:1175) by 0x440CD0: ApplyWorkerMain (worker.c:1733) by 0x411C34: StartBackgroundWorker (bgworker.c:834) by 0x41EA24: do_start_bgworker (postmaster.c:5763) by 0x41EB6F: maybe_start_bgworkers (postmaster.c:5976) by 0x41F562: sigusr1_handler (postmaster.c:5161) by 0x48A072F: ??? (in /lib/x86_64-linux-gnu/libpthread-2.28.so) by 0x4B31FF6: select (select.c:41) My best bet so far is that logicalrep_relmap_invalidate_cb is not called after the DDL on the subscriber so the relmap cache is not invalidated. So we end up with slot->tts_tupleDescriptor->natts superior than rel->remoterel->natts in slot_store_cstrings, leading to the overflow on attrmap and the sigsev. I hadn't follow this path yet. By the way, I noticed attrmap is declared as AttrNumber * in struct LogicalRepRelMapEntry, AttrNumber being typedef'd as an int16. However, attrmap is allocated based on sizeof(int) in logicalrep_rel_open: entry->attrmap = palloc(desc->natts * sizeof(int)); It doesn't look like a major problem, it just allocates more memory than needed. Regards,