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

Extend wasi-emulated-mman with mprotect #500

Merged
merged 1 commit into from
May 22, 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
1 change: 1 addition & 0 deletions expected/wasm32-wasip1-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ mmap
modf
modff
modfl
mprotect
mrand48
munmap
nan
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip1/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ mmap
modf
modff
modfl
mprotect
mrand48
munmap
nan
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasip2/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ monotonic_clock_now
monotonic_clock_resolution
monotonic_clock_subscribe_duration
monotonic_clock_subscribe_instant
mprotect
mrand48
munmap
nan
Expand Down
33 changes: 33 additions & 0 deletions libc-bottom-half/mman/mman.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <sys/mman.h>
#include <sys/types.h>

Expand Down Expand Up @@ -122,3 +123,35 @@ int munmap(void *addr, size_t length) {
// Success!
return 0;
}

int mprotect(void *addr, size_t length, int prot) {
// Address must be page-aligned.
size_t begin = (size_t)addr;
if ((begin & (PAGESIZE - 1)) != 0) {
errno = EINVAL;
return -1;
}

// Length must not be big enough to wrap around.
size_t end;
if (__builtin_add_overflow(begin, length, &end)) {
errno = ENOMEM;
return -1;
}

// Range must be in bounds of linear memory.
size_t memory_size = __builtin_wasm_memory_size(0) * PAGESIZE;
if (end > memory_size) {
errno = ENOMEM;
return -1;
}

// Can only protect memory as read/write (which is a no-op).
if (prot != (PROT_READ | PROT_WRITE)) {
errno = ENOTSUP;
return -1;
}

// Success!
return 0;
}