# Copyright 2017 syzkaller project authors. All rights reserved.
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

include <sys/types.h>
include <sys/ioctl.h>
include <sys/mman.h>
include <sys/stat.h>
include <fcntl.h>
include <unistd.h>
include <sys/time.h>
include <dirent.h>
include <poll.h>
include <sys/select.h>
include <sys/param.h>
include <sys/resource.h>
include <time.h>
include <signal.h>
include <sys/wait.h>
include <sys/ptrace.h>
include <sys/sched.h>
include <sys/signal.h>
include <sys/spawn.h>
include <sys/module.h>
include <sys/swap.h>
include <sys/ras.h>

syz_execute_func(text ptr[in, text[target]]) (disabled)

pipe(pipefd ptr[out, pipefd])

compat_12_stat12(file ptr[in, filename], ub ptr[out, stat12])
compat_30___stat13(path ptr[in, filename], ub ptr[out, stat13])
compat_50___stat30(path ptr[in, filename], ub ptr[out, stat30])
compat_43_stat43(file ptr[in, filename], ua ptr[out, stat43])
__stat50(file ptr[in, filename], ua ptr[out, stat])

compat_12_lstat12(file ptr[in, filename], ub ptr[out, stat12])
compat_30___lstat13(path ptr[in, filename], ub ptr[out, stat13])
compat_50___lstat30(path ptr[in, filename], ub ptr[out, stat30])
compat_43_lstat43(file ptr[in, filename], ua ptr[out, stat43])
__lstat50(file ptr[in, filename], ua ptr[out, stat])

compat_12_fstat12(fd fd, sb ptr[out, stat12])
compat_30___fstat13(fd fd, sb ptr[out, stat13])
compat_50___fstat30(fd fd, sb ptr[out, stat30])
compat_43_fstat43(fd fd, sb ptr[out, stat43])
__fstat50(fd fd, sb ptr[out, stat])

compat_20_statfs(path ptr[in, filename], buf ptr[out, statfs12])
compat_20_fstatfs(fd fd, buf ptr[out, statfs12])
fstatat(fd fd, path ptr[in, filename], buf ptr[out, stat], flag flags[fstatat_flags])
profil(samples buffer[inout], size len[samples], offt intptr, scale int32)

compat_43_ogetdirentries(fd fd, buf buffer[out], count int32, basep ptr[in, intptr])
compat_12_getdirentries(fd fd, buf buffer[out], count int32, basep ptr[in, intptr])

compat_09_ouname(name ptr[out, outsname])

compat_12_msync(addr vma, len int32)
__msync13(addr vma, len int32, f flags[msync_flags])

poll(fds ptr[in, array[pollfd]], nfds len[fds], timeout int32)
compat_50_select(nd len[inp], inp ptr[inout, fd_set], ou ptr[inout, fd_set], ex ptr[inout, fd_set], tv ptr[inout, timeval50])
__select50(n len[inp], inp ptr[inout, fd_set], ou ptr[inout, fd_set], ex ptr[inout, fd_set], tv ptr[inout, timeval])

mincore(addr vma, size len[addr], vec buffer[out])
minherit(addr vma, len len[addr], inherit flags[minherit_flags])

fcntl$dupfd(fd fd, cmd flags[fcntl_dupfd], arg fd) fd
fcntl$getflags(fd fd, cmd flags[fcntl_getflags])
fcntl$setflags(fd fd, cmd const[F_SETFD], flags flags[fcntl_flags])
fcntl$setstatus(fd fd, cmd const[F_SETFL], flags flags[fcntl_status])
fcntl$lock(fd fd, cmd flags[fcntl_lock], lock ptr[in, flock])
fcntl$getown(fd fd, cmd const[F_GETOWN]) pid
fcntl$setown(fd fd, cmd const[F_SETOWN], pid pid)

ioctl$FIOSEEKDATA(fd fd, cmd const[FIOSEEKDATA], arg ptr[inout, fileoff[int64]])
ioctl$FIOSEEKHOLE(fd fd, cmd const[FIOSEEKHOLE], arg ptr[inout, fileoff[int64]])
ioctl$FIONREAD(fd fd, cmd const[FIONREAD], arg ptr[out, int32])
ioctl$FIONBIO(fd fd, cmd const[FIONBIO], arg ptr[in, int32])
ioctl$FIOASYNC(fd fd, cmd const[FIOASYNC], arg ptr[in, int32])
ioctl$FIOSETOWN(fd fd, cmd const[FIOSETOWN], arg ptr[in, int32])
ioctl$FIOGETOWN(fd fd, cmd const[FIOGETOWN], arg ptr[out, int32])
ioctl$OFIOGETBMAP(fd fd, cmd const[OFIOGETBMAP], arg ptr[inout, int32])
ioctl$FIOGETBMAP(fd fd, cmd const[FIOGETBMAP], arg ptr[inout, int64])
ioctl$FIONWRITE(fd fd, cmd const[FIONWRITE], arg ptr[out, int32])
ioctl$FIONSPACE(fd fd, cmd const[FIONSPACE], arg ptr[out, int32])

compat_50_mknod(file ptr[in, filename], mode flags[mknod_mode], dev int32)
mknod(file ptr[in, filename], mode flags[mknod_mode], dev dev)
mknod$loop(file ptr[in, filename], mode flags[mknod_mode], dev proc[1792, 2])
mknodat(dirfd fd_dir, file ptr[in, filename], mode flags[mknod_mode], dev int32)
umask(mode flags[open_mode])
chmod(file ptr[in, filename], mode flags[open_mode])
lchmod(file ptr[in, filename], mode flags[open_mode])
fchmod(fd fd, mode flags[open_mode])
fchmodat(dirfd fd_dir, file ptr[in, filename], mode flags[open_mode], flags flags[at_flags])
chown(file ptr[in, filename], uid uid, gid gid)
lchown(file ptr[in, filename], uid uid, gid gid)
fchown(fd fd, uid uid, gid gid)
fchownat(dirfd fd_dir, file ptr[in, filename], uid uid, gid gid, flags flags[at_flags])
__posix_chown(path ptr[in, filename], uid uid, gid gid)
__posix_fchown(path ptr[in, filename], uid uid, gid gid)
__posix_lchown(fd fd, uid uid, gid gid)
__posix_rename(from ptr[in, filename], to ptr[in, filename])
__posix_fadvise50(fd fd, pad const[0], offset intptr, len intptr, advice flags[fadvise_flags])

access(pathname ptr[in, filename], mode flags[access_mode])
chflags(file ptr[in, filename], flags flags[chflags_flags])
fchflags(fd fd, flags flags[chflags_flags])
lchflags(file ptr[in, filename], flags flags[chflags_flags])
faccessat(dirfd fd_dir, pathname ptr[in, filename], mode flags[access_mode], flags flags[faccessat_flags])
compat_50_quotactl(path ptr[in, filename], cmd int32, id uid, addr buffer[in])

compat_50_utimes(filename ptr[in, filename], times ptr[in, timeval50])
compat_50_futimes(fd fd, times ptr[in, timeval50])
compat_50_lutimes(link ptr[in, filename], times ptr[in, timeval50])
__utimes50(filename ptr[in, filename], times ptr[in, timeval])
__futimes50(fd fd, times ptr[in, timeval])
__lutimes50(link ptr[in, filename], times ptr[in, timeval])
utimensat(dir fd_dir, pathname ptr[in, filename], times ptr[in, itimerval], flags flags[utimensat_flags])

undelete(path ptr[in, filename])

execve(file ptr[in, filename], argv ptr[in, array[ptr[in, string]]], envp ptr[in, array[ptr[in, string]]])

getgid() gid
getegid() gid
setuid(uid uid)
setgid(gid gid)
seteuid(euid uid)
setegid(egid gid)
getuid() uid
geteuid() uid
setpgid(pid pid, pgid pid)
getpgid(pid pid) pid
getpgrp() pid
getpid() pid
getppid() pid
setreuid(ruid uid, euid uid)
setregid(rgid gid, egid gid)
getgroups(size len[list], list ptr[inout, array[gid]])
setgroups(size len[list], list ptr[in, array[gid]])
__getlogin(buf buffer[out], size len[buf])
getsid(pid pid) pid
#__setlogin(buf buffer[in])		#Unsure about effects on test process
#setsid() pid		#Unsure about effects on test process
setpriority(which flags[priority_which], who pid, prio int32)
getpriority(which flags[priority_which], who pid)

compat_43_ogethostid()
compat_43_osethostid(hostid int32)
compat_09_ogetdomainname(domainname buffer[out], len len[domainname])
compat_09_osetdomainname(domainname buffer[in], len len[domainname])
compat_43_ogethostname(hostname buffer[out], len len[hostname])
compat_43_osethostname(hostname buffer[in], len len[hostname])

link(old ptr[in, filename], new ptr[in, filename])
linkat(oldfd fd_dir, old ptr[in, filename], newfd fd_dir, new ptr[in, filename], flags flags[linkat_flags])
symlinkat(old ptr[in, filename], newfd fd_dir, new ptr[in, filename])
symlink(old ptr[in, filename], new ptr[in, filename])
unlink(path ptr[in, filename])
unlinkat(fd fd_dir, path ptr[in, filename], flags flags[unlinkat_flags])
readlink(path ptr[in, filename], buf buffer[out], siz len[buf])
readlinkat(fd fd_dir, path ptr[in, filename], buf buffer[out], siz len[buf])
rename(old ptr[in, filename], new ptr[in, filename])
renameat(oldfd fd_dir, old ptr[in, filename], newfd fd_dir, new ptr[in, filename])
mkdir(path ptr[in, filename], mode flags[open_mode])
mkdirat(fd fd_dir, path ptr[in, filename], mode flags[open_mode])
rmdir(path ptr[in, filename])
truncate(file ptr[in, filename], pad const[0], len intptr)
compat_43_otruncate(file ptr[in, filename], len intptr)
ftruncate(fd fd, pad const[0], len intptr)
compat_43_oftruncate(fd fd, len intptr)
flock(fd fd, op flags[flock_op])
fsync(fd fd)
fdatasync(fd fd)
sync()
compat_30_getdents(fd fd_dir, ent buffer[out], count int32)
__getdents30(fd fd_dir, ent buffer[out], count int32)
chroot(dir ptr[in, filename])
__getcwd(buf buffer[out], size len[buf])
fchroot(fd fd)
chdir(dir ptr[in, filename])
fchdir(fd fd)
acct(filename ptr[in, filename, opt])

compat_50_getrusage(who flags[rusage_who], usage ptr[out, rusage50])
__getrusage50(who flags[rusage_who], usage ptr[out, rusage])
getrlimit(res flags[rlimit_type], rlp ptr[out, rlimit])
compat_43_ogetrlimit(res flags[rlimit_type], rlp ptr[out, orlimit])
setrlimit(res flags[rlimit_type], rlp ptr[in, rlimit])
compat_43_osetrlimit(res flags[rlimit_type], rlp ptr[in, orlimit])

rasctl(addr buffer[in], len intptr, op flags[rasctl_flags])

swapctl$SWAP_NSWAP(cmd const[SWAP_NSWAP])
swapctl$SWAP_STATS(cmd const[SWAP_STATS], arg ptr[out, swapent], misc int32)
swapctl$SWAP_ON(cmd const[SWAP_ON], arg buffer[in], misc int32)
swapctl$SWAP_OFF(cmd const[SWAP_OFF], arg buffer[in])
swapctl$SWAP_CTL(cmd const[SWAP_CTL], arg buffer[in], misc int32)
swapctl$SWAP_DUMPDEV(cmd const[SWAP_DUMPDEV], arg buffer[in], misc int32)
swapctl$SWAP_GETDUMPDEV(cmd const[SWAP_GETDUMPDEV], arg dev, misc int32)

modctl$MODCTL_LOAD(cmd const[MODCTL_LOAD], arg ptr[in, modctl_load])
modctl$MODCTL_UNLOAD(cmd const[MODCTL_UNLOAD], arg buffer[in])
modctl$MODCTL_STAT(cmd const[MODCTL_STAT], arg ptr[out, iovec_out])
modctl$MODCTL_EXISTS(cmd const[MODCTL_EXISTS], arg int32)

compat_50_clock_gettime(id flags[clock_id], tp ptr[out, timespec50])
compat_50_clock_settime(id flags[clock_id], tp ptr[in, timespec50])
compat_50_clock_getres(id flags[clock_id], res ptr[out, timespec50, opt])
clock_nanosleep(id flags[clock_id], flags flags[timer_flags], rqtp ptr[in, timespec], rmtp ptr[out, timespec, opt])
compat_50_setitimer(which flags[getitimer_which], new ptr[in, itimerval50], old ptr[out, itimerval, opt])
compat_50_getitimer(which flags[getitimer_which], value ptr[out, itimerval50])
compat_50_nanosleep(rqtp ptr[in, timespec50], rmtp ptr[out, timespec50, opt])
__setitimer50(which flags[getitimer_which], new ptr[in, itimerval], old ptr[out, itimerval, opt])
__getitimer50(which flags[getitimer_which], value ptr[out, itimerval])
__clock_gettime50(id flags[clock_id], tp ptr[out, timespec])
__clock_settime50(id flags[clock_id], tp ptr[in, timespec])
__clock_getres50(id flags[clock_id], res ptr[out, timespec, opt])
__nanosleep50(rqtp ptr[in, timespec], rmtp ptr[out, timespec, opt])
compat_50_wait4(pid pid, status ptr[out, int32, opt], options flags[wait_options], ru ptr[out, rusage50, opt])
__wait450(pid pid, status ptr[out, int32, opt], options flags[wait_options], ru ptr[out, rusage, opt])

exit(code intptr)
ptrace(request intptr, pid pid, addr ptr[out, intptr], data intptr)
__clone(flags flags[clone_flags], sp buffer[in]) pid
fork() pid
__vfork14() pid
vfork() pid
posix_spawn(pid pid, path ptr[in, string], file_actions ptr[in, posix_spawn_file_actions], attrp ptr[in, posix_spawnattr], argv ptr[in, array[string]], envp ptr[in, array[string]]) pid

outsname {
	sname	array[int8, 32]
	nname	array[int8, 32]
	release	array[int8, 32]
	version	array[int8, 32]
	mach	array[int8, 32]
}

pollfd {
	fd	fd
	events	flags[pollfd_events, int16]
	revents	const[0, int16]
}

timespec {
	sec	intptr
	nsec	intptr
}

timespec50 {
	sec	int32
	nsec	intptr
}

timeval {
	sec	intptr
	usec	intptr
}

timeval50 {
	sec	intptr
	usec	intptr
}

itimerval {
	interv	timeval
	value	timeval
}

itimerval50 {
	interv	timeval50
	value	timeval50
}

# TODO: fd_set needs to be a separate type
fd_set {
	mask0	int64
	mask1	int64
	mask2	int64
	mask3	int64
	mask4	int64
	mask5	int64
	mask6	int64
	mask7	int64
}

rusage {
	utime	timeval
	stime	timeval
	maxrss	intptr
	ixrss	intptr
	idrss	intptr
	isrss	intptr
	minflt	intptr
	majflt	intptr
	nswap	intptr
	inblock	intptr
	oublock	intptr
	msgsnd	intptr
	msgrcv	intptr
	signals	intptr
	nvcsw	intptr
	nivcsw	intptr
}

rusage50 {
	utime	timeval50
	stime	timeval50
	maxrss	intptr
	ixrss	intptr
	idrss	intptr
	isrss	intptr
	minflt	intptr
	majflt	intptr
	nswap	intptr
	inblock	intptr
	oublock	intptr
	msgsnd	intptr
	msgrcv	intptr
	signals	intptr
	nvcsw	intptr
	nivcsw	intptr
}

rlimit {
	soft	intptr
	hard	intptr
}

orlimit {
	cur	int32
	max	int32
}

flock {
	type	flags[flock_type, int16]
	whence	flags[seek_whence, int16]
	start	intptr
	len	intptr
	pid	pid
}

linger {
	onoff	int32
	linger	int32
}

ucred {
	pid	pid
	uid	uid
	gid	gid
}

posix_spawn_file_actions {
	size	int32
	len	int32
	fae	ptr[in, posix_spawn_file_actions_entry]
}

posix_spawn_file_actions_entry [
	open	posix_spawn_file_actions_entry_t[FAE_OPEN, fd, open_t]
	dup	posix_spawn_file_actions_entry_t[FAE_DUP2, fd, dup2_t]
	close	posix_spawn_file_actions_entry_t[FAE_CLOSE, fd, void]
]

type posix_spawn_file_actions_entry_t[ACTION, FILEDES, DATA] {
	fae_action	const[ACTION, int32]
	fae_filedes	FILEDES
	fae_data	DATA
}

open_t {
	path	ptr[in, string]
	oflag	int32
	mode	int32
}

dup2_t {
	newfildes	int32
}

posix_spawnattr {
	sa_flags	flags[sa_flags, int16]
	sa_pgroup	pid
	sa_schedparam	sched_param
	sa_schedpolicy	int32
	sa_sigdefault	sigset_t
	sa_sigmask	sigset_t
}

sched_param {
	sched_priority	int32
}

swapent {
	dev		intptr
	flags		flags[swapent_flags, int32]
	nblks		int32
	inuse		int32
	priority	int32
	path		buffer[in]
}

modctl_load {
	fname		buffer[in]
	flags		flags[modctl_flags, int32]
	props		buffer[in]
	propslen	len[props, intptr]
}

fstatat_flags = AT_SYMLINK_NOFOLLOW, AT_SYMLINK_FOLLOW, AT_EACCESS, AT_REMOVEDIR
pollfd_events = POLLIN, POLLPRI, POLLOUT, POLLERR, POLLHUP, POLLNVAL, POLLRDNORM, POLLRDBAND, POLLWRNORM, POLLWRBAND
mknod_mode = S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK, S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH
msync_flags = MS_ASYNC, MS_SYNC, MS_INVALIDATE
at_flags = AT_SYMLINK_NOFOLLOW, AT_SYMLINK_FOLLOW
access_mode = R_OK, W_OK, X_OK, F_OK
priority_which = PRIO_PROCESS, PRIO_PGRP, PRIO_USER
linkat_flags = AT_SYMLINK_FOLLOW
unlinkat_flags = 0, AT_REMOVEDIR
flock_op = LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB
faccessat_flags = AT_EACCESS, AT_SYMLINK_NOFOLLOW
rusage_who = RUSAGE_SELF, RUSAGE_CHILDREN
rlimit_type = RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, RLIMIT_CORE, RLIMIT_RSS, RLIMIT_MEMLOCK, RLIMIT_NPROC, RLIMIT_NOFILE, RLIMIT_SBSIZE, RLIMIT_AS, RLIMIT_NTHR
timer_flags = TIMER_RELTIME, TIMER_ABSTIME
clock_id = CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_VIRTUAL, CLOCK_PROF, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID
getitimer_which = ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, ITIMER_MONOTONIC
wait_options = WALLSIG, WALTSIG, WCONTINUED, WEXITED, WNOHANG, WNOZOMBIE, WSTOPPED, WTRAPPED, WUNTRACED, __WALL, __WCLONE
utimensat_flags = 0, AT_SYMLINK_NOFOLLOW
fcntl_dupfd = F_DUPFD, F_DUPFD_CLOEXEC
fcntl_getflags = F_GETFD, F_GETFL
fcntl_lock = F_SETLK, F_SETLKW, F_GETLK
fcntl_flags = FD_CLOEXEC
fcntl_status = O_APPEND, O_ASYNC, O_NONBLOCK, O_DSYNC, O_RSYNC, O_ALT_IO, O_DIRECT, O_NOSIGPIPE
minherit_flags = MAP_INHERIT_COPY, MAP_INHERIT_NONE, MAP_INHERIT_SHARE, MAP_INHERIT_ZERO
flock_type = F_RDLCK, F_WRLCK, F_UNLCK
fadvise_flags = POSIX_FADV_NORMAL, POSIX_FADV_RANDOM, POSIX_FADV_SEQUENTIAL, POSIX_FADV_WILLNEED, POSIX_FADV_DONTNEED, POSIX_FADV_NOREUSE
chflags_flags = SF_APPEND, SF_IMMUTABLE, SF_ARCHIVED, UF_OPAQUE, UF_APPEND, UF_IMMUTABLE, UF_NODUMP
clone_flags = CLONE_CSIGNAL, CLONE_VM, CLONE_FS, CLONE_FILES, CLONE_SIGHAND, CLONE_PTRACE, CLONE_VFORK
sa_flags = SA_ONSTACK, SA_RESTART, SA_RESETHAND, SA_NODEFER, SA_NOCLDSTOP, SA_NOCLDWAIT, SA_SIGINFO, SA_NOKERNINFO, SA_ALLBITS
dup_flags = O_CLOEXEC, O_NONBLOCK, O_NOSIGPIPE
rasctl_flags = RAS_INSTALL, RAS_PURGE, RAS_PURGE_ALL
swapent_flags = SWF_INUSE, SWF_ENABLE, SWF_BUSY, SWF_FAKE
modctl_flags = MODCTL_NO_PROP, MODCTL_LOAD_FORCE
