From 44d668964902a796e43eccbb6617a45600b4cdf4 Mon Sep 17 00:00:00 2001 From: thez3ro Date: Wed, 27 Sep 2023 14:30:39 +0200 Subject: [PATCH] optimize EZCOV coverage --- qiling/extensions/coverage/formats/ezcov.py | 26 +++++++++------------ 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/qiling/extensions/coverage/formats/ezcov.py b/qiling/extensions/coverage/formats/ezcov.py index e5d89fd6d..dd1c8efe1 100644 --- a/qiling/extensions/coverage/formats/ezcov.py +++ b/qiling/extensions/coverage/formats/ezcov.py @@ -3,20 +3,18 @@ # Cross Platform and Multi Architecture Advanced Binary Emulation Framework # +from collections import namedtuple from os.path import basename from .base import QlBaseCoverage # Adapted from https://github.com/nccgroup/Cartographer/blob/main/EZCOV.md#coverage-data -class bb_entry(dict): - def __init__(self, offset, size, mod_id): - self.offset = '0x{:08x}'.format(offset) - self.size = size - self.mod_id = f"[ {mod_id if mod_id is not None else ''} ]" - +class bb_entry(namedtuple('bb_entry', 'offset size mod_id')): def csvline(self): - return f"{self.offset},{self.size},{self.mod_id}\n" + offset = '0x{:08x}'.format(self.offset) + mod_id = f"[ {self.mod_id if self.mod_id is not None else ''} ]" + return f"{offset},{self.size},{mod_id}\n" class QlEzCoverage(QlBaseCoverage): """ @@ -30,8 +28,7 @@ class QlEzCoverage(QlBaseCoverage): FORMAT_NAME = "ezcov" def __init__(self, ql): - super().__init__() - self.ql = ql + super().__init__(ql) self.ezcov_version = 1 self.ezcov_flavor = 'ezcov' self.basic_blocks = [] @@ -39,11 +36,10 @@ def __init__(self, ql): @staticmethod def block_callback(ql, address, size, self): - for mod in ql.loader.images: - if mod.base <= address <= mod.end: - ent = bb_entry(address - mod.base, size, basename(mod.path)) - self.basic_blocks.append(ent) - break + mod = ql.loader.find_containing_image(address) + if mod is not None: + ent = bb_entry(address - mod.base, size, basename(mod.path)) + self.basic_blocks.append(ent) def activate(self): self.bb_callback = self.ql.hook_block(self.block_callback, user_data=self) @@ -54,6 +50,6 @@ def deactivate(self): def dump_coverage(self, coverage_file): with open(coverage_file, "w") as cov: cov.write(f"EZCOV VERSION: {self.ezcov_version}\n") - cov.write("# Qiling EZCOV exporter tool\n") + cov.write("# Qiling EZCOV2222 exporter tool\n") for bb in self.basic_blocks: cov.write(bb.csvline()) \ No newline at end of file