From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/3025 Path: news.gmane.org!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: Re: pthread_getattr_np Date: Mon, 1 Apr 2013 01:31:35 +0200 Message-ID: <20130331233135.GK30576@port70.net> References: <20130331173518.GH30576@port70.net> <20130331180717.GI20323@brightrain.aerifal.cx> <20130331205139.GI30576@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="+HP7ph2BbKc20aGI" X-Trace: ger.gmane.org 1364772710 14138 80.91.229.3 (31 Mar 2013 23:31:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 31 Mar 2013 23:31:50 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-3026-gllmg-musl=m.gmane.org@lists.openwall.com Mon Apr 01 01:32:18 2013 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1UMRjR-0006LJ-1J for gllmg-musl@plane.gmane.org; Mon, 01 Apr 2013 01:32:13 +0200 Original-Received: (qmail 17988 invoked by uid 550); 31 Mar 2013 23:31:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 17980 invoked from network); 31 Mar 2013 23:31:47 -0000 Content-Disposition: inline In-Reply-To: <20130331205139.GI30576@port70.net> User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:3025 Archived-At: --+HP7ph2BbKc20aGI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline * Szabolcs Nagy [2013-03-31 22:51:39 +0200]: > 1) parse /proc/self/maps which gives the current [low,high] mapping > and 'prev' the high end of the last mapping below the stack > 2) if we are the main thread check if low <= sp <= high > 3) check rlimit > > lowend = min(max(prev, high-rlimit, high-1G), low) attached a getstack for the main thread --+HP7ph2BbKc20aGI Content-Type: text/x-csrc; charset=us-ascii Content-Disposition: attachment; filename="getstack.c" #include #include #include #ifndef PAGE_SIZE #include #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) #endif /* get the stack [start,end] of the main thread: start is heuristic (smallest possible based on current rlimit and current mappings, but stack size is limited for sanity) end is precise, parsing /proc may fail */ static int getstack(size_t *start, size_t *end) { size_t limit, prevend; struct rlimit r; FILE *f; char buf[PATH_MAX+80], s[8]; int n; f = fopen("/proc/self/maps", "re"); if (!f) return -1; n = 0; while (fgets(buf, sizeof buf, f)) { n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", start, end, s); if (n >= 2) { if (n == 3 && strcmp(s, "[stack]") == 0) break; prevend = *end; } n = 0; } fclose(f); if (n == 0) return -1; limit = 100 << 20; /* 100MB stack limit */ if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit) limit = r.rlim_cur & -PAGE_SIZE; if (limit > *end) limit = *end; if (prevend < *end - limit) prevend = *end - limit; if (*start > prevend) *start = prevend; return 0; } int main() { size_t s,e; if (getstack(&s, &e)) { printf("getstack failed\n"); return -1; } printf("%zx-%zx %zu\n", s, e, e-s); return 0; } --+HP7ph2BbKc20aGI--