diff options
| author | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 20:39:37 +0000 |
|---|---|---|
| committer | kotorvr <332716892+kotorvr@users.noreply.github.com> | 2026-11-22 20:39:37 +0000 |
| commit | 9e41b6924b783e092e93416fb5d98dcf922f50ca (patch) | |
| tree | 82956ac69ee53a8a59804625d73c2c194de68f47 /tools/rtti_scan.py | |
| parent | 08b7920b64d6b5aad6cd99b91ef5db965114ce42 (diff) | |
rtti_scan: review fixes for conversion operators, nested function pointers, array sizes, lone '@'
- A conversion operator in a local scope is named after the type it returns
('Obj::operator int'), so two conversions of one class no longer collapse.
- A pointer or reference to a function pointer or to an array goes inside the
declarator's brackets: 'void (**)(int)', not 'void (*)(int) *'.
- An array with a negative dimension count or size is not read; the name is
returned mangled.
- A string that ends with one '@' counts as a type descriptor only when its
outermost scope is a function ('...Z@', '..._E@') or a back-reference; a
plain name followed by a lone '@' is not counted.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'tools/rtti_scan.py')
| -rw-r--r-- | tools/rtti_scan.py | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/tools/rtti_scan.py b/tools/rtti_scan.py index 876555730..5471c8340 100644 --- a/tools/rtti_scan.py +++ b/tools/rtti_scan.py | |||
| @@ -116,7 +116,10 @@ _OPERATOR = {"2": " new", "3": " delete", "4": "=", "5": ">>", "6": "<<", "7": " | |||
| 116 | "_3": "<<=", "_4": "&=", "_5": "|=", "_6": "^=", "_U": " new[]", "_V": " delete[]"} | 116 | "_3": "<<=", "_4": "&=", "_5": "|=", "_6": "^=", "_U": " new[]", "_V": " delete[]"} |
| 117 | _POINTER = {"P": " *", "Q": " * const", "R": " * volatile", "S": " * const volatile", "A": " &", "B": " & volatile"} | 117 | _POINTER = {"P": " *", "Q": " * const", "R": " * volatile", "S": " * const volatile", "A": " &", "B": " & volatile"} |
| 118 | _QUALIFIERS = re.compile(r"(?: const| volatile)*$") | 118 | _QUALIFIERS = re.compile(r"(?: const| volatile)*$") |
| 119 | _SLOT = "\0" # in a function or array type: where a further `*`, `&` or qualifier goes, `void (*|)(int)` | ||
| 119 | _LOCAL_SCOPE = re.compile(r"\?(?:\d|[B-P][A-P]*@)\?") | 120 | _LOCAL_SCOPE = re.compile(r"\?(?:\d|[B-P][A-P]*@)\?") |
| 121 | # How a type descriptor's name ends: '@@'; a local scope's function ('@?...Z@', '_E@' when noexcept); a back-reference. | ||
| 122 | _NAME_END = re.compile(rb"(?:@@|@\?[\x21-\x7e]*(?:Z|_E)@|@\d+@)\x00$") | ||
| 120 | 123 | ||
| 121 | 124 | ||
| 122 | class _TypeName: | 125 | class _TypeName: |
| @@ -202,6 +205,8 @@ class _TypeName: | |||
| 202 | outer = self.names, self.params | 205 | outer = self.names, self.params |
| 203 | self.names, self.params = [], [] | 206 | self.names, self.params = [], [] |
| 204 | name = self.operator() if self.peek() == "?" else self.simple() | 207 | name = self.operator() if self.peek() == "?" else self.simple() |
| 208 | if name == "operator": | ||
| 209 | raise ValueError("a conversion operator template is not read") | ||
| 205 | arguments = [] | 210 | arguments = [] |
| 206 | while not self.eat("@"): | 211 | while not self.eat("@"): |
| 207 | if self.eat("$$$V") or self.eat("$$V") or self.eat("$$Z") or self.eat("$S"): | 212 | if self.eat("$$$V") or self.eat("$$V") or self.eat("$$Z") or self.eat("$S"): |
| @@ -216,7 +221,7 @@ class _TypeName: | |||
| 216 | elif self.eat("$$Y"): | 221 | elif self.eat("$$Y"): |
| 217 | arguments.append(self.qualified()) | 222 | arguments.append(self.qualified()) |
| 218 | else: | 223 | else: |
| 219 | arguments.append(self.type()) | 224 | arguments.append(self.type().replace(_SLOT, "")) |
| 220 | self.names, self.params = outer | 225 | self.names, self.params = outer |
| 221 | return f"{name}<{', '.join(arguments)}>" | 226 | return f"{name}<{', '.join(arguments)}>" |
| 222 | 227 | ||
| @@ -260,7 +265,9 @@ class _TypeName: | |||
| 260 | self.cv() | 265 | self.cv() |
| 261 | elif kind not in "CDKLSTYZ": | 266 | elif kind not in "CDKLSTYZ": |
| 262 | raise ValueError("not a plain function") | 267 | raise ValueError("not a plain function") |
| 263 | self.signature() | 268 | result = self.signature()[0] |
| 269 | if name == "operator": # a conversion: named after the type it returns | ||
| 270 | name += " " + result.replace(_SLOT, "") | ||
| 264 | return "::".join(scope[::-1] + [name]) | 271 | return "::".join(scope[::-1] + [name]) |
| 265 | 272 | ||
| 266 | def cv(self): | 273 | def cv(self): |
| @@ -268,14 +275,28 @@ class _TypeName: | |||
| 268 | 275 | ||
| 269 | def sizes(self): | 276 | def sizes(self): |
| 270 | """An array's dimensions: how many, then each.""" | 277 | """An array's dimensions: how many, then each.""" |
| 271 | return "".join(f"[{self.number()}]" for _ in range(self.number())) | 278 | count = self.number() |
| 279 | sizes = [self.number() for _ in range(count)] | ||
| 280 | if count < 1 or min(sizes) < 0: | ||
| 281 | raise ValueError("bad array dimensions") | ||
| 282 | return "".join(f"[{n}]" for n in sizes) | ||
| 283 | |||
| 284 | @staticmethod | ||
| 285 | def declare(text, declarator, keep=False): | ||
| 286 | """The type `text` with `declarator` (` *`, ` (*)(int)`, ` [4]`, ...) applied. It goes after a plain type, | ||
| 287 | and inside the brackets of a function or array type: a pointer to `void (*)(int)` is `void (**)(int)`. | ||
| 288 | `keep` leaves the slot after a `*` or `&`; a function or array declarator brings its own.""" | ||
| 289 | if _SLOT in text: | ||
| 290 | return text.replace(_SLOT, declarator.lstrip() + (_SLOT if keep else "")) | ||
| 291 | return text + declarator | ||
| 272 | 292 | ||
| 273 | @staticmethod | 293 | @staticmethod |
| 274 | def qualify(text, qualifiers): | 294 | def qualify(text, qualifiers): |
| 275 | """`text` with `const`/`volatile` after it. A const pointer is marked twice, by its own code and by the | 295 | """`text` with `const`/`volatile` after it. A const pointer is marked twice, by its own code and by the |
| 276 | qualifiers of whatever refers to it, so one that is already there is not repeated.""" | 296 | qualifiers of whatever refers to it, so one that is already there is not repeated.""" |
| 277 | present = _QUALIFIERS.search(text).group(0).split() | 297 | head, slot, tail = text.partition(_SLOT) |
| 278 | return text + "".join(f" {q}" for q in qualifiers if q not in present) | 298 | present = _QUALIFIERS.search(head).group(0).split() |
| 299 | return head + "".join(f" {q}" for q in qualifiers if q not in present) + slot + tail | ||
| 279 | 300 | ||
| 280 | def signature(self): | 301 | def signature(self): |
| 281 | """Calling convention, return type, parameters, exception specification: (return type, parameters, | 302 | """Calling convention, return type, parameters, exception specification: (return type, parameters, |
| @@ -295,7 +316,7 @@ class _TypeName: | |||
| 295 | parameters.append(self.params[int(self.next())]) | 316 | parameters.append(self.params[int(self.next())]) |
| 296 | continue | 317 | continue |
| 297 | start = self.i | 318 | start = self.i |
| 298 | parameters.append(self.type()) | 319 | parameters.append(self.type().replace(_SLOT, "")) |
| 299 | if self.i - start > 1 and len(self.params) < 10: | 320 | if self.i - start > 1 and len(self.params) < 10: |
| 300 | self.params.append(parameters[-1]) | 321 | self.params.append(parameters[-1]) |
| 301 | if self.eat("_E"): | 322 | if self.eat("_E"): |
| @@ -306,7 +327,7 @@ class _TypeName: | |||
| 306 | 327 | ||
| 307 | def function(self, declarator): | 328 | def function(self, declarator): |
| 308 | result, parameters, noexcept = self.signature() | 329 | result, parameters, noexcept = self.signature() |
| 309 | return f"{result} {declarator}({', '.join(parameters)}){noexcept}" | 330 | return self.declare(result, f" {declarator}({', '.join(parameters)}){noexcept}") |
| 310 | 331 | ||
| 311 | def indirect(self, suffix): | 332 | def indirect(self, suffix): |
| 312 | """What a pointer or reference refers to, with `suffix` (` *`, ` &`, ...) after it.""" | 333 | """What a pointer or reference refers to, with `suffix` (` *`, ` &`, ...) after it.""" |
| @@ -315,14 +336,14 @@ class _TypeName: | |||
| 315 | if self.next() == "F": | 336 | if self.next() == "F": |
| 316 | unaligned = ("__unaligned",) | 337 | unaligned = ("__unaligned",) |
| 317 | if self.eat("6"): | 338 | if self.eat("6"): |
| 318 | return self.function(f"({suffix.strip()})") | 339 | return self.function(f"({suffix.strip()}{_SLOT})") |
| 319 | if self.eat("$A"): # a C++/CX handle | 340 | if self.eat("$A"): # a C++/CX handle |
| 320 | suffix = suffix.replace("*", "^") | 341 | suffix = suffix.replace("*", "^") |
| 321 | qualifiers = self.cv() | 342 | qualifiers = self.cv() |
| 322 | if self.eat("Y"): | 343 | if self.eat("Y"): |
| 323 | sizes = self.sizes() | 344 | sizes = self.sizes() |
| 324 | return f"{self.qualify(self.type(), qualifiers + unaligned)} ({suffix.strip()}){sizes}" | 345 | return self.declare(self.qualify(self.type(), qualifiers + unaligned), f" ({suffix.strip()}{_SLOT}){sizes}") |
| 325 | return self.qualify(self.type(), qualifiers + unaligned) + suffix | 346 | return self.declare(self.qualify(self.type(), qualifiers + unaligned), suffix, keep=True) |
| 326 | 347 | ||
| 327 | def type(self): | 348 | def type(self): |
| 328 | if self.eat("$$Q"): | 349 | if self.eat("$$Q"): |
| @@ -336,7 +357,7 @@ class _TypeName: | |||
| 336 | return self.qualify(self.type(), qualifiers) | 357 | return self.qualify(self.type(), qualifiers) |
| 337 | if self.eat("$$BY"): # an array passed as a template argument | 358 | if self.eat("$$BY"): # an array passed as a template argument |
| 338 | sizes = self.sizes() | 359 | sizes = self.sizes() |
| 339 | return f"{self.type()} {sizes}" | 360 | return self.declare(self.type(), " " + sizes) |
| 340 | c = self.next() | 361 | c = self.next() |
| 341 | if c == "_": | 362 | if c == "_": |
| 342 | c += self.next() | 363 | c += self.next() |
| @@ -365,7 +386,7 @@ def simple_name(mangled: str) -> str: | |||
| 365 | name = reader.qualified() | 386 | name = reader.qualified() |
| 366 | except (ValueError, IndexError, KeyError, RecursionError): | 387 | except (ValueError, IndexError, KeyError, RecursionError): |
| 367 | return mangled | 388 | return mangled |
| 368 | return name if reader.i == len(reader.s) else mangled | 389 | return name.replace(_SLOT, "") if reader.i == len(reader.s) else mangled |
| 369 | 390 | ||
| 370 | 391 | ||
| 371 | def scan_pe64_rtti(data: bytes) -> dict: | 392 | def scan_pe64_rtti(data: bytes) -> dict: |
| @@ -384,10 +405,13 @@ def scan_pe64_rtti(data: bytes) -> dict: | |||
| 384 | return struct.unpack_from("<I", img, rva)[0] | 405 | return struct.unpack_from("<I", img, rva)[0] |
| 385 | 406 | ||
| 386 | # 1. Type descriptors: '.?AV' / '.?AU' / '.?AW' / '.?AT' strings, 16 bytes after the descriptor start. A name | 407 | # 1. Type descriptors: '.?AV' / '.?AU' / '.?AW' / '.?AT' strings, 16 bytes after the descriptor start. A name |
| 387 | # ends with '@': '@@' for most, but a class local to a function ends with that function's 'Z@'. | 408 | # ends with '@@', except where its outermost scope is a function (a local class: '...@?1??f@@YAXXZ@') or a |
| 409 | # back-reference ('...@1@'); any other text that ends with one '@' is not counted. | ||
| 388 | tds = {} | 410 | tds = {} |
| 389 | for m in re.finditer(rb"\.\?A[TUVW][\x21-\x7e]{1,2000}?@\x00", img): | 411 | for m in re.finditer(rb"\.\?A[TUVW][\x21-\x7e]{1,2000}?@\x00", img): |
| 390 | rva = m.start() - 16 | 412 | rva = m.start() - 16 |
| 413 | if not _NAME_END.search(m.group(0)): | ||
| 414 | continue | ||
| 391 | if rva >= 0: | 415 | if rva >= 0: |
| 392 | tds[rva] = m.group(0)[:-1].decode("latin-1") | 416 | tds[rva] = m.group(0)[:-1].decode("latin-1") |
| 393 | 417 | ||