~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/bytecode_priv.h

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
Import upstream version 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Load, verify and execute ClamAV bytecode.
3
 
 *
4
 
 *  Copyright (C) 2009-2010 Sourcefire, Inc.
5
 
 *
6
 
 *  Authors: Török Edvin
7
 
 *
8
 
 *  This program is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License version 2 as
10
 
 *  published by the Free Software Foundation.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 
 *  MA 02110-1301, USA.
21
 
 */
22
 
 
23
 
#ifndef BYTECODE_PRIV_H
24
 
#define BYTECODE_PRIV_H
25
 
 
26
 
#include <zlib.h>
27
 
#include "bytecode.h"
28
 
#include "type_desc.h"
29
 
#include "execs.h"
30
 
#include "bytecode_hooks.h"
31
 
#include "fmap.h"
32
 
#include "mpool.h"
33
 
#include "hashtab.h"
34
 
#include "events.h"
35
 
 
36
 
typedef uint32_t operand_t;
37
 
typedef uint16_t bbid_t;
38
 
typedef uint16_t funcid_t;
39
 
 
40
 
struct cli_bc_callop {
41
 
    operand_t* ops;
42
 
    uint16_t* opsizes;
43
 
    funcid_t funcid;
44
 
    uint8_t numOps;
45
 
};
46
 
 
47
 
struct branch {
48
 
    operand_t condition;
49
 
    bbid_t br_true;
50
 
    bbid_t br_false;
51
 
};
52
 
 
53
 
struct cli_bc_cast {
54
 
    uint64_t mask;
55
 
    operand_t source;
56
 
    uint8_t size;/* 0: 1-bit, 1: 8b, 2: 16b, 3: 32b, 4: 64b */
57
 
};
58
 
 
59
 
typedef uint8_t interp_op_t;
60
 
struct cli_bc_inst {
61
 
    enum bc_opcode opcode;
62
 
    uint16_t type;
63
 
    operand_t dest;
64
 
    interp_op_t interp_op;/* opcode for interpreter */
65
 
    union {
66
 
        operand_t unaryop;
67
 
        struct cli_bc_cast cast;
68
 
        operand_t binop[2];
69
 
        operand_t three[3];
70
 
        struct cli_bc_callop ops;
71
 
        struct branch branch;
72
 
        bbid_t jump;
73
 
    } u;
74
 
};
75
 
 
76
 
struct cli_bc_bb {
77
 
    unsigned numInsts;
78
 
    struct cli_bc_inst *insts;
79
 
};
80
 
 
81
 
struct cli_bc_func {
82
 
    uint8_t numArgs;
83
 
    uint16_t numLocals;
84
 
    uint32_t numInsts;
85
 
    uint32_t numValues;/* without constants */
86
 
    uint32_t numConstants;
87
 
    uint32_t numBytes;/* stack size */
88
 
    uint16_t numBB;
89
 
    uint16_t returnType;
90
 
    uint16_t *types;
91
 
    uint32_t insn_idx;
92
 
    struct cli_bc_bb *BB;
93
 
    struct cli_bc_inst *allinsts;
94
 
    uint64_t *constants;
95
 
    unsigned *dbgnodes;
96
 
};
97
 
 
98
 
struct cli_bc_dbgnode_element {
99
 
    unsigned nodeid;
100
 
    unsigned len;
101
 
    char *string;
102
 
    uint64_t constant;
103
 
};
104
 
 
105
 
struct cli_bc_dbgnode {
106
 
    unsigned numelements;
107
 
    struct cli_bc_dbgnode_element* elements;
108
 
};
109
 
 
110
 
#define MAX_OP ~0u
111
 
enum trace_level {
112
 
    trace_none=0,
113
 
    trace_func,
114
 
    trace_param,
115
 
    trace_scope,
116
 
    trace_line,
117
 
    trace_col,
118
 
    trace_op,
119
 
    trace_val
120
 
};
121
 
 
122
 
struct bc_buffer {
123
 
    unsigned char *data;
124
 
    unsigned size;
125
 
    unsigned write_cursor;
126
 
    unsigned read_cursor;
127
 
};
128
 
 
129
 
struct bc_inflate {
130
 
    z_stream stream;
131
 
    int32_t from;
132
 
    int32_t to;
133
 
    int8_t  needSync;
134
 
};
135
 
 
136
 
struct bc_jsnorm {
137
 
    struct parser_state *state;
138
 
    int32_t from;
139
 
};
140
 
 
141
 
enum bc_events {
142
 
    BCEV_VIRUSNAME,
143
 
    BCEV_EXEC_RETURNVALUE,
144
 
    BCEV_WRITE,
145
 
    BCEV_OFFSET,
146
 
    BCEV_READ,
147
 
    BCEV_DBG_STR,
148
 
    BCEV_DBG_INT,
149
 
    BCEV_MEM_1,
150
 
    BCEV_MEM_2,
151
 
    BCEV_FIND,
152
 
    BCEV_EXTRACTED,
153
 
    BCEV_EXEC_TIME,
154
 
    /* API failures (that are not serious), count must be 0 for testmode */
155
 
    BCEV_API_WARN_BEGIN,
156
 
    BCEV_READ_ERR,
157
 
    BCEV_DISASM_FAIL,
158
 
    BCEV_API_WARN_END,
159
 
    /* real errors (write failure) are reported via cli_event_error_str */
160
 
    BCEV_LASTEVENT
161
 
};
162
 
 
163
 
struct cli_bc_ctx {
164
 
    uint8_t timeout;/* must be first byte in struct! */
165
 
    uint16_t funcid;
166
 
    unsigned numParams;
167
 
    /* id and params of toplevel function called */
168
 
    const struct cli_bc *bc;
169
 
    const struct cli_bc_func *func;
170
 
    uint32_t bytecode_timeout;
171
 
    unsigned bytes;
172
 
    uint16_t *opsizes;
173
 
    char *values;
174
 
    operand_t *operands;
175
 
    uint32_t file_size;
176
 
    int outfd;
177
 
    off_t off;
178
 
    fmap_t *fmap;
179
 
    fmap_t *save_map;
180
 
    const char *virname;
181
 
    struct cli_bc_hooks hooks;
182
 
    struct cli_exe_info exeinfo;
183
 
    uint32_t lsigcnt[64];
184
 
    uint32_t lsigoff[64];
185
 
    uint32_t pdf_nobjs;
186
 
    struct pdf_obj *pdf_objs;
187
 
    uint32_t* pdf_flags;
188
 
    uint32_t pdf_size;
189
 
    uint32_t pdf_startoff;
190
 
    unsigned pdf_phase;
191
 
    int32_t pdf_dumpedid;
192
 
    const struct cli_exe_section *sections;
193
 
    uint32_t resaddr;
194
 
    char *tempfile;
195
 
    void *ctx;
196
 
    unsigned written;
197
 
    unsigned filewritten;
198
 
    unsigned found;
199
 
    unsigned ninflates;
200
 
    bc_dbg_callback_trace trace;
201
 
    bc_dbg_callback_trace_op trace_op;
202
 
    bc_dbg_callback_trace_val trace_val;
203
 
    bc_dbg_callback_trace_ptr trace_ptr;
204
 
    const char *directory;
205
 
    const char *file;
206
 
    const char *scope;
207
 
    unsigned trace_level;
208
 
    uint32_t scopeid;
209
 
    unsigned line;
210
 
    unsigned col;
211
 
    mpool_t *mpool;
212
 
    struct bc_inflate* inflates;
213
 
    struct bc_buffer *buffers;
214
 
    unsigned nbuffers;
215
 
    unsigned nhashsets;
216
 
    unsigned njsnorms;
217
 
    unsigned jsnormwritten;
218
 
    struct cli_hashset *hashsets;
219
 
    struct bc_jsnorm* jsnorms;
220
 
    char *jsnormdir;
221
 
    struct cli_map *maps;
222
 
    unsigned nmaps;
223
 
    unsigned containertype;
224
 
    unsigned extracted_file_input;
225
 
    const struct cli_environment *env;
226
 
    unsigned bytecode_disable_status;
227
 
    cli_events_t *bc_events;
228
 
    int on_jit;
229
 
    int no_diff;
230
 
};
231
 
struct cli_all_bc;
232
 
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);
233
 
 
234
 
#ifdef __cplusplus
235
 
extern "C" {
236
 
#endif
237
 
 
238
 
int cli_vm_execute_jit(const struct cli_all_bc *bcs, struct cli_bc_ctx *ctx, const struct cli_bc_func *func);
239
 
int cli_bytecode_prepare_jit(struct cli_all_bc *bc);
240
 
int cli_bytecode_init_jit(struct cli_all_bc *bc, unsigned dconfmask);
241
 
int cli_bytecode_done_jit(struct cli_all_bc *bc, int partial);
242
 
 
243
 
#ifdef __cplusplus
244
 
}
245
 
#endif
246
 
#endif