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 1keYSq-0004k2-EU for pgsql-bugs@arkaria.postgresql.org; Mon, 16 Nov 2020 06:58:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1keYSp-0006Es-7e for pgsql-bugs@arkaria.postgresql.org; Mon, 16 Nov 2020 06:58: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 1keSlO-00089f-9F for pgsql-bugs@lists.postgresql.org; Mon, 16 Nov 2020 00:52:58 +0000 Received: from sprint-2.amdmi3.ru ([185.185.68.145]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1keSlK-0004Yk-VS for pgsql-bugs@lists.postgresql.org; Mon, 16 Nov 2020 00:52:56 +0000 Received: from amdmi3.ru (localhost [127.0.0.1]) by sprint-2.amdmi3.ru (Postfix) with SMTP id D3108187E60; Mon, 16 Nov 2020 03:52:51 +0300 (MSK) Received: by amdmi3.ru (nbSMTP-1.00) for uid 1000 amdmi3@amdmi3.ru; Mon, 16 Nov 2020 03:52:51 +0300 (MSK) Date: Mon, 16 Nov 2020 03:45:29 +0300 From: Dmitry Marakasov To: Andres Freund Cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16696: Backend crash in llvmjit Message-ID: <20201116004529.GC5149@hades.panopticon> References: <16696-29d944a33801fbfe@postgresql.org> <20201102214509.GV30304@hades.panopticon> <7463da2f-80f2-43c4-81d4-c3bab1f040b4@www.fastmail.com> <20201104212015.GA30304@hades.panopticon> <20201110054802.bjmjkfv4gzj3nkbe@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20201110054802.bjmjkfv4gzj3nkbe@alap3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk * Andres Freund (andres@anarazel.de) wrote: > > The problem happens when boolin() function is inlined by LLVM. > > The named function calls isspace() internally, which on FreeBSD is > > locale-specific and involves caching some locale parameters in > > thread-local variable defined as > > > > extern _Thread_local const _RuneLocale *_ThreadRuneLocale; > > > > The execution crashes on trying to access the named thread-local varible, > > probably because something related to TLS is not set up properly in/for > > LLVM. > > > > I've confirmed this hypothesis by disabling isspace() calls in boolin() > > which has also fixed the problem. > > Nice digging. I suspect your approach of disabling inlining for TLS > might be the most reasonable one for now. > > Any chance you're interested in extending your patch with a testcase > that crashes reliably without it? Best on multiple platforms, including > linux? Easiest way would probably be to add a function referencing a > thread local variable in regress.c and set jit_above_cost = 0, > jit_inline_above_cost=0 for a query using that function. Can try. Here's what I could come with so far: https://github.com/postgres/postgres/compare/master...AMDmi3:llvm-jit-regress.patch Unfortunately it doesn't crash with the dedicated function, likely because there's no llvm bitcode generation performed for regress.c. It does however reliably crash on SELECT (jsonb_array_elements('[true]'::jsonb)->>0)::boolean; or SELECT (case when random() >= 0.5 then 'true' else 'false' end)::boolean; but it would likely not reproduce on Linux due to different isspace implementation. Notably, it does not crash on simple 'true'::boolean, I guess because it may be evaluated at compile time. -- Dmitry Marakasov . 55B5 0596 FF1E 8D84 5F56 9510 D35A 80DD F9D2 F77D amdmi3@amdmi3.ru ..: https://github.com/AMDmi3