ocgraph 0.3.0__tar.gz

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 (36) hide show
  1. ocgraph-0.3.0/LICENSES/GTDGmbH.md +24 -0
  2. ocgraph-0.3.0/LICENSES/MauriMustonen.md +24 -0
  3. ocgraph-0.3.0/PKG-INFO +55 -0
  4. ocgraph-0.3.0/README.md +28 -0
  5. ocgraph-0.3.0/ocgraph/__init__.py +0 -0
  6. ocgraph-0.3.0/ocgraph/__main__.py +95 -0
  7. ocgraph-0.3.0/ocgraph/configuration/__init__.py +4 -0
  8. ocgraph-0.3.0/ocgraph/configuration/architecture/__init__.py +8 -0
  9. ocgraph-0.3.0/ocgraph/configuration/architecture/architecture.py +66 -0
  10. ocgraph-0.3.0/ocgraph/configuration/architecture/arm.py +52 -0
  11. ocgraph-0.3.0/ocgraph/configuration/architecture/msp430.py +48 -0
  12. ocgraph-0.3.0/ocgraph/configuration/architecture/ppc.py +698 -0
  13. ocgraph-0.3.0/ocgraph/configuration/architecture/sparc.py +100 -0
  14. ocgraph-0.3.0/ocgraph/configuration/architecture/x86.py +34 -0
  15. ocgraph-0.3.0/ocgraph/configuration/configuration.py +122 -0
  16. ocgraph-0.3.0/ocgraph/configuration/disassembler/__init__.py +10 -0
  17. ocgraph-0.3.0/ocgraph/configuration/disassembler/disassembler.py +148 -0
  18. ocgraph-0.3.0/ocgraph/configuration/disassembler/gdb_default.py +11 -0
  19. ocgraph-0.3.0/ocgraph/configuration/disassembler/objdump_arm.py +11 -0
  20. ocgraph-0.3.0/ocgraph/configuration/disassembler/objdump_msp430.py +27 -0
  21. ocgraph-0.3.0/ocgraph/configuration/disassembler/objdump_ppc.py +11 -0
  22. ocgraph-0.3.0/ocgraph/configuration/disassembler/objdump_sparc.py +11 -0
  23. ocgraph-0.3.0/ocgraph/configuration/disassembler/objdump_x86.py +11 -0
  24. ocgraph-0.3.0/ocgraph/configuration/logger.py +50 -0
  25. ocgraph-0.3.0/ocgraph/data/__init__.py +9 -0
  26. ocgraph-0.3.0/ocgraph/data/address.py +58 -0
  27. ocgraph-0.3.0/ocgraph/data/basic_block.py +37 -0
  28. ocgraph-0.3.0/ocgraph/data/encoding.py +34 -0
  29. ocgraph-0.3.0/ocgraph/data/function_header.py +23 -0
  30. ocgraph-0.3.0/ocgraph/data/instruction.py +102 -0
  31. ocgraph-0.3.0/ocgraph/data/jump_table.py +60 -0
  32. ocgraph-0.3.0/ocgraph/interface/__init__.py +5 -0
  33. ocgraph-0.3.0/ocgraph/interface/analyzer.py +220 -0
  34. ocgraph-0.3.0/ocgraph/interface/coverage_reader.py +70 -0
  35. ocgraph-0.3.0/ocgraph/interface/drawer.py +181 -0
  36. ocgraph-0.3.0/pyproject.toml +104 -0
@@ -0,0 +1,24 @@
1
+ Valid-License-Identifier: GTDGmbH
2
+ License-Text:
3
+
4
+ Copyright (c) 2023 GTD GmbH. All rights reserved.
5
+
6
+ MIT License
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
9
+ this software and associated documentation files (the “Software”), to deal in
10
+ the Software without restriction, including without limitation the rights to
11
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
+ of the Software, and to permit persons to whom the Software is furnished to do
13
+ so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
@@ -0,0 +1,24 @@
1
+ Valid-License-Identifier: MauriMustonen
2
+ License-Text:
3
+
4
+ MIT License
5
+
6
+ Copyright (c) 2021 Mauri Mustonen
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
ocgraph-0.3.0/PKG-INFO ADDED
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.3
2
+ Name: ocgraph
3
+ Version: 0.3.0
4
+ Summary: Tool to visualize object code as control flow graph
5
+ License: MIT
6
+ Keywords: Object Code,Control Flow Graph,ECSS Category A,asm2cfg,Coverage
7
+ Author: Mauri Mustonen
8
+ Author-email: kazooiebombchu@gmail.com
9
+ Maintainer: Christoph Weiss
10
+ Maintainer-email: christoph.weiss@gtd-gmbh.de
11
+ Requires-Python: >=3.9,<4.0
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Requires-Dist: graphviz (>=0.9,<0.10)
21
+ Project-URL: Documentation, https://gtd-gmbh.gitlab.io/beta-software/ocgraph
22
+ Project-URL: Forked from asm2cfg, https://github.com/Kazhuu/asm2cfg
23
+ Project-URL: Homepage, https://gtd-gmbh.de/cat-a
24
+ Project-URL: Repository, https://gitlab.com/gtd-gmbh/beta-software/ocgraph.git
25
+ Description-Content-Type: text/markdown
26
+
27
+ # Object Code Graph (ocgraph)
28
+
29
+ This repo contains *ocgraph*, (**Object Code Graph**), a tool to
30
+ read disassembler output and optionally corresponding coverage data to produce
31
+ control flow graphs including coverage analysis on assembler level.
32
+
33
+ The *ocgraph* source code implemented by GTD GmbH is forked from the asm2cfg
34
+ repository on [Github](https://github.com/Kazhuu/asm2cfg) and reworked to
35
+ function with several architectures and disassemblers.
36
+
37
+ The tool source code is subject to the MIT License as indicated by the headers
38
+ of the corresponding source code files.
39
+
40
+ [!WARNING]
41
+ > **WARNING**:
42
+ >
43
+ > **This tool is currently under development and in beta state It's not intended
44
+ > to be complete and using it is on your own risk.**
45
+
46
+ ## Documentation
47
+
48
+ Due to the lack of a full documentation, all previous information can be found
49
+ in separate *Markdown* files in the *doc* folder:
50
+
51
+ 1. [Installation](doc/1_Installation.md): Instructions to install the tool
52
+ 2. [How to Run](doc/2_HowToRun.md): Guideline to run the tool
53
+ 3. [Development](doc/3_Development.md): Information for Developer
54
+ 4. [Github asm2cfg](doc/4_Github.md): Original GitHub documentation
55
+
@@ -0,0 +1,28 @@
1
+ # Object Code Graph (ocgraph)
2
+
3
+ This repo contains *ocgraph*, (**Object Code Graph**), a tool to
4
+ read disassembler output and optionally corresponding coverage data to produce
5
+ control flow graphs including coverage analysis on assembler level.
6
+
7
+ The *ocgraph* source code implemented by GTD GmbH is forked from the asm2cfg
8
+ repository on [Github](https://github.com/Kazhuu/asm2cfg) and reworked to
9
+ function with several architectures and disassemblers.
10
+
11
+ The tool source code is subject to the MIT License as indicated by the headers
12
+ of the corresponding source code files.
13
+
14
+ [!WARNING]
15
+ > **WARNING**:
16
+ >
17
+ > **This tool is currently under development and in beta state It's not intended
18
+ > to be complete and using it is on your own risk.**
19
+
20
+ ## Documentation
21
+
22
+ Due to the lack of a full documentation, all previous information can be found
23
+ in separate *Markdown* files in the *doc* folder:
24
+
25
+ 1. [Installation](doc/1_Installation.md): Instructions to install the tool
26
+ 2. [How to Run](doc/2_HowToRun.md): Guideline to run the tool
27
+ 3. [Development](doc/3_Development.md): Information for Developer
28
+ 4. [Github asm2cfg](doc/4_Github.md): Original GitHub documentation
File without changes
@@ -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