[libvirt-java] [PATCH 11/65] Use virFree in order to release memory acquired from libvirt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When JNA is linked to a different runtime library than libvirt, using
JNA's Native.free will probably lead to crashes as witnessed on
Windows:

 # A fatal error has been detected by the Java Runtime Environment:
 #
 #  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000077363290, pid=10180, tid=9908
 #
 # JRE version: 7.0_25-b16
 # Java VM: Java HotSpot(TM) 64-Bit Server VM (23.25-b01 mixed mode windows-amd64 compressed oops)
 # Problematic frame:
 # C  [ntdll.dll+0x53290]  RtlFreeHeap+0xd0

The root cause is that the libvirt DLL uses MSVCRT as its runtime
library, whereas the jnidispatch DLL of JNA uses a different one.

At runtime, when calling org.sun.com.jna.Native.free() the OS function
RtlFreeHeap is called with an invalid Pointer that was actually
allocated by MSVCRT's malloc.

Basically, we cannot simply mix and match memory allocation functions
from different runtime libraries, but have to call virFree for
pointers orignating from libvirt itself.

So, this patch re-adds and uses the virFree method which had been
removed in commit 3220de292990bed71828fba2f3700bc846d440f2.

Signed-off-by: Claudio Bley <cbley@xxxxxxxxxx>
---
 src/main/java/org/libvirt/Library.java     |    4 ++--
 src/main/java/org/libvirt/jna/Libvirt.java |    2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/libvirt/Library.java b/src/main/java/org/libvirt/Library.java
index 0136095..33d3042 100644
--- a/src/main/java/org/libvirt/Library.java
+++ b/src/main/java/org/libvirt/Library.java
@@ -4,6 +4,7 @@ import org.libvirt.jna.Libvirt;
 
 import com.sun.jna.Native;
 import com.sun.jna.Pointer;
+import com.sun.jna.ptr.PointerByReference;
 
 /**
  * This class represents an instance of the JNA mapped libvirt
@@ -38,8 +39,7 @@ final class Library {
      * Free memory pointed to by ptr.
      */
     static void free(Pointer ptr) {
-        Native.free(Pointer.nativeValue(ptr));
-        Pointer.nativeValue(ptr, 0L);
+        libvirt.virFree(new PointerByReference(ptr));
     }
 
     /**
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index ffbb5a0..78dd9bc 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -8,6 +8,7 @@ import com.sun.jna.Platform;
 import com.sun.jna.Pointer;
 import com.sun.jna.ptr.IntByReference;
 import com.sun.jna.ptr.LongByReference;
+import com.sun.jna.ptr.PointerByReference;
 
 /**
  * The libvirt interface which is exposed via JNA. The complete API is
@@ -172,6 +173,7 @@ public interface Libvirt extends Library {
     int virGetVersion(LongByReference libVer, String type, LongByReference typeVer);
     int virInitialize();
     int virCopyLastError(virError error);
+    void virFree(PointerByReference ptr);
     virError virGetLastError();
     void virResetLastError();
     void virSetErrorFunc(Pointer userData, VirErrorCallback callback);
-- 
1.7.9.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]