Add draft file API, doesn't work yet.

0.96
Török Edvin 16 years ago
parent d0af4afea4
commit 4789b8a5a6
  1. 10
      libclamav/bytecode.c
  2. 1
      libclamav/bytecode.h
  3. 30
      libclamav/bytecode_api.c
  4. 17
      libclamav/bytecode_api.h
  5. 16
      libclamav/bytecode_api_decl.c
  6. 3
      libclamav/bytecode_priv.h
  7. 2
      libclamav/c++/bytecode2llvm.cpp

@ -39,6 +39,8 @@ struct cli_bc_ctx *cli_bytecode_context_alloc(void)
ctx->values = NULL;
ctx->operands = NULL;
ctx->opsizes = NULL;
ctx->fd = -1;
ctx->off = 0;
return ctx;
}
@ -940,8 +942,10 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio)
switch (state) {
case PARSE_BC_HEADER:
rc = parseHeader(bc, (unsigned char*)buffer);
if (rc == CL_BREAK) /* skip */
if (rc == CL_BREAK) /* skip */ {
bc->state = bc_skip;
return CL_SUCCESS;
}
if (rc != CL_SUCCESS) {
cli_errmsg("Error at bytecode line %u\n", row);
return rc;
@ -958,8 +962,10 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio)
break;
case PARSE_BC_APIS:
rc = parseApis(bc, (unsigned char*)buffer);
if (rc == CL_BREAK) /* skip */
if (rc == CL_BREAK) /* skip */ {
bc->state = bc_skip;
return CL_SUCCESS;
}
if (rc != CL_SUCCESS) {
cli_errmsg("Error at bytecode line %u\n", row);
return rc;

@ -34,6 +34,7 @@ struct cli_bc_engine;
struct bitset_tag;
enum bc_state {
bc_skip,
bc_loaded,
bc_jit,
bc_interp

@ -20,7 +20,10 @@
* MA 02110-1301, USA.
*/
#include <stdlib.h>
#include "cltypes.h"
#include "clambc.h"
#include "bytecode_priv.h"
#include "type_desc.h"
#include "bytecode_api.h"
#include "bytecode_api_impl.h"
@ -34,3 +37,30 @@ uint32_t cli_bcapi_test1(struct cli_bc_ctx *ctx, uint32_t a, uint32_t b)
{
return (a==0xf00dbeef && b==0xbeeff00d) ? 0x12345678 : 0x55;
}
int32_t cli_bcapi_read(struct cli_bc_ctx* ctx, uint8_t *data, int32_t size)
{
if (ctx->fd == -1)
return -1;
return pread(ctx->fd, data, size, ctx->off);
}
int32_t cli_bcapi_seek(struct cli_bc_ctx* ctx, int32_t pos, uint32_t whence)
{
off_t off;
switch (whence) {
case 0:
off = pos;
break;
case 1:
off = ctx->off + pos;
break;
case 2:
off = ctx->file_size + pos;
break;
}
if (off < 0 || off > ctx->file_size)
return -1;
ctx->off = off;
return off;
}

@ -30,5 +30,22 @@ struct foo {
struct foo *nxt;
};
#ifdef __CLAMBC__
uint32_t test0(struct foo*, uint32_t);
uint32_t test1(uint32_t, uint32_t);
/* reads @size bytes from current file (if any) to @data, returns amount read */
int32_t read(uint8_t *data, int32_t size);
enum {
SEEK_SET=0,
SEEK_CUR,
SEEK_END
};
/* seeks current position to @pos, from @whence, returns current position from
* start of file */
int32_t seek(int32_t pos, uint32_t whence);
#endif

@ -26,30 +26,38 @@
uint32_t cli_bcapi_test0(struct cli_bc_ctx *ctx, struct foo*, uint32_t);
uint32_t cli_bcapi_test1(struct cli_bc_ctx *ctx, uint32_t, uint32_t);
int32_t cli_bcapi_read(struct cli_bc_ctx *ctx, uint8_t*, int32_t);
int32_t cli_bcapi_seek(struct cli_bc_ctx *ctx, int32_t, uint32_t);
static uint16_t cli_tmp0[]={32, 70, 32};
static uint16_t cli_tmp1[]={71};
static uint16_t cli_tmp2[]={70};
static uint16_t cli_tmp3[]={32, 32, 32};
static uint16_t cli_tmp4[]={32, 65, 32};
const struct cli_bc_type cli_apicall_types[]={
{DFunctionType, cli_tmp0, 3},
{DPointerType, cli_tmp1, 1},
{DStructType, cli_tmp2, 1},
{DFunctionType, cli_tmp3, 3}
{DFunctionType, cli_tmp3, 3},
{DFunctionType, cli_tmp4, 3}
};
const unsigned cli_apicall_maxtypes=sizeof(cli_apicall_types)/sizeof(cli_apicall_types[0]);
const struct cli_apicall cli_apicalls[]={
/* Bytecode APIcalls BEGIN */
{"test0", 0, 0, 1},
{"test1", 3, 0, 0}
{"test1", 3, 0, 0},
{"read", 4, 1, 1},
{"seek", 3, 1, 0}
/* Bytecode APIcalls END */
};
const cli_apicall_int2 cli_apicalls0[] = {
cli_bcapi_test1
cli_bcapi_test1,
cli_bcapi_seek
};
const cli_apicall_pointer cli_apicalls1[] = {
(cli_apicall_pointer)cli_bcapi_test0
(cli_apicall_pointer)cli_bcapi_test0,
cli_bcapi_read
};
const unsigned cli_apicall_maxapi = sizeof(cli_apicalls)/sizeof(cli_apicalls[0]);

@ -95,6 +95,9 @@ struct cli_bc_ctx {
operand_t *operands;
uint16_t funcid;
unsigned numParams;
size_t file_size;
off_t off;
int fd;
};
struct cli_all_bc;
int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct cli_bc_func *func, const struct cli_bc_inst *inst);

@ -755,6 +755,8 @@ int cli_bytecode_prepare_jit(struct cli_all_bc *bcs)
for (unsigned i=0;i<bcs->count;i++) {
const struct cli_bc *bc = &bcs->all_bcs[i];
if (bc->state == bc_skip)
continue;
LLVMCodegen Codegen(bc, M, bcs->engine->compiledFunctions, EE,
OurFPM, apiFuncs);
if (!Codegen.generate()) {

Loading…
Cancel
Save