Skip to content

Commit edd1561

Browse files
authored
Fix broken i53 overflow checks in libsyscall.js. NFC (#24257)
These checks have been broken since they were added in #19711. Add some tests for the overload case.
1 parent 739e14f commit edd1561

File tree

9 files changed

+58
-9
lines changed

9 files changed

+58
-9
lines changed

src/lib/libsyscall.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ var SyscallsLibrary = {
153153
],
154154
_mmap_js: (len, prot, flags, fd, offset, allocated, addr) => {
155155
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
156-
if (isNaN(offset)) return {{{ cDefs.EOVERFLOW }}};
156+
#if ASSERTIONS
157+
// musl's mmap doesn't allow values over a certain limit
158+
// see OFF_MASK in mmap.c.
159+
assert(!isNaN(offset));
160+
#endif
157161
var stream = SYSCALLS.getStreamFromFD(fd);
158162
var res = FS.mmap(stream, len, offset, prot, flags);
159163
var ptr = res.ptr;
@@ -631,7 +635,7 @@ var SyscallsLibrary = {
631635
},
632636
_msync_js__i53abi: true,
633637
_msync_js: (addr, len, prot, flags, fd, offset) => {
634-
if (isNaN(offset)) return {{{ cDefs.EOVERFLOW }}};
638+
if (isNaN(offset)) return -{{{ cDefs.EOVERFLOW }}};
635639
SYSCALLS.doMsync(addr, SYSCALLS.getStreamFromFD(fd), len, flags, offset);
636640
return 0;
637641
},
@@ -670,14 +674,14 @@ var SyscallsLibrary = {
670674
},
671675
__syscall_truncate64__i53abi: true,
672676
__syscall_truncate64: (path, length) => {
673-
if (isNaN(length)) return {{{ cDefs.EOVERFLOW }}};
677+
if (isNaN(length)) return -{{{ cDefs.EOVERFLOW }}};
674678
path = SYSCALLS.getStr(path);
675679
FS.truncate(path, length);
676680
return 0;
677681
},
678682
__syscall_ftruncate64__i53abi: true,
679683
__syscall_ftruncate64: (fd, length) => {
680-
if (isNaN(length)) return {{{ cDefs.EOVERFLOW }}};
684+
if (isNaN(length)) return -{{{ cDefs.EOVERFLOW }}};
681685
FS.ftruncate(fd, length);
682686
return 0;
683687
},
@@ -995,7 +999,7 @@ var SyscallsLibrary = {
995999
},
9961000
__syscall_fallocate__i53abi: true,
9971001
__syscall_fallocate: (fd, mode, offset, len) => {
998-
if (isNaN(offset)) return {{{ cDefs.EOVERFLOW }}};
1002+
if (isNaN(offset) || isNaN(len)) return -{{{ cDefs.EOVERFLOW }}};
9991003
if (mode != 0) {
10001004
return -{{{ cDefs.ENOTSUP }}}
10011005
}

src/lib/libwasmfs_node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ addToLibrary({
159159
_wasmfs_node_truncate__i53abi: true,
160160
_wasmfs_node_truncate__deps : ['$wasmfsTry'],
161161
_wasmfs_node_truncate : (path_p, len) => {
162+
if (isNaN(len)) return -{{{ cDefs.EOVERFLOW }}};
162163
return wasmfsTry(() => fs.truncateSync(UTF8ToString(path_p), len));
163164
},
164165

165166
_wasmfs_node_ftruncate__i53abi: true,
166167
_wasmfs_node_ftruncate__deps : ['$wasmfsTry'],
167168
_wasmfs_node_ftruncate : (fd, len) => {
169+
if (isNaN(len)) return -{{{ cDefs.EOVERFLOW }}};
168170
return wasmfsTry(() => fs.ftruncateSync(fd, len));
169171
},
170172

system/lib/wasmfs/memory_backend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class MemoryDataFile : public DataFile {
2525
int flush() override { return 0; }
2626
off_t getSize() override { return buffer.size(); }
2727
int setSize(off_t size) override {
28+
if (size > buffer.max_size()) {
29+
return -EOVERFLOW;
30+
}
2831
buffer.resize(size);
2932
return 0;
3033
}

test/fcntl/test_fcntl_misc.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,19 @@ int main() {
4040

4141
err = posix_fallocate(f, -1, 7);
4242
printf("posix_fallocate 3: %s\n", strerror(err));
43-
printf("\n");
4443

4544
err = posix_fallocate(f, 3, -1);
4645
printf("posix_fallocate 4: %s\n", strerror(err));
4746

47+
// Values over 2^53 are not representable in JS and
48+
// should result in EOVERFLOW.
49+
err = posix_fallocate(f, 1, 0x00ffffffffffffff);
50+
assert(err == EOVERFLOW);
51+
printf("posix_fallocate 5: %s\n", strerror(err));
52+
53+
err = posix_fallocate(f, 0x00ffffffffffffff, 1);
54+
assert(err == EOVERFLOW);
55+
printf("posix_fallocate 6: %s\n", strerror(err));
56+
4857
return 0;
4958
}

test/fcntl/test_fcntl_misc.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ posix_fallocate 2: 0
77
st_size: 10
88

99
posix_fallocate 3: Invalid argument
10-
1110
posix_fallocate 4: Invalid argument
11+
posix_fallocate 5: Value too large for data type
12+
posix_fallocate 6: Value too large for data type

test/fs/test_mmap.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ void test_mmap_shared_with_offset() {
164164
// assert failure if offset is not a multiple of page size
165165
assert(map == MAP_FAILED);
166166

167+
map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x00ffffffffffffff);
168+
// mmap offset outside MAX_SAFE_INTEGER range.
169+
assert(map == MAP_FAILED);
170+
assert(errno == EINVAL);
171+
167172
map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
168173
assert(map != MAP_FAILED);
169174

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
169140
1+
169161
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
49964
1+
49985

test/unistd/truncate.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,30 @@ int main() {
124124
memset(&s, 0, sizeof s);
125125
errno = 0;
126126

127+
#if __wasm32__ || !defined(WASMFS) || defined(NODEFS) || defined(NODERAWFS)
128+
// These last two test don't run against the in-memory wasmfs filesystem
129+
// in wasm64 mode since in that case they fail with an malloc abort.
130+
//
131+
// If we are running in wasm32, or we are using JS-based FS then we detect
132+
// these overflows before any attempt at allocation.
133+
printf("ftruncate(0x00ffffffffffffff): %d\n", ftruncate(f, 0x00ffffffffffffff));
134+
printf("errno: %s\n", strerror(errno));
135+
fstat(f, &s);
136+
printf("st_size: %lld\n", s.st_size);
137+
assert(s.st_size == 0);
138+
memset(&s, 0, sizeof s);
139+
errno = 0;
140+
printf("\n");
141+
142+
printf("truncate(0x00ffffffffffffff): %d\n", truncate("towrite", 0x00ffffffffffffff));
143+
printf("errno: %s\n", strerror(errno));
144+
fstat(f, &s);
145+
printf("st_size: %lld\n", s.st_size);
146+
assert(s.st_size == 0);
147+
memset(&s, 0, sizeof s);
148+
errno = 0;
149+
printf("\n");
150+
#endif
151+
127152
return 0;
128153
}

0 commit comments

Comments
 (0)