Skip to content

Commit 21cc30d

Browse files
authored
Fix signedness mismatches in the syscall layer (#19631)
1 parent 671ea82 commit 21cc30d

File tree

8 files changed

+40
-36
lines changed

8 files changed

+40
-36
lines changed

src/library_sigs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ sigs = {
353353
_gmtime_js__sig: 'vpp',
354354
_localtime_js__sig: 'vpp',
355355
_mktime_js__sig: 'ip',
356-
_mmap_js__sig: 'ipiiippp',
357-
_msync_js__sig: 'ippiiip',
358-
_munmap_js__sig: 'ippiiip',
356+
_mmap_js__sig: 'ipiiijpp',
357+
_msync_js__sig: 'ippiiij',
358+
_munmap_js__sig: 'ippiiij',
359359
_setitimer_js__sig: 'iid',
360360
_timegm_js__sig: 'ip',
361361
_tzset_js__sig: 'vppp',

src/library_syscall.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ var SyscallsLibrary = {
133133
'$mmapAlloc',
134134
'emscripten_builtin_memalign',
135135
#endif
136-
],
137-
_mmap_js: function(len, prot, flags, fd, off, allocated, addr) {
136+
].concat(i53ConversionDeps),
137+
_mmap_js: function(len, prot, flags, fd, {{{ defineI64Param('offset') }}}, allocated, addr) {
138+
{{{ receiveI64ParamAsI53('offset', -cDefs.EOVERFLOW) }}}
138139
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
139140
var stream = SYSCALLS.getStreamFromFD(fd);
140-
var res = FS.mmap(stream, len, off, prot, flags);
141+
var res = FS.mmap(stream, len, offset, prot, flags);
141142
var ptr = res.ptr;
142143
{{{ makeSetValue('allocated', 0, 'res.allocated', 'i32') }}};
143144
#if CAN_ADDRESS_2GB
@@ -154,8 +155,9 @@ var SyscallsLibrary = {
154155
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
155156
'$FS',
156157
#endif
157-
],
158-
_munmap_js: function(addr, len, prot, flags, fd, offset) {
158+
].concat(i53ConversionDeps),
159+
_munmap_js: function(addr, len, prot, flags, fd, {{{ defineI64Param('offset') }}}) {
160+
{{{ receiveI64ParamAsI53('offset', -cDefs.EOVERFLOW) }}}
159161
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
160162
var stream = SYSCALLS.getStreamFromFD(fd);
161163
if (prot & {{{ cDefs.PROT_WRITE }}}) {
@@ -623,8 +625,10 @@ var SyscallsLibrary = {
623625

624626
return total;
625627
},
626-
_msync_js: function(addr, len, prot, flags, fd, offset) {
627-
SYSCALLS.doMsync(addr, SYSCALLS.getStreamFromFD(fd), len, flags, 0);
628+
_msync_js__deps: i53ConversionDeps,
629+
_msync_js: function(addr, len, prot, flags, fd, {{{ defineI64Param('offset') }}}) {
630+
{{{ receiveI64ParamAsI53('offset', -cDefs.EOVERFLOW) }}}
631+
SYSCALLS.doMsync(addr, SYSCALLS.getStreamFromFD(fd), len, flags, offset);
628632
return 0;
629633
},
630634
__syscall_fdatasync: function(fd) {

system/lib/libc/emscripten_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ int _mmap_js(size_t length,
7575
int prot,
7676
int flags,
7777
int fd,
78-
size_t offset,
78+
off_t offset,
7979
int* allocated,
8080
void** addr);
8181
int _munmap_js(
82-
intptr_t addr, size_t length, int prot, int flags, int fd, size_t offset);
82+
intptr_t addr, size_t length, int prot, int flags, int fd, off_t offset);
8383
int _msync_js(
84-
intptr_t addr, size_t length, int prot, int flags, int fd, size_t offset);
84+
intptr_t addr, size_t length, int prot, int flags, int fd, off_t offset);
8585

8686
struct dso;
8787

system/lib/libc/emscripten_mmap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ int __syscall_msync(intptr_t addr, size_t len, int flags) {
105105
return _msync_js(addr, len, map->prot, map->flags, map->fd, map->offset);
106106
}
107107

108-
intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd, size_t off) {
108+
intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd, off_t offset) {
109109
if (addr != 0) {
110110
// We don't currently support location hints for the address of the mapping
111111
return -EINVAL;
112112
}
113113

114-
off *= SYSCALL_MMAP2_UNIT;
114+
offset *= SYSCALL_MMAP2_UNIT;
115115
struct map* new_map;
116116

117117
// MAP_ANONYMOUS (aka MAP_ANON) isn't actually defined by POSIX spec,
@@ -132,7 +132,7 @@ intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd,
132132
} else {
133133
new_map = emscripten_builtin_malloc(sizeof(struct map));
134134
int rtn =
135-
_mmap_js(len, prot, flags, fd, off, &new_map->allocated, &new_map->addr);
135+
_mmap_js(len, prot, flags, fd, offset, &new_map->allocated, &new_map->addr);
136136
if (rtn < 0) {
137137
emscripten_builtin_free(new_map);
138138
return rtn;
@@ -142,7 +142,7 @@ intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd,
142142

143143
new_map->length = len;
144144
new_map->flags = flags;
145-
new_map->offset = off;
145+
new_map->offset = offset;
146146
new_map->prot = prot;
147147

148148
LOCK(lock);

system/lib/libc/musl/arch/emscripten/syscall_arch.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
#include <sys/types.h>
12
#include <wasi/api.h>
23
#include <wasi/wasi-helpers.h>
3-
#include <emscripten/em_macros.h>
44

55
// Compile as if we can pass uint64 values directly to the
66
// host. Binaryen will take care of splitting any i64 params
@@ -55,9 +55,9 @@ int __syscall_mremap(intptr_t old_addr, size_t old_size, size_t new_size, int fl
5555
int __syscall_poll(intptr_t fds, int nfds, int timeout);
5656
int __syscall_getcwd(intptr_t buf, size_t size);
5757
int __syscall_ugetrlimit(int resource, intptr_t rlim);
58-
intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd, size_t off);
59-
int __syscall_truncate64(intptr_t path, uint64_t length);
60-
int __syscall_ftruncate64(int fd, uint64_t length);
58+
intptr_t __syscall_mmap2(intptr_t addr, size_t len, int prot, int flags, int fd, off_t offset);
59+
int __syscall_truncate64(intptr_t path, off_t length);
60+
int __syscall_ftruncate64(int fd, off_t length);
6161
int __syscall_stat64(intptr_t path, intptr_t buf);
6262
int __syscall_lstat64(intptr_t path, intptr_t buf);
6363
int __syscall_fstat64(int fd, intptr_t buf);
@@ -81,7 +81,7 @@ int __syscall_getdents64(int fd, intptr_t dirp, size_t count);
8181
int __syscall_fcntl64(int fd, int cmd, ...);
8282
int __syscall_statfs64(intptr_t path, size_t size, intptr_t buf);
8383
int __syscall_fstatfs64(int fd, size_t size, intptr_t buf);
84-
int __syscall_fadvise64(int fd, uint64_t offset, uint64_t length, int advice);
84+
int __syscall_fadvise64(int fd, off_t offset, off_t length, int advice);
8585
int __syscall_openat(int dirfd, intptr_t path, int flags, ...); // mode is optional
8686
int __syscall_mkdirat(int dirfd, intptr_t path, int mode);
8787
int __syscall_mknodat(int dirfd, intptr_t path, int mode, int dev);
@@ -96,7 +96,7 @@ int __syscall_fchmodat(int dirfd, intptr_t path, int mode, ...);
9696
int __syscall_faccessat(int dirfd, intptr_t path, int amode, int flags);
9797
int __syscall_pselect6(int nfds, intptr_t readfds, intptr_t writefds, intptr_t exceptfds, intptr_t timeout, intptr_t sigmaks);
9898
int __syscall_utimensat(int dirfd, intptr_t path, intptr_t times, int flags);
99-
int __syscall_fallocate(int fd, int mode, int64_t off, int64_t len);
99+
int __syscall_fallocate(int fd, int mode, off_t offset, off_t len);
100100
int __syscall_dup3(int fd, int suggestfd, int flags);
101101
int __syscall_pipe2(intptr_t fds, int flags);
102102
int __syscall_recvmmsg(int sockfd, intptr_t msgvec, size_t vlen, int flags, ...);

system/lib/standalone/standalone.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ weak int _mmap_js(size_t length,
7474
int prot,
7575
int flags,
7676
int fd,
77-
size_t offset,
77+
off_t offset,
7878
int* allocated,
7979
void** addr) {
8080
return -ENOSYS;
8181
}
8282

8383
weak int _munmap_js(
84-
intptr_t addr, size_t length, int prot, int flags, int fd, size_t offset) {
84+
intptr_t addr, size_t length, int prot, int flags, int fd, off_t offset) {
8585
return -ENOSYS;
8686
}
8787

system/lib/wasmfs/js_api.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ int _wasmfs_open(char* path, int flags, mode_t mode) {
129129
return __syscall_openat(AT_FDCWD, (intptr_t)path, flags, mode);
130130
}
131131

132-
int _wasmfs_allocate(int fd, int64_t off, int64_t len) {
133-
return __syscall_fallocate(fd, 0, off, len);
132+
int _wasmfs_allocate(int fd, off_t offset, off_t len) {
133+
return __syscall_fallocate(fd, 0, offset, len);
134134
}
135135

136136
int _wasmfs_mknod(char* path, mode_t mode, dev_t dev) {

system/lib/wasmfs/syscalls.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,15 +1226,15 @@ static int doTruncate(std::shared_ptr<File>& file, off_t size) {
12261226
return ret;
12271227
}
12281228

1229-
int __syscall_truncate64(intptr_t path, uint64_t size) {
1229+
int __syscall_truncate64(intptr_t path, off_t size) {
12301230
auto parsed = path::parseFile((char*)path);
12311231
if (auto err = parsed.getError()) {
12321232
return err;
12331233
}
12341234
return doTruncate(parsed.getFile(), size);
12351235
}
12361236

1237-
int __syscall_ftruncate64(int fd, uint64_t size) {
1237+
int __syscall_ftruncate64(int fd, off_t size) {
12381238
auto openFile = wasmFS.getFileTable().locked().getEntry(fd);
12391239
if (!openFile) {
12401240
return -EBADF;
@@ -1358,7 +1358,7 @@ int __syscall_poll(intptr_t fds_, int nfds, int timeout) {
13581358
return nonzero;
13591359
}
13601360

1361-
int __syscall_fallocate(int fd, int mode, int64_t off, int64_t len) {
1361+
int __syscall_fallocate(int fd, int mode, off_t offset, off_t len) {
13621362
assert(mode == 0); // TODO, but other modes were never supported in the old FS
13631363

13641364
auto fileTable = wasmFS.getFileTable().locked();
@@ -1378,14 +1378,14 @@ int __syscall_fallocate(int fd, int mode, int64_t off, int64_t len) {
13781378
return -EBADF;
13791379
}
13801380

1381-
if (off < 0 || len <= 0) {
1381+
if (offset < 0 || len <= 0) {
13821382
return -EINVAL;
13831383
}
13841384

13851385
// TODO: We could only fill zeros for regions that were completely unused
13861386
// before, which for a backend with sparse data storage could make a
13871387
// difference. For that we'd need a new backend API.
1388-
auto newNeededSize = off + len;
1388+
auto newNeededSize = offset + len;
13891389
off_t size = locked.getSize();
13901390
if (size < 0) {
13911391
return size;
@@ -1534,7 +1534,7 @@ int _mmap_js(size_t length,
15341534
int prot,
15351535
int flags,
15361536
int fd,
1537-
size_t offset,
1537+
off_t offset,
15381538
int* allocated,
15391539
void** addr) {
15401540
// PROT_EXEC is not supported (although we pretend to support the absence of
@@ -1621,7 +1621,7 @@ int _mmap_js(size_t length,
16211621
}
16221622

16231623
int _msync_js(
1624-
intptr_t addr, size_t length, int prot, int flags, int fd, size_t offset) {
1624+
intptr_t addr, size_t length, int prot, int flags, int fd, off_t offset) {
16251625
// TODO: This is not correct! Mappings should be associated with files, not
16261626
// fds. Only need to sync if shared and writes are allowed.
16271627
int mapType = flags & MAP_TYPE;
@@ -1637,7 +1637,7 @@ int _msync_js(
16371637
}
16381638

16391639
int _munmap_js(
1640-
intptr_t addr, size_t length, int prot, int flags, int fd, size_t offset) {
1640+
intptr_t addr, size_t length, int prot, int flags, int fd, off_t offset) {
16411641
// TODO: This is not correct! Mappings should be associated with files, not
16421642
// fds.
16431643
// TODO: Syncing should probably be handled in __syscall_munmap instead.
@@ -1718,7 +1718,7 @@ int __syscall_recvmsg(
17181718
return -ENOSYS;
17191719
}
17201720

1721-
int __syscall_fadvise64(int fd, uint64_t offset, uint64_t length, int advice) {
1721+
int __syscall_fadvise64(int fd, off_t offset, off_t length, int advice) {
17221722
// Advice is currently ignored. TODO some backends might use it
17231723
return 0;
17241724
}

0 commit comments

Comments
 (0)