Skip to content

Commit b78271c

Browse files
authored
Improve support for FILESYSTEM=0. NFC (#16768)
Some of these syscalls were not respsecting this flag. Also, add `fd_fdstat_get` to the list of syscalls which can be referenced without cuasing filesystem code to be included (printf uses this indirectrly via isatty).
1 parent 54675ba commit b78271c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

emscripten.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def optimize_syscalls(declares):
8989
'fd_seek',
9090
'fd_write',
9191
'fd_close',
92+
'fd_fdstat_get',
9293
}):
9394
if DEBUG:
9495
logger.debug('very limited syscalls (%s) so disabling full filesystem support', ', '.join(str(s) for s in syscalls))

src/library_wasi.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ var WasiLibrary = {
219219
},
220220

221221
fd_pwrite: function(fd, iov, iovcnt, {{{ defineI64Param('offset') }}}, pnum) {
222+
#if SYSCALLS_REQUIRE_FILESYSTEM
222223
{{{ receiveI64ParamAsI32s('offset') }}}
223224
var stream = SYSCALLS.getStreamFromFD(fd)
224225
#if ASSERTIONS
@@ -227,34 +228,49 @@ var WasiLibrary = {
227228
var num = SYSCALLS.doWritev(stream, iov, iovcnt, offset_low);
228229
{{{ makeSetValue('pnum', 0, 'num', 'i32') }}};
229230
return 0;
231+
#elif ASSERTIONS
232+
abort('fd_pwrite called without SYSCALLS_REQUIRE_FILESYSTEM');
233+
#else
234+
return {{{ cDefine('ENOSYS') }}};
235+
#endif
230236
},
231237

232238
fd_close__sig: 'ii',
233239
fd_close: function(fd) {
234240
#if SYSCALLS_REQUIRE_FILESYSTEM
235241
var stream = SYSCALLS.getStreamFromFD(fd);
236242
FS.close(stream);
243+
return 0;
237244
#elif PROXY_POSIX_SOCKETS
238245
// close() is a tricky function because it can be used to close both regular file descriptors
239246
// and POSIX network socket handles, hence an implementation would need to track for each
240247
// file descriptor which kind of item it is. To simplify, when using PROXY_POSIX_SOCKETS
241248
// option, use shutdown() to close a socket, and this function should behave like a no-op.
242249
warnOnce('To close sockets with PROXY_POSIX_SOCKETS bridge, prefer to use the function shutdown() that is proxied, instead of close()')
250+
return 0;
243251
#elif ASSERTIONS
244-
abort('it should not be possible to operate on streams when !SYSCALLS_REQUIRE_FILESYSTEM');
252+
abort('fd_close called without SYSCALLS_REQUIRE_FILESYSTEM');
253+
#else
254+
return {{{ cDefine('ENOSYS') }}};
245255
#endif // SYSCALLS_REQUIRE_FILESYSTEM
246-
return 0;
247256
},
248257

249258
fd_read__sig: 'iiiii',
250259
fd_read: function(fd, iov, iovcnt, pnum) {
260+
#if SYSCALLS_REQUIRE_FILESYSTEM
251261
var stream = SYSCALLS.getStreamFromFD(fd);
252262
var num = SYSCALLS.doReadv(stream, iov, iovcnt);
253263
{{{ makeSetValue('pnum', 0, 'num', 'i32') }}};
254264
return 0;
265+
#elif ASSERTIONS
266+
abort('fd_read called without SYSCALLS_REQUIRE_FILESYSTEM');
267+
#else
268+
return {{{ cDefine('ENOSYS') }}};
269+
#endif // SYSCALLS_REQUIRE_FILESYSTEM
255270
},
256271

257272
fd_pread: function(fd, iov, iovcnt, {{{ defineI64Param('offset') }}}, pnum) {
273+
#if SYSCALLS_REQUIRE_FILESYSTEM
258274
{{{ receiveI64ParamAsI32s('offset') }}}
259275
#if ASSERTIONS
260276
assert(!offset_high, 'offsets over 2^32 not yet supported');
@@ -263,9 +279,15 @@ var WasiLibrary = {
263279
var num = SYSCALLS.doReadv(stream, iov, iovcnt, offset_low);
264280
{{{ makeSetValue('pnum', 0, 'num', 'i32') }}};
265281
return 0;
282+
#elif ASSERTIONS
283+
abort('fd_pread called without SYSCALLS_REQUIRE_FILESYSTEM');
284+
#else
285+
return {{{ cDefine('ENOSYS') }}};
286+
#endif
266287
},
267288

268289
fd_seek: function(fd, {{{ defineI64Param('offset') }}}, whence, newOffset) {
290+
#if SYSCALLS_REQUIRE_FILESYSTEM
269291
{{{ receiveI64ParamAsI32s('offset') }}}
270292
var stream = SYSCALLS.getStreamFromFD(fd);
271293
var HIGH_OFFSET = 0x100000000; // 2^32
@@ -275,14 +297,18 @@ var WasiLibrary = {
275297
var DOUBLE_LIMIT = 0x20000000000000; // 2^53
276298
// we also check for equality since DOUBLE_LIMIT + 1 == DOUBLE_LIMIT
277299
if (offset <= -DOUBLE_LIMIT || offset >= DOUBLE_LIMIT) {
278-
return -{{{ cDefine('EOVERFLOW') }}};
300+
return {{{ cDefine('EOVERFLOW') }}};
279301
}
280302

281303
FS.llseek(stream, offset, whence);
282304
{{{ makeSetValue('newOffset', '0', 'stream.position', 'i64') }}};
283305
if (stream.getdents && offset === 0 && whence === {{{ cDefine('SEEK_SET') }}}) stream.getdents = null; // reset readdir state
284306
return 0;
307+
#else
308+
return {{{ cDefine('ESPIPE') }}};
309+
#endif
285310
},
311+
286312
fd_fdstat_get__sig: 'iii',
287313
fd_fdstat_get: function(fd, pbuf) {
288314
#if SYSCALLS_REQUIRE_FILESYSTEM
@@ -306,6 +332,7 @@ var WasiLibrary = {
306332

307333
fd_sync__sig: 'ii',
308334
fd_sync: function(fd) {
335+
#if SYSCALLS_REQUIRE_FILESYSTEM
309336
var stream = SYSCALLS.getStreamFromFD(fd);
310337
#if ASYNCIFY
311338
return Asyncify.handleSleep(function(wakeUp) {
@@ -329,6 +356,11 @@ var WasiLibrary = {
329356
}
330357
return 0; // we can't do anything synchronously; the in-memory FS is already synced to
331358
#endif // ASYNCIFY
359+
#elif ASSERTIONS
360+
abort('fd_sync called without SYSCALLS_REQUIRE_FILESYSTEM');
361+
#else
362+
return {{{ cDefine('ENOSYS') }}};
363+
#endif // SYSCALLS_REQUIRE_FILESYSTEM
332364
},
333365
};
334366

0 commit comments

Comments
 (0)