fix large key read; and cursor positions in read() and write()

1. the large key read algorithm was broken. this is now fixed. we only
saw this triggered when the large key was much larger than 8kiB (and
the original CI tests just use 8kiB keys for "large").

2. the cursor position in read() and write() were being incremented
by the expected read amount, not the actual read amount.
This commit is contained in:
bunnie 2022-07-03 00:35:11 +08:00
parent 587f7ef39c
commit b10cf878db
3 changed files with 34 additions and 44 deletions

View file

@ -229,6 +229,7 @@ pub(crate) enum PddbRetcode {
DiskFull = 6,
}
pub(crate) const PDDB_BUF_DATA_LEN: usize = 4072;
/// PddbBuf is a C-representation of a page of memory that's used
/// to shuttle data for streaming channels. It must be exactly one
/// page in size, with some overhead specific to the PDDB book-keeping
@ -244,7 +245,7 @@ pub(crate) struct PddbBuf {
pub(crate) len: u16,
/// point in the key stream. 64-bit for future-compatibility; but, can't be larger than 32 bits on a 32-bit target.
pub(crate) position: u64,
pub(crate) data: [u8; 4072],
pub(crate) data: [u8; PDDB_BUF_DATA_LEN],
}
#[allow(dead_code)]

View file

@ -466,67 +466,56 @@ impl BasisCache {
return Ok(0)
}
// large pool fetch
let mut cur_offset = offset.unwrap_or(0) as u64;
let mut abs_cursor = offset.unwrap_or(0) as u64;
let mut blocks_read = 0;
let mut bytes_read = 0;
loop {
let start_vpage_addr = ((kcache.start + cur_offset) / VPAGE_SIZE as u64) * VPAGE_SIZE as u64;
// handle the case of terminating the last block read nicely:
// we can either end because we hit the end of the key data (so the data readback buffer is too large)
let key_endstop = if (kcache.len - cur_offset) < VPAGE_SIZE as u64 {
(kcache.len - cur_offset) as usize + size_of::<JournalType>()
} else {
VPAGE_SIZE + size_of::<JournalType>()
};
// or we can hit the end because we ran out of datareadback buffer
let data_endstop = if (data.len() as u64 - cur_offset) < VPAGE_SIZE as u64 {
(data.len() as u64 - cur_offset) as usize + size_of::<JournalType>()
} else {
VPAGE_SIZE + size_of::<JournalType>()
};
// of the two possible end criteria, pick the nearest one
let endstop = if key_endstop < data_endstop {
key_endstop
} else {
data_endstop
};
log::debug!("reading offset {}->{}/{}:{}", key, cur_offset, endstop, kcache.len);
let mut read_offset = (cur_offset % VPAGE_SIZE as u64) as usize;
let start_vpage_addr = ((kcache.start + abs_cursor) / VPAGE_SIZE as u64) * VPAGE_SIZE as u64;
if let Some(pp) = basis.v2p_map.get(&VirtAddr::new(start_vpage_addr).unwrap()) {
let block_start_pos = (abs_cursor % VPAGE_SIZE as u64) as usize;
assert!(pp.valid(), "v2p returned an invalid page");
let pt_data = hw.data_decrypt_page(&basis.cipher, &basis.aad, pp).expect("Decryption auth error");
if blocks_read != 0 {
assert!(read_offset == 0, "algorithm error in handling offset data");
assert!(block_start_pos == 0, "algorithm error in handling offset data");
}
if blocks_read == 0 {
log::debug!("reading {} abs: {}, block_start: {}, block: {}, data.len:{} kcache.len:{}",
key, abs_cursor, block_start_pos, blocks_read, data.len(), kcache.len);
} else {
log::debug!(" reading {} abs: {}, block_start: {}, block: {}, remaining.data:{} kcache.len:{}",
key, abs_cursor, block_start_pos, blocks_read, data.len() - bytes_read, kcache.len);
}
let data_offset = bytes_read;
for (&src, dst) in
pt_data[
size_of::<JournalType>() // always this fixed offset per block
+ read_offset // this should be 0 after the first block
..endstop
].iter().zip(data[cur_offset as usize..].iter_mut()) {
+ block_start_pos
..
].iter().zip(data[data_offset..].iter_mut()) {
*dst = src;
cur_offset += 1;
read_offset += 1;
// it'd be computationally more efficient to figure out what this should be going into
// every copy loop, but it's logically easier to think about in this form. Without this
// check, a user could read past the allocated space for a block...
if abs_cursor >= kcache.len {
break;
}
abs_cursor += 1;
bytes_read += 1;
}
blocks_read += 1;
} else {
log::warn!("Not enough bytes available to read for key {}:{} ({}/{})", dict, key, cur_offset, data.len());
return Ok(cur_offset as usize)
log::warn!("Not enough bytes available to read for key {}:{} ({}/{})", dict, key, abs_cursor, data.len());
return Ok(bytes_read as usize)
}
// cur_offset won't hit (start + len) if we're hitting an endstop, so the loop won't exit.
// if we hit an endstop, we should exit the read loop.
if endstop != VPAGE_SIZE + size_of::<JournalType>() {
if abs_cursor >= kcache.len {
break;
}
// break if we've hit or exceeded our endstop
if read_offset >= endstop {
break;
}
// also exit if we're "just nice", e.g. the size of the data happened to be exactly a multiple of the length of our block size
if cur_offset == kcache.len {
if bytes_read >= data.len() {
break;
}
}
return Ok(cur_offset as usize)
return Ok(bytes_read as usize)
}
} else {

View file

@ -108,7 +108,7 @@ impl<'a> Read for PddbKey<'a> {
*dst = src;
}
assert!(pbuf.len <= readlen, "More data returned than we requested");
self.pos += buf.len() as u64;
self.pos += pbuf.len as u64;
Ok(pbuf.len as usize)
}
PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")),
@ -157,7 +157,7 @@ impl<'a> Write for PddbKey<'a> {
match pbuf.retcode {
PddbRetcode::Ok => {
assert!(pbuf.len <= writelen, "More data written than we requested");
self.pos += writelen as u64;
self.pos += pbuf.len as u64;
Ok(pbuf.len as usize)
}
PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")),