[Pkg-clamav-commits] [SCM] Debian repository for ClamAV branch, debian/unstable, updated. debian/0.95+dfsg-1-6156-g094ec9b
Török Edvin
edwin at clamav.net
Sun Apr 4 00:56:20 UTC 2010
The following commit has been merged in the debian/unstable branch:
commit 7a9022c944c1b082fc584b0e09b799765abf092b
Author: Török Edvin <edwin at clamav.net>
Date: Tue Jul 7 19:58:37 2009 +0300
bytecode: setup context to invoke a bytecode function.
diff --git a/clambc/bcrun.c b/clambc/bcrun.c
index 846e8c1..6b1acf8 100644
--- a/clambc/bcrun.c
+++ b/clambc/bcrun.c
@@ -92,7 +92,7 @@ int main(int argc, char *argv[])
fclose(f);
printf("Bytecode loaded\n");
- ctx = cli_bytecode_alloc_context();
+ ctx = cli_bytecode_context_alloc();
if (!ctx) {
fprintf(stderr,"Out of memory\n");
exit(3);
@@ -101,7 +101,7 @@ int main(int argc, char *argv[])
printf("Running bytecode\n");
cli_bytecode_run(bc, ctx);
printf("Bytecode run finished\n");
- cli_bytecode_destroy_context(ctx);
+ cli_bytecode_context_destroy(ctx);
cli_bytecode_destroy(bc);
free(bc);
optfree(opts);
diff --git a/libclamav/Makefile.am b/libclamav/Makefile.am
index bf6e48a..9f14134 100644
--- a/libclamav/Makefile.am
+++ b/libclamav/Makefile.am
@@ -288,6 +288,8 @@ libclamav_la_SOURCES = \
bignum.h\
bytecode.c\
bytecode.h\
+ bytecode_vm.c\
+ bytecode_priv.h\
clambc.h \
cpio.c \
cpio.h
diff --git a/libclamav/Makefile.in b/libclamav/Makefile.in
index 0e2e1f1..7d036d8 100644
--- a/libclamav/Makefile.in
+++ b/libclamav/Makefile.in
@@ -121,8 +121,8 @@ am__libclamav_la_SOURCES_DIST = clamav.h matcher-ac.c matcher-ac.h \
textnorm.h dlp.c dlp.h jsparse/js-norm.c jsparse/js-norm.h \
jsparse/lexglobal.h jsparse/textbuf.h uniq.c uniq.h version.c \
version.h mpool.c mpool.h default.h sha256.c sha256.h bignum.h \
- bytecode.c bytecode.h clambc.h cpio.c cpio.h bignum.c \
- bignum_class.h
+ bytecode.c bytecode.h bytecode_vm.c bytecode_priv.h clambc.h \
+ cpio.c cpio.h bignum.c bignum_class.h
@LINK_TOMMATH_FALSE at am__objects_1 = libclamav_la-bignum.lo
am_libclamav_la_OBJECTS = libclamav_la-matcher-ac.lo \
libclamav_la-matcher-bm.lo libclamav_la-matcher.lo \
@@ -160,7 +160,8 @@ am_libclamav_la_OBJECTS = libclamav_la-matcher-ac.lo \
libclamav_la-js-norm.lo libclamav_la-uniq.lo \
libclamav_la-version.lo libclamav_la-mpool.lo \
libclamav_la-sha256.lo libclamav_la-bytecode.lo \
- libclamav_la-cpio.lo $(am__objects_1)
+ libclamav_la-bytecode_vm.lo libclamav_la-cpio.lo \
+ $(am__objects_1)
libclamav_la_OBJECTS = $(am_libclamav_la_OBJECTS)
libclamav_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
$(LIBTOOLFLAGS) --mode=link $(CCLD) $(libclamav_la_CFLAGS) \
@@ -530,8 +531,8 @@ libclamav_la_SOURCES = clamav.h matcher-ac.c matcher-ac.h matcher-bm.c \
explode.h textnorm.c textnorm.h dlp.c dlp.h jsparse/js-norm.c \
jsparse/js-norm.h jsparse/lexglobal.h jsparse/textbuf.h uniq.c \
uniq.h version.c version.h mpool.c mpool.h default.h sha256.c \
- sha256.h bignum.h bytecode.c bytecode.h clambc.h cpio.c cpio.h \
- $(am__append_7)
+ sha256.h bignum.h bytecode.c bytecode.h bytecode_vm.c \
+ bytecode_priv.h clambc.h cpio.c cpio.h $(am__append_7)
noinst_LTLIBRARIES = libclamav_internal_utils.la libclamav_internal_utils_nothreads.la
COMMON_CLEANFILES = version.h version.h.tmp *.gcda *.gcno lzma/*.gcda lzma/*.gcno
@MAINTAINER_MODE_TRUE at BUILT_SOURCES = jsparse/generated/operators.h jsparse/generated/keywords.h jsparse-keywords.gperf
@@ -647,6 +648,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-binhex.Plo at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-blob.Plo at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-bytecode.Plo at am__quote@
+ at AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-bytecode_vm.Plo at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-bzlib.Plo at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-cab.Plo at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/libclamav_la-chmunpack.Plo at am__quote@
@@ -1250,6 +1252,13 @@ libclamav_la-bytecode.lo: bytecode.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-bytecode.lo `test -f 'bytecode.c' || echo '$(srcdir)/'`bytecode.c
+libclamav_la-bytecode_vm.lo: bytecode_vm.c
+ at am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-bytecode_vm.lo -MD -MP -MF $(DEPDIR)/libclamav_la-bytecode_vm.Tpo -c -o libclamav_la-bytecode_vm.lo `test -f 'bytecode_vm.c' || echo '$(srcdir)/'`bytecode_vm.c
+ at am__fastdepCC_TRUE@ mv -f $(DEPDIR)/libclamav_la-bytecode_vm.Tpo $(DEPDIR)/libclamav_la-bytecode_vm.Plo
+ at AMDEP_TRUE@@am__fastdepCC_FALSE@ source='bytecode_vm.c' object='libclamav_la-bytecode_vm.lo' libtool=yes @AMDEPBACKSLASH@
+ at AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+ at am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-bytecode_vm.lo `test -f 'bytecode_vm.c' || echo '$(srcdir)/'`bytecode_vm.c
+
libclamav_la-cpio.lo: cpio.c
@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-cpio.lo -MD -MP -MF $(DEPDIR)/libclamav_la-cpio.Tpo -c -o libclamav_la-cpio.lo `test -f 'cpio.c' || echo '$(srcdir)/'`cpio.c
@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/libclamav_la-cpio.Tpo $(DEPDIR)/libclamav_la-cpio.Plo
diff --git a/libclamav/bytecode.c b/libclamav/bytecode.c
index a3d0f61..c0f6f4b 100644
--- a/libclamav/bytecode.c
+++ b/libclamav/bytecode.c
@@ -1,5 +1,5 @@
/*
- * Load, verify and execute ClamAV bytecode.
+ * Load, and verify ClamAV bytecode.
*
* Copyright (C) 2009 Sourcefire, Inc.
*
@@ -27,79 +27,87 @@
#include "clamav.h"
#include "others.h"
#include "bytecode.h"
+#include "bytecode_priv.h"
#include "readdb.h"
#include <string.h>
-typedef uint32_t operand_t;
-typedef uint16_t bbid_t;
-typedef uint16_t funcid_t;
-
-struct cli_bc_callop {
- operand_t* ops;
- uint8_t numOps;
- funcid_t funcid;
-};
-
-struct branch {
- operand_t condition;
- bbid_t br_true;
- bbid_t br_false;
-};
-
-#define MAX_OP (operand_t)(~0u)
-#define CONSTANT_OP (MAX_OP-1)
-#define ARG_OP (MAX_OP-1)
-struct cli_bc_value {
- uint64_t v;
- operand_t ref;/* this has CONSTANT_OP value for constants, and ARG_op for arguments */
-};
-
-struct cli_bc_inst {
- enum bc_opcode opcode;
- uint16_t type;
- union {
- operand_t unaryop;
- operand_t binop[2];
- operand_t three[3];
- struct cli_bc_callop ops;
- struct branch branch;
- bbid_t jump;
- } u;
-};
+struct cli_bc_ctx *cli_bytecode_context_alloc(void)
+{
+ struct cli_bc_ctx *ctx = cli_malloc(sizeof(*ctx));
+ ctx->bc = NULL;
+ ctx->func = NULL;
+ ctx->values = NULL;
+ ctx->operands = NULL;
+ return ctx;
+}
-struct cli_bc_bb {
- unsigned numInsts;
- struct cli_bc_inst *insts;
-};
+void cli_bytecode_context_destroy(struct cli_bc_ctx *ctx)
+{
+ cli_bytecode_context_clear(ctx);
+ free(ctx);
+}
-struct cli_bc_func {
- uint8_t numArgs;
- uint16_t numLocals;
- uint32_t numInsts;
- uint32_t numConstants;
- uint16_t numBB;
- uint16_t *types;
- uint32_t insn_idx;
- struct cli_bc_bb *BB;
- struct cli_bc_inst *allinsts;
- struct cli_bc_value *values;
-};
+int cli_bytecode_context_clear(struct cli_bc_ctx *ctx)
+{
+ free(ctx->values);
+ free(ctx->operands);
+ memset(ctx, 0, sizeof(ctx));
+}
-struct cli_bc_ctx {
- unsigned dummy;
-};
+int cli_bytecode_context_setfuncid(struct cli_bc_ctx *ctx, struct cli_bc *bc, unsigned funcid)
+{
+ unsigned i;
+ struct cli_bc_func *func;
+ if (funcid >= bc->num_func) {
+ cli_errmsg("bytecode: function ID doesn't exist: %u\n", funcid);
+ return CL_EARG;
+ }
+ ctx->func = &bc->funcs[funcid];
+ ctx->bc = bc;
+ ctx->numParams = func->numArgs;
+ ctx->funcid = funcid;
+ ctx->values = cli_malloc(sizeof(*ctx->values)*func->numArgs);
+ if (!ctx->values) {
+ cli_errmsg("bytecode: error allocating memory for parameters\n");
+ return CL_EMEM;
+ }
+ ctx->operands = cli_malloc(sizeof(*ctx->operands)*func->numArgs);
+ if (!ctx->operands) {
+ cli_errmsg("bytecode: error allocating memory for parameters\n");
+ return CL_EMEM;
+ }
+ for (i=0;i<func->numArgs;i++) {
+ ctx->values[i].ref = MAX_OP;
+ ctx->operands[i] = i;
+ }
+ return CL_SUCCESS;
+}
-struct cli_bc_ctx *cli_bytecode_alloc_context(void)
+static inline int type_isint(uint16_t type)
{
- struct cli_bc_ctx *ctx = cli_malloc(sizeof(*ctx));
- return ctx;
+ return type > 0 && type <= 64;
}
-void cli_bytecode_destroy_context(struct cli_bc_ctx *ctx)
+int cli_bytecode_context_setparam_int(struct cli_bc_ctx *ctx, unsigned i, uint64_t c)
{
- free(ctx);
+ if (i >= ctx->numParams) {
+ cli_errmsg("bytecode: param index out of bounds: %u\n", i);
+ return CL_EARG;
+ }
+ if (!type_isint(ctx->func->types[i])) {
+ cli_errmsg("bytecode: parameter type mismatch\n");
+ return CL_EARG;
+ }
+ ctx->func->values[i].v = c;
+ ctx->func->values[i].ref = CONSTANT_OP;
+ return CL_SUCCESS;
}
+int cli_bytecode_context_setparam_ptr(struct cli_bc_ctx *ctx, unsigned i, void *data, unsigned datalen)
+{
+ cli_errmsg("Pointer parameters are not implemented yet!\n");
+ return CL_EARG;
+}
static inline uint64_t readNumber(const unsigned char *p, unsigned *off, unsigned len, char *ok)
{
@@ -581,8 +589,28 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio)
return CL_SUCCESS;
}
-void cli_bytecode_run(struct cli_bc *bc, struct cli_bc_ctx *ctx)
+int cli_bytecode_run(struct cli_bc *bc, struct cli_bc_ctx *ctx)
{
+ struct cli_bc_inst inst;
+ struct cli_bc_func func;
+ unsigned i;
+ if (!ctx || !ctx->bc || !ctx->func || !ctx->values)
+ return CL_ENULLARG;
+ for (i=0;i<ctx->numParams;i++) {
+ if (ctx->values[i].ref == MAX_OP) {
+ cli_errmsg("bytecode: parameter %u is uninitialized!\n", i);
+ return CL_EARG;
+ }
+ }
+ memset(&func, 0, sizeof(func));
+ func.values = ctx->values;
+
+ inst.opcode = OP_CALL_DIRECT;
+ inst.type = 0;/* TODO: support toplevel functions with return values */
+ inst.u.ops.numOps = ctx->numParams;
+ inst.u.ops.funcid = ctx->funcid;
+ inst.u.ops.ops = ctx->operands;
+ return cli_vm_execute_inst(ctx->bc, ctx, &func, &inst);
}
void cli_bytecode_destroy(struct cli_bc *bc)
diff --git a/libclamav/bytecode.h b/libclamav/bytecode.h
index b6bd74b..983c9bd 100644
--- a/libclamav/bytecode.h
+++ b/libclamav/bytecode.h
@@ -23,10 +23,12 @@
#define BYTECODE_H
#include <stdio.h>
#include "clambc.h"
+#include "cltypes.h"
struct cli_dbio;
struct cli_bc_ctx;
struct cli_bc_func;
struct cli_bc_value;
+struct cli_bc_inst;
struct cli_bc {
unsigned verifier;
@@ -37,11 +39,17 @@ struct cli_bc {
struct cli_bc_func *funcs;
};
-struct cli_bc_ctx *cli_bytecode_alloc_context(void);
-void cli_bytecode_destroy_context(struct cli_bc_ctx *ctx);
+struct cli_bc_ctx *cli_bytecode_context_alloc(void);
+int cli_bytecode_context_setfuncid(struct cli_bc_ctx *ctx, struct cli_bc *bc, unsigned funcid);
+int cli_bytecode_context_setparam_int(struct cli_bc_ctx *ctx, unsigned i, uint64_t c);
+int cli_bytecode_context_setparam_ptr(struct cli_bc_ctx *ctx, unsigned i, void *data, unsigned datalen);
+int cli_bytecode_context_clear(struct cli_bc_ctx *ctx);
+void cli_bytecode_context_destroy(struct cli_bc_ctx *ctx);
int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio);
-void cli_bytecode_run(struct cli_bc *bc, struct cli_bc_ctx *ctx);
+int cli_bytecode_run(struct cli_bc *bc, struct cli_bc_ctx *ctx);
void cli_bytecode_destroy(struct cli_bc *bc);
+int cli_vm_execute_inst(struct cli_bc *bc, struct cli_bc_ctx *ctx, struct cli_bc_func *func, struct cli_bc_inst *inst);
+
#endif
diff --git a/libclamav/libclamav.map b/libclamav/libclamav.map
index c9c099a..35aa258 100644
--- a/libclamav/libclamav.map
+++ b/libclamav/libclamav.map
@@ -147,8 +147,12 @@ CLAMAV_PRIVATE {
cli_bytecode_load;
cli_bytecode_run;
cli_bytecode_destroy;
- cli_bytecode_alloc_context;
- cli_bytecode_destroy_context;
+ cli_bytecode_context_alloc;
+ cli_bytecode_context_destroy;
+ cli_bytecode_context_setfuncid;
+ cli_bytecode_context_setparam_int;
+ cli_bytecode_context_setparam_ptr;
+ cli_bytecode_context_clear;
local:
*;
};
--
Debian repository for ClamAV
More information about the Pkg-clamav-commits
mailing list