From efdea5b2eeba636126b6a3c3409678ad5f860149 Mon Sep 17 00:00:00 2001 From: Sunrisepeak Date: Thu, 12 Nov 2026 19:08:12 +0900 Subject: [PATCH] 0.29.3: what a readiness enquiry held is delivered with the rest, by recv as by read; handles retired before they are closed --- README.md | 16 ++++++++++++++- examples/net/src/main.c | 45 +++++++++++++++++++++++++++++++++++++++++ mcpp.toml | 8 ++++---- port/src/okm.h | 5 +++-- port/src/okm_net.c | 2 +- port/src/okm_poll.c | 37 ++++++++++++++++++++++++++++++++- port/src/okm_syscall.c | 34 ++----------------------------- 7 files changed, 106 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 2385ff4..dc6a8f8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ the claim can be checked rather than repeated. ```toml [dependencies] -openkal-musl = "0.29.2" +openkal-musl = "0.29.3" ``` The package states compatible ranges for the packages beneath it: OpenKal ^0.25, @@ -1010,6 +1010,20 @@ The port is Apache-2.0. The vendored musl sources under `musl/` are MIT, and ## OpenKal 0.25 dependency group +Version 0.29.3 delivers what a readiness enquiry held together with what else has +arrived, for every call that delivers it. `poll` and `select` answer for a +connection by taking one byte and holding it. `read` returned that byte with the +rest of what had arrived; `recv`, `recvfrom` and `recvmsg` returned the byte alone, +so a program that polls before each `recv` took a stream one byte per wakeup: +1000 calls for 1000 bytes sent in one, where `read` and a kernel's socket take one +(#52). The delivery is one function, `okm_take_ahead`, which both paths call. +`examples/net` sends a thousand bytes and counts the calls through `poll` and +`recv`, and through `poll` and `recvmsg` with two buffers. This version also names +openkal-linux ^0.26.1, openkal-macos ^0.23.1 and openkal-windows ^0.22.1, which +retire a handle before closing it (openkal-linux#53): above this C library a file +opened while another thread closed a socket had its reads and writes reach +descriptor 0. + Version 0.29.2 places further images with openkal-ld ^0.6 (openkal-image 0.6.0, whose members share one version); the loader itself is unchanged. diff --git a/examples/net/src/main.c b/examples/net/src/main.c index 96251ca..6b0b4e1 100644 --- a/examples/net/src/main.c +++ b/examples/net/src/main.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "../../../tests/mtest_protocol.h" @@ -138,6 +139,50 @@ int main(void) } check(have == 4 && memcmp(in, "pong", 4) == 0, "and arrive back"); + /* --- what a readiness enquiry held, and the rest ------------------------- */ + + /* A `poll' holds one byte to make its answer true. The call that delivers + * the byte delivers what else has arrived with it: `read' did, and `recv', + * `recvfrom' and `recvmsg' returned the one byte until 0.29.3, so a client + * that polls before each `recv' took 1000 bytes in 1000 calls + * (openkal-musl#52). A kernel's socket returns the 1000 in one. */ + { + char out[1000], big[4096]; + memset(out, 'x', sizeof out); + check(send(srv, out, sizeof out, 0) == (ssize_t)sizeof out, "a thousand bytes are sent in one call"); + struct pollfd rp = { cli, POLLIN, 0 }; + check(poll(&rp, 1, 2000) == 1 && (rp.revents & POLLIN), "the connection is readable"); + long got1000 = 0, calls = 0, first = 0; + while (got1000 < (long)sizeof out) { + if (calls && poll(&rp, 1, 2000) != 1) break; + const ssize_t r = recv(cli, big, sizeof big, 0); + if (r <= 0) break; + if (calls++ == 0) first = r; + got1000 += r; + } + printf(" poll + recv: the first call returned %ld bytes, %ld calls for 1000\n", first, calls); + check(got1000 == (long)sizeof out, "the thousand arrive through poll and recv"); + check(first > 1, "a recv after a poll returns what has arrived, not the one byte the poll held"); + + check(send(srv, out, sizeof out, 0) == (ssize_t)sizeof out, "a thousand more are sent"); + check(poll(&rp, 1, 2000) == 1 && (rp.revents & POLLIN), "the connection is readable again"); + char head[8], tail[4096]; + struct iovec iov[2] = { { head, sizeof head }, { tail, sizeof tail } }; + struct msghdr mh; + memset(&mh, 0, sizeof mh); + mh.msg_iov = iov; mh.msg_iovlen = 2; + const ssize_t m = recvmsg(cli, &mh, 0); + printf(" poll + recvmsg: the call returned %ld bytes\n", (long)m); + check(m > (ssize_t)sizeof head, "a recvmsg after a poll fills its first buffer and goes on to the second"); + long left = (long)sizeof out - (long)m; + while (left > 0) { + const ssize_t r = recv(cli, big, sizeof big, 0); + if (r <= 0) break; + left -= r; + } + check(left == 0, "and the rest of the thousand follow"); + } + /* --- half-closure ------------------------------------------------------- */ check(shutdown(cli, SHUT_WR) == 0, "the client ends its half of the connection"); diff --git a/mcpp.toml b/mcpp.toml index c6ed3dd..bef68fa 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "openkal-musl" -version = "0.29.2" +version = "0.29.3" # `embed' and `exclude' on a dependency edge (the allocator, below) are mcpp # 2026.11.2.1's; an older engine refuses the edge rather than building a C # library without its allocator. @@ -190,7 +190,7 @@ openkal = "^0.25.0" # # The consequence for a program is that it names this package and nothing else. [target.'cfg(os = "linux")'.dependencies] -openkal-linux = { version = "^0.26.0", features = ["standalone"] } +openkal-linux = { version = "^0.26.1", features = ["standalone"] } # FreeBSD (0.29.1): the kernel-level implementation, standalone, as Linux's is. # This C library is written above openkal, so the implementation beneath it is @@ -207,7 +207,7 @@ openkal-freebsd = { version = "^0.1.0", features = ["standalone"] } # is the shape of a check that silently does not run --- while the same # consumer was answered on Linux and Windows, whose implementations have # carried the array since openkal-linux 0.15.0 and openkal-windows 0.10.0. -openkal-macos = { version = "^0.23.0", features = ["standalone"] } +openkal-macos = { version = "^0.23.1", features = ["standalone"] } # FIRST STEP TOWARD A BARE MACHINE, AND NOT THE WHOLE OF IT. # @@ -292,7 +292,7 @@ sources = [ requires = ["mcpp:compiler-runtime=compiler-rt"] [target.'cfg(windows)'.dependencies] -openkal-windows = { version = "^0.21.0", features = ["standalone"] } +openkal-windows = { version = "^0.22.1", features = ["standalone"] } # The feature macros musl's own build establishes. # diff --git a/port/src/okm.h b/port/src/okm.h index 4a02ac9..baf79cd 100644 --- a/port/src/okm.h +++ b/port/src/okm.h @@ -364,7 +364,8 @@ int okm_sock_shape(struct okm_desc* d); /* --- readiness and bounded transfer, in okm_poll.c ------------------------- */ -/* The read-ahead a readiness enquiry left, delivered to a caller of `read'. +/* The read-ahead a readiness enquiry left, delivered to a caller of `read' or + * of `recv', with whatever else has already arrived on `stream' behind it. * * > 0 the count placed in the buffer * 0 nothing was held; the caller performs its own transfer @@ -374,7 +375,7 @@ int okm_sock_shape(struct okm_desc* d); * the same end again at once, so neither is lost by * forgetting it. */ #define OKM_AHEAD_EOF (-1L) -long okm_take_ahead(struct okm_desc* d, void* buf, unsigned long len); +long okm_take_ahead(struct okm_desc* d, kal_uintptr stream, void* buf, unsigned long len); /* A transfer bounded in time. `ns' of zero is openkal's spelling of "no bound" * and is not what a non-blocking descriptor wants; `OKM_NOW_NS' is the smallest diff --git a/port/src/okm_net.c b/port/src/okm_net.c index 8e1a22d..8503e4f 100644 --- a/port/src/okm_net.c +++ b/port/src/okm_net.c @@ -631,7 +631,7 @@ long okm_sock_recv(int fd, void* buf, unsigned long len, int flags, if (s->state != OKM_SOCK_CONN) return -ENOTCONN; if (addr && alen) *alen = 0; - const long held = okm_take_ahead(d, buf, len); + const long held = okm_take_ahead(d, d->stream, buf, len); if (held == OKM_AHEAD_EOF) return 0; if (held) return held; diff --git a/port/src/okm_poll.c b/port/src/okm_poll.c index a8b6ffc..77b5062 100644 --- a/port/src/okm_poll.c +++ b/port/src/okm_poll.c @@ -85,12 +85,47 @@ long okm_timed_write(kal_uintptr stream, const void* buf, unsigned long len, kal /* --- the read-ahead ---------------------------------------------------------- */ -long okm_take_ahead(struct okm_desc* d, void* buf, unsigned long len) +long okm_take_ahead(struct okm_desc* d, kal_uintptr stream, void* buf, unsigned long len) { if (!d) return 0; if (d->ahead && len) { ((unsigned char*)buf)[0] = d->ahead_byte; d->ahead = 0; + /* AND WHATEVER ELSE IS ALREADY THERE, BECAUSE ONE BYTE ON ITS OWN + * TURNED EVERY POLLED READ INTO A POLLED READ OF ONE BYTE. + * + * The enquiry takes a byte to make its answer true. Returning only that + * byte is a legal short read --- and a caller that polls goes straight + * back to `poll', which takes another byte, so the whole of a stream + * arrives ONE BYTE PER ITERATION, for ever. Measured against the host, + * on `echo one; sleep 0.4; echo two': + * + * here "o" "n" "e" "." "t" "w" "o" "." eight chunks + * host "one." "two." two + * + * Every byte is delivered and in order, so a caller that concatenates + * sees the right bytes --- which is why this survived: the defect is + * invisible to anyone who does not look at the BOUNDARIES. A caller that + * scans a chunk for a word finds none, because `two' arrives as `t' and + * `wo'. openkal-linux#13's first report said the program `only output + * one byte', and this is that. + * + * THE REMEDY IS THE OPERATION THE ENQUIRY ITSELF IS BUILT ON. A bound + * of `now' asks for whatever has already arrived and does not wait, so + * the byte and the rest of what is there come back together. Where the + * environment beneath declines `openkal.timeout' there is no such + * operation, and the single byte is what can be honestly returned. + * + * ONE PLACE, FOR EVERY CALL THAT DELIVERS THE BYTE (0.29.3). `read' did + * this and `recv' did not: `recv', `recvfrom' and `recvmsg' after a + * `poll' returned the one byte, so a client that polls before each + * `recv' took a response in one byte per wakeup (openkal-musl#52: + * 1000 calls for 1000 bytes, where `read' took one). The rest is read + * here, so a call that delivers the byte cannot leave it out. */ + if (len > 1 && okm_can_bound()) { + const long more = okm_timed_read(stream, (char*)buf + 1, len - 1, OKM_NOW_NS); + if (more > 0) return 1 + more; + } return 1; } if (d->ahead_eof) { d->ahead_eof = 0; return OKM_AHEAD_EOF; } diff --git a/port/src/okm_syscall.c b/port/src/okm_syscall.c index 21b3ebc..6270965 100644 --- a/port/src/okm_syscall.c +++ b/port/src/okm_syscall.c @@ -202,39 +202,9 @@ static syscall_arg_t do_read_stream(int fd, void* buf, size_t len) * what made that enquiry's answer true rather than momentary. okm_poll.c * states why openkal leaves no other way to answer one. A short read is a * result every caller of `read' already handles. */ - const long held = okm_take_ahead(d, buf, len); + const long held = okm_take_ahead(d, s, buf, len); if (held == OKM_AHEAD_EOF) return 0; - if (held) { - /* AND WHATEVER ELSE IS ALREADY THERE, BECAUSE ONE BYTE ON ITS OWN - * TURNED EVERY POLLED READ INTO A POLLED READ OF ONE BYTE. - * - * The enquiry takes a byte to make its answer true. Returning only that - * byte is a legal short read --- and a caller that polls goes straight - * back to `poll', which takes another byte, so the whole of a stream - * arrives ONE BYTE PER ITERATION, for ever. Measured against the host, - * on `echo one; sleep 0.4; echo two': - * - * here "o" "n" "e" "." "t" "w" "o" "." eight chunks - * host "one." "two." two - * - * Every byte is delivered and in order, so a caller that concatenates - * sees the right bytes --- which is why this survived: the defect is - * invisible to anyone who does not look at the BOUNDARIES. A caller that - * scans a chunk for a word finds none, because `two' arrives as `t' and - * `wo'. openkal-linux#13's first report said the program `only output - * one byte', and this is that. - * - * THE REMEDY IS THE OPERATION THE ENQUIRY ITSELF IS BUILT ON. A bound - * of `now' asks for whatever has already arrived and does not wait, so - * the byte and the rest of what is there come back together. Where the - * environment beneath declines `openkal.timeout' there is no such - * operation, and the single byte is what can be honestly returned. */ - if (len > 1 && okm_can_bound()) { - const long more = okm_timed_read(s, (char*)buf + 1, len - 1, OKM_NOW_NS); - if (more > 0) return (syscall_arg_t)(held + more); - } - return (syscall_arg_t)held; - } + if (held) return (syscall_arg_t)held; if (d->flags & O_NONBLOCK) return okm_timed_read(s, buf, len, OKM_NOW_NS);