Skip to content

Commit

Permalink
fix(engine-dylib): do not delete temp file in every scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
  • Loading branch information
bnjjj committed Sep 1, 2021
1 parent 297d9c1 commit efc84e1
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/engine-dylib/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use wasmer_vm::{
#[derive(MemoryUsage)]
pub struct DylibArtifact {
dylib_path: PathBuf,
is_temporary: bool,
metadata: ModuleMetadata,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
#[loupe(skip)]
Expand All @@ -60,7 +61,9 @@ pub struct DylibArtifact {

impl Drop for DylibArtifact {
fn drop(&mut self) {
std::fs::remove_file(&self.dylib_path).expect("cannot delete the temporary artifact");
if self.is_temporary {
std::fs::remove_file(&self.dylib_path).expect("cannot delete the temporary artifact");
}
}
}

Expand Down Expand Up @@ -374,12 +377,15 @@ impl DylibArtifact {

trace!("gcc command result {:?}", output);

if is_cross_compiling {
let mut artifact = if is_cross_compiling {
Self::from_parts_crosscompiled(metadata, output_filepath)
} else {
let lib = unsafe { Library::new(&output_filepath).map_err(to_compile_error)? };
Self::from_parts(&mut engine_inner, metadata, output_filepath, lib)
}
}?;
artifact.is_temporary = true;

Ok(artifact)
}

/// Get the default extension when serializing this artifact
Expand All @@ -406,6 +412,7 @@ impl DylibArtifact {
let signatures: PrimaryMap<SignatureIndex, VMSharedSignatureIndex> = PrimaryMap::new();
Ok(Self {
dylib_path,
is_temporary: false,
metadata,
finished_functions: finished_functions.into_boxed_slice(),
finished_function_call_trampolines: finished_function_call_trampolines
Expand Down Expand Up @@ -511,6 +518,7 @@ impl DylibArtifact {

Ok(Self {
dylib_path,
is_temporary: false,
metadata,
finished_functions: finished_functions.into_boxed_slice(),
finished_function_call_trampolines: finished_function_call_trampolines
Expand Down Expand Up @@ -552,7 +560,10 @@ impl DylibArtifact {
file.write_all(&bytes)?;
// We already checked for the header, so we don't need
// to check again.
Self::deserialize_from_file_unchecked(&engine, &path)
let mut artifact = Self::deserialize_from_file_unchecked(&engine, &path)?;
artifact.is_temporary = true;

Ok(artifact)
}

/// Deserialize a `DylibArtifact` from a file path.
Expand Down

0 comments on commit efc84e1

Please sign in to comment.