Skip to content

Commit

Permalink
fix(engine-dylib) Remove temporary file used to creating an artifact.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hywan committed Aug 12, 2021
1 parent 22634fa commit ef791d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
17 changes: 9 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
24 changes: 16 additions & 8 deletions lib/engine-dylib/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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_")
Expand Down Expand Up @@ -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!(
Expand All @@ -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)
}
}

Expand Down

0 comments on commit ef791d0

Please sign in to comment.