Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mmap() returns EINVAL

Tags:

c

mmap

I can't get the mmap function to work. It returns the EINVAL error code.

void* mapped = 
        mmap((void*)(map_addr + slide),
             map_size,
             PROT_WRITE | PROT_READ,
             MAP_PRIVATE | MAP_ANON,
             bprm->file,
             map_offset);

I've checked the documentation for this function on my platform (Darwin) and there doesn't seem to be anything wrong. The man page for mmap presents four cases under which EINVAL would be returned.

 [EINVAL]           MAP_FIXED was specified and the addr argument was not page
                    aligned, or part of the desired address space resides out of the
                    valid address space for a user process.

MAP_FIXED isn't specified so it isn't this.

 [EINVAL]           flags does not include either MAP_PRIVATE or MAP_SHARED.

MAP_PRIVATE is present.

 [EINVAL]           The len argument was negative.

The len (map_size) argument at the time of the call is 8192.

 [EINVAL]           The offset argument was not page-aligned based on the page size as
                    returned by getpagesize(3).

The offset argument (map_offset) is 0 so it must be page aligned. (maybe I'm wrong?)

like image 967
Kristina Brooks Avatar asked Nov 04 '22 02:11

Kristina Brooks


1 Answers

Are you sure about your reading of the first description? It could also be read as describing two disjoint cases:

  1. MAP_FIXED was specified and the addr argument was not page aligned,
  2. or part of the desired address space resides out of the valid address space for a user process.

if read like this, the actual value of the the map_addr + slide expression becomes interesting.

like image 136
unwind Avatar answered Nov 09 '22 12:11

unwind