From ef791d07d9e55b1f98da79abac90d479affcfa43 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 12 Aug 2021 15:26:48 +0200 Subject: [PATCH] fix(engine-dylib) Remove temporary file used to creating an artifact. When creating an artifact, a first temporary file is created and kept to prevent the file from being removed by the system. This patch removes that temporary file manually whatever the linker fails or not. --- CHANGELOG.md | 17 +++++++++-------- lib/engine-dylib/src/artifact.rs | 24 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cb92231bfa..9f259ae0912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,20 +9,21 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C ## **[Unreleased]** ### Added -- [#2427](https://github.com/wasmerio/wasmer/pull/2427) Wasmer can now compile to Javascript via `wasm-bindgen`. Use the `js-default` (and no default features) feature to try it! -- [#2436](https://github.com/wasmerio/wasmer/pull/2436) Added the x86-32 bit variant support to LLVM compiler +- [#2427](https://github.com/wasmerio/wasmer/pull/2427) Wasmer can now compile to Javascript via `wasm-bindgen`. Use the `js-default` (and no default features) feature to try it!. +- [#2436](https://github.com/wasmerio/wasmer/pull/2436) Added the x86-32 bit variant support to LLVM compiler. ### Changed -- [#2460](https://github.com/wasmerio/wasmer/pull/2460) **breaking change** `wasmer` API usage with `no-default-features` requires now the `sys` feature to preserve old behavior -- [#2476](https://github.com/wasmerio/wasmer/pull/2476) Removed unncessary abstraction `ModuleInfoTranslate` from `wasmer-compiler` -- [#2442](https://github.com/wasmerio/wasmer/pull/2442) Improved `WasmPtr`, added `WasmCell` for host/guest interaction +- [#2460](https://github.com/wasmerio/wasmer/pull/2460) **breaking change** `wasmer` API usage with `no-default-features` requires now the `sys` feature to preserve old behavior. +- [#2476](https://github.com/wasmerio/wasmer/pull/2476) Removed unncessary abstraction `ModuleInfoTranslate` from `wasmer-compiler`. +- [#2442](https://github.com/wasmerio/wasmer/pull/2442) Improved `WasmPtr`, added `WasmCell` for host/guest interaction. - [#2427](https://github.com/wasmerio/wasmer/pull/2427) Update `loupe` to 0.1.3. - [#2478](https://github.com/wasmerio/wasmer/pull/2478) Rename `wasm_instance_new()`’s “traps” argument to “trap”. ### Fixed -- [#2494](https://github.com/wasmerio/wasmer/pull/2494) Fixed `WasmerEnv` access when using `call_indirect` with the Singlepass compiler -- [#2449](https://github.com/wasmerio/wasmer/pull/2449) Fixed `wasmer-c-api` used `soname` -- [#2479](https://github.com/wasmerio/wasmer/pull/2479) Improved `wasmer validate` error message on non-wasm inputs +- [#2518](https://github.com/wasmerio/wasmer/pull/2518) Remove temporary file used to creating an artifact when creating a Dylib engine artifact. +- [#2494](https://github.com/wasmerio/wasmer/pull/2494) Fixed `WasmerEnv` access when using `call_indirect` with the Singlepass compiler. +- [#2449](https://github.com/wasmerio/wasmer/pull/2449) Fixed `wasmer-c-api` used `soname`. +- [#2479](https://github.com/wasmerio/wasmer/pull/2479) Improved `wasmer validate` error message on non-wasm inputs. - [#2454](https://github.com/wasmerio/wasmer/issues/2454) Won't set `WASMER_CACHE_DIR` for Windows. - [#2426](https://github.com/wasmerio/wasmer/pull/2426) Fix the `wax` script generation. diff --git a/lib/engine-dylib/src/artifact.rs b/lib/engine-dylib/src/artifact.rs index d4230e84bd4..a853d5f2cd1 100644 --- a/lib/engine-dylib/src/artifact.rs +++ b/lib/engine-dylib/src/artifact.rs @@ -6,7 +6,7 @@ use crate::serialize::{ArchivedModuleMetadata, ModuleMetadata}; use libloading::{Library, Symbol as LibrarySymbol}; use loupe::MemoryUsage; use std::error::Error; -use std::fs::File; +use std::fs::{self, File}; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; #[cfg(feature = "compiler")] @@ -59,7 +59,7 @@ pub struct DylibArtifact { } fn to_compile_error(err: impl Error) -> CompileError { - CompileError::Codegen(format!("{}", err)) + CompileError::Codegen(err.to_string()) } const WASMER_METADATA_SYMBOL: &[u8] = b"WASMER_METADATA"; @@ -266,7 +266,7 @@ impl DylibArtifact { } }; - let shared_filepath = { + let output_filepath = { let suffix = format!(".{}", Self::get_default_extension(&target_triple)); let shared_file = tempfile::Builder::new() .prefix("wasmer_dylib_") @@ -322,14 +322,20 @@ impl DylibArtifact { let output = Command::new(linker) .arg(&filepath) .arg("-o") - .arg(&shared_filepath) + .arg(&output_filepath) .args(&target_args) // .args(&wasmer_symbols) .arg("-shared") .args(&cross_compiling_args) .arg("-v") .output() - .map_err(to_compile_error)?; + .map_err(to_compile_error); + + if fs::metadata(filepath).is_ok() { + fs::remove_file(filepath).map_err(to_compile_error)?; + } + + let output = output?; if !output.status.success() { return Err(CompileError::Codegen(format!( @@ -338,12 +344,14 @@ impl DylibArtifact { String::from_utf8_lossy(&output.stdout).trim_end() ))); } + trace!("gcc command result {:?}", output); + if is_cross_compiling { - Self::from_parts_crosscompiled(metadata, shared_filepath) + Self::from_parts_crosscompiled(metadata, output_filepath) } else { - let lib = unsafe { Library::new(&shared_filepath).map_err(to_compile_error)? }; - Self::from_parts(&mut engine_inner, metadata, shared_filepath, lib) + let lib = unsafe { Library::new(&output_filepath).map_err(to_compile_error)? }; + Self::from_parts(&mut engine_inner, metadata, output_filepath, lib) } }