forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mmap.c
More file actions
228 lines (180 loc) · 6.21 KB
/
test_mmap.c
File metadata and controls
228 lines (180 loc) · 6.21 KB
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
/*
* Copyright 2015 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 <stdio.h>
#include <sys/mman.h>
#include <emscripten.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/mman.h>
int main() {
EM_ASM(
FS.mkdir('yolo');
#if NODEFS
FS.mount(NODEFS, { root: '.' }, 'yolo');
#endif
FS.writeFile('/yolo/in.txt', 'mmap ftw!');
);
// Use mmap to read in.txt
{
const char* path = "/yolo/in.txt";
int fd = open(path, O_RDONLY);
assert(fd != -1);
int filesize = 9;
char* map = (char*)mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0);
assert(map != MAP_FAILED);
printf("/yolo/in.txt content=");
for (int i = 0; i < filesize; i++) {
printf("%c", map[i]);
}
printf("\n");
int rc = munmap(map, filesize);
assert(rc == 0);
close(fd);
}
// Use mmap to write out.txt
{
const char* text = "written mmap";
const char* path = "/yolo/out.txt";
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
assert(fd != -1);
size_t textsize = strlen(text) + 1; // + \0 null character
assert(lseek(fd, textsize - 1, SEEK_SET) != -1);
// need to write something first to allow us to mmap
assert(write(fd, "", 1) != -1);
char *map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
assert(map != MAP_FAILED);
for (size_t i = 0; i < textsize; i++) {
map[i] = text[i];
}
assert(msync(map, textsize, MS_SYNC) != -1);
assert(munmap(map, textsize) != -1);
close(fd);
}
{
FILE* fd = fopen("/yolo/out.txt", "r");
if (fd == NULL) {
printf("failed to open /yolo/out.txt\n");
return 1;
}
char buffer[13];
fread(buffer, 1, 14, fd);
printf("/yolo/out.txt content=%s\n", buffer);
fclose(fd);
}
// MAP_PRIVATE
{
const char* text = "written mmap";
const char* path = "/yolo/private.txt";
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
assert(fd != -1);
size_t textsize = strlen(text) + 1; // + \0 null character
assert(lseek(fd, textsize - 1, SEEK_SET) != -1);
// need to write something first to allow us to mmap
assert(write(fd, "", 1) != -1);
char *map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(map != MAP_FAILED);
for (size_t i = 0; i < textsize; i++) {
map[i] = text[i];
}
assert(msync(map, textsize, MS_SYNC) != -1);
assert(munmap(map, textsize) != -1);
close(fd);
}
{
FILE* fd = fopen("/yolo/private.txt", "r");
if (fd == NULL) {
printf("failed to open /yolo/private.txt\n");
return 1;
}
char buffer[13];
fread(buffer, 1, 14, fd);
printf("/yolo/private.txt content=%s\n", buffer);
fclose(fd);
}
// MAP_SHARED with offset
{
const char* text = "written shared mmap with offset";
const char* path = "/yolo/sharedoffset.txt";
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
assert(fd != -1);
size_t textsize = strlen(text) + 1; // + \0 null character
// offset must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE).
size_t offset = sysconf(_SC_PAGE_SIZE) * 2;
assert(lseek(fd, textsize + offset - 1, SEEK_SET) != -1);
// need to write something first to allow us to mmap
assert(write(fd, "", 1) != -1);
char *map;
map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset - 1);
// assert failure if offset is not a multiple of page size
assert(map == MAP_FAILED);
map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
assert(map != MAP_FAILED);
for (size_t i = 0; i < textsize; i++) {
map[i] = text[i];
}
assert(msync(map, textsize, MS_SYNC) != -1);
assert(munmap(map, textsize) != -1);
close(fd);
}
{
FILE* fd = fopen("/yolo/sharedoffset.txt", "r");
if (fd == NULL) {
printf("failed to open /yolo/sharedoffset.txt\n");
return 1;
}
size_t offset = sysconf(_SC_PAGE_SIZE) * 2;
char buffer[offset + 31];
fread(buffer, 1, offset + 32, fd);
// expect text written from mmap operation to appear at offset in the file
printf("/yolo/sharedoffset.txt content=%s %d\n", buffer + offset, offset);
fclose(fd);
}
#if !defined(NODEFS)
/**
* MMAP to an 'over-allocated' file
*
* When appending to a file, the buffer size is increased in chunks, and so the actual length
* of the file could be less than the buffer size.
*
* When using mmap for an over-allocated file, we have to make sure that content from the buffer
* is not written beyond the allocated memory area for the mmap operation.
*/
{
int fd = open("/yolo/overallocatedfile.txt", O_RDWR | O_CREAT, (mode_t)0600);
assert(fd != -1);
const size_t textsize = 33;
// multiple calls to write so that the file will be over-allocated
for (int n = 0; n < textsize; n++) {
assert(write(fd, "a", 1) != -1);
}
EM_ASM_({
const stream = FS.streams.find(stream => stream.path.indexOf('/yolo/overallocatedfile.txt')>=0);
assert(stream.node.usedBytes === $0,
'Used bytes on the over-allocated file (' + stream.node.usedBytes+ ') ' +
'should be 33'
);
assert(stream.node.contents.length > stream.node.usedBytes,
'Used bytes on the over-allocated file (' + stream.node.usedBytes+ ') ' +
'should be less than the length of the content buffer (' + stream.node.contents.length + ')'
);
stream.node.contents[stream.node.usedBytes] = 98; // 'b', we don't want to see this in the mmap area
}, textsize);
char *map = (char*)mmap(NULL, textsize, PROT_READ, 0, fd, 0);
assert(map[textsize-1] == 'a');
// Assert that content from the over-allocated file buffer is not written beyond the allocated memory for the map
assert(map[textsize] != 'b');
close(fd);
}
#endif
return 0;
}