Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZZQH-0003Vf-4S for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Mar 2022 14:35:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nZZQF-0000T4-OM for pgsql-hackers@arkaria.postgresql.org; Wed, 30 Mar 2022 14:35:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZZQF-0000St-AA for pgsql-hackers@lists.postgresql.org; Wed, 30 Mar 2022 14:35:43 +0000 Received: from mail-il1-x12f.google.com ([2607:f8b0:4864:20::12f]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nZZQB-0000VY-UK for pgsql-hackers@postgresql.org; Wed, 30 Mar 2022 14:35:41 +0000 Received: by mail-il1-x12f.google.com with SMTP id 8so14666649ilq.4 for ; Wed, 30 Mar 2022 07:35:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=f8WndQvugHnUfGUU9cJw9fyKrW3tgj/MoMbT5fjQ/5s=; b=iqJ3iiISW/fD9nekoUvKpbFK94ASCstM7GH5udrUsIt+ZUi1CS76sJBMNkfVJcH89W gvdGS+Ozl6KDfU8MzXe7mmDwtjZ8L4qn8ur2+9vVFLY9kz7nQTp3pCCyOEytZfjgJqDB Wrkm/FiG2Rsgi/Ro38DNtbYuUlaTCL2Q2GbSXTZh2HFkj19AI1egkdAdHP8WRDNz447A YJTm5Ka3KCqMLTv8rY1PjvQLPteTENX2Kh1VrLS05zUYjeqnRhQkkI0WTZy/uAbJsFXu EO1BbZmPuU/al3br+oA7cwE4iMhxSpfWJSPoONbFHI9+X5i4KLxj4N/831NS+fSkW0Fd HZ+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=f8WndQvugHnUfGUU9cJw9fyKrW3tgj/MoMbT5fjQ/5s=; b=noLsQrVfzn+wW5Nc/oYNijBI1fvmV8NfV5ejPubdRYF6TVl+oL/xMyl9pkGOr2+W9P k/dxpqc0OyRdFNJ1hS5fZHsSggA9BZ5AjBM7zOnvcYfLLYoJRgG6V192t5SGmf4jVS2/ kQzjSHiplaRxeoNGCRUZlHhTO6MMRt2tY/CUrMvMCJRWkLqm6dOc6506z3tTnNG0AHR5 ni9JcohcfL7LkoPGNIi7yK61hU9sjT8uBeG2eGIeldoCQ81IEh+TWHYy5MX/Ogw5kYZ2 Qfu1hbh3xsBfY5xsKQREjKheLP4ptfmx6O7iv9lmq3w1YIp8uwkIx/Tmsg59M11iZYb6 WcjA== X-Gm-Message-State: AOAM5327sDjOwDFCPYcXUdDyeSUE/LvBkIEZSCrchrXRF05/Q85FkX0t MxOo03HczvQ9oRTUM12guU0Y3A== X-Google-Smtp-Source: ABdhPJwHioMI2vze01gEXw+ZWVFpTQVsXRAv6rV2jR1rw18xPNKddCFKkBTxP5alPdxaqdCkGOQHlw== X-Received: by 2002:a05:6e02:18c7:b0:2c8:502e:db84 with SMTP id s7-20020a056e0218c700b002c8502edb84mr10994868ilu.105.1648650938629; Wed, 30 Mar 2022 07:35:38 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id t6-20020a6b0906000000b0064963285af2sm10921761ioi.51.2022.03.30.07.35.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Mar 2022 07:35:38 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 0E2DD802FFC; Wed, 30 Mar 2022 09:35:36 -0500 (CDT) Date: Wed, 30 Mar 2022 09:35:36 -0500 From: Justin Pryzby To: Robert Haas Cc: Jeevan Ladhe , Dipesh Pandit , Andres Freund , Alvaro Herrera , "Shinoda, Noriyoshi (PN Japan FSIP)" , Abhijit Menon-Sen , Dmitry Dolgov <9erthalion6@gmail.com>, Jeevan Ladhe , Mark Dilger , pgsql-hackers@postgresql.org, tushar Subject: basebackup/lz4 crash Message-ID: <20220330143536.GG28503@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Forking: <20220316151253.GB28503@telsasoft.com> On Wed, Mar 16, 2022 at 10:12:54AM -0500, Justin Pryzby wrote: > Also, with a partial regression DB, this crashes when writing to stdout. > > $ src/bin/pg_basebackup/pg_basebackup --wal-method fetch -Ft -D - -h /tmp --no-sync --compress=lz4 |wc -c > pg_basebackup: bbstreamer_lz4.c:172: bbstreamer_lz4_compressor_content: Assertion `mystreamer->base.bbs_buffer.maxlen >= out_bound' failed. > 24117248 > > #4 0x000055555555e8b4 in bbstreamer_lz4_compressor_content (streamer=0x5555555a5260, member=0x7fffffffc760, > data=0x7ffff3068010 "{ \"PostgreSQL-Backup-Manifest-Version\": 1,\n\"Files\": [\n{ \"Path\": \"backup_label\", \"Size\": 227, \"Last-Modified\": \"2022-03-16 02:29:11 GMT\", \"Checksum-Algorithm\": \"CRC32C\", \"Checksum\": \"46f69d99\" },\n{ \"Pa"..., len=401072, context=BBSTREAMER_MEMBER_CONTENTS) at bbstreamer_lz4.c:172 > mystreamer = 0x5555555a5260 > next_in = 0x7ffff3068010 "{ \"PostgreSQL-Backup-Manifest-Version\": 1,\n\"Files\": [\n{ \"Path\": \"backup_label\", \"Size\": 227, \"Last-Modified\": \"2022-03-16 02:29:11 GMT\", \"Checksum-Algorithm\": \"CRC32C\", \"Checksum\": \"46f69d99\" },\n{ \"Pa"... > ... > > (gdb) p mystreamer->base.bbs_buffer.maxlen > $1 = 524288 > (gdb) p (int) LZ4F_compressBound(len, &mystreamer->prefs) > $4 = 524300 > > This is with: liblz4-1:amd64 1.9.2-2ubuntu0.20.04.1 It looks like maybe this code was copied from bbstreamer_lz4_compressor_finalize() which has an Assert rather than expanding the buffer like in bbstreamer_lz4_compressor_new() commit e70c12214b5ba0bc93c083fdb046304a633018ef Author: Justin Pryzby Date: Mon Mar 28 23:24:15 2022 -0500 basebackup: fix crash with lz4 + stdout + manifests That's just one known way to trigger this issue. diff --git a/src/bin/pg_basebackup/bbstreamer_lz4.c b/src/bin/pg_basebackup/bbstreamer_lz4.c index 67f841d96a9..8e8352a450c 100644 --- a/src/bin/pg_basebackup/bbstreamer_lz4.c +++ b/src/bin/pg_basebackup/bbstreamer_lz4.c @@ -170,7 +170,11 @@ bbstreamer_lz4_compressor_content(bbstreamer *streamer, * forward the content to next streamer and empty the buffer. */ out_bound = LZ4F_compressBound(len, &mystreamer->prefs); - Assert(mystreamer->base.bbs_buffer.maxlen >= out_bound); + + /* Enlarge buffer if it falls short of compression bound. */ + if (mystreamer->base.bbs_buffer.maxlen < out_bound) + enlargeStringInfo(&mystreamer->base.bbs_buffer, out_bound); + if (avail_out < out_bound) { bbstreamer_content(mystreamer->base.bbs_next, member,