Browse code

Update to latest bytecode format.

Török Edvin authored on 2009/10/02 16:26:47
Showing 3 changed files
... ...
@@ -379,6 +379,7 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer)
379 379
     bc->verifier = readNumber(buffer, &offset, len, &ok);
380 380
     bc->sigmaker = readString(buffer, &offset, len, &ok);
381 381
     bc->id = readNumber(buffer, &offset, len, &ok);
382
+    bc->kind = readNumber(buffer, &offset, len, &ok);
382 383
     bc->metadata.maxStack = readNumber(buffer, &offset, len, &ok);
383 384
     bc->metadata.maxMem = readNumber(buffer, &offset, len, &ok);
384 385
     bc->metadata.maxTime = readNumber(buffer, &offset, len, &ok);
... ...
@@ -723,12 +724,18 @@ static void readConstant(struct cli_bc *bc, unsigned i, unsigned comp,
723 723
 static int parseGlobals(struct cli_bc *bc, unsigned char *buffer)
724 724
 {
725 725
     unsigned i, offset = 1, len = strlen((const char*)buffer), numglobals;
726
+    unsigned maxglobal;
726 727
     char ok=1;
727 728
 
728 729
     if (buffer[0] != 'G') {
729 730
 	cli_errmsg("bytecode: Invalid globals header: %c\n", buffer[0]);
730 731
 	return CL_EMALFDB;
731 732
     }
733
+    maxglobal = readNumber(buffer, &offset, len, &ok);
734
+    if (maxglobal > cli_apicall_maxglobal) {
735
+	cli_dbgmsg("bytecode using global %u, but highest global known to libclamav is %u, skipping\n", maxglobal, cli_apicall_maxglobal);
736
+	return CL_BREAK;
737
+    }
732 738
     numglobals = readNumber(buffer, &offset, len, &ok);
733 739
     bc->globals = cli_calloc(numglobals, sizeof(*bc->globals));
734 740
     if (!bc->globals) {
... ...
@@ -44,6 +44,7 @@ struct cli_bc {
44 44
   unsigned verifier;
45 45
   char *sigmaker;
46 46
   unsigned id;
47
+  unsigned kind;
47 48
   struct bytecode_metadata metadata;
48 49
   unsigned num_types;
49 50
   unsigned num_func;
... ...
@@ -196,6 +196,7 @@ private:
196 196
     LLVMTypeMapper *TypeMap;
197 197
     Function **apiFuncs;
198 198
     FunctionMapTy &compiledFunctions;
199
+    LLVMTypeMapper &apiMap;
199 200
     Twine BytecodeID;
200 201
     ExecutionEngine *EE;
201 202
     TargetFolder Folder;
... ...
@@ -350,13 +351,14 @@ private:
350 350
 
351 351
 public:
352 352
     LLVMCodegen(const struct cli_bc *bc, Module *M, FunctionMapTy &cFuncs,
353
-		ExecutionEngine *EE, FunctionPassManager &PM, Function **apiFuncs)
353
+		ExecutionEngine *EE, FunctionPassManager &PM,
354
+		Function **apiFuncs, LLVMTypeMapper &apiMap)
354 355
 	: bc(bc), M(M), Context(M->getContext()), compiledFunctions(cFuncs),
355 356
 	BytecodeID("bc"+Twine(bc->id)), EE(EE),
356 357
 	Folder(EE->getTargetData(), Context), Builder(Context, Folder), PM(PM),
357
-	apiFuncs(apiFuncs)
358
+	apiFuncs(apiFuncs), apiMap(apiMap)
358 359
     {
359
-	for (unsigned i=0;i<cli_apicall_maxglobal;i++) {
360
+	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
360 361
 	    unsigned id = cli_globals[i].globalid;
361 362
 	    GVoffsetMap[id] = cli_globals[i].offset;
362 363
 	}
... ...
@@ -365,9 +367,9 @@ public:
365 365
     bool generate() {
366 366
 	TypeMap = new LLVMTypeMapper(Context, bc->types + 4, bc->num_types - 5);
367 367
 
368
-	for (unsigned i=0;i<cli_apicall_maxglobal;i++) {
368
+	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
369 369
 	    unsigned id = cli_globals[i].globalid;
370
-	    GVtypeMap[id] = TypeMap->get(cli_globals[i].type);
370
+	    GVtypeMap[id] = apiMap.get(cli_globals[i].type);
371 371
 	}
372 372
 	FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
373 373
 						    false);
... ...
@@ -891,7 +893,7 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
891 891
 	    if (bc->state == bc_skip)
892 892
 		continue;
893 893
 	    LLVMCodegen Codegen(bc, M, bcs->engine->compiledFunctions, EE,
894
-				OurFPM, apiFuncs);
894
+				OurFPM, apiFuncs, apiMap);
895 895
 	    if (!Codegen.generate()) {
896 896
 		errs() << MODULE << "JIT codegen failed\n";
897 897
 		return CL_EBYTECODE;