Skip to content

close fd before calling munmap will cause the write failure #15140

Open

Description

@Tringbtb

When using emcc to compile wasm, I try to use mmap to write the file, but if you close fd before calling munmap, the written content will be lost.

Here is the example.

int len = string.size();
int fd = open("test-shared.txt", O_RDWR | O_CREAT, 00777);
lseek(fd, len - 1, SEEK_END);
write(fd, " ", 1);
char* buffer = (char*) mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
memcpy(buffer, string.data(), len);
// close(fd); // open it will lose all content, but it's ok in the linux environment.
munmap(buffer, len);
close(fd); // it's fine.

Activity

  1. changed the title [-]close fd before calling mmap will cause the write failure[/-] [+]close fd before calling munmap will cause the write failure[/+] on Sep 28, 2021
  2. mere-human commented on Jan 25, 2023

    @mere-human
    Contributor

    I've seen the same problem in my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions