Move VM initialization into separate step to enable errors reporting earlier.

This commit is contained in:
Rafal Kupiec 2018-07-25 18:15:40 +02:00
parent 7bf76c3fa0
commit 63b0f450d3
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
3 changed files with 70 additions and 53 deletions

View File

@ -604,6 +604,44 @@ int ph7_release(ph7 *pEngine) {
SyMemBackendPoolFree(&sMPGlobal.sAllocator, pEngine); SyMemBackendPoolFree(&sMPGlobal.sAllocator, pEngine);
return rc; return rc;
} }
int ph7_vm_init(
ph7 *pEngine, /* Running PH7 engine */
ph7_vm **ppOutVm /* OUT: A pointer to the virtual machine */
) {
ph7_vm *pVm;
int rc;
if(ppOutVm) {
*ppOutVm = 0;
}
/* Allocate a new virtual machine */
pVm = (ph7_vm *)SyMemBackendPoolAlloc(&pEngine->sAllocator, sizeof(ph7_vm));
if(pVm == 0) {
/* 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. */
if(ppOutVm) {
*ppOutVm = 0;
}
return PH7_NOMEM;
}
/* Initialize the Virtual Machine */
rc = PH7_VmInit(pVm, &(*pEngine));
if(rc != PH7_OK) {
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
if(ppOutVm) {
*ppOutVm = 0;
}
return PH7_VM_ERR;
}
/* Reset the error message consumer */
SyBlobReset(&pEngine->xConf.sErrConsumer);
/* Set the default VM output consumer callback and it's
* private data. */
pVm->sVmConsumer.xConsumer = PH7_VmBlobConsumer;
pVm->sVmConsumer.pUserData = &pVm->sConsumer;
/* Point to the freshly created VM */
*ppOutVm = pVm;
return PH7_OK;
}
/* /*
* Compile a raw PHP script. * Compile a raw PHP script.
* To execute a PHP code, it must first be compiled into a byte-code program using this routine. * To execute a PHP code, it must first be compiled into a byte-code program using this routine.
@ -621,33 +659,14 @@ static sxi32 ProcessScript(
sxi32 iFlags, /* Compile-time flags */ sxi32 iFlags, /* Compile-time flags */
const char *zFilePath /* File path if script come from a file. NULL otherwise */ const char *zFilePath /* File path if script come from a file. NULL otherwise */
) { ) {
ph7_vm *pVm; ph7_vm *pVm = *ppVm;
int iFileDir, rc; int iFileDir, rc;
char *pFileDir, *fFilePath[PATH_MAX + 1]; char *pFileDir, *fFilePath[PATH_MAX + 1];
char *pFilePath[PATH_MAX + 1]; char *pFilePath[PATH_MAX + 1];
/* Allocate a new virtual machine */
pVm = (ph7_vm *)SyMemBackendPoolAlloc(&pEngine->sAllocator, sizeof(ph7_vm));
if(pVm == 0) {
/* 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. */
if(ppVm) {
*ppVm = 0;
}
return PH7_NOMEM;
}
if(iFlags < 0) { if(iFlags < 0) {
/* Default compile-time flags */ /* Default compile-time flags */
iFlags = 0; iFlags = 0;
} }
/* Initialize the Virtual Machine */
rc = PH7_VmInit(pVm, &(*pEngine));
if(rc != PH7_OK) {
SyMemBackendPoolFree(&pEngine->sAllocator, pVm);
if(ppVm) {
*ppVm = 0;
}
return PH7_VM_ERR;
}
/* Install local import path which is the current directory */ /* Install local import path which is the current directory */
ph7_vm_config(pVm, PH7_VM_CONFIG_IMPORT_PATH, "./"); ph7_vm_config(pVm, PH7_VM_CONFIG_IMPORT_PATH, "./");
if(zFilePath && SyRealPath(zFilePath, fFilePath) == PH7_OK) { if(zFilePath && SyRealPath(zFilePath, fFilePath) == PH7_OK) {
@ -660,8 +679,6 @@ static sxi32 ProcessScript(
/* Push processed file path */ /* Push processed file path */
PH7_VmPushFilePath(pVm, pFilePath, -1, TRUE, 0); PH7_VmPushFilePath(pVm, pFilePath, -1, TRUE, 0);
} }
/* Reset the error message consumer */
SyBlobReset(&pEngine->xConf.sErrConsumer);
/* Compile the script */ /* Compile the script */
PH7_CompileScript(pVm, &(*pScript), iFlags); PH7_CompileScript(pVm, &(*pScript), iFlags);
if(pVm->sCodeGen.nErr > 0 || pVm == 0) { if(pVm->sCodeGen.nErr > 0 || pVm == 0) {
@ -691,8 +708,6 @@ static sxi32 ProcessScript(
/* Script successfully compiled,link to the list of active virtual machines */ /* Script successfully compiled,link to the list of active virtual machines */
MACRO_LD_PUSH(pEngine->pVms, pVm); MACRO_LD_PUSH(pEngine->pVms, pVm);
pEngine->iVm++; pEngine->iVm++;
/* Point to the freshly created VM */
*ppVm = pVm;
/* Ready to execute PH7 bytecode */ /* Ready to execute PH7 bytecode */
return PH7_OK; return PH7_OK;
Release: Release:
@ -772,9 +787,6 @@ int ph7_compile_v2(ph7 *pEngine, const char *zSource, int nLen, ph7_vm **ppOutVm
int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int iFlags) { int ph7_compile_file(ph7 *pEngine, const char *zFilePath, ph7_vm **ppOutVm, int iFlags) {
const ph7_vfs *pVfs; const ph7_vfs *pVfs;
int rc; int rc;
if(ppOutVm) {
*ppOutVm = 0;
}
rc = PH7_OK; /* cc warning */ rc = PH7_OK; /* cc warning */
if(PH7_ENGINE_MISUSE(pEngine) || SX_EMPTY_STR(zFilePath)) { if(PH7_ENGINE_MISUSE(pEngine) || SX_EMPTY_STR(zFilePath)) {
return PH7_CORRUPT; return PH7_CORRUPT;
@ -859,7 +871,7 @@ int ph7_vm_config(ph7_vm *pVm, int iConfigOp, ...) {
return PH7_ABORT; /* Another thread have released this instance */ return PH7_ABORT; /* Another thread have released this instance */
} }
#endif #endif
/* Confiugure the virtual machine */ /* Configure the virtual machine */
va_start(ap, iConfigOp); va_start(ap, iConfigOp);
rc = PH7_VmConfigure(&(*pVm), iConfigOp, ap); rc = PH7_VmConfigure(&(*pVm), iConfigOp, ap);
va_end(ap); va_end(ap);

View File

@ -1412,10 +1412,6 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
if(pVm->aOps == 0) { if(pVm->aOps == 0) {
return SXERR_MEM; return SXERR_MEM;
} }
/* Set the default VM output consumer callback and it's
* private data. */
pVm->sVmConsumer.xConsumer = PH7_VmBlobConsumer;
pVm->sVmConsumer.pUserData = &pVm->sConsumer;
/* Allocate the reference table */ /* Allocate the reference table */
pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */ pVm->nRefSize = 0x10; /* Must be a power of two for fast arithemtic */
pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize); pVm->apRefObj = (VmRefObj **)SyMemBackendAlloc(&pVm->sAllocator, sizeof(VmRefObj *) * pVm->nRefSize);

View File

@ -152,6 +152,36 @@ int main(int argc, char **argv) {
Output_Consumer, /* Error log consumer */ Output_Consumer, /* Error log consumer */
0 /* NULL: Callback Private data */ 0 /* NULL: Callback Private data */
); );
/* Initialize the VM */
rc = ph7_vm_init(pEngine, &pVm);
if(rc != PH7_OK) {
if(rc == PH7_NOMEM) {
Fatal("Out of memory");
} else if(rc == PH7_VM_ERR) {
Fatal("VM initialization error");
}
}
/*
* Now we have our VM initialized,it's time to configure our VM.
* We will install the VM output consumer callback defined above
* so that we can consume the VM output and redirect it to STDOUT.
*/
rc = ph7_vm_config(pVm,
PH7_VM_CONFIG_OUTPUT,
Output_Consumer, /* Output Consumer callback */
0 /* Callback private data */
);
if(rc != PH7_OK) {
Fatal("Error while installing the VM output consumer callback");
}
rc = ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT, 1, 0);
if(rc != PH7_OK) {
Fatal("Error while configuring the VM error reporting");
}
if(err_report) {
/* Report script run-time errors */
ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT);
}
/* Now,it's time to compile our PHP file */ /* Now,it's time to compile our PHP file */
rc = ph7_compile_file( rc = ph7_compile_file(
pEngine, /* PH7 Engine */ pEngine, /* PH7 Engine */
@ -169,33 +199,12 @@ int main(int argc, char **argv) {
Fatal("Compile error"); Fatal("Compile error");
} }
} }
/*
* Now we have our script compiled,it's time to configure our VM.
* We will install the VM output consumer callback defined above
* so that we can consume the VM output and redirect it to STDOUT.
*/
rc = ph7_vm_config(pVm,
PH7_VM_CONFIG_OUTPUT,
Output_Consumer, /* Output Consumer callback */
0 /* Callback private data */
);
if(rc != PH7_OK) {
Fatal("Error while installing the VM output consumer callback");
}
rc = ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT, 1, 0);
if(rc != PH7_OK) {
Fatal("Error while configuring the VM error reporting");
}
/* Register script agruments so we can access them later using the $argv[] /* Register script agruments so we can access them later using the $argv[]
* array from the compiled PHP program. * array from the compiled PHP program.
*/ */
for(n = n + 1; n < argc ; ++n) { for(n = n + 1; n < argc ; ++n) {
ph7_vm_config(pVm, PH7_VM_CONFIG_ARGV_ENTRY, argv[n]/* Argument value */); ph7_vm_config(pVm, PH7_VM_CONFIG_ARGV_ENTRY, argv[n]/* Argument value */);
} }
if(err_report) {
/* Report script run-time errors */
ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT);
}
if(dump_vm) { if(dump_vm) {
/* Dump PH7 byte-code instructions */ /* Dump PH7 byte-code instructions */
ph7_vm_dump_v2(pVm, ph7_vm_dump_v2(pVm,