mmap() returns EINVAL

杀马特。学长 韩版系。学妹 提交于 2019-12-10 13:56:56

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10088962/mmap-returns-einval

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!