Skip to content

Commit 0cc4946

Browse files
committed
add a test to make rustc_incremental finalize_session_directory rename fail
Use a proc macro to observe the incremental session directory and do something platform specific so that renaming the '-working' session directory during finalize_session_directory will fail. On Unix, change the permissions on the parent directory to be read-only. On Windows, open and leak a file inside the `-working` directory.
1 parent 4845f78 commit 0cc4946

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
poison::poison_finalize!();
2+
3+
pub fn hello() -> i32 {
4+
42
5+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//! A proc macro that sabotages the incremental compilation finalize step.
2+
//!
3+
//! When invoked, it locates the `-working` session directory inside the
4+
//! incremental compilation directory (passed via POISON_INCR_DIR) and
5+
//! makes it impossible to rename:
6+
//!
7+
//! - On Unix: removes write permission from the parent (crate) directory.
8+
//! - On Windows: creates a file inside the -working directory and leaks
9+
//! the file handle, preventing the directory from being renamed.
10+
11+
extern crate proc_macro;
12+
13+
use std::fs;
14+
use std::path::PathBuf;
15+
16+
use proc_macro::TokenStream;
17+
18+
#[proc_macro]
19+
pub fn poison_finalize(_input: TokenStream) -> TokenStream {
20+
let incr_dir = std::env::var("POISON_INCR_DIR").expect("POISON_INCR_DIR must be set");
21+
22+
let crate_dir = find_crate_dir(&incr_dir);
23+
let working_dir = find_working_dir(&crate_dir);
24+
25+
#[cfg(unix)]
26+
poison_unix(&crate_dir);
27+
28+
#[cfg(windows)]
29+
poison_windows(&working_dir);
30+
31+
TokenStream::new()
32+
}
33+
34+
/// Remove write permission from the crate directory.
35+
/// This causes rename() to fail with EACCES
36+
#[cfg(unix)]
37+
fn poison_unix(crate_dir: &PathBuf) {
38+
use std::os::unix::fs::PermissionsExt;
39+
let mut perms = fs::metadata(crate_dir).unwrap().permissions();
40+
perms.set_mode(0o555); // r-xr-xr-x
41+
fs::set_permissions(crate_dir, perms).unwrap();
42+
}
43+
44+
/// Create a file inside the -working directory and leak the
45+
/// handle. Windows prevents renaming a directory when any file inside it
46+
/// has an open handle. The handle stays open until the rustc process exits.
47+
#[cfg(windows)]
48+
fn poison_windows(working_dir: &PathBuf) {
49+
let poison_file = working_dir.join("_poison_handle");
50+
let f = fs::File::create(&poison_file).unwrap();
51+
// Leak the handle so it stays open for the lifetime of the rustc process.
52+
std::mem::forget(f);
53+
}
54+
55+
/// Find the crate directory for `foo` inside the incremental compilation dir.
56+
///
57+
/// The incremental directory layout is:
58+
/// {incr_dir}/{crate_name}-{stable_crate_id}/
59+
fn find_crate_dir(incr_dir: &str) -> PathBuf {
60+
let mut dirs = fs::read_dir(incr_dir).unwrap().filter_map(|e| {
61+
let e = e.ok()?;
62+
let name = e.file_name();
63+
let name = name.to_str()?;
64+
if e.file_type().ok()?.is_dir() && name.starts_with("foo-") { Some(e.path()) } else { None }
65+
});
66+
67+
let first =
68+
dirs.next().unwrap_or_else(|| panic!("no foo-* crate directory found in {incr_dir}"));
69+
assert!(
70+
dirs.next().is_none(),
71+
"expected exactly one foo-* crate directory in {incr_dir}, found multiple"
72+
);
73+
first
74+
}
75+
76+
/// Find the session directory ending in "-working" inside the crate directory
77+
fn find_working_dir(crate_dir: &PathBuf) -> PathBuf {
78+
for entry in fs::read_dir(crate_dir).unwrap() {
79+
let entry = entry.unwrap();
80+
let name = entry.file_name();
81+
let name = name.to_str().unwrap().to_string();
82+
if name.starts_with("s-") && name.ends_with("-working") {
83+
return entry.path();
84+
}
85+
}
86+
panic!("no -working session directory found in {}", crate_dir.display());
87+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//! Test that a failure to finalize the incremental compilation session directory
2+
//! (i.e., the rename from "-working" to the SVH-based name) results in a
3+
//! note, not an ICE, and that the compilation output is still produced.
4+
//!
5+
//! Strategy:
6+
//! 1. Build the `poison` proc-macro crate
7+
//! 2. Compile foo.rs with incremental compilation
8+
//! The proc macro runs mid-compilation (after prepare_session_directory
9+
//! but before finalize_session_directory) and sabotages the rename:
10+
//! - On Unix: removes write permission from the crate directory,
11+
//! so rename() fails with EACCES.
12+
//! - On Windows: creates and leaks an open file handle inside the
13+
//! -working directory, so rename() fails with ERROR_ACCESS_DENIED.
14+
//! 3. Assert that stderr contains the finalize failure messages
15+
16+
use std::fs;
17+
use std::path::{Path, PathBuf};
18+
19+
use run_make_support::rustc;
20+
21+
/// Guard that restores permissions on the incremental directory on drop,
22+
/// to ensure cleanup is possible
23+
struct IncrDirCleanup;
24+
25+
fn main() {
26+
let _cleanup = IncrDirCleanup;
27+
28+
// Build the poison proc-macro crate
29+
rustc().input("poison/lib.rs").crate_name("poison").crate_type("proc-macro").run();
30+
31+
let poison_dylib = find_proc_macro_dylib("poison");
32+
33+
// Incremental compile with the poison macro active
34+
let out = rustc()
35+
.input("foo.rs")
36+
.crate_type("rlib")
37+
.incremental("incr")
38+
.extern_("poison", &poison_dylib)
39+
.env("POISON_INCR_DIR", "incr")
40+
.run();
41+
42+
out.assert_stderr_contains("note: did not finalize incremental compilation session directory");
43+
out.assert_stderr_contains(
44+
"help: the next build will not be able to reuse work from this compilation",
45+
);
46+
out.assert_stderr_not_contains("internal compiler error");
47+
}
48+
49+
impl Drop for IncrDirCleanup {
50+
fn drop(&mut self) {
51+
let incr = Path::new("incr");
52+
if !incr.exists() {
53+
return;
54+
}
55+
56+
#[cfg(unix)]
57+
restore_permissions(incr);
58+
}
59+
}
60+
61+
/// Recursively restore write permissions so rm -rf works after the chmod trick
62+
#[cfg(unix)]
63+
fn restore_permissions(path: &Path) {
64+
use std::os::unix::fs::PermissionsExt;
65+
if let Ok(entries) = fs::read_dir(path) {
66+
for entry in entries.filter_map(|e| e.ok()) {
67+
if entry.file_type().map_or(false, |ft| ft.is_dir()) {
68+
let mut perms = match fs::metadata(entry.path()) {
69+
Ok(m) => m.permissions(),
70+
Err(_) => continue,
71+
};
72+
perms.set_mode(0o755);
73+
let _ = fs::set_permissions(entry.path(), perms);
74+
}
75+
}
76+
}
77+
}
78+
79+
/// Locate the compiled proc-macro dylib by scanning the current directory.
80+
fn find_proc_macro_dylib(name: &str) -> PathBuf {
81+
let prefix = if cfg!(target_os = "windows") { "" } else { "lib" };
82+
83+
let ext: &str = if cfg!(target_os = "macos") {
84+
"dylib"
85+
} else if cfg!(target_os = "windows") {
86+
"dll"
87+
} else {
88+
"so"
89+
};
90+
91+
let lib_name = format!("{prefix}{name}.{ext}");
92+
93+
for entry in fs::read_dir(".").unwrap() {
94+
let entry = entry.unwrap();
95+
let name = entry.file_name();
96+
let name = name.to_str().unwrap();
97+
if name == lib_name {
98+
return entry.path();
99+
}
100+
}
101+
102+
panic!("could not find proc-macro dylib for `{name}`");
103+
}

0 commit comments

Comments
 (0)