SimpleJIT - Add a Drop impl to PtrLen (#1001)

* Implement SimpleJIT deallocation for non-windows targets.

* Remove custom Drop from feature(selinux-fix).

* Fix typo in unprotect error message.

* Drop SimpleJIT memory by default and add free_memory method instead.

* Move free_memory to a handle returned by Module::finish.

* Reduce memory handle content to necessary fields only.

* Use lower overhead method for leaking.
This commit is contained in:
Daniel
2019-11-06 05:12:34 +01:00
committed by Dan Gohman
parent 1417215532
commit 16441ed70a
2 changed files with 89 additions and 18 deletions

View File

@@ -105,8 +105,26 @@ impl PtrLen {
}
}
// `MMapMut` from `cfg(feature = "selinux-fix")` already deallocates properly.
#[cfg(all(not(target_os = "windows"), not(feature = "selinux-fix")))]
impl Drop for PtrLen {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe {
region::protect(self.ptr, self.len, region::Protection::ReadWrite)
.expect("unable to unprotect memory");
libc::free(self.ptr as _);
}
}
}
}
// TODO: add a `Drop` impl for `cfg(target_os = "windows")`
/// JIT memory manager. This manages pages of suitably aligned and
/// accessible memory.
/// accessible memory. Memory will be leaked by default to have
/// function pointers remain valid for the remainder of the
/// program's life.
pub struct Memory {
allocations: Vec<PtrLen>,
executable: usize,
@@ -209,9 +227,22 @@ impl Memory {
}
}
}
/// Frees all allocated memory regions that would be leaked otherwise.
/// Likely to invalidate existing function pointers, causing unsafety.
pub unsafe fn free_memory(&mut self) {
self.allocations.clear();
}
}
// TODO: Implement Drop to unprotect and deallocate the memory?
impl Drop for Memory {
fn drop(&mut self) {
// leak memory to guarantee validity of function pointers
mem::replace(&mut self.allocations, Vec::new())
.into_iter()
.for_each(mem::forget);
}
}
#[cfg(test)]
mod tests {