Skip to content

Commit a1bbfa0

Browse files
kripkenbelraquib
authored andcommitted
Replace musl seek syscall with wasi fd_seek (emscripten-core#9555)
Update wasi to use the new whence constants, see WebAssembly/WASI#106
1 parent 648786a commit a1bbfa0

27 files changed

+99
-55
lines changed

src/library_fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ mergeInto(LibraryManager.library, {
11171117
if (!stream.seekable || !stream.stream_ops.llseek) {
11181118
throw new FS.ErrnoError({{{ cDefine('ESPIPE') }}});
11191119
}
1120-
if (whence != 0 /* SEEK_SET */ && whence != 1 /* SEEK_CUR */ && whence != 2 /* SEEK_END */) {
1120+
if (whence != {{{ cDefine('SEEK_SET') }}} && whence != {{{ cDefine('SEEK_CUR') }}} && whence != {{{ cDefine('SEEK_END') }}}) {
11211121
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
11221122
}
11231123
stream.position = stream.stream_ops.llseek(stream, offset, whence);

src/library_lz4.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ mergeInto(LibraryManager.library, {
165165
},
166166
llseek: function (stream, offset, whence) {
167167
var position = offset;
168-
if (whence === 1) { // SEEK_CUR.
168+
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
169169
position += stream.position;
170-
} else if (whence === 2) { // SEEK_END.
170+
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
171171
if (FS.isFile(stream.node.mode)) {
172172
position += stream.node.size;
173173
}

src/library_memfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ mergeInto(LibraryManager.library, {
324324

325325
llseek: function(stream, offset, whence) {
326326
var position = offset;
327-
if (whence === 1) { // SEEK_CUR.
327+
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
328328
position += stream.position;
329-
} else if (whence === 2) { // SEEK_END.
329+
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
330330
if (FS.isFile(stream.node.mode)) {
331331
position += stream.node.usedBytes;
332332
}

src/library_nodefs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ mergeInto(LibraryManager.library, {
269269
},
270270
llseek: function (stream, offset, whence) {
271271
var position = offset;
272-
if (whence === 1) { // SEEK_CUR.
272+
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
273273
position += stream.position;
274-
} else if (whence === 2) { // SEEK_END.
274+
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
275275
if (FS.isFile(stream.node.mode)) {
276276
try {
277277
var stat = fs.fstatSync(stream.nfd);

src/library_noderawfs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ mergeInto(LibraryManager.library, {
7070
return VFS.llseek(stream, offset, whence);
7171
}
7272
var position = offset;
73-
if (whence === 1) { // SEEK_CUR.
73+
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
7474
position += stream.position;
75-
} else if (whence === 2) { // SEEK_END.
75+
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
7676
position += fs.fstatSync(stream.nfd).size;
77-
} else if (whence !== 0) { // SEEK_SET.
77+
} else if (whence !== {{{ cDefine('SEEK_SET') }}}) {
7878
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
7979
}
8080

src/library_proxyfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ mergeInto(LibraryManager.library, {
193193
},
194194
llseek: function (stream, offset, whence) {
195195
var position = offset;
196-
if (whence === 1) { // SEEK_CUR.
196+
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
197197
position += stream.position;
198-
} else if (whence === 2) { // SEEK_END.
198+
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
199199
if (FS.isFile(stream.node.mode)) {
200200
try {
201201
var stat = stream.node.mount.opts.fs.fstat(stream.nfd);

src/library_syscall.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -807,29 +807,6 @@ var SyscallsLibrary = {
807807
FS.chdir(stream.path);
808808
return 0;
809809
},
810-
__syscall140: function(which, varargs) { // llseek
811-
var stream = SYSCALLS.getStreamFromFD(), offset_high = SYSCALLS.get(), offset_low = SYSCALLS.get(), result = SYSCALLS.get(), whence = SYSCALLS.get();
812-
#if SYSCALLS_REQUIRE_FILESYSTEM
813-
var HIGH_OFFSET = 0x100000000; // 2^32
814-
// use an unsigned operator on low and shift high by 32-bits
815-
var offset = offset_high * HIGH_OFFSET + (offset_low >>> 0);
816-
817-
var DOUBLE_LIMIT = 0x20000000000000; // 2^53
818-
// we also check for equality since DOUBLE_LIMIT + 1 == DOUBLE_LIMIT
819-
if (offset <= -DOUBLE_LIMIT || offset >= DOUBLE_LIMIT) {
820-
return -{{{ cDefine('EOVERFLOW') }}};
821-
}
822-
823-
FS.llseek(stream, offset, whence);
824-
{{{ makeSetValue('result', '0', 'stream.position', 'i64') }}};
825-
if (stream.getdents && offset === 0 && whence === {{{ cDefine('SEEK_SET') }}}) stream.getdents = null; // reset readdir state
826-
#else
827-
#if ASSERTIONS
828-
abort('it should not be possible to operate on streams when !SYSCALLS_REQUIRE_FILESYSTEM');
829-
#endif
830-
#endif
831-
return 0;
832-
},
833810
__syscall142: function(which, varargs) { // newselect
834811
// readfds are supported,
835812
// writefds checks socket open status
@@ -1469,6 +1446,29 @@ var SyscallsLibrary = {
14691446
{{{ makeSetValue('pnum', 0, 'num', 'i32') }}}
14701447
return 0;
14711448
},
1449+
fd_seek: function(fd, offset_low, offset_high, whence, newOffset) {
1450+
#if SYSCALLS_REQUIRE_FILESYSTEM
1451+
var stream = SYSCALLS.getStreamFromFD(fd);
1452+
var HIGH_OFFSET = 0x100000000; // 2^32
1453+
// use an unsigned operator on low and shift high by 32-bits
1454+
var offset = offset_high * HIGH_OFFSET + (offset_low >>> 0);
1455+
1456+
var DOUBLE_LIMIT = 0x20000000000000; // 2^53
1457+
// we also check for equality since DOUBLE_LIMIT + 1 == DOUBLE_LIMIT
1458+
if (offset <= -DOUBLE_LIMIT || offset >= DOUBLE_LIMIT) {
1459+
return -{{{ cDefine('EOVERFLOW') }}};
1460+
}
1461+
1462+
FS.llseek(stream, offset, whence);
1463+
{{{ makeSetValue('newOffset', '0', 'stream.position', 'i64') }}};
1464+
if (stream.getdents && offset === 0 && whence === {{{ cDefine('SEEK_SET') }}}) stream.getdents = null; // reset readdir state
1465+
#else
1466+
#if ASSERTIONS
1467+
abort('it should not be possible to operate on streams when !SYSCALLS_REQUIRE_FILESYSTEM');
1468+
#endif
1469+
#endif
1470+
return 0;
1471+
},
14721472
};
14731473

14741474
if (SYSCALL_DEBUG) {
@@ -1828,6 +1828,7 @@ var WASI_SYSCALLS = set([
18281828
'fd_write',
18291829
'fd_close',
18301830
'fd_read',
1831+
'fd_seek',
18311832
]);
18321833

18331834
// Fallback for cases where the wasi_unstable.name prefixing fails,

src/library_workerfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ mergeInto(LibraryManager.library, {
143143
},
144144
llseek: function (stream, offset, whence) {
145145
var position = offset;
146-
if (whence === 1) { // SEEK_CUR.
146+
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
147147
position += stream.position;
148-
} else if (whence === 2) { // SEEK_END.
148+
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
149149
if (FS.isFile(stream.node.mode)) {
150150
position += stream.node.size;
151151
}

system/include/libc/fcntl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef _FCNTL_H
22
#define _FCNTL_H
33

4+
#ifdef __EMSCRIPTEN__
5+
#include <wasi/wasi.h>
6+
#endif
7+
48
#ifdef __cplusplus
59
extern "C" {
610
#endif
@@ -72,9 +76,15 @@ int posix_fallocate(int, off_t, off_t);
7276
#undef SEEK_SET
7377
#undef SEEK_CUR
7478
#undef SEEK_END
79+
#ifdef __EMSCRIPTEN__
80+
#define SEEK_SET __WASI_WHENCE_SET
81+
#define SEEK_CUR __WASI_WHENCE_CUR
82+
#define SEEK_END __WASI_WHENCE_END
83+
#else
7584
#define SEEK_SET 0
7685
#define SEEK_CUR 1
7786
#define SEEK_END 2
87+
#endif // EMSCRIPTEN
7888

7989
#ifndef S_IRUSR
8090
#define S_ISUID 04000

system/include/libc/stdio.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef _STDIO_H
22
#define _STDIO_H
33

4+
#ifdef __EMSCRIPTEN__
5+
#include <wasi/wasi.h>
6+
#endif
7+
48
#ifdef __cplusplus
59
extern "C" {
610
#endif
@@ -33,9 +37,15 @@ extern "C" {
3337
#undef SEEK_SET
3438
#undef SEEK_CUR
3539
#undef SEEK_END
40+
#ifdef __EMSCRIPTEN__
41+
#define SEEK_SET __WASI_WHENCE_SET
42+
#define SEEK_CUR __WASI_WHENCE_CUR
43+
#define SEEK_END __WASI_WHENCE_END
44+
#else
3645
#define SEEK_SET 0
3746
#define SEEK_CUR 1
3847
#define SEEK_END 2
48+
#endif // EMSCRIPTEN
3949

4050
#define _IOFBF 0
4151
#define _IOLBF 1

0 commit comments

Comments
 (0)