use std::fmt;
use std::io;
use std::io::BufRead;
use std::io::Write;
use reverie_syscalls::Sysno;
use serde::Deserialize;
use serde::Serialize;
use crate::fd::DetInode;
use crate::fd::FdSlot;
use crate::fd::OpenFileId;
use crate::futex::FutexID;
use crate::futex::MmId;
use crate::pid::DetPid;
use crate::pid::DetTid;
pub const ACCESS_TRACE_FORMAT: &str = "hermit-access-trace";
pub const ACCESS_TRACE_VERSION: u32 = 2;
pub const ACCESS_TRACE_DEFAULT_MAX_BYTES: u64 = 1 << 30;
pub const OUT_BYTES_CAP: usize = 256;
pub type EventIndex = u64;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum TraceRecord {
Header(TraceHeader),
Syscall(SyscallRecord),
Signal(SignalRecord),
Edge(EdgeRecord),
WatchHit(WatchHitRecord),
WatchSummary(WatchSummaryRecord),
End(TraceEnd),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TraceHeader {
pub format: String,
pub version: u32,
pub from: EventIndex,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub untyped_descriptors: Option<Vec<String>>,
}
impl TraceHeader {
pub fn new(from: EventIndex) -> Self {
Self {
format: ACCESS_TRACE_FORMAT.to_string(),
version: ACCESS_TRACE_VERSION,
from,
untyped_descriptors: Some(Vec::new()),
}
}
pub fn with_untyped_descriptors(mut self, settings: Vec<String>) -> Self {
self.untyped_descriptors = Some(settings);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TraceEnd {
pub events: u64,
pub records: u64,
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize
)]
pub struct EventRef {
pub tid: DetTid,
pub event: EventIndex,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SyscallRecord {
pub pre: EventIndex,
pub post: Option<EventIndex>,
pub tid: DetTid,
pub pid: DetPid,
pub nth: u64,
#[serde(with = "sysno_name")]
pub sysno: Sysno,
pub args: [u64; 6],
pub result: Option<i64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub accesses: Vec<Access>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub returned: Option<ReturnedBytes>,
#[serde(default, skip_serializing_if = "Option::is_none", with = "hex_bytes")]
pub out_bytes: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReturnedBytes {
pub len: u64,
pub digest: u64,
pub truncated: bool,
}
pub const RETURNED_DIGEST_CAP: u64 = 64 * 1024;
pub fn returned_digest(state: u64, bytes: &[u8]) -> u64 {
bytes.iter().fold(state, |hash, &b| {
(hash ^ u64::from(b)).wrapping_mul(0x0000_0100_0000_01b3)
})
}
pub const RETURNED_DIGEST_SEED: u64 = 0xcbf2_9ce4_8422_2325;
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize
)]
pub struct ByteRange {
pub start: u64,
pub end: u64,
}
impl ByteRange {
pub fn overlaps(&self, other: &ByteRange) -> bool {
self.start < other.end && other.start < self.end
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "resource", rename_all = "snake_case")]
pub enum Access {
Fd {
slot: FdSlot,
open_file: Option<OpenFileId>,
op: FdOp,
},
OpenFile {
open_file: OpenFileId,
op: CursorOp,
},
FileContents {
inode: DetInode,
range: Option<ByteRange>,
op: DataOp,
},
Channel {
open_file: OpenFileId,
id: Option<ChannelId>,
channel: ChannelKind,
op: ChannelOp,
},
Futex {
futex: FutexID,
op: FutexOp,
},
Memory {
mm: MmId,
range: ByteRange,
op: MemoryOp,
},
Timer {
timer: TimerId,
op: TimerOp,
},
Lifetime {
target: Task,
op: LifetimeOp,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FdOp {
Use,
Open {
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
kind: Option<FileKind>,
},
Dup {
source: i32,
},
Close,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FileKind {
Regular,
Directory,
Fifo,
CharDevice,
BlockDevice,
Socket,
Symlink,
}
impl FileKind {
pub fn of_mode(mode: u32) -> Option<Self> {
Some(match mode & 0o170_000 {
0o100_000 => FileKind::Regular,
0o040_000 => FileKind::Directory,
0o010_000 => FileKind::Fifo,
0o020_000 => FileKind::CharDevice,
0o060_000 => FileKind::BlockDevice,
0o140_000 => FileKind::Socket,
0o120_000 => FileKind::Symlink,
_ => return None,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CursorOp {
Read,
Write,
Seek,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DataOp {
Read,
Write,
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize
)]
#[serde(rename_all = "snake_case")]
pub enum ChannelId {
Pair(OpenFileId),
Inode(DetInode),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChannelKind {
Pipe,
Fifo,
StreamSocket,
DatagramSocket,
SeqpacketSocket,
Socket,
Eventfd,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChannelOp {
Write,
Read,
Shutdown,
Close,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FutexOp {
Wait,
Wake,
RequeueTarget,
ModifyWake,
Lock,
Unlock,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MemoryOp {
Map,
Unmap,
Protect,
Advise,
Brk,
SyscallRead,
SyscallWrite,
}
impl MemoryOp {
pub fn writes_or_remaps(self) -> bool {
self != MemoryOp::SyscallRead
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TimerId {
Posix {
process: DetPid,
id: i32,
},
Interval {
process: DetPid,
which: i32,
},
Fd {
open_file: OpenFileId,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TimerOp {
Create,
Set,
Get,
Delete,
Read,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Task {
Thread(DetTid),
Process(DetPid),
ProcessGroup(DetPid),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LifetimeOp {
Create,
Exit,
Exec,
Signal {
signo: i32,
},
Wait,
}
impl Access {
pub fn conflicts_with(&self, other: &Access) -> bool {
use Access::*;
match (self, other) {
(Fd { slot: a, op: x, .. }, Fd { slot: b, op: y, .. }) => {
a == b && !(matches!(x, FdOp::Use) && matches!(y, FdOp::Use))
}
(OpenFile { open_file: a, .. }, OpenFile { open_file: b, .. }) => a == b,
(
FileContents {
inode: a,
range: ra,
op: x,
},
FileContents {
inode: b,
range: rb,
op: y,
},
) => {
a == b
&& (*x == DataOp::Write || *y == DataOp::Write)
&& match (ra, rb) {
(Some(ra), Some(rb)) => ra.overlaps(rb),
_ => true,
}
}
(
Channel {
open_file: fa,
id: ia,
..
},
Channel {
open_file: fb,
id: ib,
..
},
) => fa == fb || (ia.is_some() && ia == ib),
(Futex { futex: a, op: x }, Futex { futex: b, op: y }) => {
a == b && !(*x == FutexOp::Wait && *y == FutexOp::Wait)
}
(
Memory {
mm: a,
range: ra,
op: x,
},
Memory {
mm: b,
range: rb,
op: y,
},
) => a == b && ra.overlaps(rb) && (x.writes_or_remaps() || y.writes_or_remaps()),
(Timer { timer: a, op: x }, Timer { timer: b, op: y }) => {
a == b && !(*x == TimerOp::Get && *y == TimerOp::Get)
}
(Lifetime { target: a, op: x }, Lifetime { target: b, op: y }) => {
a == b && !(*x == LifetimeOp::Wait && *y == LifetimeOp::Wait)
}
_ => false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObservedSignalDisposition {
Handler,
Ignored,
Default,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignalSender {
pub pid: DetPid,
pub tid: Option<DetTid>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignalRecord {
pub event: EventIndex,
pub tid: DetTid,
pub signo: i32,
pub code: i32,
pub addr: Option<u64>,
pub sender: Option<SignalSender>,
pub observed_disposition: ObservedSignalDisposition,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EdgeKind {
FutexWake,
FutexRequeue,
Create,
ExitJoin,
ChildExit,
Signal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncObject {
Futex(FutexID),
Thread(DetTid),
Process(DetPid),
Signal(i32),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EdgeRecord {
#[serde(rename = "edge")]
pub kind: EdgeKind,
pub from: EventRef,
pub to: EventRef,
pub object: SyncObject,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StackFrame {
pub ip: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub function: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub line: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WatchHitRecord {
pub after: EventIndex,
pub tid: DetTid,
pub slot: u8,
pub addr: u64,
pub ip: u64,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub stack: Vec<StackFrame>,
}
pub const WATCH_HITS_WITH_STACKS: u64 = 64;
pub const WATCH_HITS_KEPT_LAST: u64 = 64;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WatchSummaryRecord {
pub tid: DetTid,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub armed_before: Option<EventIndex>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unarmed: Option<WatchUnarmed>,
pub hits: u64,
pub recorded: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WatchUnarmed {
SigtrapBlocked,
SigtrapIgnored,
NoStop,
}
impl TraceRecord {
pub fn write_line<W: Write>(&self, out: &mut W) -> io::Result<()> {
serde_json::to_writer(&mut *out, self).map_err(io::Error::from)?;
out.write_all(b"\n")
}
pub fn to_line(&self) -> io::Result<Vec<u8>> {
let mut line = Vec::new();
self.write_line(&mut line)?;
Ok(line)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AccessTrace {
pub header: TraceHeader,
pub records: Vec<TraceRecord>,
pub end: Option<TraceEnd>,
}
#[derive(Debug)]
pub enum AccessTraceError {
Io(io::Error),
Empty,
BadLine {
line: usize,
message: String,
},
MissingHeader,
Unsupported {
format: String,
version: u32,
},
Misplaced {
line: usize,
},
}
impl fmt::Display for AccessTraceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AccessTraceError::Io(e) => write!(f, "reading the access trace failed: {e}"),
AccessTraceError::Empty => write!(
f,
"the access trace is empty; rerun with --record-access-trace-to and let the run finish"
),
AccessTraceError::BadLine { line, message } => {
write!(f, "access trace line {line} is not a record: {message}")
}
AccessTraceError::MissingHeader => write!(
f,
"the access trace does not start with a header line; it was not written by --record-access-trace-to"
),
AccessTraceError::Unsupported { format, version } => write!(
f,
"the access trace is {format} version {version}; this build reads {ACCESS_TRACE_FORMAT} version {ACCESS_TRACE_VERSION}: record it again with this build"
),
AccessTraceError::Misplaced { line } => write!(
f,
"access trace line {line} is a header or end line in the middle of the file; the file was concatenated or corrupted"
),
}
}
}
impl std::error::Error for AccessTraceError {}
impl From<io::Error> for AccessTraceError {
fn from(e: io::Error) -> Self {
AccessTraceError::Io(e)
}
}
pub fn read_access_trace<R: BufRead>(input: R) -> Result<AccessTrace, AccessTraceError> {
let mut header = None;
let mut records = Vec::new();
let mut end = None;
for (index, line) in input.lines().enumerate() {
let number = index + 1;
let line = line?;
if line.is_empty() {
continue;
}
let record: TraceRecord =
serde_json::from_str(&line).map_err(|e| AccessTraceError::BadLine {
line: number,
message: e.to_string(),
})?;
if end.is_some() {
return Err(AccessTraceError::Misplaced { line: number });
}
match (record, header.is_some()) {
(TraceRecord::Header(h), false) => {
if h.format != ACCESS_TRACE_FORMAT || h.version != ACCESS_TRACE_VERSION {
return Err(AccessTraceError::Unsupported {
format: h.format,
version: h.version,
});
}
header = Some(h);
}
(_, false) => return Err(AccessTraceError::MissingHeader),
(TraceRecord::Header(_), true) => {
return Err(AccessTraceError::Misplaced { line: number });
}
(TraceRecord::End(e), true) => end = Some(e),
(record, true) => records.push(record),
}
}
Ok(AccessTrace {
header: header.ok_or(AccessTraceError::Empty)?,
records,
end,
})
}
mod sysno_name {
use std::str::FromStr;
use reverie_syscalls::Sysno;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serializer;
use serde::de::Error;
pub fn serialize<S: Serializer>(sysno: &Sysno, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(sysno.name())
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Sysno, D::Error> {
let name = String::deserialize(d)?;
Sysno::from_str(&name).map_err(|_| D::Error::custom(format!("unknown syscall {name:?}")))
}
}
mod hex_bytes {
use std::fmt::Write;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serializer;
use serde::de::Error;
pub fn serialize<S: Serializer>(bytes: &Option<Vec<u8>>, s: S) -> Result<S::Ok, S::Error> {
match bytes {
Some(bytes) => {
let mut text = String::with_capacity(bytes.len() * 2);
for b in bytes {
write!(text, "{b:02x}").expect("writing to a String cannot fail");
}
s.serialize_some(&text)
}
None => s.serialize_none(),
}
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Vec<u8>>, D::Error> {
let Some(text) = Option::<String>::deserialize(d)? else {
return Ok(None);
};
if text.len() % 2 != 0 {
return Err(D::Error::custom("odd-length hex string"));
}
if !text.is_ascii() {
return Err(D::Error::custom("invalid hex digit"));
}
(0..text.len())
.step_by(2)
.map(|i| {
u8::from_str_radix(&text[i..i + 2], 16)
.map_err(|_| D::Error::custom("invalid hex digit"))
})
.collect::<Result<Vec<u8>, _>>()
.map(Some)
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
use crate::fd::FilesId;
use crate::futex::SharedMemoryObjectId;
#[test]
fn an_open_note_reads_with_or_without_its_kind() {
let read = |json: &str| serde_json::from_str::<FdOp>(json).unwrap();
assert_eq!(
read(r#"{"open":{"path":"/tmp/a"}}"#),
FdOp::Open {
path: Some("/tmp/a".to_owned()),
kind: None
}
);
assert_eq!(
read(r#"{"open":{"path":"/dev/null","kind":"char_device","later":1}}"#),
FdOp::Open {
path: Some("/dev/null".to_owned()),
kind: Some(FileKind::CharDevice)
}
);
let written = FdOp::Open {
path: None,
kind: Some(FileKind::Fifo),
};
assert_eq!(
serde_json::to_string(&written).unwrap(),
r#"{"open":{"kind":"fifo"}}"#
);
for (mode, kind) in [
(0o100_644, Some(FileKind::Regular)),
(0o020_666, Some(FileKind::CharDevice)),
(0o010_600, Some(FileKind::Fifo)),
(0o140_777, Some(FileKind::Socket)),
(0o040_755, Some(FileKind::Directory)),
(0o000_644, None),
] {
assert_eq!(FileKind::of_mode(mode), kind, "{mode:o}");
}
}
fn tid(n: i32) -> DetTid {
DetTid::from_raw(n)
}
fn slot(fd: i32) -> FdSlot {
FdSlot {
files: FilesId::initial(tid(3)),
fd,
}
}
fn file(creator: i32, sequence: u64) -> OpenFileId {
OpenFileId::new(tid(creator), sequence)
}
fn mm() -> MmId {
MmId::initial(tid(3))
}
fn close_record() -> SyscallRecord {
SyscallRecord {
pre: 7587,
post: Some(7588),
tid: tid(9),
pid: DetPid::from_raw(3),
nth: 1412,
sysno: Sysno::close,
args: [6, 0, 0, 0, 0, 0],
result: Some(0),
accesses: vec![
Access::Fd {
slot: slot(6),
open_file: Some(file(7, 2)),
op: FdOp::Close,
},
Access::Channel {
open_file: file(7, 2),
id: Some(ChannelId::Pair(file(7, 1))),
channel: ChannelKind::Pipe,
op: ChannelOp::Close,
},
],
returned: None,
out_bytes: None,
}
}
fn every_record() -> Vec<TraceRecord> {
let futex = FutexID::private(mm(), 0x7f00_0000_1000);
let shared = FutexID::shared(
SharedMemoryObjectId::Anonymous {
origin: mm(),
sequence: 1,
},
64,
);
vec![
TraceRecord::Header(TraceHeader::new(100)),
TraceRecord::Syscall(close_record()),
TraceRecord::Syscall(SyscallRecord {
pre: 7589,
post: Some(7590),
tid: tid(7),
pid: DetPid::from_raw(3),
nth: 388,
sysno: Sysno::write,
args: [2, 0x1000, 5, 0, 0, 0],
result: Some(5),
accesses: vec![
Access::Fd {
slot: slot(2),
open_file: Some(file(3, 0)),
op: FdOp::Use,
},
Access::OpenFile {
open_file: file(3, 0),
op: CursorOp::Write,
},
Access::FileContents {
inode: DetInode::mint(12),
range: Some(ByteRange { start: 0, end: 5 }),
op: DataOp::Write,
},
Access::Memory {
mm: mm(),
range: ByteRange {
start: 0x1000,
end: 0x1005,
},
op: MemoryOp::SyscallRead,
},
Access::Channel {
open_file: file(3, 6),
id: Some(ChannelId::Inode(DetInode::mint(77))),
channel: ChannelKind::Fifo,
op: ChannelOp::Shutdown,
},
],
returned: None,
out_bytes: Some(b"oops\n".to_vec()),
}),
TraceRecord::Syscall(SyscallRecord {
pre: 7591,
post: Some(7592),
tid: tid(7),
pid: DetPid::from_raw(3),
nth: 389,
sysno: Sysno::openat,
args: [u64::MAX - 99, 0x2000, 0o2, 0, 0, 0],
result: Some(6),
accesses: vec![Access::Fd {
slot: slot(6),
open_file: Some(file(7, 3)),
op: FdOp::Open {
path: Some("/tmp/misc".to_string()),
kind: None,
},
}],
returned: Some(ReturnedBytes {
len: 4,
digest: returned_digest(RETURNED_DIGEST_SEED, b"data"),
truncated: false,
}),
out_bytes: None,
}),
TraceRecord::Syscall(SyscallRecord {
pre: 7593,
post: Some(7594),
tid: tid(7),
pid: DetPid::from_raw(3),
nth: 390,
sysno: Sysno::dup3,
args: [6, 5, 0, 0, 0, 0],
result: Some(5),
accesses: vec![
Access::Fd {
slot: slot(6),
open_file: Some(file(7, 3)),
op: FdOp::Use,
},
Access::Fd {
slot: slot(5),
open_file: Some(file(3, 1)),
op: FdOp::Close,
},
Access::Fd {
slot: slot(5),
open_file: Some(file(7, 3)),
op: FdOp::Dup { source: 6 },
},
],
returned: Some(ReturnedBytes {
len: 4,
digest: returned_digest(RETURNED_DIGEST_SEED, b"data"),
truncated: false,
}),
out_bytes: None,
}),
TraceRecord::Syscall(SyscallRecord {
pre: 8000,
post: Some(8100),
tid: tid(5),
pid: DetPid::from_raw(3),
nth: 12,
sysno: Sysno::futex,
args: [0x7f00_0000_1000, 0x80, 2, 0, 0, 0],
result: Some(-11),
accesses: vec![
Access::Futex {
futex,
op: FutexOp::Wait,
},
Access::Futex {
futex: shared,
op: FutexOp::RequeueTarget,
},
],
returned: Some(ReturnedBytes {
len: 4,
digest: returned_digest(RETURNED_DIGEST_SEED, b"data"),
truncated: false,
}),
out_bytes: None,
}),
TraceRecord::Syscall(SyscallRecord {
pre: 8200,
post: Some(8201),
tid: tid(5),
pid: DetPid::from_raw(3),
nth: 13,
sysno: Sysno::timer_settime,
args: [0, 0, 0x3000, 0, 0, 0],
result: Some(0),
accesses: vec![
Access::Timer {
timer: TimerId::Posix {
process: DetPid::from_raw(3),
id: 0,
},
op: TimerOp::Set,
},
Access::Timer {
timer: TimerId::Interval {
process: DetPid::from_raw(3),
which: 0,
},
op: TimerOp::Get,
},
Access::Timer {
timer: TimerId::Fd {
open_file: file(5, 4),
},
op: TimerOp::Read,
},
],
returned: Some(ReturnedBytes {
len: 4,
digest: returned_digest(RETURNED_DIGEST_SEED, b"data"),
truncated: false,
}),
out_bytes: None,
}),
TraceRecord::Syscall(SyscallRecord {
pre: 9000,
post: None,
tid: tid(3),
pid: DetPid::from_raw(3),
nth: 600,
sysno: Sysno::kill,
args: [4, 9, 0, 0, 0, 0],
result: None,
accesses: vec![
Access::Lifetime {
target: Task::Process(DetPid::from_raw(4)),
op: LifetimeOp::Signal { signo: 9 },
},
Access::Lifetime {
target: Task::Thread(tid(5)),
op: LifetimeOp::Exit,
},
Access::Lifetime {
target: Task::ProcessGroup(DetPid::from_raw(3)),
op: LifetimeOp::Wait,
},
],
returned: Some(ReturnedBytes {
len: 4,
digest: returned_digest(RETURNED_DIGEST_SEED, b"data"),
truncated: false,
}),
out_bytes: None,
}),
TraceRecord::Signal(SignalRecord {
event: 9001,
tid: tid(7),
signo: 11,
code: 1,
addr: Some(0x10),
sender: None,
observed_disposition: ObservedSignalDisposition::Default,
}),
TraceRecord::Signal(SignalRecord {
event: 9002,
tid: tid(4),
signo: 10,
code: 0,
addr: None,
sender: Some(SignalSender {
pid: DetPid::from_raw(3),
tid: Some(tid(3)),
}),
observed_disposition: ObservedSignalDisposition::Handler,
}),
TraceRecord::Edge(EdgeRecord {
kind: EdgeKind::FutexWake,
from: EventRef {
tid: tid(7),
event: 8050,
},
to: EventRef {
tid: tid(5),
event: 8100,
},
object: SyncObject::Futex(futex),
}),
TraceRecord::Edge(EdgeRecord {
kind: EdgeKind::Create,
from: EventRef {
tid: tid(3),
event: 10,
},
to: EventRef {
tid: tid(5),
event: 12,
},
object: SyncObject::Thread(tid(5)),
}),
TraceRecord::Edge(EdgeRecord {
kind: EdgeKind::ChildExit,
from: EventRef {
tid: tid(4),
event: 20,
},
to: EventRef {
tid: tid(3),
event: 22,
},
object: SyncObject::Process(DetPid::from_raw(4)),
}),
TraceRecord::Edge(EdgeRecord {
kind: EdgeKind::Signal,
from: EventRef {
tid: tid(3),
event: 30,
},
to: EventRef {
tid: tid(4),
event: 31,
},
object: SyncObject::Signal(10),
}),
TraceRecord::WatchHit(WatchHitRecord {
after: 9003,
tid: tid(5),
slot: 0,
addr: 0x7f00_0000_2000,
ip: 0x40_1000,
stack: vec![StackFrame {
ip: 0x40_1000,
function: Some("main".to_string()),
file: Some("racewrite.c".to_string()),
line: Some(12),
}],
}),
TraceRecord::WatchSummary(WatchSummaryRecord {
tid: tid(5),
armed_before: Some(9001),
unarmed: None,
hits: 200,
recorded: 128,
}),
TraceRecord::WatchSummary(WatchSummaryRecord {
tid: tid(6),
armed_before: None,
unarmed: Some(WatchUnarmed::SigtrapBlocked),
hits: 0,
recorded: 0,
}),
TraceRecord::End(TraceEnd {
events: 9004,
records: 16,
}),
]
}
#[test]
fn every_record_kind_round_trips_through_json_lines() {
let records = every_record();
let mut buf = Vec::new();
for record in &records {
record.write_line(&mut buf).unwrap();
}
let text = String::from_utf8(buf.clone()).unwrap();
assert_eq!(text.lines().count(), records.len());
for (line, record) in text.lines().zip(&records) {
let back: TraceRecord = serde_json::from_str(line).unwrap();
assert_eq!(&back, record);
}
let trace = read_access_trace(&buf[..]).unwrap();
assert_eq!(trace.header, TraceHeader::new(100));
assert_eq!(trace.records, records[1..records.len() - 1].to_vec());
assert_eq!(
trace.end,
Some(TraceEnd {
events: 9004,
records: 16
})
);
}
#[test]
fn on_disk_spelling_is_stable() {
let mut buf = Vec::new();
TraceRecord::Syscall(close_record())
.write_line(&mut buf)
.unwrap();
assert_eq!(
String::from_utf8(buf).unwrap(),
concat!(
r#"{"kind":"syscall","pre":7587,"post":7588,"tid":9,"pid":3,"nth":1412,"#,
r#""sysno":"close","args":[6,0,0,0,0,0],"result":0,"accesses":["#,
r#"{"resource":"fd","slot":{"files":{"creator":3,"generation":0},"fd":6},"#,
r#""open_file":{"creator":7,"sequence":2},"op":"close"},"#,
r#"{"resource":"channel","open_file":{"creator":7,"sequence":2},"#,
r#""id":{"pair":{"creator":7,"sequence":1}},"#,
r#""channel":"pipe","op":"close"}]}"#,
"\n"
)
);
}
#[test]
fn out_bytes_are_exact_for_non_utf8_output() {
let mut record = close_record();
record.out_bytes = Some(vec![0x00, 0xff, b'\n', 0x80]);
let line = serde_json::to_string(&TraceRecord::Syscall(record.clone())).unwrap();
assert!(line.contains(r#""out_bytes":"00ff0a80""#), "{line}");
let back: TraceRecord = serde_json::from_str(&line).unwrap();
assert_eq!(back, TraceRecord::Syscall(record));
assert!(serde_json::from_str::<TraceRecord>(&line.replace("00ff0a80", "0ff0a80")).is_err());
assert!(
serde_json::from_str::<TraceRecord>(&line.replace("00ff0a80", "zzff0a80")).is_err()
);
assert!(
serde_json::from_str::<TraceRecord>(
&line.replace(r#""sysno":"close""#, r#""sysno":"nosuchcall""#)
)
.is_err()
);
}
#[test]
fn reader_rejects_non_hex_output_without_panicking() {
let header = serde_json::to_string(&TraceRecord::Header(TraceHeader::new(0))).unwrap();
let input = |encoded: &str| {
let mut call = serde_json::to_value(TraceRecord::Syscall(close_record())).unwrap();
call["out_bytes"] = serde_json::json!(encoded);
format!("{header}\n{call}\n")
};
for (encoded, expected) in [
("", &[][..]),
("00ff0a80", &[0x00, 0xff, b'\n', 0x80][..]),
("aB09", &[0xab, 0x09][..]),
] {
let trace = read_access_trace(input(encoded).as_bytes()).unwrap();
let [TraceRecord::Syscall(call)] = trace.records.as_slice() else {
panic!("{encoded:?}: {:?}", trace.records);
};
assert_eq!(call.out_bytes.as_deref(), Some(expected), "{encoded:?}");
}
for encoded in ["€a", "a€", "💥", "é", "0", "zz"] {
let error = read_access_trace(input(encoded).as_bytes())
.expect_err("malformed output bytes must be refused");
assert!(
matches!(error, AccessTraceError::BadLine { line: 2, .. }),
"{encoded:?}: {error}"
);
assert!(error.to_string().contains("hex"), "{encoded:?}: {error}");
}
}
#[test]
fn returned_digest_is_fnv1a_over_the_buffers_in_order() {
assert_eq!(
returned_digest(RETURNED_DIGEST_SEED, b""),
RETURNED_DIGEST_SEED
);
assert_eq!(
returned_digest(RETURNED_DIGEST_SEED, b"a"),
0xaf63_dc4c_8601_ec8c
);
let split = returned_digest(returned_digest(RETURNED_DIGEST_SEED, b"he"), b"llo");
assert_eq!(split, returned_digest(RETURNED_DIGEST_SEED, b"hello"));
assert_ne!(split, returned_digest(RETURNED_DIGEST_SEED, b"hellp"));
}
#[test]
fn reader_refuses_files_it_cannot_trust() {
let header = serde_json::to_string(&TraceRecord::Header(TraceHeader::new(0))).unwrap();
let syscall = serde_json::to_string(&TraceRecord::Syscall(close_record())).unwrap();
let end = serde_json::to_string(&TraceRecord::End(TraceEnd {
events: 1,
records: 1,
}))
.unwrap();
assert!(matches!(
read_access_trace("".as_bytes()),
Err(AccessTraceError::Empty)
));
assert!(matches!(
read_access_trace(format!("{syscall}\n").as_bytes()),
Err(AccessTraceError::MissingHeader)
));
assert!(matches!(
read_access_trace(format!("{header}\n{header}\n").as_bytes()),
Err(AccessTraceError::Misplaced { line: 2 })
));
assert!(matches!(
read_access_trace(format!("{header}\n{end}\n{syscall}\n").as_bytes()),
Err(AccessTraceError::Misplaced { line: 3 })
));
assert!(matches!(
read_access_trace(format!("{header}\nnot json\n").as_bytes()),
Err(AccessTraceError::BadLine { line: 2, .. })
));
let future_version = ACCESS_TRACE_VERSION + 1;
let future = header.replace(
&format!("\"version\":{ACCESS_TRACE_VERSION}"),
&format!("\"version\":{future_version}"),
);
let err = read_access_trace(future.as_bytes()).unwrap_err();
assert!(matches!(
err,
AccessTraceError::Unsupported { version, .. } if version == future_version
));
assert!(err.to_string().contains("record it again"), "{err}");
let cut = read_access_trace(format!("{header}\n{syscall}\n").as_bytes()).unwrap();
assert_eq!(cut.end, None, "a trace without an end line was cut short");
assert_eq!(cut.records.len(), 1);
}
#[test]
fn reader_refuses_version_one_delivery_claims() {
let old = concat!(
r#"{"kind":"header","format":"hermit-access-trace","version":1,"from":0}"#,
"\n",
r#"{"kind":"signal","event":0,"tid":3,"signo":10,"code":0,"addr":null,"sender":null,"delivery":"handler"}"#,
"\n",
r#"{"kind":"end","events":1,"records":1}"#,
"\n",
);
let error = read_access_trace(old.as_bytes()).expect_err("version 1 must be refused");
assert!(
matches!(error, AccessTraceError::Unsupported { version: 1, .. }),
"{error}"
);
assert!(error.to_string().contains("record it again"), "{error}");
}
#[test]
fn conflicts_follow_the_per_resource_rules() {
let use6 = Access::Fd {
slot: slot(6),
open_file: Some(file(7, 3)),
op: FdOp::Use,
};
let close6 = Access::Fd {
slot: slot(6),
open_file: Some(file(7, 3)),
op: FdOp::Close,
};
let use6_other_table = Access::Fd {
slot: FdSlot {
files: FilesId::initial(tid(4)),
fd: 6,
},
open_file: None,
op: FdOp::Close,
};
assert!(!use6.conflicts_with(&use6), "two uses commute");
assert!(use6.conflicts_with(&close6), "close against use");
assert!(close6.conflicts_with(&close6), "double close");
assert!(!close6.conflicts_with(&use6_other_table), "other table");
let futex = FutexID::private(mm(), 0x1000);
let wait = Access::Futex {
futex,
op: FutexOp::Wait,
};
let wake = Access::Futex {
futex,
op: FutexOp::Wake,
};
assert!(!wait.conflicts_with(&wait));
assert!(wait.conflicts_with(&wake), "lost wake");
let read = |start, end| Access::FileContents {
inode: DetInode::mint(1),
range: Some(ByteRange { start, end }),
op: DataOp::Read,
};
let write = |range| Access::FileContents {
inode: DetInode::mint(1),
range,
op: DataOp::Write,
};
assert!(!read(0, 10).conflicts_with(&read(0, 10)));
assert!(read(0, 10).conflicts_with(&write(Some(ByteRange { start: 9, end: 12 }))));
assert!(!read(0, 10).conflicts_with(&write(Some(ByteRange { start: 10, end: 12 }))));
assert!(read(0, 10).conflicts_with(&write(None)), "unknown range");
let unmap = Access::Memory {
mm: mm(),
range: ByteRange {
start: 0x1000,
end: 0x3000,
},
op: MemoryOp::Unmap,
};
let buffer = |op| Access::Memory {
mm: mm(),
range: ByteRange {
start: 0x2ff0,
end: 0x3010,
},
op,
};
assert!(unmap.conflicts_with(&buffer(MemoryOp::SyscallRead)));
assert!(buffer(MemoryOp::SyscallRead).conflicts_with(&buffer(MemoryOp::SyscallWrite)));
assert!(!buffer(MemoryOp::SyscallRead).conflicts_with(&buffer(MemoryOp::SyscallRead)));
let kill = Access::Lifetime {
target: Task::Process(DetPid::from_raw(4)),
op: LifetimeOp::Signal { signo: 9 },
};
let exit = Access::Lifetime {
target: Task::Process(DetPid::from_raw(4)),
op: LifetimeOp::Exit,
};
assert!(kill.conflicts_with(&exit));
assert!(
!kill.conflicts_with(&wake),
"different resources never conflict"
);
let end = |open_file, id, op| Access::Channel {
open_file,
id,
channel: ChannelKind::StreamSocket,
op,
};
let pair = Some(ChannelId::Pair(file(3, 1)));
assert!(
end(file(3, 1), pair, ChannelOp::Write).conflicts_with(&end(
file(3, 2),
pair,
ChannelOp::Read
)),
"both ends of one socketpair"
);
assert!(
!end(file(3, 1), None, ChannelOp::Write).conflicts_with(&end(
file(3, 2),
None,
ChannelOp::Read
)),
"unrelated sockets"
);
let fifo = Some(ChannelId::Inode(DetInode::mint(9)));
assert!(
end(file(3, 5), fifo, ChannelOp::Close).conflicts_with(&end(
file(4, 1),
fifo,
ChannelOp::Read
)),
"two opens of one FIFO"
);
}
}