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

Segmentation fault when trying to create foreign class #1051

Open iacore opened 3 years ago

iacore commented 3 years ago

Currently, when the bind foreign class callback returns { NULL, NULL } like the following:

WrenForeignClassMethods APITest_bindForeignClass(
    WrenVM* vm, const char* module, const char* className)
{
  WrenForeignClassMethods methods = { NULL, NULL };
  return methods;
}

Any attempt to create a class in Wren will result in segmentation fault.

In https://wren.io/embedding/storing-c-data.html, the code example given does just that.

I think Wren should throw an error about foreign class not defined when allocateFn is NULL.

I also suspect that Wren might crash when a foreign class's finalizeFn is NULL, but I haven't tried that.

iacore commented 3 years ago

To reproduce it, simply replace the above function in test/api/api_tests.c and run:

> bin/wren_test test/api/foreign_class.wren
fish: Job 1, 'bin/wren_test test/api/foreign_…' terminated by signal SIGSEGV (Address boundary error)
mhermier commented 3 years ago

Should trigger an error/assert in debug mode.

iacore commented 3 years ago

Should trigger an error/assert in debug mode.

I simply ran make to build the static library.

Anyway, I think { NULL, _ } should mean no foreign class is found. The current behavior is inconsistent with bindForeignMethodFn, where returning NULL means no method is found and raise an error.

CrazyInfin8 commented 3 years ago

Possible duplicate of #811

Should trigger an error/assert in debug mode.


foreign class A {
construct new() {}
}

var a = A.new()


![image](https://user-images.githubusercontent.com/14926524/132105810-5a3e55aa-8191-46f6-9f32-52d25a781b80.png)