use std::fmt;
use std::os::fd::AsRawFd;
use std::os::fd::OwnedFd;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::atomic::AtomicI32;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use serde::Deserialize;
use serde::Serialize;
pub const RECORD_SEPARATOR: &str = " DETLOG_RECORD=";
pub const RECORD_SCHEMA: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum DetLogEvent {
Other,
Syscall,
SyscallResult {
finished_syscall_number: u64,
},
SchedulerCommit {
scheduler_turn: u64,
virtual_nanoseconds: u64,
internal_io_poll: bool,
runtime_maps_read: bool,
},
SchedulerCommittedTime,
SchedulerEmptyQueueKick,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DetLogRecord {
pub schema: u32,
pub event: DetLogEvent,
}
impl DetLogRecord {
pub fn new(event: DetLogEvent) -> Self {
Self {
schema: RECORD_SCHEMA,
event,
}
}
pub fn split(message: &str) -> Result<(&str, Option<Self>), String> {
let Some((human, encoded)) = message.rsplit_once(RECORD_SEPARATOR) else {
return Ok((message, None));
};
let record: Self = serde_json::from_str(encoded)
.map_err(|error| format!("malformed DETLOG record: {error}"))?;
if record.schema != RECORD_SCHEMA {
return Err(format!(
"unsupported DETLOG record schema {}; expected {}",
record.schema, RECORD_SCHEMA
));
}
Ok((human, Some(record)))
}
}
#[doc(hidden)]
pub fn record_suffix(event: DetLogEvent) -> String {
let encoded = serde_json::to_string(&DetLogRecord::new(event))
.expect("DETLOG record serialization cannot fail");
format!("{RECORD_SEPARATOR}{encoded}")
}
pub type DetlogForwarder = for<'a> fn(&str, &str, u64, fmt::Arguments<'a>);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForwardPolicy {
default: bool,
targets: Vec<(String, bool)>,
}
impl ForwardPolicy {
pub fn all() -> Self {
Self {
default: true,
targets: Vec::new(),
}
}
pub fn from_current_subscriber(directives: &str) -> Self {
Self::from_probe(directives, info_enabled_for_target)
}
pub fn from_probe(directives: &str, info_enabled: impl Fn(&str) -> bool) -> Self {
let mut targets: Vec<(String, bool)> = Vec::new();
for directive in directives.split(',') {
let target = directive
.split(['[', '='])
.next()
.unwrap_or_default()
.trim();
if target.is_empty() || targets.iter().any(|(named, _)| named == target) {
continue;
}
targets.push((target.to_owned(), info_enabled(target)));
}
Self {
default: info_enabled(""),
targets,
}
}
pub fn forwards_any(&self) -> bool {
self.default || self.targets.iter().any(|(_, enabled)| *enabled)
}
pub fn forwards(&self, target: &str) -> bool {
self.targets
.iter()
.filter(|(named, _)| target.starts_with(named.as_str()))
.max_by_key(|(named, _)| named.len())
.map_or(self.default, |(_, enabled)| *enabled)
}
pub fn encode(&self) -> String {
let mut encoded = String::from(if self.default { "1" } else { "0" });
for (target, enabled) in &self.targets {
encoded.push(',');
encoded.push_str(target);
encoded.push_str(if *enabled { "=1" } else { "=0" });
}
encoded
}
pub fn decode(encoded: &str) -> Result<Self, String> {
fn flag(text: &str) -> Option<bool> {
match text {
"1" => Some(true),
"0" => Some(false),
_ => None,
}
}
let mut entries = encoded.split(',');
let default = entries
.next()
.and_then(flag)
.ok_or_else(|| format!("DETLOG forwarding policy {encoded:?} lacks a 0/1 default"))?;
let mut targets = Vec::new();
for entry in entries {
let parsed = entry
.split_once('=')
.filter(|(target, _)| !target.is_empty())
.and_then(|(target, enabled)| Some((target.to_owned(), flag(enabled)?)));
let Some(parsed) = parsed else {
return Err(format!(
"DETLOG forwarding policy {encoded:?} has malformed entry {entry:?}"
));
};
targets.push(parsed);
}
Ok(Self { default, targets })
}
}
pub fn info_enabled_for_target(target: &str) -> bool {
use tracing::Metadata;
use tracing::callsite::DefaultCallsite;
use tracing::field::FieldSet;
use tracing::level_filters::LevelFilter;
use tracing::metadata::Kind;
static HINT_CALLSITE: DefaultCallsite = DefaultCallsite::new(&HINT_METADATA);
static HINT_METADATA: Metadata<'static> = Metadata::new(
"detlog forwarding hint probe",
"detcore::detlog",
tracing::Level::INFO,
None,
None,
None,
FieldSet::new(&[], tracing::callsite::Identifier(&HINT_CALLSITE)),
Kind::HINT,
);
static PROBE_CALLSITE: DefaultCallsite = DefaultCallsite::new(&PROBE_METADATA);
static PROBE_METADATA: Metadata<'static> = Metadata::new(
"detlog forwarding probe",
"detcore::detlog",
tracing::Level::INFO,
None,
None,
None,
FieldSet::new(&["message"], tracing::callsite::Identifier(&PROBE_CALLSITE)),
Kind::EVENT,
);
if tracing::level_filters::STATIC_MAX_LEVEL < LevelFilter::INFO
|| LevelFilter::current() < LevelFilter::INFO
{
return false;
}
let hint = Metadata::new(
"detlog forwarding hint probe",
target,
tracing::Level::INFO,
None,
None,
Some(target),
FieldSet::new(&[], tracing::callsite::Identifier(&HINT_CALLSITE)),
Kind::HINT,
);
let event = Metadata::new(
"detlog forwarding probe",
target,
tracing::Level::INFO,
None,
None,
Some(target),
FieldSet::new(&["message"], tracing::callsite::Identifier(&PROBE_CALLSITE)),
Kind::EVENT,
);
tracing::dispatcher::get_default(|dispatch| dispatch.enabled(&hint) && dispatch.enabled(&event))
}
static FORWARDER: OnceLock<(DetlogForwarder, ForwardPolicy)> = OnceLock::new();
pub fn set_forwarder(
forwarder: DetlogForwarder,
policy: ForwardPolicy,
) -> Result<(), (DetlogForwarder, ForwardPolicy)> {
FORWARDER.set((forwarder, policy))
}
#[doc(hidden)]
pub fn forwarded_line(target: &str, record_suffix: &str, message: fmt::Arguments<'_>) -> Vec<u8> {
format!("INFO {target}: DETLOG {message}{record_suffix}\n").into_bytes()
}
fn preserving_errno(operation: impl FnOnce()) {
let errno = unsafe { libc::__errno_location() };
let saved = unsafe { *errno };
operation();
unsafe { *errno = saved };
}
pub fn forward_to_stderr(
target: &str,
record_suffix: &str,
_index: u64,
message: fmt::Arguments<'_>,
) {
preserving_errno(|| {
let line = forwarded_line(target, record_suffix, message);
let mut rest = line.as_slice();
while !rest.is_empty() {
let written = unsafe {
libc::write(
libc::STDERR_FILENO,
rest.as_ptr().cast::<libc::c_void>(),
rest.len(),
)
};
if written > 0 {
rest = &rest[written as usize..];
} else if written == 0
|| std::io::Error::last_os_error().kind() != std::io::ErrorKind::Interrupted
{
return;
}
}
});
}
pub const FORWARDING_LOSS_NOTICE: &str = "HERMIT_DETLOG_RECORD_LOST";
pub const FORWARDING_RETIRED_NOTICE: &[u8] = b"HERMIT_DETLOG_RECORD_LOST 0 0 socket retired\n";
static UNREPORTED_LOSSES: AtomicU64 = AtomicU64::new(0);
pub fn send_forwarded_record(
socket: libc::c_int,
target: &str,
record_suffix: &str,
index: u64,
message: fmt::Arguments<'_>,
) {
let send = |bytes: &[u8]| loop {
let sent = unsafe {
libc::send(
socket,
bytes.as_ptr().cast::<libc::c_void>(),
bytes.len(),
libc::MSG_NOSIGNAL,
)
};
if sent >= 0 {
return Ok(());
}
let error = std::io::Error::last_os_error();
if error.kind() != std::io::ErrorKind::Interrupted {
return Err(error);
}
};
let notice = |length: usize, errno: i32| format!("{FORWARDING_LOSS_NOTICE} {length} {errno}\n");
preserving_errno(|| {
let unreported = UNREPORTED_LOSSES.swap(0, Ordering::AcqRel);
if unreported > 0 && send(notice(0, 0).as_bytes()).is_err() {
UNREPORTED_LOSSES.fetch_add(unreported, Ordering::AcqRel);
}
let line = tagged_forwarded_message(
current_tid(),
index,
&forwarded_line(target, record_suffix, message),
);
if let Err(error) = send(&line) {
let errno = error.raw_os_error().unwrap_or(0);
if send(notice(line.len(), errno).as_bytes()).is_err() {
UNREPORTED_LOSSES.fetch_add(1, Ordering::AcqRel);
}
}
});
}
pub fn socket_identity(fd: libc::c_int) -> Option<(u64, u64)> {
let mut metadata: libc::stat = unsafe { std::mem::zeroed() };
let status = unsafe {
crate::util::raw_syscall(
libc::SYS_fstat,
[fd as u64, (&raw mut metadata) as u64, 0, 0, 0, 0],
)
};
(status == 0 && metadata.st_mode & libc::S_IFMT == libc::S_IFSOCK)
.then_some((metadata.st_dev, metadata.st_ino))
}
pub fn tool_output_env_value(fd: libc::c_int) -> std::io::Result<String> {
let (dev, ino) = socket_identity(fd).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"a Tool output descriptor must be a socket",
)
})?;
Ok(format!("{fd}:{dev:016x}:{ino:016x}"))
}
pub fn parse_tool_output_env_value(value: &str) -> Option<(libc::c_int, (u64, u64))> {
let mut fields = value.split(':');
let fd = fields.next()?.parse::<libc::c_int>().ok()?;
let hex = |field: Option<&str>| {
field
.filter(|field| field.len() == 16)
.and_then(|field| u64::from_str_radix(field, 16).ok())
};
let identity = (hex(fields.next())?, hex(fields.next())?);
fields.next().is_none().then_some((fd, identity))
}
pub fn tool_output_env_target(value: &str) -> Option<libc::c_int> {
let (fd, identity) = parse_tool_output_env_value(value)?;
(socket_identity(fd) == Some(identity)).then_some(fd)
}
fn current_tid() -> i32 {
unsafe { libc::syscall(libc::SYS_gettid) as i32 }
}
pub fn tagged_forwarded_message(tid: i32, index: u64, line: &[u8]) -> Vec<u8> {
let mut message = format!("T{tid}.{index} ").into_bytes();
message.extend_from_slice(line);
message
}
pub fn split_forwarded_message(message: &[u8]) -> Option<(i32, u64, &[u8])> {
let rest = message.strip_prefix(b"T")?;
let space = rest.iter().position(|&byte| byte == b' ')?;
let tag = std::str::from_utf8(&rest[..space]).ok()?;
let (tid, index) = tag.split_once('.')?;
let decimal = |digits: &str| !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit());
if !decimal(tid) || !decimal(index) {
return None;
}
Some((tid.parse().ok()?, index.parse().ok()?, &rest[space + 1..]))
}
pub type ForwardedRecordSink = Box<dyn FnMut(&[u8]) + Send>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Runner {
NoTurnYet,
Root(i32),
Exclusive(i32),
None,
}
#[derive(Debug)]
pub(crate) struct ForwardedOrder {
pending: std::collections::BTreeMap<i32, Vec<Vec<u8>>>,
runner: Runner,
ambiguous: bool,
legacy: bool,
received: std::collections::BTreeMap<i32, ReceivedInterval>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct ReceivedInterval {
pub(crate) count: u64,
pub(crate) out_of_sequence: Option<(u64, u64)>,
}
pub const FORWARDING_UNTAGGED_RECORD_NOTICE: &[u8] =
b"HERMIT_DETLOG_RECORD_LOST 0 0 untagged forwarded record from an older runtime\n";
pub const FORWARDING_AMBIGUOUS_ORDER_NOTICE: &[u8] =
b"HERMIT_DETLOG_RECORD_LOST 0 0 forwarded order ambiguous: more than one root\n";
impl ForwardedOrder {
pub(crate) fn new() -> Self {
Self {
pending: Default::default(),
runner: Runner::NoTurnYet,
ambiguous: false,
legacy: false,
received: Default::default(),
}
}
pub(crate) fn queue(&mut self, tid: i32, record: Vec<u8>) {
self.pending.entry(tid).or_default().push(record);
}
pub(crate) fn arrived(&mut self, tid: i32, index: u64) {
let interval = self.received.entry(tid).or_default();
interval.count += 1;
if index != interval.count && interval.out_of_sequence.is_none() {
interval.out_of_sequence = Some((interval.count, index));
}
}
pub(crate) fn received_since_check(&mut self, tid: i32) -> ReceivedInterval {
self.received.remove(&tid).unwrap_or_default()
}
pub(crate) fn at_request(&mut self, tid: i32) -> Vec<Vec<u8>> {
match self.runner {
Runner::NoTurnYet => {
self.runner = Runner::Root(tid);
self.pending.remove(&tid).unwrap_or_default()
}
Runner::Root(root) | Runner::Exclusive(root) if root == tid => {
self.pending.remove(&tid).unwrap_or_default()
}
Runner::Root(_) if !self.ambiguous => {
self.ambiguous = true;
vec![FORWARDING_AMBIGUOUS_ORDER_NOTICE.to_vec()]
}
_ => Vec::new(),
}
}
pub(crate) fn at_commit(&mut self, tid: i32, exclusive: bool) -> Vec<Vec<u8>> {
self.runner = if exclusive {
Runner::Exclusive(tid)
} else {
Runner::None
};
self.pending.remove(&tid).unwrap_or_default()
}
pub(crate) fn untagged(&mut self, message: &[u8]) -> Vec<Vec<u8>> {
if message.starts_with(FORWARDING_LOSS_NOTICE.as_bytes()) {
return vec![message.to_vec()];
}
let mut out = Vec::new();
if !self.legacy {
self.legacy = true;
out.push(FORWARDING_UNTAGGED_RECORD_NOTICE.to_vec());
}
out.push(message.to_vec());
out
}
pub(crate) fn finish(&mut self) -> Vec<Vec<u8>> {
std::mem::take(&mut self.pending)
.into_values()
.flatten()
.collect()
}
}
struct ForwardedSource {
socket: OwnedFd,
sink: ForwardedRecordSink,
order: ForwardedOrder,
transport_failure: Option<ForwardedSourceError>,
loss_seen: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ForwardedSourceError {
Closed,
Io(i32),
Poll(i16),
MessageChanged,
}
static FORWARDED_SOURCE: Mutex<Option<ForwardedSource>> = Mutex::new(None);
pub const DETERMINISM_LOSS_RECORD_TARGET: &str = "detcore::determinism_loss";
static FORWARDED_GENERATION: AtomicU64 = AtomicU64::new(0);
static RECEIVER_GENERATION: AtomicU64 = AtomicU64::new(0);
static RECEIVER_STOP: Mutex<Option<tokio::sync::oneshot::Sender<()>>> = Mutex::new(None);
static DETERMINISM_LOSS: Mutex<Option<String>> = Mutex::new(None);
pub fn record_determinism_loss(reason: &str) {
let mut loss = DETERMINISM_LOSS.lock().unwrap();
if loss.is_none() {
*loss = Some(reason.to_owned());
}
}
static REPLAY_REFUSAL: Mutex<Option<String>> = Mutex::new(None);
pub fn record_replay_refusal(reason: &str) {
write_loss_notice(reason);
let mut refusal = REPLAY_REFUSAL.lock().unwrap();
if refusal.is_none() {
*refusal = Some(reason.to_owned());
}
}
pub fn replay_refusal() -> Option<String> {
REPLAY_REFUSAL.lock().unwrap().clone()
}
pub fn determinism_loss() -> Option<String> {
DETERMINISM_LOSS.lock().unwrap().clone()
}
pub fn write_loss_notice(reason: &str) {
let line = format!("{FORWARDING_LOSS_NOTICE} 0 0 {reason}\n");
record_determinism_loss(reason);
if let Some(source) = FORWARDED_SOURCE.lock().unwrap().as_mut() {
(source.sink)(line.as_bytes());
}
}
fn emit(source: &mut ForwardedSource, record: &[u8]) {
if record.starts_with(FORWARDING_LOSS_NOTICE.as_bytes()) {
source.loss_seen = true;
record_determinism_loss(&String::from_utf8_lossy(record));
}
(source.sink)(record);
}
pub fn set_forwarded_source(socket: OwnedFd, sink: ForwardedRecordSink) {
let mut source = FORWARDED_SOURCE.lock().unwrap();
FORWARDED_GENERATION.fetch_add(1, Ordering::SeqCst);
stop_forwarded_receiver();
*source = Some(ForwardedSource {
socket,
sink,
order: ForwardedOrder::new(),
transport_failure: None,
loss_seen: false,
});
}
fn start_forwarded_receiver() {
let generation = FORWARDED_GENERATION.load(Ordering::SeqCst);
if RECEIVER_GENERATION.load(Ordering::SeqCst) == generation {
return;
}
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
return;
};
let watched = match FORWARDED_SOURCE.lock().unwrap().as_ref() {
Some(source) => match source.socket.try_clone() {
Ok(watched) => watched,
Err(_) => return,
},
None => return,
};
if RECEIVER_GENERATION.swap(generation, Ordering::SeqCst) == generation {
return;
}
let (stop, stopped) = tokio::sync::oneshot::channel();
stop_forwarded_receiver();
*RECEIVER_STOP.lock().unwrap() = Some(stop);
runtime.spawn(receive_forwarded(watched, generation, stopped));
}
fn stop_forwarded_receiver() {
if let Some(stop) = RECEIVER_STOP.lock().unwrap().take() {
let _ = stop.send(());
}
}
async fn receive_forwarded(
watched: OwnedFd,
generation: u64,
mut stopped: tokio::sync::oneshot::Receiver<()>,
) {
let Ok(readiness) =
tokio::io::unix::AsyncFd::with_interest(watched, tokio::io::Interest::READABLE)
else {
let _ =
RECEIVER_GENERATION.compare_exchange(generation, 0, Ordering::SeqCst, Ordering::SeqCst);
return;
};
loop {
let ready = tokio::select! {
ready = readiness.readable() => ready,
_ = &mut stopped => return,
};
let Ok(mut ready) = ready else {
let _ = RECEIVER_GENERATION.compare_exchange(
generation,
0,
Ordering::SeqCst,
Ordering::SeqCst,
);
return;
};
{
let mut source = FORWARDED_SOURCE.lock().unwrap();
if FORWARDED_GENERATION.load(Ordering::SeqCst) != generation {
return;
}
match source.as_mut() {
Some(source) => {
collect_forwarded(source);
if source.transport_failure.is_some() {
return;
}
}
None => return,
}
}
ready.clear_ready();
}
}
pub fn clear_forwarded_source() {
let mut source = FORWARDED_SOURCE.lock().unwrap();
FORWARDED_GENERATION.fetch_add(1, Ordering::SeqCst);
stop_forwarded_receiver();
if let Some(source) = source.as_mut() {
collect_forwarded(source);
let records = source.order.finish();
write_records(source, records);
}
source.take();
}
pub unsafe trait NativePhysicalExitCompletion {
fn retained_pidfd(&self) -> std::os::fd::BorrowedFd<'_>;
fn process_id(&self) -> i32;
fn birth_identity(&self) -> u64;
}
pub unsafe trait NativePhysicalWatchIdentity {
fn retained_pidfd(&self) -> std::os::fd::BorrowedFd<'_>;
fn process_id(&self) -> i32;
fn birth_identity(&self) -> u64;
fn watch_is_healthy(&self) -> bool;
}
#[derive(Debug)]
pub struct NativeForwardedSourceReceipt {
generation: u64,
identity: (u64, u64),
owner: (i32, u64),
}
pub fn retain_native_forwarded_source(
watch: &dyn NativePhysicalWatchIdentity,
) -> Result<NativeForwardedSourceReceipt, CoordinatorForwardingCutError> {
use std::os::fd::AsRawFd;
let owner = (watch.process_id(), watch.birth_identity());
let fd = watch.retained_pidfd().as_raw_fd();
let mut stat: libc::stat = unsafe { std::mem::zeroed() };
let mut fs: libc::statfs = unsafe { std::mem::zeroed() };
let mut poll = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
if owner.0 <= 0
|| owner.1 == 0
|| !watch.watch_is_healthy()
|| unsafe { libc::fstat(fd, &mut stat) } != 0
|| unsafe { libc::fstatfs(fd, &mut fs) } != 0
|| fs.f_type as i64 != 0x5049_4446
|| stat.st_ino != owner.1
|| unsafe { libc::poll(&mut poll, 1, 0) } != 0
{
return Err(CoordinatorForwardingCutError::InvalidOwner);
}
let mut source = FORWARDED_SOURCE.lock().unwrap();
let source = source
.as_mut()
.ok_or(CoordinatorForwardingCutError::MissingSource)?;
Ok(NativeForwardedSourceReceipt {
generation: FORWARDED_GENERATION.load(Ordering::SeqCst),
identity: source_identity_for_cut(source)?,
owner,
})
}
fn verify_native_physical_completion(
receipt: &dyn NativePhysicalExitCompletion,
) -> Result<(), CoordinatorForwardingCutError> {
use std::os::fd::AsRawFd;
let fd = receipt.retained_pidfd().as_raw_fd();
let mut stat: libc::stat = unsafe { std::mem::zeroed() };
let mut fs: libc::statfs = unsafe { std::mem::zeroed() };
if receipt.process_id() <= 0
|| receipt.birth_identity() == 0
|| unsafe { libc::fstat(fd, &mut stat) } != 0
|| unsafe { libc::fstatfs(fd, &mut fs) } != 0
|| fs.f_type as i64 != 0x5049_4446
|| stat.st_ino != receipt.birth_identity()
{
return Err(CoordinatorForwardingCutError::InvalidOwner);
}
let mut poll = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
let result = unsafe { libc::poll(&mut poll, 1, 0) };
if result < 0 {
return Err(CoordinatorForwardingCutError::SourceIo(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
}
if result != 1
|| poll.revents & libc::POLLIN == 0
|| poll.revents & (libc::POLLERR | libc::POLLNVAL) != 0
{
return Err(CoordinatorForwardingCutError::InvalidOwner);
}
Ok(())
}
fn inspect_native_final_source(
source: &mut ForwardedSource,
identity: (u64, u64),
owner: (i32, u64),
completion: &dyn NativePhysicalExitCompletion,
) -> Result<(), CoordinatorForwardingCutError> {
if (completion.process_id(), completion.birth_identity()) != owner {
return Err(CoordinatorForwardingCutError::InvalidOwner);
}
verify_native_physical_completion(completion)?;
if socket_identity(source.socket.as_raw_fd()) != Some(identity) {
return Err(CoordinatorForwardingCutError::SourceChanged);
}
collect_forwarded(source);
match source.transport_failure {
Some(ForwardedSourceError::Closed) => {}
Some(error) => return Err(error.into()),
None => return Err(CoordinatorForwardingCutError::ClosedSource),
}
let mut poll = libc::pollfd {
fd: source.socket.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
};
let result = unsafe { libc::poll(&mut poll, 1, 0) };
if result < 0 {
return Err(CoordinatorForwardingCutError::SourceIo(
std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO),
));
}
if result != 1
|| poll.revents & libc::POLLHUP == 0
|| poll.revents & (libc::POLLERR | libc::POLLNVAL) != 0
{
return Err(CoordinatorForwardingCutError::ClosedSource);
}
if source.loss_seen || source.order.legacy || source.order.ambiguous {
return Err(CoordinatorForwardingCutError::DeterminismLoss);
}
for (tid, interval) in &source.order.received {
if let Some((expected, sent)) = interval.out_of_sequence {
return Err(CoordinatorForwardingCutError::OutOfSequence { expected, sent });
}
if interval.count != 0 {
return Err(CoordinatorForwardingCutError::OtherOwnerPending(*tid));
}
}
if let Some(tid) = source.order.pending.keys().next() {
return Err(CoordinatorForwardingCutError::OtherOwnerPending(*tid));
}
Ok(())
}
pub fn finish_native_forwarded_source_after_exit(
expected: &NativeForwardedSourceReceipt,
completion: &dyn NativePhysicalExitCompletion,
) -> Result<(), CoordinatorForwardingCutError> {
let mut registered = FORWARDED_SOURCE.lock().unwrap();
if FORWARDED_GENERATION.load(Ordering::SeqCst) != expected.generation {
return Err(CoordinatorForwardingCutError::SourceChanged);
}
let source = registered
.as_mut()
.ok_or(CoordinatorForwardingCutError::MissingSource)?;
if DETERMINISM_LOSS.lock().unwrap().is_some() {
return Err(CoordinatorForwardingCutError::DeterminismLoss);
}
inspect_native_final_source(source, expected.identity, expected.owner, completion)?;
FORWARDED_GENERATION.fetch_add(1, Ordering::SeqCst);
stop_forwarded_receiver();
registered.take();
Ok(())
}
pub fn drain_forwarded() {
if let Some(source) = FORWARDED_SOURCE.lock().unwrap().as_mut() {
collect_forwarded(source);
}
}
pub fn write_forwarded_for(tid: i32, exclusive: bool) {
start_forwarded_receiver();
if let Some(source) = FORWARDED_SOURCE.lock().unwrap().as_mut() {
collect_forwarded(source);
let records = source.order.at_commit(tid, exclusive);
write_records(source, records);
}
}
pub fn drain_forwarded_at_request(tid: i32) {
start_forwarded_receiver();
if let Some(source) = FORWARDED_SOURCE.lock().unwrap().as_mut() {
collect_forwarded(source);
let records = source.order.at_request(tid);
write_records(source, records);
}
}
pub fn check_forwarded_count(tid: i32, produced: u64) {
if let Some(source) = FORWARDED_SOURCE.lock().unwrap().as_mut() {
collect_forwarded(source);
let interval = source.order.received_since_check(tid);
if let Some(failure) = forwarded_count_failure(produced, interval) {
report_forwarded_count_failure(source, tid, failure);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ForwardedCountFailure {
Uncounted,
Sequence { expected: u64, sent: u64 },
Count { produced: u64, received: u64 },
}
fn forwarded_count_failure(
produced: u64,
interval: ReceivedInterval,
) -> Option<ForwardedCountFailure> {
if produced == UNCOUNTED {
Some(ForwardedCountFailure::Uncounted)
} else if let Some((expected, sent)) = interval.out_of_sequence {
Some(ForwardedCountFailure::Sequence { expected, sent })
} else if interval.count != produced {
Some(ForwardedCountFailure::Count {
produced,
received: interval.count,
})
} else {
None
}
}
fn report_forwarded_count_failure(
source: &mut ForwardedSource,
tid: i32,
failure: ForwardedCountFailure,
) {
let reason = match failure {
ForwardedCountFailure::Uncounted => format!(
"forwarded records uncounted: a guest thread's records could not be counted (reported by thread {tid})"
),
ForwardedCountFailure::Sequence { expected, sent } if sent > expected => format!(
"forwarded records lost: thread {tid} sent record {sent} of its interval where {expected} was next"
),
ForwardedCountFailure::Sequence { expected, sent } => format!(
"forwarded records not the Tool's: thread {tid} sent record {sent} of its interval where {expected} was next"
),
ForwardedCountFailure::Count { produced, received } if received < produced => format!(
"forwarded records lost: thread {tid} produced {produced} since its last counted request and {received} arrived"
),
ForwardedCountFailure::Count { produced, received } => format!(
"forwarded records not the Tool's: thread {tid} produced {produced} since its last counted request and {received} arrived"
),
};
record_determinism_loss(&reason);
source.loss_seen = true;
let line = format!("{FORWARDING_LOSS_NOTICE} 0 0 {reason}\n");
(source.sink)(line.as_bytes());
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CoordinatorForwardingReceipt {
owner_tid: i32,
generation: u64,
socket_identity: (u64, u64),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CoordinatorForwardingCutError {
InvalidOwner,
MissingSource,
SourceChanged,
InvalidSource,
ClosedSource,
SourceIo(i32),
SourcePoll(i16),
MessageChanged,
DeterminismLoss,
Uncounted,
OutOfSequence {
expected: u64,
sent: u64,
},
CountMismatch {
produced: u64,
received: u64,
},
OtherOwnerPending(i32),
NotOwnerBoundary,
PendingOwnerRecords,
}
impl From<ForwardedSourceError> for CoordinatorForwardingCutError {
fn from(error: ForwardedSourceError) -> Self {
match error {
ForwardedSourceError::Closed => Self::ClosedSource,
ForwardedSourceError::Io(errno) => Self::SourceIo(errno),
ForwardedSourceError::Poll(flags) => Self::SourcePoll(flags),
ForwardedSourceError::MessageChanged => Self::MessageChanged,
}
}
}
impl From<ForwardedCountFailure> for CoordinatorForwardingCutError {
fn from(error: ForwardedCountFailure) -> Self {
match error {
ForwardedCountFailure::Uncounted => Self::Uncounted,
ForwardedCountFailure::Sequence { expected, sent } => {
Self::OutOfSequence { expected, sent }
}
ForwardedCountFailure::Count { produced, received } => {
Self::CountMismatch { produced, received }
}
}
}
}
pub fn check_native_exec_forwarding_cut(
owner_tid: i32,
produced: u64,
previous: Option<&CoordinatorForwardingReceipt>,
) -> Result<CoordinatorForwardingReceipt, CoordinatorForwardingCutError> {
let mut source = FORWARDED_SOURCE.lock().unwrap();
let generation = FORWARDED_GENERATION.load(Ordering::SeqCst);
inspect_coordinator_forwarding_cut(
source.as_mut(),
generation,
owner_tid,
produced,
previous,
|| DETERMINISM_LOSS.lock().unwrap().is_some(),
)
}
fn source_identity_for_cut(
source: &mut ForwardedSource,
) -> Result<(u64, u64), CoordinatorForwardingCutError> {
if let Some(error) = source.transport_failure {
return Err(error.into());
}
let fd = source.socket.as_raw_fd();
let identity = socket_identity(fd).ok_or(CoordinatorForwardingCutError::InvalidSource)?;
let mut kind = 0i32;
let mut length;
loop {
length = std::mem::size_of_val(&kind) as libc::socklen_t;
let result = unsafe {
libc::getsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_TYPE,
(&mut kind as *mut i32).cast(),
&mut length,
)
};
if result >= 0 {
break;
}
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
if errno == libc::EINTR {
continue;
}
let error = ForwardedSourceError::Io(errno);
source.transport_failure = Some(error);
return Err(error.into());
}
if length as usize != std::mem::size_of_val(&kind) || kind != libc::SOCK_SEQPACKET {
return Err(CoordinatorForwardingCutError::InvalidSource);
}
let mut peer: libc::sockaddr_storage = unsafe { std::mem::zeroed() };
loop {
length = std::mem::size_of_val(&peer) as libc::socklen_t;
let result = unsafe {
libc::getpeername(
fd,
(&mut peer as *mut libc::sockaddr_storage).cast(),
&mut length,
)
};
if result >= 0 {
break;
}
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
if errno == libc::EINTR {
continue;
}
let error = ForwardedSourceError::Io(errno);
source.transport_failure = Some(error);
return Err(error.into());
}
if (length as usize) < std::mem::size_of_val(&peer.ss_family)
|| i32::from(peer.ss_family) != libc::AF_UNIX
{
return Err(CoordinatorForwardingCutError::InvalidSource);
}
let mut poll = libc::pollfd {
fd,
events: libc::POLLIN | libc::POLLRDHUP,
revents: 0,
};
loop {
let result = unsafe { libc::poll(&mut poll, 1, 0) };
if result < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
if errno == libc::EINTR {
continue;
}
let error = ForwardedSourceError::Io(errno);
source.transport_failure = Some(error);
return Err(error.into());
}
let error = if poll.revents & (libc::POLLHUP | libc::POLLRDHUP) != 0 {
Some(ForwardedSourceError::Closed)
} else if poll.revents & (libc::POLLERR | libc::POLLNVAL) != 0 {
Some(ForwardedSourceError::Poll(poll.revents))
} else {
None
};
if let Some(error) = error {
source.transport_failure = Some(error);
return Err(error.into());
}
return Ok(identity);
}
}
fn check_cut_state(
source: &ForwardedSource,
owner_tid: i32,
has_loss: &impl Fn() -> bool,
) -> Result<(), CoordinatorForwardingCutError> {
if has_loss() || source.loss_seen || source.order.legacy || source.order.ambiguous {
return Err(CoordinatorForwardingCutError::DeterminismLoss);
}
if let Some((&tid, _)) = source
.order
.pending
.iter()
.find(|(tid, records)| **tid != owner_tid && !records.is_empty())
{
return Err(CoordinatorForwardingCutError::OtherOwnerPending(tid));
}
if let Some((&tid, _)) = source
.order
.received
.iter()
.find(|(tid, _)| **tid != owner_tid)
{
return Err(CoordinatorForwardingCutError::OtherOwnerPending(tid));
}
match source.order.runner {
Runner::NoTurnYet => Ok(()),
Runner::Root(tid) | Runner::Exclusive(tid) if tid == owner_tid => Ok(()),
_ => Err(CoordinatorForwardingCutError::NotOwnerBoundary),
}
}
fn inspect_coordinator_forwarding_cut(
source: Option<&mut ForwardedSource>,
generation: u64,
owner_tid: i32,
produced: u64,
previous: Option<&CoordinatorForwardingReceipt>,
has_loss: impl Fn() -> bool,
) -> Result<CoordinatorForwardingReceipt, CoordinatorForwardingCutError> {
if owner_tid <= 0 {
return Err(CoordinatorForwardingCutError::InvalidOwner);
}
let source = source.ok_or(CoordinatorForwardingCutError::MissingSource)?;
let receipt = CoordinatorForwardingReceipt {
owner_tid,
generation,
socket_identity: source_identity_for_cut(source)?,
};
if previous.is_some_and(|previous| *previous != receipt) {
return Err(CoordinatorForwardingCutError::SourceChanged);
}
collect_forwarded(source);
source_identity_for_cut(source)?;
check_cut_state(source, owner_tid, &has_loss)?;
let interval = source
.order
.received
.get(&owner_tid)
.copied()
.unwrap_or_default();
if let Some(failure) = forwarded_count_failure(produced, interval) {
report_forwarded_count_failure(source, owner_tid, failure);
return Err(failure.into());
}
let records = source.order.at_request(owner_tid);
for record in &records {
emit(source, record);
}
collect_forwarded(source);
let final_check = (|| {
if source_identity_for_cut(source)? != receipt.socket_identity {
return Err(CoordinatorForwardingCutError::SourceChanged);
}
check_cut_state(source, owner_tid, &has_loss)?;
let current = source
.order
.received
.get(&owner_tid)
.copied()
.unwrap_or_default();
if let Some(failure) = forwarded_count_failure(produced, current) {
report_forwarded_count_failure(source, owner_tid, failure);
return Err(failure.into());
}
if source
.order
.pending
.get(&owner_tid)
.is_some_and(|records| !records.is_empty())
{
return Err(CoordinatorForwardingCutError::PendingOwnerRecords);
}
Ok(())
})();
if let Err(error) = final_check {
if !records.is_empty() {
let mut retained = records;
if let Some(mut later) = source.order.pending.remove(&owner_tid) {
retained.append(&mut later);
}
source.order.pending.insert(owner_tid, retained);
}
return Err(error);
}
source.order.received_since_check(owner_tid);
Ok(receipt)
}
fn write_records(source: &mut ForwardedSource, records: Vec<Vec<u8>>) {
for record in records {
emit(source, &record);
}
}
fn collect_forwarded(source: &mut ForwardedSource) {
let fd = source.socket.as_raw_fd();
let mut buffer = Vec::new();
loop {
let size = unsafe {
libc::recv(
fd,
std::ptr::null_mut(),
0,
libc::MSG_PEEK | libc::MSG_TRUNC | libc::MSG_DONTWAIT,
)
};
if size < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
if errno == libc::EINTR {
continue;
}
if errno != libc::EAGAIN && errno != libc::EWOULDBLOCK {
source
.transport_failure
.get_or_insert(ForwardedSourceError::Io(errno));
}
return;
}
if size == 0 {
source
.transport_failure
.get_or_insert(ForwardedSourceError::Closed);
return;
}
buffer.resize(size as usize, 0);
let received = loop {
let received = unsafe {
libc::recv(
fd,
buffer.as_mut_ptr().cast::<libc::c_void>(),
buffer.len(),
libc::MSG_DONTWAIT | libc::MSG_TRUNC,
)
};
if received < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::EINTR) {
continue;
}
break received;
};
if received < 0 {
let errno = std::io::Error::last_os_error().raw_os_error().unwrap();
let error = if errno == libc::EAGAIN || errno == libc::EWOULDBLOCK {
ForwardedSourceError::MessageChanged
} else {
ForwardedSourceError::Io(errno)
};
source.transport_failure.get_or_insert(error);
return;
}
if received == 0 {
source
.transport_failure
.get_or_insert(ForwardedSourceError::Closed);
return;
}
if received != size {
source
.transport_failure
.get_or_insert(ForwardedSourceError::MessageChanged);
}
let message = &buffer[..(received as usize).min(buffer.len())];
match split_forwarded_message(message) {
Some((tid, index, record)) => {
source.order.arrived(tid, index);
source.order.queue(tid, record.to_vec());
}
None => {
for record in source.order.untagged(message) {
emit(source, &record);
}
}
}
}
}
#[doc(hidden)]
pub fn forwards_target(target: &str) -> bool {
FORWARDER
.get()
.is_some_and(|(_, policy)| policy.forwards(target))
}
#[doc(hidden)]
pub fn emit_forwarded(target: &str, record_suffix: &str, message: fmt::Arguments<'_>) {
tracing::info!("DETLOG {}{}", message, record_suffix);
let index = count_forwarded();
FORWARDER.get().expect("forwarder disappeared").0(target, record_suffix, index, message);
}
struct ForwardedCount {
tid: AtomicI32,
pid: AtomicI32,
count: AtomicU64,
}
static FORWARDED_COUNTS: [ForwardedCount; 512] = [const {
ForwardedCount {
tid: AtomicI32::new(0),
pid: AtomicI32::new(0),
count: AtomicU64::new(0),
}
}; 512];
static FORWARDING_UNCOUNTED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
static FORWARDING_STARTED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ForwardingCutError {
Uncounted,
UnreportedLosses(u64),
PendingRecords {
pid: i32,
tid: i32,
count: u64,
},
}
pub fn check_empty_forwarding_cut() -> Result<(), ForwardingCutError> {
inspect_empty_forwarding_cut(
&FORWARDED_COUNTS,
FORWARDING_UNCOUNTED.load(Ordering::Acquire),
UNREPORTED_LOSSES.load(Ordering::Acquire),
)
}
fn inspect_empty_forwarding_cut(
slots: &[ForwardedCount],
uncounted: bool,
losses: u64,
) -> Result<(), ForwardingCutError> {
if uncounted {
return Err(ForwardingCutError::Uncounted);
}
if losses != 0 {
return Err(ForwardingCutError::UnreportedLosses(losses));
}
for slot in slots {
let count = slot.count.load(Ordering::Acquire);
if count != 0 {
return Err(ForwardingCutError::PendingRecords {
pid: slot.pid.load(Ordering::Acquire),
tid: slot.tid.load(Ordering::Acquire),
count,
});
}
}
Ok(())
}
pub const UNCOUNTED: u64 = u64::MAX;
fn current_pid() -> i32 {
unsafe { crate::util::raw_syscall(libc::SYS_getpid, [0; 6]) as i32 }
}
fn forwarded_count_slot(tid: i32) -> Option<&'static ForwardedCount> {
let pid = current_pid();
let start = tid.unsigned_abs() as usize % FORWARDED_COUNTS.len();
let probe = || {
(0..FORWARDED_COUNTS.len())
.map(move |offset| &FORWARDED_COUNTS[(start + offset) % FORWARDED_COUNTS.len()])
};
let claim = |slot: &'static ForwardedCount, owner: i32| {
if slot
.tid
.compare_exchange(owner, tid, Ordering::AcqRel, Ordering::Acquire)
.is_ok()
{
slot.pid.store(pid, Ordering::Release);
slot.count.store(0, Ordering::Release);
true
} else {
false
}
};
if let Some(slot) = FORWARDED_COUNTS.iter().find(|slot| {
slot.tid.load(Ordering::Acquire) == tid && slot.pid.load(Ordering::Acquire) == pid
}) {
return Some(slot);
}
for slot in probe() {
if slot.tid.load(Ordering::Acquire) == 0 && claim(slot, 0) {
return Some(slot);
}
}
for slot in probe() {
let owner = slot.tid.load(Ordering::Acquire);
let owner_pid = slot.pid.load(Ordering::Acquire);
if owner != 0 && owner_pid != pid && private_copy(owner_pid, pid) && claim(slot, owner) {
return Some(slot);
}
}
None
}
fn private_copy(owner: i32, pid: i32) -> bool {
const KCMP_VM: libc::c_int = 1;
let answer = unsafe { libc::syscall(libc::SYS_kcmp, owner, pid, KCMP_VM, 0, 0) };
answer > 0
|| (answer < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH))
}
fn count_forwarded() -> u64 {
if !FORWARDING_STARTED.load(Ordering::Acquire) {
FORWARDING_STARTED.store(true, Ordering::Release);
}
match forwarded_count_slot(current_tid()) {
Some(slot) => slot.count.fetch_add(1, Ordering::AcqRel) + 1,
None => {
FORWARDING_UNCOUNTED.store(true, Ordering::Release);
0
}
}
}
pub fn take_forwarded_since_request() -> u64 {
if FORWARDING_UNCOUNTED.load(Ordering::Acquire) {
return UNCOUNTED;
}
if !FORWARDING_STARTED.load(Ordering::Acquire) {
return 0;
}
forwarded_count_slot(current_tid())
.map(|slot| slot.count.swap(0, Ordering::AcqRel))
.unwrap_or(0)
}
pub fn require_forwarding(expected: &str) {
if FORWARDER
.get()
.is_none_or(|(_, policy)| policy.encode() != expected)
{
FORWARDING_UNCOUNTED.store(true, Ordering::Release);
}
}
pub fn release_forwarded_count() {
if FORWARDING_STARTED.load(Ordering::Acquire) {
release_count_slot(current_tid());
}
}
fn release_count_slot(tid: i32) {
let pid = current_pid();
for slot in &FORWARDED_COUNTS {
if slot.tid.load(Ordering::Acquire) == tid && slot.pid.load(Ordering::Acquire) == pid {
slot.count.store(0, Ordering::Release);
let _ = slot
.tid
.compare_exchange(tid, 0, Ordering::AcqRel, Ordering::Acquire);
}
}
}
#[macro_export]
macro_rules! detlog {
(event = $event:expr; $($arg:tt)+) => {{
if $crate::detlog::forwards_target(::core::module_path!())
|| ::tracing::enabled!(::tracing::Level::INFO)
{
let record_suffix = $crate::detlog::record_suffix($event);
if $crate::detlog::forwards_target(::core::module_path!()) {
$crate::detlog::emit_forwarded(
::core::module_path!(),
&record_suffix,
format_args!($($arg)+),
);
} else {
::tracing::info!("DETLOG {}{}", format_args!($($arg)+), record_suffix);
}
}
}};
($($arg:tt)+) => {{
$crate::detlog!(event = $crate::detlog::DetLogEvent::Other; $($arg)+);
}};
}
#[macro_export]
macro_rules! detlog_observed {
() => {
$crate::detlog::forwards_target(::core::module_path!())
|| ::tracing::enabled!(::tracing::Level::INFO)
};
}
#[macro_export]
macro_rules! detlog_debug {
(event = $event:expr; $($arg:tt)+) => {{
if ::tracing::enabled!(::tracing::Level::DEBUG) {
let record_suffix = $crate::detlog::record_suffix($event);
::tracing::debug!("DETLOG {}{}", format_args!($($arg)+), record_suffix);
}
}};
($($arg:tt)+) => {{
$crate::detlog_debug!(event = $crate::detlog::DetLogEvent::Other; $($arg)+);
}};
}
#[cfg(test)]
mod tests {
use std::sync::atomic::Ordering;
use tracing::Metadata;
use tracing::span;
use tracing::subscriber::Interest;
use super::DetLogEvent;
use super::DetLogRecord;
use super::ForwardPolicy;
use super::RECORD_SEPARATOR;
use super::record_suffix;
#[test]
fn native_exec_forwarding_cut_refuses_retained_records_and_loss_state_without_resetting() {
use std::sync::atomic::AtomicI32;
use std::sync::atomic::AtomicU64;
use super::ForwardedCount;
use super::ForwardingCutError;
use super::inspect_empty_forwarding_cut;
let slots = [ForwardedCount {
pid: AtomicI32::new(41),
tid: AtomicI32::new(43),
count: AtomicU64::new(0),
}];
assert_eq!(inspect_empty_forwarding_cut(&slots, false, 0), Ok(()));
slots[0].count.store(3, Ordering::Release);
for _ in 0..2 {
assert_eq!(
inspect_empty_forwarding_cut(&slots, false, 0),
Err(ForwardingCutError::PendingRecords {
pid: 41,
tid: 43,
count: 3,
})
);
assert_eq!(slots[0].count.load(Ordering::Acquire), 3);
}
slots[0].tid.store(0, Ordering::Release);
assert!(matches!(
inspect_empty_forwarding_cut(&slots, false, 0),
Err(ForwardingCutError::PendingRecords {
tid: 0,
count: 3,
..
})
));
assert_eq!(
inspect_empty_forwarding_cut(&slots, true, 0),
Err(ForwardingCutError::Uncounted)
);
assert_eq!(
inspect_empty_forwarding_cut(&slots, false, 2),
Err(ForwardingCutError::UnreportedLosses(2))
);
assert_eq!(slots[0].count.load(Ordering::Acquire), 3);
slots[0].count.store(0, Ordering::Release);
assert_eq!(inspect_empty_forwarding_cut(&slots, false, 0), Ok(()));
slots[0].count.store(1, Ordering::Release);
assert!(matches!(
inspect_empty_forwarding_cut(&slots, false, 0),
Err(ForwardingCutError::PendingRecords { count: 1, .. })
));
}
fn global_source_test() -> std::sync::MutexGuard<'static, ()> {
static GLOBAL_SOURCE: std::sync::Mutex<()> = std::sync::Mutex::new(());
GLOBAL_SOURCE
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
struct NativeCutSource {
source: super::ForwardedSource,
sender: std::os::fd::OwnedFd,
written: std::sync::Arc<std::sync::Mutex<Vec<Vec<u8>>>>,
}
impl NativeCutSource {
fn new() -> Self {
use std::os::fd::FromRawFd;
let mut fds = [-1; 2];
assert_eq!(
unsafe {
libc::socketpair(
libc::AF_UNIX,
libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC,
0,
fds.as_mut_ptr(),
)
},
0
);
let written = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let sink = written.clone();
Self {
source: super::ForwardedSource {
socket: unsafe { std::os::fd::OwnedFd::from_raw_fd(fds[0]) },
sink: Box::new(move |record| sink.lock().unwrap().push(record.to_vec())),
order: super::ForwardedOrder::new(),
transport_failure: None,
loss_seen: false,
},
sender: unsafe { std::os::fd::OwnedFd::from_raw_fd(fds[1]) },
written,
}
}
fn send(&self, tid: i32, index: u64, record: &[u8]) {
use std::os::fd::AsRawFd;
let message = super::tagged_forwarded_message(tid, index, record);
assert_eq!(
unsafe {
libc::send(
self.sender.as_raw_fd(),
message.as_ptr().cast(),
message.len(),
libc::MSG_NOSIGNAL,
)
},
message.len() as isize
);
}
fn check(
&mut self,
produced: u64,
) -> Result<super::CoordinatorForwardingReceipt, super::CoordinatorForwardingCutError>
{
super::inspect_coordinator_forwarding_cut(
Some(&mut self.source),
19,
41,
produced,
None,
|| false,
)
}
}
#[test]
fn native_coordinator_cut_checks_zero_and_nonzero_real_socket_intervals() {
let _guard = global_source_test();
let mut fixture = NativeCutSource::new();
let first = fixture.check(0).unwrap();
assert!(fixture.written.lock().unwrap().is_empty());
assert!(fixture.source.order.received.is_empty());
assert_eq!(
fixture.source.transport_failure, None,
"empty EAGAIN is healthy"
);
fixture.send(41, 1, b"a\n");
fixture.send(41, 2, b"b\n");
let second = super::inspect_coordinator_forwarding_cut(
Some(&mut fixture.source),
19,
41,
2,
Some(&first),
|| false,
)
.unwrap();
assert_eq!(first, second);
assert_eq!(
*fixture.written.lock().unwrap(),
vec![b"a\n".to_vec(), b"b\n".to_vec()]
);
assert!(fixture.source.order.pending.is_empty());
assert!(fixture.source.order.received.is_empty());
fixture.send(41, 1, b"c\n");
assert_eq!(fixture.check(1), Ok(first));
assert_eq!(
super::inspect_coordinator_forwarding_cut(
Some(&mut fixture.source),
19,
41,
0,
Some(&first),
|| false
),
Ok(first)
);
assert_eq!(
*fixture.written.lock().unwrap(),
vec![b"a\n".to_vec(), b"b\n".to_vec(), b"c\n".to_vec()]
);
assert!(fixture.source.order.at_commit(41, true).is_empty());
assert_eq!(
fixture.check(0),
Ok(first),
"the actual owner's exclusive point is eligible too"
);
}
#[test]
fn native_coordinator_cut_refuses_unchecked_counts_without_consuming_them() {
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
for (indexes, produced, expected) in [
(
vec![],
1,
Error::CountMismatch {
produced: 1,
received: 0,
},
),
(
vec![1],
0,
Error::CountMismatch {
produced: 0,
received: 1,
},
),
(
vec![1],
2,
Error::CountMismatch {
produced: 2,
received: 1,
},
),
(
vec![1, 2],
1,
Error::CountMismatch {
produced: 1,
received: 2,
},
),
(
vec![2],
1,
Error::OutOfSequence {
expected: 1,
sent: 2,
},
),
(
vec![1, 1],
2,
Error::OutOfSequence {
expected: 2,
sent: 1,
},
),
(vec![1], super::UNCOUNTED, Error::Uncounted),
] {
let mut fixture = NativeCutSource::new();
for &index in &indexes {
fixture.send(41, index, b"kept\n");
}
assert_eq!(fixture.check(produced), Err(expected));
assert_eq!(
fixture
.source
.order
.received
.get(&41)
.copied()
.unwrap_or_default()
.count,
indexes.len() as u64
);
assert_eq!(
fixture.source.order.pending.get(&41).map_or(0, Vec::len),
indexes.len()
);
assert!(
fixture
.written
.lock()
.unwrap()
.iter()
.all(|record| record.starts_with(super::FORWARDING_LOSS_NOTICE.as_bytes()))
);
assert_eq!(
fixture.check(produced),
Err(Error::DeterminismLoss),
"a later check cannot clear the loss"
);
assert_eq!(
fixture
.source
.order
.received
.get(&41)
.copied()
.unwrap_or_default()
.count,
indexes.len() as u64
);
}
}
#[test]
fn native_coordinator_cut_retains_other_owner_records_and_requires_order_point() {
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
let mut fixture = NativeCutSource::new();
fixture.send(41, 1, b"owner\n");
fixture.send(43, 1, b"other\n");
for _ in 0..2 {
assert_eq!(fixture.check(1), Err(Error::OtherOwnerPending(43)));
assert_eq!(fixture.source.order.pending[&41], vec![b"owner\n".to_vec()]);
assert_eq!(fixture.source.order.pending[&43], vec![b"other\n".to_vec()]);
assert_eq!(fixture.source.order.received[&41].count, 1);
assert_eq!(fixture.source.order.received[&43].count, 1);
assert!(fixture.written.lock().unwrap().is_empty());
}
let other = fixture.source.order.at_commit(43, true);
super::write_records(&mut fixture.source, other);
assert_eq!(fixture.check(1), Err(Error::OtherOwnerPending(43)));
assert_eq!(fixture.source.order.received[&43].count, 1);
for runner in [
super::Runner::None,
super::Runner::Exclusive(43),
super::Runner::Root(43),
] {
let mut fixture = NativeCutSource::new();
fixture.send(41, 1, b"kept\n");
fixture.source.order.runner = runner;
assert_eq!(fixture.check(1), Err(Error::NotOwnerBoundary));
assert_eq!(fixture.source.order.pending[&41], vec![b"kept\n".to_vec()]);
assert_eq!(fixture.source.order.received[&41].count, 1);
assert!(fixture.written.lock().unwrap().is_empty());
}
}
#[test]
fn native_coordinator_cut_requires_a_healthy_actual_message_source() {
use std::os::fd::FromRawFd;
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
assert_eq!(
super::inspect_coordinator_forwarding_cut(None, 19, 41, 0, None, || false),
Err(Error::MissingSource)
);
assert_eq!(
super::inspect_coordinator_forwarding_cut(None, 19, 0, 0, None, || false),
Err(Error::InvalidOwner)
);
let mut fixture = NativeCutSource::new();
fixture.source.socket = std::fs::File::open("/dev/null").unwrap().into();
assert_eq!(fixture.check(0), Err(Error::InvalidSource));
super::collect_forwarded(&mut fixture.source);
assert_eq!(
fixture.source.transport_failure,
Some(super::ForwardedSourceError::Io(libc::ENOTSOCK))
);
assert_eq!(fixture.check(0), Err(Error::SourceIo(libc::ENOTSOCK)));
let (stream, _peer) = std::os::unix::net::UnixStream::pair().unwrap();
let mut fixture = NativeCutSource::new();
fixture.source.socket = stream.into();
assert_eq!(
fixture.check(0),
Err(Error::InvalidSource),
"a stream cannot stand in for the counted message transport"
);
let mut fixture = NativeCutSource::new();
let fd =
unsafe { libc::socket(libc::AF_UNIX, libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC, 0) };
assert!(fd >= 0);
fixture.source.socket = unsafe { std::os::fd::OwnedFd::from_raw_fd(fd) };
assert_eq!(fixture.check(0), Err(Error::SourceIo(libc::ENOTCONN)));
let fixture = NativeCutSource::new();
fixture.send(41, 1, b"retained on closure\n");
let NativeCutSource {
mut source, sender, ..
} = fixture;
drop(sender);
super::collect_forwarded(&mut source);
assert_eq!(
source.transport_failure,
Some(super::ForwardedSourceError::Closed)
);
for _ in 0..2 {
assert_eq!(
super::inspect_coordinator_forwarding_cut(
Some(&mut source),
19,
41,
1,
None,
|| false
),
Err(Error::ClosedSource)
);
assert_eq!(source.order.received[&41].count, 1);
assert_eq!(
source.order.pending[&41],
vec![b"retained on closure\n".to_vec()]
);
}
}
#[test]
fn native_coordinator_cut_receipt_rechecks_generation_socket_and_owner() {
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
let mut fixture = NativeCutSource::new();
let receipt = fixture.check(0).unwrap();
assert_eq!(
super::inspect_coordinator_forwarding_cut(
Some(&mut fixture.source),
20,
41,
0,
Some(&receipt),
|| false
),
Err(Error::SourceChanged)
);
assert_eq!(
super::inspect_coordinator_forwarding_cut(
Some(&mut fixture.source),
19,
43,
0,
Some(&receipt),
|| false
),
Err(Error::SourceChanged)
);
let mut replacement = NativeCutSource::new();
replacement.send(41, 1, b"new source\n");
assert_eq!(
super::inspect_coordinator_forwarding_cut(
Some(&mut replacement.source),
19,
41,
1,
Some(&receipt),
|| false
),
Err(Error::SourceChanged)
);
super::collect_forwarded(&mut replacement.source);
assert_eq!(replacement.source.order.received[&41].count, 1);
assert_eq!(
replacement.source.order.pending[&41],
vec![b"new source\n".to_vec()]
);
}
#[test]
fn native_coordinator_cut_refuses_real_loss_notices_and_persistent_process_loss() {
use std::os::fd::AsRawFd;
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
for message in [
b"HERMIT_DETLOG_RECORD_LOST 0 1 actual forwarding loss\n".as_slice(),
b"untagged actual record\n".as_slice(),
] {
let mut fixture = NativeCutSource::new();
assert_eq!(
unsafe {
libc::send(
fixture.sender.as_raw_fd(),
message.as_ptr().cast(),
message.len(),
libc::MSG_NOSIGNAL,
)
},
message.len() as isize
);
assert_eq!(fixture.check(0), Err(Error::DeterminismLoss));
assert!(fixture.source.loss_seen);
assert!(
fixture
.written
.lock()
.unwrap()
.iter()
.any(|record| record.starts_with(super::FORWARDING_LOSS_NOTICE.as_bytes()))
);
assert_eq!(fixture.check(0), Err(Error::DeterminismLoss));
}
super::clear_forwarded_source();
super::record_determinism_loss("native cut test persistent loss");
let NativeCutSource { source, sender, .. } = NativeCutSource::new();
super::set_forwarded_source(source.socket, source.sink);
assert_eq!(
super::check_native_exec_forwarding_cut(41, 0, None),
Err(Error::DeterminismLoss)
);
super::clear_forwarded_source();
assert_eq!(
super::check_native_exec_forwarding_cut(41, 0, None),
Err(Error::MissingSource)
);
drop(sender);
}
#[test]
fn native_coordinator_cut_restores_unchecked_queue_after_sink_closure() {
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
let fixture = NativeCutSource::new();
fixture.send(41, 1, b"written before closure\n");
let NativeCutSource {
mut source,
sender,
written,
} = fixture;
let mut peer = Some(sender);
let sink = written.clone();
source.sink = Box::new(move |record| {
sink.lock().unwrap().push(record.to_vec());
drop(peer.take());
});
assert_eq!(
super::inspect_coordinator_forwarding_cut(Some(&mut source), 19, 41, 1, None, || false),
Err(Error::ClosedSource)
);
assert_eq!(
*written.lock().unwrap(),
vec![b"written before closure\n".to_vec()]
);
assert_eq!(source.order.received[&41].count, 1);
assert_eq!(
source.order.pending[&41],
vec![b"written before closure\n".to_vec()]
);
}
#[test]
fn native_coordinator_cut_rechecks_loss_reported_by_the_record_sink() {
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
let fixture = NativeCutSource::new();
fixture.send(41, 1, b"sink failure\n");
let NativeCutSource {
mut source,
sender,
written,
} = fixture;
let local_loss = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let loss_from_sink = local_loss.clone();
let sink = written.clone();
source.sink = Box::new(move |record| {
sink.lock().unwrap().push(record.to_vec());
super::record_determinism_loss("native cut test sink failure");
loss_from_sink.store(true, Ordering::Release);
});
assert_eq!(
super::inspect_coordinator_forwarding_cut(Some(&mut source), 19, 41, 1, None, || {
local_loss.load(Ordering::Acquire)
}),
Err(Error::DeterminismLoss)
);
assert!(super::determinism_loss().is_some());
assert_eq!(*written.lock().unwrap(), vec![b"sink failure\n".to_vec()]);
assert_eq!(source.order.received[&41].count, 1);
assert_eq!(source.order.pending[&41], vec![b"sink failure\n".to_vec()]);
drop(sender);
}
#[test]
fn native_coordinator_cut_retains_actual_packet_that_arrives_during_sink_write() {
use std::os::fd::AsRawFd;
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
let fixture = NativeCutSource::new();
fixture.send(41, 1, b"first\n");
let NativeCutSource {
mut source,
sender,
written,
} = fixture;
let sink = written.clone();
let mut sent = false;
source.sink = Box::new(move |record| {
sink.lock().unwrap().push(record.to_vec());
if !sent {
sent = true;
let message = super::tagged_forwarded_message(41, 2, b"late\n");
assert_eq!(
unsafe {
libc::send(
sender.as_raw_fd(),
message.as_ptr().cast(),
message.len(),
libc::MSG_NOSIGNAL,
)
},
message.len() as isize
);
}
});
assert_eq!(
super::inspect_coordinator_forwarding_cut(Some(&mut source), 19, 41, 1, None, || false),
Err(Error::CountMismatch {
produced: 1,
received: 2
})
);
assert_eq!(source.order.received[&41].count, 2);
assert_eq!(
source.order.pending[&41],
vec![b"first\n".to_vec(), b"late\n".to_vec()]
);
assert_eq!(written.lock().unwrap()[0], b"first\n".to_vec());
assert!(source.loss_seen);
}
#[test]
fn forwarded_records_are_counted_per_thread_until_taken() {
std::thread::spawn(|| {
assert_eq!(super::take_forwarded_since_request(), 0);
super::count_forwarded();
super::count_forwarded();
assert_eq!(super::take_forwarded_since_request(), 2);
assert_eq!(super::take_forwarded_since_request(), 0);
super::count_forwarded();
let other = std::thread::spawn(|| {
super::count_forwarded();
super::take_forwarded_since_request()
});
assert_eq!(other.join().unwrap(), 1);
assert_eq!(super::take_forwarded_since_request(), 1);
})
.join()
.unwrap();
}
#[test]
fn count_slots_are_released_and_an_uncounted_thread_is_reported() {
let _source = global_source_test();
for _ in 0..(super::FORWARDED_COUNTS.len() * 2) {
std::thread::spawn(|| {
super::count_forwarded();
assert_eq!(super::take_forwarded_since_request(), 1);
super::release_forwarded_count();
})
.join()
.unwrap();
}
assert!(!super::FORWARDING_UNCOUNTED.load(Ordering::Acquire));
let child = unsafe { libc::fork() };
assert!(child >= 0);
if child == 0 {
unsafe { libc::alarm(20) };
for _ in 0..=super::FORWARDED_COUNTS.len() {
std::thread::spawn(super::count_forwarded).join().unwrap();
}
let reported = super::take_forwarded_since_request();
unsafe { libc::_exit(if reported == super::UNCOUNTED { 0 } else { 1 }) };
}
let mut status = 0;
assert_eq!(unsafe { libc::waitpid(child, &mut status, 0) }, child);
assert!(
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0,
"{status:#x}"
);
}
#[test]
fn counts_are_untouched_until_the_first_record() {
let _source = global_source_test();
let child = unsafe { libc::fork() };
assert!(child >= 0);
if child == 0 {
unsafe { libc::alarm(20) };
for slot in &super::FORWARDED_COUNTS {
slot.tid.store(0, Ordering::Release);
slot.count.store(0, Ordering::Release);
}
super::FORWARDING_STARTED.store(false, Ordering::Release);
let me = super::current_tid();
let claimed = || {
super::FORWARDED_COUNTS
.iter()
.any(|slot| slot.tid.load(Ordering::Acquire) != 0)
};
let ok = super::take_forwarded_since_request() == 0
&& !claimed()
&& {
super::release_forwarded_count();
!claimed()
}
&& super::count_forwarded() == 1
&& super::FORWARDING_STARTED.load(Ordering::Acquire)
&& super::FORWARDED_COUNTS
.iter()
.any(|slot| slot.tid.load(Ordering::Acquire) == me)
&& super::count_forwarded() == 2
&& super::take_forwarded_since_request() == 2
&& super::take_forwarded_since_request() == 0;
unsafe { libc::_exit(if ok { 0 } else { 1 }) };
}
let mut status = 0;
assert_eq!(unsafe { libc::waitpid(child, &mut status, 0) }, child);
assert!(
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0,
"{status:#x}"
);
}
#[test]
fn a_released_slot_on_the_probe_path_is_not_claimed_twice() {
let _source = global_source_test();
let buckets = super::FORWARDED_COUNTS.len() as i32;
let base = 0x3f00_0000 - 0x3f00_0000 % buckets + 7;
let (first, second, third) = (base, base + buckets, base + 2 * buckets);
let slot = |tid| super::forwarded_count_slot(tid).map(|slot| slot as *const _);
let first_slot = slot(first).unwrap();
let second_slot = slot(second).unwrap();
let third_slot = slot(third).unwrap();
assert_ne!(second_slot, third_slot);
super::release_count_slot(second);
assert_eq!(
slot(third),
Some(third_slot),
"the hole is not a second slot"
);
assert_eq!(slot(first), Some(first_slot));
for tid in [first, third] {
super::release_count_slot(tid);
}
let me = super::current_pid();
assert!(!super::private_copy(me, me));
let child = unsafe { libc::fork() };
assert!(child >= 0);
if child == 0 {
unsafe { libc::_exit(0) };
}
assert!(
super::private_copy(child, me),
"a forked child's memory is its own"
);
let mut status = 0;
assert_eq!(unsafe { libc::waitpid(child, &mut status, 0) }, child);
assert!(
super::private_copy(child, me),
"an exited process holds nothing"
);
extern "C" fn shared_child(parent: *mut libc::c_void) -> libc::c_int {
let own = unsafe { libc::syscall(libc::SYS_getpid) } as i32;
i32::from(super::private_copy(parent as usize as i32, own))
}
let mut stack = vec![0u8; 256 * 1024];
let top = (stack.as_mut_ptr() as usize + stack.len()) & !15;
let shared = unsafe {
libc::clone(
shared_child,
top as *mut libc::c_void,
libc::CLONE_VM | libc::SIGCHLD,
me as usize as *mut libc::c_void,
)
};
assert!(shared > 0, "{}", std::io::Error::last_os_error());
assert_eq!(unsafe { libc::waitpid(shared, &mut status, 0) }, shared);
drop(stack);
assert!(
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0,
"a CLONE_VM child shares this memory: {status:#x}"
);
}
#[test]
fn a_required_forwarder_that_is_missing_or_narrowed_makes_counts_uncounted() {
let _source = global_source_test();
let in_child = |install: Option<&str>, expected: &str| -> bool {
let child = unsafe { libc::fork() };
assert!(child >= 0);
if child == 0 {
unsafe { libc::alarm(20) };
let ok = super::FORWARDER.get().is_none()
&& install.is_none_or(|policy| {
let policy = ForwardPolicy::decode(policy).unwrap();
super::set_forwarder(|_, _, _, _| {}, policy).is_ok()
});
super::require_forwarding(expected);
let uncounted = super::take_forwarded_since_request() == super::UNCOUNTED;
unsafe {
libc::_exit(if !ok {
2
} else if uncounted {
1
} else {
0
})
};
}
let mut status = 0;
assert_eq!(unsafe { libc::waitpid(child, &mut status, 0) }, child);
assert!(
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) != 2,
"{status:#x}"
);
libc::WEXITSTATUS(status) == 1
};
let required = "1,detcore::tool_local=1";
assert!(in_child(None, required), "no forwarder: uncounted");
assert!(
in_child(Some("1,detcore::tool_local=0"), required),
"a narrowed policy: uncounted"
);
assert!(
!in_child(Some(required), required),
"the required policy: counted"
);
}
#[test]
fn a_counted_request_with_missing_records_is_a_loss() {
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::sync::Arc;
use std::sync::Mutex;
let _source = global_source_test();
let mut fds = [0; 2];
assert_eq!(
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_SEQPACKET, 0, fds.as_mut_ptr()) },
0
);
let written = Arc::new(Mutex::new(Vec::<Vec<u8>>::new()));
let sink = Arc::clone(&written);
super::set_forwarded_source(
unsafe { OwnedFd::from_raw_fd(fds[0]) },
Box::new(move |record| sink.lock().unwrap().push(record.to_vec())),
);
let send = |tid: i32, index: u64, line: &str| {
let message = super::tagged_forwarded_message(tid, index, line.as_bytes());
let sent = unsafe { libc::send(fds[1], message.as_ptr().cast(), message.len(), 0) };
assert_eq!(sent, message.len() as isize);
};
let notices = |written: &Arc<Mutex<Vec<Vec<u8>>>>| -> Vec<String> {
written
.lock()
.unwrap()
.iter()
.filter(|record| record.starts_with(super::FORWARDING_LOSS_NOTICE.as_bytes()))
.map(|record| String::from_utf8_lossy(record).into_owned())
.filter(|notice| notice.contains("thread 41 ") || notice.contains("thread 41)"))
.collect()
};
send(41, 1, "a\n");
send(41, 2, "b\n");
super::check_forwarded_count(41, 2);
assert_eq!(notices(&written).len(), 0, "as many records as produced");
send(41, 1, "c\n");
super::check_forwarded_count(41, 3);
assert_eq!(notices(&written).len(), 1, "a deficit is a loss");
assert!(
notices(&written)[0].contains(
"lost: thread 41 produced 3 since its last counted request and 1 arrived"
),
"{:?}",
notices(&written)
);
send(41, 1, "d\n");
send(41, 2, "e\n");
super::check_forwarded_count(41, 1);
assert_eq!(notices(&written).len(), 2, "a surplus is refused too");
assert!(
notices(&written)[1].contains("not the Tool's: thread 41 produced 1"),
"{:?}",
notices(&written)
);
super::check_forwarded_count(41, super::UNCOUNTED);
assert_eq!(notices(&written).len(), 3, "an uncounted thread is refused");
assert!(
notices(&written)[2].contains("uncounted"),
"{:?}",
notices(&written)
);
send(41, 1, "f\n");
send(41, 3, "g\n");
super::check_forwarded_count(41, 2);
assert_eq!(notices(&written).len(), 4, "a skipped index is a loss");
assert!(
notices(&written)[3]
.contains("lost: thread 41 sent record 3 of its interval where 2 was next"),
"{:?}",
notices(&written)
);
send(41, 1, "h\n");
send(41, 1, "padding\n");
super::check_forwarded_count(41, 2);
assert_eq!(notices(&written).len(), 5, "a repeated index is refused");
assert!(
notices(&written)[4].contains(
"not the Tool's: thread 41 sent record 1 of its interval where 2 was next"
),
"{:?}",
notices(&written)
);
send(41, 1, "i\n");
super::check_forwarded_count(41, 1);
assert_eq!(notices(&written).len(), 5, "a new interval starts at 1");
assert!(super::determinism_loss().is_some());
super::clear_forwarded_source();
unsafe { libc::close(fds[1]) };
}
#[test]
fn a_full_socket_buffer_does_not_wait_for_a_request() {
let _source = global_source_test();
use std::os::fd::FromRawFd;
use std::os::fd::OwnedFd;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use std::time::Instant;
const RECORDS: usize = 2000;
let mut fds = [0; 2];
assert_eq!(
unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_SEQPACKET, 0, fds.as_mut_ptr()) },
0
);
let small: libc::c_int = 4096;
for fd in fds {
for option in [libc::SO_SNDBUF, libc::SO_RCVBUF] {
unsafe {
libc::setsockopt(
fd,
libc::SOL_SOCKET,
option,
(&raw const small).cast(),
std::mem::size_of_val(&small) as libc::socklen_t,
)
};
}
}
let written = Arc::new(Mutex::new(Vec::<Vec<u8>>::new()));
let sink = Arc::clone(&written);
super::set_forwarded_source(
unsafe { OwnedFd::from_raw_fd(fds[0]) },
Box::new(move |record| sink.lock().unwrap().push(record.to_vec())),
);
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap();
runtime.block_on(async { super::drain_forwarded_at_request(1) });
let sender = fds[1];
let started = Instant::now();
let sending = std::thread::spawn(move || {
for index in 0..RECORDS {
let message = super::tagged_forwarded_message(
7,
index as u64 + 1,
format!("record {index:04}\n").as_bytes(),
);
let sent = unsafe {
libc::send(
sender,
message.as_ptr().cast(),
message.len(),
libc::MSG_NOSIGNAL,
)
};
assert_eq!(sent, message.len() as isize);
}
});
while !sending.is_finished() {
assert!(
started.elapsed() < Duration::from_secs(20),
"the sender stayed blocked on a full socket buffer"
);
std::thread::sleep(Duration::from_millis(10));
}
sending.join().unwrap();
let own = |written: &Arc<Mutex<Vec<Vec<u8>>>>| -> Vec<Vec<u8>> {
written
.lock()
.unwrap()
.iter()
.filter(|record| record.starts_with(b"record "))
.cloned()
.collect()
};
assert!(
own(&written).is_empty(),
"records were written before their point"
);
super::clear_forwarded_source();
let written = own(&written);
assert_eq!(written.len(), RECORDS);
for (index, record) in written.iter().enumerate() {
assert_eq!(record, format!("record {index:04}\n").as_bytes());
}
unsafe { libc::close(sender) };
}
#[test]
fn forwarded_line_names_the_emitting_module() {
assert_eq!(
super::forwarded_line(
"detcore::tool_local",
" DETLOG_RECORD={}",
format_args!("USER RAND: seeding PRNG for root thread with seed {}", 0)
),
b"INFO detcore::tool_local: DETLOG USER RAND: seeding PRNG for root thread with seed 0 DETLOG_RECORD={}\n"
.to_vec()
);
}
#[test]
fn forwarding_failures_become_loss_notices() {
let mut pair = [-1; 2];
let created = unsafe {
libc::socketpair(
libc::AF_UNIX,
libc::SOCK_SEQPACKET | libc::SOCK_CLOEXEC,
0,
pair.as_mut_ptr(),
)
};
assert_eq!(created, 0);
let [sender, receiver] = pair;
let mut buffer = vec![0u8; 2 << 20];
let mut receive = || {
let received = unsafe {
libc::recv(
receiver,
buffer.as_mut_ptr().cast(),
buffer.len(),
libc::MSG_DONTWAIT,
)
};
assert!(received > 0, "{}", std::io::Error::last_os_error());
String::from_utf8_lossy(&buffer[..received as usize]).into_owned()
};
let is_notice =
|text: &str| text.starts_with(&format!("{} ", super::FORWARDING_LOSS_NOTICE));
unsafe {
*libc::__errno_location() = libc::EAGAIN;
super::send_forwarded_record(-1, "detcore", "", 1, format_args!("lost"));
assert_eq!(*libc::__errno_location(), libc::EAGAIN);
}
let tag = |index: u64| format!("T{}.{index} ", super::current_tid());
super::send_forwarded_record(sender, "detcore", "", 2, format_args!("after"));
let notice = receive();
assert!(is_notice(¬ice), "{notice}");
assert_eq!(receive(), format!("{}INFO detcore: DETLOG after\n", tag(2)));
let small: libc::c_int = 4096;
unsafe {
libc::setsockopt(
sender,
libc::SOL_SOCKET,
libc::SO_SNDBUF,
(&raw const small).cast(),
std::mem::size_of::<libc::c_int>() as libc::socklen_t,
)
};
let huge = "x".repeat(1 << 20);
super::send_forwarded_record(sender, "detcore", "", 1, format_args!("{huge}"));
super::send_forwarded_record(sender, "detcore", "", 2, format_args!("small"));
let notice = receive();
assert!(is_notice(¬ice), "{notice}");
assert_eq!(receive(), format!("{}INFO detcore: DETLOG small\n", tag(2)));
unsafe {
libc::close(sender);
libc::close(receiver);
}
}
#[test]
fn forwarded_records_are_written_at_points_the_schedule_fixes() {
let mut order = super::ForwardedOrder::new();
let text = |records: Vec<Vec<u8>>| -> Vec<String> {
records
.into_iter()
.map(|record| String::from_utf8(record).unwrap())
.collect()
};
let none: Vec<String> = Vec::new();
order.queue(3, b"root-1".to_vec());
assert_eq!(text(order.at_request(3)), ["root-1"]);
assert_eq!(text(order.at_commit(3, true)), none);
order.queue(9, b"child-1".to_vec());
assert_eq!(text(order.at_request(9)), none);
assert_eq!(text(order.at_commit(9, true)), ["child-1"]);
order.queue(3, b"root-2".to_vec());
order.queue(9, b"child-2".to_vec());
assert_eq!(text(order.at_request(9)), ["child-2"]);
assert_eq!(text(order.at_request(3)), none);
assert_eq!(text(order.at_commit(3, false)), ["root-2"]);
order.queue(3, b"root-3".to_vec());
assert_eq!(text(order.at_request(3)), none);
order.queue(12, b"late-12".to_vec());
order.queue(5, b"late-5".to_vec());
assert_eq!(text(order.finish()), ["root-3", "late-5", "late-12"]);
assert_eq!(text(order.finish()), none);
let mut order = super::ForwardedOrder::new();
order.queue(3, b"first-root".to_vec());
order.queue(4, b"second-root".to_vec());
assert_eq!(text(order.at_request(3)), ["first-root"]);
let notice = String::from_utf8(super::FORWARDING_AMBIGUOUS_ORDER_NOTICE.to_vec()).unwrap();
assert!(notice.starts_with(&format!("{} ", super::FORWARDING_LOSS_NOTICE)));
assert_eq!(text(order.at_request(4)), [notice]);
assert_eq!(text(order.at_request(4)), none);
assert_eq!(text(order.at_commit(4, true)), ["second-root"]);
let mut order = super::ForwardedOrder::new();
let retired = String::from_utf8(super::FORWARDING_RETIRED_NOTICE.to_vec()).unwrap();
assert_eq!(
text(order.untagged(super::FORWARDING_RETIRED_NOTICE)),
[retired]
);
let untagged =
String::from_utf8(super::FORWARDING_UNTAGGED_RECORD_NOTICE.to_vec()).unwrap();
assert!(untagged.starts_with(&format!("{} ", super::FORWARDING_LOSS_NOTICE)));
assert_eq!(
text(order.untagged(b"INFO detcore: DETLOG legacy-1\n")),
[untagged, "INFO detcore: DETLOG legacy-1\n".to_string()]
);
assert_eq!(
text(order.untagged(b"INFO detcore: DETLOG legacy-2\n")),
["INFO detcore: DETLOG legacy-2\n"]
);
}
#[test]
fn a_loss_notice_records_the_determinism_loss_latch() {
let _source = global_source_test();
super::write_loss_notice("test: an unscheduled exit");
let loss = super::determinism_loss().expect("the latch must be set");
assert!(!loss.is_empty());
super::record_determinism_loss("test: a later reason");
assert_eq!(super::determinism_loss(), Some(loss));
}
#[test]
fn split_forwarded_message_reads_the_sender_tag() {
assert_eq!(
super::split_forwarded_message(b"T42.7 INFO detcore: x\n"),
Some((42, 7, &b"INFO detcore: x\n"[..]))
);
assert_eq!(
super::split_forwarded_message(b"T42 INFO detcore: x\n"),
None
);
assert_eq!(super::split_forwarded_message(b"T42. INFO"), None);
assert_eq!(super::split_forwarded_message(b"T42.x INFO"), None);
assert_eq!(
super::split_forwarded_message(super::FORWARDING_RETIRED_NOTICE),
None
);
assert_eq!(super::split_forwarded_message(b"T INFO"), None);
assert_eq!(super::split_forwarded_message(b"Tx1 INFO"), None);
assert_eq!(super::split_forwarded_message(b"INFO detcore"), None);
}
#[test]
fn test_detlog() {
detlog!("Hello : {}. From {:?}", "World", 31337);
}
#[test]
fn structured_record_round_trips_and_refuses_an_incomplete_current_shape() {
let suffix = record_suffix(DetLogEvent::SyscallResult {
finished_syscall_number: 37,
});
let line = format!("INFO detcore: DETLOG finish syscall #999{suffix}");
let (human, record) = DetLogRecord::split(&line).unwrap();
assert_eq!(human, "INFO detcore: DETLOG finish syscall #999");
assert_eq!(
record.unwrap().event,
DetLogEvent::SyscallResult {
finished_syscall_number: 37
}
);
let missing_number = format!(
"INFO detcore: DETLOG finish syscall #999{RECORD_SEPARATOR}{{\"schema\":1,\"event\":{{\"kind\":\"syscall_result\"}}}}"
);
assert!(
DetLogRecord::split(&missing_number)
.unwrap_err()
.contains("finished_syscall_number"),
"an incomplete current record must fail by field name"
);
}
struct AlwaysEnabled;
impl tracing::Subscriber for AlwaysEnabled {
fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest {
Interest::sometimes()
}
fn enabled(&self, _: &Metadata<'_>) -> bool {
true
}
fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
span::Id::from_u64(1)
}
fn record(&self, _: &span::Id, _: &span::Record<'_>) {}
fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}
fn event(&self, _: &tracing::Event<'_>) {}
fn enter(&self, _: &span::Id) {}
fn exit(&self, _: &span::Id) {}
}
#[test]
fn detlog_observed_is_true_when_a_subscriber_is_listening() {
tracing::subscriber::with_default(AlwaysEnabled, || {
assert!(detlog_observed!());
});
}
#[test]
fn detlog_observed_is_false_when_nothing_is_listening() {
tracing::subscriber::with_default(tracing::subscriber::NoSubscriber::default(), || {
assert!(!detlog_observed!());
});
}
struct RandomAtInfo;
impl tracing::Subscriber for RandomAtInfo {
fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest {
Interest::sometimes()
}
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
*metadata.level() <= tracing::Level::WARN
|| (*metadata.level() <= tracing::Level::INFO
&& metadata.target().starts_with("detcore::random"))
}
fn max_level_hint(&self) -> Option<tracing::level_filters::LevelFilter> {
Some(tracing::level_filters::LevelFilter::INFO)
}
fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
span::Id::from_u64(1)
}
fn record(&self, _: &span::Id, _: &span::Record<'_>) {}
fn record_follows_from(&self, _: &span::Id, _: &span::Id) {}
fn event(&self, _: &tracing::Event<'_>) {}
fn enter(&self, _: &span::Id) {}
fn exit(&self, _: &span::Id) {}
}
#[test]
fn forward_policy_answers_per_target_as_the_subscriber_does() {
let policy = tracing::subscriber::with_default(RandomAtInfo, || {
ForwardPolicy::from_current_subscriber("warn,detcore::random=info")
});
assert!(policy.forwards_any());
assert!(policy.forwards("detcore::random"));
assert!(policy.forwards("detcore::random::inner"));
assert!(!policy.forwards("detcore"));
assert!(!policy.forwards("detcore::tool_local"));
assert!(!policy.forwards("detcore::scheduler::runqueue"));
assert_eq!(policy.encode(), "0,warn=0,detcore::random=1");
let everything = tracing::subscriber::with_default(AlwaysEnabled, || {
ForwardPolicy::from_current_subscriber("")
});
assert_eq!(everything, ForwardPolicy::all());
let nothing =
tracing::subscriber::with_default(tracing::subscriber::NoSubscriber::default(), || {
ForwardPolicy::from_current_subscriber("warn,detcore::random=info")
});
assert!(!nothing.forwards_any());
}
#[test]
fn forward_policy_most_specific_target_decides() {
let policy = ForwardPolicy::decode("1,detcore=0,detcore::random=1").unwrap();
assert!(policy.forwards("hermit"));
assert!(!policy.forwards("detcore"));
assert!(!policy.forwards("detcore::tool_local"));
assert!(policy.forwards("detcore::random"));
assert!(policy.forwards("detcore::randomness"));
}
#[test]
fn forward_policy_encoding_round_trips_and_refuses_malformed_values() {
assert_eq!(ForwardPolicy::decode("1").unwrap(), ForwardPolicy::all());
let policy = ForwardPolicy::decode("0,warn=0,detcore::random=1").unwrap();
assert_eq!(ForwardPolicy::decode(&policy.encode()).unwrap(), policy);
for malformed in ["", "2", "1,detcore", "1,=1", "1,detcore=2", "1,detcore=1=0"] {
assert!(
ForwardPolicy::decode(malformed).is_err(),
"{malformed:?} must be refused"
);
}
}
struct ModelPhysicalCompletion {
fd: std::os::fd::OwnedFd,
pid: i32,
identity: u64,
}
unsafe impl super::NativePhysicalExitCompletion for ModelPhysicalCompletion {
fn retained_pidfd(&self) -> std::os::fd::BorrowedFd<'_> {
use std::os::fd::AsFd;
self.fd.as_fd()
}
fn process_id(&self) -> i32 {
self.pid
}
fn birth_identity(&self) -> u64 {
self.identity
}
}
fn model_completion(dead: bool) -> ModelPhysicalCompletion {
use std::os::fd::FromRawFd;
let mut child = dead.then(|| std::process::Command::new("true").spawn().unwrap());
let pid = child
.as_ref()
.map_or(std::process::id(), std::process::Child::id);
let raw = unsafe { libc::syscall(libc::SYS_pidfd_open, pid, 0) };
assert!(raw >= 0);
let fd = unsafe { std::os::fd::OwnedFd::from_raw_fd(raw as i32) };
let mut stat: libc::stat = unsafe { std::mem::zeroed() };
use std::os::fd::AsRawFd;
assert_eq!(unsafe { libc::fstat(fd.as_raw_fd(), &mut stat) }, 0);
if let Some(child) = child.as_mut() {
assert!(child.wait().unwrap().success());
}
ModelPhysicalCompletion {
fd,
pid: pid as i32,
identity: stat.st_ino,
}
}
#[test]
fn model_native_final_drain_requires_actual_death_and_credited_real_source() {
use std::os::fd::AsRawFd;
use super::CoordinatorForwardingCutError as Error;
let mut fixture = NativeCutSource::new();
fixture.send(41, 1, b"credited\n");
fixture.check(1).unwrap();
let identity = super::socket_identity(fixture.source.socket.as_raw_fd()).unwrap();
drop(fixture.sender);
let live = model_completion(false);
assert_eq!(
super::inspect_native_final_source(
&mut fixture.source,
identity,
(live.pid, live.identity),
&live
),
Err(Error::InvalidOwner)
);
let dead = model_completion(true);
assert!(
super::inspect_native_final_source(
&mut fixture.source,
identity,
(dead.pid, dead.identity),
&dead
)
.is_ok()
);
assert_eq!(*fixture.written.lock().unwrap(), [b"credited\n".to_vec()]);
}
#[test]
fn model_native_final_drain_retains_transport_loss_tail_and_identity_failures() {
use std::os::fd::AsRawFd;
use super::CoordinatorForwardingCutError as Error;
use super::ForwardedSourceError;
let dead = model_completion(true);
let mut fixture = NativeCutSource::new();
fixture.check(0).unwrap();
let identity = super::socket_identity(fixture.source.socket.as_raw_fd()).unwrap();
drop(fixture.sender);
fixture.source.transport_failure = Some(ForwardedSourceError::Io(libc::EIO));
assert_eq!(
super::inspect_native_final_source(
&mut fixture.source,
identity,
(dead.pid, dead.identity),
&dead
),
Err(Error::SourceIo(libc::EIO))
);
assert_eq!(
fixture.source.transport_failure,
Some(ForwardedSourceError::Io(libc::EIO))
);
assert_eq!(
super::inspect_native_final_source(
&mut fixture.source,
(identity.0, identity.1 ^ 1),
(dead.pid, dead.identity),
&dead
),
Err(Error::SourceChanged)
);
let mut tail = NativeCutSource::new();
tail.check(0).unwrap();
tail.send(41, 1, b"uncredited\n");
let identity = super::socket_identity(tail.source.socket.as_raw_fd()).unwrap();
drop(tail.sender);
assert_eq!(
super::inspect_native_final_source(
&mut tail.source,
identity,
(dead.pid, dead.identity),
&dead
),
Err(Error::OtherOwnerPending(41))
);
assert_eq!(tail.source.order.received[&41].count, 1);
assert!(tail.written.lock().unwrap().is_empty());
tail.source.loss_seen = true;
assert_eq!(
super::inspect_native_final_source(
&mut tail.source,
identity,
(dead.pid, dead.identity),
&dead
),
Err(Error::DeterminismLoss)
);
}
#[test]
fn model_native_final_drain_refuses_another_births_completion_without_consuming_source() {
use std::os::fd::AsRawFd;
use super::CoordinatorForwardingCutError as Error;
let owner = model_completion(false);
let unrelated = model_completion(true);
assert_ne!(owner.identity, unrelated.identity);
let mut fixture = NativeCutSource::new();
fixture.check(0).unwrap();
let identity = super::socket_identity(fixture.source.socket.as_raw_fd()).unwrap();
drop(fixture.sender);
assert_eq!(
super::inspect_native_final_source(
&mut fixture.source,
identity,
(owner.pid, owner.identity),
&unrelated
),
Err(Error::InvalidOwner)
);
assert_eq!(fixture.source.transport_failure, None);
assert!(fixture.written.lock().unwrap().is_empty());
}
type ForwardedReceiverStop =
std::sync::Arc<std::sync::Mutex<Option<tokio::sync::oneshot::Sender<()>>>>;
struct ForwardedReceiverTask {
runtime: tokio::runtime::Runtime,
task: Option<tokio::task::JoinHandle<()>>,
stop: ForwardedReceiverStop,
done: Option<std::sync::mpsc::Sender<()>>,
watchdog: Option<std::thread::JoinHandle<()>>,
watchdog_fired: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
impl ForwardedReceiverTask {
fn spawn(watched: std::os::fd::OwnedFd, generation: u64) -> Self {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap();
let (stop, stopped) = tokio::sync::oneshot::channel();
let stop = std::sync::Arc::new(std::sync::Mutex::new(Some(stop)));
let watchdog_stop = stop.clone();
let watchdog_fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let fired = watchdog_fired.clone();
let (done, completed) = std::sync::mpsc::channel();
let watchdog = std::thread::spawn(move || {
if completed
.recv_timeout(std::time::Duration::from_secs(5))
.is_err()
{
fired.store(true, Ordering::SeqCst);
if let Some(stop) = watchdog_stop.lock().unwrap().take() {
let _ = stop.send(());
}
}
});
let task = runtime.spawn(super::receive_forwarded(watched, generation, stopped));
Self {
runtime,
task: Some(task),
stop,
done: Some(done),
watchdog: Some(watchdog),
watchdog_fired,
}
}
fn request_stop(&self) {
if let Some(stop) = self.stop.lock().unwrap().take() {
let _ = stop.send(());
}
}
fn join(&mut self) -> (Result<(), tokio::task::JoinError>, bool) {
let result = self.runtime.block_on(self.task.take().unwrap());
let _ = self.done.take().unwrap().send(());
let watchdog_ok = self.watchdog.take().unwrap().join().is_ok();
(
result,
self.watchdog_fired.load(Ordering::SeqCst) || !watchdog_ok,
)
}
fn is_finished(&self) -> bool {
self.task.as_ref().unwrap().is_finished()
}
}
impl Drop for ForwardedReceiverTask {
fn drop(&mut self) {
if self.task.is_some() {
self.request_stop();
let _ = self.join();
}
}
}
struct ForwardedReceiverSourceCleanup;
impl Drop for ForwardedReceiverSourceCleanup {
fn drop(&mut self) {
super::clear_forwarded_source();
}
}
struct ReceiverSource {
sender: std::os::fd::OwnedFd,
written: std::sync::Arc<std::sync::Mutex<Vec<Vec<u8>>>>,
generation: u64,
identity: (u64, u64),
}
fn install_receiver_source() -> ReceiverSource {
use std::os::fd::AsRawFd;
let NativeCutSource {
source,
sender,
written,
} = NativeCutSource::new();
let identity = super::socket_identity(source.socket.as_raw_fd()).unwrap();
super::set_forwarded_source(source.socket, source.sink);
let generation = super::FORWARDED_GENERATION.load(Ordering::SeqCst);
ReceiverSource {
sender,
written,
generation,
identity,
}
}
fn send_receiver_record(sender: &std::os::fd::OwnedFd, record: &[u8]) {
use std::os::fd::AsRawFd;
let message = super::tagged_forwarded_message(41, 1, record);
let sent = unsafe {
libc::send(
sender.as_raw_fd(),
message.as_ptr().cast(),
message.len(),
libc::MSG_NOSIGNAL,
)
};
assert_eq!(sent, message.len() as isize);
}
fn receiver_watched_socket() -> std::os::fd::OwnedFd {
super::FORWARDED_SOURCE
.lock()
.unwrap()
.as_ref()
.unwrap()
.socket
.try_clone()
.unwrap()
}
fn wait_for_receiver_record() -> bool {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
while std::time::Instant::now() < deadline {
let received = super::FORWARDED_SOURCE
.lock()
.unwrap()
.as_ref()
.unwrap()
.order
.received
.get(&41)
.is_some_and(|interval| interval.count == 1);
if received {
return true;
}
std::thread::sleep(std::time::Duration::from_millis(1));
}
false
}
#[test]
fn forwarded_receiver_retires_after_real_eof_without_rearming() {
use std::os::fd::AsRawFd;
let _guard = global_source_test();
let _cleanup = ForwardedReceiverSourceCleanup;
let ReceiverSource {
sender,
written,
generation,
identity,
} = install_receiver_source();
super::RECEIVER_GENERATION.store(generation, Ordering::SeqCst);
let (marker, mut marker_receiver) = tokio::sync::oneshot::channel();
*super::RECEIVER_STOP.lock().unwrap() = Some(marker);
let mut receiver = ForwardedReceiverTask::spawn(receiver_watched_socket(), generation);
send_receiver_record(&sender, b"credited before EOF\n");
assert!(wait_for_receiver_record(), "actual async task must collect");
assert!(!receiver.is_finished(), "live EAGAIN must keep waiting");
let credited = {
let mut source = super::FORWARDED_SOURCE.lock().unwrap();
super::inspect_coordinator_forwarding_cut(
source.as_mut(),
generation,
41,
1,
None,
|| false,
)
};
assert!(credited.is_ok());
assert_eq!(
*written.lock().unwrap(),
[b"credited before EOF\n".to_vec()]
);
drop(sender);
let (joined, watchdog_fired) = receiver.join();
assert!(joined.is_ok(), "actual receiver task must join");
assert!(!watchdog_fired, "EOF must retire without stop or abort");
let retained = {
let source = super::FORWARDED_SOURCE.lock().unwrap();
let source = source.as_ref().unwrap();
(
source.transport_failure,
super::socket_identity(source.socket.as_raw_fd()).unwrap(),
source.order.pending.values().all(Vec::is_empty),
source.order.received.is_empty(),
)
};
assert_eq!(
retained,
(
Some(super::ForwardedSourceError::Closed),
identity,
true,
true
)
);
assert_eq!(
super::FORWARDED_GENERATION.load(Ordering::SeqCst),
generation
);
assert_eq!(
super::RECEIVER_GENERATION.load(Ordering::SeqCst),
generation
);
assert!(matches!(
marker_receiver.try_recv(),
Err(tokio::sync::oneshot::error::TryRecvError::Empty)
));
receiver
.runtime
.block_on(async { super::start_forwarded_receiver() });
assert!(matches!(
marker_receiver.try_recv(),
Err(tokio::sync::oneshot::error::TryRecvError::Empty)
));
}
#[test]
fn forwarded_receiver_replacement_preserves_new_generation() {
let _guard = global_source_test();
let _cleanup = ForwardedReceiverSourceCleanup;
let ReceiverSource {
sender: old_sender,
written: old_written,
generation: old_generation,
..
} = install_receiver_source();
let old_watched = receiver_watched_socket();
send_receiver_record(&old_sender, b"old queued packet\n");
let ReceiverSource {
sender,
written,
generation,
..
} = install_receiver_source();
assert_ne!(old_generation, generation);
super::RECEIVER_GENERATION.store(generation, Ordering::SeqCst);
let (marker, mut marker_receiver) = tokio::sync::oneshot::channel();
*super::RECEIVER_STOP.lock().unwrap() = Some(marker);
send_receiver_record(&sender, b"fresh queued packet\n");
let mut stale = ForwardedReceiverTask::spawn(old_watched, old_generation);
let (joined, watchdog_fired) = stale.join();
assert!(joined.is_ok());
assert!(!watchdog_fired, "stale generation must return naturally");
let untouched = {
let source = super::FORWARDED_SOURCE.lock().unwrap();
let source = source.as_ref().unwrap();
source.order.received.is_empty() && source.order.pending.is_empty()
};
assert!(
untouched,
"stale task cannot consume the fresh source packet"
);
assert!(old_written.lock().unwrap().is_empty());
assert!(written.lock().unwrap().is_empty());
assert_eq!(
super::RECEIVER_GENERATION.load(Ordering::SeqCst),
generation
);
assert!(matches!(
marker_receiver.try_recv(),
Err(tokio::sync::oneshot::error::TryRecvError::Empty)
));
let mut fresh = ForwardedReceiverTask::spawn(receiver_watched_socket(), generation);
assert!(wait_for_receiver_record());
let queued = {
let source = super::FORWARDED_SOURCE.lock().unwrap();
let source = source.as_ref().unwrap();
(source.transport_failure, source.order.pending[&41].clone())
};
assert_eq!(queued, (None, vec![b"fresh queued packet\n".to_vec()]));
assert!(!fresh.is_finished(), "healthy open peer must keep waiting");
fresh.request_stop();
let (joined, watchdog_fired) = fresh.join();
assert!(joined.is_ok());
assert!(!watchdog_fired, "explicit healthy-source stop must join");
assert!(written.lock().unwrap().is_empty());
assert_eq!(
super::RECEIVER_GENERATION.load(Ordering::SeqCst),
generation
);
drop(sender);
drop(old_sender);
}
#[test]
fn forwarded_receiver_retirement_preserves_uncredited_tail_refusal() {
use std::os::fd::AsRawFd;
use super::CoordinatorForwardingCutError as Error;
let _guard = global_source_test();
let _cleanup = ForwardedReceiverSourceCleanup;
let ReceiverSource {
sender,
written,
generation,
identity,
} = install_receiver_source();
super::RECEIVER_GENERATION.store(generation, Ordering::SeqCst);
let mut receiver = ForwardedReceiverTask::spawn(receiver_watched_socket(), generation);
send_receiver_record(&sender, b"uncredited tail\n");
drop(sender);
let (joined, watchdog_fired) = receiver.join();
assert!(joined.is_ok());
assert!(!watchdog_fired, "EOF must retire without stop or abort");
let dead = model_completion(true);
let refusal = {
let mut source = super::FORWARDED_SOURCE.lock().unwrap();
super::inspect_native_final_source(
source.as_mut().unwrap(),
identity,
(dead.pid, dead.identity),
&dead,
)
};
assert_eq!(refusal, Err(Error::OtherOwnerPending(41)));
let retained = {
let source = super::FORWARDED_SOURCE.lock().unwrap();
let source = source.as_ref().unwrap();
(
source.transport_failure,
super::socket_identity(source.socket.as_raw_fd()).unwrap(),
source.order.received[&41].count,
source.order.pending[&41].clone(),
)
};
assert_eq!(
retained,
(
Some(super::ForwardedSourceError::Closed),
identity,
1,
vec![b"uncredited tail\n".to_vec()]
)
);
assert!(written.lock().unwrap().is_empty());
assert_eq!(
super::FORWARDED_GENERATION.load(Ordering::SeqCst),
generation
);
}
}