简体   繁体   中英

mmap() returns EINVAL

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?)

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.

I would suggest using NULL for the addr argument and giving the implementation the complete freedom to place your mmaped region( cos the address you specify might go awry with respect to the process' address space) until there's a serious reason not to do otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM