aboutsummaryrefslogtreecommitdiffstats
path: root/.auto/check_search.py
blob: 74ce7193b5fac55b96bd4d96c60d77e3d8e74916 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
"""Correctness gate for the search fast paths (run by .auto/checks.sh).

`SearchMixin` grew two optimisations that are invisible to the scenario suite
because they produce the *same answer* when they work:

* **prefix narrowing** — typing a character onto the term rescans only the
  previous hits, because a line holding "mov" holds "mo";
* **the joined haystack** — the whole body is concatenated once so a term is
  found with a C-level `str.find` walk instead of a python loop over every row.

Both are cache-shaped, so the way they break is *staleness*, not a crash. This
drives the real `ListingView` and `DecompView` and asserts that, for every
prefix of a set of terms, the fast path returns exactly the matches and
highlight ranges the plain per-line loop does — including after the things that
are meant to invalidate them (ending a search, toggling the opcode column,
navigating).

    ~/ida-venv/bin/python .auto/check_search.py [targets/echo]
"""
from __future__ import annotations

import asyncio
import os
import shutil
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
sys.path.insert(0, ROOT)
sys.path.insert(0, os.path.join(ROOT, "tests"))
sys.path.insert(0, HERE)

from bench import stage  # noqa: E402
from idatui._sync import wait_for  # noqa: E402
from idatui.app import DecompView, IdaTui, ListingView  # noqa: E402

TERMS = ("mov", "call", "rsp", "Mov", "1a", "push", "e", "lea", "sub_", "0",
         "  ", "if", "v1", "]")


def _reference(view, term: str):
    """(matches, ranges) from the plain per-line loop, with every cache off."""
    cls = view.__class__
    saved = cls._search_haystack
    cls._search_haystack = lambda self, c, s: None
    try:
        view._reset_search_cache(body=True)
        view._term = term
        view._ci = term.islower()
        view._compute_matches()
        return (list(view._matches),
                {k: list(v) for k, v in view._ranges.items()})
    finally:
        cls._search_haystack = saved
        view._reset_search_cache(body=True)


def _fast(view, term: str):
    view._term = term
    view._ci = term.islower()
    view._compute_matches()
    return (list(view._matches), {k: list(v) for k, v in view._ranges.items()})


def _check(view, name: str, fails: list) -> int:
    """Type every prefix of every term; compare the fast path to the loop."""
    checked = 0
    for word in TERMS:
        view.search_begin(1)
        for i in range(1, len(word) + 1):
            term = word[:i]
            got = _fast(view, term)         # may narrow from the previous term
            want = _reference(view, term)
            checked += 1
            if got != want:
                a, b = set(got[0]), set(want[0])
                fails.append(
                    f"{name} {term!r}: fast={len(got[0])} loop={len(want[0])} "
                    f"only-fast={sorted(a - b)[:4]} only-loop={sorted(b - a)[:4]}")
        view.search_cancel()
    return checked


async def main() -> int:
    target = sys.argv[1] if len(sys.argv) > 1 else "targets/echo"
    fails: list[str] = []
    d, path = stage(os.path.join(ROOT, target))
    app = IdaTui(open_path=path, keepalive=False)
    try:
        async with app.run_test(size=(140, 44)) as pilot:
            async def w(pred, t=300.0):
                return await wait_for(pred, pilot.pause, t, 0.02)

            if not await w(lambda: app._func_index is not None
                           and app._func_index.complete):
                fails.append("boot: function index never completed")
                return 1
            await w(lambda: app._loading_screen is None
                    and len(app.screen_stack) == 1, 120)
            funcs = app._func_index.all_loaded()
            lst = app.query_one(ListingView)
            fn = funcs[min(5, len(funcs) - 1)]
            app._open_function(fn.addr, fn.name)
            await w(lambda: lst.total > 0 and lst._cursor_ea() == fn.addr, 120)
            lst.model.load_all()
            lst.total = len(lst.model)

            n = _check(lst, "listing", fails)

            # The opcode-bytes column is searchable text and changes width
            # without changing the row count or the model -- the one thing the
            # haystack's key cannot see.
            app.action_toggle_view.__self__  # noqa: B018 - keep app referenced
            lst.action_toggle_opcodes()
            n += _check(lst, "listing/opcodes", fails)
            lst.action_toggle_opcodes()

            # Navigating inside the same segment must NOT invalidate the body,
            # and must not leave it stale either.
            other = funcs[min(9, len(funcs) - 1)]
            app._open_function(other.addr, other.name)
            await w(lambda: lst.total > 0
                    and lst._cursor_ea() == other.addr, 120)
            lst.model.load_all()
            lst.total = len(lst.model)
            n += _check(lst, "listing/after-nav", fails)

            # The pseudocode view uses a different line source.
            app._active = "listing"
            app._show_active()
            lst.focus()
            await pilot.pause(0.05)
            app.action_toggle_view()
            dv = app.query_one(DecompView)
            if await w(lambda: dv.total > 0, 60):
                n += _check(dv, "decomp", fails)
            app.exit()
        print(f"search fast paths: {n} prefixes checked, {len(fails)} mismatches")
        for f in fails[:10]:
            print("  FAIL", f)
        return 1 if fails else 0
    finally:
        shutil.rmtree(d, ignore_errors=True)


if __name__ == "__main__":
    sys.exit(asyncio.run(main()))