Skip to content

Commit f166517

Browse files
Copilotdoublegate
andcommitted
fix: resolve rkyv alignment issues with aligned buffer copy
Co-authored-by: doublegate <6858123+doublegate@users.noreply.github.com>
1 parent 79ebaa0 commit f166517

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

crates/prtip-scanner/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pcap-file = "2.0"
8787

8888
# Memory-mapped I/O
8989
memmap2 = { workspace = true }
90-
rkyv = { workspace = true }
90+
rkyv = { workspace = true, features = ["alloc"] }
9191

9292
[dev-dependencies]
9393
tokio = { workspace = true }

crates/prtip-scanner/src/output/mmap_reader.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::Path;
88

99
const HEADER_SIZE: usize = 64;
1010
const ENTRY_SIZE: usize = 512;
11+
const LENGTH_PREFIX_SIZE: usize = 8; // 8 bytes for length to maintain alignment
1112

1213
/// Memory-mapped result reader
1314
pub struct MmapResultReader {
@@ -75,22 +76,21 @@ impl MmapResultReader {
7576
}
7677

7778
let offset = HEADER_SIZE + (index * self.entry_size);
78-
let entry_bytes = &self.mmap[offset..offset + self.entry_size];
79-
80-
// Find the end of actual data (before zero-padding)
81-
let data_end = entry_bytes.iter()
82-
.rposition(|&b| b != 0)
83-
.map(|pos| pos + 1)
84-
.unwrap_or(0);
8579

86-
if data_end == 0 {
80+
// Read length prefix (8 bytes)
81+
let len_bytes: [u8; 8] = self.mmap[offset..offset + LENGTH_PREFIX_SIZE].try_into().ok()?;
82+
let len = u64::from_le_bytes(len_bytes) as usize;
83+
84+
if len == 0 || len + LENGTH_PREFIX_SIZE > self.entry_size {
8785
return None;
8886
}
8987

90-
let actual_bytes = &entry_bytes[..data_end];
88+
// Copy data to an aligned buffer (rkyv requires alignment)
89+
let entry_bytes = &self.mmap[offset + LENGTH_PREFIX_SIZE..offset + LENGTH_PREFIX_SIZE + len];
90+
let aligned_data: Vec<u8> = entry_bytes.to_vec();
9191

9292
// Deserialize using rkyv
93-
match rkyv::from_bytes::<ScanResultRkyv, rkyv::rancor::Error>(actual_bytes) {
93+
match rkyv::from_bytes::<ScanResultRkyv, rkyv::rancor::Error>(&aligned_data) {
9494
Ok(rkyv_result) => Some(rkyv_result.into()),
9595
Err(_) => None,
9696
}

crates/prtip-scanner/src/output/mmap_writer.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::path::Path;
1212

1313
const HEADER_SIZE: usize = 64; // Version, entry_count, entry_size, checksum
1414
const ENTRY_SIZE: usize = 512; // Fixed-size entries (padded if needed)
15+
const LENGTH_PREFIX_SIZE: usize = 8; // 8 bytes for length to maintain alignment
1516

1617
/// Memory-mapped result writer
1718
pub struct MmapResultWriter {
@@ -62,22 +63,28 @@ impl MmapResultWriter {
6263
let entry_bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&rkyv_result)
6364
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))?;
6465

65-
if entry_bytes.len() > ENTRY_SIZE {
66+
// Check if data + length prefix fits
67+
if entry_bytes.len() + LENGTH_PREFIX_SIZE > ENTRY_SIZE {
6668
return Err(io::Error::new(
6769
io::ErrorKind::InvalidData,
6870
format!(
69-
"Entry size {} exceeds maximum {}",
71+
"Entry size {} (+{} length) exceeds maximum {}",
7072
entry_bytes.len(),
73+
LENGTH_PREFIX_SIZE,
7174
ENTRY_SIZE
7275
),
7376
));
7477
}
7578

76-
// Write serialized data
77-
self.mmap[offset..offset + entry_bytes.len()].copy_from_slice(&entry_bytes);
79+
// Write length prefix (8 bytes for alignment)
80+
let len = entry_bytes.len() as u64;
81+
self.mmap[offset..offset + LENGTH_PREFIX_SIZE].copy_from_slice(&len.to_le_bytes());
82+
83+
// Write serialized data after length prefix
84+
self.mmap[offset + LENGTH_PREFIX_SIZE..offset + LENGTH_PREFIX_SIZE + entry_bytes.len()].copy_from_slice(&entry_bytes);
7885

7986
// Zero-fill remaining space
80-
for i in entry_bytes.len()..ENTRY_SIZE {
87+
for i in (LENGTH_PREFIX_SIZE + entry_bytes.len())..ENTRY_SIZE {
8188
self.mmap[offset + i] = 0;
8289
}
8390

0 commit comments

Comments
 (0)