summaryrefslogtreecommitdiffstats
path: root/build/clang-plugin/ThreadAllows.py
blob: f3e1ee894c8ea83962e9e416c58a348722c65c89 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os
import posixpath
from os import PathLike

# `typing.Literal` not available until Python 3.8;
# `typing_extensions` not generally available here
from typing import Iterable, Set

FIRST_LINE = "// This file was generated by {}. DO NOT EDIT.".format(
    # `posixpath` for forward slashes, for presentation purposes
    posixpath.relpath(__file__, os.getenv("TOPSRCDIR", "/"))
)


def generate_allowed_items(
    which: str,  # should be: Literal["files", "names"],
    paths: Iterable[PathLike],
) -> str:
    def remove_trailing_comment(s: str) -> str:
        return s[0 : s.find("#")]

    def read_items_from_path(path: PathLike) -> Set[str]:
        out = set()
        with open(path) as file:
            for line in file.readlines():
                line = remove_trailing_comment(line).strip()
                if not line:
                    continue  # comment or empty line; discard
                out.add(line)
        return out

    allowed = set().union(*(read_items_from_path(path) for path in paths))
    # BUG: `json.dumps` may not correctly handle use of the quote character in
    #   thread names
    allowed_list_s = ",\n  ".join(json.dumps(elem) for elem in sorted(allowed))

    return f"""\
static const char *allow_thread_{which}[] = {{
  {allowed_list_s}
}};"""


def generate_allows(
    *, allowed_names: Iterable[PathLike], allowed_files: Iterable[PathLike]
) -> str:
    """
    This function reads in the specified sets of files -- ordinarily,
    ["ThreadAllows.txt"] and ["ThreadFileAllows.txt"] -- and generates the text
    of a header file containing two arrays with their contents, for inclusion by
    the thread-name checker.

    The checker will reject the creation of any thread via NS_NewNamedThread
    unless either:
      - the thread's name is a literal string which is found in the set of
        allowed thread names; or
      - the thread's creation occurs within a file which is found in the set of
        unchecked files.

    The latter condition exists mostly for the definition of NS_NewNamedThread,
    but there also exist a few cases where the thread name is dynamically
    computed (and so can't be checked).
    """
    output_string = (
        FIRST_LINE
        + "\n\n"
        + generate_allowed_items("files", allowed_files)
        + "\n\n"
        + generate_allowed_items("names", allowed_names)
        + "\n"
    )
    return output_string


# Entry point used by build/clang-plugin/moz.build (q.v.).
def generate_file(output, allowed_names, allowed_files):
    output.write(
        generate_allows(allowed_names=[allowed_names], allowed_files=[allowed_files])
    )