HaxeFoundation / hxcpp

Runtime files for c++ backend for haxe
Other
295 stars 188 forks source link

StaticCast to Array<Dynamic> fails? Null-pointer exception... #502

Closed vizanto closed 7 years ago

vizanto commented 8 years ago

Using Haxe nightly and hxcpp-git (and also with Haxe 3.3.0-RC1) we're getting a strange null pointer on startup:

I've been digging around using lldb to find the cause of this:

2016-08-26 13:53:08.395 dolphy[92871:1788179] 13:53:08.394 WARNING:  140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
DynamicTextField.hx:91: Warning: Could not find required font "Arial", it has not been embedded
Process 92871 stopped
* thread #1: tid = 0x1b4913, 0x000000010001d8bc dolphy`hx::EnumBase_obj::_hx_getIndex(this=0x0000003500000029) const + 12 at Enum.h:101, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x3500000045)
    frame #0: 0x000000010001d8bc dolphy`hx::EnumBase_obj::_hx_getIndex(this=0x0000003500000029) const + 12 at Enum.h:101
   98
   99
   100        String _hx_getTag() const { return _hx_tag; }
-> 101        int _hx_getIndex() const { return index; }
   102        #else
   103        Dynamic __Param(int inID) { return mArgs[inID]; }
   104        DynamicArray __EnumParams() { return mArgs; }

So a null pointer to an Enum is being dereferenced...

Jumping up to frames to get into the calling function:

(lldb) frame select 2
frame #2: 0x000000010110eb46 dolphy`format::swf::lite::StaticTextField_obj::renderGlyph(this=0x0000000118487c74, font=format::swf::lite::symbols::FontSymbol @ 0x00007fff5fbfa7f8, character=18, color=-12567489, scale=0.0094238281250000003, offsetX=0, offsetY=9) + 438 at StaticTextField.cpp:143
   140  HXDLIN(  63)        while((_g < _g1->length)){
   141  HXLINE(  63)            HX_VARI(  ::format::swf::exporters::core::ShapeCommand,command) = _g1->__get(_g).StaticCast<  ::format::swf::exporters::core::ShapeCommand >();
   142  HXDLIN(  63)            ++_g;
-> 143  HXLINE(  65)            switch((int)(_hx_getEnumValueIndex(command))){
   144                              case (int)1: {
   145  HXLINE(  69)                     ::openfl::display::Graphics _hx_tmp = this->get_graphics();
   146  HXDLIN(  69)                    _hx_tmp->beginFill(((int)color & (int)(int)16777215),((Float)((int)((int)color >> (int)(int)24) & (int)(int)255) / (Float)(int)255));

This is the loop generated by openfl/swf that switches on enum values.

For context I've added some more of the generated code here:

void StaticTextField_obj::renderGlyph( ::format::swf::lite::symbols::FontSymbol font,Int character,Int color,Float scale,Float offsetX,Float offsetY){
                HX_STACK_FRAME("format.swf.lite.StaticTextField","renderGlyph",0x858549aa,"format.swf.lite.StaticTextField.renderGlyph","format/swf/lite/StaticTextField.hx",63,0xc1c4c25b)
                HX_STACK_THIS(this)
                HX_STACK_ARG(font,"font")
                HX_STACK_ARG(character,"character")
                HX_STACK_ARG(color,"color")
                HX_STACK_ARG(scale,"scale")
                HX_STACK_ARG(offsetX,"offsetX")
                HX_STACK_ARG(offsetY,"offsetY")
HXLINE(  63)        HX_VARI( Int,_g) = (int)0;
HXDLIN(  63)        HX_VARI( ::Array< ::Dynamic>,_g1) = font->glyphs->__get(character).StaticCast< ::Array< ::Dynamic> >();
HXDLIN(  63)        while((_g < _g1->length)){
HXLINE(  63)            HX_VARI(  ::format::swf::exporters::core::ShapeCommand,command) = _g1->__get(_g).StaticCast<  ::format::swf::exporters::core::ShapeCommand >();
HXDLIN(  63)            ++_g;
HXLINE(  65)            switch((int)(_hx_getEnumValueIndex(command))){
                            case (int)1: {
HXLINE(  69)                     ::openfl::display::Graphics _hx_tmp = this->get_graphics();
HXDLIN(  69)                    _hx_tmp->beginFill(((int)color & (int)(int)16777215),((Float)((int)((int)color >> (int)(int)24) & (int)(int)255) / (Float)(int)255));
                            }
                            break;
                            case (int)3: {

Digging around the stack, you can see StaticCast changes the mPtr location:

(lldb) expr _g1
(Array<Dynamic>) $15 = {
  hx::ObjectPtr<Array_obj<Dynamic> > = {
    mPtr = 0x00000001176c0860
  }
}
(lldb) expr ((::Array<::Dynamic>) font->glyphs->__get( character ))->__get(_g)
(Dynamic) $16 = {
  hx::ObjectPtr<hx::Object> = (mPtr = 0x00000001176c0d20)
}

Calling toString() reveals the enum does exist:

(lldb) expr _g
(Int) $6 = 2
(lldb) expr (font->glyphs->__get( character ))->toString()
(String) $5 = (length = 1161, __s = "[LineStyle(null,null,null,null,null,null,null,null),EndFill,BeginFill(16777215,1),MoveTo(46,-235.5),LineTo(46,-235.5),CurveTo(48,-162,86.5,-103.5),CurveTo(124.5,-45.5,192,-16.5),CurveTo(259,12.5,358.5,12.5),CurveTo(437,12.5,500,-16),CurveTo(562.5,-45,596,-96.5),CurveTo(629.5,-148.5,629.5,-207),CurveTo(629.5,-266,599,-311),CurveTo(568.5,-356.5,504.5,-386),CurveTo(460.5,-406,342.5,-432.5),CurveTo(224,-459.5,196,-485),CurveTo(167.5,-510.5,167.5,-550),CurveTo(167.5,-595.5,208,-627.5),CurveTo(248,-660,336,-660),CurveTo(420.5,-660,464,-624.5),CurveTo(507,-589,514.5,-519.5),LineTo(607.5,-526.5),CurveTo(605,-591,571.5,-642),CurveTo(538,-693,476,-719),CurveTo(413.5,-745.5,332,-745.5),CurveTo(258,-745.5,197.5,-720.5),CurveTo(137,-695.5,105.5,-647),CurveTo(74,-599,74,-543.5),CurveTo(74,-493,100,-452),CurveTo(125.5,-411.5,178,-384),CurveTo(218.5,-362.5,319.5,-338),CurveTo(420.5,-314,450,-302.5),CurveTo(496,-285,516,-259),CurveTo(536,-233.5,536,-199),CurveTo(536,-165,515.5,-136),CurveTo(494.5,-107.5,452,-91),CurveTo(409.")
(lldb) expr ((::Array<::Dynamic>) font->glyphs->__get( character ))->__get(_g)->toString()
(String) $17 = (length = 21, __s = "BeginFill(16777215,1)")

But doesn't with the pointer that was StaticCast:

(lldb) expr _g1->toString()
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x102801310).
The process has been returned to the state before expression evaluation.

Am I way off here or is StaticCast broken? @hughsando :)

hughsando commented 8 years ago

I'm guessing that font->glyphs->__get(character) is returning a cpp:VirtualArray rather than an Array. If you can expand the vtable of _g1, you will be able to tell exactly what type of variable it is.

Quickly looking at the swflite code - it seems to be strongly typed (from what I can see - unless there is some serialization going on somewhere) so I think I would need a project to reproduce this.

Another possibility would be to try an overnight build, since RC1 is getting a bit old now, eg: http://hxbuilds.s3-website-us-east-1.amazonaws.com/builds/haxe/index.html

On Fri, Aug 26, 2016 at 8:12 PM, Simon Krajewski notifications@github.com wrote:

Assigned #502 https://github.com/HaxeFoundation/hxcpp/issues/502 to @hughsando https://github.com/hughsando.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/HaxeFoundation/hxcpp/issues/502#event-768824739, or mute the thread https://github.com/notifications/unsubscribe-auth/ABlp1mxRIcPrJifbh3t_Q4q-H-0j4I-kks5qjtgggaJpZM4JuBCV .

vizanto commented 8 years ago

Nightly versions haxe-3.3.0-rc1-dev-0558013 until and including latest haxe-3.3.0-rc1-dev-05abf4c fail to compile:

Error: While running :xcrun --sdk macosx10.11 clang++ -I/Users/danny/Development/Laerdal/export_openfl/mac64/cpp/debug/obj/obj/darwin64-debug/__pch/haxe -Iinclude -c -fvisibility=hidden -stdlib=libstdc++ -g -I/usr/local/lib/haxe/lib/hxcpp/git/include -DHX_MACOS -m64 -Wno-parentheses -Wno-null-dereference -Wno-unused-value -Wno-format-extra-args -Wno-bool-conversion -DHXCPP_DEBUG -DHXCPP_M64 -DHXCPP_VISIT_ALLOCS -DHXCPP_API_LEVEL=330 -stdlib=libc++ -x c++ -frtti -Wno-invalid-offsetof ./src/openfl/display3D/Program3D.cpp -o/Users/danny/Development/Laerdal/export_openfl/mac64/cpp/debug/obj/obj/darwin64-debug/18ff17e6_Program3D.o
./src/openfl/display3D/Program3D.cpp:176:72: error: cannot cast from type '::Dynamic' to pointer type '::hx::Object *'
HXDLIN( 102)                            HX_VARI(  ::Dynamic,result) = Dynamic(hx::DynamicPtr(( ( ::hx::Object *)(this2(hx::DynamicPtr(this->_hx___programID->id),i)) )));
                                                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

@Simn is this or similar bug already reported somewhere?

vizanto commented 8 years ago

With the last haxe-3.3.0-rc1-dev-3e5c08d nightly that compiles, I have tried to get the vtable information, but am unsure if this is what you mean:

(lldb) expr _g1
(Array<Dynamic>) $0 = {
  hx::ObjectPtr<Array_obj<Dynamic> > = {
    mPtr = 0x0000000117cfb410
  }
}
(lldb) expr font->glyphs->__get(character)
(Dynamic) $1 = {
  hx::ObjectPtr<hx::Object> = (mPtr = 0x0000000117cfb410)
}
(lldb) expr font->glyphs->__get(character)->__GetType()
(int) $2 = 5
(lldb) expr font->glyphs->__get(character)->__GetRealObject()->__GetType()
(int) $7 = 5

(lldb) expr _g1->__GetType()
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x102801790).
The process has been returned to the state before expression evaluation.
hughsando commented 8 years ago

the vtable on visual studio is easy - you can just expand the little crosses, and it shows which class the functions are implemented in.

On lldb, I guess it would be a matter of printing the "mPtr" field - but I'm not sure if this shows the contents of the vtable.

On Mon, Aug 29, 2016 at 6:55 PM, Danny Wilson notifications@github.com wrote:

With the last haxe-3.3.0-rc1-dev-3e5c08d nightly that compiles, I have tried to get the vtable information, but am unsure if this is what you mean:

(lldb) expr _g1 (Array) $0 = { hx::ObjectPtr<Array_obj > = { mPtr = 0x0000000117cfb410 } } (lldb) expr font->glyphs->get(character) (Dynamic) $1 = { hx::ObjectPtrhx::Object = (mPtr = 0x0000000117cfb410) } (lldb) expr font->glyphs->get(character)->GetType() (int) $2 = 5 (lldb) expr font->glyphs->get(character)->GetRealObject()->GetType() (int) $7 = 5

(lldb) expr _g1->__GetType() error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x102801790). The process has been returned to the state before expression evaluation.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/HaxeFoundation/hxcpp/issues/502#issuecomment-243093645, or mute the thread https://github.com/notifications/unsubscribe-auth/ABlp1uuLBBSyqrUPQxjS3LYx4iLg42adks5qkrq7gaJpZM4JuBCV .

vizanto commented 8 years ago

Ah you mean this?

(lldb) expr font->glyphs->__get(character).mPtr
(cpp::VirtualArray_obj *) $2 = 0x00000001174fb410

(lldb) expr _g1.mPtr
(cpp::VirtualArray_obj *) $3 = 0x00000001174fb410
vizanto commented 8 years ago

Small update: latest nightlies seem to be very much in flux. Can't get the generated C++ to compile with seemingly different errors every other nightly build. :)

haxe-3.3.0-rc1-dev-09af1ee has issues with cpp.Pointer and friends.

hughsando commented 7 years ago

I think this should be fixed in 3.4.