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 1pulg3-0004jI-AN for pgsql-bugs@arkaria.postgresql.org; Fri, 05 May 2023 03:00:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pulg0-0007t8-Ii for pgsql-bugs@arkaria.postgresql.org; Fri, 05 May 2023 03:00:08 +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 1pulg0-0007sz-9J for pgsql-bugs@lists.postgresql.org; Fri, 05 May 2023 03:00:08 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1pulfx-000b7H-EV for pgsql-bugs@lists.postgresql.org; Fri, 05 May 2023 03:00:06 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2ac770a99e2so13991951fa.3 for ; Thu, 04 May 2023 20:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683255603; x=1685847603; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=zZAZMeim1B7MwujURQSj1eNoXKesWwz2RFbf3IzRjn0=; b=JsK9CMnMIBXW8AdoGkdz/ktLHSuDI5F5qvAXGyDXKfH2NF6gz8wbA+mSYHHhfhDsYY NDhGNr9icR2dpgVLGnmkyv6267Y11nh2b2NiMypIucHme+kmFZ39GdJej41UJhLxXVyX OJ3z/ttVRyI98sp2t/HQx/OBgcpwkjOlq68l77BVvS16Mkpf+hhsXTBR6UOdaD/8tVPt 3HJpuHVdNjXmolc2ErA9KNq8KMBV6fgi6Q7e1C5VO66KPUic266Ft59wtW8BCHdTBWW8 tVnBjh0Ir6y0DFHbZHywtnPrPqjFu485YKKz+2J6wZ72MUAhz2o9S+aBveNujq+aItI+ J+IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683255603; x=1685847603; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zZAZMeim1B7MwujURQSj1eNoXKesWwz2RFbf3IzRjn0=; b=YO4n8y7fbACQp7dMtaV+M8vulEO7IluhKhTWEhI5Oj1qOgC/9Jd5kWCTjD+MD2EMkg XMSpnz62SJoagfeUg5mqkEfum3sqaZgdBuhrzZ0z/ZHtS03u+A0G7XgV9AuFRLQTdPbM WgKHDKsPDbZ+s94ZJwu0ohv+7QUuNQvlW/MVRt9ytEpjklHyxScCylqPk+GtJI7AE5bj AShXzJ3uDp7BUxhmPLV2FIDqwiry1ddLjyanGoGyxA6KIX++l8sCpiVeDTPWyNIm6lrG kU3/9md4fFAjg6MkROVvL45yjdl42cNgHhxyzv175do8xg62uIjTY7pgPYXj51dmo+wI LBlA== X-Gm-Message-State: AC+VfDyC0WujMa1osX+mVX8Da6OVyHIYI21kXvWKQ4n5LitKxaS0cV8T 55PXKgjjf7Wr+8zg4xe2yzU= X-Google-Smtp-Source: ACHHUZ6Al5GpG9iv/6VJzBWWvhcIY0eV4bHVUnma2i4e4ZXXL9aXQVjsiZwu8q0qqEru4a9MUEV+wA== X-Received: by 2002:a05:6512:932:b0:4d8:6540:a731 with SMTP id f18-20020a056512093200b004d86540a731mr98000lft.47.1683255602874; Thu, 04 May 2023 20:00:02 -0700 (PDT) Received: from [1.0.0.7] ([178.155.28.115]) by smtp.gmail.com with ESMTPSA id g15-20020a19ee0f000000b004f00f29abe0sm108488lfb.151.2023.05.04.20.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 May 2023 20:00:02 -0700 (PDT) Message-ID: <2006c427-e070-b58a-e6d5-6fefa9f1a268@gmail.com> Date: Fri, 5 May 2023 06:00:00 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: BUG #17920: Incorrect memory access in array_position(s) is detected (or not) Content-Language: en-US To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org, Richard Guo References: <17920-f7c228c627b6d02e@postgresql.org> <3414186.1683215628@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: <3414186.1683215628@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 04.05.2023 18:53, Tom Lane wrote: >> ==00:00:00:04.955 1823064== at 0x5EDBD0: array_positions >> (array_userfuncs.c:806) > Fixed, thanks for the report! Thank you for the fix! >> I was surprised by the fact, that Valgrind doesn't complain for the single >> query execution: >> SELECT array_positions('{}'::int[], 1) >> IIUC, this is explained by the lack of red zones around palloc'ed memory >> areas. >> sed "s/VALGRIND_CREATE_MEMPOOL(\(.*\), 0, false);/VALGRIND_CREATE_MEMPOOL(\1, 16, false);/" -i >> src/backend/utils/mmgr/mcxt.c >> on master (after 414d66220) helps Valgrind to detect the invalid read on >> the single query execution too: > I didn't try valgrind'ing this on HEAD, but I think this situation > might have been improved by 414d66220. Exactly. On 414d66220~1, with the redzones added (VALGRIND_CREATE_MEMPOOL(..., 16, ...) [1]) even initdb fails: running bootstrap script ... ==00:00:00:00.782 2770024== Invalid read of size 4 ==00:00:00:00.782 2770024==    at 0x7729F8: GetMemoryChunkMethodID (mcxt.c:195) ==00:00:00:00.782 2770024==    by 0x7729F8: pfree (mcxt.c:1439) ==00:00:00:00.782 2770024==    by 0x3D7D8D: check_temp_tablespaces (tablespace.c:1304) ==00:00:00:00.782 2770024==    by 0x757492: call_string_check_hook (guc.c:6838) ==00:00:00:00.782 2770024==    by 0x7595B6: InitializeOneGUCOption (guc.c:1689) ==00:00:00:00.782 2770024==    by 0x75BC10: InitializeGUCOptions (guc.c:1520) ==00:00:00:00.782 2770024==    by 0x2CE4DD: BootstrapModeMain (bootstrap.c:215) ==00:00:00:00.782 2770024==    by 0x4694F3: main (main.c:189) ==00:00:00:00.782 2770024==  Address 0x73b7fd0 is 8 bytes before a block of size 1 client-defined ==00:00:00:00.782 2770024==    at 0x77186D: MemoryContextAlloc (mcxt.c:1035) ==00:00:00:00.782 2770024==    by 0x7731FB: MemoryContextStrdup (mcxt.c:1616) ==00:00:00:00.782 2770024==    by 0x773225: pstrdup (mcxt.c:1626) ==00:00:00:00.782 2770024==    by 0x3D7B46: check_temp_tablespaces (tablespace.c:1210) ==00:00:00:00.782 2770024==    by 0x757492: call_string_check_hook (guc.c:6838) ==00:00:00:00.782 2770024==    by 0x7595B6: InitializeOneGUCOption (guc.c:1689) ==00:00:00:00.782 2770024==    by 0x75BC10: InitializeGUCOptions (guc.c:1520) ==00:00:00:00.782 2770024==    by 0x2CE4DD: BootstrapModeMain (bootstrap.c:215) ==00:00:00:00.782 2770024==    by 0x4694F3: main (main.c:189) ==00:00:00:00.782 2770024== But on HEAD `make check` passes for me. So may be it makes sense to add the redzones for v16?/v17 to catch such errors more reliably? Though it's not clear to me, how many previously missed cases could be detected with the redzones. Anyway, I'm going to perform tests with this additional protection and share my findings. [1] https://valgrind.org/docs/manual/mc-manual.html#mc-manual.mempools Best regards, Alexander