Size is specified on first line (which must be <8k).
Török Edvin authored on 2009/12/11 23:05:38... | ... |
@@ -403,12 +403,13 @@ static inline char *readString(const unsigned char *p, unsigned *off, unsigned l |
403 | 403 |
return str; |
404 | 404 |
} |
405 | 405 |
|
406 |
-static int parseHeader(struct cli_bc *bc, unsigned char *buffer) |
|
406 |
+static int parseHeader(struct cli_bc *bc, unsigned char *buffer, unsigned *linelength) |
|
407 | 407 |
{ |
408 | 408 |
uint64_t magic1; |
409 | 409 |
unsigned magic2; |
410 | 410 |
char ok = 1; |
411 | 411 |
unsigned offset, len, flevel; |
412 |
+ char *pos; |
|
412 | 413 |
if (strncmp((const char*)buffer, BC_HEADER, sizeof(BC_HEADER)-1)) { |
413 | 414 |
cli_errmsg("Missing file magic in bytecode"); |
414 | 415 |
return CL_EMALFDB; |
... | ... |
@@ -420,7 +421,7 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer) |
420 | 420 |
cli_errmsg("Unable to parse functionality level in bytecode header\n"); |
421 | 421 |
return CL_EMALFDB; |
422 | 422 |
} |
423 |
- if (flevel > BC_FUNC_LEVEL) { |
|
423 |
+ if (flevel != BC_FUNC_LEVEL) { |
|
424 | 424 |
cli_dbgmsg("Skipping bytecode with functionality level: %u\n", flevel); |
425 | 425 |
return CL_BREAK; |
426 | 426 |
} |
... | ... |
@@ -451,9 +452,14 @@ static int parseHeader(struct cli_bc *bc, unsigned char *buffer) |
451 | 451 |
cli_errmsg("Magic numbers don't match: %lx%lx, %u\n", m0, m1, magic2); |
452 | 452 |
return CL_EMALFDB; |
453 | 453 |
} |
454 |
- if (offset != len) { |
|
455 |
- cli_errmsg("Trailing garbage in bytecode header: %d extra bytes\n", |
|
456 |
- len-offset); |
|
454 |
+ if (buffer[offset] != ':') { |
|
455 |
+ cli_errmsg("Expected : but found: %c\n", buffer[offset]); |
|
456 |
+ return CL_EMALFDB; |
|
457 |
+ } |
|
458 |
+ offset++; |
|
459 |
+ *linelength = strtol(buffer+offset, &pos, 10); |
|
460 |
+ if (*pos != '\n') { |
|
461 |
+ cli_errmsg("Invalid number: %s\n", buffer+offset); |
|
457 | 462 |
return CL_EMALFDB; |
458 | 463 |
} |
459 | 464 |
|
... | ... |
@@ -1169,8 +1175,7 @@ static int parseBB(struct cli_bc *bc, unsigned func, unsigned bb, unsigned char |
1169 | 1169 |
} |
1170 | 1170 |
|
1171 | 1171 |
enum parse_state { |
1172 |
- PARSE_BC_HEADER=0, |
|
1173 |
- PARSE_BC_TYPES, |
|
1172 |
+ PARSE_BC_TYPES=0, |
|
1174 | 1173 |
PARSE_BC_APIS, |
1175 | 1174 |
PARSE_BC_GLOBALS, |
1176 | 1175 |
PARSE_BC_LSIG, |
... | ... |
@@ -1182,38 +1187,49 @@ enum parse_state { |
1182 | 1182 |
int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1183 | 1183 |
{ |
1184 | 1184 |
unsigned row = 0, current_func = 0, bb=0; |
1185 |
- char buffer[FILEBUFF]; |
|
1186 |
- enum parse_state state = PARSE_BC_HEADER; |
|
1185 |
+ char *buffer; |
|
1186 |
+ unsigned linelength=0; |
|
1187 |
+ char firstbuf[FILEBUFF]; |
|
1188 |
+ enum parse_state state; |
|
1189 |
+ int rc; |
|
1187 | 1190 |
|
1188 | 1191 |
if (!f && !dbio) { |
1189 | 1192 |
cli_errmsg("Unable to load bytecode (null file)\n"); |
1190 | 1193 |
return CL_ENULLARG; |
1191 | 1194 |
} |
1192 |
- while (cli_dbgets(buffer, FILEBUFF, f, dbio)) { |
|
1193 |
- int rc; |
|
1195 |
+ if (!cli_dbgets(firstbuf, FILEBUFF, f, dbio)) { |
|
1196 |
+ cli_errmsg("Unable to load bytecode (empty file)\n"); |
|
1197 |
+ return CL_EMALFDB; |
|
1198 |
+ } |
|
1199 |
+ rc = parseHeader(bc, (unsigned char*)firstbuf, &linelength); |
|
1200 |
+ if (rc == CL_BREAK) { |
|
1201 |
+ bc->state = bc_skip; |
|
1202 |
+ return CL_SUCCESS; |
|
1203 |
+ } |
|
1204 |
+ if (rc != CL_SUCCESS) { |
|
1205 |
+ cli_errmsg("Error at bytecode line %u\n", row); |
|
1206 |
+ return rc; |
|
1207 |
+ } |
|
1208 |
+ buffer = cli_malloc(linelength); |
|
1209 |
+ if (!buffer) { |
|
1210 |
+ cli_errmsg("Out of memory allocating line of length %u\n", linelength); |
|
1211 |
+ return CL_EMEM; |
|
1212 |
+ } |
|
1213 |
+ state = PARSE_BC_LSIG; |
|
1214 |
+ while (cli_dbgets(buffer, linelength, f, dbio)) { |
|
1194 | 1215 |
cli_chomp(buffer); |
1195 | 1216 |
row++; |
1196 | 1217 |
switch (state) { |
1197 |
- case PARSE_BC_HEADER: |
|
1198 |
- rc = parseHeader(bc, (unsigned char*)buffer); |
|
1199 |
- if (rc == CL_BREAK) /* skip */ { |
|
1200 |
- bc->state = bc_skip; |
|
1201 |
- return CL_SUCCESS; |
|
1202 |
- } |
|
1203 |
- if (rc != CL_SUCCESS) { |
|
1204 |
- cli_errmsg("Error at bytecode line %u\n", row); |
|
1205 |
- return rc; |
|
1206 |
- } |
|
1207 |
- state = PARSE_BC_LSIG; |
|
1208 |
- break; |
|
1209 | 1218 |
case PARSE_BC_LSIG: |
1210 | 1219 |
rc = parseLSig(bc, (unsigned char*)buffer); |
1211 | 1220 |
if (rc == CL_BREAK) /* skip */ { |
1212 | 1221 |
bc->state = bc_skip; |
1222 |
+ free(buffer); |
|
1213 | 1223 |
return CL_SUCCESS; |
1214 | 1224 |
} |
1215 | 1225 |
if (rc != CL_SUCCESS) { |
1216 | 1226 |
cli_errmsg("Error at bytecode line %u\n", row); |
1227 |
+ free(buffer); |
|
1217 | 1228 |
return rc; |
1218 | 1229 |
} |
1219 | 1230 |
state = PARSE_BC_TYPES; |
... | ... |
@@ -1222,6 +1238,7 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1222 | 1222 |
rc = parseTypes(bc, (unsigned char*)buffer); |
1223 | 1223 |
if (rc != CL_SUCCESS) { |
1224 | 1224 |
cli_errmsg("Error at bytecode line %u\n", row); |
1225 |
+ free(buffer); |
|
1225 | 1226 |
return rc; |
1226 | 1227 |
} |
1227 | 1228 |
state = PARSE_BC_APIS; |
... | ... |
@@ -1230,10 +1247,12 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1230 | 1230 |
rc = parseApis(bc, (unsigned char*)buffer); |
1231 | 1231 |
if (rc == CL_BREAK) /* skip */ { |
1232 | 1232 |
bc->state = bc_skip; |
1233 |
+ free(buffer); |
|
1233 | 1234 |
return CL_SUCCESS; |
1234 | 1235 |
} |
1235 | 1236 |
if (rc != CL_SUCCESS) { |
1236 | 1237 |
cli_errmsg("Error at bytecode line %u\n", row); |
1238 |
+ free(buffer); |
|
1237 | 1239 |
return rc; |
1238 | 1240 |
} |
1239 | 1241 |
state = PARSE_BC_GLOBALS; |
... | ... |
@@ -1242,10 +1261,12 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1242 | 1242 |
rc = parseGlobals(bc, (unsigned char*)buffer); |
1243 | 1243 |
if (rc == CL_BREAK) /* skip */ { |
1244 | 1244 |
bc->state = bc_skip; |
1245 |
+ free(buffer); |
|
1245 | 1246 |
return CL_SUCCESS; |
1246 | 1247 |
} |
1247 | 1248 |
if (rc != CL_SUCCESS) { |
1248 | 1249 |
cli_errmsg("Error at bytecode line %u\n", row); |
1250 |
+ free(buffer); |
|
1249 | 1251 |
return rc; |
1250 | 1252 |
} |
1251 | 1253 |
state = PARSE_MD_OPT_HEADER; |
... | ... |
@@ -1255,6 +1276,7 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1255 | 1255 |
rc = parseMD(bc, (unsigned char*)buffer); |
1256 | 1256 |
if (rc != CL_SUCCESS) { |
1257 | 1257 |
cli_errmsg("Error at bytecode line %u\n", row); |
1258 |
+ free(buffer); |
|
1258 | 1259 |
return rc; |
1259 | 1260 |
} |
1260 | 1261 |
break; |
... | ... |
@@ -1264,6 +1286,7 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1264 | 1264 |
rc = parseFunctionHeader(bc, current_func, (unsigned char*)buffer); |
1265 | 1265 |
if (rc != CL_SUCCESS) { |
1266 | 1266 |
cli_errmsg("Error at bytecode line %u\n", row); |
1267 |
+ free(buffer); |
|
1267 | 1268 |
return rc; |
1268 | 1269 |
} |
1269 | 1270 |
bb = 0; |
... | ... |
@@ -1273,12 +1296,14 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1273 | 1273 |
rc = parseBB(bc, current_func, bb++, (unsigned char*)buffer); |
1274 | 1274 |
if (rc != CL_SUCCESS) { |
1275 | 1275 |
cli_errmsg("Error at bytecode line %u\n", row); |
1276 |
+ free(buffer); |
|
1276 | 1277 |
return rc; |
1277 | 1278 |
} |
1278 | 1279 |
if (bb >= bc->funcs[current_func].numBB) { |
1279 | 1280 |
if (bc->funcs[current_func].insn_idx != bc->funcs[current_func].numInsts) { |
1280 | 1281 |
cli_errmsg("Parsed different number of instructions than declared: %u != %u\n", |
1281 | 1282 |
bc->funcs[current_func].insn_idx, bc->funcs[current_func].numInsts); |
1283 |
+ free(buffer); |
|
1282 | 1284 |
return CL_EMALFDB; |
1283 | 1285 |
} |
1284 | 1286 |
cli_dbgmsg("Parsed %u BBs, %u instructions\n", |
... | ... |
@@ -1289,6 +1314,7 @@ int cli_bytecode_load(struct cli_bc *bc, FILE *f, struct cli_dbio *dbio) |
1289 | 1289 |
break; |
1290 | 1290 |
} |
1291 | 1291 |
} |
1292 |
+ free(buffer); |
|
1292 | 1293 |
cli_dbgmsg("Parsed %d functions\n", current_func); |
1293 | 1294 |
if (current_func != bc->num_func) { |
1294 | 1295 |
cli_errmsg("Loaded less functions than declared: %u vs. %u\n", |
... | ... |
@@ -1187,7 +1187,10 @@ int bytecode_init(void) |
1187 | 1187 |
|
1188 | 1188 |
#ifdef CL_DEBUG |
1189 | 1189 |
llvm::JITEmitDebugInfo = true; |
1190 |
+#else |
|
1191 |
+ llvm::JITEmitDebugInfo = false; |
|
1190 | 1192 |
#endif |
1193 |
+ llvm::DwarfExceptionHandling = false; |
|
1191 | 1194 |
llvm_start_multithreaded(); |
1192 | 1195 |
|
1193 | 1196 |
// If we have a native target, initialize it to ensure it is linked in and |
... | ... |
@@ -1,4 +1,4 @@ |
1 |
-ClamBCaa`|``c``a```|`bjaabp`clamcoincidencejb |
|
1 |
+ClamBCab`|``c``a```|`bjaabp`clamcoincidencejb:326 |
|
2 | 2 |
Trojan.Foo.{A,B};Target:1;(((0|1|2)=42,2)|(3=10));EP+0:aabb;ffff;aaccee;f00d;dead |
3 | 3 |
Tedebieebheebgeebfeebeeebdeebbeebaeebadebcdaaa`aacb`bbadb`bdb`db`bcajbadbcebadbcebadbcebadbcebadbcecaab`bdagahdaeahdajahdabbaddabahdakah |
4 | 4 |
Eafaaafb`e|amcgefdgfgifbgegcgnfafmfef`` |