syscalls.zr
This commit is contained in:
@@ -5,8 +5,9 @@ A very cool language
|
|||||||
## Features
|
## Features
|
||||||
* Clean indentation-based syntax
|
* Clean indentation-based syntax
|
||||||
* Compiles to x86_64 Assembly
|
* Compiles to x86_64 Assembly
|
||||||
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
* Pretty big [standard library](https://github.com/antpiasecki/zern/tree/main/src/std)
|
||||||
* Produces tiny static executables (~30KB with musl)
|
* Produces tiny static executables (~30KB with musl)
|
||||||
|
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
|
||||||
* Sometimes works
|
* Sometimes works
|
||||||
* Has the pipe operator
|
* Has the pipe operator
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,11 @@ fn compile_file(args: Args) -> Result<(), ZernError> {
|
|||||||
let mut analyzer = analyzer::Analyzer::new();
|
let mut analyzer = analyzer::Analyzer::new();
|
||||||
let mut codegen = codegen_x86_64::CodegenX86_64::new(&mut analyzer);
|
let mut codegen = codegen_x86_64::CodegenX86_64::new(&mut analyzer);
|
||||||
codegen.emit_prologue()?;
|
codegen.emit_prologue()?;
|
||||||
|
compile_file_to(
|
||||||
|
&mut codegen,
|
||||||
|
"syscalls.zr",
|
||||||
|
include_str!("std/syscalls.zr").into(),
|
||||||
|
)?;
|
||||||
compile_file_to(&mut codegen, "std.zr", include_str!("std/std.zr").into())?;
|
compile_file_to(&mut codegen, "std.zr", include_str!("std/std.zr").into())?;
|
||||||
compile_file_to(
|
compile_file_to(
|
||||||
&mut codegen,
|
&mut codegen,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func crypto.blake2b._G[v: ptr, a: i64, b: i64, c: i64, d: i64, x: i64, y: i64] :
|
|||||||
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: array] : void
|
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: array] : void
|
||||||
let v: ptr = mem.alloc(16 * 8)
|
let v: ptr = mem.alloc(16 * 8)
|
||||||
let m: ptr = mem.alloc(16 * 8)
|
let m: ptr = mem.alloc(16 * 8)
|
||||||
|
// https://crypto.stackexchange.com/questions/108987/rationale-for-blake2-message-schedule
|
||||||
let sigma: array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
let sigma: array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
||||||
|
|
||||||
for j in 0..8
|
for j in 0..8
|
||||||
@@ -35,9 +36,9 @@ func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, i
|
|||||||
mem.write64(v + 14 * 8, mem.read64(v + 14 * 8) ^ 0xffffffffffffffff)
|
mem.write64(v + 14 * 8, mem.read64(v + 14 * 8) ^ 0xffffffffffffffff)
|
||||||
|
|
||||||
for j in 0..16
|
for j in 0..16
|
||||||
let w: i64 = 0
|
let w = 0
|
||||||
for k in 0..8
|
for k in 0..8
|
||||||
w = w | ((mem.read8(block + j*8 + k) & 0xff) << (8 * k))
|
w = w | ((block[j * 8 + k] & 0xff) << (8 * k))
|
||||||
mem.write64(m + j * 8, w)
|
mem.write64(m + j * 8, w)
|
||||||
|
|
||||||
for r in 0..12
|
for r in 0..12
|
||||||
@@ -63,13 +64,17 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
|
|||||||
dbg.panic("invalid length passed to crypto.blake2b.hash")
|
dbg.panic("invalid length passed to crypto.blake2b.hash")
|
||||||
let out: ptr = mem.alloc(outlen)
|
let out: ptr = mem.alloc(outlen)
|
||||||
|
|
||||||
|
// IV[i] = floor(2**w * frac(sqrt(prime(i+1)))), where prime(i)
|
||||||
|
// is the i:th prime number ( 2, 3, 5, 7, 11, 13, 17, 19 )
|
||||||
|
// and sqrt(x) is the square root of x.
|
||||||
|
// https://www.ietf.org/rfc/rfc7693#section-2.6
|
||||||
let iv: array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
let iv: array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
||||||
|
|
||||||
let h: ptr = mem.alloc(8 * 8)
|
let h: ptr = mem.alloc(8 * 8)
|
||||||
let t0: i64 = 0
|
let t0 = 0
|
||||||
let t1: i64 = 0
|
let t1 = 0
|
||||||
let b: ptr = mem.alloc(128)
|
let block: ptr = mem.alloc(128)
|
||||||
let c: i64 = 0
|
let c = 0
|
||||||
|
|
||||||
for i in 0..8
|
for i in 0..8
|
||||||
mem.write64(h + i * 8, array.nth(iv, i))
|
mem.write64(h + i * 8, array.nth(iv, i))
|
||||||
@@ -78,9 +83,9 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
|
|||||||
|
|
||||||
if keylen > 0
|
if keylen > 0
|
||||||
for i in 0..keylen
|
for i in 0..keylen
|
||||||
mem.write8(b + i, key[i])
|
mem.write8(block + i, key[i])
|
||||||
for i in (keylen)..128
|
for i in (keylen)..128
|
||||||
mem.write8(b + i, 0)
|
mem.write8(block + i, 0)
|
||||||
c = 128
|
c = 128
|
||||||
else
|
else
|
||||||
c = 0
|
c = 0
|
||||||
@@ -90,9 +95,9 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
|
|||||||
t0 = t0 + c
|
t0 = t0 + c
|
||||||
if t0 < c
|
if t0 < c
|
||||||
t1 = t1 + 1
|
t1 = t1 + 1
|
||||||
crypto.blake2b._compress(h, b, t0, t1, 0, iv)
|
crypto.blake2b._compress(h, block, t0, t1, 0, iv)
|
||||||
c = 0
|
c = 0
|
||||||
mem.write8(b + c, input[i])
|
mem.write8(block + c, input[i])
|
||||||
c = c + 1
|
c = c + 1
|
||||||
|
|
||||||
t0 = t0 + c
|
t0 = t0 + c
|
||||||
@@ -101,14 +106,14 @@ func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputle
|
|||||||
|
|
||||||
if c < 128
|
if c < 128
|
||||||
for i in (c)..128
|
for i in (c)..128
|
||||||
mem.write8(b + i, 0)
|
mem.write8(block + i, 0)
|
||||||
crypto.blake2b._compress(h, b, t0, t1, 1, iv)
|
crypto.blake2b._compress(h, block, t0, t1, 1, iv)
|
||||||
|
|
||||||
for i in 0..outlen
|
for i in 0..outlen
|
||||||
mem.write8(out + i, ((mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff))
|
mem.write8(out + i, ((mem.read64(h + (i >> 3) * 8) >> (8 * (i & 7))) & 0xff))
|
||||||
|
|
||||||
mem.free(h)
|
mem.free(h)
|
||||||
mem.free(b)
|
mem.free(block)
|
||||||
array.free(iv)
|
array.free(iv)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@@ -207,13 +212,13 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
|
|||||||
for i in 0..8
|
for i in 0..8
|
||||||
mem.write8(nonce12 + 4 + i, nonce[16 + i])
|
mem.write8(nonce12 + 4 + i, nonce[16 + i])
|
||||||
|
|
||||||
let blocknum: i64 = 0
|
let blocknum = 0
|
||||||
let remaining: i64 = len
|
let remaining: i64 = len
|
||||||
let block: ptr = mem.alloc(64)
|
let block: ptr = mem.alloc(64)
|
||||||
|
|
||||||
while remaining > 0
|
while remaining > 0
|
||||||
crypto.xchacha20._block(subkey, nonce12, blocknum, block)
|
crypto.xchacha20._block(subkey, nonce12, blocknum, block)
|
||||||
let take: i64 = 64
|
let take = 64
|
||||||
if remaining < 64
|
if remaining < 64
|
||||||
take = remaining
|
take = remaining
|
||||||
for i in 0..take
|
for i in 0..take
|
||||||
@@ -226,7 +231,9 @@ func crypto.xchacha20._stream[key: ptr, nonce: ptr, out: ptr, len: i64] : void
|
|||||||
|
|
||||||
func crypto.xchacha20.xor[key: ptr, nonce: ptr, input: ptr, len: i64] : ptr
|
func crypto.xchacha20.xor[key: ptr, nonce: ptr, input: ptr, len: i64] : ptr
|
||||||
if len <= 0
|
if len <= 0
|
||||||
return dbg.panic("empty buffer passed to crypto.xchacha20.xor")
|
let out: ptr = mem.alloc(1)
|
||||||
|
mem.write8(out, 0)
|
||||||
|
return out
|
||||||
let out: ptr = mem.alloc(len)
|
let out: ptr = mem.alloc(len)
|
||||||
let ks: ptr = mem.alloc(len)
|
let ks: ptr = mem.alloc(len)
|
||||||
crypto.xchacha20._stream(key, nonce, ks, len)
|
crypto.xchacha20._stream(key, nonce, ks, len)
|
||||||
@@ -334,7 +341,7 @@ func crypto.x25519.scalarmult[scalar: ptr, point: ptr] : ptr
|
|||||||
|
|
||||||
let magic: ptr = mem.alloc(16 * 8)
|
let magic: ptr = mem.alloc(16 * 8)
|
||||||
mem.zero(magic, 16 * 8)
|
mem.zero(magic, 16 * 8)
|
||||||
mem.write64(magic, 0xdb41) // 121665
|
mem.write64(magic, 0xdb41) // 0xdb41 = 121665 = (486662 + 2)/4
|
||||||
mem.write64(magic + 8, 1)
|
mem.write64(magic + 8, 1)
|
||||||
|
|
||||||
// copy and clamp scalar
|
// copy and clamp scalar
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func mem.write64[x: ptr, d: i64] : void
|
|||||||
_builtin_set64(x, d)
|
_builtin_set64(x, d)
|
||||||
|
|
||||||
func io.print_sized[x: str, size: i64] : void
|
func io.print_sized[x: str, size: i64] : void
|
||||||
_builtin_syscall(1, 1, x, size) // write
|
_builtin_syscall(SYS_write, 1, x, size)
|
||||||
|
|
||||||
func io.print[x: str] : void
|
func io.print[x: str] : void
|
||||||
io.print_sized(x, str.len(x))
|
io.print_sized(x, str.len(x))
|
||||||
@@ -68,39 +68,39 @@ func io.println_i64[x: i64] : void
|
|||||||
|
|
||||||
func io.read_char[] : u8
|
func io.read_char[] : u8
|
||||||
let c: u8 = 0
|
let c: u8 = 0
|
||||||
_builtin_syscall(0, 0, ^c, 1) // read
|
_builtin_syscall(SYS_read, 0, ^c, 1)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
func io.read_line[]: str
|
func io.read_line[]: str
|
||||||
let MAX_SIZE = 60000
|
let MAX_SIZE = 60000
|
||||||
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
||||||
let n: i64 = _builtin_syscall(0, 0, buffer, MAX_SIZE) // read
|
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
|
||||||
if n < 0
|
if n < 0
|
||||||
return ""
|
return ""
|
||||||
str.set(buffer, n, 0)
|
str.set(buffer, n, 0)
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.read_file[path: str]: str
|
func io.read_file[path: str]: str
|
||||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd <= 0
|
if fd <= 0
|
||||||
dbg.panic("failed to open file")
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
let size: i64 = _builtin_syscall(8, fd, 0, 2) // lseek to the end
|
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||||
_builtin_syscall(8, fd, 0, 0) // lseek back to start
|
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||||
|
|
||||||
let buffer: str = mem.alloc(size + 1)
|
let buffer: str = mem.alloc(size + 1)
|
||||||
let n: i64 = _builtin_syscall(0, fd, buffer, size) // read
|
let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size)
|
||||||
str.set(buffer, n, 0)
|
str.set(buffer, n, 0)
|
||||||
_builtin_syscall(3, fd) // close
|
_builtin_syscall(SYS_close, fd)
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.write_file[path: str, content: str] : void
|
func io.write_file[path: str, content: str] : void
|
||||||
let fd: ptr = _builtin_syscall(257, -100, path, 0x241, 0o644) // openat
|
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||||
if fd < 0
|
if fd < 0
|
||||||
dbg.panic("failed to open file")
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
_builtin_syscall(1, fd, content, str.len(content)) // write
|
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||||
_builtin_syscall(3, fd) // close
|
_builtin_syscall(SYS_close, fd)
|
||||||
|
|
||||||
func str.len[s: str] : i64
|
func str.len[s: str] : i64
|
||||||
let i = 0
|
let i = 0
|
||||||
@@ -513,13 +513,13 @@ func alg.reduce[arr: array, fn: ptr, acc: i64] : i64
|
|||||||
return acc
|
return acc
|
||||||
|
|
||||||
func os.exit[code: i64] : void
|
func os.exit[code: i64] : void
|
||||||
_builtin_syscall(60, code)
|
_builtin_syscall(SYS_exit, code)
|
||||||
|
|
||||||
func os.urandom[n: i64]: ptr
|
func os.urandom[n: i64]: ptr
|
||||||
let buffer: ptr = mem.alloc(n)
|
let buffer: ptr = mem.alloc(n)
|
||||||
let fd: i64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, "/dev/urandom", 0, 0)
|
||||||
_builtin_syscall(0, fd, buffer, n) // read
|
_builtin_syscall(SYS_read, fd, buffer, n)
|
||||||
_builtin_syscall(3, fd) // close
|
_builtin_syscall(SYS_close, fd)
|
||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func os.urandom_i64[]: i64
|
func os.urandom_i64[]: i64
|
||||||
@@ -530,7 +530,7 @@ func os.urandom_i64[]: i64
|
|||||||
|
|
||||||
func os.time[] : i64
|
func os.time[] : i64
|
||||||
let tv: ptr = mem.alloc(16)
|
let tv: ptr = mem.alloc(16)
|
||||||
_builtin_syscall(96, tv, 0) // gettimeofday
|
_builtin_syscall(SYS_gettimeofday, tv, 0)
|
||||||
let seconds: i64 = mem.read64(tv)
|
let seconds: i64 = mem.read64(tv)
|
||||||
let microseconds: i64 = mem.read64(tv + 8)
|
let microseconds: i64 = mem.read64(tv + 8)
|
||||||
mem.free(tv)
|
mem.free(tv)
|
||||||
@@ -538,15 +538,15 @@ func os.time[] : i64
|
|||||||
|
|
||||||
// voodoo magic
|
// voodoo magic
|
||||||
func os.shell[command: str] : i64
|
func os.shell[command: str] : i64
|
||||||
let pid: i64 = _builtin_syscall(57) // fork
|
let pid: i64 = _builtin_syscall(SYS_fork)
|
||||||
if pid == 0
|
if pid == 0
|
||||||
// leaky but not sure where can i free it
|
// leaky but not sure where can i free it
|
||||||
let argv: array = ["sh", "-c", command, 0]
|
let argv: array = ["sh", "-c", command, 0]
|
||||||
_builtin_syscall(59, "/bin/sh", mem.read64(argv), _builtin_environ()) // execve
|
_builtin_syscall(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ())
|
||||||
_builtin_syscall(60, 1) // exit
|
_builtin_syscall(SYS_exit, 1)
|
||||||
else
|
else
|
||||||
let status = 0
|
let status = 0
|
||||||
let wp: i64 = _builtin_syscall(61, pid, ^status, 0, 0) // waitpid
|
let wp: i64 = _builtin_syscall(SYS_wait4, pid, ^status, 0, 0)
|
||||||
if wp == -1
|
if wp == -1
|
||||||
return -1
|
return -1
|
||||||
let st: i64 = status & 0xffffffff
|
let st: i64 = status & 0xffffffff
|
||||||
@@ -557,14 +557,14 @@ func os.shell[command: str] : i64
|
|||||||
return -(st & 0x7f)
|
return -(st & 0x7f)
|
||||||
|
|
||||||
func os.listdir[path: str] : array
|
func os.listdir[path: str] : array
|
||||||
let fd: i64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||||
if fd < 0
|
if fd < 0
|
||||||
return []
|
return []
|
||||||
|
|
||||||
let files: array = []
|
let files: array = []
|
||||||
let buf: ptr = mem.alloc(1024)
|
let buf: ptr = mem.alloc(1024)
|
||||||
while true
|
while true
|
||||||
let n: i64 = _builtin_syscall(217, fd, buf, 1024) // getdents64
|
let n: i64 = _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
||||||
if n <= 0
|
if n <= 0
|
||||||
break
|
break
|
||||||
|
|
||||||
@@ -586,17 +586,18 @@ func os.listdir[path: str] : array
|
|||||||
pos = pos + len
|
pos = pos + len
|
||||||
|
|
||||||
mem.free(buf)
|
mem.free(buf)
|
||||||
_builtin_syscall(3, fd) // close
|
_builtin_syscall(SYS_close, fd)
|
||||||
return files
|
return files
|
||||||
|
|
||||||
func net.listen[packed_host: i64, port: i64] : i64
|
func net.listen[packed_host: i64, port: i64] : i64
|
||||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
||||||
if s < 0
|
if s < 0
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
|
// setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||||
let optval = 1
|
let optval = 1
|
||||||
if _builtin_syscall(54, s, 1, 2, ^optval, 8) < 0 // setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
if _builtin_syscall(SYS_setsockopt, s, 1, 2, ^optval, 8) < 0
|
||||||
_builtin_syscall(3, s) // close
|
_builtin_syscall(SYS_close, s)
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
let sa: ptr = mem.alloc(16)
|
let sa: ptr = mem.alloc(16)
|
||||||
@@ -610,14 +611,14 @@ func net.listen[packed_host: i64, port: i64] : i64
|
|||||||
mem.write8(sa + 6, (packed_host >> 8) & 255)
|
mem.write8(sa + 6, (packed_host >> 8) & 255)
|
||||||
mem.write8(sa + 7, packed_host & 255)
|
mem.write8(sa + 7, packed_host & 255)
|
||||||
|
|
||||||
if _builtin_syscall(49, s, sa, 16) < 0 // bind
|
if _builtin_syscall(SYS_bind, s, sa, 16) < 0
|
||||||
_builtin_syscall(3, s) // close
|
_builtin_syscall(SYS_close, s)
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
return -1
|
return -1
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
|
|
||||||
if _builtin_syscall(50, s, 1) < 0 // listen
|
if _builtin_syscall(SYS_listen, s, 1) < 0
|
||||||
_builtin_syscall(3, s) // close
|
_builtin_syscall(SYS_close, s)
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
return s
|
return s
|
||||||
@@ -629,7 +630,7 @@ func net.connect[host: str, port: i64] : i64
|
|||||||
|
|
||||||
let ip_ptr: ptr = mem.read64(mem.read64(he + 24))
|
let ip_ptr: ptr = mem.read64(mem.read64(he + 24))
|
||||||
|
|
||||||
let s: i64 = _builtin_syscall(41, 2, 1, 0) // socket
|
let s: i64 = _builtin_syscall(SYS_socket, 2, 1, 0)
|
||||||
if s < 0
|
if s < 0
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@@ -643,25 +644,25 @@ func net.connect[host: str, port: i64] : i64
|
|||||||
mem.write8(sa + 6, ip_ptr[2])
|
mem.write8(sa + 6, ip_ptr[2])
|
||||||
mem.write8(sa + 7, ip_ptr[3])
|
mem.write8(sa + 7, ip_ptr[3])
|
||||||
|
|
||||||
if _builtin_syscall(42, s, sa, 16) < 0 // connect
|
if _builtin_syscall(SYS_connect, s, sa, 16) < 0
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
_builtin_syscall(3, s) // close
|
_builtin_syscall(SYS_close, s)
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
mem.free(sa)
|
mem.free(sa)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
func net.accept[s: i64] : i64
|
func net.accept[s: i64] : i64
|
||||||
return _builtin_syscall(43, s, 0, 0)
|
return _builtin_syscall(SYS_accept, s, 0, 0)
|
||||||
|
|
||||||
func net.send[s: i64, data: str, size: i64] : void
|
func net.send[s: i64, data: str, size: i64] : void
|
||||||
_builtin_syscall(44, s, data, size, 0, 0, 0)
|
_builtin_syscall(SYS_sendto, s, data, size, 0, 0, 0)
|
||||||
|
|
||||||
func net.read[s: i64, buffer: ptr, size: i64] : i64
|
func net.read[s: i64, buffer: ptr, size: i64] : i64
|
||||||
return _builtin_syscall(0, s, buffer, size)
|
return _builtin_syscall(SYS_read, s, buffer, size)
|
||||||
|
|
||||||
func net.close[s: i64] : void
|
func net.close[s: i64] : void
|
||||||
_builtin_syscall(3, s)
|
_builtin_syscall(SYS_close, s)
|
||||||
|
|
||||||
func net.pack_addr[a: i64, b: i64, c: i64, d: i64] : i64
|
func net.pack_addr[a: i64, b: i64, c: i64, d: i64] : i64
|
||||||
return (a << 24) | (b << 16) | (c << 8) | d
|
return (a << 24) | (b << 16) | (c << 8) | d
|
||||||
381
src/std/syscalls.zr
Normal file
381
src/std/syscalls.zr
Normal file
@@ -0,0 +1,381 @@
|
|||||||
|
// https://github.com/torvalds/linux/blob/19272b37aa4f83ca52bdf9c16d5d81bdd1354494/arch/x86/entry/syscalls/syscall_64.tbl
|
||||||
|
const SYS_read = 0
|
||||||
|
const SYS_write = 1
|
||||||
|
const SYS_open = 2
|
||||||
|
const SYS_close = 3
|
||||||
|
const SYS_stat = 4
|
||||||
|
const SYS_fstat = 5
|
||||||
|
const SYS_lstat = 6
|
||||||
|
const SYS_poll = 7
|
||||||
|
const SYS_lseek = 8
|
||||||
|
const SYS_mmap = 9
|
||||||
|
const SYS_mprotect = 10
|
||||||
|
const SYS_munmap = 11
|
||||||
|
const SYS_brk = 12
|
||||||
|
const SYS_rt_sigaction = 13
|
||||||
|
const SYS_rt_sigprocmask = 14
|
||||||
|
const SYS_rt_sigreturn = 15
|
||||||
|
const SYS_ioctl = 16
|
||||||
|
const SYS_pread64 = 17
|
||||||
|
const SYS_pwrite64 = 18
|
||||||
|
const SYS_readv = 19
|
||||||
|
const SYS_writev = 20
|
||||||
|
const SYS_access = 21
|
||||||
|
const SYS_pipe = 22
|
||||||
|
const SYS_select = 23
|
||||||
|
const SYS_sched_yield = 24
|
||||||
|
const SYS_mremap = 25
|
||||||
|
const SYS_msync = 26
|
||||||
|
const SYS_mincore = 27
|
||||||
|
const SYS_madvise = 28
|
||||||
|
const SYS_shmget = 29
|
||||||
|
const SYS_shmat = 30
|
||||||
|
const SYS_shmctl = 31
|
||||||
|
const SYS_dup = 32
|
||||||
|
const SYS_dup2 = 33
|
||||||
|
const SYS_pause = 34
|
||||||
|
const SYS_nanosleep = 35
|
||||||
|
const SYS_getitimer = 36
|
||||||
|
const SYS_alarm = 37
|
||||||
|
const SYS_setitimer = 38
|
||||||
|
const SYS_getpid = 39
|
||||||
|
const SYS_sendfile = 40
|
||||||
|
const SYS_socket = 41
|
||||||
|
const SYS_connect = 42
|
||||||
|
const SYS_accept = 43
|
||||||
|
const SYS_sendto = 44
|
||||||
|
const SYS_recvfrom = 45
|
||||||
|
const SYS_sendmsg = 46
|
||||||
|
const SYS_recvmsg = 47
|
||||||
|
const SYS_shutdown = 48
|
||||||
|
const SYS_bind = 49
|
||||||
|
const SYS_listen = 50
|
||||||
|
const SYS_getsockname = 51
|
||||||
|
const SYS_getpeername = 52
|
||||||
|
const SYS_socketpair = 53
|
||||||
|
const SYS_setsockopt = 54
|
||||||
|
const SYS_getsockopt = 55
|
||||||
|
const SYS_clone = 56
|
||||||
|
const SYS_fork = 57
|
||||||
|
const SYS_vfork = 58
|
||||||
|
const SYS_execve = 59
|
||||||
|
const SYS_exit = 60
|
||||||
|
const SYS_wait4 = 61
|
||||||
|
const SYS_kill = 62
|
||||||
|
const SYS_uname = 63
|
||||||
|
const SYS_semget = 64
|
||||||
|
const SYS_semop = 65
|
||||||
|
const SYS_semctl = 66
|
||||||
|
const SYS_shmdt = 67
|
||||||
|
const SYS_msgget = 68
|
||||||
|
const SYS_msgsnd = 69
|
||||||
|
const SYS_msgrcv = 70
|
||||||
|
const SYS_msgctl = 71
|
||||||
|
const SYS_fcntl = 72
|
||||||
|
const SYS_flock = 73
|
||||||
|
const SYS_fsync = 74
|
||||||
|
const SYS_fdatasync = 75
|
||||||
|
const SYS_truncate = 76
|
||||||
|
const SYS_ftruncate = 77
|
||||||
|
const SYS_getdents = 78
|
||||||
|
const SYS_getcwd = 79
|
||||||
|
const SYS_chdir = 80
|
||||||
|
const SYS_fchdir = 81
|
||||||
|
const SYS_rename = 82
|
||||||
|
const SYS_mkdir = 83
|
||||||
|
const SYS_rmdir = 84
|
||||||
|
const SYS_creat = 85
|
||||||
|
const SYS_link = 86
|
||||||
|
const SYS_unlink = 87
|
||||||
|
const SYS_symlink = 88
|
||||||
|
const SYS_readlink = 89
|
||||||
|
const SYS_chmod = 90
|
||||||
|
const SYS_fchmod = 91
|
||||||
|
const SYS_chown = 92
|
||||||
|
const SYS_fchown = 93
|
||||||
|
const SYS_lchown = 94
|
||||||
|
const SYS_umask = 95
|
||||||
|
const SYS_gettimeofday = 96
|
||||||
|
const SYS_getrlimit = 97
|
||||||
|
const SYS_getrusage = 98
|
||||||
|
const SYS_sysinfo = 99
|
||||||
|
const SYS_times = 100
|
||||||
|
const SYS_ptrace = 101
|
||||||
|
const SYS_getuid = 102
|
||||||
|
const SYS_syslog = 103
|
||||||
|
const SYS_getgid = 104
|
||||||
|
const SYS_setuid = 105
|
||||||
|
const SYS_setgid = 106
|
||||||
|
const SYS_geteuid = 107
|
||||||
|
const SYS_getegid = 108
|
||||||
|
const SYS_setpgid = 109
|
||||||
|
const SYS_getppid = 110
|
||||||
|
const SYS_getpgrp = 111
|
||||||
|
const SYS_setsid = 112
|
||||||
|
const SYS_setreuid = 113
|
||||||
|
const SYS_setregid = 114
|
||||||
|
const SYS_getgroups = 115
|
||||||
|
const SYS_setgroups = 116
|
||||||
|
const SYS_setresuid = 117
|
||||||
|
const SYS_getresuid = 118
|
||||||
|
const SYS_setresgid = 119
|
||||||
|
const SYS_getresgid = 120
|
||||||
|
const SYS_getpgid = 121
|
||||||
|
const SYS_setfsuid = 122
|
||||||
|
const SYS_setfsgid = 123
|
||||||
|
const SYS_getsid = 124
|
||||||
|
const SYS_capget = 125
|
||||||
|
const SYS_capset = 126
|
||||||
|
const SYS_rt_sigpending = 127
|
||||||
|
const SYS_rt_sigtimedwait = 128
|
||||||
|
const SYS_rt_sigqueueinfo = 129
|
||||||
|
const SYS_rt_sigsuspend = 130
|
||||||
|
const SYS_sigaltstack = 131
|
||||||
|
const SYS_utime = 132
|
||||||
|
const SYS_mknod = 133
|
||||||
|
const SYS_uselib = 134
|
||||||
|
const SYS_personality = 135
|
||||||
|
const SYS_ustat = 136
|
||||||
|
const SYS_statfs = 137
|
||||||
|
const SYS_fstatfs = 138
|
||||||
|
const SYS_sysfs = 139
|
||||||
|
const SYS_getpriority = 140
|
||||||
|
const SYS_setpriority = 141
|
||||||
|
const SYS_sched_setparam = 142
|
||||||
|
const SYS_sched_getparam = 143
|
||||||
|
const SYS_sched_setscheduler = 144
|
||||||
|
const SYS_sched_getscheduler = 145
|
||||||
|
const SYS_sched_get_priority_max = 146
|
||||||
|
const SYS_sched_get_priority_min = 147
|
||||||
|
const SYS_sched_rr_get_interval = 148
|
||||||
|
const SYS_mlock = 149
|
||||||
|
const SYS_munlock = 150
|
||||||
|
const SYS_mlockall = 151
|
||||||
|
const SYS_munlockall = 152
|
||||||
|
const SYS_vhangup = 153
|
||||||
|
const SYS_modify_ldt = 154
|
||||||
|
const SYS_pivot_root = 155
|
||||||
|
const SYS__sysctl = 156
|
||||||
|
const SYS_prctl = 157
|
||||||
|
const SYS_arch_prctl = 158
|
||||||
|
const SYS_adjtimex = 159
|
||||||
|
const SYS_setrlimit = 160
|
||||||
|
const SYS_chroot = 161
|
||||||
|
const SYS_sync = 162
|
||||||
|
const SYS_acct = 163
|
||||||
|
const SYS_settimeofday = 164
|
||||||
|
const SYS_mount = 165
|
||||||
|
const SYS_umount2 = 166
|
||||||
|
const SYS_swapon = 167
|
||||||
|
const SYS_swapoff = 168
|
||||||
|
const SYS_reboot = 169
|
||||||
|
const SYS_sethostname = 170
|
||||||
|
const SYS_setdomainname = 171
|
||||||
|
const SYS_iopl = 172
|
||||||
|
const SYS_ioperm = 173
|
||||||
|
const SYS_create_module = 174
|
||||||
|
const SYS_init_module = 175
|
||||||
|
const SYS_delete_module = 176
|
||||||
|
const SYS_get_kernel_syms = 177
|
||||||
|
const SYS_query_module = 178
|
||||||
|
const SYS_quotactl = 179
|
||||||
|
const SYS_nfsservctl = 180
|
||||||
|
const SYS_getpmsg = 181
|
||||||
|
const SYS_putpmsg = 182
|
||||||
|
const SYS_afs_syscall = 183
|
||||||
|
const SYS_tuxcall = 184
|
||||||
|
const SYS_security = 185
|
||||||
|
const SYS_gettid = 186
|
||||||
|
const SYS_readahead = 187
|
||||||
|
const SYS_setxattr = 188
|
||||||
|
const SYS_lsetxattr = 189
|
||||||
|
const SYS_fsetxattr = 190
|
||||||
|
const SYS_getxattr = 191
|
||||||
|
const SYS_lgetxattr = 192
|
||||||
|
const SYS_fgetxattr = 193
|
||||||
|
const SYS_listxattr = 194
|
||||||
|
const SYS_llistxattr = 195
|
||||||
|
const SYS_flistxattr = 196
|
||||||
|
const SYS_removexattr = 197
|
||||||
|
const SYS_lremovexattr = 198
|
||||||
|
const SYS_fremovexattr = 199
|
||||||
|
const SYS_tkill = 200
|
||||||
|
const SYS_time = 201
|
||||||
|
const SYS_futex = 202
|
||||||
|
const SYS_sched_setaffinity = 203
|
||||||
|
const SYS_sched_getaffinity = 204
|
||||||
|
const SYS_set_thread_area = 205
|
||||||
|
const SYS_io_setup = 206
|
||||||
|
const SYS_io_destroy = 207
|
||||||
|
const SYS_io_getevents = 208
|
||||||
|
const SYS_io_submit = 209
|
||||||
|
const SYS_io_cancel = 210
|
||||||
|
const SYS_get_thread_area = 211
|
||||||
|
const SYS_lookup_dcookie = 212
|
||||||
|
const SYS_epoll_create = 213
|
||||||
|
const SYS_epoll_ctl_old = 214
|
||||||
|
const SYS_epoll_wait_old = 215
|
||||||
|
const SYS_remap_file_pages = 216
|
||||||
|
const SYS_getdents64 = 217
|
||||||
|
const SYS_set_tid_address = 218
|
||||||
|
const SYS_restart_syscall = 219
|
||||||
|
const SYS_semtimedop = 220
|
||||||
|
const SYS_fadvise64 = 221
|
||||||
|
const SYS_timer_create = 222
|
||||||
|
const SYS_timer_settime = 223
|
||||||
|
const SYS_timer_gettime = 224
|
||||||
|
const SYS_timer_getoverrun = 225
|
||||||
|
const SYS_timer_delete = 226
|
||||||
|
const SYS_clock_settime = 227
|
||||||
|
const SYS_clock_gettime = 228
|
||||||
|
const SYS_clock_getres = 229
|
||||||
|
const SYS_clock_nanosleep = 230
|
||||||
|
const SYS_exit_group = 231
|
||||||
|
const SYS_epoll_wait = 232
|
||||||
|
const SYS_epoll_ctl = 233
|
||||||
|
const SYS_tgkill = 234
|
||||||
|
const SYS_utimes = 235
|
||||||
|
const SYS_vserver = 236
|
||||||
|
const SYS_mbind = 237
|
||||||
|
const SYS_set_mempolicy = 238
|
||||||
|
const SYS_get_mempolicy = 239
|
||||||
|
const SYS_mq_open = 240
|
||||||
|
const SYS_mq_unlink = 241
|
||||||
|
const SYS_mq_timedsend = 242
|
||||||
|
const SYS_mq_timedreceive = 243
|
||||||
|
const SYS_mq_notify = 244
|
||||||
|
const SYS_mq_getsetattr = 245
|
||||||
|
const SYS_kexec_load = 246
|
||||||
|
const SYS_waitid = 247
|
||||||
|
const SYS_add_key = 248
|
||||||
|
const SYS_request_key = 249
|
||||||
|
const SYS_keyctl = 250
|
||||||
|
const SYS_ioprio_set = 251
|
||||||
|
const SYS_ioprio_get = 252
|
||||||
|
const SYS_inotify_init = 253
|
||||||
|
const SYS_inotify_add_watch = 254
|
||||||
|
const SYS_inotify_rm_watch = 255
|
||||||
|
const SYS_migrate_pages = 256
|
||||||
|
const SYS_openat = 257
|
||||||
|
const SYS_mkdirat = 258
|
||||||
|
const SYS_mknodat = 259
|
||||||
|
const SYS_fchownat = 260
|
||||||
|
const SYS_futimesat = 261
|
||||||
|
const SYS_newfstatat = 262
|
||||||
|
const SYS_unlinkat = 263
|
||||||
|
const SYS_renameat = 264
|
||||||
|
const SYS_linkat = 265
|
||||||
|
const SYS_symlinkat = 266
|
||||||
|
const SYS_readlinkat = 267
|
||||||
|
const SYS_fchmodat = 268
|
||||||
|
const SYS_faccessat = 269
|
||||||
|
const SYS_pselect6 = 270
|
||||||
|
const SYS_ppoll = 271
|
||||||
|
const SYS_unshare = 272
|
||||||
|
const SYS_set_robust_list = 273
|
||||||
|
const SYS_get_robust_list = 274
|
||||||
|
const SYS_splice = 275
|
||||||
|
const SYS_tee = 276
|
||||||
|
const SYS_sync_file_range = 277
|
||||||
|
const SYS_vmsplice = 278
|
||||||
|
const SYS_move_pages = 279
|
||||||
|
const SYS_utimensat = 280
|
||||||
|
const SYS_epoll_pwait = 281
|
||||||
|
const SYS_signalfd = 282
|
||||||
|
const SYS_timerfd_create = 283
|
||||||
|
const SYS_eventfd = 284
|
||||||
|
const SYS_fallocate = 285
|
||||||
|
const SYS_timerfd_settime = 286
|
||||||
|
const SYS_timerfd_gettime = 287
|
||||||
|
const SYS_accept4 = 288
|
||||||
|
const SYS_signalfd4 = 289
|
||||||
|
const SYS_eventfd2 = 290
|
||||||
|
const SYS_epoll_create1 = 291
|
||||||
|
const SYS_dup3 = 292
|
||||||
|
const SYS_pipe2 = 293
|
||||||
|
const SYS_inotify_init1 = 294
|
||||||
|
const SYS_preadv = 295
|
||||||
|
const SYS_pwritev = 296
|
||||||
|
const SYS_rt_tgsigqueueinfo = 297
|
||||||
|
const SYS_perf_event_open = 298
|
||||||
|
const SYS_recvmmsg = 299
|
||||||
|
const SYS_fanotify_init = 300
|
||||||
|
const SYS_fanotify_mark = 301
|
||||||
|
const SYS_prlimit64 = 302
|
||||||
|
const SYS_name_to_handle_at = 303
|
||||||
|
const SYS_open_by_handle_at = 304
|
||||||
|
const SYS_clock_adjtime = 305
|
||||||
|
const SYS_syncfs = 306
|
||||||
|
const SYS_sendmmsg = 307
|
||||||
|
const SYS_setns = 308
|
||||||
|
const SYS_getcpu = 309
|
||||||
|
const SYS_process_vm_readv = 310
|
||||||
|
const SYS_process_vm_writev = 311
|
||||||
|
const SYS_kcmp = 312
|
||||||
|
const SYS_finit_module = 313
|
||||||
|
const SYS_sched_setattr = 314
|
||||||
|
const SYS_sched_getattr = 315
|
||||||
|
const SYS_renameat2 = 316
|
||||||
|
const SYS_seccomp = 317
|
||||||
|
const SYS_getrandom = 318
|
||||||
|
const SYS_memfd_create = 319
|
||||||
|
const SYS_kexec_file_load = 320
|
||||||
|
const SYS_bpf = 321
|
||||||
|
const SYS_execveat = 322
|
||||||
|
const SYS_userfaultfd = 323
|
||||||
|
const SYS_membarrier = 324
|
||||||
|
const SYS_mlock2 = 325
|
||||||
|
const SYS_copy_file_range = 326
|
||||||
|
const SYS_preadv2 = 327
|
||||||
|
const SYS_pwritev2 = 328
|
||||||
|
const SYS_pkey_mprotect = 329
|
||||||
|
const SYS_pkey_alloc = 330
|
||||||
|
const SYS_pkey_free = 331
|
||||||
|
const SYS_statx = 332
|
||||||
|
const SYS_io_pgetevents = 333
|
||||||
|
const SYS_rseq = 334
|
||||||
|
const SYS_uretprobe = 335
|
||||||
|
const SYS_pidfd_send_signal = 424
|
||||||
|
const SYS_io_uring_setup = 425
|
||||||
|
const SYS_io_uring_enter = 426
|
||||||
|
const SYS_io_uring_register = 427
|
||||||
|
const SYS_open_tree = 428
|
||||||
|
const SYS_move_mount = 429
|
||||||
|
const SYS_fsopen = 430
|
||||||
|
const SYS_fsconfig = 431
|
||||||
|
const SYS_fsmount = 432
|
||||||
|
const SYS_fspick = 433
|
||||||
|
const SYS_pidfd_open = 434
|
||||||
|
const SYS_clone3 = 435
|
||||||
|
const SYS_close_range = 436
|
||||||
|
const SYS_openat2 = 437
|
||||||
|
const SYS_pidfd_getfd = 438
|
||||||
|
const SYS_faccessat2 = 439
|
||||||
|
const SYS_process_madvise = 440
|
||||||
|
const SYS_epoll_pwait2 = 441
|
||||||
|
const SYS_mount_setattr = 442
|
||||||
|
const SYS_quotactl_fd = 443
|
||||||
|
const SYS_landlock_create_ruleset = 444
|
||||||
|
const SYS_landlock_add_rule = 445
|
||||||
|
const SYS_landlock_restrict_self = 446
|
||||||
|
const SYS_memfd_secret = 447
|
||||||
|
const SYS_process_mrelease = 448
|
||||||
|
const SYS_futex_waitv = 449
|
||||||
|
const SYS_set_mempolicy_home_node = 450
|
||||||
|
const SYS_cachestat = 451
|
||||||
|
const SYS_fchmodat2 = 452
|
||||||
|
const SYS_map_shadow_stack = 453
|
||||||
|
const SYS_futex_wake = 454
|
||||||
|
const SYS_futex_wait = 455
|
||||||
|
const SYS_futex_requeue = 456
|
||||||
|
const SYS_statmount = 457
|
||||||
|
const SYS_listmount = 458
|
||||||
|
const SYS_lsm_get_self_attr = 459
|
||||||
|
const SYS_lsm_set_self_attr = 460
|
||||||
|
const SYS_lsm_list_modules = 461
|
||||||
|
const SYS_mseal = 462
|
||||||
|
const SYS_setxattrat = 463
|
||||||
|
const SYS_getxattrat = 464
|
||||||
|
const SYS_listxattrat = 465
|
||||||
|
const SYS_removexattrat = 466
|
||||||
|
const SYS_open_tree_attr = 467
|
||||||
Reference in New Issue
Block a user