From d3e6ed3c9b838394d57fe617ebd9cffc930c0c1b Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Fri, 7 Jun 2019 17:23:46 +0300 Subject: [PATCH] - added null check for self pointer before calling a native function With JIT enabled, an implicit test for null self pointer is added to generated code This has no effect without JIT as VM verifies a pointer before calling a native method https://forum.zdoom.org/viewtopic.php?t=64961 --- src/scripting/vm/jit_call.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/scripting/vm/jit_call.cpp b/src/scripting/vm/jit_call.cpp index 137203017..14b3d5787 100644 --- a/src/scripting/vm/jit_call.cpp +++ b/src/scripting/vm/jit_call.cpp @@ -323,6 +323,28 @@ void JitCompiler::EmitNativeCall(VMNativeFunction *target) I_Error("Native direct member function calls not implemented\n"); } + if (target->ImplicitArgs > 0) + { + auto label = EmitThrowExceptionLabel(X_READ_NIL); + + assert(ParamOpcodes.Size() > 0); + const VMOP *param = ParamOpcodes[0]; + const int bc = param->i16u; + asmjit::X86Gp *reg = nullptr; + + switch (param->a & REGT_TYPE) + { + case REGT_STRING: reg = ®S[bc]; break; + case REGT_POINTER: reg = ®A[bc]; break; + default: + I_Error("Unexpected register type for self pointer\n"); + break; + } + + cc.test(*reg, *reg); + cc.jz(label); + } + asmjit::CBNode *cursorBefore = cc.getCursor(); auto call = cc.call(imm_ptr(target->DirectNativeCall), CreateFuncSignature()); call->setInlineComment(target->PrintableName.GetChars());