ocgraph 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. ocgraph/__init__.py +0 -0
  2. ocgraph/__main__.py +95 -0
  3. ocgraph/configuration/__init__.py +4 -0
  4. ocgraph/configuration/architecture/__init__.py +8 -0
  5. ocgraph/configuration/architecture/architecture.py +66 -0
  6. ocgraph/configuration/architecture/arm.py +52 -0
  7. ocgraph/configuration/architecture/msp430.py +48 -0
  8. ocgraph/configuration/architecture/ppc.py +698 -0
  9. ocgraph/configuration/architecture/sparc.py +100 -0
  10. ocgraph/configuration/architecture/x86.py +34 -0
  11. ocgraph/configuration/configuration.py +122 -0
  12. ocgraph/configuration/disassembler/__init__.py +10 -0
  13. ocgraph/configuration/disassembler/disassembler.py +148 -0
  14. ocgraph/configuration/disassembler/gdb_default.py +11 -0
  15. ocgraph/configuration/disassembler/objdump_arm.py +11 -0
  16. ocgraph/configuration/disassembler/objdump_msp430.py +27 -0
  17. ocgraph/configuration/disassembler/objdump_ppc.py +11 -0
  18. ocgraph/configuration/disassembler/objdump_sparc.py +11 -0
  19. ocgraph/configuration/disassembler/objdump_x86.py +11 -0
  20. ocgraph/configuration/logger.py +50 -0
  21. ocgraph/data/__init__.py +9 -0
  22. ocgraph/data/address.py +58 -0
  23. ocgraph/data/basic_block.py +37 -0
  24. ocgraph/data/encoding.py +34 -0
  25. ocgraph/data/function_header.py +23 -0
  26. ocgraph/data/instruction.py +102 -0
  27. ocgraph/data/jump_table.py +60 -0
  28. ocgraph/interface/__init__.py +5 -0
  29. ocgraph/interface/analyzer.py +220 -0
  30. ocgraph/interface/coverage_reader.py +70 -0
  31. ocgraph/interface/drawer.py +181 -0
  32. ocgraph-0.3.0.dist-info/LICENSES/GTDGmbH.md +24 -0
  33. ocgraph-0.3.0.dist-info/LICENSES/MauriMustonen.md +24 -0
  34. ocgraph-0.3.0.dist-info/METADATA +55 -0
  35. ocgraph-0.3.0.dist-info/RECORD +37 -0
  36. ocgraph-0.3.0.dist-info/WHEEL +4 -0
  37. ocgraph-0.3.0.dist-info/entry_points.txt +3 -0
ocgraph/__init__.py ADDED
File without changes
ocgraph/__main__.py ADDED
@@ -0,0 +1,95 @@
1
+ # SPDX-License-Identifier: GTDGmbH
2
+
3
+ """
4
+ Module entry point for the ocgraph package.
5
+
6
+ Let this module be executed from the command line with python -m ocgraph from root of the project.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ from pathlib import Path
13
+ from typing import TYPE_CHECKING
14
+
15
+ from .configuration import OcGraphConfiguration
16
+ from .interface import Analyzer, CoverageReader, Drawer
17
+
18
+ if TYPE_CHECKING: # pragma: no cover
19
+ from collections.abc import Sequence
20
+
21
+
22
+ def read_lines(file_path: str) -> list[str]:
23
+ """Read all lines from the file and return them as a list."""
24
+ with Path(file_path).open("r", encoding="utf-8-sig") as file:
25
+ return file.readlines()
26
+
27
+
28
+ def parse_args(args: Sequence[str] | None = None) -> argparse.Namespace:
29
+ """Separate argument parser for testing purposes."""
30
+ parser = argparse.ArgumentParser(
31
+ description="Assembly to Control-Flow-Graph rendering.",
32
+ )
33
+
34
+ parser.add_argument(
35
+ "-f",
36
+ "--file",
37
+ help="Disassembled object file",
38
+ required=True,
39
+ )
40
+ parser.add_argument(
41
+ "-d",
42
+ "--diss",
43
+ help="Disassembler option",
44
+ required=True,
45
+ choices=OcGraphConfiguration.disassemblers(),
46
+ )
47
+ parser.add_argument(
48
+ "-a",
49
+ "--arch",
50
+ help="Architecture option",
51
+ required=True,
52
+ choices=OcGraphConfiguration.architectures(),
53
+ )
54
+
55
+ parser.add_argument("-c", "--coverage", help="Coverage file for printing coverage")
56
+ parser.add_argument("-v", "--view", action="store_true", help="View as a dot graph")
57
+ parser.add_argument("-o", "--output", help="Target output filename")
58
+ parser.add_argument(
59
+ "-l",
60
+ "--logger",
61
+ choices=OcGraphConfiguration.loggers(),
62
+ default="default",
63
+ help="Logging mechanism preset",
64
+ )
65
+ return parser.parse_args(args)
66
+
67
+
68
+ def main(args: Sequence[str] | None = None) -> None:
69
+ """Command-line entry point to the program."""
70
+ parsed_args = parse_args(args)
71
+
72
+ # Create configuration
73
+ config = OcGraphConfiguration(
74
+ disassembler=parsed_args.diss,
75
+ arch=parsed_args.arch,
76
+ logging_preset=parsed_args.logger,
77
+ )
78
+
79
+ lines = read_lines(parsed_args.file)
80
+
81
+ analyser = Analyzer(config=config)
82
+ analyser.parse_lines(lines=lines)
83
+
84
+ if parsed_args.coverage:
85
+ cov_reader = CoverageReader(instructions=analyser.instructions, config=config)
86
+ cov_reader.update_by_csv(parsed_args.coverage)
87
+
88
+ analyser.analyze()
89
+
90
+ drawer = Drawer(analyser.configuration)
91
+ drawer.draw_cfg(
92
+ name=analyser.function_name or "",
93
+ basic_blocks=analyser.basic_blocks,
94
+ output=parsed_args.output,
95
+ )
@@ -0,0 +1,4 @@
1
+ """The main file of the configuration module."""
2
+
3
+ from .configuration import OcGraphConfiguration as OcGraphConfiguration
4
+ from .logger import OCGraphLogger as OCGraphLogger
@@ -0,0 +1,8 @@
1
+ """The main file of the architecture module."""
2
+
3
+ from .architecture import Architecture as Architecture
4
+ from .arm import ArmArchitecture as ArmArchitecture
5
+ from .msp430 import MSP430Architecture as MSP430Architecture
6
+ from .ppc import PpcArchitecture as PpcArchitecture
7
+ from .sparc import SparcArchitecture as SparcArchitecture
8
+ from .x86 import X86Architecture as X86Architecture
@@ -0,0 +1,66 @@
1
+ # SPDX-License-Identifier: GTDGmbH
2
+ """Contains all necessary functions for a TargetInfo class."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import re
7
+ from abc import ABC, abstractmethod
8
+ from typing import TYPE_CHECKING
9
+
10
+ if TYPE_CHECKING: # pragma: no cover
11
+ from ocgraph.data import Instruction
12
+
13
+ # Common regexes
14
+ HEX_PATTERN = r"[0-9a-fA-F]+"
15
+ HEX_LONG_PATTERN = r"(?:0x0*)?" + HEX_PATTERN
16
+
17
+
18
+ class Architecture(ABC):
19
+ """TargetInfo Class defining the target specific instruction set characteristics."""
20
+
21
+ @abstractmethod
22
+ def is_call(self, instruction: Instruction) -> bool:
23
+ """Return if disassembled instruction is a subroutine call."""
24
+ raise NotImplementedError # pragma:no cover
25
+
26
+ @abstractmethod
27
+ def is_unconditional_branch(self, instruction: Instruction) -> bool:
28
+ """Return if disassembled instruction is an unconditional branch."""
29
+ raise NotImplementedError # pragma:no cover
30
+
31
+ @abstractmethod
32
+ def get_branch_delay(self, instruction: Instruction) -> int:
33
+ """Return the branch delay of an instruction."""
34
+ raise NotImplementedError # pragma:no cover
35
+
36
+ @abstractmethod
37
+ def is_direct_branch(self, instruction: Instruction) -> bool:
38
+ """Return true when the given instruction is a direct branch."""
39
+ raise NotImplementedError # pragma:no cover
40
+
41
+ @abstractmethod
42
+ def is_branch(self, instruction: Instruction) -> bool:
43
+ """
44
+ Return if disassembled instruction is a branch instruction
45
+ (conditional or unconditional).
46
+ """
47
+ raise NotImplementedError # pragma:no cover
48
+
49
+ @abstractmethod
50
+ def is_sink(self, instruction: Instruction) -> bool:
51
+ """Return if disassembled instruction serves as sink (e.g. ret)."""
52
+ raise NotImplementedError # pragma:no cover
53
+
54
+
55
+ class GenericArchitecture(Architecture):
56
+ """TargetInfo Class defining the target specific instruction set characteristics."""
57
+
58
+ def get_branch_delay(self, instruction: Instruction) -> int:
59
+ """Return the branch delay of an instruction."""
60
+ return 1
61
+
62
+ def is_direct_branch(self, instruction: Instruction) -> bool:
63
+ """Return true if the given instruction is a direct branch."""
64
+ return self.is_branch(instruction) and bool(
65
+ re.search(HEX_LONG_PATTERN, "|".join(instruction.ops)),
66
+ )
@@ -0,0 +1,52 @@
1
+ # SPDX-License-Identifier: GTDGmbH
2
+ """Contains instruction info for ARM-compatible targets."""
3
+
4
+ import re
5
+
6
+ from ocgraph.data import Instruction
7
+
8
+ from .architecture import GenericArchitecture
9
+
10
+
11
+ class ArmArchitecture(GenericArchitecture):
12
+ """ArmArchitecture Class."""
13
+
14
+ def is_call(self, instruction: Instruction) -> bool:
15
+ # Various flavors of call:
16
+ # bl 0x19d90 <_IO_vtable_check>
17
+ # Note that we should be careful to not mix it with conditional
18
+ # branches like 'ble'.
19
+ if instruction.opcode:
20
+ return instruction.opcode.startswith("bl") and instruction.opcode not in (
21
+ "blt",
22
+ "ble",
23
+ "bls",
24
+ )
25
+ return False
26
+
27
+ def is_branch(self, instruction: Instruction) -> bool:
28
+ if instruction.opcode:
29
+ return instruction.opcode[0] == "b" and not self.is_call(instruction)
30
+ return False
31
+
32
+ def is_unconditional_branch(self, instruction: Instruction) -> bool:
33
+ return instruction.opcode == "b"
34
+
35
+ def is_sink(self, instruction: Instruction) -> bool:
36
+ """
37
+ Is this an instruction which terminates function execution e.g. return?
38
+ Detect various flavors of return like
39
+ bx lr
40
+ pop {r2-r6,pc}
41
+ Note that we do not consider conditional branches (e.g. 'bxle') to sink.
42
+ """
43
+ return (
44
+ bool(
45
+ re.search(
46
+ r"\bpop\b.*\bpc\b",
47
+ f"{instruction.opcode} {','.join(instruction.ops)}" or "",
48
+ ),
49
+ )
50
+ or (instruction.opcode == "bx" and instruction.ops[0] == "lr")
51
+ or instruction.opcode == "udf"
52
+ )
@@ -0,0 +1,48 @@
1
+ # SPDX-License-Identifier: GTDGmbH
2
+ """Contains instruction info for MSP430-compatible targets."""
3
+
4
+ from ocgraph.data import Instruction
5
+
6
+ from .architecture import GenericArchitecture
7
+
8
+ msp430_conditional_branches = [
9
+ "jc",
10
+ "jhs",
11
+ "jeq",
12
+ "jz",
13
+ "jge",
14
+ "jl",
15
+ "jn",
16
+ "jnc",
17
+ "jlo",
18
+ "jne",
19
+ "jnz",
20
+ ]
21
+
22
+ msp430_unconditional_branches = [
23
+ "br",
24
+ "jmp",
25
+ ]
26
+
27
+
28
+ class MSP430Architecture(GenericArchitecture):
29
+ """Msp430Architecture Class."""
30
+
31
+ def is_call(self, instruction: Instruction) -> bool:
32
+ if instruction.opcode:
33
+ return "call" in instruction.opcode
34
+ return False
35
+
36
+ def is_branch(self, instruction: Instruction) -> bool:
37
+ return instruction.opcode in (msp430_conditional_branches + msp430_unconditional_branches)
38
+
39
+ def is_direct_branch(self, instruction: Instruction) -> bool:
40
+ return self.is_branch(instruction)
41
+
42
+ def is_unconditional_branch(self, instruction: Instruction) -> bool:
43
+ return instruction.opcode in msp430_unconditional_branches
44
+
45
+ def is_sink(self, instruction: Instruction) -> bool:
46
+ if instruction.opcode:
47
+ return instruction.opcode.startswith("ret")
48
+ return False