[Mapguide-trac] [mapguide-trac] #2898: Java/PHP leaks byte arrays passed to MgByteReader

MapGuide Open Source trac_mapguide at osgeo.org
Wed Nov 4 19:41:10 PST 2026


#2898: Java/PHP leaks byte arrays passed to MgByteReader
---------------------+----------------------
 Reporter:  jng      |        Owner:  jng
     Type:  defect   |       Status:  closed
 Priority:  low      |    Milestone:  4.0.1
Component:  Web API  |      Version:
 Severity:  trivial  |   Resolution:  fixed
 Keywords:           |  External ID:
---------------------+----------------------
Changes (by jng):

 * resolution:   => fixed
 * status:  assigned => closed

Comment:

 In [changeset:"10267" 10267]:
 {{{#!CommitTicketReference repository="" revision="10267"
 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
 }}}
-- 
Ticket URL: <https://trac.osgeo.org/mapguide/ticket/2898#comment:1>
MapGuide Open Source <http://mapguide.osgeo.org/>
MapGuide Open Source Internals


More information about the mapguide-trac mailing list