From owner-freebsd-hackers@freebsd.org Mon Aug 12 17:41:03 2019 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 57FB4BAB0B for ; Mon, 12 Aug 2019 17:41:03 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 466jnB2gzpz4dk3 for ; Mon, 12 Aug 2019 17:41:02 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id x7CHedOJ049783 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Mon, 12 Aug 2019 20:40:42 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua x7CHedOJ049783 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id x7CHedfi049782; Mon, 12 Aug 2019 20:40:39 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Mon, 12 Aug 2019 20:40:39 +0300 From: Konstantin Belousov To: Greg Lewis Cc: freebsd-hackers@freebsd.org Subject: Re: Java stack overflow segfaults Message-ID: <20190812174039.GE2738@kib.kiev.ua> References: <20190812161629.GA99971@misty.eyesbeyond.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190812161629.GA99971@misty.eyesbeyond.com> User-Agent: Mutt/1.12.1 (2019-06-15) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-Rspamd-Queue-Id: 466jnB2gzpz4dk3 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=gmail.com (policy=none); spf=softfail (mx1.freebsd.org: 2001:470:d5e7:1::1 is neither permitted nor denied by domain of kostikbel@gmail.com) smtp.mailfrom=kostikbel@gmail.com X-Spamd-Result: default: False [-2.98 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; RCVD_TLS_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; IP_SCORE_FREEMAIL(0.00)[]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_HAM_SHORT(-0.98)[-0.976,0]; RCPT_COUNT_TWO(0.00)[2]; IP_SCORE(0.00)[ip: (-2.51), ipnet: 2001:470::/32(-4.49), asn: 6939(-3.02), country: US(-0.05)]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; FREEMAIL_ENVFROM(0.00)[gmail.com]; DMARC_POLICY_SOFTFAIL(0.10)[gmail.com : No valid SPF, No valid DKIM,none] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Aug 2019 17:41:03 -0000 On Mon, Aug 12, 2019 at 09:16:29AM -0700, Greg Lewis wrote: > Hi all, > > I'm investigating an issue where, on FreeBSD, Java will crash rather than > throw a StackOverflowError given a simple test program with a function > that just calls itself over and over. There's an example of such a test > in https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=222146 > > This affects, I suspect, every native version of Java in the ports tree, > although I've only tried openjdk8 and higher. My investigation has mostly > focused on openjdk11. > > To outline the situation, Java uses pthreads internally for threading. It > doesn't use the pthreads own guard page(s), but instead creates it's own > guard area at the bottom of the stack (which grows downward) using > mprotect. It then installs a signal handler and examines any SIGSEGV's > fault address to see if it falls within the guard area, and if so throws a > StackOverflowError. This logic is the same across all of the OSes I've > looked at and works on OpenBSD, Linux, etc. On FreeBSD though, the fault > address lies in the page above the guard zone, rather than in the guard > zone, which results in a crash rather than throwing StackOverflowError. > > An diagram may help here: > > --- <- Stack top > | > | Untouched memory + stack frames + etc. > | > | > | <-- SIGSEGV signal info fault address (< 1 page above guard zone) > --- <- Start of JVM reserved zone / guard zone > | > | JVM Reserved page > | > --- <- Start of JVM yellow zone > | > | JVM Yellow pages > | > --- <- Start of JVM red zone > | > | JVM Red page > | > --- <- Stack bottom > | > | Pthread guard page(s) > | > --- > > On my FreeBSD 11.3/amd64 machine the JVM uses a total of four pages for the > guard zone (1 reserved, 2 yellow, 1 red). The page size is 4K, and I see > the follow mprotect calls with truss: > > mprotect(stack bottom address, 4K, PROT_NONE) (Just the red zone) > mprotect(stack bottom address, 16K, PROT_NONE) (The entire guard zone) > mprotect(top of red zone address, 12K, PROT_READ|PROT_WRITE) (Reserved + yellow) > mprotect(top of red zone address, 12K, PROT_NONE) (Reserved + yellow) > > While I've committed a workaround for openjdk8, which just rounds down the > fault address, it isn't entirely satisfactory (it's a hack) and I wondered > if anyone had any insight into what may be going on. I've done an analysis > of the sizes and addresses being used and used truss to check the parameters > to the mprotect calls, and everything appears to add up. > > The same problem also occurs under FreeBSD 12.0/i386 and on aarch64, so it > doesn't appear to be either version or platform specific. I've simplified > a little here, but am happy to provide additional details and code > references. Can you provide me with the java class that demonstrates the issue ? What exact environment do I need to reproduce it ? Is amd64 stable/11 openjdk8 good enough ?