[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:59:47 UTC 2010
The following commit has been merged in the debian/unstable branch:
commit 8cc286f57dfb26a537a7042429f2f7a761ce52d5
Author: Török Edvin <edwin at clamav.net>
Date: Fri Jul 31 15:28:36 2009 +0300
Add support for type loader.
diff --git a/libclamav/bytecode.c b/libclamav/bytecode.c
index 4252baf..1b5c381 100644
--- a/libclamav/bytecode.c
+++ b/libclamav/bytecode.c
@@ -310,6 +310,7 @@ static inline char *readString(const unsigned char *p, unsigned *off, unsigned l
}
return str;
}
+
static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
{
uint64_t magic1;
@@ -339,6 +340,7 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
bc->metadata.maxMem = readNumber(buffer, &offset, len, &ok);
bc->metadata.maxTime = readNumber(buffer, &offset, len, &ok);
bc->metadata.targetExclude = readString(buffer, &offset, len, &ok);
+ bc->num_types = readNumber(buffer, &offset, len, &ok);
bc->num_func = readNumber(buffer, &offset, len, &ok);
bc->state = bc_loaded;
if (!ok) {
@@ -364,6 +366,139 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
cli_errmsg("Out of memory allocating %u functions\n", bc->num_func);
return CL_EMEM;
}
+ bc->types = cli_calloc(bc->num_types, sizeof(*bc->types));
+ if (!bc->types) {
+ cli_errmsg("Out of memory allocating %u types\n", bc->num_types);
+ return CL_EMEM;
+ }
+ return CL_SUCCESS;
+}
+
+static uint16_t readTypeID(struct cli_bc *bc, unsigned char *buffer,
+ unsigned *offset, unsigned len, char *ok)
+{
+ uint64_t t = readNumber(buffer, offset, len, ok);
+ if (!ok)
+ return ~0u;
+ if (t >= bc->num_types + bc->start_tid) {
+ cli_errmsg("Invalid type id: %u\n", t);
+ *ok = 0;
+ return ~0u;
+ }
+ return t;
+}
+
+static void parseType(struct cli_bc *bc, struct cli_bc_type *ty,
+ unsigned char *buffer, unsigned *off, unsigned len,
+ char *ok)
+{
+ unsigned j;
+
+ ty->numElements = readFixedNumber(buffer, off, len, ok, 1);
+ if (!ok) {
+ cli_errmsg("Error parsing type\n");
+ *ok = 0;
+ return;
+ }
+ ty->containedTypes = cli_malloc(sizeof(*ty->containedTypes)*ty->numElements);
+ if (!ty->containedTypes) {
+ cli_errmsg("Out of memory allocating %u types\n", ty->numElements);
+ *ok = 0;
+ return;
+ }
+ for (j=0;j<ty->numElements;j++) {
+ ty->containedTypes[j] = readTypeID(bc, buffer, off, len, ok);
+ }
+}
+
+static uint16_t containedTy[] = {8,16,32,64};
+
+static void add_static_types(struct cli_bc *bc)
+{
+ unsigned i;
+ for (i=0;i<4;i++) {
+ bc->types[i].kind = PointerType;
+ bc->types[i].numElements = 1;
+ bc->types[i].containedTypes = &containedTy[i];
+ }
+}
+
+static int parseTypes(struct cli_bc *bc, unsigned char *buffer)
+{
+ unsigned i, j, offset = 1, ok=1, len = strlen(buffer);
+ if (buffer[0] != 'T') {
+ cli_errmsg("Invalid function types header: %c\n", buffer[0]);
+ return CL_EMALFDB;
+ }
+ bc->start_tid = readFixedNumber(buffer, &offset, len, &ok, 2);
+ if (bc->start_tid != BC_START_TID) {
+ cli_warnmsg("Type start id mismatch: %u != %u\n", bc->start_tid,
+ BC_START_TID);
+ return CL_BREAK;
+ }
+ add_static_types(bc);
+ for (i=(BC_START_TID - 64);i<bc->num_types;i++) {
+ struct cli_bc_type *ty = &bc->types[i];
+ uint8_t t = readFixedNumber(buffer, &offset, len, &ok, 1);
+ uint16_t tid;
+ if (!ok) {
+ cli_errmsg("Error reading type kind\n");
+ return CL_EMALFDB;
+ }
+ switch (t) {
+ case 1:
+ ty->kind = FunctionType;
+ parseType(bc, ty, buffer, &offset, len, &ok);
+ if (!ok) {
+ cli_errmsg("Error parsing type %u\n", i);
+ return CL_EMALFDB;
+ }
+ break;
+ case 2:
+ case 3:
+ ty->kind = (t == 2) ? StructType : PackedStructType;
+ parseType(bc, ty, buffer, &offset, len, &ok);
+ if (!ok) {
+ cli_errmsg("Error parsing type %u\n", i);
+ return CL_EMALFDB;
+ }
+ break;
+ case 4:
+ ty->kind = ArrayType;
+ /* number of elements of array, not subtypes! */
+ ty->numElements = readNumber(buffer, &offset, len, &ok);
+ if (!ok) {
+ cli_errmsg("Error parsing type %u\n", i);
+ return CL_EMALFDB;
+ }
+ /* fall-through */
+ case 5:
+ if (t == 5) {
+ ty->kind = PointerType;
+ ty->numElements = 1;
+ }
+ ty->containedTypes = cli_malloc(sizeof(*ty->containedTypes));
+ if (!ty->containedTypes) {
+ cli_errmsg("Out of memory allocating containedType\n");
+ return CL_EMALFDB;
+ }
+ ty->containedTypes[0] = readTypeID(bc, buffer, &offset, len, &ok);
+ if (!ok) {
+ cli_errmsg("Error parsing type %u\n", i);
+ return CL_EMALFDB;
+ }
+ break;
+ default:
+ cli_errmsg("Invalid type kind: %u\n", t);
+ return CL_EMALFDB;
+ }
+ }
+ return CL_SUCCESS;
+}
+
+static int parseApis(struct cli_bc *bc, unsigned char *buffer)
+{
+ //TODO
return CL_SUCCESS;
}
@@ -629,6 +764,8 @@ static int parseBB(struct cli_bc *bc, unsigned func, unsigned bb, unsigned char
enum parse_state {
PARSE_BC_HEADER=0,
+ PARSE_BC_TYPES,
+ PARSE_BC_APIS,
PARSE_FUNC_HEADER,
PARSE_BB
};
@@ -656,6 +793,24 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio)
cli_errmsg("Error at bytecode line %u\n", row);
return rc;
}
+ state = PARSE_BC_TYPES;
+ break;
+ case PARSE_BC_TYPES:
+ rc = parseTypes(bc, (unsigned char*)buffer);
+ if (rc != CL_SUCCESS) {
+ cli_errmsg("Error at bytecode line %u\n", row);
+ return rc;
+ }
+ state = PARSE_BC_APIS;
+ break;
+ case PARSE_BC_APIS:
+ rc = parseApis(bc, (unsigned char*)buffer);
+ if (rc == CL_BREAK) /* skip */
+ return CL_SUCCESS;
+ if (rc != CL_SUCCESS) {
+ cli_errmsg("Error at bytecode line %u\n", row);
+ return rc;
+ }
state = PARSE_FUNC_HEADER;
break;
case PARSE_FUNC_HEADER:
diff --git a/libclamav/bytecode.h b/libclamav/bytecode.h
index 39dab43..965145a 100644
--- a/libclamav/bytecode.h
+++ b/libclamav/bytecode.h
@@ -29,6 +29,7 @@ struct cli_bc_ctx;
struct cli_bc_func;
struct cli_bc_value;
struct cli_bc_inst;
+struct cli_bc_type;
enum bc_state {
bc_loaded,
@@ -41,9 +42,12 @@ struct cli_bc {
char *sigmaker;
unsigned id;
struct bytecode_metadata metadata;
+ unsigned num_types;
unsigned num_func;
struct cli_bc_func *funcs;
+ struct cli_bc_type *types;
enum bc_state state;
+ uint16_t start_tid;
};
struct cli_bc_ctx *cli_bytecode_context_alloc(void);
diff --git a/libclamav/bytecode_priv.h b/libclamav/bytecode_priv.h
index cde7d16..052f89c 100644
--- a/libclamav/bytecode_priv.h
+++ b/libclamav/bytecode_priv.h
@@ -22,6 +22,8 @@
#ifndef BYTECODE_PRIV_H
#define BYTECODE_PRIV_H
+
+#include "type_desc.h"
typedef uint32_t operand_t;
typedef uint16_t bbid_t;
typedef uint16_t funcid_t;
diff --git a/libclamav/clambc.h b/libclamav/clambc.h
index ccff495..de95949 100644
--- a/libclamav/clambc.h
+++ b/libclamav/clambc.h
@@ -66,6 +66,7 @@ enum bc_opcode {
OP_ICMP_SLT,
OP_SELECT,
OP_CALL_DIRECT,
+ OP_CALL_API,
OP_COPY,
OP_GEP1,
OP_GEP2,
@@ -88,10 +89,13 @@ static const unsigned char operand_counts[] = {
/* SELECT */
3,
/* CALLs have variable number of operands */
- 0,
+ 0, 0,
/* OP_COPY */
2,
/* OP_GEP1, OP_GEP2, OP_GEPN, OP_STORE, OP_LOAD*/
2, 3, 0, 2, 1
};
+
+#define BC_START_TID 69
+
#endif
diff --git a/libclamav/type_desc.h b/libclamav/type_desc.h
new file mode 100644
index 0000000..966644f
--- /dev/null
+++ b/libclamav/type_desc.h
@@ -0,0 +1,29 @@
+enum derived_t {
+ FunctionType,
+ PointerType,
+ StructType,
+ PackedStructType,
+ ArrayType
+};
+
+struct cli_bc_type {
+ enum derived_t kind;
+ uint16_t *containedTypes;
+ unsigned numElements;
+};
+
+typedef int32_t (*cli_apicall_int2)(int32_t, int32_t);
+typedef int32_t (*cli_apicall_pointer)(void*, uint32_t);
+
+struct cli_apicall {
+ const char *name;
+ const struct cli_bc_type *type;
+ uint8_t kind;
+};
+
+extern const struct cli_bc_type cli_apicall_types[];
+
+extern const struct cli_apicall cli_apicalls[];
+extern const cli_apicall_int2 cli_apicalls0[];
+extern const cli_apicall_pointer cli_apicalls1[];
+extern const unsigned cli_apicall_maxapi;
diff --git a/unit_tests/input/arith.cbc b/unit_tests/input/arith.cbc
index ba74bc8..78721b5 100644
--- a/unit_tests/input/arith.cbc
+++ b/unit_tests/input/arith.cbc
@@ -1,4 +1,6 @@
-ClamBCaa`|`````|`bbep`clamcoincidencejb
+ClamBCaa`|`````|`aebbep`clamcoincidencejb
+Ted
+E``
A`Lbabb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bb`bFbbbaa
Bb`b`oa`abb`baaoa`acb`baboa`adb`bacoa`aeb`badoa`afb`baeoa`agb`bafoa`ahb`bagoa`aib`bahoa`ajb`baioa`akb`bajoa`alb`bakoa`amb`baloa`anb`bamoa`aob`banoa`b`ab`baooa`baab`bb`aoa`bbab`bbaaa`aa`b`bbbaa`baaabb`bbcaa`bbaacb`bbdaa`bcaadb`bbeaa`bdaaeb`bbfaa`beaafb`bbgaa`bfaagb`bbhaa`bgaahb`bbiaa`bhaaib`bbjaa`biaajb`bbkaa`bjaakb`bblaa`bkaalb`bbmaa`blaamb`bbnaa`bmaanb`bboaa`bnaaob`bb`ba`boab`aTcab`bb`bE
A`Lb`cahaab`bahaab`bahaab`bahaab`bb`aaab`bb`aaab`bb`aaab`bb`aaab`bb`baab`bb`baab`bb`baab`bb`baab`bb`daab`bb`daab`bb`daab`bb`daab`bFbaebab
diff --git a/unit_tests/input/retmagic.cbc b/unit_tests/input/retmagic.cbc
index 87d0044..6c0f6b1 100644
--- a/unit_tests/input/retmagic.cbc
+++ b/unit_tests/input/retmagic.cbc
@@ -1,3 +1,5 @@
-ClamBCaa`|`````|`aap`clamcoincidencejb
+ClamBCaa`|`````|`aeaap`clamcoincidencejb
+Ted
+E``
A`L`Faaaa
BTcaHm``odcbab`bE
--
Debian repository for ClamAV
More information about the Pkg-clamav-commits
mailing list