Skip to content

Commit

Permalink
Detect truncated incr comp files
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Mar 9, 2024
1 parent 4282576 commit c0b1ccf
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion compiler/rustc_incremental/src/persist/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use rustc_session::Session;
use std::borrow::Cow;
use std::env;
use std::fs;
use std::io::{self, Read};
use std::io::{self, Read, Write};
use std::io::{Seek, SeekFrom};
use std::path::{Path, PathBuf};

/// The first few bytes of files generated by incremental compilation.
Expand All @@ -35,6 +36,8 @@ pub(crate) fn write_file_header(stream: &mut FileEncoder, sess: &Session) {
assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize);
stream.emit_raw_bytes(&[rustc_version.len() as u8]);
stream.emit_raw_bytes(rustc_version.as_bytes());
// Reserve space for the u64 where we will later write in the expected total size of this file.
stream.emit_raw_bytes(&0u64.to_le_bytes());
}

pub(crate) fn save_in<F>(sess: &Session, path_buf: PathBuf, name: &str, encode: F)
Expand Down Expand Up @@ -65,6 +68,8 @@ where

write_file_header(&mut encoder, sess);

let len_offset = encoder.position() - 8;

match encode(encoder) {
Ok(position) => {
sess.prof.artifact_size(
Expand All @@ -76,6 +81,17 @@ where
}
Err((path, err)) => sess.dcx().emit_fatal(errors::WriteNew { name, path, err }),
}

if let Err(err) = write_file_len(&path_buf, len_offset as u64) {
sess.dcx().emit_fatal(errors::WriteNew { name, path: path_buf, err });
}
}

fn write_file_len(path: &Path, offset: u64) -> Result<(), std::io::Error> {
let mut file = fs::File::options().write(true).create(false).open(path)?;
let len = file.metadata()?.len();
file.seek(SeekFrom::Start(offset))?;
file.write_all(&len.to_le_bytes())
}

/// Reads the contents of a file with a file header as defined in this module.
Expand Down Expand Up @@ -147,6 +163,14 @@ pub fn read_file(
}
}

// Check FILE_LEN
{
let mut len = [0u8; 8];
file.read_exact(&mut len)?;
let len_in_header = u64::from_le_bytes(len);
assert_eq!(len_in_header, fs::metadata(path)?.len());
}

let post_header_start_pos = file.position() as usize;
Ok(Some((mmap, post_header_start_pos)))
}
Expand Down

0 comments on commit c0b1ccf

Please sign in to comment.