gpt4 book ai didi

c - 使用 mmap 重叠页面 (MAP_FIXED)

转载 作者:IT王子 更新时间:2023-10-28 23:53:48 26 4
gpt4 key购买 nike

由于一些与这个问题无关的模糊原因,我需要求助于使用 MAP_FIXED 以获得接近 libc 的文本部分在内存中的位置的页面。

在阅读 mmap(2)(我一开始就应该这样做)之前,如果我使用 MAP_FIXED 调用 mmap 并且基地址与已映射区域重叠,我预计会出现错误。

然而事实并非如此。例如,这是某些进程的/proc/maps 的一部分

7ffff7299000-7ffff744c000 r-xp 00000000 08:05 654098                     /lib/x86_64-linux-gnu/libc-2.15.so

在进行以下 mmap 调用之后...

  mmap(0x7ffff731b000,
getpagesize(),
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED,
0,
0);

... 变成:

7ffff7299000-7ffff731b000 r-xp 00000000 08:05 654098                     /lib/x86_64-linux-gnu/libc-2.15.so
7ffff731b000-7ffff731c000 rwxp 00000000 00:00 0
7ffff731c000-7ffff744c000 r-xp 00083000 08:05 654098 /lib/x86_64-linux-gnu/libc-2.15.so

这意味着我已经用我自己的页面覆盖了一部分专用于 libc 的虚拟地址空间。显然不是我想要的...

在 mmap(2) 手册的 MAP_FIXED 部分,它明确指出:

If the memory region specified by addr and len overlaps pages of any existing mapping(s), then the overlapped part of the existing mapping(s) will be discarded.

这解释了我所看到的,但我有几个问题:

  1. 有没有办法检测某些东西是否已经映射到某个地址?不访问/proc/maps?
  2. 在查找重叠页面的情况下,是否有办法强制 mmap 失败?

最佳答案

  1. 使用 page = sysconf(SC_PAGE_SIZE) 找出页面大小,然后使用 msync(addr, page, 0) 扫描您希望检查的每个页面大小的 block 。 (使用 (unsigned long)addr % page == 0,即 addr 与页面对齐)。如果它返回 -1errno == ENOMEM,则该页面未映射。

    编辑:正如 fons 在下面评论的那样,mincore(addr,page,&dummy)优于 msync()。 (系统调用的实现在 Linux 内核源代码的 mm/mincore.c 中,C 库通常提供更新 errno 的包装器。当系统调用执行映射时在确保 addr 是页面对齐后立即检查,它在未映射的情况下是最佳的 (ENOMEM)。如果页面已经映射,它会做一些工作,所以如果性能是最重要的,请尽量避免检查您知道已映射的页面。

    您必须针对每个页面单独执行此操作,因为对于大于单个页面的区域,ENOMEM 表示该区域未完全映射;它可能仍被部分映射。映射始终精细到页面大小的单位。

  2. 据我所知,如果该区域已映射或包含已映射的页面,则无法告诉 mmap() 失败。 (这同样适用于 mremap(),因此您不能创建一个映射,然后将其移动到所需的区域。)

    这意味着您面临竞争条件的风险。最好自己执行实际的系统调用,而不是 C 库包装器,以防它们在内部进行内存分配或更改内存映射:

    #define _GNU_SOURCE
    #include <unistd.h>
    #include <sys/syscall.h>

    static size_t page = 0;
    static inline size_t page_size(void)
    {
    if (!page)
    page = (size_t)sysconf(_SC_PAGESIZE);
    return page;
    }


    static inline int raw_msync(void *addr, size_t length, int flags)
    {
    return syscall(SYS_msync, addr, length, flags);
    }

    static inline void *raw_mmap(void *addr, size_t length, int prot, int flags)
    {
    return (void *)syscall(SYS_mmap, addr, length, prot, flags, -1, (off_t)0);
    }

但是,我怀疑无论您尝试做什么,您最终都需要解析 /proc/self/maps

  • 我建议完全避免使用标准 I/O stdio.h(因为各种操作会动态分配内存,从而改变映射),而是使用较低级别的 unistd.h 接口(interface),这不太可能影响映射。这是一组简单粗暴的函数,您可以使用它们来找出每个映射区域和该区域中启用的保护(并丢弃其他信息)。在实践中,它使用大约 1 KB 的代码,少于堆栈中的代码,因此即使在有限的架构(例如,嵌入式设备)上也非常有用。

    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>

    #ifndef INPUT_BUFFER
    #define INPUT_BUFFER 512
    #endif /* INPUT_BUFFER */

    #ifndef INPUT_EOF
    #define INPUT_EOF -256
    #endif /* INPUT_EOF */

    #define PERM_PRIVATE 16
    #define PERM_SHARED 8
    #define PERM_READ 4
    #define PERM_WRITE 2
    #define PERM_EXEC 1

    typedef struct {
    int descriptor;
    int status;
    unsigned char *next;
    unsigned char *ends;
    unsigned char buffer[INPUT_BUFFER + 16];
    } input_buffer;

    /* Refill input buffer. Returns the number of new bytes.
    * Sets status to ENODATA at EOF.
    */
    static size_t input_refill(input_buffer *const input)
    {
    ssize_t n;

    if (input->status)
    return (size_t)0;

    if (input->next > input->buffer) {
    if (input->ends > input->next) {
    memmove(input->buffer, input->next,
    (size_t)(input->ends - input->next));
    input->ends = input->buffer + (size_t)(input->ends - input->next);
    input->next = input->buffer;
    } else {
    input->ends = input->buffer;
    input->next = input->buffer;
    }
    }

    do {
    n = read(input->descriptor, input->ends,
    INPUT_BUFFER - (size_t)(input->ends - input->buffer));
    } while (n == (ssize_t)-1 && errno == EINTR);
    if (n > (ssize_t)0) {
    input->ends += n;
    return (size_t)n;

    } else
    if (n == (ssize_t)0) {
    input->status = ENODATA;
    return (size_t)0;
    }

    if (n == (ssize_t)-1)
    input->status = errno;
    else
    input->status = EIO;

    return (size_t)0;
    }

    /* Low-lever getchar() equivalent.
    */
    static inline int input_next(input_buffer *const input)
    {
    if (input->next < input->ends)
    return *(input->next++);
    else
    if (input_refill(input) > 0)
    return *(input->next++);
    else
    return INPUT_EOF;
    }

    /* Low-level ungetc() equivalent.
    */
    static inline int input_back(input_buffer *const input, const int c)
    {
    if (c < 0 || c > 255)
    return INPUT_EOF;
    else
    if (input->next > input->buffer)
    return *(--input->next) = c;
    else
    if (input->ends >= input->buffer + sizeof input->buffer)
    return INPUT_EOF;

    memmove(input->next + 1, input->next, (size_t)(input->ends - input->next));
    input->ends++;
    return *(input->next) = c;
    }

    /* Low-level fopen() equivalent.
    */
    static int input_open(input_buffer *const input, const char *const filename)
    {
    if (!input)
    return errno = EINVAL;

    input->descriptor = -1;
    input->status = 0;
    input->next = input->buffer;
    input->ends = input->buffer;

    if (!filename || !*filename)
    return errno = input->status = EINVAL;

    do {
    input->descriptor = open(filename, O_RDONLY | O_NOCTTY);
    } while (input->descriptor == -1 && errno == EINTR);
    if (input->descriptor == -1)
    return input->status = errno;

    return 0;
    }

    /* Low-level fclose() equivalent.
    */
    static int input_close(input_buffer *const input)
    {
    int result;

    if (!input)
    return errno = EINVAL;

    /* EOF is not an error; we use ENODATA for that. */
    if (input->status == ENODATA)
    input->status = 0;

    if (input->descriptor != -1) {
    do {
    result = close(input->descriptor);
    } while (result == -1 && errno == EINTR);
    if (result == -1 && !input->status)
    input->status = errno;
    }

    input->descriptor = -1;
    input->next = input->buffer;
    input->ends = input->buffer;

    return errno = input->status;
    }

    /* Read /proc/self/maps, and fill in the arrays corresponding to the fields.
    * The function will return the number of mappings, even if not all are saved.
    */
    size_t read_maps(size_t const n,
    void **const ptr, size_t *const len,
    unsigned char *const mode)
    {
    input_buffer input;
    size_t i = 0;
    unsigned long curr_start, curr_end;
    unsigned char curr_mode;
    int c;

    errno = 0;

    if (input_open(&input, "/proc/self/maps"))
    return (size_t)0; /* errno already set. */

    c = input_next(&input);
    while (c >= 0) {

    /* Skip leading controls and whitespace */
    while (c >= 0 && c <= 32)
    c = input_next(&input);

    /* EOF? */
    if (c < 0)
    break;

    curr_start = 0UL;
    curr_end = 0UL;
    curr_mode = 0U;

    /* Start of address range. */
    while (1)
    if (c >= '0' && c <= '9') {
    curr_start = (16UL * curr_start) + c - '0';
    c = input_next(&input);
    } else
    if (c >= 'A' && c <= 'F') {
    curr_start = (16UL * curr_start) + c - 'A' + 10;
    c = input_next(&input);
    } else
    if (c >= 'a' && c <= 'f') {
    curr_start = (16UL * curr_start) + c - 'a' + 10;
    c = input_next(&input);
    } else
    break;
    if (c == '-')
    c = input_next(&input);
    else {
    errno = EIO;
    return (size_t)0;
    }

    /* End of address range. */
    while (1)
    if (c >= '0' && c <= '9') {
    curr_end = (16UL * curr_end) + c - '0';
    c = input_next(&input);
    } else
    if (c >= 'A' && c <= 'F') {
    curr_end = (16UL * curr_end) + c - 'A' + 10;
    c = input_next(&input);
    } else
    if (c >= 'a' && c <= 'f') {
    curr_end = (16UL * curr_end) + c - 'a' + 10;
    c = input_next(&input);
    } else
    break;
    if (c == ' ')
    c = input_next(&input);
    else {
    errno = EIO;
    return (size_t)0;
    }

    /* Permissions. */
    while (1)
    if (c == 'r') {
    curr_mode |= PERM_READ;
    c = input_next(&input);
    } else
    if (c == 'w') {
    curr_mode |= PERM_WRITE;
    c = input_next(&input);
    } else
    if (c == 'x') {
    curr_mode |= PERM_EXEC;
    c = input_next(&input);
    } else
    if (c == 's') {
    curr_mode |= PERM_SHARED;
    c = input_next(&input);
    } else
    if (c == 'p') {
    curr_mode |= PERM_PRIVATE;
    c = input_next(&input);
    } else
    if (c == '-') {
    c = input_next(&input);
    } else
    break;
    if (c == ' ')
    c = input_next(&input);
    else {
    errno = EIO;
    return (size_t)0;
    }

    /* Skip the rest of the line. */
    while (c >= 0 && c != '\n')
    c = input_next(&input);

    /* Add to arrays, if possible. */
    if (i < n) {
    if (ptr) ptr[i] = (void *)curr_start;
    if (len) len[i] = (size_t)(curr_end - curr_start);
    if (mode) mode[i] = curr_mode;
    }
    i++;
    }

    if (input_close(&input))
    return (size_t)0; /* errno already set. */

    errno = 0;
    return i;
    }

    read_maps() 函数最多读取n 个区域,起始地址为void *ptr数组,len 数组的长度,mode 数组的权限,返回 map 的总数(可能大于 n),如果发生错误,则设置 errno 为零。

    很可能对上面的低级 I/O 使用系统调用,这样您就不会使用任何 C 库功能,但我认为完全没有必要。 (据我所知,C 库在实际系统调用周围使用非常简单的包装器。)

我希望你觉得这很有用。

关于c - 使用 mmap 重叠页面 (MAP_FIXED),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com