lkml.org 
[lkml]   [1999]   [Dec]   [17]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: Recursive malloc crashing Linux. (Well almost)
On Fri, 17 Dec 1999, Giuliano Pochini wrote:

> > # ./killmem
> > Allocating 256 amounts of 4MB == 1024MB
> > Successful
> > Filling block 0
> > Filling block 1
> > killmem: memory violation at pc=0x0200047c, lr=0x4005f7d8 (bad address=0x4065d000, code 1)
> > Bus error
> >
> > Which is kind of not very graceful. On my PC, 128MB ram, ~133MB swap, it
> > gets as far as block 34 (136MB) before causes a complete lockup (not even
> > magic sysrq works) - which is also not very graceful.
>
> It seems to crash when it tries to expand the stack and there is no available
> mem. If you try:
>
> while(1){
> malloc(256K);
> fill;
> }
>
> the program will be killed. But an infinite recurse will crash even if you
> don't malloc anything.

Sorry but this isn't my program you're describing --

My program (killmem.c, attached) doesn't recurse.

It allocates (in a loop) lots of memory in 4MB chunk mmaps. (1GB total)
It then fills each chunk one at a time until it dies.
This demonstrates the ability to allocate far more memory than is
available, and subsequently die when accessing it. This is overcommit,
even when overcommit is turned off.

Allocating memory in a single 1GB chunk fails gracefully, but allocating
in 4MB chunks works until you access it.

- John Ripley.
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>

#define NBUFS 256
#define MMAP_SIZE (4*1048576)

main()
{
int i, j;
char *buf[NBUFS], *p;

printf("Allocating %d amounts of %dMB == %dMB\n",
NBUFS, MMAP_SIZE >> 20, (MMAP_SIZE >> 20) * NBUFS);

for(i=0; i<NBUFS; i++) {
buf[i] = (char *)
mmap(0, 4096*1024, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if(buf[i] == MAP_FAILED) {
printf("Failed for block %d\n", i);
return 1;
}
}

printf("Successful\n");

for(i=0; i<NBUFS; i++) {
printf("Filling block %d\n", i);

p = buf[i];
for(j=MMAP_SIZE; j; j--) *p++ = 0;
}

printf("Done\n");
}
\
 
 \ /
  Last update: 2005-03-22 13:55    [W:0.051 / U:0.312 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site