2009-06-26 20:23:52 +03:00
|
|
|
/*
|
|
|
|
* ClamAV bytecode handler tool.
|
|
|
|
*
|
2019-01-25 10:15:50 -05:00
|
|
|
* Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
|
|
|
* Copyright (C) 2009-2013 Sourcefire, Inc.
|
2009-06-26 20:23:52 +03:00
|
|
|
*
|
|
|
|
* Authors: Török Edvin
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
2009-12-11 20:57:41 +02:00
|
|
|
#ifndef _WIN32
|
2009-12-03 11:37:38 +02:00
|
|
|
#include <sys/time.h>
|
2009-12-11 20:57:41 +02:00
|
|
|
#endif
|
2009-12-03 11:37:38 +02:00
|
|
|
#include <stdlib.h>
|
2014-02-08 00:31:12 -05:00
|
|
|
|
2009-06-26 20:23:52 +03:00
|
|
|
#include "bytecode.h"
|
2009-12-08 23:02:49 +02:00
|
|
|
#include "bytecode_priv.h"
|
2009-06-26 20:23:52 +03:00
|
|
|
#include "clamav.h"
|
2009-06-30 18:15:12 +03:00
|
|
|
#include "shared/optparser.h"
|
2009-06-30 19:27:26 +03:00
|
|
|
#include "shared/misc.h"
|
2010-02-15 14:37:09 +02:00
|
|
|
#include "libclamav/dconf.h"
|
2010-05-13 20:02:02 +03:00
|
|
|
#include "libclamav/others.h"
|
2009-06-26 20:23:52 +03:00
|
|
|
|
2009-09-08 22:25:33 +03:00
|
|
|
#include <fcntl.h>
|
2009-06-26 20:23:52 +03:00
|
|
|
#include <stdlib.h>
|
2009-09-08 22:25:33 +03:00
|
|
|
#include <errno.h>
|
2009-10-02 12:27:52 +03:00
|
|
|
#include <string.h>
|
2009-12-11 20:57:41 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2009-10-02 12:27:52 +03:00
|
|
|
#include <unistd.h>
|
2009-12-11 20:57:41 +02:00
|
|
|
#endif
|
2009-06-26 20:23:52 +03:00
|
|
|
|
2009-06-30 18:15:12 +03:00
|
|
|
static void help(void)
|
|
|
|
{
|
|
|
|
printf("\n");
|
2018-02-06 16:23:07 -05:00
|
|
|
printf(" Clam AntiVirus: Bytecode Testing Tool %s\n", get_version());
|
2018-03-27 13:06:46 -04:00
|
|
|
printf(" By The ClamAV Team: https://www.clamav.net/about.html#credits\n");
|
2019-01-25 10:15:50 -05:00
|
|
|
printf(" (C) 2019 Cisco Systems, Inc.\n");
|
2018-02-06 16:23:07 -05:00
|
|
|
printf("\n");
|
|
|
|
printf(" clambc <file> [function] [param1 ...]\n");
|
|
|
|
printf("\n");
|
|
|
|
printf(" --help -h Show this help\n");
|
2009-06-30 18:15:12 +03:00
|
|
|
printf(" --version -V Show version\n");
|
2014-09-22 17:12:34 -04:00
|
|
|
printf(" --debug Show debug\n");
|
|
|
|
printf(" --force-interpreter -f Force using the interpreter instead of the JIT\n");
|
|
|
|
printf(" --trust-bytecode -t Trust loaded bytecode (default yes)\n");
|
2010-02-13 19:57:33 +02:00
|
|
|
printf(" --info -i Print information about bytecode\n");
|
|
|
|
printf(" --printsrc -p Print bytecode source\n");
|
2014-09-22 17:12:34 -04:00
|
|
|
printf(" --printbcir -c Print IR of bytecode signature\n");
|
2018-02-06 16:23:07 -05:00
|
|
|
printf(" --input -c Input file to run the bytecode on\n");
|
2014-10-02 16:49:32 -04:00
|
|
|
printf(" --trace <level> -T Set bytecode trace level 0..7 (default 7)\n");
|
2014-09-22 17:12:34 -04:00
|
|
|
printf(" --no-trace-showsource -s Don't show source line during tracing\n");
|
2015-02-19 12:47:20 -05:00
|
|
|
printf(" --statistics=bytecode Collect and print bytecode execution statistics\n");
|
2018-02-06 16:23:07 -05:00
|
|
|
printf(" file File to test\n");
|
2009-06-30 18:15:12 +03:00
|
|
|
printf("\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-09 16:50:55 +02:00
|
|
|
static struct dbg_state {
|
|
|
|
const char *directory;
|
|
|
|
const char *file;
|
|
|
|
const char *scope;
|
|
|
|
uint32_t scopeid;
|
|
|
|
unsigned line;
|
|
|
|
unsigned col;
|
|
|
|
unsigned showline;
|
|
|
|
} dbg_state;
|
|
|
|
|
|
|
|
static void tracehook(struct cli_bc_ctx *ctx, unsigned event)
|
|
|
|
{
|
|
|
|
dbg_state.directory = ctx->directory;
|
|
|
|
if (*ctx->file == '?')
|
2018-12-03 12:40:13 -05:00
|
|
|
return;
|
2009-12-09 16:50:55 +02:00
|
|
|
switch (event) {
|
2018-12-03 12:40:13 -05:00
|
|
|
case trace_func:
|
|
|
|
fprintf(stderr, "[trace] %s:%u:%u -> %s:%u:%u Entered function %s\n",
|
|
|
|
dbg_state.file, dbg_state.line, dbg_state.col,
|
|
|
|
ctx->file, ctx->line, ctx->col, ctx->scope);
|
|
|
|
dbg_state.scope = ctx->scope;
|
|
|
|
break;
|
|
|
|
case trace_param:
|
|
|
|
fprintf(stderr, "[trace] function parameter:\n");
|
|
|
|
return;
|
|
|
|
case trace_scope:
|
|
|
|
fprintf(stderr, "[trace] %s:%u:%u -> %s:%u:%u\n",
|
|
|
|
dbg_state.file, dbg_state.line, dbg_state.col,
|
|
|
|
ctx->file, ctx->line, ctx->col);
|
|
|
|
dbg_state.scope = ctx->scope;
|
|
|
|
break;
|
|
|
|
case trace_line:
|
|
|
|
case trace_col:
|
|
|
|
if (dbg_state.showline)
|
|
|
|
cli_bytecode_debug_printsrc(ctx);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "[trace] %s:%u:%u\n",
|
|
|
|
dbg_state.file, dbg_state.line, dbg_state.col);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2009-12-09 16:50:55 +02:00
|
|
|
}
|
|
|
|
dbg_state.file = ctx->file;
|
|
|
|
dbg_state.line = ctx->line;
|
2018-12-03 12:40:13 -05:00
|
|
|
dbg_state.col = ctx->col;
|
2009-12-09 16:50:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tracehook_op(struct cli_bc_ctx *ctx, const char *op)
|
|
|
|
{
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(ctx);
|
2009-12-11 18:09:33 +02:00
|
|
|
fprintf(stderr, "[trace] %s\n", op);
|
2009-12-09 16:50:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tracehook_val(struct cli_bc_ctx *ctx, const char *name, uint32_t value)
|
|
|
|
{
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(ctx);
|
2009-12-11 18:09:33 +02:00
|
|
|
fprintf(stderr, "[trace] %s = %u\n", name, value);
|
2009-12-09 16:50:55 +02:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:40:35 +02:00
|
|
|
static void tracehook_ptr(struct cli_bc_ctx *ctx, const void *ptr)
|
|
|
|
{
|
2014-07-11 09:30:58 -04:00
|
|
|
UNUSEDPARAM(ctx);
|
2009-12-17 17:40:35 +02:00
|
|
|
fprintf(stderr, "[trace] %p\n", ptr);
|
|
|
|
}
|
|
|
|
|
2010-05-14 10:40:04 +03:00
|
|
|
static uint8_t debug_flag = 0;
|
2010-02-06 17:53:17 +02:00
|
|
|
static void print_src(const char *file)
|
|
|
|
{
|
2018-12-03 12:40:13 -05:00
|
|
|
char buf[4096];
|
|
|
|
int nread, i, found = 0, lcnt = 0;
|
|
|
|
FILE *f = fopen(file, "r");
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "Unable to reopen %s\n", file);
|
|
|
|
return;
|
2010-02-06 17:53:17 +02:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
do {
|
|
|
|
nread = fread(buf, 1, sizeof(buf), f);
|
|
|
|
for (i = 0; i < nread - 1; i++) {
|
|
|
|
if (buf[i] == '\n') {
|
|
|
|
lcnt++;
|
|
|
|
}
|
|
|
|
/* skip over the logical trigger */
|
|
|
|
if (lcnt >= 2 && buf[i] == '\n' && buf[i + 1] == 'S') {
|
|
|
|
found = 1;
|
|
|
|
i += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!found && (nread == sizeof(buf)));
|
|
|
|
if (debug_flag)
|
|
|
|
printf("[clambc] Source code:");
|
|
|
|
do {
|
|
|
|
for (; i + 1 < nread; i++) {
|
|
|
|
if (buf[i] == 'S' || buf[i] == '\n') {
|
|
|
|
putc('\n', stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
putc(((buf[i] & 0xf) | ((buf[i + 1] & 0xf) << 4)), stdout);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i == nread - 1 && nread != 1)
|
|
|
|
fseek(f, -1, SEEK_CUR);
|
|
|
|
i = 0;
|
|
|
|
nread = fread(buf, 1, sizeof(buf), f);
|
|
|
|
} while (nread > 0);
|
|
|
|
fclose(f);
|
2010-02-06 17:53:17 +02:00
|
|
|
}
|
2010-03-23 21:47:57 +02:00
|
|
|
static uint32_t deadbeefcounts[64] = {
|
|
|
|
0xdeadbeef,
|
|
|
|
0,
|
|
|
|
0xbeefdead,
|
|
|
|
0,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
0xdeadbeef,
|
|
|
|
};
|
2009-06-26 20:23:52 +03:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
struct cli_bc *bc;
|
|
|
|
struct cli_bc_ctx *ctx;
|
2018-12-03 12:40:13 -05:00
|
|
|
int rc, dbgargc, bc_stats = 0;
|
2009-06-30 18:15:12 +03:00
|
|
|
struct optstruct *opts;
|
2009-09-08 22:25:33 +03:00
|
|
|
const struct optstruct *opt;
|
2018-12-03 12:40:13 -05:00
|
|
|
unsigned funcid = 0, i;
|
2009-08-25 18:54:14 +03:00
|
|
|
struct cli_all_bc bcs;
|
2010-04-13 16:19:47 +03:00
|
|
|
int fd = -1;
|
2009-12-10 15:17:40 +02:00
|
|
|
unsigned tracelevel;
|
2009-06-30 18:15:12 +03:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (check_flevel())
|
|
|
|
exit(1);
|
2010-05-10 17:05:16 +02:00
|
|
|
|
2009-06-30 18:15:12 +03:00
|
|
|
opts = optparse(NULL, argc, argv, 1, OPT_CLAMBC, 0, NULL);
|
|
|
|
if (!opts) {
|
2018-12-03 12:40:13 -05:00
|
|
|
fprintf(stderr, "ERROR: Can't parse command line options\n");
|
|
|
|
exit(1);
|
2009-06-26 20:23:52 +03:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
if (optget(opts, "version")->enabled) {
|
|
|
|
printf("Clam AntiVirus Bytecode Testing Tool %s\n", get_version());
|
|
|
|
cl_init(CL_INIT_DEFAULT);
|
|
|
|
cli_bytecode_printversion();
|
|
|
|
optfree(opts);
|
|
|
|
exit(0);
|
2009-06-30 18:15:12 +03:00
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
if (optget(opts, "help")->enabled || !opts->filename) {
|
|
|
|
optfree(opts);
|
|
|
|
help();
|
|
|
|
exit(0);
|
2009-06-30 18:15:12 +03:00
|
|
|
}
|
2009-07-07 23:36:36 +03:00
|
|
|
f = fopen(opts->filename[0], "r");
|
2009-06-26 20:23:52 +03:00
|
|
|
if (!f) {
|
2018-12-03 12:40:13 -05:00
|
|
|
fprintf(stderr, "Unable to load %s\n", argv[1]);
|
|
|
|
optfree(opts);
|
|
|
|
exit(2);
|
2009-06-26 20:23:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bc = malloc(sizeof(*bc));
|
|
|
|
if (!bc) {
|
2018-12-03 12:40:13 -05:00
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
optfree(opts);
|
|
|
|
exit(3);
|
2009-06-26 20:23:52 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (optget(opts, "debug")->enabled) {
|
|
|
|
cl_debug();
|
|
|
|
debug_flag = 1;
|
2010-02-13 19:57:33 +02:00
|
|
|
}
|
2009-08-25 18:54:14 +03:00
|
|
|
rc = cl_init(CL_INIT_DEFAULT);
|
|
|
|
if (rc != CL_SUCCESS) {
|
2018-12-03 12:40:13 -05:00
|
|
|
fprintf(stderr, "Unable to init libclamav: %s\n", cl_strerror(rc));
|
|
|
|
optfree(opts);
|
|
|
|
exit(4);
|
2009-08-25 18:54:14 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
dbgargc = 1;
|
2009-08-27 18:12:39 +03:00
|
|
|
while (opts->filename[dbgargc]) dbgargc++;
|
|
|
|
|
|
|
|
if (dbgargc > 1)
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_bytecode_debug(dbgargc, opts->filename);
|
2009-09-04 12:09:17 +03:00
|
|
|
|
|
|
|
if (optget(opts, "force-interpreter")->enabled) {
|
2018-12-03 12:40:13 -05:00
|
|
|
bcs.engine = NULL;
|
2009-09-04 12:09:17 +03:00
|
|
|
} else {
|
2018-12-03 12:40:13 -05:00
|
|
|
rc = cli_bytecode_init(&bcs);
|
|
|
|
if (rc != CL_SUCCESS) {
|
|
|
|
fprintf(stderr, "Unable to init bytecode engine: %s\n", cl_strerror(rc));
|
|
|
|
optfree(opts);
|
|
|
|
exit(4);
|
|
|
|
}
|
2009-08-25 18:54:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bcs.all_bcs = bc;
|
2018-12-03 12:40:13 -05:00
|
|
|
bcs.count = 1;
|
2009-08-25 18:54:14 +03:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if ((opt = optget(opts, "statistics"))->enabled) {
|
|
|
|
while (opt) {
|
|
|
|
if (!strcasecmp(opt->strarg, "bytecode"))
|
|
|
|
bc_stats = 1;
|
|
|
|
opt = opt->nextarg;
|
2015-02-19 08:03:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = cli_bytecode_load(bc, f, NULL, optget(opts, "trust-bytecode")->enabled, bc_stats);
|
2009-06-26 20:23:52 +03:00
|
|
|
if (rc != CL_SUCCESS) {
|
2018-12-03 12:40:13 -05:00
|
|
|
fprintf(stderr, "Unable to load bytecode: %s\n", cl_strerror(rc));
|
|
|
|
optfree(opts);
|
|
|
|
exit(4);
|
2009-06-26 20:23:52 +03:00
|
|
|
}
|
|
|
|
fclose(f);
|
2010-04-26 18:19:28 +03:00
|
|
|
if (bc->state == bc_skip) {
|
2018-12-03 12:40:13 -05:00
|
|
|
fprintf(stderr, "bytecode load skipped\n");
|
|
|
|
exit(0);
|
2010-04-26 18:19:28 +03:00
|
|
|
}
|
2010-05-14 10:40:04 +03:00
|
|
|
if (debug_flag)
|
2018-12-03 12:40:13 -05:00
|
|
|
printf("[clambc] Bytecode loaded\n");
|
2010-02-13 19:57:33 +02:00
|
|
|
if (optget(opts, "info")->enabled) {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_bytecode_describe(bc);
|
2010-02-13 19:57:33 +02:00
|
|
|
} else if (optget(opts, "printsrc")->enabled) {
|
2010-02-06 17:53:17 +02:00
|
|
|
print_src(opts->filename[0]);
|
2014-05-22 14:30:03 -04:00
|
|
|
} else if (optget(opts, "printbcir")->enabled) {
|
2019-02-13 21:12:29 -05:00
|
|
|
unsigned i;
|
2014-05-22 14:30:03 -04:00
|
|
|
cli_bytetype_describe(bc);
|
2019-02-13 21:12:29 -05:00
|
|
|
for (i = 0; i < bc->num_func; ++i) {
|
2018-12-19 11:56:13 +01:00
|
|
|
printf("########################################################################\n"
|
|
|
|
"####################### Function id %3u ################################\n"
|
|
|
|
"########################################################################\n", i);
|
|
|
|
cli_bytevalue_describe(bc, i);
|
|
|
|
cli_bytefunc_describe(bc, i);
|
|
|
|
}
|
2010-01-22 16:50:16 +02:00
|
|
|
} else {
|
2018-12-03 12:40:13 -05:00
|
|
|
cli_ctx cctx;
|
|
|
|
struct cl_engine *engine = cl_engine_new();
|
|
|
|
fmap_t *map = NULL;
|
|
|
|
memset(&cctx, 0, sizeof(cctx));
|
|
|
|
if (!engine) {
|
|
|
|
fprintf(stderr, "Unable to create engine\n");
|
|
|
|
optfree(opts);
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
rc = cl_engine_compile(engine);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "Unable to compile engine: %s\n", cl_strerror(rc));
|
|
|
|
optfree(opts);
|
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
rc = cli_bytecode_prepare2(engine, &bcs, BYTECODE_ENGINE_MASK);
|
|
|
|
if (rc != CL_SUCCESS) {
|
|
|
|
fprintf(stderr, "Unable to prepare bytecode: %s\n", cl_strerror(rc));
|
|
|
|
optfree(opts);
|
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
if (debug_flag)
|
|
|
|
printf("[clambc] Bytecode prepared\n");
|
2009-06-26 20:23:52 +03:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
ctx = cli_bytecode_context_alloc();
|
|
|
|
if (!ctx) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
ctx->ctx = &cctx;
|
|
|
|
cctx.engine = engine;
|
|
|
|
cctx.fmap = cli_calloc(sizeof(fmap_t *), engine->maxreclevel + 2);
|
|
|
|
if (!cctx.fmap) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
memset(&dbg_state, 0, sizeof(dbg_state));
|
|
|
|
dbg_state.file = "<libclamav>";
|
|
|
|
dbg_state.line = 0;
|
|
|
|
dbg_state.col = 0;
|
|
|
|
dbg_state.showline = !optget(opts, "no-trace-showsource")->enabled;
|
|
|
|
tracelevel = optget(opts, "trace")->numarg;
|
|
|
|
cli_bytecode_context_set_trace(ctx, tracelevel,
|
|
|
|
tracehook,
|
|
|
|
tracehook_op,
|
|
|
|
tracehook_val,
|
|
|
|
tracehook_ptr);
|
2009-07-07 23:36:36 +03:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (opts->filename[1]) {
|
|
|
|
funcid = atoi(opts->filename[1]);
|
|
|
|
}
|
|
|
|
cli_bytecode_context_setfuncid(ctx, bc, funcid);
|
|
|
|
if (debug_flag)
|
|
|
|
printf("[clambc] Running bytecode function :%u\n", funcid);
|
2009-07-07 23:36:36 +03:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (opts->filename[1]) {
|
|
|
|
i = 2;
|
|
|
|
while (opts->filename[i]) {
|
|
|
|
rc = cli_bytecode_context_setparam_int(ctx, i - 2, atoi(opts->filename[i]));
|
|
|
|
if (rc != CL_SUCCESS) {
|
|
|
|
fprintf(stderr, "Unable to set param %u: %s\n", i - 2, cl_strerror(rc));
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2010-01-22 16:50:16 +02:00
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if ((opt = optget(opts, "input"))->enabled) {
|
|
|
|
fd = open(opt->strarg, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Unable to open input file %s: %s\n", opt->strarg, strerror(errno));
|
|
|
|
optfree(opts);
|
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
map = fmap(fd, 0, 0);
|
|
|
|
if (!map) {
|
|
|
|
fprintf(stderr, "Unable to map input file %s\n", opt->strarg);
|
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
rc = cli_bytecode_context_setfile(ctx, map);
|
|
|
|
if (rc != CL_SUCCESS) {
|
|
|
|
fprintf(stderr, "Unable to set file %s: %s\n", opt->strarg, cl_strerror(rc));
|
|
|
|
optfree(opts);
|
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* for testing */
|
|
|
|
ctx->hooks.match_counts = deadbeefcounts;
|
|
|
|
ctx->hooks.match_offsets = deadbeefcounts;
|
|
|
|
rc = cli_bytecode_run(&bcs, bc, ctx);
|
|
|
|
if (rc != CL_SUCCESS) {
|
|
|
|
fprintf(stderr, "Unable to run bytecode: %s\n", cl_strerror(rc));
|
|
|
|
} else {
|
|
|
|
uint64_t v;
|
|
|
|
if (debug_flag)
|
|
|
|
printf("[clambc] Bytecode run finished\n");
|
|
|
|
v = cli_bytecode_context_getresult_int(ctx);
|
|
|
|
if (debug_flag)
|
|
|
|
printf("[clambc] Bytecode returned: 0x%llx\n", (long long)v);
|
|
|
|
}
|
|
|
|
cli_bytecode_context_destroy(ctx);
|
|
|
|
if (map)
|
|
|
|
funmap(map);
|
|
|
|
cl_engine_free(engine);
|
|
|
|
free(cctx.fmap);
|
2009-07-07 23:36:36 +03:00
|
|
|
}
|
2009-06-26 20:23:52 +03:00
|
|
|
cli_bytecode_destroy(bc);
|
2009-08-25 18:54:14 +03:00
|
|
|
cli_bytecode_done(&bcs);
|
2009-06-26 20:23:52 +03:00
|
|
|
free(bc);
|
2009-06-30 19:40:26 +03:00
|
|
|
optfree(opts);
|
2009-09-08 22:25:33 +03:00
|
|
|
if (fd != -1)
|
2018-12-03 12:40:13 -05:00
|
|
|
close(fd);
|
2010-05-14 10:40:04 +03:00
|
|
|
if (debug_flag)
|
2018-12-03 12:40:13 -05:00
|
|
|
printf("[clambc] Exiting\n");
|
2014-05-09 17:09:29 -04:00
|
|
|
cl_cleanup_crypto();
|
2009-06-26 20:23:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|