From owner-freebsd-hackers@FreeBSD.ORG Mon May 10 17:45:31 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 4B22C1065672; Mon, 10 May 2010 17:45:31 +0000 (UTC) (envelope-from lev@FreeBSD.org) Received: from ftp.translate.ru (ftp.translate.ru [80.249.188.42]) by mx1.freebsd.org (Postfix) with ESMTP id 037628FC1C; Mon, 10 May 2010 17:45:30 +0000 (UTC) Received: from desktop.home.serebryakov.spb.ru (85-142-52-164.well-com.net [85.142.52.164]) (Authenticated sender: lev@serebryakov.spb.ru) by ftp.translate.ru (Postfix) with ESMTPA id 975B013DF46; Mon, 10 May 2010 21:45:29 +0400 (MSD) Date: Mon, 10 May 2010 21:45:21 +0400 From: Lev Serebryakov Organization: FreeBSD Project X-Priority: 3 (Normal) Message-ID: <1638216268.20100510214521@serebryakov.spb.ru> To: Kostik Belousov In-Reply-To: <20100510145817.GO83316@deviant.kiev.zoral.com.ua> References: <1127023465.20100510115708@serebryakov.spb.ru> <20100510145817.GO83316@deviant.kiev.zoral.com.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=windows-1251 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org, Lev Serebryakov Subject: Re: How to get stack bounds of current process? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: lev@FreeBSD.org List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 May 2010 17:45:31 -0000 Hello, Kostik. You wrote 10 =EC=E0=FF 2010 =E3., 18:58:17: >> I'm proting some application from Linux, which discover its stack >> bounds by reading and pasing "/proc/self/maps". FreeBSD have >> "/prov/curproc/map", but I can not find how to determine which record >> is for stack (I've looked into implementation of proc_fs, but it >> doesn't contain any specail processing for process stack). >>=20 >> How could I determine stack bounds of current process on FreeBSD >> 7/8/9? > I think the right question is why the program needs the information at al= l. > Really, the system has no data to answer your question. Which stack are > you asking for ? The stack of main thread, set up by kernel, is very > different from the stack established by the threading library for > newly created thread. What should happen for signal altstacks ? > Also, the threading library clips the main thread stack to match its > size with default stack size (I do think this is unsafe and wrong). It is port of new openjdk7 build. It adds function with this comment in Linux-specific code (BSD port is based on Linux one): // Linux uses a growable mapping for the stack, and if the mapping for // the stack guard pages is not removed when we detach a thread the // stack cannot grow beyond the pages where the stack guard was // mapped. If at some point later in the process the stack expands to // that point, the Bsd kernel cannot expand the stack any further // because the guard pages are in the way, and a segfault occurs. // // However, it's essential not to split the stack region by unmapping // a region (leaving a hole) that's already part of the stack mapping, // so if the stack mapping has already grown beyond the guard pages at // the time we create them, we have to truncate the stack mapping. // So, we need to know the extent of the stack mapping when // create_stack_guard_pages() is called. // Find the bounds of the stack mapping. Return true for success. // // We only need this for stacks that are growable: at the time of // writing thread stacks don't use growable mappings (i.e. those // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this // only applies to the main thread. // If the (growable) stack mapping already extends beyond the point // where we're going to put our guard pages, truncate the mapping at // that point by munmap()ping it. This ensures that when we later // munmap() the guard pages we don't leave a hole in the stack // mapping. Solaris code simple map/unmap needed pages, and Linux port checks stack region and applies special processing for growable stack area... I'm not sure, should BSD port behaves as Linux or as Solaris one. --=20 // Black Lion AKA Lev Serebryakov