snmalloc icon indicating copy to clipboard operation
snmalloc copied to clipboard

Delete pagemap allocation

Open NeilMonday opened this issue 8 months ago • 4 comments

We are hitting a situation where the FlatPageMap's body allocation seems to be leaking in an unusual way. We use snmalloc in a dll, and when the dll gets loaded and the first allocation is made, FlatPageMap::init reserves memory for the body member. This allocation for the pagemap is in addition to the actual allocation that was needed for the malloc/new. When the dll is unloaded, it seems that the pagemap allocation never gets freed. Loading and unloading the dll multiple times can cause these pagemap allocations leftover from previous loads to get out of hand and eventually prevent snmalloc from initializing.

Hopefully my explanation makes sense. Is there some concept that I am missing? If I understood this correctly, do you have any suggestion for freeing the pagemap allocation?

NeilMonday avatar Apr 10 '25 20:04 NeilMonday

This is where the memory for the pagemap gets reserved: https://github.com/microsoft/snmalloc/blob/b8e28be14b3fd98e27c2fe87c0296570f6d3990e/src/snmalloc/ds/pagemap.h#L206

NeilMonday avatar Apr 10 '25 20:04 NeilMonday

@mjp41 do you have any suggestions on this issue? Thanks.

NeilMonday avatar Apr 14 '25 13:04 NeilMonday

Hi @NeilMonday, so we never really designed snmalloc to be unloaded. We don't track enough state to do this. I think this would need a few additions

  • PAL - This currently only supports reserving memory, it would need extending with VirtualDealloc/munmap
  • We don't currently track all the memory regions that snmalloc is using in a concise way.
  • There aren't currently paths for returning memory from the global buddy allocator back to the OS.
  • There is no path to dealloc the pagemap (this is the problem you have observed.)

I think it would be a reasonable amount of work to do this. We could do an OnDestruct for the pagemap, like you added for the exception handler, but we'd have to be very careful about what happens during teardown as the pagemap is often assumed to exist.

mjp41 avatar Apr 14 '25 14:04 mjp41

Perhaps the so-far-only-on-CHERI authmap could be useful here (though it is another thing that needs careful handling during teardown!), as it might be useful as the start of an answer to "We don't currently track all the memory regions that snmalloc is using in a concise way."

nwf avatar Apr 14 '25 16:04 nwf

#773 completed for Windows.

mjp41 avatar Jul 01 '25 09:07 mjp41