Browse code

Fix bytecode load bugs. Make loading more robust: print error message instead of crashing. Able to load and dry-run yc_bytecode.o1.cbc now.

Török Edvin authored on 2009/11/26 18:18:40
Showing 5 changed files
... ...
@@ -257,6 +257,8 @@ char *nc_recv(int s) {
257 257
 	}
258 258
 	if(res==-1) {
259 259
 	    char er[256];
260
+	    if (errno == EAGAIN)
261
+		continue;
260 262
 	    strerror_print("!recv failed after successful select");
261 263
 	    close(s);
262 264
 	    return NULL;
... ...
@@ -63,6 +63,7 @@ int cli_bytecode_context_reset(struct cli_bc_ctx *ctx)
63 63
     free(ctx->opsizes);
64 64
     free(ctx->values);
65 65
     free(ctx->operands);
66
+    ctx->operands = ctx->values = ctx->opsizes = NULL;
66 67
     return CL_SUCCESS;
67 68
 }
68 69
 
... ...
@@ -230,7 +230,12 @@ private:
230 230
 		return V;
231 231
 	    }
232 232
 	    V = Builder.CreateLoad(V);
233
-	    assert(V->getType() == Ty);
233
+	    if (V->getType() != Ty) {
234
+		errs() << operand << " ";
235
+		V->dump();
236
+		Ty->dump();
237
+		llvm_report_error("(libclamav) Type mismatch converting operand");
238
+	    }
234 239
 	    return V;
235 240
 	}
236 241
 	unsigned w = (Ty->getPrimitiveSizeInBits()+7)/8;
... ...
@@ -372,11 +377,9 @@ public:
372 372
     }
373 373
 
374 374
     template <typename InputIterator>
375
-    bool createGEP(unsigned dest, Value *Base, InputIterator Start, InputIterator End) {
376
-	assert(dest >= numArgs && dest < numLocals+numArgs && "Instruction destination out of range");
375
+    Value* createGEP(Value *Base, const Type *ETy, InputIterator Start, InputIterator End) {
377 376
 	const Type *Ty = GetElementPtrInst::getIndexedType(Base->getType(), Start, End);
378
-	const Type *ETy = cast<PointerType>(cast<PointerType>(Values[dest]->getType())->getElementType())->getElementType();
379
-	if (!Ty || (Ty != ETy && (!isa<IntegerType>(Ty) || !isa<IntegerType>(ETy)))) {
377
+	if (!Ty || (ETy && (Ty != ETy && (!isa<IntegerType>(Ty) || !isa<IntegerType>(ETy))))) {
380 378
 	    errs() << MODULE << "Wrong indices for GEP opcode: "
381 379
 		<< " expected type: " << *ETy;
382 380
 	    if (Ty)
... ...
@@ -386,12 +389,19 @@ public:
386 386
 		errs() << **I << ", ";
387 387
 	    }
388 388
 	    errs() << "\n";
389
-	    return false;
390
-	}
391
-	Value *V = Builder.CreateGEP(Base, Start, End);
392
-	if (Ty != ETy) {
393
-	    V = Builder.CreateBitCast(V, PointerType::getUnqual(ETy));
389
+	    return 0;
394 390
 	}
391
+	return Builder.CreateGEP(Base, Start, End);
392
+    }
393
+
394
+    template <typename InputIterator>
395
+    bool createGEP(unsigned dest, Value *Base, InputIterator Start, InputIterator End) {
396
+	assert(dest >= numArgs && dest < numLocals+numArgs && "Instruction destination out of range");
397
+	const Type *ETy = cast<PointerType>(cast<PointerType>(Values[dest]->getType())->getElementType())->getElementType();
398
+	Value *V = createGEP(Base, ETy, Start, End);
399
+	if (!V)
400
+	    return false;
401
+	V = Builder.CreateBitCast(V, PointerType::getUnqual(ETy));
395 402
 	Store(dest, V);
396 403
 	return true;
397 404
     }
... ...
@@ -402,8 +412,8 @@ public:
402 402
 	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
403 403
 	    unsigned id = cli_globals[i].globalid;
404 404
 	    const Type *Ty = apiMap.get(cli_globals[i].type);
405
-	    if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
406
-		Ty = PointerType::getUnqual(ATy->getElementType());
405
+	    /*if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
406
+		Ty = PointerType::getUnqual(ATy->getElementType());*/
407 407
 	    GVtypeMap[id] = Ty;
408 408
 	}
409 409
 	FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
... ...
@@ -563,8 +573,14 @@ public:
563 563
 			ConstantInt::get(Type::getInt32Ty(Context), 0),
564 564
 			ConstantInt::get(Type::getInt32Ty(Context), bc->globals[i][0])
565 565
 		    };
566
-		    globals[i] = Builder.CreateInBoundsGEP(SpecialGV, C,
567
-							   C+2);
566
+		    globals[i] = createGEP(SpecialGV, 0, C, C+2);
567
+		    if (!globals[i]) {
568
+			errs() << i << ":" << g << ":" << bc->globals[i][0] <<"\n";
569
+			Ty->dump();
570
+			llvm_report_error("(libclamav) unable to create fake global");
571
+		    }
572
+		    else if(GetElementPtrInst *GI = dyn_cast<GetElementPtrInst>(globals[i]))
573
+			GI->setIsInBounds(true);
568 574
 		}
569 575
 	    }
570 576
 
... ...
@@ -602,6 +618,11 @@ public:
602 602
 				case 2:
603 603
 				    Op0 = convertOperand(func, inst, inst->u.binop[0]);
604 604
 				    Op1 = convertOperand(func, inst, inst->u.binop[1]);
605
+				    if (Op0->getType() != Op1->getType()) {
606
+					Op0->dump();
607
+					Op1->dump();
608
+					llvm_report_error("(libclamav) binop type mismatch");
609
+				    }
605 610
 				    break;
606 611
 				case 3:
607 612
 				    Op0 = convertOperand(func, inst, inst->u.three[0]);
... ...
@@ -816,9 +837,11 @@ public:
816 816
 			case OP_BC_STORE:
817 817
 			{
818 818
 			    Value *Dest = convertOperand(func, inst, inst->u.binop[1]);
819
-			    const Type *ETy = cast<PointerType>(Dest->getType())->getElementType();
820
-			    Builder.CreateStore(convertOperand(func, ETy, inst->u.binop[0]),
821
-						Dest);
819
+			    Value *V = convertOperand(func, inst, inst->u.binop[0]);
820
+			    const Type *VPTy = PointerType::getUnqual(V->getType());
821
+			    if (VPTy != Dest->getType())
822
+				Dest = Builder.CreateBitCast(Dest, VPTy);
823
+			    Builder.CreateStore(V, Dest);
822 824
 			    break;
823 825
 			}
824 826
 			case OP_BC_LOAD:
... ...
@@ -4,7 +4,7 @@ Tedaa`cabjdebjdacb`bbjdb`bacb`bb`bb`bebfd
4 4
 Eababaabhd|afdgefcgdg`c``abbid|afdgefcgdgac``
5 5
 G``
6 6
 A`b`bLahbfdabgd```b`b`aa`b`b`aa`b`b`Fajac
7
-Bbgdaadb`@d@d``bb``b`bacabbabHonnkm``odHm``oonnkdaaadeab`bacHhgfedcbadTaaadaaab
7
+Bbgdaadb`@d@d``fb`aab`bacabbabHonnkm``odHm``oonnkdaaadeab`bacHhgfedcbadTaaadaaab
8 8
 Bb`baeabbaa`Honnkmjnmdaaafeab`baeHhgfedcbadb`bagoaafDm``odDmjnmdTcab`bag
9 9
 BTcab`bDmjnmdE
10 10
 Aab`bLabah`aa`b`b`Facaa
... ...
@@ -1,4 +1,4 @@
1
-ClamBCaa`|``````|`bjaabp`clamcoincidencejb
1
+ClamBCaa`|``c``a```|`bjaabp`clamcoincidencejb
2 2
 Trojan.Foo.{A,B};Target:0;((0|1|2)=42,2);aabb;ffffffff;aaccee;f00d
3 3
 Tedebieebheebgeebfeebeeebdeebbeebaeebadebcdaa`acb`bbadb`bdb`db`bchbadbcebadbcebadbcebadbcecab`bdagahdaiahdaeahdabbaddabahdakah
4 4
 Eafaaafb`e|amcgefdgfgifbgegcgnfafmfef``