Mosquitto version: 2.0.11, 2.0.20
Platform: 32-bit ARM running Debian Stable (Bookworm)
Bug: The value reported via $SYS/broker/heap/current gradually increases when the broker is receiving MQTT v5 publish with properties, despite the memory usage for mosquitto reported by ps not increasing.
Having tracked this down, this is due to the mosquitto_property_add_*() family of allocating the mosquitto_property struct using mosquitto__calloc() (which adds the usable size of the allocation to memcount) but property__free() (called by e.g. mosquitto_property_free_all() ) using a direct call to free(), and thus not removing the usable size from memcount.
Thus, if one makes use of the memory_limit configuration option, one eventually runs into the allocator functions incorrectly refusing to allocate memory.
Expected behaviour: $SYS/broker/heap/current should gradually increase over time when the actual memory usage of the mosquitto process is not increasing.
The incorrect call to free() was located using a custom-written preloaded shared object to intercept the C library allocation/free calls, track the memory allocations, verify that there are indeed no leaks, but also check how they are called (e.g. from where in the binary, or in the case of the tail-called free(), whether ARM register R2 contains the pointer to memcount) and print this information. This was then followed by analysis of the assembly and C code to validate the results.
Mosquitto version: 2.0.11, 2.0.20
Platform: 32-bit ARM running Debian Stable (Bookworm)
Bug: The value reported via
$SYS/broker/heap/currentgradually increases when the broker is receiving MQTT v5 publish with properties, despite the memory usage for mosquitto reported bypsnot increasing.Having tracked this down, this is due to the
mosquitto_property_add_*()family of allocating themosquitto_propertystruct usingmosquitto__calloc()(which adds the usable size of the allocation tomemcount) butproperty__free()(called by e.g.mosquitto_property_free_all()) using a direct call tofree(), and thus not removing the usable size frommemcount.Thus, if one makes use of the
memory_limitconfiguration option, one eventually runs into the allocator functions incorrectly refusing to allocate memory.Expected behaviour:
$SYS/broker/heap/currentshould gradually increase over time when the actual memory usage of the mosquitto process is not increasing.The incorrect call to
free()was located using a custom-written preloaded shared object to intercept the C library allocation/free calls, track the memory allocations, verify that there are indeed no leaks, but also check how they are called (e.g. from where in the binary, or in the case of the tail-calledfree(), whether ARM register R2 contains the pointer to memcount) and print this information. This was then followed by analysis of the assembly and C code to validate the results.