Identification of malloc and free

26 views
Skip to first unread message

NgulaDE

unread,
Jul 8, 2024, 11:56:55 AMJul 8
to emscripten-discuss
Hey, I am working on a patcher for Wasm.
Is there a way to identify the malloc and the free function of the dlmalloc and emmalloc allocator? My supervisor says that malloc prints an error message, such as "out of memory", "allocation failed" or "insufficient memory", if memory allocation via malloc fails. Supposedly, these strings will be stored in the data section on the linear memory. In some wasm modules, i saw these error messages stored in the data section, but I could not quite figure out how these strings are printed nor how they are accessed. 

Thank you

Sam Clegg

unread,
Jul 8, 2024, 3:36:28 PMJul 8
to emscripte...@googlegroups.com
Hi Ngula,

In emscripten the allocators such as dlmalloc and emmalloc use `sbrk()` when they run out of memory.   `sbrk()` then calls `emscripten_resize_heap`, which by default will abort with an error message here: https://github.com/emscripten-core/emscripten/blob/b3c25673f53974ff1f30df43701dc63af7cbbdc4/src/library.js#L280-L290.  So the error messages in this case live in the JavaScript, not in the wasm module.

This behaviour is affected by the `-sABORTTING_MALLOC` setting though.   If you disable this setting then you don't get any kind of error message and malloc simply returns zero.

cheers,
sam



--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/01d093c1-fb3c-497d-88c9-8a48cd4ec975n%40googlegroups.com.

NgulaDE

unread,
Jul 9, 2024, 5:28:32 AMJul 9
to emscripten-discuss
Thank you so much for the answer. 
Does dlmalloc and emmalloc still use sbrk and emscripten_resize_heap if -sABORTTING_MALLOC is disabled? Because I need an approach that does not depend on any emscripten options or flags and that still detects malloc and free.
Another idea is to look out for the memory.size instruction. I think both, dlmalloc and emmalloc, will call memory.size within sbrk (corrrect me, if I am wrong) to identify, if there is enough memory.


Or maybe you have any other ideas how I can reliably detect malloc and free :)

Sam Clegg

unread,
Jul 9, 2024, 3:37:54 PMJul 9
to emscripte...@googlegroups.com
On Tue, Jul 9, 2024 at 2:28 AM NgulaDE <nguy...@gmail.com> wrote:
Thank you so much for the answer. 
Does dlmalloc and emmalloc still use sbrk and emscripten_resize_heap if -sABORTTING_MALLOC is disabled? Because I need an approach that does not depend on any emscripten options or flags and that still detects malloc and free.
Another idea is to look out for the memory.size instruction. I think both, dlmalloc and emmalloc, will call memory.size within sbrk (corrrect me, if I am wrong) to identify, if there is enough memory.

Yes,  emscripten_resize_heap is always used to attempt to grow the heap whichever allocator is used and whichever flags are set.   If that function fails it means the memory could not be grown and the malloc call will fail.


Or maybe you have any other ideas how I can reliably detect malloc and free :)

s...@google.com schrieb am Montag, 8. Juli 2024 um 21:36:28 UTC+2:
Hi Ngula,

In emscripten the allocators such as dlmalloc and emmalloc use `sbrk()` when they run out of memory.   `sbrk()` then calls `emscripten_resize_heap`, which by default will abort with an error message here: https://github.com/emscripten-core/emscripten/blob/b3c25673f53974ff1f30df43701dc63af7cbbdc4/src/library.js#L280-L290.  So the error messages in this case live in the JavaScript, not in the wasm module.

This behaviour is affected by the `-sABORTTING_MALLOC` setting though.   If you disable this setting then you don't get any kind of error message and malloc simply returns zero.

cheers,
sam



On Mon, Jul 8, 2024 at 8:56 AM NgulaDE <nguy...@gmail.com> wrote:
Hey, I am working on a patcher for Wasm.
Is there a way to identify the malloc and the free function of the dlmalloc and emmalloc allocator? My supervisor says that malloc prints an error message, such as "out of memory", "allocation failed" or "insufficient memory", if memory allocation via malloc fails. Supposedly, these strings will be stored in the data section on the linear memory. In some wasm modules, i saw these error messages stored in the data section, but I could not quite figure out how these strings are printed nor how they are accessed. 

Thank you

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/01d093c1-fb3c-497d-88c9-8a48cd4ec975n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages