浏览代码

src/jit.rs: Fix memory leak in JIT-compiler

We have a memory leak in the JIT-compiler: we never free the memory area
used to store the machine code for the program. Let's implement the
Drop() trait for the struct JitMemory so we can clean after ourselves.

Reported-by: Ben Kimock <kimockb@gmail.com>
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Quentin Monnet 2 年之前
父节点
当前提交
beb469fff9
共有 1 个文件被更改,包括 8 次插入1 次删除
  1. 8 1
      src/jit.rs

+ 8 - 1
src/jit.rs

@@ -510,6 +510,14 @@ impl<'a> IndexMut<usize> for JitMemory<'a> {
     }
 }
 
+impl<'a> Drop for JitMemory<'a> {
+    fn drop(&mut self) {
+        unsafe {
+            libc::free(self.contents.as_mut_ptr() as *mut libc::c_void);
+        }
+    }
+}
+
 impl<'a> std::fmt::Debug for JitMemory<'a> {
     fn fmt(&self, fmt: &mut Formatter) -> Result<(), FormatterError> {
         fmt.write_str("JIT contents: [")?;
@@ -1023,4 +1031,3 @@ impl JitCompiler {
         self.special_targets.insert(target, jit.offset);
     }
 } // struct JitCompiler
-