diff options
| author | IlyassRS <ilyassroussi2000@gmail.com> | 2026-11-25 06:48:33 +0100 |
|---|---|---|
| committer | IlyassRS <ilyassroussi2000@gmail.com> | 2026-11-25 06:48:33 +0100 |
| commit | ec6dffd32d692a7b13572ee2bc42e26978013516 (patch) | |
| tree | 7d08e4bbd0456c4c447f0aeeed69beedc7b95978 /scripts | |
| parent | 42537e7b6f84c233ac009b924d3a25d2a1178197 (diff) | |
shader_stats: reject traversal shader names, support UTF-8 BOM, and harden CLI/number guards
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/shader_stats.py | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/scripts/shader_stats.py b/scripts/shader_stats.py index 34b61cd8a..85e408099 100644 --- a/scripts/shader_stats.py +++ b/scripts/shader_stats.py | |||
| @@ -50,17 +50,27 @@ def original(path: str) -> tuple[int, int]: | |||
| 50 | if kind == "vgpr": | 50 | if kind == "vgpr": |
| 51 | top = max(top, operand["r"] + operand.get("n", 1)) | 51 | top = max(top, operand["r"] + operand.get("n", 1)) |
| 52 | elif kind == "vlist": | 52 | elif kind == "vlist": |
| 53 | top = max(top, max(operand["r"]) + 1) | 53 | if operand["r"]: |
| 54 | top = max(top, max(operand["r"]) + 1) | ||
| 54 | return len(insns), top | 55 | return len(insns), top |
| 55 | 56 | ||
| 56 | 57 | ||
| 57 | REQUIRED = ("name", "stage", "statistic", "value") | 58 | REQUIRED = ("name", "stage", "statistic", "value") |
| 58 | 59 | ||
| 59 | 60 | ||
| 61 | def _safe_shader_name(name): | ||
| 62 | """True when `name` is a single non-empty filename stem inside the disasm directory.""" | ||
| 63 | if not name or name in (".", "..") or "\x00" in name: | ||
| 64 | return False | ||
| 65 | if "/" in name or "\\" in name or os.path.splitdrive(name)[0]: | ||
| 66 | return False | ||
| 67 | return os.path.basename(name) == name | ||
| 68 | |||
| 69 | |||
| 60 | def load_stats(path): | 70 | def load_stats(path): |
| 61 | """Read counters keyed by (shader, stage) from vk_pipeline_check --stats.""" | 71 | """Read counters keyed by (shader, stage) from vk_pipeline_check --stats.""" |
| 62 | out = defaultdict(dict) | 72 | out = defaultdict(dict) |
| 63 | with open(path, newline="", encoding="utf-8") as f: | 73 | with open(path, newline="", encoding="utf-8-sig") as f: |
| 64 | reader = csv.DictReader(f, strict=True) | 74 | reader = csv.DictReader(f, strict=True) |
| 65 | header = reader.fieldnames or [] | 75 | header = reader.fieldnames or [] |
| 66 | missing = [c for c in REQUIRED if c not in header] | 76 | missing = [c for c in REQUIRED if c not in header] |
| @@ -76,12 +86,16 @@ def load_stats(path): | |||
| 76 | absent = [c for c in REQUIRED if row[c] is None] | 86 | absent = [c for c in REQUIRED if row[c] is None] |
| 77 | if absent: | 87 | if absent: |
| 78 | raise ValueError(f"line {line}: row missing required column(s): {', '.join(absent)}") | 88 | raise ValueError(f"line {line}: row missing required column(s): {', '.join(absent)}") |
| 89 | if not _safe_shader_name(row["name"]): | ||
| 90 | raise ValueError(f"line {line}: invalid shader name {row['name']!r}") | ||
| 79 | out[row["name"], row["stage"]][row["statistic"]] = row["value"] | 91 | out[row["name"], row["stage"]][row["statistic"]] = row["value"] |
| 80 | return out | 92 | return out |
| 81 | 93 | ||
| 82 | 94 | ||
| 83 | def number(raw): | 95 | def number(raw): |
| 84 | """The exact int, or else the finite float, a raw value holds; None if it is missing or not such a number.""" | 96 | """The exact int, or else the finite float, a raw value holds; None if it is missing or not such a number.""" |
| 97 | if isinstance(raw, bool): | ||
| 98 | return None | ||
| 85 | try: | 99 | try: |
| 86 | value = float(raw) | 100 | value = float(raw) |
| 87 | except (TypeError, ValueError, OverflowError): | 101 | except (TypeError, ValueError, OverflowError): |
| @@ -149,18 +163,18 @@ def median_clause(rs, key, label): | |||
| 149 | USAGE = "usage: shader_stats.py <stats.csv> <disasm dir> [--details]" | 163 | USAGE = "usage: shader_stats.py <stats.csv> <disasm dir> [--details]" |
| 150 | 164 | ||
| 151 | 165 | ||
| 152 | def main(): | 166 | def main(argv=None): |
| 153 | argv = sys.argv[1:] | 167 | argv = sys.argv[1:] if argv is None else list(argv) |
| 154 | if "-h" in argv or "--help" in argv: | 168 | if "-h" in argv or "--help" in argv: |
| 155 | print(__doc__) | 169 | print(__doc__) |
| 156 | return 0 | 170 | return 0 |
| 157 | # A misspelt option (--detail) used to be dropped, and the summary printed as if it had been asked for. | 171 | # A misspelt option (--detail, -d) used to be dropped or misparsed as a positional argument. |
| 158 | unknown = [a for a in argv if a.startswith("--") and a != "--details"] | 172 | unknown = [a for a in argv if a.startswith("-") and a != "--details"] |
| 159 | if unknown: | 173 | if unknown: |
| 160 | print(f"unknown option {unknown[0]}\n{USAGE}", file=sys.stderr) | 174 | print(f"unknown option {unknown[0]}\n{USAGE}", file=sys.stderr) |
| 161 | return 2 | 175 | return 2 |
| 162 | args = [a for a in argv if not a.startswith("--")] | 176 | args = [a for a in argv if not a.startswith("-")] |
| 163 | if len(args) != 2: | 177 | if len(args) != 2 or not args[0].strip() or not args[1].strip(): |
| 164 | print(USAGE, file=sys.stderr) | 178 | print(USAGE, file=sys.stderr) |
| 165 | return 2 | 179 | return 2 |
| 166 | path = args[0] | 180 | path = args[0] |
| @@ -169,6 +183,8 @@ def main(): | |||
| 169 | except (OSError, ValueError, csv.Error) as e: | 183 | except (OSError, ValueError, csv.Error) as e: |
| 170 | sys.exit(f"{path}: {e}") | 184 | sys.exit(f"{path}: {e}") |
| 171 | disasm = args[1] | 185 | disasm = args[1] |
| 186 | if not os.path.isdir(disasm): | ||
| 187 | sys.exit(f"{disasm}: not a directory") | ||
| 172 | rows = [] | 188 | rows = [] |
| 173 | for (name, stage), st in sorted(stats.items()): | 189 | for (name, stage), st in sorted(stats.items()): |
| 174 | src = os.path.join(disasm, name + ".s") | 190 | src = os.path.join(disasm, name + ".s") |
| @@ -179,7 +195,7 @@ def main(): | |||
| 179 | sys.exit(f"{src}: {e.strerror or e}") | 195 | sys.exit(f"{src}: {e.strerror or e}") |
| 180 | if not rows: | 196 | if not rows: |
| 181 | sys.exit("no shader is in both the statistics and the disasm folder") | 197 | sys.exit("no shader is in both the statistics and the disasm folder") |
| 182 | if "--details" in sys.argv: | 198 | if "--details" in argv: |
| 183 | print(f"{'shader':26} {'stage':5} " + " ".join(f"{head:>{width}}" for _, head, width, _ in COLUMNS)) | 199 | print(f"{'shader':26} {'stage':5} " + " ".join(f"{head:>{width}}" for _, head, width, _ in COLUMNS)) |
| 184 | for r in rows: | 200 | for r in rows: |
| 185 | print(f"{r['name']:26} {r['stage']:5} " | 201 | print(f"{r['name']:26} {r['stage']:5} " |