1
0
Fork 0

Fix crash with Nasal bindings.

naBindFunction doesn't save the function code to the global
hash, so pass an explicit context to various 'call' overloads so
the function can't be GC-ed in between parsing and calling.

(Patch by Nicholas Scheel)

https://code.google.com/p/flightgear-bugs/issues/detail?id=1397
This commit is contained in:
James Turner 2014-04-15 14:13:46 +01:00
parent 740b3f35e9
commit b3c7cb7c15
2 changed files with 37 additions and 20 deletions

View file

@ -236,6 +236,11 @@ naRef FGNasalSys::call(naRef code, int argc, naRef* args, naRef locals)
return callMethod(code, naNil(), argc, args, locals); return callMethod(code, naNil(), argc, args, locals);
} }
naRef FGNasalSys::callWithContext(naContext ctx, naRef code, int argc, naRef* args, naRef locals)
{
return callMethodWithContext(ctx, code, naNil(), argc, args, locals);
}
// Does a naCall() in a new context. Wrapped here to make lock // Does a naCall() in a new context. Wrapped here to make lock
// tracking easier. Extension functions are called with the lock, but // tracking easier. Extension functions are called with the lock, but
// we have to release it before making a new naCall(). So rather than // we have to release it before making a new naCall(). So rather than
@ -248,6 +253,11 @@ naRef FGNasalSys::callMethod(naRef code, naRef self, int argc, naRef* args, naRe
return naCallMethod(code, self, argc, args, locals); return naCallMethod(code, self, argc, args, locals);
} }
naRef FGNasalSys::callMethodWithContext(naContext ctx, naRef code, naRef self, int argc, naRef* args, naRef locals)
{
return naCallMethodCtx(ctx, code, self, argc, args, locals);
}
FGNasalSys::~FGNasalSys() FGNasalSys::~FGNasalSys()
{ {
if (_inited) { if (_inited) {
@ -258,11 +268,15 @@ FGNasalSys::~FGNasalSys()
bool FGNasalSys::parseAndRun(const char* sourceCode) bool FGNasalSys::parseAndRun(const char* sourceCode)
{ {
naRef code = parse("FGNasalSys::parseAndRun()", sourceCode, naContext ctx = naNewContext();
naRef code = parse(ctx, "FGNasalSys::parseAndRun()", sourceCode,
strlen(sourceCode)); strlen(sourceCode));
if(naIsNil(code)) if(naIsNil(code)) {
naFreeContext(ctx);
return false; return false;
call(code, 0, 0, naNil()); }
callWithContext(ctx, code, 0, 0, naNil());
naFreeContext(ctx);
return true; return true;
} }
@ -1072,11 +1086,13 @@ bool FGNasalSys::createModule(const char* moduleName, const char* fileName,
const SGPropertyNode* cmdarg, const SGPropertyNode* cmdarg,
int argc, naRef* args) int argc, naRef* args)
{ {
naRef code = parse(fileName, src, len);
if(naIsNil(code))
return false;
naContext ctx = naNewContext(); naContext ctx = naNewContext();
naRef code = parse(ctx, fileName, src, len);
if(naIsNil(code)) {
naFreeContext(ctx);
return false;
}
// See if we already have a module hash to use. This allows the // See if we already have a module hash to use. This allows the
// user to, for example, add functions to the built-in math // user to, for example, add functions to the built-in math
@ -1089,7 +1105,7 @@ bool FGNasalSys::createModule(const char* moduleName, const char* fileName,
_cmdArg = (SGPropertyNode*)cmdarg; _cmdArg = (SGPropertyNode*)cmdarg;
call(code, argc, args, locals); callWithContext(ctx, code, argc, args, locals);
hashset(_globals, moduleName, locals); hashset(_globals, moduleName, locals);
naFreeContext(ctx); naFreeContext(ctx);
@ -1111,10 +1127,9 @@ void FGNasalSys::deleteModule(const char* moduleName)
naFreeContext(ctx); naFreeContext(ctx);
} }
naRef FGNasalSys::parse(const char* filename, const char* buf, int len) naRef FGNasalSys::parse(naContext ctx, const char* filename, const char* buf, int len)
{ {
int errLine = -1; int errLine = -1;
naContext ctx = naNewContext();
naRef srcfile = naNewString(ctx); naRef srcfile = naNewString(ctx);
naStr_fromdata(srcfile, (char*)filename, strlen(filename)); naStr_fromdata(srcfile, (char*)filename, strlen(filename));
naRef code = naParseCode(ctx, srcfile, 1, (char*)buf, len, &errLine); naRef code = naParseCode(ctx, srcfile, 1, (char*)buf, len, &errLine);
@ -1122,14 +1137,11 @@ naRef FGNasalSys::parse(const char* filename, const char* buf, int len)
SG_LOG(SG_NASAL, SG_ALERT, SG_LOG(SG_NASAL, SG_ALERT,
"Nasal parse error: " << naGetError(ctx) << "Nasal parse error: " << naGetError(ctx) <<
" in "<< filename <<", line " << errLine); " in "<< filename <<", line " << errLine);
naFreeContext(ctx);
return naNil(); return naNil();
} }
// Bind to the global namespace before returning // Bind to the global namespace before returning
naRef bound = naBindFunction(ctx, code, _globals); return naBindFunction(ctx, code, _globals);
naFreeContext(ctx);
return bound;
} }
bool FGNasalSys::handleCommand( const char* moduleName, bool FGNasalSys::handleCommand( const char* moduleName,
@ -1137,22 +1149,24 @@ bool FGNasalSys::handleCommand( const char* moduleName,
const char* src, const char* src,
const SGPropertyNode* arg ) const SGPropertyNode* arg )
{ {
naRef code = parse(fileName, src, strlen(src)); naContext ctx = naNewContext();
if(naIsNil(code)) return false; naRef code = parse(ctx, fileName, src, strlen(src));
if(naIsNil(code)) {
naFreeContext(ctx);
return false;
}
// Commands can be run "in" a module. Make sure that module // Commands can be run "in" a module. Make sure that module
// exists, and set it up as the local variables hash for the // exists, and set it up as the local variables hash for the
// command. // command.
naRef locals = naNil(); naRef locals = naNil();
if(moduleName[0]) { if(moduleName[0]) {
naContext ctx = naNewContext();
naRef modname = naNewString(ctx); naRef modname = naNewString(ctx);
naStr_fromdata(modname, (char*)moduleName, strlen(moduleName)); naStr_fromdata(modname, (char*)moduleName, strlen(moduleName));
if(!naHash_get(_globals, modname, &locals)) { if(!naHash_get(_globals, modname, &locals)) {
locals = naNewHash(ctx); locals = naNewHash(ctx);
naHash_set(_globals, modname, locals); naHash_set(_globals, modname, locals);
} }
naFreeContext(ctx);
} }
// Cache this command's argument for inspection via cmdarg(). For // Cache this command's argument for inspection via cmdarg(). For
@ -1160,7 +1174,8 @@ bool FGNasalSys::handleCommand( const char* moduleName,
// code doesn't need it. // code doesn't need it.
_cmdArg = (SGPropertyNode*)arg; _cmdArg = (SGPropertyNode*)arg;
call(code, 0, 0, locals); callWithContext(ctx, code, 0, 0, locals);
naFreeContext(ctx);
return true; return true;
} }

View file

@ -102,8 +102,10 @@ public:
void globalsSet(const char* key, naRef val); void globalsSet(const char* key, naRef val);
naRef call(naRef code, int argc, naRef* args, naRef locals); naRef call(naRef code, int argc, naRef* args, naRef locals);
naRef callWithContext(naContext ctx, naRef code, int argc, naRef* args, naRef locals);
naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals); naRef callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals);
naRef callMethodWithContext(naContext ctx, naRef code, naRef self, int argc, naRef* args, naRef locals);
naRef propNodeGhost(SGPropertyNode* handle); naRef propNodeGhost(SGPropertyNode* handle);
@ -169,7 +171,7 @@ private:
void loadScriptDirectory(simgear::Dir nasalDir); void loadScriptDirectory(simgear::Dir nasalDir);
void addModule(std::string moduleName, simgear::PathList scripts); void addModule(std::string moduleName, simgear::PathList scripts);
static void logError(naContext); static void logError(naContext);
naRef parse(const char* filename, const char* buf, int len); naRef parse(naContext ctx, const char* filename, const char* buf, int len);
naRef genPropsModule(); naRef genPropsModule();
bool _inited; bool _inited;