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 1pDKKj-0000Wc-F3 for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jan 2023 07:06:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pDKKi-0005Cb-AR for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jan 2023 07:06:36 +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 1pDKKi-0005CS-1e for pgsql-hackers@lists.postgresql.org; Thu, 05 Jan 2023 07:06:36 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pDKKg-0004V7-OS for pgsql-hackers@postgresql.org; Thu, 05 Jan 2023 07:06:35 +0000 Received: by mail-pl1-x631.google.com with SMTP id d15so38517564pls.6 for ; Wed, 04 Jan 2023 23:06:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=6+IsmHPkndOluIjVBQu6rsppF4weenI09CnijQnRbHg=; b=HmvxuyD1lXhHFMewv4kviX+uP0LA6egITEIIm81OgrRIUzDpackViQtvp2TWAAnZ7q 6AsNz+98cpupf+yki5mNX+0HcfK0Iwa6uQq3yHjYFRpbMzxsiZ4FIE8X+6YXlJqcSgUZ ZJjnc+R5ncQLxAGr53YhxlKBtVWW0PAJ/AqzAWV0ka9xBLy72Cw5Q8zwG4saxSLy/1mp oOR2WvxkzD50eV/DVNjoadYEX+C1aMZM2tg/Vv+zfdSATCPPPgTVubGJX85TPcd93mT4 TndiQUUugDTYUNMNZ7AJfD788uDGCiUtJEOpcGcKJTVnEJforZRs9LJ0wKeSwsnjnUKN g03w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6+IsmHPkndOluIjVBQu6rsppF4weenI09CnijQnRbHg=; b=A18iw6fiIVSeLMvcqvR9SkneeLam72Q+LY00pB+mH6qs0x+EtbZqaMyZMlgpq8lfUO mahd4suEQTf+Jgcce3QK3jXOrIWWKuMHF/Dg3e/LHwF1S7IIqJYZ/uWFMMgBU0+LMRHu WLJJo2+oUTp/KS8lbPC9cI7xaKyHKHDzjj1g6MYAYbrUf7GCh+TgcxfX5npxdRGp3lMr lUlgly8Ax824lHTnQqznF4A9EEDiCa65/6QOoL1gRTl1QachW5wDAhc8TsmQzWY2fMtU pEwGBfAW8R4B8v7ogMovB9wesaFjkrDwuRMpZSp719fvMMlRBSxBhXP0v9pJJ6/QHWAT /BKg== X-Gm-Message-State: AFqh2kqhUXIuC/WIBi8lXgF1uRrhsr2qXLnSWnIq/K9EByPucsPuAS+c xwI9FW9sfLbpIgk6PSDlenxqAn4o3intSgD5nNg= X-Google-Smtp-Source: AMrXdXtuvpeJQ5oldePBbqixqBkrj8CB1/2n1PrBgFxLcTrXTr4IgTAM68ShRmc8wfsrAbSho5RA7rvLW6HPFklpoVQ= X-Received: by 2002:a17:902:cecb:b0:190:f88e:255 with SMTP id d11-20020a170902cecb00b00190f88e0255mr3840289plg.114.1672902392938; Wed, 04 Jan 2023 23:06:32 -0800 (PST) MIME-Version: 1.0 References: <1650235.1672694719@sss.pgh.pa.us> In-Reply-To: From: Richard Guo Date: Thu, 5 Jan 2023 15:06:21 +0800 Message-ID: Subject: Re: An oversight in ExecInitAgg for grouping sets To: David Rowley Cc: Tom Lane , PostgreSQL-development Content-Type: multipart/alternative; boundary="000000000000e0049c05f17ef0d4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e0049c05f17ef0d4 Content-Type: text/plain; charset="UTF-8" On Thu, Jan 5, 2023 at 6:18 AM David Rowley wrote: > On Tue, 3 Jan 2023 at 10:25, Tom Lane wrote: > > The thing that I find really distressing here is that it's been > > like this for years and none of our automated testing caught it. > > You'd have expected valgrind testing to do so ... but it does not, > > because we've never marked that word NOACCESS. Maybe we should > > rethink that? It'd require making mcxt.c do some valgrind flag > > manipulations so it could access the hdrmask when appropriate. > > Yeah, that probably could have been improved during the recent change. > Here's a patch for it. Thanks for the patch. With it Valgrind is able to catch the invalid read discussed in the initial email of this thread. VALGRINDERROR-BEGIN Invalid read of size 8 at 0x4DB056: ExecInitAgg by 0x4C486A: ExecInitNode by 0x4B92B7: InitPlan by 0x4B81D7: standard_ExecutorStart by 0x4B7F1B: ExecutorStart I reviewed this patch and have some comments. It seems that for MemoryContextMethods in alignedalloc.c the access to the chunk header is not wrapped by VALGRIND_MAKE_MEM_DEFINED and VALGRIND_MAKE_MEM_NOACCESS. Should we do that? In GenerationFree, I think the VALGRIND_MAKE_MEM_DEFINED should be moved to the start of this function, before we call MemoryChunkIsExternal. In SlabFree, we should call MemoryChunkGetBlock after the call of VALGRIND_MAKE_MEM_DEFINED, just like how we do in SlabRealloc. In AllocSetStats, we have a call of MemoryChunkGetValue in Assert. I think we should wrap it with VALGRIND_MAKE_MEM_DEFINED and VALGRIND_MAKE_MEM_NOACCESS. Thanks Richard --000000000000e0049c05f17ef0d4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Thu, Jan 5, 2023 at 6:18 AM David Rowl= ey <dgrowleyml@gmail.com>= wrote:
On Tue, = 3 Jan 2023 at 10:25, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> The thing that I find really distressing here is that it's been > like this for years and none of our automated testing caught it.
> You'd have expected valgrind testing to do so ... but it does not,=
> because we've never marked that word NOACCESS.=C2=A0 Maybe we shou= ld
> rethink that?=C2=A0 It'd require making mcxt.c do some valgrind fl= ag
> manipulations so it could access the hdrmask when appropriate.

Yeah, that probably could have been improved during the recent change.
Here's a patch for it.
=C2=A0
Thanks for the= patch.=C2=A0 With it Valgrind is able to catch the invalid
read discuss= ed in the initial email of this thread.

=C2=A0VALGRINDERROR-BEGIN=C2=A0Invalid read of size 8
=C2=A0 =C2=A0 at 0x4DB056: ExecInitAgg
= =C2=A0 =C2=A0 by 0x4C486A: ExecInitNode
=C2=A0 =C2=A0 by 0x4B92B7: InitP= lan
=C2=A0 =C2=A0 by 0x4B81D7: standard_ExecutorStart
=C2=A0 =C2=A0 b= y 0x4B7F1B: ExecutorStart

I reviewed this patch and have some commen= ts.

It seems that for MemoryContextMethods in alignedalloc.c the acc= ess to
the chunk header is not wrapped by VALGRIND_MAKE_MEM_DEFINED and<= br>VALGRIND_MAKE_MEM_NOACCESS.=C2=A0 Should we do that?

In Generatio= nFree, I think the VALGRIND_MAKE_MEM_DEFINED should be moved
to the star= t of this function, before we call MemoryChunkIsExternal.

In SlabFre= e, we should call MemoryChunkGetBlock after the call of
VALGRIND_MAKE_ME= M_DEFINED, just like how we do in SlabRealloc.

In AllocSetStats, we = have a call of MemoryChunkGetValue in Assert. =C2=A0I
think we should wr= ap it with VALGRIND_MAKE_MEM_DEFINED and
VALGRIND_MAKE_MEM_NOACCESS.
=
Thanks
Richard
--000000000000e0049c05f17ef0d4--