forked from rsenn/qjs-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickjs-mmap.c
237 lines (200 loc) · 6.03 KB
/
quickjs-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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "defines.h"
#include <cutils.h>
#include <quickjs.h>
#include "utils.h"
#ifdef _WIN32
#include "mmap-win32.h"
#else
#include <sys/mman.h>
#include <errno.h>
#endif
/**
* \defgroup quickjs-mmap quickjs-mmap: Memory mapped I/O
* @{
*/
static void
js_mmap_free_func(JSRuntime* rt, void* opaque, void* ptr) {
munmap(ptr, (size_t)opaque);
}
static JSValue
js_mmap_map(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
uint64_t addr, length, offset;
int32_t prot, flags, fd;
void* ptr;
if(js_is_null_or_undefined(argv[0]))
addr = 0;
else if(JS_ToIndex(ctx, &addr, argv[0]))
return JS_EXCEPTION;
if(JS_ToIndex(ctx, &length, argv[1]))
return JS_EXCEPTION;
if(argc <= 2 || !JS_IsNumber(argv[2]) || JS_ToInt32(ctx, &prot, argv[2]))
prot = PROT_READ | PROT_WRITE;
if(argc <= 3 || !JS_IsNumber(argv[3]) || JS_ToInt32(ctx, &flags, argv[3]))
flags = MAP_ANONYMOUS;
if(argc <= 4 || !JS_IsNumber(argv[4]) || JS_ToInt32(ctx, &fd, argv[4]))
fd = -1;
if(argc <= 5 || !JS_IsNumber(argv[5]) || JS_ToIndex(ctx, &offset, argv[5]))
offset = 0;
ptr = mmap((void*)addr, length, prot, flags, fd, offset);
if(ptr == 0)
return JS_EXCEPTION;
if(ptr == MAP_FAILED)
return JS_NewInt32(ctx, -1);
return JS_NewArrayBuffer(ctx, ptr, length, &js_mmap_free_func, (void*)length, !!(flags & MAP_SHARED));
}
static JSValue
js_mmap_unmap(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JS_DetachArrayBuffer(ctx, argv[0]);
return JS_UNDEFINED;
}
static JSValue
js_mmap_msync(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
uint64_t length;
uint8_t* data;
int32_t flags = 0;
size_t len;
if(!(data = JS_GetArrayBuffer(ctx, &len, argv[0])))
return JS_ThrowTypeError(ctx, "argument 1 must be an ArrayBuffer");
if(!JS_ToIndex(ctx, &length, argv[1])) {
len = MIN_NUM(len, length);
}
JS_ToInt32(ctx, &flags, argv[2]);
return JS_NewInt32(ctx, msync(data, len, flags));
}
static JSValue
js_mmap_mprotect(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
int64_t offset = 0, length;
uint8_t* data;
int32_t flags = 0;
size_t len;
if(!(data = JS_GetArrayBuffer(ctx, &len, argv[0])))
return JS_ThrowTypeError(ctx, "argument 1 must be an ArrayBuffer");
argc--;
argv++;
length = len;
if(argc >= 3 && JS_IsNumber(argv[0])) {
JS_ToInt64(ctx, &offset, argv[0]);
argc--;
argv++;
}
if(argc >= 2 && JS_IsNumber(argv[0])) {
JS_ToInt64(ctx, &length, argv[0]);
argc--;
argv++;
}
JS_ToInt32(ctx, &flags, argv[0]);
return JS_NewInt32(ctx, mprotect(data + offset, MIN_NUM(length, (int64_t)len), flags));
}
static JSValue
js_mmap_filename(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
uint8_t* data;
size_t len;
FILE* fp;
JSValue ret = JS_UNDEFINED;
char buf[1024];
size_t start, end;
if(!(data = JS_GetArrayBuffer(ctx, &len, argv[0])))
return JS_ThrowTypeError(ctx, "argument 1 must be an ArrayBuffer");
if((fp = fopen("/proc/self/maps", "r")) == 0)
return JS_ThrowInternalError(ctx, "Unable to open /proc/self/maps");
while(fgets(buf, sizeof(buf) - 1, fp)) {
if(sscanf(buf, "%zx-%zx", &start, &end) < 2)
continue;
if((size_t)data >= start && (size_t)data < end) {
ret = JS_NewString(ctx, &buf[73]);
break;
}
}
fclose(fp);
return ret;
}
static JSValue
js_mmap_tostring(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSValue ret = JS_UNDEFINED;
if(js_is_arraybuffer(ctx, argv[0])) {
uint8_t* data;
size_t len;
if((data = JS_GetArrayBuffer(ctx, &len, argv[0]))) {
ret = JS_NewStringLen(ctx, (const char*)data, len);
}
}
return ret;
}
static const JSCFunctionListEntry js_mmap_funcs[] = {
JS_CFUNC_DEF("mmap", 2, js_mmap_map),
JS_CFUNC_DEF("munmap", 1, js_mmap_unmap),
JS_CFUNC_DEF("msync", 3, js_mmap_msync),
JS_CFUNC_DEF("mprotect", 3, js_mmap_mprotect),
JS_CFUNC_DEF("filename", 1, js_mmap_filename),
JS_CFUNC_DEF("toString", 1, js_mmap_tostring),
JS_PROP_INT32_DEF("PROT_READ", 0x01, 0),
JS_PROP_INT32_DEF("PROT_WRITE", 0x02, 0),
JS_PROP_INT32_DEF("PROT_EXEC", 0x04, 0),
// JS_PROP_INT32_DEF("PROT_SEM", 0x08, 0),
JS_PROP_INT32_DEF("PROT_NONE", 0x00, 0),
JS_PROP_INT32_DEF("PROT_GROWSDOWN", 0x01000000, 0),
JS_PROP_INT32_DEF("PROT_GROWSUP", 0x02000000, 0),
JS_PROP_INT32_DEF("MAP_SHARED", 0x01, 0),
JS_PROP_INT32_DEF("MAP_PRIVATE", 0x02, 0),
JS_PROP_INT32_DEF("MAP_TYPE", 0x0f, 0),
JS_PROP_INT32_DEF("MAP_FIXED", 0x10, 0),
JS_PROP_INT32_DEF("MAP_ANONYMOUS", 0x20, 0),
JS_PROP_INT32_DEF("MAP_GROWSDOWN", 0x0100, 0),
JS_PROP_INT32_DEF("MAP_DENYWRITE", 0x0800, 0),
JS_PROP_INT32_DEF("MAP_EXECUTABLE", 0x1000, 0),
JS_PROP_INT32_DEF("MAP_LOCKED", 0x2000, 0),
JS_PROP_INT32_DEF("MAP_NORESERVE", 0x4000, 0),
JS_PROP_INT32_DEF("MAP_POPULATE", 0x8000, 0),
JS_PROP_INT32_DEF("MAP_NONBLOCK", 0x10000, 0),
JS_PROP_INT32_DEF("MAP_STACK", 0x20000, 0),
JS_PROP_INT32_DEF("MAP_HUGETLB", 0x40000, 0),
JS_PROP_INT32_DEF("MAP_FAILED", -1, 0),
#ifdef EBUSY
JS_CONSTANT(EBUSY),
#endif
#ifdef EFAULT
JS_CONSTANT(EFAULT),
#endif
#ifdef EINVAL
JS_CONSTANT(EINVAL),
#endif
#ifdef ENOMEM
JS_CONSTANT(ENOMEM),
#endif
#ifdef MS_ASYNC
JS_CONSTANT(MS_ASYNC),
#endif
#ifdef MS_INVALIDATE
JS_CONSTANT(MS_INVALIDATE),
#endif
#ifdef MS_SYNC
JS_CONSTANT(MS_SYNC),
#endif
#ifdef PROT_SAO
JS_CONSTANT(PROT_SAO),
#endif
#ifdef PROT_SEM
JS_CONSTANT(PROT_SEM),
#endif
};
static int
js_mmap_init(JSContext* ctx, JSModuleDef* m) {
JS_SetModuleExportList(ctx, m, js_mmap_funcs, countof(js_mmap_funcs));
return 0;
}
#ifdef JS_SHARED_LIBRARY
#define JS_INIT_MODULE js_init_module
#else
#define JS_INIT_MODULE js_init_module_mmap
#endif
VISIBLE JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if((m = JS_NewCModule(ctx, module_name, js_mmap_init))) {
JS_AddModuleExportList(ctx, m, js_mmap_funcs, countof(js_mmap_funcs));
}
return m;
}
/**
* @}
*/