Skip to content

Commit c2dbfa9

Browse files
committed
Define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS in compiler-rt
In defining this I noticed that musl will also choose to use these canonical syscalls when the alternative is not defined. This allows us to remove a bunch of duplicate syscalls and their implementation.
1 parent e05a26f commit c2dbfa9

File tree

10 files changed

+40
-70
lines changed

10 files changed

+40
-70
lines changed

src/library_syscall.js

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,6 @@ var SyscallsLibrary = {
300300
return 0;
301301
},
302302

303-
__syscall_open: function(path, flags, varargs) {
304-
var pathname = SYSCALLS.getStr(path);
305-
var mode = varargs ? SYSCALLS.get() : 0;
306-
var stream = FS.open(pathname, flags, mode);
307-
return stream.fd;
308-
},
309-
__syscall_unlink: function(path) {
310-
path = SYSCALLS.getStr(path);
311-
FS.unlink(path);
312-
return 0;
313-
},
314303
__syscall_chdir: function(path) {
315304
path = SYSCALLS.getStr(path);
316305
FS.chdir(path);
@@ -329,12 +318,6 @@ var SyscallsLibrary = {
329318
path = SYSCALLS.getStr(path);
330319
return SYSCALLS.doAccess(path, amode);
331320
},
332-
__syscall_rename: function(old_path, new_path) {
333-
old_path = SYSCALLS.getStr(old_path);
334-
new_path = SYSCALLS.getStr(new_path);
335-
FS.rename(old_path, new_path);
336-
return 0;
337-
},
338321
__syscall_mkdir: function(path, mode) {
339322
path = SYSCALLS.getStr(path);
340323
return SYSCALLS.doMkdir(path, mode);
@@ -418,21 +401,12 @@ var SyscallsLibrary = {
418401
}
419402
#endif // SYSCALLS_REQUIRE_FILESYSTEM
420403
},
421-
__syscall_dup2: function(oldfd, suggestFD) {
422-
var old = SYSCALLS.getStreamFromFD(oldfd);
423-
if (old.fd === suggestFD) return suggestFD;
424-
return SYSCALLS.doDup(old.path, old.flags, suggestFD);
425-
},
426404
__syscall_symlink: function(target, linkpath) {
427405
target = SYSCALLS.getStr(target);
428406
linkpath = SYSCALLS.getStr(linkpath);
429407
FS.symlink(target, linkpath);
430408
return 0;
431409
},
432-
__syscall_readlink: function(path, buf, bufsize) {
433-
path = SYSCALLS.getStr(path);
434-
return SYSCALLS.doReadlink(path, buf, bufsize);
435-
},
436410
__syscall_munmap__deps: ['$syscallMunmap'],
437411
__syscall_munmap: function(addr, len) {
438412
return syscallMunmap(addr, len);
@@ -982,9 +956,6 @@ var SyscallsLibrary = {
982956
return 0; // your advice is important to us (but we can't use it)
983957
},
984958
__syscall_openat: function(dirfd, path, flags, varargs) {
985-
#if SYSCALL_DEBUG
986-
err('warning: untested syscall');
987-
#endif
988959
path = SYSCALLS.getStr(path);
989960
path = SYSCALLS.calculateAt(dirfd, path);
990961
var mode = varargs ? SYSCALLS.get() : 0;
@@ -1018,7 +989,7 @@ var SyscallsLibrary = {
1018989
FS.chown(path, owner, group);
1019990
return 0;
1020991
},
1021-
__syscall_fstatat64: function(dirfd, path, buf, flags) {
992+
__syscall_newfstatat: function(dirfd, path, buf, flags) {
1022993
path = SYSCALLS.getStr(path);
1023994
var nofollow = flags & {{{ cDefine('AT_SYMLINK_NOFOLLOW') }}};
1024995
var allowEmpty = flags & {{{ cDefine('AT_EMPTY_PATH') }}};
@@ -1042,9 +1013,6 @@ var SyscallsLibrary = {
10421013
return 0;
10431014
},
10441015
__syscall_renameat: function(olddirfd, oldpath, newdirfd, newpath) {
1045-
#if SYSCALL_DEBUG
1046-
err('warning: untested syscall');
1047-
#endif
10481016
oldpath = SYSCALLS.getStr(oldpath);
10491017
newpath = SYSCALLS.getStr(newpath);
10501018
oldpath = SYSCALLS.calculateAt(olddirfd, oldpath);
@@ -1066,9 +1034,6 @@ var SyscallsLibrary = {
10661034
return 0;
10671035
},
10681036
__syscall_readlinkat: function(dirfd, path, buf, bufsize) {
1069-
#if SYSCALL_DEBUG
1070-
err('warning: untested syscall');
1071-
#endif
10721037
path = SYSCALLS.getStr(path);
10731038
path = SYSCALLS.calculateAt(dirfd, path);
10741039
return SYSCALLS.doReadlink(path, buf, bufsize);
@@ -1120,9 +1085,6 @@ var SyscallsLibrary = {
11201085
return 0;
11211086
},
11221087
__syscall_dup3: function(fd, suggestFD, flags) {
1123-
#if SYSCALL_DEBUG
1124-
err('warning: untested syscall: dup3');
1125-
#endif
11261088
var old = SYSCALLS.getStreamFromFD(fd);
11271089
#if ASSERTIONS
11281090
assert(!flags);

system/lib/compiler-rt/lib/sanitizer_common/sanitizer_platform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@
286286
#define SANITIZER_SIGN_EXTENDED_ADDRESSES 0
287287
#endif
288288

289+
// Emscripten emulates the canonical linux syscall set.
290+
#if !defined SANITIZER_USES_CANONICAL_LINUX_SYSCALLS && SANITIZER_EMSCRIPTEN
291+
# define SANITIZER_USES_CANONICAL_LINUX_SYSCALLS 1
292+
#endif
293+
289294
// The AArch64 and RISC-V linux ports use the canonical syscall set as
290295
// mandated by the upstream linux community for all new ports. Other ports
291296
// may still use legacy syscalls.

system/lib/fetch/asmfs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ static long open(const char* pathname, int flags, int mode) {
10541054
return (long)desc;
10551055
}
10561056

1057-
long __syscall_open(long path, long flags, ...)
1057+
long __syscall_openat(long dirfd, long path, long flags, ...)
10581058
{
10591059
va_list vl;
10601060
va_start(vl, flags);
@@ -1206,7 +1206,7 @@ long __syscall_link(long oldpath, long newpath)
12061206
RETURN_ERRNO(ENOTSUP, "TODO: link() is a stub and not yet implemented in ASMFS");
12071207
}
12081208

1209-
long __syscall_unlink(long path)
1209+
long __syscall_unlinkat(long dirfd, long path, long flags)
12101210
{
12111211
const char* pathname = (const char *)path;
12121212
#ifdef ASMFS_DEBUG

system/lib/libc/musl/arch/emscripten/bits/syscall.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#define SYS_open __syscall_open
21
#define SYS_link __syscall_link
3-
#define SYS_unlink __syscall_unlink
42
#define SYS_chdir __syscall_chdir
53
#define SYS_mknod __syscall_mknod
64
#define SYS_chmod __syscall_chmod
@@ -9,7 +7,6 @@
97
#define SYS_access __syscall_access
108
#define SYS_nice __syscall_nice
119
#define SYS_sync __syscall_sync
12-
#define SYS_rename __syscall_rename
1310
#define SYS_mkdir __syscall_mkdir
1411
#define SYS_rmdir __syscall_rmdir
1512
#define SYS_dup __syscall_dup
@@ -18,13 +15,11 @@
1815
#define SYS_ioctl __syscall_ioctl
1916
#define SYS_setpgid __syscall_setpgid
2017
#define SYS_umask __syscall_umask
21-
#define SYS_dup2 __syscall_dup2
2218
#define SYS_getppid __syscall_getppid
2319
#define SYS_setsid __syscall_setsid
2420
#define SYS_setrlimit __syscall_setrlimit
2521
#define SYS_getrusage __syscall_getrusage
2622
#define SYS_symlink __syscall_symlink
27-
#define SYS_readlink __syscall_readlink
2823
#define SYS_munmap __syscall_munmap
2924
#define SYS_fchmod __syscall_fchmod
3025
#define SYS_getpriority __syscall_getpriority
@@ -76,7 +71,7 @@
7671
#define SYS_mkdirat __syscall_mkdirat
7772
#define SYS_mknodat __syscall_mknodat
7873
#define SYS_fchownat __syscall_fchownat
79-
#define SYS_fstatat64 __syscall_fstatat64
74+
#define SYS_newfstatat __syscall_newfstatat
8075
#define SYS_unlinkat __syscall_unlinkat
8176
#define SYS_renameat __syscall_renameat
8277
#define SYS_linkat __syscall_linkat

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
extern "C" {
1212
#endif
1313

14-
long __syscall_open(long path, long flags, ...); // mode is optional
1514
long __syscall_link(long oldpath, long newpath);
16-
long __syscall_unlink(long path);
1715
long __syscall_chdir(long path);
1816
long __syscall_mknod(long path, long mode, long dev);
1917
long __syscall_chmod(long path, long mode);
@@ -22,7 +20,6 @@ long __syscall_pause(void);
2220
long __syscall_access(long path, long amode);
2321
long __syscall_nice(long inc);
2422
long __syscall_sync(void);
25-
long __syscall_rename(long old_path, long new_path);
2623
long __syscall_mkdir(long path, long mode);
2724
long __syscall_rmdir(long path);
2825
long __syscall_dup(long fd);
@@ -31,14 +28,12 @@ long __syscall_acct(long filename);
3128
long __syscall_ioctl(long fd, long request, ...);
3229
long __syscall_setpgid(long pid, long gpid);
3330
long __syscall_umask(long mask);
34-
long __syscall_dup2(long oldfd, long newfd);
3531
long __syscall_getppid(void);
3632
long __syscall_getpgrp(void);
3733
long __syscall_setsid(void);
3834
long __syscall_setrlimit(long resource, long limit);
3935
long __syscall_getrusage(long who, long usage);
4036
long __syscall_symlink(long target, long linkpath);
41-
long __syscall_readlink(long path, long buf, long bufsize);
4237
long __syscall_munmap(long addr, long len);
4338
long __syscall_fchmod(long fd, long mode);
4439
long __syscall_getpriority(long which, long who);
@@ -93,11 +88,11 @@ long __syscall_fcntl64(long fd, long cmd, ...);
9388
long __syscall_statfs64(long path, long size, long buf);
9489
long __syscall_fstatfs64(long fd, long size, long buf);
9590
long __syscall_fadvise64_64(long fd, long low, long high, long low2, long high2, long advice);
96-
long __syscall_openat(long dirfd, long path, long flags, ...);
91+
long __syscall_openat(long dirfd, long path, long flags, ...); // mode is optional
9792
long __syscall_mkdirat(long dirfd, long path, long mode);
9893
long __syscall_mknodat(long dirfd, long path, long mode, long dev);
9994
long __syscall_fchownat(long dirfd, long path, long owner, long group, long flags);
100-
long __syscall_fstatat64(long dirfd, long path, long buf, long flags);
95+
long __syscall_newfstatat(long dirfd, long path, long buf, long flags);
10196
long __syscall_unlinkat(long dirfd, long path, long flags);
10297
long __syscall_renameat(long olddirfd, long oldpath, long newdirfd, long newpath);
10398
long __syscall_linkat(long olddirfd, long oldpath, long newdirfd, long newpath, long flags);

system/lib/libc/musl/src/internal/syscall.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ long __syscall_ret(unsigned long),
257257
#define __sys_open_cp3(x,pn,fl,mo) __syscall_cp4(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE, mo)
258258
#endif
259259
#else // __EMSCRIPTEN__
260-
#define __sys_open2(x,pn,fl) __syscall_open(__scc(pn), __scc((fl)|O_LARGEFILE))
261-
#define __sys_open3(x,pn,fl,mo) __syscall_open(__scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo))
262-
#define __sys_open_cp2(x,pn,fl) __syscall_open(__scc(pn), __scc((fl)|O_LARGEFILE))
263-
#define __sys_open_cp3(x,pn,fl,mo) __syscall_open(__scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo))
260+
#define __sys_open2(x,pn,fl) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE))
261+
#define __sys_open3(x,pn,fl,mo) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo))
262+
#define __sys_open_cp2(x,pn,fl) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE))
263+
#define __sys_open_cp3(x,pn,fl,mo) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo))
264264
#endif
265265
#define __sys_open(...) __SYSCALL_DISP(__sys_open,,__VA_ARGS__)
266266
#define sys_open(...) __syscall_ret(__sys_open(__VA_ARGS__))

system/lib/libc/musl/src/unistd/dup2.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#ifdef __EMSCRIPTEN__
2-
#include <stropts.h>
3-
#endif
41
#include <unistd.h>
52
#include <errno.h>
63
#include <fcntl.h>
@@ -19,9 +16,11 @@ int dup2(int old, int new)
1916
r = __syscall(SYS_fcntl, old, F_GETFD);
2017
#endif
2118
if (r >= 0) return old;
22-
} else {
23-
while ((r=__syscall(SYS_dup3, old, new, 0))==-EBUSY);
2419
}
20+
// __EMSCRIPTEN__ removed else block here to avoid returning
21+
// uninitilized `r` when old and new are equal but old is
22+
// invalid.
23+
while ((r=__syscall(SYS_dup3, old, new, 0))==-EBUSY);
2524
#endif
2625
return __syscall_ret(r);
2726
}

system/lib/libc/musl/src/unistd/dup3.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ int __dup3(int old, int new, int flags)
1515
if (r!=-ENOSYS) return __syscall_ret(r);
1616
}
1717
while ((r=__syscall(SYS_dup2, old, new))==-EBUSY);
18-
#ifndef __EMSCRIPTEN__ // CLOEXEC makes no sense for a single process
19-
if (flags & O_CLOEXEC) __syscall(SYS_fcntl, new, F_SETFD, FD_CLOEXEC);
20-
#endif
2118
#else
2219
while ((r=__syscall(SYS_dup3, old, new, flags))==-EBUSY);
2320
#endif

system/lib/standalone/standalone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ long __syscall_mmap2(long addr, long len, long prot, long flags, long fd, long o
7070
// corner case error checking; everything else is not permitted.
7171
// TODO: full file support for WASI, or an option for it
7272
// open()
73-
long __syscall_open(const char* path, long flags, ...) {
73+
long __syscall_openat(int dirfd, const char* path, long flags, ...) {
7474
if (!strcmp(path, "/dev/stdin")) return STDIN_FILENO;
7575
if (!strcmp(path, "/dev/stdout")) return STDOUT_FILENO;
7676
if (!strcmp(path, "/dev/stderr")) return STDERR_FILENO;

system/lib/wasmfs/syscalls.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern "C" {
2323

2424
using namespace wasmfs;
2525

26-
long __syscall_dup2(long oldfd, long newfd) {
26+
long __syscall_dup3(long oldfd, long newfd, long flags) {
2727
auto fileTable = wasmFS.getLockedFileTable();
2828

2929
auto oldOpenFile = fileTable[oldfd];
@@ -38,7 +38,7 @@ long __syscall_dup2(long oldfd, long newfd) {
3838
}
3939

4040
if (oldfd == newfd) {
41-
return oldfd;
41+
return -EINVAL;
4242
}
4343

4444
// If the file descriptor newfd was previously open, it will just be
@@ -282,7 +282,9 @@ long __syscall_fstat64(long fd, long buf) {
282282
return __WASI_ERRNO_SUCCESS;
283283
}
284284

285-
__wasi_fd_t __syscall_open(long pathname, long flags, long mode) {
285+
__wasi_fd_t __syscall_openat(long dirfd, long pathname, long flags, long mode) {
286+
// TODO: implement fd-relative open
287+
assert(dirfd == AT_FDCWD);
286288
int accessMode = (flags & O_ACCMODE);
287289
bool canWrite = false;
288290

@@ -426,4 +428,19 @@ __wasi_errno_t __wasi_fd_seek(__wasi_fd_t fd,
426428

427429
return __WASI_ERRNO_SUCCESS;
428430
}
431+
432+
__wasi_errno_t __wasi_fd_fdstat_get(__wasi_fd_t fd, __wasi_fdstat_t *stat) {
433+
auto openFile = wasmFS.getLockedFileTable()[fd];
434+
if (!openFile) {
435+
return __WASI_ERRNO_BADF;
436+
}
437+
438+
if (openFile.locked().getFile()->is<Directory>()) {
439+
stat->fs_filetype = __WASI_FILETYPE_DIRECTORY;
440+
} else {
441+
stat->fs_filetype = __WASI_FILETYPE_REGULAR_FILE;
442+
}
443+
return __WASI_ERRNO_SUCCESS;
444+
}
445+
429446
}

0 commit comments

Comments
 (0)