From cb894f66fd85293f7e3b3cb73d0c39f20f39e672 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Mon, 23 Nov 2026 23:45:16 +0100 Subject: [PATCH] Add VM SAPI configuration option and restrict console module to the CLI SAPI --- engine/vm.c | 97 +++++++++++++++++++++++++++++++++++++-- include/ph7.h | 2 + include/ph7int.h | 4 ++ modules/console/console.c | 3 ++ sapi/cli/main.c | 1 + 5 files changed, 102 insertions(+), 5 deletions(-) diff --git a/engine/vm.c b/engine/vm.c index a0dcd0a..88c96e6 100644 --- a/engine/vm.c +++ b/engine/vm.c @@ -8,6 +8,7 @@ * David Carlier */ #include +#include #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)) { diff --git a/include/ph7.h b/include/ph7.h index d56554e..f86809d 100644 --- a/include/ph7.h +++ b/include/ph7.h @@ -257,6 +257,7 @@ struct SyMutexMethods { #define PH7_LOOKED SXERR_LOCKED /* Forbidden Operation */ #define PH7_COMPILE_ERR (-70) /* Compilation 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 */ /* * 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_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_SAPI 18 /* ONE ARGUMENT: const char *zSapi [a-z0-9-]; set only before the VM is prepared */ /* * Global Library Configuration Commands. * diff --git a/include/ph7int.h b/include/ph7int.h index 0a46d63..c45ed39 100644 --- a/include/ph7int.h +++ b/include/ph7int.h @@ -1270,6 +1270,8 @@ struct ph7_vm { sxbool bInit; /* Unpublished bootstrap: errors return to bulk cleanup */ sxi32 iInitError; /* Sticky bootstrap compiler failure */ 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 */ ph7_class *pStagedClass; /* Unpublished declaration visible only to preparation */ 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 PH7_VmDump(ph7_vm *pVm, ProcConsumer xConsumer, void *pUserData); 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_VmByteCodeExec(ph7_vm *pVm); PH7_PRIVATE sxi32 PH7_VmRelease(ph7_vm *pVm); diff --git a/modules/console/console.c b/modules/console/console.c index d82e29b..b898a45 100644 --- a/modules/console/console.c +++ b/modules/console/console.c @@ -652,6 +652,9 @@ Cleanup: } PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) { + if(!PH7_VmSapiIs(pVm, "cli")) { + return PH7_MODULE_SAPI_DENIED; + } #ifdef __WINNT__ /* Enable virtual terminal processing once, at module initialization, * never from a redirected-state query. */ diff --git a/sapi/cli/main.c b/sapi/cli/main.c index 4e4f569..6c8e513 100644 --- a/sapi/cli/main.c +++ b/sapi/cli/main.c @@ -151,6 +151,7 @@ int main(int argc, char **argv) { 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. * We will install the VM output consumer callback defined above