Skip to content

Commit 5f33647

Browse files
committed
Add unit tests based on files that return odd sizes to stat
1 parent 272cd38 commit 5f33647

File tree

1 file changed

+27
-0
lines changed
  • compiler/rustc_span/src/source_map

1 file changed

+27
-0
lines changed

Diff for: compiler/rustc_span/src/source_map/tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,30 @@ fn test_next_point() {
567567
assert_eq!(span.hi().0, 6);
568568
assert!(sm.span_to_snippet(span).is_err());
569569
}
570+
571+
#[cfg(target_os = "linux")]
572+
#[test]
573+
fn read_binary_file_handles_lying_stat() {
574+
// read_binary_file tries to read the contents of a file into an Lrc<[u8]> while
575+
// never having two copies of the data in memory at once. This is an optimization
576+
// to support include_bytes! with large files. But since Rust allocators are
577+
// sensitive to alignment, our implementation can't be bootstrapped off calling
578+
// std::fs::read. So we test that we have the same behavior even on files where
579+
// fs::metadata lies.
580+
581+
// stat always says that /proc/self/cmdline is length 0, but it isn't.
582+
let cmdline = Path::new("/proc/self/cmdline");
583+
let len = std::fs::metadata(cmdline).unwrap().len() as usize;
584+
let real = std::fs::read(cmdline).unwrap();
585+
assert!(len < real.len());
586+
let bin = RealFileLoader.read_binary_file(cmdline).unwrap();
587+
assert_eq!(&real[..], &bin[..]);
588+
589+
// stat always says that /sys/devices/system/cpu/kernel_max is the size of a block.
590+
let kernel_max = Path::new("/sys/devices/system/cpu/kernel_max");
591+
let len = std::fs::metadata(kernel_max).unwrap().len() as usize;
592+
let real = std::fs::read(kernel_max).unwrap();
593+
assert!(len > real.len());
594+
let bin = RealFileLoader.read_binary_file(kernel_max).unwrap();
595+
assert_eq!(&real[..], &bin[..]);
596+
}

0 commit comments

Comments
 (0)