[bgp/vm] Drop vm->isRunning and other metadata, Bgp_VmExec() could leave the VM in an inconsistent state on longjmp(), causing troubles

This commit is contained in:
Lorenzo Cogotti 2021-06-08 00:07:48 +02:00
parent 739f798d11
commit 2480f1fa75
2 changed files with 1 additions and 22 deletions

View File

@ -69,8 +69,6 @@ Judgement Bgp_InitVm(Bgpvm *vm, size_t heapSiz)
Judgement Bgp_VmEmit(Bgpvm *vm, Bgpvmbytec bytec) Judgement Bgp_VmEmit(Bgpvm *vm, Bgpvmbytec bytec)
{ {
assert(!vm->isRunning);
BGP_VMCLRERR(vm); BGP_VMCLRERR(vm);
if (BGP_VMOPC(bytec) == BGP_VMOP_END) if (BGP_VMOPC(bytec) == BGP_VMOP_END)
@ -100,8 +98,6 @@ Judgement Bgp_VmEmit(Bgpvm *vm, Bgpvmbytec bytec)
void *Bgp_VmPermAlloc(Bgpvm *vm, size_t size) void *Bgp_VmPermAlloc(Bgpvm *vm, size_t size)
{ {
assert(!vm->isRunning);
BGP_VMCLRERR(vm); BGP_VMCLRERR(vm);
size = ALIGN(size, ALIGNMENT); size = ALIGN(size, ALIGNMENT);
@ -124,8 +120,6 @@ void *Bgp_VmPermAlloc(Bgpvm *vm, size_t size)
void *Bgp_VmTempAlloc(Bgpvm *vm, size_t size) void *Bgp_VmTempAlloc(Bgpvm *vm, size_t size)
{ {
assert(vm->isRunning);
size = ALIGN(size, ALIGNMENT); size = ALIGN(size, ALIGNMENT);
size_t stksiz = vm->si * sizeof(Bgpvmval); size_t stksiz = vm->si * sizeof(Bgpvmval);
@ -144,8 +138,6 @@ void *Bgp_VmTempAlloc(Bgpvm *vm, size_t size)
void Bgp_VmTempFree(Bgpvm *vm, size_t size) void Bgp_VmTempFree(Bgpvm *vm, size_t size)
{ {
assert(vm->isRunning);
size = ALIGN(size, ALIGNMENT); size = ALIGN(size, ALIGNMENT);
assert(size + vm->hHighMark <= vm->hMemSiz); assert(size + vm->hHighMark <= vm->hMemSiz);
@ -155,8 +147,6 @@ void Bgp_VmTempFree(Bgpvm *vm, size_t size)
Boolean Bgp_VmExec(Bgpvm *vm, Bgpmsg *msg) Boolean Bgp_VmExec(Bgpvm *vm, Bgpmsg *msg)
{ {
// Fundamental sanity checks // Fundamental sanity checks
assert(!vm->isRunning);
if (vm->setupFailed) UNLIKELY { if (vm->setupFailed) UNLIKELY {
vm->errCode = BGPEBADVM; vm->errCode = BGPEBADVM;
goto cant_run; goto cant_run;
@ -177,7 +167,7 @@ Boolean Bgp_VmExec(Bgpvm *vm, Bgpmsg *msg)
vm->msg = msg; vm->msg = msg;
vm->curMatch = &discardMatch; vm->curMatch = &discardMatch;
vm->matches = NULL; vm->matches = NULL;
vm->errCode = BGPENOERR; BGP_VMCLRERR(vm);
// Populate computed goto table if necessary // Populate computed goto table if necessary
#ifdef DF_BGP_VM_USES_COMPUTED_GOTO #ifdef DF_BGP_VM_USES_COMPUTED_GOTO
@ -187,7 +177,6 @@ Boolean Bgp_VmExec(Bgpvm *vm, Bgpmsg *msg)
// Execute bytecode according to the #included vm_<impl>def.h // Execute bytecode according to the #included vm_<impl>def.h
Bgpvmbytec ir; // Instruction Register Bgpvmbytec ir; // Instruction Register
vm->isRunning = TRUE;
while (TRUE) { while (TRUE) {
// FETCH stage // FETCH stage
FETCH(ir, vm); FETCH(ir, vm);
@ -316,9 +305,6 @@ Boolean Bgp_VmExec(Bgpvm *vm, Bgpmsg *msg)
} }
terminate: terminate:
vm->curMatch = NULL; // prevent accidental access outside Bgp_VmExec()
vm->isRunning = FALSE;
if (Bgp_SetErrStat(vm->errCode) != OK) UNLIKELY if (Bgp_SetErrStat(vm->errCode) != OK) UNLIKELY
result = FALSE; result = FALSE;
@ -352,8 +338,6 @@ Judgement Bgp_VmStoreMsgTypeMatch(Bgpvm *vm, Boolean isMatching)
void Bgp_VmStoreMatch(Bgpvm *vm) void Bgp_VmStoreMatch(Bgpvm *vm)
{ {
assert(vm->isRunning);
if (vm->curMatch == &discardMatch) if (vm->curMatch == &discardMatch)
return; // discard store request return; // discard store request
@ -811,8 +795,6 @@ void Bgp_VmDoRelt(Bgpvm *vm, Uint8 arg)
void Bgp_ResetVm(Bgpvm *vm) void Bgp_ResetVm(Bgpvm *vm)
{ {
assert(!vm->isRunning);
vm->nk = 0; vm->nk = 0;
vm->nfuncs = 0; vm->nfuncs = 0;
vm->nmatches = 0; vm->nmatches = 0;
@ -831,8 +813,6 @@ void Bgp_ResetVm(Bgpvm *vm)
void Bgp_ClearVm(Bgpvm *vm) void Bgp_ClearVm(Bgpvm *vm)
{ {
assert(!vm->isRunning);
free(vm->heap); free(vm->heap);
free(vm->prog); free(vm->prog);
} }

View File

@ -241,7 +241,6 @@ struct Bgpvm {
Uint16 nk; ///< count of constants (K) available in `k` Uint16 nk; ///< count of constants (K) available in `k`
Uint8 nfuncs; ///< count of functions (FN) available in `funcs` Uint8 nfuncs; ///< count of functions (FN) available in `funcs`
Boolean8 setupFailed; ///< whether a `Bgp_VmEmit()` or `Bgp_VmPermAlloc()` on this VM ever failed. Boolean8 setupFailed; ///< whether a `Bgp_VmEmit()` or `Bgp_VmPermAlloc()` on this VM ever failed.
Boolean8 isRunning; ///< whether the VM is being executed
BgpvmRet errCode; ///< whether the VM encountered an error BgpvmRet errCode; ///< whether the VM encountered an error
Uint32 hLowMark; ///< VM heap low memory mark Uint32 hLowMark; ///< VM heap low memory mark
Uint32 hHighMark; ///< VM heap high memory mark Uint32 hHighMark; ///< VM heap high memory mark