mprotect() failed: Cannot allocate memory

Yann Droneaud yann@droneaud.fr
Wed Apr 21 08:23:00 GMT 2010


Le mercredi 21 avril 2010 à 01:44 +0200, Paweł Sikora a écrit :
> On Wednesday 21 April 2010 01:17:22 Mike Frysinger wrote:
> > On Tuesday 20 April 2010 19:05:20 Paweł Sikora wrote:
> > > i'm trying to debug an ugly application with ElectricFence.
> > 
> > electricfence does a lot of ugly memory tricks to do its thing, including,
> > but not limited to, overriding memory related symbols.  best to seek help
> > from the electricfence authors.
> 
> so, let's avoid EF and run following test:
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/mman.h>
> 
> void* my_alloc( size_t n )
> {
>         size_t ps = getpagesize();
>         printf( "request for %Zd bytes => ", n );
>         /* alloc PAGE_SIZE + n */
>         char* p = mmap( 0, ps + n, PROT_READ | PROT_WRITE, MAP_SHARED | 
> MAP_ANONYMOUS, -1, 0 );
>         if ( p == MAP_FAILED )
>                 __builtin_abort();
>         /* block guard page */
>         int rc = mprotect( p, ps, PROT_NONE );
>         if ( rc != 0 )
>                 __builtin_abort();
>         char* q = p + ps;
>         printf( "guard page @ %p, allocated region @ %p\n", p, q );
>         return q;
> }
> 
> int main()
> {
>         #define N 100  
>         size_t NN = 4*100*100;
>         size_t kmax = 100;
>         int i;
> 
>         double **bm = (double **)my_alloc( NN * sizeof( double* ) );
>         for( i = 0; i < NN; ++i )
>         {
>                 bm[ i ] = (double*)my_alloc( kmax * sizeof( double ) );
>         }
>         // leak...
>         return 0;
> }
> 
> and the result is...
> 
> (...)
> mmap(NULL, 4896, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0) = 
> 0x7f5fd97df000
> mprotect(0x7f5fd97df000, 4096, PROT_NONE) = -1 ENOMEM (Cannot allocate memory)

Have you checked available memory on your system ? Or user limit ?

You test program is going to allocate 
 79 + 1 pages for bm
 1 + 1 for each double arrays (x 40000)

So in the end your program is allocating 80080 pages, so about
312MBytes.

It not that big for a 64bits system.

Check limits such as 
-d	the maximum size of a process's data segment
-l	the maximum size a process may lock into memory
-m	the maximum resident set size

Regards.

-- 
Yann Droneaud




More information about the Libc-help mailing list