| @@ -24,20 +24,24 @@ |
| 24 | 24 | //! another actor holding it can stop a handshake that hangs. |
| 25 | 25 | |
| 26 | 26 | use std::io::{ErrorKind, Read, Write}; |
| 27 | | −use std::net::{SocketAddr, ToSocketAddrs}; |
| 28 | | −use std::sync::Arc; |
| 27 | +use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; |
| 29 | 28 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 29 | +use std::sync::{Arc, OnceLock}; |
| 30 | 30 | |
| 31 | 31 | use mio::net::{TcpListener, TcpStream}; |
| 32 | 32 | use mio::{Interest, Token}; |
| 33 | 33 | |
| 34 | 34 | use crate::abi::{Ctx, UNIT, Value, YIELD_NONE, int}; |
| 35 | +use crate::blocking::{IO, Thunk, tl_call_blocking}; |
| 35 | 36 | use crate::heap::drop_value; |
| 36 | 37 | use crate::int::saturating; |
| 37 | 38 | use crate::otp::Wait; |
| 39 | +use crate::process::Code; |
| 38 | 40 | use crate::resource::{tl_resource_get, tl_resource_new}; |
| 39 | 41 | use crate::sched::{park, this}; |
| 40 | | −use crate::values::{bytes_from, bytes_of, cell, error, list, ok, string, text}; |
| 42 | +use crate::values::{ |
| 43 | + bytes_from, bytes_of, cell, error, list, ok, string, text, tl_borrowed1, tl_borrowed2, |
| 44 | +}; |
| 41 | 45 | |
| 42 | 46 | enum Socket { |
| 43 | 47 | Listener(TcpListener), |
| @@ -152,6 +156,163 @@ pub(crate) unsafe fn wait_for(ctx: *mut Ctx, token: Token, seen: u64) -> bool { |
| 152 | 156 | } |
| 153 | 157 | } |
| 154 | 158 | |
| 159 | +/// The clauses of `std/net.net`, in `louise_rt_prims::NET_OPS` order. Only |
| 160 | +/// resolution and the two address-level opening operations belong to this root. |
| 161 | +/// Socket values returned by it retain their existing handle-mediated `<io>` |
| 162 | +/// operations (`accept`, `read`, `write`, `peer` and close). |
| 163 | +const NET_CLAUSES: [(Code, Code); louise_rt_prims::NET_OPS.len()] = [ |
| 164 | + (op_resolve, tl_borrowed1), |
| 165 | + (op_listen_at, tl_borrowed2), |
| 166 | + (op_connect_to, tl_borrowed2), |
| 167 | +]; |
| 168 | + |
| 169 | +/// The immortal root capability for `net`: three stateless clauses, shared by |
| 170 | +/// every actor, outside the counted heap (ADR 0084). |
| 171 | +pub(crate) fn net_cap() -> Value { |
| 172 | + static CAP: OnceLock<Value> = OnceLock::new(); |
| 173 | + *CAP.get_or_init(|| crate::clock::immortal_cap(&NET_CLAUSES)) |
| 174 | +} |
| 175 | + |
| 176 | +/// Consume an owned remote-denied invocation without inspecting its host or |
| 177 | +/// entering either the resolver pool or socket code. All three net operations |
| 178 | +/// have a `Result<_, String>` result. |
| 179 | +unsafe fn deny_result(ctx: *mut Ctx, this_: Value, args: &[Value]) -> Value { |
| 180 | + // SAFETY: the root invocation owns `this_` and every argument. |
| 181 | + unsafe { |
| 182 | + drop_value(ctx, this_); |
| 183 | + for &arg in args { |
| 184 | + drop_value(ctx, arg); |
| 185 | + } |
| 186 | + } |
| 187 | + error(ctx, louise_rt_prims::NET_REMOTE_DENIED) |
| 188 | +} |
| 189 | + |
| 190 | +unsafe extern "C" fn resolve_thunk(ctx: *mut Ctx, args: *const Value) -> Value { |
| 191 | + #[cfg(test)] |
| 192 | + note_io_thread(); |
| 193 | + // SAFETY: the I/O pool passes one live borrowed host string. The retained |
| 194 | + // C ABI has no capability argument and receives exactly the old arguments. |
| 195 | + unsafe { tl_net_resolve(ctx, args.read()) } |
| 196 | +} |
| 197 | + |
| 198 | +unsafe extern "C" fn op_resolve( |
| 199 | + ctx: *mut Ctx, |
| 200 | + this_: Value, |
| 201 | + host: Value, |
| 202 | + _: Value, |
| 203 | + _: Value, |
| 204 | + _: Value, |
| 205 | + _: Value, |
| 206 | +) -> Value { |
| 207 | + // SAFETY: an owned root-clause invocation on the running actor. |
| 208 | + unsafe { |
| 209 | + if crate::sched::remote_net_denied(ctx) { |
| 210 | + return deny_result(ctx, this_, &[host]); |
| 211 | + } |
| 212 | + drop_value(ctx, this_); |
| 213 | + // Only this blocking DNS step leaves the scheduler. Address opening |
| 214 | + // below keeps using the existing nonblocking reactor paths. |
| 215 | + let out = tl_call_blocking(ctx, IO, resolve_thunk as Thunk, 1, &host); |
| 216 | + drop_value(ctx, host); |
| 217 | + out |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +/// Call an old borrowed two-argument socket kernel from its owned root clause. |
| 222 | +/// The arguments stay live across reactor parking; a stopped actor returns |
| 223 | +/// through the same unwind path and still releases its owned references once. |
| 224 | +unsafe fn opening2( |
| 225 | + ctx: *mut Ctx, |
| 226 | + this_: Value, |
| 227 | + address: Value, |
| 228 | + port: Value, |
| 229 | + kernel: unsafe extern "C" fn(*mut Ctx, Value, Value) -> Value, |
| 230 | +) -> Value { |
| 231 | + // SAFETY: the root owns all inputs; the old kernels merely borrow the two |
| 232 | + // operation arguments and have their original fixed C arity. |
| 233 | + unsafe { |
| 234 | + // The root deliberately exposes address-level operations. Refuse a |
| 235 | + // hostname rather than letting ToSocketAddrs do scheduler-thread DNS; |
| 236 | + // high-level listen/connect still resolve through the I/O pool first. |
| 237 | + let literal = text(address).parse::<IpAddr>().is_ok(); |
| 238 | + if !literal { |
| 239 | + drop_value(ctx, this_); |
| 240 | + drop_value(ctx, address); |
| 241 | + drop_value(ctx, port); |
| 242 | + return error(ctx, "address is not an IP literal"); |
| 243 | + } |
| 244 | + drop_value(ctx, this_); |
| 245 | + let out = kernel(ctx, address, port); |
| 246 | + drop_value(ctx, address); |
| 247 | + drop_value(ctx, port); |
| 248 | + out |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +unsafe extern "C" fn op_listen_at( |
| 253 | + ctx: *mut Ctx, |
| 254 | + this_: Value, |
| 255 | + address: Value, |
| 256 | + port: Value, |
| 257 | + _: Value, |
| 258 | + _: Value, |
| 259 | + _: Value, |
| 260 | +) -> Value { |
| 261 | + // SAFETY: an owned root-clause invocation on the running actor. |
| 262 | + unsafe { |
| 263 | + if crate::sched::remote_net_denied(ctx) { |
| 264 | + return deny_result(ctx, this_, &[address, port]); |
| 265 | + } |
| 266 | + opening2(ctx, this_, address, port, tl_net_listen) |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +unsafe extern "C" fn op_connect_to( |
| 271 | + ctx: *mut Ctx, |
| 272 | + this_: Value, |
| 273 | + address: Value, |
| 274 | + port: Value, |
| 275 | + _: Value, |
| 276 | + _: Value, |
| 277 | + _: Value, |
| 278 | +) -> Value { |
| 279 | + // SAFETY: an owned root-clause invocation on the running actor. |
| 280 | + unsafe { |
| 281 | + if crate::sched::remote_net_denied(ctx) { |
| 282 | + return deny_result(ctx, this_, &[address, port]); |
| 283 | + } |
| 284 | + opening2(ctx, this_, address, port, tl_net_connect) |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +#[cfg(test)] |
| 289 | +fn note_io_thread() { |
| 290 | + note_io_threads().lock().expect("net test threads").push( |
| 291 | + std::thread::current() |
| 292 | + .name() |
| 293 | + .unwrap_or("unnamed") |
| 294 | + .to_owned(), |
| 295 | + ); |
| 296 | +} |
| 297 | + |
| 298 | +#[cfg(test)] |
| 299 | +fn io_threads() -> Vec<String> { |
| 300 | + let mut threads = Vec::new(); |
| 301 | + // Bounded by the two explicit resolver calls in the root roundtrip. |
| 302 | + std::mem::swap( |
| 303 | + &mut threads, |
| 304 | + &mut *note_io_threads().lock().expect("net test threads"), |
| 305 | + ); |
| 306 | + threads |
| 307 | +} |
| 308 | + |
| 309 | +#[cfg(test)] |
| 310 | +fn note_io_threads() -> &'static std::sync::Mutex<Vec<String>> { |
| 311 | + use std::sync::Mutex; |
| 312 | + static THREADS: Mutex<Vec<String>> = Mutex::new(Vec::new()); |
| 313 | + &THREADS |
| 314 | +} |
| 315 | + |
| 155 | 316 | /// `resolve(host)`: the addresses `host` names, as text (`127.0.0.1`, |
| 156 | 317 | /// `::1`), in the resolver's order. A lookup can take seconds, so this is |
| 157 | 318 | /// a `blocking` extern: it runs on the blocking pool while its actor waits |
| @@ -503,26 +664,105 @@ pub(crate) unsafe fn upgrade(conn: Value) -> Result<(Arc<TcpStream>, Token), Str |
| 503 | 664 | mod tests { |
| 504 | 665 | use super::*; |
| 505 | 666 | use crate::abi::{Block, UNIT, int}; |
| 506 | | − use crate::values::string; |
| 667 | + #[cfg(test)] |
| 668 | + use crate::heap::dup_value; |
| 669 | + use crate::values::{closure_code_borrowed, string}; |
| 507 | 670 | |
| 508 | | − /// The one field of the `Ok` cell an extern returned. |
| 671 | + #[test] |
| 672 | + fn the_net_capability_is_immortal_and_built_once() { |
| 673 | + let cap = net_cap(); |
| 674 | + assert_eq!(cap, net_cap()); |
| 675 | + // Construction returns the same static clauses without mutating their |
| 676 | + // parameters or allocating replacements in the counted heap. |
| 677 | + // SAFETY: `net_cap` constructs the immutable block and its statically |
| 678 | + // declared three clauses. The tested layout has the known field count. |
| 679 | + unsafe { |
| 680 | + let b = cap as *mut Block; |
| 681 | + assert_eq!((*b).header.rc, 0); |
| 682 | + let n = (*b).header.scan_fsize as usize; |
| 683 | + assert_eq!(n, louise_rt_prims::NET_OPS.len() + 1); |
| 684 | + let fields = Block::fields_ptr(b); |
| 685 | + assert_eq!(fields.add(n - 1).read(), int(0), "the marker"); |
| 686 | + for i in 0..n - 1 { |
| 687 | + let clause = fields.add(i).read() as *mut Block; |
| 688 | + assert_eq!((*clause).header.rc, 0); |
| 689 | + } |
| 690 | + } |
| 691 | + } |
| 692 | + |
| 693 | + /// Perform an operation through its actual root clause, either through the |
| 694 | + /// owned general call or the fixed-arity borrowed adapter. At most two |
| 695 | + /// arguments, never a reinterpreted hidden capability argument. |
| 696 | + unsafe fn perform(ctx: *mut Ctx, op: &str, args: &[Value], owned: bool) -> Value { |
| 697 | + // SAFETY: callers provide the active context and correctly typed live |
| 698 | + // arguments. The root outlives the call; its declared closure carries |
| 699 | + // the actual C function with five fixed argument registers. |
| 700 | + unsafe { |
| 701 | + assert!(args.len() <= 2); |
| 702 | + let i = louise_rt_prims::NET_OPS |
| 703 | + .iter() |
| 704 | + .position(|name| *name == op) |
| 705 | + .expect("a net operation"); |
| 706 | + let cap = crate::process::root_cap(crate::process::RootCap::Net); |
| 707 | + let clause = Block::fields_ptr(cap as *mut Block).add(i).read(); |
| 708 | + if owned { |
| 709 | + let clause = dup_value(clause); |
| 710 | + drop_value(ctx, cap); |
| 711 | + return crate::prims::call_closure(ctx, clause, args); |
| 712 | + } |
| 713 | + let mut regs = [UNIT; 5]; |
| 714 | + regs[..args.len()].copy_from_slice(args); |
| 715 | + let code: Code = std::mem::transmute(closure_code_borrowed(clause)); |
| 716 | + let out = code(ctx, clause, regs[0], regs[1], regs[2], regs[3], regs[4]); |
| 717 | + drop_value(ctx, cap); |
| 718 | + out |
| 719 | + } |
| 720 | + } |
| 721 | + |
| 722 | + /// The one field of the `Ok` cell an operation returned. |
| 509 | 723 | unsafe fn ok0(v: Value) -> Value { |
| 510 | | − // SAFETY: the test reads the cell the extern just made. |
| 724 | + // SAFETY: the test reads the cell the operation just made. |
| 511 | 725 | unsafe { *Block::fields_ptr(v as *mut Block) } |
| 512 | 726 | } |
| 513 | 727 | |
| 514 | | − /// Accepted and connected sockets both have Nagle off: a response is |
| 515 | | − /// one flushed write per chunk (ADR 0053), which Nagle would hold |
| 516 | | − /// back for a delayed ACK. |
| 517 | | − unsafe extern "C" fn accepted_sockets_disable_nagle(ctx: *mut Ctx) -> Value { |
| 518 | | − // SAFETY: the test follows the generated-code convention; every |
| 519 | | − // value it makes is dropped below. Loopback: the connect is |
| 520 | | − // established at once, so the accept never parks. |
| 728 | + /// Resolve, open, connect and accept through the real root, on loopback only. |
| 729 | + /// Both owned and borrowed root adapters are exercised and the returned |
| 730 | + /// socket Resources are then used by the same handle-mediated io kernels. |
| 731 | + unsafe extern "C" fn root_roundtrip(ctx: *mut Ctx) -> Value { |
| 732 | + // SAFETY: generated-code convention; every result is released below. |
| 733 | + // Loopback connect is established before the matching accept here. |
| 521 | 734 | unsafe { |
| 522 | 735 | let host = string(ctx, "127.0.0.1"); |
| 523 | | − let listener = tl_net_listen(ctx, host, int(0)); |
| 736 | + let resolved = perform(ctx, "resolve", &[dup_value(host)], true); |
| 737 | + assert_eq!((*(resolved as *mut Block)).header.tag, 0, "resolver Ok"); |
| 738 | + let resolved_borrowed = perform(ctx, "resolve", &[host], false); |
| 739 | + assert_eq!( |
| 740 | + (*(resolved_borrowed as *mut Block)).header.tag, |
| 741 | + 0, |
| 742 | + "borrowed resolver Ok" |
| 743 | + ); |
| 744 | + let hostname = string(ctx, "not-a-literal.invalid"); |
| 745 | + let bad_listen = perform(ctx, "listen_at", &[dup_value(hostname), int(0)], true); |
| 746 | + let bad_connect = perform(ctx, "connect_to", &[hostname, int(443)], false); |
| 747 | + for refused in [bad_listen, bad_connect] { |
| 748 | + assert_eq!( |
| 749 | + (*(refused as *mut Block)).header.tag, |
| 750 | + 1, |
| 751 | + "address-level roots must not do scheduler DNS" |
| 752 | + ); |
| 753 | + let why = Block::fields_ptr(refused as *mut Block).read(); |
| 754 | + assert_eq!( |
| 755 | + text(why), |
| 756 | + "address is not an IP literal", |
| 757 | + "a resolver error is not the deterministic guard" |
| 758 | + ); |
| 759 | + } |
| 760 | + |
| 761 | + let listener = perform(ctx, "listen_at", &[dup_value(host), int(0)], true); |
| 762 | + assert_eq!((*(listener as *mut Block)).header.tag, 0, "listener Ok"); |
| 524 | 763 | let port = tl_net_port(ctx, ok0(listener)); |
| 525 | | − let conn = tl_net_connect(ctx, host, port); |
| 764 | + let conn = perform(ctx, "connect_to", &[host, port], false); |
| 765 | + assert_eq!((*(conn as *mut Block)).header.tag, 0, "connection Ok"); |
| 526 | 766 | let accepted = tl_net_accept(ctx, ok0(listener)); |
| 527 | 767 | for end in [ok0(conn), ok0(accepted)] { |
| 528 | 768 | let Socket::Stream(s) = &handle(end).socket else { |
| @@ -530,18 +770,39 @@ mod tests { |
| 530 | 770 | }; |
| 531 | 771 | assert!(s.nodelay().expect("nodelay is readable")); |
| 532 | 772 | } |
| 533 | | − for v in [host, listener, conn, accepted] { |
| 773 | + tl_net_close(ctx, ok0(conn)); |
| 774 | + tl_net_close(ctx, ok0(accepted)); |
| 775 | + tl_net_close_listener(ctx, ok0(listener)); |
| 776 | + for v in [ |
| 777 | + resolved, |
| 778 | + resolved_borrowed, |
| 779 | + bad_listen, |
| 780 | + bad_connect, |
| 781 | + hostname, |
| 782 | + host, |
| 783 | + listener, |
| 784 | + conn, |
| 785 | + accepted, |
| 786 | + ] { |
| 534 | 787 | drop_value(ctx, v); |
| 535 | 788 | } |
| 789 | + let threads = io_threads(); |
| 790 | + assert_eq!(threads.len(), 2, "one record per resolution: {threads:?}"); |
| 791 | + assert!( |
| 792 | + threads |
| 793 | + .iter() |
| 794 | + .all(|name| name.starts_with("louise-blocking-io-")), |
| 795 | + "DNS must not run on a scheduler: {threads:?}" |
| 796 | + ); |
| 536 | 797 | } |
| 537 | 798 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 538 | 799 | UNIT |
| 539 | 800 | } |
| 540 | 801 | |
| 541 | 802 | #[test] |
| 542 | | − fn sockets_disable_nagle() { |
| 803 | + fn root_adapters_resolve_and_open_loopback_sockets_without_leaks() { |
| 543 | 804 | // SAFETY: the test follows the generated-code convention. |
| 544 | | − let code = unsafe { crate::sched::run_test_actor(accepted_sockets_disable_nagle) }; |
| 805 | + let code = unsafe { crate::sched::run_test_actor(root_roundtrip) }; |
| 545 | 806 | assert_eq!(code, 0); |
| 546 | 807 | } |
| 547 | 808 | } |