Skip to content

Commit 6511178

Browse files
authored
Remove memory buffer param from mmap (#11156)
mmap always operates on the HEAP8 buffer, so this parameter wasn't useful and it caused the code to have to special case HEAP8 validation.
1 parent bab3f12 commit 6511178

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

ChangeLog.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ See docs/process.md for how version tagging works.
1717

1818
Current Trunk
1919
-------------
20+
- The mmap method of JavaScript filesysem drivers (based on library_fs.js) no
21+
longer takes a target memory. Its safer/cleaner/smaller to assume the target
22+
is the global memory buffer.
2023

2124
1.39.16: 05/15/2020
2225
-------------------

src/library_fs.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,9 @@ FS.staticInit();` +
12251225
}
12261226
stream.stream_ops.allocate(stream, offset, length);
12271227
},
1228-
mmap: function(stream, buffer, offset, length, position, prot, flags) {
1228+
mmap: function(stream, address, length, position, prot, flags) {
12291229
#if CAN_ADDRESS_2GB
1230-
offset >>>= 0;
1230+
address >>>= 0;
12311231
#endif
12321232
// User requests writing to file (prot & PROT_WRITE != 0).
12331233
// Checking if we have permissions to write to the file unless
@@ -1246,7 +1246,7 @@ FS.staticInit();` +
12461246
if (!stream.stream_ops.mmap) {
12471247
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
12481248
}
1249-
return stream.stream_ops.mmap(stream, buffer, offset, length, position, prot, flags);
1249+
return stream.stream_ops.mmap(stream, address, length, position, prot, flags);
12501250
},
12511251
msync: function(stream, buffer, offset, length, mmapFlags) {
12521252
#if CAN_ADDRESS_2GB

src/library_memfs.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,18 @@ mergeInto(LibraryManager.library, {
354354
MEMFS.expandFileStorage(stream.node, offset + length);
355355
stream.node.usedBytes = Math.max(stream.node.usedBytes, offset + length);
356356
},
357-
mmap: function(stream, buffer, offset, length, position, prot, flags) {
358-
#if ASSERTIONS
359-
// The data buffer should be a typed array view
360-
assert(!(buffer instanceof ArrayBuffer));
361-
#endif
357+
mmap: function(stream, address, length, position, prot, flags) {
358+
// We don't currently support location hints for the address of the mapping
359+
assert(address === 0);
360+
362361
if (!FS.isFile(stream.node.mode)) {
363362
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
364363
}
365364
var ptr;
366365
var allocated;
367366
var contents = stream.node.contents;
368367
// Only make a new copy when MAP_PRIVATE is specified.
369-
if ( !(flags & {{{ cDefine('MAP_PRIVATE') }}}) &&
370-
contents.buffer === buffer.buffer ) {
368+
if (!(flags & {{{ cDefine('MAP_PRIVATE') }}}) && contents.buffer === buffer) {
371369
// We can't emulate MAP_SHARED when the file is not backed by the buffer
372370
// we're mapping to (e.g. the HEAP buffer).
373371
allocated = false;
@@ -382,17 +380,14 @@ mergeInto(LibraryManager.library, {
382380
}
383381
}
384382
allocated = true;
385-
// malloc() can lead to growing the heap. If targeting the heap, we need to
386-
// re-acquire the heap buffer object in case growth had occurred.
387-
var fromHeap = (buffer.buffer == HEAP8.buffer);
388383
ptr = _malloc(length);
389384
if (!ptr) {
390385
throw new FS.ErrnoError({{{ cDefine('ENOMEM') }}});
391386
}
392387
#if CAN_ADDRESS_2GB
393388
ptr >>>= 0;
394389
#endif
395-
(fromHeap ? HEAP8 : buffer).set(contents, ptr);
390+
HEAP8.set(contents, ptr);
396391
}
397392
return { ptr: ptr, allocated: allocated };
398393
},

src/library_nodefs.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,17 @@ mergeInto(LibraryManager.library, {
294294

295295
return position;
296296
},
297-
mmap: function(stream, buffer, offset, length, position, prot, flags) {
297+
mmap: function(stream, address, length, position, prot, flags) {
298+
// We don't currently support location hints for the address of the mapping
299+
assert(address === 0);
300+
298301
if (!FS.isFile(stream.node.mode)) {
299302
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
300303
}
301304

302-
// malloc() can lead to growing the heap. If targeting the heap, we need to
303-
// re-acquire the heap buffer object in case growth had occurred.
304-
var fromHeap = (buffer == HEAPU8);
305-
306305
var ptr = _malloc(length);
307306

308-
assert(offset === 0);
309-
NODEFS.stream_ops.read(stream, fromHeap ? HEAP8 : buffer, ptr + offset, length, position);
310-
307+
NODEFS.stream_ops.read(stream, HEAP8, ptr, length, position);
311308
return { ptr: ptr, allocated: true };
312309
},
313310
msync: function(stream, buffer, offset, length, mmapFlags) {

src/library_syscall.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ var SyscallsLibrary = {
249249
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
250250
var info = FS.getStream(fd);
251251
if (!info) return -{{{ cDefine('EBADF') }}};
252-
var res = FS.mmap(info, HEAPU8, addr, len, off, prot, flags);
252+
var res = FS.mmap(info, addr, len, off, prot, flags);
253253
ptr = res.ptr;
254254
allocated = res.allocated;
255255
#else // no filesystem support; report lack of support

0 commit comments

Comments
 (0)