1 /*
2  *  JIT compile ClamAV bytecode.
3  *
4  *  Copyright (C) 2013-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *  Copyright (C) 2009-2013 Sourcefire, Inc.
6  *
7  *  Authors: Török Edvin
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  *  MA 02110-1301, USA.
22  */
23 
24 #define DEBUG_TYPE "clamavjit"
25 #include <pthread.h>
26 #ifndef _WIN32
27 #include <sys/time.h>
28 #endif
29 #include <cstdlib>
30 #include <csetjmp>
31 #include <new>
32 #include <cerrno>
33 #include <string>
34 
35 #include "ClamBCModule.h"
36 #include "ClamBCDiagnostics.h"
37 #include "llvm30_compat.h"
38 
39 #include "llvm/ADT/DenseMap.h"
40 #include "llvm/ADT/BitVector.h"
41 #include "llvm/ADT/PostOrderIterator.h"
42 #include "llvm/ADT/StringMap.h"
43 #include "llvm/ADT/StringSwitch.h"
44 #include "llvm/ADT/Triple.h"
45 #include "llvm/ADT/SmallSet.h"
46 #include "llvm/ADT/SmallVector.h"
47 #include "llvm/Analysis/LoopInfo.h"
48 #include "llvm/Analysis/ScalarEvolution.h"
49 #if LLVM_VERSION < 35
50 #include "llvm/Analysis/Verifier.h"
51 #include "llvm/AutoUpgrade.h"
52 #include "llvm/Support/TargetFolder.h"
53 #else
54 #include "llvm/IR/Verifier.h"
55 #include "llvm/IR/AutoUpgrade.h"
56 #include "llvm/Analysis/TargetFolder.h"
57 #endif
58 #include "llvm/ExecutionEngine/ExecutionEngine.h"
59 #if LLVM_VERSION < 36
60 #include "llvm/ExecutionEngine/JIT.h"
61 #else
62 #include "llvm/ExecutionEngine/MCJIT.h"
63 #include "llvm/Support/DynamicLibrary.h"
64 #include "llvm/Object/ObjectFile.h"
65 #endif
66 #include "llvm/ExecutionEngine/JITEventListener.h"
67 #include "llvm/PassManager.h"
68 #include "llvm/Support/Compiler.h"
69 #include "llvm/Support/Debug.h"
70 #include "llvm/Support/CommandLine.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/Support/ManagedStatic.h"
73 #include "llvm/Support/MemoryBuffer.h"
74 #include "llvm/Support/raw_ostream.h"
75 #include "llvm/Support/SourceMgr.h"
76 #include "llvm/Support/PrettyStackTrace.h"
77 
78 #if LLVM_VERSION < 29
79 #include "llvm/System/DataTypes.h"
80 #include "llvm/System/Host.h"
81 #include "llvm/System/Memory.h"
82 #include "llvm/System/Mutex.h"
83 #include "llvm/System/Signals.h"
84 #include "llvm/System/Threading.h"
85 #include "llvm/System/ThreadLocal.h"
86 #else
87 #include "llvm/PassRegistry.h"
88 #include "llvm/Support/DataTypes.h"
89 #include "llvm/Support/FileSystem.h"
90 #include "llvm/Support/Host.h"
91 #include "llvm/Support/Memory.h"
92 #include "llvm/Support/Mutex.h"
93 #include "llvm/Support/Signals.h"
94 #include "llvm/Support/Threading.h"
95 #include "llvm/Support/ThreadLocal.h"
96 #endif
97 
98 #if LLVM_VERSION < 33
99 #include "llvm/IntrinsicInst.h"
100 #else
101 #include "llvm/IR/IntrinsicInst.h"
102 #endif
103 
104 #include "llvm/Support/Timer.h"
105 
106 extern "C" {
107 void LLVMInitializeX86AsmPrinter();
108 void LLVMInitializePowerPCAsmPrinter();
109 }
110 
111 #if LLVM_VERSION < 30
112 #include "llvm/Target/TargetSelect.h"
113 #else
114 #include "llvm/Support/TargetSelect.h"
115 #endif
116 
117 #include "llvm/Target/TargetOptions.h"
118 #include "llvm/Transforms/Scalar.h"
119 #include "llvm/Transforms/IPO.h"
120 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
121 
122 #if LLVM_VERSION < 32
123 #include "llvm/Analysis/DebugInfo.h"
124 #elif LLVM_VERSION < 35
125 #include "llvm/DebugInfo.h"
126 #else
127 #include "llvm/IR/DebugInfo.h"
128 #endif
129 
130 #if LLVM_VERSION < 32
131 #include "llvm/Support/IRBuilder.h"
132 #include "llvm/Target/TargetData.h"
133 #elif LLVM_VERSION < 33
134 #include "llvm/IRBuilder.h"
135 #include "llvm/DataLayout.h"
136 #else
137 #include "llvm/IR/IRBuilder.h"
138 #include "llvm/IR/DataLayout.h"
139 #endif
140 
141 #if LLVM_VERSION < 33
142 #include "llvm/CallingConv.h"
143 #include "llvm/DerivedTypes.h"
144 #include "llvm/Function.h"
145 #include "llvm/LLVMContext.h"
146 #include "llvm/Intrinsics.h"
147 #include "llvm/Module.h"
148 #else
149 #include "llvm/IR/CallingConv.h"
150 #include "llvm/IR/DerivedTypes.h"
151 #include "llvm/IR/Function.h"
152 #include "llvm/IR/LLVMContext.h"
153 #include "llvm/IR/Intrinsics.h"
154 #include "llvm/IR/Module.h"
155 #endif
156 
157 #if LLVM_VERSION < 34
158 #include "llvm/Support/CFG.h"
159 #else
160 #include "llvm/Analysis/CFG.h"
161 #endif
162 
163 #if LLVM_VERSION >= 35
164 #include "llvm/IR/Dominators.h"
165 #endif
166 
167 //#define TIMING
168 #undef TIMING
169 
170 #include "llvm/Config/config.h"
171 #ifdef ENABLE_THREADS
172 #if !ENABLE_THREADS
173 #error "Thread support was explicitly disabled. Cannot continue"
174 #endif
175 #endif
176 
177 #ifdef LLVM_ENABLE_THREADS
178 #if !LLVM_ENABLE_THREADS
179 #error "Thread support was explicitly disabled. Cannot continue"
180 #endif
181 #endif
182 
183 #ifdef _GLIBCXX_PARALLEL
184 #error "libstdc++ parallel mode is not supported for ClamAV. Please remove -D_GLIBCXX_PARALLEL from CXXFLAGS!"
185 #endif
186 
187 #ifdef HAVE_CONFIG_H
188 #undef PACKAGE_BUGREPORT
189 #undef PACKAGE_NAME
190 #undef PACKAGE_STRING
191 #undef PACKAGE_TARNAME
192 #undef PACKAGE_VERSION
193 #undef PACKAGE_URL
194 #include "clamav-config.h"
195 #endif
196 
197 #include <openssl/ssl.h>
198 #include <openssl/err.h>
199 
200 #include "dconf.h"
201 #include "clamav.h"
202 #include "clambc.h"
203 #include "bytecode.h"
204 #include "bytecode_priv.h"
205 #include "type_desc.h"
206 
207 #define MODULE "libclamav JIT: "
208 
209 extern "C" unsigned int cli_rndnum(unsigned int max);
210 using namespace llvm;
211 typedef DenseMap<const struct cli_bc_func*, void*> FunctionMapTy;
212 struct cli_bcengine {
213     ExecutionEngine *EE;
214     JITEventListener *Listener;
215     LLVMContext Context;
216     FunctionMapTy compiledFunctions;
217     union {
218 	unsigned char b[16];
219 	void* align;/* just to align field to ptr */
220     } guard;
221 };
222 
223 extern "C" uint8_t cli_debug_flag;
224 #if LLVM_VERSION >= 29
225 namespace llvm {
226     void initializeRuntimeLimitsPass(PassRegistry&);
227 };
228 #endif
229 namespace {
230 
231 #if LLVM_VERSION >= 28
232 #define llvm_report_error(x) report_fatal_error(x)
233 #define llvm_install_error_handler(x) install_fatal_error_handler(x)
234 #define DwarfExceptionHandling JITExceptionHandling
235 #define SetCurrentDebugLocation(x) SetCurrentDebugLocation(DebugLoc::getFromDILocation(x))
236 #define DEFINEPASS(passname) passname() : FunctionPass(ID)
237 #else
238 #define DEFINEPASS(passname) passname() : FunctionPass(&ID)
239 #endif
240 
241 #if LLVM_VERSION >= 29
242 #define NORETURN LLVM_ATTRIBUTE_NORETURN
243 #endif
244 
245 static sys::ThreadLocal<const jmp_buf> ExceptionReturn;
246 
UpgradeCall(CallInst * & C,Function * Intr)247 static void UpgradeCall(CallInst *&C, Function *Intr)
248 {
249     Function *New;
250     if (!UpgradeIntrinsicFunction(Intr, New) || New == Intr)
251 	return;
252     UpgradeIntrinsicCall(C, New);
253 }
254 
255 extern "C" {
256 #ifdef __GNUC__
257 void cli_errmsg(const char *str, ...) __attribute__((format(printf, 1, 2)));
258 #else
259 void cli_errmsg(const char *str, ...);
260 #endif
261 
262 #ifdef __GNUC__
263 void cli_warnmsg(const char *str, ...) __attribute__((format(printf, 1, 2)));
264 #else
265 void cli_warnmsg(const char *str, ...);
266 #endif
267 
268 #ifdef __GNUC__
269 void cli_dbgmsg_internal(const char *str, ...) __attribute__((format(printf, 1, 2)));
270 #else
271 void cli_dbgmsg_internal(const char *str, ...);
272 #endif
273 }
274 
275 class ScopedExceptionHandler {
276     public:
getEnv()277 	jmp_buf &getEnv() { return env;}
Set()278 	void Set() {
279 	    /* set the exception handler's return location to here for the
280 	     * current thread */
281 	    ExceptionReturn.set((const jmp_buf*)&env);
282 	}
~ScopedExceptionHandler()283 	~ScopedExceptionHandler() {
284 	    /* leaving scope, remove exception handler for current thread */
285 	    ExceptionReturn.erase();
286 	}
287     private:
288 	jmp_buf env;
289 };
290 #define HANDLER_TRY(handler) \
291     if (setjmp(handler.getEnv()) == 0) {\
292 	handler.Set();
293 
294 #define HANDLER_END(handler) \
295     } else cli_warnmsg("[Bytecode JIT]: recovered from error\n");
296 
297 
do_shutdown()298 void do_shutdown() {
299     ScopedExceptionHandler handler;
300     HANDLER_TRY(handler) {
301 	// TODO: be on the safe side, and clear errors here,
302 	// otherwise destructor calls report_fatal_error
303 	((class raw_fd_ostream&)errs()).clear_error();
304 
305 	llvm_shutdown();
306 
307 	((class raw_fd_ostream&)errs()).clear_error();
308     }
309     HANDLER_END(handler);
310     remove_fatal_error_handler();
311 }
312 
jit_exception_handler(void)313 static void NORETURN jit_exception_handler(void)
314 {
315     jmp_buf* buf = const_cast<jmp_buf*>(ExceptionReturn.get());
316     if (buf) {
317 	// For errors raised during bytecode generation and execution.
318 	longjmp(*buf, 1);
319     } else {
320 	// Oops, got no error recovery pointer set up,
321 	// this is probably an error raised during shutdown.
322 	cli_errmsg("[Bytecode JIT]: exception handler called, but no recovery point set up");
323 	// should never happen, we remove the error handler when we don't use
324 	// LLVM anymore, and when we use it, we do set an error recovery point.
325 	llvm_unreachable("Bytecode JIT]: no exception handler recovery installed, but exception hit!");
326     }
327 }
328 
jit_ssp_handler(void)329 static void NORETURN jit_ssp_handler(void)
330 {
331     cli_errmsg("[Bytecode JIT]: *** stack smashing detected, bytecode aborted\n");
332     jit_exception_handler();
333 }
334 
335 #if LLVM_VERSION < 33
llvm_error_handler(void * user_data,const std::string & reason)336 void llvm_error_handler(void *user_data, const std::string &reason)
337 #else
338 void llvm_error_handler(void *user_data, const std::string &reason, bool gen_crash_diag = true)
339 #endif
340 {
341     // Output it to stderr, it might exceed the 1k/4k limit of cli_errmsg
342     cli_errmsg("[Bytecode JIT]: [LLVM error] %s\n", reason.c_str());
343     jit_exception_handler();
344 }
345 
346 // Since libgcc is not available on all compilers (for example on win32),
347 // just define what these functions should do, the compiler will forward to
348 // the appropriate libcall if needed.
rtlib_sdiv_i64(int64_t a,int64_t b)349 static int64_t rtlib_sdiv_i64(int64_t a, int64_t b)
350 {
351     return a/b;
352 }
353 
rtlib_udiv_i64(uint64_t a,uint64_t b)354 static uint64_t rtlib_udiv_i64(uint64_t a, uint64_t b)
355 {
356     return a/b;
357 }
358 
rtlib_srem_i64(int64_t a,int64_t b)359 static int64_t rtlib_srem_i64(int64_t a, int64_t b)
360 {
361     return a%b;
362 }
363 
rtlib_urem_i64(uint64_t a,uint64_t b)364 static uint64_t rtlib_urem_i64(uint64_t a, uint64_t b)
365 {
366     return a%b;
367 }
368 
rtlib_mul_i64(uint64_t a,uint64_t b)369 static int64_t rtlib_mul_i64(uint64_t a, uint64_t b)
370 {
371     return a*b;
372 }
373 
rtlib_shl_i64(int64_t a,int32_t b)374 static int64_t rtlib_shl_i64(int64_t a, int32_t b)
375 {
376     return a << b;
377 }
378 
rtlib_srl_i64(int64_t a,int32_t b)379 static int64_t rtlib_srl_i64(int64_t a, int32_t b)
380 {
381     return (uint64_t)a >> b;
382 }
383 /* Implementation independent sign-extended signed right shift */
384 #ifdef HAVE_SAR
385 #define CLI_SRS(n,s) ((n)>>(s))
386 #else
387 #define CLI_SRS(n,s) ((((n)>>(s)) ^ (1<<(sizeof(n)*8-1-s))) - (1<<(sizeof(n)*8-1-s)))
388 #endif
rtlib_sra_i64(int64_t a,int32_t b)389 static int64_t rtlib_sra_i64(int64_t a, int32_t b)
390 {
391     return CLI_SRS(a, b);//CLI_./..
392 }
393 
rtlib_bzero(void * s,size_t n)394 static void rtlib_bzero(void *s, size_t n)
395 {
396     memset(s, 0, n);
397 }
398 
399 #ifdef _WIN32
400 #ifdef _WIN64
401 extern "C" void __chkstk(void);
402 #else
403 extern "C" void _chkstk(void);
404 #endif
405 #endif
406 // Resolve integer libcalls, but nothing else.
noUnknownFunctions(const std::string & name)407 static void* noUnknownFunctions(const std::string& name) {
408     void *addr =
409 	StringSwitch<void*>(name)
410 	.Case("__divdi3", (void*)(intptr_t)rtlib_sdiv_i64)
411 	.Case("__udivdi3", (void*)(intptr_t)rtlib_udiv_i64)
412 	.Case("__moddi3", (void*)(intptr_t)rtlib_srem_i64)
413 	.Case("__umoddi3", (void*)(intptr_t)rtlib_urem_i64)
414 	.Case("__muldi3", (void*)(intptr_t)rtlib_mul_i64)
415 	.Case("__ashrdi3", (void*)(intptr_t)rtlib_sra_i64)
416 	.Case("__ashldi3", (void*)(intptr_t)rtlib_shl_i64)
417 	.Case("__lshrdi3", (void*)(intptr_t)rtlib_srl_i64)
418 	.Case("__bzero", (void*)(intptr_t)rtlib_bzero)
419 	.Case("memmove", (void*)(intptr_t)memmove)
420 	.Case("memcpy", (void*)(intptr_t)memcpy)
421 	.Case("memset", (void*)(intptr_t)memset)
422 	.Case("abort", (void*)(intptr_t)jit_exception_handler)
423 #ifdef _WIN32
424 #ifdef _WIN64
425 	.Case("_chkstk", (void*)(intptr_t)__chkstk)
426 #else
427 	.Case("_chkstk", (void*)(intptr_t)_chkstk)
428 #endif
429 #endif
430 	.Default(0);
431     if (addr)
432 	return addr;
433 
434 #if LLVM_VERSION < 36
435     std::string reason((Twine("Attempt to call external function ")+name).str());
436     llvm_error_handler(0, reason);
437 #else
438     // noUnknownFunctions relies on addGlobalMapping, which doesn't work with MCJIT.
439     // Now the function pointers are found with SymbolSearching.
440 #endif
441     return 0;
442 }
443 
444 class NotifyListener : public JITEventListener {
445 public:
446 #if LLVM_VERSION < 36
NotifyFunctionEmitted(const Function & F,void * Code,size_t Size,const EmittedFunctionDetails & Details)447     virtual void NotifyFunctionEmitted(const Function &F,
448 				       void *Code, size_t Size,
449 				       const EmittedFunctionDetails &Details)
450     {
451 	if (!cli_debug_flag)
452 	    return;
453 	cli_dbgmsg_internal("[Bytecode JIT]: emitted function %s of %ld bytes at %p\n",
454 #if LLVM_VERSION < 31
455 			    F.getNameStr().c_str(), (long)Size, Code);
456 #else
457 			    F.getName().str().c_str(), (long)Size, Code);
458 #endif
459     }
460 #else
461     // MCJIT doesn't emit single functions, but instead whole objects.
462     virtual void NotifyObjectEmitted(const object::ObjectFile &Obj,
463                                      const RuntimeDyld::LoadedObjectInfo &L)
464     {
465         if (!cli_debug_flag)
466             return;
467         cli_dbgmsg_internal("[Bytecode JIT]; emitted %s %s of %zd bytes\n",
468                             Obj.getFileFormatName().str().c_str(),
469                             Obj.getFileName().str().c_str(), Obj.getData().size());
470     }
471 #endif
472 };
473 
474 class TimerWrapper {
475 private:
476     Timer *t;
477 public:
TimerWrapper(const std::string & name)478     TimerWrapper(const std::string &name) {
479 	t = 0;
480 #ifdef TIMING
481 	t = new Timer(name);
482 #endif
483     }
~TimerWrapper()484     ~TimerWrapper()
485     {
486 	if (t)
487 	    delete t;
488     }
startTimer()489     void startTimer()
490     {
491 	if (t)
492 	    t->startTimer();
493     }
stopTimer()494     void stopTimer()
495     {
496 	if (t)
497 	    t->stopTimer();
498     }
499 };
500 
501 class LLVMTypeMapper {
502 private:
503 #if LLVM_VERSION < 30
504     std::vector<PATypeHolder> TypeMap;
505 #else
506     std::vector<Type*> TypeMap;
507 #endif
508     LLVMContext &Context;
509     unsigned numTypes;
getStatic(uint16_t ty)510     constType *getStatic(uint16_t ty)
511     {
512 	if (!ty)
513 	    return Type::getVoidTy(Context);
514 	if (ty <= 64)
515 	    return IntegerType::get(Context, ty);
516 	switch (ty) {
517 	    case 65:
518 		return PointerType::getUnqual(Type::getInt8Ty(Context));
519 	    case 66:
520 		return PointerType::getUnqual(Type::getInt16Ty(Context));
521 	    case 67:
522 		return PointerType::getUnqual(Type::getInt32Ty(Context));
523 	    case 68:
524 		return PointerType::getUnqual(Type::getInt64Ty(Context));
525 	}
526 	llvm_unreachable("getStatic");
527     }
528 public:
529     TimerWrapper pmTimer;
530     TimerWrapper irgenTimer;
531 
LLVMTypeMapper(LLVMContext & Context,const struct cli_bc_type * types,unsigned count,constType * Hidden=0)532     LLVMTypeMapper(LLVMContext &Context, const struct cli_bc_type *types,
533 		   unsigned count, constType *Hidden=0) : Context(Context), numTypes(count),
534     pmTimer("Function passes"),irgenTimer("IR generation")
535     {
536 	TypeMap.reserve(count);
537 	// During recursive type construction pointers to Type* may be
538 	// invalidated, so we must use a TypeHolder to an Opaque type as a
539 	// start.
540 	for (unsigned i=0;i<count;i++) {
541 #if LLVM_VERSION < 30
542 	    TypeMap.push_back(OpaqueType::get(Context));
543 #else
544 	    TypeMap.push_back(0);
545 #endif
546 	}
547 	for (unsigned i=0;i<count;i++) {
548 	    const struct cli_bc_type *type = &types[i];
549 
550 	    constType *Ty = buildType(type, types, Hidden, 0);
551 #if LLVM_VERSION < 30
552 	    // Make the opaque type a concrete type, doing recursive type
553 	    // unification if needed.
554 	    cast<OpaqueType>(TypeMap[i].get())->refineAbstractTypeTo(Ty);
555 #else
556 	    TypeMap[i] = Ty;
557 #endif
558 	}
559     }
560 
buildType(const struct cli_bc_type * type,const struct cli_bc_type * types,constType * Hidden,int recursive)561     constType *buildType(const struct cli_bc_type *type, const struct cli_bc_type *types, constType *Hidden,
562 			 int recursive)
563     {
564 	std::vector<constType*> Elts;
565 	unsigned n = type->kind == DArrayType ? 1 : type->numElements;
566 	for (unsigned j=0;j<n;j++) {
567 	    Elts.push_back(get(type->containedTypes[j], types, Hidden));
568 	}
569 	constType *Ty;
570 	switch (type->kind) {
571 	    case DFunctionType:
572 		{
573 		    assert(Elts.size() > 0 && "Function with no return type?");
574 		    constType *RetTy = Elts[0];
575 		    if (Hidden)
576 			Elts[0] = Hidden;
577 		    else
578 			Elts.erase(Elts.begin());
579 		    Ty = FunctionType::get(RetTy, Elts, false);
580 		    break;
581 		}
582 	    case DPointerType:
583 		if (!PointerType::isValidElementType(Elts[0]))
584 		    Ty = PointerType::getUnqual(Type::getInt8Ty(Context));
585 		else
586 		    Ty = PointerType::getUnqual(Elts[0]);
587 		break;
588 	    case DStructType:
589 	    case DPackedStructType:
590 		Ty = StructType::get(Context, Elts, type->kind == DPackedStructType);
591 		break;
592 	    case DArrayType:
593 		Ty = ArrayType::get(Elts[0], type->numElements);
594 		break;
595 	    default:
596 		llvm_unreachable("type->kind");
597 	}
598 	return Ty;
599     }
600 
get(uint16_t ty,const struct cli_bc_type * types,constType * Hidden)601     constType *get(uint16_t ty, const struct cli_bc_type *types, constType *Hidden)
602     {
603 	ty &= 0x7fff;
604 	if (ty < 69)
605 	    return getStatic(ty);
606 	ty -= 69;
607 	assert(ty < numTypes && "TypeID out of range");
608 #if LLVM_VERSION < 30
609 	return TypeMap[ty].get();
610 #else
611 	Type *Ty = TypeMap[ty];
612 	if (Ty)
613 	    return Ty;
614 	assert(types && Hidden || "accessing not-yet-built type");
615 	Ty = buildType(&types[ty], types, Hidden, 1);
616 	TypeMap[ty] = Ty;
617 	return Ty;
618 #endif
619     }
620 };
621 
622 struct CommonFunctions {
623     Function *FHandler;
624     Function *FMemset;
625     Function *FMemmove;
626     Function *FMemcpy;
627     Function *FRealmemset;
628     Function *FRealMemmove;
629     Function *FRealmemcmp;
630     Function *FRealmemcpy;
631     Function *FBSwap16;
632     Function *FBSwap32;
633     Function *FBSwap64;
634 };
635 
636 // loops with tripcounts higher than this need timeout check
637 static const unsigned LoopThreshold = 1000;
638 
639 // after every N API calls we need timeout check
640 static const unsigned ApiThreshold = 100;
641 
642 class RuntimeLimits : public FunctionPass {
643     typedef SmallVector<std::pair<const BasicBlock*, const BasicBlock*>, 16>
644 	BBPairVectorTy;
645     typedef SmallSet<BasicBlock*, 16> BBSetTy;
646     typedef DenseMap<const BasicBlock*, unsigned> BBMapTy;
loopNeedsTimeoutCheck(ScalarEvolution & SE,const Loop * L,BBMapTy & Map)647     bool loopNeedsTimeoutCheck(ScalarEvolution &SE, const Loop *L, BBMapTy &Map) {
648 	// This BB is a loop header, if trip count is small enough
649 	// no timeout checks are needed here.
650 	const SCEV *S = SE.getMaxBackedgeTakenCount(L);
651 	if (isa<SCEVCouldNotCompute>(S))
652 	    return true;
653 	DEBUG(errs() << "Found loop trip count" << *S << "\n");
654 	ConstantRange CR = SE.getUnsignedRange(S);
655 	uint64_t max = CR.getUnsignedMax().getLimitedValue();
656 	DEBUG(errs() << "Found max trip count " << max << "\n");
657 	if (max > LoopThreshold)
658 	    return true;
659 	unsigned apicalls = 0;
660 	for (Loop::block_iterator J=L->block_begin(),JE=L->block_end();
661 	     J != JE; ++J) {
662 	    apicalls += Map[*J];
663 	}
664 	apicalls *= max;
665 	if (apicalls > ApiThreshold) {
666 	    DEBUG(errs() << "apicall threshold exceeded: " << apicalls << "\n");
667 	    return true;
668 	}
669 	Map[L->getHeader()] = apicalls;
670 	return false;
671     }
672 
673 public:
674     static char ID;
DEFINEPASS(RuntimeLimits)675     DEFINEPASS(RuntimeLimits) {
676 #if LLVM_VERSION >= 29
677 	PassRegistry &Registry = *PassRegistry::getPassRegistry();
678 	initializeRuntimeLimitsPass(Registry);
679 #endif
680     }
681 
runOnFunction(Function & F)682     virtual bool runOnFunction(Function &F) {
683 	BBSetTy BackedgeTargets;
684 	if (!F.isDeclaration()) {
685 	    // Get the common backedge targets.
686 	    // Note that we don't rely on LoopInfo here, since
687 	    // it is possible to construct a CFG that doesn't have natural loops,
688 	    // yet it does have backedges, and thus can lead to unbounded/high
689 	    // execution time.
690 	    BBPairVectorTy V;
691 	    FindFunctionBackedges(F, V);
692 	    for (BBPairVectorTy::iterator I=V.begin(),E=V.end();I != E; ++I) {
693 		BackedgeTargets.insert(const_cast<BasicBlock*>(I->second));
694 	    }
695 	}
696 	BBSetTy  needsTimeoutCheck;
697 	BBMapTy BBMap;
698 #if LLVM_VERSION < 35
699 	DominatorTree &DT = getAnalysis<DominatorTree>();
700 #else
701 	DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
702 #endif
703 	for (Function::iterator I=F.begin(),E=F.end(); I != E; ++I) {
704 	    BasicBlock *BB = &*I;
705 	    unsigned apicalls = 0;
706 	    for (BasicBlock::const_iterator J=BB->begin(),JE=BB->end();
707 		 J != JE; ++J) {
708 		if (const CallInst *CI = dyn_cast<CallInst>(J)) {
709 		    Function *F = CI->getCalledFunction();
710 		    if (!F || F->isDeclaration())
711 			apicalls++;
712 		}
713 	    }
714 	    if (apicalls > ApiThreshold) {
715 		DEBUG(errs() << "apicall threshold exceeded: " << apicalls << "\n");
716 		needsTimeoutCheck.insert(BB);
717 		apicalls = 0;
718 	    }
719 	    BBMap[BB] = apicalls;
720 	}
721 	if (!BackedgeTargets.empty()) {
722 	    LoopInfo &LI = getAnalysis<LoopInfo>();
723 	    ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
724 
725 	    // Now check whether any of these backedge targets are part of a loop
726 	    // with a small constant trip count
727 	    for (BBSetTy::iterator I=BackedgeTargets.begin(),E=BackedgeTargets.end();
728 		 I != E; ++I) {
729 		const Loop *L = LI.getLoopFor(*I);
730 		if (L && L->getHeader() == *I &&
731 		    !loopNeedsTimeoutCheck(SE, L, BBMap))
732 		    continue;
733 		needsTimeoutCheck.insert(*I);
734 		BBMap[*I] = 0;
735 	    }
736 	}
737 	// Estimate number of apicalls by walking dominator-tree bottom-up.
738 	// BBs that have timeout checks are considered to have 0 APIcalls
739 	// (since we already checked for timeout).
740 	for (po_iterator<DomTreeNode*> I = po_begin(DT.getRootNode()),
741 	     E = po_end(DT.getRootNode()); I != E; ++I) {
742 	    if (needsTimeoutCheck.count(I->getBlock()))
743 		continue;
744 	    unsigned apicalls = BBMap[I->getBlock()];
745 	    for (DomTreeNode::iterator J=I->begin(),JE=I->end();
746 		 J != JE; ++J) {
747 		apicalls += BBMap[(*J)->getBlock()];
748 	    }
749 	    if (apicalls > ApiThreshold) {
750 		needsTimeoutCheck.insert(I->getBlock());
751 		apicalls = 0;
752 	    }
753 	    BBMap[I->getBlock()] = apicalls;
754 	}
755 	if (needsTimeoutCheck.empty())
756 	    return false;
757 	DEBUG(errs() << "needs timeoutcheck:\n");
758 	std::vector<constType*>args;
759 	FunctionType* abrtTy = FunctionType::get(
760 	    Type::getVoidTy(F.getContext()),args,false);
761 	Constant *func_abort =
762 	    F.getParent()->getOrInsertFunction("abort", abrtTy);
763 	BasicBlock *AbrtBB = BasicBlock::Create(F.getContext(), "", &F);
764         CallInst* AbrtC = CallInst::Create(func_abort, "", AbrtBB);
765         AbrtC->setCallingConv(CallingConv::C);
766         AbrtC->setTailCall(true);
767 #if LLVM_VERSION < 32
768         AbrtC->setDoesNotReturn(true);
769         AbrtC->setDoesNotThrow(true);
770 #else
771         AbrtC->setDoesNotReturn();
772         AbrtC->setDoesNotThrow();
773 #endif
774         new UnreachableInst(F.getContext(), AbrtBB);
775 	IRBuilder<false> Builder(F.getContext());
776 
777 	Value *Flag = F.arg_begin();
778 #if LLVM_VERSION < 30
779 	Function *LSBarrier = Intrinsic::getDeclaration(F.getParent(),
780 							Intrinsic::memory_barrier);
781 	Value *MBArgs[] = {
782 	    ConstantInt::getFalse(F.getContext()),
783 	    ConstantInt::getFalse(F.getContext()),
784 	    ConstantInt::getTrue(F.getContext()),
785 	    ConstantInt::getFalse(F.getContext()),
786 	    ConstantInt::getFalse(F.getContext())
787 	};
788 #endif
789 	verifyFunction(F);
790 	BasicBlock *BB = &F.getEntryBlock();
791 	Builder.SetInsertPoint(BB, BB->getTerminator());
792 	Flag = Builder.CreatePointerCast(Flag, PointerType::getUnqual(
793 		Type::getInt1Ty(F.getContext())));
794 	for (BBSetTy::iterator I=needsTimeoutCheck.begin(),
795 	     E=needsTimeoutCheck.end(); I != E; ++I) {
796 	    BasicBlock *BB = *I;
797 	    Builder.SetInsertPoint(BB, BB->getTerminator());
798 #if LLVM_VERSION < 30
799 	    // store-load barrier: will be a no-op on x86 but not other arches
800 	    Builder.CreateCall(LSBarrier, ARRAYREF(Value*, MBArgs, MBArgs+5));
801 #else
802 	    Builder.CreateFence(Release);
803 #endif
804 	    // Load Flag that tells us we timed out (first byte in bc_ctx)
805 	    Value *Cond = Builder.CreateLoad(Flag, true);
806 	    BasicBlock *newBB = SplitBlock(BB, BB->getTerminator(), this);
807 	    TerminatorInst *TI = BB->getTerminator();
808 	    BranchInst::Create(AbrtBB, newBB, Cond, TI);
809 	    TI->eraseFromParent();
810 	    // Update dominator info
811 	    DomTreeNode *N = DT.getNode(AbrtBB);
812 	    if (!N) {
813 		DT.addNewBlock(AbrtBB, BB);
814 	    } else {
815 		BasicBlock *DomBB = DT.findNearestCommonDominator(BB,
816 								  N->getIDom()->getBlock());
817 		DT.changeImmediateDominator(AbrtBB, DomBB);
818 	    }
819 	    DEBUG(errs() << *I << "\n");
820 	}
821 	//verifyFunction(F);
822 	return true;
823     }
824 
getAnalysisUsage(AnalysisUsage & AU) const825     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
826       AU.setPreservesAll();
827       AU.addRequired<LoopInfo>();
828       AU.addRequired<ScalarEvolution>();
829 #if LLVM_VERSION < 35
830       AU.addRequired<DominatorTree>();
831 #else
832       AU.addRequired<DominatorTreeWrapperPass>();
833 #endif
834     }
835 };
836 char RuntimeLimits::ID;
837 
838 // select i1 false ... which instcombine would simplify but we don't run
839 // instcombine.
840 class BrSimplifier : public FunctionPass {
841 public:
842     static char ID;
DEFINEPASS(BrSimplifier)843     DEFINEPASS(BrSimplifier) {}
844 
runOnFunction(Function & F)845     virtual bool runOnFunction(Function &F) {
846 	bool Changed = false;
847 	for (Function::iterator I=F.begin(),E=F.end(); I != E; ++I) {
848 	    if (BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator())) {
849 		if (BI->isUnconditional())
850 		    continue;
851 		Value *V = BI->getCondition();
852 		if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
853 		    BasicBlock *Other;
854 		    if (CI->isOne()) {
855 			BranchInst::Create(BI->getSuccessor(0), &*I);
856 			Other = BI->getSuccessor(1);
857 		    } else {
858 			BranchInst::Create(BI->getSuccessor(1), &*I);
859 			Other = BI->getSuccessor(0);
860 		    }
861 		    Other->removePredecessor(&*I);
862 		    BI->eraseFromParent();
863 		    Changed = true;
864 		}
865 	    }
866 	    for (BasicBlock::iterator J=I->begin(),JE=I->end();
867 		 J != JE;) {
868 		SelectInst *SI = dyn_cast<SelectInst>(J);
869 		++J;
870 		if (!SI)
871 		    continue;
872 		ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
873 		if (!CI)
874 		    continue;
875 		if (CI->isOne())
876 		    SI->replaceAllUsesWith(SI->getTrueValue());
877 		else
878 		    SI->replaceAllUsesWith(SI->getFalseValue());
879 		SI->eraseFromParent();
880 		Changed = true;
881 	    }
882 	}
883 
884 	return Changed;
885     }
886 };
887 char BrSimplifier::ID;
888 /*
889 class SimpleGlobalDCE : public ModulePass {
890     ExecutionEngine *EE;
891 public:
892     static char ID;
893     SimpleGlobalDCE(ExecutionEngine *EE) : ModulePass(&ID), EE(EE) {}
894 
895     virtual bool runOnModule(Module &M) {
896 	bool Changed = false;
897 	std::vector<GlobalValue*> toErase;
898 	for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
899 	    GlobalValue *GV = &*I;
900 	    if (GV->use_empty() && !EE->getPointerToGlobalIfAvailable(GV))
901 		toErase.push_back(GV);
902 	}
903 	for (std::vector<GlobalValue*>::iterator I=toErase.begin(), E=toErase.end();
904 	     I != E; ++I) {
905 	    (*I)->eraseFromParent();
906 	    Changed = true;
907 	}
908 
909 	return Changed;
910     }
911 };
912 char SimpleGlobalDCE::ID;
913 */
914 class LLVMCodegen {
915 private:
916     const struct cli_bc *bc;
917     Module *M;
918     LLVMContext &Context;
919     ExecutionEngine *EE;
920     FunctionPassManager &PM, &PMUnsigned;
921     LLVMTypeMapper *TypeMap;
922 
923     Function **apiFuncs;
924     LLVMTypeMapper &apiMap;
925     FunctionMapTy &compiledFunctions;
926     Twine BytecodeID;
927 
928     TargetFolder Folder;
929     IRBuilder<false, TargetFolder> Builder;
930 
931     std::vector<Value*> globals;
932     DenseMap<unsigned, unsigned> GVoffsetMap;
933     DenseMap<unsigned, constType*> GVtypeMap;
934     Value **Values;
935     unsigned numLocals;
936     unsigned numArgs;
937     std::vector<MDNode*> mdnodes;
938 
939     struct CommonFunctions *CF;
940 
getOperand(const struct cli_bc_func * func,constType * Ty,operand_t operand)941     Value *getOperand(const struct cli_bc_func *func, constType *Ty, operand_t operand)
942     {
943 	unsigned map[] = {0, 1, 2, 3, 3, 4, 4, 4, 4};
944 	if (operand < func->numValues)
945 	    return Values[operand];
946 	unsigned w = Ty->getPrimitiveSizeInBits();
947 	if (w > 1)
948 	    w = (w+7)/8;
949 	else
950 	    w = 0;
951 	return convertOperand(func, map[w], operand);
952     }
953 
convertOperand(const struct cli_bc_func * func,constType * Ty,operand_t operand)954     Value *convertOperand(const struct cli_bc_func *func, constType *Ty, operand_t operand)
955     {
956 	unsigned map[] = {0, 1, 2, 3, 3, 4, 4, 4, 4};
957 	if (operand < func->numArgs)
958 	    return Values[operand];
959 	if (operand < func->numValues) {
960 	    Value *V = Values[operand];
961 	    if (func->types[operand]&0x8000 && V->getType() == Ty) {
962 		return V;
963 	    }
964 	    V = Builder.CreateLoad(V);
965 	    if (V->getType() != Ty &&
966 		isa<PointerType>(V->getType()) &&
967 		isa<PointerType>(Ty))
968 		V = Builder.CreateBitCast(V, Ty);
969 	    if (V->getType() != Ty) {
970 		if (cli_debug_flag) {
971 		    std::string str;
972 		    raw_string_ostream ostr(str);
973 		    ostr << operand << " " ;
974 		    V->print(ostr);
975 		    Ty->print(ostr);
976 		    M->dump();
977 		    cli_dbgmsg_internal("[Bytecode JIT]: operand %d: %s\n", operand,ostr.str().c_str());
978 		}
979 		llvm_report_error("(libclamav) Type mismatch converting operand");
980 	    }
981 	    return V;
982 	}
983 	unsigned w = Ty->getPrimitiveSizeInBits();
984 	if (w > 1)
985 	    w = (w+7)/8;
986 	else
987 	    w = 0;
988 	return convertOperand(func, map[w], operand);
989     }
990 
convertOperand(const struct cli_bc_func * func,const struct cli_bc_inst * inst,operand_t operand)991     Value *convertOperand(const struct cli_bc_func *func,
992 			  const struct cli_bc_inst *inst,  operand_t operand)
993     {
994 	return convertOperand(func, inst->interp_op%5, operand);
995     }
996 
convertOperand(const struct cli_bc_func * func,unsigned w,operand_t operand)997     Value *convertOperand(const struct cli_bc_func *func,
998 			  unsigned w, operand_t operand) {
999 	if (operand < func->numArgs)
1000 	    return Values[operand];
1001 	if (operand < func->numValues) {
1002 	    if (func->types[operand]&0x8000)
1003 		return Values[operand];
1004 	    return Builder.CreateLoad(Values[operand]);
1005 	}
1006 
1007 	if (operand & 0x80000000) {
1008 	    operand &= 0x7fffffff;
1009 	    assert(operand < globals.size() && "Global index out of range");
1010 	    // Global
1011 	    if (!operand)
1012 		return ConstantPointerNull::get(PointerType::getUnqual(Type::getInt8Ty(Context)));
1013 	    assert(globals[operand]);
1014 	    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(globals[operand])) {
1015 		if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
1016 		    return CE;
1017 		}
1018 		return GV;
1019 	    }
1020 	    return globals[operand];
1021 	}
1022 	// Constant
1023 	operand -= func->numValues;
1024 	// This was already validated by libclamav.
1025 	assert(operand < func->numConstants && "Constant out of range");
1026 	uint64_t *c = &func->constants[operand];
1027 	uint64_t v;
1028 	constType *Ty;
1029 	switch (w) {
1030 	    case 0:
1031 	    case 1:
1032 		Ty = w ? Type::getInt8Ty(Context) :
1033 		    Type::getInt1Ty(Context);
1034 		v = *(uint8_t*)c;
1035 		break;
1036 	    case 2:
1037 		Ty = Type::getInt16Ty(Context);
1038 		v = *(uint16_t*)c;
1039 		break;
1040 	    case 3:
1041 		Ty = Type::getInt32Ty(Context);
1042 		v = *(uint32_t*)c;
1043 		break;
1044 	    case 4:
1045 		Ty = Type::getInt64Ty(Context);
1046 		v = *(uint64_t*)c;
1047 		break;
1048 	    default:
1049 		llvm_unreachable("width");
1050 	}
1051 	return ConstantInt::get(Ty, v);
1052     }
1053 
Store(uint16_t dest,Value * V)1054     void Store(uint16_t dest, Value *V)
1055     {
1056 	assert(dest >= numArgs && dest < numLocals+numArgs && "Instruction destination out of range");
1057 	Builder.CreateStore(V, Values[dest]);
1058     }
1059 
1060     // Insert code that calls \arg CF->FHandler if \arg FailCond is true.
InsertVerify(Value * FailCond,BasicBlock * & Fail,Function * FHandler,Function * F)1061     void InsertVerify(Value *FailCond, BasicBlock *&Fail, Function *FHandler,
1062 		      Function *F) {
1063 	if (!Fail) {
1064 	    Fail = BasicBlock::Create(Context, "fail", F);
1065 	    CallInst::Create(FHandler,"",Fail);
1066 	    new UnreachableInst(Context, Fail);
1067 	}
1068 	BasicBlock *OkBB = BasicBlock::Create(Context, "", F);
1069 	Builder.CreateCondBr(FailCond, Fail, OkBB);
1070 	Builder.SetInsertPoint(OkBB);
1071     }
1072 
mapType(uint16_t typeID)1073     constType* mapType(uint16_t typeID)
1074     {
1075 	return TypeMap->get(typeID&0x7fffffff, NULL, NULL);
1076     }
1077 
buildConstant(constType * Ty,uint64_t * components,unsigned & c)1078     Constant *buildConstant(constType *Ty, uint64_t *components, unsigned &c)
1079     {
1080         if (constPointerType *PTy = dyn_cast<PointerType>(Ty)) {
1081           Value *idxs[1] = {
1082 	      ConstantInt::get(Type::getInt64Ty(Context), components[c++])
1083 	  };
1084 	  unsigned idx = components[c++];
1085 	  if (!idx)
1086 	      return ConstantPointerNull::get(PTy);
1087 	  assert(idx < globals.size());
1088 	  GlobalVariable *GV = cast<GlobalVariable>(globals[idx]);
1089 	  Type *IP8Ty = PointerType::getUnqual(Type::getInt8Ty(Ty->getContext()));
1090 	  Constant *C = ConstantExpr::getPointerCast(GV, IP8Ty);
1091 	  //TODO: check constant bounds here
1092 	  return ConstantExpr::getPointerCast(
1093 	      ConstantExpr::getInBoundsGetElementPtr(C, ARRAYREF(Value*, idxs, 1)),
1094 	      PTy);
1095         }
1096 	if (isa<IntegerType>(Ty)) {
1097 	    return ConstantInt::get(Ty, components[c++]);
1098 	}
1099 	if (constArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1100 	   std::vector<Constant*> elements;
1101 	   elements.reserve(ATy->getNumElements());
1102 	   for (unsigned i=0;i<ATy->getNumElements();i++) {
1103 	       elements.push_back(buildConstant(ATy->getElementType(), components, c));
1104 	   }
1105 	   return ConstantArray::get(ATy, elements);
1106 	}
1107 	if (constStructType *STy = dyn_cast<StructType>(Ty)) {
1108 	   std::vector<Constant*> elements;
1109 	   elements.reserve(STy->getNumElements());
1110 	   for (unsigned i=0;i<STy->getNumElements();i++) {
1111 	       elements.push_back(buildConstant(STy->getElementType(i), components, c));
1112 	   }
1113 	   return ConstantStruct::get(STy, elements);
1114 	}
1115 	Ty->dump();
1116 	llvm_unreachable("invalid type");
1117 	return 0;
1118     }
1119 
1120 public:
LLVMCodegen(const struct cli_bc * bc,Module * M,struct CommonFunctions * CF,FunctionMapTy & cFuncs,ExecutionEngine * EE,FunctionPassManager & PM,FunctionPassManager & PMUnsigned,Function ** apiFuncs,LLVMTypeMapper & apiMap)1121     LLVMCodegen(const struct cli_bc *bc, Module *M, struct CommonFunctions *CF, FunctionMapTy &cFuncs,
1122 		ExecutionEngine *EE, FunctionPassManager &PM, FunctionPassManager &PMUnsigned,
1123 		Function **apiFuncs, LLVMTypeMapper &apiMap)
1124 	: bc(bc), M(M), Context(M->getContext()), EE(EE),
1125 	PM(PM),PMUnsigned(PMUnsigned), TypeMap(), apiFuncs(apiFuncs),apiMap(apiMap),
1126 	compiledFunctions(cFuncs), BytecodeID("bc"+Twine(bc->id)),
1127 #if LLVM_VERSION < 32
1128 	Folder(EE->getTargetData()), Builder(Context, Folder), Values(), CF(CF) {
1129 #else
1130 	Folder(EE->getDataLayout()), Builder(Context, Folder), Values(), CF(CF) {
1131 #endif
1132 
1133 	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
1134 	    unsigned id = cli_globals[i].globalid;
1135 	    GVoffsetMap[id] = cli_globals[i].offset;
1136 	}
1137 	numLocals = 0;
1138 	numArgs = 0;
1139     }
1140 
1141 #if LLVM_VERSION < 30
1142     template <typename InputIterator>
1143 #endif
1144     Value* createGEP(Value *Base, constType *ETy, ARRAYREFPARAM(Value*,InputIterator Start,InputIterator End,ARef)) {
1145 	constType *Ty = GetElementPtrInst::getIndexedType(Base->getType(),ARRAYREFP(Start,End,ARef));
1146 	if (!Ty || (ETy && (Ty != ETy && (!isa<IntegerType>(Ty) || !isa<IntegerType>(ETy))))) {
1147 	    if (cli_debug_flag) {
1148 		std::string str;
1149 		raw_string_ostream ostr(str);
1150 
1151 		ostr << "Wrong indices for GEP opcode: "
1152 		    << " expected type: " << *ETy;
1153 		if (Ty)
1154 		    ostr << " actual type: " << *Ty;
1155 		ostr << " base: " << *Base << ";";
1156 		Base->getType()->print(ostr);
1157 		ostr << "\n indices: ";
1158 #if LLVM_VERSION < 30
1159 		for (InputIterator I=Start; I != End; I++) {
1160 #else
1161 		for (ArrayRef<Value*>::iterator I=ARef.begin(); I != ARef.end(); I++) {
1162 #endif
1163 		    ostr << **I << ", ";
1164 		}
1165 		ostr << "\n";
1166 		cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
1167 	    } else {
1168 		cli_warnmsg("[Bytecode JIT]: Wrong indices for GEP opcode\n");
1169 	    }
1170 	    return 0;
1171 	}
1172 	return Builder.CreateGEP(Base,ARRAYREFP(Start,End,ARef));
1173     }
1174 
1175 #if LLVM_VERSION < 30
1176     template <typename InputIterator>
1177 #endif
1178     bool createGEP(unsigned dest, Value *Base, ARRAYREFPARAM(Value*,InputIterator Start,InputIterator End,ARef)) {
1179 	assert(dest >= numArgs && dest < numLocals+numArgs && "Instruction destination out of range");
1180 	constType *ETy = cast<PointerType>(cast<PointerType>(Values[dest]->getType())->getElementType())->getElementType();
1181 	Value *V = createGEP(Base, ETy, ARRAYREFP(Start,End,ARef));
1182 	if (!V) {
1183 	    if (cli_debug_flag)
1184 		cli_dbgmsg_internal("[Bytecode JIT] @%d\n", dest);
1185 	    return false;
1186 	}
1187 	V = Builder.CreateBitCast(V, PointerType::getUnqual(ETy));
1188 	Store(dest, V);
1189 	return true;
1190     }
1191 
1192     MDNode *convertMDNode(unsigned i) {
1193 	if (i < mdnodes.size()) {
1194 	    if (mdnodes[i])
1195 		return mdnodes[i];
1196 	} else
1197 	    mdnodes.resize(i+1);
1198 	assert(i < mdnodes.size());
1199 	const struct cli_bc_dbgnode *node = &bc->dbgnodes[i];
1200 #if LLVM_VERSION < 36
1201 	Value **Vals = new Value*[node->numelements];
1202 #else
1203 	Metadata **Vals = new Metadata*[node->numelements];
1204 #endif
1205 	for (unsigned j=0;j<node->numelements;j++) {
1206 	    const struct cli_bc_dbgnode_element* el = &node->elements[j];
1207 #if LLVM_VERSION < 36
1208 	    Value *V;
1209 #else
1210 	    Metadata *V;
1211 #endif
1212 	    if (!el->len) {
1213 		if (el->nodeid == ~0u)
1214 		    V = 0;
1215 		else if (el->nodeid)
1216 		    V = convertMDNode(el->nodeid);
1217 		else
1218 		    V = MDString::get(Context, "");
1219 	    } else if (el->string) {
1220 		V = MDString::get(Context, StringRef(el->string, el->len));
1221 	    } else {
1222 #if LLVM_VERSION < 36
1223 		V = ConstantInt::get(IntegerType::get(Context, el->len),
1224 				     el->constant);
1225 #else
1226 		V = ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(Context, el->len),
1227 				     el->constant));
1228 #endif
1229 	    }
1230 	    Vals[j] = V;
1231 	}
1232 #if LLVM_VERSION < 36
1233 	MDNode *N = MDNode::get(Context, ARRAYREF(Value*,Vals, node->numelements));
1234 #else
1235 	MDNode *N = MDNode::get(Context, ARRAYREF(Metadata*,Vals, node->numelements));
1236 #endif
1237 	delete[] Vals;
1238 	mdnodes[i] = N;
1239 	return N;
1240     }
1241 
1242     void AddStackProtect(Function *F)
1243     {
1244 	BasicBlock &BB = F->getEntryBlock();
1245 	if (isa<AllocaInst>(BB.begin())) {
1246 	    // Have an alloca -> some instruction uses its address otherwise
1247 	    // mem2reg would have converted it to an SSA register.
1248 	    // Enable stack protector for this function.
1249 #if LLVM_VERSION < 29
1250 	    // LLVM 2.9 has broken SSP, it does a 'mov 0x28, $rax', which tries
1251 	    // to read from the address 0x28 and crashes
1252 	    F->addFnAttr(Attribute::StackProtectReq);
1253 #endif
1254 	}
1255 	// always add stackprotect attribute (bb #2239), so we know this
1256 	// function was verified. If there is no alloca it won't actually add
1257 	// stack protector in emitted code so this won't slow down the app.
1258 #if LLVM_VERSION < 29
1259 	F->addFnAttr(Attribute::StackProtect);
1260 #endif
1261     }
1262 
1263     Value *GEPOperand(Value *V) {
1264 	if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
1265 	    Value *VI = LI->getOperand(0);
1266 	    StoreInst *SI = 0;
1267 	    for (Value::use_iterator I=VI->use_begin(),
1268 		 E=VI->use_end(); I != E; ++I) {
1269 		Value *I_V = *I;
1270 		if (StoreInst *S = dyn_cast<StoreInst>(I_V)) {
1271 		    if (SI)
1272 			return V;
1273 		    SI = S;
1274 		} else if (!isa<LoadInst>(I_V))
1275 		    return V;
1276 	    }
1277 	    V = SI->getOperand(0);
1278 	}
1279 #if LLVM_VERSION < 32
1280 	if (EE->getTargetData()->getPointerSize() == 8) {
1281 #else
1282 	if (EE->getDataLayout()->getPointerSize() == 8) {
1283 #endif
1284 	    // eliminate useless trunc, GEP can take i64 too
1285 	    if (TruncInst *I = dyn_cast<TruncInst>(V)) {
1286 		Value *Src = I->getOperand(0);
1287 		if (Src->getType() == Type::getInt64Ty(Context) &&
1288 		    I->getType() == Type::getInt32Ty(Context))
1289 		    return Src;
1290 	    }
1291 	}
1292 	return V;
1293     }
1294 
1295    Function* generate() {
1296         PrettyStackTraceString CrashInfo("Generate LLVM IR functions");
1297 	apiMap.irgenTimer.startTimer();
1298 	TypeMap = new LLVMTypeMapper(Context, bc->types + 4, bc->num_types - 5);
1299 	for (unsigned i=0;i<bc->dbgnode_cnt;i++) {
1300 	    mdnodes.push_back(convertMDNode(i));
1301 	}
1302 
1303 	for (unsigned i=0;i<cli_apicall_maxglobal - _FIRST_GLOBAL;i++) {
1304 	    unsigned id = cli_globals[i].globalid;
1305 	    constType *Ty = apiMap.get(cli_globals[i].type, NULL, NULL);
1306 	    /*if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
1307 		Ty = PointerType::getUnqual(ATy->getElementType());*/
1308 	    GVtypeMap[id] = Ty;
1309 	}
1310 
1311 	// The hidden ctx param to all functions
1312 	unsigned maxh = cli_globals[0].offset + sizeof(struct cli_bc_hooks);
1313 	constType *HiddenCtx = PointerType::getUnqual(ArrayType::get(Type::getInt8Ty(Context), maxh));
1314 
1315 	globals.reserve(bc->num_globals);
1316 	BitVector FakeGVs;
1317 	FakeGVs.resize(bc->num_globals);
1318 	globals.push_back(0);
1319 	for (unsigned i=1;i<bc->num_globals;i++) {
1320 	    constType *Ty = mapType(bc->globaltys[i]);
1321 
1322 	    // TODO: validate number of components against type_components
1323 	    unsigned c = 0;
1324 	    GlobalVariable *GV;
1325 	    if (isa<PointerType>(Ty)) {
1326 		unsigned g = bc->globals[i][1];
1327 		if (GVoffsetMap.count(g)) {
1328 		    FakeGVs.set(i);
1329 		    globals.push_back(0);
1330 		    continue;
1331 		}
1332 	    }
1333 	    Constant *C = buildConstant(Ty, bc->globals[i], c);
1334 	    GV = new GlobalVariable(*M, Ty, true,
1335 				    GlobalValue::InternalLinkage,
1336 				    C, "glob"+Twine(i));
1337 	    globals.push_back(GV);
1338 	}
1339 	Function **Functions = new Function*[bc->num_func];
1340 	for (unsigned j=0;j<bc->num_func;j++) {
1341 	    // Create LLVM IR Function
1342 	    const struct cli_bc_func *func = &bc->funcs[j];
1343 	    std::vector<constType*> argTypes;
1344 	    argTypes.push_back(HiddenCtx);
1345 	    for (unsigned a=0;a<func->numArgs;a++) {
1346 		argTypes.push_back(mapType(func->types[a]));
1347 	    }
1348 	    constType *RetTy = mapType(func->returnType);
1349 	    FunctionType *FTy =  FunctionType::get(RetTy, argTypes,
1350 							 false);
1351 	    Functions[j] = Function::Create(FTy, Function::InternalLinkage,
1352 					   BytecodeID+"f"+Twine(j), M);
1353 	    Functions[j]->setDoesNotThrow();
1354 	    Functions[j]->setCallingConv(CallingConv::Fast);
1355 	    Functions[j]->setLinkage(GlobalValue::InternalLinkage);
1356 #ifdef C_LINUX
1357 	    /* bb #2270, this should really be fixed either by LLVM or GCC.*/
1358 #if LLVM_VERSION < 32
1359 	    Functions[j]->addFnAttr(Attribute::constructStackAlignmentFromInt(16));
1360 #else
1361 	// TODO: How does this translate?
1362 //	    Functions[j]->addFnAttr(Attribute::StackAlignment);
1363 #endif
1364 #endif
1365 	}
1366 	constType *I32Ty = Type::getInt32Ty(Context);
1367 	for (unsigned j=0;j<bc->num_func;j++) {
1368 	    PrettyStackTraceString CrashInfo("Generate LLVM IR");
1369 	    const struct cli_bc_func *func = &bc->funcs[j];
1370 	    bool broken = false;
1371 
1372 	    // Create all BasicBlocks
1373 	    Function *F = Functions[j];
1374 	    BasicBlock **BB = new BasicBlock*[func->numBB];
1375 	    for (unsigned i=0;i<func->numBB;i++) {
1376 		BB[i] = BasicBlock::Create(Context, "", F);
1377 	    }
1378 
1379 	    BasicBlock *Fail = 0;
1380 	    Values = new Value*[func->numValues];
1381 	    Builder.SetInsertPoint(BB[0]);
1382 	    Function::arg_iterator I = F->arg_begin();
1383 	    assert(F->arg_size() == (unsigned)(func->numArgs + 1) && "Mismatched args");
1384 	    ++I;
1385 	    for (unsigned i=0;i<func->numArgs; i++) {
1386 		assert(I != F->arg_end());
1387 		Values[i] = &*I;
1388 		++I;
1389 	    }
1390 	    for (unsigned i=func->numArgs;i<func->numValues;i++) {
1391 		if (!func->types[i]) {
1392 		    //instructions without return value, like store
1393 		    Values[i] = 0;
1394 		    continue;
1395 		}
1396 		Values[i] = Builder.CreateAlloca(mapType(func->types[i]));
1397 	    }
1398 	    numLocals = func->numLocals;
1399 	    numArgs = func->numArgs;
1400 
1401 	    if (FakeGVs.any()) {
1402 		Argument *Ctx = F->arg_begin();
1403 		for (unsigned i=0;i<bc->num_globals;i++) {
1404 		    if (!FakeGVs[i])
1405 			continue;
1406 		    unsigned g = bc->globals[i][1];
1407 		    unsigned offset = GVoffsetMap[g];
1408 
1409 		    Constant *Idx = ConstantInt::get(Type::getInt32Ty(Context),
1410 						     offset);
1411 		    Value *Idxs[2] = {
1412 			ConstantInt::get(Type::getInt32Ty(Context), 0),
1413 			Idx
1414 		    };
1415 		    Value *GEP = Builder.CreateInBoundsGEP(Ctx, ARRAYREF(Value*, Idxs, Idxs+2));
1416 		    constType *Ty = GVtypeMap[g];
1417 		    Ty = PointerType::getUnqual(PointerType::getUnqual(Ty));
1418 		    Value *Cast = Builder.CreateBitCast(GEP, Ty);
1419 		    Value *SpecialGV = Builder.CreateLoad(Cast);
1420 		    constType *IP8Ty = Type::getInt8Ty(Context);
1421 		    IP8Ty = PointerType::getUnqual(IP8Ty);
1422 		    SpecialGV = Builder.CreateBitCast(SpecialGV, IP8Ty);
1423 		    SpecialGV->setName("g"+Twine(g-_FIRST_GLOBAL)+"_");
1424 		    Value *C[] = {
1425 			ConstantInt::get(Type::getInt32Ty(Context), bc->globals[i][0])
1426 		    };
1427 		    globals[i] = createGEP(SpecialGV, 0, ARRAYREF(Value*,C, C+1));
1428 		    if (!globals[i]) {
1429 			if (cli_debug_flag) {
1430 			    std::string str;
1431 			    raw_string_ostream ostr(str);
1432 			    ostr << i << ":" << g << ":" << bc->globals[i][0] <<"\n";
1433 			    Ty->print(ostr);
1434 			    cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
1435 			}
1436 			llvm_report_error("(libclamav) unable to create fake global");
1437 		    }
1438 		    globals[i] = Builder.CreateBitCast(globals[i], Ty);
1439 		    if(GetElementPtrInst *GI = dyn_cast<GetElementPtrInst>(globals[i])) {
1440 			GI->setIsInBounds(true);
1441 			GI->setName("geped"+Twine(i)+"_");
1442 		    }
1443 		}
1444 	    }
1445 
1446 	    // Generate LLVM IR for each BB
1447 	    for (unsigned i=0;i<func->numBB && !broken;i++) {
1448 		bool unreachable = false;
1449 		const struct cli_bc_bb *bb = &func->BB[i];
1450 		Builder.SetInsertPoint(BB[i]);
1451 		unsigned c = 0;
1452 		for (unsigned j=0;j<bb->numInsts && !broken;j++) {
1453 		    const struct cli_bc_inst *inst = &bb->insts[j];
1454 		    Value *Op0=0, *Op1=0, *Op2=0;
1455 		    // libclamav has already validated this.
1456 		    assert(inst->opcode < OP_BC_INVALID && "Invalid opcode");
1457 		    if (func->dbgnodes) {
1458 			if (func->dbgnodes[c] != ~0u) {
1459 			unsigned j = func->dbgnodes[c];
1460 			assert(j < mdnodes.size());
1461 			Builder.SetCurrentDebugLocation(mdnodes[j]);
1462 			} else
1463 			    Builder.SetCurrentDebugLocation(0);
1464 		    }
1465 		    c++;
1466 		    switch (inst->opcode) {
1467 			case OP_BC_JMP:
1468 			case OP_BC_BRANCH:
1469 			case OP_BC_CALL_API:
1470 			case OP_BC_CALL_DIRECT:
1471 			case OP_BC_ZEXT:
1472 			case OP_BC_SEXT:
1473 			case OP_BC_TRUNC:
1474 			case OP_BC_GEP1:
1475 			case OP_BC_GEPZ:
1476 			case OP_BC_GEPN:
1477 			case OP_BC_STORE:
1478 			case OP_BC_COPY:
1479 			case OP_BC_RET:
1480 			case OP_BC_PTRDIFF32:
1481 			case OP_BC_PTRTOINT64:
1482 			    // these instructions represents operands differently
1483 			    break;
1484 			default:
1485 			    switch (operand_counts[inst->opcode]) {
1486 				case 1:
1487 				    Op0 = convertOperand(func, inst, inst->u.unaryop);
1488 				    break;
1489 				case 2:
1490 				    Op0 = convertOperand(func, inst, inst->u.binop[0]);
1491 				    Op1 = convertOperand(func, inst, inst->u.binop[1]);
1492 				    if (Op0->getType() != Op1->getType()) {
1493 					Op0->dump();
1494 					Op1->dump();
1495 					llvm_report_error("(libclamav) binop type mismatch");
1496 				    }
1497 				    break;
1498 				case 3:
1499 				    Op0 = convertOperand(func, inst, inst->u.three[0]);
1500 				    Op1 = convertOperand(func, inst, inst->u.three[1]);
1501 				    Op2 = convertOperand(func, inst, inst->u.three[2]);
1502 				    break;
1503 			    }
1504 		    }
1505 
1506 		    switch (inst->opcode) {
1507 			case OP_BC_ADD:
1508 			    Store(inst->dest, Builder.CreateAdd(Op0, Op1));
1509 			    break;
1510 			case OP_BC_SUB:
1511 			    Store(inst->dest, Builder.CreateSub(Op0, Op1));
1512 			    break;
1513 			case OP_BC_MUL:
1514 			    Store(inst->dest, Builder.CreateMul(Op0, Op1));
1515 			    break;
1516 			case OP_BC_UDIV:
1517 			{
1518 			    Value *Bad = Builder.CreateICmpEQ(Op1, ConstantInt::get(Op1->getType(), 0));
1519 			    InsertVerify(Bad, Fail, CF->FHandler, F);
1520 			    Store(inst->dest, Builder.CreateUDiv(Op0, Op1));
1521 			    break;
1522 			}
1523 			case OP_BC_SDIV:
1524 			{
1525 			    //TODO: also verify Op0 == -1 && Op1 = INT_MIN
1526 			    Value *Bad = Builder.CreateICmpEQ(Op1, ConstantInt::get(Op1->getType(), 0));
1527 			    InsertVerify(Bad, Fail, CF->FHandler, F);
1528 			    Store(inst->dest, Builder.CreateSDiv(Op0, Op1));
1529 			    break;
1530 			}
1531 			case OP_BC_UREM:
1532 			{
1533 			    Value *Bad = Builder.CreateICmpEQ(Op1, ConstantInt::get(Op1->getType(), 0));
1534 			    InsertVerify(Bad, Fail, CF->FHandler, F);
1535 			    Store(inst->dest, Builder.CreateURem(Op0, Op1));
1536 			    break;
1537 			}
1538 			case OP_BC_SREM:
1539 			{
1540 			    //TODO: also verify Op0 == -1 && Op1 = INT_MIN
1541 			    Value *Bad = Builder.CreateICmpEQ(Op1, ConstantInt::get(Op1->getType(), 0));
1542 			    InsertVerify(Bad, Fail, CF->FHandler, F);
1543 			    Store(inst->dest, Builder.CreateSRem(Op0, Op1));
1544 			    break;
1545 			}
1546 			case OP_BC_SHL:
1547 			    Store(inst->dest, Builder.CreateShl(Op0, Op1));
1548 			    break;
1549 			case OP_BC_LSHR:
1550 			    Store(inst->dest, Builder.CreateLShr(Op0, Op1));
1551 			    break;
1552 			case OP_BC_ASHR:
1553 			    Store(inst->dest, Builder.CreateAShr(Op0, Op1));
1554 			    break;
1555 			case OP_BC_AND:
1556 			    Store(inst->dest, Builder.CreateAnd(Op0, Op1));
1557 			    break;
1558 			case OP_BC_OR:
1559 			    Store(inst->dest, Builder.CreateOr(Op0, Op1));
1560 			    break;
1561 			case OP_BC_XOR:
1562 			    Store(inst->dest, Builder.CreateXor(Op0, Op1));
1563 			    break;
1564 			case OP_BC_TRUNC:
1565 			{
1566 			    Value *Src = convertOperand(func, inst, inst->u.cast.source);
1567 			    constType *Ty = mapType(func->types[inst->dest]);
1568 			    Store(inst->dest, Builder.CreateTrunc(Src,  Ty));
1569 			    break;
1570 			}
1571 			case OP_BC_ZEXT:
1572 			{
1573 			    Value *Src = convertOperand(func, inst, inst->u.cast.source);
1574 			    constType *Ty = mapType(func->types[inst->dest]);
1575 			    Store(inst->dest, Builder.CreateZExt(Src,  Ty));
1576 			    break;
1577 			}
1578 			case OP_BC_SEXT:
1579 			{
1580 			    Value *Src = convertOperand(func, inst, inst->u.cast.source);
1581 			    constType *Ty = mapType(func->types[inst->dest]);
1582 			    Store(inst->dest, Builder.CreateSExt(Src,  Ty));
1583 			    break;
1584 			}
1585 			case OP_BC_BRANCH:
1586 			{
1587 			    Value *Cond = convertOperand(func, inst, inst->u.branch.condition);
1588 			    BasicBlock *True = BB[inst->u.branch.br_true];
1589 			    BasicBlock *False = BB[inst->u.branch.br_false];
1590 			    if (Cond->getType() != Type::getInt1Ty(Context)) {
1591 				cli_warnmsg("[Bytecode JIT]: type mismatch in condition");
1592 				broken = true;
1593 				break;
1594 			    }
1595 			    Builder.CreateCondBr(Cond, True, False);
1596 			    break;
1597 			}
1598 			case OP_BC_JMP:
1599 			{
1600 			    BasicBlock *Jmp = BB[inst->u.jump];
1601 			    Builder.CreateBr(Jmp);
1602 			    break;
1603 			}
1604 			case OP_BC_RET:
1605 			{
1606 			    Op0 = convertOperand(func, F->getReturnType(), inst->u.unaryop);
1607 			    Builder.CreateRet(Op0);
1608 			    break;
1609 			}
1610 			case OP_BC_RET_VOID:
1611 			    Builder.CreateRetVoid();
1612 			    break;
1613 			case OP_BC_ICMP_EQ:
1614 			    Store(inst->dest, Builder.CreateICmpEQ(Op0, Op1));
1615 			    break;
1616 			case OP_BC_ICMP_NE:
1617 			    Store(inst->dest, Builder.CreateICmpNE(Op0, Op1));
1618 			    break;
1619 			case OP_BC_ICMP_UGT:
1620 			    Store(inst->dest, Builder.CreateICmpUGT(Op0, Op1));
1621 			    break;
1622 			case OP_BC_ICMP_UGE:
1623 			    Store(inst->dest, Builder.CreateICmpUGE(Op0, Op1));
1624 			    break;
1625 			case OP_BC_ICMP_ULT:
1626 			    Store(inst->dest, Builder.CreateICmpULT(Op0, Op1));
1627 			    break;
1628 			case OP_BC_ICMP_ULE:
1629 			    Store(inst->dest, Builder.CreateICmpULE(Op0, Op1));
1630 			    break;
1631 			case OP_BC_ICMP_SGT:
1632 			    Store(inst->dest, Builder.CreateICmpSGT(Op0, Op1));
1633 			    break;
1634 			case OP_BC_ICMP_SGE:
1635 			    Store(inst->dest, Builder.CreateICmpSGE(Op0, Op1));
1636 			    break;
1637 			case OP_BC_ICMP_SLT:
1638 			    Store(inst->dest, Builder.CreateICmpSLT(Op0, Op1));
1639 			    break;
1640 			case OP_BC_ICMP_SLE:
1641 			    Store(inst->dest, Builder.CreateICmpSLE(Op0, Op1));
1642 			    break;
1643 			case OP_BC_SELECT:
1644 			    Store(inst->dest, Builder.CreateSelect(Op0, Op1, Op2));
1645 			    break;
1646 			case OP_BC_COPY:
1647 			{
1648 			    Value *Dest = Values[inst->u.binop[1]];
1649 			    constPointerType *PTy = cast<PointerType>(Dest->getType());
1650 			    Op0 = convertOperand(func, PTy->getElementType(), inst->u.binop[0]);
1651 			    PTy = PointerType::getUnqual(Op0->getType());
1652 			    Dest = Builder.CreateBitCast(Dest, PTy);
1653 			    Builder.CreateStore(Op0, Dest);
1654 			    break;
1655 			}
1656 			case OP_BC_CALL_DIRECT:
1657 			{
1658 			    Function *DestF = Functions[inst->u.ops.funcid];
1659 			    SmallVector<Value*, 2> args;
1660 			    args.push_back(&*F->arg_begin()); // pass hidden arg
1661 			    for (unsigned a=0;a<inst->u.ops.numOps;a++) {
1662 				operand_t op = inst->u.ops.ops[a];
1663 				args.push_back(convertOperand(func, DestF->getFunctionType()->getParamType(a+1), op));
1664 			    }
1665 			    CallInst *CI = Builder.CreateCall(DestF, ARRAYREF(Value*, args.begin(), args.end()));
1666 			    CI->setCallingConv(CallingConv::Fast);
1667 #if LLVM_VERSION < 32
1668 			    CI->setDoesNotThrow(true);
1669 #else
1670 			    CI->setDoesNotThrow();
1671 #endif
1672 			    if (CI->getType()->getTypeID() != Type::VoidTyID)
1673 				Store(inst->dest, CI);
1674 			    break;
1675 			}
1676 			case OP_BC_CALL_API:
1677 			{
1678 			    assert(inst->u.ops.funcid < cli_apicall_maxapi && "APICall out of range");
1679 			    std::vector<Value*> args;
1680 			    Function *DestF = apiFuncs[inst->u.ops.funcid];
1681 			    if (!strcmp(cli_apicalls[inst->u.ops.funcid].name, "engine_functionality_level")) {
1682 				Store(inst->dest,
1683 				      ConstantInt::get(Type::getInt32Ty(Context),
1684 						       cl_retflevel()));
1685 			    } else {
1686 			    args.push_back(&*F->arg_begin()); // pass hidden arg
1687 			    for (unsigned a=0;a<inst->u.ops.numOps;a++) {
1688 				operand_t op = inst->u.ops.ops[a];
1689 				args.push_back(convertOperand(func, DestF->getFunctionType()->getParamType(a+1), op));
1690 			    }
1691 			    CallInst *CI = Builder.CreateCall(DestF, ARRAYREFVECTOR(Value*, args));
1692 #if LLVM_VERSION < 32
1693 			    CI->setDoesNotThrow(true);
1694 #else
1695 			    CI->setDoesNotThrow();
1696 #endif
1697 			    Store(inst->dest, CI);
1698 			    }
1699 			    break;
1700 			}
1701 			case OP_BC_GEP1:
1702 			{
1703 			    constType *SrcTy = mapType(inst->u.three[0]);
1704 			    Value *V = convertOperand(func, SrcTy, inst->u.three[1]);
1705 			    Value *Op = convertOperand(func, I32Ty, inst->u.three[2]);
1706 			    Op = GEPOperand(Op);
1707 			    if (!createGEP(inst->dest, V, ARRAYREF(Value*, &Op, &Op+1))) {
1708 				cli_warnmsg("[Bytecode JIT]: OP_BC_GEP1 createGEP failed\n");
1709 				broken = true;
1710 			    }
1711 			    break;
1712 			}
1713 			case OP_BC_GEPZ:
1714 			{
1715 			    Value *Ops[2];
1716 			    Ops[0] = ConstantInt::get(Type::getInt32Ty(Context), 0);
1717 			    constType *SrcTy = mapType(inst->u.three[0]);
1718 			    Value *V = convertOperand(func, SrcTy, inst->u.three[1]);
1719 			    Ops[1] = convertOperand(func, I32Ty, inst->u.three[2]);
1720 			    Ops[1] = GEPOperand(Ops[1]);
1721 			    if (!createGEP(inst->dest, V, ARRAYREF(Value*, Ops, Ops+2))) {
1722 				cli_warnmsg("[Bytecode JIT]: OP_BC_GEPZ createGEP failed\n");
1723 				broken = true;
1724 			    }
1725 			    break;
1726 			}
1727 			case OP_BC_GEPN:
1728 			{
1729 			    std::vector<Value*> Idxs;
1730 			    assert(inst->u.ops.numOps > 2);
1731 			    constType *SrcTy = mapType(inst->u.ops.ops[0]);
1732 			    Value *V = convertOperand(func, SrcTy, inst->u.ops.ops[1]);
1733 			    for (unsigned a=2;a<inst->u.ops.numOps;a++) {
1734 				Value *Op = convertOperand(func, I32Ty, inst->u.ops.ops[a]);
1735 				Op = GEPOperand(Op);
1736 				Idxs.push_back(Op);
1737 			    }
1738 			    if (!createGEP(inst->dest, V, ARRAYREFVECTOR(Value*, Idxs))) {
1739 				cli_warnmsg("[Bytecode JIT]: OP_BC_GEPN createGEP failed\n");
1740 				broken = true;
1741 			    }
1742 			    break;
1743 			}
1744 			case OP_BC_STORE:
1745 			{
1746 			    Value *Dest = convertOperand(func, inst, inst->u.binop[1]);
1747 			    Value *V = convertOperand(func, inst, inst->u.binop[0]);
1748 			    constType *VPTy = PointerType::getUnqual(V->getType());
1749 			    if (VPTy != Dest->getType())
1750 				Dest = Builder.CreateBitCast(Dest, VPTy);
1751 			    Builder.CreateStore(V, Dest);
1752 			    break;
1753 			}
1754 			case OP_BC_LOAD:
1755 			{
1756 			    Op0 = Builder.CreateBitCast(Op0,
1757 							Values[inst->dest]->getType());
1758 			    Op0 = Builder.CreateLoad(Op0);
1759 			    Store(inst->dest, Op0);
1760 			    break;
1761 			}
1762 			case OP_BC_MEMSET:
1763 			{
1764 			    Value *Dst = convertOperand(func, inst, inst->u.three[0]);
1765 			    Dst = Builder.CreatePointerCast(Dst, PointerType::getUnqual(Type::getInt8Ty(Context)));
1766 			    Value *Val = convertOperand(func, Type::getInt8Ty(Context), inst->u.three[1]);
1767 			    Value *Len = convertOperand(func, Type::getInt32Ty(Context), inst->u.three[2]);
1768 #if LLVM_VERSION < 29
1769 			    CallInst *c = Builder.CreateCall4(CF->FMemset, Dst, Val, Len,
1770 								ConstantInt::get(Type::getInt32Ty(Context), 1));
1771 #else
1772 			    CallInst *c = Builder.CreateCall5(CF->FMemset, Dst, Val, Len,
1773 								ConstantInt::get(Type::getInt32Ty(Context), 1),
1774 								ConstantInt::get(Type::getInt1Ty(Context), 0)
1775 								);
1776 #endif
1777 			    c->setTailCall(true);
1778 			    c->setDoesNotThrow();
1779 			    UpgradeCall(c, CF->FMemset);
1780 			    break;
1781 			}
1782 			case OP_BC_MEMCPY:
1783 			{
1784 			    Value *Dst = convertOperand(func, inst, inst->u.three[0]);
1785 			    Dst = Builder.CreatePointerCast(Dst, PointerType::getUnqual(Type::getInt8Ty(Context)));
1786 			    Value *Src = convertOperand(func, inst, inst->u.three[1]);
1787 			    Src = Builder.CreatePointerCast(Src, PointerType::getUnqual(Type::getInt8Ty(Context)));
1788 			    Value *Len = convertOperand(func, Type::getInt32Ty(Context), inst->u.three[2]);
1789 #if LLVM_VERSION < 29
1790 			    CallInst *c = Builder.CreateCall4(CF->FMemcpy, Dst, Src, Len,
1791 								ConstantInt::get(Type::getInt32Ty(Context), 1));
1792 #else
1793 			    CallInst *c = Builder.CreateCall5(CF->FMemcpy, Dst, Src, Len,
1794 								ConstantInt::get(Type::getInt32Ty(Context), 1),
1795 								ConstantInt::get(Type::getInt1Ty(Context), 0)
1796 								);
1797 #endif
1798 			    c->setTailCall(true);
1799 			    c->setDoesNotThrow();
1800 			    UpgradeCall(c, CF->FMemcpy);
1801 			    break;
1802 			}
1803 			case OP_BC_MEMMOVE:
1804 			{
1805 			    Value *Dst = convertOperand(func, inst, inst->u.three[0]);
1806 			    Dst = Builder.CreatePointerCast(Dst, PointerType::getUnqual(Type::getInt8Ty(Context)));
1807 			    Value *Src = convertOperand(func, inst, inst->u.three[1]);
1808 			    Src = Builder.CreatePointerCast(Src, PointerType::getUnqual(Type::getInt8Ty(Context)));
1809 			    Value *Len = convertOperand(func, Type::getInt32Ty(Context), inst->u.three[2]);
1810 #if LLVM_VERSION < 29
1811 			    CallInst *c = Builder.CreateCall4(CF->FMemmove, Dst, Src, Len,
1812 								ConstantInt::get(Type::getInt32Ty(Context), 1));
1813 #else
1814 			    CallInst *c = Builder.CreateCall5(CF->FMemmove, Dst, Src, Len,
1815 								ConstantInt::get(Type::getInt32Ty(Context), 1),
1816 								ConstantInt::get(Type::getInt1Ty(Context), 0));
1817 #endif
1818 			    c->setTailCall(true);
1819 			    c->setDoesNotThrow();
1820 			    UpgradeCall(c, CF->FMemmove);
1821 			    break;
1822 			}
1823 			case OP_BC_MEMCMP:
1824 			{
1825 			    Value *Dst = convertOperand(func, inst, inst->u.three[0]);
1826 			    Dst = Builder.CreatePointerCast(Dst, PointerType::getUnqual(Type::getInt8Ty(Context)));
1827 			    Value *Src = convertOperand(func, inst, inst->u.three[1]);
1828 			    Src = Builder.CreatePointerCast(Src, PointerType::getUnqual(Type::getInt8Ty(Context)));
1829 #if LLVM_VERSION < 32
1830 			    Value *Len = convertOperand(func, EE->getTargetData()->getIntPtrType(Context), inst->u.three[2]);
1831 #else
1832 			    Value *Len = convertOperand(func, EE->getDataLayout()->getIntPtrType(Context), inst->u.three[2]);
1833 #endif
1834 			    CallInst *c = Builder.CreateCall3(CF->FRealmemcmp, Dst, Src, Len);
1835 			    c->setTailCall(true);
1836 			    c->setDoesNotThrow();
1837 			    Store(inst->dest, c);
1838 			    break;
1839 			}
1840 			case OP_BC_ISBIGENDIAN:
1841 			    Store(inst->dest, WORDS_BIGENDIAN ?
1842 				  ConstantInt::getTrue(Context) :
1843 				  ConstantInt::getFalse(Context));
1844 			    break;
1845 			case OP_BC_ABORT:
1846 			    if (!unreachable) {
1847 				CallInst *CI = Builder.CreateCall(CF->FHandler);
1848 				CI->setDoesNotReturn();
1849 				CI->setDoesNotThrow();
1850 				Builder.CreateUnreachable();
1851 				unreachable = true;
1852 			    }
1853 			    break;
1854 			case OP_BC_BSWAP16:
1855 			    {
1856 				CallInst *C = Builder.CreateCall(CF->FBSwap16, convertOperand(func, inst, inst->u.unaryop));
1857 				C->setTailCall(true);
1858 #if LLVM_VERSION < 32
1859 				C->setDoesNotThrow(true);
1860 #else
1861 				C->setDoesNotThrow();
1862 #endif
1863 				Store(inst->dest, C);
1864 				break;
1865 			    }
1866 			case OP_BC_BSWAP32:
1867 			    {
1868 				CallInst *C = Builder.CreateCall(CF->FBSwap32, convertOperand(func, inst, inst->u.unaryop));
1869 				C->setTailCall(true);
1870 #if LLVM_VERSION < 32
1871 				C->setDoesNotThrow(true);
1872 #else
1873 				C->setDoesNotThrow();
1874 #endif
1875 				Store(inst->dest, C);
1876 				break;
1877 			    }
1878 			case OP_BC_BSWAP64:
1879 			    {
1880 				CallInst *C = Builder.CreateCall(CF->FBSwap64, convertOperand(func, inst, inst->u.unaryop));
1881 				C->setTailCall(true);
1882 #if LLVM_VERSION < 32
1883 				C->setDoesNotThrow(true);
1884 #else
1885 				C->setDoesNotThrow();
1886 #endif
1887 				Store(inst->dest, C);
1888 				break;
1889 			    }
1890 			case OP_BC_PTRDIFF32:
1891 			    {
1892 				Value *P1 = convertOperand(func, inst, inst->u.binop[0]);
1893 				Value *P2 = convertOperand(func, inst, inst->u.binop[1]);
1894 				P1 = Builder.CreatePtrToInt(P1, Type::getInt64Ty(Context));
1895 				P2 = Builder.CreatePtrToInt(P2, Type::getInt64Ty(Context));
1896 				Value *R = Builder.CreateSub(P1, P2);
1897 				R = Builder.CreateTrunc(R, Type::getInt32Ty(Context));
1898 				Store(inst->dest, R);
1899 				break;
1900 			    }
1901 			case OP_BC_PTRTOINT64:
1902 			    {
1903 				Value *P1 = convertOperand(func, inst, inst->u.unaryop);
1904 				P1 = Builder.CreatePtrToInt(P1, Type::getInt64Ty(Context));
1905 				Store(inst->dest, P1);
1906 				break;
1907 			    }
1908 			default:
1909 			    cli_warnmsg("[Bytecode JIT]: JIT doesn't implement opcode %d yet!\n",
1910 					inst->opcode);
1911 			    broken = true;
1912 			    break;
1913 		    }
1914 		}
1915 	    }
1916 
1917 	    // If successful so far, run verifyFunction
1918 	    if (!broken) {
1919 #if LLVM_VERSION < 35
1920 		if (verifyFunction(*F, PrintMessageAction)) {
1921 #else
1922 		if (verifyFunction(*F, &errs())) {
1923 #endif
1924 		    // verification failed
1925 		    broken = true;
1926 		    cli_warnmsg("[Bytecode JIT]: Verification failed\n");
1927 		    if (cli_debug_flag) {
1928 			std::string str;
1929 			raw_string_ostream ostr(str);
1930 			F->print(ostr);
1931 			cli_dbgmsg_internal("[Bytecode JIT]: %s\n", ostr.str().c_str());
1932 		    }
1933 		}
1934 	    }
1935 
1936 	    delete [] Values;
1937 
1938 	    // Cleanup after failure and return 0
1939 	    if (broken) {
1940 		for (unsigned z=0; z < func->numBB ; z++) {
1941 		    delete BB[z];
1942 		}
1943 		delete [] BB;
1944 		apiMap.irgenTimer.stopTimer();
1945 		delete TypeMap;
1946 		for (unsigned z=0; z < bc->num_func; z++) {
1947 		    delete Functions[z];
1948 		}
1949 		delete [] Functions;
1950 		return 0;
1951 	    }
1952 
1953 	    delete [] BB;
1954 	    apiMap.irgenTimer.stopTimer();
1955 	    apiMap.pmTimer.startTimer();
1956 	    if (bc->trusted) {
1957 		PM.doInitialization();
1958 		PM.run(*F);
1959 		PM.doFinalization();
1960 	    }
1961 	    else {
1962 		PMUnsigned.doInitialization();
1963 		PMUnsigned.run(*F);
1964 		PMUnsigned.doFinalization();
1965 	    }
1966 	    apiMap.pmTimer.stopTimer();
1967 	    apiMap.irgenTimer.startTimer();
1968 	}
1969 
1970 	for (unsigned j=0;j<bc->num_func;j++) {
1971 	    Function *F = Functions[j];
1972 	    AddStackProtect(F);
1973 	}
1974 	delete TypeMap;
1975 	std::vector<constType*> args;
1976 	args.clear();
1977 	args.push_back(HiddenCtx);
1978 	FunctionType *Callable = FunctionType::get(Type::getInt32Ty(Context),
1979 						   args, false);
1980 
1981 	// If prototype matches, add to callable functions
1982 	if (Functions[0]->getFunctionType() != Callable) {
1983 	    cli_warnmsg("[Bytecode JIT]: Wrong prototype for function 0 in bytecode %d\n",  bc->id);
1984 	    apiMap.irgenTimer.stopTimer();
1985 	    for (unsigned z=0; z < bc->num_func; z++) {
1986 		delete Functions[z];
1987 	    }
1988 	    delete [] Functions;
1989 	    return 0;
1990 	}
1991 	// All functions have the Fast calling convention, however
1992 	// entrypoint can only be C, emit wrapper
1993 	Function *F = Function::Create(Functions[0]->getFunctionType(),
1994 				       Function::ExternalLinkage,
1995 #if LLVM_VERSION < 33
1996 				       Functions[0]->getName()+"_wrap", M);
1997 #else
1998 				       Functions[0]->getName().str()+"_wrap", M);
1999 #endif
2000 	F->setDoesNotThrow();
2001 	BasicBlock *BB = BasicBlock::Create(Context, "", F);
2002 	std::vector<Value*> Args;
2003 	for (Function::arg_iterator J=F->arg_begin(),
2004 	     JE=F->arg_end(); J != JE; ++JE) {
2005 	    Args.push_back(&*J);
2006 	}
2007 	CallInst *CI = CallInst::Create(Functions[0], ARRAYREFVECTOR(Value*, Args), "", BB);
2008 	CI->setCallingConv(CallingConv::Fast);
2009 	ReturnInst::Create(Context, CI, BB);
2010 
2011 	delete [] Functions;
2012 #if LLVM_VERSION < 35
2013 	if (verifyFunction(*F, PrintMessageAction))
2014 #else
2015 	if (verifyFunction(*F, &errs()))
2016 #endif
2017 	    return 0;
2018 
2019 /*			DEBUG(errs() << "Generating code\n");
2020 			// Codegen current function as executable machine code.
2021 			EE->getPointerToFunction(Functions[j]);
2022 			void *code = EE->getPointerToFunction(F);
2023 			DEBUG(errs() << "Code generation finished\n");*/
2024 
2025 //		compiledFunctions[func] = code;
2026 	apiMap.irgenTimer.stopTimer();
2027 	return F;
2028     }
2029 };
2030 
2031 static sys::Mutex llvm_api_lock;
2032 
2033 // This class automatically acquires the lock when instantiated,
2034 // and releases the lock when leaving scope.
2035 class LLVMApiScopedLock {
2036     public:
2037 	// when multithreaded mode is false (no atomics available),
2038 	// we need to wrap all LLVM API calls with a giant mutex lock, but
2039 	// only then.
2040 	LLVMApiScopedLock() {
2041 	    // It is safer to just run all codegen under the mutex,
2042 	    // it is not like we are going to codegen from multiple threads
2043 	    // at a time anyway.
2044 //	    if (!llvm_is_multithreaded())
2045 #if LLVM_VERSION < 36
2046 		llvm_api_lock.acquire();
2047 #else
2048 		llvm_api_lock.lock();
2049 #endif
2050 	}
2051 	~LLVMApiScopedLock() {
2052 //	    if (!llvm_is_multithreaded())
2053 #if LLVM_VERSION < 36
2054 		llvm_api_lock.release();
2055 #else
2056 		llvm_api_lock.unlock();
2057 #endif
2058 	}
2059 };
2060 
2061 static void addFunctionProtos(struct CommonFunctions *CF, ExecutionEngine *EE, Module *M)
2062 {
2063     LLVMContext &Context = M->getContext();
2064     FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context),
2065 					  false);
2066     CF->FHandler = Function::Create(FTy, Function::ExternalLinkage,
2067 					  "clamjit.fail", M);
2068     CF->FHandler->setDoesNotReturn();
2069     CF->FHandler->setDoesNotThrow();
2070 #if LLVM_VERSION == 32
2071     CF->FHandler->addFnAttr(Attributes::NoInline);
2072 #else
2073     CF->FHandler->addFnAttr(Attribute::NoInline);
2074 #endif
2075 #if LLVM_VERSION < 36
2076     // addGlobalMapping still exists in LLVM 3.6, but it doesn't work with MCJIT, which now replaces the JIT implementation.
2077     EE->addGlobalMapping(CF->FHandler, (void*)(intptr_t)jit_exception_handler);
2078 #else
2079     sys::DynamicLibrary::AddSymbol(CF->FHandler->getName(), (void*)(intptr_t)jit_exception_handler);
2080 #endif
2081     EE->InstallLazyFunctionCreator(noUnknownFunctions);
2082     EE->getPointerToFunction(CF->FHandler);
2083 
2084     std::vector<constType*> args;
2085     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
2086     args.push_back(Type::getInt8Ty(Context));
2087     args.push_back(Type::getInt32Ty(Context));
2088     args.push_back(Type::getInt32Ty(Context));
2089 #if LLVM_VERSION >= 29
2090     args.push_back(Type::getInt1Ty(Context));
2091 #endif
2092     FunctionType* FuncTy_3 = FunctionType::get(Type::getVoidTy(Context),
2093 					       args, false);
2094     CF->FMemset = Function::Create(FuncTy_3, GlobalValue::ExternalLinkage,
2095 #if LLVM_VERSION < 29
2096                                    "llvm.memset.i32",
2097 #else
2098                                    "llvm.memset.p0i8.i32",
2099 #endif
2100                                    M);
2101     CF->FMemset->setDoesNotThrow();
2102 #if LLVM_VERSION < 32
2103     CF->FMemset->setDoesNotCapture(1, true);
2104 #else
2105     CF->FMemset->setDoesNotCapture(1);
2106 #endif
2107 
2108     args.clear();
2109     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
2110     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
2111     args.push_back(Type::getInt32Ty(Context));
2112     args.push_back(Type::getInt32Ty(Context));
2113 #if LLVM_VERSION >= 29
2114     args.push_back(Type::getInt1Ty(Context));
2115 #endif
2116     FunctionType* FuncTy_4 = FunctionType::get(Type::getVoidTy(Context),
2117 					       args, false);
2118     CF->FMemmove = Function::Create(FuncTy_4, GlobalValue::ExternalLinkage,
2119 #if LLVM_VERSION < 29
2120                                     "llvm.memmove.i32",
2121 #else
2122                                     "llvm.memmove.p0i8.i32",
2123 #endif
2124                                     M);
2125     CF->FMemmove->setDoesNotThrow();
2126 #if LLVM_VERSION < 32
2127     CF->FMemmove->setDoesNotCapture(1, true);
2128 #else
2129     CF->FMemmove->setDoesNotCapture(1);
2130 #endif
2131 
2132     CF->FMemcpy = Function::Create(FuncTy_4, GlobalValue::ExternalLinkage,
2133 #if LLVM_VERSION < 29
2134                                    "llvm.memcpy.i32",
2135 #else
2136                                    "llvm.memcpy.p0i8.p0i8.i32",
2137 #endif
2138                                    M);
2139     CF->FMemcpy->setDoesNotThrow();
2140 #if LLVM_VERSION < 32
2141     CF->FMemcpy->setDoesNotCapture(1, true);
2142 #else
2143     CF->FMemcpy->setDoesNotCapture(1);
2144 #endif
2145 
2146     args.clear();
2147     args.push_back(Type::getInt16Ty(Context));
2148     FunctionType *FuncTy_5 = FunctionType::get(Type::getInt16Ty(Context), args, false);
2149     CF->FBSwap16 = Function::Create(FuncTy_5, GlobalValue::ExternalLinkage,
2150 					  "llvm.bswap.i16", M);
2151     CF->FBSwap16->setDoesNotThrow();
2152 
2153     args.clear();
2154     args.push_back(Type::getInt32Ty(Context));
2155     FunctionType *FuncTy_6 = FunctionType::get(Type::getInt32Ty(Context), args, false);
2156     CF->FBSwap32 = Function::Create(FuncTy_6, GlobalValue::ExternalLinkage,
2157 					  "llvm.bswap.i32", M);
2158     CF->FBSwap32->setDoesNotThrow();
2159 
2160     args.clear();
2161     args.push_back(Type::getInt64Ty(Context));
2162     FunctionType *FuncTy_7 = FunctionType::get(Type::getInt64Ty(Context), args, false);
2163     CF->FBSwap64 = Function::Create(FuncTy_7, GlobalValue::ExternalLinkage,
2164 					  "llvm.bswap.i64", M);
2165     CF->FBSwap64->setDoesNotThrow();
2166 
2167     FunctionType* DummyTy = FunctionType::get(Type::getVoidTy(Context), false);
2168     CF->FRealmemset = Function::Create(DummyTy, GlobalValue::ExternalLinkage,
2169 					     "memset", M);
2170 #if LLVM_VERSION < 36
2171     EE->addGlobalMapping(CF->FRealmemset, (void*)(intptr_t)memset);
2172 #else
2173     sys::DynamicLibrary::AddSymbol(CF->FRealmemset->getName(), (void*)(intptr_t)memset);
2174 #endif
2175     EE->getPointerToFunction(CF->FRealmemset);
2176     CF->FRealMemmove = Function::Create(DummyTy, GlobalValue::ExternalLinkage,
2177 					      "memmove", M);
2178 #if LLVM_VERSION < 36
2179     EE->addGlobalMapping(CF->FRealMemmove, (void*)(intptr_t)memmove);
2180 #else
2181     sys::DynamicLibrary::AddSymbol(CF->FRealMemmove->getName(), (void*)(intptr_t)memmove);
2182 #endif
2183     EE->getPointerToFunction(CF->FRealMemmove);
2184     CF->FRealmemcpy = Function::Create(DummyTy, GlobalValue::ExternalLinkage,
2185 					     "memcpy", M);
2186 #if LLVM_VERSION < 36
2187     EE->addGlobalMapping(CF->FRealmemcpy, (void*)(intptr_t)memcpy);
2188 #else
2189     sys::DynamicLibrary::AddSymbol(CF->FRealmemcpy->getName(), (void*)(intptr_t)memcpy);
2190 #endif
2191     EE->getPointerToFunction(CF->FRealmemcpy);
2192 
2193     args.clear();
2194     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
2195     args.push_back(PointerType::getUnqual(Type::getInt8Ty(Context)));
2196 #if LLVM_VERSION < 32
2197     args.push_back(EE->getTargetData()->getIntPtrType(Context));
2198 #else
2199     args.push_back(EE->getDataLayout()->getIntPtrType(Context));
2200 #endif
2201     FuncTy_5 = FunctionType::get(Type::getInt32Ty(Context),
2202 				 args, false);
2203     CF->FRealmemcmp = Function::Create(FuncTy_5, GlobalValue::ExternalLinkage, "memcmp", M);
2204 #if LLVM_VERSION < 36
2205     EE->addGlobalMapping(CF->FRealmemcmp, (void*)(intptr_t)memcmp);
2206 #else
2207     sys::DynamicLibrary::AddSymbol(CF->FRealmemcmp->getName(), (void*)(intptr_t)memcmp);
2208 #endif
2209     EE->getPointerToFunction(CF->FRealmemcmp);
2210 }
2211 
2212 }
2213 #if LLVM_VERSION >= 29
2214 INITIALIZE_PASS_BEGIN(RuntimeLimits, "rl", "Runtime Limits", false, false)
2215 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
2216 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
2217 #if LLVM_VERSION < 35
2218 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
2219 #else
2220 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2221 #endif
2222 INITIALIZE_PASS_END(RuntimeLimits, "rl" ,"Runtime Limits", false, false)
2223 #endif
2224 
2225 static pthread_mutex_t watchdog_mutex = PTHREAD_MUTEX_INITIALIZER;
2226 static pthread_cond_t watchdog_cond = PTHREAD_COND_INITIALIZER;
2227 static pthread_cond_t watchdog_cond2 = PTHREAD_COND_INITIALIZER;
2228 static int watchdog_running = 0;
2229 
2230 struct watchdog_item {
2231     volatile uint8_t* timeout;
2232     struct timespec abstimeout;
2233     struct watchdog_item *next;
2234     int in_use;
2235 };
2236 
2237 static struct watchdog_item* watchdog_head = NULL;
2238 static struct watchdog_item* watchdog_tail = NULL;
2239 
2240 extern "C" const char *cli_strerror(int errnum, char* buf, size_t len);
2241 #define WATCHDOG_IDLE 10
2242 static void *bytecode_watchdog(void *arg)
2243 {
2244     struct timeval tv;
2245     struct timespec out;
2246     int ret;
2247     char err[128];
2248     pthread_mutex_lock(&watchdog_mutex);
2249     if (cli_debug_flag)
2250 	cli_dbgmsg_internal("bytecode watchdog is running\n");
2251     do {
2252 	struct watchdog_item *item;
2253 	gettimeofday(&tv, NULL);
2254 	out.tv_sec = tv.tv_sec + WATCHDOG_IDLE;
2255 	out.tv_nsec = tv.tv_usec*1000;
2256 	/* wait for some work, up to WATCHDOG_IDLE time */
2257 	while (watchdog_head == NULL) {
2258 	    ret = pthread_cond_timedwait(&watchdog_cond, &watchdog_mutex,
2259 					 &out);
2260 	    if (ret == ETIMEDOUT)
2261 		break;
2262 	    if (ret) {
2263 		cli_warnmsg("bytecode_watchdog: cond_timedwait(1) failed: %s\n",
2264 			    cli_strerror(ret, err, sizeof(err)));
2265 		break;
2266 	    }
2267 	}
2268 	if (watchdog_head == NULL)
2269 	    break;
2270 	/* wait till timeout is reached on this item */
2271 	item = watchdog_head;
2272 	while (item == watchdog_head) {
2273 	    item->in_use = 1;
2274 	    ret = pthread_cond_timedwait(&watchdog_cond, &watchdog_mutex,
2275 					 &item->abstimeout);
2276 	    if (ret == ETIMEDOUT)
2277 		break;
2278 	    if (ret) {
2279 		cli_warnmsg("bytecode_watchdog: cond_timedwait(2) failed: %s\n",
2280 			    cli_strerror(ret, err, sizeof(err)));
2281 		break;
2282 	    }
2283 	}
2284 	item->in_use = 0;
2285 	pthread_cond_signal(&watchdog_cond2);
2286 	if (item != watchdog_head)
2287 	    continue;/* got removed meanwhile */
2288 	/* timeout reached, signal it to bytecode */
2289 	*item->timeout = 1;
2290 	cli_warnmsg("[Bytecode JIT]: Bytecode run timed out, timeout flag set\n");
2291 	watchdog_head = item->next;
2292 	if (!watchdog_head)
2293 	    watchdog_tail = NULL;
2294     } while (1);
2295     watchdog_running = 0;
2296     if (cli_debug_flag)
2297 	cli_dbgmsg_internal("bytecode watchdog quiting\n");
2298     pthread_mutex_unlock(&watchdog_mutex);
2299     return NULL;
2300 }
2301 
2302 static void watchdog_disarm(struct watchdog_item *item)
2303 {
2304     struct watchdog_item *q, *p = NULL;
2305     if (!item)
2306 	return;
2307     pthread_mutex_lock(&watchdog_mutex);
2308     for (q=watchdog_head;q && q != item;p = q, q = q->next) {}
2309     if (q == item) {
2310 	if (p)
2311 	    p->next = q->next;
2312 	if (q == watchdog_head)
2313 	    watchdog_head = q->next;
2314 	if (q == watchdog_tail)
2315 	    watchdog_tail = p;
2316     }
2317     /* don't remove the item from the list until the watchdog is sleeping on
2318      * item, or it'll wake up on uninit data */
2319     while (item->in_use) {
2320 	pthread_cond_signal(&watchdog_cond);
2321 	pthread_cond_wait(&watchdog_cond2, &watchdog_mutex);
2322     }
2323     pthread_mutex_unlock(&watchdog_mutex);
2324 }
2325 
2326 static int watchdog_arm(struct watchdog_item *item, int ms, volatile uint8_t *timeout)
2327 {
2328     int rc = 0;
2329     struct timeval tv0;
2330 
2331     *timeout = 0;
2332     item->timeout = timeout;
2333     item->next = NULL;
2334     item->in_use = 0;
2335 
2336     gettimeofday(&tv0, NULL);
2337     tv0.tv_usec += ms * 1000;
2338     item->abstimeout.tv_sec = tv0.tv_sec + tv0.tv_usec/1000000;
2339     item->abstimeout.tv_nsec = (tv0.tv_usec%1000000)*1000;
2340 
2341     pthread_mutex_lock(&watchdog_mutex);
2342     if (!watchdog_running) {
2343 	pthread_t thread;
2344 	pthread_attr_t attr;
2345 	pthread_attr_init(&attr);
2346 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2347 
2348 	if ((rc = pthread_create(&thread, &attr, bytecode_watchdog, NULL))) {
2349 	    char buf[256];
2350 	    cli_errmsg("(watchdog) pthread_create failed: %s\n", cli_strerror(rc, buf, sizeof(buf)));
2351 	}
2352 	if (!rc)
2353 	    watchdog_running = 1;
2354 	pthread_attr_destroy(&attr);
2355     }
2356     if (!rc) {
2357 	if (watchdog_tail)
2358 	    watchdog_tail->next = item;
2359 	watchdog_tail = item;
2360 	if (!watchdog_head)
2361 	    watchdog_head = item;
2362     }
2363     pthread_cond_signal(&watchdog_cond);
2364     pthread_mutex_unlock(&watchdog_mutex);
2365     return rc;
2366 }
2367 
2368 static int bytecode_execute(intptr_t code, struct cli_bc_ctx *ctx)
2369 {
2370     ScopedExceptionHandler handler;
2371     // execute;
2372     HANDLER_TRY(handler) {
2373 	// setup exception handler to longjmp back here
2374 	uint32_t result = ((uint32_t (*)(struct cli_bc_ctx *))(intptr_t)code)(ctx);
2375 	*(uint32_t*)ctx->values = result;
2376 	return 0;
2377     }
2378     HANDLER_END(handler);
2379     cli_warnmsg("[Bytecode JIT]: JITed code intercepted runtime error!\n");
2380     return CL_EBYTECODE;
2381 }
2382 
2383 int cli_vm_execute_jit(const struct cli_all_bc *bcs, struct cli_bc_ctx *ctx,
2384 		       const struct cli_bc_func *func)
2385 {
2386     int ret;
2387     struct timeval tv0, tv1;
2388     struct watchdog_item witem;
2389     // no locks needed here, since LLVM automatically acquires a JIT lock
2390     // if needed.
2391     void *code = bcs->engine->compiledFunctions[func];
2392     if (!code) {
2393 	cli_warnmsg("[Bytecode JIT]: Unable to find compiled function\n");
2394 	if (func->numArgs)
2395 	    cli_warnmsg("[Bytecode JIT] Function has %d arguments, it must have 0 to be called as entrypoint\n",
2396 			func->numArgs);
2397 	return CL_EBYTECODE;
2398     }
2399     if (cli_debug_flag)
2400 	gettimeofday(&tv0, NULL);
2401 
2402     if (ctx->bytecode_timeout) {
2403 	/* only spawn if timeout is set.
2404 	 * we don't set timeout for selfcheck (see bb #2235) */
2405 	if (watchdog_arm(&witem, ctx->bytecode_timeout, &ctx->timeout))
2406 	    return CL_EBYTECODE;
2407     }
2408 
2409     ret = bytecode_execute((intptr_t)code, ctx);
2410 
2411     if (ctx->bytecode_timeout)
2412 	watchdog_disarm(&witem);
2413 
2414     if (cli_debug_flag) {
2415 	long diff;
2416 	gettimeofday(&tv1, NULL);
2417 	tv1.tv_sec -= tv0.tv_sec;
2418 	tv1.tv_usec -= tv0.tv_usec;
2419 	diff = tv1.tv_sec*1000000 + tv1.tv_usec;
2420 	cli_dbgmsg_internal("bytecode finished in %ld us\n", diff);
2421     }
2422     return ctx->timeout ? CL_ETIMEOUT : ret;
2423 }
2424 
2425 static unsigned char name_salt[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
2426 static void setGuard(unsigned char* guardbuf)
2427 {
2428     char salt[48];
2429     memcpy(salt, name_salt, 16);
2430     for(unsigned i = 16; i < 48; i++)
2431 	salt[i] = cli_rndnum(255);
2432 
2433     cl_hash_data((char*)"md5", salt, 48, guardbuf, NULL);
2434 }
2435 #if LLVM_VERSION < 32
2436 static void addFPasses(FunctionPassManager &FPM, bool trusted, const TargetData *TD)
2437 #elif LLVM_VERSION < 35
2438 static void addFPasses(FunctionPassManager &FPM, bool trusted, const DataLayout *TD)
2439 #elif LLVM_VERSION < 36
2440 static void addFPasses(FunctionPassManager &FPM, bool trusted, const Module *M)
2441 #else
2442 static void addFPasses(FunctionPassManager &FPM, bool trusted, Module *M)
2443 #endif
2444 {
2445     // Set up the optimizer pipeline.  Start with registering info about how
2446     // the target lays out data structures.
2447 #if LLVM_VERSION < 32
2448     FPM.add(new TargetData(*TD));
2449 #elif LLVM_VERSION < 35
2450     FPM.add(new DataLayout(*TD));
2451 #elif LLVM_VERSION < 36
2452     FPM.add(new DataLayoutPass(M));
2453 #else
2454     DataLayoutPass *DLP = new DataLayoutPass();
2455     DLP->doInitialization(*M);
2456     FPM.add(DLP);
2457 #endif
2458     // Promote allocas to registers.
2459     FPM.add(createPromoteMemoryToRegisterPass());
2460     FPM.add(new BrSimplifier());
2461     FPM.add(createDeadCodeEliminationPass());
2462 }
2463 
2464 int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
2465 {
2466   if (!bcs->engine)
2467       return CL_EBYTECODE;
2468   ScopedExceptionHandler handler;
2469   LLVMApiScopedLock scopedLock;
2470   // setup exception handler to longjmp back here
2471   HANDLER_TRY(handler) {
2472   // LLVM itself never throws exceptions, but operator new may throw bad_alloc
2473   try {
2474     Module *M = new Module("ClamAV jit module", bcs->engine->Context);
2475     {
2476 	// Create the JIT.
2477 	std::string ErrorMsg;
2478 #if LLVM_VERSION < 36
2479 	EngineBuilder builder(M);
2480 #else
2481 	EngineBuilder builder(std::move(std::unique_ptr<Module>(M)));
2482 #endif
2483 
2484 #if LLVM_VERSION >= 31
2485 	TargetOptions Options;
2486 #ifdef CL_DEBUG
2487 	//disable this for now, it leaks
2488 	Options.JITEmitDebugInfo = false;
2489 //	Options.JITEmitDebugInfo = true;
2490 #else
2491 	Options.JITEmitDebugInfo = false;
2492 #endif
2493 #if LLVM_VERSION < 34
2494 	Options.DwarfExceptionHandling = false;
2495 #else
2496 	// TODO: How to do this now?
2497 #endif
2498 	builder.setTargetOptions(Options);
2499 #endif
2500 
2501 	builder.setErrorStr(&ErrorMsg);
2502 	builder.setEngineKind(EngineKind::JIT);
2503 	builder.setOptLevel(CodeGenOpt::Default);
2504 	ExecutionEngine *EE = bcs->engine->EE = builder.create();
2505 	if (!EE) {
2506 	    if (!ErrorMsg.empty())
2507 		cli_errmsg("[Bytecode JIT]: error creating execution engine: %s\n",
2508 			   ErrorMsg.c_str());
2509 	    else
2510 		cli_errmsg("[Bytecode JIT]: JIT not registered?\n");
2511 	    return CL_EBYTECODE;
2512 	}
2513 	bcs->engine->Listener  = new NotifyListener();
2514 	EE->RegisterJITEventListener(bcs->engine->Listener);
2515 //	EE->RegisterJITEventListener(createOProfileJITEventListener());
2516 	// Due to LLVM PR4816 only X86 supports non-lazy compilation, disable
2517 	// for now.
2518 	EE->DisableLazyCompilation();
2519 #if LLVM_VERSION < 36
2520 	EE->DisableSymbolSearching();
2521 #else
2522 	// This must be enabled for AddSymbol to work.
2523 	EE->DisableSymbolSearching(false);
2524 #endif
2525 
2526 	struct CommonFunctions CF;
2527 	addFunctionProtos(&CF, EE, M);
2528 
2529 	FunctionPassManager OurFPM(M), OurFPMUnsigned(M);
2530 #if LLVM_VERSION < 32
2531 	M->setDataLayout(EE->getTargetData()->getStringRepresentation());
2532 #else
2533 	M->setDataLayout(EE->getDataLayout()->getStringRepresentation());
2534 #endif
2535 #if LLVM_VERSION < 31
2536 	M->setTargetTriple(sys::getHostTriple());
2537 #else
2538 	M->setTargetTriple(sys::getDefaultTargetTriple());
2539 #endif
2540 #if LLVM_VERSION < 32
2541 	addFPasses(OurFPM, true, EE->getTargetData());
2542 	addFPasses(OurFPMUnsigned, false, EE->getTargetData());
2543 #elif LLVM_VERSION < 35
2544 	addFPasses(OurFPM, true, EE->getDataLayout());
2545 	addFPasses(OurFPMUnsigned, false, EE->getDataLayout());
2546 #else
2547 	addFPasses(OurFPM, true, M);
2548 	addFPasses(OurFPMUnsigned, false, M);
2549 #endif
2550 
2551 
2552 	//TODO: create a wrapper that calls pthread_getspecific
2553 	unsigned maxh = cli_globals[0].offset + sizeof(struct cli_bc_hooks);
2554 	constType *HiddenCtx = PointerType::getUnqual(ArrayType::get(Type::getInt8Ty(bcs->engine->Context), maxh));
2555 
2556 	LLVMTypeMapper apiMap(bcs->engine->Context, cli_apicall_types, cli_apicall_maxtypes, HiddenCtx);
2557 	Function **apiFuncs = new Function *[cli_apicall_maxapi];
2558 	for (unsigned i=0;i<cli_apicall_maxapi;i++) {
2559 	    const struct cli_apicall *api = &cli_apicalls[i];
2560 	    constFunctionType *FTy = cast<FunctionType>(apiMap.get(69+api->type, NULL, NULL));
2561 	    Function *F = Function::Create(FTy, Function::ExternalLinkage,
2562 					   api->name, M);
2563 	    void *dest;
2564 	    switch (api->kind) {
2565 		case 0:
2566 		    dest = (void*)(intptr_t)cli_apicalls0[api->idx];
2567 		    break;
2568 		case 1:
2569 		    dest = (void*)(intptr_t)cli_apicalls1[api->idx];
2570 		    break;
2571 		case 2:
2572 		    dest = (void*)(intptr_t)cli_apicalls2[api->idx];
2573 		    break;
2574 		case 3:
2575 		    dest = (void*)(intptr_t)cli_apicalls3[api->idx];
2576 		    break;
2577 		case 4:
2578 		    dest = (void*)(intptr_t)cli_apicalls4[api->idx];
2579 		    break;
2580 		case 5:
2581 		    dest = (void*)(intptr_t)cli_apicalls5[api->idx];
2582 		    break;
2583 		case 6:
2584 		    dest = (void*)(intptr_t)cli_apicalls6[api->idx];
2585 		    break;
2586 		case 7:
2587 		    dest = (void*)(intptr_t)cli_apicalls7[api->idx];
2588 		    break;
2589 		case 8:
2590 		    dest = (void*)(intptr_t)cli_apicalls8[api->idx];
2591 		    break;
2592 		case 9:
2593 		    dest = (void*)(intptr_t)cli_apicalls9[api->idx];
2594 		    break;
2595 		default:
2596 		    llvm_unreachable("invalid api type");
2597 	    }
2598 	    if (!dest) {
2599 		std::string reason((Twine("No mapping for builtin api ")+api->name).str());
2600 		llvm_error_handler(0, reason);
2601 	    }
2602 #if LLVM_VERSION < 36
2603 	    EE->addGlobalMapping(F, dest);
2604 #else
2605     // addGlobalMapping doesn't work with MCJIT, so use symbol searching instead.
2606     sys::DynamicLibrary::AddSymbol(F->getName(), dest);
2607 #endif
2608 	    EE->getPointerToFunction(F);
2609 	    apiFuncs[i] = F;
2610 	}
2611 
2612 	// stack protector
2613 	FunctionType *FTy = FunctionType::get(Type::getVoidTy(M->getContext()),
2614 						    false);
2615 	GlobalVariable *Guard = new GlobalVariable(*M, PointerType::getUnqual(Type::getInt8Ty(M->getContext())),
2616 						    true, GlobalValue::ExternalLinkage, 0, "__stack_chk_guard");
2617 	unsigned plus = 0;
2618 	if (2*sizeof(void*) <= 16 && cli_rndnum(2)==2) {
2619 	    plus = sizeof(void*);
2620 	}
2621 #if LLVM_VERSION < 36
2622 	EE->addGlobalMapping(Guard, (void*)(&bcs->engine->guard.b[plus]));
2623 #else
2624     sys::DynamicLibrary::AddSymbol(Guard->getName(), (void*)(&bcs->engine->guard.b[plus]));
2625 #endif
2626 	setGuard(bcs->engine->guard.b);
2627 	bcs->engine->guard.b[plus+sizeof(void*)-1] = 0x00;
2628 //	printf("%p\n", *(void**)(&bcs->engine->guard.b[plus]));
2629 	Function *SFail = Function::Create(FTy, Function::ExternalLinkage,
2630 					      "__stack_chk_fail", M);
2631 #if LLVM_VERSION < 36
2632 	EE->addGlobalMapping(SFail, (void*)(intptr_t)jit_ssp_handler);
2633 #else
2634     sys::DynamicLibrary::AddSymbol(SFail->getName(), (void*)(intptr_t)jit_ssp_handler);
2635 #endif
2636         EE->getPointerToFunction(SFail);
2637 
2638 	llvm::Function **Functions = new Function*[bcs->count];
2639 	for (unsigned i=0;i<bcs->count;i++) {
2640 	    const struct cli_bc *bc = &bcs->all_bcs[i];
2641 	    if (bc->state == bc_skip || bc->state == bc_interp) {
2642 		Functions[i] = 0;
2643 		continue;
2644 	    }
2645 	    LLVMCodegen Codegen(bc, M, &CF, bcs->engine->compiledFunctions, EE,
2646 				OurFPM, OurFPMUnsigned, apiFuncs, apiMap);
2647 	    Function *F = Codegen.generate();
2648 	    if (!F) {
2649 		cli_errmsg("[Bytecode JIT]: JIT codegen failed\n");
2650 		delete [] apiFuncs;
2651 		for (unsigned z=0; z < i; z++) {
2652 		    delete Functions[z];
2653 		}
2654 		delete [] Functions;
2655 		return CL_EBYTECODE;
2656 	    }
2657 	    Functions[i] = F;
2658 	}
2659 	delete [] apiFuncs;
2660 
2661 	bool has_untrusted = false;
2662 
2663 	for (unsigned i=0;i<bcs->count;i++) {
2664 	    if (!bcs->all_bcs[i].trusted) {
2665 		has_untrusted = true;
2666 		break;
2667 	    }
2668 	}
2669 	PassManager PM;
2670 #if LLVM_VERSION < 32
2671 	PM.add(new TargetData(*EE->getTargetData()));
2672 #elif LLVM_VERSION < 35
2673 	PM.add(new DataLayout(*EE->getDataLayout()));
2674 #elif LLVM_VERSION < 36
2675 	PM.add(new DataLayoutPass(M));
2676 #else
2677     DataLayoutPass *DLP = new DataLayoutPass();
2678     DLP->doInitialization(*M);
2679     PM.add(DLP);
2680 #endif
2681 	// TODO: only run this on the untrusted bytecodes, not all of them...
2682 	if (has_untrusted)
2683 	    PM.add(createClamBCRTChecks());
2684 #if LLVM_VERSION >= 36
2685 	// With LLVM 3.6 (MCJIT) this Pass is required to work around
2686 	// a crash in LLVM caused by the SCCP Pass:
2687 	// Pass 'Sparse Conditional Constant Propagation' is not initialized.
2688 	// Verify if there is a pass dependency cycle.
2689 	// Required Passes:
2690 	//
2691 	// Program received signal SIGSEGV, Segmentation fault.
2692 	PM.add(createGVNPass());
2693 #endif
2694 	PM.add(createSCCPPass());
2695 	PM.add(createCFGSimplificationPass());
2696 	PM.add(createGlobalOptimizerPass());
2697 	PM.add(createConstantMergePass());
2698 
2699 	RuntimeLimits *RL = new RuntimeLimits();
2700 	PM.add(RL);
2701 	TimerWrapper pmTimer2("Transform passes");
2702 	pmTimer2.startTimer();
2703 	PM.run(*M);
2704 	pmTimer2.stopTimer();
2705 	DEBUG(M->dump());
2706 
2707 #if LLVM_VERSION >= 36
2708 	EE->finalizeObject();
2709 #endif
2710 
2711 	{
2712 	    PrettyStackTraceString CrashInfo2("Native machine codegen");
2713 	    TimerWrapper codegenTimer("Native codegen");
2714 	    codegenTimer.startTimer();
2715 	    // compile all functions now, not lazily!
2716 	    for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
2717 		Function *Fn = &*I;
2718 		if (!Fn->isDeclaration()) {
2719 		    EE->getPointerToFunction(Fn);
2720 		}
2721 	    }
2722 	    codegenTimer.stopTimer();
2723 	}
2724 
2725 	for (unsigned i=0;i<bcs->count;i++) {
2726 	    const struct cli_bc_func *func = &bcs->all_bcs[i].funcs[0];
2727 	    if (!Functions[i])
2728 		continue;// not JITed
2729 	    bcs->engine->compiledFunctions[func] = EE->getPointerToFunction(Functions[i]);
2730 	    bcs->all_bcs[i].state = bc_jit;
2731 	}
2732 	delete [] Functions;
2733     }
2734     return CL_SUCCESS;
2735   } catch (std::bad_alloc &badalloc) {
2736       cli_errmsg("[Bytecode JIT]: bad_alloc: %s\n",
2737 		 badalloc.what());
2738       return CL_EMEM;
2739   } catch (...) {
2740       cli_errmsg("[Bytecode JIT]: Unexpected unknown exception occurred\n");
2741       return CL_EBYTECODE;
2742   }
2743   return 0;
2744   } HANDLER_END(handler);
2745   cli_errmsg("[Bytecode JIT] *** FATAL error encountered during bytecode generation\n");
2746   return CL_EBYTECODE;
2747 }
2748 
2749 int bytecode_init(void)
2750 {
2751     // If already initialized return
2752 #if LLVM_VERSION < 35
2753     if (llvm_is_multithreaded()) {
2754 	cli_warnmsg("bytecode_init: already initialized\n");
2755 	return CL_EARG;
2756     }
2757 #else
2758     if (!LLVMIsMultithreaded()) {
2759         cli_warnmsg("bytecode_init: LLVM is compiled without multithreading support\n");
2760     }
2761 #endif
2762 
2763     // LLVM safety assertion prevention fix
2764     // TODO: do we want to do a full shutdown?
2765     remove_fatal_error_handler();
2766     llvm_install_error_handler(llvm_error_handler);
2767 #ifdef CL_DEBUG
2768     sys::PrintStackTraceOnErrorSignal();
2769 #if LLVM_VERSION >= 34
2770     llvm::EnablePrettyStackTrace();
2771 #endif
2772 #else
2773 #if LLVM_VERSION < 34
2774     llvm::DisablePrettyStackTrace = true;
2775 #endif
2776 #endif
2777     atexit(do_shutdown);
2778 
2779 #if LLVM_VERSION < 31
2780 #ifdef CL_DEBUG
2781     //disable this for now, it leaks
2782     llvm::JITEmitDebugInfo = false;
2783 //    llvm::JITEmitDebugInfo = true;
2784 #else
2785     llvm::JITEmitDebugInfo = false;
2786 #endif
2787     llvm::DwarfExceptionHandling = false;
2788 #endif
2789 #if LLVM_VERSION < 33
2790     llvm_start_multithreaded();
2791 #else
2792     // This is now deprecated/useless: Multi-threading can only be enabled/disabled with the compile time define LLVM_ENABLE_THREADS in LLVM.
2793 #endif
2794 
2795     // If we have a native target, initialize it to ensure it is linked in and
2796     // usable by the JIT.
2797 #ifndef AC_APPLE_UNIVERSAL_BUILD
2798     InitializeNativeTarget();
2799 #if LLVM_VERSION >= 36
2800     InitializeNativeTargetAsmPrinter();
2801     InitializeNativeTargetAsmParser();
2802 #endif
2803 #else
2804     InitializeAllTargets();
2805 #endif
2806 
2807 #if LLVM_VERSION < 35
2808     if (!llvm_is_multithreaded()) {
2809 #else
2810     if (!LLVMIsMultithreaded()) {
2811 #endif
2812 	//TODO:cli_dbgmsg
2813 	DEBUG(errs() << "WARNING: ClamAV JIT built w/o atomic builtins\n"
2814 	      << "On x86 for best performance ClamAV should be built for i686, not i386!\n");
2815     }
2816     return 0;
2817 }
2818 
2819 // Called once when loading a new set of BC files
2820 int cli_bytecode_init_jit(struct cli_all_bc *bcs, unsigned dconfmask)
2821 {
2822     LLVMApiScopedLock scopedLock;
2823     bcs->engine = new(std::nothrow) cli_bcengine;
2824     if (!bcs->engine)
2825 	return CL_EMEM;
2826     bcs->engine->EE = 0;
2827     bcs->engine->Listener = 0;
2828     return 0;
2829 }
2830 
2831 int cli_bytecode_done_jit(struct cli_all_bc *bcs, int partial)
2832 {
2833     LLVMApiScopedLock scopedLock;
2834     if (bcs->engine) {
2835 	if (bcs->engine->EE) {
2836 	    if (bcs->engine->Listener)
2837 		bcs->engine->EE->UnregisterJITEventListener(bcs->engine->Listener);
2838 	    delete bcs->engine->EE;
2839 	    bcs->engine->EE = 0;
2840 	}
2841 	delete bcs->engine->Listener;
2842 	bcs->engine->Listener = 0;
2843 	if (!partial) {
2844 	    delete bcs->engine;
2845 	    bcs->engine = 0;
2846 	}
2847     }
2848     return 0;
2849 }
2850 
2851 void cli_bytecode_debug(int argc, char **argv)
2852 {
2853   cl::ParseCommandLineOptions(argc, argv);
2854 }
2855 
2856 typedef struct lines {
2857     MemoryBuffer *buffer;
2858     std::vector<const char*> linev;
2859 } linesTy;
2860 
2861 static struct lineprinter {
2862     StringMap<linesTy*> files;
2863 } LinePrinter;
2864 
2865 void cli_bytecode_debug_printsrc(const struct cli_bc_ctx *ctx)
2866 {
2867     if (!ctx->file || !ctx->directory || !ctx->line) {
2868 	errs() << (ctx->directory ? "d":"null") << ":" << (ctx->file ? "f" : "null")<< ":" << ctx->line << "\n";
2869 	return;
2870     }
2871     // acquire a mutex here
2872     sys::Mutex mtx(false);
2873     sys::SmartScopedLock<false> lock(mtx);
2874 
2875     std::string path = std::string(ctx->directory) + "/" + std::string(ctx->file);
2876     StringMap<linesTy*>::iterator I = LinePrinter.files.find(path);
2877     linesTy *lines;
2878     if (I == LinePrinter.files.end()) {
2879 	lines = new linesTy;
2880 	std::string ErrorMessage;
2881 #if LLVM_VERSION < 29
2882 	lines->buffer = MemoryBuffer::getFile(path, &ErrorMessage);
2883 #elif LLVM_VERSION < 35
2884 	OwningPtr<MemoryBuffer> File;
2885 	error_code ec = MemoryBuffer::getFile(path, File);
2886 	if (ec) {
2887 	    ErrorMessage = ec.message();
2888 	    lines->buffer = 0;
2889 	} else
2890 	    lines->buffer = File.take();
2891 #else
2892 	ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = MemoryBuffer::getFile(path);
2893 	if (!FileOrErr) {
2894 		// TODO: How to handle ErrorMessage?
2895 		lines->buffer = 0;
2896 	}
2897 	else {
2898 		lines->buffer = FileOrErr.get().release();
2899 	}
2900 #endif
2901 	if (!lines->buffer) {
2902 	    errs() << "Unable to open file '" << path << "'\n";
2903 	    delete lines;
2904 	    return ;
2905 	}
2906 	LinePrinter.files[path] = lines;
2907     } else {
2908 	lines = I->getValue();
2909     }
2910     while (lines->linev.size() <= ctx->line+1) {
2911 	const char *p;
2912 	if (lines->linev.empty()) {
2913 	    p = lines->buffer->getBufferStart();
2914 	    lines->linev.push_back(p);
2915 	} else {
2916 	    p = lines->linev.back();
2917 	    if (p == lines->buffer->getBufferEnd())
2918 		break;
2919 	    p = strchr(p, '\n');
2920 	    if (!p) {
2921 		p = lines->buffer->getBufferEnd();
2922 		lines->linev.push_back(p);
2923 	    } else
2924 		lines->linev.push_back(p+1);
2925 	}
2926     }
2927     if (ctx->line >= lines->linev.size()) {
2928 	errs() << "Line number " << ctx->line << "out of file\n";
2929 	return;
2930     }
2931     assert(ctx->line < lines->linev.size());
2932 
2933 #if LLVM_VERSION < 28
2934     int line = (int)ctx->line ? (int)ctx->line : -1;
2935     int col = (int)ctx->col ? (int)ctx->col : -1;
2936     //TODO: print this ourselves, instead of using SMDiagnostic
2937     SMDiagnostic diag(ctx->file, line, col,
2938 		 "", std::string(lines->linev[ctx->line-1], lines->linev[ctx->line]-1));
2939     diag.Print("[trace]", errs());
2940 #endif
2941 }
2942 
2943 int have_clamjit=1;
2944 void cli_bytecode_printversion()
2945 {
2946   cl::PrintVersionMessage();
2947 }
2948 
2949 void cli_printcxxver()
2950 {
2951     /* Try to print information about some commonly used compilers */
2952 #ifdef __GNUC__
2953     printf("GNU C++: %s (%u.%u.%u)\n", __VERSION__, __GNUC__, __GNUC_MINOR__,
2954 	   __GNUC_PATCHLEVEL__);
2955 #endif
2956 #ifdef __INTEL_COMPILER
2957     printf("Intel Compiler C++ %u\n", __INTEL_COMPILER);
2958 #endif
2959 #ifdef _MSC_VER
2960     printf("Microsoft Visual C++ %u\n", _MSC_VER);
2961 #endif
2962 }
2963 
2964 namespace ClamBCModule {
2965 void stop(const char *msg, llvm::Function* F, llvm::Instruction* I)
2966 {
2967     if (F && F->hasName()) {
2968 #if LLVM_VERSION < 31
2969 	cli_warnmsg("[Bytecode JIT] in function %s: %s", F->getNameStr().c_str(), msg);
2970 #else
2971 	cli_warnmsg("[Bytecode JIT] in function %s: %s", F->getName().str().c_str(), msg);
2972 #endif
2973     } else {
2974 	cli_warnmsg("[Bytecode JIT] %s", msg);
2975     }
2976 }
2977 }
2978 
2979 #if LLVM_VERSION >= 29
2980 #if LLVM_VERSION < 36
2981 static Value *findDbgGlobalDeclare(GlobalVariable *V) {
2982 #else
2983 static Metadata *findDbgGlobalDeclare(GlobalVariable *V) {
2984 #endif
2985   const Module *M = V->getParent();
2986   NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
2987   if (!NMD)
2988     return 0;
2989 
2990   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
2991     DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
2992     if (!DIG.isGlobalVariable())
2993       continue;
2994     if (DIGlobalVariable(DIG).getGlobal() == V)
2995       return DIG;
2996   }
2997   return 0;
2998 }
2999 
3000 /// Find the debug info descriptor corresponding to this function.
3001 #if LLVM_VERSION < 36
3002 static Value *findDbgSubprogramDeclare(Function *V) {
3003 #else
3004 static Metadata *findDbgSubprogramDeclare(Function *V) {
3005 #endif
3006   const Module *M = V->getParent();
3007   NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp");
3008   if (!NMD)
3009     return 0;
3010 
3011   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
3012     DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
3013     if (!DIG.isSubprogram())
3014       continue;
3015     if (DISubprogram(DIG).getFunction() == V)
3016       return DIG;
3017   }
3018   return 0;
3019 }
3020 
3021 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
3022 /// It looks through pointer casts too.
3023 static const DbgDeclareInst *findDbgDeclare(const Value *V) {
3024   V = V->stripPointerCasts();
3025 
3026   if (!isa<Instruction>(V) && !isa<Argument>(V))
3027     return 0;
3028 
3029   const Function *F = NULL;
3030   if (const Instruction *I = dyn_cast<Instruction>(V))
3031     F = I->getParent()->getParent();
3032   else if (const Argument *A = dyn_cast<Argument>(V))
3033     F = A->getParent();
3034 
3035   for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
3036     for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
3037          BI != BE; ++BI)
3038       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
3039         if (DDI->getAddress() == V)
3040           return DDI;
3041 
3042   return 0;
3043 }
3044 static bool getLocationInfo(const Value *V, std::string &DisplayName,
3045                             std::string &Type, unsigned &LineNo,
3046                             std::string &File, std::string &Dir) {
3047 #if LLVM_VERSION < 33
3048   DICompileUnit Unit;
3049 #else
3050   StringRef G;
3051   StringRef H;
3052 #endif
3053 #if LLVM_VERSION < 35
3054   DIType TypeD;
3055 #endif
3056   StringRef T;
3057 
3058   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
3059 #if LLVM_VERSION < 36
3060     Value *DIGV = findDbgGlobalDeclare(GV);
3061 #else
3062     Metadata *DIGV = findDbgGlobalDeclare(GV);
3063 #endif
3064     if (!DIGV) return false;
3065     DIGlobalVariable Var(cast<MDNode>(DIGV));
3066 
3067     StringRef D = Var.getDisplayName();
3068     if (!D.empty())
3069       DisplayName = D;
3070     LineNo = Var.getLineNumber();
3071 #if LLVM_VERSION < 33
3072     Unit = Var.getCompileUnit();
3073 #else
3074     G = Var.getFilename();
3075     H = Var.getDirectory();
3076 #endif
3077 #if LLVM_VERSION < 35
3078     TypeD = Var.getType();
3079 #else
3080     T = Var.getType().getName();
3081 #endif
3082   } else if (Function *F = dyn_cast<Function>(const_cast<Value*>(V))){
3083 #if LLVM_VERSION < 36
3084     Value *DIF = findDbgSubprogramDeclare(F);
3085 #else
3086     Metadata *DIF = findDbgSubprogramDeclare(F);
3087 #endif
3088     if (!DIF) return false;
3089     DISubprogram Var(cast<MDNode>(DIF));
3090 
3091     StringRef D = Var.getDisplayName();
3092     if (!D.empty())
3093       DisplayName = D;
3094     LineNo = Var.getLineNumber();
3095 #if LLVM_VERSION < 33
3096     Unit = Var.getCompileUnit();
3097 #else
3098     G = Var.getFilename();
3099     H = Var.getDirectory();
3100 #endif
3101 #if LLVM_VERSION < 35
3102     TypeD = Var.getType();
3103 #else
3104     T = Var.getType().getName();
3105 #endif
3106   } else {
3107     const DbgDeclareInst *DDI = findDbgDeclare(V);
3108     if (!DDI) return false;
3109     DIVariable Var(cast<MDNode>(DDI->getVariable()));
3110 
3111     StringRef D = Var.getName();
3112     if (!D.empty())
3113       DisplayName = D;
3114     LineNo = Var.getLineNumber();
3115 #if LLVM_VERSION < 33
3116     Unit = Var.getCompileUnit();
3117 #else
3118     // getFilename and getDirectory are not defined
3119     G = StringRef();
3120     H = StringRef();
3121 #endif
3122 #if LLVM_VERSION < 35
3123     TypeD = Var.getType();
3124 #else
3125     T = Var.getType().getName();
3126 #endif
3127   }
3128 
3129 #if LLVM_VERSION < 35
3130   T = TypeD.getName();
3131 #endif
3132   if (!T.empty())
3133     Type = T;
3134 #if LLVM_VERSION < 33
3135   StringRef G = Unit.getFilename();
3136   StringRef H = Unit.getDirectory();
3137 #endif
3138   if (!G.empty())
3139     File = G;
3140   if (!H.empty())
3141     Dir = H;
3142   return true;
3143 }
3144 #endif
3145 
3146 void printValue(llvm::Value *V, bool a, bool b) {
3147     std::string DisplayName;
3148     std::string Type;
3149     unsigned Line;
3150     std::string File;
3151     std::string Dir;
3152     if (!getLocationInfo(V, DisplayName, Type, Line, File, Dir)) {
3153 	errs() << *V << "\n";
3154 	return;
3155     }
3156     errs() << "'" << DisplayName << "' (" << File << ":" << Line << ")";
3157 }
3158 
3159 void printLocation(llvm::Instruction *I, bool a, bool b) {
3160     if (MDNode *N = I->getMetadata("dbg")) {
3161 	DILocation Loc(N);
3162 	errs() << Loc.getFilename() << ":" << Loc.getLineNumber();
3163 	if (unsigned Col = Loc.getColumnNumber()) {
3164   	    errs() << ":" << Col;
3165   	}
3166   	errs() << ": ";
3167   	return;
3168     }
3169     errs() << *I << ":\n";
3170 }
3171