diff --git a/CHANGELOG.md b/CHANGELOG.md index 7512b1b..396f86f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- dista declares libc's `send`, `recv`, `connect`, `close`, `setsockopt`, `getsockopt`, `poll` and `getaddrinfo` as Mosaad-M/tcp and tls do, so a binary can import both ([#134](https://github.com/morca-dev/dista/issues/134)). + ## [0.5.1] - 2026-11-14 ### Changed diff --git a/pixi.toml b/pixi.toml index bb82cde..b5a115b 100644 --- a/pixi.toml +++ b/pixi.toml @@ -31,6 +31,7 @@ test-head-drivers = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_he test-scale = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_scale.mojo -o build/test-scale && ./build/test-scale" } test-executors = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_executors.mojo -o build/test-executors && ./build/test-executors" } test-slots = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_slots.mojo -o build/test-slots && ./build/test-slots" } +test-libc-abi = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_libc_abi.mojo -o build/test-libc-abi && ./build/test-libc-abi" } test-sizing = { cmd = "scripts/mojo-build.sh -I src $DEP_INC tests/test_sizing.mojo -o build/test-sizing && ./build/test-sizing" } test-cluster = { cmd = "tests/cluster.sh" } test-restart = { cmd = "tests/head_restart.sh" } @@ -47,7 +48,7 @@ test-kind = { cmd = "tests/kind.sh" } # The test binaries built several at once before the tasks that copy them # (scripts/build-tests.sh, scripts/mojo-build.sh). build-tests = { cmd = "scripts/build-tests.sh" } -test = { depends-on = ["build-tests", "test-codec", "test-frame", "test-auth", "test-runtime", "test-batches", "test-free", "test-take", "test-timeline", "test-scale", "test-executors", "test-slots", "test-sizing", "test-head-drivers", "test-actors", "test-arity", "test-shuffle", "test-spill", "test-fetch", "test-admission", "test-head-loop", "test-head-queue", "test-cancel", "test-cluster", "test-restart", "test-locality", "test-compile-fail", "test-metrics", "test-drain", "test-peer-loss", "test-peer-connect", "test-autoscale", "test-cli", "test-chaos"] } +test = { depends-on = ["build-tests", "test-codec", "test-frame", "test-auth", "test-runtime", "test-batches", "test-free", "test-take", "test-timeline", "test-scale", "test-executors", "test-slots", "test-sizing", "test-libc-abi", "test-head-drivers", "test-actors", "test-arity", "test-shuffle", "test-spill", "test-fetch", "test-admission", "test-head-loop", "test-head-queue", "test-cancel", "test-cluster", "test-restart", "test-locality", "test-compile-fail", "test-metrics", "test-drain", "test-peer-loss", "test-peer-connect", "test-autoscale", "test-cli", "test-chaos"] } cli = { cmd = "scripts/mojo-build.sh -I src $DEP_INC cli/main.mojo -o build/dista" } bench = { cmd = "scripts/mojo-build.sh -I src $DEP_INC bench/fanout.mojo -o build/bench-fanout && ./build/bench-fanout" } bench-slots = { cmd = "scripts/mojo-build.sh -I src $DEP_INC bench/slots.mojo -o build/bench-slots && ./build/bench-slots" } diff --git a/src/dista/executors.mojo b/src/dista/executors.mojo index 9e13510..7a44e7b 100644 --- a/src/dista/executors.mojo +++ b/src/dista/executors.mojo @@ -42,7 +42,7 @@ from threads import ( from dista.client import TaskContext from dista.sizing import AUTO, Sizing -from dista.socket import set_nonblocking, wall_ns +from dista.socket import close_fd, set_nonblocking, wall_ns comptime DispatchFn = def( UInt64, List[UInt8], mut List[UInt8], mut TaskContext @@ -331,8 +331,8 @@ struct Executors[dispatch: DispatchFn](Movable): self._threads[k].join() except: pass - _ = external_call["close", c_int](self._wake_r) - _ = external_call["close", c_int](sh.wake_w) + _ = close_fd(self._wake_r) + _ = close_fd(sh.wake_w) _ = self._shared.unsafe_take_pointee() self._shared.unsafe_free() diff --git a/src/dista/signals.mojo b/src/dista/signals.mojo index 2b44445..0d7e436 100644 --- a/src/dista/signals.mojo +++ b/src/dista/signals.mojo @@ -14,7 +14,7 @@ duplicated onto a fixed descriptor, `WAKE_FD`, which the handler writes to. from std.ffi import c_int, external_call -from dista.socket import fcntl +from dista.socket import close_fd, fcntl comptime SIGTERM: c_int = 15 comptime WAKE_FD: c_int = 250 @@ -54,11 +54,11 @@ def wake_on_sigterm() -> c_int: exit_on_sigterm() return -1 if external_call["dup2", c_int](fds[1], WAKE_FD) != WAKE_FD: - _ = external_call["close", c_int](fds[0]) - _ = external_call["close", c_int](fds[1]) + _ = close_fd(fds[0]) + _ = close_fd(fds[1]) exit_on_sigterm() return -1 - _ = external_call["close", c_int](fds[1]) + _ = close_fd(fds[1]) _ = external_call["signal", Int](SIGTERM, _wake) return fds[0] diff --git a/src/dista/socket.mojo b/src/dista/socket.mojo index 3f15036..54e29c7 100644 --- a/src/dista/socket.mojo +++ b/src/dista/socket.mojo @@ -103,11 +103,11 @@ def resolve_ipv4(host: String) raises -> Array[UInt8, 4]: var slot = stack_allocation[1, UInt64]() slot[] = 0 var name = host - var rc = external_call["getaddrinfo", c_int]( - name.as_c_string_span(), - Optional[Pointer[UInt8, MutUntrackedOrigin]](None), - hints.unsafe_bitcast[NoneType](), - slot.unsafe_bitcast[NoneType](), + var rc = external_call["getaddrinfo", Int32]( + Int(name.as_c_string_span().ptr()), + Int(0), + Int(hints), + Int(slot), ) if rc != 0 or slot[] == 0: raise Error("cannot resolve host ", host, " (getaddrinfo ", rc, ")") @@ -129,15 +129,48 @@ def resolve_ipv4(host: String) raises -> Array[UInt8, 4]: return ip^ +# The libc calls that Mosaad-M/tcp and tls also declare. Mojo allows one +# signature per C symbol in a program, so these are declared exactly as tcp +# does (`Int` addresses, `Int32` ints) and a binary can import both. +def _send(fd: Int32, buf_addr: Int, length: Int, flags: Int32) -> Int: + return external_call["send", Int](fd, buf_addr, length, flags) + + +def _recv(fd: Int32, buf_addr: Int, length: Int, flags: Int32) -> Int: + return external_call["recv", Int](fd, buf_addr, length, flags) + + +def _c_connect(fd: Int32, addr: Int, addrlen: Int32) -> Int32: + return external_call["connect", Int32](fd, addr, addrlen) + + +def close_fd(fd: Int32) -> Int32: + """`close(2)`: the only declaration of it in dista.""" + return external_call["close", Int32](fd) + + +def _setsockopt( + fd: Int32, level: Int32, name: Int32, value: Int, length: Int32 +) -> Int32: + return external_call["setsockopt", Int32](fd, level, name, value, length) + + +def _getsockopt( + fd: Int32, level: Int32, name: Int32, value: Int, length_addr: Int +) -> Int32: + return external_call["getsockopt", Int32]( + fd, level, name, value, length_addr + ) + + +def _c_poll(fds_addr: Int, nfds: Int, timeout_ms: Int32) -> Int32: + return external_call["poll", Int32](fds_addr, nfds, timeout_ms) + + def send_some(fd: c_int, data: Pointer[UInt8, _], size: Int) raises -> Int: """Sends what the socket takes without blocking. Returns the bytes sent.""" while True: - var n = external_call["send", c_ssize_t]( - fd, - data.unsafe_bitcast[NoneType](), - c_size_t(size), - MSG_NOSIGNAL | MSG_DONTWAIT, - ) + var n = _send(fd, Int(data), size, MSG_NOSIGNAL | MSG_DONTWAIT) if n >= 0: return Int(n) var e = get_errno().value @@ -153,9 +186,7 @@ def recv_some(fd: c_int, buf: Pointer[UInt8, _], size: Int) raises -> Int: Returns the bytes read, 0 at end of stream, or -1 if nothing is waiting. """ while True: - var n = external_call["recv", c_ssize_t]( - fd, buf.unsafe_bitcast[NoneType](), c_size_t(size), MSG_DONTWAIT - ) + var n = _recv(fd, Int(buf), size, MSG_DONTWAIT) if n >= 0: return Int(n) var e = get_errno().value @@ -192,22 +223,12 @@ def _new_socket() raises -> c_int: _fail("socket") var one = stack_allocation[1, c_int]() one[] = 1 - _ = external_call["setsockopt", c_int]( - fd, SOL_SOCKET, SO_REUSEADDR, one.unsafe_bitcast[NoneType](), c_uint(4) - ) + _ = _setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, Int(one), c_int(4)) # Frames are small request/response pairs: without this, Nagle's algorithm # and delayed ACKs stall each one for milliseconds. - _ = external_call["setsockopt", c_int]( - fd, IPPROTO_TCP, TCP_NODELAY, one.unsafe_bitcast[NoneType](), c_uint(4) - ) + _ = _setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, Int(one), c_int(4)) comptime if not CompilationTarget.is_linux(): - _ = external_call["setsockopt", c_int]( - fd, - SOL_SOCKET, - SO_NOSIGPIPE, - one.unsafe_bitcast[NoneType](), - c_uint(4), - ) + _ = _setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, Int(one), c_int(4)) set_keepalive(fd) return fd @@ -224,9 +245,7 @@ def keepalive_seconds() -> Int: def _setopt(fd: c_int, level: c_int, name: c_int, value: Int): var v = stack_allocation[1, c_int]() v[] = c_int(value) - _ = external_call["setsockopt", c_int]( - fd, level, name, v.unsafe_bitcast[NoneType](), c_uint(4) - ) + _ = _setsockopt(fd, level, name, Int(v), c_int(4)) def set_keepalive(fd: c_int): @@ -259,12 +278,7 @@ def get_int_option(fd: c_int, level: c_int, name: c_int) -> Int: v[] = 0 var len = stack_allocation[1, c_uint]() len[] = c_uint(4) - if ( - external_call["getsockopt", c_int]( - fd, level, name, v.unsafe_bitcast[NoneType](), len - ) - != 0 - ): + if _getsockopt(fd, level, name, Int(v), Int(len)) != 0: return -1 return Int(v[]) @@ -301,7 +315,7 @@ def _refuse_self(fd: c_int) raises: for i in range(2, 8): if mine[i] != theirs[i]: return - _ = external_call["close", c_int](fd) + _ = close_fd(fd) raise Error("connect: the socket connected to itself") @@ -312,12 +326,7 @@ def socket_error(fd: c_int) -> c_int: err[] = 0 var len = stack_allocation[1, c_uint]() len[] = c_uint(4) - if ( - external_call["getsockopt", c_int]( - fd, SOL_SOCKET, SO_ERROR, err.unsafe_bitcast[NoneType](), len - ) - != 0 - ): + if _getsockopt(fd, SOL_SOCKET, SO_ERROR, Int(err), Int(len)) != 0: return get_errno().value return err[] @@ -331,25 +340,18 @@ def _connect( is a `poll` that can end.""" var fd = _new_socket() _ = set_nonblocking(fd) - if ( - external_call["connect", c_int]( - fd, - addr.unsafe_ptr().unsafe_bitcast[NoneType](), - c_uint(SOCKADDR_IN_SIZE), - ) - != 0 - ): + if _c_connect(fd, Int(addr.unsafe_ptr()), c_int(SOCKADDR_IN_SIZE)) != 0: var e = get_errno().value if e != EINPROGRESS and e != EINTR: - _ = external_call["close", c_int](fd) + _ = close_fd(fd) raise Error(what, " failed: errno ", e) var rev = poll([fd], [POLLOUT], timeout_ms) if rev[0] == 0: - _ = external_call["close", c_int](fd) + _ = close_fd(fd) raise Error(what, " timed out after ", timeout_ms, " ms") var err = socket_error(fd) if err != 0: - _ = external_call["close", c_int](fd) + _ = close_fd(fd) raise Error(what, " failed: errno ", err) _refuse_self(fd) return fd @@ -407,17 +409,10 @@ struct Stream(Movable): var addr = _sockaddr(ip, port) var fd = _new_socket() _ = set_nonblocking(fd) - if ( - external_call["connect", c_int]( - fd, - addr.unsafe_ptr().unsafe_bitcast[NoneType](), - c_uint(SOCKADDR_IN_SIZE), - ) - != 0 - ): + if _c_connect(fd, Int(addr.unsafe_ptr()), c_int(SOCKADDR_IN_SIZE)) != 0: var e = get_errno().value if e != EINPROGRESS and e != EINTR: - _ = external_call["close", c_int](fd) + _ = close_fd(fd) raise Error( "connect to ", addr_key(ip, port), " failed: errno ", e ) @@ -452,12 +447,10 @@ struct Stream(Movable): """Writes every byte of `data`.""" var sent = 0 while sent < len(data): - var n = external_call["send", c_ssize_t]( + var n = _send( self.fd, - data.unsafe_ptr() - .unsafe_offset(sent) - .unsafe_bitcast[NoneType](), - c_size_t(len(data) - sent), + Int(data.unsafe_ptr().unsafe_offset(sent)), + len(data) - sent, MSG_NOSIGNAL, ) if n < 0: @@ -478,11 +471,8 @@ struct Stream(Movable): """ var got = 0 while got < size: - var n = external_call["recv", c_ssize_t]( - self.fd, - buf.unsafe_offset(got).unsafe_bitcast[NoneType](), - c_size_t(size - got), - c_int(0), + var n = _recv( + self.fd, Int(buf.unsafe_offset(got)), size - got, c_int(0) ) if n < 0: var e = get_errno().value @@ -501,12 +491,12 @@ struct Stream(Movable): def close(mut self): if self.fd >= 0: - _ = external_call["close", c_int](self.fd) + _ = close_fd(self.fd) self.fd = -1 def __deinit__(deinit self): if self.fd >= 0: - _ = external_call["close", c_int](self.fd) + _ = close_fd(self.fd) def wall_ns() -> UInt64: @@ -593,12 +583,12 @@ struct Listener(Movable): def close(mut self): if self.fd >= 0: - _ = external_call["close", c_int](self.fd) + _ = close_fd(self.fd) self.fd = -1 def __deinit__(deinit self): if self.fd >= 0: - _ = external_call["close", c_int](self.fd) + _ = close_fd(self.fd) comptime POLLIN: Int16 = 0x1 @@ -623,9 +613,7 @@ def poll( at.unsafe_bitcast[Int32]().unsafe_write(Int32(fds[i])) at.unsafe_offset(4).unsafe_bitcast[Int16]().unsafe_write(events[i]) while True: - var r = external_call["poll", c_int]( - base.unsafe_bitcast[NoneType](), c_uint(n), c_int(timeout_ms) - ) + var r = _c_poll(Int(base), n, c_int(timeout_ms)) if r >= 0: break if get_errno().value != EINTR: diff --git a/tests/test_libc_abi.mojo b/tests/test_libc_abi.mojo new file mode 100644 index 0000000..3afcf06 --- /dev/null +++ b/tests/test_libc_abi.mojo @@ -0,0 +1,100 @@ +"""dista declares libc's socket calls as Mosaad-M/tcp and tls do. + +Mojo allows one signature per C symbol in a program, so a binary importing +dista and tcp (or tls, or requests) compiles only if both declare `send`, +`recv`, `connect`, `close`, `setsockopt`, `getsockopt` and `poll` alike. +This test declares them as tcp does, in the same program as dista's own, and +sends a byte over a dista socket through them. +""" + +from std.ffi import external_call +from std.memory.alloc import unsafe_alloc +from std.sys.info import CompilationTarget +from std.testing import TestSuite, assert_equal + +from dista.socket import Listener, Stream + + +def _tcp_send(fd: Int32, buf_addr: Int, length: Int, flags: Int32) -> Int: + return external_call["send", Int](fd, buf_addr, length, flags) + + +def _tcp_recv(fd: Int32, buf_addr: Int, length: Int, flags: Int32) -> Int: + return external_call["recv", Int](fd, buf_addr, length, flags) + + +def _tcp_connect(fd: Int32, addr: Int, addrlen: Int32) -> Int32: + return external_call["connect", Int32](fd, addr, addrlen) + + +def _tcp_close(fd: Int32) -> Int32: + return external_call["close", Int32](fd) + + +def _tcp_setsockopt( + fd: Int32, level: Int32, name: Int32, value: Int, length: Int32 +) -> Int32: + return external_call["setsockopt", Int32](fd, level, name, value, length) + + +def _tcp_getsockopt( + fd: Int32, level: Int32, name: Int32, value: Int, length_addr: Int +) -> Int32: + return external_call["getsockopt", Int32]( + fd, level, name, value, length_addr + ) + + +def _tcp_poll(fds_addr: Int, nfds: Int, timeout_ms: Int32) -> Int32: + return external_call["poll", Int32](fds_addr, nfds, timeout_ms) + + +def _is_macos() -> Bool: + return CompilationTarget.is_macos() + + +def test_dista_and_tcp_declarations_link_together() raises: + var l = Listener(0) + var c = Stream.connect(l.port) + var s = l.accept() + var one = List[UInt8](length=1, fill=7) + assert_equal(_tcp_send(c.fd, Int(one.unsafe_ptr()), 1, Int32(0)), 1) + assert_equal(one[0], 7) # keeps `one` alive across the send + var got = List[UInt8](length=1, fill=0) + assert_equal(s.read_into(got.unsafe_ptr(), 1), 1) + assert_equal(got[0], 7) + s.write_all(got) + # The stream is non-blocking: wait for the echo with poll(POLLIN). + var pfd = List[Int32](length=2, fill=0) + pfd[0] = c.fd + pfd[1] = 1 # events = POLLIN, revents = 0 + assert_equal(_tcp_poll(Int(pfd.unsafe_ptr()), 1, Int32(5000)), 1) + assert_equal(pfd[0], c.fd) # keeps `pfd` alive across the poll + var back = List[UInt8](length=1, fill=0) + assert_equal(_tcp_recv(c.fd, Int(back.unsafe_ptr()), 1, Int32(0)), 1) + assert_equal(back[0], 7) + # The other declarations: each call is real, and fails or does nothing. + var v = unsafe_alloc[Int32](2) + v[unsafe_offset=0] = 99 + v[unsafe_offset=1] = 4 # the option's length, after its value + var level: Int32 = 0xFFFF if _is_macos() else 1 + var so_error: Int32 = 0x1007 if _is_macos() else 4 + var r = _tcp_getsockopt( + c.fd, level, so_error, Int(v), Int(v.unsafe_offset(1)) + ) + var err = v[unsafe_offset=0] + v.unsafe_free() + assert_equal(r, 0) + assert_equal(err, 0) + assert_equal(_tcp_setsockopt(-1, 0, 0, 0, Int32(0)), -1) + assert_equal(_tcp_connect(-1, 0, Int32(0)), -1) + assert_equal(_tcp_poll(Int(pfd.unsafe_ptr()), 0, Int32(0)), 0) + assert_equal(_tcp_close(-1), -1) + # Both ends stay open until here: a value dies after its last use, which + # can be before a call that was passed its `fd` runs. + c.close() + s.close() + + +def main() raises: + TestSuite.discover_tests[__functions_in_module()]().run()