]>
git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/mman/mman.c
575a49578ce830358e7b9f9efc5491ae7ca61907
2 * Userspace emulation of mmap and munmap. Restrictions apply.
4 * This is meant to be complete enough to be compatible with code that uses
5 * mmap for simple file I/O. It just allocates memory with malloc and reads
6 * and writes data with pread and pwrite.
9 #define _WASI_EMULATED_MMAN
15 #include <sys/types.h>
25 void *mmap(void *addr
, size_t length
, int prot
, int flags
,
26 int fd
, off_t offset
) {
27 // Check for unsupported flags.
28 if ((flags
& MAP_FIXED
) != 0 ||
29 #ifdef MAP_SHARED_VALIDATE
30 (flags
& MAP_SHARED_VALIDATE
) != 0 ||
33 (flags
& MAP_NORESERVE
) != 0 ||
36 (flags
& MAP_GROWSDOWN
) != 0 ||
39 (flags
& MAP_HUGETLB
) != 0 ||
41 #ifdef MAP_FIXED_NOREPLACE
42 (flags
& MAP_FIXED_NOREPLACE
) != 0 ||
50 // Check for unsupported protection requests.
51 if (prot
== PROT_NONE
||
53 (prot
& PROT_EXEC
) != 0 ||
61 // Check for integer overflow.
63 if(__builtin_add_overflow(length
, sizeof(struct map
), &buf_len
)) {
68 // Allocate the memory.
69 struct map
*map
= malloc(buf_len
);
75 // Initialize the header.
81 // Initialize the main memory buffer, either with the contents of a file,
83 if ((flags
& MAP_ANON
) == 0) {
84 char *body
= map
->body
;
86 const ssize_t nread
= pread(fd
, body
, length
, offset
);
94 length
-= (size_t)nread
;
95 offset
+= (size_t)nread
;
96 body
+= (size_t)nread
;
99 memset(map
->body
, 0, length
);
105 int munmap(void *addr
, size_t length
) {
106 struct map
*map
= (struct map
*)addr
- 1;
108 // We don't support partial munmapping.
109 if (map
->length
!= length
) {
114 // Release the memory.