Skip to content

Commit f5a1916

Browse files
authored
Fix return value of _mmap_js (#17853)
_mmap_js previously returned an inptr_t that was either a pointer on success or a negative error code on failure. However, all negative results were interpreted as failure, so successfully allocated memory in the second half of memory would have been incorrectly freed before being returned to the user. To fix this bug, have _mmap_js return 0 on success and write the returned pointer in an out-param.
1 parent 6577be0 commit f5a1916

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

src/library_syscall.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ var SyscallsLibrary = {
121121
#endif // SYSCALLS_REQUIRE_FILESYSTEM
122122
},
123123

124-
_mmap_js__sig: 'ppiiipp',
124+
_mmap_js__sig: 'ipiiippp',
125125
_mmap_js__deps: ['$SYSCALLS',
126126
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
127127
'$FS',
128128
#endif
129129
],
130-
_mmap_js: function(len, prot, flags, fd, off, allocated) {
130+
_mmap_js: function(len, prot, flags, fd, off, allocated, addr) {
131131
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
132132
var stream = SYSCALLS.getStreamFromFD(fd);
133133
var res = FS.mmap(stream, len, off, prot, flags);
@@ -136,7 +136,8 @@ var SyscallsLibrary = {
136136
#if CAN_ADDRESS_2GB
137137
ptr >>>= 0;
138138
#endif
139-
return ptr;
139+
{{{ makeSetValue('addr', 0, 'ptr', '*') }}};
140+
return 0;
140141
#else // no filesystem support; report lack of support
141142
return -{{{ cDefine('ENOSYS') }}};
142143
#endif

system/lib/libc/emscripten_mmap.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ static volatile int lock[1];
3333
static struct map* mappings;
3434

3535
// JS library functions. Used only when mapping files (not MAP_ANONYMOUS)
36-
intptr_t _mmap_js(size_t length, int prot, int flags, int fd, size_t offset, int* allocated);
36+
int _mmap_js(size_t length,
37+
int prot,
38+
int flags,
39+
int fd,
40+
size_t offset,
41+
int* allocated,
42+
void** addr);
3743
int _munmap_js(intptr_t addr, size_t length, int prot, int flags, int fd, size_t offset);
3844
int _msync_js(intptr_t addr, size_t length, int flags, int fd);
3945

@@ -132,12 +138,12 @@ intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd,
132138
new_map->allocated = true;
133139
} else {
134140
new_map = emscripten_builtin_malloc(sizeof(struct map));
135-
long rtn = _mmap_js(len, prot, flags, fd, off, &new_map->allocated);
141+
int rtn =
142+
_mmap_js(len, prot, flags, fd, off, &new_map->allocated, &new_map->addr);
136143
if (rtn < 0) {
137144
emscripten_builtin_free(new_map);
138145
return rtn;
139146
}
140-
new_map->addr = (void*)rtn;
141147
new_map->fd = fd;
142148
}
143149

system/lib/standalone/standalone.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ int clock_getres(clockid_t clk_id, struct timespec *tp) {
6666

6767
// Mark these as weak so that wasmfs does not collide with it. That is, if
6868
// wasmfs is in use, we want to use that and not this.
69-
__attribute__((__weak__)) intptr_t _mmap_js(
70-
size_t length, int prot, int flags, int fd, size_t offset, int* allocated) {
69+
__attribute__((__weak__)) int _mmap_js(size_t length,
70+
int prot,
71+
int flags,
72+
int fd,
73+
size_t offset,
74+
int* allocated,
75+
void** addr) {
7176
return -ENOSYS;
7277
}
7378

system/lib/wasmfs/syscalls.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,13 @@ int __syscall_fstatfs64(int fd, size_t size, intptr_t buf) {
15271527
return doStatFS(openFile->locked().getFile(), size, (struct statfs*)buf);
15281528
}
15291529

1530-
intptr_t _mmap_js(
1531-
size_t length, int prot, int flags, int fd, size_t offset, int* allocated) {
1530+
int _mmap_js(size_t length,
1531+
int prot,
1532+
int flags,
1533+
int fd,
1534+
size_t offset,
1535+
int* allocated,
1536+
void** addr) {
15321537
auto openFile = wasmFS.getFileTable().locked().getEntry(fd);
15331538
if (!openFile) {
15341539
return -EBADF;
@@ -1585,14 +1590,15 @@ intptr_t _mmap_js(
15851590
// From here on, we have succeeded, and can mark the allocation as having
15861591
// occurred (which means that the caller has the responsibility to free it).
15871592
*allocated = true;
1593+
*addr = (void*)ptr;
15881594

15891595
// The read must be of a valid amount, or we have had an internal logic error.
15901596
assert(nread <= length);
15911597

15921598
// mmap clears any extra bytes after the data itself.
15931599
memset(ptr + nread, 0, length - nread);
15941600

1595-
return (intptr_t)ptr;
1601+
return 0;
15961602
}
15971603

15981604
// Stubs (at least for now)

0 commit comments

Comments
 (0)