wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.9k stars 552 forks source link

Segfault on OOM Failure? #692

Open ghost opened 5 years ago

ghost commented 5 years ago

I wanted to test what happened when wren was pushed to it's limits so I wrote the following script, test.wren:

var l = ["banana"]
while (true) {
    l.add("banana")
}

to generate out-of-memory conditions for the CLI VM.

Running the script on a fresh build of the CLI I get:

$ ./wren test.wren 
Segmentation fault (core dumped)

I would expect something like Java's OutOfMemoryError or Python's MemoryError to bubble up to the user, or at the very least have a somewhat graceful abort, rather than a segmentation fault.

Tested on:

mhermier commented 5 years ago

Error is trivial, code doesn't check if list's array gets properly resized.

ruby0x1 commented 5 years ago

We can definitely add an OOM error in debug mode! It has interesting implications when the user is the one in control of the allocations, i.e if the config is specifying a realloc function where exactly the check is handled and how it propagates to the user is an unknown.

mhermier commented 5 years ago

Not sure if it is the right way to think. I mean it is an error that is expected to happen at run-time, unless you can always control what you feed the system with. So considering a debugging feature is an error to me. That said handling it gracefully is a huge topic, that might imply we way need to rethink error handling in the VM.

ghost commented 5 years ago

Given that users can use their own allocator, potential OOM error could be propigated back up the call stack for every bad alloc or allocations could call some sort of user-defined handler whenever there is bad alloc.

I've seen both methods used in library code. The former requires a lot of boilerplate error handling code, which can get complex and a bit confusing. The latter allows you to assume allocs are good and just defer to a handler otherwise, perhaps some sort of setjmp/longjump mechanism, but can lead to memory leaks unless there is a way to cleanup resources.

I'm not sure if either of these might be good ways to handle OOM or other panic-worthy conditions, but they're ideas none the less.

mhermier commented 5 years ago

I thought about set/longjmp since a while. But I wonder about portability and possible errors due to partially initialised values.