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 1pesS9-00075a-SV for pgsql-bugs@arkaria.postgresql.org; Wed, 22 Mar 2023 07:00:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pesS8-0003Bo-EB for pgsql-bugs@arkaria.postgresql.org; Wed, 22 Mar 2023 07:00:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pesS8-0003Be-2i for pgsql-bugs@lists.postgresql.org; Wed, 22 Mar 2023 07:00:08 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pesS5-0000g0-08 for pgsql-bugs@lists.postgresql.org; Wed, 22 Mar 2023 07:00:07 +0000 Received: by mail-lj1-x232.google.com with SMTP id q14so3496219ljm.11 for ; Wed, 22 Mar 2023 00:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679468403; h=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=IEJDBZh+l3FRBEfCERnFsnBHp5976+NW8Z2DbF47kQg=; b=AmK9uuz1W08fKBjzcnJe3bN15O01oGfQod3576ThY9ApiNJNym37mRAmzc8N1hKK9f V+fvs/W5cjID6f/pCZyKVfIyqJ9Iv4CxgZdRijdXoKYDEwmLWklZfVx8qbYUBltIS2bf pj5ucC4VRgjdl7DqJWoaHa449d6tbxz4pDywxL3c2+YihtNf3dVvWTaEoFJmFmJjwpOf 0nH/V7eIhSZlmGPhIaBM5TxPibj4ZJV17c0/3ZTZYbN5Czo7OML1QaBFFmBFu3Tk1w2O +0wFHWODmjbKISGy64IWdZAPZA4/DA1NzjhakcyaZnMk/5GDpCaw//cPcnVkm0jET78R 1NKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679468403; h=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=IEJDBZh+l3FRBEfCERnFsnBHp5976+NW8Z2DbF47kQg=; b=mE/Qa/QlAsYS65UXgXBpfAh4XaT97v9Ff6WX7gPKtbn7jV1xMpefHJgEsp1qAyX6KR 7CctEIV3uXFE9oaMAbXY4KFO8I/Hvzc2Shi4lfkM8ZEHGr5o1eynqg2FbBwa/tfghqMv yiXiazN/Z4qE6q9DIi4vxBBRIgYiXNN2n0XHO0DF8+9ekprL/2BCgQ+P4NC+Rbwhhp6a wfC3Tzlo/1a/UTTQff84gMQAgCDInp2FTYSp7aeCPUVdmWwKLaYuNPIOwylktYOHx4xx YxBk6EUr234jawlDdOsgxegyRRoV3V6eGkBNlqYK4rvnng8Yh8PXVdODxxtaCfEjoo6Z 6UOg== X-Gm-Message-State: AO0yUKUWGo/nH5G9kGpbgntCFvbUZYm16znBFmU5jiTyuBSHb7X/dfyP OMsPGPqsSRGBlnrpwwrghjU= X-Google-Smtp-Source: AK7set/bkKU4xx0m2lSugqdKvaBsV8486Ce4uMUXnWLmoi8NEuHJ44b5cQB1UT3skZ9PH+DWfO0QDQ== X-Received: by 2002:a2e:981a:0:b0:29c:94c7:46c0 with SMTP id a26-20020a2e981a000000b0029c94c746c0mr2232639ljj.5.1679468402950; Wed, 22 Mar 2023 00:00:02 -0700 (PDT) Received: from [1.0.0.7] ([178.155.6.98]) by smtp.gmail.com with ESMTPSA id o4-20020a2e90c4000000b00299fe6c262csm2499102ljg.77.2023.03.22.00.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 22 Mar 2023 00:00:02 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------0HnbQH2IxeoqrEfLaNAln0c8" Message-ID: <3c2ca9f5-a69a-71e2-1d70-6c33702009b2@gmail.com> Date: Wed, 22 Mar 2023 10: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 #17858: ExecEvalArrayExpr() leaves uninitialised memory for multidim array with nulls Content-Language: en-US To: Richard Guo , Tom Lane Cc: pgsql-bugs@lists.postgresql.org References: <17858-8fd287fd3663d051@postgresql.org> <4240d86f-7e9f-388b-fd96-f8944860c0f5@gmail.com> <1216375.1679408668@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------0HnbQH2IxeoqrEfLaNAln0c8 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 22.03.2023 06:07, Richard Guo wrote: > > On Tue, Mar 21, 2023 at 10:24 PM Tom Lane wrote: > > Alexander Lakhin writes: > > I'm afraid that zeroing only bytes behind nitems bits is not enough, as outDatum() doesn't bother to calculate > the exact > > size of nulls bitmap, it just outputs all bytes of a datum (40 bytes in that case): > > In that case, won't padding bytes between array elements also create > issues?  Seems like we have to just zero the whole array area, like > those other functions do. > > > Yeah, this should be the right fix, to use palloc0 instead here.  FWIW > currently in the codes there are 14 places that explicitly allocate > ArrayType, 13 of them using palloc0, the only exception is the one > discussed here. > > BTW, in array_set_slice() and array_set_element() we explicitly zero the > null bitmap although the whole array area is allocated with palloc0.  Is > this necessary? > I had also wondered, how come this call of array_bitmap_copy() shown as covered here: https://coverage.postgresql.org/src/backend/executor/execExprInterp.c.gcov.html but valgrind doesn't complain during `make check` (with WRITE_READ_PARSE_PLAN_TREES). I've found the query, which is reaching that call: select percentile_disc(array[[null,1,0.5],[0.75,0.25,null]]) within group (order by thousand) from tenk1; and the absence of the valgrind complaint here is explained by the array coercion performed implicitly. The query modification: select percentile_disc(array[[null,1,0.5],[0.75,0.25,null]]::float8[]) within group (order by thousand) from tenk1; can confirm the fix (if needed). With the following check: if (result) VALGRIND_CHECK_MEM_IS_DEFINED(result, VARSIZE(result)); added at the end of ExecEvalArrayExpr() I see no failures during `make check` (except for select percentile_disc(...)) so it seems that uninitialised padding bytes between array elements are not produced with the existing regression tests. Though a comment in array.h says:  * The actual data starts on a MAXALIGN boundary.  Individual items in the  * array are aligned as specified by the array element type. ... I'll try to find cases where the alignment padding is possible... Though if palloc0() used in 13 places, maybe the minor optimization (with zeroing only the null bitmap) doesn't justify the inconsistency. Best regards, Alexander --------------0HnbQH2IxeoqrEfLaNAln0c8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
22.03.2023 06:07, Richard Guo wrote:

On Tue, Mar 21, 2023 at 10:24 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Alexander Lakhin <exclusion@gmail.com> writes:
> I'm afraid that zeroing only bytes behind nitems bits is not enough, as outDatum() doesn't bother to calculate the exact
> size of nulls bitmap, it just outputs all bytes of a datum (40 bytes in that case):

In that case, won't padding bytes between array elements also create
issues?  Seems like we have to just zero the whole array area, like
those other functions do.

Yeah, this should be the right fix, to use palloc0 instead here.  FWIW
currently in the codes there are 14 places that explicitly allocate
ArrayType, 13 of them using palloc0, the only exception is the one
discussed here.

BTW, in array_set_slice() and array_set_element() we explicitly zero the
null bitmap although the whole array area is allocated with palloc0.  Is
this necessary?


I had also wondered, how come this call of array_bitmap_copy() shown as covered here:
https://coverage.postgresql.org/src/backend/executor/execExprInterp.c.gcov.html
but valgrind doesn't complain during `make check` (with WRITE_READ_PARSE_PLAN_TREES).
I've found the query, which is reaching that call:
select percentile_disc(array[[null,1,0.5],[0.75,0.25,null]]) within group (order by thousand)
from tenk1;
and the absence of the valgrind complaint here is explained by the array coercion performed implicitly.
The query modification:
select percentile_disc(array[[null,1,0.5],[0.75,0.25,null]]::float8[]) within group (order by thousand)
from tenk1;
can confirm the fix (if needed).

With the following check:
if (result) VALGRIND_CHECK_MEM_IS_DEFINED(result, VARSIZE(result));
added at the end of ExecEvalArrayExpr() I see no failures during `make check` (except for select percentile_disc(...)) so it seems that uninitialised padding bytes between array elements are not produced with the existing regression tests. Though a comment in array.h says:
 * The actual data starts on a MAXALIGN boundary.  Individual items in the
 * array are aligned as specified by the array element type. ...

I'll try to find cases where the alignment padding is possible...

Though if palloc0() used in 13 places, maybe the minor optimization (with zeroing only the null bitmap) doesn't justify the inconsistency.

Best regards,
Alexander
--------------0HnbQH2IxeoqrEfLaNAln0c8--