You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Jeff Trawick <tr...@bellsouth.net> on 2001/02/01 21:09:10 UTC

debugging heap corruption on FreeBSD

A couple of the types of problems encountered last night running
apache 2.0 on apache.org seem to be related to heap corruption.

Do the FreeBSD folks have any suggestions for debugging this?  Link
with electric fence (not sure if it is installed)?  Something else?

-- 
Jeff Trawick | trawickj@bellsouth.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: debugging heap corruption on FreeBSD

Posted by Greg Ames <gr...@raleigh.ibm.com>.
Bill Stoddard wrote:
> 
> I neglected to account for calls to calloc() in my last analysis. Here is the
> analysis of HEAD (this AM):
> Requesting 500 byte file, not cached. AP_MIN_SENDFILE_BYTES set to 250.
> 
> malloc() - Total of 17 per request
>     apr_bucket_make_pool() - 1
>     apr_bucket_make_file()  - 1
>     apr_bucket_make_heap() - 1
>     apr_bucket_make_shared() - 3
>     socket_read() - 1
>     apr_bucket_copy_shared() - 10
> 
> calloc() - Total of 6 per request
>     apr_bucket_create_eos() - 1
>     apr_bucket_create_flush() - 1
>     apr_bucket_create_file() - 1
>     apr_bucket_create_pool() - 1
>     apr_bucket_create_socket() - 2
> 
> free() - Total 23 per requst
>     file_destroy() - 1
>     pool_destroy() - 1
>     ap_getline() - 5
>     apr_brigade_cleanup() - 6
>     heap_destroy() - 2
>     apr_bucket_destroy_shared() -  8
> 
> Bill

hmmmm...so we don't have a straightforward "double free()" bug hanging
out there, at least not when serving this file.

In light of that, I will rebuild on apache.org, bring it up on port
8092, beat it up some, and turn it over to Brian if it looks good. 
Hopefully, one of the patches committed late last week has taken care of
the heap corruption problems.  If not, we'll know soon.

Greg

Re: debugging heap corruption on FreeBSD

Posted by Cliff Woolley <cl...@yahoo.com>.
--- Greg Ames <gr...@raleigh.ibm.com> wrote:
> Jeff also committed a patch that fixed seg faults, which is what most of
> our core dumps were for.  It could be that every once in a while the
> bogus pointer pointed to something in the heap that was still valid as
> far as the virtual memory manager was concerned, and clobbered it.  So
> in other words, we could have two different syptoms for the same
> underlying bug. 

Excellent point.  Crossing my fingers here, too...

--Cliff


__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by Greg Ames <gr...@raleigh.ibm.com>.
Cliff Woolley wrote:
> 
> --- Bill Stoddard <bi...@wstoddard.com> wrote:
> > I neglected to account for calls to calloc() in my last analysis. Here is the
> > analysis of HEAD (this AM):
> 
> > malloc() - Total of 17 per request
> > calloc() - Total of 6 per request
> > free() - Total 23 per requst
> 
> Ahh, yes, of course.  And they match up now.  That's good to know.  =-)
> 
> So now the question is this: after the file_destroy patch, are we still seeing heap
> corruption?  My guess is yes.  But confirmation would be good.

Jeff also committed a patch that fixed seg faults, which is what most of
our core dumps were for.  It could be that every once in a while the
bogus pointer pointed to something in the heap that was still valid as
far as the virtual memory manager was concerned, and clobbered it.  So
in other words, we could have two different syptoms for the same
underlying bug. 

Greg "crossing my fingers" Ames

Re: debugging heap corruption on FreeBSD

Posted by Cliff Woolley <cl...@yahoo.com>.
--- Bill Stoddard <bi...@wstoddard.com> wrote:
> I neglected to account for calls to calloc() in my last analysis. Here is the
> analysis of HEAD (this AM):

> malloc() - Total of 17 per request
> calloc() - Total of 6 per request
> free() - Total 23 per requst

Ahh, yes, of course.  And they match up now.  That's good to know.  =-)

So now the question is this: after the file_destroy patch, are we still seeing heap
corruption?  My guess is yes.  But confirmation would be good.

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by Bill Stoddard <bi...@wstoddard.com>.
I neglected to account for calls to calloc() in my last analysis. Here is the
analysis of HEAD (this AM):
Requesting 500 byte file, not cached. AP_MIN_SENDFILE_BYTES set to 250.

malloc() - Total of 17 per request
    apr_bucket_make_pool() - 1
    apr_bucket_make_file()  - 1
    apr_bucket_make_heap() - 1
    apr_bucket_make_shared() - 3
    socket_read() - 1
    apr_bucket_copy_shared() - 10

calloc() - Total of 6 per request
    apr_bucket_create_eos() - 1
    apr_bucket_create_flush() - 1
    apr_bucket_create_file() - 1
    apr_bucket_create_pool() - 1
    apr_bucket_create_socket() - 2

free() - Total 23 per requst
    file_destroy() - 1
    pool_destroy() - 1
    ap_getline() - 5
    apr_brigade_cleanup() - 6
    heap_destroy() - 2
    apr_bucket_destroy_shared() -  8

Bill


>
> Here is the break out.
>
> malloc() - Total 17 per req (using ab w/o keep alive to test with)
>     apr_bucket_make_pool() - 1
>     apr_bucket_make_file()  - 1
>     apr_bucket_make_heap() - 1
>     apr_bucket_make_shared() - 3
>     socket_read() - 1
>     apr_bucket_copy_shared() - 10
>
> free() - Total 21 per requst
>     pool_destroy() - 1
>     ap_getline - 5
>     apr_brigade_cleanup - 6
>     apr_bucket_destroy_shared - 7
>     heap_destroy - 2
>
> I suspect there is a cleanup not being deregistered. Haven't been able to
pin
> point ityet.
>
> Bill
>


Re: debugging heap corruption on FreeBSD

Posted by Cliff Woolley <cl...@yahoo.com>.
--- Bill Stoddard <bi...@wstoddard.com> wrote:
> I suspect there is a cleanup not being deregistered. Haven't been able to pin
> point ityet.

Forgive a stupid question...

(jcw5q)~/httpd-2.0/srclib/apr-util/buckets# grep register_cleanup *.c
apr_brigade.c:    apr_register_cleanup(b->p, b, apr_brigade_cleanup,
apr_brigade_cleanup);
apr_buckets_pool.c:    apr_register_cleanup(h->p, b->data, pool_bucket_cleanup,
apr_null_cleanup);

Why does apr_brigade_create's call to apr_register_cleanup register apr_brigade_cleanup
for both regular cleanup and child cleanup, while the pool bucket's cleanup function is
only registered for regular cleanup?

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by Cliff Woolley <cl...@yahoo.com>.
--- rbb@covalent.net wrote:
> It should actually cause the corruption.  Think of this.  You split a file
> bucket, that is two one malloc, but whenever we destroy the file, we call
> free.

Not quite, I think.

after create file:
   apr_bucket -> apr_bucket_shared -> apr_bucket_file(refcount=1)

after split file:
   apr_bucket -> apr_bucket_shared -> apr_bucket_file(refcount=2)
   apr_bucket -> apr_bucket_shared ---^

after destroy file:  [free(apr_bucket_shared*),free(apr_bucket*) leaves:]
   apr_bucket -> apr_bucket_shared -> apr_bucket_file(refcount=2)  [wrong refcount!]

after destroy file:  [free(apr_bucket_shared*),free(apr_bucket*) leaves:]
   apr_bucket_file(refcount=2)  [lost pointer to file bucket, refcount even more wrong]


The bucket destruction macro automatically frees the apr_bucket, and the b->data of that
bucket is freed by the type-specific destruction function.  Using free() for the
type-specific destruction function for file buckets frees b->data, sure, but b->data
points to an apr_bucket_shared, not the apr_bucket_file.  So after the two destructs are
called, the apr_bucket's and the apr_bucket_shared's are gone, but the apr_bucket_file
remains.

No corruption, just a memory leak.

Speaking of which--should file_destroy() close the file, or should we just leave that to
the pool cleanup like we do with my patch?

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by rb...@covalent.net.
> Patch forthcoming to fix that little bug.  But AFAICT, all the bug does is cause a memory
> leak... not the corruption we're seeing.  So it's back to the detective work on that
> part.

It should actually cause the corruption.  Think of this.  You split a file
bucket, that is two one malloc, but whenever we destroy the file, we call
free.

Ryan

_______________________________________________________________________________
Ryan Bloom                        	rbb@apache.org
406 29th St.
San Francisco, CA 94131
-------------------------------------------------------------------------------


[PATCH] memory leak in file bucket destruction

Posted by Cliff Woolley <cl...@yahoo.com>.
> --- rbb@covalent.net wrote:
> > You are correct, it should call destroy_shared.  This was broken when I
> > added native copy and split to files, and I didn't think of the free
> > function.

This little patch simultaneously removes file_split() (because all it did was duplicate
the effort of apr_bucket_split_shared()) and add file_destroy(), which is needed now that
the file buckets are refcounted.  This should fix a memory leak, but as I said in my last
post, I can't see any way that it would help with the heap corruption problem.  That
one's still up in the air.

--Cliff


Index: apr_buckets_file.c
===================================================================
RCS file: /home/cvspublic/apr-util/buckets/apr_buckets_file.c,v
retrieving revision 1.25
diff -u -r1.25 apr_buckets_file.c
--- apr_buckets_file.c	2001/01/24 08:26:19	1.25
+++ apr_buckets_file.c	2001/02/01 22:08:34
@@ -164,21 +164,15 @@
     return APR_SUCCESS;
 }
 
-static apr_status_t file_split(apr_bucket *e, apr_off_t offset)
+static void file_destroy(void *data)
 {
-    apr_bucket *b;
-    apr_bucket_shared *s;
-    apr_bucket_shared *temp = e->data;
     apr_bucket_file *f;
 
-    apr_bucket_split_shared(e, offset);
-    b = APR_BUCKET_NEXT(e);
-
-    s = b->data;
-    f = s->data;
-    s->start = offset + temp->start;
-
-    return APR_SUCCESS;
+    f = apr_bucket_destroy_shared(data);
+    if (f == NULL) {
+        return;
+    }
+    free(f);
 }
 
 APU_DECLARE(apr_bucket *) apr_bucket_make_file(apr_bucket *b, apr_file_t *fd,
@@ -211,9 +205,9 @@
 
 APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_file = {
     "FILE", 5,
-    free,
+    file_destroy,
     file_read,
     apr_bucket_setaside_notimpl,
-    file_split,
+    apr_bucket_split_shared,
     apr_bucket_copy_shared
 };

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by Cliff Woolley <cl...@yahoo.com>.
--- rbb@covalent.net wrote:
> You are correct, it should call destroy_shared.  This was broken when I
> added native copy and split to files, and I didn't think of the free
> function.

Patch forthcoming to fix that little bug.  But AFAICT, all the bug does is cause a memory
leak... not the corruption we're seeing.  So it's back to the detective work on that
part.

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by rb...@covalent.net.
> I'm looking into a potential problem (which may well be related) in the file buckets. 
> For some strange reason, the file destruction method is just defined as free(), when it
> ought to go through and call apr_bucket_destroy_shared to decrement the reference count
> and so on.  There may be other things in there as well... I'm going through and
> whiteboarding the file bucket type just to make sure I catch all the details.  Update in
> 15-30 minutes.

You are correct, it should call destroy_shared.  This was broken when I
added native copy and split to files, and I didn't think of the free
function.

Ryan

_______________________________________________________________________________
Ryan Bloom                        	rbb@apache.org
406 29th St.
San Francisco, CA 94131
-------------------------------------------------------------------------------


Re: debugging heap corruption on FreeBSD

Posted by Cliff Woolley <cl...@yahoo.com>.
--- Bill Stoddard <bi...@wstoddard.com> wrote:
> malloc() - Total 17 per req (using ab w/o keep alive to test with)
>     apr_bucket_make_pool() - 1
>     apr_bucket_make_file()  - 1
>     apr_bucket_make_heap() - 1
>     apr_bucket_make_shared() - 3
>     socket_read() - 1
>     apr_bucket_copy_shared() - 10
> 
> free() - Total 21 per requst
>     pool_destroy() - 1
>     ap_getline - 5
>     apr_brigade_cleanup - 6
>     apr_bucket_destroy_shared - 7
>     heap_destroy - 2
> 
> I suspect there is a cleanup not being deregistered. Haven't been able to pin
> point ityet.

I'm looking into a potential problem (which may well be related) in the file buckets. 
For some strange reason, the file destruction method is just defined as free(), when it
ought to go through and call apr_bucket_destroy_shared to decrement the reference count
and so on.  There may be other things in there as well... I'm going through and
whiteboarding the file bucket type just to make sure I catch all the details.  Update in
15-30 minutes.

--Cliff

__________________________________________________
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Re: debugging heap corruption on FreeBSD

Posted by Bill Stoddard <bi...@wstoddard.com>.
> "Bill Stoddard" <bi...@wstoddard.com> writes:
>
> > FWIW...
> > Playing with HiProf on Windows and I am seeing 17 calls to malloc() and 21
> > calls to free() per request. This seems to be playing havoc with all the
> > filters as the 'self time' spent in byterange, contentlength and header
> > filters are way higher than reasonable. Just got hiprof working but I have
> > only maybe 30 min to look at the results today.
>
> Heck, if it is that blatant then I shouldn't worry about trying to
> reproduce on apache.org :)
>

Here is the break out.

malloc() - Total 17 per req (using ab w/o keep alive to test with)
    apr_bucket_make_pool() - 1
    apr_bucket_make_file()  - 1
    apr_bucket_make_heap() - 1
    apr_bucket_make_shared() - 3
    socket_read() - 1
    apr_bucket_copy_shared() - 10

free() - Total 21 per requst
    pool_destroy() - 1
    ap_getline - 5
    apr_brigade_cleanup - 6
    apr_bucket_destroy_shared - 7
    heap_destroy - 2

I suspect there is a cleanup not being deregistered. Haven't been able to pin
point ityet.

Bill


Re: debugging heap corruption on FreeBSD

Posted by Jeff Trawick <tr...@bellsouth.net>.
"Bill Stoddard" <bi...@wstoddard.com> writes:

> FWIW...
> Playing with HiProf on Windows and I am seeing 17 calls to malloc() and 21
> calls to free() per request. This seems to be playing havoc with all the
> filters as the 'self time' spent in byterange, contentlength and header
> filters are way higher than reasonable. Just got hiprof working but I have
> only maybe 30 min to look at the results today.

Heck, if it is that blatant then I shouldn't worry about trying to
reproduce on apache.org :)

-- 
Jeff Trawick | trawickj@bellsouth.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Re: debugging heap corruption on FreeBSD

Posted by Bill Stoddard <bi...@wstoddard.com>.
But this may just be natural pool growth that happens early in the performance
run (storage allocated for pools but not freed until the process is taken
down). Investigating...
Bill

> FWIW...
> Playing with HiProf on Windows and I am seeing 17 calls to malloc() and 21
> calls to free() per request. This seems to be playing havoc with all the
> filters as the 'self time' spent in byterange, contentlength and header
> filters are way higher than reasonable. Just got hiprof working but I have
> only maybe 30 min to look at the results today.
>
> Bill
>
> ----- Original Message -----
> From: Jeff Trawick <tr...@bellsouth.net>
> To: <ne...@apache.org>
> Sent: Thursday, February 01, 2001 3:09 PM
> Subject: debugging heap corruption on FreeBSD
>
>
> > A couple of the types of problems encountered last night running
> > apache 2.0 on apache.org seem to be related to heap corruption.
> >
> > Do the FreeBSD folks have any suggestions for debugging this?  Link
> > with electric fence (not sure if it is installed)?  Something else?
> >
> > --
> > Jeff Trawick | trawickj@bellsouth.net | PGP public key at web site:
> >        http://www.geocities.com/SiliconValley/Park/9289/
> >              Born in Roswell... married an alien...
> >
>


Re: debugging heap corruption on FreeBSD

Posted by Bill Stoddard <bi...@wstoddard.com>.
FWIW...
Playing with HiProf on Windows and I am seeing 17 calls to malloc() and 21
calls to free() per request. This seems to be playing havoc with all the
filters as the 'self time' spent in byterange, contentlength and header
filters are way higher than reasonable. Just got hiprof working but I have
only maybe 30 min to look at the results today.

Bill

----- Original Message -----
From: Jeff Trawick <tr...@bellsouth.net>
To: <ne...@apache.org>
Sent: Thursday, February 01, 2001 3:09 PM
Subject: debugging heap corruption on FreeBSD


> A couple of the types of problems encountered last night running
> apache 2.0 on apache.org seem to be related to heap corruption.
>
> Do the FreeBSD folks have any suggestions for debugging this?  Link
> with electric fence (not sure if it is installed)?  Something else?
>
> --
> Jeff Trawick | trawickj@bellsouth.net | PGP public key at web site:
>        http://www.geocities.com/SiliconValley/Park/9289/
>              Born in Roswell... married an alien...
>