Make sure tempfiles don't get dropped too early

This commit is contained in:
Richard Feldman 2022-11-22 20:22:08 -05:00
parent 6b446fe592
commit 0b73ea69af
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
6 changed files with 46 additions and 25 deletions

View file

@ -1417,19 +1417,19 @@ pub fn preprocess_host_wasm32(host_input_path: &Path, preprocessed_host_path: &P
(but seems to be an unofficial API)
*/
let builtins_host_file = tempfile::Builder::new()
let builtins_host_tempfile = tempfile::Builder::new()
.prefix("host_bitcode")
.suffix(".wasm")
.rand_bytes(5)
.tempfile()
.unwrap();
std::fs::write(builtins_host_file.path(), bitcode::HOST_WASM)
std::fs::write(builtins_host_tempfile.path(), bitcode::HOST_WASM)
.expect("failed to write host builtins object to tempfile");
let mut zig_cmd = zig();
let args = &[
"wasm-ld",
builtins_host_file.path().to_str().unwrap(),
builtins_host_tempfile.path().to_str().unwrap(),
host_input,
WASI_LIBC_PATH,
WASI_COMPILER_RT_PATH, // builtins need __multi3, __udivti3, __fixdfti
@ -1446,7 +1446,11 @@ pub fn preprocess_host_wasm32(host_input_path: &Path, preprocessed_host_path: &P
// println!("\npreprocess_host_wasm32");
// println!("zig {}\n", args.join(" "));
run_build_command(zig_cmd, output_file, 0)
run_build_command(zig_cmd, output_file, 0);
// Extend the lifetime of the tempfile so it doesn't get dropped
// (and thus deleted) before the Zig process is done using it!
let _ = builtins_host_tempfile;
}
fn run_build_command(mut command: Command, file_to_build: &str, flaky_fail_counter: usize) {

View file

@ -100,18 +100,18 @@ fn build_wasm_test_host() {
let mut outfile = PathBuf::from(&out_dir).join(PLATFORM_FILENAME);
outfile.set_extension("wasm");
let builtins_host_file = tempfile::Builder::new()
let builtins_host_tempfile = tempfile::Builder::new()
.prefix("host_bitcode")
.suffix(".wasm")
.rand_bytes(5)
.tempfile()
.unwrap();
std::fs::write(builtins_host_file.path(), bitcode::HOST_WASM)
std::fs::write(builtins_host_tempfile.path(), bitcode::HOST_WASM)
.expect("failed to write host builtins object to tempfile");
run_zig(&[
"wasm-ld",
builtins_host_file.path().to_str().unwrap(),
builtins_host_tempfile.path().to_str().unwrap(),
platform_path.to_str().unwrap(),
WASI_COMPILER_RT_PATH,
WASI_LIBC_PATH,
@ -120,6 +120,10 @@ fn build_wasm_test_host() {
"--no-entry",
"--relocatable",
]);
// Extend the lifetime of the tempfile so it doesn't get dropped
// (and thus deleted) before the Zig process is done using it!
let _ = builtins_host_tempfile;
}
fn build_wasm_platform(out_dir: &str, source_path: &str) -> PathBuf {

View file

@ -193,13 +193,13 @@ pub fn helper(
.expect("failed to build output object");
std::fs::write(&app_o_file, module_out).expect("failed to write object to file");
let builtins_host_file = tempfile::Builder::new()
let builtins_host_tempfile = tempfile::Builder::new()
.prefix("host_bitcode")
.suffix(".o")
.rand_bytes(5)
.tempfile()
.unwrap();
std::fs::write(builtins_host_file.path(), bitcode::HOST_UNIX)
std::fs::write(builtins_host_tempfile.path(), bitcode::HOST_UNIX)
.expect("failed to write host builtins object to tempfile");
let (mut child, dylib_path) = link(
@ -209,7 +209,7 @@ pub fn helper(
// With the current method all methods are kept and it adds about 100k to all outputs.
&[
app_o_file.to_str().unwrap(),
builtins_host_file.path().to_str().unwrap(),
builtins_host_tempfile.path().to_str().unwrap(),
],
LinkType::Dylib,
)
@ -217,6 +217,10 @@ pub fn helper(
child.wait().unwrap();
// Extend the lifetime of the tempfile so it doesn't get dropped
// (and thus deleted) before the linking process is done using it!
let _ = builtins_host_tempfile;
// Load the dylib
let path = dylib_path.as_path().to_str().unwrap();