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 1pZQMv-0004GV-Cq for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Mar 2023 06:00:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pZQMt-00021s-O7 for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Mar 2023 06:00:11 +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 1pZQMt-0001uL-DJ for pgsql-bugs@lists.postgresql.org; Tue, 07 Mar 2023 06:00:11 +0000 Received: from mail-lj1-x22e.google.com ([2a00:1450:4864:20::22e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pZQMn-0000qS-TY for pgsql-bugs@lists.postgresql.org; Tue, 07 Mar 2023 06:00:10 +0000 Received: by mail-lj1-x22e.google.com with SMTP id a32so12056667ljr.9 for ; Mon, 06 Mar 2023 22:00:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678168804; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=h2nZpOlijmqyS3oyqd9HhoW1+J9lI+da0xIXgXBhjOg=; b=XNFMeVUwZKNoF9+xTkeumQKBlO1skuh0uAuZHPH0p7P2xwcJp5W9nL3+533FqU2bjB NghxSb9bKWJGoTBrpufWkHO6oKXxXe4cXbq9z26SM0BWqpIFofMBPHJgbfpcBLKX2BGh qEe6iLW27cb6A1m17WwRr/DTZtG1fjbVO0KJ1j6dyNEazP/Mf6nhU3b8ZAsqGGd6j3Ev xwD9G3MWrfr6esgNq6xubSXVJbe+ijJ6Hsq3Dk5X1TGuYIUpgLqg25psxlD/RzWhVgBj lXejcu8rLeLKjzuCGz1Zht1wjgaxDQ//wTRR+USqys2qwMxFrE00vKCJfe+Mk0m8baCV 88ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678168804; h=in-reply-to:from:references: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=h2nZpOlijmqyS3oyqd9HhoW1+J9lI+da0xIXgXBhjOg=; b=ypLClzEr1TvLLKlpWvMmSZwEJQWlUPJbS+yKiD+UazzuiFddCwAOu6sD3icuC9UiBf bos84Bp73HMO16xE9zVtdFQs3Slva6uQCqPtWeymGb/Vf7VKqsaHo0j3iK+CxKKSWDuB msNyBuP8x+0CD4qN23TUq+Ah4RL8/l794WfyPM7nLDo3oWsFq64JHZFnLrbnxSp0uHdJ DO4n6o82aq9dTGfqOWPR9ETRYVRPGp6PS+Xg/Z7Y72iJfqDIBdNFLvX1x6qWgk1lcJ5C YnsFHEyuA2Rc9rY/PQpAIThZYRlgvAl1yr5/UtfAMpY0/HGCMuqg4x0enGstH4bNv92N YMkg== X-Gm-Message-State: AO0yUKXzsu0tWaX8ZjtV30KCG4ua4krZwaOHp/sgOBbObm0vLlhnxOBP prNKPhvyxNzHQIpt5ebVVaQh0N/mpFI= X-Google-Smtp-Source: AK7set917USm01ZTaxDVlN+XUUG2mPHiIEwEv2prZqaQLZR26LVQNnTBVdfALRVmAaGM3YrcR5SzeQ== X-Received: by 2002:a2e:a593:0:b0:293:4be1:a581 with SMTP id m19-20020a2ea593000000b002934be1a581mr4903951ljp.7.1678168803668; Mon, 06 Mar 2023 22:00:03 -0800 (PST) Received: from [1.0.0.7] ([178.155.5.178]) by smtp.gmail.com with ESMTPSA id d16-20020a2e3310000000b00295b1afd27bsm1975003ljc.66.2023.03.06.22.00.01 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Mar 2023 22:00:02 -0800 (PST) Content-Type: multipart/mixed; boundary="------------t0xaa0vFwpI0asFivMd0YgTP" Message-ID: <87fe6c28-80b2-f709-a8db-d105e5240e39@gmail.com> Date: Tue, 7 Mar 2023 09:00:01 +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 #17798: Incorrect memory access occurs when using BEFORE ROW UPDATE trigger Content-Language: en-US To: pgsql-bugs@lists.postgresql.org References: <17798-0907404928dcf0dd@postgresql.org> From: Alexander Lakhin In-Reply-To: <17798-0907404928dcf0dd@postgresql.org> 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. --------------t0xaa0vFwpI0asFivMd0YgTP Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 17.02.2023 13:00, PG Bug reporting form wrote: > The following bug has been logged on the website: > ... > > I've found the following explanation for the failure: > 1) After the ExecGetUpdateNewTuple() call the newslot and the oldslot are > linked together (their slot->tts_values[1] in this case point to the same > memory address (inside the oldslot' buffer)). > 2) Previously, GetTupleForTrigger() could get a tuple with a new buffer, > so the oldslot would be the only user of the buffer at that moment. > (The newslot doesn't become an official user of the buffer.) > 3) Then, trigtuple = ExecFetchSlotHeapTuple(oldslot, ...) invokes > tts_buffer_heap_materialize() where the oldslot->buffer is released. > 4) Finally, newtuple = ExecFetchSlotHeapTuple(newslot, ...) invokes > tts_buffer_heap_materialize() where an incorrect access to memory > that became anonymous occurs, and that is detected by valgrind. > If not detected, different consequences are possible (in the asan case > it was memcpy with an incorrectly read extra large data_len). I've tried to materialize newslot before the oldslot materialization (in ExecFetchSlotHeapTuple(), where their common memory is released), and it looks like it fixes the issue. The similar thing done in 75e03eabe. But I don't know the code good enough so maybe I'm missing something. Best regards, Alexander --------------t0xaa0vFwpI0asFivMd0YgTP Content-Type: text/x-patch; charset=UTF-8; name="v1-01-Fix-memory-access-in-BRU-trigger-DRAFT.patch" Content-Disposition: attachment; filename="v1-01-Fix-memory-access-in-BRU-trigger-DRAFT.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2NvbW1hbmRzL3RyaWdnZXIuYyBiL3NyYy9iYWNr ZW5kL2NvbW1hbmRzL3RyaWdnZXIuYwppbmRleCA2NjQwMWYyODM5Li5hNDA5ZmYwYmUxIDEw MDY0NAotLS0gYS9zcmMvYmFja2VuZC9jb21tYW5kcy90cmlnZ2VyLmMKKysrIGIvc3JjL2Jh Y2tlbmQvY29tbWFuZHMvdHJpZ2dlci5jCkBAIC0yOTkyLDYgKzI5OTIsOCBAQCBFeGVjQlJV cGRhdGVUcmlnZ2VycyhFU3RhdGUgKmVzdGF0ZSwgRVBRU3RhdGUgKmVwcXN0YXRlLAogCiAJ CQlpZiAobmV3c2xvdCAhPSBlcHFzbG90X2NsZWFuKQogCQkJCUV4ZWNDb3B5U2xvdChuZXdz bG90LCBlcHFzbG90X2NsZWFuKTsKKwkJCWVsc2UKKwkJCQlFeGVjTWF0ZXJpYWxpemVTbG90 KG5ld3Nsb3QpOwogCQl9CiAKIAkJdHJpZ3R1cGxlID0gRXhlY0ZldGNoU2xvdEhlYXBUdXBs ZShvbGRzbG90LCB0cnVlLCAmc2hvdWxkX2ZyZWVfdHJpZyk7Cg== --------------t0xaa0vFwpI0asFivMd0YgTP--