"""nx2/discover.py's discovery in parts gives the kit's discover. Game-free: tests/test_walk.py's made-up image of random words (with jump tables planted), the parts walked here in turn (the run stand-in) or by new processes. - discover.discover over 12 parts: the same functions (bodies and labels), jump tables, misses, counters, PLT stubs and setjmp owners as a64_lift.Lifter.discover on a lifter of its own, in the same order; - the same through the real path: 2 spawned processes, each rebuilding the image from an image.bin written here (this file's module level installs what nx2/lift.py's does, so the workers, which import it again, walk as this process); - a worker whose walk is not the lift's, or whose image.bin is not the lift's size, stops (_init), and the processes' discovery then walks here, with the kit's result; - parts_of cuts only where the unwind range changes, keeps every entry once and in order; - conflicts: a range whose predecessors one part read is a conflict when another part found a table or read them there, not when only one part touched it; - a run that raises (a worker that stopped), or parts that meet in a searched range: the kit's discover, the same result, and the lift says so. It has teeth (by hand, then restored): merge dropping the parts' counters, parts_of cutting inside an unwind range, and conflicts ignoring a range read by one part where another found a table, each fail it. python -I -B tests/test_discover.py """ import collections import contextlib import importlib.util import io import os import struct import sys import tempfile import unittest from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "nx2")) import kit # noqa: E402,F401 (a64-kit on the path) import a64_lift # noqa: E402 import discover # noqa: E402 import insn # noqa: E402 import walk # noqa: E402 spec = importlib.util.spec_from_file_location("test_walk", os.path.join(ROOT, "tests", "test_walk.py")) test_walk = importlib.util.module_from_spec(spec) spec.loader.exec_module(test_walk) # (before the installs: it keeps the kit's walk) insn.install() # (at module level, as nx2/lift.py: the spawned workers import this file again and must walk the same) walk.install() def discover_chunks(): import chunks return chunks def in_turn(fn, parts, lifter): return [fn(lifter, p) for p in parts] def state(L): return (list(L.funcs.items()), list(L.jt.items()), dict(L.jt_missing), dict(L.stats), dict(L.plt), dict(getattr(L, "setjmp_owner", {}))) class DiscoverTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.img = test_walk.made_up(size=0x40000, seed=9) # (`adr x5, .+8; br x5` written in here and there: a BR to a code constant, which the kit's jump_table resolves) lo, hi, _ = cls.img.code[2] for pc in range(lo + 0x100, hi - 8, 0x400): struct.pack_into("