1
/* vm.c - the arithmetic stack of a virtual machine.
3
* Module begun 2008-02-22 by Rainer Gerhards
5
* Copyright 2008 Rainer Gerhards and Adiscon GmbH.
7
* This file is part of the rsyslog runtime library.
9
* The rsyslog runtime library is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation, either version 3 of the License, or
12
* (at your option) any later version.
14
* The rsyslog runtime library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public License
20
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
22
* A copy of the GPL can be found in the file "COPYING" in this distribution.
23
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
34
#include "stringbuf.h"
43
/* ------------------------------ instruction set implementation ------------------------------ *
44
* The following functions implement the VM's instruction set.
46
#define BEGINop(instruction) \
47
static rsRetVal op##instruction(vm_t *pThis, __attribute__((unused)) vmop_t *pOp) \
51
#define CODESTARTop(instruction) \
52
ISOBJ_TYPE_assert(pThis, vm);
54
#define PUSHRESULTop(operand, res) \
55
/* we have a result, so let's push it */ \
56
var.SetNumber(operand, res); \
57
vmstk.Push(pThis->pStk, operand); /* result */
59
#define ENDop(instruction) \
63
/* code generator for boolean operations */
64
#define BOOLOP(name, OPERATION) \
65
BEGINop(name) /* remember to set the instruction also in the ENDop macro! */ \
69
vmstk.PopBool(pThis->pStk, &operand1); \
70
vmstk.PopBool(pThis->pStk, &operand2); \
71
if(operand1->val.num OPERATION operand2->val.num) { \
72
CHKiRet(var.SetNumber(operand1, 1)); \
74
CHKiRet(var.SetNumber(operand1, 0)); \
76
vmstk.Push(pThis->pStk, operand1); /* result */ \
77
var.Destruct(&operand2); /* no longer needed */ \
85
/* code generator for numerical operations */
86
#define NUMOP(name, OPERATION) \
87
BEGINop(name) /* remember to set the instruction also in the ENDop macro! */ \
91
vmstk.PopNumber(pThis->pStk, &operand1); \
92
vmstk.PopNumber(pThis->pStk, &operand2); \
93
operand1->val.num = operand1->val.num OPERATION operand2->val.num; \
94
vmstk.Push(pThis->pStk, operand1); /* result */ \
95
var.Destruct(&operand2); /* no longer needed */ \
105
/* code generator for compare operations */
106
#define BEGINCMPOP(name) \
112
CHKiRet(vmstk.Pop2CommOp(pThis->pStk, &operand1, &operand2)); \
113
/* data types are equal (so we look only at operand1), but we must \
114
* check which type we have to deal with... \
116
switch(operand1->varType) {
117
#define ENDCMPOP(name) \
119
bRes = 0; /* we do not abort just so that we have a value. TODO: reconsider */ \
123
/* we have a result, so let's push it */ \
124
var.SetNumber(operand1, bRes); \
125
vmstk.Push(pThis->pStk, operand1); /* result */ \
126
var.Destruct(&operand2); /* no longer needed */ \
130
BEGINCMPOP(CMP_EQ) /* remember to change the name also in the END macro! */
132
bRes = operand1->val.num == operand2->val.num;
135
bRes = !rsCStrCStrCmp(operand1->val.pStr, operand2->val.pStr);
139
BEGINCMPOP(CMP_NEQ) /* remember to change the name also in the END macro! */
141
bRes = operand1->val.num != operand2->val.num;
144
bRes = rsCStrCStrCmp(operand1->val.pStr, operand2->val.pStr);
148
BEGINCMPOP(CMP_LT) /* remember to change the name also in the END macro! */
150
bRes = operand1->val.num < operand2->val.num;
153
bRes = rsCStrCStrCmp(operand1->val.pStr, operand2->val.pStr) < 0;
157
BEGINCMPOP(CMP_GT) /* remember to change the name also in the END macro! */
159
bRes = operand1->val.num > operand2->val.num;
162
bRes = rsCStrCStrCmp(operand1->val.pStr, operand2->val.pStr) > 0;
166
BEGINCMPOP(CMP_LTEQ) /* remember to change the name also in the END macro! */
168
bRes = operand1->val.num <= operand2->val.num;
171
bRes = rsCStrCStrCmp(operand1->val.pStr, operand2->val.pStr) <= 0;
175
BEGINCMPOP(CMP_GTEQ) /* remember to change the name also in the END macro! */
177
bRes = operand1->val.num >= operand2->val.num;
180
bRes = rsCStrCStrCmp(operand1->val.pStr, operand2->val.pStr) >= 0;
186
/* end regular compare operations */
188
/* comare operations that work on strings, only */
189
BEGINop(CMP_CONTAINS) /* remember to set the instruction also in the ENDop macro! */
193
CODESTARTop(CMP_CONTAINS)
194
/* operand2 is on top of stack, so needs to be popped first */
195
vmstk.PopString(pThis->pStk, &operand2);
196
vmstk.PopString(pThis->pStk, &operand1);
197
/* TODO: extend cstr class so that it supports location of cstr inside cstr */
198
bRes = (rsCStrLocateInSzStr(operand2->val.pStr, rsCStrGetSzStr(operand1->val.pStr)) == -1) ? 0 : 1;
200
/* we have a result, so let's push it */
201
RUNLOG_VAR("%lld", bRes); \
202
PUSHRESULTop(operand1, bRes);
203
var.Destruct(&operand2); /* no longer needed */
207
BEGINop(CMP_CONTAINSI) /* remember to set the instruction also in the ENDop macro! */
211
CODESTARTop(CMP_CONTAINSI)
212
/* operand2 is on top of stack, so needs to be popped first */
213
vmstk.PopString(pThis->pStk, &operand2);
214
vmstk.PopString(pThis->pStk, &operand1);
215
var.DebugPrint(operand1); \
216
var.DebugPrint(operand2); \
217
/* TODO: extend cstr class so that it supports location of cstr inside cstr */
218
bRes = (rsCStrCaseInsensitiveLocateInSzStr(operand2->val.pStr, rsCStrGetSzStr(operand1->val.pStr)) == -1) ? 0 : 1;
220
/* we have a result, so let's push it */
221
RUNLOG_VAR("%lld", bRes); \
222
PUSHRESULTop(operand1, bRes);
223
var.Destruct(&operand2); /* no longer needed */
227
BEGINop(CMP_STARTSWITH) /* remember to set the instruction also in the ENDop macro! */
231
CODESTARTop(CMP_STARTSWITH)
232
/* operand2 is on top of stack, so needs to be popped first */
233
vmstk.PopString(pThis->pStk, &operand2);
234
vmstk.PopString(pThis->pStk, &operand1);
235
/* TODO: extend cstr class so that it supports location of cstr inside cstr */
236
bRes = (rsCStrStartsWithSzStr(operand1->val.pStr, rsCStrGetSzStr(operand2->val.pStr),
237
rsCStrLen(operand2->val.pStr)) == 0) ? 1 : 0;
239
/* we have a result, so let's push it */
240
RUNLOG_VAR("%lld", bRes); \
241
PUSHRESULTop(operand1, bRes);
242
var.Destruct(&operand2); /* no longer needed */
243
ENDop(CMP_STARTSWITH)
246
BEGINop(CMP_STARTSWITHI) /* remember to set the instruction also in the ENDop macro! */
250
CODESTARTop(CMP_STARTSWITHI)
251
/* operand2 is on top of stack, so needs to be popped first */
252
vmstk.PopString(pThis->pStk, &operand2);
253
vmstk.PopString(pThis->pStk, &operand1);
254
/* TODO: extend cstr class so that it supports location of cstr inside cstr */
255
bRes = (rsCStrCaseInsensitveStartsWithSzStr(operand1->val.pStr, rsCStrGetSzStr(operand2->val.pStr),
256
rsCStrLen(operand2->val.pStr)) == 0) ? 1 : 0;
258
/* we have a result, so let's push it */
259
PUSHRESULTop(operand1, bRes);
260
var.Destruct(&operand2); /* no longer needed */
261
ENDop(CMP_STARTSWITHI)
263
/* end comare operations that work on strings, only */
265
BEGINop(STRADD) /* remember to set the instruction also in the ENDop macro! */
269
vmstk.PopString(pThis->pStk, &operand2);
270
vmstk.PopString(pThis->pStk, &operand1);
272
CHKiRet(rsCStrAppendCStr(operand1->val.pStr, operand2->val.pStr));
274
/* we have a result, so let's push it */
275
vmstk.Push(pThis->pStk, operand1);
276
var.Destruct(&operand2); /* no longer needed */
280
BEGINop(NOT) /* remember to set the instruction also in the ENDop macro! */
283
vmstk.PopBool(pThis->pStk, &operand);
284
PUSHRESULTop(operand, !operand->val.num);
287
BEGINop(UNARY_MINUS) /* remember to set the instruction also in the ENDop macro! */
289
CODESTARTop(UNARY_MINUS)
290
vmstk.PopNumber(pThis->pStk, &operand);
291
PUSHRESULTop(operand, -operand->val.num);
295
BEGINop(PUSHCONSTANT) /* remember to set the instruction also in the ENDop macro! */
296
var_t *pVarDup; /* we need to duplicate the var, as we need to hand it over */
297
CODESTARTop(PUSHCONSTANT)
298
CHKiRet(var.Duplicate(pOp->operand.pVar, &pVarDup));
299
vmstk.Push(pThis->pStk, pVarDup);
304
BEGINop(PUSHMSGVAR) /* remember to set the instruction also in the ENDop macro! */
305
var_t *pVal; /* the value to push */
307
CODESTARTop(PUSHMSGVAR)
308
if(pThis->pMsg == NULL) {
309
/* TODO: flag an error message! As a work-around, we permit
310
* execution to continue here with an empty string
312
/* TODO: create a method in var to create a string var? */
313
CHKiRet(var.Construct(&pVal));
314
CHKiRet(var.ConstructFinalize(pVal));
315
CHKiRet(rsCStrConstructFromszStr(&pstrVal, (uchar*)""));
316
CHKiRet(var.SetString(pVal, pstrVal));
318
/* we have a message, so pull value from there */
319
CHKiRet(msgGetMsgVar(pThis->pMsg, pOp->operand.pVar->val.pStr, &pVal));
322
/* if we reach this point, we have a valid pVal and can push it */
323
vmstk.Push(pThis->pStk, pVal);
328
BEGINop(PUSHSYSVAR) /* remember to set the instruction also in the ENDop macro! */
329
var_t *pVal; /* the value to push */
330
CODESTARTop(PUSHSYSVAR)
331
CHKiRet(sysvar.GetVar(pOp->operand.pVar->val.pStr, &pVal));
332
vmstk.Push(pThis->pStk, pVal);
337
/* ------------------------------ end instruction set implementation ------------------------------ */
340
/* Standard-Constructor
342
BEGINobjConstruct(vm) /* be sure to specify the object type also in END macro! */
346
/* ConstructionFinalizer
347
* rgerhards, 2008-01-09
350
vmConstructFinalize(vm_t __attribute__((unused)) *pThis)
353
ISOBJ_TYPE_assert(pThis, vm);
355
CHKiRet(vmstk.Construct(&pThis->pStk));
356
CHKiRet(vmstk.ConstructFinalize(pThis->pStk));
363
/* destructor for the vm object */
364
BEGINobjDestruct(vm) /* be sure to specify the object type also in END and CODESTART macros! */
365
CODESTARTobjDestruct(vm)
366
if(pThis->pStk != NULL)
367
vmstk.Destruct(&pThis->pStk);
368
if(pThis->pMsg != NULL)
369
msgDestruct(&pThis->pMsg);
373
/* debugprint for the vm object */
374
BEGINobjDebugPrint(vm) /* be sure to specify the object type also in END and CODESTART macros! */
375
CODESTARTobjDebugPrint(vm)
376
dbgoprint((obj_t*) pThis, "rsyslog virtual machine, currently no state info available\n");
383
execProg(vm_t *pThis, vmprg_t *pProg)
386
vmop_t *pCurrOp; /* virtual instruction pointer */
388
ISOBJ_TYPE_assert(pThis, vm);
389
ISOBJ_TYPE_assert(pProg, vmprg);
391
#define doOP(OP) case opcode_##OP: CHKiRet(op##OP(pThis, pCurrOp)); break
392
pCurrOp = pProg->vmopRoot; /* TODO: do this via a method! */
393
while(pCurrOp != NULL && pCurrOp->opcode != opcode_END_PROG) {
394
switch(pCurrOp->opcode) {
405
doOP(CMP_STARTSWITH);
406
doOP(CMP_STARTSWITHI);
419
ABORT_FINALIZE(RS_RET_INVALID_VMOP);
420
dbgoprint((obj_t*) pThis, "invalid instruction %d in vmprg\n", pCurrOp->opcode);
423
/* so far, we have plain sequential execution, so on to next... */
424
pCurrOp = pCurrOp->pNext;
428
/* if we reach this point, our program has intintionally terminated
437
/* Set the current message object for the VM. It *is* valid to set a
438
* NULL message object, what simply means there is none. Message
439
* objects are properly reference counted.
442
SetMsg(vm_t *pThis, msg_t *pMsg)
445
if(pThis->pMsg != NULL) {
446
msgDestruct(&pThis->pMsg);
450
pThis->pMsg = MsgAddRef(pMsg);
457
/* Pop a var from the stack and return it to caller. The variable type is not
458
* changed, it is taken from the stack as is. This functionality is
459
* partly needed. We may (or may not ;)) be able to remove it once we have
460
* full RainerScript support. -- rgerhards, 2008-02-25
463
PopVarFromStack(vm_t *pThis, var_t **ppVar)
466
CHKiRet(vmstk.Pop(pThis->pStk, ppVar));
472
/* Pop a boolean from the stack and return it to caller. This functionality is
473
* partly needed. We may (or may not ;)) be able to remove it once we have
474
* full RainerScript support. -- rgerhards, 2008-02-25
477
PopBoolFromStack(vm_t *pThis, var_t **ppVar)
480
CHKiRet(vmstk.PopBool(pThis->pStk, ppVar));
486
/* queryInterface function
487
* rgerhards, 2008-02-21
489
BEGINobjQueryInterface(vm)
490
CODESTARTobjQueryInterface(vm)
491
if(pIf->ifVersion != vmCURR_IF_VERSION) { /* check for current version, increment on each change */
492
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
495
/* ok, we have the right interface, so let's fill it
496
* Please note that we may also do some backwards-compatibility
497
* work here (if we can support an older interface version - that,
498
* of course, also affects the "if" above).
500
pIf->Construct = vmConstruct;
501
pIf->ConstructFinalize = vmConstructFinalize;
502
pIf->Destruct = vmDestruct;
503
pIf->DebugPrint = vmDebugPrint;
504
pIf->ExecProg = execProg;
505
pIf->PopBoolFromStack = PopBoolFromStack;
506
pIf->PopVarFromStack = PopVarFromStack;
507
pIf->SetMsg = SetMsg;
509
ENDobjQueryInterface(vm)
512
/* Initialize the vm class. Must be called as the very first method
513
* before anything else is called inside this class.
514
* rgerhards, 2008-02-19
516
BEGINObjClassInit(vm, 1, OBJ_IS_CORE_MODULE) /* class, version */
517
/* request objects we use */
518
CHKiRet(objUse(vmstk, CORE_COMPONENT));
519
CHKiRet(objUse(var, CORE_COMPONENT));
520
CHKiRet(objUse(sysvar, CORE_COMPONENT));
522
/* set our own handlers */
523
OBJSetMethodHandler(objMethod_DEBUGPRINT, vmDebugPrint);
524
OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, vmConstructFinalize);