use core::{
cell::Cell,
mem::{offset_of, size_of},
ptr::{self, NonNull},
sync::atomic::{Ordering, fence},
};
use std::io;
use libdd_trace_protobuf::opentelemetry::proto::common::v1::{
AnyValue, KeyValue, ProcessContext, any_value,
};
use prost::Message;
use super::{MappingHeaderSnapshot, PROCESS_CTX_VERSION, SIGNATURE, UNPUBLISHED_OR_UPDATING};
#[cfg(target_os = "linux")]
mod copy_pipe_unix;
#[cfg(target_os = "linux")]
pub(super) mod linux;
#[cfg(target_os = "linux")]
use copy_pipe_unix::CopyPipe as PlatformCopyPipe;
#[cfg(target_os = "linux")]
type PlatformHeaderDiscovery = linux::HeaderDiscovery;
pub(super) trait ReaderPlatform {
fn discover_header() -> io::Result<NonNull<u8>>;
}
pub(super) trait ProcessMemoryCopy: Sized + Send {
fn new() -> io::Result<Self>;
fn copy(self, addr: *const u8, len: usize) -> (io::Result<Vec<u8>>, Option<Self>);
}
pub struct ProcessContextSelfReader {
pid: u32,
pub(in crate::otel_process_ctx) header_ptr: NonNull<u8>,
pipe: Cell<Option<PlatformCopyPipe>>,
}
unsafe impl Send for ProcessContextSelfReader {}
impl ProcessContextSelfReader {
pub fn new() -> io::Result<Self> {
Ok(Self {
pid: std::process::id(),
header_ptr: <PlatformHeaderDiscovery as ReaderPlatform>::discover_header()?,
pipe: Cell::new(None),
})
}
pub fn read(&self) -> io::Result<ProcessContext> {
if self.pid != std::process::id() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"process context reader is stale after fork; construct a new reader",
));
}
let published_at = self.read_published_at()?;
if published_at == UNPUBLISHED_OR_UPDATING {
return Err(io::Error::new(
io::ErrorKind::WouldBlock,
"process context is currently being updated",
));
}
fence(Ordering::SeqCst);
let header = self.read_header()?;
if header.signature != *SIGNATURE {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"invalid signature in process context header",
));
}
if header.version != PROCESS_CTX_VERSION {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("unsupported process context version {}", header.version),
));
}
if header.payload_ptr.is_null() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"process context payload pointer is null",
));
}
let payload = self.read_process_memory(header.payload_ptr, header.payload_size as usize)?;
fence(Ordering::SeqCst);
if self.read_published_at()? != published_at {
return Err(io::Error::new(
io::ErrorKind::WouldBlock,
"process context changed while being read",
));
}
ProcessContext::decode(payload.as_slice())
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
}
pub fn threadlocal_attribute_key_map(context: &ProcessContext) -> Option<Vec<String>> {
Self::find_attr(&context.extra_attributes, "threadlocal.attribute_key_map")
.and_then(Self::string_array)
}
fn string_array(value: &AnyValue) -> Option<Vec<String>> {
let any_value::Value::ArrayValue(array) = value.value.as_ref()? else {
return None;
};
array
.values
.iter()
.map(|value| match value.value.as_ref()? {
any_value::Value::StringValue(value) => Some(value.clone()),
_ => None,
})
.collect()
}
fn find_attr<'a>(attrs: &'a [KeyValue], key: &str) -> Option<&'a AnyValue> {
attrs
.iter()
.find(|attribute| attribute.key == key)
.and_then(|attribute| attribute.value.as_ref())
}
fn read_published_at(&self) -> io::Result<u64> {
let timestamp_ptr = self
.header_ptr
.as_ptr()
.wrapping_add(offset_of!(MappingHeaderSnapshot, monotonic_published_at_ns))
.cast_const();
let bytes = self.read_process_memory(timestamp_ptr, size_of::<u64>())?;
Ok(u64::from_ne_bytes(
Self::field_bytes::<{ size_of::<u64>() }>(&bytes, 0, "monotonic_published_at_ns")?,
))
}
fn read_header(&self) -> io::Result<MappingHeaderSnapshot> {
let bytes = self.read_process_memory(
self.header_ptr.as_ptr().cast_const(),
size_of::<MappingHeaderSnapshot>(),
)?;
Self::mapping_header_from_bytes(&bytes)
}
fn mapping_header_from_bytes(bytes: &[u8]) -> io::Result<MappingHeaderSnapshot> {
if bytes.len() != size_of::<MappingHeaderSnapshot>() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"process context header copy had size {}, expected {}",
bytes.len(),
size_of::<MappingHeaderSnapshot>()
),
));
}
let signature = Self::field_bytes::<8>(
bytes,
offset_of!(MappingHeaderSnapshot, signature),
"signature",
)?;
let version = u32::from_ne_bytes(Self::field_bytes::<{ size_of::<u32>() }>(
bytes,
offset_of!(MappingHeaderSnapshot, version),
"version",
)?);
let payload_size = u32::from_ne_bytes(Self::field_bytes::<{ size_of::<u32>() }>(
bytes,
offset_of!(MappingHeaderSnapshot, payload_size),
"payload_size",
)?);
let monotonic_published_at_ns =
u64::from_ne_bytes(Self::field_bytes::<{ size_of::<u64>() }>(
bytes,
offset_of!(MappingHeaderSnapshot, monotonic_published_at_ns),
"monotonic_published_at_ns",
)?);
let payload_addr = usize::from_ne_bytes(Self::field_bytes::<{ size_of::<usize>() }>(
bytes,
offset_of!(MappingHeaderSnapshot, payload_ptr),
"payload_ptr",
)?);
Ok(MappingHeaderSnapshot {
signature,
version,
payload_size,
monotonic_published_at_ns,
payload_ptr: ptr::with_exposed_provenance(payload_addr),
})
}
fn field_bytes<const N: usize>(
bytes: &[u8],
offset: usize,
field: &'static str,
) -> io::Result<[u8; N]> {
let end = offset.checked_add(N).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("process context header field {field} offset overflowed"),
)
})?;
let slice = bytes.get(offset..end).ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("process context header field {field} is out of bounds"),
)
})?;
let mut result = [0; N];
result.copy_from_slice(slice);
Ok(result)
}
fn read_process_memory(&self, addr: *const u8, len: usize) -> io::Result<Vec<u8>> {
if len == 0 {
return Ok(Vec::new());
}
let pipe = match self.pipe.take() {
Some(pipe) => pipe,
None => PlatformCopyPipe::new()?,
};
let (result, pipe) = pipe.copy(addr, len);
self.pipe.set(pipe);
result
}
}