Unverified Commit 662fa5a2 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

turbo-persistence: bounds-check SST block reads

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 4872a879
......@@ -286,7 +286,9 @@ is used (checksum verification and decompression). On Unix, the lookup mapping t
- A best-effort `MADV_WILLNEED` for a block that spans more than one page, before any of its bytes is
read: on its first full read in this open, and on every read of a medium value block (those bypass
the block cache). Without the hint, `MADV_RANDOM` would fault such a block in one page at a time.
Blocks within a single page, and blocks whose checksum was already verified, get no hint.
Blocks within a single page, and blocks whose checksum was already verified, get no hint. The hint
is issued only after the block's range has passed the bounds check, so a corrupt block offset
never causes a hint outside the data section.
If the system page size can't be determined, block hints are skipped, so `MADV_RANDOM` isn't applied
either and the mapping keeps the default advice.
......
......@@ -14,7 +14,7 @@ use std::{
path::{Path, PathBuf},
};
use anyhow::{Context, Result, bail};
use anyhow::{Context, Result, bail, ensure};
use byteorder::{BE, ReadBytesExt};
use fs_err::{self as fs, File};
use lz4_flex::block::decompress_into;
......@@ -306,28 +306,29 @@ struct RawBlock {
}
/// Reads, checksums, and decompresses a single block from the mmap.
///
/// The block's range is validated with the same check the database's readers use, so a corrupt
/// block directory is reported as an error instead of an out-of-bounds slice.
fn read_block(
mmap: &Mmap,
block_offsets_start: usize,
block_index: u16,
sequence_number: u32,
meta: &StaticSortedFileMetaData,
compression: Compression,
) -> Result<RawBlock> {
let offset = block_offsets_start + block_index as usize * size_of::<u32>();
let block_start = if block_index == 0 {
0
} else {
(&mmap[offset - size_of::<u32>()..offset]).read_u32::<BE>()? as usize
};
let block_end = (&mmap[offset..offset + size_of::<u32>()]).read_u32::<BE>()? as usize;
let sequence_number = meta.sequence_number;
let range = meta.block_range(block_index, block_offsets_start, |i| {
let offset = block_offsets_start + i * size_of::<u32>();
u32::from_be_bytes(mmap[offset..offset + size_of::<u32>()].try_into().unwrap())
})?;
let block_start = range.start;
let uncompressed_length =
(&mmap[block_start..block_start + size_of::<u32>()]).read_u32::<BE>()?;
let expected_checksum = (&mmap
[block_start + size_of::<u32>()..block_start + BLOCK_HEADER_SIZE])
.read_u32::<BE>()?;
let compressed_data = &mmap[block_start + BLOCK_HEADER_SIZE..block_end];
let compressed_data = &mmap[block_start + BLOCK_HEADER_SIZE..range.end];
let compressed_size = compressed_data.len() as u64;
let was_compressed = uncompressed_length > 0;
......@@ -358,9 +359,13 @@ fn read_block(
Compression::Zstd3 => zstd::bulk::decompress_to_buffer(compressed_data, &mut buffer)
.context("zstd decompression failed")?,
};
assert_eq!(
bytes_written, uncompressed_length as usize,
"Decompressed length does not match expected"
ensure!(
bytes_written == uncompressed_length as usize,
"Decompressed length {} of block {} in {:08}.sst does not match expected {}",
bytes_written,
block_index,
sequence_number,
uncompressed_length
);
buffer.into_boxed_slice()
} else {
......@@ -498,11 +503,11 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> {
..Default::default()
};
let block_offsets_start = StaticSortedFileMetaData {
let meta = StaticSortedFileMetaData {
sequence_number: info.sequence_number,
block_count: info.block_count,
}
.checked_block_offsets_start(mmap.len())?;
};
let block_offsets_start = meta.checked_block_offsets_start(mmap.len())?;
// Read the index block (always the last block) first to learn which blocks are key blocks.
// Without this, we'd have to guess block types from their first byte, which is wrong for
......@@ -512,7 +517,7 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> {
&mmap,
block_offsets_start,
index_block_index,
info.sequence_number,
&meta,
compression,
)?;
let key_block_indices = parse_key_block_indices(&index_raw.data);
......@@ -525,13 +530,7 @@ fn analyze_sst_file(db_path: &Path, info: &SstInfo) -> Result<SstStats> {
// Now iterate through all blocks, using the key block set for classification.
for block_index in 0..index_block_index {
let raw = match read_block(
&mmap,
block_offsets_start,
block_index,
info.sequence_number,
compression,
) {
let raw = match read_block(&mmap, block_offsets_start, block_index, &meta, compression) {
Ok(raw) => raw,
Err(e) => {
eprintln!(
......@@ -986,3 +985,105 @@ fn main() -> Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
/// Writes `bytes` as an SST file, memory-maps it and returns the mapping and its metadata.
/// The temporary directory is returned so the file outlives the mapping.
fn map_sst(
bytes: &[u8],
block_count: u16,
) -> (tempfile::TempDir, Mmap, StaticSortedFileMetaData) {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("00000001.sst");
std::fs::write(&path, bytes).unwrap();
let file = File::open(&path).unwrap();
let mmap = unsafe { Mmap::map(file.file()).unwrap() };
let meta = StaticSortedFileMetaData {
sequence_number: 1,
block_count,
};
(dir, mmap, meta)
}
/// Builds an SST from raw block bytes (header included) followed by their end offsets.
fn sst_bytes(blocks: &[Vec<u8>]) -> Vec<u8> {
let mut bytes = blocks.concat();
let mut end = 0;
for block in blocks {
end += block.len() as u32;
bytes.extend_from_slice(&end.to_be_bytes());
}
bytes
}
fn uncompressed_block(data: &[u8]) -> Vec<u8> {
let mut block = 0_u32.to_be_bytes().to_vec();
block.extend_from_slice(&checksum_block(data).to_be_bytes());
block.extend_from_slice(data);
block
}
fn read_err(bytes: &[u8], block_count: u16, block_index: u16) -> String {
let (_dir, mmap, meta) = map_sst(bytes, block_count);
let block_offsets_start = meta.checked_block_offsets_start(mmap.len()).unwrap();
let Err(err) = read_block(
&mmap,
block_offsets_start,
block_index,
&meta,
Compression::Lz4,
) else {
panic!("read_block accepted block {block_index}");
};
format!("{err:#}")
}
#[test]
fn read_block_reads_valid_blocks() {
let bytes = sst_bytes(&[uncompressed_block(b"value"), uncompressed_block(b"\0index")]);
let (_dir, mmap, meta) = map_sst(&bytes, 2);
let block_offsets_start = meta.checked_block_offsets_start(mmap.len()).unwrap();
let raw = read_block(&mmap, block_offsets_start, 0, &meta, Compression::Lz4).unwrap();
assert_eq!(&*raw.data, b"value");
}
#[test]
fn read_block_rejects_out_of_range_block_index() {
let bytes = sst_bytes(&[uncompressed_block(b"index")]);
let message = read_err(&bytes, 1, 1);
assert!(message.contains("out of range"), "{message}");
}
#[test]
fn read_block_rejects_corrupt_value_block_offset() {
let mut bytes = sst_bytes(&[uncompressed_block(b"value"), uncompressed_block(b"index")]);
let directory_start = bytes.len() - 2 * size_of::<u32>();
bytes[directory_start..directory_start + 4].copy_from_slice(&u32::MAX.to_be_bytes());
let message = read_err(&bytes, 2, 0);
assert!(message.contains("not a valid block"), "{message}");
}
#[test]
fn read_block_rejects_corrupt_index_block_offset() {
let mut bytes = sst_bytes(&[uncompressed_block(b"value"), uncompressed_block(b"index")]);
let last_entry = bytes.len() - size_of::<u32>();
bytes[last_entry..].copy_from_slice(&(last_entry as u32 + 1).to_be_bytes());
let message = read_err(&bytes, 2, 1);
assert!(message.contains("not a valid block"), "{message}");
}
#[test]
fn read_block_rejects_decompressed_length_mismatch() {
let data = b"compressible compressible compressible";
let compressed = lz4_flex::block::compress(data);
// Claim one more byte than the compressed data decodes to.
let mut block = (data.len() as u32 + 1).to_be_bytes().to_vec();
block.extend_from_slice(&checksum_block(&compressed).to_be_bytes());
block.extend_from_slice(&compressed);
let message = read_err(&sst_bytes(&[block]), 1, 0);
assert!(message.contains("Decompressed length"), "{message}");
}
}
......@@ -1480,10 +1480,12 @@ impl<W: Write> IndexBlockBuilder<W> {
mod tests {
use super::*;
use crate::{
AccessMode,
key::hash_key,
lookup_entry::LookupValue,
static_sorted_file::{
BlockCache, SstLookupResult, StaticSortedFile, StaticSortedFileMetaData,
BlockCache, SstLookupResult, StaticSortedFile, StaticSortedFileIter,
StaticSortedFileMetaData,
},
};
......@@ -1937,6 +1939,51 @@ mod tests {
Ok(())
}
/// A corrupt block offset is rejected by the shared bounds check before `MADV_WILLNEED`, so
/// no hint ever covers bytes outside the data section.
#[cfg(all(feature = "mmap", unix))]
#[test]
fn will_need_skips_corrupt_block_ranges() -> Result<()> {
use crate::static_sorted_file::will_need_log;
let dir = tempfile::tempdir()?;
let entries = will_need_entries();
let meta = write_sst(dir.path(), 1, &entries, MetaEntryFlags::default())?;
let path = dir.path().join("00000001.sst");
let original = std::fs::read(&path)?;
let table_start = original.len() - meta.block_count as usize * 4;
// Every block below the last data block: the last data block's end is the start of the
// index block, which is validated when the file is opened.
for block_index in 0..meta.block_count as usize - 2 {
let mut bytes = original.clone();
let entry = table_start + block_index * 4;
// Inside the mapping, but past the end of the data section.
bytes[entry..entry + 4].copy_from_slice(&(table_start as u32 + 4).to_be_bytes());
std::fs::write(&path, &bytes)?;
let sst = open_sst(dir.path(), 1, &meta)?;
will_need_log::take();
let kc = make_cache();
let vc = make_cache();
let errors = entries
.iter()
.filter_map(|e| sst.lookup::<_, false>(e.hash, &e.key, &kc, &vc).err())
.map(|err| format!("{err:#}"))
.collect::<Vec<_>>();
assert!(!errors.is_empty(), "block {block_index}: lookups succeeded");
assert!(
errors.iter().all(|err| err.contains("not a valid block")),
"block {block_index}: {errors:?}"
);
let hinted = will_need_log::take();
assert!(
hinted.iter().all(|range| range.end <= table_start),
"block {block_index}: hinted outside the data section: {hinted:?}"
);
}
Ok(())
}
#[test]
fn many_medium_values() -> Result<()> {
let dir = tempfile::tempdir()?;
......@@ -2434,6 +2481,109 @@ mod tests {
assert_corruption_detected(dir.path(), 1, &meta, &entries);
}
/// Reads every entry through both readers in every access mode and collects all errors.
/// A corrupt block offset must surface here as an error, not as a panic.
fn read_all_entries(
dir: &Path,
meta: StaticSortedFileMetaData,
entries: &[TestEntry],
) -> Vec<anyhow::Error> {
let mut errors = Vec::new();
let access_modes: &[AccessMode] = &[
#[cfg(feature = "mmap")]
AccessMode::Mmap,
AccessMode::File,
];
for &access_mode in access_modes {
match StaticSortedFile::open(dir, meta, Compression::Lz4, access_mode) {
Ok(sst) => {
let kc = make_cache();
let vc = make_cache();
// Look up every entry rather than stopping at the first error, so that each
// block, and with it the corrupt one, is read.
let lookup_errors = entries
.iter()
.filter_map(|e| sst.lookup::<_, false>(e.hash, &e.key, &kc, &vc).err())
.collect::<Vec<_>>();
assert!(
!lookup_errors.is_empty(),
"lookups ({access_mode:?}) succeeded despite a corrupt block offset"
);
errors.extend(lookup_errors);
}
Err(err) => errors.push(err),
}
match StaticSortedFileIter::open(dir, meta, Compression::Lz4, access_mode) {
Ok(iter) => {
if let Some(err) = iter.filter_map(|r| r.err()).next() {
errors.push(err);
} else {
panic!(
"iteration ({access_mode:?}) succeeded despite a corrupt block offset"
);
}
}
Err(err) => errors.push(err),
}
}
errors
}
/// Overwriting the directory entry of any non-index block with an offset outside the data
/// section must make reads fail with the shared block-range error, with or without
/// `strict_checks`.
#[test]
fn corrupt_block_offsets_are_rejected_on_read() -> Result<()> {
let mut entries = vec![TestEntry::medium(b"medium", &vec![0xAB; 8192])];
for i in 0..64u32 {
entries.push(TestEntry::small(
format!("small-{i:04}").as_bytes(),
&[i as u8; 64],
));
}
sort_entries(&mut entries);
let dir = tempfile::tempdir()?;
let written = write_sst(dir.path(), 1, &entries, MetaEntryFlags::default())?;
let meta = StaticSortedFileMetaData {
sequence_number: 1,
block_count: written.block_count,
};
assert!(
meta.block_count >= 3,
"expected value, key and index blocks, got {} blocks",
meta.block_count
);
let path = dir.path().join("00000001.sst");
let original = std::fs::read(&path)?;
let directory_start = meta.checked_block_offsets_start(original.len())?;
// Sanity check: the uncorrupted file reads back with the expected values.
let sst = open_sst(dir.path(), 1, &written)?;
let kc = make_cache();
let vc = make_cache();
for entry in &entries {
assert_lookup(&sst, entry, &kc, &vc)?;
}
drop(sst);
for block_index in 0..usize::from(meta.block_count) - 1 {
for bad_end in [u32::MAX, directory_start as u32 + 4] {
let mut bytes = original.clone();
let entry = directory_start + block_index * 4;
bytes[entry..entry + 4].copy_from_slice(&bad_end.to_be_bytes());
std::fs::write(&path, &bytes)?;
for err in read_all_entries(dir.path(), meta, &entries) {
let message = format!("{err:#}");
assert!(
message.contains("not a valid block") && message.contains("00000001.sst"),
"block {block_index} end {bad_end}: unexpected error: {message}"
);
}
}
}
Ok(())
}
#[test]
fn be_key_order_matches_byte_order() {
let keys4: Vec<[u8; 4]> = vec![
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment