-
-
Notifications
You must be signed in to change notification settings - Fork 583
Description
Following from the discussion in #890 (comment) and #1711 (comment) ...
Sol's tutorials depict an approach to writing script bindings that can lead to heap corruption if those bindings are invoked from a coroutine, even if the coroutine is created and operated by scripts. This can come as a nasty surprise to library users who neglect to read the documentation subpages about threads because they aren't using sol's coroutine API.
The technical problem as I understand it — take this with a grain of salt:
Heap corruption can occur whether the coroutine is created and operated by scripts or the host app, so long as the host app either storessol::reference-based values somewhere in its own datamodel or (worse yet) passes native data types containing any type ofsol::referenceback to a script. Sol bindings written in the 'standard style' will tend to use thelua_Stateof the coroutine that invoked them, and create these references in a non-main registry. When the coroutine completes, that registry may be deleted or recycled, causing all of these references go out of scope. This invalidates thosesol::references; what happens next is undefined behavior but typically corrupts a heap or freelist causing a crash with unpredictable timing deep in the runtime.
This is a major pitfall, responsible for a number of issues on the repo. I think it could be resolved by adding a prominent item to Sol's table of contents with a title like "how to write coroutine-safe code".
This could either lead to an updated version of the thread page or to a new page written as a guide. Ideally it should explain when and why types like main_reference and this_main_state are necessary without venturing into topics like CPU threading.