Commit graph

162 commits

Author SHA1 Message Date
Török Edvin
ab402e6aae Speed up bytecode load.
Don't codegen bytecodes that have a functionality_level test in entrypoint
that would prevent them from running.
2011-01-20 16:10:30 +02:00
aCaB
26b86d8d8e add missing EOL 2011-01-20 15:04:51 +01:00
Török Edvin
ce2386732a Fix build with external LLVM28.
It declares those 2 initialize functions as C linkage, so make sure we match it
(in our version they are not declared at all).
2010-11-12 09:53:09 +02:00
Török Edvin
e92dda94ce chkstk: another try. 2010-11-10 16:21:48 +02:00
Török Edvin
3d8ca9f6aa chkstk 2010-11-10 16:19:59 +02:00
Török Edvin
ca3e6c5860 I'm told _chkstk needs to be called as __chkstk on Win64.
Same to the backend (added _), but if I want the linker to find it, I need to do
it like this. Hope it works.
2010-11-10 16:00:39 +02:00
Török Edvin
3a35520bf9 Fix recently introduced ScopedExceptionHandler.
can't use setjmp inside a function that is not the parent.
just write and use some macros.

Otherwise the "else" from handler.Set() was never reached, which caused a unit
test to fail.
2010-11-10 14:53:39 +02:00
Török Edvin
29553b8e26 bytecode2llvm: convert errs() to cli_{err,warn,dbg} so they are not lost. 2010-11-10 14:21:51 +02:00
Török Edvin
4955886036 Fix win32 bytecode: allow _chkstk.
Win32 needs this when stack >4k, this was the message that was logged to stderr
previously, causing the crash.
2010-11-10 14:21:51 +02:00
Török Edvin
f62d53980a bytecode2llvm: make sure we don't return from a NORETURN function. 2010-11-10 14:21:50 +02:00
Török Edvin
162c2e431b bytecode2llvm: don't crash when stderr is unavailable.
Writing to errs() when stderr is not open results in write Error getting flagged
in LLVM's raw_fd_ostream, which results in a call to llvm_report_error.
However since jmp_buf is not valid anymore we end up crashing on longjmp.

Fix:
 - reset the thread-local recovery (jmp_buf*) to NULL when exiting the block
 where it is valid
 - clear errors on errs() on shutdown to avoid the report_fatal_error
2010-11-10 14:21:47 +02:00
Török Edvin
dccafff069 define LLVM28, since we are using that now. 2010-11-06 15:50:31 +02:00
Török Edvin
a7cf187a0c Make cl_load thread safe (bb #2333).
Parallel cl_load() crash (bb #2333).
Reason is twofold:
 - cache.c had 2 'static' global variables, thus trying to initialize same cache
 from multiple threads
  - bytecode2llvm.cpp: something in LLVM 2.7 is crashing when loading in
  parallel

Fix is to drop the 'static' on the variable (cache is per engine already).
This also fixes a potential memory leak in clamd!

The other part of the fix is to turn on the mutex around bytecode compilation
always. We don't call cl_load in parallel, so this doesn't affect clamd, but
some may need to call cl_load in parallel.
2010-11-04 21:53:03 +02:00
Török Edvin
49eb01089a Make sure Functions[i] is initialized, and not used as NULL. 2010-10-22 12:31:53 +03:00
Török Edvin
34306523a1 Fix startup.cbc load (bb #2330).
BC_STARTUP is already prepared with JIT, so Functions[i] is garbage,
don't attempt to codegen it.
2010-10-18 13:45:20 +03:00
Török Edvin
dec93ea86f Fix bytecode and autoitea06 for Apple-style universal builds (bb #2030)
Bytecode needs to enable both PPC and X86, link in all targets,
and autoit needs fpu_endian == cpu_endian for universal builds.
2010-10-18 11:27:06 +03:00
Török Edvin
1755adf042 Fix build with internal LLVM. 2010-09-28 16:43:25 +03:00
Török Edvin
45ba739216 Fix constructors for LLVM 2.8. 2010-09-28 14:45:04 +03:00
Török Edvin
b22af13f7a Merge branch 'llvm2.8'
* llvm2.8:
  Regenerate configure and Makefile.
  LLVM 2.8 changed llvm.mem{cpy,move,set}.* to take 5 arguments.
  LLVM 2.8 API update.
  Don't cast the iterator directly.
  Use CallSite instead of CI->getOperand.
  Support building with external LLVM 2.8.

Conflicts:
	libclamav/c++/Makefile.in
	libclamav/c++/bytecode2llvm.cpp
	libclamav/c++/configure
2010-09-28 14:28:59 +03:00
Török Edvin
744c40d142 LLVM 2.8 changed llvm.mem{cpy,move,set}.* to take 5 arguments.
Use AutoUpgrade to upgrade these calls.
Intrinsic::getDeclaration can't be used because in 2.7 it needs 1 type,
in 2.8 it needs 2/3, and there is no function to query how many it needs.
2010-09-28 14:25:22 +03:00
Török Edvin
e3a5450335 LLVM 2.8 API update.
Some APIs got renamed, provide macro wrappers.
2010-09-28 14:25:22 +03:00
Török Edvin
1300b00244 Don't cast the iterator directly.
LLVM 2.8 doesn't allow this, first dereference, then cast.
This is compatible with 2.7.
2010-09-28 14:25:22 +03:00
Török Edvin
c0a306b291 Fix valgrind warning introduced in clamav-0.96.3-6-g6e52ce6.
It reports some possible lost memory in llvm::sys::Timer, since by default
timer is not used add a wrapper so it only constructs Timer as necessary.
2010-09-28 14:22:41 +03:00
Török Edvin
6e52ce67c7 Improve bytecode load time and optimization (bb #2278).
Avoid quadratic load times: run module passes at the end, and run
rtcheck inserter at the end also.

Also optimize away some simple situations that the compiler couldn't,
like result of __is_bigendian() which is a constant at load time (but not at
compile time).
If we would have run LLVM's -O1 optimizers these would have been
optimized already, but we don't do that for 2 reasons:
 - optimizations may introduce new bugs
 - may take a bit longer time
Just run some simple transforms, and some custom optimizations for the bigendian
case.
2010-09-21 20:24:06 +03:00
Török Edvin
3eef86ee9f Workaround crash due to gcc stack alignment requirements (bb #2270).
GCC (4.4 at least) requires and creates functions with 16-byte stack alignment
on 32-bit Linux too.
If they happen to use SSE instructions they will crash if incoming stack
alignment is not 16-byte.

LLVM claims that 4-byte stack alignment is the standard, and it only aligns to
4-byte, hence the crash. Apparently older versions of GCC/glibc would crash
if it set alignment to 16-byte.

But since the oldest GCC we support is 4.1.2, and disable LLVM on anything older
just set stack alignment to 16-byte for all functions. LLVM will realign stack
as needed. To be safe turn this on only on Linux.

This should really be fixed by either GCC or LLVM, but until that happens (see
LLVM PR8152) lets use this workaround.
2010-09-15 13:19:48 +03:00
Török Edvin
cb1260cc8e Fix __bzero on darwin 10.
LLVM calls __bzero directly which is not allowed by libclamav.
Provide wrapper.
2010-09-10 18:03:11 +03:00
Török Edvin
71ca3536c4 Fix hung clamd on FreeBSD (bb #2235).
bytecode selfcheck running under JIT ran too early, and spawned a thread.
Then clamd forked. threads + fork = bad idea.
So prevent the thread from being spawned in selfcheck mode.
So at the time of fork clamd will still be single threaded as in 0.96.1.
2010-09-02 21:24:20 +03:00
Török Edvin
f1d071c8ed Avoid false 'Verification error' messages (bb #2239).
The fix for bb #1924 already fixed most of these, by using the stackprotectreq
attribute to determine if a function was already verified, not to verify it
twice.
However very simple bytecodes don't get that attribute applied. Fix this by
always applying the stackprotect attribute, and checking for that attribute
instead of stackprotectreq.
2010-09-01 10:04:36 +03:00
Török Edvin
c85060ff79 Move environment detection code to bytecode_detect.c.
This is in preparation for bug #2078, so that the bytecodes can turn themselves off
based on more than just the arch itself.
2010-07-29 13:48:13 +03:00
Török Edvin
8977ed61fe Update paxctl message to 'paxctl -cm' (bb #2092). 2010-07-26 13:19:08 +03:00
Török Edvin
a5a19f4518 bytecode: PaX and selfcheck.
Detect PaX and fallback to intepreter if needed (bb #2092).
Recent PaX versions deny the RWX mapping, but older versions silently change it
to RW, which causes the program to die as soon as it tries to execute JITed
code.

Add selfcheck on startup (bb #2092).
This will run a very simple bytecode on startup in both JIT and interpreter
mode. The bytecode only calls 1 libclamav API and returns.
2010-07-22 22:34:07 +03:00
Török Edvin
fa0a9143bb Fix types for store/copy instructions. 2010-05-13 19:51:27 +03:00
Török Edvin
daad92ace3 Relax bounds checks. It was rejecting correct code.
Bounds checks were too strict, causing the bytecode to abort when it shouldn't.
This happened when trying to access the last byte of an array, the verifier
was too conservative and considered to be out of bounds, when in fact it wasn't.

This is an update of the runtime verifier from the bytecode compiler.
2010-05-13 12:41:59 +03:00
Török Edvin
7a7365efe9 0.96.1 new APIs (cli_map etc.) 2010-05-12 23:51:20 +03:00
Török Edvin
b0a5ddb3a5 Fix memcmp and memmove.
Due to a bug src and dest were always the same, so they were never working in
JIT mode.
2010-05-12 23:51:20 +03:00
Török Edvin
2a7f1cdaf5 Print C++ compiler version.
This requires exporting a function from libclamav, since clamconf is not C++...
2010-04-19 18:35:30 +03:00
Török Edvin
e4a0f2c94f fix compiler warnings (bb #1872, bb #1934, bb #1935) 2010-04-13 16:19:47 +03:00
Török Edvin
f121d43df8 Workaround LLVM JIT PowerPC relocation bug (bb #1921).
Based on Gary Benson's workaround for OpenJDK Shark / IcedTea.
2010-04-02 13:13:23 +03:00
Török Edvin
556eaf0442 Update ClamBCRTChecks.cpp from bytecode compiler:
Check bounds of each pointer passed to/from APIcalls,
forbid recursion.
2010-03-30 23:23:41 +03:00
Török Edvin
b2de4fd85f Minor optimization in JIT loader. 2010-03-30 11:07:05 +03:00
Török Edvin
cbec3f2446 Update SELinux message. 2010-03-30 00:34:49 +03:00
Török Edvin
44e134312c More fixes for pdf.cbc. 2010-03-30 00:04:38 +03:00
Török Edvin
c506c2c555 Fallback to interpreter mode when SELinux denies 'execmem' access. (bb #1901).
This also fixes a crash when run under SELinux: MemoryBlock() needs to
initialize its field to 0!
2010-03-28 13:52:29 +03:00
Török Edvin
11cee1b7de Disable JITDebugRegisterer, and fix unit_tests rule. 2010-03-24 17:22:34 +02:00
Török Edvin
6ea339aeab Fix bswap. 2010-03-24 15:27:15 +02:00
Török Edvin
bdd9aeaeeb Use a watchdog thread. Also make timeout be ms instead of us. 2010-03-23 16:33:41 +02:00
Török Edvin
99536a178c Insert timeout checks directly into the JITed code.
pthread_cancel is broken on Mac OS X (it only works if the thread
you want to kill calls pthread_testcancel, which is never the situation
when you need async cancelation).
Anyway async cancelation is risky, it may leave bc_ctx in an inconsistent state.
So rather than doing using pthread_cancel (or pthread_kill+siglongjmp)
just insert the timeout checks into the JITed code directly.

These are inserted in each loop, if the loop's tripcount is unknown, or
higher than a threshold. They are also inserted after a certain amount
of APIcalls are made (even in absence of loops).
Note that 'loop' in this sense is not LLVM's notion of a natural loop,
it is simply a BB which is reachable both directly and via a backedge.

For example this doesn't contain natural loops but contains backedges (and a
potential infinite loop):
int foo(int a)
{
    int x=4;
    if (a == 42)
        goto head2;
head:
    x++;
head2:
    if (a >= 2) {
        x += 3;
        goto head;
    } else if (a >= 0) {
        x += 9;
        goto head;
    }
    return x;
}
2010-03-23 15:54:41 +02:00
Török Edvin
b63681a52b Introduce BytecodeTimeout. 2010-03-22 17:16:07 +02:00
Török Edvin
884a0b8f8d Support for timeouts. 2010-03-22 16:57:27 +02:00
Török Edvin
84edf09bba Fix clambc -p. 2010-03-22 14:58:51 +02:00