Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing modules wthich don't use the Lua memory allocator #2303

Closed
TerryE opened this issue Mar 17, 2018 · 7 comments
Closed

Fixing modules wthich don't use the Lua memory allocator #2303

TerryE opened this issue Mar 17, 2018 · 7 comments
Assignees
Labels

Comments

@TerryE
Copy link
Collaborator

TerryE commented Mar 17, 2018

The following modules include bare malloc / free API calls: coap, crypto, enduser_setup, file, http, mqtt, net, pcm, rotary, sntp, sqlite3, tls, tm1829, u8g_glue, websocket, wifi, wifi_eventmon, wifi_monitor, ws2812.

The lua allocator should always be used in Lua modules. The reason for this is that the Lua allocator is a thin wrapper on top of the standard allocator, but handles out of memory conditions differently. If this occurs, the Lua system runs the GC and then retries the allocation. It only fails is there there is not enough memory at this point. Also instead of returning a null pointer, a Lua error is thrown.

Expected behavior

Libraries should not raise a memory allocation error if the EGC mode ON_ALLOC_FAILURE is set and enough memory is available after GC to satisfy the allocation.

Actual behavior

The application can crash with an out of memory error even though there is enough heap available to satisfay the request.

NodeMCU version

master and dev

@pjsg
Copy link
Member

pjsg commented Mar 17, 2018

I'm thinking that the easiest (although nasty) approach to this is to define a malloc/free wrapper over the lua allocator which allocates using the lua allocator, then stuffs the block into a table indexed by the memory address of the userdata. Then on free we can find the block in our table and remove that entry. I think that would preserve the correct semantics (apart from the out-of-memory condition when malloc would return NULL, and this approach would panic the system. This may not be bad as I bet that most places don't handle the out of memory condition gracefully)

@pjsg
Copy link
Member

pjsg commented Mar 17, 2018

Of course, this still leaves the espressif core libraries which will continue to use their own malloc/free and maybe those do get it right.

@TerryE
Copy link
Collaborator Author

TerryE commented Mar 17, 2018

If you look at the code in mem.c the lua mem wrapper does end up doing an os_malloc without any framing record. It's just that if the malloc return NULL then it executes the GC and retries before dying. If the malloc are a one-off at library initialisation (which is the case in some of the libraries, then this isn't so much of an issue. (an this is mostly what the SDK routines do). It's when the modules do dynamic malloc/free calls that you can get the potential problems.

Oh, yes, whilst I think on the othe tweek that I would do would be to change the panic handler so that instead of just rebooting on stopping an uncaught error. it prints out the error traceback first so at least the developer can see where the uncaught error occurted.

@stale
Copy link

stale bot commented Jul 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 21, 2019
@TerryE TerryE self-assigned this Jul 21, 2019
@stale stale bot removed the stale label Jul 21, 2019
@stale
Copy link

stale bot commented Jul 16, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 16, 2020
@TerryE TerryE removed the stale label Jul 16, 2020
@nwf
Copy link
Member

nwf commented Jul 18, 2020

Just some updates to note:

LwIP, unfortunately, uses raw malloc and friends internally (rather than having us pass it memory; a real anti-pattern in C libraries, but one so heavily entrenched as to require a time machine to undo), so tls is stuck with one call to free. The other LwIP-adjacent modules are in similar boats.

@stale
Copy link

stale bot commented Jul 21, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 21, 2021
@stale stale bot closed this as completed Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants