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> * David Carlier <devnexen@gmail.com>
*/ */
#include <math.h> #include <math.h>
#include <string.h>
#include "ph7int.h" #include "ph7int.h"
/* /*
@@ -2412,6 +2413,22 @@ PH7_PRIVATE ph7_value *VmReserveMemObj(ph7_vm *pVm, sxu32 *pIndex) {
} }
/* Forward declaration */ /* Forward declaration */
static sxi32 VmEvalChunk(ph7_vm *pVm, ph7_context *pCtx, SyString *pChunk, int iFlags, ph7_value *pResult, ph7_include *pInclude); 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 * Initialize a freshly allocated PH7 Virtual Machine so that we can
* start compiling the target PHP program. * start compiling the target PHP program.
@@ -2431,6 +2448,10 @@ PH7_PRIVATE sxi32 PH7_VmInit(
if(rc != SXRET_OK) { if(rc != SXRET_OK) {
return rc; 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; pVm->bInit = TRUE;
/* Instructions containers */ /* Instructions containers */
SySetInit(&pVm->aInstrSet, &pVm->sAllocator, sizeof(VmInstr)); SySetInit(&pVm->aInstrSet, &pVm->sAllocator, sizeof(VmInstr));
@@ -2732,7 +2753,9 @@ PH7_PRIVATE sxi32 PH7_VmMakeReady(
return rc; 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); return VmMountNativeClasses(pVm);
} }
/* /*
@@ -2918,6 +2941,12 @@ PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm) {
SyBlobRelease(&pVm->sArgvEntries); SyBlobRelease(&pVm->sArgvEntries);
pVm->iPendingTransfer = 0; pVm->iPendingTransfer = 0;
PH7_MemObjRelease(&pVm->sPendingTransfer); 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 */ /* Release the private memory subsystem */
SyMemBackendRelease(&pVm->sAllocator); SyMemBackendRelease(&pVm->sAllocator);
return SXRET_OK; return SXRET_OK;
@@ -3489,6 +3518,42 @@ PH7_PRIVATE sxi32 PH7_VmConfigure(
/* Run-Time Error report */ /* Run-Time Error report */
pVm->bErrReport = 1; pVm->bErrReport = 1;
break; 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_SUPER:
case PH7_VM_CONFIG_CREATE_VAR: { case PH7_VM_CONFIG_CREATE_VAR: {
/* Create a new superglobal/global variable */ /* 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 * execution of the same bytecode after ph7_vm_reset() sees the module still
* registered in aModules and reports "already loaded". */ * registered in aModules and reports "already loaded". */
#define VM_IMPORT_REPLAYED 7 #define VM_IMPORT_REPLAYED 7
#define VM_IMPORT_FAIL_SAPI 8
/* Do the actual dlopen()/initializeModule() work. Warnings are suppressed in /* Do the actual dlopen()/initializeModule() work. Warnings are suppressed in
* the pre-scan (there is no script frame yet, so they would point at * 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. */ * [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; char *zFile;
int nFile; int nFile;
sxu32 nSize, nSlot; sxu32 nSize, nSlot;
sxi32 iInitRc;
sxi32 (*init)(ph7_vm *, ph7_real *, SyString *); sxi32 (*init)(ph7_vm *, ph7_real *, SyString *);
int nLen = SyStrlen(zModule); int nLen = SyStrlen(zModule);
if(nLen < 1) { if(nLen < 1) {
@@ -3939,9 +4006,24 @@ static sxi32 VmImportModule(ph7_vm *pVm, const char *zModule, VmInstr *pInstr,
SyMemBackendFree(&pVm->sAllocator, zFile); SyMemBackendFree(&pVm->sAllocator, zFile);
return SXRET_OK; return SXRET_OK;
} }
/* Init may publish callbacks even on failure. Retain its handle until VM /* Other init failures may publish callbacks even on failure. Retain their
* release, but only successful imports enter the lookup registry. */ * handles until VM release, but only successful imports enter the lookup registry. */
if(init(pVm, &pModule.fVer, &pModule.sDesc) != SXRET_OK) { 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; pInstr->iP1 = VM_IMPORT_FAILED; pInstr->iP2 = VM_IMPORT_FAIL_INIT;
} else { } else {
nSize = pVm->aModules.nSize; nSize = pVm->aModules.nSize;
@@ -3978,6 +4060,11 @@ static void VmImportReport(ph7_vm *pVm, const char *zModule, VmInstr *pInstr) {
case VM_IMPORT_FAIL_REGISTER: case VM_IMPORT_FAIL_REGISTER:
PH7_VmThrowError(pVm, PH7_CTX_WARNING, "ImportError: Cannot register module '%s'", zModule); PH7_VmThrowError(pVm, PH7_CTX_WARNING, "ImportError: Cannot register module '%s'", zModule);
break; 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; iTransfer = VM_TRANSFER_THROW;
nTarget = pc + 1; 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, * own regions inside-out; a handler completion replaces, never combines with,
* the pending completion. The compiled finally remains reusable. */ * the pending completion. The compiled finally remains reusable. */
while(SySetUsed(&aRegion)) { while(SySetUsed(&aRegion)) {
+2
View File
@@ -257,6 +257,7 @@ struct SyMutexMethods {
#define PH7_LOOKED SXERR_LOCKED /* Forbidden Operation */ #define PH7_LOOKED SXERR_LOCKED /* Forbidden Operation */
#define PH7_COMPILE_ERR (-70) /* Compilation error */ #define PH7_COMPILE_ERR (-70) /* Compilation error */
#define PH7_VM_ERR (-71) /* Virtual machine error */ #define PH7_VM_ERR (-71) /* Virtual machine error */
#define PH7_MODULE_SAPI_DENIED 1 /* Returned by initializeModule: module not available for the current SAPI */
/* end-of-error-codes */ /* end-of-error-codes */
/* /*
* If compiling for a processor that lacks floating point * If compiling for a processor that lacks floating point
@@ -318,6 +319,7 @@ typedef sxi64 ph7_int64;
#define PH7_VM_CONFIG_IO_STREAM 15 /* ONE ARGUMENT: const ph7_io_stream *pStream */ #define PH7_VM_CONFIG_IO_STREAM 15 /* ONE ARGUMENT: const ph7_io_stream *pStream */
#define PH7_VM_CONFIG_ARGV_ENTRY 16 /* ONE ARGUMENT: const char *zValue */ #define PH7_VM_CONFIG_ARGV_ENTRY 16 /* ONE ARGUMENT: const char *zValue */
#define PH7_VM_CONFIG_EXTRACT_OUTPUT 17 /* TWO ARGUMENTS: const void **ppOut,unsigned int *pOutputLen */ #define PH7_VM_CONFIG_EXTRACT_OUTPUT 17 /* TWO ARGUMENTS: const void **ppOut,unsigned int *pOutputLen */
#define PH7_VM_CONFIG_SAPI 18 /* ONE ARGUMENT: const char *zSapi [a-z0-9-]; set only before the VM is prepared */
/* /*
* Global Library Configuration Commands. * Global Library Configuration Commands.
* *
+4
View File
@@ -1270,6 +1270,8 @@ struct ph7_vm {
sxbool bInit; /* Unpublished bootstrap: errors return to bulk cleanup */ sxbool bInit; /* Unpublished bootstrap: errors return to bulk cleanup */
sxi32 iInitError; /* Sticky bootstrap compiler failure */ sxi32 iInitError; /* Sticky bootstrap compiler failure */
sxbool bErrReport; /* TRUE to report all runtime Error/Warning/Notice/Deprecated */ sxbool bErrReport; /* TRUE to report all runtime Error/Warning/Notice/Deprecated */
char *zSapi; /* VM-owned copy of the SAPI name (NULL means default "cli") */
sxbool bReady; /* TRUE once PH7_VmMakeReady() completed; no further VM configuration */
SySet *pClassJournal; /* Non-reentrant, callback-free metadata preparation */ SySet *pClassJournal; /* Non-reentrant, callback-free metadata preparation */
ph7_class *pStagedClass; /* Unpublished declaration visible only to preparation */ ph7_class *pStagedClass; /* Unpublished declaration visible only to preparation */
sxi32 bClassDeferred; /* A forward hierarchy name is not installed yet */ sxi32 bClassDeferred; /* A forward hierarchy name is not installed yet */
@@ -1804,6 +1806,8 @@ PH7_PRIVATE void PH7_VmExpandConstantValue(ph7_value *pVal, void *pUserData);
PH7_PRIVATE sxi32 VmExtractDebugTrace(ph7_vm *pVm, SySet *pDebugTrace); PH7_PRIVATE sxi32 VmExtractDebugTrace(ph7_vm *pVm, SySet *pDebugTrace);
PH7_PRIVATE sxi32 PH7_VmDump(ph7_vm *pVm, ProcConsumer xConsumer, void *pUserData); PH7_PRIVATE sxi32 PH7_VmDump(ph7_vm *pVm, ProcConsumer xConsumer, void *pUserData);
PH7_PRIVATE sxi32 PH7_VmInit(ph7_vm *pVm, ph7 *pEngine, sxbool bDebug); PH7_PRIVATE sxi32 PH7_VmInit(ph7_vm *pVm, ph7 *pEngine, sxbool bDebug);
PH7_PRIVATE const char *PH7_VmSapiName(ph7_vm *pVm);
PH7_PRIVATE int PH7_VmSapiIs(ph7_vm *pVm, const char *zSapi);
PH7_PRIVATE sxi32 PH7_VmConfigure(ph7_vm *pVm, sxi32 nOp, va_list ap); PH7_PRIVATE sxi32 PH7_VmConfigure(ph7_vm *pVm, sxi32 nOp, va_list ap);
PH7_PRIVATE sxi32 PH7_VmByteCodeExec(ph7_vm *pVm); PH7_PRIVATE sxi32 PH7_VmByteCodeExec(ph7_vm *pVm);
PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm); PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm);
+3
View File
@@ -652,6 +652,9 @@ Cleanup:
} }
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
if(!PH7_VmSapiIs(pVm, "cli")) {
return PH7_MODULE_SAPI_DENIED;
}
#ifdef __WINNT__ #ifdef __WINNT__
/* Enable virtual terminal processing once, at module initialization, /* Enable virtual terminal processing once, at module initialization,
* never from a redirected-state query. */ * never from a redirected-state query. */
+1
View File
@@ -151,6 +151,7 @@ int main(int argc, char **argv) {
Fatal("VM initialization error"); Fatal("VM initialization error");
} }
} }
ph7_vm_config(pVm, PH7_VM_CONFIG_SAPI, "cli");
/* /*
* Now we have our VM initialized,it's time to configure our VM. * Now we have our VM initialized,it's time to configure our VM.
* We will install the VM output consumer callback defined above * We will install the VM output consumer callback defined above