summaryrefslogtreecommitdiff
path: root/build-aux/measurestack/util.py
blob: c94ce07b231343d9ada9a79f3e2a9892ef060a48 (plain)
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
# build-aux/measurestack/util.py - Analyze stack sizes for compiled objects
#
# Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

import re
import typing

from . import analyze, vcg
from .analyze import BaseName, Node, QName, maybe_sorted

# pylint: disable=unused-variable
__all__ = [
    "synthetic_node",
    "read_source",
    "get_zero_or_one",
    "re_call_other",
    "Plugin",
    "PluginApplication",
]


def synthetic_node(
    name: str, nstatic: int, calls: typing.Collection[str] = frozenset()
) -> Node:
    n = Node()

    n.funcname = QName(name)

    n.location = "<synthetic>"
    n.usage_kind = "static"
    n.nstatic = nstatic
    n.ndynamic = 0

    n.calls = dict((QName(c), False) for c in maybe_sorted(calls))

    return n


re_location = re.compile(r"(?P<filename>.+):(?P<row>[0-9]+):(?P<col>[0-9]+)")


def read_source(location: str) -> str:
    m = re_location.fullmatch(location)
    if not m:
        raise ValueError(f"unexpected label value {location!r}")
    filename = m.group("filename")
    row = int(m.group("row")) - 1
    # col = int(m.group("col")) - 1
    with open(filename, "r", encoding="utf-8") as fh:
        return fh.readlines()[row].strip()


def get_zero_or_one(
    pred: typing.Callable[[str], bool], fnames: typing.Collection[str]
) -> str | None:
    count = sum(1 for fname in fnames if pred(fname))
    assert count < 2
    if count:
        return next(fname for fname in fnames if pred(fname))
    return None


re_call_other = re.compile(r".*?\b(?P<func>(?!if\b)[->.a-zA-Z0-9_]+)\(.*")


class Plugin(typing.Protocol):
    def is_intrhandler(self, name: QName) -> bool: ...

    # init_array returns a list of functions that are placed in the
    # `.init_array.*` section; AKA functions marked with
    # `__attribute__((constructor))`.
    def init_array(self) -> typing.Collection[QName]: ...

    # extra_includes returns a list of functions that are never
    # called, but are included in the binary anyway.  This may because
    # it is an unused method in a used vtable.  This may be because it
    # is an atexit() callback (we never exit).
    def extra_includes(self) -> typing.Collection[BaseName]: ...

    def extra_nodes(self) -> typing.Collection[Node]: ...
    def mutate_node(self, node: Node) -> None: ...
    def indirect_callees(
        self, loc: str, line: str
    ) -> tuple[typing.Collection[QName], bool] | None: ...
    def skipmodels(self) -> dict[BaseName, analyze.SkipModel]: ...


class PluginApplication:
    _location_xform: typing.Callable[[str], str]
    _plugins: list[Plugin]

    def __init__(
        self, location_xform: typing.Callable[[str], str], plugins: list[Plugin]
    ) -> None:
        self._location_xform = location_xform
        self._plugins = plugins

    def extra_nodes(self) -> typing.Collection[Node]:
        ret: list[Node] = []
        for plugin in self._plugins:
            ret.extend(plugin.extra_nodes())
        return ret

    def mutate_node(self, node: Node) -> None:
        for plugin in self._plugins:
            plugin.mutate_node(node)

    def indirect_callees(
        self, elem: vcg.VCGElem
    ) -> tuple[typing.Collection[QName], bool]:
        loc = elem.attrs.get("label", "")
        line = read_source(loc)

        for plugin in self._plugins:
            ret = plugin.indirect_callees(loc, line)
            if ret is not None:
                assert (
                    len(ret[0]) > 0
                ), f"{plugin.__class__.__name__} returning 0 calles for {loc} indicates the code would crash"
                return ret

        placeholder = "__indirect_call"
        if m := re_call_other.fullmatch(line):
            placeholder += ":" + m.group("func")
        placeholder += " at " + self._location_xform(elem.attrs.get("label", ""))
        return [QName(placeholder)], False

    def skipmodels(self) -> dict[BaseName, analyze.SkipModel]:
        ret: dict[BaseName, analyze.SkipModel] = {}
        for plugin in self._plugins:
            ret.update(plugin.skipmodels())
        return ret