Skip to content

Commit

Permalink
Merge pull request #1 from quarkslab/code-clean
Browse files Browse the repository at this point in the history
style: various improvements on the code style
  • Loading branch information
dm authored Nov 4, 2022
2 parents 97f9b82 + d7f1756 commit 3093c57
Show file tree
Hide file tree
Showing 26 changed files with 387 additions and 138 deletions.
4 changes: 2 additions & 2 deletions bindings/python/quokka/addresser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def file(self, offset: int) -> int:
"""
try:
segment = self.program.get_segment(offset)
except KeyError:
raise quokka.NotInFileError("Unable to find the segment")
except KeyError as exc:
raise quokka.NotInFileError("Unable to find the segment") from exc

if segment.file_offset != -1:
return offset + segment.file_offset
Expand Down
4 changes: 4 additions & 0 deletions bindings/python/quokka/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""Analysis modules
This module contain some utilities function to manipulate the binaries.
"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
16 changes: 12 additions & 4 deletions bindings/python/quokka/analysis/arch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Architecture module"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,9 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import capstone
import collections
from enum import IntEnum, IntFlag

import capstone

from quokka.types import List, RegType


Expand All @@ -35,11 +38,11 @@ def make_enums(
A list of IntEnum/IntFlag
"""
data = collections.defaultdict(dict)
for k, v in getattr(capstone_module, "__dict__").items():
_, cat, name = k.split("_", maxsplit=2)
for key, val in getattr(capstone_module, "__dict__").items():
_, cat, name = key.split("_", maxsplit=2)
if name not in blacklist:
name = "_" + name if "0" <= name[0] <= "9" else name
data[cat][name] = v
data[cat][name] = val

return [
IntEnum(x, names=data[x], module=__name__)
Expand Down Expand Up @@ -73,6 +76,7 @@ class ArchEnum(IntEnum):

class QuokkaArch:
"""Base class for a QuokkaArch"""

address_size: int
compared_mnemonics: List[str]
stack_pointer: RegType
Expand Down Expand Up @@ -100,6 +104,7 @@ class ArchX86(QuokkaArch): # type: ignore

class ArchX64(ArchX86): # type: ignore
"""Arch X64 definition"""

address_size = 64

frame_pointer = ArchX86.regs.RBP
Expand All @@ -109,6 +114,7 @@ class ArchX64(ArchX86): # type: ignore

class ArchARM(QuokkaArch): # type: ignore
"""ArchARM definition"""

(
cc,
cpsflag,
Expand Down Expand Up @@ -157,11 +163,13 @@ class ArchARM(QuokkaArch): # type: ignore

class ArchARMThumb(ArchARM): # type: ignore
"""Arch Arm Thum definition"""

thumb: bool = True


class ArchARM64(QuokkaArch): # type: ignore
"""Arch Arm64 definition"""

(
at,
barrier,
Expand Down
9 changes: 9 additions & 0 deletions bindings/python/quokka/analysis/calling_convention.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Calling conventions"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,6 +22,7 @@

class CallingConvention:
"""Base class for a calling convention"""

name: str
argument_registers: List[RegType]
floating_point_registers: List[RegType]
Expand All @@ -33,6 +35,7 @@ class CallingConvention:

class CCdecl(CallingConvention):
"""Cdecl calling convention"""

name = "cdecl"
argument_registers: List[RegType] = [] # All arguments are on the stack
floating_point_registers: List[RegType] = [] # Same
Expand All @@ -48,12 +51,14 @@ class CCdecl(CallingConvention):

class Stdcall(CCdecl):
"""Stdcall calling convention"""

name = "stdcall"
callee_saved = True


class Fastcall(CallingConvention):
"""Fastcall calling convention"""

name = "fastcall"
argument_registers = [ArchX86.regs.ECX, ArchX86.regs.EDX]
floating_point_registers = [ArchX86.regs.ST0, ArchX86.regs.ST1] # TODO(dm) check
Expand All @@ -70,6 +75,7 @@ class Fastcall(CallingConvention):

class MicrosoftAMD64(CallingConvention):
"""Microsoft 64 calling convention"""

name = "ms"
argument_registers = [
ArchX64.regs.RCX,
Expand All @@ -91,6 +97,7 @@ class MicrosoftAMD64(CallingConvention):

class SystemVAMD(CallingConvention):
"""SysV calling convention"""

name = "sysv"
argument_registers = [
ArchX64.regs.RDI,
Expand Down Expand Up @@ -124,6 +131,7 @@ class SystemVAMD(CallingConvention):

class ARMCC(CallingConvention):
"""AAPCS calling convention for ARM"""

name = "aapcs"
argument_registers = [
ArchARM.regs.R0,
Expand All @@ -138,6 +146,7 @@ class ARMCC(CallingConvention):

class ARM64CC(CallingConvention):
"""AAPCS calling convention for Aarch64"""

name = "aapcs"
argument_registers = [
ArchARM64.regs.X0,
Expand Down
7 changes: 5 additions & 2 deletions bindings/python/quokka/analysis/env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Environment module"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -55,8 +56,8 @@ def get_calling_convention_for_arch_platform(
for architecture, convention in platform_mapping.items():
if issubclass(arch, architecture):
return convention
else:
return cc.CallingConvention

return cc.CallingConvention


class Environment:
Expand All @@ -72,6 +73,7 @@ class Environment:
calling_convention: Calling convention
"""

def __init__(
self,
platform: quokka.analysis.Platform,
Expand All @@ -87,6 +89,7 @@ def __init__(

class Platform(enum.Enum):
"""Platform enumeration"""

UNKNOWN = enum.auto()
WINDOWS = enum.auto()
LINUX = enum.auto()
Expand Down
3 changes: 2 additions & 1 deletion bindings/python/quokka/analysis/replacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
class Replacer:
"""Replacer class
Warning: This class has been used in some experiments but will should probably be
Warning: This class has been used in some experiments but will/should probably be
removed because it does not belong to the project.
"""

ignored_registers: List[str]
ignored_mnemonics: List[str]

Expand Down
7 changes: 4 additions & 3 deletions bindings/python/quokka/analysis/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Utilities functions for the analysis"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,7 +17,7 @@
import networkx as nx

import quokka
from quokka.types import Union, List
from quokka.types import Union


def split_chunk(
Expand All @@ -36,5 +37,5 @@ def split_chunk(
components = list(nx.connected_components(nx.Graph(chunk.graph)))
if len(components) <= 1:
return chunk
else:
return quokka.function.SuperChunk(chunk, components)

return quokka.function.SuperChunk(chunk, components)
1 change: 1 addition & 0 deletions bindings/python/quokka/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Backends integration"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
5 changes: 3 additions & 2 deletions bindings/python/quokka/backends/capstone.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Capstone integration"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -46,8 +47,8 @@ def get_capstone_context(arch: Type[quokka.analysis.QuokkaArch]) -> capstone.Cs:

try:
capstone_arch, capstone_mode = mapping.get(arch)
except TypeError:
raise quokka.CapstoneError("Unable to find the Architecture")
except TypeError as exc:
raise quokka.CapstoneError("Unable to find the Architecture") from exc

context = capstone.Cs(capstone_arch, capstone_mode)
context.detail = True
Expand Down
20 changes: 11 additions & 9 deletions bindings/python/quokka/backends/pypcode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""PyPCode integration"""
# Copyright 2022 Quarkslab
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -75,8 +76,10 @@ def get_pypcode_context(

try:
target_id = names[arch]
except KeyError:
raise quokka.PypcodeError("Unable to find the appropriate arch: missing id")
except KeyError as exc:
raise quokka.PypcodeError(
"Unable to find the appropriate arch: missing id"
) from exc

pcode_arch = get_arch_from_string(target_id)
return pypcode.Context(pcode_arch)
Expand Down Expand Up @@ -157,20 +160,20 @@ def combine_instructions(
while remaining_size > 0:
try:
pcode_inst: pypcode.Translation = next(translated_instructions)
except StopIteration:
except StopIteration as exc:
logger.error(
f"Disassembly discrepancy between Pypcode / IDA: missing inst"
"Disassembly discrepancy between Pypcode / IDA: missing inst"
)
raise quokka.PypcodeError(
f"Decoding error for block at 0x{block.start:x}"
)
) from exc

remaining_size -= pcode_inst.length
instruction._pcode_insts.extend(pcode_inst.ops)

if remaining_size < 0:
logger.error(
f"Disassembly discrepancy between Pypcode / IDA: sizes mismatch"
"Disassembly discrepancy between Pypcode / IDA: sizes mismatch"
)
raise quokka.PypcodeError(
f"Decoding error for block at 0x{block.start:x}"
Expand Down Expand Up @@ -287,6 +290,5 @@ def pypcode_decode_instruction(
)
return instructions

else:
logger.error(translation.error.explain)
raise quokka.PypcodeError("Unable to decode instruction")
logger.error(translation.error.explain)
raise quokka.PypcodeError("Unable to decode instruction")
1 change: 0 additions & 1 deletion bindings/python/quokka/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
MutableMapping,
ReferenceType,
Set,
Union,
)


Expand Down
Loading

0 comments on commit 3093c57

Please sign in to comment.