about summary refs log tree commit diff
path: root/compiler/rustc_span/src/source_map
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2023-09-20 19:26:04 -0400
committerBen Kimock <kimockb@gmail.com>2023-09-20 21:01:13 -0400
commit5f33647fb392c2d4f71af94a223bc1a9abb80643 (patch)
treedc8edde4ca5683bc025cffb59edc373e6bf2df3c /compiler/rustc_span/src/source_map
parent272cd384e8d1006240ecaed5077fe2990f576dbb (diff)
downloadrust-5f33647fb392c2d4f71af94a223bc1a9abb80643.tar.gz
rust-5f33647fb392c2d4f71af94a223bc1a9abb80643.zip
Add unit tests based on files that return odd sizes to stat
Diffstat (limited to 'compiler/rustc_span/src/source_map')
-rw-r--r--compiler/rustc_span/src/source_map/tests.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/compiler/rustc_span/src/source_map/tests.rs b/compiler/rustc_span/src/source_map/tests.rs
index e393db02064..a12f50c87a2 100644
--- a/compiler/rustc_span/src/source_map/tests.rs
+++ b/compiler/rustc_span/src/source_map/tests.rs
@@ -567,3 +567,30 @@ fn test_next_point() {
     assert_eq!(span.hi().0, 6);
     assert!(sm.span_to_snippet(span).is_err());
 }
+
+#[cfg(target_os = "linux")]
+#[test]
+fn read_binary_file_handles_lying_stat() {
+    // read_binary_file tries to read the contents of a file into an Lrc<[u8]> while
+    // never having two copies of the data in memory at once. This is an optimization
+    // to support include_bytes! with large files. But since Rust allocators are
+    // sensitive to alignment, our implementation can't be bootstrapped off calling
+    // std::fs::read. So we test that we have the same behavior even on files where
+    // fs::metadata lies.
+
+    // stat always says that /proc/self/cmdline is length 0, but it isn't.
+    let cmdline = Path::new("/proc/self/cmdline");
+    let len = std::fs::metadata(cmdline).unwrap().len() as usize;
+    let real = std::fs::read(cmdline).unwrap();
+    assert!(len < real.len());
+    let bin = RealFileLoader.read_binary_file(cmdline).unwrap();
+    assert_eq!(&real[..], &bin[..]);
+
+    // stat always says that /sys/devices/system/cpu/kernel_max is the size of a block.
+    let kernel_max = Path::new("/sys/devices/system/cpu/kernel_max");
+    let len = std::fs::metadata(kernel_max).unwrap().len() as usize;
+    let real = std::fs::read(kernel_max).unwrap();
+    assert!(len > real.len());
+    let bin = RealFileLoader.read_binary_file(kernel_max).unwrap();
+    assert_eq!(&real[..], &bin[..]);
+}