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.96) (envelope-from ) id 1wSEEH-0032wS-1I for pgsql-hackers@arkaria.postgresql.org; Wed, 27 May 2026 13:23:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wSEEE-008LGe-08 for pgsql-hackers@arkaria.postgresql.org; Wed, 27 May 2026 13:23:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wSEED-008LGW-0Q for pgsql-hackers@lists.postgresql.org; Wed, 27 May 2026 13:23:22 +0000 Received: from fout-b8-smtp.messagingengine.com ([202.12.124.151] helo=fout-c8-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wSEEA-00000001hBr-2rM6 for pgsql-hackers@postgresql.org; Wed, 27 May 2026 13:23:21 +0000 Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfout.stl.internal (Postfix) with ESMTP id 901001D00108 for ; Wed, 27 May 2026 09:23:14 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Wed, 27 May 2026 09:23:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm3; t=1779888194; x=1779974594; bh=ohKgsiumpULqNkHjbsOVdW6+emyfxGbt nD7UQxXTT7s=; b=qnYjhCY+bOX1h0ZzPwaz6riCMicErTviLWQIqZrZg6vTo02D b70l/vi6cxxMjH9eGcGwV2dGdIlohLHAOFkBuOrM/TgOCNQ8EscMQc5R/q8HOCz6 etff6xb3PyAy++U/VmXDwV0/oekL1AsYJAHPfHY66rTp1ba2FTjfYvzQCH9S+Dai aTozI8neLJMkyEsKm+eh+3xC1rjnhNDhH1LV+YNguPU7xpUM9d4Sobr4MSj9wHGx lqOPXJybgBpsiso9ZFPFxsJUdCw1p7n+lfYn5jAsD0EKnK7RRHiBajMb93EMe54L rtJaXX25HPt4wAmCXtRtp9QRWRGSS8toCj1tOw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1779888194; x= 1779974594; bh=ohKgsiumpULqNkHjbsOVdW6+emyfxGbtnD7UQxXTT7s=; b=N Db4Z1NYvyQeWkMrxKg1IxZZ+T+BtnBdRHZNaI/CJ68AO//742t+OsTUMX/zGTY3N 3jOo/lFcBkV0zM21hgphTvjePzt0h9kdZCrOWGtok9OfhksNmqDiGOoq47KiM7zD qOnRs5nQkbS2lpYELUaA8kPWOBxfo2N4cBRlv4ArDT+EcS/icmq0ixNzaxM3+xmC uxJN0xVQCdGlY1QX450hFcMGnWnaYryfayVa+HmeeqT9IbBw8YU9aoKvopcIEBOD Q8a6XvHXshIj/QJErjRQDMDzq9BfqaSkOSoK60sdiLkI9bw0PQ/C1LW31N8e+3Aw xUEe2GPb4Y3+re4h1Dsbw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTEuLjaDUtpSc6drXv8DnINrJTivQoC6PERXgKYQFnoO+r89BnTwwLi2MaVp0DrZ8z 5saMbGO+vPoiG/TEKZWr0+CrfddqXkmiW8FWYkeQcfwv33EkHMHtXmB0LCEa0FqgaHo9/F Vfoa5etk0brLrO3/dr52p55Q636oXq6S7lsoMVxhgOmwKa5aotNIMcL4xq3ximAXY06dzL qrnQdcnhIvBcX1xHeEhCWI+fBSSagQ7UREM0piIEzzT0bG0NWWdFdnL3EqUcpQv4NFQH2m to2tqn4yussqaRE+6gh4uZUih8z+nVbv4SZIgpTUNTCN5O9E9nfvnuwNG3rNIPrxu9BbMo EWw0Wns0Vkqk0upTe7JRMxlf2E/ox8cnXfglmth734p1mvifQxT9D1z65GupnlnMt6wBn1 /uFIg7N8SEkzoconhSHbTY+rqOPQfC+Hy3WQWkJNGYERUxdj3g5A+qjpWEAbqR+Bmgv/Uz CI8wyina3x6MV8w5UORQdLI/9hpz2UY58RQCqGu3HyAElo25wND+F7D8MypRn96jSIF00x AysPmQ8nVMz2F9MAYaRDgL9349HJRbDHqaLJLcdJaDk+AiAoNbwnoX8PiAuaqAyS0rFaAJ Ig7lKa+DL9Zpep/RQw35BSktdhJxB1Xw00qq7P4x04rL7jvjMtIboIaQnNfg X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 27 May 2026 09:23:13 -0400 (EDT) Date: Wed, 27 May 2026 09:23:13 -0400 From: Andres Freund To: pgsql-hackers@postgresql.org Subject: Make stack depth check work with asan's use-after-return Message-ID: <2kk4z4odvuyrg7qlwjd7ft4eron4cle4btb33v4qatgsdkayir@gj6e62rgsel4> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="7e2sypi5gxvkgog6" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --7e2sypi5gxvkgog6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, When running asan without further options against the backend, random tests fail because the stack-depth check fails. One can work around that by putting detect_stack_use_after_return=0 into ASAN_OPTIONS (at least with gcc, clang still uses a lot of stack, but i think that's a separate issue). The reason for that breakage is that with the stack-use-after-return logic, stack variables are moved to heap allocations, to allow to detect references to the memory at a later time. That breaks our stack-depth check, which is why we had to disable detect_stack_use_after_return in CI. Luckily __builtin_frame_address() works correctly, even under asan, so use that. I think we should backpatch this. I'd be worried about using __builtin_frame_address(), but we already do, for the base address of the stack. Greetings, Andres Freund --7e2sypi5gxvkgog6 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Make-stack-depth-check-work-with-asan-s-use-after.patch" From 86c44c934d4aa0604f82f289a0b6bd6ec0c88e31 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sat, 7 Feb 2026 14:04:06 -0500 Subject: [PATCH v2 1/4] Make stack depth check work with asan's use-after-return With address sanitizer's stack-use-after-return check, stack variables are moved to heap allocations, to allow to detect references to the memory at a later time. That broke our stack-depth check, which is why we had to disable detect_stack_use_after_return in CI. Luckily __builtin_frame_address() works correctly, even under asan, so use that. Author: Reviewed-by: Discussion: https://postgr.es/m/ Backpatch: --- src/backend/utils/misc/stack_depth.c | 17 ++++++++++++++++- .cirrus.tasks.yml | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/misc/stack_depth.c b/src/backend/utils/misc/stack_depth.c index 61a07cf824e..9955e28c176 100644 --- a/src/backend/utils/misc/stack_depth.c +++ b/src/backend/utils/misc/stack_depth.c @@ -108,13 +108,28 @@ check_stack_depth(void) bool stack_is_too_deep(void) { +#ifndef HAVE__BUILTIN_FRAME_ADDRESS char stack_top_loc; +#endif ssize_t stack_depth; + char *stack_address; + + /* + * With address sanitizer's stack-use-after-return check, stack variables + * are moved to heap allocations, to allow to detect references to the + * memory at a later time. That would break our stack-depth check. Luckily + * __builtin_frame_address() works correctly, even under asan. + */ +#ifndef HAVE__BUILTIN_FRAME_ADDRESS + stack_address = &stack_top_loc; +#else + stack_address = (char *) __builtin_frame_address(0); +#endif /* * Compute distance from reference point to my local variables */ - stack_depth = (ssize_t) (stack_base_ptr - &stack_top_loc); + stack_depth = (ssize_t) (stack_base_ptr - stack_address); /* * Take abs value, since stacks grow up on some machines, down on others diff --git a/.cirrus.tasks.yml b/.cirrus.tasks.yml index a22cef063f3..8683d1ae9c7 100644 --- a/.cirrus.tasks.yml +++ b/.cirrus.tasks.yml @@ -447,7 +447,7 @@ task: # print_stacktraces=1,verbosity=2, duh # detect_leaks=0: too many uninteresting leak errors in short-lived binaries UBSAN_OPTIONS: print_stacktrace=1:disable_coredump=0:abort_on_error=1:verbosity=2 - ASAN_OPTIONS: print_stacktrace=1:disable_coredump=0:abort_on_error=1:detect_leaks=0:detect_stack_use_after_return=0 + ASAN_OPTIONS: print_stacktrace=1:disable_coredump=0:abort_on_error=1:detect_leaks=0 # SANITIZER_FLAGS is set in the tasks below CFLAGS: -Og -ggdb -fno-sanitize-recover=all $SANITIZER_FLAGS -- 2.53.0.1.gb2826b52eb --7e2sypi5gxvkgog6--