Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slkKR-000vfM-Bn for pgsql-bugs@arkaria.postgresql.org; Wed, 04 Sep 2024 07:21:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1slkKP-005b2W-DR for pgsql-bugs@arkaria.postgresql.org; Wed, 04 Sep 2024 07:21:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slkKO-005b2N-Pu for pgsql-bugs@lists.postgresql.org; Wed, 04 Sep 2024 07:21:21 +0000 Received: from relay8-d.mail.gandi.net ([2001:4b98:dc4:8::228]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slkKJ-0004fW-Dc for pgsql-bugs@lists.postgresql.org; Wed, 04 Sep 2024 07:21:19 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id C47C21BF208; Wed, 4 Sep 2024 07:21:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vondra.me; s=gm1; t=1725434472; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8NqIYsvng4XhFCRlw8aB7Nr+3GAs+K3ThuilF76bhtI=; b=hynR6ctbmYA621E2dlEK3WDyHaZLcSf3ixmkpdR28XpE4OO96vwnjWerHCRgqBBDDCycwe oGHpR4IcgFq/vHQuAKJ7daH+sjjxLdE4stK+un+RoLtH4v0NMFROB8g83YA1YMcCmhcA8F 90bpkQDQTnJO52mgev8dshzcEFCJFqyQLx9oD1wZXCS89SzrUfb/q6d5I/FcpSf7bjXBN0 s1DP/COSEyBHdDUNBE9npC3uZrrv2QjnLNIAbpGraB1CVbC3GNHSUUjbMRZJvv4gKLd5DM nQGLy9l/qUEbUlpRMvGYMKJCUkbs8h95W+AslCOrFTx4FdLCLiuOvM+/Lb9piQ== Message-ID: <98481f61-abf6-46df-8e83-82f6ff66cefb@vondra.me> Date: Wed, 4 Sep 2024 09:21:10 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #18598: AddressSanitizer detects use after free inside json_unique_hash_match() To: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <18598-3279ed972a2347c7@postgresql.org> Content-Language: en-US From: Tomas Vondra In-Reply-To: <18598-3279ed972a2347c7@postgresql.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-GND-Sasl: tomas@vondra.me List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 9/1/24 21:00, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 18598 > Logged by: Alexander Lakhin > Email address: exclusion@gmail.com > PostgreSQL version: 17beta3 > Operating system: Ubuntu 22.04 > Description: > > The following query: > SELECT JSON_OBJECTAGG(i: (i)::text FORMAT JSON WITH UNIQUE) > FROM generate_series(1, 100000) i; > > triggers an asan-detected error: > ==973230==ERROR: AddressSanitizer: heap-use-after-free on address > 0x7fde473f4428 at pc 0x558af80f20a6 bp 0x7ffe6b8e2df0 sp 0x7ffe6b8e2598 > READ of size 7 at 0x7fde473f4428 thread T0 > #0 0x558af80f20a5 in __interceptor_strncmp.part.0 > (.../usr/local/pgsql/bin/postgres+0x32d40a5) > #1 0x558af9ed5276 in json_unique_hash_match > ... > > Reproduced starting from 7081ac46a. > FWIW I can reproduce this using valgrind, with the same stacks reported. This feels very much like a classical memory context bug - pointing to memory in a short-lived memory context. I see datum_to_json_internal() allocates the result in ExprContext, and that's bound to be reset pretty often. But I'm not too familiar with the JSON aggregate stuff enough to pinpoint what it does wrong. regards -- Tomas Vondra