From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5726 Path: news.gmane.org!not-for-mail From: "Weiming Zhao" Newsgroups: gmane.linux.lib.musl.general Subject: How to setup pre-allocated heap stack space? Date: Tue, 5 Aug 2014 13:26:51 -0700 Message-ID: <00a001cfb0eb$97409930$c5c1cb90$@codeaurora.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_00A1_01CFB0B0.EAE23660" X-Trace: ger.gmane.org 1407270435 19018 80.91.229.3 (5 Aug 2014 20:27:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Aug 2014 20:27:15 +0000 (UTC) To: Original-X-From: musl-return-5731-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 05 22:27:06 2014 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 1XElK6-00028J-1X for gllmg-musl@plane.gmane.org; Tue, 05 Aug 2014 22:27:06 +0200 Original-Received: (qmail 5194 invoked by uid 550); 5 Aug 2014 20:27:04 -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 5185 invoked from network); 5 Aug 2014 20:27:04 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-caf-smtp.dmz.codeaurora.org X-Spam-Level: * X-Spam-Status: No, score=1.2 required=2.0 tests=ALL_TRUSTED,BAYES_40, HTML_MESSAGE,TVD_RCVD_SINGLE autolearn=no version=3.3.1 X-Mailer: Microsoft Outlook 15.0 Thread-Index: Ac+w6ytzMOHlQZaCSoe0I1hPO8MIBA== Content-Language: en-us X-Virus-Scanned: ClamAV using ClamSMTP Xref: news.gmane.org gmane.linux.lib.musl.general:5726 Archived-At: This is a multipart message in MIME format. ------=_NextPart_000_00A1_01CFB0B0.EAE23660 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi, In musl's crt1.c , I want to let libc to use preallocated heap and stack. I can set the SP register in crt1. But how do I specify the base address of heap? So malloc will starts from there. Thanks, Weiming ------=_NextPart_000_00A1_01CFB0B0.EAE23660 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Hi,

 

In = musl’s crt1.c , I want to let libc to use preallocated heap and = stack.

I can set the SP register in = crt1. But how do I specify the base address of heap? So malloc will = starts from there.

 

Thanks,

Weiming

------=_NextPart_000_00A1_01CFB0B0.EAE23660-- From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5729 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: How to setup pre-allocated heap stack space? Date: Tue, 5 Aug 2014 16:47:26 -0400 Message-ID: <20140805204726.GK1674@brightrain.aerifal.cx> References: <00a001cfb0eb$97409930$c5c1cb90$@codeaurora.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1407271667 842 80.91.229.3 (5 Aug 2014 20:47:47 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Aug 2014 20:47:47 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-5734-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 05 22:47:41 2014 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 1XEldz-0004Jm-Of for gllmg-musl@plane.gmane.org; Tue, 05 Aug 2014 22:47:39 +0200 Original-Received: (qmail 18149 invoked by uid 550); 5 Aug 2014 20:47:39 -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 18141 invoked from network); 5 Aug 2014 20:47:38 -0000 Content-Disposition: inline In-Reply-To: <00a001cfb0eb$97409930$c5c1cb90$@codeaurora.org> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:5729 Archived-At: On Tue, Aug 05, 2014 at 01:26:51PM -0700, Weiming Zhao wrote: > Hi, > > In musl's crt1.c , I want to let libc to use preallocated heap and stack. > > I can set the SP register in crt1. But how do I specify the base address of > heap? So malloc will starts from there. Are you using musl pretty much as-is on Linux, or on a custom OS, bare-metal setup, etc.? On Linux the stack is *always* pre-allocated (with a constant size of 128k, with more committed only lazily as needed, and with possible failure resulting in process death) so I don't know why you'd need to do your own stack pre-allocation, but like you said, it's not really hard. For the heap, if you have a custom OS, you can just define the SYS_brk syscall to give a pointer to your pre-allocated heap, but beware that musl will start using mmap if this heap runs out of space. If you're using Linux and you just want a pre-allocated heap to ensure that memory is committed at exec-time, so allocations can't fail later, the way to achieve this from crt1 would be to use the strategy reclaim_gaps() uses in the dynamic linker, which essentially amounts to passing the pointer to free() after setting up some adjacent bookkeeping structures. However I would strongly advise AGAINST this approach unless you're static-linking with a known version of musl. If dynamic linking, you would be encoding a dependency on the internal heap representation into your application binary, and things would horribly break if musl is later upgraded and the representation no longer matches. Rich From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5730 Path: news.gmane.org!not-for-mail From: "Weiming Zhao" Newsgroups: gmane.linux.lib.musl.general Subject: RE: How to setup pre-allocated heap stack space? Date: Tue, 5 Aug 2014 13:59:21 -0700 Message-ID: <00ba01cfb0f0$21992d00$64cb8700$@codeaurora.org> References: <00a001cfb0eb$97409930$c5c1cb90$@codeaurora.org> <20140805204726.GK1674@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1407272382 10861 80.91.229.3 (5 Aug 2014 20:59:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Aug 2014 20:59:42 +0000 (UTC) To: Original-X-From: musl-return-5735-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 05 22:59:36 2014 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 1XElpX-0003LS-Rc for gllmg-musl@plane.gmane.org; Tue, 05 Aug 2014 22:59:35 +0200 Original-Received: (qmail 26102 invoked by uid 550); 5 Aug 2014 20:59:35 -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 26094 invoked from network); 5 Aug 2014 20:59:34 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-caf-smtp.dmz.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=2.0 tests=ALL_TRUSTED,BAYES_00, TVD_RCVD_SINGLE autolearn=no version=3.3.1 In-Reply-To: <20140805204726.GK1674@brightrain.aerifal.cx> X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQJ85JC3XRPbmtirSP3c4N3mlvv07wF1wLCrmlxmRQA= Content-Language: en-us X-Virus-Scanned: ClamAV using ClamSMTP Xref: news.gmane.org gmane.linux.lib.musl.general:5730 Archived-At: Hi Rich, I'm using it on a bare-metal setup. So the heap space some fixed address. So I think I should modify __brk() like: uintptr_t __brk(uintptr_t newbrk) { return newbrk == 0 ? HEAP_BASE : newbrk ; // assume heap is large enough } Is that correct? I don't need to worry about free(), right? Thanks, Weiming -----Original Message----- From: Rich Felker [mailto:dalias@aerifal.cx] On Behalf Of Rich Felker Sent: Tuesday, August 05, 2014 1:47 PM To: musl@lists.openwall.com Subject: Re: [musl] How to setup pre-allocated heap stack space? On Tue, Aug 05, 2014 at 01:26:51PM -0700, Weiming Zhao wrote: > Hi, > > In musl's crt1.c , I want to let libc to use preallocated heap and stack. > > I can set the SP register in crt1. But how do I specify the base > address of heap? So malloc will starts from there. Are you using musl pretty much as-is on Linux, or on a custom OS, bare-metal setup, etc.? On Linux the stack is *always* pre-allocated (with a constant size of 128k, with more committed only lazily as needed, and with possible failure resulting in process death) so I don't know why you'd need to do your own stack pre-allocation, but like you said, it's not really hard. For the heap, if you have a custom OS, you can just define the SYS_brk syscall to give a pointer to your pre-allocated heap, but beware that musl will start using mmap if this heap runs out of space. If you're using Linux and you just want a pre-allocated heap to ensure that memory is committed at exec-time, so allocations can't fail later, the way to achieve this from crt1 would be to use the strategy reclaim_gaps() uses in the dynamic linker, which essentially amounts to passing the pointer to free() after setting up some adjacent bookkeeping structures. However I would strongly advise AGAINST this approach unless you're static-linking with a known version of musl. If dynamic linking, you would be encoding a dependency on the internal heap representation into your application binary, and things would horribly break if musl is later upgraded and the representation no longer matches. Rich From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5732 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: How to setup pre-allocated heap stack space? Date: Tue, 5 Aug 2014 17:04:39 -0400 Message-ID: <20140805210439.GM1674@brightrain.aerifal.cx> References: <00a001cfb0eb$97409930$c5c1cb90$@codeaurora.org> <20140805204726.GK1674@brightrain.aerifal.cx> <00ba01cfb0f0$21992d00$64cb8700$@codeaurora.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1407272699 14126 80.91.229.3 (5 Aug 2014 21:04:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Aug 2014 21:04:59 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-5737-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 05 23:04:53 2014 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 1XElue-00022Q-Kd for gllmg-musl@plane.gmane.org; Tue, 05 Aug 2014 23:04:52 +0200 Original-Received: (qmail 29985 invoked by uid 550); 5 Aug 2014 21:04:52 -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 29977 invoked from network); 5 Aug 2014 21:04:51 -0000 Content-Disposition: inline In-Reply-To: <00ba01cfb0f0$21992d00$64cb8700$@codeaurora.org> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:5732 Archived-At: On Tue, Aug 05, 2014 at 01:59:21PM -0700, Weiming Zhao wrote: > Hi Rich, > > I'm using it on a bare-metal setup. So the heap space some fixed address. > So I think I should modify __brk() like: > uintptr_t __brk(uintptr_t newbrk) { > return newbrk == 0 ? HEAP_BASE : newbrk ; // assume heap is large enough > } > Is that correct? I don't need to worry about free(), right? Well I would at least make sure you don't overflow into other memory, by checking against an upper bound for the heap. But yes, something like the above looks right. Rich From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5734 Path: news.gmane.org!not-for-mail From: "Weiming Zhao" Newsgroups: gmane.linux.lib.musl.general Subject: RE: How to setup pre-allocated heap stack space? Date: Tue, 5 Aug 2014 14:07:16 -0700 Message-ID: <00c001cfb0f1$3c88a9f0$b599fdd0$@codeaurora.org> References: <00a001cfb0eb$97409930$c5c1cb90$@codeaurora.org> <20140805204726.GK1674@brightrain.aerifal.cx> <00ba01cfb0f0$21992d00$64cb8700$@codeaurora.org> <20140805210439.GM1674@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1407272861 16054 80.91.229.3 (5 Aug 2014 21:07:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 5 Aug 2014 21:07:41 +0000 (UTC) To: Original-X-From: musl-return-5739-gllmg-musl=m.gmane.org@lists.openwall.com Tue Aug 05 23:07:31 2014 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 1XElxC-0005VY-3O for gllmg-musl@plane.gmane.org; Tue, 05 Aug 2014 23:07:30 +0200 Original-Received: (qmail 32295 invoked by uid 550); 5 Aug 2014 21:07:29 -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 32287 invoked from network); 5 Aug 2014 21:07:29 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-caf-smtp.dmz.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.7 required=2.0 tests=ALL_TRUSTED,BAYES_00, TVD_RCVD_SINGLE autolearn=no version=3.3.1 In-Reply-To: <20140805210439.GM1674@brightrain.aerifal.cx> X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQJ85JC3XRPbmtirSP3c4N3mlvv07wF1wLCrAo3fNJkB2Orx+Jo5NKGQ Content-Language: en-us X-Virus-Scanned: ClamAV using ClamSMTP Xref: news.gmane.org gmane.linux.lib.musl.general:5734 Archived-At: Yes, I will check the limit of the heap. Just want to check with you if the basic idea is correct. Thanks a lot! -----Original Message----- From: Rich Felker [mailto:dalias@aerifal.cx] On Behalf Of Rich Felker Sent: Tuesday, August 05, 2014 2:05 PM To: musl@lists.openwall.com Subject: Re: [musl] How to setup pre-allocated heap stack space? On Tue, Aug 05, 2014 at 01:59:21PM -0700, Weiming Zhao wrote: > Hi Rich, > > I'm using it on a bare-metal setup. So the heap space some fixed address. > So I think I should modify __brk() like: > uintptr_t __brk(uintptr_t newbrk) { > return newbrk == 0 ? HEAP_BASE : newbrk ; // assume heap is large > enough } Is that correct? I don't need to worry about free(), right? Well I would at least make sure you don't overflow into other memory, by checking against an upper bound for the heap. But yes, something like the above looks right. Rich