Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: Fail fast when compiling a source file larger than 4 GiB #132791

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,8 @@ impl StableSourceFileId {
}

impl SourceFile {
const MAX_FILE_SIZE: u32 = u32::MAX - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the -1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #132862

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is fail fast for files LARGER than 4 GiB. 4GiB is 2^32B is (u32::MAX + 1) bytes, so U32::MAX is 4Gi - 1, and U32::MAX - 1 is 4Gi - 2. What is the purpose of a constant for 4GiB - 2B when preventing use of files larger than 4GiB? Is that not off by 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally this PR was for enforcing that files of size 4 GiB and larger would not work, meaning the maximum file size would be 4 GiB - 1 B = u32::MAX bytes.

However, I then discovered #132862, so files of size 4 GiB - 1 B and larger would not work, so the maximum file size is actually 4 GiB - 2 B = u32::MAX - 1. So no the current value is correct.

I've just tested this again to make sure. Using the current stable and nightly version of rustc, I get the following results:

File size (bytes) stable result nightly result
4,294,967,294 success success
4,294,967,295 ICE (#132862) instant failure
4,294,967,296 failure after reading the file to memory instant failure


pub fn new(
name: FileName,
mut src: String,
Expand All @@ -1863,6 +1865,9 @@ impl SourceFile {
let stable_id = StableSourceFileId::from_filename_in_current_crate(&name);
let source_len = src.len();
let source_len = u32::try_from(source_len).map_err(|_| OffsetOverflowError)?;
if source_len > Self::MAX_FILE_SIZE {
return Err(OffsetOverflowError);
}

let (lines, multibyte_chars) = analyze_source_file::analyze_source_file(&src);

Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl FileLoader for RealFileLoader {
}

fn read_file(&self, path: &Path) -> io::Result<String> {
if path.metadata().is_ok_and(|metadata| metadata.len() > SourceFile::MAX_FILE_SIZE.into()) {
return Err(io::Error::other(format!(
"text files larger than {} bytes are unsupported",
SourceFile::MAX_FILE_SIZE
)));
}
fs::read_to_string(path)
}

Expand Down Expand Up @@ -297,7 +303,10 @@ impl SourceMap {
/// unmodified.
pub fn new_source_file(&self, filename: FileName, src: String) -> Lrc<SourceFile> {
self.try_new_source_file(filename, src).unwrap_or_else(|OffsetOverflowError| {
eprintln!("fatal error: rustc does not support files larger than 4GB");
eprintln!(
"fatal error: rustc does not support text files larger than {} bytes",
SourceFile::MAX_FILE_SIZE
);
crate::fatal_error::FatalError.raise()
})
}
Expand Down
Loading