Skip to content

Commit

Permalink
Always free allocaed memory in mmunmap.
Browse files Browse the repository at this point in the history
Without this change we were returning early from `__syscall_munmap`
if `_munmap_js` failed.  This could happen if `fd` was no longer a valid
open file descriptor.  However, even in this case we still want to go
ahead and free any allocated memory.

Fixes: emscripten-core#21360
  • Loading branch information
sbc100 committed Feb 16, 2024
1 parent a95c44e commit 1b8ca02
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 11 deletions.
1 change: 0 additions & 1 deletion src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,6 @@ FS.staticInit();` +
}
return stream.stream_ops.msync(stream, buffer, offset, length, mmapFlags);
},
munmap: (stream) => 0,
ioctl(stream, cmd, arg) {
if (!stream.stream_ops.ioctl) {
throw new FS.ErrnoError({{{ cDefs.ENOTTY }}});
Expand Down
3 changes: 0 additions & 3 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ addToLibrary({
// should we check if bytesWritten and length are the same?
return 0;
},
munmap() {
return 0;
},
ioctl() {
throw new FS.ErrnoError({{{ cDefs.ENOTTY }}});
}
Expand Down
3 changes: 0 additions & 3 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,10 @@ var SyscallsLibrary = {
_munmap_js__i53abi: true,
_munmap_js: (addr, len, prot, flags, fd, offset) => {
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
if (isNaN(offset)) return {{{ cDefs.EOVERFLOW }}};
var stream = SYSCALLS.getStreamFromFD(fd);
if (prot & {{{ cDefs.PROT_WRITE }}}) {
SYSCALLS.doMsync(addr, stream, len, flags, offset);
}
FS.munmap(stream);
// implicitly return 0
#endif
},

Expand Down
5 changes: 1 addition & 4 deletions system/lib/libc/emscripten_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ int __syscall_munmap(intptr_t addr, size_t length) {
UNLOCK(lock);

if (!(map->flags & MAP_ANONYMOUS)) {
int rtn = _munmap_js(addr, length, map->prot, map->flags, map->fd, map->offset);
if (rtn) {
return rtn;
}
_munmap_js(addr, length, map->prot, map->flags, map->fd, map->offset);
}

// Release the memory.
Expand Down

0 comments on commit 1b8ca02

Please sign in to comment.