-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
emscripten_mmap.c
151 lines (131 loc) · 3.56 KB
/
emscripten_mmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2022 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <assert.h>
#include <errno.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <emscripten/heap.h>
#include "emscripten_internal.h"
#include "lock.h"
#include "syscall.h"
struct map {
void* addr;
long length;
int allocated;
int fd;
int flags;
off_t offset;
int prot;
struct map* next;
} __attribute__((aligned (1)));
#define ALIGN_TO(value,alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1))
// Linked list of all mapping, guarded by a musl-style lock (LOCK/UNLOCK)
static volatile int lock[1];
static struct map* mappings;
static struct map* find_mapping(intptr_t addr, struct map** prev) {
struct map* map = mappings;
while (map) {
if (map->addr == (void*)addr) {
return map;
}
if (prev) {
*prev = map;
}
map = map->next;
}
return map;
}
int __syscall_munmap(intptr_t addr, size_t length) {
LOCK(lock);
struct map* prev = NULL;
struct map* map = find_mapping(addr, &prev);
if (!map || !length) {
UNLOCK(lock);
return -EINVAL;
}
// We don't support partial munmapping.
if (map->length != length) {
UNLOCK(lock);
return -EINVAL;
}
// Remove map from linked list
if (prev) {
prev->next = map->next;
} else {
mappings = map->next;
}
UNLOCK(lock);
if (!(map->flags & MAP_ANONYMOUS)) {
_munmap_js(addr, length, map->prot, map->flags, map->fd, map->offset);
}
// Release the memory.
if (map->allocated) {
emscripten_builtin_free(map->addr);
}
if (!(map->flags & MAP_ANONYMOUS)) {
emscripten_builtin_free(map);
}
// Success!
return 0;
}
int __syscall_msync(intptr_t addr, size_t len, int flags) {
LOCK(lock);
struct map* map = find_mapping(addr, NULL);
UNLOCK(lock);
if (!map) {
return -EINVAL;
}
if (map->flags & MAP_ANONYMOUS) {
return 0;
}
return _msync_js(addr, len, map->prot, map->flags, map->fd, map->offset);
}
intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd, off_t offset) {
if (addr != 0) {
// We don't currently support location hints for the address of the mapping
return -EINVAL;
}
offset *= SYSCALL_MMAP2_UNIT;
struct map* new_map;
// MAP_ANONYMOUS (aka MAP_ANON) isn't actually defined by POSIX spec,
// but it is widely used way to allocate memory pages on Linux, BSD and Mac.
// In this case fd argument is ignored.
if (flags & MAP_ANONYMOUS) {
size_t alloc_len = ALIGN_TO(len, 16);
// For anonymous maps, allocate that mapping at the end of the region.
void* ptr = emscripten_builtin_memalign(WASM_PAGE_SIZE, alloc_len + sizeof(struct map));
if (!ptr) {
return -ENOMEM;
}
memset(ptr, 0, alloc_len);
new_map = (struct map*)((char*)ptr + alloc_len);
new_map->addr = ptr;
new_map->fd = -1;
new_map->allocated = true;
} else {
new_map = emscripten_builtin_malloc(sizeof(struct map));
int rtn =
_mmap_js(len, prot, flags, fd, offset, &new_map->allocated, &new_map->addr);
if (rtn < 0) {
emscripten_builtin_free(new_map);
return rtn;
}
new_map->fd = fd;
}
new_map->length = len;
new_map->flags = flags;
new_map->offset = offset;
new_map->prot = prot;
LOCK(lock);
new_map->next = mappings;
mappings = new_map;
UNLOCK(lock);
return (long)new_map->addr;
}