[alt.sources] Paranoid malloc

ss7m+@andrew.cmu.edu (Steven M. Stadnicki) (06/13/91)

Does anyone know where any librarys of this nature can be found?  I've got a
lurking memory leak somewhere in the code I'm working with, and was hoping for
something that'll match mallocs and frees and tell me what I'm missing...

                                 Steven Stadnicki
                                 ss7m@andrew.cmu.edu

jtkohl@MIT.EDU (John T Kohl) (06/13/91)

I highly recommend the mprof package; a copy is on decwrl.dec.com
somewhere.

If you're using a DEC RISC box, I can send you some patches to improve
some problems it used to have with static functions with missing symbol
table entries.
--
John Kohl <jtkohl@MIT.EDU>
Digital Equipment Corporation/Project Athena
(The above opinions are MINE.  Don't put my words in somebody else's mouth!)
___This signature printed on recycled bits___ [not original; heard 2nd hand]

vandys@sequent.com (Andrew Valencia) (06/13/91)

ss7m+@andrew.cmu.edu (Steven M. Stadnicki) writes:

>Does anyone know where any librarys of this nature can be found?  I've got a
>lurking memory leak somewhere in the code I'm working with, and was hoping for
>something that'll match mallocs and frees and tell me what I'm missing...

Well, I don't have quite that.  But here's my malloc/free package for
catching abuse of allocated and use of free'ed or realloc'd memory.  Use
at your own risk!  It uses mmap() and munmap(), so you'll need a later
vintage System V or BSD.

						Andy Valencia
						vandys@sequent.com

/*
 * A brute-force malloc() interface which attempts to back up each
 * allocated piece of data against an invalid page, so that references
 * off the end cause a segmentation violation.
 */
extern char *sbrk();

static unsigned long pagesize;

char *
malloc(size)
	unsigned size;
{
	static int setup = 0;
	unsigned long l;
	char *p;

	/*
	 * Adjust up so we can slip a "size" word up front.  Also pad by an
	 * extra longword because the C library expects strings to not fall
	 * into an invalid page within one word from their end.
	 */
	size += sizeof(unsigned) + sizeof(long);

	/*
	 * Round size to longword boundary
	 */
	if (size & (sizeof(long)-1)) {
		size += (sizeof(long) - (size & (sizeof(long)-1)));
	}

	/*
	 * Get some overhead over with once
	 */
	if (!setup) {
		pagesize = getpagesize();
		setup = 1;
	}

	/*
	 * Make sure break is on a page boundry
	 */
	l = (unsigned long)sbrk(0);
	l &= (pagesize-1);
	l = pagesize - l;
	(void)sbrk((int)l);

	/*
	 * Now allocate one page beyond size needed to hold data
	 */
	l = (size + (pagesize-1)) & ~(pagesize-1);
	l = pagesize * (l/pagesize + 1);
	p = sbrk((int)l);

	/*
	 * Out of memory--foo
	 */
	if (p == 0)
		return(p);

	/*
	 * Unvirtualize last page, return pointer to data backed
	 * up against top of remaining memory.
	 */
	p += l;
	(void) munmap(p - pagesize, pagesize);

	/*
	 * Adjust data to back up against the end of the memory
	 */
	p -= (pagesize + size);
	*(unsigned *)p = size;
	return (p+sizeof(unsigned));
}

/*
 * Free memory.  Unvirtualize and thus catch stale-memory cheaters.
 */
void
free(ptr)
	char *ptr;
{
	unsigned long l;
	int npages;

	/*
	 * Figure out how big a chunk we're done with
	 */
	ptr -= sizeof(unsigned);
	l = *(unsigned *)ptr;
	l = (l + (pagesize-1)) & ~(pagesize-1);
	npages = l/pagesize;

	/*
	 * Get base of memory, unmap it
	 */
	l = (unsigned long)ptr;
	l &= ~(pagesize-1);
	(void) munmap(l, npages*pagesize);
}

char *
realloc(ptr, size)
	char *ptr;
	unsigned size;
{
	register char *p;
	unsigned osize;

	if ((p = malloc(size)) == 0)
		return(p);
	osize = *(unsigned *)(ptr - sizeof(unsigned));
	memcpy(p, ptr, osize);
	free(ptr);
	return(p);
}

ehrlich@cs.psu.edu (Dan Ehrlich) (06/15/91)

In article <wcJfUZu00WB40_POJc@andrew.cmu.edu> ss7m+@andrew.cmu.edu (Steven M. Stadnicki) writes:

SMS> Does anyone know where any librarys of this nature can be found?  I've got a
SMS> lurking memory leak somewhere in the code I'm working with, and was hoping for
SMS> something that'll match mallocs and frees and tell me what I'm missing...

SMS>                                  Steven Stadnicki
SMS>                                  ss7m@andrew.cmu.edu

There is a debugging malloc that come with ZMailer 2.1.  You can find it
in the Zmailer distribution on ftp.cs.toronto.edu in pub/zmailer.tar.Z.

>psuvax1:6> ftp ftp.cs.toronto.edu
>Connected to ftp.cs.toronto.edu.
>220 relay.cs.toronto.edu FTP server (Version 5.80 Fri Feb 1 17:12:21 EST 1991) ready.
>331 Please give your email address as password.
>230 Guest login ok, access restrictions apply.
>ftp> cd pub
>250 CWD command successful.
>ftp> dir *zmail*
>200 PORT command successful.
>150 Opening ASCII mode data connection for /bin/ls.
>-rw-r--r--  1 917      10           1650 Jun  4 21:39 zmailer.Patch01
>-rw-r--r--  1 7        10           2227 Jun  5 20:27 zmailer.Patch02
>-rw-r--r--  1 0        10           7397 Jun  3 16:51 zmailer.README
>-rw-r--r--  1 0        0         1162209 Jun  5 20:15 zmailer.tar.Z
>226 Transfer complete.
>remote: *zmail*
>281 bytes received in 0.01 seconds (27 Kbytes/s)
>ftp> quit
>221 Goodbye.

--
Dan Ehrlich - Sr. Systems Programmer - Penn State Computer Science
<ehrlich@cs.psu.edu>/Voice: +1 814 863 1142/FAX: +1 814 865 3176