[mapguide-commits] r10267 - in branches/4.0/MgDev: Bindings/src/Bindings/Java Bindings/src/Bindings/Php Portable/PortableUnmanagedApi/JavaApiEx

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Wed Nov 4 19:41:09 PST 2026


Author: jng
Date: 2026-11-04 19:41:07 -0800 (Wed, 04 Nov 2026)
New Revision: 10267

Modified:
   branches/4.0/MgDev/Bindings/src/Bindings/Java/java.i
   branches/4.0/MgDev/Bindings/src/Bindings/Php/php.i
   branches/4.0/MgDev/Portable/PortableUnmanagedApi/JavaApiEx/java.i
Log:
Fix the Java and PHP bindings leaking a buffer per MgByteReader::Read

Both bindings marshall the same single API - Read(byte[], int), the only BYTE_ARRAY_OUT
parameter in the API - and each leaked the caller's buffer once per call, so an application
draining a resource in chunks leaked the whole payload per fetch however it sized its reads.
The allocations live in the client process's own runtime (the JVM heap and the Zend memory
manager), so the server's ASan/LSan runs cannot see either one: the web tier suites stay green
while the client grows without bound.

Java: the BYTE_ARRAY_OUT freearg typemap released the array with JNI_COMMIT, which copies the
buffer back but deliberately does not free it. A client fetching an 18 MB resource data stream
12 times grew RSS by 206,260 KB (17,188 KB per fetch) with the Java heap flat at 23 MB, and
massif attributed all of it to jni_GetByteArrayElements <- JNIEnv_::GetByteArrayElements <-
Java_..._MgByteReader_1read. Release mode 0 copies back and frees; the same 12 fetches now
grow 2,804 KB, and 40 fetches with 1 MB reads grow 32,324 KB in total with the heap oscillating
between 17 and 44 MB.

PHP: the BYTE_ARRAY_OUT argout typemap assigned the filled buffer over the caller's
by-reference value with ZVAL_STRINGL, which stores the new string without releasing the value
already there, so every read after the first leaked the previous chunk. Draining the same 18 MB
resource through a local MgByteReader grew memory_get_usage(false) by 27,649 KB per resource
with 8 KB reads and 18,505 KB with 1 MB reads. Both are now flat (393 KB used, arena unchanged),
and a 12-fetch proxied loop grows 28 KB in total.

Neither defect is in the two BYTE_ARRAY_IN constructors (MgByteSource and MgByteReader from a
byte[]); those already released with mode 0 and are unchanged - 200 iterations of a 4 MB payload
with each object released grow RSS by ~1 MB in total. The .NET binding has no equivalent
defect: byte[] is a blittable array that the P/Invoke layer pins for the call, so nothing is
allocated per call and the same proxied flow is flat.

  * Bindings/src/Bindings/Java/java.i: release the BYTE_ARRAY_OUT array with mode 0 instead of
    JNI_COMMIT, which is a copy-back but not a release.

  * Bindings/src/Bindings/Php/php.i: dtor the by-reference zval before assigning the read result
    over it.

  * Portable/PortableUnmanagedApi/JavaApiEx/java.i: the same JNI_COMMIT fix, for the
    MapGuideApiEx Java binding that is generated from this tree.

Only the typemap bodies changed, so the generated C++ is the whole of the difference and no
binding surface moved.

Verified on ubuntu22 x64 in the Debug/ASan tree: the web tier java suite reports "Tests
failed/run: 0/618" and the php suite "Tests failed/run: 0/593", each with the server exiting 0
on SIGTERM after logging "Server stopped". The plateau measurements above were taken against
the same build.

Fixes #2898

Modified: branches/4.0/MgDev/Bindings/src/Bindings/Java/java.i
===================================================================
--- branches/4.0/MgDev/Bindings/src/Bindings/Java/java.i	2026-11-05 01:03:45 UTC (rev 10266)
+++ branches/4.0/MgDev/Bindings/src/Bindings/Java/java.i	2026-11-05 03:41:07 UTC (rev 10267)
@@ -118,7 +118,10 @@
 }
 %typemap(freearg) BYTE_ARRAY_OUT
 {
-    jenv->ReleaseByteArrayElements($input, bytes$argnum, JNI_COMMIT);
+    // Mode 0, not JNI_COMMIT: JNI_COMMIT copies back but does not free the buffer that
+    // GetByteArrayElements allocated, so every call that fills a byte[] (MgByteReader::Read
+    // above all) leaked one JVM-heap buffer of the array's length.
+    jenv->ReleaseByteArrayElements($input, bytes$argnum, 0);
 }
 %typemap(javain) BYTE_ARRAY_OUT "$javainput"
 

Modified: branches/4.0/MgDev/Bindings/src/Bindings/Php/php.i
===================================================================
--- branches/4.0/MgDev/Bindings/src/Bindings/Php/php.i	2026-11-05 01:03:45 UTC (rev 10266)
+++ branches/4.0/MgDev/Bindings/src/Bindings/Php/php.i	2026-11-05 03:41:07 UTC (rev 10267)
@@ -145,6 +145,11 @@
 {
     if (Z_ISREF($input)) 
     {
+        // ZVAL_STRINGL only stores into the zval, so without releasing the value the caller's
+        // buffer already holds, every successful read leaked the previous chunk. MgByteReader::Read
+        // is the only user, and a loop that drains a resource in chunks leaked the whole payload
+        // by the time it reached the end.
+        zval_ptr_dtor(Z_REFVAL($input));
         ZVAL_STRINGL(Z_REFVAL($input), (char*)$1, result);
         efree($1);
     }

Modified: branches/4.0/MgDev/Portable/PortableUnmanagedApi/JavaApiEx/java.i
===================================================================
--- branches/4.0/MgDev/Portable/PortableUnmanagedApi/JavaApiEx/java.i	2026-11-05 01:03:45 UTC (rev 10266)
+++ branches/4.0/MgDev/Portable/PortableUnmanagedApi/JavaApiEx/java.i	2026-11-05 03:41:07 UTC (rev 10267)
@@ -91,7 +91,10 @@
 }
 %typemap(freearg) BYTE_ARRAY_OUT
 {
-    jenv->ReleaseByteArrayElements($input, bytes$argnum, JNI_COMMIT);
+    // Mode 0, not JNI_COMMIT: JNI_COMMIT copies back but does not free the buffer that
+    // GetByteArrayElements allocated, so every call that fills a byte[] (MgByteReader::Read
+    // above all) leaked one JVM-heap buffer of the array's length.
+    jenv->ReleaseByteArrayElements($input, bytes$argnum, 0);
 }
 %typemap(javain) BYTE_ARRAY_OUT "$javainput"
 



More information about the mapguide-commits mailing list