Skip to content

Commit

Permalink
- allocate VMFunction's PrintableName from the ClassDataAllocator arena.
Browse files Browse the repository at this point in the history
This avoids execution order issues on shutdown. VMFunction should not use FString.
  • Loading branch information
coelckers committed May 27, 2023
1 parent 7d30f19 commit 213bdba
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion source/common/scripting/backend/codegen.cpp
Expand Up @@ -9151,7 +9151,7 @@ FxExpression *FxVMFunctionCall::Resolve(FCompileContext& ctx)
// [Player701] Catch attempts to call abstract functions directly at compile time
if (NoVirtual && Function->Variants[0].Implementation->VarFlags & VARF_Abstract)
{
ScriptPosition.Message(MSG_ERROR, "Cannot call abstract function %s", Function->Variants[0].Implementation->PrintableName.GetChars());
ScriptPosition.Message(MSG_ERROR, "Cannot call abstract function %s", Function->Variants[0].Implementation->PrintableName);
delete this;
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion source/common/scripting/backend/vmbuilder.cpp
Expand Up @@ -790,7 +790,7 @@ VMFunction *FFunctionBuildList::AddFunction(PNamespace *gnspc, const VersionInfo
it.PrintableName = name;
it.Function = new VMScriptFunction;
it.Function->Name = functype->SymbolName;
it.Function->PrintableName = name;
it.Function->PrintableName = ClassDataAllocator.Strdup(name);
it.Function->ImplicitArgs = functype->GetImplicitArgs();
it.Proto = nullptr;
it.FromDecorate = fromdecorate;
Expand Down
2 changes: 1 addition & 1 deletion source/common/scripting/core/imports.cpp
Expand Up @@ -198,7 +198,7 @@ void InitImports()
{
assert(afunc->VMPointer != NULL);
*(afunc->VMPointer) = new VMNativeFunction(afunc->Function, afunc->FuncName);
(*(afunc->VMPointer))->PrintableName.Format("%s.%s [Native]", afunc->ClassName+1, afunc->FuncName);
(*(afunc->VMPointer))->PrintableName = ClassDataAllocator.Strdup(FStringf("%s.%s [Native]", afunc->ClassName+1, afunc->FuncName));
(*(afunc->VMPointer))->DirectNativeCall = afunc->DirectNative;
AFTable.Push(*afunc);
});
Expand Down
2 changes: 1 addition & 1 deletion source/common/scripting/core/scopebarrier.cpp
Expand Up @@ -221,5 +221,5 @@ void FScopeBarrier::ValidateCall(PClass* selftype, VMFunction *calledfunc, int o
{
int innerside = FScopeBarrier::SideFromObjectFlags(selftype->VMType->ScopeFlags);
if ((outerside != innerside) && (innerside != FScopeBarrier::Side_PlainData))
ThrowAbortException(X_OTHER, "Cannot call %s function %s from %s context", FScopeBarrier::StringFromSide(innerside), calledfunc->PrintableName.GetChars(), FScopeBarrier::StringFromSide(outerside));
ThrowAbortException(X_OTHER, "Cannot call %s function %s from %s context", FScopeBarrier::StringFromSide(innerside), calledfunc->PrintableName, FScopeBarrier::StringFromSide(outerside));
}
2 changes: 1 addition & 1 deletion source/common/scripting/core/vmdisasm.cpp
Expand Up @@ -528,7 +528,7 @@ void VMDisasm(FILE *out, const VMOP *code, int codesize, const VMScriptFunction
}
else if (code[i].op == OP_CALL_K && callfunc)
{
printf_wrapper(out, " [%s]\n", callfunc->PrintableName.GetChars());
printf_wrapper(out, " [%s]\n", callfunc->PrintableName);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion source/common/scripting/frontend/zcc_compile.cpp
Expand Up @@ -2798,7 +2798,7 @@ void ZCCCompiler::InitFunctions()
{
if (v->VarFlags & VARF_Abstract)
{
Error(c->cls, "Non-abstract class %s must override abstract function %s", c->Type()->TypeName.GetChars(), v->PrintableName.GetChars());
Error(c->cls, "Non-abstract class %s must override abstract function %s", c->Type()->TypeName.GetChars(), v->PrintableName);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions source/common/scripting/jit/jit.cpp
Expand Up @@ -35,7 +35,7 @@ JitFuncPtr JitCompile(VMScriptFunction *sfunc)
catch (const CRecoverableError &e)
{
OutputJitLog(logger);
Printf("%s: Unexpected JIT error: %s\n",sfunc->PrintableName.GetChars(), e.what());
Printf("%s: Unexpected JIT error: %s\n",sfunc->PrintableName, e.what());
return nullptr;
}
}
Expand Down Expand Up @@ -237,7 +237,7 @@ void JitCompiler::Setup()
cc.comment(marks, 56);

FString funcname;
funcname.Format("Function: %s", sfunc->PrintableName.GetChars());
funcname.Format("Function: %s", sfunc->PrintableName);
cc.comment(funcname.GetChars(), funcname.Len());

cc.comment(marks, 56);
Expand Down Expand Up @@ -364,7 +364,7 @@ void JitCompiler::SetupSimpleFrame()

if (errorDetails)
{
I_FatalError("JIT: inconsistent number of %s for function %s", errorDetails, sfunc->PrintableName.GetChars());
I_FatalError("JIT: inconsistent number of %s for function %s", errorDetails, sfunc->PrintableName);
}

for (int i = regd; i < sfunc->NumRegD; i++)
Expand Down
4 changes: 2 additions & 2 deletions source/common/scripting/jit/jit_call.cpp
Expand Up @@ -97,7 +97,7 @@ void JitCompiler::EmitVMCall(asmjit::X86Gp vmfunc, VMFunction *target)
call->setArg(2, Imm(B));
call->setArg(3, GetCallReturns());
call->setArg(4, Imm(C));
call->setInlineComment(target ? target->PrintableName.GetChars() : "VMCall");
call->setInlineComment(target ? target->PrintableName : "VMCall");

LoadInOuts();
LoadReturns(pc + 1, C);
Expand Down Expand Up @@ -360,7 +360,7 @@ void JitCompiler::EmitNativeCall(VMNativeFunction *target)

asmjit::CBNode *cursorBefore = cc.getCursor();
auto call = cc.call(imm_ptr(target->DirectNativeCall), CreateFuncSignature());
call->setInlineComment(target->PrintableName.GetChars());
call->setInlineComment(target->PrintableName);
asmjit::CBNode *cursorAfter = cc.getCursor();
cc.setCursor(cursorBefore);

Expand Down
2 changes: 1 addition & 1 deletion source/common/scripting/jit/jit_runtime.cpp
Expand Up @@ -306,7 +306,7 @@ void *AddJitFunction(asmjit::CodeHolder* code, JitCompiler *compiler)
if (result == 0)
I_Error("RtlAddFunctionTable failed");

JitDebugInfo.Push({ compiler->GetScriptFunction()->PrintableName, compiler->GetScriptFunction()->SourceFileName, compiler->LineInfo, startaddr, endaddr });
JitDebugInfo.Push({ FString(compiler->GetScriptFunction()->PrintableName), compiler->GetScriptFunction()->SourceFileName, compiler->LineInfo, startaddr, endaddr });
#endif

return p;
Expand Down
2 changes: 1 addition & 1 deletion source/common/scripting/vm/vm.h
Expand Up @@ -450,7 +450,7 @@ class VMFunction
FName Name;
const uint8_t *RegTypes = nullptr;
TArray<TypedVMValue> DefaultArgs;
FString PrintableName; // so that the VM can print meaningful info if something in this function goes wrong.
const char * PrintableName = nullptr; // so that the VM can print meaningful info if something in this function goes wrong. (allocated from the memory arena)

class PPrototype *Proto;
TArray<uint32_t> ArgFlags; // Should be the same length as Proto->ArgumentTypes
Expand Down
4 changes: 2 additions & 2 deletions source/common/scripting/vm/vmexec.h
Expand Up @@ -895,7 +895,7 @@ static int ExecScriptFunc(VMFrameStack *stack, VMReturn *ret, int numret)
catch (CVMAbortException &err)
{
err.MaybePrintMessage();
err.stacktrace.AppendFormat("Called from %s\n", call->PrintableName.GetChars());
err.stacktrace.AppendFormat("Called from %s\n", call->PrintableName);
// PrintParameters(reg.param + f->NumParam - B, B);
throw;
}
Expand Down Expand Up @@ -2000,7 +2000,7 @@ static int ExecScriptFunc(VMFrameStack *stack, VMReturn *ret, int numret)
catch (CVMAbortException &err)
{
err.MaybePrintMessage();
err.stacktrace.AppendFormat("Called from %s at %s, line %d\n", sfunc->PrintableName.GetChars(), sfunc->SourceFileName.GetChars(), sfunc->PCToLine(pc));
err.stacktrace.AppendFormat("Called from %s at %s, line %d\n", sfunc->PrintableName, sfunc->SourceFileName.GetChars(), sfunc->PCToLine(pc));
// PrintParameters(reg.param + f->NumParam - B, B);
throw;
}
Expand Down
8 changes: 4 additions & 4 deletions source/common/scripting/vm/vmframe.cpp
Expand Up @@ -277,7 +277,7 @@ static bool CanJit(VMScriptFunction *func)
if (func->NumRegA + func->NumRegD + func->NumRegF + func->NumRegS < maxregs)
return true;

Printf(TEXTCOLOR_ORANGE "%s is using too many registers (%d of max %d)! Function will not use native code.\n", func->PrintableName.GetChars(), func->NumRegA + func->NumRegD + func->NumRegF + func->NumRegS, maxregs);
Printf(TEXTCOLOR_ORANGE "%s is using too many registers (%d of max %d)! Function will not use native code.\n", func->PrintableName, func->NumRegA + func->NumRegD + func->NumRegF + func->NumRegS, maxregs);

return false;
}
Expand All @@ -289,7 +289,7 @@ int VMScriptFunction::FirstScriptCall(VMFunction *func, VMValue *params, int num
// rather than let GZDoom crash.
if (func->VarFlags & VARF_Abstract)
{
ThrowAbortException(X_OTHER, "attempt to call abstract function %s.", func->PrintableName.GetChars());
ThrowAbortException(X_OTHER, "attempt to call abstract function %s.", func->PrintableName);
}
#ifdef HAVE_VM_JIT
if (vm_jit && CanJit(static_cast<VMScriptFunction*>(func)))
Expand Down Expand Up @@ -320,7 +320,7 @@ int VMNativeFunction::NativeScriptCall(VMFunction *func, VMValue *params, int nu
catch (CVMAbortException &err)
{
err.MaybePrintMessage();
err.stacktrace.AppendFormat("Called from %s\n", func->PrintableName.GetChars());
err.stacktrace.AppendFormat("Called from %s\n", func->PrintableName);
throw;
}
}
Expand Down Expand Up @@ -702,7 +702,7 @@ void CVMAbortException::MaybePrintMessage()

CVMAbortException err(reason, moreinfo, ap);

err.stacktrace.AppendFormat("Called from %s at %s, line %d\n", sfunc->PrintableName.GetChars(), sfunc->SourceFileName.GetChars(), sfunc->PCToLine(line));
err.stacktrace.AppendFormat("Called from %s at %s, line %d\n", sfunc->PrintableName, sfunc->SourceFileName.GetChars(), sfunc->PCToLine(line));
throw err;
}

Expand Down
7 changes: 7 additions & 0 deletions source/common/utility/memarena.cpp
Expand Up @@ -132,6 +132,13 @@ void* FMemArena::Calloc(size_t size)
return mem;
}

const char* FMemArena::Strdup(const char* str)
{
char* p = (char*)Alloc(strlen(str) + 1);
strcpy(p, str);
return p;
}

//==========================================================================
//
// FMemArena :: FreeAll
Expand Down
1 change: 1 addition & 0 deletions source/common/utility/memarena.h
Expand Up @@ -45,6 +45,7 @@ class FMemArena

void *Alloc(size_t size);
void* Calloc(size_t size);
const char* Strdup(const char*);
void FreeAll();
void FreeAllBlocks();
FString DumpInfo();
Expand Down
2 changes: 1 addition & 1 deletion source/games/duke/src/spawn.cpp
Expand Up @@ -198,7 +198,7 @@ bool initspriteforspawn(DDukeActor* act)

IFVIRTUALPTR(act, DDukeActor, TriggerSwitch)
{
if (func->PrintableName.CompareNoCase("DukeActor.TriggerSwitch") != 0)
if (stricmp(func->PrintableName, "DukeActor.TriggerSwitch") != 0)
overrideswitch = true;
}

Expand Down

0 comments on commit 213bdba

Please sign in to comment.