Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Segmentation fault - void asCScriptEngine::CallObjectMethod(...)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 30 May 2012 - 12:20 PM

Hi, I'm getting a segmentation fault here:

void asCScriptEngine::CallObjectMethod(...)
{
.....f(obj, param);			  // <- line 3373 - causes segmentation fault
}


"f" comes out with some weird value.

This function is being called by the asCScriptEngine::WriteMessage(...). I'm registering my callback function like this:
void scriptInterface::setMessageCallback(asIScriptEngine* compiler)
{
compiler->SetMessageCallback( asMETHOD(scriptInterface,MessageCallback),this,asCALL_THISCALL );
}



virtual void scriptInterface::MessageCallback(const asSMessageInfo *msg)
{
// Implement a simple message callback function
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";

printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}



I have the feeling that the problem is here (file angelscript.h line 1024)

// Template specialization
template <>
struct asSMethodPtr<SINGLE_PTR_SIZE>
{
template<class M>
static asSFuncPtr Convert(M Mthd)
{
  asSFuncPtr p;
  asMemClear(&p, sizeof(p));
  asMemCopy(&p, &Mthd, SINGLE_PTR_SIZE); //<- after this, "f" (inside "p") gets wrong value
  // Mark this as a class method
  p.flag = 3;
  return p;
}
};

To make things even weirder, everything works as expected under Windows XP. I'm getting this error under Linux (Ubuntu) - using Code::Blocks as IDE.

Any ideas?

Carlos

Sponsor:

#2 behc   Members   -  Reputation: 623

Like
0Likes
Like

Posted 30 May 2012 - 01:11 PM

Did you try with non virtual version of MessageCallback method?

#3 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 30 May 2012 - 02:18 PM

Just tried - under Linux it only works with the non-virtual version.... but under Windows it works with the virtual version too.

#4 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 30 May 2012 - 03:50 PM

It might be that when taking the address of a virtual method from within the actual class GNUC is returning the method pointer in a different way.

Can you check what the difference is between the following two ways of doing it:




void global_setMessageCallback(scriptInteface *obj, asIScriptEngine * compiler)
{
  compiler->SetMessageCallback( asMETHOD(scriptInterface,MessageCallback),obj,asCALL_THISCALL );
}

void scriptInterface::setMessageCallback(asIScriptEngine* compiler){  // Get the method from within the class  compiler->SetMessageCallback( asMETHOD(scriptInterface,MessageCallback),this,asCALL_THISCALL );  // Get the method from a global function  global_setMessageCallback(this, compiler);}

If there is a difference between these two ways of doing it, there has to be a way of identifying that difference so the call can be made correctly anyway.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#5 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 01 June 2012 - 11:00 AM

Just tried. Exactly the same behaviour. Any other idea?

#6 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 01 June 2012 - 11:35 AM

Interesting..... when the callback function is not virtual, the

static asSFuncPtr Convert (M Mthd)

method (angelscript.h line 1029) is getting the Mthd value as :

(void(scriptInterface::*)(scriptInterface *const)) 0x8068370 <scriptInterface::MessageCallback(asSMessageInfo const*)>

But when the callback function is virtual (even if it hasn´t been overridden) is passed as:

&virtual scriptInterface::MessageCallback(asSMessageInfo const*)

(as reported by code::blocks IDE)

Is that ok?

Edited by Tzarls, 01 June 2012 - 11:42 AM.


#7 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 01 June 2012 - 04:52 PM

On gnuc a class method pointer has the following structure

struct {
    union {
        void *funcPtr;
        int  virtualFunctionIndex;
    };
    int baseOffset;
};

If the method is a virtual method, then union will have its least significant bit set, i.e. (int(funcPtr) & 1) will be true, otherwise it will be false, meaning that the funcPtr is the absolute address of the function that will be called.

If the class for the method pointer doesn't have multiple base classes, the baseOffset will always be 0, otherwise it will be the offset that must be added to the 'this' pointer to point to the correct base class when calling the method.

When calling a virtual method, the correct address of the function must be looked up in the virtual function table. The pointer to the virtual function table is stored as the first pointer in the class, i.e. you get to it by derefencing the this pointer, then you get the index into this table by dividing the virtualFunctionIndex by 4.

Of course, if the this pointer is corrupt, or if the first pointer in the class has been overwritten, for example by a buffer overflow or other memory invasion, then the function pointer retrieved from the virtual function table will be incorrect and most likely crash the application.

With what you describe it seems your problem is the latter, i.e. the this pointer is either wrong, or the pointer to the virtual function table has been overwritten. As your message callback doesn't really use the object, the crash doesn't happen when the method is not virtual.


I suggest you check the address at the first position in the scriptInterface. If the address is different when you register the message callback, and when the segv happens, then you definitely have a memory issue in your code. If that is so, you should be able to set a memory breakpoint on that address to identify where that memory issue is occurring.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#8 Tzarls   Members   -  Reputation: 808

Like
1Likes
Like

Posted 01 June 2012 - 06:49 PM

Apparently this isn´t my fault :D

Here´s a minimal code that exhibits the same behaviour:

main.h :

#ifndef _MAIN_
#define _MAIN_
#include <cstdio>
#include <angelscript.h>
class msgClass
{
    public:
    /*virtual*/ void MessageCallback(const asSMessageInfo *msg)  // uncomment "virtual" and the app fails
    {
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
  type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
  type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
};
#endif

main.cpp :

#include "main.h"
int main (int argc, char ** argv)
{
    msgClass *msg = new msgClass;
// Create the script engine
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
if( engine == 0 )
{
  printf("Failed to create script engine.\n");
  return -1;
}
engine->SetMessageCallback( asMETHOD(msgClass,MessageCallback),msg,asCALL_THISCALL );
engine->WriteMessage("My Section", 11, 38, asMSGTYPE_INFORMATION, "Testing Message Callback" );
printf ("Done!");
return 0;
}


#9 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 01 June 2012 - 08:00 PM

It's good that you managed to narrow it down to this. Of course, it means there is a problem in AngelScript but I'm sure there is a solution. Hopefully it won't be too difficult to find.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#10 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 01 June 2012 - 08:13 PM

Nice! If you need any help just let me know.

#11 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 01 June 2012 - 08:44 PM

Thanks for the offer. I'll try to reproduce it on my mac first. If I don't succeed I'll definitely need help in figuring out how to make this work.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#12 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 06 June 2012 - 09:57 AM

Hi Andreas. Any success in finding the cause of the problem?

#13 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 06 June 2012 - 11:18 AM

Unfortunately I haven't had the time to look into this problem yet. I'll let you know when I do.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#14 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 09 June 2012 - 01:44 PM

I've fixed this in revision 1338.

Thanks,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#15 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 10 June 2012 - 09:59 AM

Nice, thanks! Can I ask what the problem was?

#16 Andreas Jonsson   Moderators   -  Reputation: 3199

Like
0Likes
Like

Posted 10 June 2012 - 10:56 AM

One of the internal functions for calling class methods didn't have the implementation to support virtual class methods. Somehow this had gone unnoticed for many years.

Here's the diff showing what was changed.

Regards,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#17 Tzarls   Members   -  Reputation: 808

Like
0Likes
Like

Posted 13 June 2012 - 01:02 PM

FWIW, I´ve just tested this fix in my original project and it works as expected. Thanks again!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS