Reject changed nested file sizes before checkpoint replay
This commit is contained in:
1 parent
ffd84cd15b
commit
061e484001
4 files changed
+101
-30
No files matched your search
@@ -616,8 +616,8 @@ impl PublishedCheckpoints {
|
||||
)?;
|
||||
let selected = selected.expect("resume preparation chose a verified checkpoint");
|
||||
let guard = selected.guard();
|
||||
let partial_source = guard.nested_source.as_deref()
|
||||
.and_then(|path| project.file(path)).map(|file| &file.version);
|
||||
let partial_source = guard.nested_source.as_ref()
|
||||
.and_then(|source| project.file(&source.path)).map(|file| &file.version);
|
||||
for (index, call) in selected.calls().iter().enumerate() {
|
||||
check(cancellation)?;
|
||||
if let Err(error) = call.replay(&mut prepared.files, partial_source) {
|
||||
@@ -692,9 +692,9 @@ impl CachedCheckpoint {
|
||||
{
|
||||
return Err("checkpoint entry prefix changed".into());
|
||||
}
|
||||
if let Some(path) = &self.guard.nested_source {
|
||||
let child = project.file(path).ok_or("checkpoint child is unavailable")?;
|
||||
if !self.entry.accepts_nested_source(&path.to_string_lossy(), &child.data) {
|
||||
if let Some(source) = &self.guard.nested_source {
|
||||
let child = project.file(&source.path).ok_or("checkpoint child is unavailable")?;
|
||||
if !self.entry.accepts_nested_source(&source.path.to_string_lossy(), &child.data) {
|
||||
return Err("checkpoint child prefix changed".into());
|
||||
}
|
||||
}
|
||||
@@ -774,7 +774,7 @@ impl SelectedCheckpoint {
|
||||
let frontier = checkpoint.entry.frontier();
|
||||
work.retained_checkpoints = self.set.entries.len();
|
||||
work.reused_frontier = Some(frontier);
|
||||
work.reused_source = checkpoint.guard.nested_source.clone();
|
||||
work.reused_source = checkpoint.guard.nested_source.as_ref().map(|source| source.path.clone());
|
||||
work.reused_steps = checkpoint.entry.completed_steps();
|
||||
work.replayed_calls = checkpoint.replay_count;
|
||||
let changed_prefix = || CompileError::Host("prepared checkpoint prefix changed".into());
|
||||
@@ -794,8 +794,8 @@ impl SelectedCheckpoint {
|
||||
let mut schedule = CaptureSchedule::new(source.data.len(), None, edit);
|
||||
let mut nested_schedule = NestedSchedule::default();
|
||||
let entry_frontier = checkpoint.entry.entry_frontier();
|
||||
let current_nested = checkpoint.guard.nested_source.as_deref()
|
||||
.and_then(|path| request.project.as_ref()?.file(path));
|
||||
let current_nested = checkpoint.guard.nested_source.as_ref()
|
||||
.and_then(|source| request.project.as_ref()?.file(&source.path));
|
||||
let nested_source = current_nested.map(|file| SourceFile {
|
||||
name: file.version.path.display().to_string(),
|
||||
data: file.data.to_vec(),
|
||||
@@ -930,12 +930,17 @@ fn changed_line_start(previous: &[u8], current: &[u8]) -> Option<usize> {
|
||||
)
|
||||
}
|
||||
|
||||
struct NestedSourceGuard {
|
||||
path: PathBuf,
|
||||
observed_size: Option<u64>,
|
||||
}
|
||||
|
||||
struct PrefixInputGuard {
|
||||
original: AttemptRequest,
|
||||
project: Arc<ProjectSnapshot>,
|
||||
initial: GeneratedInitialState,
|
||||
entry: PathBuf,
|
||||
nested_source: Option<PathBuf>,
|
||||
nested_source: Option<NestedSourceGuard>,
|
||||
immutable: BTreeSet<PathBuf>,
|
||||
immutable_stream_prefixes: BTreeMap<PathBuf, usize>,
|
||||
generated: BTreeSet<PathBuf>,
|
||||
@@ -1057,16 +1062,21 @@ impl PrefixInputGuard {
|
||||
|
||||
fn allow_nested_source(&mut self, path: &Path, trace: &ResourceTrace, host: &RecordingHost<'_>) -> bool {
|
||||
let Some(file) = self.project.file(path) else { return false };
|
||||
// Presence and size queries can be replayed against the new bytes.
|
||||
// All content reads other than the active scanner pin the old bytes.
|
||||
if !host.source_read_is_rebindable(&file.version)
|
||||
|| trace.stream_reads.iter().any(|read| read.path == path)
|
||||
// The scanner pins its loaded prefix. A size query also pins the
|
||||
// observed byte count. Other content reads still pin the full input.
|
||||
let Some(observations) = host.rebindable_source_observations(&file.version) else {
|
||||
return false;
|
||||
};
|
||||
if trace.stream_reads.iter().any(|read| read.path == path)
|
||||
|| trace.file_modifications.iter().any(|read| read.path == path)
|
||||
|| trace.generated_lookups.iter().any(|read| read.target == path)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
self.nested_source = Some(path.to_path_buf());
|
||||
self.nested_source = Some(NestedSourceGuard {
|
||||
path: path.to_path_buf(),
|
||||
observed_size: observations.size,
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
@@ -1095,11 +1105,22 @@ impl PrefixInputGuard {
|
||||
if !self.project.same_lookup_configuration(current)? {
|
||||
return Err("checkpoint lookup configuration changed".into());
|
||||
}
|
||||
// An immutable source's recorded size can rule out the checkpoint
|
||||
// before drafting or replay. A changed namespace still goes through
|
||||
// the live lookup and all successful candidates replay their calls.
|
||||
if let Some(source) = &self.nested_source
|
||||
&& let Some(expected) = source.observed_size
|
||||
&& let Some(file) = current.file(&source.path)
|
||||
&& file.version.bytes != expected
|
||||
&& self.project.immutable_paths().eq(current.immutable_paths())
|
||||
{
|
||||
return Err(format!("checkpoint file size changed: {}", source.path.display()));
|
||||
}
|
||||
for path in &self.immutable {
|
||||
let same_bytes = self.project.file(path).zip(current.file(path)).is_some_and(
|
||||
|(old, new)| {
|
||||
old.version == new.version
|
||||
|| self.nested_source.as_ref() == Some(path)
|
||||
|| self.nested_source.as_ref().is_some_and(|source| &source.path == path)
|
||||
|| self.immutable_stream_prefixes.get(path).is_some_and(|&end| {
|
||||
new.data.get(..end) == Some(&old.data[..end])
|
||||
})
|
||||
|
||||
@@ -31,6 +31,10 @@ pub(super) struct SizeRead {
|
||||
read: Option<ResourceVersion>,
|
||||
}
|
||||
|
||||
pub(super) struct RebindableSourceObservations {
|
||||
pub(super) size: Option<u64>,
|
||||
}
|
||||
|
||||
impl Call {
|
||||
pub(super) fn replay(&self, files: &mut Files, partial_source: Option<&ResourceVersion>) -> Result<(), String> {
|
||||
match self {
|
||||
@@ -105,19 +109,30 @@ impl<'a> RecordingHost<'a> {
|
||||
(count < MAX_REPLAY_CALLS).then(|| (self.files.resource_trace(), count))
|
||||
}
|
||||
|
||||
pub(super) fn source_read_is_rebindable(&self, version: &ResourceVersion) -> bool {
|
||||
let Some(calls) = &self.calls else { return false };
|
||||
pub(super) fn rebindable_source_observations(
|
||||
&self,
|
||||
version: &ResourceVersion,
|
||||
) -> Option<RebindableSourceObservations> {
|
||||
let calls = self.calls.as_ref()?;
|
||||
let mut source_reads = 0;
|
||||
let mut size_reads = 0;
|
||||
for call in calls {
|
||||
match call {
|
||||
Call::ReadSource(read) if read.expected.as_ref() == Ok(version) => source_reads += 1,
|
||||
Call::FileSize(read) if read.read.as_ref().is_some_and(|r| r.path == version.path) => size_reads += 1,
|
||||
Call::FileSize(read) if read.read.as_ref().is_some_and(|r| r.path == version.path) => {
|
||||
if read.observed != Ok(Some(version.bytes)) {
|
||||
return None;
|
||||
}
|
||||
size_reads += 1;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
source_reads == 1 && self.files.resource_trace().reads.iter()
|
||||
.filter(|read| read.path == version.path).count() == source_reads + size_reads
|
||||
let actual_reads = self.files.resource_trace().reads.iter()
|
||||
.filter(|read| read.path == version.path).count();
|
||||
(source_reads == 1 && actual_reads == source_reads + size_reads).then_some(
|
||||
RebindableSourceObservations { size: (size_reads > 0).then_some(version.bytes) },
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn files(&self) -> &Files {
|
||||
|
||||
Reference in new issue
Block a user