Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for the msync syscall #42

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ additional extensions:
- pipe and event support (`pipe`, `event` )
- DNS resolution support (`resolve` )

# NOTES
- Memory mapping is currently not compatible with threads

# WASI(X) Extensions Spec

[WASIX](https://wasix.org) is maintained by wasix.org [here](https://github.com/wasix-org/wasix-witx)
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ modf
modff
modfl
mrand48
msync
mtx_destroy
mtx_init
mtx_lock
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ modf
modff
modfl
mrand48
msync
mtx_destroy
mtx_init
mtx_lock
Expand Down
1 change: 1 addition & 0 deletions expected/wasm64-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ modf
modff
modfl
mrand48
msync
mtx_destroy
mtx_init
mtx_lock
Expand Down
61 changes: 61 additions & 0 deletions libc-bottom-half/mman/mman.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct map {
int flags;
off_t offset;
size_t length;
int fd;
};

void *mmap(void *addr, size_t length, int prot, int flags,
Expand Down Expand Up @@ -90,6 +91,17 @@ void *mmap(void *addr, size_t length, int prot, int flags,
// or with zeros.
addr = map + 1;
if ((flags & MAP_ANON) == 0) {
int new_fd = dup(fd);

if (new_fd < 0) {
errno = EINVAL;
free(map);

return NULL;
}

map->fd = new_fd;

char *body = (char *)addr;
while (length > 0) {
const ssize_t nread = pread(fd, body, length, offset);
Expand All @@ -105,6 +117,7 @@ void *mmap(void *addr, size_t length, int prot, int flags,
body += (size_t)nread;
}
} else {
map->fd = -1;
memset(addr, 0, length);
}

Expand All @@ -123,6 +136,54 @@ int munmap(void *addr, size_t length) {
// Release the memory.
free(map);

// Write the data back to the backing file and close
// the file handle
if (map->fd > 0) {
if ((map->prot & PROT_WRITE) != 0) {
msync(addr, length, MS_SYNC);
}

close(map->fd);
}

// Success!
return 0;
}

int msync (void *addr, size_t length, int flags) {
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
struct map *map = (struct map *)addr - 1;
size_t map_flags = map->flags;
off_t map_offset = map->offset;
size_t map_length = map->length;
int fd = map->fd;

if (length > map_length) {
errno = EINVAL;
return -1;
}

if ((map->prot & PROT_WRITE) != 0) {
errno = EINVAL;
return -1;
}

if ((map_flags & MAP_ANON) == 0) {
char *body = (char *)addr;

while (length > 0) {
const ssize_t nwrite = pwrite(fd, body, length, map_offset);

if (nwrite > 0) {
length -= (size_t)nwrite;
map_offset += (size_t)nwrite;
body += (size_t)nwrite;
} else if (errno == EINTR) {
continue;
} else {
return -1;
}
}
}

return 0;
}
Loading