Add VM SAPI configuration option and restrict console module to the CLI SAPI
Build / AerScript (push) Successful in 12s

This commit is contained in:
2026-11-23 23:45:16 +01:00
parent 948487fd31
commit cb894f66fd
5 changed files with 102 additions and 5 deletions
+92 -5
View File
@@ -8,6 +8,7 @@
* David Carlier <devnexen@gmail.com>
*/
#include <math.h>
#include <string.h>
#include "ph7int.h"
/*
@@ -2412,6 +2413,22 @@ PH7_PRIVATE ph7_value *VmReserveMemObj(ph7_vm *pVm, sxu32 *pIndex) {
}
/* Forward declaration */
static sxi32 VmEvalChunk(ph7_vm *pVm, ph7_context *pCtx, SyString *pChunk, int iFlags, ph7_value *pResult, ph7_include *pInclude);
/*
* SAPI identity of a VM. A NULL VM or a missing name means the default
* CLI SAPI, so callers never have to special-case the unset configuration.
*/
PH7_PRIVATE const char *PH7_VmSapiName(ph7_vm *pVm) {
if(pVm != 0 && pVm->zSapi != 0) {
return pVm->zSapi;
}
return "cli";
}
PH7_PRIVATE int PH7_VmSapiIs(ph7_vm *pVm, const char *zSapi) {
if(zSapi == 0) {
return 0;
}
return strcmp(PH7_VmSapiName(pVm), zSapi) == 0;
}
/*
* Initialize a freshly allocated PH7 Virtual Machine so that we can
* start compiling the target PHP program.
@@ -2431,6 +2448,10 @@ PH7_PRIVATE sxi32 PH7_VmInit(
if(rc != SXRET_OK) {
return rc;
}
/* The VM knows its SAPI from the start. Default to the CLI SAPI; an
* allocation failure leaves zSapi NULL and the helpers fall back to
* "cli" rather than aborting VM initialization. */
pVm->zSapi = SyMemBackendStrDup(&pVm->sAllocator, "cli", 3);
pVm->bInit = TRUE;
/* Instructions containers */
SySetInit(&pVm->aInstrSet, &pVm->sAllocator, sizeof(VmInstr));
@@ -2732,7 +2753,9 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
return rc;
}
}
/* VM is ready for bytecode execution */
/* VM is ready for bytecode execution. Only now lock VM configuration:
* the import pre-scan above must still observe the pre-ready state. */
pVm->bReady = TRUE;
return VmMountNativeClasses(pVm);
}
/*
@@ -2918,6 +2941,12 @@ PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm) {
SyBlobRelease(&pVm->sArgvEntries);
pVm->iPendingTransfer = 0;
PH7_MemObjRelease(&pVm->sPendingTransfer);
/* Release the SAPI name with the allocator that owns it, before the
* allocator itself goes away. */
if(pVm->zSapi) {
SyMemBackendFree(&pVm->sAllocator, (void *)pVm->zSapi);
pVm->zSapi = 0;
}
/* Release the private memory subsystem */
SyMemBackendRelease(&pVm->sAllocator);
return SXRET_OK;
@@ -3489,6 +3518,42 @@ PH7_PRIVATE sxi32 PH7_VmConfigure(
/* Run-Time Error report */
pVm->bErrReport = 1;
break;
case PH7_VM_CONFIG_SAPI: {
/* SAPI name: nonempty, only [a-z0-9-]. Immutable once ready. */
const char *zSapi = va_arg(ap, const char *);
const char *zScan;
char *zDup;
if(pVm->bReady) {
/* The module pre-scan already ran; refuse late changes. */
rc = SXERR_INVALID;
break;
}
if(SX_EMPTY_STR(zSapi)) {
rc = SXERR_INVALID;
break;
}
for(zScan = zSapi ; *zScan ; ++zScan) {
if(!((*zScan >= 'a' && *zScan <= 'z') || (*zScan >= '0' && *zScan <= '9') || *zScan == '-')) {
rc = SXERR_INVALID;
break;
}
}
if(rc != SXRET_OK) {
break;
}
/* Duplicate before releasing the old value: an allocation
* failure must leave the current SAPI untouched. */
zDup = SyMemBackendStrDup(&pVm->sAllocator, zSapi, (sxu32)SyStrlen(zSapi));
if(zDup == 0) {
rc = SXERR_MEM;
break;
}
if(pVm->zSapi) {
SyMemBackendFree(&pVm->sAllocator, (void *)pVm->zSapi);
}
pVm->zSapi = zDup;
break;
}
case PH7_VM_CONFIG_CREATE_SUPER:
case PH7_VM_CONFIG_CREATE_VAR: {
/* Create a new superglobal/global variable */
@@ -3874,6 +3939,7 @@ PH7_PRIVATE sxi32 PH7_VmSetModuleData(ph7_vm *pVm, const char *zModule,
* execution of the same bytecode after ph7_vm_reset() sees the module still
* registered in aModules and reports "already loaded". */
#define VM_IMPORT_REPLAYED 7
#define VM_IMPORT_FAIL_SAPI 8
/* Do the actual dlopen()/initializeModule() work. Warnings are suppressed in
* the pre-scan (there is no script frame yet, so they would point at
* [MEMORY]:1); the instruction instead records what the runtime must report. */
@@ -3884,6 +3950,7 @@ static sxi32 VmImportModule(ph7_vm *pVm, const char *zModule, VmInstr *pInstr,
char *zFile;
int nFile;
sxu32 nSize, nSlot;
sxi32 iInitRc;
sxi32 (*init)(ph7_vm *, ph7_real *, SyString *);
int nLen = SyStrlen(zModule);
if(nLen < 1) {
@@ -3939,9 +4006,24 @@ static sxi32 VmImportModule(ph7_vm *pVm, const char *zModule, VmInstr *pInstr,
SyMemBackendFree(&pVm->sAllocator, zFile);
return SXRET_OK;
}
/* Init may publish callbacks even on failure. Retain its handle until VM
* release, but only successful imports enter the lookup registry. */
if(init(pVm, &pModule.fVer, &pModule.sDesc) != SXRET_OK) {
/* Other init failures may publish callbacks even on failure. Retain their
* handles until VM release, but only successful imports enter the lookup registry. */
iInitRc = init(pVm, &pModule.fVer, &pModule.sDesc);
if(iInitRc == PH7_MODULE_SAPI_DENIED) {
/* The module declined this SAPI before publishing anything: remove the
* handle retained above and unload now so VM release cannot close it
* a second time. Other init failures keep their handle on purpose. */
pInstr->iP1 = VM_IMPORT_FAILED; pInstr->iP2 = VM_IMPORT_FAIL_SAPI;
(void)SySetPop(&pVm->aModuleHandles);
#ifdef __WINNT__
FreeLibrary(pModule.pHandle);
#else
dlclose(pModule.pHandle);
#endif
SyMemBackendFree(&pVm->sAllocator, zFile);
return SXRET_OK;
}
if(iInitRc != SXRET_OK) {
pInstr->iP1 = VM_IMPORT_FAILED; pInstr->iP2 = VM_IMPORT_FAIL_INIT;
} else {
nSize = pVm->aModules.nSize;
@@ -3978,6 +4060,11 @@ static void VmImportReport(ph7_vm *pVm, const char *zModule, VmInstr *pInstr) {
case VM_IMPORT_FAIL_REGISTER:
PH7_VmThrowError(pVm, PH7_CTX_WARNING, "ImportError: Cannot register module '%s'", zModule);
break;
case VM_IMPORT_FAIL_SAPI:
PH7_VmThrowError(pVm, PH7_CTX_WARNING,
"ImportError: Module '%s' is not available under the '%s' SAPI",
zModule, PH7_VmSapiName(pVm));
break;
}
}
/*
@@ -7554,7 +7641,7 @@ Unwind:
iTransfer = VM_TRANSFER_THROW;
nTarget = pc + 1;
}
/* Freeze a completion before running handlers. Each invocation consumes its
/* Seal a completion before running handlers. Each invocation consumes its
* own regions inside-out; a handler completion replaces, never combines with,
* the pending completion. The compiled finally remains reusable. */
while(SySetUsed(&aRegion)) {