16
16
#include <sys/stat.h>
17
17
#include <unistd.h>
18
18
#include <sys/io.h>
19
- #include <sys/mman .h>
19
+ #include <errno .h>
20
20
21
21
int main () {
22
22
EM_ASM (
23
23
FS .mkdir ('yolo' );
24
24
#if NODEFS
25
25
FS .mount (NODEFS , { root : '.' }, 'yolo' );
26
26
#endif
27
- FS .writeFile ('/ yolo/in.txt' , 'mmap ftw!' );
27
+ FS .writeFile ('yolo/in.txt' , 'mmap ftw!' );
28
28
);
29
29
30
30
// Use mmap to read in.txt
31
31
{
32
- const char * path = "/ yolo/in.txt" ;
32
+ const char * path = "yolo/in.txt" ;
33
33
int fd = open (path , O_RDONLY );
34
34
assert (fd != -1 );
35
35
36
36
int filesize = 9 ;
37
37
char * map = (char * )mmap (NULL , filesize , PROT_READ , MAP_PRIVATE , fd , 0 );
38
38
assert (map != MAP_FAILED );
39
39
40
- printf ("/ yolo/in.txt content=" );
40
+ printf ("yolo/in.txt content=" );
41
41
for (int i = 0 ; i < filesize ; i ++ ) {
42
42
printf ("%c" , map [i ]);
43
43
}
@@ -52,7 +52,7 @@ int main() {
52
52
// Use mmap to write out.txt
53
53
{
54
54
const char * text = "written mmap" ;
55
- const char * path = "/ yolo/out.txt" ;
55
+ const char * path = "yolo/out.txt" ;
56
56
57
57
int fd = open (path , O_RDWR | O_CREAT | O_TRUNC , (mode_t )0600 );
58
58
assert (fd != -1 );
@@ -78,15 +78,15 @@ int main() {
78
78
}
79
79
80
80
{
81
- FILE * fd = fopen ("/ yolo/out.txt" , "r" );
81
+ FILE * fd = fopen ("yolo/out.txt" , "r" );
82
82
if (fd == NULL ) {
83
- printf ("failed to open / yolo/out.txt\n" );
83
+ printf ("failed to open yolo/out.txt\n" );
84
84
return 1 ;
85
85
}
86
86
char buffer [15 ];
87
87
memset (buffer , 0 , 15 );
88
88
fread (buffer , 1 , 14 , fd );
89
- printf ("/ yolo/out.txt content=%s\n" , buffer );
89
+ printf ("yolo/out.txt content=%s\n" , buffer );
90
90
fclose (fd );
91
91
}
92
92
@@ -95,7 +95,7 @@ int main() {
95
95
{
96
96
const char * readonlytext = "readonly mmap\0" ;
97
97
const char * text = "write mmap\0" ;
98
- const char * path = "/ yolo/outreadonly.txt" ;
98
+ const char * path = "yolo/outreadonly.txt" ;
99
99
size_t readonlytextsize = strlen (readonlytext );
100
100
size_t textsize = strlen (text );
101
101
@@ -117,22 +117,22 @@ int main() {
117
117
}
118
118
119
119
{
120
- FILE * fd = fopen ("/ yolo/outreadonly.txt" , "r" );
120
+ FILE * fd = fopen ("yolo/outreadonly.txt" , "r" );
121
121
if (fd == NULL ) {
122
- printf ("failed to open / yolo/outreadonly.txt\n" );
122
+ printf ("failed to open yolo/outreadonly.txt\n" );
123
123
return 1 ;
124
124
}
125
125
char buffer [16 ];
126
126
memset (buffer , 0 , 16 );
127
127
fread (buffer , 1 , 15 , fd );
128
- printf ("/ yolo/outreadonly.txt content=%s\n" , buffer );
128
+ printf ("yolo/outreadonly.txt content=%s\n" , buffer );
129
129
fclose (fd );
130
130
}
131
131
132
132
// MAP_PRIVATE
133
133
{
134
134
const char * text = "written mmap" ;
135
- const char * path = "/ yolo/private.txt" ;
135
+ const char * path = "yolo/private.txt" ;
136
136
137
137
int fd = open (path , O_RDWR | O_CREAT | O_TRUNC , (mode_t )0600 );
138
138
assert (fd != -1 );
@@ -158,22 +158,22 @@ int main() {
158
158
}
159
159
160
160
{
161
- FILE * fd = fopen ("/ yolo/private.txt" , "r" );
161
+ FILE * fd = fopen ("yolo/private.txt" , "r" );
162
162
if (fd == NULL ) {
163
- printf ("failed to open / yolo/private.txt\n" );
163
+ printf ("failed to open yolo/private.txt\n" );
164
164
return 1 ;
165
165
}
166
166
char buffer [15 ];
167
167
memset (buffer , 0 , 15 );
168
168
fread (buffer , 1 , 14 , fd );
169
- printf ("/ yolo/private.txt content=%s\n" , buffer );
169
+ printf ("yolo/private.txt content=%s\n" , buffer );
170
170
fclose (fd );
171
171
}
172
172
173
173
// MAP_SHARED with offset
174
174
{
175
175
const char * text = "written shared mmap with offset" ;
176
- const char * path = "/ yolo/sharedoffset.txt" ;
176
+ const char * path = "yolo/sharedoffset.txt" ;
177
177
178
178
int fd = open (path , O_RDWR | O_CREAT | O_TRUNC , (mode_t )0600 );
179
179
assert (fd != -1 );
@@ -206,10 +206,31 @@ int main() {
206
206
close (fd );
207
207
}
208
208
209
+ // mmap with a address will fail
210
+ {
211
+ const char * path = "yolo/private.txt" ;
212
+
213
+ int fd = open (path , O_RDWR | O_CREAT | O_TRUNC , (mode_t )0600 );
214
+ assert (fd != -1 );
215
+
216
+ size_t map_size = 1 << 16 ;
217
+
218
+ // Reserve some address space in which to perform the experiment
219
+ char * alloc = (char * )mmap (NULL , map_size , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 );
220
+ assert (alloc != MAP_FAILED );
221
+
222
+ char * addr = (char * )mmap ((void * )alloc , map_size , PROT_READ , MAP_PRIVATE | MAP_FIXED , fd , 0 );
223
+ assert (addr == MAP_FAILED && errno == EINVAL ); // Emscripten
224
+ //assert(addr == alloc); // Native environments
225
+
226
+ assert (munmap (alloc , map_size ) != -1 );
227
+ close (fd );
228
+ }
229
+
209
230
{
210
- FILE * fd = fopen ("/ yolo/sharedoffset.txt" , "r" );
231
+ FILE * fd = fopen ("yolo/sharedoffset.txt" , "r" );
211
232
if (fd == NULL ) {
212
- printf ("failed to open / yolo/sharedoffset.txt\n" );
233
+ printf ("failed to open yolo/sharedoffset.txt\n" );
213
234
return 1 ;
214
235
}
215
236
size_t offset = sysconf (_SC_PAGE_SIZE ) * 2 ;
@@ -218,11 +239,11 @@ int main() {
218
239
memset (buffer , 0 , offset + 33 );
219
240
fread (buffer , 1 , offset + 32 , fd );
220
241
// expect text written from mmap operation to appear at offset in the file
221
- printf ("/ yolo/sharedoffset.txt content=%s %zu\n" , buffer + offset , offset );
242
+ printf ("yolo/sharedoffset.txt content=%s %zu\n" , buffer + offset , offset );
222
243
fclose (fd );
223
244
}
224
245
225
- #if !defined(NODEFS )
246
+ #if !defined(NODEFS ) && !defined( NODERAWFS )
226
247
/**
227
248
* MMAP to an 'over-allocated' file
228
249
*
@@ -233,7 +254,7 @@ int main() {
233
254
* is not written beyond the allocated memory area for the mmap operation.
234
255
*/
235
256
{
236
- int fd = open ("/ yolo/overallocatedfile.txt" , O_RDWR | O_CREAT , (mode_t )0600 );
257
+ int fd = open ("yolo/overallocatedfile.txt" , O_RDWR | O_CREAT , (mode_t )0600 );
237
258
assert (fd != -1 );
238
259
239
260
const size_t textsize = 33 ;
@@ -244,13 +265,13 @@ int main() {
244
265
}
245
266
246
267
EM_ASM_ ({
247
- const stream = FS .streams .find (stream = > stream .path .indexOf ('/ yolo/overallocatedfile.txt' )>= 0 );
268
+ const stream = FS .streams .find (stream = > stream .path .indexOf ('yolo/overallocatedfile.txt' ) >= 0 );
248
269
assert (stream .node .usedBytes == = $0 ,
249
- 'Used bytes on the over-allocated file (' + stream .node .usedBytes + ') ' +
270
+ 'Used bytes on the over-allocated file (' + stream .node .usedBytes + ') ' +
250
271
'should be 33'
251
272
);
252
273
assert (stream .node .contents .length > stream .node .usedBytes ,
253
- 'Used bytes on the over-allocated file (' + stream .node .usedBytes + ') ' +
274
+ 'Used bytes on the over-allocated file (' + stream .node .usedBytes + ') ' +
254
275
'should be less than the length of the content buffer (' + stream .node .contents .length + ')'
255
276
);
256
277
stream .node .contents [stream .node .usedBytes ] = 98 ; // 'b', we don't want to see this in the mmap area
0 commit comments