From owner-freebsd-stable@FreeBSD.ORG Thu Jul 24 08:15:47 2003 Return-Path: Delivered-To: freebsd-stable@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B14DF37B401 for ; Thu, 24 Jul 2003 08:15:47 -0700 (PDT) Received: from pop018.verizon.net (pop018pub.verizon.net [206.46.170.212]) by mx1.FreeBSD.org (Postfix) with ESMTP id D4F5543FDD for ; Thu, 24 Jul 2003 08:15:46 -0700 (PDT) (envelope-from cswiger@mac.com) Received: from mac.com ([141.149.47.46]) by pop018.verizon.net (InterMail vM.5.01.05.33 201-253-122-126-133-20030313) with ESMTP id <20030724151546.PGIU11703.pop018.verizon.net@mac.com> for ; Thu, 24 Jul 2003 10:15:46 -0500 Message-ID: <3F1FF81F.5050701@mac.com> Date: Thu, 24 Jul 2003 11:15:43 -0400 From: Chuck Swiger Organization: The Courts of Chaos User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: freebsd-stable@freebsd.org References: <20030723173427.GA72876@vmunix.com> <20030723173427.GA72876@vmunix.com> <5.2.0.9.0.20030723234250.052821e8@192.168.0.12> <20030724070936.GA16762@rot13.obsecurity.org> In-Reply-To: <20030724070936.GA16762@rot13.obsecurity.org> X-Enigmail-Version: 0.76.1.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Authentication-Info: Submitted using SMTP AUTH at pop018.verizon.net from [141.149.47.46] at Thu, 24 Jul 2003 10:15:45 -0500 Subject: Re: malloc does not return null when out of memory X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Jul 2003 15:15:48 -0000 Kris Kennaway wrote: > On Wed, Jul 23, 2003 at 11:44:11PM -0400, Mike Tancsa wrote: [ ... ] >>> Ah, the annual "memory overcommit" thread. I thought we were overdue >>> for one. >> >> But why does the man page for malloc (3) say, >> >> If malloc() fails, a NULL pointer is returned. > > Words fail me. Don't worry about it; you've still got sarcasm to fall back on. :-) I don't think the following is a particularly good idea, as the existing prezero ('Z') or junk ('J') options will also serve to reference memory and prevent the "memory overcommit issue", but: 22-sec# diff -du malloc.c_old malloc.c --- malloc.c_old Thu Jul 24 10:36:43 2003 +++ malloc.c Thu Jul 24 10:49:41 2003 @@ -229,6 +229,9 @@ /* junk fill ? */ static int malloc_junk; +/* write a single byte per page to disable overcommit behavior */ +static int malloc_overcommit; + #ifdef HAS_UTRACE /* utrace ? */ @@ -418,6 +421,8 @@ case 'R': malloc_realloc = 1; break; case 'j': malloc_junk = 0; break; case 'J': malloc_junk = 1; break; + case 'o': malloc_overcommit = 0; break + case 'O': malloc_overcommit = 1; break #ifdef HAS_UTRACE case 'u': malloc_utrace = 0; break; case 'U': malloc_utrace = 1; break; @@ -705,6 +710,7 @@ imalloc(size_t size) { void *result; + int stride; if (suicide) abort(); @@ -716,8 +722,13 @@ else result = malloc_pages(size); - if (malloc_zero && result) - memset(result, 0, size); + if (result) { + if (malloc_zero) + memset(result, 0, size); + else if (malloc_overcommit) + for (stride = 0; stride <= size; stride += malloc_pagesize) + ((char *)result)[stride] = SOME_JUNK; + } return result; } -- -Chuck