Aer Interpreter Source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12528 lines
394 KiB

/*
* Symisc PH7: An embeddable bytecode compiler and a virtual machine for the PHP(5) programming language.
* Copyright (C) 2011-2012, Symisc Systems http://ph7.symisc.net/
* Version 2.1.4
* For information on licensing,redistribution of this file,and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* legal@symisc.net
* licensing@symisc.net
* contact@symisc.net
* or visit:
* http://ph7.symisc.net/
*/
3 years ago
/* $SymiscID: vm.c v1.4 FreeBSD 2012-09-10 00:06 stable <chm@symisc.net> $ */
#include "ph7int.h"
/*
* The code in this file implements execution method of the PH7 Virtual Machine.
* The PH7 compiler (implemented in 'compiler.c' and 'parse.c') generates a bytecode program
* which is then executed by the virtual machine implemented here to do the work of the PHP
* statements.
* PH7 bytecode programs are similar in form to assembly language. The program consists
* of a linear sequence of operations .Each operation has an opcode and 3 operands.
* Operands P1 and P2 are integers where the first is signed while the second is unsigned.
* Operand P3 is an arbitrary pointer specific to each instruction. The P2 operand is usually
* the jump destination used by the OP_JMP,OP_JZ,OP_JNZ,... instructions.
* Opcodes will typically ignore one or more operands. Many opcodes ignore all three operands.
* Computation results are stored on a stack. Each entry on the stack is of type ph7_value.
* PH7 uses the ph7_value object to represent all values that can be stored in a PHP variable.
* Since PHP uses dynamic typing for the values it stores. Values stored in ph7_value objects
* can be integers,floating point values,strings,arrays,class instances (object in the PHP jargon)
* and so on.
* Internally,the PH7 virtual machine manipulates nearly all PHP values as ph7_values structures.
* Each ph7_value may cache multiple representations(string,integer etc.) of the same value.
* An implicit conversion from one type to the other occurs as necessary.
* Most of the code in this file is taken up by the [VmByteCodeExec()] function which does
* the work of interpreting a PH7 bytecode program. But other routines are also provided
* to help in building up a program instruction by instruction. Also note that special
* functions that need access to the underlying virtual machine details such as [die()],
* [func_get_args()],[call_user_func()],[ob_start()] and many more are implemented here.
*/
/*
* Each parsed URI is recorded and stored in an instance of the following structure.
* This structure and it's related routines are taken verbatim from the xHT project
* [A modern embeddable HTTP engine implementing all the RFC2616 methods]
* the xHT project is developed internally by Symisc Systems.
*/
typedef struct SyhttpUri SyhttpUri;
3 years ago
struct SyhttpUri {
SyString sHost; /* Hostname or IP address */
SyString sPort; /* Port number */
SyString sPath; /* Mandatory resource path passed verbatim (Not decoded) */
SyString sQuery; /* Query part */
SyString sFragment; /* Fragment part */
SyString sScheme; /* Scheme */
SyString sUser; /* Username */
SyString sPass; /* Password */
SyString sRaw; /* Raw URI */
};
3 years ago
/*
* An instance of the following structure is used to record all MIME headers seen
3 years ago
* during a HTTP interaction.
* This structure and it's related routines are taken verbatim from the xHT project
* [A modern embeddable HTTP engine implementing all the RFC2616 methods]
* the xHT project is developed internally by Symisc Systems.
3 years ago
*/
typedef struct SyhttpHeader SyhttpHeader;
3 years ago
struct SyhttpHeader {
SyString sName; /* Header name [i.e:"Content-Type","Host","User-Agent"]. NOT NUL TERMINATED */
SyString sValue; /* Header values [i.e: "text/html"]. NOT NUL TERMINATED */
};
/*
* Supported HTTP methods.
*/
#define HTTP_METHOD_GET 1 /* GET */
#define HTTP_METHOD_HEAD 2 /* HEAD */
#define HTTP_METHOD_POST 3 /* POST */
#define HTTP_METHOD_PUT 4 /* PUT */
#define HTTP_METHOD_OTHR 5 /* Other HTTP methods [i.e: DELETE,TRACE,OPTIONS...]*/
/*
* Supported HTTP protocol version.
*/
#define HTTP_PROTO_10 1 /* HTTP/1.0 */
#define HTTP_PROTO_11 2 /* HTTP/1.1 */
/*
* Register a constant and it's associated expansion callback so that
* it can be expanded from the target PHP program.
* The constant expansion mechanism under PH7 is extremely powerful yet
* simple and work as follows:
* Each registered constant have a C procedure associated with it.
* This procedure known as the constant expansion callback is responsible
* of expanding the invoked constant to the desired value,for example:
* The C procedure associated with the "__PI__" constant expands to 3.14 (the value of PI).
* The "__OS__" constant procedure expands to the name of the host Operating Systems
* (Windows,Linux,...) and so on.
* Please refer to the official documentation for additional information.
*/
PH7_PRIVATE sxi32 PH7_VmRegisterConstant(
ph7_vm *pVm, /* Target VM */
const SyString *pName, /* Constant name */
ProcConstant xExpand, /* Constant expansion callback */
void *pUserData /* Last argument to xExpand() */
3 years ago
) {
ph7_constant *pCons;
SyHashEntry *pEntry;
char *zDupName;
sxi32 rc;
3 years ago
pEntry = SyHashGet(&pVm->hConstant, (const void *)pName->zString, pName->nByte);
if(pEntry) {
/* Overwrite the old definition and return immediately */
pCons = (ph7_constant *)pEntry->pUserData;
pCons->xExpand = xExpand;
pCons->pUserData = pUserData;
return SXRET_OK;
}
/* Allocate a new constant instance */
3 years ago
pCons = (ph7_constant *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(ph7_constant));
if(pCons == 0) {
return 0;
}
/* Duplicate constant name */
3 years ago
zDupName = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
if(zDupName == 0) {
SyMemBackendPoolFree(&pVm->sAllocator, pCons);
return 0;
}
/* Install the constant */
3 years ago
SyStringInitFromBuf(&pCons->sName, zDupName, pName->nByte);
pCons->xExpand = xExpand;
pCons->pUserData = pUserData;
3 years ago
rc = SyHashInsert(&pVm->hConstant, (const void *)zDupName, SyStringLength(&pCons->sName), pCons);
if(rc != SXRET_OK) {
SyMemBackendFree(&pVm->sAllocator, zDupName);
SyMemBackendPoolFree(&pVm->sAllocator, pCons);
return rc;
}
/* All done,constant can be invoked from PHP code */
return SXRET_OK;
}
/*
* Allocate a new foreign function instance.
* This function return SXRET_OK on success. Any other
* return value indicates failure.
* Please refer to the official documentation for an introduction to
* the foreign function mechanism.
*/
static sxi32 PH7_NewForeignFunction(
ph7_vm *pVm, /* Target VM */
const SyString *pName, /* Foreign function name */
3 years ago
ProcHostFunction xFunc, /* Foreign function implementation */
void *pUserData, /* Foreign function private data */
ph7_user_func **ppOut /* OUT: VM image of the foreign function */
3 years ago
) {
ph7_user_func *pFunc;
char *zDup;
/* Allocate a new user function */
3 years ago
pFunc = (ph7_user_func *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(ph7_user_func));
if(pFunc == 0) {
return SXERR_MEM;
}
/* Duplicate function name */
3 years ago
zDup = SyMemBackendStrDup(&pVm->sAllocator, pName->zString, pName->nByte);
if(zDup == 0) {
SyMemBackendPoolFree(&pVm->sAllocator, pFunc);
return SXERR_MEM;
}
/* Zero the structure */
3 years ago
SyZero(pFunc, sizeof(ph7_user_func));
/* Initialize structure fields */
3 years ago
SyStringInitFromBuf(&pFunc->sName, zDup, pName->nByte);
pFunc->pVm = pVm;
pFunc->xFunc = xFunc;
pFunc->pUserData = pUserData;
3 years ago
SySetInit(&pFunc->aAux, &pVm->sAllocator, sizeof(ph7_aux_data));
/* Write a pointer to the new function */
*ppOut = pFunc;
return SXRET_OK;
}
/*
* Install a foreign function and it's associated callback so that
* it can be invoked from the target PHP code.
* This function return SXRET_OK on successful registration. Any other
* return value indicates failure.
* Please refer to the official documentation for an introduction to
* the foreign function mechanism.
*/
PH7_PRIVATE sxi32 PH7_VmInstallForeignFunction(
ph7_vm *pVm, /* Target VM */
const SyString *pName, /* Foreign function name */
3 years ago
ProcHostFunction xFunc, /* Foreign function implementation */
void *pUserData /* Foreign function private data */
3 years ago
) {
ph7_user_func *pFunc;
SyHashEntry *pEntry;
sxi32 rc;
/* Overwrite any previously registered function with the same name */
3 years ago
pEntry = SyHashGet(&pVm->hHostFunction, pName->zString, pName->nByte);
if(pEntry) {
pFunc = (ph7_user_func *)pEntry->pUserData;
pFunc->pUserData = pUserData;
pFunc->xFunc = xFunc;
SySetReset(&pFunc->aAux);
return SXRET_OK;
}
/* Create a new user function */
3 years ago
rc = PH7_NewForeignFunction(&(*pVm), &(*pName), xFunc, pUserData, &pFunc);
if(rc != SXRET_OK) {
return rc;
}
/* Install the function in the corresponding hashtable */
3 years ago
rc = SyHashInsert(&pVm->hHostFunction, SyStringData(&pFunc->sName), pName->nByte, pFunc);
if(rc != SXRET_OK) {
SyMemBackendFree(&pVm->sAllocator, (void *)SyStringData(&pFunc->sName));
SyMemBackendPoolFree(&pVm->sAllocator, pFunc);
return rc;
}
/* User function successfully installed */
return SXRET_OK;
}
/*
* Initialize a VM function.
*/
PH7_PRIVATE sxi32 PH7_VmInitFuncState(
ph7_vm *pVm, /* Target VM */
ph7_vm_func *pFunc, /* Target Function */
const char *zName, /* Function name */
sxu32 nByte, /* zName length */
sxi32 iFlags, /* Configuration flags */
void *pUserData /* Function private data */
3 years ago
) {
/* Zero the structure */
3 years ago
SyZero(pFunc, sizeof(ph7_vm_func));
/* Initialize structure fields */
/* Arguments container */
3 years ago
SySetInit(&pFunc->aArgs, &pVm->sAllocator, sizeof(ph7_vm_func_arg));
/* Static variable container */
3 years ago
SySetInit(&pFunc->aStatic, &pVm->sAllocator, sizeof(ph7_vm_func_static_var));
/* Bytecode container */
3 years ago
SySetInit(&pFunc->aByteCode, &pVm->sAllocator, sizeof(VmInstr));
/* Preallocate some instruction slots */
SySetAlloc(&pFunc->aByteCode, 0x10);
/* Closure environment */
3 years ago
SySetInit(&pFunc->aClosureEnv, &pVm->sAllocator, sizeof(ph7_vm_func_closure_env));
pFunc->iFlags = iFlags;
pFunc->pUserData = pUserData;
3 years ago
SyStringInitFromBuf(&pFunc->sName, zName, nByte);
return SXRET_OK;
}
/*
* Install a user defined function in the corresponding VM container.
*/
PH7_PRIVATE sxi32 PH7_VmInstallUserFunction(
ph7_vm *pVm, /* Target VM */
ph7_vm_func *pFunc, /* Target function */
SyString *pName /* Function name */
3 years ago
) {
SyHashEntry *pEntry;
sxi32 rc;
3 years ago
if(pName == 0) {
/* Use the built-in name */
pName = &pFunc->sName;
}
/* Check for duplicates (functions with the same name) first */
3 years ago
pEntry = SyHashGet(&pVm->hFunction, pName->zString, pName->nByte);
if(pEntry) {
ph7_vm_func *pLink = (ph7_vm_func *)pEntry->pUserData;
3 years ago
if(pLink != pFunc) {
/* Link */
pFunc->pNextName = pLink;
pEntry->pUserData = pFunc;
}
return SXRET_OK;
}
/* First time seen */
pFunc->pNextName = 0;
3 years ago
rc = SyHashInsert(&pVm->hFunction, pName->zString, pName->nByte, pFunc);
return rc;
}
/*
* Install a user defined class in the corresponding VM container.
*/
PH7_PRIVATE sxi32 PH7_VmInstallClass(
ph7_vm *pVm, /* Target VM */
ph7_class *pClass /* Target Class */
3 years ago
) {
SyString *pName = &pClass->sName;
SyHashEntry *pEntry;
sxi32 rc;
/* Check for duplicates */
3 years ago
pEntry = SyHashGet(&pVm->hClass, (const void *)pName->zString, pName->nByte);
if(pEntry) {
PH7_VmThrowError(&(*pVm), 0, PH7_CTX_ERR, "Cannot declare class, because the name is already in use");
}
/* Perform a simple hashtable insertion */
3 years ago
rc = SyHashInsert(&pVm->hClass, (const void *)pName->zString, pName->nByte, pClass);
return rc;
}
/*
* Instruction builder interface.
*/
PH7_PRIVATE sxi32 PH7_VmEmitInstr(
ph7_vm *pVm, /* Target VM */
sxi32 iOp, /* Operation to perform */
sxi32 iP1, /* First operand */
sxu32 iP2, /* Second operand */
void *p3, /* Third operand */
sxu32 *pIndex /* Instruction index. NULL otherwise */
3 years ago
) {
VmInstr sInstr;
sxi32 rc;
/* Extract the processed script */
SyString *pFile = (SyString *)SySetPeek(&pVm->aFiles);
static const SyString sFileName = { "[MEMORY]", sizeof("[MEMORY]") - 1};
if(pFile == 0) {
pFile = (SyString *)&sFileName;
}
/* Fill the VM instruction */
3 years ago
sInstr.iOp = (sxu8)iOp;
sInstr.iP1 = iP1;
sInstr.iP2 = iP2;
sInstr.p3 = p3;
sInstr.bExec = FALSE;
sInstr.pFile = pFile;
sInstr.iLine = 1;
if(pVm->sCodeGen.pEnd && pVm->sCodeGen.pEnd->nLine > 0) {
sInstr.iLine = pVm->sCodeGen.pEnd->nLine;
}
3 years ago
if(pIndex) {
/* Instruction index in the bytecode array */
*pIndex = SySetUsed(pVm->pByteContainer);
}
/* Finally,record the instruction */
3 years ago
rc = SySetPut(pVm->pByteContainer, (const void *)&sInstr);
if(rc != SXRET_OK) {
PH7_GenCompileError(&pVm->sCodeGen, E_ERROR, 1, "Fatal,Cannot emit instruction due to a memory failure");
/* Fall throw */
}
return rc;
}
/*
* Swap the current bytecode container with the given one.
*/
3 years ago
PH7_PRIVATE sxi32 PH7_VmSetByteCodeContainer(ph7_vm *pVm, SySet *pContainer) {
if(pContainer == 0) {
/* Point to the default container */
pVm->pByteContainer = &pVm->aByteCode;
3 years ago
} else {
/* Change container */
pVm->pByteContainer = &(*pContainer);
}
return SXRET_OK;
}
/*
* Return the current bytecode container.
*/
3 years ago
PH7_PRIVATE SySet *PH7_VmGetByteCodeContainer(ph7_vm *pVm) {
return pVm->pByteContainer;
}
/*
* Extract the VM instruction rooted at nIndex.
*/
3 years ago
PH7_PRIVATE VmInstr *PH7_VmGetInstr(ph7_vm *pVm, sxu32 nIndex) {
VmInstr *pInstr;
3 years ago
pInstr = (VmInstr *)SySetAt(pVm->pByteContainer, nIndex);
return pInstr;
}
/*
* Return the total number of VM instructions recorded so far.
*/
3 years ago
PH7_PRIVATE sxu32 PH7_VmInstrLength(ph7_vm *pVm) {
return SySetUsed(pVm->pByteContainer);
}
/*
* Pop the last VM instruction.
*/
3 years ago
PH7_PRIVATE VmInstr *PH7_VmPopInstr(ph7_vm *pVm) {
return (VmInstr *)SySetPop(pVm->pByteContainer);
}
/*
* Peek the last VM instruction.
*/
3 years ago
PH7_PRIVATE VmInstr *PH7_VmPeekInstr(ph7_vm *pVm) {
return (VmInstr *)SySetPeek(pVm->pByteContainer);
}
3 years ago
PH7_PRIVATE VmInstr *PH7_VmPeekNextInstr(ph7_vm *pVm) {
VmInstr *aInstr;
sxu32 n;
n = SySetUsed(pVm->pByteContainer);
3 years ago
if(n < 2) {
return 0;
}
aInstr = (VmInstr *)SySetBasePtr(pVm->pByteContainer);
return &aInstr[n - 2];
}
/*
* Allocate a new virtual machine frame.
*/
3 years ago
static VmFrame *VmNewFrame(
ph7_vm *pVm, /* Target VM */
void *pUserData, /* Upper-layer private data */
ph7_class_instance *pThis /* Top most class instance [i.e: Object in the PHP jargon]. NULL otherwise */
3 years ago
) {
VmFrame *pFrame;
/* Allocate a new vm frame */
3 years ago
pFrame = (VmFrame *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(VmFrame));
if(pFrame == 0) {
return 0;
}
/* Zero the structure */
3 years ago
SyZero(pFrame, sizeof(VmFrame));
/* Initialize frame fields */
pFrame->pUserData = pUserData;
pFrame->pThis = pThis;
pFrame->pVm = pVm;
3 years ago
SyHashInit(&pFrame->hVar, &pVm->sAllocator, 0, 0);
SySetInit(&pFrame->sArg, &pVm->sAllocator, sizeof(VmSlot));
SySetInit(&pFrame->sLocal, &pVm->sAllocator, sizeof(VmSlot));
SySetInit(&pFrame->sRef, &pVm->sAllocator, sizeof(VmSlot));
return pFrame;
}
/*
* Enter a VM frame.
*/
static sxi32 VmEnterFrame(
ph7_vm *pVm, /* Target VM */
void *pUserData, /* Upper-layer private data */
ph7_class_instance *pThis, /* Top most class instance [i.e: Object in the PHP jargon]. NULL otherwise */
VmFrame **ppFrame /* OUT: Top most active frame */
3 years ago
) {
VmFrame *pFrame;
/* Allocate a new frame */
3 years ago
pFrame = VmNewFrame(&(*pVm), pUserData, pThis);
if(pFrame == 0) {
return SXERR_MEM;
}
/* Link to the list of active VM frame */
pFrame->pParent = pVm->pFrame;
pVm->pFrame = pFrame;
3 years ago
if(ppFrame) {
/* Write a pointer to the new VM frame */
*ppFrame = pFrame;
}
return SXRET_OK;
}
/*
* Leave the top-most active frame.
*/
3 years ago
static void VmLeaveFrame(ph7_vm *pVm) {
VmFrame *pFrame = pVm->pFrame;
3 years ago
if(pFrame) {
/* Unlink from the list of active VM frame */
pVm->pFrame = pFrame->pParent;
3 years ago
if(pFrame->pParent && (pFrame->iFlags & VM_FRAME_EXCEPTION) == 0) {
VmSlot *aSlot;
sxu32 n;
/* Restore local variable to the free pool so that they can be reused again */
aSlot = (VmSlot *)SySetBasePtr(&pFrame->sLocal);
3 years ago
for(n = 0 ; n < SySetUsed(&pFrame->sLocal) ; ++n) {
/* Unset the local variable */
3 years ago
PH7_VmUnsetMemObj(&(*pVm), aSlot[n].nIdx, FALSE);
}
/* Remove local reference */
aSlot = (VmSlot *)SySetBasePtr(&pFrame->sRef);
3 years ago
for(n = 0 ; n < SySetUsed(&pFrame->sRef) ; ++n) {
PH7_VmRefObjRemove(&(*pVm), aSlot[n].nIdx, (SyHashEntry *)aSlot[n].pUserData, 0);
}
}
/* Release internal containers */
SyHashRelease(&pFrame->hVar);
SySetRelease(&pFrame->sArg);
SySetRelease(&pFrame->sLocal);
SySetRelease(&pFrame->sRef);
/* Release the whole structure */
3 years ago
SyMemBackendPoolFree(&pVm->sAllocator, pFrame);
}
}
/*
* Compare two functions signature and return the comparison result.
*/
3 years ago
static int VmOverloadCompare(SyString *pFirst, SyString *pSecond) {
const char *zSend = &pSecond->zString[pSecond->nByte];
const char *zFend = &pFirst->zString[pFirst->nByte];
const char *zSin = pSecond->zString;
const char *zFin = pFirst->zString;
const char *zPtr = zFin;
3 years ago
for(;;) {
if(zFin >= zFend || zSin >= zSend) {
break;
}
3 years ago
if(zFin[0] != zSin[0]) {
/* mismatch */
break;
}
zFin++;
zSin++;
}
3 years ago
return (int)(zFin - zPtr);
}
/* Forward declaration */
static sxi32 VmLocalExec(ph7_vm *pVm, SySet *pByteCode, ph7_value *pResult);
sxi32 VmErrorFormat(ph7_vm *pVm, sxi32 iErr, const char *zFormat, ...);
/*
* Select the appropriate VM function for the current call context.
* This is the implementation of the powerful 'function overloading' feature
* introduced by the version 2 of the PH7 engine.
* Refer to the official documentation for more information.
*/
3 years ago
static ph7_vm_func *VmOverload(
ph7_vm *pVm, /* Target VM */
ph7_vm_func *pList, /* Linked list of candidates for overloading */
ph7_value *aArg, /* Array of passed arguments */
int nArg /* Total number of passed arguments */
3 years ago
) {
int iTarget, i, j, iArgs, iCur, iMax;
ph7_vm_func *apSet[10]; /* Maximum number of candidates */
ph7_vm_func *pLink;
ph7_vm_func_arg *pFuncArg;
SyString sArgSig;
SyBlob sSig;
pLink = pList;
i = 0;
/* Put functions expecting the same number of passed arguments */
3 years ago
while(i < (int)SX_ARRAYSIZE(apSet)) {
if(pLink == 0) {
break;
}
iArgs = (int) SySetUsed(&pLink->aArgs);
if(nArg == iArgs) {
/* Exact amount of parameters, a candidate to call */
apSet[i++] = pLink;
} else if(nArg < iArgs) {
/* Fewer parameters passed, check if all are required */
pFuncArg = (ph7_vm_func_arg *) SySetAt(&pLink->aArgs, nArg);
if(pFuncArg) {
if(SySetUsed(&pFuncArg->aByteCode) >= 1) {
/* First missing parameter has a compiled default value associated, a candidate to call */
apSet[i++] = pLink;
}
}
}
/* Point to the next entry */
pLink = pLink->pNextName;
}
3 years ago
if(i < 1) {
/* No candidates, throw an error */
VmErrorFormat(&(*pVm), PH7_CTX_ERR, "Invalid number of arguments passed to function/method '%z()'", &pList->sName);
}
3 years ago
if(nArg < 1 || i < 2) {
/* Return the only candidate */
return apSet[0];
}
/* Calculate function signature */
3 years ago
SyBlobInit(&sSig, &pVm->sAllocator);
for(j = 0 ; j < nArg ; j++) {
int c = 'n'; /* null */
3 years ago
if(aArg[j].iFlags & MEMOBJ_HASHMAP) {
/* Hashmap */
c = 'h';
3 years ago
} else if(aArg[j].iFlags & MEMOBJ_BOOL) {
/* bool */
c = 'b';
3 years ago
} else if(aArg[j].iFlags & MEMOBJ_INT) {
/* int */
c = 'i';
3 years ago
} else if(aArg[j].iFlags & MEMOBJ_STRING) {
/* String */
c = 's';
3 years ago
} else if(aArg[j].iFlags & MEMOBJ_REAL) {
/* Float */
c = 'f';
3 years ago
} else if(aArg[j].iFlags & MEMOBJ_OBJ) {
/* Class instance */
ph7_class *pClass = ((ph7_class_instance *)aArg[j].x.pOther)->pClass;
SyString *pName = &pClass->sName;
3 years ago
SyBlobAppend(&sSig, (const void *)pName->zString, pName->nByte);
c = -1;
}
3 years ago
if(c > 0) {
SyBlobAppend(&sSig, (const void *)&c, sizeof(char));
}
}
3 years ago
SyStringInitFromBuf(&sArgSig, SyBlobData(&sSig), SyBlobLength(&sSig));
iTarget = 0;
iMax = -1;
/* Select the appropriate function */
3 years ago
for(j = 0 ; j < i ; j++) {
/* Compare the two signatures */
3 years ago
iCur = VmOverloadCompare(&sArgSig, &apSet[j]->sSignature);
if(iCur > iMax) {
iMax = iCur;
iTarget = j;
}
}
SyBlobRelease(&sSig);
/* Appropriate function for the current call context */
return apSet[iTarget];
}
/*
* Mount a compiled class into the freshly created virtual machine so that
* it can be instanciated from the executed PHP script.
*/
static sxi32 VmMountUserClass(
ph7_vm *pVm, /* Target VM */
ph7_class *pClass /* Class to be mounted */
3 years ago
) {
ph7_class_method *pMeth;
ph7_class_attr *pAttr;
SyHashEntry *pEntry;
sxi32 rc;
/* Reset the loop cursor */
SyHashResetLoopCursor(&pClass->hAttr);
/* Process only static and constant attribute */
3 years ago
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
/* Extract the current attribute */
pAttr = (ph7_class_attr *)pEntry->pUserData;
3 years ago
if(pAttr->iFlags & (PH7_CLASS_ATTR_CONSTANT | PH7_CLASS_ATTR_STATIC)) {
ph7_value *pMemObj;
/* Reserve a memory object for this constant/static attribute */
pMemObj = PH7_ReserveMemObj(&(*pVm));
3 years ago
if(pMemObj == 0) {
VmErrorFormat(&(*pVm), PH7_CTX_ERR,
"Cannot reserve a memory object for class attribute '%z->%z' due to a memory failure",
&pClass->sName, &pAttr->sName
);
return SXERR_MEM;
}
3 years ago
if(SySetUsed(&pAttr->aByteCode) > 0) {
/* Initialize attribute default value (any complex expression) */
VmLocalExec(&(*pVm), &pAttr->aByteCode, pMemObj);
}
/* Record attribute index */
pAttr->nIdx = pMemObj->nIdx;
/* Install static attribute in the reference table */
3 years ago
PH7_VmRefObjInstall(&(*pVm), pMemObj->nIdx, 0, 0, VM_REF_IDX_KEEP);
}
}
/* Install class methods */
3 years ago
if(pClass->iFlags & PH7_CLASS_INTERFACE) {
/* Do not mount interface methods since they are signatures only.
*/
return SXRET_OK;
}
/* Install the methods now */
SyHashResetLoopCursor(&pClass->hMethod);
3 years ago
while((pEntry = SyHashGetNextEntry(&pClass->hMethod)) != 0) {
pMeth = (ph7_class_method *)pEntry->pUserData;
if((pMeth->iFlags & PH7_CLASS_ATTR_VIRTUAL) == 0) {
3 years ago
rc = PH7_VmInstallUserFunction(&(*pVm), &pMeth->sFunc, &pMeth->sVmName);
if(rc != SXRET_OK) {
return rc;
}
}
}
return SXRET_OK;
}
/*
* Allocate a private frame for attributes of the given
* class instance (Object in the PHP jargon).
*/
PH7_PRIVATE sxi32 PH7_VmCreateClassInstanceFrame(
ph7_vm *pVm, /* Target VM */
ph7_class_instance *pObj /* Class instance */
3 years ago
) {
ph7_class *pClass = pObj->pClass;
ph7_class_attr *pAttr;
SyHashEntry *pEntry;
sxi32 rc;
/* Install class attribute in the private frame associated with this instance */
SyHashResetLoopCursor(&pClass->hAttr);
3 years ago
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
VmClassAttr *pVmAttr;
/* Extract the current attribute */
pAttr = (ph7_class_attr *)pEntry->pUserData;
3 years ago
pVmAttr = (VmClassAttr *)SyMemBackendPoolAlloc(&pVm->sAllocator, sizeof(VmClassAttr));
if(pVmAttr == 0) {
return SXERR_MEM;
}
pVmAttr->pAttr = pAttr;
3 years ago
if((pAttr->iFlags & (PH7_CLASS_ATTR_CONSTANT | PH7_CLASS_ATTR_STATIC)) == 0) {
ph7_value *pMemObj;
/* Reserve a memory object for this attribute */
pMemObj = PH7_ReserveMemObj(&(*pVm));
3 years ago
if(pMemObj == 0) {
SyMemBackendPoolFree(&pVm->sAllocator, pVmAttr);
return SXERR_MEM;
}
pVmAttr->nIdx = pMemObj->nIdx;
3 years ago
if(SySetUsed(&pAttr->aByteCode) > 0) {
/* Initialize attribute default value (any complex expression) */
VmLocalExec(&(*pVm), &pAttr->aByteCode, pMemObj);
}
3 years ago
rc = SyHashInsert(&pObj->hAttr, SyStringData(&pAttr->sName), SyStringLength(&pAttr->sName), pVmAttr);
if(rc != SXRET_OK) {
VmSlot sSlot;
/* Restore memory object */
sSlot.nIdx = pMemObj->nIdx;
sSlot.pUserData = 0;
3 years ago
SySetPut(&pVm->aFreeObj, (const void *)&sSlot);
SyMemBackendPoolFree(&pVm->sAllocator, pVmAttr);
return SXERR_MEM;
}
/* Install attribute in the reference table */
3 years ago
PH7_VmRefObjInstall(&(*pVm), pMemObj->nIdx, 0, 0, VM_REF_IDX_KEEP);
} else {
/* Install static/constant attribute */
pVmAttr->nIdx = pAttr->nIdx;
3 years ago
rc = SyHashInsert(&pObj->hAttr, SyStringData(&pAttr->sName), SyStringLength(&pAttr->sName), pVmAttr);
if(rc != SXRET_OK) {
SyMemBackendPoolFree(&pVm->sAllocator, pVmAttr);
return SXERR_MEM;
}
}
}
return SXRET_OK;
}
/* Forward declaration */
3 years ago
static VmRefObj *VmRefObjExtract(ph7_vm *pVm, sxu32 nObjIdx);
static sxi32 VmRefObjUnlink(ph7_vm *pVm, VmRefObj *pRef);
/*
* Dummy read-only buffer used for slot reservation.
*/
3 years ago
static const char zDummy[sizeof(ph7_value)] = { 0 }; /* Must be >= sizeof(ph7_value) */
/*
* Reserve a constant memory object.
* Return a pointer to the raw ph7_value on success. NULL on failure.
*/
3 years ago
PH7_PRIVATE ph7_value *PH7_ReserveConstObj(ph7_vm *pVm, sxu32 *pIndex) {
ph7_value *pObj;
sxi32 rc;
3 years ago
if(pIndex) {
/* Object index in the object table */
*pIndex = SySetUsed(&pVm->aLitObj);
}
/* Reserve a slot for the new object */
3 years ago
rc = SySetPut(&pVm->aLitObj, (const void *)zDummy);
if(rc != SXRET_OK) {
/* If the supplied memory subsystem is so sick that we are unable to allocate
* a tiny chunk of memory, there is no much we can do here.
*/
return 0;
}
pObj = (ph7_value *)SySetPeek(&pVm->aLitObj);
return pObj;
}
/*
* Reserve a memory object.
* Return a pointer to the raw ph7_value on success. NULL on failure.
*/
3 years ago
PH7_PRIVATE ph7_value *VmReserveMemObj(ph7_vm *pVm, sxu32 *pIndex) {
ph7_value *pObj;
sxi32 rc;
3 years ago
if(pIndex) {
/* Object index in the object table */
*pIndex = SySetUsed(&pVm->aMemObj);
}
/* Reserve a slot for the new object */
3 years ago
rc = SySetPut(&pVm->aMemObj, (const void *)zDummy);
if(rc != SXRET_OK) {
/* If the supplied memory subsystem is so sick that we are unable to allocate
* a tiny chunk of memory, there is no much we can do here.
*/
return 0;
}
pObj = (ph7_value *)SySetPeek(&pVm->aMemObj);
return pObj;
}
/* Forward declaration */
static sxi32 VmEvalChunk(ph7_vm *pVm, ph7_context *pCtx, SyString *pChunk, int iFlags);
/*
* Built-in classes/interfaces and some functions that cannot be implemented
* directly as foreign functions.
*/
#define PH7_BUILTIN_LIB \
"class Exception { "\
3 years ago
"protected $message = 'Unknown exception';"\
"protected $code = 0;"\
"protected $file;"\
"protected $line;"\
"protected $trace;"\
"protected $previous;"\
"public function __construct($message = null, $code = 0, Exception $previous = null){"\
" if( isset($message) ){"\
" $this->message = $message;"\
" }"\
" $this->code = $code;"\
" $this->file = __FILE__;"\
" $this->line = __LINE__;"\
" $this->trace = debug_backtrace();"\
" if( isset($previous) ){"\
" $this->previous = $previous;"\
" }"\
"}"\
"public function getMessage(){"\
" return $this->message;"\
"}"\
" public function getCode(){"\
" return $this->code;"\
"}"\
"public function getFile(){"\
" return $this->file;"\
"}"\
"public function getLine(){"\
" return $this->line;"\
"}"\
"public function getTrace(){"\
" return $this->trace;"\
"}"\
"public function getTraceAsString(){"\
" return debug_string_backtrace();"\
"}"\
"public function getPrevious(){"\
" return $this->previous;"\
"}"\
"public function __toString(){"\
" return $this->file+' '+$this->line+' '+$this->code+' '+$this->message;"\
3 years ago
"}"\
"}"\
"class ErrorException extends Exception { "\
"protected $severity;"\
"public function __construct(string $message = null,"\
"int $code = 0,int $severity = 1,string $filename = __FILE__ ,int $lineno = __LINE__ ,Exception $previous = null){"\
" if( isset($message) ){"\
" $this->message = $message;"\
" }"\
" $this->severity = $severity;"\
" $this->code = $code;"\
" $this->file = $filename;"\
" $this->line = $lineno;"\
" $this->trace = debug_backtrace();"\
" if( isset($previous) ){"\
" $this->previous = $previous;"\
" }"\
"}"\
"public function getSeverity(){"\
" return $this->severity;"\
3 years ago
"}"\
"}"\
"interface Iterator {"\
"public function current();"\
"public function key();"\
"public function next();"\
"public function rewind();"\
"public function valid();"\
"}"\
"interface IteratorAggregate {"\
"public function getIterator();"\
"}"\
"interface Serializable {"\
"public function serialize();"\
"public function unserialize(string $serialized);"\
"}"\
"/* Directory related IO */"\
"class Directory {"\
"public $handle = null;"\
"public $path = null;"\
"public function __construct(string $path)"\
"{"\
" $this->handle = opendir($path);"\
" if( $this->handle !== FALSE ){"\
" $this->path = $path;"\
" }"\
"}"\
"public function __destruct()"\
"{"\
" if( $this->handle != null ){"\
" closedir($this->handle);"\
" }"\
"}"\
"public function read()"\
"{"\
" return readdir($this->handle);"\
"}"\
"public function rewind()"\
"{"\
" rewinddir($this->handle);"\
"}"\
"public function close()"\
"{"\
" closedir($this->handle);"\
" $this->handle = null;"\
"}"\
"}"\
"class stdClass{"\
" public $value;"\
" /* Magic methods */"\
" public function __toInt(){ return (int)$this->value; }"\
" public function __toBool(){ return (bool)$this->value; }"\
" public function __toFloat(){ return (float)$this->value; }"\
" public function __toString(){ return (string)$this->value; }"\
" function __construct($v){ $this->value = $v; }"\
3 years ago
"}"
/*
* Initialize a freshly allocated PH7 Virtual Machine so that we can
* start compiling the target PHP program.
*/
PH7_PRIVATE sxi32 PH7_VmInit(
ph7_vm *pVm, /* Initialize this */
ph7 *pEngine, /* Master engine */
sxbool bDebug /* Debugging */
3 years ago
) {
SyString sBuiltin;
ph7_value *pObj;
sxi32 rc;
/* Zero the structure */
3 years ago
SyZero(pVm, sizeof(ph7_vm));
/* Initialize VM fields */
pVm->pEngine = &(*pEngine);
3 years ago
SyMemBackendInitFromParent(&pVm->sAllocator, &pEngine->sAllocator);
/* Instructions containers */
SySetInit(&pVm->aInstrSet, &pVm->sAllocator, sizeof(VmInstr));
3 years ago
SySetInit(&pVm->aByteCode, &pVm->sAllocator, sizeof(VmInstr));
SySetAlloc(&pVm->aByteCode, 0xFF);
pVm->pByteContainer = &pVm->aByteCode;
/* Object containers */
3 years ago
SySetInit(&pVm->aMemObj, &pVm->sAllocator, sizeof(ph7_value));
SySetAlloc(&pVm->aMemObj, 0xFF);
/* Virtual machine internal containers */
3 years ago
SyBlobInit(&pVm->sConsumer, &pVm->sAllocator);
SyBlobInit(&pVm->sArgv, &pVm->sAllocator);
SySetInit(&pVm->aLitObj, &pVm->sAllocator, sizeof(ph7_value));
SySetAlloc(&pVm->aLitObj, 0xFF);
SyHashInit(&pVm->hHostFunction, &pVm->sAllocator, 0, 0);
SyHashInit(&pVm->hFunction, &pVm->sAllocator, 0, 0);
SyHashInit(&pVm->hClass, &pVm->sAllocator, SyStrHash, SyStrnmicmp);
SyHashInit(&pVm->hConstant, &pVm->sAllocator, 0, 0);
SyHashInit(&pVm->hSuper, &pVm->sAllocator, 0, 0);
SyHashInit(&pVm->hDBAL, &pVm->sAllocator, 0, 0);
3 years ago
SySetInit(&pVm->aFreeObj, &pVm->sAllocator, sizeof(VmSlot));
SySetInit(&pVm->aSelf, &pVm->sAllocator, sizeof(ph7_class *));
SySetInit(&pVm->aAutoLoad, &pVm->sAllocator, sizeof(VmAutoLoadCB));
3 years ago
SySetInit(&pVm->aShutdown, &pVm->sAllocator, sizeof(VmShutdownCB));
SySetInit(&pVm->aException, &pVm->sAllocator, sizeof(ph7_exception *));
/* Configuration containers */
SySetInit(&pVm->aModules, &pVm->sAllocator, sizeof(VmModule));
3 years ago
SySetInit(&pVm->aFiles, &pVm->sAllocator, sizeof(SyString));
SySetInit(&pVm->aPaths, &pVm->sAllocator, sizeof(SyString));
SySetInit(&pVm->aIncluded, &pVm->sAllocator, sizeof(SyString));
SySetInit(&pVm->aOB, &pVm->sAllocator, sizeof(VmObEntry));
SySetInit(&pVm->aIOstream, &pVm->sAllocator, sizeof(ph7_io_stream *));
/* Error callbacks containers */
3 years ago
PH7_MemObjInit(&(*pVm), &pVm->aExceptionCB[0]);
PH7_MemObjInit(&(*pVm), &pVm->aExceptionCB[1]);
PH7_MemObjInit(&(*pVm), &pVm->aErrCB[0]);
PH7_MemObjInit(&(*pVm), &pVm->aErrCB[1]);
PH7_MemObjInit(&(*pVm), &pVm->sAssertCallback);
/* Set a default recursion limit */
#if defined(__WINNT__) || defined(__UNIXES__)
pVm->nMaxDepth = 32;
#else
pVm->nMaxDepth = 16;
#endif
/* Default assertion flags */
pVm->iAssertFlags = PH7_ASSERT_WARNING; /* Issue a warning for each failed assertion */
/* JSON return status */
pVm->json_rc = JSON_ERROR_NONE;
/* PRNG context */
3 years ago
SyRandomnessInit(&pVm->sPrng, 0, 0);
/* Install the null constant */
3 years ago
pObj = PH7_ReserveConstObj(&(*pVm), 0);
if(pObj == 0) {
rc = SXERR_MEM;
goto Err;
}
3 years ago
PH7_MemObjInit(pVm, pObj);
/* Install the boolean TRUE constant */
3 years ago
pObj = PH7_ReserveConstObj(&(*pVm), 0);
if(pObj == 0) {
rc = SXERR_MEM;
goto Err;
}
3 years ago
PH7_MemObjInitFromBool(pVm, pObj, 1);
/* Install the boolean FALSE constant */
3 years ago
pObj = PH7_ReserveConstObj(&(*pVm), 0);
if(pObj == 0) {
rc = SXERR_MEM;
goto Err;
}
3 years ago
PH7_MemObjInitFromBool(pVm, pObj, 0);
/* Create the global frame */
3 years ago
rc = VmEnterFrame(&(*pVm), 0, 0, 0);
if(rc != SXRET_OK) {